fuse_i.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. u64 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_WRITING,
  104. FUSE_REQ_FINISHED
  105. };
  106. struct fuse_conn;
  107. /**
  108. * A request to the client
  109. */
  110. struct fuse_req {
  111. /** This can be on either pending processing or io lists in
  112. fuse_conn */
  113. struct list_head list;
  114. /** Entry on the interrupts list */
  115. struct list_head intr_entry;
  116. /** refcount */
  117. atomic_t count;
  118. /** Unique ID for the interrupt request */
  119. u64 intr_unique;
  120. /*
  121. * The following bitfields are either set once before the
  122. * request is queued or setting/clearing them is protected by
  123. * fuse_conn->lock
  124. */
  125. /** True if the request has reply */
  126. unsigned isreply:1;
  127. /** Force sending of the request even if interrupted */
  128. unsigned force:1;
  129. /** The request was aborted */
  130. unsigned aborted:1;
  131. /** Request is sent in the background */
  132. unsigned background:1;
  133. /** The request has been interrupted */
  134. unsigned interrupted:1;
  135. /** Data is being copied to/from the request */
  136. unsigned locked:1;
  137. /** Request is counted as "waiting" */
  138. unsigned waiting:1;
  139. /** State of the request */
  140. enum fuse_req_state state;
  141. /** The request input */
  142. struct fuse_in in;
  143. /** The request output */
  144. struct fuse_out out;
  145. /** Used to wake up the task waiting for completion of request*/
  146. wait_queue_head_t waitq;
  147. /** Data for asynchronous requests */
  148. union {
  149. struct fuse_forget_in forget_in;
  150. struct fuse_release_in release_in;
  151. struct fuse_init_in init_in;
  152. struct fuse_init_out init_out;
  153. struct fuse_read_in read_in;
  154. struct fuse_lk_in lk_in;
  155. } misc;
  156. /** page vector */
  157. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  158. /** number of pages in vector */
  159. unsigned num_pages;
  160. /** offset of data on first page */
  161. unsigned page_offset;
  162. /** File used in the request (or NULL) */
  163. struct file *file;
  164. /** vfsmount used in release */
  165. struct vfsmount *vfsmount;
  166. /** dentry used in release */
  167. struct dentry *dentry;
  168. /** Request completion callback */
  169. void (*end)(struct fuse_conn *, struct fuse_req *);
  170. /** Request is stolen from fuse_file->reserved_req */
  171. struct file *stolen_file;
  172. };
  173. /**
  174. * A Fuse connection.
  175. *
  176. * This structure is created, when the filesystem is mounted, and is
  177. * destroyed, when the client device is closed and the filesystem is
  178. * unmounted.
  179. */
  180. struct fuse_conn {
  181. /** Lock protecting accessess to members of this structure */
  182. spinlock_t lock;
  183. /** Mutex protecting against directory alias creation */
  184. struct mutex inst_mutex;
  185. /** Refcount */
  186. atomic_t count;
  187. /** The user id for this mount */
  188. uid_t user_id;
  189. /** The group id for this mount */
  190. gid_t group_id;
  191. /** The fuse mount flags for this mount */
  192. unsigned flags;
  193. /** Maximum read size */
  194. unsigned max_read;
  195. /** Maximum write size */
  196. unsigned max_write;
  197. /** Readers of the connection are waiting on this */
  198. wait_queue_head_t waitq;
  199. /** The list of pending requests */
  200. struct list_head pending;
  201. /** The list of requests being processed */
  202. struct list_head processing;
  203. /** The list of requests under I/O */
  204. struct list_head io;
  205. /** Number of requests currently in the background */
  206. unsigned num_background;
  207. /** Pending interrupts */
  208. struct list_head interrupts;
  209. /** Flag indicating if connection is blocked. This will be
  210. the case before the INIT reply is received, and if there
  211. are too many outstading backgrounds requests */
  212. int blocked;
  213. /** waitq for blocked connection */
  214. wait_queue_head_t blocked_waitq;
  215. /** The next unique request id */
  216. u64 reqctr;
  217. /** Connection established, cleared on umount, connection
  218. abort and device release */
  219. unsigned connected;
  220. /** Connection failed (version mismatch). Cannot race with
  221. setting other bitfields since it is only set once in INIT
  222. reply, before any other request, and never cleared */
  223. unsigned conn_error : 1;
  224. /** Connection successful. Only set in INIT */
  225. unsigned conn_init : 1;
  226. /** Do readpages asynchronously? Only set in INIT */
  227. unsigned async_read : 1;
  228. /*
  229. * The following bitfields are only for optimization purposes
  230. * and hence races in setting them will not cause malfunction
  231. */
  232. /** Is fsync not implemented by fs? */
  233. unsigned no_fsync : 1;
  234. /** Is fsyncdir not implemented by fs? */
  235. unsigned no_fsyncdir : 1;
  236. /** Is flush not implemented by fs? */
  237. unsigned no_flush : 1;
  238. /** Is setxattr not implemented by fs? */
  239. unsigned no_setxattr : 1;
  240. /** Is getxattr not implemented by fs? */
  241. unsigned no_getxattr : 1;
  242. /** Is listxattr not implemented by fs? */
  243. unsigned no_listxattr : 1;
  244. /** Is removexattr not implemented by fs? */
  245. unsigned no_removexattr : 1;
  246. /** Are file locking primitives not implemented by fs? */
  247. unsigned no_lock : 1;
  248. /** Is access not implemented by fs? */
  249. unsigned no_access : 1;
  250. /** Is create not implemented by fs? */
  251. unsigned no_create : 1;
  252. /** Is interrupt not implemented by fs? */
  253. unsigned no_interrupt : 1;
  254. /** Is bmap not implemented by fs? */
  255. unsigned no_bmap : 1;
  256. /** The number of requests waiting for completion */
  257. atomic_t num_waiting;
  258. /** Negotiated minor version */
  259. unsigned minor;
  260. /** Backing dev info */
  261. struct backing_dev_info bdi;
  262. /** Entry on the fuse_conn_list */
  263. struct list_head entry;
  264. /** Unique ID */
  265. u64 id;
  266. /** Dentries in the control filesystem */
  267. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  268. /** number of dentries used in the above array */
  269. int ctl_ndents;
  270. /** O_ASYNC requests */
  271. struct fasync_struct *fasync;
  272. /** Key for lock owner ID scrambling */
  273. u32 scramble_key[4];
  274. /** Reserved request for the DESTROY message */
  275. struct fuse_req *destroy_req;
  276. };
  277. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  278. {
  279. return sb->s_fs_info;
  280. }
  281. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  282. {
  283. return get_fuse_conn_super(inode->i_sb);
  284. }
  285. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  286. {
  287. return container_of(inode, struct fuse_inode, inode);
  288. }
  289. static inline u64 get_node_id(struct inode *inode)
  290. {
  291. return get_fuse_inode(inode)->nodeid;
  292. }
  293. /** Device operations */
  294. extern const struct file_operations fuse_dev_operations;
  295. /**
  296. * Get a filled in inode
  297. */
  298. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  299. int generation, struct fuse_attr *attr);
  300. /**
  301. * Send FORGET command
  302. */
  303. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  304. unsigned long nodeid, u64 nlookup);
  305. /**
  306. * Initialize READ or READDIR request
  307. */
  308. void fuse_read_fill(struct fuse_req *req, struct file *file,
  309. struct inode *inode, loff_t pos, size_t count, int opcode);
  310. /**
  311. * Send OPEN or OPENDIR request
  312. */
  313. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  314. struct fuse_file *fuse_file_alloc(void);
  315. void fuse_file_free(struct fuse_file *ff);
  316. void fuse_finish_open(struct inode *inode, struct file *file,
  317. struct fuse_file *ff, struct fuse_open_out *outarg);
  318. /** */
  319. struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
  320. int opcode);
  321. /**
  322. * Send RELEASE or RELEASEDIR request
  323. */
  324. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  325. /**
  326. * Send FSYNC or FSYNCDIR request
  327. */
  328. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  329. int isdir);
  330. /**
  331. * Initialize file operations on a regular file
  332. */
  333. void fuse_init_file_inode(struct inode *inode);
  334. /**
  335. * Initialize inode operations on regular files and special files
  336. */
  337. void fuse_init_common(struct inode *inode);
  338. /**
  339. * Initialize inode and file operations on a directory
  340. */
  341. void fuse_init_dir(struct inode *inode);
  342. /**
  343. * Initialize inode operations on a symlink
  344. */
  345. void fuse_init_symlink(struct inode *inode);
  346. /**
  347. * Change attributes of an inode
  348. */
  349. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
  350. /**
  351. * Initialize the client device
  352. */
  353. int fuse_dev_init(void);
  354. /**
  355. * Cleanup the client device
  356. */
  357. void fuse_dev_cleanup(void);
  358. int fuse_ctl_init(void);
  359. void fuse_ctl_cleanup(void);
  360. /**
  361. * Allocate a request
  362. */
  363. struct fuse_req *fuse_request_alloc(void);
  364. /**
  365. * Free a request
  366. */
  367. void fuse_request_free(struct fuse_req *req);
  368. /**
  369. * Get a request, may fail with -ENOMEM
  370. */
  371. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  372. /**
  373. * Gets a requests for a file operation, always succeeds
  374. */
  375. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  376. /**
  377. * Decrement reference count of a request. If count goes to zero free
  378. * the request.
  379. */
  380. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  381. /**
  382. * Send a request (synchronous)
  383. */
  384. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  385. /**
  386. * Send a request with no reply
  387. */
  388. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  389. /**
  390. * Send a request in the background
  391. */
  392. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  393. /* Abort all requests */
  394. void fuse_abort_conn(struct fuse_conn *fc);
  395. /**
  396. * Get the attributes of a file
  397. */
  398. int fuse_do_getattr(struct inode *inode);
  399. /**
  400. * Invalidate inode attributes
  401. */
  402. void fuse_invalidate_attr(struct inode *inode);
  403. /**
  404. * Acquire reference to fuse_conn
  405. */
  406. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  407. /**
  408. * Release reference to fuse_conn
  409. */
  410. void fuse_conn_put(struct fuse_conn *fc);
  411. /**
  412. * Add connection to control filesystem
  413. */
  414. int fuse_ctl_add_conn(struct fuse_conn *fc);
  415. /**
  416. * Remove connection from control filesystem
  417. */
  418. void fuse_ctl_remove_conn(struct fuse_conn *fc);
  419. /**
  420. * Is file type valid?
  421. */
  422. int fuse_valid_type(int m);