diff options
Diffstat (limited to 'fs/fuse')
| -rw-r--r-- | fs/fuse/Makefile | 7 | ||||
| -rw-r--r-- | fs/fuse/dev.c | 877 | ||||
| -rw-r--r-- | fs/fuse/dir.c | 982 | ||||
| -rw-r--r-- | fs/fuse/file.c | 555 | ||||
| -rw-r--r-- | fs/fuse/fuse_i.h | 451 | ||||
| -rw-r--r-- | fs/fuse/inode.c | 591 |
6 files changed, 3463 insertions, 0 deletions
diff --git a/fs/fuse/Makefile b/fs/fuse/Makefile new file mode 100644 index 000000000000..c3e1f760cac9 --- /dev/null +++ b/fs/fuse/Makefile | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | # | ||
| 2 | # Makefile for the FUSE filesystem. | ||
| 3 | # | ||
| 4 | |||
| 5 | obj-$(CONFIG_FUSE_FS) += fuse.o | ||
| 6 | |||
| 7 | fuse-objs := dev.o dir.o file.o inode.o | ||
diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c new file mode 100644 index 000000000000..d4c869c6d01b --- /dev/null +++ b/fs/fuse/dev.c | |||
| @@ -0,0 +1,877 @@ | |||
| 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 "fuse_i.h" | ||
| 10 | |||
| 11 | #include <linux/init.h> | ||
| 12 | #include <linux/module.h> | ||
| 13 | #include <linux/poll.h> | ||
| 14 | #include <linux/uio.h> | ||
| 15 | #include <linux/miscdevice.h> | ||
| 16 | #include <linux/pagemap.h> | ||
| 17 | #include <linux/file.h> | ||
| 18 | #include <linux/slab.h> | ||
| 19 | |||
| 20 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); | ||
| 21 | |||
| 22 | static kmem_cache_t *fuse_req_cachep; | ||
| 23 | |||
| 24 | static inline struct fuse_conn *fuse_get_conn(struct file *file) | ||
| 25 | { | ||
| 26 | struct fuse_conn *fc; | ||
| 27 | spin_lock(&fuse_lock); | ||
| 28 | fc = file->private_data; | ||
| 29 | if (fc && !fc->mounted) | ||
| 30 | fc = NULL; | ||
| 31 | spin_unlock(&fuse_lock); | ||
| 32 | return fc; | ||
| 33 | } | ||
| 34 | |||
| 35 | static inline void fuse_request_init(struct fuse_req *req) | ||
| 36 | { | ||
| 37 | memset(req, 0, sizeof(*req)); | ||
| 38 | INIT_LIST_HEAD(&req->list); | ||
| 39 | init_waitqueue_head(&req->waitq); | ||
| 40 | atomic_set(&req->count, 1); | ||
| 41 | } | ||
| 42 | |||
| 43 | struct fuse_req *fuse_request_alloc(void) | ||
| 44 | { | ||
| 45 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL); | ||
| 46 | if (req) | ||
| 47 | fuse_request_init(req); | ||
| 48 | return req; | ||
| 49 | } | ||
| 50 | |||
| 51 | void fuse_request_free(struct fuse_req *req) | ||
| 52 | { | ||
| 53 | kmem_cache_free(fuse_req_cachep, req); | ||
| 54 | } | ||
| 55 | |||
| 56 | static inline void block_sigs(sigset_t *oldset) | ||
| 57 | { | ||
| 58 | sigset_t mask; | ||
| 59 | |||
| 60 | siginitsetinv(&mask, sigmask(SIGKILL)); | ||
| 61 | sigprocmask(SIG_BLOCK, &mask, oldset); | ||
| 62 | } | ||
| 63 | |||
| 64 | static inline void restore_sigs(sigset_t *oldset) | ||
| 65 | { | ||
| 66 | sigprocmask(SIG_SETMASK, oldset, NULL); | ||
| 67 | } | ||
| 68 | |||
| 69 | void fuse_reset_request(struct fuse_req *req) | ||
| 70 | { | ||
| 71 | int preallocated = req->preallocated; | ||
| 72 | BUG_ON(atomic_read(&req->count) != 1); | ||
| 73 | fuse_request_init(req); | ||
| 74 | req->preallocated = preallocated; | ||
| 75 | } | ||
| 76 | |||
| 77 | static void __fuse_get_request(struct fuse_req *req) | ||
| 78 | { | ||
| 79 | atomic_inc(&req->count); | ||
| 80 | } | ||
| 81 | |||
| 82 | /* Must be called with > 1 refcount */ | ||
| 83 | static void __fuse_put_request(struct fuse_req *req) | ||
| 84 | { | ||
| 85 | BUG_ON(atomic_read(&req->count) < 2); | ||
| 86 | atomic_dec(&req->count); | ||
| 87 | } | ||
| 88 | |||
| 89 | static struct fuse_req *do_get_request(struct fuse_conn *fc) | ||
| 90 | { | ||
| 91 | struct fuse_req *req; | ||
| 92 | |||
| 93 | spin_lock(&fuse_lock); | ||
| 94 | BUG_ON(list_empty(&fc->unused_list)); | ||
| 95 | req = list_entry(fc->unused_list.next, struct fuse_req, list); | ||
| 96 | list_del_init(&req->list); | ||
| 97 | spin_unlock(&fuse_lock); | ||
| 98 | fuse_request_init(req); | ||
| 99 | req->preallocated = 1; | ||
| 100 | req->in.h.uid = current->fsuid; | ||
| 101 | req->in.h.gid = current->fsgid; | ||
| 102 | req->in.h.pid = current->pid; | ||
| 103 | return req; | ||
| 104 | } | ||
| 105 | |||
| 106 | /* This can return NULL, but only in case it's interrupted by a SIGKILL */ | ||
| 107 | struct fuse_req *fuse_get_request(struct fuse_conn *fc) | ||
| 108 | { | ||
| 109 | int intr; | ||
| 110 | sigset_t oldset; | ||
| 111 | |||
| 112 | block_sigs(&oldset); | ||
| 113 | intr = down_interruptible(&fc->outstanding_sem); | ||
| 114 | restore_sigs(&oldset); | ||
| 115 | return intr ? NULL : do_get_request(fc); | ||
| 116 | } | ||
| 117 | |||
| 118 | static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req) | ||
| 119 | { | ||
| 120 | spin_lock(&fuse_lock); | ||
| 121 | if (req->preallocated) | ||
| 122 | list_add(&req->list, &fc->unused_list); | ||
| 123 | else | ||
| 124 | fuse_request_free(req); | ||
| 125 | |||
| 126 | /* If we are in debt decrease that first */ | ||
| 127 | if (fc->outstanding_debt) | ||
| 128 | fc->outstanding_debt--; | ||
| 129 | else | ||
| 130 | up(&fc->outstanding_sem); | ||
| 131 | spin_unlock(&fuse_lock); | ||
| 132 | } | ||
| 133 | |||
| 134 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) | ||
| 135 | { | ||
| 136 | if (atomic_dec_and_test(&req->count)) | ||
| 137 | fuse_putback_request(fc, req); | ||
| 138 | } | ||
| 139 | |||
| 140 | void fuse_release_background(struct fuse_req *req) | ||
| 141 | { | ||
| 142 | iput(req->inode); | ||
| 143 | iput(req->inode2); | ||
| 144 | if (req->file) | ||
| 145 | fput(req->file); | ||
| 146 | spin_lock(&fuse_lock); | ||
| 147 | list_del(&req->bg_entry); | ||
| 148 | spin_unlock(&fuse_lock); | ||
| 149 | } | ||
| 150 | |||
| 151 | /* | ||
| 152 | * This function is called when a request is finished. Either a reply | ||
| 153 | * has arrived or it was interrupted (and not yet sent) or some error | ||
| 154 | * occured during communication with userspace, or the device file was | ||
| 155 | * closed. It decreases the referece count for the request. In case | ||
| 156 | * of a background request the referece to the stored objects are | ||
| 157 | * released. The requester thread is woken up (if still waiting), and | ||
| 158 | * finally the request is either freed or put on the unused_list | ||
| 159 | * | ||
| 160 | * Called with fuse_lock, unlocks it | ||
| 161 | */ | ||
| 162 | static void request_end(struct fuse_conn *fc, struct fuse_req *req) | ||
| 163 | { | ||
| 164 | int putback; | ||
| 165 | req->finished = 1; | ||
| 166 | putback = atomic_dec_and_test(&req->count); | ||
| 167 | spin_unlock(&fuse_lock); | ||
| 168 | if (req->background) { | ||
| 169 | down_read(&fc->sbput_sem); | ||
| 170 | if (fc->mounted) | ||
| 171 | fuse_release_background(req); | ||
| 172 | up_read(&fc->sbput_sem); | ||
| 173 | } | ||
| 174 | wake_up(&req->waitq); | ||
| 175 | if (req->in.h.opcode == FUSE_INIT) { | ||
| 176 | int i; | ||
| 177 | |||
| 178 | if (req->misc.init_in_out.major != FUSE_KERNEL_VERSION) | ||
| 179 | fc->conn_error = 1; | ||
| 180 | |||
| 181 | /* After INIT reply is received other requests can go | ||
| 182 | out. So do (FUSE_MAX_OUTSTANDING - 1) number of | ||
| 183 | up()s on outstanding_sem. The last up() is done in | ||
| 184 | fuse_putback_request() */ | ||
| 185 | for (i = 1; i < FUSE_MAX_OUTSTANDING; i++) | ||
| 186 | up(&fc->outstanding_sem); | ||
| 187 | } | ||
| 188 | if (putback) | ||
| 189 | fuse_putback_request(fc, req); | ||
| 190 | } | ||
| 191 | |||
| 192 | /* | ||
| 193 | * Unfortunately request interruption not just solves the deadlock | ||
| 194 | * problem, it causes problems too. These stem from the fact, that an | ||
| 195 | * interrupted request is continued to be processed in userspace, | ||
| 196 | * while all the locks and object references (inode and file) held | ||
| 197 | * during the operation are released. | ||
| 198 | * | ||
| 199 | * To release the locks is exactly why there's a need to interrupt the | ||
| 200 | * request, so there's not a lot that can be done about this, except | ||
| 201 | * introduce additional locking in userspace. | ||
| 202 | * | ||
| 203 | * More important is to keep inode and file references until userspace | ||
| 204 | * has replied, otherwise FORGET and RELEASE could be sent while the | ||
| 205 | * inode/file is still used by the filesystem. | ||
| 206 | * | ||
| 207 | * For this reason the concept of "background" request is introduced. | ||
| 208 | * An interrupted request is backgrounded if it has been already sent | ||
| 209 | * to userspace. Backgrounding involves getting an extra reference to | ||
| 210 | * inode(s) or file used in the request, and adding the request to | ||
| 211 | * fc->background list. When a reply is received for a background | ||
| 212 | * request, the object references are released, and the request is | ||
| 213 | * removed from the list. If the filesystem is unmounted while there | ||
| 214 | * are still background requests, the list is walked and references | ||
| 215 | * are released as if a reply was received. | ||
| 216 | * | ||
| 217 | * There's one more use for a background request. The RELEASE message is | ||
| 218 | * always sent as background, since it doesn't return an error or | ||
| 219 | * data. | ||
| 220 | */ | ||
| 221 | static void background_request(struct fuse_conn *fc, struct fuse_req *req) | ||
| 222 | { | ||
| 223 | req->background = 1; | ||
| 224 | list_add(&req->bg_entry, &fc->background); | ||
| 225 | if (req->inode) | ||
| 226 | req->inode = igrab(req->inode); | ||
| 227 | if (req->inode2) | ||
| 228 | req->inode2 = igrab(req->inode2); | ||
| 229 | if (req->file) | ||
| 230 | get_file(req->file); | ||
| 231 | } | ||
| 232 | |||
| 233 | /* Called with fuse_lock held. Releases, and then reacquires it. */ | ||
| 234 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) | ||
| 235 | { | ||
| 236 | sigset_t oldset; | ||
| 237 | |||
| 238 | spin_unlock(&fuse_lock); | ||
| 239 | block_sigs(&oldset); | ||
| 240 | wait_event_interruptible(req->waitq, req->finished); | ||
| 241 | restore_sigs(&oldset); | ||
| 242 | spin_lock(&fuse_lock); | ||
| 243 | if (req->finished) | ||
| 244 | return; | ||
| 245 | |||
| 246 | req->out.h.error = -EINTR; | ||
| 247 | req->interrupted = 1; | ||
| 248 | if (req->locked) { | ||
| 249 | /* This is uninterruptible sleep, because data is | ||
| 250 | being copied to/from the buffers of req. During | ||
| 251 | locked state, there mustn't be any filesystem | ||
| 252 | operation (e.g. page fault), since that could lead | ||
| 253 | to deadlock */ | ||
| 254 | spin_unlock(&fuse_lock); | ||
| 255 | wait_event(req->waitq, !req->locked); | ||
| 256 | spin_lock(&fuse_lock); | ||
| 257 | } | ||
| 258 | if (!req->sent && !list_empty(&req->list)) { | ||
| 259 | list_del(&req->list); | ||
| 260 | __fuse_put_request(req); | ||
| 261 | } else if (!req->finished && req->sent) | ||
| 262 | background_request(fc, req); | ||
| 263 | } | ||
| 264 | |||
| 265 | static unsigned len_args(unsigned numargs, struct fuse_arg *args) | ||
| 266 | { | ||
| 267 | unsigned nbytes = 0; | ||
| 268 | unsigned i; | ||
| 269 | |||
| 270 | for (i = 0; i < numargs; i++) | ||
| 271 | nbytes += args[i].size; | ||
| 272 | |||
| 273 | return nbytes; | ||
| 274 | } | ||
| 275 | |||
| 276 | static void queue_request(struct fuse_conn *fc, struct fuse_req *req) | ||
| 277 | { | ||
| 278 | fc->reqctr++; | ||
| 279 | /* zero is special */ | ||
| 280 | if (fc->reqctr == 0) | ||
| 281 | fc->reqctr = 1; | ||
| 282 | req->in.h.unique = fc->reqctr; | ||
| 283 | req->in.h.len = sizeof(struct fuse_in_header) + | ||
| 284 | len_args(req->in.numargs, (struct fuse_arg *) req->in.args); | ||
| 285 | if (!req->preallocated) { | ||
| 286 | /* If request is not preallocated (either FORGET or | ||
| 287 | RELEASE), then still decrease outstanding_sem, so | ||
| 288 | user can't open infinite number of files while not | ||
| 289 | processing the RELEASE requests. However for | ||
| 290 | efficiency do it without blocking, so if down() | ||
| 291 | would block, just increase the debt instead */ | ||
| 292 | if (down_trylock(&fc->outstanding_sem)) | ||
| 293 | fc->outstanding_debt++; | ||
| 294 | } | ||
| 295 | list_add_tail(&req->list, &fc->pending); | ||
| 296 | wake_up(&fc->waitq); | ||
| 297 | } | ||
| 298 | |||
| 299 | /* | ||
| 300 | * This can only be interrupted by a SIGKILL | ||
| 301 | */ | ||
| 302 | void request_send(struct fuse_conn *fc, struct fuse_req *req) | ||
| 303 | { | ||
| 304 | req->isreply = 1; | ||
| 305 | spin_lock(&fuse_lock); | ||
| 306 | if (!fc->connected) | ||
| 307 | req->out.h.error = -ENOTCONN; | ||
| 308 | else if (fc->conn_error) | ||
| 309 | req->out.h.error = -ECONNREFUSED; | ||
| 310 | else { | ||
| 311 | queue_request(fc, req); | ||
| 312 | /* acquire extra reference, since request is still needed | ||
| 313 | after request_end() */ | ||
| 314 | __fuse_get_request(req); | ||
| 315 | |||
| 316 | request_wait_answer(fc, req); | ||
| 317 | } | ||
| 318 | spin_unlock(&fuse_lock); | ||
| 319 | } | ||
| 320 | |||
| 321 | static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req) | ||
| 322 | { | ||
| 323 | spin_lock(&fuse_lock); | ||
| 324 | if (fc->connected) { | ||
| 325 | queue_request(fc, req); | ||
| 326 | spin_unlock(&fuse_lock); | ||
| 327 | } else { | ||
| 328 | req->out.h.error = -ENOTCONN; | ||
| 329 | request_end(fc, req); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req) | ||
| 334 | { | ||
| 335 | req->isreply = 0; | ||
| 336 | request_send_nowait(fc, req); | ||
| 337 | } | ||
| 338 | |||
| 339 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req) | ||
| 340 | { | ||
| 341 | req->isreply = 1; | ||
| 342 | spin_lock(&fuse_lock); | ||
| 343 | background_request(fc, req); | ||
| 344 | spin_unlock(&fuse_lock); | ||
| 345 | request_send_nowait(fc, req); | ||
| 346 | } | ||
| 347 | |||
| 348 | void fuse_send_init(struct fuse_conn *fc) | ||
| 349 | { | ||
| 350 | /* This is called from fuse_read_super() so there's guaranteed | ||
| 351 | to be a request available */ | ||
| 352 | struct fuse_req *req = do_get_request(fc); | ||
| 353 | struct fuse_init_in_out *arg = &req->misc.init_in_out; | ||
| 354 | arg->major = FUSE_KERNEL_VERSION; | ||
| 355 | arg->minor = FUSE_KERNEL_MINOR_VERSION; | ||
| 356 | req->in.h.opcode = FUSE_INIT; | ||
| 357 | req->in.numargs = 1; | ||
| 358 | req->in.args[0].size = sizeof(*arg); | ||
| 359 | req->in.args[0].value = arg; | ||
| 360 | req->out.numargs = 1; | ||
| 361 | req->out.args[0].size = sizeof(*arg); | ||
| 362 | req->out.args[0].value = arg; | ||
| 363 | request_send_background(fc, req); | ||
| 364 | } | ||
| 365 | |||
| 366 | /* | ||
| 367 | * Lock the request. Up to the next unlock_request() there mustn't be | ||
| 368 | * anything that could cause a page-fault. If the request was already | ||
| 369 | * interrupted bail out. | ||
| 370 | */ | ||
| 371 | static inline int lock_request(struct fuse_req *req) | ||
| 372 | { | ||
| 373 | int err = 0; | ||
| 374 | if (req) { | ||
| 375 | spin_lock(&fuse_lock); | ||
| 376 | if (req->interrupted) | ||
| 377 | err = -ENOENT; | ||
| 378 | else | ||
| 379 | req->locked = 1; | ||
| 380 | spin_unlock(&fuse_lock); | ||
| 381 | } | ||
| 382 | return err; | ||
| 383 | } | ||
| 384 | |||
| 385 | /* | ||
| 386 | * Unlock request. If it was interrupted during being locked, the | ||
| 387 | * requester thread is currently waiting for it to be unlocked, so | ||
| 388 | * wake it up. | ||
| 389 | */ | ||
| 390 | static inline void unlock_request(struct fuse_req *req) | ||
| 391 | { | ||
| 392 | if (req) { | ||
| 393 | spin_lock(&fuse_lock); | ||
| 394 | req->locked = 0; | ||
| 395 | if (req->interrupted) | ||
| 396 | wake_up(&req->waitq); | ||
| 397 | spin_unlock(&fuse_lock); | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | struct fuse_copy_state { | ||
| 402 | int write; | ||
| 403 | struct fuse_req *req; | ||
| 404 | const struct iovec *iov; | ||
| 405 | unsigned long nr_segs; | ||
| 406 | unsigned long seglen; | ||
| 407 | unsigned long addr; | ||
| 408 | struct page *pg; | ||
| 409 | void *mapaddr; | ||
| 410 | void *buf; | ||
| 411 | unsigned len; | ||
| 412 | }; | ||
| 413 | |||
| 414 | static void fuse_copy_init(struct fuse_copy_state *cs, int write, | ||
| 415 | struct fuse_req *req, const struct iovec *iov, | ||
| 416 | unsigned long nr_segs) | ||
| 417 | { | ||
| 418 | memset(cs, 0, sizeof(*cs)); | ||
| 419 | cs->write = write; | ||
| 420 | cs->req = req; | ||
| 421 | cs->iov = iov; | ||
| 422 | cs->nr_segs = nr_segs; | ||
| 423 | } | ||
| 424 | |||
| 425 | /* Unmap and put previous page of userspace buffer */ | ||
| 426 | static inline void fuse_copy_finish(struct fuse_copy_state *cs) | ||
| 427 | { | ||
| 428 | if (cs->mapaddr) { | ||
| 429 | kunmap_atomic(cs->mapaddr, KM_USER0); | ||
| 430 | if (cs->write) { | ||
| 431 | flush_dcache_page(cs->pg); | ||
| 432 | set_page_dirty_lock(cs->pg); | ||
| 433 | } | ||
| 434 | put_page(cs->pg); | ||
| 435 | cs->mapaddr = NULL; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | /* | ||
| 440 | * Get another pagefull of userspace buffer, and map it to kernel | ||
| 441 | * address space, and lock request | ||
| 442 | */ | ||
| 443 | static int fuse_copy_fill(struct fuse_copy_state *cs) | ||
| 444 | { | ||
| 445 | unsigned long offset; | ||
| 446 | int err; | ||
| 447 | |||
| 448 | unlock_request(cs->req); | ||
| 449 | fuse_copy_finish(cs); | ||
| 450 | if (!cs->seglen) { | ||
| 451 | BUG_ON(!cs->nr_segs); | ||
| 452 | cs->seglen = cs->iov[0].iov_len; | ||
| 453 | cs->addr = (unsigned long) cs->iov[0].iov_base; | ||
| 454 | cs->iov ++; | ||
| 455 | cs->nr_segs --; | ||
| 456 | } | ||
| 457 | down_read(¤t->mm->mmap_sem); | ||
| 458 | err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0, | ||
| 459 | &cs->pg, NULL); | ||
| 460 | up_read(¤t->mm->mmap_sem); | ||
| 461 | if (err < 0) | ||
| 462 | return err; | ||
| 463 | BUG_ON(err != 1); | ||
| 464 | offset = cs->addr % PAGE_SIZE; | ||
| 465 | cs->mapaddr = kmap_atomic(cs->pg, KM_USER0); | ||
| 466 | cs->buf = cs->mapaddr + offset; | ||
| 467 | cs->len = min(PAGE_SIZE - offset, cs->seglen); | ||
| 468 | cs->seglen -= cs->len; | ||
| 469 | cs->addr += cs->len; | ||
| 470 | |||
| 471 | return lock_request(cs->req); | ||
| 472 | } | ||
| 473 | |||
| 474 | /* Do as much copy to/from userspace buffer as we can */ | ||
| 475 | static inline int fuse_copy_do(struct fuse_copy_state *cs, void **val, | ||
| 476 | unsigned *size) | ||
| 477 | { | ||
| 478 | unsigned ncpy = min(*size, cs->len); | ||
| 479 | if (val) { | ||
| 480 | if (cs->write) | ||
| 481 | memcpy(cs->buf, *val, ncpy); | ||
| 482 | else | ||
| 483 | memcpy(*val, cs->buf, ncpy); | ||
| 484 | *val += ncpy; | ||
| 485 | } | ||
| 486 | *size -= ncpy; | ||
| 487 | cs->len -= ncpy; | ||
| 488 | cs->buf += ncpy; | ||
| 489 | return ncpy; | ||
| 490 | } | ||
| 491 | |||
| 492 | /* | ||
| 493 | * Copy a page in the request to/from the userspace buffer. Must be | ||
| 494 | * done atomically | ||
| 495 | */ | ||
| 496 | static inline int fuse_copy_page(struct fuse_copy_state *cs, struct page *page, | ||
| 497 | unsigned offset, unsigned count, int zeroing) | ||
| 498 | { | ||
| 499 | if (page && zeroing && count < PAGE_SIZE) { | ||
| 500 | void *mapaddr = kmap_atomic(page, KM_USER1); | ||
| 501 | memset(mapaddr, 0, PAGE_SIZE); | ||
| 502 | kunmap_atomic(mapaddr, KM_USER1); | ||
| 503 | } | ||
| 504 | while (count) { | ||
| 505 | int err; | ||
| 506 | if (!cs->len && (err = fuse_copy_fill(cs))) | ||
| 507 | return err; | ||
| 508 | if (page) { | ||
| 509 | void *mapaddr = kmap_atomic(page, KM_USER1); | ||
| 510 | void *buf = mapaddr + offset; | ||
| 511 | offset += fuse_copy_do(cs, &buf, &count); | ||
| 512 | kunmap_atomic(mapaddr, KM_USER1); | ||
| 513 | } else | ||
| 514 | offset += fuse_copy_do(cs, NULL, &count); | ||
| 515 | } | ||
| 516 | if (page && !cs->write) | ||
| 517 | flush_dcache_page(page); | ||
| 518 | return 0; | ||
| 519 | } | ||
| 520 | |||
| 521 | /* Copy pages in the request to/from userspace buffer */ | ||
| 522 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, | ||
| 523 | int zeroing) | ||
| 524 | { | ||
| 525 | unsigned i; | ||
| 526 | struct fuse_req *req = cs->req; | ||
| 527 | unsigned offset = req->page_offset; | ||
| 528 | unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset); | ||
| 529 | |||
| 530 | for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) { | ||
| 531 | struct page *page = req->pages[i]; | ||
| 532 | int err = fuse_copy_page(cs, page, offset, count, zeroing); | ||
| 533 | if (err) | ||
| 534 | return err; | ||
| 535 | |||
| 536 | nbytes -= count; | ||
| 537 | count = min(nbytes, (unsigned) PAGE_SIZE); | ||
| 538 | offset = 0; | ||
| 539 | } | ||
| 540 | return 0; | ||
| 541 | } | ||
| 542 | |||
| 543 | /* Copy a single argument in the request to/from userspace buffer */ | ||
| 544 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) | ||
| 545 | { | ||
| 546 | while (size) { | ||
| 547 | int err; | ||
| 548 | if (!cs->len && (err = fuse_copy_fill(cs))) | ||
| 549 | return err; | ||
| 550 | fuse_copy_do(cs, &val, &size); | ||
| 551 | } | ||
| 552 | return 0; | ||
| 553 | } | ||
| 554 | |||
| 555 | /* Copy request arguments to/from userspace buffer */ | ||
| 556 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, | ||
| 557 | unsigned argpages, struct fuse_arg *args, | ||
| 558 | int zeroing) | ||
| 559 | { | ||
| 560 | int err = 0; | ||
| 561 | unsigned i; | ||
| 562 | |||
| 563 | for (i = 0; !err && i < numargs; i++) { | ||
| 564 | struct fuse_arg *arg = &args[i]; | ||
| 565 | if (i == numargs - 1 && argpages) | ||
| 566 | err = fuse_copy_pages(cs, arg->size, zeroing); | ||
| 567 | else | ||
| 568 | err = fuse_copy_one(cs, arg->value, arg->size); | ||
| 569 | } | ||
| 570 | return err; | ||
| 571 | } | ||
| 572 | |||
| 573 | /* Wait until a request is available on the pending list */ | ||
| 574 | static void request_wait(struct fuse_conn *fc) | ||
| 575 | { | ||
| 576 | DECLARE_WAITQUEUE(wait, current); | ||
| 577 | |||
| 578 | add_wait_queue_exclusive(&fc->waitq, &wait); | ||
| 579 | while (fc->mounted && list_empty(&fc->pending)) { | ||
| 580 | set_current_state(TASK_INTERRUPTIBLE); | ||
| 581 | if (signal_pending(current)) | ||
| 582 | break; | ||
| 583 | |||
| 584 | spin_unlock(&fuse_lock); | ||
| 585 | schedule(); | ||
| 586 | spin_lock(&fuse_lock); | ||
| 587 | } | ||
| 588 | set_current_state(TASK_RUNNING); | ||
| 589 | remove_wait_queue(&fc->waitq, &wait); | ||
| 590 | } | ||
| 591 | |||
| 592 | /* | ||
| 593 | * Read a single request into the userspace filesystem's buffer. This | ||
| 594 | * function waits until a request is available, then removes it from | ||
| 595 | * the pending list and copies request data to userspace buffer. If | ||
| 596 | * no reply is needed (FORGET) or request has been interrupted or | ||
| 597 | * there was an error during the copying then it's finished by calling | ||
| 598 | * request_end(). Otherwise add it to the processing list, and set | ||
| 599 | * the 'sent' flag. | ||
| 600 | */ | ||
| 601 | static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov, | ||
| 602 | unsigned long nr_segs, loff_t *off) | ||
| 603 | { | ||
| 604 | int err; | ||
| 605 | struct fuse_conn *fc; | ||
| 606 | struct fuse_req *req; | ||
| 607 | struct fuse_in *in; | ||
| 608 | struct fuse_copy_state cs; | ||
| 609 | unsigned reqsize; | ||
| 610 | |||
| 611 | spin_lock(&fuse_lock); | ||
| 612 | fc = file->private_data; | ||
| 613 | err = -EPERM; | ||
| 614 | if (!fc) | ||
| 615 | goto err_unlock; | ||
| 616 | request_wait(fc); | ||
| 617 | err = -ENODEV; | ||
| 618 | if (!fc->mounted) | ||
| 619 | goto err_unlock; | ||
| 620 | err = -ERESTARTSYS; | ||
| 621 | if (list_empty(&fc->pending)) | ||
| 622 | goto err_unlock; | ||
| 623 | |||
| 624 | req = list_entry(fc->pending.next, struct fuse_req, list); | ||
| 625 | list_del_init(&req->list); | ||
| 626 | spin_unlock(&fuse_lock); | ||
| 627 | |||
| 628 | in = &req->in; | ||
| 629 | reqsize = req->in.h.len; | ||
| 630 | fuse_copy_init(&cs, 1, req, iov, nr_segs); | ||
| 631 | err = -EINVAL; | ||
| 632 | if (iov_length(iov, nr_segs) >= reqsize) { | ||
| 633 | err = fuse_copy_one(&cs, &in->h, sizeof(in->h)); | ||
| 634 | if (!err) | ||
| 635 | err = fuse_copy_args(&cs, in->numargs, in->argpages, | ||
| 636 | (struct fuse_arg *) in->args, 0); | ||
| 637 | } | ||
| 638 | fuse_copy_finish(&cs); | ||
| 639 | |||
| 640 | spin_lock(&fuse_lock); | ||
| 641 | req->locked = 0; | ||
| 642 | if (!err && req->interrupted) | ||
| 643 | err = -ENOENT; | ||
| 644 | if (err) { | ||
| 645 | if (!req->interrupted) | ||
| 646 | req->out.h.error = -EIO; | ||
| 647 | request_end(fc, req); | ||
| 648 | return err; | ||
| 649 | } | ||
| 650 | if (!req->isreply) | ||
| 651 | request_end(fc, req); | ||
| 652 | else { | ||
| 653 | req->sent = 1; | ||
| 654 | list_add_tail(&req->list, &fc->processing); | ||
| 655 | spin_unlock(&fuse_lock); | ||
| 656 | } | ||
| 657 | return reqsize; | ||
| 658 | |||
| 659 | err_unlock: | ||
| 660 | spin_unlock(&fuse_lock); | ||
| 661 | return err; | ||
| 662 | } | ||
| 663 | |||
| 664 | static ssize_t fuse_dev_read(struct file *file, char __user *buf, | ||
| 665 | size_t nbytes, loff_t *off) | ||
| 666 | { | ||
| 667 | struct iovec iov; | ||
| 668 | iov.iov_len = nbytes; | ||
| 669 | iov.iov_base = buf; | ||
| 670 | return fuse_dev_readv(file, &iov, 1, off); | ||
| 671 | } | ||
| 672 | |||
| 673 | /* Look up request on processing list by unique ID */ | ||
| 674 | static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique) | ||
| 675 | { | ||
| 676 | struct list_head *entry; | ||
| 677 | |||
| 678 | list_for_each(entry, &fc->processing) { | ||
| 679 | struct fuse_req *req; | ||
| 680 | req = list_entry(entry, struct fuse_req, list); | ||
| 681 | if (req->in.h.unique == unique) | ||
| 682 | return req; | ||
| 683 | } | ||
| 684 | return NULL; | ||
| 685 | } | ||
| 686 | |||
| 687 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out, | ||
| 688 | unsigned nbytes) | ||
| 689 | { | ||
| 690 | unsigned reqsize = sizeof(struct fuse_out_header); | ||
| 691 | |||
| 692 | if (out->h.error) | ||
| 693 | return nbytes != reqsize ? -EINVAL : 0; | ||
| 694 | |||
| 695 | reqsize += len_args(out->numargs, out->args); | ||
| 696 | |||
| 697 | if (reqsize < nbytes || (reqsize > nbytes && !out->argvar)) | ||
| 698 | return -EINVAL; | ||
| 699 | else if (reqsize > nbytes) { | ||
| 700 | struct fuse_arg *lastarg = &out->args[out->numargs-1]; | ||
| 701 | unsigned diffsize = reqsize - nbytes; | ||
| 702 | if (diffsize > lastarg->size) | ||
| 703 | return -EINVAL; | ||
| 704 | lastarg->size -= diffsize; | ||
| 705 | } | ||
| 706 | return fuse_copy_args(cs, out->numargs, out->argpages, out->args, | ||
| 707 | out->page_zeroing); | ||
| 708 | } | ||
| 709 | |||
| 710 | /* | ||
| 711 | * Write a single reply to a request. First the header is copied from | ||
| 712 | * the write buffer. The request is then searched on the processing | ||
| 713 | * list by the unique ID found in the header. If found, then remove | ||
| 714 | * it from the list and copy the rest of the buffer to the request. | ||
| 715 | * The request is finished by calling request_end() | ||
| 716 | */ | ||
| 717 | static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov, | ||
| 718 | unsigned long nr_segs, loff_t *off) | ||
| 719 | { | ||
| 720 | int err; | ||
| 721 | unsigned nbytes = iov_length(iov, nr_segs); | ||
| 722 | struct fuse_req *req; | ||
| 723 | struct fuse_out_header oh; | ||
| 724 | struct fuse_copy_state cs; | ||
| 725 | struct fuse_conn *fc = fuse_get_conn(file); | ||
| 726 | if (!fc) | ||
| 727 | return -ENODEV; | ||
| 728 | |||
| 729 | fuse_copy_init(&cs, 0, NULL, iov, nr_segs); | ||
| 730 | if (nbytes < sizeof(struct fuse_out_header)) | ||
| 731 | return -EINVAL; | ||
| 732 | |||
| 733 | err = fuse_copy_one(&cs, &oh, sizeof(oh)); | ||
| 734 | if (err) | ||
| 735 | goto err_finish; | ||
| 736 | err = -EINVAL; | ||
| 737 | if (!oh.unique || oh.error <= -1000 || oh.error > 0 || | ||
| 738 | oh.len != nbytes) | ||
| 739 | goto err_finish; | ||
| 740 | |||
| 741 | spin_lock(&fuse_lock); | ||
| 742 | req = request_find(fc, oh.unique); | ||
| 743 | err = -EINVAL; | ||
| 744 | if (!req) | ||
| 745 | goto err_unlock; | ||
| 746 | |||
| 747 | list_del_init(&req->list); | ||
| 748 | if (req->interrupted) { | ||
| 749 | request_end(fc, req); | ||
| 750 | fuse_copy_finish(&cs); | ||
| 751 | return -ENOENT; | ||
| 752 | } | ||
| 753 | req->out.h = oh; | ||
| 754 | req->locked = 1; | ||
| 755 | cs.req = req; | ||
| 756 | spin_unlock(&fuse_lock); | ||
| 757 | |||
| 758 | err = copy_out_args(&cs, &req->out, nbytes); | ||
| 759 | fuse_copy_finish(&cs); | ||
| 760 | |||
| 761 | spin_lock(&fuse_lock); | ||
| 762 | req->locked = 0; | ||
| 763 | if (!err) { | ||
| 764 | if (req->interrupted) | ||
| 765 | err = -ENOENT; | ||
| 766 | } else if (!req->interrupted) | ||
| 767 | req->out.h.error = -EIO; | ||
| 768 | request_end(fc, req); | ||
| 769 | |||
| 770 | return err ? err : nbytes; | ||
| 771 | |||
| 772 | err_unlock: | ||
| 773 | spin_unlock(&fuse_lock); | ||
| 774 | err_finish: | ||
| 775 | fuse_copy_finish(&cs); | ||
| 776 | return err; | ||
| 777 | } | ||
| 778 | |||
| 779 | static ssize_t fuse_dev_write(struct file *file, const char __user *buf, | ||
| 780 | size_t nbytes, loff_t *off) | ||
| 781 | { | ||
| 782 | struct iovec iov; | ||
| 783 | iov.iov_len = nbytes; | ||
| 784 | iov.iov_base = (char __user *) buf; | ||
| 785 | return fuse_dev_writev(file, &iov, 1, off); | ||
| 786 | } | ||
| 787 | |||
| 788 | static unsigned fuse_dev_poll(struct file *file, poll_table *wait) | ||
| 789 | { | ||
| 790 | struct fuse_conn *fc = fuse_get_conn(file); | ||
| 791 | unsigned mask = POLLOUT | POLLWRNORM; | ||
| 792 | |||
| 793 | if (!fc) | ||
| 794 | return -ENODEV; | ||
| 795 | |||
| 796 | poll_wait(file, &fc->waitq, wait); | ||
| 797 | |||
| 798 | spin_lock(&fuse_lock); | ||
| 799 | if (!list_empty(&fc->pending)) | ||
| 800 | mask |= POLLIN | POLLRDNORM; | ||
| 801 | spin_unlock(&fuse_lock); | ||
| 802 | |||
| 803 | return mask; | ||
| 804 | } | ||
| 805 | |||
| 806 | /* Abort all requests on the given list (pending or processing) */ | ||
| 807 | static void end_requests(struct fuse_conn *fc, struct list_head *head) | ||
| 808 | { | ||
| 809 | while (!list_empty(head)) { | ||
| 810 | struct fuse_req *req; | ||
| 811 | req = list_entry(head->next, struct fuse_req, list); | ||
| 812 | list_del_init(&req->list); | ||
| 813 | req->out.h.error = -ECONNABORTED; | ||
| 814 | request_end(fc, req); | ||
| 815 | spin_lock(&fuse_lock); | ||
| 816 | } | ||
| 817 | } | ||
| 818 | |||
| 819 | static int fuse_dev_release(struct inode *inode, struct file *file) | ||
| 820 | { | ||
| 821 | struct fuse_conn *fc; | ||
| 822 | |||
| 823 | spin_lock(&fuse_lock); | ||
| 824 | fc = file->private_data; | ||
| 825 | if (fc) { | ||
| 826 | fc->connected = 0; | ||
| 827 | end_requests(fc, &fc->pending); | ||
| 828 | end_requests(fc, &fc->processing); | ||
| 829 | fuse_release_conn(fc); | ||
| 830 | } | ||
| 831 | spin_unlock(&fuse_lock); | ||
| 832 | return 0; | ||
| 833 | } | ||
| 834 | |||
| 835 | struct file_operations fuse_dev_operations = { | ||
| 836 | .owner = THIS_MODULE, | ||
| 837 | .llseek = no_llseek, | ||
| 838 | .read = fuse_dev_read, | ||
| 839 | .readv = fuse_dev_readv, | ||
| 840 | .write = fuse_dev_write, | ||
| 841 | .writev = fuse_dev_writev, | ||
| 842 | .poll = fuse_dev_poll, | ||
| 843 | .release = fuse_dev_release, | ||
| 844 | }; | ||
| 845 | |||
| 846 | static struct miscdevice fuse_miscdevice = { | ||
| 847 | .minor = FUSE_MINOR, | ||
| 848 | .name = "fuse", | ||
| 849 | .fops = &fuse_dev_operations, | ||
| 850 | }; | ||
| 851 | |||
| 852 | int __init fuse_dev_init(void) | ||
| 853 | { | ||
| 854 | int err = -ENOMEM; | ||
| 855 | fuse_req_cachep = kmem_cache_create("fuse_request", | ||
| 856 | sizeof(struct fuse_req), | ||
| 857 | 0, 0, NULL, NULL); | ||
| 858 | if (!fuse_req_cachep) | ||
| 859 | goto out; | ||
| 860 | |||
| 861 | err = misc_register(&fuse_miscdevice); | ||
| 862 | if (err) | ||
| 863 | goto out_cache_clean; | ||
| 864 | |||
| 865 | return 0; | ||
| 866 | |||
| 867 | out_cache_clean: | ||
| 868 | kmem_cache_destroy(fuse_req_cachep); | ||
| 869 | out: | ||
| 870 | return err; | ||
| 871 | } | ||
| 872 | |||
| 873 | void fuse_dev_cleanup(void) | ||
| 874 | { | ||
| 875 | misc_deregister(&fuse_miscdevice); | ||
| 876 | kmem_cache_destroy(fuse_req_cachep); | ||
| 877 | } | ||
diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c new file mode 100644 index 000000000000..e79e49b3eec7 --- /dev/null +++ b/fs/fuse/dir.c | |||
| @@ -0,0 +1,982 @@ | |||
| 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 "fuse_i.h" | ||
| 10 | |||
| 11 | #include <linux/pagemap.h> | ||
| 12 | #include <linux/file.h> | ||
| 13 | #include <linux/gfp.h> | ||
| 14 | #include <linux/sched.h> | ||
| 15 | #include <linux/namei.h> | ||
| 16 | |||
| 17 | static inline unsigned long time_to_jiffies(unsigned long sec, | ||
| 18 | unsigned long nsec) | ||
| 19 | { | ||
| 20 | struct timespec ts = {sec, nsec}; | ||
| 21 | return jiffies + timespec_to_jiffies(&ts); | ||
| 22 | } | ||
| 23 | |||
| 24 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, | ||
| 25 | struct dentry *entry, | ||
| 26 | struct fuse_entry_out *outarg) | ||
| 27 | { | ||
| 28 | req->in.h.opcode = FUSE_LOOKUP; | ||
| 29 | req->in.h.nodeid = get_node_id(dir); | ||
| 30 | req->inode = dir; | ||
| 31 | req->in.numargs = 1; | ||
| 32 | req->in.args[0].size = entry->d_name.len + 1; | ||
| 33 | req->in.args[0].value = entry->d_name.name; | ||
| 34 | req->out.numargs = 1; | ||
| 35 | req->out.args[0].size = sizeof(struct fuse_entry_out); | ||
| 36 | req->out.args[0].value = outarg; | ||
| 37 | } | ||
| 38 | |||
| 39 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) | ||
| 40 | { | ||
| 41 | if (!entry->d_inode || is_bad_inode(entry->d_inode)) | ||
| 42 | return 0; | ||
| 43 | else if (time_after(jiffies, entry->d_time)) { | ||
| 44 | int err; | ||
| 45 | struct fuse_entry_out outarg; | ||
| 46 | struct inode *inode = entry->d_inode; | ||
| 47 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 48 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 49 | struct fuse_req *req = fuse_get_request(fc); | ||
| 50 | if (!req) | ||
| 51 | return 0; | ||
| 52 | |||
| 53 | fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg); | ||
| 54 | request_send(fc, req); | ||
| 55 | err = req->out.h.error; | ||
| 56 | if (!err) { | ||
| 57 | if (outarg.nodeid != get_node_id(inode)) { | ||
| 58 | fuse_send_forget(fc, req, outarg.nodeid, 1); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | fi->nlookup ++; | ||
| 62 | } | ||
| 63 | fuse_put_request(fc, req); | ||
| 64 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) | ||
| 65 | return 0; | ||
| 66 | |||
| 67 | fuse_change_attributes(inode, &outarg.attr); | ||
| 68 | entry->d_time = time_to_jiffies(outarg.entry_valid, | ||
| 69 | outarg.entry_valid_nsec); | ||
| 70 | fi->i_time = time_to_jiffies(outarg.attr_valid, | ||
| 71 | outarg.attr_valid_nsec); | ||
| 72 | } | ||
| 73 | return 1; | ||
| 74 | } | ||
| 75 | |||
| 76 | static struct dentry_operations fuse_dentry_operations = { | ||
| 77 | .d_revalidate = fuse_dentry_revalidate, | ||
| 78 | }; | ||
| 79 | |||
| 80 | static int fuse_lookup_iget(struct inode *dir, struct dentry *entry, | ||
| 81 | struct inode **inodep) | ||
| 82 | { | ||
| 83 | int err; | ||
| 84 | struct fuse_entry_out outarg; | ||
| 85 | struct inode *inode = NULL; | ||
| 86 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 87 | struct fuse_req *req; | ||
| 88 | |||
| 89 | if (entry->d_name.len > FUSE_NAME_MAX) | ||
| 90 | return -ENAMETOOLONG; | ||
| 91 | |||
| 92 | req = fuse_get_request(fc); | ||
| 93 | if (!req) | ||
| 94 | return -EINTR; | ||
| 95 | |||
| 96 | fuse_lookup_init(req, dir, entry, &outarg); | ||
| 97 | request_send(fc, req); | ||
| 98 | err = req->out.h.error; | ||
| 99 | if (!err) { | ||
| 100 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, | ||
| 101 | &outarg.attr); | ||
| 102 | if (!inode) { | ||
| 103 | fuse_send_forget(fc, req, outarg.nodeid, 1); | ||
| 104 | return -ENOMEM; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | fuse_put_request(fc, req); | ||
| 108 | if (err && err != -ENOENT) | ||
| 109 | return err; | ||
| 110 | |||
| 111 | if (inode) { | ||
| 112 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 113 | entry->d_time = time_to_jiffies(outarg.entry_valid, | ||
| 114 | outarg.entry_valid_nsec); | ||
| 115 | fi->i_time = time_to_jiffies(outarg.attr_valid, | ||
| 116 | outarg.attr_valid_nsec); | ||
| 117 | } | ||
| 118 | |||
| 119 | entry->d_op = &fuse_dentry_operations; | ||
| 120 | *inodep = inode; | ||
| 121 | return 0; | ||
| 122 | } | ||
| 123 | |||
| 124 | void fuse_invalidate_attr(struct inode *inode) | ||
| 125 | { | ||
| 126 | get_fuse_inode(inode)->i_time = jiffies - 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | static void fuse_invalidate_entry(struct dentry *entry) | ||
| 130 | { | ||
| 131 | d_invalidate(entry); | ||
| 132 | entry->d_time = jiffies - 1; | ||
| 133 | } | ||
| 134 | |||
| 135 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, | ||
| 136 | struct inode *dir, struct dentry *entry, | ||
| 137 | int mode) | ||
| 138 | { | ||
| 139 | struct fuse_entry_out outarg; | ||
| 140 | struct inode *inode; | ||
| 141 | struct fuse_inode *fi; | ||
| 142 | int err; | ||
| 143 | |||
| 144 | req->in.h.nodeid = get_node_id(dir); | ||
| 145 | req->inode = dir; | ||
| 146 | req->out.numargs = 1; | ||
| 147 | req->out.args[0].size = sizeof(outarg); | ||
| 148 | req->out.args[0].value = &outarg; | ||
| 149 | request_send(fc, req); | ||
| 150 | err = req->out.h.error; | ||
| 151 | if (err) { | ||
| 152 | fuse_put_request(fc, req); | ||
| 153 | return err; | ||
| 154 | } | ||
| 155 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, | ||
| 156 | &outarg.attr); | ||
| 157 | if (!inode) { | ||
| 158 | fuse_send_forget(fc, req, outarg.nodeid, 1); | ||
| 159 | return -ENOMEM; | ||
| 160 | } | ||
| 161 | fuse_put_request(fc, req); | ||
| 162 | |||
| 163 | /* Don't allow userspace to do really stupid things... */ | ||
| 164 | if ((inode->i_mode ^ mode) & S_IFMT) { | ||
| 165 | iput(inode); | ||
| 166 | return -EIO; | ||
| 167 | } | ||
| 168 | |||
| 169 | entry->d_time = time_to_jiffies(outarg.entry_valid, | ||
| 170 | outarg.entry_valid_nsec); | ||
| 171 | |||
| 172 | fi = get_fuse_inode(inode); | ||
| 173 | fi->i_time = time_to_jiffies(outarg.attr_valid, | ||
| 174 | outarg.attr_valid_nsec); | ||
| 175 | |||
| 176 | d_instantiate(entry, inode); | ||
| 177 | fuse_invalidate_attr(dir); | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, | ||
| 182 | dev_t rdev) | ||
| 183 | { | ||
| 184 | struct fuse_mknod_in inarg; | ||
| 185 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 186 | struct fuse_req *req = fuse_get_request(fc); | ||
| 187 | if (!req) | ||
| 188 | return -EINTR; | ||
| 189 | |||
| 190 | memset(&inarg, 0, sizeof(inarg)); | ||
| 191 | inarg.mode = mode; | ||
| 192 | inarg.rdev = new_encode_dev(rdev); | ||
| 193 | req->in.h.opcode = FUSE_MKNOD; | ||
| 194 | req->in.numargs = 2; | ||
| 195 | req->in.args[0].size = sizeof(inarg); | ||
| 196 | req->in.args[0].value = &inarg; | ||
| 197 | req->in.args[1].size = entry->d_name.len + 1; | ||
| 198 | req->in.args[1].value = entry->d_name.name; | ||
| 199 | return create_new_entry(fc, req, dir, entry, mode); | ||
| 200 | } | ||
| 201 | |||
| 202 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, | ||
| 203 | struct nameidata *nd) | ||
| 204 | { | ||
| 205 | return fuse_mknod(dir, entry, mode, 0); | ||
| 206 | } | ||
| 207 | |||
| 208 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) | ||
| 209 | { | ||
| 210 | struct fuse_mkdir_in inarg; | ||
| 211 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 212 | struct fuse_req *req = fuse_get_request(fc); | ||
| 213 | if (!req) | ||
| 214 | return -EINTR; | ||
| 215 | |||
| 216 | memset(&inarg, 0, sizeof(inarg)); | ||
| 217 | inarg.mode = mode; | ||
| 218 | req->in.h.opcode = FUSE_MKDIR; | ||
| 219 | req->in.numargs = 2; | ||
| 220 | req->in.args[0].size = sizeof(inarg); | ||
| 221 | req->in.args[0].value = &inarg; | ||
| 222 | req->in.args[1].size = entry->d_name.len + 1; | ||
| 223 | req->in.args[1].value = entry->d_name.name; | ||
| 224 | return create_new_entry(fc, req, dir, entry, S_IFDIR); | ||
| 225 | } | ||
| 226 | |||
| 227 | static int fuse_symlink(struct inode *dir, struct dentry *entry, | ||
| 228 | const char *link) | ||
| 229 | { | ||
| 230 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 231 | unsigned len = strlen(link) + 1; | ||
| 232 | struct fuse_req *req; | ||
| 233 | |||
| 234 | if (len > FUSE_SYMLINK_MAX) | ||
| 235 | return -ENAMETOOLONG; | ||
| 236 | |||
| 237 | req = fuse_get_request(fc); | ||
| 238 | if (!req) | ||
| 239 | return -EINTR; | ||
| 240 | |||
| 241 | req->in.h.opcode = FUSE_SYMLINK; | ||
| 242 | req->in.numargs = 2; | ||
| 243 | req->in.args[0].size = entry->d_name.len + 1; | ||
| 244 | req->in.args[0].value = entry->d_name.name; | ||
| 245 | req->in.args[1].size = len; | ||
| 246 | req->in.args[1].value = link; | ||
| 247 | return create_new_entry(fc, req, dir, entry, S_IFLNK); | ||
| 248 | } | ||
| 249 | |||
| 250 | static int fuse_unlink(struct inode *dir, struct dentry *entry) | ||
| 251 | { | ||
| 252 | int err; | ||
| 253 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 254 | struct fuse_req *req = fuse_get_request(fc); | ||
| 255 | if (!req) | ||
| 256 | return -EINTR; | ||
| 257 | |||
| 258 | req->in.h.opcode = FUSE_UNLINK; | ||
| 259 | req->in.h.nodeid = get_node_id(dir); | ||
| 260 | req->inode = dir; | ||
| 261 | req->in.numargs = 1; | ||
| 262 | req->in.args[0].size = entry->d_name.len + 1; | ||
| 263 | req->in.args[0].value = entry->d_name.name; | ||
| 264 | request_send(fc, req); | ||
| 265 | err = req->out.h.error; | ||
| 266 | fuse_put_request(fc, req); | ||
| 267 | if (!err) { | ||
| 268 | struct inode *inode = entry->d_inode; | ||
| 269 | |||
| 270 | /* Set nlink to zero so the inode can be cleared, if | ||
| 271 | the inode does have more links this will be | ||
| 272 | discovered at the next lookup/getattr */ | ||
| 273 | inode->i_nlink = 0; | ||
| 274 | fuse_invalidate_attr(inode); | ||
| 275 | fuse_invalidate_attr(dir); | ||
| 276 | } else if (err == -EINTR) | ||
| 277 | fuse_invalidate_entry(entry); | ||
| 278 | return err; | ||
| 279 | } | ||
| 280 | |||
| 281 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | ||
| 282 | { | ||
| 283 | int err; | ||
| 284 | struct fuse_conn *fc = get_fuse_conn(dir); | ||
| 285 | struct fuse_req *req = fuse_get_request(fc); | ||
| 286 | if (!req) | ||
| 287 | return -EINTR; | ||
| 288 | |||
| 289 | req->in.h.opcode = FUSE_RMDIR; | ||
| 290 | req->in.h.nodeid = get_node_id(dir); | ||
| 291 | req->inode = dir; | ||
| 292 | req->in.numargs = 1; | ||
| 293 | req->in.args[0].size = entry->d_name.len + 1; | ||
| 294 | req->in.args[0].value = entry->d_name.name; | ||
| 295 | request_send(fc, req); | ||
| 296 | err = req->out.h.error; | ||
| 297 | fuse_put_request(fc, req); | ||
| 298 | if (!err) { | ||
| 299 | entry->d_inode->i_nlink = 0; | ||
| 300 | fuse_invalidate_attr(dir); | ||
| 301 | } else if (err == -EINTR) | ||
| 302 | fuse_invalidate_entry(entry); | ||
| 303 | return err; | ||
| 304 | } | ||
| 305 | |||
| 306 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, | ||
| 307 | struct inode *newdir, struct dentry *newent) | ||
| 308 | { | ||
| 309 | int err; | ||
| 310 | struct fuse_rename_in inarg; | ||
| 311 | struct fuse_conn *fc = get_fuse_conn(olddir); | ||
| 312 | struct fuse_req *req = fuse_get_request(fc); | ||
| 313 | if (!req) | ||
| 314 | return -EINTR; | ||
| 315 | |||
| 316 | memset(&inarg, 0, sizeof(inarg)); | ||
| 317 | inarg.newdir = get_node_id(newdir); | ||
| 318 | req->in.h.opcode = FUSE_RENAME; | ||
| 319 | req->in.h.nodeid = get_node_id(olddir); | ||
| 320 | req->inode = olddir; | ||
| 321 | req->inode2 = newdir; | ||
| 322 | req->in.numargs = 3; | ||
| 323 | req->in.args[0].size = sizeof(inarg); | ||
| 324 | req->in.args[0].value = &inarg; | ||
| 325 | req->in.args[1].size = oldent->d_name.len + 1; | ||
| 326 | req->in.args[1].value = oldent->d_name.name; | ||
| 327 | req->in.args[2].size = newent->d_name.len + 1; | ||
| 328 | req->in.args[2].value = newent->d_name.name; | ||
| 329 | request_send(fc, req); | ||
| 330 | err = req->out.h.error; | ||
| 331 | fuse_put_request(fc, req); | ||
| 332 | if (!err) { | ||
| 333 | fuse_invalidate_attr(olddir); | ||
| 334 | if (olddir != newdir) | ||
| 335 | fuse_invalidate_attr(newdir); | ||
| 336 | } else if (err == -EINTR) { | ||
| 337 | /* If request was interrupted, DEITY only knows if the | ||
| 338 | rename actually took place. If the invalidation | ||
| 339 | fails (e.g. some process has CWD under the renamed | ||
| 340 | directory), then there can be inconsistency between | ||
| 341 | the dcache and the real filesystem. Tough luck. */ | ||
| 342 | fuse_invalidate_entry(oldent); | ||
| 343 | if (newent->d_inode) | ||
| 344 | fuse_invalidate_entry(newent); | ||
| 345 | } | ||
| 346 | |||
| 347 | return err; | ||
| 348 | } | ||
| 349 | |||
| 350 | static int fuse_link(struct dentry *entry, struct inode *newdir, | ||
| 351 | struct dentry *newent) | ||
| 352 | { | ||
| 353 | int err; | ||
| 354 | struct fuse_link_in inarg; | ||
| 355 | struct inode *inode = entry->d_inode; | ||
| 356 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 357 | struct fuse_req *req = fuse_get_request(fc); | ||
| 358 | if (!req) | ||
| 359 | return -EINTR; | ||
| 360 | |||
| 361 | memset(&inarg, 0, sizeof(inarg)); | ||
| 362 | inarg.oldnodeid = get_node_id(inode); | ||
| 363 | req->in.h.opcode = FUSE_LINK; | ||
| 364 | req->inode2 = inode; | ||
| 365 | req->in.numargs = 2; | ||
| 366 | req->in.args[0].size = sizeof(inarg); | ||
| 367 | req->in.args[0].value = &inarg; | ||
| 368 | req->in.args[1].size = newent->d_name.len + 1; | ||
| 369 | req->in.args[1].value = newent->d_name.name; | ||
| 370 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); | ||
| 371 | /* Contrary to "normal" filesystems it can happen that link | ||
| 372 | makes two "logical" inodes point to the same "physical" | ||
| 373 | inode. We invalidate the attributes of the old one, so it | ||
| 374 | will reflect changes in the backing inode (link count, | ||
| 375 | etc.) | ||
| 376 | */ | ||
| 377 | if (!err || err == -EINTR) | ||
| 378 | fuse_invalidate_attr(inode); | ||
| 379 | return err; | ||
| 380 | } | ||
| 381 | |||
| 382 | int fuse_do_getattr(struct inode *inode) | ||
| 383 | { | ||
| 384 | int err; | ||
| 385 | struct fuse_attr_out arg; | ||
| 386 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 387 | struct fuse_req *req = fuse_get_request(fc); | ||
| 388 | if (!req) | ||
| 389 | return -EINTR; | ||
| 390 | |||
| 391 | req->in.h.opcode = FUSE_GETATTR; | ||
| 392 | req->in.h.nodeid = get_node_id(inode); | ||
| 393 | req->inode = inode; | ||
| 394 | req->out.numargs = 1; | ||
| 395 | req->out.args[0].size = sizeof(arg); | ||
| 396 | req->out.args[0].value = &arg; | ||
| 397 | request_send(fc, req); | ||
| 398 | err = req->out.h.error; | ||
| 399 | fuse_put_request(fc, req); | ||
| 400 | if (!err) { | ||
| 401 | if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) { | ||
| 402 | make_bad_inode(inode); | ||
| 403 | err = -EIO; | ||
| 404 | } else { | ||
| 405 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 406 | fuse_change_attributes(inode, &arg.attr); | ||
| 407 | fi->i_time = time_to_jiffies(arg.attr_valid, | ||
| 408 | arg.attr_valid_nsec); | ||
| 409 | } | ||
| 410 | } | ||
| 411 | return err; | ||
| 412 | } | ||
| 413 | |||
| 414 | /* | ||
| 415 | * Calling into a user-controlled filesystem gives the filesystem | ||
| 416 | * daemon ptrace-like capabilities over the requester process. This | ||
| 417 | * means, that the filesystem daemon is able to record the exact | ||
| 418 | * filesystem operations performed, and can also control the behavior | ||
| 419 | * of the requester process in otherwise impossible ways. For example | ||
| 420 | * it can delay the operation for arbitrary length of time allowing | ||
| 421 | * DoS against the requester. | ||
| 422 | * | ||
| 423 | * For this reason only those processes can call into the filesystem, | ||
| 424 | * for which the owner of the mount has ptrace privilege. This | ||
| 425 | * excludes processes started by other users, suid or sgid processes. | ||
| 426 | */ | ||
| 427 | static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) | ||
| 428 | { | ||
| 429 | if (fc->flags & FUSE_ALLOW_OTHER) | ||
| 430 | return 1; | ||
| 431 | |||
| 432 | if (task->euid == fc->user_id && | ||
| 433 | task->suid == fc->user_id && | ||
| 434 | task->uid == fc->user_id && | ||
| 435 | task->egid == fc->group_id && | ||
| 436 | task->sgid == fc->group_id && | ||
| 437 | task->gid == fc->group_id) | ||
| 438 | return 1; | ||
| 439 | |||
| 440 | return 0; | ||
| 441 | } | ||
| 442 | |||
| 443 | static int fuse_revalidate(struct dentry *entry) | ||
| 444 | { | ||
| 445 | struct inode *inode = entry->d_inode; | ||
| 446 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 447 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 448 | |||
| 449 | if (!fuse_allow_task(fc, current)) | ||
| 450 | return -EACCES; | ||
| 451 | if (get_node_id(inode) != FUSE_ROOT_ID && | ||
| 452 | time_before_eq(jiffies, fi->i_time)) | ||
| 453 | return 0; | ||
| 454 | |||
| 455 | return fuse_do_getattr(inode); | ||
| 456 | } | ||
| 457 | |||
| 458 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) | ||
| 459 | { | ||
| 460 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 461 | |||
| 462 | if (!fuse_allow_task(fc, current)) | ||
| 463 | return -EACCES; | ||
| 464 | else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { | ||
| 465 | int err = generic_permission(inode, mask, NULL); | ||
| 466 | |||
| 467 | /* If permission is denied, try to refresh file | ||
| 468 | attributes. This is also needed, because the root | ||
| 469 | node will at first have no permissions */ | ||
| 470 | if (err == -EACCES) { | ||
| 471 | err = fuse_do_getattr(inode); | ||
| 472 | if (!err) | ||
| 473 | err = generic_permission(inode, mask, NULL); | ||
| 474 | } | ||
| 475 | |||
| 476 | /* FIXME: Need some mechanism to revoke permissions: | ||
| 477 | currently if the filesystem suddenly changes the | ||
| 478 | file mode, we will not be informed about it, and | ||
| 479 | continue to allow access to the file/directory. | ||
| 480 | |||
| 481 | This is actually not so grave, since the user can | ||
| 482 | simply keep access to the file/directory anyway by | ||
| 483 | keeping it open... */ | ||
| 484 | |||
| 485 | return err; | ||
| 486 | } else { | ||
| 487 | int mode = inode->i_mode; | ||
| 488 | if ((mask & MAY_WRITE) && IS_RDONLY(inode) && | ||
| 489 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) | ||
| 490 | return -EROFS; | ||
| 491 | if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO)) | ||
| 492 | return -EACCES; | ||
| 493 | return 0; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, | ||
| 498 | void *dstbuf, filldir_t filldir) | ||
| 499 | { | ||
| 500 | while (nbytes >= FUSE_NAME_OFFSET) { | ||
| 501 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; | ||
| 502 | size_t reclen = FUSE_DIRENT_SIZE(dirent); | ||
| 503 | int over; | ||
| 504 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | ||
| 505 | return -EIO; | ||
| 506 | if (reclen > nbytes) | ||
| 507 | break; | ||
| 508 | |||
| 509 | over = filldir(dstbuf, dirent->name, dirent->namelen, | ||
| 510 | file->f_pos, dirent->ino, dirent->type); | ||
| 511 | if (over) | ||
| 512 | break; | ||
| 513 | |||
| 514 | buf += reclen; | ||
| 515 | nbytes -= reclen; | ||
| 516 | file->f_pos = dirent->off; | ||
| 517 | } | ||
| 518 | |||
| 519 | return 0; | ||
| 520 | } | ||
| 521 | |||
| 522 | static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file, | ||
| 523 | struct inode *inode, loff_t pos, | ||
| 524 | size_t count) | ||
| 525 | { | ||
| 526 | return fuse_send_read_common(req, file, inode, pos, count, 1); | ||
| 527 | } | ||
| 528 | |||
| 529 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) | ||
| 530 | { | ||
| 531 | int err; | ||
| 532 | size_t nbytes; | ||
| 533 | struct page *page; | ||
| 534 | struct inode *inode = file->f_dentry->d_inode; | ||
| 535 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 536 | struct fuse_req *req = fuse_get_request(fc); | ||
| 537 | if (!req) | ||
| 538 | return -EINTR; | ||
| 539 | |||
| 540 | page = alloc_page(GFP_KERNEL); | ||
| 541 | if (!page) { | ||
| 542 | fuse_put_request(fc, req); | ||
| 543 | return -ENOMEM; | ||
| 544 | } | ||
| 545 | req->num_pages = 1; | ||
| 546 | req->pages[0] = page; | ||
| 547 | nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE); | ||
| 548 | err = req->out.h.error; | ||
| 549 | fuse_put_request(fc, req); | ||
| 550 | if (!err) | ||
| 551 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, | ||
| 552 | filldir); | ||
| 553 | |||
| 554 | __free_page(page); | ||
| 555 | fuse_invalidate_attr(inode); /* atime changed */ | ||
| 556 | return err; | ||
| 557 | } | ||
| 558 | |||
| 559 | static char *read_link(struct dentry *dentry) | ||
| 560 | { | ||
| 561 | struct inode *inode = dentry->d_inode; | ||
| 562 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 563 | struct fuse_req *req = fuse_get_request(fc); | ||
| 564 | char *link; | ||
| 565 | |||
| 566 | if (!req) | ||
| 567 | return ERR_PTR(-EINTR); | ||
| 568 | |||
| 569 | link = (char *) __get_free_page(GFP_KERNEL); | ||
| 570 | if (!link) { | ||
| 571 | link = ERR_PTR(-ENOMEM); | ||
| 572 | goto out; | ||
| 573 | } | ||
| 574 | req->in.h.opcode = FUSE_READLINK; | ||
| 575 | req->in.h.nodeid = get_node_id(inode); | ||
| 576 | req->inode = inode; | ||
| 577 | req->out.argvar = 1; | ||
| 578 | req->out.numargs = 1; | ||
| 579 | req->out.args[0].size = PAGE_SIZE - 1; | ||
| 580 | req->out.args[0].value = link; | ||
| 581 | request_send(fc, req); | ||
| 582 | if (req->out.h.error) { | ||
| 583 | free_page((unsigned long) link); | ||
| 584 | link = ERR_PTR(req->out.h.error); | ||
| 585 | } else | ||
| 586 | link[req->out.args[0].size] = '\0'; | ||
| 587 | out: | ||
| 588 | fuse_put_request(fc, req); | ||
| 589 | fuse_invalidate_attr(inode); /* atime changed */ | ||
| 590 | return link; | ||
| 591 | } | ||
| 592 | |||
| 593 | static void free_link(char *link) | ||
| 594 | { | ||
| 595 | if (!IS_ERR(link)) | ||
| 596 | free_page((unsigned long) link); | ||
| 597 | } | ||
| 598 | |||
| 599 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) | ||
| 600 | { | ||
| 601 | nd_set_link(nd, read_link(dentry)); | ||
| 602 | return NULL; | ||
| 603 | } | ||
| 604 | |||
| 605 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) | ||
| 606 | { | ||
| 607 | free_link(nd_get_link(nd)); | ||
| 608 | } | ||
| 609 | |||
| 610 | static int fuse_dir_open(struct inode *inode, struct file *file) | ||
| 611 | { | ||
| 612 | return fuse_open_common(inode, file, 1); | ||
| 613 | } | ||
| 614 | |||
| 615 | static int fuse_dir_release(struct inode *inode, struct file *file) | ||
| 616 | { | ||
| 617 | return fuse_release_common(inode, file, 1); | ||
| 618 | } | ||
| 619 | |||
| 620 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) | ||
| 621 | { | ||
| 622 | /* nfsd can call this with no file */ | ||
| 623 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; | ||
| 624 | } | ||
| 625 | |||
| 626 | static unsigned iattr_to_fattr(struct iattr *iattr, struct fuse_attr *fattr) | ||
| 627 | { | ||
| 628 | unsigned ivalid = iattr->ia_valid; | ||
| 629 | unsigned fvalid = 0; | ||
| 630 | |||
| 631 | memset(fattr, 0, sizeof(*fattr)); | ||
| 632 | |||
| 633 | if (ivalid & ATTR_MODE) | ||
| 634 | fvalid |= FATTR_MODE, fattr->mode = iattr->ia_mode; | ||
| 635 | if (ivalid & ATTR_UID) | ||
| 636 | fvalid |= FATTR_UID, fattr->uid = iattr->ia_uid; | ||
| 637 | if (ivalid & ATTR_GID) | ||
| 638 | fvalid |= FATTR_GID, fattr->gid = iattr->ia_gid; | ||
| 639 | if (ivalid & ATTR_SIZE) | ||
| 640 | fvalid |= FATTR_SIZE, fattr->size = iattr->ia_size; | ||
| 641 | /* You can only _set_ these together (they may change by themselves) */ | ||
| 642 | if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) { | ||
| 643 | fvalid |= FATTR_ATIME | FATTR_MTIME; | ||
| 644 | fattr->atime = iattr->ia_atime.tv_sec; | ||
| 645 | fattr->mtime = iattr->ia_mtime.tv_sec; | ||
| 646 | } | ||
| 647 | |||
| 648 | return fvalid; | ||
| 649 | } | ||
| 650 | |||
| 651 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) | ||
| 652 | { | ||
| 653 | struct inode *inode = entry->d_inode; | ||
| 654 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 655 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 656 | struct fuse_req *req; | ||
| 657 | struct fuse_setattr_in inarg; | ||
| 658 | struct fuse_attr_out outarg; | ||
| 659 | int err; | ||
| 660 | int is_truncate = 0; | ||
| 661 | |||
| 662 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { | ||
| 663 | err = inode_change_ok(inode, attr); | ||
| 664 | if (err) | ||
| 665 | return err; | ||
| 666 | } | ||
| 667 | |||
| 668 | if (attr->ia_valid & ATTR_SIZE) { | ||
| 669 | unsigned long limit; | ||
| 670 | is_truncate = 1; | ||
| 671 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; | ||
| 672 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { | ||
| 673 | send_sig(SIGXFSZ, current, 0); | ||
| 674 | return -EFBIG; | ||
| 675 | } | ||
| 676 | } | ||
| 677 | |||
| 678 | req = fuse_get_request(fc); | ||
| 679 | if (!req) | ||
| 680 | return -EINTR; | ||
| 681 | |||
| 682 | memset(&inarg, 0, sizeof(inarg)); | ||
| 683 | inarg.valid = iattr_to_fattr(attr, &inarg.attr); | ||
| 684 | req->in.h.opcode = FUSE_SETATTR; | ||
| 685 | req->in.h.nodeid = get_node_id(inode); | ||
| 686 | req->inode = inode; | ||
| 687 | req->in.numargs = 1; | ||
| 688 | req->in.args[0].size = sizeof(inarg); | ||
| 689 | req->in.args[0].value = &inarg; | ||
| 690 | req->out.numargs = 1; | ||
| 691 | req->out.args[0].size = sizeof(outarg); | ||
| 692 | req->out.args[0].value = &outarg; | ||
| 693 | request_send(fc, req); | ||
| 694 | err = req->out.h.error; | ||
| 695 | fuse_put_request(fc, req); | ||
| 696 | if (!err) { | ||
| 697 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { | ||
| 698 | make_bad_inode(inode); | ||
| 699 | err = -EIO; | ||
| 700 | } else { | ||
| 701 | if (is_truncate) { | ||
| 702 | loff_t origsize = i_size_read(inode); | ||
| 703 | i_size_write(inode, outarg.attr.size); | ||
| 704 | if (origsize > outarg.attr.size) | ||
| 705 | vmtruncate(inode, outarg.attr.size); | ||
| 706 | } | ||
| 707 | fuse_change_attributes(inode, &outarg.attr); | ||
| 708 | fi->i_time = time_to_jiffies(outarg.attr_valid, | ||
| 709 | outarg.attr_valid_nsec); | ||
| 710 | } | ||
| 711 | } else if (err == -EINTR) | ||
| 712 | fuse_invalidate_attr(inode); | ||
| 713 | |||
| 714 | return err; | ||
| 715 | } | ||
| 716 | |||
| 717 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, | ||
| 718 | struct kstat *stat) | ||
| 719 | { | ||
| 720 | struct inode *inode = entry->d_inode; | ||
| 721 | int err = fuse_revalidate(entry); | ||
| 722 | if (!err) | ||
| 723 | generic_fillattr(inode, stat); | ||
| 724 | |||
| 725 | return err; | ||
| 726 | } | ||
| 727 | |||
| 728 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, | ||
| 729 | struct nameidata *nd) | ||
| 730 | { | ||
| 731 | struct inode *inode; | ||
| 732 | int err = fuse_lookup_iget(dir, entry, &inode); | ||
| 733 | if (err) | ||
| 734 | return ERR_PTR(err); | ||
| 735 | if (inode && S_ISDIR(inode->i_mode)) { | ||
| 736 | /* Don't allow creating an alias to a directory */ | ||
| 737 | struct dentry *alias = d_find_alias(inode); | ||
| 738 | if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) { | ||
| 739 | dput(alias); | ||
| 740 | iput(inode); | ||
| 741 | return ERR_PTR(-EIO); | ||
| 742 | } | ||
| 743 | } | ||
| 744 | return d_splice_alias(inode, entry); | ||
| 745 | } | ||
| 746 | |||
| 747 | static int fuse_setxattr(struct dentry *entry, const char *name, | ||
| 748 | const void *value, size_t size, int flags) | ||
| 749 | { | ||
| 750 | struct inode *inode = entry->d_inode; | ||
| 751 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 752 | struct fuse_req *req; | ||
| 753 | struct fuse_setxattr_in inarg; | ||
| 754 | int err; | ||
| 755 | |||
| 756 | if (size > FUSE_XATTR_SIZE_MAX) | ||
| 757 | return -E2BIG; | ||
| 758 | |||
| 759 | if (fc->no_setxattr) | ||
| 760 | return -EOPNOTSUPP; | ||
| 761 | |||
| 762 | req = fuse_get_request(fc); | ||
| 763 | if (!req) | ||
| 764 | return -EINTR; | ||
| 765 | |||
| 766 | memset(&inarg, 0, sizeof(inarg)); | ||
| 767 | inarg.size = size; | ||
| 768 | inarg.flags = flags; | ||
| 769 | req->in.h.opcode = FUSE_SETXATTR; | ||
| 770 | req->in.h.nodeid = get_node_id(inode); | ||
| 771 | req->inode = inode; | ||
| 772 | req->in.numargs = 3; | ||
| 773 | req->in.args[0].size = sizeof(inarg); | ||
| 774 | req->in.args[0].value = &inarg; | ||
| 775 | req->in.args[1].size = strlen(name) + 1; | ||
| 776 | req->in.args[1].value = name; | ||
| 777 | req->in.args[2].size = size; | ||
| 778 | req->in.args[2].value = value; | ||
| 779 | request_send(fc, req); | ||
| 780 | err = req->out.h.error; | ||
| 781 | fuse_put_request(fc, req); | ||
| 782 | if (err == -ENOSYS) { | ||
| 783 | fc->no_setxattr = 1; | ||
| 784 | err = -EOPNOTSUPP; | ||
| 785 | } | ||
| 786 | return err; | ||
| 787 | } | ||
| 788 | |||
| 789 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, | ||
| 790 | void *value, size_t size) | ||
| 791 | { | ||
| 792 | struct inode *inode = entry->d_inode; | ||
| 793 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 794 | struct fuse_req *req; | ||
| 795 | struct fuse_getxattr_in inarg; | ||
| 796 | struct fuse_getxattr_out outarg; | ||
| 797 | ssize_t ret; | ||
| 798 | |||
| 799 | if (fc->no_getxattr) | ||
| 800 | return -EOPNOTSUPP; | ||
| 801 | |||
| 802 | req = fuse_get_request(fc); | ||
| 803 | if (!req) | ||
| 804 | return -EINTR; | ||
| 805 | |||
| 806 | memset(&inarg, 0, sizeof(inarg)); | ||
| 807 | inarg.size = size; | ||
| 808 | req->in.h.opcode = FUSE_GETXATTR; | ||
| 809 | req->in.h.nodeid = get_node_id(inode); | ||
| 810 | req->inode = inode; | ||
| 811 | req->in.numargs = 2; | ||
| 812 | req->in.args[0].size = sizeof(inarg); | ||
| 813 | req->in.args[0].value = &inarg; | ||
| 814 | req->in.args[1].size = strlen(name) + 1; | ||
| 815 | req->in.args[1].value = name; | ||
| 816 | /* This is really two different operations rolled into one */ | ||
| 817 | req->out.numargs = 1; | ||
| 818 | if (size) { | ||
| 819 | req->out.argvar = 1; | ||
| 820 | req->out.args[0].size = size; | ||
| 821 | req->out.args[0].value = value; | ||
| 822 | } else { | ||
| 823 | req->out.args[0].size = sizeof(outarg); | ||
| 824 | req->out.args[0].value = &outarg; | ||
| 825 | } | ||
| 826 | request_send(fc, req); | ||
| 827 | ret = req->out.h.error; | ||
| 828 | if (!ret) | ||
| 829 | ret = size ? req->out.args[0].size : outarg.size; | ||
| 830 | else { | ||
| 831 | if (ret == -ENOSYS) { | ||
| 832 | fc->no_getxattr = 1; | ||
| 833 | ret = -EOPNOTSUPP; | ||
| 834 | } | ||
| 835 | } | ||
| 836 | fuse_put_request(fc, req); | ||
| 837 | return ret; | ||
| 838 | } | ||
| 839 | |||
| 840 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) | ||
| 841 | { | ||
| 842 | struct inode *inode = entry->d_inode; | ||
| 843 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 844 | struct fuse_req *req; | ||
| 845 | struct fuse_getxattr_in inarg; | ||
| 846 | struct fuse_getxattr_out outarg; | ||
| 847 | ssize_t ret; | ||
| 848 | |||
| 849 | if (fc->no_listxattr) | ||
| 850 | return -EOPNOTSUPP; | ||
| 851 | |||
| 852 | req = fuse_get_request(fc); | ||
| 853 | if (!req) | ||
| 854 | return -EINTR; | ||
| 855 | |||
| 856 | memset(&inarg, 0, sizeof(inarg)); | ||
| 857 | inarg.size = size; | ||
| 858 | req->in.h.opcode = FUSE_LISTXATTR; | ||
| 859 | req->in.h.nodeid = get_node_id(inode); | ||
| 860 | req->inode = inode; | ||
| 861 | req->in.numargs = 1; | ||
| 862 | req->in.args[0].size = sizeof(inarg); | ||
| 863 | req->in.args[0].value = &inarg; | ||
| 864 | /* This is really two different operations rolled into one */ | ||
| 865 | req->out.numargs = 1; | ||
| 866 | if (size) { | ||
| 867 | req->out.argvar = 1; | ||
| 868 | req->out.args[0].size = size; | ||
| 869 | req->out.args[0].value = list; | ||
| 870 | } else { | ||
| 871 | req->out.args[0].size = sizeof(outarg); | ||
| 872 | req->out.args[0].value = &outarg; | ||
| 873 | } | ||
| 874 | request_send(fc, req); | ||
| 875 | ret = req->out.h.error; | ||
| 876 | if (!ret) | ||
| 877 | ret = size ? req->out.args[0].size : outarg.size; | ||
| 878 | else { | ||
| 879 | if (ret == -ENOSYS) { | ||
| 880 | fc->no_listxattr = 1; | ||
| 881 | ret = -EOPNOTSUPP; | ||
| 882 | } | ||
| 883 | } | ||
| 884 | fuse_put_request(fc, req); | ||
| 885 | return ret; | ||
| 886 | } | ||
| 887 | |||
| 888 | static int fuse_removexattr(struct dentry *entry, const char *name) | ||
| 889 | { | ||
| 890 | struct inode *inode = entry->d_inode; | ||
| 891 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 892 | struct fuse_req *req; | ||
| 893 | int err; | ||
| 894 | |||
| 895 | if (fc->no_removexattr) | ||
| 896 | return -EOPNOTSUPP; | ||
| 897 | |||
| 898 | req = fuse_get_request(fc); | ||
| 899 | if (!req) | ||
| 900 | return -EINTR; | ||
| 901 | |||
| 902 | req->in.h.opcode = FUSE_REMOVEXATTR; | ||
| 903 | req->in.h.nodeid = get_node_id(inode); | ||
| 904 | req->inode = inode; | ||
| 905 | req->in.numargs = 1; | ||
| 906 | req->in.args[0].size = strlen(name) + 1; | ||
| 907 | req->in.args[0].value = name; | ||
| 908 | request_send(fc, req); | ||
| 909 | err = req->out.h.error; | ||
| 910 | fuse_put_request(fc, req); | ||
| 911 | if (err == -ENOSYS) { | ||
| 912 | fc->no_removexattr = 1; | ||
| 913 | err = -EOPNOTSUPP; | ||
| 914 | } | ||
| 915 | return err; | ||
| 916 | } | ||
| 917 | |||
| 918 | static struct inode_operations fuse_dir_inode_operations = { | ||
| 919 | .lookup = fuse_lookup, | ||
| 920 | .mkdir = fuse_mkdir, | ||
| 921 | .symlink = fuse_symlink, | ||
| 922 | .unlink = fuse_unlink, | ||
| 923 | .rmdir = fuse_rmdir, | ||
| 924 | .rename = fuse_rename, | ||
| 925 | .link = fuse_link, | ||
| 926 | .setattr = fuse_setattr, | ||
| 927 | .create = fuse_create, | ||
| 928 | .mknod = fuse_mknod, | ||
| 929 | .permission = fuse_permission, | ||
| 930 | .getattr = fuse_getattr, | ||
| 931 | .setxattr = fuse_setxattr, | ||
| 932 | .getxattr = fuse_getxattr, | ||
| 933 | .listxattr = fuse_listxattr, | ||
| 934 | .removexattr = fuse_removexattr, | ||
| 935 | }; | ||
| 936 | |||
| 937 | static struct file_operations fuse_dir_operations = { | ||
| 938 | .llseek = generic_file_llseek, | ||
| 939 | .read = generic_read_dir, | ||
| 940 | .readdir = fuse_readdir, | ||
| 941 | .open = fuse_dir_open, | ||
| 942 | .release = fuse_dir_release, | ||
| 943 | .fsync = fuse_dir_fsync, | ||
| 944 | }; | ||
| 945 | |||
| 946 | static struct inode_operations fuse_common_inode_operations = { | ||
| 947 | .setattr = fuse_setattr, | ||
| 948 | .permission = fuse_permission, | ||
| 949 | .getattr = fuse_getattr, | ||
| 950 | .setxattr = fuse_setxattr, | ||
| 951 | .getxattr = fuse_getxattr, | ||
| 952 | .listxattr = fuse_listxattr, | ||
| 953 | .removexattr = fuse_removexattr, | ||
| 954 | }; | ||
| 955 | |||
| 956 | static struct inode_operations fuse_symlink_inode_operations = { | ||
| 957 | .setattr = fuse_setattr, | ||
| 958 | .follow_link = fuse_follow_link, | ||
| 959 | .put_link = fuse_put_link, | ||
| 960 | .readlink = generic_readlink, | ||
| 961 | .getattr = fuse_getattr, | ||
| 962 | .setxattr = fuse_setxattr, | ||
| 963 | .getxattr = fuse_getxattr, | ||
| 964 | .listxattr = fuse_listxattr, | ||
| 965 | .removexattr = fuse_removexattr, | ||
| 966 | }; | ||
| 967 | |||
| 968 | void fuse_init_common(struct inode *inode) | ||
| 969 | { | ||
| 970 | inode->i_op = &fuse_common_inode_operations; | ||
| 971 | } | ||
| 972 | |||
| 973 | void fuse_init_dir(struct inode *inode) | ||
| 974 | { | ||
| 975 | inode->i_op = &fuse_dir_inode_operations; | ||
| 976 | inode->i_fop = &fuse_dir_operations; | ||
| 977 | } | ||
| 978 | |||
| 979 | void fuse_init_symlink(struct inode *inode) | ||
| 980 | { | ||
| 981 | inode->i_op = &fuse_symlink_inode_operations; | ||
| 982 | } | ||
diff --git a/fs/fuse/file.c b/fs/fuse/file.c new file mode 100644 index 000000000000..6454022b0536 --- /dev/null +++ b/fs/fuse/file.c | |||
| @@ -0,0 +1,555 @@ | |||
| 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 "fuse_i.h" | ||
| 10 | |||
| 11 | #include <linux/pagemap.h> | ||
| 12 | #include <linux/slab.h> | ||
| 13 | #include <linux/kernel.h> | ||
| 14 | |||
| 15 | static struct file_operations fuse_direct_io_file_operations; | ||
| 16 | |||
| 17 | int fuse_open_common(struct inode *inode, struct file *file, int isdir) | ||
| 18 | { | ||
| 19 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 20 | struct fuse_req *req; | ||
| 21 | struct fuse_open_in inarg; | ||
| 22 | struct fuse_open_out outarg; | ||
| 23 | struct fuse_file *ff; | ||
| 24 | int err; | ||
| 25 | |||
| 26 | err = generic_file_open(inode, file); | ||
| 27 | if (err) | ||
| 28 | return err; | ||
| 29 | |||
| 30 | /* If opening the root node, no lookup has been performed on | ||
| 31 | it, so the attributes must be refreshed */ | ||
| 32 | if (get_node_id(inode) == FUSE_ROOT_ID) { | ||
| 33 | int err = fuse_do_getattr(inode); | ||
| 34 | if (err) | ||
| 35 | return err; | ||
| 36 | } | ||
| 37 | |||
| 38 | req = fuse_get_request(fc); | ||
| 39 | if (!req) | ||
| 40 | return -EINTR; | ||
| 41 | |||
| 42 | err = -ENOMEM; | ||
| 43 | ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL); | ||
| 44 | if (!ff) | ||
| 45 | goto out_put_request; | ||
| 46 | |||
| 47 | ff->release_req = fuse_request_alloc(); | ||
| 48 | if (!ff->release_req) { | ||
| 49 | kfree(ff); | ||
| 50 | goto out_put_request; | ||
| 51 | } | ||
| 52 | |||
| 53 | memset(&inarg, 0, sizeof(inarg)); | ||
| 54 | inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); | ||
| 55 | req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; | ||
| 56 | req->in.h.nodeid = get_node_id(inode); | ||
| 57 | req->inode = inode; | ||
| 58 | req->in.numargs = 1; | ||
| 59 | req->in.args[0].size = sizeof(inarg); | ||
| 60 | req->in.args[0].value = &inarg; | ||
| 61 | req->out.numargs = 1; | ||
| 62 | req->out.args[0].size = sizeof(outarg); | ||
| 63 | req->out.args[0].value = &outarg; | ||
| 64 | request_send(fc, req); | ||
| 65 | err = req->out.h.error; | ||
| 66 | if (err) { | ||
| 67 | fuse_request_free(ff->release_req); | ||
| 68 | kfree(ff); | ||
| 69 | } else { | ||
| 70 | if (!isdir && (outarg.open_flags & FOPEN_DIRECT_IO)) | ||
| 71 | file->f_op = &fuse_direct_io_file_operations; | ||
| 72 | if (!(outarg.open_flags & FOPEN_KEEP_CACHE)) | ||
| 73 | invalidate_inode_pages(inode->i_mapping); | ||
| 74 | ff->fh = outarg.fh; | ||
| 75 | file->private_data = ff; | ||
| 76 | } | ||
| 77 | |||
| 78 | out_put_request: | ||
| 79 | fuse_put_request(fc, req); | ||
| 80 | return err; | ||
| 81 | } | ||
| 82 | |||
| 83 | int fuse_release_common(struct inode *inode, struct file *file, int isdir) | ||
| 84 | { | ||
| 85 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 86 | struct fuse_file *ff = file->private_data; | ||
| 87 | struct fuse_req *req = ff->release_req; | ||
| 88 | struct fuse_release_in *inarg = &req->misc.release_in; | ||
| 89 | |||
| 90 | inarg->fh = ff->fh; | ||
| 91 | inarg->flags = file->f_flags & ~O_EXCL; | ||
| 92 | req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE; | ||
| 93 | req->in.h.nodeid = get_node_id(inode); | ||
| 94 | req->inode = inode; | ||
| 95 | req->in.numargs = 1; | ||
| 96 | req->in.args[0].size = sizeof(struct fuse_release_in); | ||
| 97 | req->in.args[0].value = inarg; | ||
| 98 | request_send_background(fc, req); | ||
| 99 | kfree(ff); | ||
| 100 | |||
| 101 | /* Return value is ignored by VFS */ | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | static int fuse_open(struct inode *inode, struct file *file) | ||
| 106 | { | ||
| 107 | return fuse_open_common(inode, file, 0); | ||
| 108 | } | ||
| 109 | |||
| 110 | static int fuse_release(struct inode *inode, struct file *file) | ||
| 111 | { | ||
| 112 | return fuse_release_common(inode, file, 0); | ||
| 113 | } | ||
| 114 | |||
| 115 | static int fuse_flush(struct file *file) | ||
| 116 | { | ||
| 117 | struct inode *inode = file->f_dentry->d_inode; | ||
| 118 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 119 | struct fuse_file *ff = file->private_data; | ||
| 120 | struct fuse_req *req; | ||
| 121 | struct fuse_flush_in inarg; | ||
| 122 | int err; | ||
| 123 | |||
| 124 | if (fc->no_flush) | ||
| 125 | return 0; | ||
| 126 | |||
| 127 | req = fuse_get_request(fc); | ||
| 128 | if (!req) | ||
| 129 | return -EINTR; | ||
| 130 | |||
| 131 | memset(&inarg, 0, sizeof(inarg)); | ||
| 132 | inarg.fh = ff->fh; | ||
| 133 | req->in.h.opcode = FUSE_FLUSH; | ||
| 134 | req->in.h.nodeid = get_node_id(inode); | ||
| 135 | req->inode = inode; | ||
| 136 | req->file = file; | ||
| 137 | req->in.numargs = 1; | ||
| 138 | req->in.args[0].size = sizeof(inarg); | ||
| 139 | req->in.args[0].value = &inarg; | ||
| 140 | request_send(fc, req); | ||
| 141 | err = req->out.h.error; | ||
| 142 | fuse_put_request(fc, req); | ||
| 143 | if (err == -ENOSYS) { | ||
| 144 | fc->no_flush = 1; | ||
| 145 | err = 0; | ||
| 146 | } | ||
| 147 | return err; | ||
| 148 | } | ||
| 149 | |||
| 150 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | ||
| 151 | int isdir) | ||
| 152 | { | ||
| 153 | struct inode *inode = de->d_inode; | ||
| 154 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 155 | struct fuse_file *ff = file->private_data; | ||
| 156 | struct fuse_req *req; | ||
| 157 | struct fuse_fsync_in inarg; | ||
| 158 | int err; | ||
| 159 | |||
| 160 | if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir)) | ||
| 161 | return 0; | ||
| 162 | |||
| 163 | req = fuse_get_request(fc); | ||
| 164 | if (!req) | ||
| 165 | return -EINTR; | ||
| 166 | |||
| 167 | memset(&inarg, 0, sizeof(inarg)); | ||
| 168 | inarg.fh = ff->fh; | ||
| 169 | inarg.fsync_flags = datasync ? 1 : 0; | ||
| 170 | req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC; | ||
| 171 | req->in.h.nodeid = get_node_id(inode); | ||
| 172 | req->inode = inode; | ||
| 173 | req->file = file; | ||
| 174 | req->in.numargs = 1; | ||
| 175 | req->in.args[0].size = sizeof(inarg); | ||
| 176 | req->in.args[0].value = &inarg; | ||
| 177 | request_send(fc, req); | ||
| 178 | err = req->out.h.error; | ||
| 179 | fuse_put_request(fc, req); | ||
| 180 | if (err == -ENOSYS) { | ||
| 181 | if (isdir) | ||
| 182 | fc->no_fsyncdir = 1; | ||
| 183 | else | ||
| 184 | fc->no_fsync = 1; | ||
| 185 | err = 0; | ||
| 186 | } | ||
| 187 | return err; | ||
| 188 | } | ||
| 189 | |||
| 190 | static int fuse_fsync(struct file *file, struct dentry *de, int datasync) | ||
| 191 | { | ||
| 192 | return fuse_fsync_common(file, de, datasync, 0); | ||
| 193 | } | ||
| 194 | |||
| 195 | size_t fuse_send_read_common(struct fuse_req *req, struct file *file, | ||
| 196 | struct inode *inode, loff_t pos, size_t count, | ||
| 197 | int isdir) | ||
| 198 | { | ||
| 199 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 200 | struct fuse_file *ff = file->private_data; | ||
| 201 | struct fuse_read_in inarg; | ||
| 202 | |||
| 203 | memset(&inarg, 0, sizeof(struct fuse_read_in)); | ||
| 204 | inarg.fh = ff->fh; | ||
| 205 | inarg.offset = pos; | ||
| 206 | inarg.size = count; | ||
| 207 | req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ; | ||
| 208 | req->in.h.nodeid = get_node_id(inode); | ||
| 209 | req->inode = inode; | ||
| 210 | req->file = file; | ||
| 211 | req->in.numargs = 1; | ||
| 212 | req->in.args[0].size = sizeof(struct fuse_read_in); | ||
| 213 | req->in.args[0].value = &inarg; | ||
| 214 | req->out.argpages = 1; | ||
| 215 | req->out.argvar = 1; | ||
| 216 | req->out.numargs = 1; | ||
| 217 | req->out.args[0].size = count; | ||
| 218 | request_send(fc, req); | ||
| 219 | return req->out.args[0].size; | ||
| 220 | } | ||
| 221 | |||
| 222 | static inline size_t fuse_send_read(struct fuse_req *req, struct file *file, | ||
| 223 | struct inode *inode, loff_t pos, | ||
| 224 | size_t count) | ||
| 225 | { | ||
| 226 | return fuse_send_read_common(req, file, inode, pos, count, 0); | ||
| 227 | } | ||
| 228 | |||
| 229 | static int fuse_readpage(struct file *file, struct page *page) | ||
| 230 | { | ||
| 231 | struct inode *inode = page->mapping->host; | ||
| 232 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 233 | loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT; | ||
| 234 | struct fuse_req *req = fuse_get_request(fc); | ||
| 235 | int err = -EINTR; | ||
| 236 | if (!req) | ||
| 237 | goto out; | ||
| 238 | |||
| 239 | req->out.page_zeroing = 1; | ||
| 240 | req->num_pages = 1; | ||
| 241 | req->pages[0] = page; | ||
| 242 | fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE); | ||
| 243 | err = req->out.h.error; | ||
| 244 | fuse_put_request(fc, req); | ||
| 245 | if (!err) | ||
| 246 | SetPageUptodate(page); | ||
| 247 | fuse_invalidate_attr(inode); /* atime changed */ | ||
| 248 | out: | ||
| 249 | unlock_page(page); | ||
| 250 | return err; | ||
| 251 | } | ||
| 252 | |||
| 253 | static int fuse_send_readpages(struct fuse_req *req, struct file *file, | ||
| 254 | struct inode *inode) | ||
| 255 | { | ||
| 256 | loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT; | ||
| 257 | size_t count = req->num_pages << PAGE_CACHE_SHIFT; | ||
| 258 | unsigned i; | ||
| 259 | req->out.page_zeroing = 1; | ||
| 260 | fuse_send_read(req, file, inode, pos, count); | ||
| 261 | for (i = 0; i < req->num_pages; i++) { | ||
| 262 | struct page *page = req->pages[i]; | ||
| 263 | if (!req->out.h.error) | ||
| 264 | SetPageUptodate(page); | ||
| 265 | unlock_page(page); | ||
| 266 | } | ||
| 267 | return req->out.h.error; | ||
| 268 | } | ||
| 269 | |||
| 270 | struct fuse_readpages_data { | ||
| 271 | struct fuse_req *req; | ||
| 272 | struct file *file; | ||
| 273 | struct inode *inode; | ||
| 274 | }; | ||
| 275 | |||
| 276 | static int fuse_readpages_fill(void *_data, struct page *page) | ||
| 277 | { | ||
| 278 | struct fuse_readpages_data *data = _data; | ||
| 279 | struct fuse_req *req = data->req; | ||
| 280 | struct inode *inode = data->inode; | ||
| 281 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 282 | |||
| 283 | if (req->num_pages && | ||
| 284 | (req->num_pages == FUSE_MAX_PAGES_PER_REQ || | ||
| 285 | (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read || | ||
| 286 | req->pages[req->num_pages - 1]->index + 1 != page->index)) { | ||
| 287 | int err = fuse_send_readpages(req, data->file, inode); | ||
| 288 | if (err) { | ||
| 289 | unlock_page(page); | ||
| 290 | return err; | ||
| 291 | } | ||
| 292 | fuse_reset_request(req); | ||
| 293 | } | ||
| 294 | req->pages[req->num_pages] = page; | ||
| 295 | req->num_pages ++; | ||
| 296 | return 0; | ||
| 297 | } | ||
| 298 | |||
| 299 | static int fuse_readpages(struct file *file, struct address_space *mapping, | ||
| 300 | struct list_head *pages, unsigned nr_pages) | ||
| 301 | { | ||
| 302 | struct inode *inode = mapping->host; | ||
| 303 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 304 | struct fuse_readpages_data data; | ||
| 305 | int err; | ||
| 306 | data.file = file; | ||
| 307 | data.inode = inode; | ||
| 308 | data.req = fuse_get_request(fc); | ||
| 309 | if (!data.req) | ||
| 310 | return -EINTR; | ||
| 311 | |||
| 312 | err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data); | ||
| 313 | if (!err && data.req->num_pages) | ||
| 314 | err = fuse_send_readpages(data.req, file, inode); | ||
| 315 | fuse_put_request(fc, data.req); | ||
| 316 | fuse_invalidate_attr(inode); /* atime changed */ | ||
| 317 | return err; | ||
| 318 | } | ||
| 319 | |||
| 320 | static size_t fuse_send_write(struct fuse_req *req, struct file *file, | ||
| 321 | struct inode *inode, loff_t pos, size_t count) | ||
| 322 | { | ||
| 323 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 324 | struct fuse_file *ff = file->private_data; | ||
| 325 | struct fuse_write_in inarg; | ||
| 326 | struct fuse_write_out outarg; | ||
| 327 | |||
| 328 | memset(&inarg, 0, sizeof(struct fuse_write_in)); | ||
| 329 | inarg.fh = ff->fh; | ||
| 330 | inarg.offset = pos; | ||
| 331 | inarg.size = count; | ||
| 332 | req->in.h.opcode = FUSE_WRITE; | ||
| 333 | req->in.h.nodeid = get_node_id(inode); | ||
| 334 | req->inode = inode; | ||
| 335 | req->file = file; | ||
| 336 | req->in.argpages = 1; | ||
| 337 | req->in.numargs = 2; | ||
| 338 | req->in.args[0].size = sizeof(struct fuse_write_in); | ||
| 339 | req->in.args[0].value = &inarg; | ||
| 340 | req->in.args[1].size = count; | ||
| 341 | req->out.numargs = 1; | ||
| 342 | req->out.args[0].size = sizeof(struct fuse_write_out); | ||
| 343 | req->out.args[0].value = &outarg; | ||
| 344 | request_send(fc, req); | ||
| 345 | return outarg.size; | ||
| 346 | } | ||
| 347 | |||
| 348 | static int fuse_prepare_write(struct file *file, struct page *page, | ||
| 349 | unsigned offset, unsigned to) | ||
| 350 | { | ||
| 351 | /* No op */ | ||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | |||
| 355 | static int fuse_commit_write(struct file *file, struct page *page, | ||
| 356 | unsigned offset, unsigned to) | ||
| 357 | { | ||
| 358 | int err; | ||
| 359 | size_t nres; | ||
| 360 | unsigned count = to - offset; | ||
| 361 | struct inode *inode = page->mapping->host; | ||
| 362 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 363 | loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset; | ||
| 364 | struct fuse_req *req = fuse_get_request(fc); | ||
| 365 | if (!req) | ||
| 366 | return -EINTR; | ||
| 367 | |||
| 368 | req->num_pages = 1; | ||
| 369 | req->pages[0] = page; | ||
| 370 | req->page_offset = offset; | ||
| 371 | nres = fuse_send_write(req, file, inode, pos, count); | ||
| 372 | err = req->out.h.error; | ||
| 373 | fuse_put_request(fc, req); | ||
| 374 | if (!err && nres != count) | ||
| 375 | err = -EIO; | ||
| 376 | if (!err) { | ||
| 377 | pos += count; | ||
| 378 | if (pos > i_size_read(inode)) | ||
| 379 | i_size_write(inode, pos); | ||
| 380 | |||
| 381 | if (offset == 0 && to == PAGE_CACHE_SIZE) { | ||
| 382 | clear_page_dirty(page); | ||
| 383 | SetPageUptodate(page); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | fuse_invalidate_attr(inode); | ||
| 387 | return err; | ||
| 388 | } | ||
| 389 | |||
| 390 | static void fuse_release_user_pages(struct fuse_req *req, int write) | ||
| 391 | { | ||
| 392 | unsigned i; | ||
| 393 | |||
| 394 | for (i = 0; i < req->num_pages; i++) { | ||
| 395 | struct page *page = req->pages[i]; | ||
| 396 | if (write) | ||
| 397 | set_page_dirty_lock(page); | ||
| 398 | put_page(page); | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf, | ||
| 403 | unsigned nbytes, int write) | ||
| 404 | { | ||
| 405 | unsigned long user_addr = (unsigned long) buf; | ||
| 406 | unsigned offset = user_addr & ~PAGE_MASK; | ||
| 407 | int npages; | ||
| 408 | |||
| 409 | /* This doesn't work with nfsd */ | ||
| 410 | if (!current->mm) | ||
| 411 | return -EPERM; | ||
| 412 | |||
| 413 | nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT); | ||
| 414 | npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT; | ||
| 415 | npages = min(npages, FUSE_MAX_PAGES_PER_REQ); | ||
| 416 | down_read(¤t->mm->mmap_sem); | ||
| 417 | npages = get_user_pages(current, current->mm, user_addr, npages, write, | ||
| 418 | 0, req->pages, NULL); | ||
| 419 | up_read(¤t->mm->mmap_sem); | ||
| 420 | if (npages < 0) | ||
| 421 | return npages; | ||
| 422 | |||
| 423 | req->num_pages = npages; | ||
| 424 | req->page_offset = offset; | ||
| 425 | return 0; | ||
| 426 | } | ||
| 427 | |||
| 428 | static ssize_t fuse_direct_io(struct file *file, const char __user *buf, | ||
| 429 | size_t count, loff_t *ppos, int write) | ||
| 430 | { | ||
| 431 | struct inode *inode = file->f_dentry->d_inode; | ||
| 432 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 433 | size_t nmax = write ? fc->max_write : fc->max_read; | ||
| 434 | loff_t pos = *ppos; | ||
| 435 | ssize_t res = 0; | ||
| 436 | struct fuse_req *req = fuse_get_request(fc); | ||
| 437 | if (!req) | ||
| 438 | return -EINTR; | ||
| 439 | |||
| 440 | while (count) { | ||
| 441 | size_t tmp; | ||
| 442 | size_t nres; | ||
| 443 | size_t nbytes = min(count, nmax); | ||
| 444 | int err = fuse_get_user_pages(req, buf, nbytes, !write); | ||
| 445 | if (err) { | ||
| 446 | res = err; | ||
| 447 | break; | ||
| 448 | } | ||
| 449 | tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset; | ||
| 450 | nbytes = min(nbytes, tmp); | ||
| 451 | if (write) | ||
| 452 | nres = fuse_send_write(req, file, inode, pos, nbytes); | ||
| 453 | else | ||
| 454 | nres = fuse_send_read(req, file, inode, pos, nbytes); | ||
| 455 | fuse_release_user_pages(req, !write); | ||
| 456 | if (req->out.h.error) { | ||
| 457 | if (!res) | ||
| 458 | res = req->out.h.error; | ||
| 459 | break; | ||
| 460 | } else if (nres > nbytes) { | ||
| 461 | res = -EIO; | ||
| 462 | break; | ||
| 463 | } | ||
| 464 | count -= nres; | ||
| 465 | res += nres; | ||
| 466 | pos += nres; | ||
| 467 | buf += nres; | ||
| 468 | if (nres != nbytes) | ||
| 469 | break; | ||
| 470 | if (count) | ||
| 471 | fuse_reset_request(req); | ||
| 472 | } | ||
| 473 | fuse_put_request(fc, req); | ||
| 474 | if (res > 0) { | ||
| 475 | if (write && pos > i_size_read(inode)) | ||
| 476 | i_size_write(inode, pos); | ||
| 477 | *ppos = pos; | ||
| 478 | } | ||
| 479 | fuse_invalidate_attr(inode); | ||
| 480 | |||
| 481 | return res; | ||
| 482 | } | ||
| 483 | |||
| 484 | static ssize_t fuse_direct_read(struct file *file, char __user *buf, | ||
| 485 | size_t count, loff_t *ppos) | ||
| 486 | { | ||
| 487 | return fuse_direct_io(file, buf, count, ppos, 0); | ||
| 488 | } | ||
| 489 | |||
| 490 | static ssize_t fuse_direct_write(struct file *file, const char __user *buf, | ||
| 491 | size_t count, loff_t *ppos) | ||
| 492 | { | ||
| 493 | struct inode *inode = file->f_dentry->d_inode; | ||
| 494 | ssize_t res; | ||
| 495 | /* Don't allow parallel writes to the same file */ | ||
| 496 | down(&inode->i_sem); | ||
| 497 | res = fuse_direct_io(file, buf, count, ppos, 1); | ||
| 498 | up(&inode->i_sem); | ||
| 499 | return res; | ||
| 500 | } | ||
| 501 | |||
| 502 | static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma) | ||
| 503 | { | ||
| 504 | if ((vma->vm_flags & VM_SHARED)) { | ||
| 505 | if ((vma->vm_flags & VM_WRITE)) | ||
| 506 | return -ENODEV; | ||
| 507 | else | ||
| 508 | vma->vm_flags &= ~VM_MAYWRITE; | ||
| 509 | } | ||
| 510 | return generic_file_mmap(file, vma); | ||
| 511 | } | ||
| 512 | |||
| 513 | static int fuse_set_page_dirty(struct page *page) | ||
| 514 | { | ||
| 515 | printk("fuse_set_page_dirty: should not happen\n"); | ||
| 516 | dump_stack(); | ||
| 517 | return 0; | ||
| 518 | } | ||
| 519 | |||
| 520 | static struct file_operations fuse_file_operations = { | ||
| 521 | .llseek = generic_file_llseek, | ||
| 522 | .read = generic_file_read, | ||
| 523 | .write = generic_file_write, | ||
| 524 | .mmap = fuse_file_mmap, | ||
| 525 | .open = fuse_open, | ||
| 526 | .flush = fuse_flush, | ||
| 527 | .release = fuse_release, | ||
| 528 | .fsync = fuse_fsync, | ||
| 529 | .sendfile = generic_file_sendfile, | ||
| 530 | }; | ||
| 531 | |||
| 532 | static struct file_operations fuse_direct_io_file_operations = { | ||
| 533 | .llseek = generic_file_llseek, | ||
| 534 | .read = fuse_direct_read, | ||
| 535 | .write = fuse_direct_write, | ||
| 536 | .open = fuse_open, | ||
| 537 | .flush = fuse_flush, | ||
| 538 | .release = fuse_release, | ||
| 539 | .fsync = fuse_fsync, | ||
| 540 | /* no mmap and sendfile */ | ||
| 541 | }; | ||
| 542 | |||
| 543 | static struct address_space_operations fuse_file_aops = { | ||
| 544 | .readpage = fuse_readpage, | ||
| 545 | .prepare_write = fuse_prepare_write, | ||
| 546 | .commit_write = fuse_commit_write, | ||
| 547 | .readpages = fuse_readpages, | ||
| 548 | .set_page_dirty = fuse_set_page_dirty, | ||
| 549 | }; | ||
| 550 | |||
| 551 | void fuse_init_file_inode(struct inode *inode) | ||
| 552 | { | ||
| 553 | inode->i_fop = &fuse_file_operations; | ||
| 554 | inode->i_data.a_ops = &fuse_file_aops; | ||
| 555 | } | ||
diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h new file mode 100644 index 000000000000..24d761518d86 --- /dev/null +++ b/fs/fuse/fuse_i.h | |||
| @@ -0,0 +1,451 @@ | |||
| 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 | /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem | ||
| 25 | module will check permissions based on the file mode. Otherwise no | ||
| 26 | permission checking is done in the kernel */ | ||
| 27 | #define FUSE_DEFAULT_PERMISSIONS (1 << 0) | ||
| 28 | |||
| 29 | /** If the FUSE_ALLOW_OTHER flag is given, then not only the user | ||
| 30 | doing the mount will be allowed to access the filesystem */ | ||
| 31 | #define FUSE_ALLOW_OTHER (1 << 1) | ||
| 32 | |||
| 33 | |||
| 34 | /** FUSE inode */ | ||
| 35 | struct fuse_inode { | ||
| 36 | /** Inode data */ | ||
| 37 | struct inode inode; | ||
| 38 | |||
| 39 | /** Unique ID, which identifies the inode between userspace | ||
| 40 | * and kernel */ | ||
| 41 | u64 nodeid; | ||
| 42 | |||
| 43 | /** Number of lookups on this inode */ | ||
| 44 | u64 nlookup; | ||
| 45 | |||
| 46 | /** The request used for sending the FORGET message */ | ||
| 47 | struct fuse_req *forget_req; | ||
| 48 | |||
| 49 | /** Time in jiffies until the file attributes are valid */ | ||
| 50 | unsigned long i_time; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /** FUSE specific file data */ | ||
| 54 | struct fuse_file { | ||
| 55 | /** Request reserved for flush and release */ | ||
| 56 | struct fuse_req *release_req; | ||
| 57 | |||
| 58 | /** File handle used by userspace */ | ||
| 59 | u64 fh; | ||
| 60 | }; | ||
| 61 | |||
| 62 | /** One input argument of a request */ | ||
| 63 | struct fuse_in_arg { | ||
| 64 | unsigned size; | ||
| 65 | const void *value; | ||
| 66 | }; | ||
| 67 | |||
| 68 | /** The request input */ | ||
| 69 | struct fuse_in { | ||
| 70 | /** The request header */ | ||
| 71 | struct fuse_in_header h; | ||
| 72 | |||
| 73 | /** True if the data for the last argument is in req->pages */ | ||
| 74 | unsigned argpages:1; | ||
| 75 | |||
| 76 | /** Number of arguments */ | ||
| 77 | unsigned numargs; | ||
| 78 | |||
| 79 | /** Array of arguments */ | ||
| 80 | struct fuse_in_arg args[3]; | ||
| 81 | }; | ||
| 82 | |||
| 83 | /** One output argument of a request */ | ||
| 84 | struct fuse_arg { | ||
| 85 | unsigned size; | ||
| 86 | void *value; | ||
| 87 | }; | ||
| 88 | |||
| 89 | /** The request output */ | ||
| 90 | struct fuse_out { | ||
| 91 | /** Header returned from userspace */ | ||
| 92 | struct fuse_out_header h; | ||
| 93 | |||
| 94 | /** Last argument is variable length (can be shorter than | ||
| 95 | arg->size) */ | ||
| 96 | unsigned argvar:1; | ||
| 97 | |||
| 98 | /** Last argument is a list of pages to copy data to */ | ||
| 99 | unsigned argpages:1; | ||
| 100 | |||
| 101 | /** Zero partially or not copied pages */ | ||
| 102 | unsigned page_zeroing:1; | ||
| 103 | |||
| 104 | /** Number or arguments */ | ||
| 105 | unsigned numargs; | ||
| 106 | |||
| 107 | /** Array of arguments */ | ||
| 108 | struct fuse_arg args[3]; | ||
| 109 | }; | ||
| 110 | |||
| 111 | struct fuse_req; | ||
| 112 | struct fuse_conn; | ||
| 113 | |||
| 114 | /** | ||
| 115 | * A request to the client | ||
| 116 | */ | ||
| 117 | struct fuse_req { | ||
| 118 | /** This can be on either unused_list, pending or processing | ||
| 119 | lists in fuse_conn */ | ||
| 120 | struct list_head list; | ||
| 121 | |||
| 122 | /** Entry on the background list */ | ||
| 123 | struct list_head bg_entry; | ||
| 124 | |||
| 125 | /** refcount */ | ||
| 126 | atomic_t count; | ||
| 127 | |||
| 128 | /** True if the request has reply */ | ||
| 129 | unsigned isreply:1; | ||
| 130 | |||
| 131 | /** The request is preallocated */ | ||
| 132 | unsigned preallocated:1; | ||
| 133 | |||
| 134 | /** The request was interrupted */ | ||
| 135 | unsigned interrupted:1; | ||
| 136 | |||
| 137 | /** Request is sent in the background */ | ||
| 138 | unsigned background:1; | ||
| 139 | |||
| 140 | /** Data is being copied to/from the request */ | ||
| 141 | unsigned locked:1; | ||
| 142 | |||
| 143 | /** Request has been sent to userspace */ | ||
| 144 | unsigned sent:1; | ||
| 145 | |||
| 146 | /** The request is finished */ | ||
| 147 | unsigned finished:1; | ||
| 148 | |||
| 149 | /** The request input */ | ||
| 150 | struct fuse_in in; | ||
| 151 | |||
| 152 | /** The request output */ | ||
| 153 | struct fuse_out out; | ||
| 154 | |||
| 155 | /** Used to wake up the task waiting for completion of request*/ | ||
| 156 | wait_queue_head_t waitq; | ||
| 157 | |||
| 158 | /** Data for asynchronous requests */ | ||
| 159 | union { | ||
| 160 | struct fuse_forget_in forget_in; | ||
| 161 | struct fuse_release_in release_in; | ||
| 162 | struct fuse_init_in_out init_in_out; | ||
| 163 | } misc; | ||
| 164 | |||
| 165 | /** page vector */ | ||
| 166 | struct page *pages[FUSE_MAX_PAGES_PER_REQ]; | ||
| 167 | |||
| 168 | /** number of pages in vector */ | ||
| 169 | unsigned num_pages; | ||
| 170 | |||
| 171 | /** offset of data on first page */ | ||
| 172 | unsigned page_offset; | ||
| 173 | |||
| 174 | /** Inode used in the request */ | ||
| 175 | struct inode *inode; | ||
| 176 | |||
| 177 | /** Second inode used in the request (or NULL) */ | ||
| 178 | struct inode *inode2; | ||
| 179 | |||
| 180 | /** File used in the request (or NULL) */ | ||
| 181 | struct file *file; | ||
| 182 | }; | ||
| 183 | |||
| 184 | /** | ||
| 185 | * A Fuse connection. | ||
| 186 | * | ||
| 187 | * This structure is created, when the filesystem is mounted, and is | ||
| 188 | * destroyed, when the client device is closed and the filesystem is | ||
| 189 | * unmounted. | ||
| 190 | */ | ||
| 191 | struct fuse_conn { | ||
| 192 | /** Reference count */ | ||
| 193 | int count; | ||
| 194 | |||
| 195 | /** The user id for this mount */ | ||
| 196 | uid_t user_id; | ||
| 197 | |||
| 198 | /** The group id for this mount */ | ||
| 199 | gid_t group_id; | ||
| 200 | |||
| 201 | /** The fuse mount flags for this mount */ | ||
| 202 | unsigned flags; | ||
| 203 | |||
| 204 | /** Maximum read size */ | ||
| 205 | unsigned max_read; | ||
| 206 | |||
| 207 | /** Maximum write size */ | ||
| 208 | unsigned max_write; | ||
| 209 | |||
| 210 | /** Readers of the connection are waiting on this */ | ||
| 211 | wait_queue_head_t waitq; | ||
| 212 | |||
| 213 | /** The list of pending requests */ | ||
| 214 | struct list_head pending; | ||
| 215 | |||
| 216 | /** The list of requests being processed */ | ||
| 217 | struct list_head processing; | ||
| 218 | |||
| 219 | /** Requests put in the background (RELEASE or any other | ||
| 220 | interrupted request) */ | ||
| 221 | struct list_head background; | ||
| 222 | |||
| 223 | /** Controls the maximum number of outstanding requests */ | ||
| 224 | struct semaphore outstanding_sem; | ||
| 225 | |||
| 226 | /** This counts the number of outstanding requests if | ||
| 227 | outstanding_sem would go negative */ | ||
| 228 | unsigned outstanding_debt; | ||
| 229 | |||
| 230 | /** RW semaphore for exclusion with fuse_put_super() */ | ||
| 231 | struct rw_semaphore sbput_sem; | ||
| 232 | |||
| 233 | /** The list of unused requests */ | ||
| 234 | struct list_head unused_list; | ||
| 235 | |||
| 236 | /** The next unique request id */ | ||
| 237 | u64 reqctr; | ||
| 238 | |||
| 239 | /** Mount is active */ | ||
| 240 | unsigned mounted : 1; | ||
| 241 | |||
| 242 | /** Connection established */ | ||
| 243 | unsigned connected : 1; | ||
| 244 | |||
| 245 | /** Connection failed (version mismatch) */ | ||
| 246 | unsigned conn_error : 1; | ||
| 247 | |||
| 248 | /** Is fsync not implemented by fs? */ | ||
| 249 | unsigned no_fsync : 1; | ||
| 250 | |||
| 251 | /** Is fsyncdir not implemented by fs? */ | ||
| 252 | unsigned no_fsyncdir : 1; | ||
| 253 | |||
| 254 | /** Is flush not implemented by fs? */ | ||
| 255 | unsigned no_flush : 1; | ||
| 256 | |||
| 257 | /** Is setxattr not implemented by fs? */ | ||
| 258 | unsigned no_setxattr : 1; | ||
| 259 | |||
| 260 | /** Is getxattr not implemented by fs? */ | ||
| 261 | unsigned no_getxattr : 1; | ||
| 262 | |||
| 263 | /** Is listxattr not implemented by fs? */ | ||
| 264 | unsigned no_listxattr : 1; | ||
| 265 | |||
| 266 | /** Is removexattr not implemented by fs? */ | ||
| 267 | unsigned no_removexattr : 1; | ||
| 268 | |||
| 269 | /** Backing dev info */ | ||
| 270 | struct backing_dev_info bdi; | ||
| 271 | }; | ||
| 272 | |||
| 273 | static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb) | ||
| 274 | { | ||
| 275 | return (struct fuse_conn **) &sb->s_fs_info; | ||
| 276 | } | ||
| 277 | |||
| 278 | static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) | ||
| 279 | { | ||
| 280 | return *get_fuse_conn_super_p(sb); | ||
| 281 | } | ||
| 282 | |||
| 283 | static inline struct fuse_conn *get_fuse_conn(struct inode *inode) | ||
| 284 | { | ||
| 285 | return get_fuse_conn_super(inode->i_sb); | ||
| 286 | } | ||
| 287 | |||
| 288 | static inline struct fuse_inode *get_fuse_inode(struct inode *inode) | ||
| 289 | { | ||
| 290 | return container_of(inode, struct fuse_inode, inode); | ||
| 291 | } | ||
| 292 | |||
| 293 | static inline u64 get_node_id(struct inode *inode) | ||
| 294 | { | ||
| 295 | return get_fuse_inode(inode)->nodeid; | ||
| 296 | } | ||
| 297 | |||
| 298 | /** Device operations */ | ||
| 299 | extern struct file_operations fuse_dev_operations; | ||
| 300 | |||
| 301 | /** | ||
| 302 | * This is the single global spinlock which protects FUSE's structures | ||
| 303 | * | ||
| 304 | * The following data is protected by this lock: | ||
| 305 | * | ||
| 306 | * - the private_data field of the device file | ||
| 307 | * - the s_fs_info field of the super block | ||
| 308 | * - unused_list, pending, processing lists in fuse_conn | ||
| 309 | * - background list in fuse_conn | ||
| 310 | * - the unique request ID counter reqctr in fuse_conn | ||
| 311 | * - the sb (super_block) field in fuse_conn | ||
| 312 | * - the file (device file) field in fuse_conn | ||
| 313 | */ | ||
| 314 | extern spinlock_t fuse_lock; | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Get a filled in inode | ||
| 318 | */ | ||
| 319 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | ||
| 320 | int generation, struct fuse_attr *attr); | ||
| 321 | |||
| 322 | /** | ||
| 323 | * Send FORGET command | ||
| 324 | */ | ||
| 325 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, | ||
| 326 | unsigned long nodeid, u64 nlookup); | ||
| 327 | |||
| 328 | /** | ||
| 329 | * Send READ or READDIR request | ||
| 330 | */ | ||
| 331 | size_t fuse_send_read_common(struct fuse_req *req, struct file *file, | ||
| 332 | struct inode *inode, loff_t pos, size_t count, | ||
| 333 | int isdir); | ||
| 334 | |||
| 335 | /** | ||
| 336 | * Send OPEN or OPENDIR request | ||
| 337 | */ | ||
| 338 | int fuse_open_common(struct inode *inode, struct file *file, int isdir); | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Send RELEASE or RELEASEDIR request | ||
| 342 | */ | ||
| 343 | int fuse_release_common(struct inode *inode, struct file *file, int isdir); | ||
| 344 | |||
| 345 | /** | ||
| 346 | * Send FSYNC or FSYNCDIR request | ||
| 347 | */ | ||
| 348 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | ||
| 349 | int isdir); | ||
| 350 | |||
| 351 | /** | ||
| 352 | * Initialise file operations on a regular file | ||
| 353 | */ | ||
| 354 | void fuse_init_file_inode(struct inode *inode); | ||
| 355 | |||
| 356 | /** | ||
| 357 | * Initialise inode operations on regular files and special files | ||
| 358 | */ | ||
| 359 | void fuse_init_common(struct inode *inode); | ||
| 360 | |||
| 361 | /** | ||
| 362 | * Initialise inode and file operations on a directory | ||
| 363 | */ | ||
| 364 | void fuse_init_dir(struct inode *inode); | ||
| 365 | |||
| 366 | /** | ||
| 367 | * Initialise inode operations on a symlink | ||
| 368 | */ | ||
| 369 | void fuse_init_symlink(struct inode *inode); | ||
| 370 | |||
| 371 | /** | ||
| 372 | * Change attributes of an inode | ||
| 373 | */ | ||
| 374 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr); | ||
| 375 | |||
| 376 | /** | ||
| 377 | * Check if the connection can be released, and if yes, then free the | ||
| 378 | * connection structure | ||
| 379 | */ | ||
| 380 | void fuse_release_conn(struct fuse_conn *fc); | ||
| 381 | |||
| 382 | /** | ||
| 383 | * Initialize the client device | ||
| 384 | */ | ||
| 385 | int fuse_dev_init(void); | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Cleanup the client device | ||
| 389 | */ | ||
| 390 | void fuse_dev_cleanup(void); | ||
| 391 | |||
| 392 | /** | ||
| 393 | * Allocate a request | ||
| 394 | */ | ||
| 395 | struct fuse_req *fuse_request_alloc(void); | ||
| 396 | |||
| 397 | /** | ||
| 398 | * Free a request | ||
| 399 | */ | ||
| 400 | void fuse_request_free(struct fuse_req *req); | ||
| 401 | |||
| 402 | /** | ||
| 403 | * Reinitialize a request, the preallocated flag is left unmodified | ||
| 404 | */ | ||
| 405 | void fuse_reset_request(struct fuse_req *req); | ||
| 406 | |||
| 407 | /** | ||
| 408 | * Reserve a preallocated request | ||
| 409 | */ | ||
| 410 | struct fuse_req *fuse_get_request(struct fuse_conn *fc); | ||
| 411 | |||
| 412 | /** | ||
| 413 | * Decrement reference count of a request. If count goes to zero put | ||
| 414 | * on unused list (preallocated) or free reqest (not preallocated). | ||
| 415 | */ | ||
| 416 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Send a request (synchronous) | ||
| 420 | */ | ||
| 421 | void request_send(struct fuse_conn *fc, struct fuse_req *req); | ||
| 422 | |||
| 423 | /** | ||
| 424 | * Send a request with no reply | ||
| 425 | */ | ||
| 426 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req); | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Send a request in the background | ||
| 430 | */ | ||
| 431 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req); | ||
| 432 | |||
| 433 | /** | ||
| 434 | * Release inodes and file assiciated with background request | ||
| 435 | */ | ||
| 436 | void fuse_release_background(struct fuse_req *req); | ||
| 437 | |||
| 438 | /** | ||
| 439 | * Get the attributes of a file | ||
| 440 | */ | ||
| 441 | int fuse_do_getattr(struct inode *inode); | ||
| 442 | |||
| 443 | /** | ||
| 444 | * Invalidate inode attributes | ||
| 445 | */ | ||
| 446 | void fuse_invalidate_attr(struct inode *inode); | ||
| 447 | |||
| 448 | /** | ||
| 449 | * Send the INIT message | ||
| 450 | */ | ||
| 451 | void fuse_send_init(struct fuse_conn *fc); | ||
diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c new file mode 100644 index 000000000000..e69a546844d0 --- /dev/null +++ b/fs/fuse/inode.c | |||
| @@ -0,0 +1,591 @@ | |||
| 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 "fuse_i.h" | ||
| 10 | |||
| 11 | #include <linux/pagemap.h> | ||
| 12 | #include <linux/slab.h> | ||
| 13 | #include <linux/file.h> | ||
| 14 | #include <linux/mount.h> | ||
| 15 | #include <linux/seq_file.h> | ||
| 16 | #include <linux/init.h> | ||
| 17 | #include <linux/module.h> | ||
| 18 | #include <linux/parser.h> | ||
| 19 | #include <linux/statfs.h> | ||
| 20 | |||
| 21 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | ||
| 22 | MODULE_DESCRIPTION("Filesystem in Userspace"); | ||
| 23 | MODULE_LICENSE("GPL"); | ||
| 24 | |||
| 25 | spinlock_t fuse_lock; | ||
| 26 | static kmem_cache_t *fuse_inode_cachep; | ||
| 27 | |||
| 28 | #define FUSE_SUPER_MAGIC 0x65735546 | ||
| 29 | |||
| 30 | struct fuse_mount_data { | ||
| 31 | int fd; | ||
| 32 | unsigned rootmode; | ||
| 33 | unsigned user_id; | ||
| 34 | unsigned group_id; | ||
| 35 | unsigned fd_present : 1; | ||
| 36 | unsigned rootmode_present : 1; | ||
| 37 | unsigned user_id_present : 1; | ||
| 38 | unsigned group_id_present : 1; | ||
| 39 | unsigned flags; | ||
| 40 | unsigned max_read; | ||
| 41 | }; | ||
| 42 | |||
| 43 | static struct inode *fuse_alloc_inode(struct super_block *sb) | ||
| 44 | { | ||
| 45 | struct inode *inode; | ||
| 46 | struct fuse_inode *fi; | ||
| 47 | |||
| 48 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); | ||
| 49 | if (!inode) | ||
| 50 | return NULL; | ||
| 51 | |||
| 52 | fi = get_fuse_inode(inode); | ||
| 53 | fi->i_time = jiffies - 1; | ||
| 54 | fi->nodeid = 0; | ||
| 55 | fi->nlookup = 0; | ||
| 56 | fi->forget_req = fuse_request_alloc(); | ||
| 57 | if (!fi->forget_req) { | ||
| 58 | kmem_cache_free(fuse_inode_cachep, inode); | ||
| 59 | return NULL; | ||
| 60 | } | ||
| 61 | |||
| 62 | return inode; | ||
| 63 | } | ||
| 64 | |||
| 65 | static void fuse_destroy_inode(struct inode *inode) | ||
| 66 | { | ||
| 67 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 68 | if (fi->forget_req) | ||
| 69 | fuse_request_free(fi->forget_req); | ||
| 70 | kmem_cache_free(fuse_inode_cachep, inode); | ||
| 71 | } | ||
| 72 | |||
| 73 | static void fuse_read_inode(struct inode *inode) | ||
| 74 | { | ||
| 75 | /* No op */ | ||
| 76 | } | ||
| 77 | |||
| 78 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, | ||
| 79 | unsigned long nodeid, u64 nlookup) | ||
| 80 | { | ||
| 81 | struct fuse_forget_in *inarg = &req->misc.forget_in; | ||
| 82 | inarg->nlookup = nlookup; | ||
| 83 | req->in.h.opcode = FUSE_FORGET; | ||
| 84 | req->in.h.nodeid = nodeid; | ||
| 85 | req->in.numargs = 1; | ||
| 86 | req->in.args[0].size = sizeof(struct fuse_forget_in); | ||
| 87 | req->in.args[0].value = inarg; | ||
| 88 | request_send_noreply(fc, req); | ||
| 89 | } | ||
| 90 | |||
| 91 | static void fuse_clear_inode(struct inode *inode) | ||
| 92 | { | ||
| 93 | if (inode->i_sb->s_flags & MS_ACTIVE) { | ||
| 94 | struct fuse_conn *fc = get_fuse_conn(inode); | ||
| 95 | struct fuse_inode *fi = get_fuse_inode(inode); | ||
| 96 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); | ||
| 97 | fi->forget_req = NULL; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) | ||
| 102 | { | ||
| 103 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) | ||
| 104 | invalidate_inode_pages(inode->i_mapping); | ||
| 105 | |||
| 106 | inode->i_ino = attr->ino; | ||
| 107 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); | ||
| 108 | inode->i_nlink = attr->nlink; | ||
| 109 | inode->i_uid = attr->uid; | ||
| 110 | inode->i_gid = attr->gid; | ||
| 111 | i_size_write(inode, attr->size); | ||
| 112 | inode->i_blksize = PAGE_CACHE_SIZE; | ||
| 113 | inode->i_blocks = attr->blocks; | ||
| 114 | inode->i_atime.tv_sec = attr->atime; | ||
| 115 | inode->i_atime.tv_nsec = attr->atimensec; | ||
| 116 | inode->i_mtime.tv_sec = attr->mtime; | ||
| 117 | inode->i_mtime.tv_nsec = attr->mtimensec; | ||
| 118 | inode->i_ctime.tv_sec = attr->ctime; | ||
| 119 | inode->i_ctime.tv_nsec = attr->ctimensec; | ||
| 120 | } | ||
| 121 | |||
| 122 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) | ||
| 123 | { | ||
| 124 | inode->i_mode = attr->mode & S_IFMT; | ||
| 125 | i_size_write(inode, attr->size); | ||
| 126 | if (S_ISREG(inode->i_mode)) { | ||
| 127 | fuse_init_common(inode); | ||
| 128 | fuse_init_file_inode(inode); | ||
| 129 | } else if (S_ISDIR(inode->i_mode)) | ||
| 130 | fuse_init_dir(inode); | ||
| 131 | else if (S_ISLNK(inode->i_mode)) | ||
| 132 | fuse_init_symlink(inode); | ||
| 133 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | ||
| 134 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | ||
| 135 | fuse_init_common(inode); | ||
| 136 | init_special_inode(inode, inode->i_mode, | ||
| 137 | new_decode_dev(attr->rdev)); | ||
| 138 | } else { | ||
| 139 | /* Don't let user create weird files */ | ||
| 140 | inode->i_mode = S_IFREG; | ||
| 141 | fuse_init_common(inode); | ||
| 142 | fuse_init_file_inode(inode); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) | ||
| 147 | { | ||
| 148 | unsigned long nodeid = *(unsigned long *) _nodeidp; | ||
| 149 | if (get_node_id(inode) == nodeid) | ||
| 150 | return 1; | ||
| 151 | else | ||
| 152 | return 0; | ||
| 153 | } | ||
| 154 | |||
| 155 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) | ||
| 156 | { | ||
| 157 | unsigned long nodeid = *(unsigned long *) _nodeidp; | ||
| 158 | get_fuse_inode(inode)->nodeid = nodeid; | ||
| 159 | return 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | ||
| 163 | int generation, struct fuse_attr *attr) | ||
| 164 | { | ||
| 165 | struct inode *inode; | ||
| 166 | struct fuse_inode *fi; | ||
| 167 | struct fuse_conn *fc = get_fuse_conn_super(sb); | ||
| 168 | int retried = 0; | ||
| 169 | |||
| 170 | retry: | ||
| 171 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); | ||
| 172 | if (!inode) | ||
| 173 | return NULL; | ||
| 174 | |||
| 175 | if ((inode->i_state & I_NEW)) { | ||
| 176 | inode->i_flags |= S_NOATIME|S_NOCMTIME; | ||
| 177 | inode->i_generation = generation; | ||
| 178 | inode->i_data.backing_dev_info = &fc->bdi; | ||
| 179 | fuse_init_inode(inode, attr); | ||
| 180 | unlock_new_inode(inode); | ||
| 181 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { | ||
| 182 | BUG_ON(retried); | ||
| 183 | /* Inode has changed type, any I/O on the old should fail */ | ||
| 184 | make_bad_inode(inode); | ||
| 185 | iput(inode); | ||
| 186 | retried = 1; | ||
| 187 | goto retry; | ||
| 188 | } | ||
| 189 | |||
| 190 | fi = get_fuse_inode(inode); | ||
| 191 | fi->nlookup ++; | ||
| 192 | fuse_change_attributes(inode, attr); | ||
| 193 | return inode; | ||
| 194 | } | ||
| 195 | |||
| 196 | static void fuse_put_super(struct super_block *sb) | ||
| 197 | { | ||
| 198 | struct fuse_conn *fc = get_fuse_conn_super(sb); | ||
| 199 | |||
| 200 | down_write(&fc->sbput_sem); | ||
| 201 | while (!list_empty(&fc->background)) | ||
| 202 | fuse_release_background(list_entry(fc->background.next, | ||
| 203 | struct fuse_req, bg_entry)); | ||
| 204 | |||
| 205 | spin_lock(&fuse_lock); | ||
| 206 | fc->mounted = 0; | ||
| 207 | fc->user_id = 0; | ||
| 208 | fc->group_id = 0; | ||
| 209 | fc->flags = 0; | ||
| 210 | /* Flush all readers on this fs */ | ||
| 211 | wake_up_all(&fc->waitq); | ||
| 212 | up_write(&fc->sbput_sem); | ||
| 213 | fuse_release_conn(fc); | ||
| 214 | spin_unlock(&fuse_lock); | ||
| 215 | } | ||
| 216 | |||
| 217 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) | ||
| 218 | { | ||
| 219 | stbuf->f_type = FUSE_SUPER_MAGIC; | ||
| 220 | stbuf->f_bsize = attr->bsize; | ||
| 221 | stbuf->f_blocks = attr->blocks; | ||
| 222 | stbuf->f_bfree = attr->bfree; | ||
| 223 | stbuf->f_bavail = attr->bavail; | ||
| 224 | stbuf->f_files = attr->files; | ||
| 225 | stbuf->f_ffree = attr->ffree; | ||
| 226 | stbuf->f_namelen = attr->namelen; | ||
| 227 | /* fsid is left zero */ | ||
| 228 | } | ||
| 229 | |||
| 230 | static int fuse_statfs(struct super_block *sb, struct kstatfs *buf) | ||
| 231 | { | ||
| 232 | struct fuse_conn *fc = get_fuse_conn_super(sb); | ||
| 233 | struct fuse_req *req; | ||
| 234 | struct fuse_statfs_out outarg; | ||
| 235 | int err; | ||
| 236 | |||
| 237 | req = fuse_get_request(fc); | ||
| 238 | if (!req) | ||
| 239 | return -EINTR; | ||
| 240 | |||
| 241 | req->in.numargs = 0; | ||
| 242 | req->in.h.opcode = FUSE_STATFS; | ||
| 243 | req->out.numargs = 1; | ||
| 244 | req->out.args[0].size = sizeof(outarg); | ||
| 245 | req->out.args[0].value = &outarg; | ||
| 246 | request_send(fc, req); | ||
| 247 | err = req->out.h.error; | ||
| 248 | if (!err) | ||
| 249 | convert_fuse_statfs(buf, &outarg.st); | ||
| 250 | fuse_put_request(fc, req); | ||
| 251 | return err; | ||
| 252 | } | ||
| 253 | |||
| 254 | enum { | ||
| 255 | OPT_FD, | ||
| 256 | OPT_ROOTMODE, | ||
| 257 | OPT_USER_ID, | ||
| 258 | OPT_GROUP_ID, | ||
| 259 | OPT_DEFAULT_PERMISSIONS, | ||
| 260 | OPT_ALLOW_OTHER, | ||
| 261 | OPT_MAX_READ, | ||
| 262 | OPT_ERR | ||
| 263 | }; | ||
| 264 | |||
| 265 | static match_table_t tokens = { | ||
| 266 | {OPT_FD, "fd=%u"}, | ||
| 267 | {OPT_ROOTMODE, "rootmode=%o"}, | ||
| 268 | {OPT_USER_ID, "user_id=%u"}, | ||
| 269 | {OPT_GROUP_ID, "group_id=%u"}, | ||
| 270 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, | ||
| 271 | {OPT_ALLOW_OTHER, "allow_other"}, | ||
| 272 | {OPT_MAX_READ, "max_read=%u"}, | ||
| 273 | {OPT_ERR, NULL} | ||
| 274 | }; | ||
| 275 | |||
| 276 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) | ||
| 277 | { | ||
| 278 | char *p; | ||
| 279 | memset(d, 0, sizeof(struct fuse_mount_data)); | ||
| 280 | d->max_read = ~0; | ||
| 281 | |||
| 282 | while ((p = strsep(&opt, ",")) != NULL) { | ||
| 283 | int token; | ||
| 284 | int value; | ||
| 285 | substring_t args[MAX_OPT_ARGS]; | ||
| 286 | if (!*p) | ||
| 287 | continue; | ||
| 288 | |||
| 289 | token = match_token(p, tokens, args); | ||
| 290 | switch (token) { | ||
| 291 | case OPT_FD: | ||
| 292 | if (match_int(&args[0], &value)) | ||
| 293 | return 0; | ||
| 294 | d->fd = value; | ||
| 295 | d->fd_present = 1; | ||
| 296 | break; | ||
| 297 | |||
| 298 | case OPT_ROOTMODE: | ||
| 299 | if (match_octal(&args[0], &value)) | ||
| 300 | return 0; | ||
| 301 | d->rootmode = value; | ||
| 302 | d->rootmode_present = 1; | ||
| 303 | break; | ||
| 304 | |||
| 305 | case OPT_USER_ID: | ||
| 306 | if (match_int(&args[0], &value)) | ||
| 307 | return 0; | ||
| 308 | d->user_id = value; | ||
| 309 | d->user_id_present = 1; | ||
| 310 | break; | ||
| 311 | |||
| 312 | case OPT_GROUP_ID: | ||
| 313 | if (match_int(&args[0], &value)) | ||
| 314 | return 0; | ||
| 315 | d->group_id = value; | ||
| 316 | d->group_id_present = 1; | ||
| 317 | break; | ||
| 318 | |||
| 319 | case OPT_DEFAULT_PERMISSIONS: | ||
| 320 | d->flags |= FUSE_DEFAULT_PERMISSIONS; | ||
| 321 | break; | ||
| 322 | |||
| 323 | case OPT_ALLOW_OTHER: | ||
| 324 | d->flags |= FUSE_ALLOW_OTHER; | ||
| 325 | break; | ||
| 326 | |||
| 327 | case OPT_MAX_READ: | ||
| 328 | if (match_int(&args[0], &value)) | ||
| 329 | return 0; | ||
| 330 | d->max_read = value; | ||
| 331 | break; | ||
| 332 | |||
| 333 | default: | ||
| 334 | return 0; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | if (!d->fd_present || !d->rootmode_present || | ||
| 339 | !d->user_id_present || !d->group_id_present) | ||
| 340 | return 0; | ||
| 341 | |||
| 342 | return 1; | ||
| 343 | } | ||
| 344 | |||
| 345 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) | ||
| 346 | { | ||
| 347 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); | ||
| 348 | |||
| 349 | seq_printf(m, ",user_id=%u", fc->user_id); | ||
| 350 | seq_printf(m, ",group_id=%u", fc->group_id); | ||
| 351 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) | ||
| 352 | seq_puts(m, ",default_permissions"); | ||
| 353 | if (fc->flags & FUSE_ALLOW_OTHER) | ||
| 354 | seq_puts(m, ",allow_other"); | ||
| 355 | if (fc->max_read != ~0) | ||
| 356 | seq_printf(m, ",max_read=%u", fc->max_read); | ||
| 357 | return 0; | ||
| 358 | } | ||
| 359 | |||
| 360 | static void free_conn(struct fuse_conn *fc) | ||
| 361 | { | ||
| 362 | while (!list_empty(&fc->unused_list)) { | ||
| 363 | struct fuse_req *req; | ||
| 364 | req = list_entry(fc->unused_list.next, struct fuse_req, list); | ||
| 365 | list_del(&req->list); | ||
| 366 | fuse_request_free(req); | ||
| 367 | } | ||
| 368 | kfree(fc); | ||
| 369 | } | ||
| 370 | |||
| 371 | /* Must be called with the fuse lock held */ | ||
| 372 | void fuse_release_conn(struct fuse_conn *fc) | ||
| 373 | { | ||
| 374 | fc->count--; | ||
| 375 | if (!fc->count) | ||
| 376 | free_conn(fc); | ||
| 377 | } | ||
| 378 | |||
| 379 | static struct fuse_conn *new_conn(void) | ||
| 380 | { | ||
| 381 | struct fuse_conn *fc; | ||
| 382 | |||
| 383 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); | ||
| 384 | if (fc != NULL) { | ||
| 385 | int i; | ||
| 386 | memset(fc, 0, sizeof(*fc)); | ||
| 387 | init_waitqueue_head(&fc->waitq); | ||
| 388 | INIT_LIST_HEAD(&fc->pending); | ||
| 389 | INIT_LIST_HEAD(&fc->processing); | ||
| 390 | INIT_LIST_HEAD(&fc->unused_list); | ||
| 391 | INIT_LIST_HEAD(&fc->background); | ||
| 392 | sema_init(&fc->outstanding_sem, 0); | ||
| 393 | init_rwsem(&fc->sbput_sem); | ||
| 394 | for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) { | ||
| 395 | struct fuse_req *req = fuse_request_alloc(); | ||
| 396 | if (!req) { | ||
| 397 | free_conn(fc); | ||
| 398 | return NULL; | ||
| 399 | } | ||
| 400 | list_add(&req->list, &fc->unused_list); | ||
| 401 | } | ||
| 402 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; | ||
| 403 | fc->bdi.unplug_io_fn = default_unplug_io_fn; | ||
| 404 | fc->reqctr = 0; | ||
| 405 | } | ||
| 406 | return fc; | ||
| 407 | } | ||
| 408 | |||
| 409 | static struct fuse_conn *get_conn(struct file *file, struct super_block *sb) | ||
| 410 | { | ||
| 411 | struct fuse_conn *fc; | ||
| 412 | |||
| 413 | if (file->f_op != &fuse_dev_operations) | ||
| 414 | return ERR_PTR(-EINVAL); | ||
| 415 | fc = new_conn(); | ||
| 416 | if (fc == NULL) | ||
| 417 | return ERR_PTR(-ENOMEM); | ||
| 418 | spin_lock(&fuse_lock); | ||
| 419 | if (file->private_data) { | ||
| 420 | free_conn(fc); | ||
| 421 | fc = ERR_PTR(-EINVAL); | ||
| 422 | } else { | ||
| 423 | file->private_data = fc; | ||
| 424 | *get_fuse_conn_super_p(sb) = fc; | ||
| 425 | fc->mounted = 1; | ||
| 426 | fc->connected = 1; | ||
| 427 | fc->count = 2; | ||
| 428 | } | ||
| 429 | spin_unlock(&fuse_lock); | ||
| 430 | return fc; | ||
| 431 | } | ||
| 432 | |||
| 433 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) | ||
| 434 | { | ||
| 435 | struct fuse_attr attr; | ||
| 436 | memset(&attr, 0, sizeof(attr)); | ||
| 437 | |||
| 438 | attr.mode = mode; | ||
| 439 | attr.ino = FUSE_ROOT_ID; | ||
| 440 | return fuse_iget(sb, 1, 0, &attr); | ||
| 441 | } | ||
| 442 | |||
| 443 | static struct super_operations fuse_super_operations = { | ||
| 444 | .alloc_inode = fuse_alloc_inode, | ||
| 445 | .destroy_inode = fuse_destroy_inode, | ||
| 446 | .read_inode = fuse_read_inode, | ||
| 447 | .clear_inode = fuse_clear_inode, | ||
| 448 | .put_super = fuse_put_super, | ||
| 449 | .statfs = fuse_statfs, | ||
| 450 | .show_options = fuse_show_options, | ||
| 451 | }; | ||
| 452 | |||
| 453 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) | ||
| 454 | { | ||
| 455 | struct fuse_conn *fc; | ||
| 456 | struct inode *root; | ||
| 457 | struct fuse_mount_data d; | ||
| 458 | struct file *file; | ||
| 459 | int err; | ||
| 460 | |||
| 461 | if (!parse_fuse_opt((char *) data, &d)) | ||
| 462 | return -EINVAL; | ||
| 463 | |||
| 464 | sb->s_blocksize = PAGE_CACHE_SIZE; | ||
| 465 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | ||
| 466 | sb->s_magic = FUSE_SUPER_MAGIC; | ||
| 467 | sb->s_op = &fuse_super_operations; | ||
| 468 | sb->s_maxbytes = MAX_LFS_FILESIZE; | ||
| 469 | |||
| 470 | file = fget(d.fd); | ||
| 471 | if (!file) | ||
| 472 | return -EINVAL; | ||
| 473 | |||
| 474 | fc = get_conn(file, sb); | ||
| 475 | fput(file); | ||
| 476 | if (IS_ERR(fc)) | ||
| 477 | return PTR_ERR(fc); | ||
| 478 | |||
| 479 | fc->flags = d.flags; | ||
| 480 | fc->user_id = d.user_id; | ||
| 481 | fc->group_id = d.group_id; | ||
| 482 | fc->max_read = d.max_read; | ||
| 483 | if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages) | ||
| 484 | fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE; | ||
| 485 | fc->max_write = FUSE_MAX_IN / 2; | ||
| 486 | |||
| 487 | err = -ENOMEM; | ||
| 488 | root = get_root_inode(sb, d.rootmode); | ||
| 489 | if (root == NULL) | ||
| 490 | goto err; | ||
| 491 | |||
| 492 | sb->s_root = d_alloc_root(root); | ||
| 493 | if (!sb->s_root) { | ||
| 494 | iput(root); | ||
| 495 | goto err; | ||
| 496 | } | ||
| 497 | fuse_send_init(fc); | ||
| 498 | return 0; | ||
| 499 | |||
| 500 | err: | ||
| 501 | spin_lock(&fuse_lock); | ||
| 502 | fuse_release_conn(fc); | ||
| 503 | spin_unlock(&fuse_lock); | ||
| 504 | return err; | ||
| 505 | } | ||
| 506 | |||
| 507 | static struct super_block *fuse_get_sb(struct file_system_type *fs_type, | ||
| 508 | int flags, const char *dev_name, | ||
| 509 | void *raw_data) | ||
| 510 | { | ||
| 511 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super); | ||
| 512 | } | ||
| 513 | |||
| 514 | static struct file_system_type fuse_fs_type = { | ||
| 515 | .owner = THIS_MODULE, | ||
| 516 | .name = "fuse", | ||
| 517 | .get_sb = fuse_get_sb, | ||
| 518 | .kill_sb = kill_anon_super, | ||
| 519 | }; | ||
| 520 | |||
| 521 | static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, | ||
| 522 | unsigned long flags) | ||
| 523 | { | ||
| 524 | struct inode * inode = foo; | ||
| 525 | |||
| 526 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | ||
| 527 | SLAB_CTOR_CONSTRUCTOR) | ||
| 528 | inode_init_once(inode); | ||
| 529 | } | ||
| 530 | |||
| 531 | static int __init fuse_fs_init(void) | ||
| 532 | { | ||
| 533 | int err; | ||
| 534 | |||
| 535 | err = register_filesystem(&fuse_fs_type); | ||
| 536 | if (err) | ||
| 537 | printk("fuse: failed to register filesystem\n"); | ||
| 538 | else { | ||
| 539 | fuse_inode_cachep = kmem_cache_create("fuse_inode", | ||
| 540 | sizeof(struct fuse_inode), | ||
| 541 | 0, SLAB_HWCACHE_ALIGN, | ||
| 542 | fuse_inode_init_once, NULL); | ||
| 543 | if (!fuse_inode_cachep) { | ||
| 544 | unregister_filesystem(&fuse_fs_type); | ||
| 545 | err = -ENOMEM; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | |||
| 549 | return err; | ||
| 550 | } | ||
| 551 | |||
| 552 | static void fuse_fs_cleanup(void) | ||
| 553 | { | ||
| 554 | unregister_filesystem(&fuse_fs_type); | ||
| 555 | kmem_cache_destroy(fuse_inode_cachep); | ||
| 556 | } | ||
| 557 | |||
| 558 | static int __init fuse_init(void) | ||
| 559 | { | ||
| 560 | int res; | ||
| 561 | |||
| 562 | printk("fuse init (API version %i.%i)\n", | ||
| 563 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); | ||
| 564 | |||
| 565 | spin_lock_init(&fuse_lock); | ||
| 566 | res = fuse_fs_init(); | ||
| 567 | if (res) | ||
| 568 | goto err; | ||
| 569 | |||
| 570 | res = fuse_dev_init(); | ||
| 571 | if (res) | ||
| 572 | goto err_fs_cleanup; | ||
| 573 | |||
| 574 | return 0; | ||
| 575 | |||
| 576 | err_fs_cleanup: | ||
| 577 | fuse_fs_cleanup(); | ||
| 578 | err: | ||
| 579 | return res; | ||
| 580 | } | ||
| 581 | |||
| 582 | static void __exit fuse_exit(void) | ||
| 583 | { | ||
| 584 | printk(KERN_DEBUG "fuse exit\n"); | ||
| 585 | |||
| 586 | fuse_fs_cleanup(); | ||
| 587 | fuse_dev_cleanup(); | ||
| 588 | } | ||
| 589 | |||
| 590 | module_init(fuse_init); | ||
| 591 | module_exit(fuse_exit); | ||
