fuse_i.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 *release_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. };
  164. /**
  165. * A Fuse connection.
  166. *
  167. * This structure is created, when the filesystem is mounted, and is
  168. * destroyed, when the client device is closed and the filesystem is
  169. * unmounted.
  170. */
  171. struct fuse_conn {
  172. /** Lock protecting accessess to members of this structure */
  173. spinlock_t lock;
  174. /** Refcount */
  175. atomic_t count;
  176. /** The user id for this mount */
  177. uid_t user_id;
  178. /** The group id for this mount */
  179. gid_t group_id;
  180. /** The fuse mount flags for this mount */
  181. unsigned flags;
  182. /** Maximum read size */
  183. unsigned max_read;
  184. /** Maximum write size */
  185. unsigned max_write;
  186. /** Readers of the connection are waiting on this */
  187. wait_queue_head_t waitq;
  188. /** The list of pending requests */
  189. struct list_head pending;
  190. /** The list of requests being processed */
  191. struct list_head processing;
  192. /** The list of requests under I/O */
  193. struct list_head io;
  194. /** Number of requests currently in the background */
  195. unsigned num_background;
  196. /** Flag indicating if connection is blocked. This will be
  197. the case before the INIT reply is received, and if there
  198. are too many outstading backgrounds requests */
  199. int blocked;
  200. /** waitq for blocked connection */
  201. wait_queue_head_t blocked_waitq;
  202. /** The next unique request id */
  203. u64 reqctr;
  204. /** Connection established, cleared on umount, connection
  205. abort and device release */
  206. unsigned connected;
  207. /** Connection failed (version mismatch). Cannot race with
  208. setting other bitfields since it is only set once in INIT
  209. reply, before any other request, and never cleared */
  210. unsigned conn_error : 1;
  211. /** Do readpages asynchronously? Only set in INIT */
  212. unsigned async_read : 1;
  213. /*
  214. * The following bitfields are only for optimization purposes
  215. * and hence races in setting them will not cause malfunction
  216. */
  217. /** Is fsync not implemented by fs? */
  218. unsigned no_fsync : 1;
  219. /** Is fsyncdir not implemented by fs? */
  220. unsigned no_fsyncdir : 1;
  221. /** Is flush not implemented by fs? */
  222. unsigned no_flush : 1;
  223. /** Is setxattr not implemented by fs? */
  224. unsigned no_setxattr : 1;
  225. /** Is getxattr not implemented by fs? */
  226. unsigned no_getxattr : 1;
  227. /** Is listxattr not implemented by fs? */
  228. unsigned no_listxattr : 1;
  229. /** Is removexattr not implemented by fs? */
  230. unsigned no_removexattr : 1;
  231. /** Are file locking primitives not implemented by fs? */
  232. unsigned no_lock : 1;
  233. /** Is access not implemented by fs? */
  234. unsigned no_access : 1;
  235. /** Is create not implemented by fs? */
  236. unsigned no_create : 1;
  237. /** The number of requests waiting for completion */
  238. atomic_t num_waiting;
  239. /** Negotiated minor version */
  240. unsigned minor;
  241. /** Backing dev info */
  242. struct backing_dev_info bdi;
  243. /** Entry on the fuse_conn_list */
  244. struct list_head entry;
  245. /** Unique ID */
  246. u64 id;
  247. /** Dentries in the control filesystem */
  248. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  249. /** number of dentries used in the above array */
  250. int ctl_ndents;
  251. /** O_ASYNC requests */
  252. struct fasync_struct *fasync;
  253. };
  254. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  255. {
  256. return sb->s_fs_info;
  257. }
  258. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  259. {
  260. return get_fuse_conn_super(inode->i_sb);
  261. }
  262. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  263. {
  264. return container_of(inode, struct fuse_inode, inode);
  265. }
  266. static inline u64 get_node_id(struct inode *inode)
  267. {
  268. return get_fuse_inode(inode)->nodeid;
  269. }
  270. /** Device operations */
  271. extern const struct file_operations fuse_dev_operations;
  272. /**
  273. * Get a filled in inode
  274. */
  275. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  276. int generation, struct fuse_attr *attr);
  277. /**
  278. * Send FORGET command
  279. */
  280. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  281. unsigned long nodeid, u64 nlookup);
  282. /**
  283. * Initialize READ or READDIR request
  284. */
  285. void fuse_read_fill(struct fuse_req *req, struct file *file,
  286. struct inode *inode, loff_t pos, size_t count, int opcode);
  287. /**
  288. * Send OPEN or OPENDIR request
  289. */
  290. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  291. struct fuse_file *fuse_file_alloc(void);
  292. void fuse_file_free(struct fuse_file *ff);
  293. void fuse_finish_open(struct inode *inode, struct file *file,
  294. struct fuse_file *ff, struct fuse_open_out *outarg);
  295. /** */
  296. struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
  297. int opcode);
  298. /**
  299. * Send RELEASE or RELEASEDIR request
  300. */
  301. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  302. /**
  303. * Send FSYNC or FSYNCDIR request
  304. */
  305. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  306. int isdir);
  307. /**
  308. * Initialize file operations on a regular file
  309. */
  310. void fuse_init_file_inode(struct inode *inode);
  311. /**
  312. * Initialize inode operations on regular files and special files
  313. */
  314. void fuse_init_common(struct inode *inode);
  315. /**
  316. * Initialize inode and file operations on a directory
  317. */
  318. void fuse_init_dir(struct inode *inode);
  319. /**
  320. * Initialize inode operations on a symlink
  321. */
  322. void fuse_init_symlink(struct inode *inode);
  323. /**
  324. * Change attributes of an inode
  325. */
  326. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  327. /**
  328. * Initialize the client device
  329. */
  330. int fuse_dev_init(void);
  331. /**
  332. * Cleanup the client device
  333. */
  334. void fuse_dev_cleanup(void);
  335. int fuse_ctl_init(void);
  336. void fuse_ctl_cleanup(void);
  337. /**
  338. * Allocate a request
  339. */
  340. struct fuse_req *fuse_request_alloc(void);
  341. /**
  342. * Free a request
  343. */
  344. void fuse_request_free(struct fuse_req *req);
  345. /**
  346. * Reserve a preallocated request
  347. */
  348. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  349. /**
  350. * Decrement reference count of a request. If count goes to zero free
  351. * the request.
  352. */
  353. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  354. /**
  355. * Send a request (synchronous)
  356. */
  357. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  358. /**
  359. * Send a request with no reply
  360. */
  361. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  362. /**
  363. * Send a request in the background
  364. */
  365. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  366. /* Abort all requests */
  367. void fuse_abort_conn(struct fuse_conn *fc);
  368. /**
  369. * Get the attributes of a file
  370. */
  371. int fuse_do_getattr(struct inode *inode);
  372. /**
  373. * Invalidate inode attributes
  374. */
  375. void fuse_invalidate_attr(struct inode *inode);
  376. /**
  377. * Acquire reference to fuse_conn
  378. */
  379. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  380. /**
  381. * Release reference to fuse_conn
  382. */
  383. void fuse_conn_put(struct fuse_conn *fc);
  384. /**
  385. * Add connection to control filesystem
  386. */
  387. int fuse_ctl_add_conn(struct fuse_conn *fc);
  388. /**
  389. * Remove connection from control filesystem
  390. */
  391. void fuse_ctl_remove_conn(struct fuse_conn *fc);