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