fuse_i.h 13 KB

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