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 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 24 /** It could be as large as PATH_MAX, but would that have any uses? */ 25 #define FUSE_NAME_MAX 1024 26 27 /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem 28 module will check permissions based on the file mode. Otherwise no 29 permission checking is done in the kernel */ 30 #define FUSE_DEFAULT_PERMISSIONS (1 << 0) 31 32 /** If the FUSE_ALLOW_OTHER flag is given, then not only the user 33 doing the mount will be allowed to access the filesystem */ 34 #define FUSE_ALLOW_OTHER (1 << 1) 35 36 37 /** FUSE inode */ 38 struct fuse_inode { 39 /** Inode data */ 40 struct inode inode; 41 42 /** Unique ID, which identifies the inode between userspace 43 * and kernel */ 44 u64 nodeid; 45 46 /** Number of lookups on this inode */ 47 u64 nlookup; 48 49 /** The request used for sending the FORGET message */ 50 struct fuse_req *forget_req; 51 52 /** Time in jiffies until the file attributes are valid */ 53 unsigned long i_time; 54 }; 55 56 /** FUSE specific file data */ 57 struct fuse_file { 58 /** Request reserved for flush and release */ 59 struct fuse_req *release_req; 60 61 /** File handle used by userspace */ 62 u64 fh; 63 }; 64 65 /** One input argument of a request */ 66 struct fuse_in_arg { 67 unsigned size; 68 const void *value; 69 }; 70 71 /** The request input */ 72 struct fuse_in { 73 /** The request header */ 74 struct fuse_in_header h; 75 76 /** True if the data for the last argument is in req->pages */ 77 unsigned argpages:1; 78 79 /** Number of arguments */ 80 unsigned numargs; 81 82 /** Array of arguments */ 83 struct fuse_in_arg args[3]; 84 }; 85 86 /** One output argument of a request */ 87 struct fuse_arg { 88 unsigned size; 89 void *value; 90 }; 91 92 /** The request output */ 93 struct fuse_out { 94 /** Header returned from userspace */ 95 struct fuse_out_header h; 96 97 /* 98 * The following bitfields are not changed during the request 99 * processing 100 */ 101 102 /** Last argument is variable length (can be shorter than 103 arg->size) */ 104 unsigned argvar:1; 105 106 /** Last argument is a list of pages to copy data to */ 107 unsigned argpages:1; 108 109 /** Zero partially or not copied pages */ 110 unsigned page_zeroing:1; 111 112 /** Number or arguments */ 113 unsigned numargs; 114 115 /** Array of arguments */ 116 struct fuse_arg args[3]; 117 }; 118 119 /** The request state */ 120 enum fuse_req_state { 121 FUSE_REQ_INIT = 0, 122 FUSE_REQ_PENDING, 123 FUSE_REQ_READING, 124 FUSE_REQ_SENT, 125 FUSE_REQ_FINISHED 126 }; 127 128 struct fuse_conn; 129 130 /** 131 * A request to the client 132 */ 133 struct fuse_req { 134 /** This can be on either unused_list, pending processing or 135 io lists in fuse_conn */ 136 struct list_head list; 137 138 /** Entry on the background list */ 139 struct list_head bg_entry; 140 141 /** refcount */ 142 atomic_t count; 143 144 /* 145 * The following bitfields are either set once before the 146 * request is queued or setting/clearing them is protected by 147 * fuse_lock 148 */ 149 150 /** True if the request has reply */ 151 unsigned isreply:1; 152 153 /** The request is preallocated */ 154 unsigned preallocated:1; 155 156 /** The request was interrupted */ 157 unsigned interrupted:1; 158 159 /** Request is sent in the background */ 160 unsigned background:1; 161 162 /** Data is being copied to/from the request */ 163 unsigned locked:1; 164 165 /** State of the request */ 166 enum fuse_req_state state; 167 168 /** The request input */ 169 struct fuse_in in; 170 171 /** The request output */ 172 struct fuse_out out; 173 174 /** Used to wake up the task waiting for completion of request*/ 175 wait_queue_head_t waitq; 176 177 /** Data for asynchronous requests */ 178 union { 179 struct fuse_forget_in forget_in; 180 struct fuse_release_in release_in; 181 struct fuse_init_in init_in; 182 struct fuse_init_out init_out; 183 struct fuse_read_in read_in; 184 } misc; 185 186 /** page vector */ 187 struct page *pages[FUSE_MAX_PAGES_PER_REQ]; 188 189 /** number of pages in vector */ 190 unsigned num_pages; 191 192 /** offset of data on first page */ 193 unsigned page_offset; 194 195 /** Inode used in the request */ 196 struct inode *inode; 197 198 /** Second inode used in the request (or NULL) */ 199 struct inode *inode2; 200 201 /** File used in the request (or NULL) */ 202 struct file *file; 203 204 /** Request completion callback */ 205 void (*end)(struct fuse_conn *, struct fuse_req *); 206 }; 207 208 /** 209 * A Fuse connection. 210 * 211 * This structure is created, when the filesystem is mounted, and is 212 * destroyed, when the client device is closed and the filesystem is 213 * unmounted. 214 */ 215 struct fuse_conn { 216 /** The user id for this mount */ 217 uid_t user_id; 218 219 /** The group id for this mount */ 220 gid_t group_id; 221 222 /** The fuse mount flags for this mount */ 223 unsigned flags; 224 225 /** Maximum read size */ 226 unsigned max_read; 227 228 /** Maximum write size */ 229 unsigned max_write; 230 231 /** Readers of the connection are waiting on this */ 232 wait_queue_head_t waitq; 233 234 /** The list of pending requests */ 235 struct list_head pending; 236 237 /** The list of requests being processed */ 238 struct list_head processing; 239 240 /** The list of requests under I/O */ 241 struct list_head io; 242 243 /** Requests put in the background (RELEASE or any other 244 interrupted request) */ 245 struct list_head background; 246 247 /** Controls the maximum number of outstanding requests */ 248 struct semaphore outstanding_sem; 249 250 /** This counts the number of outstanding requests if 251 outstanding_sem would go negative */ 252 unsigned outstanding_debt; 253 254 /** RW semaphore for exclusion with fuse_put_super() */ 255 struct rw_semaphore sbput_sem; 256 257 /** The list of unused requests */ 258 struct list_head unused_list; 259 260 /** The next unique request id */ 261 u64 reqctr; 262 263 /** Mount is active */ 264 unsigned mounted; 265 266 /** Connection established, cleared on umount, connection 267 abort and device release */ 268 unsigned connected; 269 270 /** Connection failed (version mismatch). Cannot race with 271 setting other bitfields since it is only set once in INIT 272 reply, before any other request, and never cleared */ 273 unsigned conn_error : 1; 274 275 /** Do readpages asynchronously? Only set in INIT */ 276 unsigned async_read : 1; 277 278 /* 279 * The following bitfields are only for optimization purposes 280 * and hence races in setting them will not cause malfunction 281 */ 282 283 /** Is fsync not implemented by fs? */ 284 unsigned no_fsync : 1; 285 286 /** Is fsyncdir not implemented by fs? */ 287 unsigned no_fsyncdir : 1; 288 289 /** Is flush not implemented by fs? */ 290 unsigned no_flush : 1; 291 292 /** Is setxattr not implemented by fs? */ 293 unsigned no_setxattr : 1; 294 295 /** Is getxattr not implemented by fs? */ 296 unsigned no_getxattr : 1; 297 298 /** Is listxattr not implemented by fs? */ 299 unsigned no_listxattr : 1; 300 301 /** Is removexattr not implemented by fs? */ 302 unsigned no_removexattr : 1; 303 304 /** Is access not implemented by fs? */ 305 unsigned no_access : 1; 306 307 /** Is create not implemented by fs? */ 308 unsigned no_create : 1; 309 310 /** The number of requests waiting for completion */ 311 atomic_t num_waiting; 312 313 /** Negotiated minor version */ 314 unsigned minor; 315 316 /** Backing dev info */ 317 struct backing_dev_info bdi; 318 319 /** kobject */ 320 struct kobject kobj; 321 }; 322 323 static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) 324 { 325 return sb->s_fs_info; 326 } 327 328 static inline struct fuse_conn *get_fuse_conn(struct inode *inode) 329 { 330 return get_fuse_conn_super(inode->i_sb); 331 } 332 333 static inline struct fuse_conn *get_fuse_conn_kobj(struct kobject *obj) 334 { 335 return container_of(obj, struct fuse_conn, kobj); 336 } 337 338 static inline struct fuse_inode *get_fuse_inode(struct inode *inode) 339 { 340 return container_of(inode, struct fuse_inode, inode); 341 } 342 343 static inline u64 get_node_id(struct inode *inode) 344 { 345 return get_fuse_inode(inode)->nodeid; 346 } 347 348 /** Device operations */ 349 extern const struct file_operations fuse_dev_operations; 350 351 /** 352 * This is the single global spinlock which protects FUSE's structures 353 * 354 * The following data is protected by this lock: 355 * 356 * - the private_data field of the device file 357 * - the s_fs_info field of the super block 358 * - unused_list, pending, processing lists in fuse_conn 359 * - background list in fuse_conn 360 * - the unique request ID counter reqctr in fuse_conn 361 * - the sb (super_block) field in fuse_conn 362 * - the file (device file) field in fuse_conn 363 */ 364 extern spinlock_t fuse_lock; 365 366 /** 367 * Get a filled in inode 368 */ 369 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, 370 int generation, struct fuse_attr *attr); 371 372 /** 373 * Send FORGET command 374 */ 375 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, 376 unsigned long nodeid, u64 nlookup); 377 378 /** 379 * Initialize READ or READDIR request 380 */ 381 void fuse_read_fill(struct fuse_req *req, struct file *file, 382 struct inode *inode, loff_t pos, size_t count, int opcode); 383 384 /** 385 * Send OPEN or OPENDIR request 386 */ 387 int fuse_open_common(struct inode *inode, struct file *file, int isdir); 388 389 struct fuse_file *fuse_file_alloc(void); 390 void fuse_file_free(struct fuse_file *ff); 391 void fuse_finish_open(struct inode *inode, struct file *file, 392 struct fuse_file *ff, struct fuse_open_out *outarg); 393 394 /** 395 * Send a RELEASE request 396 */ 397 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff, 398 u64 nodeid, struct inode *inode, int flags, int isdir); 399 400 /** 401 * Send RELEASE or RELEASEDIR request 402 */ 403 int fuse_release_common(struct inode *inode, struct file *file, int isdir); 404 405 /** 406 * Send FSYNC or FSYNCDIR request 407 */ 408 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, 409 int isdir); 410 411 /** 412 * Initialize file operations on a regular file 413 */ 414 void fuse_init_file_inode(struct inode *inode); 415 416 /** 417 * Initialize inode operations on regular files and special files 418 */ 419 void fuse_init_common(struct inode *inode); 420 421 /** 422 * Initialize inode and file operations on a directory 423 */ 424 void fuse_init_dir(struct inode *inode); 425 426 /** 427 * Initialize inode operations on a symlink 428 */ 429 void fuse_init_symlink(struct inode *inode); 430 431 /** 432 * Change attributes of an inode 433 */ 434 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr); 435 436 /** 437 * Initialize the client device 438 */ 439 int fuse_dev_init(void); 440 441 /** 442 * Cleanup the client device 443 */ 444 void fuse_dev_cleanup(void); 445 446 /** 447 * Allocate a request 448 */ 449 struct fuse_req *fuse_request_alloc(void); 450 451 /** 452 * Free a request 453 */ 454 void fuse_request_free(struct fuse_req *req); 455 456 /** 457 * Reinitialize a request, the preallocated flag is left unmodified 458 */ 459 void fuse_reset_request(struct fuse_req *req); 460 461 /** 462 * Reserve a preallocated request 463 */ 464 struct fuse_req *fuse_get_request(struct fuse_conn *fc); 465 466 /** 467 * Decrement reference count of a request. If count goes to zero put 468 * on unused list (preallocated) or free request (not preallocated). 469 */ 470 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); 471 472 /** 473 * Send a request (synchronous) 474 */ 475 void request_send(struct fuse_conn *fc, struct fuse_req *req); 476 477 /** 478 * Send a request with no reply 479 */ 480 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req); 481 482 /** 483 * Send a request in the background 484 */ 485 void request_send_background(struct fuse_conn *fc, struct fuse_req *req); 486 487 /** 488 * Release inodes and file associated with background request 489 */ 490 void fuse_release_background(struct fuse_req *req); 491 492 /* Abort all requests */ 493 void fuse_abort_conn(struct fuse_conn *fc); 494 495 /** 496 * Get the attributes of a file 497 */ 498 int fuse_do_getattr(struct inode *inode); 499 500 /** 501 * Invalidate inode attributes 502 */ 503 void fuse_invalidate_attr(struct inode *inode); 504