1 /** 2 * libf2fs.c 3 * 4 * Copyright (c) 2013 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 <stdlib.h> 13 #include <string.h> 14 #include <errno.h> 15 #include <unistd.h> 16 #include <fcntl.h> 17 #include <mntent.h> 18 #include <time.h> 19 #include <sys/stat.h> 20 #include <sys/mount.h> 21 #include <sys/ioctl.h> 22 #include <linux/hdreg.h> 23 24 #include <f2fs_fs.h> 25 26 struct f2fs_configuration config; 27 28 /* 29 * IO interfaces 30 */ 31 int dev_read(void *buf, __u64 offset, size_t len) 32 { 33 if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0) 34 return -1; 35 if (read(config.fd, buf, len) < 0) 36 return -1; 37 return 0; 38 } 39 40 int dev_write(void *buf, __u64 offset, size_t len) 41 { 42 if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0) 43 return -1; 44 if (write(config.fd, buf, len) < 0) 45 return -1; 46 return 0; 47 } 48 49 int dev_fill(void *buf, __u64 offset, size_t len) 50 { 51 /* Only allow fill to zero */ 52 if (*((__u8*)buf)) 53 return -1; 54 if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0) 55 return -1; 56 if (write(config.fd, buf, len) < 0) 57 return -1; 58 return 0; 59 } 60 61 int dev_read_block(void *buf, __u64 blk_addr) 62 { 63 return dev_read(buf, blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE); 64 } 65 66 int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks) 67 { 68 return dev_read(buf, addr * F2FS_BLKSIZE, nr_blks * F2FS_BLKSIZE); 69 } 70 71 void f2fs_finalize_device(struct f2fs_configuration *c) 72 { 73 /* 74 * We should call fsync() to flush out all the dirty pages 75 * in the block device page cache. 76 */ 77 if (fsync(c->fd) < 0) 78 MSG(0, "\tError: Could not conduct fsync!!!\n"); 79 80 if (close(c->fd) < 0) 81 MSG(0, "\tError: Failed to close device file!!!\n"); 82 } 83