fuse_i.h 13 KB

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