Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 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> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 17 | #include <linux/parser.h> |
| 18 | #include <linux/statfs.h> |
Miklos Szeredi | 9c8ef56 | 2006-06-25 05:48:55 -0700 | [diff] [blame] | 19 | #include <linux/random.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 21 | #include <linux/exportfs.h> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 22 | |
| 23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 24 | MODULE_DESCRIPTION("Filesystem in Userspace"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 27 | static struct kmem_cache *fuse_inode_cachep; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 28 | struct list_head fuse_conn_list; |
| 29 | DEFINE_MUTEX(fuse_mutex); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 30 | |
| 31 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 32 | |
Miklos Szeredi | d1875db | 2008-02-08 04:21:43 -0800 | [diff] [blame] | 33 | #define FUSE_DEFAULT_BLKSIZE 512 |
| 34 | |
Csaba Henk | 7a6d3c8 | 2009-07-01 17:28:41 -0700 | [diff] [blame^] | 35 | /** Maximum number of outstanding background requests */ |
| 36 | #define FUSE_DEFAULT_MAX_BACKGROUND 12 |
| 37 | |
| 38 | /** Congestion starts at 75% of maximum */ |
| 39 | #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4) |
| 40 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 41 | struct fuse_mount_data { |
| 42 | int fd; |
| 43 | unsigned rootmode; |
| 44 | unsigned user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 45 | unsigned group_id; |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 46 | unsigned fd_present:1; |
| 47 | unsigned rootmode_present:1; |
| 48 | unsigned user_id_present:1; |
| 49 | unsigned group_id_present:1; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 50 | unsigned flags; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 51 | unsigned max_read; |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 52 | unsigned blksize; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
| 56 | { |
| 57 | struct inode *inode; |
| 58 | struct fuse_inode *fi; |
| 59 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 60 | inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 61 | if (!inode) |
| 62 | return NULL; |
| 63 | |
| 64 | fi = get_fuse_inode(inode); |
Miklos Szeredi | 0a0898c | 2006-07-30 03:04:10 -0700 | [diff] [blame] | 65 | fi->i_time = 0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 66 | fi->nodeid = 0; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 67 | fi->nlookup = 0; |
John Muir | fbee36b | 2007-11-28 16:22:02 -0800 | [diff] [blame] | 68 | fi->attr_version = 0; |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 69 | fi->writectr = 0; |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 70 | INIT_LIST_HEAD(&fi->write_files); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 71 | INIT_LIST_HEAD(&fi->queued_writes); |
| 72 | INIT_LIST_HEAD(&fi->writepages); |
| 73 | init_waitqueue_head(&fi->page_waitq); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 74 | fi->forget_req = fuse_request_alloc(); |
| 75 | if (!fi->forget_req) { |
| 76 | kmem_cache_free(fuse_inode_cachep, inode); |
| 77 | return NULL; |
| 78 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 79 | |
| 80 | return inode; |
| 81 | } |
| 82 | |
| 83 | static void fuse_destroy_inode(struct inode *inode) |
| 84 | { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 85 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 93a8c3c | 2007-10-18 03:07:03 -0700 | [diff] [blame] | 86 | BUG_ON(!list_empty(&fi->write_files)); |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 87 | BUG_ON(!list_empty(&fi->queued_writes)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 88 | if (fi->forget_req) |
| 89 | fuse_request_free(fi->forget_req); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 90 | kmem_cache_free(fuse_inode_cachep, inode); |
| 91 | } |
| 92 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 93 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
Miklos Szeredi | b48badf | 2008-04-30 00:54:44 -0700 | [diff] [blame] | 94 | u64 nodeid, u64 nlookup) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 95 | { |
| 96 | struct fuse_forget_in *inarg = &req->misc.forget_in; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 97 | inarg->nlookup = nlookup; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 98 | req->in.h.opcode = FUSE_FORGET; |
| 99 | req->in.h.nodeid = nodeid; |
| 100 | req->in.numargs = 1; |
| 101 | req->in.args[0].size = sizeof(struct fuse_forget_in); |
| 102 | req->in.args[0].value = inarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 103 | fuse_request_send_noreply(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 106 | static void fuse_clear_inode(struct inode *inode) |
| 107 | { |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 108 | if (inode->i_sb->s_flags & MS_ACTIVE) { |
| 109 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 110 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 111 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 112 | fi->forget_req = NULL; |
| 113 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 116 | static int fuse_remount_fs(struct super_block *sb, int *flags, char *data) |
| 117 | { |
| 118 | if (*flags & MS_MANDLOCK) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 124 | void fuse_truncate(struct address_space *mapping, loff_t offset) |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 125 | { |
| 126 | /* See vmtruncate() */ |
| 127 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
| 128 | truncate_inode_pages(mapping, offset); |
| 129 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
| 130 | } |
| 131 | |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 132 | void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, |
| 133 | u64 attr_valid) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 134 | { |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 135 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | ebc14c4 | 2007-10-16 23:31:03 -0700 | [diff] [blame] | 136 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 137 | |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 138 | fi->attr_version = ++fc->attr_version; |
| 139 | fi->i_time = attr_valid; |
| 140 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 141 | inode->i_ino = attr->ino; |
Miklos Szeredi | ebc14c4 | 2007-10-16 23:31:03 -0700 | [diff] [blame] | 142 | inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 143 | inode->i_nlink = attr->nlink; |
| 144 | inode->i_uid = attr->uid; |
| 145 | inode->i_gid = attr->gid; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 146 | inode->i_blocks = attr->blocks; |
| 147 | inode->i_atime.tv_sec = attr->atime; |
| 148 | inode->i_atime.tv_nsec = attr->atimensec; |
| 149 | inode->i_mtime.tv_sec = attr->mtime; |
| 150 | inode->i_mtime.tv_nsec = attr->mtimensec; |
| 151 | inode->i_ctime.tv_sec = attr->ctime; |
| 152 | inode->i_ctime.tv_nsec = attr->ctimensec; |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 153 | |
Miklos Szeredi | 0e9663e | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 154 | if (attr->blksize != 0) |
| 155 | inode->i_blkbits = ilog2(attr->blksize); |
| 156 | else |
| 157 | inode->i_blkbits = inode->i_sb->s_blocksize_bits; |
| 158 | |
Miklos Szeredi | ebc14c4 | 2007-10-16 23:31:03 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Don't set the sticky bit in i_mode, unless we want the VFS |
| 161 | * to check permissions. This prevents failures due to the |
| 162 | * check in may_delete(). |
| 163 | */ |
| 164 | fi->orig_i_mode = inode->i_mode; |
| 165 | if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS)) |
| 166 | inode->i_mode &= ~S_ISVTX; |
Miklos Szeredi | 3be5a52 | 2008-04-30 00:54:41 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, |
| 170 | u64 attr_valid, u64 attr_version) |
| 171 | { |
| 172 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 173 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 174 | loff_t oldsize; |
| 175 | |
| 176 | spin_lock(&fc->lock); |
| 177 | if (attr_version != 0 && fi->attr_version > attr_version) { |
| 178 | spin_unlock(&fc->lock); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | fuse_change_attributes_common(inode, attr, attr_valid); |
Miklos Szeredi | ebc14c4 | 2007-10-16 23:31:03 -0700 | [diff] [blame] | 183 | |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 184 | oldsize = inode->i_size; |
| 185 | i_size_write(inode, attr->size); |
| 186 | spin_unlock(&fc->lock); |
| 187 | |
| 188 | if (S_ISREG(inode->i_mode) && oldsize != attr->size) { |
| 189 | if (attr->size < oldsize) |
| 190 | fuse_truncate(inode->i_mapping, attr->size); |
Miklos Szeredi | b100997 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 191 | invalidate_inode_pages2(inode->i_mapping); |
Miklos Szeredi | e00d2c2 | 2007-10-16 23:31:01 -0700 | [diff] [blame] | 192 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
| 196 | { |
| 197 | inode->i_mode = attr->mode & S_IFMT; |
Miklos Szeredi | 9ffbb91 | 2006-10-17 00:10:06 -0700 | [diff] [blame] | 198 | inode->i_size = attr->size; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 199 | if (S_ISREG(inode->i_mode)) { |
| 200 | fuse_init_common(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 201 | fuse_init_file_inode(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 202 | } else if (S_ISDIR(inode->i_mode)) |
| 203 | fuse_init_dir(inode); |
| 204 | else if (S_ISLNK(inode->i_mode)) |
| 205 | fuse_init_symlink(inode); |
| 206 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 207 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 208 | fuse_init_common(inode); |
| 209 | init_special_inode(inode, inode->i_mode, |
| 210 | new_decode_dev(attr->rdev)); |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 211 | } else |
| 212 | BUG(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 213 | } |
| 214 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 215 | int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 216 | { |
Miklos Szeredi | b48badf | 2008-04-30 00:54:44 -0700 | [diff] [blame] | 217 | u64 nodeid = *(u64 *) _nodeidp; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 218 | if (get_node_id(inode) == nodeid) |
| 219 | return 1; |
| 220 | else |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 225 | { |
Miklos Szeredi | b48badf | 2008-04-30 00:54:44 -0700 | [diff] [blame] | 226 | u64 nodeid = *(u64 *) _nodeidp; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 227 | get_fuse_inode(inode)->nodeid = nodeid; |
| 228 | return 0; |
| 229 | } |
| 230 | |
Miklos Szeredi | b48badf | 2008-04-30 00:54:44 -0700 | [diff] [blame] | 231 | struct inode *fuse_iget(struct super_block *sb, u64 nodeid, |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 232 | int generation, struct fuse_attr *attr, |
| 233 | u64 attr_valid, u64 attr_version) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 234 | { |
| 235 | struct inode *inode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 236 | struct fuse_inode *fi; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 237 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 238 | |
| 239 | retry: |
| 240 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 241 | if (!inode) |
| 242 | return NULL; |
| 243 | |
| 244 | if ((inode->i_state & I_NEW)) { |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 245 | inode->i_flags |= S_NOATIME|S_NOCMTIME; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 246 | inode->i_generation = generation; |
| 247 | inode->i_data.backing_dev_info = &fc->bdi; |
| 248 | fuse_init_inode(inode, attr); |
| 249 | unlock_new_inode(inode); |
| 250 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 251 | /* Inode has changed type, any I/O on the old should fail */ |
| 252 | make_bad_inode(inode); |
| 253 | iput(inode); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 254 | goto retry; |
| 255 | } |
| 256 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 257 | fi = get_fuse_inode(inode); |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 258 | spin_lock(&fc->lock); |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 259 | fi->nlookup++; |
Miklos Szeredi | 8da5ff2 | 2006-10-17 00:10:08 -0700 | [diff] [blame] | 260 | spin_unlock(&fc->lock); |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 261 | fuse_change_attributes(inode, attr, attr_valid, attr_version); |
| 262 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 263 | return inode; |
| 264 | } |
| 265 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 266 | int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid, |
| 267 | loff_t offset, loff_t len) |
| 268 | { |
| 269 | struct inode *inode; |
| 270 | pgoff_t pg_start; |
| 271 | pgoff_t pg_end; |
| 272 | |
| 273 | inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid); |
| 274 | if (!inode) |
| 275 | return -ENOENT; |
| 276 | |
| 277 | fuse_invalidate_attr(inode); |
| 278 | if (offset >= 0) { |
| 279 | pg_start = offset >> PAGE_CACHE_SHIFT; |
| 280 | if (len <= 0) |
| 281 | pg_end = -1; |
| 282 | else |
| 283 | pg_end = (offset + len - 1) >> PAGE_CACHE_SHIFT; |
| 284 | invalidate_inode_pages2_range(inode->i_mapping, |
| 285 | pg_start, pg_end); |
| 286 | } |
| 287 | iput(inode); |
| 288 | return 0; |
| 289 | } |
| 290 | |
Al Viro | 42faad9 | 2008-04-24 07:21:56 -0400 | [diff] [blame] | 291 | static void fuse_umount_begin(struct super_block *sb) |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 292 | { |
Al Viro | 42faad9 | 2008-04-24 07:21:56 -0400 | [diff] [blame] | 293 | fuse_abort_conn(get_fuse_conn_super(sb)); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 296 | static void fuse_send_destroy(struct fuse_conn *fc) |
| 297 | { |
| 298 | struct fuse_req *req = fc->destroy_req; |
| 299 | if (req && fc->conn_init) { |
| 300 | fc->destroy_req = NULL; |
| 301 | req->in.h.opcode = FUSE_DESTROY; |
| 302 | req->force = 1; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 303 | fuse_request_send(fc, req); |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 304 | fuse_put_request(fc, req); |
| 305 | } |
| 306 | } |
| 307 | |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 308 | static void fuse_bdi_destroy(struct fuse_conn *fc) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 309 | { |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 310 | if (fc->bdi_initialized) |
| 311 | bdi_destroy(&fc->bdi); |
| 312 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 313 | |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 314 | void fuse_conn_kill(struct fuse_conn *fc) |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 315 | { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 316 | spin_lock(&fc->lock); |
Miklos Szeredi | 9ba7cbb | 2006-01-16 22:14:34 -0800 | [diff] [blame] | 317 | fc->connected = 0; |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 318 | fc->blocked = 0; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 319 | spin_unlock(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 320 | /* Flush all readers on this fs */ |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 321 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 322 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 51eb01e | 2006-06-25 05:48:50 -0700 | [diff] [blame] | 323 | wake_up_all(&fc->blocked_waitq); |
Miklos Szeredi | de5e3de | 2007-10-16 23:31:00 -0700 | [diff] [blame] | 324 | wake_up_all(&fc->reserved_req_waitq); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 325 | mutex_lock(&fuse_mutex); |
| 326 | list_del(&fc->entry); |
| 327 | fuse_ctl_remove_conn(fc); |
| 328 | mutex_unlock(&fuse_mutex); |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 329 | fuse_bdi_destroy(fc); |
| 330 | } |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 331 | EXPORT_SYMBOL_GPL(fuse_conn_kill); |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 332 | |
| 333 | static void fuse_put_super(struct super_block *sb) |
| 334 | { |
| 335 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 336 | |
| 337 | fuse_send_destroy(fc); |
| 338 | fuse_conn_kill(fc); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 339 | fuse_conn_put(fc); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 342 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
| 343 | { |
| 344 | stbuf->f_type = FUSE_SUPER_MAGIC; |
| 345 | stbuf->f_bsize = attr->bsize; |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 346 | stbuf->f_frsize = attr->frsize; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 347 | stbuf->f_blocks = attr->blocks; |
| 348 | stbuf->f_bfree = attr->bfree; |
| 349 | stbuf->f_bavail = attr->bavail; |
| 350 | stbuf->f_files = attr->files; |
| 351 | stbuf->f_ffree = attr->ffree; |
| 352 | stbuf->f_namelen = attr->namelen; |
| 353 | /* fsid is left zero */ |
| 354 | } |
| 355 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 356 | static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 357 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 358 | struct super_block *sb = dentry->d_sb; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 359 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 360 | struct fuse_req *req; |
| 361 | struct fuse_statfs_out outarg; |
| 362 | int err; |
| 363 | |
Miklos Szeredi | e57ac68 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 364 | if (!fuse_allow_task(fc, current)) { |
| 365 | buf->f_type = FUSE_SUPER_MAGIC; |
| 366 | return 0; |
| 367 | } |
| 368 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 369 | req = fuse_get_req(fc); |
| 370 | if (IS_ERR(req)) |
| 371 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 372 | |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 373 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 374 | req->in.numargs = 0; |
| 375 | req->in.h.opcode = FUSE_STATFS; |
Miklos Szeredi | 5b35e8e | 2006-09-29 01:59:34 -0700 | [diff] [blame] | 376 | req->in.h.nodeid = get_node_id(dentry->d_inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 377 | req->out.numargs = 1; |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 378 | req->out.args[0].size = |
| 379 | fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 380 | req->out.args[0].value = &outarg; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 381 | fuse_request_send(fc, req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 382 | err = req->out.h.error; |
| 383 | if (!err) |
| 384 | convert_fuse_statfs(buf, &outarg.st); |
| 385 | fuse_put_request(fc, req); |
| 386 | return err; |
| 387 | } |
| 388 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 389 | enum { |
| 390 | OPT_FD, |
| 391 | OPT_ROOTMODE, |
| 392 | OPT_USER_ID, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 393 | OPT_GROUP_ID, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 394 | OPT_DEFAULT_PERMISSIONS, |
| 395 | OPT_ALLOW_OTHER, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 396 | OPT_MAX_READ, |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 397 | OPT_BLKSIZE, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 398 | OPT_ERR |
| 399 | }; |
| 400 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 401 | static const match_table_t tokens = { |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 402 | {OPT_FD, "fd=%u"}, |
| 403 | {OPT_ROOTMODE, "rootmode=%o"}, |
| 404 | {OPT_USER_ID, "user_id=%u"}, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 405 | {OPT_GROUP_ID, "group_id=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 406 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 407 | {OPT_ALLOW_OTHER, "allow_other"}, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 408 | {OPT_MAX_READ, "max_read=%u"}, |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 409 | {OPT_BLKSIZE, "blksize=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 410 | {OPT_ERR, NULL} |
| 411 | }; |
| 412 | |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 413 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 414 | { |
| 415 | char *p; |
| 416 | memset(d, 0, sizeof(struct fuse_mount_data)); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 417 | d->max_read = ~0; |
Miklos Szeredi | d1875db | 2008-02-08 04:21:43 -0800 | [diff] [blame] | 418 | d->blksize = FUSE_DEFAULT_BLKSIZE; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 419 | |
| 420 | while ((p = strsep(&opt, ",")) != NULL) { |
| 421 | int token; |
| 422 | int value; |
| 423 | substring_t args[MAX_OPT_ARGS]; |
| 424 | if (!*p) |
| 425 | continue; |
| 426 | |
| 427 | token = match_token(p, tokens, args); |
| 428 | switch (token) { |
| 429 | case OPT_FD: |
| 430 | if (match_int(&args[0], &value)) |
| 431 | return 0; |
| 432 | d->fd = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 433 | d->fd_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 434 | break; |
| 435 | |
| 436 | case OPT_ROOTMODE: |
| 437 | if (match_octal(&args[0], &value)) |
| 438 | return 0; |
Timo Savola | a5bfffa | 2007-04-08 16:04:00 -0700 | [diff] [blame] | 439 | if (!fuse_valid_type(value)) |
| 440 | return 0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 441 | d->rootmode = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 442 | d->rootmode_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 443 | break; |
| 444 | |
| 445 | case OPT_USER_ID: |
| 446 | if (match_int(&args[0], &value)) |
| 447 | return 0; |
| 448 | d->user_id = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 449 | d->user_id_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 450 | break; |
| 451 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 452 | case OPT_GROUP_ID: |
| 453 | if (match_int(&args[0], &value)) |
| 454 | return 0; |
| 455 | d->group_id = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 456 | d->group_id_present = 1; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 457 | break; |
| 458 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 459 | case OPT_DEFAULT_PERMISSIONS: |
| 460 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 461 | break; |
| 462 | |
| 463 | case OPT_ALLOW_OTHER: |
| 464 | d->flags |= FUSE_ALLOW_OTHER; |
| 465 | break; |
| 466 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 467 | case OPT_MAX_READ: |
| 468 | if (match_int(&args[0], &value)) |
| 469 | return 0; |
| 470 | d->max_read = value; |
| 471 | break; |
| 472 | |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 473 | case OPT_BLKSIZE: |
| 474 | if (!is_bdev || match_int(&args[0], &value)) |
| 475 | return 0; |
| 476 | d->blksize = value; |
| 477 | break; |
| 478 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 479 | default: |
| 480 | return 0; |
| 481 | } |
| 482 | } |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 483 | |
| 484 | if (!d->fd_present || !d->rootmode_present || |
| 485 | !d->user_id_present || !d->group_id_present) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 486 | return 0; |
| 487 | |
| 488 | return 1; |
| 489 | } |
| 490 | |
| 491 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 492 | { |
| 493 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); |
| 494 | |
| 495 | seq_printf(m, ",user_id=%u", fc->user_id); |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 496 | seq_printf(m, ",group_id=%u", fc->group_id); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 497 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 498 | seq_puts(m, ",default_permissions"); |
| 499 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 500 | seq_puts(m, ",allow_other"); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 501 | if (fc->max_read != ~0) |
| 502 | seq_printf(m, ",max_read=%u", fc->max_read); |
Miklos Szeredi | d1875db | 2008-02-08 04:21:43 -0800 | [diff] [blame] | 503 | if (mnt->mnt_sb->s_bdev && |
| 504 | mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE) |
| 505 | seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 509 | void fuse_conn_init(struct fuse_conn *fc) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 510 | { |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 511 | memset(fc, 0, sizeof(*fc)); |
| 512 | spin_lock_init(&fc->lock); |
| 513 | mutex_init(&fc->inst_mutex); |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 514 | init_rwsem(&fc->killsb); |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 515 | atomic_set(&fc->count, 1); |
| 516 | init_waitqueue_head(&fc->waitq); |
| 517 | init_waitqueue_head(&fc->blocked_waitq); |
| 518 | init_waitqueue_head(&fc->reserved_req_waitq); |
| 519 | INIT_LIST_HEAD(&fc->pending); |
| 520 | INIT_LIST_HEAD(&fc->processing); |
| 521 | INIT_LIST_HEAD(&fc->io); |
| 522 | INIT_LIST_HEAD(&fc->interrupts); |
| 523 | INIT_LIST_HEAD(&fc->bg_queue); |
| 524 | INIT_LIST_HEAD(&fc->entry); |
| 525 | atomic_set(&fc->num_waiting, 0); |
Csaba Henk | 7a6d3c8 | 2009-07-01 17:28:41 -0700 | [diff] [blame^] | 526 | fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND; |
| 527 | fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD; |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 528 | fc->khctr = 0; |
| 529 | fc->polled_files = RB_ROOT; |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 530 | fc->reqctr = 0; |
| 531 | fc->blocked = 1; |
| 532 | fc->attr_version = 1; |
| 533 | get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key)); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 534 | } |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 535 | EXPORT_SYMBOL_GPL(fuse_conn_init); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 536 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 537 | void fuse_conn_put(struct fuse_conn *fc) |
| 538 | { |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 539 | if (atomic_dec_and_test(&fc->count)) { |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 540 | if (fc->destroy_req) |
| 541 | fuse_request_free(fc->destroy_req); |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 542 | mutex_destroy(&fc->inst_mutex); |
Tejun Heo | 43901aa | 2008-11-26 12:03:56 +0100 | [diff] [blame] | 543 | fc->release(fc); |
Miklos Szeredi | d2a8516 | 2006-10-17 00:10:11 -0700 | [diff] [blame] | 544 | } |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 545 | } |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 546 | EXPORT_SYMBOL_GPL(fuse_conn_put); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 547 | |
| 548 | struct fuse_conn *fuse_conn_get(struct fuse_conn *fc) |
| 549 | { |
| 550 | atomic_inc(&fc->count); |
| 551 | return fc; |
| 552 | } |
Tejun Heo | 08cbf54 | 2009-04-14 10:54:53 +0900 | [diff] [blame] | 553 | EXPORT_SYMBOL_GPL(fuse_conn_get); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 554 | |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 555 | static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 556 | { |
| 557 | struct fuse_attr attr; |
| 558 | memset(&attr, 0, sizeof(attr)); |
| 559 | |
| 560 | attr.mode = mode; |
| 561 | attr.ino = FUSE_ROOT_ID; |
Miklos Szeredi | 074406f | 2007-10-16 23:31:02 -0700 | [diff] [blame] | 562 | attr.nlink = 1; |
Miklos Szeredi | 1fb69e7 | 2007-10-18 03:06:58 -0700 | [diff] [blame] | 563 | return fuse_iget(sb, 1, 0, &attr, 0, 0); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 566 | struct fuse_inode_handle { |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 567 | u64 nodeid; |
| 568 | u32 generation; |
| 569 | }; |
| 570 | |
| 571 | static struct dentry *fuse_get_dentry(struct super_block *sb, |
| 572 | struct fuse_inode_handle *handle) |
| 573 | { |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 574 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 575 | struct inode *inode; |
| 576 | struct dentry *entry; |
| 577 | int err = -ESTALE; |
| 578 | |
| 579 | if (handle->nodeid == 0) |
| 580 | goto out_err; |
| 581 | |
| 582 | inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid); |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 583 | if (!inode) { |
| 584 | struct fuse_entry_out outarg; |
| 585 | struct qstr name; |
| 586 | |
| 587 | if (!fc->export_support) |
| 588 | goto out_err; |
| 589 | |
| 590 | name.len = 1; |
| 591 | name.name = "."; |
| 592 | err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg, |
| 593 | &inode); |
| 594 | if (err && err != -ENOENT) |
| 595 | goto out_err; |
| 596 | if (err || !inode) { |
| 597 | err = -ESTALE; |
| 598 | goto out_err; |
| 599 | } |
| 600 | err = -EIO; |
| 601 | if (get_node_id(inode) != handle->nodeid) |
| 602 | goto out_iput; |
| 603 | } |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 604 | err = -ESTALE; |
| 605 | if (inode->i_generation != handle->generation) |
| 606 | goto out_iput; |
| 607 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 608 | entry = d_obtain_alias(inode); |
| 609 | if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) { |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 610 | entry->d_op = &fuse_dentry_operations; |
| 611 | fuse_invalidate_entry_cache(entry); |
| 612 | } |
| 613 | |
| 614 | return entry; |
| 615 | |
| 616 | out_iput: |
| 617 | iput(inode); |
| 618 | out_err: |
| 619 | return ERR_PTR(err); |
| 620 | } |
| 621 | |
| 622 | static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len, |
| 623 | int connectable) |
| 624 | { |
| 625 | struct inode *inode = dentry->d_inode; |
| 626 | bool encode_parent = connectable && !S_ISDIR(inode->i_mode); |
| 627 | int len = encode_parent ? 6 : 3; |
| 628 | u64 nodeid; |
| 629 | u32 generation; |
| 630 | |
| 631 | if (*max_len < len) |
| 632 | return 255; |
| 633 | |
| 634 | nodeid = get_fuse_inode(inode)->nodeid; |
| 635 | generation = inode->i_generation; |
| 636 | |
| 637 | fh[0] = (u32)(nodeid >> 32); |
| 638 | fh[1] = (u32)(nodeid & 0xffffffff); |
| 639 | fh[2] = generation; |
| 640 | |
| 641 | if (encode_parent) { |
| 642 | struct inode *parent; |
| 643 | |
| 644 | spin_lock(&dentry->d_lock); |
| 645 | parent = dentry->d_parent->d_inode; |
| 646 | nodeid = get_fuse_inode(parent)->nodeid; |
| 647 | generation = parent->i_generation; |
| 648 | spin_unlock(&dentry->d_lock); |
| 649 | |
| 650 | fh[3] = (u32)(nodeid >> 32); |
| 651 | fh[4] = (u32)(nodeid & 0xffffffff); |
| 652 | fh[5] = generation; |
| 653 | } |
| 654 | |
| 655 | *max_len = len; |
| 656 | return encode_parent ? 0x82 : 0x81; |
| 657 | } |
| 658 | |
| 659 | static struct dentry *fuse_fh_to_dentry(struct super_block *sb, |
| 660 | struct fid *fid, int fh_len, int fh_type) |
| 661 | { |
| 662 | struct fuse_inode_handle handle; |
| 663 | |
| 664 | if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3) |
| 665 | return NULL; |
| 666 | |
| 667 | handle.nodeid = (u64) fid->raw[0] << 32; |
| 668 | handle.nodeid |= (u64) fid->raw[1]; |
| 669 | handle.generation = fid->raw[2]; |
| 670 | return fuse_get_dentry(sb, &handle); |
| 671 | } |
| 672 | |
| 673 | static struct dentry *fuse_fh_to_parent(struct super_block *sb, |
| 674 | struct fid *fid, int fh_len, int fh_type) |
| 675 | { |
| 676 | struct fuse_inode_handle parent; |
| 677 | |
| 678 | if (fh_type != 0x82 || fh_len < 6) |
| 679 | return NULL; |
| 680 | |
| 681 | parent.nodeid = (u64) fid->raw[3] << 32; |
| 682 | parent.nodeid |= (u64) fid->raw[4]; |
| 683 | parent.generation = fid->raw[5]; |
| 684 | return fuse_get_dentry(sb, &parent); |
| 685 | } |
| 686 | |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 687 | static struct dentry *fuse_get_parent(struct dentry *child) |
| 688 | { |
| 689 | struct inode *child_inode = child->d_inode; |
| 690 | struct fuse_conn *fc = get_fuse_conn(child_inode); |
| 691 | struct inode *inode; |
| 692 | struct dentry *parent; |
| 693 | struct fuse_entry_out outarg; |
| 694 | struct qstr name; |
| 695 | int err; |
| 696 | |
| 697 | if (!fc->export_support) |
| 698 | return ERR_PTR(-ESTALE); |
| 699 | |
| 700 | name.len = 2; |
| 701 | name.name = ".."; |
| 702 | err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode), |
| 703 | &name, &outarg, &inode); |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 704 | if (err) { |
| 705 | if (err == -ENOENT) |
| 706 | return ERR_PTR(-ESTALE); |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 707 | return ERR_PTR(err); |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 708 | } |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 709 | |
| 710 | parent = d_obtain_alias(inode); |
| 711 | if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) { |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 712 | parent->d_op = &fuse_dentry_operations; |
| 713 | fuse_invalidate_entry_cache(parent); |
| 714 | } |
| 715 | |
| 716 | return parent; |
| 717 | } |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 718 | |
| 719 | static const struct export_operations fuse_export_operations = { |
| 720 | .fh_to_dentry = fuse_fh_to_dentry, |
| 721 | .fh_to_parent = fuse_fh_to_parent, |
| 722 | .encode_fh = fuse_encode_fh, |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 723 | .get_parent = fuse_get_parent, |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 724 | }; |
| 725 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 726 | static const struct super_operations fuse_super_operations = { |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 727 | .alloc_inode = fuse_alloc_inode, |
| 728 | .destroy_inode = fuse_destroy_inode, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 729 | .clear_inode = fuse_clear_inode, |
Miklos Szeredi | ead5f0b | 2007-05-23 13:57:54 -0700 | [diff] [blame] | 730 | .drop_inode = generic_delete_inode, |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 731 | .remount_fs = fuse_remount_fs, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 732 | .put_super = fuse_put_super, |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 733 | .umount_begin = fuse_umount_begin, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 734 | .statfs = fuse_statfs, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 735 | .show_options = fuse_show_options, |
| 736 | }; |
| 737 | |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 738 | static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) |
| 739 | { |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 740 | struct fuse_init_out *arg = &req->misc.init_out; |
| 741 | |
| 742 | if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) |
| 743 | fc->conn_error = 1; |
| 744 | else { |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 745 | unsigned long ra_pages; |
| 746 | |
Csaba Henk | 7a6d3c8 | 2009-07-01 17:28:41 -0700 | [diff] [blame^] | 747 | if (arg->minor >= 13) { |
| 748 | if (arg->max_background) |
| 749 | fc->max_background = arg->max_background; |
| 750 | if (arg->congestion_threshold) |
| 751 | fc->congestion_threshold = arg->congestion_threshold; |
| 752 | } |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 753 | if (arg->minor >= 6) { |
| 754 | ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; |
| 755 | if (arg->flags & FUSE_ASYNC_READ) |
| 756 | fc->async_read = 1; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 757 | if (!(arg->flags & FUSE_POSIX_LOCKS)) |
| 758 | fc->no_lock = 1; |
Miklos Szeredi | 6ff958e | 2007-10-18 03:07:02 -0700 | [diff] [blame] | 759 | if (arg->flags & FUSE_ATOMIC_O_TRUNC) |
| 760 | fc->atomic_o_trunc = 1; |
Miklos Szeredi | 33670fa | 2008-07-25 01:49:02 -0700 | [diff] [blame] | 761 | if (arg->minor >= 9) { |
| 762 | /* LOOKUP has dependency on proto version */ |
| 763 | if (arg->flags & FUSE_EXPORT_SUPPORT) |
| 764 | fc->export_support = 1; |
| 765 | } |
Miklos Szeredi | 78bb6cb | 2008-05-12 14:02:32 -0700 | [diff] [blame] | 766 | if (arg->flags & FUSE_BIG_WRITES) |
| 767 | fc->big_writes = 1; |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 768 | if (arg->flags & FUSE_DONT_MASK) |
| 769 | fc->dont_mask = 1; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 770 | } else { |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 771 | ra_pages = fc->max_read / PAGE_CACHE_SIZE; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 772 | fc->no_lock = 1; |
| 773 | } |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 774 | |
| 775 | fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 776 | fc->minor = arg->minor; |
| 777 | fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; |
Miklos Szeredi | f948d56 | 2008-06-17 18:05:40 +0200 | [diff] [blame] | 778 | fc->max_write = max_t(unsigned, 4096, fc->max_write); |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 779 | fc->conn_init = 1; |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 780 | } |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 781 | fc->blocked = 0; |
| 782 | wake_up_all(&fc->blocked_waitq); |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 785 | static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 786 | { |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 787 | struct fuse_init_in *arg = &req->misc.init_in; |
Miklos Szeredi | 095da6c | 2006-01-16 22:14:52 -0800 | [diff] [blame] | 788 | |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 789 | arg->major = FUSE_KERNEL_VERSION; |
| 790 | arg->minor = FUSE_KERNEL_MINOR_VERSION; |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 791 | arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; |
Miklos Szeredi | 78bb6cb | 2008-05-12 14:02:32 -0700 | [diff] [blame] | 792 | arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC | |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 793 | FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK; |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 794 | req->in.h.opcode = FUSE_INIT; |
| 795 | req->in.numargs = 1; |
| 796 | req->in.args[0].size = sizeof(*arg); |
| 797 | req->in.args[0].value = arg; |
| 798 | req->out.numargs = 1; |
| 799 | /* Variable length arguement used for backward compatibility |
| 800 | with interface version < 7.5. Rest of init_out is zeroed |
| 801 | by do_get_request(), so a short reply is not a problem */ |
| 802 | req->out.argvar = 1; |
| 803 | req->out.args[0].size = sizeof(struct fuse_init_out); |
| 804 | req->out.args[0].value = &req->misc.init_out; |
| 805 | req->end = process_init_reply; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 806 | fuse_request_send_background(fc, req); |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Tejun Heo | 43901aa | 2008-11-26 12:03:56 +0100 | [diff] [blame] | 809 | static void fuse_free_conn(struct fuse_conn *fc) |
| 810 | { |
| 811 | kfree(fc); |
| 812 | } |
| 813 | |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 814 | static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb) |
| 815 | { |
| 816 | int err; |
| 817 | |
| 818 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 819 | fc->bdi.unplug_io_fn = default_unplug_io_fn; |
| 820 | /* fuse does it's own writeback accounting */ |
| 821 | fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB; |
| 822 | |
| 823 | err = bdi_init(&fc->bdi); |
| 824 | if (err) |
| 825 | return err; |
| 826 | |
| 827 | fc->bdi_initialized = 1; |
| 828 | |
| 829 | if (sb->s_bdev) { |
| 830 | err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk", |
| 831 | MAJOR(fc->dev), MINOR(fc->dev)); |
| 832 | } else { |
| 833 | err = bdi_register_dev(&fc->bdi, fc->dev); |
| 834 | } |
| 835 | |
| 836 | if (err) |
| 837 | return err; |
| 838 | |
| 839 | /* |
| 840 | * For a single fuse filesystem use max 1% of dirty + |
| 841 | * writeback threshold. |
| 842 | * |
| 843 | * This gives about 1M of write buffer for memory maps on a |
| 844 | * machine with 1G and 10% dirty_ratio, which should be more |
| 845 | * than enough. |
| 846 | * |
| 847 | * Privileged users can raise it by writing to |
| 848 | * |
| 849 | * /sys/class/bdi/<bdi>/max_ratio |
| 850 | */ |
| 851 | bdi_set_max_ratio(&fc->bdi, 1); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 856 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) |
| 857 | { |
| 858 | struct fuse_conn *fc; |
| 859 | struct inode *root; |
| 860 | struct fuse_mount_data d; |
| 861 | struct file *file; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 862 | struct dentry *root_dentry; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 863 | struct fuse_req *init_req; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 864 | int err; |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 865 | int is_bdev = sb->s_bdev != NULL; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 866 | |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 867 | err = -EINVAL; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 868 | if (sb->s_flags & MS_MANDLOCK) |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 869 | goto err; |
Miklos Szeredi | 7142125 | 2006-06-25 05:48:52 -0700 | [diff] [blame] | 870 | |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 871 | if (!parse_fuse_opt((char *) data, &d, is_bdev)) |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 872 | goto err; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 873 | |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 874 | if (is_bdev) { |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 875 | #ifdef CONFIG_BLOCK |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 876 | err = -EINVAL; |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 877 | if (!sb_set_blocksize(sb, d.blksize)) |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 878 | goto err; |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 879 | #endif |
Miklos Szeredi | d809161 | 2006-12-06 20:35:48 -0800 | [diff] [blame] | 880 | } else { |
| 881 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 882 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 883 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 884 | sb->s_magic = FUSE_SUPER_MAGIC; |
| 885 | sb->s_op = &fuse_super_operations; |
| 886 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Miklos Szeredi | dbd561d | 2008-07-25 01:49:00 -0700 | [diff] [blame] | 887 | sb->s_export_op = &fuse_export_operations; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 888 | |
| 889 | file = fget(d.fd); |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 890 | err = -EINVAL; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 891 | if (!file) |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 892 | goto err; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 893 | |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 894 | if (file->f_op != &fuse_dev_operations) |
| 895 | goto err_fput; |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 896 | |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 897 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 898 | err = -ENOMEM; |
| 899 | if (!fc) |
| 900 | goto err_fput; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 901 | |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 902 | fuse_conn_init(fc); |
| 903 | |
| 904 | fc->dev = sb->s_dev; |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 905 | fc->sb = sb; |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 906 | err = fuse_bdi_init(fc, sb); |
| 907 | if (err) |
| 908 | goto err_put_conn; |
Tejun Heo | 0d179aa | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 909 | |
Miklos Szeredi | e0a43dd | 2009-06-30 20:12:23 +0200 | [diff] [blame] | 910 | /* Handle umasking inside the fuse code */ |
| 911 | if (sb->s_flags & MS_POSIXACL) |
| 912 | fc->dont_mask = 1; |
| 913 | sb->s_flags |= MS_POSIXACL; |
| 914 | |
Tejun Heo | 43901aa | 2008-11-26 12:03:56 +0100 | [diff] [blame] | 915 | fc->release = fuse_free_conn; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 916 | fc->flags = d.flags; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 917 | fc->user_id = d.user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 918 | fc->group_id = d.group_id; |
Miklos Szeredi | f948d56 | 2008-06-17 18:05:40 +0200 | [diff] [blame] | 919 | fc->max_read = max_t(unsigned, 4096, d.max_read); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 920 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 921 | /* Used by get_root_inode() */ |
| 922 | sb->s_fs_info = fc; |
| 923 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 924 | err = -ENOMEM; |
Tejun Heo | b93f858 | 2008-11-26 12:03:55 +0100 | [diff] [blame] | 925 | root = fuse_get_root_inode(sb, d.rootmode); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 926 | if (!root) |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 927 | goto err_put_conn; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 928 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 929 | root_dentry = d_alloc_root(root); |
| 930 | if (!root_dentry) { |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 931 | iput(root); |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 932 | goto err_put_conn; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 933 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 934 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 935 | init_req = fuse_request_alloc(); |
| 936 | if (!init_req) |
| 937 | goto err_put_root; |
| 938 | |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 939 | if (is_bdev) { |
| 940 | fc->destroy_req = fuse_request_alloc(); |
| 941 | if (!fc->destroy_req) |
Julia Lawall | 17e18ab | 2008-10-16 16:08:56 +0200 | [diff] [blame] | 942 | goto err_free_init_req; |
Miklos Szeredi | 0ec7ca4 | 2006-12-06 20:35:52 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 945 | mutex_lock(&fuse_mutex); |
Miklos Szeredi | 8aa09a5 | 2006-04-26 10:49:16 +0200 | [diff] [blame] | 946 | err = -EINVAL; |
| 947 | if (file->private_data) |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 948 | goto err_unlock; |
Miklos Szeredi | 8aa09a5 | 2006-04-26 10:49:16 +0200 | [diff] [blame] | 949 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 950 | err = fuse_ctl_add_conn(fc); |
| 951 | if (err) |
| 952 | goto err_unlock; |
| 953 | |
| 954 | list_add_tail(&fc->entry, &fuse_conn_list); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 955 | sb->s_root = root_dentry; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 956 | fc->connected = 1; |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 957 | file->private_data = fuse_conn_get(fc); |
| 958 | mutex_unlock(&fuse_mutex); |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 959 | /* |
| 960 | * atomic_dec_and_test() in fput() provides the necessary |
| 961 | * memory barrier for file->private_data to be visible on all |
| 962 | * CPUs after this |
| 963 | */ |
| 964 | fput(file); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 965 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 966 | fuse_send_init(fc, init_req); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 967 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 968 | return 0; |
| 969 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 970 | err_unlock: |
| 971 | mutex_unlock(&fuse_mutex); |
Julia Lawall | 17e18ab | 2008-10-16 16:08:56 +0200 | [diff] [blame] | 972 | err_free_init_req: |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 973 | fuse_request_free(init_req); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 974 | err_put_root: |
| 975 | dput(root_dentry); |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 976 | err_put_conn: |
Tejun Heo | a325f9b | 2009-04-14 10:54:52 +0900 | [diff] [blame] | 977 | fuse_bdi_destroy(fc); |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 978 | fuse_conn_put(fc); |
Miklos Szeredi | c2b8f00 | 2009-01-26 15:00:58 +0100 | [diff] [blame] | 979 | err_fput: |
| 980 | fput(file); |
| 981 | err: |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 982 | return err; |
| 983 | } |
| 984 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 985 | static int fuse_get_sb(struct file_system_type *fs_type, |
| 986 | int flags, const char *dev_name, |
| 987 | void *raw_data, struct vfsmount *mnt) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 988 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 989 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 990 | } |
| 991 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 992 | static void fuse_kill_sb_anon(struct super_block *sb) |
| 993 | { |
| 994 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 995 | |
| 996 | if (fc) { |
| 997 | down_write(&fc->killsb); |
| 998 | fc->sb = NULL; |
| 999 | up_write(&fc->killsb); |
| 1000 | } |
| 1001 | |
| 1002 | kill_anon_super(sb); |
| 1003 | } |
| 1004 | |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1005 | static struct file_system_type fuse_fs_type = { |
| 1006 | .owner = THIS_MODULE, |
| 1007 | .name = "fuse", |
Miklos Szeredi | 79c0b2d | 2007-05-08 00:25:43 -0700 | [diff] [blame] | 1008 | .fs_flags = FS_HAS_SUBTYPE, |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1009 | .get_sb = fuse_get_sb, |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1010 | .kill_sb = fuse_kill_sb_anon, |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1011 | }; |
| 1012 | |
| 1013 | #ifdef CONFIG_BLOCK |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1014 | static int fuse_get_sb_blk(struct file_system_type *fs_type, |
| 1015 | int flags, const char *dev_name, |
| 1016 | void *raw_data, struct vfsmount *mnt) |
| 1017 | { |
| 1018 | return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super, |
| 1019 | mnt); |
| 1020 | } |
| 1021 | |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1022 | static void fuse_kill_sb_blk(struct super_block *sb) |
| 1023 | { |
| 1024 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 1025 | |
| 1026 | if (fc) { |
| 1027 | down_write(&fc->killsb); |
| 1028 | fc->sb = NULL; |
| 1029 | up_write(&fc->killsb); |
| 1030 | } |
| 1031 | |
| 1032 | kill_block_super(sb); |
| 1033 | } |
| 1034 | |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1035 | static struct file_system_type fuseblk_fs_type = { |
| 1036 | .owner = THIS_MODULE, |
| 1037 | .name = "fuseblk", |
| 1038 | .get_sb = fuse_get_sb_blk, |
John Muir | 3b463ae | 2009-05-31 11:13:57 -0400 | [diff] [blame] | 1039 | .kill_sb = fuse_kill_sb_blk, |
Alexey Dobriyan | edad01e | 2007-06-16 10:16:05 -0700 | [diff] [blame] | 1040 | .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE, |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1041 | }; |
| 1042 | |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1043 | static inline int register_fuseblk(void) |
| 1044 | { |
| 1045 | return register_filesystem(&fuseblk_fs_type); |
| 1046 | } |
| 1047 | |
| 1048 | static inline void unregister_fuseblk(void) |
| 1049 | { |
| 1050 | unregister_filesystem(&fuseblk_fs_type); |
| 1051 | } |
| 1052 | #else |
| 1053 | static inline int register_fuseblk(void) |
| 1054 | { |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
| 1058 | static inline void unregister_fuseblk(void) |
| 1059 | { |
| 1060 | } |
| 1061 | #endif |
| 1062 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 1063 | static void fuse_inode_init_once(void *foo) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1064 | { |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 1065 | struct inode *inode = foo; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1066 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 1067 | inode_init_once(inode); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | static int __init fuse_fs_init(void) |
| 1071 | { |
| 1072 | int err; |
| 1073 | |
| 1074 | err = register_filesystem(&fuse_fs_type); |
| 1075 | if (err) |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1076 | goto out; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1077 | |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1078 | err = register_fuseblk(); |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1079 | if (err) |
| 1080 | goto out_unreg; |
| 1081 | |
| 1082 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
| 1083 | sizeof(struct fuse_inode), |
| 1084 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1085 | fuse_inode_init_once); |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1086 | err = -ENOMEM; |
| 1087 | if (!fuse_inode_cachep) |
| 1088 | goto out_unreg2; |
| 1089 | |
| 1090 | return 0; |
| 1091 | |
| 1092 | out_unreg2: |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1093 | unregister_fuseblk(); |
Miklos Szeredi | d6392f8 | 2006-12-06 20:35:44 -0800 | [diff] [blame] | 1094 | out_unreg: |
| 1095 | unregister_filesystem(&fuse_fs_type); |
| 1096 | out: |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | static void fuse_fs_cleanup(void) |
| 1101 | { |
| 1102 | unregister_filesystem(&fuse_fs_type); |
Miklos Szeredi | 875d95e | 2006-12-06 20:35:54 -0800 | [diff] [blame] | 1103 | unregister_fuseblk(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1104 | kmem_cache_destroy(fuse_inode_cachep); |
| 1105 | } |
| 1106 | |
Greg Kroah-Hartman | 5c89e17 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 1107 | static struct kobject *fuse_kobj; |
| 1108 | static struct kobject *connections_kobj; |
| 1109 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1110 | static int fuse_sysfs_init(void) |
| 1111 | { |
| 1112 | int err; |
| 1113 | |
Greg Kroah-Hartman | 00d2666 | 2007-10-29 14:17:23 -0600 | [diff] [blame] | 1114 | fuse_kobj = kobject_create_and_add("fuse", fs_kobj); |
Greg Kroah-Hartman | 5c89e17 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 1115 | if (!fuse_kobj) { |
| 1116 | err = -ENOMEM; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1117 | goto out_err; |
Greg Kroah-Hartman | 5c89e17 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 1118 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1119 | |
Greg Kroah-Hartman | 5c89e17 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 1120 | connections_kobj = kobject_create_and_add("connections", fuse_kobj); |
| 1121 | if (!connections_kobj) { |
| 1122 | err = -ENOMEM; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1123 | goto out_fuse_unregister; |
Greg Kroah-Hartman | 5c89e17 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 1124 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1125 | |
| 1126 | return 0; |
| 1127 | |
| 1128 | out_fuse_unregister: |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1129 | kobject_put(fuse_kobj); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1130 | out_err: |
| 1131 | return err; |
| 1132 | } |
| 1133 | |
| 1134 | static void fuse_sysfs_cleanup(void) |
| 1135 | { |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 1136 | kobject_put(connections_kobj); |
| 1137 | kobject_put(fuse_kobj); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1140 | static int __init fuse_init(void) |
| 1141 | { |
| 1142 | int res; |
| 1143 | |
Miklos Szeredi | 1729a16 | 2008-11-26 12:03:54 +0100 | [diff] [blame] | 1144 | printk(KERN_INFO "fuse init (API version %i.%i)\n", |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1145 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1146 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1147 | INIT_LIST_HEAD(&fuse_conn_list); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1148 | res = fuse_fs_init(); |
| 1149 | if (res) |
| 1150 | goto err; |
| 1151 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1152 | res = fuse_dev_init(); |
| 1153 | if (res) |
| 1154 | goto err_fs_cleanup; |
| 1155 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1156 | res = fuse_sysfs_init(); |
| 1157 | if (res) |
| 1158 | goto err_dev_cleanup; |
| 1159 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1160 | res = fuse_ctl_init(); |
| 1161 | if (res) |
| 1162 | goto err_sysfs_cleanup; |
| 1163 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1164 | return 0; |
| 1165 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1166 | err_sysfs_cleanup: |
| 1167 | fuse_sysfs_cleanup(); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1168 | err_dev_cleanup: |
| 1169 | fuse_dev_cleanup(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1170 | err_fs_cleanup: |
| 1171 | fuse_fs_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1172 | err: |
| 1173 | return res; |
| 1174 | } |
| 1175 | |
| 1176 | static void __exit fuse_exit(void) |
| 1177 | { |
| 1178 | printk(KERN_DEBUG "fuse exit\n"); |
| 1179 | |
Miklos Szeredi | bafa965 | 2006-06-25 05:48:51 -0700 | [diff] [blame] | 1180 | fuse_ctl_cleanup(); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 1181 | fuse_sysfs_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1182 | fuse_fs_cleanup(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 1183 | fuse_dev_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | module_init(fuse_init); |
| 1187 | module_exit(fuse_exit); |