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.

678 lines
21 KiB

10 months ago
10 months ago
  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 (*mkdir)(fuse_req_t req, struct fuse_in_header *hdr);
  134. void (*mknod)(fuse_req_t req, struct fuse_in_header *hdr);
  135. void (*open)(fuse_req_t req, struct fuse_in_header *hdr);
  136. void (*opendir)(fuse_req_t req, struct fuse_in_header *hdr);
  137. void (*poll)(fuse_req_t req, const struct fuse_in_header *hdr);
  138. void (*read)(fuse_req_t req, struct fuse_in_header *hdr);
  139. void (*readdir)(fuse_req_t req, struct fuse_in_header *hdr);
  140. void (*readdir_plus)(fuse_req_t req, struct fuse_in_header *hdr);
  141. void (*readlink)(fuse_req_t req, struct fuse_in_header *hdr);
  142. void (*release)(fuse_req_t req, struct fuse_in_header *hdr);
  143. void (*releasedir)(fuse_req_t req, struct fuse_in_header *hdr);
  144. void (*removemapping)(fuse_req_t req, const struct fuse_in_header *hdr);
  145. void (*removexattr)(fuse_req_t req, const struct fuse_in_header *hdr);
  146. void (*rename)(fuse_req_t req, struct fuse_in_header *hdr);
  147. void (*retrieve_reply)(fuse_req_t req, void *cookie, uint64_t ino, off_t offset);
  148. void (*rmdir)(fuse_req_t req, struct fuse_in_header *hdr);
  149. void (*setattr)(fuse_req_t req, struct fuse_in_header *hdr);
  150. void (*setlk)(fuse_req_t req, uint64_t ino, fuse_file_info_t *fi, struct flock *lock, int sleep);
  151. void (*setupmapping)(fuse_req_t req, const struct fuse_in_header *hdr);
  152. void (*setxattr)(fuse_req_t req, struct fuse_in_header *hdr);
  153. void (*statfs)(fuse_req_t req, struct fuse_in_header *hdr);
  154. void (*symlink)(fuse_req_t req, struct fuse_in_header *hdr);
  155. void (*syncfs)(fuse_req_t req, const struct fuse_in_header *hdr);
  156. void (*tmpfile)(fuse_req_t req, const struct fuse_in_header *hdr);
  157. void (*unlink)(fuse_req_t req, struct fuse_in_header *hdr);
  158. void (*write)(fuse_req_t req, struct fuse_in_header *hdr);
  159. };
  160. /**
  161. * Reply with an error code or success
  162. *
  163. * Possible requests:
  164. * all except forget
  165. *
  166. * unlink, rmdir, rename, flush, release, fsync, fsyncdir, setxattr,
  167. * removexattr and setlk may send a zero code
  168. *
  169. * @param req request handle
  170. * @param err the positive error value, or zero for success
  171. * @return zero for success, -errno for failure to send reply
  172. */
  173. int fuse_reply_err(fuse_req_t req, int err);
  174. /**
  175. * Don't send reply
  176. *
  177. * Possible requests:
  178. * forget
  179. *
  180. * @param req request handle
  181. */
  182. void fuse_reply_none(fuse_req_t req);
  183. /**
  184. * Reply with a directory entry
  185. *
  186. * Possible requests:
  187. * lookup, mknod, mkdir, symlink, link
  188. *
  189. * Side effects:
  190. * increments the lookup count on success
  191. *
  192. * @param req request handle
  193. * @param e the entry parameters
  194. * @return zero for success, -errno for failure to send reply
  195. */
  196. int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e);
  197. /**
  198. * Reply with a directory entry and open parameters
  199. *
  200. * currently the following members of 'fi' are used:
  201. * fh, direct_io, keep_cache
  202. *
  203. * Possible requests:
  204. * create
  205. *
  206. * Side effects:
  207. * increments the lookup count on success
  208. *
  209. * @param req request handle
  210. * @param e the entry parameters
  211. * @param fi file information
  212. * @return zero for success, -errno for failure to send reply
  213. */
  214. int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
  215. const fuse_file_info_t *fi);
  216. /**
  217. * Reply with attributes
  218. *
  219. * Possible requests:
  220. * getattr, setattr
  221. *
  222. * @param req request handle
  223. * @param attr the attributes
  224. * @param attr_timeout validity timeout (in seconds) for the attributes
  225. * @return zero for success, -errno for failure to send reply
  226. */
  227. int fuse_reply_attr(fuse_req_t req,
  228. const struct stat *attr,
  229. const uint64_t timeout);
  230. /**
  231. * Reply with the contents of a symbolic link
  232. *
  233. * Possible requests:
  234. * readlink
  235. *
  236. * @param req request handle
  237. * @param link symbolic link contents
  238. * @return zero for success, -errno for failure to send reply
  239. */
  240. int fuse_reply_readlink(fuse_req_t req, const char *link);
  241. /**
  242. * Reply with open parameters
  243. *
  244. * currently the following members of 'fi' are used:
  245. * fh, direct_io, keep_cache
  246. *
  247. * Possible requests:
  248. * open, opendir
  249. *
  250. * @param req request handle
  251. * @param fi file information
  252. * @return zero for success, -errno for failure to send reply
  253. */
  254. int fuse_reply_open(fuse_req_t req,
  255. const fuse_file_info_t *fi);
  256. /**
  257. * Reply with number of bytes written
  258. *
  259. * Possible requests:
  260. * write
  261. *
  262. * @param req request handle
  263. * @param count the number of bytes written
  264. * @return zero for success, -errno for failure to send reply
  265. */
  266. int fuse_reply_write(fuse_req_t req, size_t count);
  267. /**
  268. * Reply with data
  269. *
  270. * Possible requests:
  271. * read, readdir, getxattr, listxattr
  272. *
  273. * @param req request handle
  274. * @param buf buffer containing data
  275. * @param size the size of data in bytes
  276. * @return zero for success, -errno for failure to send reply
  277. */
  278. int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size);
  279. int fuse_reply_data(fuse_req_t req,
  280. char *buf,
  281. size_t bufsize);
  282. /**
  283. * Reply with data vector
  284. *
  285. * Possible requests:
  286. * read, readdir, getxattr, listxattr
  287. *
  288. * @param req request handle
  289. * @param iov the vector containing the data
  290. * @param count the size of vector
  291. * @return zero for success, -errno for failure to send reply
  292. */
  293. int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count);
  294. /**
  295. * Reply with filesystem statistics
  296. *
  297. * Possible requests:
  298. * statfs
  299. *
  300. * @param req request handle
  301. * @param stbuf filesystem statistics
  302. * @return zero for success, -errno for failure to send reply
  303. */
  304. int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf);
  305. /**
  306. * Reply with needed buffer size
  307. *
  308. * Possible requests:
  309. * getxattr, listxattr
  310. *
  311. * @param req request handle
  312. * @param count the buffer size needed in bytes
  313. * @return zero for success, -errno for failure to send reply
  314. */
  315. int fuse_reply_xattr(fuse_req_t req, size_t count);
  316. /**
  317. * Reply with file lock information
  318. *
  319. * Possible requests:
  320. * getlk
  321. *
  322. * @param req request handle
  323. * @param lock the lock information
  324. * @return zero for success, -errno for failure to send reply
  325. */
  326. int fuse_reply_lock(fuse_req_t req, const struct flock *lock);
  327. /**
  328. * Reply with block index
  329. *
  330. * Possible requests:
  331. * bmap
  332. *
  333. * @param req request handle
  334. * @param idx block index within device
  335. * @return zero for success, -errno for failure to send reply
  336. */
  337. int fuse_reply_bmap(fuse_req_t req, uint64_t idx);
  338. /**
  339. * Reply to ask for data fetch and output buffer preparation. ioctl
  340. * will be retried with the specified input data fetched and output
  341. * buffer prepared.
  342. *
  343. * Possible requests:
  344. * ioctl
  345. *
  346. * @param req request handle
  347. * @param in_iov iovec specifying data to fetch from the caller
  348. * @param in_count number of entries in in_iov
  349. * @param out_iov iovec specifying addresses to write output to
  350. * @param out_count number of entries in out_iov
  351. * @return zero for success, -errno for failure to send reply
  352. */
  353. int fuse_reply_ioctl_retry(fuse_req_t req,
  354. const struct iovec *in_iov, size_t in_count,
  355. const struct iovec *out_iov, size_t out_count);
  356. /**
  357. * Reply to finish ioctl
  358. *
  359. * Possible requests:
  360. * ioctl
  361. *
  362. * @param req request handle
  363. * @param result result to be passed to the caller
  364. * @param buf buffer containing output data
  365. * @param size length of output data
  366. */
  367. int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, uint32_t size);
  368. /**
  369. * Reply to finish ioctl with iov buffer
  370. *
  371. * Possible requests:
  372. * ioctl
  373. *
  374. * @param req request handle
  375. * @param result result to be passed to the caller
  376. * @param iov the vector containing the data
  377. * @param count the size of vector
  378. */
  379. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  380. int count);
  381. /**
  382. * Reply with poll result event mask
  383. *
  384. * @param req request handle
  385. * @param revents poll result event mask
  386. */
  387. int fuse_reply_poll(fuse_req_t req, unsigned revents);
  388. /* ----------------------------------------------------------- *
  389. * Notification *
  390. * ----------------------------------------------------------- */
  391. /**
  392. * Notify IO readiness event
  393. *
  394. * For more information, please read comment for poll operation.
  395. *
  396. * @param ph poll handle to notify IO readiness event for
  397. */
  398. int fuse_lowlevel_notify_poll(fuse_pollhandle_t *ph);
  399. /**
  400. * Notify to invalidate cache for an inode
  401. *
  402. * @param ch the channel through which to send the invalidation
  403. * @param ino the inode number
  404. * @param off the offset in the inode where to start invalidating
  405. * or negative to invalidate attributes only
  406. * @param len the amount of cache to invalidate or 0 for all
  407. * @return zero for success, -errno for failure
  408. */
  409. int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, uint64_t ino,
  410. off_t off, off_t len);
  411. /**
  412. * Notify to invalidate parent attributes and the dentry matching
  413. * parent/name
  414. *
  415. * To avoid a deadlock don't call this function from a filesystem operation and
  416. * don't call it with a lock held that can also be held by a filesystem
  417. * operation.
  418. *
  419. * @param ch the channel through which to send the invalidation
  420. * @param parent inode number
  421. * @param name file name
  422. * @param namelen strlen() of file name
  423. * @return zero for success, -errno for failure
  424. */
  425. int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, uint64_t parent,
  426. const char *name, size_t namelen);
  427. /**
  428. * Notify to invalidate parent attributes and delete the dentry matching
  429. * parent/name if the dentry's inode number matches child (otherwise it
  430. * will invalidate the matching dentry).
  431. *
  432. * To avoid a deadlock don't call this function from a filesystem operation and
  433. * don't call it with a lock held that can also be held by a filesystem
  434. * operation.
  435. *
  436. * @param ch the channel through which to send the notification
  437. * @param parent inode number
  438. * @param child inode number
  439. * @param name file name
  440. * @param namelen strlen() of file name
  441. * @return zero for success, -errno for failure
  442. */
  443. int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  444. uint64_t parent, uint64_t child,
  445. const char *name, size_t namelen);
  446. /**
  447. * Store data to the kernel buffers
  448. *
  449. * Synchronously store data in the kernel buffers belonging to the
  450. * given inode. The stored data is marked up-to-date (no read will be
  451. * performed against it, unless it's invalidated or evicted from the
  452. * cache).
  453. *
  454. * If the stored data overflows the current file size, then the size
  455. * is extended, similarly to a write(2) on the filesystem.
  456. *
  457. * If this function returns an error, then the store wasn't fully
  458. * completed, but it may have been partially completed.
  459. *
  460. * @param ch the channel through which to send the invalidation
  461. * @param ino the inode number
  462. * @param offset the starting offset into the file to store to
  463. * @param bufv buffer vector
  464. * @param flags flags controlling the copy
  465. * @return zero for success, -errno for failure
  466. */
  467. int fuse_lowlevel_notify_store(struct fuse_chan *ch, uint64_t ino,
  468. off_t offset, struct fuse_bufvec *bufv,
  469. enum fuse_buf_copy_flags flags);
  470. /**
  471. * Retrieve data from the kernel buffers
  472. *
  473. * Retrieve data in the kernel buffers belonging to the given inode.
  474. * If successful then the retrieve_reply() method will be called with
  475. * the returned data.
  476. *
  477. * Only present pages are returned in the retrieve reply. Retrieving
  478. * stops when it finds a non-present page and only data prior to that is
  479. * returned.
  480. *
  481. * If this function returns an error, then the retrieve will not be
  482. * completed and no reply will be sent.
  483. *
  484. * This function doesn't change the dirty state of pages in the kernel
  485. * buffer. For dirty pages the write() method will be called
  486. * regardless of having been retrieved previously.
  487. *
  488. * @param ch the channel through which to send the invalidation
  489. * @param ino the inode number
  490. * @param size the number of bytes to retrieve
  491. * @param offset the starting offset into the file to retrieve from
  492. * @param cookie user data to supply to the reply callback
  493. * @return zero for success, -errno for failure
  494. */
  495. int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, uint64_t ino,
  496. size_t size, off_t offset, void *cookie);
  497. /* ----------------------------------------------------------- *
  498. * Utility functions *
  499. * ----------------------------------------------------------- */
  500. /**
  501. * Get the userdata from the request
  502. *
  503. * @param req request handle
  504. * @return the user data passed to fuse_lowlevel_new()
  505. */
  506. void *fuse_req_userdata(fuse_req_t req);
  507. /**
  508. * Get the context from the request
  509. *
  510. * The pointer returned by this function will only be valid for the
  511. * request's lifetime
  512. *
  513. * @param req request handle
  514. * @return the context structure
  515. */
  516. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req);
  517. /**
  518. * Get the current supplementary group IDs for the specified request
  519. *
  520. * Similar to the getgroups(2) system call, except the return value is
  521. * always the total number of group IDs, even if it is larger than the
  522. * specified size.
  523. *
  524. * The current fuse kernel module in linux (as of 2.6.30) doesn't pass
  525. * the group list to userspace, hence this function needs to parse
  526. * "/proc/$TID/task/$TID/status" to get the group IDs.
  527. *
  528. * This feature may not be supported on all operating systems. In
  529. * such a case this function will return -ENOSYS.
  530. *
  531. * @param req request handle
  532. * @param size size of given array
  533. * @param list array of group IDs to be filled in
  534. * @return the total number of supplementary group IDs or -errno on failure
  535. */
  536. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]);
  537. /* ----------------------------------------------------------- *
  538. * Filesystem setup *
  539. * ----------------------------------------------------------- */
  540. /* Deprecated, don't use */
  541. int fuse_lowlevel_is_lib_option(const char *opt);
  542. /**
  543. * Create a low level session
  544. *
  545. * @param args argument vector
  546. * @param op the low level filesystem operations
  547. * @param op_size sizeof(struct fuse_lowlevel_ops)
  548. * @param userdata user data
  549. * @return the created session object, or NULL on failure
  550. */
  551. struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
  552. const struct fuse_lowlevel_ops *op,
  553. size_t op_size, void *userdata);
  554. /* ----------------------------------------------------------- *
  555. * Session interface *
  556. * ----------------------------------------------------------- */
  557. struct fuse_session *fuse_session_new(void *data,
  558. void *receive_buf,
  559. void *process_buf,
  560. void *destroy);
  561. void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch);
  562. void fuse_session_remove_chan(struct fuse_chan *ch);
  563. void fuse_session_destroy(struct fuse_session *se);
  564. void fuse_session_exit(struct fuse_session *se);
  565. int fuse_session_exited(struct fuse_session *se);
  566. void fuse_session_reset(struct fuse_session *se);
  567. void *fuse_session_data(struct fuse_session *se);
  568. int fuse_session_receive(struct fuse_session *se,
  569. struct fuse_buf *buf);
  570. void fuse_session_process(struct fuse_session *se,
  571. const void *buf,
  572. const size_t bufsize);
  573. int fuse_session_loop_mt(struct fuse_session *se,
  574. const int read_thread_count,
  575. const int process_thread_count,
  576. const int process_thread_queue_depth,
  577. const char *pin_threads_type);
  578. /* ----------------------------------------------------------- *
  579. * Channel interface *
  580. * ----------------------------------------------------------- */
  581. struct fuse_chan *fuse_chan_new(int fd, size_t bufsize);
  582. /**
  583. * Query the file descriptor of the channel
  584. *
  585. * @param ch the channel
  586. * @return the file descriptor passed to fuse_chan_new()
  587. */
  588. int fuse_chan_fd(struct fuse_chan *ch);
  589. /**
  590. * Query the minimal receive buffer size
  591. *
  592. * @param ch the channel
  593. * @return the buffer size passed to fuse_chan_new()
  594. */
  595. size_t fuse_chan_bufsize(struct fuse_chan *ch);
  596. /**
  597. * Query the user data
  598. *
  599. * @param ch the channel
  600. * @return the user data passed to fuse_chan_new()
  601. */
  602. void *fuse_chan_data(struct fuse_chan *ch);
  603. /**
  604. * Query the session to which this channel is assigned
  605. *
  606. * @param ch the channel
  607. * @return the session, or NULL if the channel is not assigned
  608. */
  609. struct fuse_session *fuse_chan_session(struct fuse_chan *ch);
  610. void fuse_chan_destroy(struct fuse_chan *ch);
  611. EXTERN_C_END
  612. #endif /* _FUSE_LOWLEVEL_H_ */