^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) Tino Reichardt, 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "jfs_incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "jfs_superblock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "jfs_discard.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "jfs_dmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "jfs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * NAME: jfs_issue_discard()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * FUNCTION: TRIM the specified block range on device, if supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * PARAMETERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * ip - pointer to in-core inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * blkno - starting block number to be trimmed (0..N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * nblocks - number of blocks to be trimmed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * RETURN VALUES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * serialization: IREAD_LOCK(ipbmap) held on entry/exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void jfs_issue_discard(struct inode *ip, u64 blkno, u64 nblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct super_block *sb = ip->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) r = sb_issue_discard(sb, blkno, nblocks, GFP_NOFS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (unlikely(r != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) jfs_err("JFS: sb_issue_discard(%p, %llu, %llu, GFP_NOFS, 0) = %d => failed!",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) sb, (unsigned long long)blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) (unsigned long long)nblocks, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) jfs_info("JFS: sb_issue_discard(%p, %llu, %llu, GFP_NOFS, 0) = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) sb, (unsigned long long)blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) (unsigned long long)nblocks, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * NAME: jfs_ioc_trim()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * FUNCTION: attempt to discard (TRIM) all free blocks from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * PARAMETERS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * ip - pointer to in-core inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * range - the range, given by user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * RETURN VALUES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * 0 - success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * -EIO - i/o error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int jfs_ioc_trim(struct inode *ip, struct fstrim_range *range)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct super_block *sb = ipbmap->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int agno, agno_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u64 start, end, minlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u64 trimmed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * convert byte values to block size of filesystem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * start: First Byte to trim
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * len: number of Bytes to trim from start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * minlen: minimum extent length in Bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) start = range->start >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) end = start + (range->len >> sb->s_blocksize_bits) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) minlen = range->minlen >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (minlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) minlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (minlen > bmp->db_agsize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) start >= bmp->db_mapsize ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) range->len < sb->s_blocksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (end >= bmp->db_mapsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) end = bmp->db_mapsize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * we trim all ag's within the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) agno = BLKTOAG(start, JFS_SBI(ip->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) agno_end = BLKTOAG(end, JFS_SBI(ip->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while (agno <= agno_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) trimmed += dbDiscardAG(ip, agno, minlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) agno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) range->len = trimmed << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }