aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Chinner <dchinner@redhat.com>2013-08-12 06:49:42 -0400
committerBen Myers <bpm@sgi.com>2013-08-12 17:45:17 -0400
commit6898811459ff523ed256c07d1d5c54ba915d51ac (patch)
tree3079fecb7b8420aef10673d4d6b08d69645feacc
parentff55068c2010f39bcd899ca14b0a3d401d14da54 (diff)
xfs: create xfs_bmap_util.[ch]
There is a bunch of code in xfs_bmap.c that is kernel specific and not shared with userspace. To minimise the difference between the kernel and userspace code, shift this unshared code to xfs_bmap_util.c, and the declarations to xfs_bmap_util.h. The biggest issue here is xfs_bmap_finish() - userspace has it's own definition of this function, and so we need to move it out of xfs_bmap.[ch]. This means several other files need to include xfs_bmap_util.h as well. It also introduces and interesting dance for the stack switching code in xfs_bmapi_allocate(). The stack switching/workqueue code is actually moved to xfs_bmap_util.c, so that userspace can simply use a #define in a header file to connect the dots without needing to know about the stack switch code at all. Signed-off-by: Dave Chinner <dchinner@redhat.com> Reviewed-by: Mark Tinguely <tinguely@sgi.com> Signed-off-by: Ben Myers <bpm@sgi.com>
-rw-r--r--fs/xfs/Makefile1
-rw-r--r--fs/xfs/xfs_aops.c1
-rw-r--r--fs/xfs/xfs_attr.c1
-rw-r--r--fs/xfs/xfs_attr_remote.c1
-rw-r--r--fs/xfs/xfs_bmap.c804
-rw-r--r--fs/xfs/xfs_bmap.h56
-rw-r--r--fs/xfs/xfs_bmap_util.c834
-rw-r--r--fs/xfs/xfs_bmap_util.h96
-rw-r--r--fs/xfs/xfs_dfrag.c1
-rw-r--r--fs/xfs/xfs_dquot.c1
-rw-r--r--fs/xfs/xfs_filestream.c5
-rw-r--r--fs/xfs/xfs_inode.c1
-rw-r--r--fs/xfs/xfs_ioctl.c1
-rw-r--r--fs/xfs/xfs_iomap.c1
-rw-r--r--fs/xfs/xfs_iops.c1
-rw-r--r--fs/xfs/xfs_rename.c1
-rw-r--r--fs/xfs/xfs_rtalloc.c1
-rw-r--r--fs/xfs/xfs_symlink.c1
-rw-r--r--fs/xfs/xfs_trans_resv.c1
-rw-r--r--fs/xfs/xfs_vnodeops.c1
20 files changed, 957 insertions, 853 deletions
diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile
index fb14461d414e..dcd21b95cd98 100644
--- a/fs/xfs/Makefile
+++ b/fs/xfs/Makefile
@@ -30,6 +30,7 @@ xfs-y += xfs_aops.o \
30 xfs_attr_inactive.o \ 30 xfs_attr_inactive.o \
31 xfs_attr_list.o \ 31 xfs_attr_list.o \
32 xfs_bit.o \ 32 xfs_bit.o \
33 xfs_bmap_util.o \
33 xfs_buf.o \ 34 xfs_buf.o \
34 xfs_dfrag.o \ 35 xfs_dfrag.o \
35 xfs_dir2_readdir.o \ 36 xfs_dir2_readdir.o \
diff --git a/fs/xfs/xfs_aops.c b/fs/xfs/xfs_aops.c
index 79670869d436..709347d6f468 100644
--- a/fs/xfs/xfs_aops.c
+++ b/fs/xfs/xfs_aops.c
@@ -31,6 +31,7 @@
31#include "xfs_vnodeops.h" 31#include "xfs_vnodeops.h"
32#include "xfs_trace.h" 32#include "xfs_trace.h"
33#include "xfs_bmap.h" 33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
34#include <linux/aio.h> 35#include <linux/aio.h>
35#include <linux/gfp.h> 36#include <linux/gfp.h>
36#include <linux/mpage.h> 37#include <linux/mpage.h>
diff --git a/fs/xfs/xfs_attr.c b/fs/xfs/xfs_attr.c
index 6ab77e356a9d..91acc38f664b 100644
--- a/fs/xfs/xfs_attr.c
+++ b/fs/xfs/xfs_attr.c
@@ -33,6 +33,7 @@
33#include "xfs_alloc.h" 33#include "xfs_alloc.h"
34#include "xfs_inode_item.h" 34#include "xfs_inode_item.h"
35#include "xfs_bmap.h" 35#include "xfs_bmap.h"
36#include "xfs_bmap_util.h"
36#include "xfs_attr.h" 37#include "xfs_attr.h"
37#include "xfs_attr_leaf.h" 38#include "xfs_attr_leaf.h"
38#include "xfs_attr_remote.h" 39#include "xfs_attr_remote.h"
diff --git a/fs/xfs/xfs_attr_remote.c b/fs/xfs/xfs_attr_remote.c
index 39a59ea1fba8..13a0ed9cb40a 100644
--- a/fs/xfs/xfs_attr_remote.c
+++ b/fs/xfs/xfs_attr_remote.c
@@ -34,6 +34,7 @@
34#include "xfs_alloc.h" 34#include "xfs_alloc.h"
35#include "xfs_inode_item.h" 35#include "xfs_inode_item.h"
36#include "xfs_bmap.h" 36#include "xfs_bmap.h"
37#include "xfs_bmap_util.h"
37#include "xfs_attr.h" 38#include "xfs_attr.h"
38#include "xfs_attr_leaf.h" 39#include "xfs_attr_leaf.h"
39#include "xfs_attr_remote.h" 40#include "xfs_attr_remote.h"
diff --git a/fs/xfs/xfs_bmap.c b/fs/xfs/xfs_bmap.c
index 72a2eea597e9..1f09fafa07fa 100644
--- a/fs/xfs/xfs_bmap.c
+++ b/fs/xfs/xfs_bmap.c
@@ -40,6 +40,7 @@
40#include "xfs_extfree_item.h" 40#include "xfs_extfree_item.h"
41#include "xfs_alloc.h" 41#include "xfs_alloc.h"
42#include "xfs_bmap.h" 42#include "xfs_bmap.h"
43#include "xfs_bmap_util.h"
43#include "xfs_rtalloc.h" 44#include "xfs_rtalloc.h"
44#include "xfs_error.h" 45#include "xfs_error.h"
45#include "xfs_attr_leaf.h" 46#include "xfs_attr_leaf.h"
@@ -109,19 +110,6 @@ xfs_bmap_compute_maxlevels(
109 mp->m_bm_maxlevels[whichfork] = level; 110 mp->m_bm_maxlevels[whichfork] = level;
110} 111}
111 112
112/*
113 * Convert the given file system block to a disk block. We have to treat it
114 * differently based on whether the file is a real time file or not, because the
115 * bmap code does.
116 */
117xfs_daddr_t
118xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
119{
120 return (XFS_IS_REALTIME_INODE(ip) ? \
121 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
122 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
123}
124
125STATIC int /* error */ 113STATIC int /* error */
126xfs_bmbt_lookup_eq( 114xfs_bmbt_lookup_eq(
127 struct xfs_btree_cur *cur, 115 struct xfs_btree_cur *cur,
@@ -264,173 +252,6 @@ xfs_bmap_forkoff_reset(
264} 252}
265 253
266/* 254/*
267 * Extent tree block counting routines.
268 */
269
270/*
271 * Count leaf blocks given a range of extent records.
272 */
273STATIC void
274xfs_bmap_count_leaves(
275 xfs_ifork_t *ifp,
276 xfs_extnum_t idx,
277 int numrecs,
278 int *count)
279{
280 int b;
281
282 for (b = 0; b < numrecs; b++) {
283 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
284 *count += xfs_bmbt_get_blockcount(frp);
285 }
286}
287
288/*
289 * Count leaf blocks given a range of extent records originally
290 * in btree format.
291 */
292STATIC void
293xfs_bmap_disk_count_leaves(
294 struct xfs_mount *mp,
295 struct xfs_btree_block *block,
296 int numrecs,
297 int *count)
298{
299 int b;
300 xfs_bmbt_rec_t *frp;
301
302 for (b = 1; b <= numrecs; b++) {
303 frp = XFS_BMBT_REC_ADDR(mp, block, b);
304 *count += xfs_bmbt_disk_get_blockcount(frp);
305 }
306}
307
308/*
309 * Recursively walks each level of a btree
310 * to count total fsblocks is use.
311 */
312STATIC int /* error */
313xfs_bmap_count_tree(
314 xfs_mount_t *mp, /* file system mount point */
315 xfs_trans_t *tp, /* transaction pointer */
316 xfs_ifork_t *ifp, /* inode fork pointer */
317 xfs_fsblock_t blockno, /* file system block number */
318 int levelin, /* level in btree */
319 int *count) /* Count of blocks */
320{
321 int error;
322 xfs_buf_t *bp, *nbp;
323 int level = levelin;
324 __be64 *pp;
325 xfs_fsblock_t bno = blockno;
326 xfs_fsblock_t nextbno;
327 struct xfs_btree_block *block, *nextblock;
328 int numrecs;
329
330 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
331 &xfs_bmbt_buf_ops);
332 if (error)
333 return error;
334 *count += 1;
335 block = XFS_BUF_TO_BLOCK(bp);
336
337 if (--level) {
338 /* Not at node above leaves, count this level of nodes */
339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
340 while (nextbno != NULLFSBLOCK) {
341 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
342 XFS_BMAP_BTREE_REF,
343 &xfs_bmbt_buf_ops);
344 if (error)
345 return error;
346 *count += 1;
347 nextblock = XFS_BUF_TO_BLOCK(nbp);
348 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
349 xfs_trans_brelse(tp, nbp);
350 }
351
352 /* Dive to the next level */
353 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
354 bno = be64_to_cpu(*pp);
355 if (unlikely((error =
356 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
357 xfs_trans_brelse(tp, bp);
358 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
359 XFS_ERRLEVEL_LOW, mp);
360 return XFS_ERROR(EFSCORRUPTED);
361 }
362 xfs_trans_brelse(tp, bp);
363 } else {
364 /* count all level 1 nodes and their leaves */
365 for (;;) {
366 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
367 numrecs = be16_to_cpu(block->bb_numrecs);
368 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
369 xfs_trans_brelse(tp, bp);
370 if (nextbno == NULLFSBLOCK)
371 break;
372 bno = nextbno;
373 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
374 XFS_BMAP_BTREE_REF,
375 &xfs_bmbt_buf_ops);
376 if (error)
377 return error;
378 *count += 1;
379 block = XFS_BUF_TO_BLOCK(bp);
380 }
381 }
382 return 0;
383}
384
385/*
386 * Count fsblocks of the given fork.
387 */
388int /* error */
389xfs_bmap_count_blocks(
390 xfs_trans_t *tp, /* transaction pointer */
391 xfs_inode_t *ip, /* incore inode */
392 int whichfork, /* data or attr fork */
393 int *count) /* out: count of blocks */
394{
395 struct xfs_btree_block *block; /* current btree block */
396 xfs_fsblock_t bno; /* block # of "block" */
397 xfs_ifork_t *ifp; /* fork structure */
398 int level; /* btree level, for checking */
399 xfs_mount_t *mp; /* file system mount structure */
400 __be64 *pp; /* pointer to block address */
401
402 bno = NULLFSBLOCK;
403 mp = ip->i_mount;
404 ifp = XFS_IFORK_PTR(ip, whichfork);
405 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
406 xfs_bmap_count_leaves(ifp, 0,
407 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
408 count);
409 return 0;
410 }
411
412 /*
413 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
414 */
415 block = ifp->if_broot;
416 level = be16_to_cpu(block->bb_level);
417 ASSERT(level > 0);
418 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
419 bno = be64_to_cpu(*pp);
420 ASSERT(bno != NULLDFSBNO);
421 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
422 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
423
424 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
425 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
426 mp);
427 return XFS_ERROR(EFSCORRUPTED);
428 }
429
430 return 0;
431}
432
433/*
434 * Debug/sanity checking code 255 * Debug/sanity checking code
435 */ 256 */
436 257
@@ -824,7 +645,7 @@ xfs_bmap_add_free(
824 * Remove the entry "free" from the free item list. Prev points to the 645 * Remove the entry "free" from the free item list. Prev points to the
825 * previous entry, unless "free" is the head of the list. 646 * previous entry, unless "free" is the head of the list.
826 */ 647 */
827STATIC void 648void
828xfs_bmap_del_free( 649xfs_bmap_del_free(
829 xfs_bmap_free_t *flist, /* free item list header */ 650 xfs_bmap_free_t *flist, /* free item list header */
830 xfs_bmap_free_item_t *prev, /* previous item on list, if any */ 651 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
@@ -838,92 +659,6 @@ xfs_bmap_del_free(
838 kmem_zone_free(xfs_bmap_free_item_zone, free); 659 kmem_zone_free(xfs_bmap_free_item_zone, free);
839} 660}
840 661
841
842/*
843 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
844 * caller. Frees all the extents that need freeing, which must be done
845 * last due to locking considerations. We never free any extents in
846 * the first transaction.
847 *
848 * Return 1 if the given transaction was committed and a new one
849 * started, and 0 otherwise in the committed parameter.
850 */
851int /* error */
852xfs_bmap_finish(
853 xfs_trans_t **tp, /* transaction pointer addr */
854 xfs_bmap_free_t *flist, /* i/o: list extents to free */
855 int *committed) /* xact committed or not */
856{
857 xfs_efd_log_item_t *efd; /* extent free data */
858 xfs_efi_log_item_t *efi; /* extent free intention */
859 int error; /* error return value */
860 xfs_bmap_free_item_t *free; /* free extent item */
861 unsigned int logres; /* new log reservation */
862 unsigned int logcount; /* new log count */
863 xfs_mount_t *mp; /* filesystem mount structure */
864 xfs_bmap_free_item_t *next; /* next item on free list */
865 xfs_trans_t *ntp; /* new transaction pointer */
866
867 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
868 if (flist->xbf_count == 0) {
869 *committed = 0;
870 return 0;
871 }
872 ntp = *tp;
873 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
874 for (free = flist->xbf_first; free; free = free->xbfi_next)
875 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
876 free->xbfi_blockcount);
877 logres = ntp->t_log_res;
878 logcount = ntp->t_log_count;
879 ntp = xfs_trans_dup(*tp);
880 error = xfs_trans_commit(*tp, 0);
881 *tp = ntp;
882 *committed = 1;
883 /*
884 * We have a new transaction, so we should return committed=1,
885 * even though we're returning an error.
886 */
887 if (error)
888 return error;
889
890 /*
891 * transaction commit worked ok so we can drop the extra ticket
892 * reference that we gained in xfs_trans_dup()
893 */
894 xfs_log_ticket_put(ntp->t_ticket);
895
896 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
897 logcount)))
898 return error;
899 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
900 for (free = flist->xbf_first; free != NULL; free = next) {
901 next = free->xbfi_next;
902 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
903 free->xbfi_blockcount))) {
904 /*
905 * The bmap free list will be cleaned up at a
906 * higher level. The EFI will be canceled when
907 * this transaction is aborted.
908 * Need to force shutdown here to make sure it
909 * happens, since this transaction may not be
910 * dirty yet.
911 */
912 mp = ntp->t_mountp;
913 if (!XFS_FORCED_SHUTDOWN(mp))
914 xfs_force_shutdown(mp,
915 (error == EFSCORRUPTED) ?
916 SHUTDOWN_CORRUPT_INCORE :
917 SHUTDOWN_META_IO_ERROR);
918 return error;
919 }
920 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
921 free->xbfi_blockcount);
922 xfs_bmap_del_free(flist, NULL, free);
923 }
924 return 0;
925}
926
927/* 662/*
928 * Free up any items left in the list. 663 * Free up any items left in the list.
929 */ 664 */
@@ -1864,7 +1599,7 @@ xfs_bmap_last_before(
1864 return 0; 1599 return 0;
1865} 1600}
1866 1601
1867STATIC int 1602int
1868xfs_bmap_last_extent( 1603xfs_bmap_last_extent(
1869 struct xfs_trans *tp, 1604 struct xfs_trans *tp,
1870 struct xfs_inode *ip, 1605 struct xfs_inode *ip,
@@ -1928,29 +1663,6 @@ xfs_bmap_isaeof(
1928} 1663}
1929 1664
1930/* 1665/*
1931 * Check if the endoff is outside the last extent. If so the caller will grow
1932 * the allocation to a stripe unit boundary. All offsets are considered outside
1933 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1934 */
1935int
1936xfs_bmap_eof(
1937 struct xfs_inode *ip,
1938 xfs_fileoff_t endoff,
1939 int whichfork,
1940 int *eof)
1941{
1942 struct xfs_bmbt_irec rec;
1943 int error;
1944
1945 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1946 if (error || *eof)
1947 return error;
1948
1949 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1950 return 0;
1951}
1952
1953/*
1954 * Returns the file-relative block number of the first block past eof in 1666 * Returns the file-relative block number of the first block past eof in
1955 * the file. This is not based on i_size, it is based on the extent records. 1667 * the file. This is not based on i_size, it is based on the extent records.
1956 * Returns 0 for local files, as they do not have extent records. 1668 * Returns 0 for local files, as they do not have extent records.
@@ -3489,7 +3201,7 @@ done:
3489/* 3201/*
3490 * Adjust the size of the new extent based on di_extsize and rt extsize. 3202 * Adjust the size of the new extent based on di_extsize and rt extsize.
3491 */ 3203 */
3492STATIC int 3204int
3493xfs_bmap_extsize_align( 3205xfs_bmap_extsize_align(
3494 xfs_mount_t *mp, 3206 xfs_mount_t *mp,
3495 xfs_bmbt_irec_t *gotp, /* next extent pointer */ 3207 xfs_bmbt_irec_t *gotp, /* next extent pointer */
@@ -3651,9 +3363,9 @@ xfs_bmap_extsize_align(
3651 3363
3652#define XFS_ALLOC_GAP_UNITS 4 3364#define XFS_ALLOC_GAP_UNITS 4
3653 3365
3654STATIC void 3366void
3655xfs_bmap_adjacent( 3367xfs_bmap_adjacent(
3656 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3368 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3657{ 3369{
3658 xfs_fsblock_t adjust; /* adjustment to block numbers */ 3370 xfs_fsblock_t adjust; /* adjustment to block numbers */
3659 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ 3371 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
@@ -3800,109 +3512,6 @@ xfs_bmap_adjacent(
3800} 3512}
3801 3513
3802STATIC int 3514STATIC int
3803xfs_bmap_rtalloc(
3804 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3805{
3806 xfs_alloctype_t atype = 0; /* type for allocation routines */
3807 int error; /* error return value */
3808 xfs_mount_t *mp; /* mount point structure */
3809 xfs_extlen_t prod = 0; /* product factor for allocators */
3810 xfs_extlen_t ralen = 0; /* realtime allocation length */
3811 xfs_extlen_t align; /* minimum allocation alignment */
3812 xfs_rtblock_t rtb;
3813
3814 mp = ap->ip->i_mount;
3815 align = xfs_get_extsz_hint(ap->ip);
3816 prod = align / mp->m_sb.sb_rextsize;
3817 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3818 align, 1, ap->eof, 0,
3819 ap->conv, &ap->offset, &ap->length);
3820 if (error)
3821 return error;
3822 ASSERT(ap->length);
3823 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
3824
3825 /*
3826 * If the offset & length are not perfectly aligned
3827 * then kill prod, it will just get us in trouble.
3828 */
3829 if (do_mod(ap->offset, align) || ap->length % align)
3830 prod = 1;
3831 /*
3832 * Set ralen to be the actual requested length in rtextents.
3833 */
3834 ralen = ap->length / mp->m_sb.sb_rextsize;
3835 /*
3836 * If the old value was close enough to MAXEXTLEN that
3837 * we rounded up to it, cut it back so it's valid again.
3838 * Note that if it's a really large request (bigger than
3839 * MAXEXTLEN), we don't hear about that number, and can't
3840 * adjust the starting point to match it.
3841 */
3842 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3843 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
3844
3845 /*
3846 * Lock out other modifications to the RT bitmap inode.
3847 */
3848 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
3849 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
3850
3851 /*
3852 * If it's an allocation to an empty file at offset 0,
3853 * pick an extent that will space things out in the rt area.
3854 */
3855 if (ap->eof && ap->offset == 0) {
3856 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3857
3858 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3859 if (error)
3860 return error;
3861 ap->blkno = rtx * mp->m_sb.sb_rextsize;
3862 } else {
3863 ap->blkno = 0;
3864 }
3865
3866 xfs_bmap_adjacent(ap);
3867
3868 /*
3869 * Realtime allocation, done through xfs_rtallocate_extent.
3870 */
3871 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3872 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3873 rtb = ap->blkno;
3874 ap->length = ralen;
3875 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
3876 &ralen, atype, ap->wasdel, prod, &rtb)))
3877 return error;
3878 if (rtb == NULLFSBLOCK && prod > 1 &&
3879 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3880 ap->length, &ralen, atype,
3881 ap->wasdel, 1, &rtb)))
3882 return error;
3883 ap->blkno = rtb;
3884 if (ap->blkno != NULLFSBLOCK) {
3885 ap->blkno *= mp->m_sb.sb_rextsize;
3886 ralen *= mp->m_sb.sb_rextsize;
3887 ap->length = ralen;
3888 ap->ip->i_d.di_nblocks += ralen;
3889 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3890 if (ap->wasdel)
3891 ap->ip->i_delayed_blks -= ralen;
3892 /*
3893 * Adjust the disk quota also. This was reserved
3894 * earlier.
3895 */
3896 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3897 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3898 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3899 } else {
3900 ap->length = 0;
3901 }
3902 return 0;
3903}
3904
3905STATIC int
3906xfs_bmap_btalloc_nullfb( 3515xfs_bmap_btalloc_nullfb(
3907 struct xfs_bmalloca *ap, 3516 struct xfs_bmalloca *ap,
3908 struct xfs_alloc_arg *args, 3517 struct xfs_alloc_arg *args,
@@ -4019,7 +3628,7 @@ xfs_bmap_btalloc_nullfb(
4019 3628
4020STATIC int 3629STATIC int
4021xfs_bmap_btalloc( 3630xfs_bmap_btalloc(
4022 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3631 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4023{ 3632{
4024 xfs_mount_t *mp; /* mount point structure */ 3633 xfs_mount_t *mp; /* mount point structure */
4025 xfs_alloctype_t atype = 0; /* type for allocation routines */ 3634 xfs_alloctype_t atype = 0; /* type for allocation routines */
@@ -4251,7 +3860,7 @@ xfs_bmap_btalloc(
4251 */ 3860 */
4252STATIC int 3861STATIC int
4253xfs_bmap_alloc( 3862xfs_bmap_alloc(
4254 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3863 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4255{ 3864{
4256 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) 3865 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
4257 return xfs_bmap_rtalloc(ap); 3866 return xfs_bmap_rtalloc(ap);
@@ -4639,7 +4248,7 @@ xfs_bmapi_delay(
4639} 4248}
4640 4249
4641 4250
4642STATIC int 4251int
4643__xfs_bmapi_allocate( 4252__xfs_bmapi_allocate(
4644 struct xfs_bmalloca *bma) 4253 struct xfs_bmalloca *bma)
4645{ 4254{
@@ -4757,45 +4366,6 @@ __xfs_bmapi_allocate(
4757 return 0; 4366 return 0;
4758} 4367}
4759 4368
4760static void
4761xfs_bmapi_allocate_worker(
4762 struct work_struct *work)
4763{
4764 struct xfs_bmalloca *args = container_of(work,
4765 struct xfs_bmalloca, work);
4766 unsigned long pflags;
4767
4768 /* we are in a transaction context here */
4769 current_set_flags_nested(&pflags, PF_FSTRANS);
4770
4771 args->result = __xfs_bmapi_allocate(args);
4772 complete(args->done);
4773
4774 current_restore_flags_nested(&pflags, PF_FSTRANS);
4775}
4776
4777/*
4778 * Some allocation requests often come in with little stack to work on. Push
4779 * them off to a worker thread so there is lots of stack to use. Otherwise just
4780 * call directly to avoid the context switch overhead here.
4781 */
4782int
4783xfs_bmapi_allocate(
4784 struct xfs_bmalloca *args)
4785{
4786 DECLARE_COMPLETION_ONSTACK(done);
4787
4788 if (!args->stack_switch)
4789 return __xfs_bmapi_allocate(args);
4790
4791
4792 args->done = &done;
4793 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4794 queue_work(xfs_alloc_wq, &args->work);
4795 wait_for_completion(&done);
4796 return args->result;
4797}
4798
4799STATIC int 4369STATIC int
4800xfs_bmapi_convert_unwritten( 4370xfs_bmapi_convert_unwritten(
4801 struct xfs_bmalloca *bma, 4371 struct xfs_bmalloca *bma,
@@ -5790,359 +5360,3 @@ error0:
5790 } 5360 }
5791 return error; 5361 return error;
5792} 5362}
5793
5794/*
5795 * returns 1 for success, 0 if we failed to map the extent.
5796 */
5797STATIC int
5798xfs_getbmapx_fix_eof_hole(
5799 xfs_inode_t *ip, /* xfs incore inode pointer */
5800 struct getbmapx *out, /* output structure */
5801 int prealloced, /* this is a file with
5802 * preallocated data space */
5803 __int64_t end, /* last block requested */
5804 xfs_fsblock_t startblock)
5805{
5806 __int64_t fixlen;
5807 xfs_mount_t *mp; /* file system mount point */
5808 xfs_ifork_t *ifp; /* inode fork pointer */
5809 xfs_extnum_t lastx; /* last extent pointer */
5810 xfs_fileoff_t fileblock;
5811
5812 if (startblock == HOLESTARTBLOCK) {
5813 mp = ip->i_mount;
5814 out->bmv_block = -1;
5815 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
5816 fixlen -= out->bmv_offset;
5817 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5818 /* Came to hole at EOF. Trim it. */
5819 if (fixlen <= 0)
5820 return 0;
5821 out->bmv_length = fixlen;
5822 }
5823 } else {
5824 if (startblock == DELAYSTARTBLOCK)
5825 out->bmv_block = -2;
5826 else
5827 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5828 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5829 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5830 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5831 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5832 out->bmv_oflags |= BMV_OF_LAST;
5833 }
5834
5835 return 1;
5836}
5837
5838/*
5839 * Get inode's extents as described in bmv, and format for output.
5840 * Calls formatter to fill the user's buffer until all extents
5841 * are mapped, until the passed-in bmv->bmv_count slots have
5842 * been filled, or until the formatter short-circuits the loop,
5843 * if it is tracking filled-in extents on its own.
5844 */
5845int /* error code */
5846xfs_getbmap(
5847 xfs_inode_t *ip,
5848 struct getbmapx *bmv, /* user bmap structure */
5849 xfs_bmap_format_t formatter, /* format to user */
5850 void *arg) /* formatter arg */
5851{
5852 __int64_t bmvend; /* last block requested */
5853 int error = 0; /* return value */
5854 __int64_t fixlen; /* length for -1 case */
5855 int i; /* extent number */
5856 int lock; /* lock state */
5857 xfs_bmbt_irec_t *map; /* buffer for user's data */
5858 xfs_mount_t *mp; /* file system mount point */
5859 int nex; /* # of user extents can do */
5860 int nexleft; /* # of user extents left */
5861 int subnex; /* # of bmapi's can do */
5862 int nmap; /* number of map entries */
5863 struct getbmapx *out; /* output structure */
5864 int whichfork; /* data or attr fork */
5865 int prealloced; /* this is a file with
5866 * preallocated data space */
5867 int iflags; /* interface flags */
5868 int bmapi_flags; /* flags for xfs_bmapi */
5869 int cur_ext = 0;
5870
5871 mp = ip->i_mount;
5872 iflags = bmv->bmv_iflags;
5873 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5874
5875 if (whichfork == XFS_ATTR_FORK) {
5876 if (XFS_IFORK_Q(ip)) {
5877 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5878 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5879 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5880 return XFS_ERROR(EINVAL);
5881 } else if (unlikely(
5882 ip->i_d.di_aformat != 0 &&
5883 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5884 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5885 ip->i_mount);
5886 return XFS_ERROR(EFSCORRUPTED);
5887 }
5888
5889 prealloced = 0;
5890 fixlen = 1LL << 32;
5891 } else {
5892 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5893 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5894 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5895 return XFS_ERROR(EINVAL);
5896
5897 if (xfs_get_extsz_hint(ip) ||
5898 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5899 prealloced = 1;
5900 fixlen = mp->m_super->s_maxbytes;
5901 } else {
5902 prealloced = 0;
5903 fixlen = XFS_ISIZE(ip);
5904 }
5905 }
5906
5907 if (bmv->bmv_length == -1) {
5908 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5909 bmv->bmv_length =
5910 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5911 } else if (bmv->bmv_length == 0) {
5912 bmv->bmv_entries = 0;
5913 return 0;
5914 } else if (bmv->bmv_length < 0) {
5915 return XFS_ERROR(EINVAL);
5916 }
5917
5918 nex = bmv->bmv_count - 1;
5919 if (nex <= 0)
5920 return XFS_ERROR(EINVAL);
5921 bmvend = bmv->bmv_offset + bmv->bmv_length;
5922
5923
5924 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5925 return XFS_ERROR(ENOMEM);
5926 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5927 if (!out) {
5928 out = kmem_zalloc_large(bmv->bmv_count *
5929 sizeof(struct getbmapx));
5930 if (!out)
5931 return XFS_ERROR(ENOMEM);
5932 }
5933
5934 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5935 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5936 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
5937 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
5938 if (error)
5939 goto out_unlock_iolock;
5940 }
5941 /*
5942 * even after flushing the inode, there can still be delalloc
5943 * blocks on the inode beyond EOF due to speculative
5944 * preallocation. These are not removed until the release
5945 * function is called or the inode is inactivated. Hence we
5946 * cannot assert here that ip->i_delayed_blks == 0.
5947 */
5948 }
5949
5950 lock = xfs_ilock_map_shared(ip);
5951
5952 /*
5953 * Don't let nex be bigger than the number of extents
5954 * we can have assuming alternating holes and real extents.
5955 */
5956 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5957 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5958
5959 bmapi_flags = xfs_bmapi_aflag(whichfork);
5960 if (!(iflags & BMV_IF_PREALLOC))
5961 bmapi_flags |= XFS_BMAPI_IGSTATE;
5962
5963 /*
5964 * Allocate enough space to handle "subnex" maps at a time.
5965 */
5966 error = ENOMEM;
5967 subnex = 16;
5968 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5969 if (!map)
5970 goto out_unlock_ilock;
5971
5972 bmv->bmv_entries = 0;
5973
5974 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5975 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5976 error = 0;
5977 goto out_free_map;
5978 }
5979
5980 nexleft = nex;
5981
5982 do {
5983 nmap = (nexleft > subnex) ? subnex : nexleft;
5984 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5985 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5986 map, &nmap, bmapi_flags);
5987 if (error)
5988 goto out_free_map;
5989 ASSERT(nmap <= subnex);
5990
5991 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5992 out[cur_ext].bmv_oflags = 0;
5993 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5994 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5995 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5996 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5997 out[cur_ext].bmv_offset =
5998 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5999 out[cur_ext].bmv_length =
6000 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6001 out[cur_ext].bmv_unused1 = 0;
6002 out[cur_ext].bmv_unused2 = 0;
6003
6004 /*
6005 * delayed allocation extents that start beyond EOF can
6006 * occur due to speculative EOF allocation when the
6007 * delalloc extent is larger than the largest freespace
6008 * extent at conversion time. These extents cannot be
6009 * converted by data writeback, so can exist here even
6010 * if we are not supposed to be finding delalloc
6011 * extents.
6012 */
6013 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6014 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6015 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6016
6017 if (map[i].br_startblock == HOLESTARTBLOCK &&
6018 whichfork == XFS_ATTR_FORK) {
6019 /* came to the end of attribute fork */
6020 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
6021 goto out_free_map;
6022 }
6023
6024 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6025 prealloced, bmvend,
6026 map[i].br_startblock))
6027 goto out_free_map;
6028
6029 bmv->bmv_offset =
6030 out[cur_ext].bmv_offset +
6031 out[cur_ext].bmv_length;
6032 bmv->bmv_length =
6033 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
6034
6035 /*
6036 * In case we don't want to return the hole,
6037 * don't increase cur_ext so that we can reuse
6038 * it in the next loop.
6039 */
6040 if ((iflags & BMV_IF_NO_HOLES) &&
6041 map[i].br_startblock == HOLESTARTBLOCK) {
6042 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6043 continue;
6044 }
6045
6046 nexleft--;
6047 bmv->bmv_entries++;
6048 cur_ext++;
6049 }
6050 } while (nmap && nexleft && bmv->bmv_length);
6051
6052 out_free_map:
6053 kmem_free(map);
6054 out_unlock_ilock:
6055 xfs_iunlock_map_shared(ip, lock);
6056 out_unlock_iolock:
6057 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6058
6059 for (i = 0; i < cur_ext; i++) {
6060 int full = 0; /* user array is full */
6061
6062 /* format results & advance arg */
6063 error = formatter(&arg, &out[i], &full);
6064 if (error || full)
6065 break;
6066 }
6067
6068 if (is_vmalloc_addr(out))
6069 kmem_free_large(out);
6070 else
6071 kmem_free(out);
6072 return error;
6073}
6074
6075/*
6076 * dead simple method of punching delalyed allocation blocks from a range in
6077 * the inode. Walks a block at a time so will be slow, but is only executed in
6078 * rare error cases so the overhead is not critical. This will alays punch out
6079 * both the start and end blocks, even if the ranges only partially overlap
6080 * them, so it is up to the caller to ensure that partial blocks are not
6081 * passed in.
6082 */
6083int
6084xfs_bmap_punch_delalloc_range(
6085 struct xfs_inode *ip,
6086 xfs_fileoff_t start_fsb,
6087 xfs_fileoff_t length)
6088{
6089 xfs_fileoff_t remaining = length;
6090 int error = 0;
6091
6092 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6093
6094 do {
6095 int done;
6096 xfs_bmbt_irec_t imap;
6097 int nimaps = 1;
6098 xfs_fsblock_t firstblock;
6099 xfs_bmap_free_t flist;
6100
6101 /*
6102 * Map the range first and check that it is a delalloc extent
6103 * before trying to unmap the range. Otherwise we will be
6104 * trying to remove a real extent (which requires a
6105 * transaction) or a hole, which is probably a bad idea...
6106 */
6107 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6108 XFS_BMAPI_ENTIRE);
6109
6110 if (error) {
6111 /* something screwed, just bail */
6112 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6113 xfs_alert(ip->i_mount,
6114 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6115 ip->i_ino, start_fsb);
6116 }
6117 break;
6118 }
6119 if (!nimaps) {
6120 /* nothing there */
6121 goto next_block;
6122 }
6123 if (imap.br_startblock != DELAYSTARTBLOCK) {
6124 /* been converted, ignore */
6125 goto next_block;
6126 }
6127 WARN_ON(imap.br_blockcount == 0);
6128
6129 /*
6130 * Note: while we initialise the firstblock/flist pair, they
6131 * should never be used because blocks should never be
6132 * allocated or freed for a delalloc extent and hence we need
6133 * don't cancel or finish them after the xfs_bunmapi() call.
6134 */
6135 xfs_bmap_init(&flist, &firstblock);
6136 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6137 &flist, &done);
6138 if (error)
6139 break;
6140
6141 ASSERT(!flist.xbf_count && !flist.xbf_first);
6142next_block:
6143 start_fsb++;
6144 remaining--;
6145 } while(remaining > 0);
6146
6147 return error;
6148}
diff --git a/fs/xfs/xfs_bmap.h b/fs/xfs/xfs_bmap.h
index 314dca1775dc..8b2fe9785b3a 100644
--- a/fs/xfs/xfs_bmap.h
+++ b/fs/xfs/xfs_bmap.h
@@ -108,43 +108,6 @@ static inline void xfs_bmap_init(xfs_bmap_free_t *flp, xfs_fsblock_t *fbp)
108} 108}
109 109
110/* 110/*
111 * Argument structure for xfs_bmap_alloc.
112 */
113typedef struct xfs_bmalloca {
114 xfs_fsblock_t *firstblock; /* i/o first block allocated */
115 struct xfs_bmap_free *flist; /* bmap freelist */
116 struct xfs_trans *tp; /* transaction pointer */
117 struct xfs_inode *ip; /* incore inode pointer */
118 struct xfs_bmbt_irec prev; /* extent before the new one */
119 struct xfs_bmbt_irec got; /* extent after, or delayed */
120
121 xfs_fileoff_t offset; /* offset in file filling in */
122 xfs_extlen_t length; /* i/o length asked/allocated */
123 xfs_fsblock_t blkno; /* starting block of new extent */
124
125 struct xfs_btree_cur *cur; /* btree cursor */
126 xfs_extnum_t idx; /* current extent index */
127 int nallocs;/* number of extents alloc'd */
128 int logflags;/* flags for transaction logging */
129
130 xfs_extlen_t total; /* total blocks needed for xaction */
131 xfs_extlen_t minlen; /* minimum allocation size (blocks) */
132 xfs_extlen_t minleft; /* amount must be left after alloc */
133 char eof; /* set if allocating past last extent */
134 char wasdel; /* replacing a delayed allocation */
135 char userdata;/* set if is user data */
136 char aeof; /* allocated space at eof */
137 char conv; /* overwriting unwritten extents */
138 char stack_switch;
139 int flags;
140#ifdef __KERNEL__
141 struct completion *done;
142 struct work_struct work;
143 int result;
144#endif /* __KERNEL__ */
145} xfs_bmalloca_t;
146
147/*
148 * Flags for xfs_bmap_add_extent*. 111 * Flags for xfs_bmap_add_extent*.
149 */ 112 */
150#define BMAP_LEFT_CONTIG (1 << 0) 113#define BMAP_LEFT_CONTIG (1 << 0)
@@ -207,23 +170,4 @@ int xfs_check_nostate_extents(struct xfs_ifork *ifp, xfs_extnum_t idx,
207 xfs_extnum_t num); 170 xfs_extnum_t num);
208uint xfs_default_attroffset(struct xfs_inode *ip); 171uint xfs_default_attroffset(struct xfs_inode *ip);
209 172
210#ifdef __KERNEL__
211/* bmap to userspace formatter - copy to user & advance pointer */
212typedef int (*xfs_bmap_format_t)(void **, struct getbmapx *, int *);
213
214int xfs_bmap_finish(struct xfs_trans **tp, struct xfs_bmap_free *flist,
215 int *committed);
216int xfs_getbmap(struct xfs_inode *ip, struct getbmapx *bmv,
217 xfs_bmap_format_t formatter, void *arg);
218int xfs_bmap_eof(struct xfs_inode *ip, xfs_fileoff_t endoff,
219 int whichfork, int *eof);
220int xfs_bmap_count_blocks(struct xfs_trans *tp, struct xfs_inode *ip,
221 int whichfork, int *count);
222int xfs_bmap_punch_delalloc_range(struct xfs_inode *ip,
223 xfs_fileoff_t start_fsb, xfs_fileoff_t length);
224
225xfs_daddr_t xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb);
226
227#endif /* __KERNEL__ */
228
229#endif /* __XFS_BMAP_H__ */ 173#endif /* __XFS_BMAP_H__ */
diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c
new file mode 100644
index 000000000000..8e48ddf1c11c
--- /dev/null
+++ b/fs/xfs/xfs_bmap_util.c
@@ -0,0 +1,834 @@
1/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * 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.
13 *
14 * 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
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_format.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_da_btree.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
32#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_btree.h"
35#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
38#include "xfs_bmap_util.h"
39#include "xfs_rtalloc.h"
40#include "xfs_error.h"
41#include "xfs_quota.h"
42#include "xfs_trans_space.h"
43#include "xfs_trace.h"
44
45/* Kernel only BMAP related definitions and functions */
46
47/*
48 * Convert the given file system block to a disk block. We have to treat it
49 * differently based on whether the file is a real time file or not, because the
50 * bmap code does.
51 */
52xfs_daddr_t
53xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
54{
55 return (XFS_IS_REALTIME_INODE(ip) ? \
56 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
57 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
58}
59
60/*
61 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
62 * caller. Frees all the extents that need freeing, which must be done
63 * last due to locking considerations. We never free any extents in
64 * the first transaction.
65 *
66 * Return 1 if the given transaction was committed and a new one
67 * started, and 0 otherwise in the committed parameter.
68 */
69int /* error */
70xfs_bmap_finish(
71 xfs_trans_t **tp, /* transaction pointer addr */
72 xfs_bmap_free_t *flist, /* i/o: list extents to free */
73 int *committed) /* xact committed or not */
74{
75 xfs_efd_log_item_t *efd; /* extent free data */
76 xfs_efi_log_item_t *efi; /* extent free intention */
77 int error; /* error return value */
78 xfs_bmap_free_item_t *free; /* free extent item */
79 unsigned int logres; /* new log reservation */
80 unsigned int logcount; /* new log count */
81 xfs_mount_t *mp; /* filesystem mount structure */
82 xfs_bmap_free_item_t *next; /* next item on free list */
83 xfs_trans_t *ntp; /* new transaction pointer */
84
85 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
86 if (flist->xbf_count == 0) {
87 *committed = 0;
88 return 0;
89 }
90 ntp = *tp;
91 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
92 for (free = flist->xbf_first; free; free = free->xbfi_next)
93 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
94 free->xbfi_blockcount);
95 logres = ntp->t_log_res;
96 logcount = ntp->t_log_count;
97 ntp = xfs_trans_dup(*tp);
98 error = xfs_trans_commit(*tp, 0);
99 *tp = ntp;
100 *committed = 1;
101 /*
102 * We have a new transaction, so we should return committed=1,
103 * even though we're returning an error.
104 */
105 if (error)
106 return error;
107
108 /*
109 * transaction commit worked ok so we can drop the extra ticket
110 * reference that we gained in xfs_trans_dup()
111 */
112 xfs_log_ticket_put(ntp->t_ticket);
113
114 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
115 logcount)))
116 return error;
117 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
118 for (free = flist->xbf_first; free != NULL; free = next) {
119 next = free->xbfi_next;
120 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
121 free->xbfi_blockcount))) {
122 /*
123 * The bmap free list will be cleaned up at a
124 * higher level. The EFI will be canceled when
125 * this transaction is aborted.
126 * Need to force shutdown here to make sure it
127 * happens, since this transaction may not be
128 * dirty yet.
129 */
130 mp = ntp->t_mountp;
131 if (!XFS_FORCED_SHUTDOWN(mp))
132 xfs_force_shutdown(mp,
133 (error == EFSCORRUPTED) ?
134 SHUTDOWN_CORRUPT_INCORE :
135 SHUTDOWN_META_IO_ERROR);
136 return error;
137 }
138 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
139 free->xbfi_blockcount);
140 xfs_bmap_del_free(flist, NULL, free);
141 }
142 return 0;
143}
144
145int
146xfs_bmap_rtalloc(
147 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
148{
149 xfs_alloctype_t atype = 0; /* type for allocation routines */
150 int error; /* error return value */
151 xfs_mount_t *mp; /* mount point structure */
152 xfs_extlen_t prod = 0; /* product factor for allocators */
153 xfs_extlen_t ralen = 0; /* realtime allocation length */
154 xfs_extlen_t align; /* minimum allocation alignment */
155 xfs_rtblock_t rtb;
156
157 mp = ap->ip->i_mount;
158 align = xfs_get_extsz_hint(ap->ip);
159 prod = align / mp->m_sb.sb_rextsize;
160 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
161 align, 1, ap->eof, 0,
162 ap->conv, &ap->offset, &ap->length);
163 if (error)
164 return error;
165 ASSERT(ap->length);
166 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
167
168 /*
169 * If the offset & length are not perfectly aligned
170 * then kill prod, it will just get us in trouble.
171 */
172 if (do_mod(ap->offset, align) || ap->length % align)
173 prod = 1;
174 /*
175 * Set ralen to be the actual requested length in rtextents.
176 */
177 ralen = ap->length / mp->m_sb.sb_rextsize;
178 /*
179 * If the old value was close enough to MAXEXTLEN that
180 * we rounded up to it, cut it back so it's valid again.
181 * Note that if it's a really large request (bigger than
182 * MAXEXTLEN), we don't hear about that number, and can't
183 * adjust the starting point to match it.
184 */
185 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
186 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
187
188 /*
189 * Lock out other modifications to the RT bitmap inode.
190 */
191 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
192 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
193
194 /*
195 * If it's an allocation to an empty file at offset 0,
196 * pick an extent that will space things out in the rt area.
197 */
198 if (ap->eof && ap->offset == 0) {
199 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
200
201 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
202 if (error)
203 return error;
204 ap->blkno = rtx * mp->m_sb.sb_rextsize;
205 } else {
206 ap->blkno = 0;
207 }
208
209 xfs_bmap_adjacent(ap);
210
211 /*
212 * Realtime allocation, done through xfs_rtallocate_extent.
213 */
214 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
215 do_div(ap->blkno, mp->m_sb.sb_rextsize);
216 rtb = ap->blkno;
217 ap->length = ralen;
218 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
219 &ralen, atype, ap->wasdel, prod, &rtb)))
220 return error;
221 if (rtb == NULLFSBLOCK && prod > 1 &&
222 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
223 ap->length, &ralen, atype,
224 ap->wasdel, 1, &rtb)))
225 return error;
226 ap->blkno = rtb;
227 if (ap->blkno != NULLFSBLOCK) {
228 ap->blkno *= mp->m_sb.sb_rextsize;
229 ralen *= mp->m_sb.sb_rextsize;
230 ap->length = ralen;
231 ap->ip->i_d.di_nblocks += ralen;
232 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
233 if (ap->wasdel)
234 ap->ip->i_delayed_blks -= ralen;
235 /*
236 * Adjust the disk quota also. This was reserved
237 * earlier.
238 */
239 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
240 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
241 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
242 } else {
243 ap->length = 0;
244 }
245 return 0;
246}
247
248/*
249 * Stack switching interfaces for allocation
250 */
251static void
252xfs_bmapi_allocate_worker(
253 struct work_struct *work)
254{
255 struct xfs_bmalloca *args = container_of(work,
256 struct xfs_bmalloca, work);
257 unsigned long pflags;
258
259 /* we are in a transaction context here */
260 current_set_flags_nested(&pflags, PF_FSTRANS);
261
262 args->result = __xfs_bmapi_allocate(args);
263 complete(args->done);
264
265 current_restore_flags_nested(&pflags, PF_FSTRANS);
266}
267
268/*
269 * Some allocation requests often come in with little stack to work on. Push
270 * them off to a worker thread so there is lots of stack to use. Otherwise just
271 * call directly to avoid the context switch overhead here.
272 */
273int
274xfs_bmapi_allocate(
275 struct xfs_bmalloca *args)
276{
277 DECLARE_COMPLETION_ONSTACK(done);
278
279 if (!args->stack_switch)
280 return __xfs_bmapi_allocate(args);
281
282
283 args->done = &done;
284 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
285 queue_work(xfs_alloc_wq, &args->work);
286 wait_for_completion(&done);
287 return args->result;
288}
289
290/*
291 * Check if the endoff is outside the last extent. If so the caller will grow
292 * the allocation to a stripe unit boundary. All offsets are considered outside
293 * the end of file for an empty fork, so 1 is returned in *eof in that case.
294 */
295int
296xfs_bmap_eof(
297 struct xfs_inode *ip,
298 xfs_fileoff_t endoff,
299 int whichfork,
300 int *eof)
301{
302 struct xfs_bmbt_irec rec;
303 int error;
304
305 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
306 if (error || *eof)
307 return error;
308
309 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
310 return 0;
311}
312
313/*
314 * Extent tree block counting routines.
315 */
316
317/*
318 * Count leaf blocks given a range of extent records.
319 */
320STATIC void
321xfs_bmap_count_leaves(
322 xfs_ifork_t *ifp,
323 xfs_extnum_t idx,
324 int numrecs,
325 int *count)
326{
327 int b;
328
329 for (b = 0; b < numrecs; b++) {
330 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
331 *count += xfs_bmbt_get_blockcount(frp);
332 }
333}
334
335/*
336 * Count leaf blocks given a range of extent records originally
337 * in btree format.
338 */
339STATIC void
340xfs_bmap_disk_count_leaves(
341 struct xfs_mount *mp,
342 struct xfs_btree_block *block,
343 int numrecs,
344 int *count)
345{
346 int b;
347 xfs_bmbt_rec_t *frp;
348
349 for (b = 1; b <= numrecs; b++) {
350 frp = XFS_BMBT_REC_ADDR(mp, block, b);
351 *count += xfs_bmbt_disk_get_blockcount(frp);
352 }
353}
354
355/*
356 * Recursively walks each level of a btree
357 * to count total fsblocks is use.
358 */
359STATIC int /* error */
360xfs_bmap_count_tree(
361 xfs_mount_t *mp, /* file system mount point */
362 xfs_trans_t *tp, /* transaction pointer */
363 xfs_ifork_t *ifp, /* inode fork pointer */
364 xfs_fsblock_t blockno, /* file system block number */
365 int levelin, /* level in btree */
366 int *count) /* Count of blocks */
367{
368 int error;
369 xfs_buf_t *bp, *nbp;
370 int level = levelin;
371 __be64 *pp;
372 xfs_fsblock_t bno = blockno;
373 xfs_fsblock_t nextbno;
374 struct xfs_btree_block *block, *nextblock;
375 int numrecs;
376
377 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
378 &xfs_bmbt_buf_ops);
379 if (error)
380 return error;
381 *count += 1;
382 block = XFS_BUF_TO_BLOCK(bp);
383
384 if (--level) {
385 /* Not at node above leaves, count this level of nodes */
386 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
387 while (nextbno != NULLFSBLOCK) {
388 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
389 XFS_BMAP_BTREE_REF,
390 &xfs_bmbt_buf_ops);
391 if (error)
392 return error;
393 *count += 1;
394 nextblock = XFS_BUF_TO_BLOCK(nbp);
395 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
396 xfs_trans_brelse(tp, nbp);
397 }
398
399 /* Dive to the next level */
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
402 if (unlikely((error =
403 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
404 xfs_trans_brelse(tp, bp);
405 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
406 XFS_ERRLEVEL_LOW, mp);
407 return XFS_ERROR(EFSCORRUPTED);
408 }
409 xfs_trans_brelse(tp, bp);
410 } else {
411 /* count all level 1 nodes and their leaves */
412 for (;;) {
413 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
414 numrecs = be16_to_cpu(block->bb_numrecs);
415 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
416 xfs_trans_brelse(tp, bp);
417 if (nextbno == NULLFSBLOCK)
418 break;
419 bno = nextbno;
420 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
421 XFS_BMAP_BTREE_REF,
422 &xfs_bmbt_buf_ops);
423 if (error)
424 return error;
425 *count += 1;
426 block = XFS_BUF_TO_BLOCK(bp);
427 }
428 }
429 return 0;
430}
431
432/*
433 * Count fsblocks of the given fork.
434 */
435int /* error */
436xfs_bmap_count_blocks(
437 xfs_trans_t *tp, /* transaction pointer */
438 xfs_inode_t *ip, /* incore inode */
439 int whichfork, /* data or attr fork */
440 int *count) /* out: count of blocks */
441{
442 struct xfs_btree_block *block; /* current btree block */
443 xfs_fsblock_t bno; /* block # of "block" */
444 xfs_ifork_t *ifp; /* fork structure */
445 int level; /* btree level, for checking */
446 xfs_mount_t *mp; /* file system mount structure */
447 __be64 *pp; /* pointer to block address */
448
449 bno = NULLFSBLOCK;
450 mp = ip->i_mount;
451 ifp = XFS_IFORK_PTR(ip, whichfork);
452 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
453 xfs_bmap_count_leaves(ifp, 0,
454 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
455 count);
456 return 0;
457 }
458
459 /*
460 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
461 */
462 block = ifp->if_broot;
463 level = be16_to_cpu(block->bb_level);
464 ASSERT(level > 0);
465 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
466 bno = be64_to_cpu(*pp);
467 ASSERT(bno != NULLDFSBNO);
468 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
469 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
470
471 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
472 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
473 mp);
474 return XFS_ERROR(EFSCORRUPTED);
475 }
476
477 return 0;
478}
479
480/*
481 * returns 1 for success, 0 if we failed to map the extent.
482 */
483STATIC int
484xfs_getbmapx_fix_eof_hole(
485 xfs_inode_t *ip, /* xfs incore inode pointer */
486 struct getbmapx *out, /* output structure */
487 int prealloced, /* this is a file with
488 * preallocated data space */
489 __int64_t end, /* last block requested */
490 xfs_fsblock_t startblock)
491{
492 __int64_t fixlen;
493 xfs_mount_t *mp; /* file system mount point */
494 xfs_ifork_t *ifp; /* inode fork pointer */
495 xfs_extnum_t lastx; /* last extent pointer */
496 xfs_fileoff_t fileblock;
497
498 if (startblock == HOLESTARTBLOCK) {
499 mp = ip->i_mount;
500 out->bmv_block = -1;
501 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
502 fixlen -= out->bmv_offset;
503 if (prealloced && out->bmv_offset + out->bmv_length == end) {
504 /* Came to hole at EOF. Trim it. */
505 if (fixlen <= 0)
506 return 0;
507 out->bmv_length = fixlen;
508 }
509 } else {
510 if (startblock == DELAYSTARTBLOCK)
511 out->bmv_block = -2;
512 else
513 out->bmv_block = xfs_fsb_to_db(ip, startblock);
514 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
515 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
516 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
517 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
518 out->bmv_oflags |= BMV_OF_LAST;
519 }
520
521 return 1;
522}
523
524/*
525 * Get inode's extents as described in bmv, and format for output.
526 * Calls formatter to fill the user's buffer until all extents
527 * are mapped, until the passed-in bmv->bmv_count slots have
528 * been filled, or until the formatter short-circuits the loop,
529 * if it is tracking filled-in extents on its own.
530 */
531int /* error code */
532xfs_getbmap(
533 xfs_inode_t *ip,
534 struct getbmapx *bmv, /* user bmap structure */
535 xfs_bmap_format_t formatter, /* format to user */
536 void *arg) /* formatter arg */
537{
538 __int64_t bmvend; /* last block requested */
539 int error = 0; /* return value */
540 __int64_t fixlen; /* length for -1 case */
541 int i; /* extent number */
542 int lock; /* lock state */
543 xfs_bmbt_irec_t *map; /* buffer for user's data */
544 xfs_mount_t *mp; /* file system mount point */
545 int nex; /* # of user extents can do */
546 int nexleft; /* # of user extents left */
547 int subnex; /* # of bmapi's can do */
548 int nmap; /* number of map entries */
549 struct getbmapx *out; /* output structure */
550 int whichfork; /* data or attr fork */
551 int prealloced; /* this is a file with
552 * preallocated data space */
553 int iflags; /* interface flags */
554 int bmapi_flags; /* flags for xfs_bmapi */
555 int cur_ext = 0;
556
557 mp = ip->i_mount;
558 iflags = bmv->bmv_iflags;
559 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
560
561 if (whichfork == XFS_ATTR_FORK) {
562 if (XFS_IFORK_Q(ip)) {
563 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
564 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
565 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
566 return XFS_ERROR(EINVAL);
567 } else if (unlikely(
568 ip->i_d.di_aformat != 0 &&
569 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
570 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
571 ip->i_mount);
572 return XFS_ERROR(EFSCORRUPTED);
573 }
574
575 prealloced = 0;
576 fixlen = 1LL << 32;
577 } else {
578 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
579 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
580 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
581 return XFS_ERROR(EINVAL);
582
583 if (xfs_get_extsz_hint(ip) ||
584 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
585 prealloced = 1;
586 fixlen = mp->m_super->s_maxbytes;
587 } else {
588 prealloced = 0;
589 fixlen = XFS_ISIZE(ip);
590 }
591 }
592
593 if (bmv->bmv_length == -1) {
594 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
595 bmv->bmv_length =
596 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
597 } else if (bmv->bmv_length == 0) {
598 bmv->bmv_entries = 0;
599 return 0;
600 } else if (bmv->bmv_length < 0) {
601 return XFS_ERROR(EINVAL);
602 }
603
604 nex = bmv->bmv_count - 1;
605 if (nex <= 0)
606 return XFS_ERROR(EINVAL);
607 bmvend = bmv->bmv_offset + bmv->bmv_length;
608
609
610 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
611 return XFS_ERROR(ENOMEM);
612 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
613 if (!out) {
614 out = kmem_zalloc_large(bmv->bmv_count *
615 sizeof(struct getbmapx));
616 if (!out)
617 return XFS_ERROR(ENOMEM);
618 }
619
620 xfs_ilock(ip, XFS_IOLOCK_SHARED);
621 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
622 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
623 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
624 if (error)
625 goto out_unlock_iolock;
626 }
627 /*
628 * even after flushing the inode, there can still be delalloc
629 * blocks on the inode beyond EOF due to speculative
630 * preallocation. These are not removed until the release
631 * function is called or the inode is inactivated. Hence we
632 * cannot assert here that ip->i_delayed_blks == 0.
633 */
634 }
635
636 lock = xfs_ilock_map_shared(ip);
637
638 /*
639 * Don't let nex be bigger than the number of extents
640 * we can have assuming alternating holes and real extents.
641 */
642 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
643 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
644
645 bmapi_flags = xfs_bmapi_aflag(whichfork);
646 if (!(iflags & BMV_IF_PREALLOC))
647 bmapi_flags |= XFS_BMAPI_IGSTATE;
648
649 /*
650 * Allocate enough space to handle "subnex" maps at a time.
651 */
652 error = ENOMEM;
653 subnex = 16;
654 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
655 if (!map)
656 goto out_unlock_ilock;
657
658 bmv->bmv_entries = 0;
659
660 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
661 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
662 error = 0;
663 goto out_free_map;
664 }
665
666 nexleft = nex;
667
668 do {
669 nmap = (nexleft > subnex) ? subnex : nexleft;
670 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
671 XFS_BB_TO_FSB(mp, bmv->bmv_length),
672 map, &nmap, bmapi_flags);
673 if (error)
674 goto out_free_map;
675 ASSERT(nmap <= subnex);
676
677 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
678 out[cur_ext].bmv_oflags = 0;
679 if (map[i].br_state == XFS_EXT_UNWRITTEN)
680 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
681 else if (map[i].br_startblock == DELAYSTARTBLOCK)
682 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
683 out[cur_ext].bmv_offset =
684 XFS_FSB_TO_BB(mp, map[i].br_startoff);
685 out[cur_ext].bmv_length =
686 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
687 out[cur_ext].bmv_unused1 = 0;
688 out[cur_ext].bmv_unused2 = 0;
689
690 /*
691 * delayed allocation extents that start beyond EOF can
692 * occur due to speculative EOF allocation when the
693 * delalloc extent is larger than the largest freespace
694 * extent at conversion time. These extents cannot be
695 * converted by data writeback, so can exist here even
696 * if we are not supposed to be finding delalloc
697 * extents.
698 */
699 if (map[i].br_startblock == DELAYSTARTBLOCK &&
700 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
701 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
702
703 if (map[i].br_startblock == HOLESTARTBLOCK &&
704 whichfork == XFS_ATTR_FORK) {
705 /* came to the end of attribute fork */
706 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
707 goto out_free_map;
708 }
709
710 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
711 prealloced, bmvend,
712 map[i].br_startblock))
713 goto out_free_map;
714
715 bmv->bmv_offset =
716 out[cur_ext].bmv_offset +
717 out[cur_ext].bmv_length;
718 bmv->bmv_length =
719 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
720
721 /*
722 * In case we don't want to return the hole,
723 * don't increase cur_ext so that we can reuse
724 * it in the next loop.
725 */
726 if ((iflags & BMV_IF_NO_HOLES) &&
727 map[i].br_startblock == HOLESTARTBLOCK) {
728 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
729 continue;
730 }
731
732 nexleft--;
733 bmv->bmv_entries++;
734 cur_ext++;
735 }
736 } while (nmap && nexleft && bmv->bmv_length);
737
738 out_free_map:
739 kmem_free(map);
740 out_unlock_ilock:
741 xfs_iunlock_map_shared(ip, lock);
742 out_unlock_iolock:
743 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
744
745 for (i = 0; i < cur_ext; i++) {
746 int full = 0; /* user array is full */
747
748 /* format results & advance arg */
749 error = formatter(&arg, &out[i], &full);
750 if (error || full)
751 break;
752 }
753
754 if (is_vmalloc_addr(out))
755 kmem_free_large(out);
756 else
757 kmem_free(out);
758 return error;
759}
760
761/*
762 * dead simple method of punching delalyed allocation blocks from a range in
763 * the inode. Walks a block at a time so will be slow, but is only executed in
764 * rare error cases so the overhead is not critical. This will alays punch out
765 * both the start and end blocks, even if the ranges only partially overlap
766 * them, so it is up to the caller to ensure that partial blocks are not
767 * passed in.
768 */
769int
770xfs_bmap_punch_delalloc_range(
771 struct xfs_inode *ip,
772 xfs_fileoff_t start_fsb,
773 xfs_fileoff_t length)
774{
775 xfs_fileoff_t remaining = length;
776 int error = 0;
777
778 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
779
780 do {
781 int done;
782 xfs_bmbt_irec_t imap;
783 int nimaps = 1;
784 xfs_fsblock_t firstblock;
785 xfs_bmap_free_t flist;
786
787 /*
788 * Map the range first and check that it is a delalloc extent
789 * before trying to unmap the range. Otherwise we will be
790 * trying to remove a real extent (which requires a
791 * transaction) or a hole, which is probably a bad idea...
792 */
793 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
794 XFS_BMAPI_ENTIRE);
795
796 if (error) {
797 /* something screwed, just bail */
798 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
799 xfs_alert(ip->i_mount,
800 "Failed delalloc mapping lookup ino %lld fsb %lld.",
801 ip->i_ino, start_fsb);
802 }
803 break;
804 }
805 if (!nimaps) {
806 /* nothing there */
807 goto next_block;
808 }
809 if (imap.br_startblock != DELAYSTARTBLOCK) {
810 /* been converted, ignore */
811 goto next_block;
812 }
813 WARN_ON(imap.br_blockcount == 0);
814
815 /*
816 * Note: while we initialise the firstblock/flist pair, they
817 * should never be used because blocks should never be
818 * allocated or freed for a delalloc extent and hence we need
819 * don't cancel or finish them after the xfs_bunmapi() call.
820 */
821 xfs_bmap_init(&flist, &firstblock);
822 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
823 &flist, &done);
824 if (error)
825 break;
826
827 ASSERT(!flist.xbf_count && !flist.xbf_first);
828next_block:
829 start_fsb++;
830 remaining--;
831 } while(remaining > 0);
832
833 return error;
834}
diff --git a/fs/xfs/xfs_bmap_util.h b/fs/xfs/xfs_bmap_util.h
new file mode 100644
index 000000000000..004fe4b28d49
--- /dev/null
+++ b/fs/xfs/xfs_bmap_util.h
@@ -0,0 +1,96 @@
1/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * 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.
13 *
14 * 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
17 */
18#ifndef __XFS_BMAP_UTIL_H__
19#define __XFS_BMAP_UTIL_H__
20
21/* Kernel only BMAP related definitions and functions */
22
23struct xfs_bmbt_irec;
24struct xfs_ifork;
25struct xfs_inode;
26struct xfs_mount;
27struct xfs_trans;
28
29/*
30 * Argument structure for xfs_bmap_alloc.
31 */
32struct xfs_bmalloca {
33 xfs_fsblock_t *firstblock; /* i/o first block allocated */
34 struct xfs_bmap_free *flist; /* bmap freelist */
35 struct xfs_trans *tp; /* transaction pointer */
36 struct xfs_inode *ip; /* incore inode pointer */
37 struct xfs_bmbt_irec prev; /* extent before the new one */
38 struct xfs_bmbt_irec got; /* extent after, or delayed */
39
40 xfs_fileoff_t offset; /* offset in file filling in */
41 xfs_extlen_t length; /* i/o length asked/allocated */
42 xfs_fsblock_t blkno; /* starting block of new extent */
43
44 struct xfs_btree_cur *cur; /* btree cursor */
45 xfs_extnum_t idx; /* current extent index */
46 int nallocs;/* number of extents alloc'd */
47 int logflags;/* flags for transaction logging */
48
49 xfs_extlen_t total; /* total blocks needed for xaction */
50 xfs_extlen_t minlen; /* minimum allocation size (blocks) */
51 xfs_extlen_t minleft; /* amount must be left after alloc */
52 char eof; /* set if allocating past last extent */
53 char wasdel; /* replacing a delayed allocation */
54 char userdata;/* set if is user data */
55 char aeof; /* allocated space at eof */
56 char conv; /* overwriting unwritten extents */
57 char stack_switch;
58 int flags;
59 struct completion *done;
60 struct work_struct work;
61 int result;
62};
63
64int xfs_bmap_finish(struct xfs_trans **tp, struct xfs_bmap_free *flist,
65 int *committed);
66int xfs_bmap_rtalloc(struct xfs_bmalloca *ap);
67int xfs_bmapi_allocate(struct xfs_bmalloca *args);
68int __xfs_bmapi_allocate(struct xfs_bmalloca *args);
69int xfs_bmap_eof(struct xfs_inode *ip, xfs_fileoff_t endoff,
70 int whichfork, int *eof);
71int xfs_bmap_count_blocks(struct xfs_trans *tp, struct xfs_inode *ip,
72 int whichfork, int *count);
73int xfs_bmap_punch_delalloc_range(struct xfs_inode *ip,
74 xfs_fileoff_t start_fsb, xfs_fileoff_t length);
75
76/* bmap to userspace formatter - copy to user & advance pointer */
77typedef int (*xfs_bmap_format_t)(void **, struct getbmapx *, int *);
78int xfs_getbmap(struct xfs_inode *ip, struct getbmapx *bmv,
79 xfs_bmap_format_t formatter, void *arg);
80
81/* functions in xfs_bmap.c that are only needed by xfs_bmap_util.c */
82void xfs_bmap_del_free(struct xfs_bmap_free *flist,
83 struct xfs_bmap_free_item *prev,
84 struct xfs_bmap_free_item *free);
85int xfs_bmap_extsize_align(struct xfs_mount *mp, struct xfs_bmbt_irec *gotp,
86 struct xfs_bmbt_irec *prevp, xfs_extlen_t extsz,
87 int rt, int eof, int delay, int convert,
88 xfs_fileoff_t *offp, xfs_extlen_t *lenp);
89void xfs_bmap_adjacent(struct xfs_bmalloca *ap);
90int xfs_bmap_last_extent(struct xfs_trans *tp, struct xfs_inode *ip,
91 int whichfork, struct xfs_bmbt_irec *rec,
92 int *is_empty);
93
94xfs_daddr_t xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb);
95
96#endif /* __XFS_BMAP_UTIL_H__ */
diff --git a/fs/xfs/xfs_dfrag.c b/fs/xfs/xfs_dfrag.c
index e36445ceaf80..815dace5e57a 100644
--- a/fs/xfs/xfs_dfrag.c
+++ b/fs/xfs/xfs_dfrag.c
@@ -31,6 +31,7 @@
31#include "xfs_inode.h" 31#include "xfs_inode.h"
32#include "xfs_inode_item.h" 32#include "xfs_inode_item.h"
33#include "xfs_bmap.h" 33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
34#include "xfs_itable.h" 35#include "xfs_itable.h"
35#include "xfs_dfrag.h" 36#include "xfs_dfrag.h"
36#include "xfs_error.h" 37#include "xfs_error.h"
diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
index 36e1cf32f741..e90e123b0085 100644
--- a/fs/xfs/xfs_dquot.c
+++ b/fs/xfs/xfs_dquot.c
@@ -29,6 +29,7 @@
29#include "xfs_bmap_btree.h" 29#include "xfs_bmap_btree.h"
30#include "xfs_inode.h" 30#include "xfs_inode.h"
31#include "xfs_bmap.h" 31#include "xfs_bmap.h"
32#include "xfs_bmap_util.h"
32#include "xfs_rtalloc.h" 33#include "xfs_rtalloc.h"
33#include "xfs_error.h" 34#include "xfs_error.h"
34#include "xfs_itable.h" 35#include "xfs_itable.h"
diff --git a/fs/xfs/xfs_filestream.c b/fs/xfs/xfs_filestream.c
index 9eae097ca223..d0e8890dac86 100644
--- a/fs/xfs/xfs_filestream.c
+++ b/fs/xfs/xfs_filestream.c
@@ -26,6 +26,7 @@
26#include "xfs_sb.h" 26#include "xfs_sb.h"
27#include "xfs_mount.h" 27#include "xfs_mount.h"
28#include "xfs_bmap.h" 28#include "xfs_bmap.h"
29#include "xfs_bmap_util.h"
29#include "xfs_alloc.h" 30#include "xfs_alloc.h"
30#include "xfs_utils.h" 31#include "xfs_utils.h"
31#include "xfs_mru_cache.h" 32#include "xfs_mru_cache.h"
@@ -668,8 +669,8 @@ exit:
668 */ 669 */
669int 670int
670xfs_filestream_new_ag( 671xfs_filestream_new_ag(
671 xfs_bmalloca_t *ap, 672 struct xfs_bmalloca *ap,
672 xfs_agnumber_t *agp) 673 xfs_agnumber_t *agp)
673{ 674{
674 int flags, err; 675 int flags, err;
675 xfs_inode_t *ip, *pip = NULL; 676 xfs_inode_t *ip, *pip = NULL;
diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c
index fcda5b39cc17..1029f5670207 100644
--- a/fs/xfs/xfs_inode.c
+++ b/fs/xfs/xfs_inode.c
@@ -39,6 +39,7 @@
39#include "xfs_alloc.h" 39#include "xfs_alloc.h"
40#include "xfs_ialloc.h" 40#include "xfs_ialloc.h"
41#include "xfs_bmap.h" 41#include "xfs_bmap.h"
42#include "xfs_bmap_util.h"
42#include "xfs_error.h" 43#include "xfs_error.h"
43#include "xfs_utils.h" 44#include "xfs_utils.h"
44#include "xfs_quota.h" 45#include "xfs_quota.h"
diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
index 3124efafb8bf..7c275380d40a 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -33,6 +33,7 @@
33#include "xfs_error.h" 33#include "xfs_error.h"
34#include "xfs_attr.h" 34#include "xfs_attr.h"
35#include "xfs_bmap.h" 35#include "xfs_bmap.h"
36#include "xfs_bmap_util.h"
36#include "xfs_buf_item.h" 37#include "xfs_buf_item.h"
37#include "xfs_utils.h" 38#include "xfs_utils.h"
38#include "xfs_dfrag.h" 39#include "xfs_dfrag.h"
diff --git a/fs/xfs/xfs_iomap.c b/fs/xfs/xfs_iomap.c
index e4577f819f1f..b04a60f66411 100644
--- a/fs/xfs/xfs_iomap.c
+++ b/fs/xfs/xfs_iomap.c
@@ -33,6 +33,7 @@
33#include "xfs_inode_item.h" 33#include "xfs_inode_item.h"
34#include "xfs_btree.h" 34#include "xfs_btree.h"
35#include "xfs_bmap.h" 35#include "xfs_bmap.h"
36#include "xfs_bmap_util.h"
36#include "xfs_rtalloc.h" 37#include "xfs_rtalloc.h"
37#include "xfs_error.h" 38#include "xfs_error.h"
38#include "xfs_itable.h" 39#include "xfs_itable.h"
diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index e44169843c40..17b70f0ccf8a 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -30,6 +30,7 @@
30#include "xfs_dinode.h" 30#include "xfs_dinode.h"
31#include "xfs_inode.h" 31#include "xfs_inode.h"
32#include "xfs_bmap.h" 32#include "xfs_bmap.h"
33#include "xfs_bmap_util.h"
33#include "xfs_rtalloc.h" 34#include "xfs_rtalloc.h"
34#include "xfs_error.h" 35#include "xfs_error.h"
35#include "xfs_itable.h" 36#include "xfs_itable.h"
diff --git a/fs/xfs/xfs_rename.c b/fs/xfs/xfs_rename.c
index 4715c7f65073..68cb7251cb99 100644
--- a/fs/xfs/xfs_rename.c
+++ b/fs/xfs/xfs_rename.c
@@ -31,6 +31,7 @@
31#include "xfs_inode.h" 31#include "xfs_inode.h"
32#include "xfs_inode_item.h" 32#include "xfs_inode_item.h"
33#include "xfs_bmap.h" 33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
34#include "xfs_error.h" 35#include "xfs_error.h"
35#include "xfs_quota.h" 36#include "xfs_quota.h"
36#include "xfs_utils.h" 37#include "xfs_utils.h"
diff --git a/fs/xfs/xfs_rtalloc.c b/fs/xfs/xfs_rtalloc.c
index bb4f7a114c8b..084caa81bf1c 100644
--- a/fs/xfs/xfs_rtalloc.c
+++ b/fs/xfs/xfs_rtalloc.c
@@ -29,6 +29,7 @@
29#include "xfs_inode.h" 29#include "xfs_inode.h"
30#include "xfs_alloc.h" 30#include "xfs_alloc.h"
31#include "xfs_bmap.h" 31#include "xfs_bmap.h"
32#include "xfs_bmap_util.h"
32#include "xfs_rtalloc.h" 33#include "xfs_rtalloc.h"
33#include "xfs_fsops.h" 34#include "xfs_fsops.h"
34#include "xfs_error.h" 35#include "xfs_error.h"
diff --git a/fs/xfs/xfs_symlink.c b/fs/xfs/xfs_symlink.c
index 18f24de4dc2a..1ce2756dfb71 100644
--- a/fs/xfs/xfs_symlink.c
+++ b/fs/xfs/xfs_symlink.c
@@ -35,6 +35,7 @@
35#include "xfs_ialloc.h" 35#include "xfs_ialloc.h"
36#include "xfs_alloc.h" 36#include "xfs_alloc.h"
37#include "xfs_bmap.h" 37#include "xfs_bmap.h"
38#include "xfs_bmap_util.h"
38#include "xfs_error.h" 39#include "xfs_error.h"
39#include "xfs_quota.h" 40#include "xfs_quota.h"
40#include "xfs_utils.h" 41#include "xfs_utils.h"
diff --git a/fs/xfs/xfs_trans_resv.c b/fs/xfs/xfs_trans_resv.c
index 5a1537e2a180..ed6446ecb9a5 100644
--- a/fs/xfs/xfs_trans_resv.c
+++ b/fs/xfs/xfs_trans_resv.c
@@ -37,6 +37,7 @@
37#include "xfs_alloc.h" 37#include "xfs_alloc.h"
38#include "xfs_extent_busy.h" 38#include "xfs_extent_busy.h"
39#include "xfs_bmap.h" 39#include "xfs_bmap.h"
40#include "xfs_bmap_util.h"
40#include "xfs_quota.h" 41#include "xfs_quota.h"
41#include "xfs_qm.h" 42#include "xfs_qm.h"
42#include "xfs_trans_space.h" 43#include "xfs_trans_space.h"
diff --git a/fs/xfs/xfs_vnodeops.c b/fs/xfs/xfs_vnodeops.c
index 221e2e3cec8c..dc81a8d86ba5 100644
--- a/fs/xfs/xfs_vnodeops.c
+++ b/fs/xfs/xfs_vnodeops.c
@@ -38,6 +38,7 @@
38#include "xfs_ialloc.h" 38#include "xfs_ialloc.h"
39#include "xfs_alloc.h" 39#include "xfs_alloc.h"
40#include "xfs_bmap.h" 40#include "xfs_bmap.h"
41#include "xfs_bmap_util.h"
41#include "xfs_acl.h" 42#include "xfs_acl.h"
42#include "xfs_attr.h" 43#include "xfs_attr.h"
43#include "xfs_error.h" 44#include "xfs_error.h"