1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBRARIES_NACL_IO_FUSE_H_ 6 #define LIBRARIES_NACL_IO_FUSE_H_ 7 8 #include "osinttypes.h" 9 #include "ostypes.h" 10 11 // These interfaces are copied from the FUSE library. 12 // 13 // FUSE has two interfaces that can be implemented: low-level and high-level. 14 // In nacl_io, we only support the high-level interface. 15 // 16 // See http://fuse.sourceforge.net/ for more information. 17 18 // This struct is typically passed to functions that would normally use return 19 // or receive an fd; that is, operations to open/create a node, or operations 20 // that act on an already opened node. 21 struct fuse_file_info { 22 // This is filled with the flags passed to open() 23 int flags; 24 // Deprecated in FUSE. Use fh instead. 25 unsigned long fh_old; 26 int writepage; 27 // Currently unsupported 28 unsigned int direct_io : 1; 29 // Currently unsupported 30 unsigned int keep_cache : 1; 31 // Currently unsupported 32 unsigned int flush : 1; 33 // Currently unsupported 34 unsigned int nonseekable : 1; 35 // Currently unsupported 36 unsigned int padding : 27; 37 // This value is not used by nacl_io. It can be filled by the developer when 38 // open() is called, and reused for subsequent calls on the same node. 39 uint64_t fh; 40 // Currently unsupported 41 uint64_t lock_owner; 42 // Currently unsupported 43 uint32_t poll_events; 44 }; 45 46 // A dummy structure that currently exists only to match the FUSE interface. 47 struct fuse_conn_info {}; 48 49 // A function of this type will be passed to readdir (see below). The developer 50 // should call this function once for each directory entry. 51 // 52 // See the documentation for readdir() below for more information on how to use 53 // this function. 54 typedef int (*fuse_fill_dir_t)(void* buf, 55 const char* name, 56 const struct stat* stbuf, 57 off_t off); 58 59 // This structure defines the interface to create a user mount. Pass this to 60 // nacl_io_register_mount_type(). (see nacl_io.h) 61 // 62 // Example: 63 // 64 // struct fuse_operations g_my_fuse_operations = { ... }; 65 // ... 66 // nacl_io_register_mount_type("myfusefs", &g_my_fuse_operations); 67 // ... 68 // mount("", "/mnt/fuse", "myfusefs", 0, NULL); 69 // 70 // It is not necessary to implement every function -- nacl_io will first check 71 // if the function pointer is NULL before calling it. If it is NULL and 72 // required by the current operation, the call will fail and return ENOSYS in 73 // errno. 74 // 75 // Except where specified below, each function should return one of the 76 // following values: 77 // == 0: operation completed successfully. 78 // < 0: operation failed. The error is a negative errno. e.g. -EACCES, -EPERM, 79 // etc. The sign will be flipped when the error is actually set. 80 // 81 // Some functions (e.g. read, write) also return a positive count, which is the 82 // number of bytes read/written. 83 // 84 struct fuse_operations { 85 // Currently unsupported 86 unsigned int flag_nopath : 1; 87 unsigned int flag_reserved : 31; 88 89 // Called when a mount of this type is initialized. 90 void* (*init)(struct fuse_conn_info* conn); 91 // Called when a mount of this type is unmounted. 92 void (*destroy)(void*); 93 // Called by access() 94 int (*access)(const char* path, int mode); 95 // Called when O_CREAT is passed to open() 96 int (*create)(const char* path, mode_t mode, struct fuse_file_info*); 97 // Called by stat()/fstat(). If this function pointer is non-NULL, it is 98 // called, otherwise fuse_operations.getattr will be called. 99 int (*fgetattr)(const char* path, struct stat*, struct fuse_file_info*); 100 // Called by fsync(). The datasync paramater is not currently supported. 101 int (*fsync)(const char* path, int datasync, struct fuse_file_info*); 102 // Called by ftruncate() 103 int (*ftruncate)(const char* path, off_t, struct fuse_file_info*); 104 // Called by stat()/fstat(), but only when fuse_operations.fgetattr is NULL. 105 // Also called by open() to determine if the path is a directory or a regular 106 // file. 107 int (*getattr)(const char* path, struct stat*); 108 // Called by mkdir() 109 int (*mkdir)(const char* path, mode_t); 110 // Called when O_CREAT is passed to open(), but only if fuse_operations.create 111 // is non-NULL. 112 int (*mknod)(const char* path, mode_t, dev_t); 113 // Called by open() 114 int (*open)(const char* path, struct fuse_file_info*); 115 // Called by getdents(), which is called by the more standard functions 116 // opendir()/readdir(). 117 int (*opendir)(const char* path, struct fuse_file_info*); 118 // Called by read(). Note that FUSE specifies that all reads will fill the 119 // entire requested buffer. If this function returns less than that, the 120 // remainder of the buffer is zeroed. 121 int (*read)(const char* path, char* buf, size_t count, off_t, 122 struct fuse_file_info*); 123 // Called by getdents(), which is called by the more standard function 124 // readdir(). 125 // 126 // NOTE: it is the responsibility of this function to add the "." and ".." 127 // entries. 128 // 129 // This function can be implemented one of two ways: 130 // 1) Ignore the offset, and always write every entry in a given directory. 131 // In this case, you should always call filler() with an offset of 0. You 132 // can ignore the return value of the filler. 133 // 134 // int my_readdir(const char* path, void* buf, fuse_fill_dir_t filler, 135 // off_t offset, struct fuse_file_info*) { 136 // ... 137 // filler(buf, ".", NULL, 0); 138 // filler(buf, "..", NULL, 0); 139 // filler(buf, "file1", &file1stat, 0); 140 // filler(buf, "file2", &file2stat, 0); 141 // return 0; 142 // } 143 // 144 // 2) Only write entries starting from offset. Always pass the correct offset 145 // to the filler function. When the filler function returns 1, the buffer 146 // is full so you can exit readdir. 147 // 148 // int my_readdir(const char* path, void* buf, fuse_fill_dir_t filler, 149 // off_t offset, struct fuse_file_info*) { 150 // ... 151 // size_t kNumEntries = 4; 152 // const char* my_entries[] = { ".", "..", "file1", "file2" }; 153 // int entry_index = offset / sizeof(dirent); 154 // offset = entry_index * sizeof(dirent); 155 // while (entry_index < kNumEntries) { 156 // int result = filler(buf, my_entries[entry_index], NULL, offset); 157 // if (filler == 1) { 158 // // buffer filled, we're done. 159 // return 0; 160 // } 161 // offset += sizeof(dirent); 162 // entry_index++; 163 // } 164 // 165 // // No more entries, we're done. 166 // return 0; 167 // } 168 // 169 int (*readdir)(const char* path, void* buf, fuse_fill_dir_t filldir, off_t, 170 struct fuse_file_info*); 171 // Called when the last reference to this node is released. This is only 172 // called for regular files. For directories, fuse_operations.releasedir is 173 // called instead. 174 int (*release)(const char* path, struct fuse_file_info*); 175 // Called when the last reference to this node is released. This is only 176 // called for directories. For regular files, fuse_operations.release is 177 // called instead. 178 int (*releasedir)(const char* path, struct fuse_file_info*); 179 // Called by rename() 180 int (*rename)(const char* path, const char* new_path); 181 // Called by rmdir() 182 int (*rmdir)(const char* path); 183 // Called by truncate(), as well as open() when O_TRUNC is passed. 184 int (*truncate)(const char* path, off_t); 185 // Called by unlink() 186 int (*unlink)(const char* path); 187 // Called by write(). Note that FUSE specifies that a write should always 188 // return the full count, unless an error occurs. 189 int (*write)(const char* path, const char* buf, size_t count, off_t, 190 struct fuse_file_info*); 191 192 // The following functions are not currently called by the nacl_io 193 // implementation of FUSE. 194 int (*bmap)(const char*, size_t blocksize, uint64_t* idx); 195 int (*chmod)(const char*, mode_t); 196 int (*chown)(const char*, uid_t, gid_t); 197 int (*fallocate)(const char*, int, off_t, off_t, struct fuse_file_info*); 198 int (*flock)(const char*, struct fuse_file_info*, int op); 199 int (*flush)(const char*, struct fuse_file_info*); 200 int (*fsyncdir)(const char*, int, struct fuse_file_info*); 201 int (*getxattr)(const char*, const char*, char*, size_t); 202 int (*ioctl)(const char*, int cmd, void* arg, struct fuse_file_info*, 203 unsigned int flags, void* data); 204 int (*link)(const char*, const char*); 205 int (*listxattr)(const char*, char*, size_t); 206 int (*lock)(const char*, struct fuse_file_info*, int cmd, struct flock*); 207 int (*poll)(const char*, struct fuse_file_info*, struct fuse_pollhandle* ph, 208 unsigned* reventsp); 209 int (*read_buf)(const char*, struct fuse_bufvec** bufp, size_t size, 210 off_t off, struct fuse_file_info*); 211 int (*readlink)(const char*, char*, size_t); 212 int (*removexattr)(const char*, const char*); 213 int (*setxattr)(const char*, const char*, const char*, size_t, int); 214 int (*statfs)(const char*, struct statvfs*); 215 int (*symlink)(const char*, const char*); 216 int (*utimens)(const char*, const struct timespec tv[2]); 217 int (*write_buf)(const char*, struct fuse_bufvec* buf, off_t off, 218 struct fuse_file_info*); 219 }; 220 221 #endif // LIBRARIES_NACL_IO_FUSE_H_ 222