2266 lines · 53.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/pagemap.h>#include <linux/slab.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/module.h>#include <linux/compat.h>#include <linux/swap.h> static const struct file_operations fuse_direct_io_file_operations; static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,			  int opcode, struct fuse_open_out *outargp){	struct fuse_open_in inarg;	struct fuse_req *req;	int err; 	req = fuse_get_req(fc);	if (IS_ERR(req))		return PTR_ERR(req); 	memset(&inarg, 0, sizeof(inarg));	inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);	if (!fc->atomic_o_trunc)		inarg.flags &= ~O_TRUNC;	req->in.h.opcode = opcode;	req->in.h.nodeid = nodeid;	req->in.numargs = 1;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	req->out.numargs = 1;	req->out.args[0].size = sizeof(*outargp);	req->out.args[0].value = outargp;	fuse_request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req); 	return err;} struct fuse_file *fuse_file_alloc(struct fuse_conn *fc){	struct fuse_file *ff; 	ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);	if (unlikely(!ff))		return NULL; 	ff->fc = fc;	ff->reserved_req = fuse_request_alloc();	if (unlikely(!ff->reserved_req)) {		kfree(ff);		return NULL;	} 	INIT_LIST_HEAD(&ff->write_entry);	atomic_set(&ff->count, 0);	RB_CLEAR_NODE(&ff->polled_node);	init_waitqueue_head(&ff->poll_wait); 	spin_lock(&fc->lock);	ff->kh = ++fc->khctr;	spin_unlock(&fc->lock); 	return ff;} void fuse_file_free(struct fuse_file *ff){	fuse_request_free(ff->reserved_req);	kfree(ff);} struct fuse_file *fuse_file_get(struct fuse_file *ff){	atomic_inc(&ff->count);	return ff;} static void fuse_release_async(struct work_struct *work){	struct fuse_req *req;	struct fuse_conn *fc;	struct path path; 	req = container_of(work, struct fuse_req, misc.release.work);	path = req->misc.release.path;	fc = get_fuse_conn(path.dentry->d_inode); 	fuse_put_request(fc, req);	path_put(&path);} static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req){	if (fc->destroy_req) {		/*		 * If this is a fuseblk mount, then it's possible that		 * releasing the path will result in releasing the		 * super block and sending the DESTROY request.  If		 * the server is single threaded, this would hang.		 * For this reason do the path_put() in a separate		 * thread.		 */		atomic_inc(&req->count);		INIT_WORK(&req->misc.release.work, fuse_release_async);		schedule_work(&req->misc.release.work);	} else {		path_put(&req->misc.release.path);	}} static void fuse_file_put(struct fuse_file *ff, bool sync){	if (atomic_dec_and_test(&ff->count)) {		struct fuse_req *req = ff->reserved_req; 		if (sync) {			fuse_request_send(ff->fc, req);			path_put(&req->misc.release.path);			fuse_put_request(ff->fc, req);		} else {			req->end = fuse_release_end;			fuse_request_send_background(ff->fc, req);		}		kfree(ff);	}} int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,		 bool isdir){	struct fuse_open_out outarg;	struct fuse_file *ff;	int err;	int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN; 	ff = fuse_file_alloc(fc);	if (!ff)		return -ENOMEM; 	err = fuse_send_open(fc, nodeid, file, opcode, &outarg);	if (err) {		fuse_file_free(ff);		return err;	} 	if (isdir)		outarg.open_flags &= ~FOPEN_DIRECT_IO; 	ff->fh = outarg.fh;	ff->nodeid = nodeid;	ff->open_flags = outarg.open_flags;	file->private_data = fuse_file_get(ff); 	return 0;}EXPORT_SYMBOL_GPL(fuse_do_open); void fuse_finish_open(struct inode *inode, struct file *file){	struct fuse_file *ff = file->private_data;	struct fuse_conn *fc = get_fuse_conn(inode); 	if (ff->open_flags & FOPEN_DIRECT_IO)		file->f_op = &fuse_direct_io_file_operations;	if (!(ff->open_flags & FOPEN_KEEP_CACHE))		invalidate_inode_pages2(inode->i_mapping);	if (ff->open_flags & FOPEN_NONSEEKABLE)		nonseekable_open(inode, file);	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {		struct fuse_inode *fi = get_fuse_inode(inode); 		spin_lock(&fc->lock);		fi->attr_version = ++fc->attr_version;		i_size_write(inode, 0);		spin_unlock(&fc->lock);		fuse_invalidate_attr(inode);	}} int fuse_open_common(struct inode *inode, struct file *file, bool isdir){	struct fuse_conn *fc = get_fuse_conn(inode);	int err; 	err = generic_file_open(inode, file);	if (err)		return err; 	err = fuse_do_open(fc, get_node_id(inode), file, isdir);	if (err)		return err; 	fuse_finish_open(inode, file); 	return 0;} static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode){	struct fuse_conn *fc = ff->fc;	struct fuse_req *req = ff->reserved_req;	struct fuse_release_in *inarg = &req->misc.release.in; 	spin_lock(&fc->lock);	list_del(&ff->write_entry);	if (!RB_EMPTY_NODE(&ff->polled_node))		rb_erase(&ff->polled_node, &fc->polled_files);	spin_unlock(&fc->lock); 	wake_up_interruptible_all(&ff->poll_wait); 	inarg->fh = ff->fh;	inarg->flags = flags;	req->in.h.opcode = opcode;	req->in.h.nodeid = ff->nodeid;	req->in.numargs = 1;	req->in.args[0].size = sizeof(struct fuse_release_in);	req->in.args[0].value = inarg;} void fuse_release_common(struct file *file, int opcode){	struct fuse_file *ff;	struct fuse_req *req; 	ff = file->private_data;	if (unlikely(!ff))		return; 	req = ff->reserved_req;	fuse_prepare_release(ff, file->f_flags, opcode); 	if (ff->flock) {		struct fuse_release_in *inarg = &req->misc.release.in;		inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;		inarg->lock_owner = fuse_lock_owner_id(ff->fc,						       (fl_owner_t) file);	}	/* Hold vfsmount and dentry until release is finished */	path_get(&file->f_path);	req->misc.release.path = file->f_path; 	/*	 * Normally this will send the RELEASE request, however if	 * some asynchronous READ or WRITE requests are outstanding,	 * the sending will be delayed.	 *	 * Make the release synchronous if this is a fuseblk mount,	 * synchronous RELEASE is allowed (and desirable) in this case	 * because the server can be trusted not to screw up.	 */	fuse_file_put(ff, ff->fc->destroy_req != NULL);} static int fuse_open(struct inode *inode, struct file *file){	return fuse_open_common(inode, file, false);} static int fuse_release(struct inode *inode, struct file *file){	fuse_release_common(file, FUSE_RELEASE); 	/* return value is ignored by VFS */	return 0;} void fuse_sync_release(struct fuse_file *ff, int flags){	WARN_ON(atomic_read(&ff->count) > 1);	fuse_prepare_release(ff, flags, FUSE_RELEASE);	ff->reserved_req->force = 1;	fuse_request_send(ff->fc, ff->reserved_req);	fuse_put_request(ff->fc, ff->reserved_req);	kfree(ff);}EXPORT_SYMBOL_GPL(fuse_sync_release); /* * Scramble the ID space with XTEA, so that the value of the files_struct * pointer is not exposed to userspace. */u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id){	u32 *k = fc->scramble_key;	u64 v = (unsigned long) id;	u32 v0 = v;	u32 v1 = v >> 32;	u32 sum = 0;	int i; 	for (i = 0; i < 32; i++) {		v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);		sum += 0x9E3779B9;		v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);	} 	return (u64) v0 + ((u64) v1 << 32);} /* * Check if page is under writeback * * This is currently done by walking the list of writepage requests * for the inode, which can be pretty inefficient. */static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index){	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_inode *fi = get_fuse_inode(inode);	struct fuse_req *req;	bool found = false; 	spin_lock(&fc->lock);	list_for_each_entry(req, &fi->writepages, writepages_entry) {		pgoff_t curr_index; 		BUG_ON(req->inode != inode);		curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;		if (curr_index == index) {			found = true;			break;		}	}	spin_unlock(&fc->lock); 	return found;} /* * Wait for page writeback to be completed. * * Since fuse doesn't rely on the VM writeback tracking, this has to * use some other means. */static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index){	struct fuse_inode *fi = get_fuse_inode(inode); 	wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));	return 0;} static int fuse_flush(struct file *file, fl_owner_t id){	struct inode *inode = file->f_path.dentry->d_inode;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_file *ff = file->private_data;	struct fuse_req *req;	struct fuse_flush_in inarg;	int err; 	if (is_bad_inode(inode))		return -EIO; 	if (fc->no_flush)		return 0; 	req = fuse_get_req_nofail(fc, file);	memset(&inarg, 0, sizeof(inarg));	inarg.fh = ff->fh;	inarg.lock_owner = fuse_lock_owner_id(fc, id);	req->in.h.opcode = FUSE_FLUSH;	req->in.h.nodeid = get_node_id(inode);	req->in.numargs = 1;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	req->force = 1;	fuse_request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (err == -ENOSYS) {		fc->no_flush = 1;		err = 0;	}	return err;} /* * Wait for all pending writepages on the inode to finish. * * This is currently done by blocking further writes with FUSE_NOWRITE * and waiting for all sent writes to complete. * * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage * could conflict with truncation. */static void fuse_sync_writes(struct inode *inode){	fuse_set_nowrite(inode);	fuse_release_nowrite(inode);} int fuse_fsync_common(struct file *file, loff_t start, loff_t end,		      int datasync, int isdir){	struct inode *inode = file->f_mapping->host;	struct fuse_conn *fc = get_fuse_conn(inode);	struct fuse_file *ff = file->private_data;	struct fuse_req *req;	struct fuse_fsync_in inarg;	int err; 	if (is_bad_inode(inode))		return -EIO; 	err = filemap_write_and_wait_range(inode->i_mapping, start, end);	if (err)		return err; 	if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))		return 0; 	mutex_lock(&inode->i_mutex); 	/*	 * Start writeback against all dirty pages of the inode, then	 * wait for all outstanding writes, before sending the FSYNC	 * request.	 */	err = write_inode_now(inode, 0);	if (err)		goto out; 	fuse_sync_writes(inode); 	req = fuse_get_req(fc);	if (IS_ERR(req)) {		err = PTR_ERR(req);		goto out;	} 	memset(&inarg, 0, sizeof(inarg));	inarg.fh = ff->fh;	inarg.fsync_flags = datasync ? 1 : 0;	req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;	req->in.h.nodeid = get_node_id(inode);	req->in.numargs = 1;	req->in.args[0].size = sizeof(inarg);	req->in.args[0].value = &inarg;	fuse_request_send(fc, req);	err = req->out.h.error;	fuse_put_request(fc, req);	if (err == -ENOSYS) {		if (isdir)			fc->no_fsyncdir = 1;		else			fc->no_fsync = 1;		err = 0;	}out:	mutex_unlock(&inode->i_mutex);	return err;} static int fuse_fsync(struct file *file, loff_t start, loff_t end,		      int datasync){	return fuse_fsync_common(file, start, end, datasync, 0);} void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,		    size_t count, int opcode){	struct fuse_read_in *inarg = &req->misc.read.in;	struct fuse_file *ff = file->private_data; 	inarg->fh = ff->fh;	inarg->offset = pos;	inarg->size = count;	inarg->flags = file->f_flags;	req->in.h.opcode = opcode;	req->in.h.nodeid = ff->nodeid;	req->in.numargs = 1;	req->in.args[0].size = sizeof(struct fuse_read_in);	req->in.args[0].value = inarg;	req->out.argvar = 1;	req->out.numargs = 1;	req->out.args[0].size = count;} static size_t fuse_send_read(struct fuse_req *req, struct file *file,			     loff_t pos, size_t count, fl_owner_t owner){	struct fuse_file *ff = file->private_data;	struct fuse_conn *fc = ff->fc; 	fuse_read_fill(req, file, pos, count, FUSE_READ);

Lines 1–500 of 2266.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t