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