fuse_i.h 14 KB

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