fuse_i.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 fuse_release_in release_in;
  164. struct fuse_init_in init_in;
  165. struct fuse_init_out init_out;
  166. struct fuse_read_in read_in;
  167. struct {
  168. struct fuse_write_in in;
  169. struct fuse_write_out out;
  170. } write;
  171. struct fuse_lk_in lk_in;
  172. } misc;
  173. /** page vector */
  174. struct page *pages[FUSE_MAX_PAGES_PER_REQ];
  175. /** number of pages in vector */
  176. unsigned num_pages;
  177. /** offset of data on first page */
  178. unsigned page_offset;
  179. /** File used in the request (or NULL) */
  180. struct fuse_file *ff;
  181. /** vfsmount used in release */
  182. struct vfsmount *vfsmount;
  183. /** dentry used in release */
  184. struct dentry *dentry;
  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. /** Pending interrupts */
  225. struct list_head interrupts;
  226. /** Flag indicating if connection is blocked. This will be
  227. the case before the INIT reply is received, and if there
  228. are too many outstading backgrounds requests */
  229. int blocked;
  230. /** waitq for blocked connection */
  231. wait_queue_head_t blocked_waitq;
  232. /** waitq for reserved requests */
  233. wait_queue_head_t reserved_req_waitq;
  234. /** The next unique request id */
  235. u64 reqctr;
  236. /** Connection established, cleared on umount, connection
  237. abort and device release */
  238. unsigned connected;
  239. /** Connection failed (version mismatch). Cannot race with
  240. setting other bitfields since it is only set once in INIT
  241. reply, before any other request, and never cleared */
  242. unsigned conn_error : 1;
  243. /** Connection successful. Only set in INIT */
  244. unsigned conn_init : 1;
  245. /** Do readpages asynchronously? Only set in INIT */
  246. unsigned async_read : 1;
  247. /** Do not send separate SETATTR request before open(O_TRUNC) */
  248. unsigned atomic_o_trunc : 1;
  249. /*
  250. * The following bitfields are only for optimization purposes
  251. * and hence races in setting them will not cause malfunction
  252. */
  253. /** Is fsync not implemented by fs? */
  254. unsigned no_fsync : 1;
  255. /** Is fsyncdir not implemented by fs? */
  256. unsigned no_fsyncdir : 1;
  257. /** Is flush not implemented by fs? */
  258. unsigned no_flush : 1;
  259. /** Is setxattr not implemented by fs? */
  260. unsigned no_setxattr : 1;
  261. /** Is getxattr not implemented by fs? */
  262. unsigned no_getxattr : 1;
  263. /** Is listxattr not implemented by fs? */
  264. unsigned no_listxattr : 1;
  265. /** Is removexattr not implemented by fs? */
  266. unsigned no_removexattr : 1;
  267. /** Are file locking primitives not implemented by fs? */
  268. unsigned no_lock : 1;
  269. /** Is access not implemented by fs? */
  270. unsigned no_access : 1;
  271. /** Is create not implemented by fs? */
  272. unsigned no_create : 1;
  273. /** Is interrupt not implemented by fs? */
  274. unsigned no_interrupt : 1;
  275. /** Is bmap not implemented by fs? */
  276. unsigned no_bmap : 1;
  277. /** The number of requests waiting for completion */
  278. atomic_t num_waiting;
  279. /** Negotiated minor version */
  280. unsigned minor;
  281. /** Backing dev info */
  282. struct backing_dev_info bdi;
  283. /** Entry on the fuse_conn_list */
  284. struct list_head entry;
  285. /** Unique ID */
  286. u64 id;
  287. /** Dentries in the control filesystem */
  288. struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
  289. /** number of dentries used in the above array */
  290. int ctl_ndents;
  291. /** O_ASYNC requests */
  292. struct fasync_struct *fasync;
  293. /** Key for lock owner ID scrambling */
  294. u32 scramble_key[4];
  295. /** Reserved request for the DESTROY message */
  296. struct fuse_req *destroy_req;
  297. /** Version counter for attribute changes */
  298. u64 attr_version;
  299. };
  300. static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
  301. {
  302. return sb->s_fs_info;
  303. }
  304. static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
  305. {
  306. return get_fuse_conn_super(inode->i_sb);
  307. }
  308. static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
  309. {
  310. return container_of(inode, struct fuse_inode, inode);
  311. }
  312. static inline u64 get_node_id(struct inode *inode)
  313. {
  314. return get_fuse_inode(inode)->nodeid;
  315. }
  316. /** Device operations */
  317. extern const struct file_operations fuse_dev_operations;
  318. /**
  319. * Get a filled in inode
  320. */
  321. struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
  322. int generation, struct fuse_attr *attr,
  323. u64 attr_valid, u64 attr_version);
  324. /**
  325. * Send FORGET command
  326. */
  327. void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
  328. unsigned long nodeid, u64 nlookup);
  329. /**
  330. * Initialize READ or READDIR request
  331. */
  332. void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
  333. struct inode *inode, loff_t pos, size_t count, int opcode);
  334. /**
  335. * Send OPEN or OPENDIR request
  336. */
  337. int fuse_open_common(struct inode *inode, struct file *file, int isdir);
  338. struct fuse_file *fuse_file_alloc(void);
  339. void fuse_file_free(struct fuse_file *ff);
  340. void fuse_finish_open(struct inode *inode, struct file *file,
  341. struct fuse_file *ff, struct fuse_open_out *outarg);
  342. /** Fill in ff->reserved_req with a RELEASE request */
  343. void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode);
  344. /**
  345. * Send RELEASE or RELEASEDIR request
  346. */
  347. int fuse_release_common(struct inode *inode, struct file *file, int isdir);
  348. /**
  349. * Send FSYNC or FSYNCDIR request
  350. */
  351. int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
  352. int isdir);
  353. /**
  354. * Initialize file operations on a regular file
  355. */
  356. void fuse_init_file_inode(struct inode *inode);
  357. /**
  358. * Initialize inode operations on regular files and special files
  359. */
  360. void fuse_init_common(struct inode *inode);
  361. /**
  362. * Initialize inode and file operations on a directory
  363. */
  364. void fuse_init_dir(struct inode *inode);
  365. /**
  366. * Initialize inode operations on a symlink
  367. */
  368. void fuse_init_symlink(struct inode *inode);
  369. /**
  370. * Change attributes of an inode
  371. */
  372. void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
  373. u64 attr_valid, u64 attr_version);
  374. /**
  375. * Initialize the client device
  376. */
  377. int fuse_dev_init(void);
  378. /**
  379. * Cleanup the client device
  380. */
  381. void fuse_dev_cleanup(void);
  382. int fuse_ctl_init(void);
  383. void fuse_ctl_cleanup(void);
  384. /**
  385. * Allocate a request
  386. */
  387. struct fuse_req *fuse_request_alloc(void);
  388. /**
  389. * Free a request
  390. */
  391. void fuse_request_free(struct fuse_req *req);
  392. /**
  393. * Get a request, may fail with -ENOMEM
  394. */
  395. struct fuse_req *fuse_get_req(struct fuse_conn *fc);
  396. /**
  397. * Gets a requests for a file operation, always succeeds
  398. */
  399. struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file);
  400. /**
  401. * Decrement reference count of a request. If count goes to zero free
  402. * the request.
  403. */
  404. void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
  405. /**
  406. * Send a request (synchronous)
  407. */
  408. void request_send(struct fuse_conn *fc, struct fuse_req *req);
  409. /**
  410. * Send a request with no reply
  411. */
  412. void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
  413. /**
  414. * Send a request in the background
  415. */
  416. void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
  417. /* Abort all requests */
  418. void fuse_abort_conn(struct fuse_conn *fc);
  419. /**
  420. * Invalidate inode attributes
  421. */
  422. void fuse_invalidate_attr(struct inode *inode);
  423. /**
  424. * Acquire reference to fuse_conn
  425. */
  426. struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
  427. /**
  428. * Release reference to fuse_conn
  429. */
  430. void fuse_conn_put(struct fuse_conn *fc);
  431. /**
  432. * Add connection to control filesystem
  433. */
  434. int fuse_ctl_add_conn(struct fuse_conn *fc);
  435. /**
  436. * Remove connection from control filesystem
  437. */
  438. void fuse_ctl_remove_conn(struct fuse_conn *fc);
  439. /**
  440. * Is file type valid?
  441. */
  442. int fuse_valid_type(int m);
  443. /**
  444. * Is task allowed to perform filesystem operation?
  445. */
  446. int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task);
  447. u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id);