2300 lines · 51.6 KB · C
RawDownload
/*  FUSE: Filesystem in Userspace  Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>   This program can be distributed under the terms of the GNU GPL.  See the file COPYING.*/ #include "fuse_i.h" #include <linux/init.h>#include <linux/module.h>#include <linux/poll.h>#include <linux/sched/signal.h>#include <linux/uio.h>#include <linux/miscdevice.h>#include <linux/pagemap.h>#include <linux/file.h>#include <linux/slab.h>#include <linux/pipe_fs_i.h>#include <linux/swap.h>#include <linux/splice.h>#include <linux/sched.h> MODULE_ALIAS_MISCDEV(FUSE_MINOR);MODULE_ALIAS("devname:fuse"); /* Ordinary requests have even IDs, while interrupts IDs are odd */#define FUSE_INT_REQ_BIT (1ULL << 0)#define FUSE_REQ_ID_STEP (1ULL << 1) static struct kmem_cache *fuse_req_cachep; static struct fuse_dev *fuse_get_dev(struct file *file){	/*	 * Lockless access is OK, because file->private data is set	 * once during mount and is valid until the file is released.	 */	return READ_ONCE(file->private_data);} static void fuse_request_init(struct fuse_req *req){	INIT_LIST_HEAD(&req->list);	INIT_LIST_HEAD(&req->intr_entry);	init_waitqueue_head(&req->waitq);	refcount_set(&req->count, 1);	__set_bit(FR_PENDING, &req->flags);} static struct fuse_req *fuse_request_alloc(gfp_t flags){	struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);	if (req)		fuse_request_init(req); 	return req;} static void fuse_request_free(struct fuse_req *req){	kmem_cache_free(fuse_req_cachep, req);} static void __fuse_get_request(struct fuse_req *req){	refcount_inc(&req->count);} /* Must be called with > 1 refcount */static void __fuse_put_request(struct fuse_req *req){	refcount_dec(&req->count);} void fuse_set_initialized(struct fuse_conn *fc){	/* Make sure stores before this are seen on another CPU */	smp_wmb();	fc->initialized = 1;} static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background){	return !fc->initialized || (for_background && fc->blocked);} static void fuse_drop_waiting(struct fuse_conn *fc){	/*	 * lockess check of fc->connected is okay, because atomic_dec_and_test()	 * provides a memory barrier mached with the one in fuse_wait_aborted()	 * to ensure no wake-up is missed.	 */	if (atomic_dec_and_test(&fc->num_waiting) &&	    !READ_ONCE(fc->connected)) {		/* wake up aborters */		wake_up_all(&fc->blocked_waitq);	}} static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); static struct fuse_req *fuse_get_req(struct fuse_conn *fc, bool for_background){	struct fuse_req *req;	int err;	atomic_inc(&fc->num_waiting); 	if (fuse_block_alloc(fc, for_background)) {		err = -EINTR;		if (wait_event_killable_exclusive(fc->blocked_waitq,				!fuse_block_alloc(fc, for_background)))			goto out;	}	/* Matches smp_wmb() in fuse_set_initialized() */	smp_rmb(); 	err = -ENOTCONN;	if (!fc->connected)		goto out; 	err = -ECONNREFUSED;	if (fc->conn_error)		goto out; 	req = fuse_request_alloc(GFP_KERNEL);	err = -ENOMEM;	if (!req) {		if (for_background)			wake_up(&fc->blocked_waitq);		goto out;	} 	req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());	req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());	req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns); 	__set_bit(FR_WAITING, &req->flags);	if (for_background)		__set_bit(FR_BACKGROUND, &req->flags); 	if (unlikely(req->in.h.uid == ((uid_t)-1) ||		     req->in.h.gid == ((gid_t)-1))) {		fuse_put_request(fc, req);		return ERR_PTR(-EOVERFLOW);	}	return req;  out:	fuse_drop_waiting(fc);	return ERR_PTR(err);} static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req){	if (refcount_dec_and_test(&req->count)) {		if (test_bit(FR_BACKGROUND, &req->flags)) {			/*			 * We get here in the unlikely case that a background			 * request was allocated but not sent			 */			spin_lock(&fc->bg_lock);			if (!fc->blocked)				wake_up(&fc->blocked_waitq);			spin_unlock(&fc->bg_lock);		} 		if (test_bit(FR_WAITING, &req->flags)) {			__clear_bit(FR_WAITING, &req->flags);			fuse_drop_waiting(fc);		} 		fuse_request_free(req);	}} unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args){	unsigned nbytes = 0;	unsigned i; 	for (i = 0; i < numargs; i++)		nbytes += args[i].size; 	return nbytes;}EXPORT_SYMBOL_GPL(fuse_len_args); u64 fuse_get_unique(struct fuse_iqueue *fiq){	fiq->reqctr += FUSE_REQ_ID_STEP;	return fiq->reqctr;}EXPORT_SYMBOL_GPL(fuse_get_unique); static unsigned int fuse_req_hash(u64 unique){	return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);} /** * A new request is available, wake fiq->waitq */static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)__releases(fiq->lock){	wake_up(&fiq->waitq);	kill_fasync(&fiq->fasync, SIGIO, POLL_IN);	spin_unlock(&fiq->lock);} const struct fuse_iqueue_ops fuse_dev_fiq_ops = {	.wake_forget_and_unlock		= fuse_dev_wake_and_unlock,	.wake_interrupt_and_unlock	= fuse_dev_wake_and_unlock,	.wake_pending_and_unlock	= fuse_dev_wake_and_unlock,};EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops); static void queue_request_and_unlock(struct fuse_iqueue *fiq,				     struct fuse_req *req)__releases(fiq->lock){	req->in.h.len = sizeof(struct fuse_in_header) +		fuse_len_args(req->args->in_numargs,			      (struct fuse_arg *) req->args->in_args);	list_add_tail(&req->list, &fiq->pending);	fiq->ops->wake_pending_and_unlock(fiq);} void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,		       u64 nodeid, u64 nlookup){	struct fuse_iqueue *fiq = &fc->iq; 	forget->forget_one.nodeid = nodeid;	forget->forget_one.nlookup = nlookup; 	spin_lock(&fiq->lock);	if (fiq->connected) {		fiq->forget_list_tail->next = forget;		fiq->forget_list_tail = forget;		fiq->ops->wake_forget_and_unlock(fiq);	} else {		kfree(forget);		spin_unlock(&fiq->lock);	}} static void flush_bg_queue(struct fuse_conn *fc){	struct fuse_iqueue *fiq = &fc->iq; 	while (fc->active_background < fc->max_background &&	       !list_empty(&fc->bg_queue)) {		struct fuse_req *req; 		req = list_first_entry(&fc->bg_queue, struct fuse_req, list);		list_del(&req->list);		fc->active_background++;		spin_lock(&fiq->lock);		req->in.h.unique = fuse_get_unique(fiq);		queue_request_and_unlock(fiq, req);	}} /* * This function is called when a request is finished.  Either a reply * has arrived or it was aborted (and not yet sent) or some error * occurred during communication with userspace, or the device file * was closed.  The requester thread is woken up (if still waiting), * the 'end' callback is called if given, else the reference to the * request is released */void fuse_request_end(struct fuse_conn *fc, struct fuse_req *req){	struct fuse_iqueue *fiq = &fc->iq;	bool async; 	if (test_and_set_bit(FR_FINISHED, &req->flags))		goto put_request; 	async = req->args->end;	/*	 * test_and_set_bit() implies smp_mb() between bit	 * changing and below intr_entry check. Pairs with	 * smp_mb() from queue_interrupt().	 */	if (!list_empty(&req->intr_entry)) {		spin_lock(&fiq->lock);		list_del_init(&req->intr_entry);		spin_unlock(&fiq->lock);	}	WARN_ON(test_bit(FR_PENDING, &req->flags));	WARN_ON(test_bit(FR_SENT, &req->flags));	if (test_bit(FR_BACKGROUND, &req->flags)) {		spin_lock(&fc->bg_lock);		clear_bit(FR_BACKGROUND, &req->flags);		if (fc->num_background == fc->max_background) {			fc->blocked = 0;			wake_up(&fc->blocked_waitq);		} else if (!fc->blocked) {			/*			 * Wake up next waiter, if any.  It's okay to use			 * waitqueue_active(), as we've already synced up			 * fc->blocked with waiters with the wake_up() call			 * above.			 */			if (waitqueue_active(&fc->blocked_waitq))				wake_up(&fc->blocked_waitq);		} 		if (fc->num_background == fc->congestion_threshold && fc->sb) {			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);		}		fc->num_background--;		fc->active_background--;		flush_bg_queue(fc);		spin_unlock(&fc->bg_lock);	} else {		/* Wake up waiter sleeping in request_wait_answer() */		wake_up(&req->waitq);	} 	if (async)		req->args->end(fc, req->args, req->out.h.error);put_request:	fuse_put_request(fc, req);}EXPORT_SYMBOL_GPL(fuse_request_end); static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req){	spin_lock(&fiq->lock);	/* Check for we've sent request to interrupt this req */	if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {		spin_unlock(&fiq->lock);		return -EINVAL;	} 	if (list_empty(&req->intr_entry)) {		list_add_tail(&req->intr_entry, &fiq->interrupts);		/*		 * Pairs with smp_mb() implied by test_and_set_bit()		 * from request_end().		 */		smp_mb();		if (test_bit(FR_FINISHED, &req->flags)) {			list_del_init(&req->intr_entry);			spin_unlock(&fiq->lock);			return 0;		}		fiq->ops->wake_interrupt_and_unlock(fiq);	} else {		spin_unlock(&fiq->lock);	}	return 0;} static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req){	struct fuse_iqueue *fiq = &fc->iq;	int err; 	if (!fc->no_interrupt) {		/* Any signal may interrupt this */		err = wait_event_interruptible(req->waitq,					test_bit(FR_FINISHED, &req->flags));		if (!err)			return; 		set_bit(FR_INTERRUPTED, &req->flags);		/* matches barrier in fuse_dev_do_read() */		smp_mb__after_atomic();		if (test_bit(FR_SENT, &req->flags))			queue_interrupt(fiq, req);	} 	if (!test_bit(FR_FORCE, &req->flags)) {		/* Only fatal signals may interrupt this */		err = wait_event_killable(req->waitq,					test_bit(FR_FINISHED, &req->flags));		if (!err)			return; 		spin_lock(&fiq->lock);		/* Request is not yet in userspace, bail out */		if (test_bit(FR_PENDING, &req->flags)) {			list_del(&req->list);			spin_unlock(&fiq->lock);			__fuse_put_request(req);			req->out.h.error = -EINTR;			return;		}		spin_unlock(&fiq->lock);	} 	/*	 * Either request is already in userspace, or it was forced.	 * Wait it out.	 */	wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));} static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req){	struct fuse_iqueue *fiq = &fc->iq; 	BUG_ON(test_bit(FR_BACKGROUND, &req->flags));	spin_lock(&fiq->lock);	if (!fiq->connected) {		spin_unlock(&fiq->lock);		req->out.h.error = -ENOTCONN;	} else {		req->in.h.unique = fuse_get_unique(fiq);		/* acquire extra reference, since request is still needed		   after fuse_request_end() */		__fuse_get_request(req);		queue_request_and_unlock(fiq, req); 		request_wait_answer(fc, req);		/* Pairs with smp_wmb() in fuse_request_end() */		smp_rmb();	}} static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args){	if (fc->minor < 4 && args->opcode == FUSE_STATFS)		args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE; 	if (fc->minor < 9) {		switch (args->opcode) {		case FUSE_LOOKUP:		case FUSE_CREATE:		case FUSE_MKNOD:		case FUSE_MKDIR:		case FUSE_SYMLINK:		case FUSE_LINK:			args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;			break;		case FUSE_GETATTR:		case FUSE_SETATTR:			args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;			break;		}	}	if (fc->minor < 12) {		switch (args->opcode) {		case FUSE_CREATE:			args->in_args[0].size = sizeof(struct fuse_open_in);			break;		case FUSE_MKNOD:			args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;			break;		}	}} static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req){	req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());	req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());	req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);} static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args){	req->in.h.opcode = args->opcode;	req->in.h.nodeid = args->nodeid;	req->args = args;} ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args){	struct fuse_req *req;	ssize_t ret; 	if (args->force) {		atomic_inc(&fc->num_waiting);		req = fuse_request_alloc(GFP_KERNEL | __GFP_NOFAIL); 		if (!args->nocreds)			fuse_force_creds(fc, req); 		__set_bit(FR_WAITING, &req->flags);		__set_bit(FR_FORCE, &req->flags);	} else {		WARN_ON(args->nocreds);		req = fuse_get_req(fc, false);		if (IS_ERR(req))			return PTR_ERR(req);	} 	/* Needs to be done after fuse_get_req() so that fc->minor is valid */	fuse_adjust_compat(fc, args);	fuse_args_to_req(req, args); 

Lines 1–500 of 2300.