Home | History | Annotate | Download | only in lib
      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_write_block(void *buf, __u64 blk_addr)
     50 {
     51 	return dev_write(buf, blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE);
     52 }
     53 
     54 int dev_write_dump(void *buf, __u64 offset, size_t len)
     55 {
     56 	if (lseek64(config.dump_fd, (off64_t)offset, SEEK_SET) < 0)
     57 		return -1;
     58 	if (write(config.dump_fd, buf, len) < 0)
     59 		return -1;
     60 	return 0;
     61 }
     62 
     63 int dev_fill(void *buf, __u64 offset, size_t len)
     64 {
     65 	/* Only allow fill to zero */
     66 	if (*((__u8*)buf))
     67 		return -1;
     68 	if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
     69 		return -1;
     70 	if (write(config.fd, buf, len) < 0)
     71 		return -1;
     72 	return 0;
     73 }
     74 
     75 int dev_read_block(void *buf, __u64 blk_addr)
     76 {
     77 	return dev_read(buf, blk_addr * F2FS_BLKSIZE, F2FS_BLKSIZE);
     78 }
     79 
     80 int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks)
     81 {
     82 	return dev_read(buf, addr * F2FS_BLKSIZE, nr_blks * F2FS_BLKSIZE);
     83 }
     84 
     85 void f2fs_finalize_device(struct f2fs_configuration *c)
     86 {
     87 	/*
     88 	 * We should call fsync() to flush out all the dirty pages
     89 	 * in the block device page cache.
     90 	 */
     91 	if (fsync(c->fd) < 0)
     92 		MSG(0, "\tError: Could not conduct fsync!!!\n");
     93 
     94 	if (close(c->fd) < 0)
     95 		MSG(0, "\tError: Failed to close device file!!!\n");
     96 }
     97