Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/fuse.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/wait.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | #include <linux/mm.h> |
| 15 | #include <linux/backing-dev.h> |
| 16 | #include <asm/semaphore.h> |
| 17 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 18 | /** Max number of pages that can be used in a single read request */ |
| 19 | #define FUSE_MAX_PAGES_PER_REQ 32 |
| 20 | |
| 21 | /** If more requests are outstanding, then the operation will block */ |
| 22 | #define FUSE_MAX_OUTSTANDING 10 |
| 23 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 24 | /** FUSE inode */ |
| 25 | struct fuse_inode { |
| 26 | /** Inode data */ |
| 27 | struct inode inode; |
| 28 | |
| 29 | /** Unique ID, which identifies the inode between userspace |
| 30 | * and kernel */ |
| 31 | u64 nodeid; |
| 32 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame^] | 33 | /** The request used for sending the FORGET message */ |
| 34 | struct fuse_req *forget_req; |
| 35 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 36 | /** Time in jiffies until the file attributes are valid */ |
| 37 | unsigned long i_time; |
| 38 | }; |
| 39 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 40 | /** One input argument of a request */ |
| 41 | struct fuse_in_arg { |
| 42 | unsigned size; |
| 43 | const void *value; |
| 44 | }; |
| 45 | |
| 46 | /** The request input */ |
| 47 | struct fuse_in { |
| 48 | /** The request header */ |
| 49 | struct fuse_in_header h; |
| 50 | |
| 51 | /** True if the data for the last argument is in req->pages */ |
| 52 | unsigned argpages:1; |
| 53 | |
| 54 | /** Number of arguments */ |
| 55 | unsigned numargs; |
| 56 | |
| 57 | /** Array of arguments */ |
| 58 | struct fuse_in_arg args[3]; |
| 59 | }; |
| 60 | |
| 61 | /** One output argument of a request */ |
| 62 | struct fuse_arg { |
| 63 | unsigned size; |
| 64 | void *value; |
| 65 | }; |
| 66 | |
| 67 | /** The request output */ |
| 68 | struct fuse_out { |
| 69 | /** Header returned from userspace */ |
| 70 | struct fuse_out_header h; |
| 71 | |
| 72 | /** Last argument is variable length (can be shorter than |
| 73 | arg->size) */ |
| 74 | unsigned argvar:1; |
| 75 | |
| 76 | /** Last argument is a list of pages to copy data to */ |
| 77 | unsigned argpages:1; |
| 78 | |
| 79 | /** Zero partially or not copied pages */ |
| 80 | unsigned page_zeroing:1; |
| 81 | |
| 82 | /** Number or arguments */ |
| 83 | unsigned numargs; |
| 84 | |
| 85 | /** Array of arguments */ |
| 86 | struct fuse_arg args[3]; |
| 87 | }; |
| 88 | |
| 89 | struct fuse_req; |
| 90 | struct fuse_conn; |
| 91 | |
| 92 | /** |
| 93 | * A request to the client |
| 94 | */ |
| 95 | struct fuse_req { |
| 96 | /** This can be on either unused_list, pending or processing |
| 97 | lists in fuse_conn */ |
| 98 | struct list_head list; |
| 99 | |
| 100 | /** refcount */ |
| 101 | atomic_t count; |
| 102 | |
| 103 | /** True if the request has reply */ |
| 104 | unsigned isreply:1; |
| 105 | |
| 106 | /** The request is preallocated */ |
| 107 | unsigned preallocated:1; |
| 108 | |
| 109 | /** The request was interrupted */ |
| 110 | unsigned interrupted:1; |
| 111 | |
| 112 | /** Request is sent in the background */ |
| 113 | unsigned background:1; |
| 114 | |
| 115 | /** Data is being copied to/from the request */ |
| 116 | unsigned locked:1; |
| 117 | |
| 118 | /** Request has been sent to userspace */ |
| 119 | unsigned sent:1; |
| 120 | |
| 121 | /** The request is finished */ |
| 122 | unsigned finished:1; |
| 123 | |
| 124 | /** The request input */ |
| 125 | struct fuse_in in; |
| 126 | |
| 127 | /** The request output */ |
| 128 | struct fuse_out out; |
| 129 | |
| 130 | /** Used to wake up the task waiting for completion of request*/ |
| 131 | wait_queue_head_t waitq; |
| 132 | |
| 133 | /** Data for asynchronous requests */ |
| 134 | union { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame^] | 135 | struct fuse_forget_in forget_in; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 136 | struct fuse_init_in_out init_in_out; |
| 137 | } misc; |
| 138 | |
| 139 | /** page vector */ |
| 140 | struct page *pages[FUSE_MAX_PAGES_PER_REQ]; |
| 141 | |
| 142 | /** number of pages in vector */ |
| 143 | unsigned num_pages; |
| 144 | |
| 145 | /** offset of data on first page */ |
| 146 | unsigned page_offset; |
| 147 | |
| 148 | /** Inode used in the request */ |
| 149 | struct inode *inode; |
| 150 | |
| 151 | /** Second inode used in the request (or NULL) */ |
| 152 | struct inode *inode2; |
| 153 | |
| 154 | /** File used in the request (or NULL) */ |
| 155 | struct file *file; |
| 156 | }; |
| 157 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 158 | /** |
| 159 | * A Fuse connection. |
| 160 | * |
| 161 | * This structure is created, when the filesystem is mounted, and is |
| 162 | * destroyed, when the client device is closed and the filesystem is |
| 163 | * unmounted. |
| 164 | */ |
| 165 | struct fuse_conn { |
| 166 | /** The superblock of the mounted filesystem */ |
| 167 | struct super_block *sb; |
| 168 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 169 | /** The opened client device */ |
| 170 | struct file *file; |
| 171 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 172 | /** The user id for this mount */ |
| 173 | uid_t user_id; |
| 174 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 175 | /** Readers of the connection are waiting on this */ |
| 176 | wait_queue_head_t waitq; |
| 177 | |
| 178 | /** The list of pending requests */ |
| 179 | struct list_head pending; |
| 180 | |
| 181 | /** The list of requests being processed */ |
| 182 | struct list_head processing; |
| 183 | |
| 184 | /** Controls the maximum number of outstanding requests */ |
| 185 | struct semaphore outstanding_sem; |
| 186 | |
| 187 | /** This counts the number of outstanding requests if |
| 188 | outstanding_sem would go negative */ |
| 189 | unsigned outstanding_debt; |
| 190 | |
| 191 | /** The list of unused requests */ |
| 192 | struct list_head unused_list; |
| 193 | |
| 194 | /** The next unique request id */ |
| 195 | u64 reqctr; |
| 196 | |
| 197 | /** Connection failed (version mismatch) */ |
| 198 | unsigned conn_error : 1; |
| 199 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 200 | /** Backing dev info */ |
| 201 | struct backing_dev_info bdi; |
| 202 | }; |
| 203 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame^] | 204 | struct fuse_getdir_out_i { |
| 205 | int fd; |
| 206 | void *file; /* Used by kernel only */ |
| 207 | }; |
| 208 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 209 | static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb) |
| 210 | { |
| 211 | return (struct fuse_conn **) &sb->s_fs_info; |
| 212 | } |
| 213 | |
| 214 | static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) |
| 215 | { |
| 216 | return *get_fuse_conn_super_p(sb); |
| 217 | } |
| 218 | |
| 219 | static inline struct fuse_conn *get_fuse_conn(struct inode *inode) |
| 220 | { |
| 221 | return get_fuse_conn_super(inode->i_sb); |
| 222 | } |
| 223 | |
| 224 | static inline struct fuse_inode *get_fuse_inode(struct inode *inode) |
| 225 | { |
| 226 | return container_of(inode, struct fuse_inode, inode); |
| 227 | } |
| 228 | |
| 229 | static inline u64 get_node_id(struct inode *inode) |
| 230 | { |
| 231 | return get_fuse_inode(inode)->nodeid; |
| 232 | } |
| 233 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 234 | /** Device operations */ |
| 235 | extern struct file_operations fuse_dev_operations; |
| 236 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 237 | /** |
| 238 | * This is the single global spinlock which protects FUSE's structures |
| 239 | * |
| 240 | * The following data is protected by this lock: |
| 241 | * |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 242 | * - the private_data field of the device file |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 243 | * - the s_fs_info field of the super block |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 244 | * - unused_list, pending, processing lists in fuse_conn |
| 245 | * - the unique request ID counter reqctr in fuse_conn |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 246 | * - the sb (super_block) field in fuse_conn |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 247 | * - the file (device file) field in fuse_conn |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 248 | */ |
| 249 | extern spinlock_t fuse_lock; |
| 250 | |
| 251 | /** |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame^] | 252 | * Get a filled in inode |
| 253 | */ |
| 254 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
| 255 | int generation, struct fuse_attr *attr, int version); |
| 256 | |
| 257 | /** |
| 258 | * Send FORGET command |
| 259 | */ |
| 260 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
| 261 | unsigned long nodeid, int version); |
| 262 | |
| 263 | /** |
| 264 | * Initialise inode operations on regular files and special files |
| 265 | */ |
| 266 | void fuse_init_common(struct inode *inode); |
| 267 | |
| 268 | /** |
| 269 | * Initialise inode and file operations on a directory |
| 270 | */ |
| 271 | void fuse_init_dir(struct inode *inode); |
| 272 | |
| 273 | /** |
| 274 | * Initialise inode operations on a symlink |
| 275 | */ |
| 276 | void fuse_init_symlink(struct inode *inode); |
| 277 | |
| 278 | /** |
| 279 | * Change attributes of an inode |
| 280 | */ |
| 281 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr); |
| 282 | |
| 283 | /** |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 284 | * Check if the connection can be released, and if yes, then free the |
| 285 | * connection structure |
| 286 | */ |
| 287 | void fuse_release_conn(struct fuse_conn *fc); |
| 288 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 289 | /** |
| 290 | * Initialize the client device |
| 291 | */ |
| 292 | int fuse_dev_init(void); |
| 293 | |
| 294 | /** |
| 295 | * Cleanup the client device |
| 296 | */ |
| 297 | void fuse_dev_cleanup(void); |
| 298 | |
| 299 | /** |
| 300 | * Allocate a request |
| 301 | */ |
| 302 | struct fuse_req *fuse_request_alloc(void); |
| 303 | |
| 304 | /** |
| 305 | * Free a request |
| 306 | */ |
| 307 | void fuse_request_free(struct fuse_req *req); |
| 308 | |
| 309 | /** |
| 310 | * Reinitialize a request, the preallocated flag is left unmodified |
| 311 | */ |
| 312 | void fuse_reset_request(struct fuse_req *req); |
| 313 | |
| 314 | /** |
| 315 | * Reserve a preallocated request |
| 316 | */ |
| 317 | struct fuse_req *fuse_get_request(struct fuse_conn *fc); |
| 318 | |
| 319 | /** |
| 320 | * Reserve a preallocated request, only interruptible by SIGKILL |
| 321 | */ |
| 322 | struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc); |
| 323 | |
| 324 | /** |
| 325 | * Decrement reference count of a request. If count goes to zero put |
| 326 | * on unused list (preallocated) or free reqest (not preallocated). |
| 327 | */ |
| 328 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); |
| 329 | |
| 330 | /** |
| 331 | * Send a request (synchronous, interruptible) |
| 332 | */ |
| 333 | void request_send(struct fuse_conn *fc, struct fuse_req *req); |
| 334 | |
| 335 | /** |
| 336 | * Send a request (synchronous, non-interruptible except by SIGKILL) |
| 337 | */ |
| 338 | void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req); |
| 339 | |
| 340 | /** |
| 341 | * Send a request with no reply |
| 342 | */ |
| 343 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req); |
| 344 | |
| 345 | /** |
| 346 | * Send a request in the background |
| 347 | */ |
| 348 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req); |
| 349 | |
| 350 | /** |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame^] | 351 | * Get the attributes of a file |
| 352 | */ |
| 353 | int fuse_do_getattr(struct inode *inode); |
| 354 | |
| 355 | /** |
| 356 | * Invalidate inode attributes |
| 357 | */ |
| 358 | void fuse_invalidate_attr(struct inode *inode); |
| 359 | |
| 360 | /** |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 361 | * Send the INIT message |
| 362 | */ |
| 363 | void fuse_send_init(struct fuse_conn *fc); |