fuse_i.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. */
  7. #include <linux/fuse.h>
  8. #include <linux/fs.h>
  9. #include <linux/mount.h>
  10. #include <linux/wait.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/mm.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/mutex.h>
  16. /** Max number of pages that can be used in a single read request */
  17. #define FUSE_MAX_PAGES_PER_REQ 32
  18. /** Maximum number of outstanding background requests */
  19. #define FUSE_MAX_BACKGROUND 10
  20. /** It could be as large as PATH_MAX, but would that have any uses? */
  21. #define FUSE_NAME_MAX 1024
  22. /** Number of dentries for each connection in the control filesystem */
  23. #define FUSE_CTL_NUM_DENTRIES 3
  24. /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
  25. module will check permissions based on the file mode. Otherwise no
  26. permission checking is done in the kernel */
  27. #define FUSE_DEFAULT_PERMISSIONS (1 << 0)
  28. /** If the FUSE_ALLOW_OTHER flag is given, then not only the user
  29. doing the mount will be allowed to access the filesystem */
  30. #define FUSE_ALLOW_OTHER (1 << 1)
  31. /** List of active connections */
  32. extern struct list_head fuse_conn_list;
  33. /** Global mutex protecting fuse_conn_list and the control filesystem */
  34. extern struct mutex fuse_mutex;
  35. /** FUSE inode */
  36. struct fuse_inode {
  37. /** Inode data */
  38. struct inode inode;
  39. /** Unique ID, which identifies the inode between userspace
  40. * and kernel */
  41. u64 nodeid;
  42. /** Number of lookups on this inode */
  43. u64 nlookup;
  44. /** The request used for sending the FORGET message */
  45. struct fuse_req *forget_req;
  46. /** Time in jiffies until the file attributes are valid */
  47. unsigned long i_time;
  48. };
  49. /** FUSE specific file data */
  50. struct fuse_file {
  51. /** Request reserved for flush and release */
  52. struct fuse_req *reserved_req;
  53. /** File handle used by userspace */
  54. u64 fh;
  55. };
  56. /** One input argument of a request */
  57. struct fuse_in_arg {
  58. unsigned size;
  59. const void *value;
  60. };
  61. /** The request input */
  62. struct fuse_in {
  63. /** The request header */
  64. struct fuse_in_header h;
  65. /** True if the data for the last argument is in req->pages */
  66. unsigned argpages:1;
  67. /** Number of arguments */
  68. unsigned numargs;
  69. /** Array of arguments */
  70. struct fuse_in_arg args[3];
  71. };
  72. /** One output argument of a request */
  73. struct fuse_arg {
  74. unsigned size;
  75. void *value;
  76. };
  77. /** The request output */
  78. struct fuse_out {
  79. /** Header returned from userspace */
  80. struct fuse_out_header h;
  81. /*
  82. * The following bitfields are not changed during the request
  83. * processing
  84. */
  85. /** Last argument is variable length (can be shorter than
  86. arg->size) */
  87. unsigned argvar:1;
  88. /** Last argument is a list of pages to copy data to */
  89. unsigned argpages:1;
  90. /** Zero partially or not copied pages */
  91. unsigned page_zeroing:1;
  92. /** Number or arguments */
  93. unsigned numargs;
  94. /** Array of arguments */
  95. struct fuse_arg args[3];
  96. };
  97. /** The request state */
  98. enum fuse_req_state {
  99. FUSE_REQ_INIT = 0,
  100. FUSE_REQ_PENDING,
  101. FUSE_REQ_READING,
  102. FUSE_REQ_SENT,
  103. FUSE_REQ_FINISHED
  104. };
  105. struct fuse_conn;
  106. /**
  107. * A request to the client
  108. */
  109. struct fuse_req {
  110. /** This can be on either pending processing or io lists in
  111. fuse_conn */
  112. struct list_head list;
  113. /** refcount */
  114. atomic_t count;
  115. /*
  116. * The following bitfields are either set once before the
  117. * request is queued or setting/clearing them is protected by
  118. * fuse_conn->lock
  119. */
  120. /** True if the request has reply */
  121. unsigned isreply:1;
  122. /** Force sending of the request even if interrupted */
  123. unsigned force:1;
  124. /** The request was interrupted */
  125. unsigned interrupted:1;
  126. /** Request is sent in the background */
  127. unsigned background:1;
  128. /** Data is being copied to/from the request */
  129. unsigned locked:1;
  130. /** Request is counted as "waiting" */
  131. unsigned waiting:1;
  132. /** State of the request */
  133. enum fuse_req_state state;
  134. /** The request input */
  135. struct fuse_in in;
  136. /** The request output */
  137. struct fuse_out out;
  138. /** Used to wake up the task waiting for completion of request*/
  139. wait_queue_head_t waitq;
  140. /** Data for asynchronous requests */
  141. union {
  142. struct fuse_forget_in forget_in;
  143. struct fuse_release_in release_in;
  144. struct fuse_init_in init_in;
  145. struct fuse_init_out init_out;
  146. struct fuse_read_in read_in;
  147. struct fuse_lk_in lk_in;
  148. } misc;
  149. /** page vector */
  150. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  151. /** number of pages in vector */
  152. unsigned num_pages;
  153. /** offset of data on first page */
  154. unsigned page_offset;
  155. /** File used in the request (or NULL) */
  156. struct file *file;
  157. /** vfsmount used in release */
  158. struct vfsmount *vfsmount;
  159. /** dentry used in release */
  160. struct dentry *dentry;
  161. /** Request completion callback */
  162. void (*end)(struct fuse_conn *, struct fuse_req *);
  163. /** Request is stolen from fuse_file->reserved_req */
  164. struct file *stolen_file;
  165. };
  166. /**
  167. * A Fuse connection.
  168. *
  169. * This structure is created, when the filesystem is mounted, and is
  170. * destroyed, when the client device is closed and the filesystem is
  171. * unmounted.
  172. */
  173. struct fuse_conn {
  174. /** Lock protecting accessess to members of this structure */
  175. spinlock_t lock;
  176. /** Refcount */
  177. atomic_t count;
  178. /** The user id for this mount */
  179. uid_t user_id;
  180. /** The group id for this mount */
  181. gid_t group_id;
  182. /** The fuse mount flags for this mount */
  183. unsigned flags;
  184. /** Maximum read size */
  185. unsigned max_read;
  186. /** Maximum write size */
  187. unsigned max_write;
  188. /** Readers of the connection are waiting on this */
  189. wait_queue_head_t waitq;
  190. /** The list of pending requests */
  191. struct list_head pending;
  192. /** The list of requests being processed */
  193. struct list_head processing;
  194. /** The list of requests under I/O */
  195. struct list_head io;
  196. /** Number of requests currently in the background */
  197. unsigned num_background;
  198. /** Flag indicating if connection is blocked. This will be
  199. the case before the INIT reply is received, and if there
  200. are too many outstading backgrounds requests */
  201. int blocked;
  202. /** waitq for blocked connection */
  203. wait_queue_head_t blocked_waitq;
  204. /** The next unique request id */
  205. u64 reqctr;
  206. /** Connection established, cleared on umount, connection
  207. abort and device release */
  208. unsigned connected;
  209. /** Connection failed (version mismatch). Cannot race with
  210. setting other bitfields since it is only set once in INIT
  211. reply, before any other request, and never cleared */
  212. unsigned conn_error : 1;
  213. /** Do readpages asynchronously? Only set in INIT */
  214. unsigned async_read : 1;
  215. /*
  216. * The following bitfields are only for optimization purposes
  217. * and hence races in setting them will not cause malfunction
  218. */
  219. /** Is fsync not implemented by fs? */
  220. unsigned no_fsync : 1;
  221. /** Is fsyncdir not implemented by fs? */
  222. unsigned no_fsyncdir : 1;
  223. /** Is flush not implemented by fs? */
  224. unsigned no_flush : 1;
  225. /** Is setxattr not implemented by fs? */
  226. unsigned no_setxattr : 1;
  227. /** Is getxattr not implemented by fs? */
  228. unsigned no_getxattr : 1;
  229. /** Is listxattr not implemented by fs? */
  230. unsigned no_listxattr : 1;
  231. /** Is removexattr not implemented by fs? */
  232. unsigned no_removexattr : 1;
  233. /** Are file locking primitives not implemented by fs? */
  234. unsigned no_lock : 1;
  235. /** Is access not implemented by fs? */
  236. unsigned no_access : 1;
  237. /** Is create not implemented by fs? */
  238. unsigned no_create : 1;
  239. /** The number of requests waiting for completion */
  240. atomic_t num_waiting;
  241. /** Negotiated minor version */
  242. unsigned minor;
  243. /** Backing dev info */
  244. struct backing_dev_info bdi;
  245. /** Entry on the fuse_conn_list */
  246. struct list_head entry;
  247. /** Unique ID */
  248. u64 id;
  249. /** Dentries in the control filesystem */
  250. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  251. /** number of dentries used in the above array */
  252. int ctl_ndents;
  253. /** O_ASYNC requests */
  254. struct fasync_struct *fasync;
  255. };
  256. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  257. {
  258. return sb->s_fs_info;
  259. }
  260. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  261. {
  262. return get_fuse_conn_super(inode->i_sb);
  263. }
  264. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  265. {
  266. return container_of(inode, struct fuse_inode, inode);
  267. }
  268. static inline u64 get_node_id(struct inode *inode)
  269. {
  270. return get_fuse_inode(inode)->nodeid;
  271. }
  272. /** Device operations */
  273. extern const struct file_operations fuse_dev_operations;
  274. /**
  275. * Get a filled in inode
  276. */
  277. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  278. int generation, struct fuse_attr *attr);
  279. /**
  280. * Send FORGET command
  281. */
  282. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  283. unsigned long nodeid, u64 nlookup);
  284. /**
  285. * Initialize READ or READDIR request
  286. */
  287. void fuse_read_fill(struct fuse_req *req, struct file *file,
  288. struct inode *inode, loff_t pos, size_t count, int opcode);
  289. /**
  290. * Send OPEN or OPENDIR request
  291. */
  292. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  293. struct fuse_file *fuse_file_alloc(void);
  294. void fuse_file_free(struct fuse_file *ff);
  295. void fuse_finish_open(struct inode *inode, struct file *file,
  296. struct fuse_file *ff, struct fuse_open_out *outarg);
  297. /** */
  298. struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
  299. int opcode);
  300. /**
  301. * Send RELEASE or RELEASEDIR request
  302. */
  303. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  304. /**
  305. * Send FSYNC or FSYNCDIR request
  306. */
  307. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  308. int isdir);
  309. /**
  310. * Initialize file operations on a regular file
  311. */
  312. void fuse_init_file_inode(struct inode *inode);
  313. /**
  314. * Initialize inode operations on regular files and special files
  315. */
  316. void fuse_init_common(struct inode *inode);
  317. /**
  318. * Initialize inode and file operations on a directory
  319. */
  320. void fuse_init_dir(struct inode *inode);
  321. /**
  322. * Initialize inode operations on a symlink
  323. */
  324. void fuse_init_symlink(struct inode *inode);
  325. /**
  326. * Change attributes of an inode
  327. */
  328. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  329. /**
  330. * Initialize the client device
  331. */
  332. int fuse_dev_init(void);
  333. /**
  334. * Cleanup the client device
  335. */
  336. void fuse_dev_cleanup(void);
  337. int fuse_ctl_init(void);
  338. void fuse_ctl_cleanup(void);
  339. /**
  340. * Allocate a request
  341. */
  342. struct fuse_req *fuse_request_alloc(void);
  343. /**
  344. * Free a request
  345. */
  346. void fuse_request_free(struct fuse_req *req);
  347. /**
  348. * Get a request, may fail with -ENOMEM
  349. */
  350. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  351. /**
  352. * Gets a requests for a file operation, always succeeds
  353. */
  354. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  355. /**
  356. * Decrement reference count of a request. If count goes to zero free
  357. * the request.
  358. */
  359. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  360. /**
  361. * Send a request (synchronous)
  362. */
  363. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  364. /**
  365. * Send a request with no reply
  366. */
  367. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  368. /**
  369. * Send a request in the background
  370. */
  371. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  372. /* Abort all requests */
  373. void fuse_abort_conn(struct fuse_conn *fc);
  374. /**
  375. * Get the attributes of a file
  376. */
  377. int fuse_do_getattr(struct inode *inode);
  378. /**
  379. * Invalidate inode attributes
  380. */
  381. void fuse_invalidate_attr(struct inode *inode);
  382. /**
  383. * Acquire reference to fuse_conn
  384. */
  385. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  386. /**
  387. * Release reference to fuse_conn
  388. */
  389. void fuse_conn_put(struct fuse_conn *fc);
  390. /**
  391. * Add connection to control filesystem
  392. */
  393. int fuse_ctl_add_conn(struct fuse_conn *fc);
  394. /**
  395. * Remove connection from control filesystem
  396. */
  397. void fuse_ctl_remove_conn(struct fuse_conn *fc);