1 /** 2 * f2fs_format_utils.c 3 * 4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * Dual licensed under the GPL or LGPL version 2 licenses. 8 */ 9 #define _LARGEFILE64_SOURCE 10 11 #include <stdio.h> 12 #include <unistd.h> 13 #include <sys/ioctl.h> 14 #include <sys/stat.h> 15 16 #include "f2fs_fs.h" 17 f2fs_trim_device()18int f2fs_trim_device() 19 { 20 unsigned long long range[2]; 21 struct stat stat_buf; 22 23 if (!config.trim) 24 return 0; 25 26 range[0] = 0; 27 range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE; 28 29 if (fstat(config.fd, &stat_buf) < 0 ) { 30 MSG(1, "\tError: Failed to get the device stat!!!\n"); 31 return -1; 32 } 33 34 #if defined(BLKDISCARD) 35 MSG(0, "Info: Discarding device\n"); 36 if (S_ISREG(stat_buf.st_mode)) 37 return 0; 38 else if (S_ISBLK(stat_buf.st_mode)) { 39 if (ioctl(config.fd, BLKDISCARD, &range) < 0) { 40 MSG(0, "Info: This device doesn't support TRIM\n"); 41 } else { 42 MSG(0, "Info: Discarded %lu sectors\n", 43 config.total_sectors); 44 } 45 } else 46 return -1; 47 #endif 48 return 0; 49 } 50 51