fuse_i.h 13 KB

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