Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/file.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 18 | #include <linux/parser.h> |
| 19 | #include <linux/statfs.h> |
| 20 | |
| 21 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 22 | MODULE_DESCRIPTION("Filesystem in Userspace"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
| 25 | spinlock_t fuse_lock; |
| 26 | static kmem_cache_t *fuse_inode_cachep; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 27 | |
| 28 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 29 | |
| 30 | struct fuse_mount_data { |
| 31 | int fd; |
| 32 | unsigned rootmode; |
| 33 | unsigned user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 34 | unsigned group_id; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 35 | unsigned flags; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 36 | unsigned max_read; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
| 40 | { |
| 41 | struct inode *inode; |
| 42 | struct fuse_inode *fi; |
| 43 | |
| 44 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); |
| 45 | if (!inode) |
| 46 | return NULL; |
| 47 | |
| 48 | fi = get_fuse_inode(inode); |
| 49 | fi->i_time = jiffies - 1; |
| 50 | fi->nodeid = 0; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 51 | fi->nlookup = 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 52 | fi->forget_req = fuse_request_alloc(); |
| 53 | if (!fi->forget_req) { |
| 54 | kmem_cache_free(fuse_inode_cachep, inode); |
| 55 | return NULL; |
| 56 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 57 | |
| 58 | return inode; |
| 59 | } |
| 60 | |
| 61 | static void fuse_destroy_inode(struct inode *inode) |
| 62 | { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 63 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 64 | if (fi->forget_req) |
| 65 | fuse_request_free(fi->forget_req); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 66 | kmem_cache_free(fuse_inode_cachep, inode); |
| 67 | } |
| 68 | |
| 69 | static void fuse_read_inode(struct inode *inode) |
| 70 | { |
| 71 | /* No op */ |
| 72 | } |
| 73 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 74 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 75 | unsigned long nodeid, u64 nlookup) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 76 | { |
| 77 | struct fuse_forget_in *inarg = &req->misc.forget_in; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 78 | inarg->nlookup = nlookup; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 79 | req->in.h.opcode = FUSE_FORGET; |
| 80 | req->in.h.nodeid = nodeid; |
| 81 | req->in.numargs = 1; |
| 82 | req->in.args[0].size = sizeof(struct fuse_forget_in); |
| 83 | req->in.args[0].value = inarg; |
| 84 | request_send_noreply(fc, req); |
| 85 | } |
| 86 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 87 | static void fuse_clear_inode(struct inode *inode) |
| 88 | { |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 89 | if (inode->i_sb->s_flags & MS_ACTIVE) { |
| 90 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 91 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 92 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 93 | fi->forget_req = NULL; |
| 94 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) |
| 98 | { |
| 99 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) |
| 100 | invalidate_inode_pages(inode->i_mapping); |
| 101 | |
| 102 | inode->i_ino = attr->ino; |
| 103 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); |
| 104 | inode->i_nlink = attr->nlink; |
| 105 | inode->i_uid = attr->uid; |
| 106 | inode->i_gid = attr->gid; |
| 107 | i_size_write(inode, attr->size); |
| 108 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 109 | inode->i_blocks = attr->blocks; |
| 110 | inode->i_atime.tv_sec = attr->atime; |
| 111 | inode->i_atime.tv_nsec = attr->atimensec; |
| 112 | inode->i_mtime.tv_sec = attr->mtime; |
| 113 | inode->i_mtime.tv_nsec = attr->mtimensec; |
| 114 | inode->i_ctime.tv_sec = attr->ctime; |
| 115 | inode->i_ctime.tv_nsec = attr->ctimensec; |
| 116 | } |
| 117 | |
| 118 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
| 119 | { |
| 120 | inode->i_mode = attr->mode & S_IFMT; |
| 121 | i_size_write(inode, attr->size); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 122 | if (S_ISREG(inode->i_mode)) { |
| 123 | fuse_init_common(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 124 | fuse_init_file_inode(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 125 | } else if (S_ISDIR(inode->i_mode)) |
| 126 | fuse_init_dir(inode); |
| 127 | else if (S_ISLNK(inode->i_mode)) |
| 128 | fuse_init_symlink(inode); |
| 129 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 130 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 131 | fuse_init_common(inode); |
| 132 | init_special_inode(inode, inode->i_mode, |
| 133 | new_decode_dev(attr->rdev)); |
| 134 | } else { |
| 135 | /* Don't let user create weird files */ |
| 136 | inode->i_mode = S_IFREG; |
| 137 | fuse_init_common(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 138 | fuse_init_file_inode(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 139 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
| 143 | { |
| 144 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 145 | if (get_node_id(inode) == nodeid) |
| 146 | return 1; |
| 147 | else |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 152 | { |
| 153 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 154 | get_fuse_inode(inode)->nodeid = nodeid; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 159 | int generation, struct fuse_attr *attr) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 160 | { |
| 161 | struct inode *inode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 162 | struct fuse_inode *fi; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 163 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 164 | int retried = 0; |
| 165 | |
| 166 | retry: |
| 167 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 168 | if (!inode) |
| 169 | return NULL; |
| 170 | |
| 171 | if ((inode->i_state & I_NEW)) { |
| 172 | inode->i_generation = generation; |
| 173 | inode->i_data.backing_dev_info = &fc->bdi; |
| 174 | fuse_init_inode(inode, attr); |
| 175 | unlock_new_inode(inode); |
| 176 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
| 177 | BUG_ON(retried); |
| 178 | /* Inode has changed type, any I/O on the old should fail */ |
| 179 | make_bad_inode(inode); |
| 180 | iput(inode); |
| 181 | retried = 1; |
| 182 | goto retry; |
| 183 | } |
| 184 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 185 | fi = get_fuse_inode(inode); |
| 186 | fi->nlookup ++; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 187 | fuse_change_attributes(inode, attr); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 188 | return inode; |
| 189 | } |
| 190 | |
| 191 | static void fuse_put_super(struct super_block *sb) |
| 192 | { |
| 193 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 194 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 195 | down_write(&fc->sbput_sem); |
| 196 | while (!list_empty(&fc->background)) |
| 197 | fuse_release_background(list_entry(fc->background.next, |
| 198 | struct fuse_req, bg_entry)); |
| 199 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 200 | spin_lock(&fuse_lock); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 201 | fc->mounted = 0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 202 | fc->user_id = 0; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 203 | fc->group_id = 0; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 204 | fc->flags = 0; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 205 | /* Flush all readers on this fs */ |
| 206 | wake_up_all(&fc->waitq); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 207 | up_write(&fc->sbput_sem); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 208 | fuse_release_conn(fc); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 209 | spin_unlock(&fuse_lock); |
| 210 | } |
| 211 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 212 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
| 213 | { |
| 214 | stbuf->f_type = FUSE_SUPER_MAGIC; |
| 215 | stbuf->f_bsize = attr->bsize; |
| 216 | stbuf->f_blocks = attr->blocks; |
| 217 | stbuf->f_bfree = attr->bfree; |
| 218 | stbuf->f_bavail = attr->bavail; |
| 219 | stbuf->f_files = attr->files; |
| 220 | stbuf->f_ffree = attr->ffree; |
| 221 | stbuf->f_namelen = attr->namelen; |
| 222 | /* fsid is left zero */ |
| 223 | } |
| 224 | |
| 225 | static int fuse_statfs(struct super_block *sb, struct kstatfs *buf) |
| 226 | { |
| 227 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 228 | struct fuse_req *req; |
| 229 | struct fuse_statfs_out outarg; |
| 230 | int err; |
| 231 | |
| 232 | req = fuse_get_request(fc); |
| 233 | if (!req) |
| 234 | return -ERESTARTSYS; |
| 235 | |
| 236 | req->in.numargs = 0; |
| 237 | req->in.h.opcode = FUSE_STATFS; |
| 238 | req->out.numargs = 1; |
| 239 | req->out.args[0].size = sizeof(outarg); |
| 240 | req->out.args[0].value = &outarg; |
| 241 | request_send(fc, req); |
| 242 | err = req->out.h.error; |
| 243 | if (!err) |
| 244 | convert_fuse_statfs(buf, &outarg.st); |
| 245 | fuse_put_request(fc, req); |
| 246 | return err; |
| 247 | } |
| 248 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 249 | enum { |
| 250 | OPT_FD, |
| 251 | OPT_ROOTMODE, |
| 252 | OPT_USER_ID, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 253 | OPT_GROUP_ID, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 254 | OPT_DEFAULT_PERMISSIONS, |
| 255 | OPT_ALLOW_OTHER, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 256 | OPT_KERNEL_CACHE, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 257 | OPT_MAX_READ, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 258 | OPT_ERR |
| 259 | }; |
| 260 | |
| 261 | static match_table_t tokens = { |
| 262 | {OPT_FD, "fd=%u"}, |
| 263 | {OPT_ROOTMODE, "rootmode=%o"}, |
| 264 | {OPT_USER_ID, "user_id=%u"}, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 265 | {OPT_GROUP_ID, "group_id=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 266 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 267 | {OPT_ALLOW_OTHER, "allow_other"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 268 | {OPT_KERNEL_CACHE, "kernel_cache"}, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 269 | {OPT_MAX_READ, "max_read=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 270 | {OPT_ERR, NULL} |
| 271 | }; |
| 272 | |
| 273 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) |
| 274 | { |
| 275 | char *p; |
| 276 | memset(d, 0, sizeof(struct fuse_mount_data)); |
| 277 | d->fd = -1; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 278 | d->max_read = ~0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 279 | |
| 280 | while ((p = strsep(&opt, ",")) != NULL) { |
| 281 | int token; |
| 282 | int value; |
| 283 | substring_t args[MAX_OPT_ARGS]; |
| 284 | if (!*p) |
| 285 | continue; |
| 286 | |
| 287 | token = match_token(p, tokens, args); |
| 288 | switch (token) { |
| 289 | case OPT_FD: |
| 290 | if (match_int(&args[0], &value)) |
| 291 | return 0; |
| 292 | d->fd = value; |
| 293 | break; |
| 294 | |
| 295 | case OPT_ROOTMODE: |
| 296 | if (match_octal(&args[0], &value)) |
| 297 | return 0; |
| 298 | d->rootmode = value; |
| 299 | break; |
| 300 | |
| 301 | case OPT_USER_ID: |
| 302 | if (match_int(&args[0], &value)) |
| 303 | return 0; |
| 304 | d->user_id = value; |
| 305 | break; |
| 306 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 307 | case OPT_GROUP_ID: |
| 308 | if (match_int(&args[0], &value)) |
| 309 | return 0; |
| 310 | d->group_id = value; |
| 311 | break; |
| 312 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 313 | case OPT_DEFAULT_PERMISSIONS: |
| 314 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 315 | break; |
| 316 | |
| 317 | case OPT_ALLOW_OTHER: |
| 318 | d->flags |= FUSE_ALLOW_OTHER; |
| 319 | break; |
| 320 | |
| 321 | case OPT_KERNEL_CACHE: |
| 322 | d->flags |= FUSE_KERNEL_CACHE; |
| 323 | break; |
| 324 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 325 | case OPT_MAX_READ: |
| 326 | if (match_int(&args[0], &value)) |
| 327 | return 0; |
| 328 | d->max_read = value; |
| 329 | break; |
| 330 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 331 | default: |
| 332 | return 0; |
| 333 | } |
| 334 | } |
| 335 | if (d->fd == -1) |
| 336 | return 0; |
| 337 | |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 342 | { |
| 343 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); |
| 344 | |
| 345 | seq_printf(m, ",user_id=%u", fc->user_id); |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 346 | seq_printf(m, ",group_id=%u", fc->group_id); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 347 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 348 | seq_puts(m, ",default_permissions"); |
| 349 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 350 | seq_puts(m, ",allow_other"); |
| 351 | if (fc->flags & FUSE_KERNEL_CACHE) |
| 352 | seq_puts(m, ",kernel_cache"); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 353 | if (fc->max_read != ~0) |
| 354 | seq_printf(m, ",max_read=%u", fc->max_read); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 358 | static void free_conn(struct fuse_conn *fc) |
| 359 | { |
| 360 | while (!list_empty(&fc->unused_list)) { |
| 361 | struct fuse_req *req; |
| 362 | req = list_entry(fc->unused_list.next, struct fuse_req, list); |
| 363 | list_del(&req->list); |
| 364 | fuse_request_free(req); |
| 365 | } |
| 366 | kfree(fc); |
| 367 | } |
| 368 | |
| 369 | /* Must be called with the fuse lock held */ |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 370 | void fuse_release_conn(struct fuse_conn *fc) |
| 371 | { |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 372 | fc->count--; |
| 373 | if (!fc->count) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 374 | free_conn(fc); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static struct fuse_conn *new_conn(void) |
| 378 | { |
| 379 | struct fuse_conn *fc; |
| 380 | |
| 381 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); |
| 382 | if (fc != NULL) { |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 383 | int i; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 384 | memset(fc, 0, sizeof(*fc)); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 385 | init_waitqueue_head(&fc->waitq); |
| 386 | INIT_LIST_HEAD(&fc->pending); |
| 387 | INIT_LIST_HEAD(&fc->processing); |
| 388 | INIT_LIST_HEAD(&fc->unused_list); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 389 | INIT_LIST_HEAD(&fc->background); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 390 | sema_init(&fc->outstanding_sem, 0); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 391 | init_rwsem(&fc->sbput_sem); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 392 | for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) { |
| 393 | struct fuse_req *req = fuse_request_alloc(); |
| 394 | if (!req) { |
| 395 | free_conn(fc); |
| 396 | return NULL; |
| 397 | } |
| 398 | list_add(&req->list, &fc->unused_list); |
| 399 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 400 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 401 | fc->bdi.unplug_io_fn = default_unplug_io_fn; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 402 | fc->reqctr = 0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 403 | } |
| 404 | return fc; |
| 405 | } |
| 406 | |
| 407 | static struct fuse_conn *get_conn(struct file *file, struct super_block *sb) |
| 408 | { |
| 409 | struct fuse_conn *fc; |
| 410 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 411 | if (file->f_op != &fuse_dev_operations) |
| 412 | return ERR_PTR(-EINVAL); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 413 | fc = new_conn(); |
| 414 | if (fc == NULL) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 415 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 416 | spin_lock(&fuse_lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 417 | if (file->private_data) { |
| 418 | free_conn(fc); |
| 419 | fc = ERR_PTR(-EINVAL); |
| 420 | } else { |
| 421 | file->private_data = fc; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 422 | *get_fuse_conn_super_p(sb) = fc; |
| 423 | fc->mounted = 1; |
| 424 | fc->connected = 1; |
| 425 | fc->count = 2; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 426 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 427 | spin_unlock(&fuse_lock); |
| 428 | return fc; |
| 429 | } |
| 430 | |
| 431 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) |
| 432 | { |
| 433 | struct fuse_attr attr; |
| 434 | memset(&attr, 0, sizeof(attr)); |
| 435 | |
| 436 | attr.mode = mode; |
| 437 | attr.ino = FUSE_ROOT_ID; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 438 | return fuse_iget(sb, 1, 0, &attr); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static struct super_operations fuse_super_operations = { |
| 442 | .alloc_inode = fuse_alloc_inode, |
| 443 | .destroy_inode = fuse_destroy_inode, |
| 444 | .read_inode = fuse_read_inode, |
| 445 | .clear_inode = fuse_clear_inode, |
| 446 | .put_super = fuse_put_super, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 447 | .statfs = fuse_statfs, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 448 | .show_options = fuse_show_options, |
| 449 | }; |
| 450 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 451 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) |
| 452 | { |
| 453 | struct fuse_conn *fc; |
| 454 | struct inode *root; |
| 455 | struct fuse_mount_data d; |
| 456 | struct file *file; |
| 457 | int err; |
| 458 | |
| 459 | if (!parse_fuse_opt((char *) data, &d)) |
| 460 | return -EINVAL; |
| 461 | |
| 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; |
| 467 | |
| 468 | file = fget(d.fd); |
| 469 | if (!file) |
| 470 | return -EINVAL; |
| 471 | |
| 472 | fc = get_conn(file, sb); |
| 473 | fput(file); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 474 | if (IS_ERR(fc)) |
| 475 | return PTR_ERR(fc); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 476 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 477 | fc->flags = d.flags; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 478 | fc->user_id = d.user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame^] | 479 | fc->group_id = d.group_id; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 480 | fc->max_read = d.max_read; |
| 481 | if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages) |
| 482 | fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 483 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 484 | err = -ENOMEM; |
| 485 | root = get_root_inode(sb, d.rootmode); |
| 486 | if (root == NULL) |
| 487 | goto err; |
| 488 | |
| 489 | sb->s_root = d_alloc_root(root); |
| 490 | if (!sb->s_root) { |
| 491 | iput(root); |
| 492 | goto err; |
| 493 | } |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 494 | fuse_send_init(fc); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 495 | return 0; |
| 496 | |
| 497 | err: |
| 498 | spin_lock(&fuse_lock); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 499 | fuse_release_conn(fc); |
| 500 | spin_unlock(&fuse_lock); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 501 | return err; |
| 502 | } |
| 503 | |
| 504 | static struct super_block *fuse_get_sb(struct file_system_type *fs_type, |
| 505 | int flags, const char *dev_name, |
| 506 | void *raw_data) |
| 507 | { |
| 508 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super); |
| 509 | } |
| 510 | |
| 511 | static struct file_system_type fuse_fs_type = { |
| 512 | .owner = THIS_MODULE, |
| 513 | .name = "fuse", |
| 514 | .get_sb = fuse_get_sb, |
| 515 | .kill_sb = kill_anon_super, |
| 516 | }; |
| 517 | |
| 518 | static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, |
| 519 | unsigned long flags) |
| 520 | { |
| 521 | struct inode * inode = foo; |
| 522 | |
| 523 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 524 | SLAB_CTOR_CONSTRUCTOR) |
| 525 | inode_init_once(inode); |
| 526 | } |
| 527 | |
| 528 | static int __init fuse_fs_init(void) |
| 529 | { |
| 530 | int err; |
| 531 | |
| 532 | err = register_filesystem(&fuse_fs_type); |
| 533 | if (err) |
| 534 | printk("fuse: failed to register filesystem\n"); |
| 535 | else { |
| 536 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
| 537 | sizeof(struct fuse_inode), |
| 538 | 0, SLAB_HWCACHE_ALIGN, |
| 539 | fuse_inode_init_once, NULL); |
| 540 | if (!fuse_inode_cachep) { |
| 541 | unregister_filesystem(&fuse_fs_type); |
| 542 | err = -ENOMEM; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return err; |
| 547 | } |
| 548 | |
| 549 | static void fuse_fs_cleanup(void) |
| 550 | { |
| 551 | unregister_filesystem(&fuse_fs_type); |
| 552 | kmem_cache_destroy(fuse_inode_cachep); |
| 553 | } |
| 554 | |
| 555 | static int __init fuse_init(void) |
| 556 | { |
| 557 | int res; |
| 558 | |
| 559 | printk("fuse init (API version %i.%i)\n", |
| 560 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 561 | |
| 562 | spin_lock_init(&fuse_lock); |
| 563 | res = fuse_fs_init(); |
| 564 | if (res) |
| 565 | goto err; |
| 566 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 567 | res = fuse_dev_init(); |
| 568 | if (res) |
| 569 | goto err_fs_cleanup; |
| 570 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 573 | err_fs_cleanup: |
| 574 | fuse_fs_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 575 | err: |
| 576 | return res; |
| 577 | } |
| 578 | |
| 579 | static void __exit fuse_exit(void) |
| 580 | { |
| 581 | printk(KERN_DEBUG "fuse exit\n"); |
| 582 | |
| 583 | fuse_fs_cleanup(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 584 | fuse_dev_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | module_init(fuse_init); |
| 588 | module_exit(fuse_exit); |