jfs_discard.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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" \
  48. "(%p, %llu, %llu, GFP_NOFS, 0) = %d => failed!\n",
  49. sb, (unsigned long long)blkno,
  50. (unsigned long long)nblocks, r);
  51. }
  52. jfs_info("JFS: sb_issue_discard" \
  53. "(%p, %llu, %llu, GFP_NOFS, 0) = %d\n",
  54. sb, (unsigned long long)blkno,
  55. (unsigned long long)nblocks, r);
  56. return;
  57. }
  58. /*
  59. * NAME: jfs_ioc_trim()
  60. *
  61. * FUNCTION: attempt to discard (TRIM) all free blocks from the
  62. * filesystem.
  63. *
  64. * PARAMETERS:
  65. * ip - pointer to in-core inode;
  66. * range - the range, given by user space
  67. *
  68. * RETURN VALUES:
  69. * 0 - success
  70. * -EIO - i/o error
  71. */
  72. int jfs_ioc_trim(struct inode *ip, struct fstrim_range *range)
  73. {
  74. struct inode *ipbmap = JFS_SBI(ip->i_sb)->ipbmap;
  75. struct bmap *bmp = JFS_SBI(ip->i_sb)->bmap;
  76. struct super_block *sb = ipbmap->i_sb;
  77. int agno, agno_end;
  78. u64 start, end, minlen;
  79. u64 trimmed = 0;
  80. /**
  81. * convert byte values to block size of filesystem:
  82. * start: First Byte to trim
  83. * len: number of Bytes to trim from start
  84. * minlen: minimum extent length in Bytes
  85. */
  86. start = range->start >> sb->s_blocksize_bits;
  87. end = start + (range->len >> sb->s_blocksize_bits) - 1;
  88. minlen = range->minlen >> sb->s_blocksize_bits;
  89. if (minlen == 0)
  90. minlen = 1;
  91. if (minlen > bmp->db_agsize ||
  92. start >= bmp->db_mapsize ||
  93. range->len < sb->s_blocksize)
  94. return -EINVAL;
  95. if (end >= bmp->db_mapsize)
  96. end = bmp->db_mapsize - 1;
  97. /**
  98. * we trim all ag's within the range
  99. */
  100. agno = BLKTOAG(start, JFS_SBI(ip->i_sb));
  101. agno_end = BLKTOAG(end, JFS_SBI(ip->i_sb));
  102. while (agno <= agno_end) {
  103. trimmed += dbDiscardAG(ip, agno, minlen);
  104. agno++;
  105. }
  106. range->len = trimmed << sb->s_blocksize_bits;
  107. return 0;
  108. }