You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

681 lines
22 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB.
  6. */
  7. #ifndef _FUSE_LOWLEVEL_H_
  8. #define _FUSE_LOWLEVEL_H_
  9. #include "extern_c.h"
  10. #include "fuse_common.h"
  11. #include "fuse_kernel.h"
  12. #include <fcntl.h>
  13. #include <stdint.h>
  14. #include <sys/stat.h>
  15. #include <sys/statvfs.h>
  16. #include <sys/types.h>
  17. #include <sys/uio.h>
  18. #include <utime.h>
  19. EXTERN_C_BEGIN
  20. /* ----------------------------------------------------------- *
  21. * Miscellaneous definitions *
  22. * ----------------------------------------------------------- */
  23. /** The node ID of the root inode */
  24. #define FUSE_ROOT_ID 1
  25. /** Request pointer type */
  26. typedef struct fuse_req *fuse_req_t;
  27. /**
  28. * Session
  29. *
  30. * This provides hooks for processing requests, and exiting
  31. */
  32. struct fuse_session;
  33. /**
  34. * Channel
  35. *
  36. * A communication channel, providing hooks for sending and receiving
  37. * messages
  38. */
  39. struct fuse_chan;
  40. /** Directory entry parameters supplied to fuse_reply_entry() */
  41. struct fuse_entry_param
  42. {
  43. /** Unique inode number
  44. *
  45. * In lookup, zero means negative entry (from version 2.5)
  46. * Returning ENOENT also means negative entry, but by setting zero
  47. * ino the kernel may cache negative entries for entry_timeout
  48. * seconds.
  49. */
  50. uint64_t ino;
  51. /** Generation number for this entry.
  52. *
  53. * If the file system will be exported over NFS, the
  54. * ino/generation pairs need to be unique over the file
  55. * system's lifetime (rather than just the mount time). So if
  56. * the file system reuses an inode after it has been deleted,
  57. * it must assign a new, previously unused generation number
  58. * to the inode at the same time.
  59. *
  60. * The generation must be non-zero, otherwise FUSE will treat
  61. * it as an error.
  62. *
  63. */
  64. uint64_t generation;
  65. /** Inode attributes.
  66. *
  67. * Even if attr_timeout == 0, attr must be correct. For example,
  68. * for open(), FUSE uses attr.st_size from lookup() to determine
  69. * how many bytes to request. If this value is not correct,
  70. * incorrect data will be returned.
  71. */
  72. struct stat attr;
  73. fuse_timeouts_t timeout;
  74. };
  75. /** Additional context associated with requests */
  76. struct fuse_ctx
  77. {
  78. /** User ID of the calling process */
  79. uid_t uid;
  80. /** Group ID of the calling process */
  81. gid_t gid;
  82. /** Thread ID of the calling process */
  83. pid_t pid;
  84. /** Umask of the calling process (introduced in version 2.8) */
  85. mode_t umask;
  86. };
  87. /* ----------------------------------------------------------- *
  88. * Request methods and replies *
  89. * ----------------------------------------------------------- */
  90. /**
  91. * Low level filesystem operations
  92. *
  93. * Most of the methods (with the exception of init and destroy)
  94. * receive a request handle (fuse_req_t) as their first argument.
  95. * This handle must be passed to one of the specified reply functions.
  96. *
  97. * This may be done inside the method invocation, or after the call
  98. * has returned. The request handle is valid until one of the reply
  99. * functions is called.
  100. *
  101. * Other pointer arguments (name, fuse_file_info, etc) are not valid
  102. * after the call has returned, so if they are needed later, their
  103. * contents have to be copied.
  104. *
  105. * The filesystem sometimes needs to handle a return value of -ENOENT
  106. * from the reply function, which means, that the request was
  107. * interrupted, and the reply discarded. For example if
  108. * fuse_reply_open() return -ENOENT means, that the release method for
  109. * this file will not be called.
  110. */
  111. struct fuse_lowlevel_ops
  112. {
  113. void (*access)(fuse_req_t req, struct fuse_in_header *hdr);
  114. void (*bmap)(fuse_req_t req, const struct fuse_in_header *hdr);
  115. void (*copy_file_range)(fuse_req_t req, const struct fuse_in_header *hdr);
  116. void (*create)(fuse_req_t req, struct fuse_in_header *hdr);
  117. void (*destroy)(void *userdata);
  118. void (*fallocate)(fuse_req_t req, const struct fuse_in_header *hdr);
  119. void (*flock)(fuse_req_t req, uint64_t ino, fuse_file_info_t *fi, int op);
  120. void (*flush)(fuse_req_t req, struct fuse_in_header *hdr);
  121. void (*forget)(fuse_req_t req, struct fuse_in_header *hdr);
  122. void (*forget_multi)(fuse_req_t req, struct fuse_in_header *hdr);
  123. void (*fsync)(fuse_req_t req, struct fuse_in_header *hdr);
  124. void (*fsyncdir)(fuse_req_t req, struct fuse_in_header *hdr);
  125. void (*getattr)(fuse_req_t req, struct fuse_in_header *hdr);
  126. void (*getlk)(fuse_req_t req, const struct fuse_in_header *hdr);
  127. void (*getxattr)(fuse_req_t req, struct fuse_in_header *hdr);
  128. void (*init)(void *userdata, struct fuse_conn_info *conn);
  129. void (*ioctl)(fuse_req_t req, const struct fuse_in_header *hdr);
  130. void (*link)(fuse_req_t req, struct fuse_in_header *hdr);
  131. void (*listxattr)(fuse_req_t req, struct fuse_in_header *hdr);
  132. void (*lookup)(fuse_req_t req, struct fuse_in_header *hdr);
  133. void (*lseek)(fuse_req_t req, struct fuse_in_header *hdr);
  134. void (*mkdir)(fuse_req_t req, struct fuse_in_header *hdr);
  135. void (*mknod)(fuse_req_t req, struct fuse_in_header *hdr);
  136. void (*open)(fuse_req_t req, struct fuse_in_header *hdr);
  137. void (*opendir)(fuse_req_t req, struct fuse_in_header *hdr);
  138. void (*poll)(fuse_req_t req, const struct fuse_in_header *hdr);
  139. void (*read)(fuse_req_t req, struct fuse_in_header *hdr);
  140. void (*readdir)(fuse_req_t req, struct fuse_in_header *hdr);
  141. void (*readdir_plus)(fuse_req_t req, struct fuse_in_header *hdr);
  142. void (*readlink)(fuse_req_t req, struct fuse_in_header *hdr);
  143. void (*release)(fuse_req_t req, struct fuse_in_header *hdr);
  144. void (*releasedir)(fuse_req_t req, struct fuse_in_header *hdr);
  145. void (*removemapping)(fuse_req_t req, const struct fuse_in_header *hdr);
  146. void (*removexattr)(fuse_req_t req, const struct fuse_in_header *hdr);
  147. void (*rename)(fuse_req_t req, struct fuse_in_header *hdr);
  148. void (*rename2)(fuse_req_t req, struct fuse_in_header *hdr);
  149. void (*retrieve_reply)(fuse_req_t req, void *cookie, uint64_t ino, off_t offset);
  150. void (*rmdir)(fuse_req_t req, struct fuse_in_header *hdr);
  151. void (*setattr)(fuse_req_t req, struct fuse_in_header *hdr);
  152. void (*setlk)(fuse_req_t req, uint64_t ino, fuse_file_info_t *fi, struct flock *lock, int sleep);
  153. void (*setupmapping)(fuse_req_t req, const struct fuse_in_header *hdr);
  154. void (*setxattr)(fuse_req_t req, struct fuse_in_header *hdr);
  155. void (*statfs)(fuse_req_t req, struct fuse_in_header *hdr);
  156. void (*statx)(fuse_req_t req, struct fuse_in_header *hdr);
  157. void (*symlink)(fuse_req_t req, struct fuse_in_header *hdr);
  158. void (*syncfs)(fuse_req_t req, const struct fuse_in_header *hdr);
  159. void (*tmpfile)(fuse_req_t req, const struct fuse_in_header *hdr);
  160. void (*unlink)(fuse_req_t req, struct fuse_in_header *hdr);
  161. void (*write)(fuse_req_t req, struct fuse_in_header *hdr);
  162. };
  163. /**
  164. * Reply with an error code or success
  165. *
  166. * Possible requests:
  167. * all except forget
  168. *
  169. * unlink, rmdir, rename, flush, release, fsync, fsyncdir, setxattr,
  170. * removexattr and setlk may send a zero code
  171. *
  172. * @param req request handle
  173. * @param err the positive error value, or zero for success
  174. * @return zero for success, -errno for failure to send reply
  175. */
  176. int fuse_reply_err(fuse_req_t req, int err);
  177. /**
  178. * Don't send reply
  179. *
  180. * Possible requests:
  181. * forget
  182. *
  183. * @param req request handle
  184. */
  185. void fuse_reply_none(fuse_req_t req);
  186. /**
  187. * Reply with a directory entry
  188. *
  189. * Possible requests:
  190. * lookup, mknod, mkdir, symlink, link
  191. *
  192. * Side effects:
  193. * increments the lookup count on success
  194. *
  195. * @param req request handle
  196. * @param e the entry parameters
  197. * @return zero for success, -errno for failure to send reply
  198. */
  199. int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e);
  200. /**
  201. * Reply with a directory entry and open parameters
  202. *
  203. * currently the following members of 'fi' are used:
  204. * fh, direct_io, keep_cache
  205. *
  206. * Possible requests:
  207. * create
  208. *
  209. * Side effects:
  210. * increments the lookup count on success
  211. *
  212. * @param req request handle
  213. * @param e the entry parameters
  214. * @param fi file information
  215. * @return zero for success, -errno for failure to send reply
  216. */
  217. int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
  218. const fuse_file_info_t *fi);
  219. /**
  220. * Reply with attributes
  221. *
  222. * Possible requests:
  223. * getattr, setattr
  224. *
  225. * @param req request handle
  226. * @param attr the attributes
  227. * @param attr_timeout validity timeout (in seconds) for the attributes
  228. * @return zero for success, -errno for failure to send reply
  229. */
  230. int fuse_reply_attr(fuse_req_t req,
  231. const struct stat *attr,
  232. const uint64_t timeout);
  233. /**
  234. * Reply with the contents of a symbolic link
  235. *
  236. * Possible requests:
  237. * readlink
  238. *
  239. * @param req request handle
  240. * @param link symbolic link contents
  241. * @return zero for success, -errno for failure to send reply
  242. */
  243. int fuse_reply_readlink(fuse_req_t req, const char *link);
  244. /**
  245. * Reply with open parameters
  246. *
  247. * currently the following members of 'fi' are used:
  248. * fh, direct_io, keep_cache
  249. *
  250. * Possible requests:
  251. * open, opendir
  252. *
  253. * @param req request handle
  254. * @param fi file information
  255. * @return zero for success, -errno for failure to send reply
  256. */
  257. int fuse_reply_open(fuse_req_t req,
  258. const fuse_file_info_t *fi);
  259. /**
  260. * Reply with number of bytes written
  261. *
  262. * Possible requests:
  263. * write
  264. *
  265. * @param req request handle
  266. * @param count the number of bytes written
  267. * @return zero for success, -errno for failure to send reply
  268. */
  269. int fuse_reply_write(fuse_req_t req, size_t count);
  270. /**
  271. * Reply with data
  272. *
  273. * Possible requests:
  274. * read, readdir, getxattr, listxattr
  275. *
  276. * @param req request handle
  277. * @param buf buffer containing data
  278. * @param size the size of data in bytes
  279. * @return zero for success, -errno for failure to send reply
  280. */
  281. int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size);
  282. int fuse_reply_data(fuse_req_t req,
  283. char *buf,
  284. size_t bufsize);
  285. /**
  286. * Reply with data vector
  287. *
  288. * Possible requests:
  289. * read, readdir, getxattr, listxattr
  290. *
  291. * @param req request handle
  292. * @param iov the vector containing the data
  293. * @param count the size of vector
  294. * @return zero for success, -errno for failure to send reply
  295. */
  296. int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count);
  297. /**
  298. * Reply with filesystem statistics
  299. *
  300. * Possible requests:
  301. * statfs
  302. *
  303. * @param req request handle
  304. * @param stbuf filesystem statistics
  305. * @return zero for success, -errno for failure to send reply
  306. */
  307. int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf);
  308. /**
  309. * Reply with needed buffer size
  310. *
  311. * Possible requests:
  312. * getxattr, listxattr
  313. *
  314. * @param req request handle
  315. * @param count the buffer size needed in bytes
  316. * @return zero for success, -errno for failure to send reply
  317. */
  318. int fuse_reply_xattr(fuse_req_t req, size_t count);
  319. /**
  320. * Reply with file lock information
  321. *
  322. * Possible requests:
  323. * getlk
  324. *
  325. * @param req request handle
  326. * @param lock the lock information
  327. * @return zero for success, -errno for failure to send reply
  328. */
  329. int fuse_reply_lock(fuse_req_t req, const struct flock *lock);
  330. /**
  331. * Reply with block index
  332. *
  333. * Possible requests:
  334. * bmap
  335. *
  336. * @param req request handle
  337. * @param idx block index within device
  338. * @return zero for success, -errno for failure to send reply
  339. */
  340. int fuse_reply_bmap(fuse_req_t req, uint64_t idx);
  341. /**
  342. * Reply to ask for data fetch and output buffer preparation. ioctl
  343. * will be retried with the specified input data fetched and output
  344. * buffer prepared.
  345. *
  346. * Possible requests:
  347. * ioctl
  348. *
  349. * @param req request handle
  350. * @param in_iov iovec specifying data to fetch from the caller
  351. * @param in_count number of entries in in_iov
  352. * @param out_iov iovec specifying addresses to write output to
  353. * @param out_count number of entries in out_iov
  354. * @return zero for success, -errno for failure to send reply
  355. */
  356. int fuse_reply_ioctl_retry(fuse_req_t req,
  357. const struct iovec *in_iov, size_t in_count,
  358. const struct iovec *out_iov, size_t out_count);
  359. /**
  360. * Reply to finish ioctl
  361. *
  362. * Possible requests:
  363. * ioctl
  364. *
  365. * @param req request handle
  366. * @param result result to be passed to the caller
  367. * @param buf buffer containing output data
  368. * @param size length of output data
  369. */
  370. int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, uint32_t size);
  371. /**
  372. * Reply to finish ioctl with iov buffer
  373. *
  374. * Possible requests:
  375. * ioctl
  376. *
  377. * @param req request handle
  378. * @param result result to be passed to the caller
  379. * @param iov the vector containing the data
  380. * @param count the size of vector
  381. */
  382. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  383. int count);
  384. /**
  385. * Reply with poll result event mask
  386. *
  387. * @param req request handle
  388. * @param revents poll result event mask
  389. */
  390. int fuse_reply_poll(fuse_req_t req, unsigned revents);
  391. /* ----------------------------------------------------------- *
  392. * Notification *
  393. * ----------------------------------------------------------- */
  394. /**
  395. * Notify IO readiness event
  396. *
  397. * For more information, please read comment for poll operation.
  398. *
  399. * @param ph poll handle to notify IO readiness event for
  400. */
  401. int fuse_lowlevel_notify_poll(fuse_pollhandle_t *ph);
  402. /**
  403. * Notify to invalidate cache for an inode
  404. *
  405. * @param ch the channel through which to send the invalidation
  406. * @param ino the inode number
  407. * @param off the offset in the inode where to start invalidating
  408. * or negative to invalidate attributes only
  409. * @param len the amount of cache to invalidate or 0 for all
  410. * @return zero for success, -errno for failure
  411. */
  412. int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, uint64_t ino,
  413. off_t off, off_t len);
  414. /**
  415. * Notify to invalidate parent attributes and the dentry matching
  416. * parent/name
  417. *
  418. * To avoid a deadlock don't call this function from a filesystem operation and
  419. * don't call it with a lock held that can also be held by a filesystem
  420. * operation.
  421. *
  422. * @param ch the channel through which to send the invalidation
  423. * @param parent inode number
  424. * @param name file name
  425. * @param namelen strlen() of file name
  426. * @return zero for success, -errno for failure
  427. */
  428. int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, uint64_t parent,
  429. const char *name, size_t namelen);
  430. /**
  431. * Notify to invalidate parent attributes and delete the dentry matching
  432. * parent/name if the dentry's inode number matches child (otherwise it
  433. * will invalidate the matching dentry).
  434. *
  435. * To avoid a deadlock don't call this function from a filesystem operation and
  436. * don't call it with a lock held that can also be held by a filesystem
  437. * operation.
  438. *
  439. * @param ch the channel through which to send the notification
  440. * @param parent inode number
  441. * @param child inode number
  442. * @param name file name
  443. * @param namelen strlen() of file name
  444. * @return zero for success, -errno for failure
  445. */
  446. int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  447. uint64_t parent, uint64_t child,
  448. const char *name, size_t namelen);
  449. /**
  450. * Store data to the kernel buffers
  451. *
  452. * Synchronously store data in the kernel buffers belonging to the
  453. * given inode. The stored data is marked up-to-date (no read will be
  454. * performed against it, unless it's invalidated or evicted from the
  455. * cache).
  456. *
  457. * If the stored data overflows the current file size, then the size
  458. * is extended, similarly to a write(2) on the filesystem.
  459. *
  460. * If this function returns an error, then the store wasn't fully
  461. * completed, but it may have been partially completed.
  462. *
  463. * @param ch the channel through which to send the invalidation
  464. * @param ino the inode number
  465. * @param offset the starting offset into the file to store to
  466. * @param bufv buffer vector
  467. * @param flags flags controlling the copy
  468. * @return zero for success, -errno for failure
  469. */
  470. int fuse_lowlevel_notify_store(struct fuse_chan *ch, uint64_t ino,
  471. off_t offset, struct fuse_bufvec *bufv,
  472. enum fuse_buf_copy_flags flags);
  473. /**
  474. * Retrieve data from the kernel buffers
  475. *
  476. * Retrieve data in the kernel buffers belonging to the given inode.
  477. * If successful then the retrieve_reply() method will be called with
  478. * the returned data.
  479. *
  480. * Only present pages are returned in the retrieve reply. Retrieving
  481. * stops when it finds a non-present page and only data prior to that is
  482. * returned.
  483. *
  484. * If this function returns an error, then the retrieve will not be
  485. * completed and no reply will be sent.
  486. *
  487. * This function doesn't change the dirty state of pages in the kernel
  488. * buffer. For dirty pages the write() method will be called
  489. * regardless of having been retrieved previously.
  490. *
  491. * @param ch the channel through which to send the invalidation
  492. * @param ino the inode number
  493. * @param size the number of bytes to retrieve
  494. * @param offset the starting offset into the file to retrieve from
  495. * @param cookie user data to supply to the reply callback
  496. * @return zero for success, -errno for failure
  497. */
  498. int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, uint64_t ino,
  499. size_t size, off_t offset, void *cookie);
  500. /* ----------------------------------------------------------- *
  501. * Utility functions *
  502. * ----------------------------------------------------------- */
  503. /**
  504. * Get the userdata from the request
  505. *
  506. * @param req request handle
  507. * @return the user data passed to fuse_lowlevel_new()
  508. */
  509. void *fuse_req_userdata(fuse_req_t req);
  510. /**
  511. * Get the context from the request
  512. *
  513. * The pointer returned by this function will only be valid for the
  514. * request's lifetime
  515. *
  516. * @param req request handle
  517. * @return the context structure
  518. */
  519. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req);
  520. /**
  521. * Get the current supplementary group IDs for the specified request
  522. *
  523. * Similar to the getgroups(2) system call, except the return value is
  524. * always the total number of group IDs, even if it is larger than the
  525. * specified size.
  526. *
  527. * The current fuse kernel module in linux (as of 2.6.30) doesn't pass
  528. * the group list to userspace, hence this function needs to parse
  529. * "/proc/$TID/task/$TID/status" to get the group IDs.
  530. *
  531. * This feature may not be supported on all operating systems. In
  532. * such a case this function will return -ENOSYS.
  533. *
  534. * @param req request handle
  535. * @param size size of given array
  536. * @param list array of group IDs to be filled in
  537. * @return the total number of supplementary group IDs or -errno on failure
  538. */
  539. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]);
  540. /* ----------------------------------------------------------- *
  541. * Filesystem setup *
  542. * ----------------------------------------------------------- */
  543. /* Deprecated, don't use */
  544. int fuse_lowlevel_is_lib_option(const char *opt);
  545. /**
  546. * Create a low level session
  547. *
  548. * @param args argument vector
  549. * @param op the low level filesystem operations
  550. * @param op_size sizeof(struct fuse_lowlevel_ops)
  551. * @param userdata user data
  552. * @return the created session object, or NULL on failure
  553. */
  554. struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
  555. const struct fuse_lowlevel_ops *op,
  556. size_t op_size, void *userdata);
  557. /* ----------------------------------------------------------- *
  558. * Session interface *
  559. * ----------------------------------------------------------- */
  560. struct fuse_session *fuse_session_new(void *data,
  561. void *receive_buf,
  562. void *process_buf,
  563. void *destroy);
  564. void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch);
  565. void fuse_session_remove_chan(struct fuse_chan *ch);
  566. void fuse_session_destroy(struct fuse_session *se);
  567. void fuse_session_exit(struct fuse_session *se);
  568. int fuse_session_exited(struct fuse_session *se);
  569. void fuse_session_reset(struct fuse_session *se);
  570. void *fuse_session_data(struct fuse_session *se);
  571. int fuse_session_receive(struct fuse_session *se,
  572. struct fuse_buf *buf);
  573. void fuse_session_process(struct fuse_session *se,
  574. const void *buf,
  575. const size_t bufsize);
  576. int fuse_session_loop_mt(struct fuse_session *se,
  577. const int read_thread_count,
  578. const int process_thread_count,
  579. const int process_thread_queue_depth,
  580. const char *pin_threads_type);
  581. /* ----------------------------------------------------------- *
  582. * Channel interface *
  583. * ----------------------------------------------------------- */
  584. struct fuse_chan *fuse_chan_new(int fd, size_t bufsize);
  585. /**
  586. * Query the file descriptor of the channel
  587. *
  588. * @param ch the channel
  589. * @return the file descriptor passed to fuse_chan_new()
  590. */
  591. int fuse_chan_fd(struct fuse_chan *ch);
  592. /**
  593. * Query the minimal receive buffer size
  594. *
  595. * @param ch the channel
  596. * @return the buffer size passed to fuse_chan_new()
  597. */
  598. size_t fuse_chan_bufsize(struct fuse_chan *ch);
  599. /**
  600. * Query the user data
  601. *
  602. * @param ch the channel
  603. * @return the user data passed to fuse_chan_new()
  604. */
  605. void *fuse_chan_data(struct fuse_chan *ch);
  606. /**
  607. * Query the session to which this channel is assigned
  608. *
  609. * @param ch the channel
  610. * @return the session, or NULL if the channel is not assigned
  611. */
  612. struct fuse_session *fuse_chan_session(struct fuse_chan *ch);
  613. void fuse_chan_destroy(struct fuse_chan *ch);
  614. EXTERN_C_END
  615. #endif /* _FUSE_LOWLEVEL_H_ */