jfs_discard.c 3.1 KB

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