Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 1 | /* |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 9 | #include "fuse_i.h" |
| 10 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 12 | #include <linux/slab.h> |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 13 | #include <linux/file.h> |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 14 | #include <linux/mount.h> |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 16 | #include <linux/module.h> |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 17 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 18 | #include <linux/moduleparam.h> |
Miklos Szeredi | 8ec48ec | 2004-04-19 10:24:41 +0000 | [diff] [blame] | 19 | #include <linux/parser.h> |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 20 | #include <linux/statfs.h> |
Miklos Szeredi | 8ec48ec | 2004-04-19 10:24:41 +0000 | [diff] [blame] | 21 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 22 | #include <linux/proc_fs.h> |
Miklos Szeredi | 8ec48ec | 2004-04-19 10:24:41 +0000 | [diff] [blame] | 23 | #include "compat/parser.h" |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 24 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 26 | static kmem_cache_t *fuse_inode_cachep; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 27 | static int mount_count; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 28 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 29 | static int user_allow_other; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 30 | static int mount_max = 1000; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 31 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 32 | module_param(user_allow_other, int, 0644); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 33 | module_param(mount_max, int, 0644); |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 34 | #else |
| 35 | MODULE_PARM(user_allow_other, "i"); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 36 | MODULE_PARM(mount_max, "i"); |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 37 | #endif |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 38 | MODULE_PARM_DESC(user_allow_other, "Allow non root user to specify the \"allow_other\" or \"allow_root\" mount options"); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 39 | MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)"); |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 40 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 41 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 42 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 43 | #ifndef KERNEL_2_6 |
| 44 | #define kstatfs statfs |
| 45 | #endif |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 46 | #ifndef FS_SAFE |
| 47 | #define FS_SAFE 0 |
| 48 | #endif |
Miklos Szeredi | b1ab8b5 | 2004-11-01 10:57:41 +0000 | [diff] [blame] | 49 | #ifndef MAX_LFS_FILESIZE |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 50 | #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) |
Miklos Szeredi | b1ab8b5 | 2004-11-01 10:57:41 +0000 | [diff] [blame] | 51 | #endif |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 52 | struct fuse_mount_data { |
| 53 | int fd; |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 54 | unsigned rootmode; |
| 55 | unsigned uid; |
| 56 | unsigned flags; |
| 57 | unsigned max_read; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 58 | }; |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 59 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 60 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 61 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 62 | struct inode *inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 63 | struct fuse_inode *fi; |
| 64 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 65 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); |
| 66 | if (!inode) |
| 67 | return NULL; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 68 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 69 | #ifndef KERNEL_2_6 |
| 70 | inode->u.generic_ip = NULL; |
| 71 | #endif |
| 72 | |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 73 | fi = get_fuse_inode(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 74 | memset(fi, 0, sizeof(*fi)); |
| 75 | fi->forget_req = fuse_request_alloc(); |
| 76 | if (!fi->forget_req) { |
| 77 | kmem_cache_free(fuse_inode_cachep, inode); |
| 78 | return NULL; |
| 79 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 80 | |
| 81 | return inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 84 | static void fuse_destroy_inode(struct inode *inode) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 85 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 86 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 87 | if (fi->forget_req) |
| 88 | fuse_request_free(fi->forget_req); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 89 | kmem_cache_free(fuse_inode_cachep, inode); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 92 | static void fuse_read_inode(struct inode *inode) |
| 93 | { |
Miklos Szeredi | 90d8bef | 2001-10-26 14:55:42 +0000 | [diff] [blame] | 94 | /* No op */ |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 97 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
| 98 | unsigned long nodeid, int version) |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 99 | { |
| 100 | struct fuse_forget_in *inarg = &req->misc.forget_in; |
| 101 | inarg->version = version; |
| 102 | req->in.h.opcode = FUSE_FORGET; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 103 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 104 | req->in.numargs = 1; |
| 105 | req->in.args[0].size = sizeof(struct fuse_forget_in); |
| 106 | req->in.args[0].value = inarg; |
| 107 | request_send_noreply(fc, req); |
| 108 | } |
| 109 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 110 | static void fuse_clear_inode(struct inode *inode) |
| 111 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 112 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 113 | if (fc) { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 114 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 115 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, inode->i_version); |
| 116 | fi->forget_req = NULL; |
Miklos Szeredi | d3dd2d5 | 2004-06-22 18:46:02 +0000 | [diff] [blame] | 117 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 120 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) |
| 121 | { |
| 122 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) |
| 123 | #ifdef KERNEL_2_6 |
| 124 | invalidate_inode_pages(inode->i_mapping); |
| 125 | #else |
| 126 | invalidate_inode_pages(inode); |
| 127 | #endif |
| 128 | |
| 129 | inode->i_ino = attr->ino; |
| 130 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); |
| 131 | inode->i_nlink = attr->nlink; |
| 132 | inode->i_uid = attr->uid; |
| 133 | inode->i_gid = attr->gid; |
| 134 | i_size_write(inode, attr->size); |
| 135 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 136 | inode->i_blocks = attr->blocks; |
| 137 | #ifdef KERNEL_2_6 |
| 138 | inode->i_atime.tv_sec = attr->atime; |
| 139 | inode->i_atime.tv_nsec = attr->atimensec; |
| 140 | inode->i_mtime.tv_sec = attr->mtime; |
| 141 | inode->i_mtime.tv_nsec = attr->mtimensec; |
| 142 | inode->i_ctime.tv_sec = attr->ctime; |
| 143 | inode->i_ctime.tv_nsec = attr->ctimensec; |
| 144 | #else |
| 145 | inode->i_atime = attr->atime; |
| 146 | inode->i_mtime = attr->mtime; |
| 147 | inode->i_ctime = attr->ctime; |
| 148 | #endif |
| 149 | } |
| 150 | |
| 151 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
| 152 | { |
| 153 | inode->i_mode = attr->mode & S_IFMT; |
| 154 | i_size_write(inode, attr->size); |
| 155 | if (S_ISREG(inode->i_mode)) { |
| 156 | fuse_init_common(inode); |
| 157 | fuse_init_file_inode(inode); |
| 158 | } else if (S_ISDIR(inode->i_mode)) |
| 159 | fuse_init_dir(inode); |
| 160 | else if (S_ISLNK(inode->i_mode)) |
| 161 | fuse_init_symlink(inode); |
| 162 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 163 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 164 | fuse_init_common(inode); |
| 165 | init_special_inode(inode, inode->i_mode, |
| 166 | new_decode_dev(attr->rdev)); |
| 167 | } else { |
| 168 | /* Don't let user create weird files */ |
| 169 | inode->i_mode = S_IFREG; |
| 170 | fuse_init_common(inode); |
| 171 | fuse_init_file_inode(inode); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #ifdef KERNEL_2_6 |
| 176 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
| 177 | { |
| 178 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 179 | if (get_node_id(inode) == nodeid) |
| 180 | return 1; |
| 181 | else |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 186 | { |
| 187 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 188 | get_fuse_inode(inode)->nodeid = nodeid; |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
| 193 | int generation, struct fuse_attr *attr, int version) |
| 194 | { |
| 195 | struct inode *inode; |
| 196 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 197 | int retried = 0; |
| 198 | |
| 199 | retry: |
| 200 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 201 | if (!inode) |
| 202 | return NULL; |
| 203 | |
| 204 | if ((inode->i_state & I_NEW)) { |
| 205 | inode->i_generation = generation; |
| 206 | inode->i_data.backing_dev_info = &fc->bdi; |
| 207 | fuse_init_inode(inode, attr); |
| 208 | unlock_new_inode(inode); |
| 209 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
| 210 | BUG_ON(retried); |
| 211 | /* Inode has changed type, any I/O on the old should fail */ |
| 212 | make_bad_inode(inode); |
| 213 | iput(inode); |
| 214 | retried = 1; |
| 215 | goto retry; |
| 216 | } |
| 217 | |
| 218 | fuse_change_attributes(inode, attr); |
| 219 | inode->i_version = version; |
| 220 | return inode; |
| 221 | } |
| 222 | #else |
| 223 | static int fuse_inode_eq(struct inode *inode, unsigned long ino, void *_nodeidp){ |
| 224 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 225 | if (inode->u.generic_ip && get_node_id(inode) == nodeid) |
| 226 | return 1; |
| 227 | else |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
| 232 | int generation, struct fuse_attr *attr, int version) |
| 233 | { |
| 234 | struct inode *inode; |
| 235 | int retried = 0; |
| 236 | |
| 237 | retry: |
| 238 | inode = iget4(sb, attr->ino, fuse_inode_eq, &nodeid); |
| 239 | if (!inode) |
| 240 | return NULL; |
| 241 | |
| 242 | if (!inode->u.generic_ip) { |
| 243 | get_fuse_inode(inode)->nodeid = nodeid; |
| 244 | inode->u.generic_ip = inode; |
| 245 | inode->i_generation = generation; |
| 246 | fuse_init_inode(inode, attr); |
| 247 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
| 248 | BUG_ON(retried); |
| 249 | /* Inode has changed type, any I/O on the old should fail */ |
| 250 | remove_inode_hash(inode); |
| 251 | make_bad_inode(inode); |
| 252 | iput(inode); |
| 253 | retried = 1; |
| 254 | goto retry; |
| 255 | } |
| 256 | |
| 257 | fuse_change_attributes(inode, attr); |
| 258 | inode->i_version = version; |
| 259 | return inode; |
| 260 | } |
| 261 | #endif |
| 262 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 263 | static void fuse_put_super(struct super_block *sb) |
| 264 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 265 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 266 | |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 267 | spin_lock(&fuse_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 268 | mount_count --; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 269 | fc->sb = NULL; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 270 | fc->uid = 0; |
| 271 | fc->flags = 0; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 272 | /* Flush all readers on this fs */ |
| 273 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 79b461a | 2003-03-10 09:35:34 +0000 | [diff] [blame] | 274 | fuse_release_conn(fc); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 275 | *get_fuse_conn_super_p(sb) = NULL; |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 276 | spin_unlock(&fuse_lock); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 279 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 280 | { |
| 281 | stbuf->f_type = FUSE_SUPER_MAGIC; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 282 | stbuf->f_bsize = attr->bsize; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 283 | stbuf->f_blocks = attr->blocks; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 284 | stbuf->f_bfree = attr->bfree; |
| 285 | stbuf->f_bavail = attr->bavail; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 286 | stbuf->f_files = attr->files; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 287 | stbuf->f_ffree = attr->ffree; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 288 | stbuf->f_namelen = attr->namelen; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 289 | /* fsid is left zero */ |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 292 | static int fuse_statfs(struct super_block *sb, struct kstatfs *buf) |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 293 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 294 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 295 | struct fuse_req *req; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 296 | struct fuse_statfs_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 297 | int err; |
| 298 | |
| 299 | req = fuse_get_request(fc); |
| 300 | if (!req) |
| 301 | return -ERESTARTSYS; |
| 302 | |
| 303 | req->in.numargs = 0; |
| 304 | req->in.h.opcode = FUSE_STATFS; |
| 305 | req->out.numargs = 1; |
| 306 | req->out.args[0].size = sizeof(outarg); |
| 307 | req->out.args[0].value = &outarg; |
| 308 | request_send(fc, req); |
| 309 | err = req->out.h.error; |
| 310 | if (!err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 311 | convert_fuse_statfs(buf, &outarg.st); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 312 | fuse_put_request(fc, req); |
| 313 | return err; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 316 | enum { |
| 317 | OPT_FD, |
| 318 | OPT_ROOTMODE, |
| 319 | OPT_UID, |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 320 | OPT_DEFAULT_PERMISSIONS, |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 321 | OPT_ALLOW_OTHER, |
| 322 | OPT_ALLOW_ROOT, |
| 323 | OPT_KERNEL_CACHE, |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 324 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 325 | OPT_LARGE_READ, |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 326 | #endif |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 327 | OPT_DIRECT_IO, |
| 328 | OPT_MAX_READ, |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 329 | OPT_ERR |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 330 | }; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 331 | |
| 332 | static match_table_t tokens = { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 333 | {OPT_FD, "fd=%u"}, |
| 334 | {OPT_ROOTMODE, "rootmode=%o"}, |
| 335 | {OPT_UID, "uid=%u"}, |
| 336 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 337 | {OPT_ALLOW_OTHER, "allow_other"}, |
| 338 | {OPT_ALLOW_ROOT, "allow_root"}, |
| 339 | {OPT_KERNEL_CACHE, "kernel_cache"}, |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 340 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 341 | {OPT_LARGE_READ, "large_read"}, |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 342 | #endif |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 343 | {OPT_DIRECT_IO, "direct_io"}, |
| 344 | {OPT_MAX_READ, "max_read=%u"}, |
| 345 | {OPT_ERR, NULL} |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) |
| 349 | { |
| 350 | char *p; |
| 351 | memset(d, 0, sizeof(struct fuse_mount_data)); |
| 352 | d->fd = -1; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 353 | d->max_read = ~0; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 354 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 355 | while ((p = strsep(&opt, ",")) != NULL) { |
| 356 | int token; |
| 357 | int value; |
| 358 | substring_t args[MAX_OPT_ARGS]; |
| 359 | if (!*p) |
| 360 | continue; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 361 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 362 | token = match_token(p, tokens, args); |
| 363 | switch (token) { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 364 | case OPT_FD: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 365 | if (match_int(&args[0], &value)) |
| 366 | return 0; |
| 367 | d->fd = value; |
| 368 | break; |
| 369 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 370 | case OPT_ROOTMODE: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 371 | if (match_octal(&args[0], &value)) |
| 372 | return 0; |
| 373 | d->rootmode = value; |
| 374 | break; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 375 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 376 | case OPT_UID: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 377 | if (match_int(&args[0], &value)) |
| 378 | return 0; |
| 379 | d->uid = value; |
| 380 | break; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 381 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 382 | case OPT_DEFAULT_PERMISSIONS: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 383 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 384 | break; |
| 385 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 386 | case OPT_ALLOW_OTHER: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 387 | d->flags |= FUSE_ALLOW_OTHER; |
| 388 | break; |
| 389 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 390 | case OPT_ALLOW_ROOT: |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 391 | d->flags |= FUSE_ALLOW_ROOT; |
| 392 | break; |
| 393 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 394 | case OPT_KERNEL_CACHE: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 395 | d->flags |= FUSE_KERNEL_CACHE; |
| 396 | break; |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 397 | |
Miklos Szeredi | 1bf53ee | 2004-09-13 11:47:59 +0000 | [diff] [blame] | 398 | #ifndef KERNEL_2_6 |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 399 | case OPT_LARGE_READ: |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 400 | d->flags |= FUSE_LARGE_READ; |
Miklos Szeredi | 1bf53ee | 2004-09-13 11:47:59 +0000 | [diff] [blame] | 401 | break; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 402 | #endif |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 403 | case OPT_DIRECT_IO: |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 404 | d->flags |= FUSE_DIRECT_IO; |
| 405 | break; |
| 406 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 407 | case OPT_MAX_READ: |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 408 | if (match_int(&args[0], &value)) |
| 409 | return 0; |
| 410 | d->max_read = value; |
| 411 | break; |
| 412 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 413 | default: |
| 414 | return 0; |
| 415 | } |
| 416 | } |
| 417 | if (d->fd == -1) |
| 418 | return 0; |
| 419 | |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 424 | { |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 425 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 426 | |
| 427 | seq_printf(m, ",uid=%u", fc->uid); |
| 428 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 429 | seq_puts(m, ",default_permissions"); |
| 430 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 431 | seq_puts(m, ",allow_other"); |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 432 | if (fc->flags & FUSE_ALLOW_ROOT) |
| 433 | seq_puts(m, ",allow_root"); |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 434 | if (fc->flags & FUSE_KERNEL_CACHE) |
| 435 | seq_puts(m, ",kernel_cache"); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 436 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 437 | if (fc->flags & FUSE_LARGE_READ) |
| 438 | seq_puts(m, ",large_read"); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 439 | #endif |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 440 | if (fc->flags & FUSE_DIRECT_IO) |
| 441 | seq_puts(m, ",direct_io"); |
| 442 | if (fc->max_read != ~0) |
| 443 | seq_printf(m, ",max_read=%u", fc->max_read); |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 447 | static void free_conn(struct fuse_conn *fc) |
| 448 | { |
| 449 | while (!list_empty(&fc->unused_list)) { |
| 450 | struct fuse_req *req; |
| 451 | req = list_entry(fc->unused_list.next, struct fuse_req, list); |
| 452 | list_del(&req->list); |
| 453 | fuse_request_free(req); |
| 454 | } |
| 455 | kfree(fc); |
| 456 | } |
| 457 | |
| 458 | /* Must be called with the fuse lock held */ |
| 459 | void fuse_release_conn(struct fuse_conn *fc) |
| 460 | { |
| 461 | if (!fc->sb && !fc->file) |
| 462 | free_conn(fc); |
| 463 | } |
| 464 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 465 | static struct fuse_conn *new_conn(void) |
| 466 | { |
| 467 | struct fuse_conn *fc; |
| 468 | |
| 469 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); |
| 470 | if (fc != NULL) { |
| 471 | int i; |
| 472 | memset(fc, 0, sizeof(*fc)); |
| 473 | fc->sb = NULL; |
| 474 | fc->file = NULL; |
| 475 | fc->flags = 0; |
| 476 | fc->uid = 0; |
| 477 | init_waitqueue_head(&fc->waitq); |
| 478 | INIT_LIST_HEAD(&fc->pending); |
| 479 | INIT_LIST_HEAD(&fc->processing); |
| 480 | INIT_LIST_HEAD(&fc->unused_list); |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 481 | sema_init(&fc->outstanding_sem, 0); |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 482 | for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) { |
| 483 | struct fuse_req *req = fuse_request_alloc(); |
| 484 | if (!req) { |
| 485 | free_conn(fc); |
| 486 | return NULL; |
| 487 | } |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 488 | list_add(&req->list, &fc->unused_list); |
| 489 | } |
Miklos Szeredi | b622019 | 2005-01-05 16:19:10 +0000 | [diff] [blame^] | 490 | #ifdef KERNEL_2_6_6_PLUS |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 491 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 492 | fc->bdi.unplug_io_fn = default_unplug_io_fn; |
| 493 | #endif |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 494 | fc->reqctr = 1; |
| 495 | } |
| 496 | return fc; |
| 497 | } |
| 498 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 499 | static struct fuse_conn *get_conn(struct file *file, struct super_block *sb) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 500 | { |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 501 | struct fuse_conn *fc; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 502 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 503 | if (file->f_op != &fuse_dev_operations) |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 504 | return NULL; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 505 | fc = new_conn(); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 506 | if (fc == NULL) |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 507 | return NULL; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 508 | spin_lock(&fuse_lock); |
| 509 | if (file->private_data) { |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame] | 510 | free_conn(fc); |
| 511 | fc = NULL; |
| 512 | } else { |
| 513 | file->private_data = fc; |
| 514 | fc->sb = sb; |
| 515 | fc->file = file; |
| 516 | } |
| 517 | spin_unlock(&fuse_lock); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 518 | return fc; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame] | 521 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 522 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 523 | struct fuse_attr attr; |
| 524 | memset(&attr, 0, sizeof(attr)); |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 525 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 526 | attr.mode = mode; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 527 | attr.ino = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 528 | return fuse_iget(sb, 1, 0, &attr, 0); |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 531 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 532 | static struct dentry *fuse_get_dentry(struct super_block *sb, void *vobjp) |
| 533 | { |
| 534 | __u32 *objp = vobjp; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 535 | unsigned long nodeid = objp[0]; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 536 | __u32 generation = objp[1]; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 537 | struct inode *inode; |
| 538 | struct dentry *entry; |
| 539 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 540 | if (nodeid == 0) |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 541 | return ERR_PTR(-ESTALE); |
| 542 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 543 | inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 544 | if (!inode || inode->i_generation != generation) |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 545 | return ERR_PTR(-ESTALE); |
| 546 | |
| 547 | entry = d_alloc_anon(inode); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 548 | if (!entry) { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 549 | iput(inode); |
| 550 | return ERR_PTR(-ENOMEM); |
| 551 | } |
| 552 | |
| 553 | return entry; |
| 554 | } |
| 555 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 556 | static int fuse_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len, |
| 557 | int connectable) |
| 558 | { |
| 559 | struct inode *inode = dentry->d_inode; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 560 | int len = *max_len; |
| 561 | int type = 1; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 562 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 563 | if (len < 2 || (connectable && len < 4)) |
| 564 | return 255; |
| 565 | |
| 566 | len = 2; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 567 | fh[0] = get_fuse_inode(inode)->nodeid; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 568 | fh[1] = inode->i_generation; |
| 569 | if (connectable && !S_ISDIR(inode->i_mode)) { |
| 570 | struct inode *parent; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 571 | |
| 572 | spin_lock(&dentry->d_lock); |
| 573 | parent = dentry->d_parent->d_inode; |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 574 | fh[2] = get_fuse_inode(parent)->nodeid; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 575 | fh[3] = parent->i_generation; |
| 576 | spin_unlock(&dentry->d_lock); |
| 577 | len = 4; |
| 578 | type = 2; |
| 579 | } |
| 580 | *max_len = len; |
| 581 | return type; |
| 582 | } |
| 583 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 584 | static struct export_operations fuse_export_operations = { |
| 585 | .get_dentry = fuse_get_dentry, |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 586 | .encode_fh = fuse_encode_fh, |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 587 | }; |
| 588 | #endif |
| 589 | |
| 590 | static struct super_operations fuse_super_operations = { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 591 | .alloc_inode = fuse_alloc_inode, |
| 592 | .destroy_inode = fuse_destroy_inode, |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 593 | .read_inode = fuse_read_inode, |
| 594 | .clear_inode = fuse_clear_inode, |
| 595 | .put_super = fuse_put_super, |
| 596 | .statfs = fuse_statfs, |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 597 | .show_options = fuse_show_options, |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 598 | }; |
| 599 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 600 | static int inc_mount_count(void) |
| 601 | { |
| 602 | int success = 0; |
| 603 | spin_lock(&fuse_lock); |
| 604 | mount_count ++; |
| 605 | if (mount_max == -1 || mount_count <= mount_max) |
| 606 | success = 1; |
| 607 | spin_unlock(&fuse_lock); |
| 608 | return success; |
| 609 | } |
| 610 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 611 | static int fuse_read_super(struct super_block *sb, void *data, int silent) |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 612 | { |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 613 | struct fuse_conn *fc; |
| 614 | struct inode *root; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 615 | struct fuse_mount_data d; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 616 | struct file *file; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 617 | int err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 618 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 619 | if (!parse_fuse_opt((char *) data, &d)) |
| 620 | return -EINVAL; |
| 621 | |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 622 | if (!user_allow_other && |
| 623 | (d.flags & (FUSE_ALLOW_OTHER | FUSE_ALLOW_ROOT)) && |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 624 | current->uid != 0) |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 625 | return -EPERM; |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 626 | |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 627 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 628 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 629 | sb->s_magic = FUSE_SUPER_MAGIC; |
| 630 | sb->s_op = &fuse_super_operations; |
| 631 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 632 | #ifdef KERNEL_2_6 |
| 633 | sb->s_export_op = &fuse_export_operations; |
| 634 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 635 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 636 | file = fget(d.fd); |
| 637 | if (!file) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 638 | return -EINVAL; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 639 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 640 | fc = get_conn(file, sb); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 641 | fput(file); |
| 642 | if (fc == NULL) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 643 | return -EINVAL; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 644 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 645 | fc->flags = d.flags; |
| 646 | fc->uid = d.uid; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 647 | fc->max_read = d.max_read; |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 648 | #ifdef KERNEL_2_6 |
| 649 | if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages) |
| 650 | fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE; |
| 651 | #endif |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 652 | fc->max_write = FUSE_MAX_IN / 2; |
Miklos Szeredi | aa63b6b | 2004-12-03 13:24:35 +0000 | [diff] [blame] | 653 | |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 654 | *get_fuse_conn_super_p(sb) = fc; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 655 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 656 | err = -ENFILE; |
| 657 | if (!inc_mount_count() && current->uid != 0) |
| 658 | goto err; |
| 659 | |
| 660 | err = -ENOMEM; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 661 | root = get_root_inode(sb, d.rootmode); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 662 | if (root == NULL) |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 663 | goto err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 664 | |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 665 | sb->s_root = d_alloc_root(root); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 666 | if (!sb->s_root) { |
| 667 | iput(root); |
| 668 | goto err; |
| 669 | } |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 670 | fuse_send_init(fc); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 671 | return 0; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 672 | |
| 673 | err: |
| 674 | spin_lock(&fuse_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 675 | mount_count --; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 676 | fc->sb = NULL; |
| 677 | fuse_release_conn(fc); |
| 678 | spin_unlock(&fuse_lock); |
Miklos Szeredi | 039322d | 2004-12-01 18:39:12 +0000 | [diff] [blame] | 679 | *get_fuse_conn_super_p(sb) = NULL; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 680 | return err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 683 | #ifdef KERNEL_2_6 |
| 684 | static struct super_block *fuse_get_sb(struct file_system_type *fs_type, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 685 | int flags, const char *dev_name, |
| 686 | void *raw_data) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 687 | { |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 688 | return get_sb_nodev(fs_type, flags, raw_data, fuse_read_super); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 689 | } |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 690 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 691 | static struct file_system_type fuse_fs_type = { |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 692 | .owner = THIS_MODULE, |
| 693 | .name = "fuse", |
| 694 | .get_sb = fuse_get_sb, |
| 695 | .kill_sb = kill_anon_super, |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 696 | #ifndef FUSE_MAINLINE |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 697 | .fs_flags = FS_SAFE, |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 698 | #endif |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 699 | }; |
| 700 | #else |
| 701 | static struct super_block *fuse_read_super_compat(struct super_block *sb, |
| 702 | void *data, int silent) |
| 703 | { |
| 704 | int err = fuse_read_super(sb, data, silent); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 705 | if (err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 706 | return NULL; |
| 707 | else |
| 708 | return sb; |
| 709 | } |
| 710 | |
| 711 | static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super_compat, 0); |
| 712 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 713 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 714 | static void fuse_inode_init_once(void * foo, kmem_cache_t * cachep, |
| 715 | unsigned long flags) |
| 716 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 717 | struct inode * inode = foo; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 718 | |
| 719 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 720 | SLAB_CTOR_CONSTRUCTOR) |
| 721 | inode_init_once(inode); |
| 722 | } |
| 723 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 724 | int fuse_fs_init(void) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 725 | { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 726 | int err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 727 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 728 | err = register_filesystem(&fuse_fs_type); |
| 729 | if (err) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 730 | printk("fuse: failed to register filesystem\n"); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 731 | else { |
| 732 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 733 | sizeof(struct inode) + sizeof(struct fuse_inode) , |
| 734 | 0, SLAB_HWCACHE_ALIGN, |
| 735 | fuse_inode_init_once, NULL); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 736 | if (!fuse_inode_cachep) { |
| 737 | unregister_filesystem(&fuse_fs_type); |
| 738 | err = -ENOMEM; |
| 739 | } |
| 740 | } |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 741 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 742 | return err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 745 | void fuse_fs_cleanup(void) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 746 | { |
| 747 | unregister_filesystem(&fuse_fs_type); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 748 | kmem_cache_destroy(fuse_inode_cachep); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 749 | } |