fuse.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FUSE_H_
  17. #define FUSE_H_
  18. #include <dirent.h>
  19. #include <fcntl.h>
  20. #include <linux/fuse.h>
  21. #include <pthread.h>
  22. #include <stdbool.h>
  23. #include <stdlib.h>
  24. #include <sys/param.h>
  25. #include <sys/stat.h>
  26. #include <sys/statfs.h>
  27. #include <sys/types.h>
  28. #include <sys/uio.h>
  29. #include <unistd.h>
  30. #include <map>
  31. #include <string>
  32. #include <android-base/logging.h>
  33. #include <cutils/fs.h>
  34. #include <cutils/multiuser.h>
  35. #include <packagelistparser/packagelistparser.h>
  36. #include <private/android_filesystem_config.h>
  37. #define FUSE_TRACE 0
  38. #if FUSE_TRACE
  39. static constexpr bool kEnableDLog = true;
  40. #else // FUSE_TRACE == 0
  41. static constexpr bool kEnableDLog = false;
  42. #endif
  43. // Use same strategy as DCHECK().
  44. #define DLOG(x) \
  45. if (kEnableDLog) LOG(x)
  46. /* Maximum number of bytes to write in one request. */
  47. #define MAX_WRITE (256 * 1024)
  48. /* Maximum number of bytes to read in one request. */
  49. #define MAX_READ (128 * 1024)
  50. /* Largest possible request.
  51. * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
  52. * the largest possible data payload. */
  53. #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
  54. namespace {
  55. struct CaseInsensitiveCompare {
  56. bool operator()(const std::string& lhs, const std::string& rhs) const {
  57. return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
  58. }
  59. };
  60. }
  61. using AppIdMap = std::map<std::string, appid_t, CaseInsensitiveCompare>;
  62. /* Permission mode for a specific node. Controls how file permissions
  63. * are derived for children nodes. */
  64. typedef enum {
  65. /* Nothing special; this node should just inherit from its parent. */
  66. PERM_INHERIT,
  67. /* This node is one level above a normal root; used for legacy layouts
  68. * which use the first level to represent user_id. */
  69. PERM_PRE_ROOT,
  70. /* This node is "/" */
  71. PERM_ROOT,
  72. /* This node is "/Android" */
  73. PERM_ANDROID,
  74. /* This node is "/Android/data" */
  75. PERM_ANDROID_DATA,
  76. /* This node is "/Android/obb" */
  77. PERM_ANDROID_OBB,
  78. /* This node is "/Android/media" */
  79. PERM_ANDROID_MEDIA,
  80. } perm_t;
  81. struct handle {
  82. int fd;
  83. };
  84. struct dirhandle {
  85. DIR *d;
  86. };
  87. struct node {
  88. __u32 refcount;
  89. __u64 nid;
  90. __u64 gen;
  91. /*
  92. * The inode number for this FUSE node. Note that this isn't stable across
  93. * multiple invocations of the FUSE daemon.
  94. */
  95. __u32 ino;
  96. /* State derived based on current position in hierarchy. */
  97. perm_t perm;
  98. userid_t userid;
  99. uid_t uid;
  100. bool under_android;
  101. struct node *next; /* per-dir sibling list */
  102. struct node *child; /* first contained file by this dir */
  103. struct node *parent; /* containing directory */
  104. size_t namelen;
  105. char *name;
  106. /* If non-null, this is the real name of the file in the underlying storage.
  107. * This may differ from the field "name" only by case.
  108. * strlen(actual_name) will always equal strlen(name), so it is safe to use
  109. * namelen for both fields.
  110. */
  111. char *actual_name;
  112. /* If non-null, an exact underlying path that should be grafted into this
  113. * position. Used to support things like OBB. */
  114. char* graft_path;
  115. size_t graft_pathlen;
  116. bool deleted;
  117. };
  118. /* Global data for all FUSE mounts */
  119. struct fuse_global {
  120. pthread_mutex_t lock;
  121. uid_t uid;
  122. gid_t gid;
  123. bool multi_user;
  124. char source_path[PATH_MAX];
  125. char obb_path[PATH_MAX];
  126. AppIdMap* package_to_appid;
  127. __u64 next_generation;
  128. struct node root;
  129. /* Used to allocate unique inode numbers for fuse nodes. We use
  130. * a simple counter based scheme where inode numbers from deleted
  131. * nodes aren't reused. Note that inode allocations are not stable
  132. * across multiple invocation of the sdcard daemon, but that shouldn't
  133. * be a huge problem in practice.
  134. *
  135. * Note that we restrict inodes to 32 bit unsigned integers to prevent
  136. * truncation on 32 bit processes when unsigned long long stat.st_ino is
  137. * assigned to an unsigned long ino_t type in an LP32 process.
  138. *
  139. * Also note that fuse_attr and fuse_dirent inode values are 64 bits wide
  140. * on both LP32 and LP64, but the fuse kernel code doesn't squash 64 bit
  141. * inode numbers into 32 bit values on 64 bit kernels (see fuse_squash_ino
  142. * in fs/fuse/inode.c).
  143. *
  144. * Accesses must be guarded by |lock|.
  145. */
  146. __u32 inode_ctr;
  147. struct fuse* fuse_default;
  148. struct fuse* fuse_read;
  149. struct fuse* fuse_write;
  150. struct fuse* fuse_full;
  151. };
  152. /* Single FUSE mount */
  153. struct fuse {
  154. struct fuse_global* global;
  155. char dest_path[PATH_MAX];
  156. int fd;
  157. gid_t gid;
  158. mode_t mask;
  159. };
  160. /* Private data used by a single FUSE handler */
  161. struct fuse_handler {
  162. struct fuse* fuse;
  163. int token;
  164. /* To save memory, we never use the contents of the request buffer and the read
  165. * buffer at the same time. This allows us to share the underlying storage. */
  166. union {
  167. __u8 request_buffer[MAX_REQUEST_SIZE];
  168. __u8 read_buffer[MAX_READ + PAGE_SIZE];
  169. };
  170. };
  171. void handle_fuse_requests(struct fuse_handler* handler);
  172. void derive_permissions_recursive_locked(struct fuse* fuse, struct node *parent);
  173. #endif /* FUSE_H_ */