Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ocfs2: Add ocfs2_trim_fs for SSD trim support.

Add ocfs2_trim_fs to support trimming freed clusters in the
volume. A range will be given and all the freed clusters greater
than minlen will be discarded to the block layer.

Signed-off-by: Tao Ma <boyu.mt@taobao.com>
Signed-off-by: Joel Becker <jlbec@evilplan.org>

authored by

Tao Ma and committed by
Joel Becker
e80de36d 69a60c4d

+160
+159
fs/ocfs2/alloc.c
··· 29 29 #include <linux/highmem.h> 30 30 #include <linux/swap.h> 31 31 #include <linux/quotaops.h> 32 + #include <linux/blkdev.h> 32 33 33 34 #include <cluster/masklog.h> 34 35 ··· 7182 7181 out_commit: 7183 7182 ocfs2_commit_trans(osb, handle); 7184 7183 7184 + out: 7185 + return ret; 7186 + } 7187 + 7188 + static int ocfs2_trim_extent(struct super_block *sb, 7189 + struct ocfs2_group_desc *gd, 7190 + u32 start, u32 count) 7191 + { 7192 + u64 discard, bcount; 7193 + 7194 + bcount = ocfs2_clusters_to_blocks(sb, count); 7195 + discard = le64_to_cpu(gd->bg_blkno) + 7196 + ocfs2_clusters_to_blocks(sb, start); 7197 + 7198 + return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0); 7199 + } 7200 + 7201 + static int ocfs2_trim_group(struct super_block *sb, 7202 + struct ocfs2_group_desc *gd, 7203 + u32 start, u32 max, u32 minbits) 7204 + { 7205 + int ret = 0, count = 0, next; 7206 + void *bitmap = gd->bg_bitmap; 7207 + 7208 + if (le16_to_cpu(gd->bg_free_bits_count) < minbits) 7209 + return 0; 7210 + 7211 + while (start < max) { 7212 + start = ocfs2_find_next_zero_bit(bitmap, max, start); 7213 + if (start >= max) 7214 + break; 7215 + next = ocfs2_find_next_bit(bitmap, max, start); 7216 + 7217 + if ((next - start) >= minbits) { 7218 + ret = ocfs2_trim_extent(sb, gd, 7219 + start, next - start); 7220 + if (ret < 0) { 7221 + mlog_errno(ret); 7222 + break; 7223 + } 7224 + count += next - start; 7225 + } 7226 + start = next + 1; 7227 + 7228 + if (fatal_signal_pending(current)) { 7229 + count = -ERESTARTSYS; 7230 + break; 7231 + } 7232 + 7233 + if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits) 7234 + break; 7235 + } 7236 + 7237 + if (ret < 0) 7238 + count = ret; 7239 + 7240 + return count; 7241 + } 7242 + 7243 + int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range) 7244 + { 7245 + struct ocfs2_super *osb = OCFS2_SB(sb); 7246 + u64 start, len, trimmed, first_group, last_group, group; 7247 + int ret, cnt; 7248 + u32 first_bit, last_bit, minlen; 7249 + struct buffer_head *main_bm_bh = NULL; 7250 + struct inode *main_bm_inode = NULL; 7251 + struct buffer_head *gd_bh = NULL; 7252 + struct ocfs2_dinode *main_bm; 7253 + struct ocfs2_group_desc *gd = NULL; 7254 + 7255 + start = range->start >> osb->s_clustersize_bits; 7256 + len = range->len >> osb->s_clustersize_bits; 7257 + minlen = range->minlen >> osb->s_clustersize_bits; 7258 + trimmed = 0; 7259 + 7260 + if (!len) { 7261 + range->len = 0; 7262 + return 0; 7263 + } 7264 + 7265 + if (minlen >= osb->bitmap_cpg) 7266 + return -EINVAL; 7267 + 7268 + main_bm_inode = ocfs2_get_system_file_inode(osb, 7269 + GLOBAL_BITMAP_SYSTEM_INODE, 7270 + OCFS2_INVALID_SLOT); 7271 + if (!main_bm_inode) { 7272 + ret = -EIO; 7273 + mlog_errno(ret); 7274 + goto out; 7275 + } 7276 + 7277 + mutex_lock(&main_bm_inode->i_mutex); 7278 + 7279 + ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0); 7280 + if (ret < 0) { 7281 + mlog_errno(ret); 7282 + goto out_mutex; 7283 + } 7284 + main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data; 7285 + 7286 + if (start >= le32_to_cpu(main_bm->i_clusters)) { 7287 + ret = -EINVAL; 7288 + goto out_unlock; 7289 + } 7290 + 7291 + if (start + len > le32_to_cpu(main_bm->i_clusters)) 7292 + len = le32_to_cpu(main_bm->i_clusters) - start; 7293 + 7294 + /* Determine first and last group to examine based on start and len */ 7295 + first_group = ocfs2_which_cluster_group(main_bm_inode, start); 7296 + if (first_group == osb->first_cluster_group_blkno) 7297 + first_bit = start; 7298 + else 7299 + first_bit = start - ocfs2_blocks_to_clusters(sb, first_group); 7300 + last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1); 7301 + last_bit = osb->bitmap_cpg; 7302 + 7303 + for (group = first_group; group <= last_group;) { 7304 + if (first_bit + len >= osb->bitmap_cpg) 7305 + last_bit = osb->bitmap_cpg; 7306 + else 7307 + last_bit = first_bit + len; 7308 + 7309 + ret = ocfs2_read_group_descriptor(main_bm_inode, 7310 + main_bm, group, 7311 + &gd_bh); 7312 + if (ret < 0) { 7313 + mlog_errno(ret); 7314 + break; 7315 + } 7316 + 7317 + gd = (struct ocfs2_group_desc *)gd_bh->b_data; 7318 + cnt = ocfs2_trim_group(sb, gd, first_bit, last_bit, minlen); 7319 + brelse(gd_bh); 7320 + gd_bh = NULL; 7321 + if (cnt < 0) { 7322 + ret = cnt; 7323 + mlog_errno(ret); 7324 + break; 7325 + } 7326 + 7327 + trimmed += cnt; 7328 + len -= osb->bitmap_cpg - first_bit; 7329 + first_bit = 0; 7330 + if (group == osb->first_cluster_group_blkno) 7331 + group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); 7332 + else 7333 + group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); 7334 + } 7335 + range->len = trimmed * sb->s_blocksize; 7336 + out_unlock: 7337 + ocfs2_inode_unlock(main_bm_inode, 0); 7338 + brelse(main_bm_bh); 7339 + out_mutex: 7340 + mutex_unlock(&main_bm_inode->i_mutex); 7341 + iput(main_bm_inode); 7185 7342 out: 7186 7343 return ret; 7187 7344 }
+1
fs/ocfs2/alloc.h
··· 239 239 struct buffer_head **leaf_bh); 240 240 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster); 241 241 242 + int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range); 242 243 /* 243 244 * Helper function to look at the # of clusters in an extent record. 244 245 */