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 #ifndef _LARGEFILE_SOURCE 10 #define _LARGEFILE_SOURCE 11 #endif 12 #ifndef _LARGEFILE64_SOURCE 13 #define _LARGEFILE64_SOURCE 14 #endif 15 #ifndef _GNU_SOURCE 16 #define _GNU_SOURCE 17 #endif 18 19 #include <f2fs_fs.h> 20 21 #include <stdio.h> 22 #include <unistd.h> 23 #include <stdlib.h> 24 #ifndef ANDROID_WINDOWS_HOST 25 #include <sys/ioctl.h> 26 #endif 27 #include <sys/stat.h> 28 #include <fcntl.h> 29 30 #ifdef HAVE_LINUX_FS_H 31 #include <linux/fs.h> 32 #endif 33 #ifdef HAVE_LINUX_FALLOC_H 34 #include <linux/falloc.h> 35 #endif 36 37 #ifndef BLKDISCARD 38 #define BLKDISCARD _IO(0x12,119) 39 #endif 40 #ifndef BLKSECDISCARD 41 #define BLKSECDISCARD _IO(0x12,125) 42 #endif 43 44 static int trim_device(int i) 45 { 46 #ifndef ANDROID_WINDOWS_HOST 47 unsigned long long range[2]; 48 struct stat *stat_buf; 49 struct device_info *dev = c.devices + i; 50 u_int64_t bytes = dev->total_sectors * dev->sector_size; 51 int fd = dev->fd; 52 53 stat_buf = malloc(sizeof(struct stat)); 54 if (fstat(fd, stat_buf) < 0 ) { 55 MSG(1, "\tError: Failed to get the device stat!!!\n"); 56 free(stat_buf); 57 return -1; 58 } 59 60 range[0] = 0; 61 range[1] = bytes; 62 63 #if defined(WITH_BLKDISCARD) && defined(BLKDISCARD) 64 MSG(0, "Info: [%s] Discarding device\n", dev->path); 65 if (S_ISREG(stat_buf->st_mode)) { 66 #if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_PUNCH_HOLE) 67 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 68 range[0], range[1]) < 0) { 69 MSG(0, "Info: fallocate(PUNCH_HOLE|KEEP_SIZE) is failed\n"); 70 } 71 #endif 72 free(stat_buf); 73 return 0; 74 } else if (S_ISBLK(stat_buf->st_mode)) { 75 if (dev->zoned_model != F2FS_ZONED_NONE) { 76 free(stat_buf); 77 return f2fs_reset_zones(i); 78 } 79 #ifdef BLKSECDISCARD 80 if (ioctl(fd, BLKSECDISCARD, &range) < 0) { 81 MSG(0, "Info: This device doesn't support BLKSECDISCARD\n"); 82 } else { 83 MSG(0, "Info: Secure Discarded %lu MB\n", 84 (unsigned long)stat_buf->st_size >> 20); 85 free(stat_buf); 86 return 0; 87 } 88 #endif 89 if (ioctl(fd, BLKDISCARD, &range) < 0) { 90 MSG(0, "Info: This device doesn't support BLKDISCARD\n"); 91 } else { 92 MSG(0, "Info: Discarded %llu MB\n", range[1] >> 20); 93 } 94 } else { 95 free(stat_buf); 96 return -1; 97 } 98 #endif 99 free(stat_buf); 100 #endif 101 return 0; 102 } 103 104 int f2fs_trim_devices(void) 105 { 106 int i; 107 108 for (i = 0; i < c.ndevs; i++) 109 if (trim_device(i)) 110 return -1; 111 c.trimmed = 1; 112 return 0; 113 } 114