Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 2e6b6f2 | 2004-07-07 19:19:53 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 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 | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 12 | #include <linux/sched.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 13 | #include <linux/slab.h> |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 14 | #include <linux/file.h> |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 15 | #include <linux/mount.h> |
Miklos Szeredi | 60c2352 | 2002-10-24 09:19:43 +0000 | [diff] [blame] | 16 | #include <linux/proc_fs.h> |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 18 | #ifdef KERNEL_2_6 |
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 |
| 22 | #include "compat/parser.h" |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 23 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 24 | |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 25 | |
| 26 | static int user_allow_other; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 27 | static kmem_cache_t *fuse_inode_cachep; |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef KERNEL_2_6 |
| 30 | #include <linux/moduleparam.h> |
| 31 | module_param(user_allow_other, int, 0); |
| 32 | #else |
| 33 | MODULE_PARM(user_allow_other, "i"); |
| 34 | #endif |
| 35 | |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 36 | MODULE_PARM_DESC(user_allow_other, "Allow non root user to specify the \"allow_other\" or \"allow_root\" mount options"); |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 37 | |
| 38 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 39 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 40 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 41 | #ifndef KERNEL_2_6 |
| 42 | #define kstatfs statfs |
| 43 | #endif |
| 44 | |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 45 | #ifndef FS_SAFE |
| 46 | #define FS_SAFE 0 |
| 47 | #endif |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 48 | |
Miklos Szeredi | b1ab8b5 | 2004-11-01 10:57:41 +0000 | [diff] [blame] | 49 | #ifndef MAX_LFS_FILESIZE |
| 50 | #define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1) |
| 51 | #endif |
| 52 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 53 | struct fuse_mount_data { |
| 54 | int fd; |
| 55 | unsigned int rootmode; |
| 56 | unsigned int uid; |
| 57 | unsigned int flags; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 58 | unsigned int max_read; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 59 | }; |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 60 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 61 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 62 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 63 | struct inode *inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 64 | struct fuse_inode *fi; |
| 65 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 66 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); |
| 67 | if (!inode) |
| 68 | return NULL; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 69 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 70 | #ifndef KERNEL_2_6 |
| 71 | inode->u.generic_ip = NULL; |
| 72 | #endif |
| 73 | |
| 74 | fi = INO_FI(inode); |
| 75 | memset(fi, 0, sizeof(*fi)); |
| 76 | fi->forget_req = fuse_request_alloc(); |
| 77 | if (!fi->forget_req) { |
| 78 | kmem_cache_free(fuse_inode_cachep, inode); |
| 79 | return NULL; |
| 80 | } |
| 81 | init_rwsem(&fi->write_sem); |
| 82 | INIT_LIST_HEAD(&fi->write_files); |
| 83 | |
| 84 | return inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 87 | static void fuse_destroy_inode(struct inode *inode) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 88 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 89 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 90 | BUG_ON(!list_empty(&fi->write_files)); |
| 91 | if (fi->forget_req) |
| 92 | fuse_request_free(fi->forget_req); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 93 | kmem_cache_free(fuse_inode_cachep, inode); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 96 | static void fuse_read_inode(struct inode *inode) |
| 97 | { |
Miklos Szeredi | 90d8bef | 2001-10-26 14:55:42 +0000 | [diff] [blame] | 98 | /* No op */ |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 101 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
| 102 | unsigned long nodeid, int version) |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 103 | { |
| 104 | struct fuse_forget_in *inarg = &req->misc.forget_in; |
| 105 | inarg->version = version; |
| 106 | req->in.h.opcode = FUSE_FORGET; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 107 | req->in.h.nodeid = nodeid; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 108 | req->in.numargs = 1; |
| 109 | req->in.args[0].size = sizeof(struct fuse_forget_in); |
| 110 | req->in.args[0].value = inarg; |
| 111 | request_send_noreply(fc, req); |
| 112 | } |
| 113 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 114 | static void fuse_clear_inode(struct inode *inode) |
| 115 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 116 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 117 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 118 | if (fc) { |
| 119 | struct fuse_inode *fi = INO_FI(inode); |
| 120 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, inode->i_version); |
| 121 | fi->forget_req = NULL; |
Miklos Szeredi | d3dd2d5 | 2004-06-22 18:46:02 +0000 | [diff] [blame] | 122 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 125 | static void fuse_put_super(struct super_block *sb) |
| 126 | { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 127 | struct fuse_conn *fc = SB_FC(sb); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 128 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 129 | down(&fc->sb_sem); |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 130 | spin_lock(&fuse_lock); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 131 | fc->sb = NULL; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 132 | up(&fc->sb_sem); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 133 | fc->uid = 0; |
| 134 | fc->flags = 0; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 135 | /* Flush all readers on this fs */ |
| 136 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 79b461a | 2003-03-10 09:35:34 +0000 | [diff] [blame] | 137 | fuse_release_conn(fc); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 138 | SB_FC(sb) = NULL; |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 139 | spin_unlock(&fuse_lock); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 142 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 143 | { |
| 144 | stbuf->f_type = FUSE_SUPER_MAGIC; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 145 | stbuf->f_bsize = attr->bsize; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 146 | stbuf->f_blocks = attr->blocks; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 147 | stbuf->f_bfree = attr->bfree; |
| 148 | stbuf->f_bavail = attr->bavail; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 149 | stbuf->f_files = attr->files; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 150 | stbuf->f_ffree = attr->ffree; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 151 | stbuf->f_namelen = attr->namelen; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 152 | /* fsid is left zero */ |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 155 | static int fuse_statfs(struct super_block *sb, struct kstatfs *buf) |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 156 | { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 157 | struct fuse_conn *fc = SB_FC(sb); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 158 | struct fuse_req *req; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 159 | struct fuse_statfs_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 160 | int err; |
| 161 | |
| 162 | req = fuse_get_request(fc); |
| 163 | if (!req) |
| 164 | return -ERESTARTSYS; |
| 165 | |
| 166 | req->in.numargs = 0; |
| 167 | req->in.h.opcode = FUSE_STATFS; |
| 168 | req->out.numargs = 1; |
| 169 | req->out.args[0].size = sizeof(outarg); |
| 170 | req->out.args[0].value = &outarg; |
| 171 | request_send(fc, req); |
| 172 | err = req->out.h.error; |
| 173 | if (!err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 174 | convert_fuse_statfs(buf, &outarg.st); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 175 | fuse_put_request(fc, req); |
| 176 | return err; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 179 | enum { opt_fd, |
| 180 | opt_rootmode, |
| 181 | opt_uid, |
| 182 | opt_default_permissions, |
| 183 | opt_allow_other, |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 184 | opt_allow_root, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 185 | opt_kernel_cache, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 186 | opt_large_read, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 187 | opt_direct_io, |
| 188 | opt_max_read, |
| 189 | opt_err }; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 190 | |
| 191 | static match_table_t tokens = { |
| 192 | {opt_fd, "fd=%u"}, |
| 193 | {opt_rootmode, "rootmode=%o"}, |
| 194 | {opt_uid, "uid=%u"}, |
| 195 | {opt_default_permissions, "default_permissions"}, |
| 196 | {opt_allow_other, "allow_other"}, |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 197 | {opt_allow_root, "allow_root"}, |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 198 | {opt_kernel_cache, "kernel_cache"}, |
| 199 | {opt_large_read, "large_read"}, |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 200 | {opt_direct_io, "direct_io"}, |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 201 | {opt_max_read, "max_read=%u" }, |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 202 | {opt_err, NULL} |
| 203 | }; |
| 204 | |
| 205 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) |
| 206 | { |
| 207 | char *p; |
| 208 | memset(d, 0, sizeof(struct fuse_mount_data)); |
| 209 | d->fd = -1; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 210 | d->max_read = ~0; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 211 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 212 | while ((p = strsep(&opt, ",")) != NULL) { |
| 213 | int token; |
| 214 | int value; |
| 215 | substring_t args[MAX_OPT_ARGS]; |
| 216 | if (!*p) |
| 217 | continue; |
| 218 | |
| 219 | token = match_token(p, tokens, args); |
| 220 | switch (token) { |
| 221 | case opt_fd: |
| 222 | if (match_int(&args[0], &value)) |
| 223 | return 0; |
| 224 | d->fd = value; |
| 225 | break; |
| 226 | |
| 227 | case opt_rootmode: |
| 228 | if (match_octal(&args[0], &value)) |
| 229 | return 0; |
| 230 | d->rootmode = value; |
| 231 | break; |
| 232 | |
| 233 | case opt_uid: |
| 234 | if (match_int(&args[0], &value)) |
| 235 | return 0; |
| 236 | d->uid = value; |
| 237 | break; |
| 238 | |
| 239 | case opt_default_permissions: |
| 240 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 241 | break; |
| 242 | |
| 243 | case opt_allow_other: |
| 244 | d->flags |= FUSE_ALLOW_OTHER; |
| 245 | break; |
| 246 | |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 247 | case opt_allow_root: |
| 248 | d->flags |= FUSE_ALLOW_ROOT; |
| 249 | break; |
| 250 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 251 | case opt_kernel_cache: |
| 252 | d->flags |= FUSE_KERNEL_CACHE; |
| 253 | break; |
| 254 | |
| 255 | case opt_large_read: |
Miklos Szeredi | 1bf53ee | 2004-09-13 11:47:59 +0000 | [diff] [blame] | 256 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 257 | d->flags |= FUSE_LARGE_READ; |
Miklos Szeredi | 1bf53ee | 2004-09-13 11:47:59 +0000 | [diff] [blame] | 258 | #else |
| 259 | { |
| 260 | static int warned = 0; |
| 261 | if (!warned) { |
| 262 | printk("fuse: large_read option is deprecated for 2.6 kernels\n"); |
| 263 | warned = 1; |
| 264 | } |
| 265 | } |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 266 | #endif |
Miklos Szeredi | 1bf53ee | 2004-09-13 11:47:59 +0000 | [diff] [blame] | 267 | break; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 268 | |
| 269 | case opt_direct_io: |
| 270 | d->flags |= FUSE_DIRECT_IO; |
| 271 | break; |
| 272 | |
| 273 | case opt_max_read: |
| 274 | if (match_int(&args[0], &value)) |
| 275 | return 0; |
| 276 | d->max_read = value; |
| 277 | break; |
| 278 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 279 | default: |
| 280 | return 0; |
| 281 | } |
| 282 | } |
| 283 | if (d->fd == -1) |
| 284 | return 0; |
| 285 | |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 290 | { |
| 291 | struct fuse_conn *fc = SB_FC(mnt->mnt_sb); |
| 292 | |
| 293 | seq_printf(m, ",uid=%u", fc->uid); |
| 294 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 295 | seq_puts(m, ",default_permissions"); |
| 296 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 297 | seq_puts(m, ",allow_other"); |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 298 | if (fc->flags & FUSE_ALLOW_ROOT) |
| 299 | seq_puts(m, ",allow_root"); |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 300 | if (fc->flags & FUSE_KERNEL_CACHE) |
| 301 | seq_puts(m, ",kernel_cache"); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 302 | #ifndef KERNEL_2_6 |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 303 | if (fc->flags & FUSE_LARGE_READ) |
| 304 | seq_puts(m, ",large_read"); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 305 | #endif |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 306 | if (fc->flags & FUSE_DIRECT_IO) |
| 307 | seq_puts(m, ",direct_io"); |
| 308 | if (fc->max_read != ~0) |
| 309 | seq_printf(m, ",max_read=%u", fc->max_read); |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 313 | static void free_conn(struct fuse_conn *fc) |
| 314 | { |
| 315 | while (!list_empty(&fc->unused_list)) { |
| 316 | struct fuse_req *req; |
| 317 | req = list_entry(fc->unused_list.next, struct fuse_req, list); |
| 318 | list_del(&req->list); |
| 319 | fuse_request_free(req); |
| 320 | } |
| 321 | kfree(fc); |
| 322 | } |
| 323 | |
| 324 | /* Must be called with the fuse lock held */ |
| 325 | void fuse_release_conn(struct fuse_conn *fc) |
| 326 | { |
| 327 | if (!fc->sb && !fc->file) |
| 328 | free_conn(fc); |
| 329 | } |
| 330 | |
| 331 | |
| 332 | static struct fuse_conn *new_conn(void) |
| 333 | { |
| 334 | struct fuse_conn *fc; |
| 335 | |
| 336 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); |
| 337 | if (fc != NULL) { |
| 338 | int i; |
| 339 | memset(fc, 0, sizeof(*fc)); |
| 340 | fc->sb = NULL; |
| 341 | fc->file = NULL; |
| 342 | fc->flags = 0; |
| 343 | fc->uid = 0; |
| 344 | init_waitqueue_head(&fc->waitq); |
| 345 | INIT_LIST_HEAD(&fc->pending); |
| 346 | INIT_LIST_HEAD(&fc->processing); |
| 347 | INIT_LIST_HEAD(&fc->unused_list); |
| 348 | sema_init(&fc->unused_sem, FUSE_MAX_OUTSTANDING); |
| 349 | sema_init(&fc->sb_sem, 1); |
| 350 | for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) { |
| 351 | struct fuse_req *req = fuse_request_alloc(); |
| 352 | if (!req) { |
| 353 | free_conn(fc); |
| 354 | return NULL; |
| 355 | } |
| 356 | req->preallocated = 1; |
| 357 | list_add(&req->list, &fc->unused_list); |
| 358 | } |
| 359 | fc->reqctr = 1; |
| 360 | } |
| 361 | return fc; |
| 362 | } |
| 363 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 364 | 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] | 365 | { |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 366 | struct fuse_conn *fc; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 367 | struct inode *ino; |
| 368 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 369 | ino = file->f_dentry->d_inode; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 370 | if (!ino || !proc_fuse_dev || |
| 371 | strcmp(ino->i_sb->s_type->name, "proc") != 0 || |
| 372 | proc_fuse_dev->low_ino != ino->i_ino) { |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 373 | printk("FUSE: bad communication file descriptor\n"); |
| 374 | return NULL; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 375 | } |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 376 | fc = new_conn(); |
| 377 | if (fc == NULL) { |
| 378 | printk("FUSE: failed to allocate connection data\n"); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 379 | return NULL; |
| 380 | } |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 381 | spin_lock(&fuse_lock); |
| 382 | if (file->private_data) { |
| 383 | printk("fuse_read_super: connection already mounted\n"); |
| 384 | free_conn(fc); |
| 385 | fc = NULL; |
| 386 | } else { |
| 387 | file->private_data = fc; |
| 388 | fc->sb = sb; |
| 389 | fc->file = file; |
| 390 | } |
| 391 | spin_unlock(&fuse_lock); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 392 | return fc; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 395 | static struct inode *get_root_inode(struct super_block *sb, unsigned int mode) |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 396 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 397 | struct fuse_attr attr; |
| 398 | memset(&attr, 0, sizeof(attr)); |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 399 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 400 | attr.mode = mode; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 401 | attr.ino = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 402 | return fuse_iget(sb, 1, 0, &attr, 0); |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 405 | |
| 406 | #ifdef KERNEL_2_6 |
| 407 | |
| 408 | static struct dentry *fuse_get_dentry(struct super_block *sb, void *vobjp) |
| 409 | { |
| 410 | __u32 *objp = vobjp; |
| 411 | unsigned long ino = objp[0]; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 412 | __u32 generation = objp[1]; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 413 | struct inode *inode; |
| 414 | struct dentry *entry; |
| 415 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 416 | if (ino == 0) |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 417 | return ERR_PTR(-ESTALE); |
| 418 | |
| 419 | inode = ilookup(sb, ino); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 420 | if (!inode || inode->i_generation != generation) |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 421 | return ERR_PTR(-ESTALE); |
| 422 | |
| 423 | entry = d_alloc_anon(inode); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 424 | if (!entry) { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 425 | iput(inode); |
| 426 | return ERR_PTR(-ENOMEM); |
| 427 | } |
| 428 | |
| 429 | return entry; |
| 430 | } |
| 431 | |
| 432 | static struct export_operations fuse_export_operations = { |
| 433 | .get_dentry = fuse_get_dentry, |
| 434 | }; |
| 435 | #endif |
| 436 | |
| 437 | static struct super_operations fuse_super_operations = { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 438 | .alloc_inode = fuse_alloc_inode, |
| 439 | .destroy_inode = fuse_destroy_inode, |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 440 | .read_inode = fuse_read_inode, |
| 441 | .clear_inode = fuse_clear_inode, |
| 442 | .put_super = fuse_put_super, |
| 443 | .statfs = fuse_statfs, |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 444 | .show_options = fuse_show_options, |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 445 | }; |
| 446 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 447 | static int fuse_read_super(struct super_block *sb, void *data, int silent) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 448 | { |
| 449 | struct fuse_conn *fc; |
| 450 | struct inode *root; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 451 | struct fuse_mount_data d; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 452 | struct file *file; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 453 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 454 | if (!parse_fuse_opt((char *) data, &d)) |
| 455 | return -EINVAL; |
| 456 | |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 457 | if (!user_allow_other && |
| 458 | (d.flags & (FUSE_ALLOW_OTHER | FUSE_ALLOW_ROOT)) && |
Miklos Szeredi | acb4d36 | 2004-07-02 16:20:45 +0000 | [diff] [blame] | 459 | current->uid != 0) |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 460 | return -EPERM; |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 461 | |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 462 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 463 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 464 | sb->s_magic = FUSE_SUPER_MAGIC; |
| 465 | sb->s_op = &fuse_super_operations; |
| 466 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 467 | #ifdef KERNEL_2_6 |
| 468 | sb->s_export_op = &fuse_export_operations; |
| 469 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 470 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 471 | file = fget(d.fd); |
| 472 | if (!file) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 473 | return -EINVAL; |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 474 | |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 475 | fc = get_conn(file, sb); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 476 | fput(file); |
| 477 | if (fc == NULL) |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 478 | return -EINVAL; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 479 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 480 | fc->flags = d.flags; |
| 481 | fc->uid = d.uid; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 482 | fc->max_read = d.max_read; |
| 483 | fc->max_write = FUSE_MAX_IN / 2; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 484 | |
| 485 | /* fc is needed in fuse_init_file_inode which could be called |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 486 | from get_root_inode */ |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 487 | SB_FC(sb) = fc; |
| 488 | |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 489 | root = get_root_inode(sb, d.rootmode); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 490 | if (root == NULL) { |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 491 | printk("fuse_read_super: failed to get root inode\n"); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 492 | goto err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Miklos Szeredi | 79b52f6 | 2001-10-24 14:37:13 +0000 | [diff] [blame] | 495 | sb->s_root = d_alloc_root(root); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 496 | if (!sb->s_root) { |
| 497 | iput(root); |
| 498 | goto err; |
| 499 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 500 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 501 | return 0; |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 502 | |
| 503 | err: |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 504 | down(&fc->sb_sem); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 505 | spin_lock(&fuse_lock); |
| 506 | fc->sb = NULL; |
Miklos Szeredi | 3d60e76 | 2004-11-11 14:44:04 +0000 | [diff] [blame^] | 507 | up(&fc->sb_sem); |
Miklos Szeredi | ed62d86 | 2004-06-20 08:57:39 +0000 | [diff] [blame] | 508 | fuse_release_conn(fc); |
| 509 | spin_unlock(&fuse_lock); |
| 510 | SB_FC(sb) = NULL; |
| 511 | return -EINVAL; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 514 | #ifdef KERNEL_2_6 |
| 515 | 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] | 516 | int flags, const char *dev_name, |
| 517 | void *raw_data) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 518 | { |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 519 | return get_sb_nodev(fs_type, flags, raw_data, fuse_read_super); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 520 | } |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 521 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 522 | static struct file_system_type fuse_fs_type = { |
Miklos Szeredi | 377b2ea | 2004-02-06 09:48:51 +0000 | [diff] [blame] | 523 | .owner = THIS_MODULE, |
| 524 | .name = "fuse", |
| 525 | .get_sb = fuse_get_sb, |
| 526 | .kill_sb = kill_anon_super, |
Miklos Szeredi | 7f6bc89 | 2004-04-13 10:49:14 +0000 | [diff] [blame] | 527 | .fs_flags = FS_SAFE, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 528 | }; |
| 529 | #else |
| 530 | static struct super_block *fuse_read_super_compat(struct super_block *sb, |
| 531 | void *data, int silent) |
| 532 | { |
| 533 | int err = fuse_read_super(sb, data, silent); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 534 | if (err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | else |
| 537 | return sb; |
| 538 | } |
| 539 | |
| 540 | static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super_compat, 0); |
| 541 | #endif |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 542 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 543 | static void fuse_inode_init_once(void * foo, kmem_cache_t * cachep, |
| 544 | unsigned long flags) |
| 545 | { |
| 546 | struct inode * inode = (struct inode *) foo; |
| 547 | |
| 548 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 549 | SLAB_CTOR_CONSTRUCTOR) |
| 550 | inode_init_once(inode); |
| 551 | } |
| 552 | |
| 553 | |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 554 | int fuse_fs_init() |
| 555 | { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 556 | int err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 557 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 558 | err = register_filesystem(&fuse_fs_type); |
| 559 | if (err) |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 560 | printk("fuse: failed to register filesystem\n"); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 561 | else { |
| 562 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 563 | sizeof(struct inode) + sizeof(struct fuse_inode) , |
| 564 | 0, SLAB_HWCACHE_ALIGN, |
| 565 | fuse_inode_init_once, NULL); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 566 | if (!fuse_inode_cachep) { |
| 567 | unregister_filesystem(&fuse_fs_type); |
| 568 | err = -ENOMEM; |
| 569 | } |
| 570 | } |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 571 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 572 | return err; |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void fuse_fs_cleanup() |
| 576 | { |
| 577 | unregister_filesystem(&fuse_fs_type); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 578 | kmem_cache_destroy(fuse_inode_cachep); |
Miklos Szeredi | bc22e7b | 2001-10-23 19:26:04 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Local Variables: |
| 583 | * indent-tabs-mode: t |
| 584 | * c-basic-offset: 8 |
Miklos Szeredi | 63a63bc | 2001-12-02 20:37:53 +0000 | [diff] [blame] | 585 | * End: |
| 586 | */ |