Linux 3.12.28
[linux/fpc-iii.git] / fs / jfs / jfs_discard.c
blobdfcd5030455933bcfbc795c251bc3f0b6fa8e36a
1 /*
2 * Copyright (C) Tino Reichardt, 2012
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/fs.h>
20 #include <linux/slab.h>
21 #include <linux/blkdev.h>
23 #include "jfs_incore.h"
24 #include "jfs_superblock.h"
25 #include "jfs_discard.h"
26 #include "jfs_dmap.h"
27 #include "jfs_debug.h"
31 * NAME: jfs_issue_discard()
33 * FUNCTION: TRIM the specified block range on device, if supported
35 * PARAMETERS:
36 * ip - pointer to in-core inode
37 * blkno - starting block number to be trimmed (0..N)
38 * nblocks - number of blocks to be trimmed
40 * RETURN VALUES:
41 * none
43 * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
45 void jfs_issue_discard(struct inode *ip, u64 blkno, u64 nblocks)
47 struct super_block *sb = ip->i_sb;
48 int r = 0;
50 r = sb_issue_discard(sb, blkno, nblocks, GFP_NOFS, 0);
51 if (unlikely(r != 0)) {
52 jfs_err("JFS: sb_issue_discard" \
53 "(%p, %llu, %llu, GFP_NOFS, 0) = %d => failed!\n",
54 sb, (unsigned long long)blkno,
55 (unsigned long long)nblocks, r);
58 jfs_info("JFS: sb_issue_discard" \
59 "(%p, %llu, %llu, GFP_NOFS, 0) = %d\n",
60 sb, (unsigned long long)blkno,
61 (unsigned long long)nblocks, r);
63 return;
67 * NAME: jfs_ioc_trim()
69 * FUNCTION: attempt to discard (TRIM) all free blocks from the
70 * filesystem.
72 * PARAMETERS:
73 * ip - pointer to in-core inode;
74 * range - the range, given by user space
76 * RETURN VALUES:
77 * 0 - success
78 * -EIO - i/o error
80 int jfs_ioc_trim(struct inode *ip, struct fstrim_range *range)
82 struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
83 struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
84 struct super_block *sb = ipbmap->i_sb;
85 int agno, agno_end;
86 u64 start, end, minlen;
87 u64 trimmed = 0;
89 /**
90 * convert byte values to block size of filesystem:
91 * start: First Byte to trim
92 * len: number of Bytes to trim from start
93 * minlen: minimum extent length in Bytes
95 start = range->start >> sb->s_blocksize_bits;
96 end = start + (range->len >> sb->s_blocksize_bits) - 1;
97 minlen = range->minlen >> sb->s_blocksize_bits;
98 if (minlen == 0)
99 minlen = 1;
101 if (minlen > bmp->db_agsize ||
102 start >= bmp->db_mapsize ||
103 range->len < sb->s_blocksize)
104 return -EINVAL;
106 if (end >= bmp->db_mapsize)
107 end = bmp->db_mapsize - 1;
110 * we trim all ag's within the range
112 agno = BLKTOAG(start, JFS_SBI(ip->i_sb));
113 agno_end = BLKTOAG(end, JFS_SBI(ip->i_sb));
114 while (agno <= agno_end) {
115 trimmed += dbDiscardAG(ip, agno, minlen);
116 agno++;
118 range->len = trimmed << sb->s_blocksize_bits;
120 return 0;