nfs-ganesha 1.4
|
00001 00002 #include <utime.h> 00003 #include <fcntl.h> 00004 #include <sys/types.h> 00005 #include <sys/stat.h> 00006 #include <sys/statvfs.h> 00007 #include <sys/uio.h> 00008 #include <stdint.h> 00009 00010 /* ---------------------------------------------------------------------------*/ 00011 00012 /* 00013 * First define FUSE-like types. Their definition is 00014 * compliant with FUSE use (include FUSE usual fields). 00015 */ 00016 00017 typedef unsigned long ganefuse_ino_t; 00018 typedef struct ganefuse_req *ganefuse_req_t; 00019 struct ganefuse_session; 00020 struct ganefuse_chan; 00021 00022 struct ganefuse_entry_param 00023 { 00024 ganefuse_ino_t ino; 00025 unsigned long generation; 00026 struct stat attr; 00027 double attr_timeout; 00028 double entry_timeout; 00029 00030 }; 00031 00032 struct ganefuse_ctx 00033 { 00034 uid_t uid; 00035 gid_t gid; 00036 pid_t pid; 00037 00038 }; 00039 00040 /* 'to_set' flags in setattr */ 00041 #define GANEFUSE_SET_ATTR_MODE (1 << 0) 00042 #define GANEFUSE_SET_ATTR_UID (1 << 1) 00043 #define GANEFUSE_SET_ATTR_GID (1 << 2) 00044 #define GANEFUSE_SET_ATTR_SIZE (1 << 3) 00045 #define GANEFUSE_SET_ATTR_ATIME (1 << 4) 00046 #define GANEFUSE_SET_ATTR_MTIME (1 << 5) 00047 00048 struct ganefuse_file_info 00049 { 00050 int flags; 00051 unsigned long fh_old; 00052 int writepage; 00053 unsigned int direct_io:1; 00054 unsigned int keep_cache:1; 00055 unsigned int flush:1; 00056 unsigned int padding:29; 00057 uint64_t fh; 00058 uint64_t lock_owner; 00059 00060 }; 00061 00062 struct ganefuse_conn_info 00063 { 00064 unsigned proto_major; 00065 unsigned proto_minor; 00066 unsigned async_read; 00067 unsigned max_write; 00068 unsigned max_readahead; 00069 unsigned reserved[27]; 00070 00071 }; 00072 00073 struct ganefuse_lowlevel_ops 00074 { 00075 void (*init) (void *userdata, struct ganefuse_conn_info * conn); 00076 void (*destroy) (void *userdata); 00077 void (*lookup) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00078 void (*forget) (ganefuse_req_t req, ganefuse_ino_t ino, unsigned long nlookup); 00079 void (*getattr) (ganefuse_req_t req, ganefuse_ino_t ino, 00080 struct ganefuse_file_info * fi); 00081 void (*setattr) (ganefuse_req_t req, ganefuse_ino_t ino, struct stat * attr, int to_set, 00082 struct ganefuse_file_info * fi); 00083 void (*readlink) (ganefuse_req_t req, ganefuse_ino_t ino); 00084 void (*mknod) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00085 mode_t mode, dev_t rdev); 00086 void (*mkdir) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00087 mode_t mode); 00088 void (*unlink) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00089 void (*rmdir) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00090 void (*symlink) (ganefuse_req_t req, const char *link, ganefuse_ino_t parent, 00091 const char *name); 00092 void (*rename) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00093 ganefuse_ino_t newparent, const char *newname); 00094 void (*link) (ganefuse_req_t req, ganefuse_ino_t ino, ganefuse_ino_t newparent, 00095 const char *newname); 00096 void (*open) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi); 00097 void (*read) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size, off_t off, 00098 struct ganefuse_file_info * fi); 00099 void (*write) (ganefuse_req_t req, ganefuse_ino_t ino, const char *buf, 00100 size_t size, off_t off, struct ganefuse_file_info * fi); 00101 void (*flush) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi); 00102 void (*release) (ganefuse_req_t req, ganefuse_ino_t ino, 00103 struct ganefuse_file_info * fi); 00104 void (*fsync) (ganefuse_req_t req, ganefuse_ino_t ino, int datasync, 00105 struct ganefuse_file_info * fi); 00106 void (*opendir) (ganefuse_req_t req, ganefuse_ino_t ino, 00107 struct ganefuse_file_info * fi); 00108 void (*readdir) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size, off_t off, 00109 struct ganefuse_file_info * fi); 00110 void (*releasedir) (ganefuse_req_t req, ganefuse_ino_t ino, 00111 struct ganefuse_file_info * fi); 00112 00113 void (*fsyncdir) (ganefuse_req_t req, ganefuse_ino_t ino, int datasync, 00114 struct ganefuse_file_info * fi); 00115 void (*statfs) (ganefuse_req_t req, ganefuse_ino_t ino); 00116 void (*setxattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name, 00117 const char *value, size_t size, int flags); 00118 void (*getxattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name, 00119 size_t size); 00120 void (*listxattr) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size); 00121 void (*removexattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name); 00122 void (*access) (ganefuse_req_t req, ganefuse_ino_t ino, int mask); 00123 void (*create) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00124 mode_t mode, struct ganefuse_file_info * fi); 00125 void (*getlk) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi, 00126 struct flock * lock); 00127 void (*setlk) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi, 00128 struct flock * lock, int sleep); 00129 void (*bmap) (ganefuse_req_t req, ganefuse_ino_t ino, size_t blocksize, uint64_t idx); 00130 }; 00131 00132 struct ganefuse_lowlevel_ops25 00133 { 00134 void (*init) (void *userdata, struct ganefuse_conn_info * conn); 00135 void (*destroy) (void *userdata); 00136 void (*lookup) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00137 void (*forget) (ganefuse_req_t req, ganefuse_ino_t ino, unsigned long nlookup); 00138 void (*getattr) (ganefuse_req_t req, ganefuse_ino_t ino, 00139 struct ganefuse_file_info * fi); 00140 void (*setattr) (ganefuse_req_t req, ganefuse_ino_t ino, struct stat * attr, int to_set, 00141 struct ganefuse_file_info * fi); 00142 void (*readlink) (ganefuse_req_t req, ganefuse_ino_t ino); 00143 void (*mknod) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00144 mode_t mode, dev_t rdev); 00145 void (*mkdir) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00146 mode_t mode); 00147 void (*unlink) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00148 void (*rmdir) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name); 00149 void (*symlink) (ganefuse_req_t req, const char *link, ganefuse_ino_t parent, 00150 const char *name); 00151 void (*rename) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00152 ganefuse_ino_t newparent, const char *newname); 00153 void (*link) (ganefuse_req_t req, ganefuse_ino_t ino, ganefuse_ino_t newparent, 00154 const char *newname); 00155 void (*open) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi); 00156 void (*read) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size, off_t off, 00157 struct ganefuse_file_info * fi); 00158 void (*write) (ganefuse_req_t req, ganefuse_ino_t ino, const char *buf, 00159 size_t size, off_t off, struct ganefuse_file_info * fi); 00160 void (*flush) (ganefuse_req_t req, ganefuse_ino_t ino, struct ganefuse_file_info * fi); 00161 void (*release) (ganefuse_req_t req, ganefuse_ino_t ino, 00162 struct ganefuse_file_info * fi); 00163 void (*fsync) (ganefuse_req_t req, ganefuse_ino_t ino, int datasync, 00164 struct ganefuse_file_info * fi); 00165 void (*opendir) (ganefuse_req_t req, ganefuse_ino_t ino, 00166 struct ganefuse_file_info * fi); 00167 void (*readdir) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size, off_t off, 00168 struct ganefuse_file_info * fi); 00169 void (*releasedir) (ganefuse_req_t req, ganefuse_ino_t ino, 00170 struct ganefuse_file_info * fi); 00171 00172 void (*fsyncdir) (ganefuse_req_t req, ganefuse_ino_t ino, int datasync, 00173 struct ganefuse_file_info * fi); 00174 void (*statfs) (ganefuse_req_t req); 00175 void (*setxattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name, 00176 const char *value, size_t size, int flags); 00177 void (*getxattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name, 00178 size_t size); 00179 void (*listxattr) (ganefuse_req_t req, ganefuse_ino_t ino, size_t size); 00180 void (*removexattr) (ganefuse_req_t req, ganefuse_ino_t ino, const char *name); 00181 void (*access) (ganefuse_req_t req, ganefuse_ino_t ino, int mask); 00182 void (*create) (ganefuse_req_t req, ganefuse_ino_t parent, const char *name, 00183 mode_t mode, struct ganefuse_file_info * fi); 00184 }; 00185 00186 struct ganefuse_args 00187 { 00188 int argc; 00189 char **argv; 00190 int allocated; 00191 }; 00192 00193 struct ganefuse_opt 00194 { 00195 const char *templ; 00196 unsigned long offset; 00197 int value; 00198 }; 00199 00200 /* function/macro definitions */ 00201 00202 #define GANEFUSE_ARGS_INIT(_argc_, _argv_) { _argc_, _argv_, 0 } 00203 00204 int ganefuse_parse_cmdline(struct ganefuse_args *args, char **mountpoint, 00205 int *multithreaded, int *foreground); 00206 00207 typedef int (*ganefuse_opt_proc_t) (void *data, const char *arg, int key, 00208 struct ganefuse_args * outargs); 00209 int ganefuse_opt_parse(struct ganefuse_args *args, void *data, 00210 const struct ganefuse_opt opts[], ganefuse_opt_proc_t proc); 00211 int ganefuse_opt_add_opt(char **opts, const char *opt); 00212 int ganefuse_opt_add_arg(struct ganefuse_args *args, const char *arg); 00213 int ganefuse_opt_insert_arg(struct ganefuse_args *args, int pos, const char *arg); 00214 void ganefuse_opt_free_args(struct ganefuse_args *args); 00215 int ganefuse_opt_match(const struct ganefuse_opt opts[], const char *opt); 00216 00217 /* reply functions */ 00218 00219 int ganefuse_reply_err(ganefuse_req_t req, int err); 00220 void ganefuse_reply_none(ganefuse_req_t req); 00221 int ganefuse_reply_entry(ganefuse_req_t req, const struct ganefuse_entry_param *e); 00222 int ganefuse_reply_create(ganefuse_req_t req, const struct ganefuse_entry_param *e, 00223 const struct ganefuse_file_info *fi); 00224 int ganefuse_reply_attr(ganefuse_req_t req, const struct stat *attr, double attr_timeout); 00225 int ganefuse_reply_readlink(ganefuse_req_t req, const char *link); 00226 int ganefuse_reply_open(ganefuse_req_t req, const struct ganefuse_file_info *fi); 00227 int ganefuse_reply_write(ganefuse_req_t req, size_t count); 00228 int ganefuse_reply_buf(ganefuse_req_t req, const char *buf, size_t size); 00229 int ganefuse_reply_iov(ganefuse_req_t req, const struct iovec *iov, int count); 00230 int ganefuse_reply_statfs(ganefuse_req_t req, const struct statvfs *stbuf); 00231 int ganefuse_reply_xattr(ganefuse_req_t req, size_t count); 00232 int ganefuse_reply_lock(ganefuse_req_t req, struct flock *lock); 00233 int ganefuse_reply_bmap(ganefuse_req_t req, uint64_t idx); 00234 size_t ganefuse_add_direntry(ganefuse_req_t req, char *buf, size_t bufsize, 00235 const char *name, const struct stat *stbuf, off_t off); 00236 00237 /* req functions */ 00238 00239 void *ganefuse_req_userdata(ganefuse_req_t req); 00240 const struct ganefuse_ctx *ganefuse_req_ctx(ganefuse_req_t req); 00241 00242 typedef void (*ganefuse_interrupt_func_t) (ganefuse_req_t req, void *data); 00243 00244 void ganefuse_req_interrupt_func(ganefuse_req_t req, ganefuse_interrupt_func_t func, 00245 void *data); 00246 int ganefuse_req_interrupted(ganefuse_req_t req); 00247 00248 struct ganefuse_session *ganefuse_lowlevel_new(struct ganefuse_args *args, 00249 const struct ganefuse_lowlevel_ops *op, 00250 size_t op_size, void *userdata); 00251 00252 struct ganefuse_session *ganefuse_lowlevel_new25(struct ganefuse_args *args, 00253 const struct ganefuse_lowlevel_ops25 *op, 00254 size_t op_size, void *userdata); 00255 00256 /* session type and calls */ 00257 00258 struct ganefuse_session_ops 00259 { 00260 void (*process) (void *data, const char *buf, size_t len, struct ganefuse_chan * ch); 00261 void (*exit) (void *data, int val); 00262 int (*exited) (void *data); 00263 void (*destroy) (void *data); 00264 }; 00265 00266 struct ganefuse_session *ganefuse_session_new(struct ganefuse_session_ops *op, 00267 void *data); 00268 void ganefuse_session_add_chan(struct ganefuse_session *se, struct ganefuse_chan *ch); 00269 void ganefuse_session_remove_chan(struct ganefuse_chan *ch); 00270 struct ganefuse_chan *ganefuse_session_next_chan(struct ganefuse_session *se, 00271 struct ganefuse_chan *ch); 00272 void ganefuse_session_process(struct ganefuse_session *se, const char *buf, size_t len, 00273 struct ganefuse_chan *ch); 00274 void ganefuse_session_destroy(struct ganefuse_session *se); 00275 void ganefuse_session_exit(struct ganefuse_session *se); 00276 void ganefuse_session_reset(struct ganefuse_session *se); 00277 int ganefuse_session_exited(struct ganefuse_session *se); 00278 int ganefuse_session_loop(struct ganefuse_session *se); 00279 int ganefuse_session_loop_mt(struct ganefuse_session *se); 00280 00281 /* chan type and calls */ 00282 00283 struct ganefuse_chan_ops 00284 { 00285 int (*receive) (struct ganefuse_chan ** chp, char *buf, size_t size); 00286 int (*send) (struct ganefuse_chan * ch, const struct iovec iov[], size_t count); 00287 void (*destroy) (struct ganefuse_chan * ch); 00288 }; 00289 00290 struct ganefuse_chan *ganefuse_chan_new(struct ganefuse_chan_ops *op, int fd, 00291 size_t bufsize, void *data); 00292 int ganefuse_chan_fd(struct ganefuse_chan *ch); 00293 size_t ganefuse_chan_bufsize(struct ganefuse_chan *ch); 00294 void *ganefuse_chan_data(struct ganefuse_chan *ch); 00295 struct ganefuse_session *ganefuse_chan_session(struct ganefuse_chan *ch); 00296 int ganefuse_chan_recv(struct ganefuse_chan **ch, char *buf, size_t size); 00297 int ganefuse_chan_send(struct ganefuse_chan *ch, const struct iovec iov[], size_t count); 00298 void ganefuse_chan_destroy(struct ganefuse_chan *ch); 00299 00300 struct ganefuse_chan *ganefuse_mount(const char *mountpoint, struct ganefuse_args *args); 00301 int ganefuse_mount25(const char *mountpoint, struct ganefuse_args *args); 00302 00303 void ganefuse_unmount25(const char *mountpoint); 00304 void ganefuse_unmount(const char *mountpoint, struct ganefuse_chan *ch); 00305 00306 /* for backward compatibility */ 00307 int ganefuse_chan_receive(struct ganefuse_chan *ch, char *buf, size_t size); 00308 struct ganefuse_chan *ganefuse_kern_chan_new(int fd); 00309 size_t ganefuse_dirent_size(size_t namelen); 00310 char *ganefuse_add_dirent(char *buf, const char *name, const struct stat *stbuf, 00311 off_t off); 00312 00313 /* ---------------------------------------------------------------------------*/ 00314 00315 /* 00316 * Associate them to fuse types with the same name 00317 * so no change is needed for FUSE-binded filesystems. 00318 */ 00319 #define fuse_ino_t ganefuse_ino_t 00320 #define fuse_req_t ganefuse_req_t 00321 #define fuse_session ganefuse_session 00322 #define fuse_chan ganefuse_chan 00323 #define fuse_entry_param ganefuse_entry_param 00324 #define fuse_ctx ganefuse_ctx 00325 #define fuse_conn_info ganefuse_conn_info 00326 #define fuse_file_info ganefuse_file_info 00327 #define fuse_args ganefuse_args 00328 #define fuse_opt ganefuse_opt 00329 #define fuse_opt_proc_t ganefuse_opt_proc_t 00330 #define fuse_interrupt_func_t ganefuse_interrupt_func_t 00331 #define fuse_session_ops ganefuse_session_ops 00332 #define fuse_chan_ops ganefuse_chan_ops 00333 00334 /* call bindings */ 00335 #define fuse_parse_cmdline ganefuse_parse_cmdline 00336 00337 #define fuse_reply_err ganefuse_reply_err 00338 #define fuse_reply_none ganefuse_reply_none 00339 #define fuse_reply_entry ganefuse_reply_entry 00340 #define fuse_reply_create ganefuse_reply_create 00341 #define fuse_reply_attr ganefuse_reply_attr 00342 #define fuse_reply_readlink ganefuse_reply_readlink 00343 #define fuse_reply_open ganefuse_reply_open 00344 #define fuse_reply_write ganefuse_reply_write 00345 #define fuse_reply_buf ganefuse_reply_buf 00346 #define fuse_reply_iov ganefuse_reply_iov 00347 #define fuse_reply_statfs ganefuse_reply_statfs 00348 #define fuse_reply_xattr ganefuse_reply_xattr 00349 #define fuse_reply_lock ganefuse_reply_lock 00350 #define fuse_reply_bmap ganefuse_reply_bmap 00351 #define fuse_add_direntry ganefuse_add_direntry 00352 00353 #define fuse_req_userdata ganefuse_req_userdata 00354 #define fuse_req_ctx ganefuse_req_ctx 00355 #define fuse_req_interrupt_func ganefuse_req_interrupt_func 00356 #define fuse_req_interrupted ganefuse_req_interrupted 00357 00358 #define fuse_session_new ganefuse_session_new 00359 #define fuse_session_add_chan ganefuse_session_add_chan 00360 #define fuse_session_remove_chan ganefuse_session_remove_chan 00361 #define fuse_session_next_chan ganefuse_session_next_chan 00362 #define fuse_session_process ganefuse_session_process 00363 #define fuse_session_destroy ganefuse_session_destroy 00364 #define fuse_session_exit ganefuse_session_exit 00365 #define fuse_session_reset ganefuse_session_reset 00366 #define fuse_session_exited ganefuse_session_exited 00367 #define fuse_session_loop ganefuse_session_loop 00368 #define fuse_session_loop_m ganefuse_session_loop_mt 00369 00370 #define fuse_chan_new ganefuse_chan_new 00371 #define fuse_chan_fd ganefuse_chan_fd 00372 #define fuse_chan_bufsize ganefuse_chan_bufsize 00373 #define fuse_chan_data ganefuse_chan_data 00374 #define fuse_chan_session ganefuse_chan_session 00375 #define fuse_chan_recv ganefuse_chan_recv 00376 #define fuse_chan_send ganefuse_chan_send 00377 #define fuse_chan_destroy ganefuse_chan_destroy 00378 #define fuse_chan_receive ganefuse_chan_receive 00379 #define fuse_kern_chan_new ganefuse_kern_chan_new 00380 00381 #define fuse_opt_parse ganefuse_opt_parse 00382 #define fuse_opt_add_opt ganefuse_opt_add_opt 00383 #define fuse_opt_add_arg ganefuse_opt_add_arg 00384 #define fuse_opt_insert_arg ganefuse_opt_insert_arg 00385 #define fuse_opt_free_args ganefuse_opt_free_args 00386 #define fuse_opt_match ganefuse_opt_match 00387 00388 #if FUSE_USE_VERSION < 25 00389 #error "FUSE bindings before version 25 are not supported" 00390 #elif FUSE_USE_VERSION == 25 00391 # define fuse_mount ganefuse_mount25 00392 # define fuse_lowlevel_ops ganefuse_lowlevel_ops25 00393 # define fuse_lowlevel_new ganefuse_lowlevel_new25 00394 #else 00395 # define fuse_mount ganefuse_mount 00396 # define fuse_lowlevel_ops ganefuse_lowlevel_ops 00397 # define fuse_lowlevel_new ganefuse_lowlevel_new 00398 #endif 00399 00400 #if FUSE_USE_VERSION < 26 00401 # define fuse_unmount ganefuse_unmount25 00402 #else 00403 # define fuse_unmount ganefuse_unmount 00404 #endif 00405 00406 #define fuse_dirent_size ganefuse_dirent_size 00407 #define fuse_add_dirent ganefuse_add_dirent 00408 00409 /* macro binding */ 00410 #define FUSE_ARGS_INIT GANEFUSE_ARGS_INIT 00411 00412 #define FUSE_SET_ATTR_MODE GANEFUSE_SET_ATTR_MODE 00413 #define FUSE_SET_ATTR_UID GANEFUSE_SET_ATTR_UID 00414 #define FUSE_SET_ATTR_GID GANEFUSE_SET_ATTR_GID 00415 #define FUSE_SET_ATTR_SIZE GANEFUSE_SET_ATTR_SIZE 00416 #define FUSE_SET_ATTR_ATIME GANEFUSE_SET_ATTR_ATIME 00417 #define FUSE_SET_ATTR_MTIME GANEFUSE_SET_ATTR_MTIME