Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/file.h> |
| 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 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 25 | static kmem_cache_t *fuse_inode_cachep; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 26 | static struct subsystem connections_subsys; |
| 27 | |
| 28 | struct fuse_conn_attr { |
| 29 | struct attribute attr; |
| 30 | ssize_t (*show)(struct fuse_conn *, char *); |
| 31 | ssize_t (*store)(struct fuse_conn *, const char *, size_t); |
| 32 | }; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 33 | |
| 34 | #define FUSE_SUPER_MAGIC 0x65735546 |
| 35 | |
| 36 | struct fuse_mount_data { |
| 37 | int fd; |
| 38 | unsigned rootmode; |
| 39 | unsigned user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 40 | unsigned group_id; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 41 | unsigned fd_present : 1; |
| 42 | unsigned rootmode_present : 1; |
| 43 | unsigned user_id_present : 1; |
| 44 | unsigned group_id_present : 1; |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 45 | unsigned flags; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 46 | unsigned max_read; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static struct inode *fuse_alloc_inode(struct super_block *sb) |
| 50 | { |
| 51 | struct inode *inode; |
| 52 | struct fuse_inode *fi; |
| 53 | |
| 54 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); |
| 55 | if (!inode) |
| 56 | return NULL; |
| 57 | |
| 58 | fi = get_fuse_inode(inode); |
| 59 | fi->i_time = jiffies - 1; |
| 60 | fi->nodeid = 0; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 61 | fi->nlookup = 0; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 62 | fi->forget_req = fuse_request_alloc(); |
| 63 | if (!fi->forget_req) { |
| 64 | kmem_cache_free(fuse_inode_cachep, inode); |
| 65 | return NULL; |
| 66 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 67 | |
| 68 | return inode; |
| 69 | } |
| 70 | |
| 71 | static void fuse_destroy_inode(struct inode *inode) |
| 72 | { |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 73 | struct fuse_inode *fi = get_fuse_inode(inode); |
| 74 | if (fi->forget_req) |
| 75 | fuse_request_free(fi->forget_req); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 76 | kmem_cache_free(fuse_inode_cachep, inode); |
| 77 | } |
| 78 | |
| 79 | static void fuse_read_inode(struct inode *inode) |
| 80 | { |
| 81 | /* No op */ |
| 82 | } |
| 83 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 84 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 85 | unsigned long nodeid, u64 nlookup) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 86 | { |
| 87 | struct fuse_forget_in *inarg = &req->misc.forget_in; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 88 | inarg->nlookup = nlookup; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 89 | req->in.h.opcode = FUSE_FORGET; |
| 90 | req->in.h.nodeid = nodeid; |
| 91 | req->in.numargs = 1; |
| 92 | req->in.args[0].size = sizeof(struct fuse_forget_in); |
| 93 | req->in.args[0].value = inarg; |
| 94 | request_send_noreply(fc, req); |
| 95 | } |
| 96 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 97 | static void fuse_clear_inode(struct inode *inode) |
| 98 | { |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 99 | if (inode->i_sb->s_flags & MS_ACTIVE) { |
| 100 | struct fuse_conn *fc = get_fuse_conn(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 101 | struct fuse_inode *fi = get_fuse_inode(inode); |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 102 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 103 | fi->forget_req = NULL; |
| 104 | } |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) |
| 108 | { |
| 109 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) |
| 110 | invalidate_inode_pages(inode->i_mapping); |
| 111 | |
| 112 | inode->i_ino = attr->ino; |
| 113 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); |
| 114 | inode->i_nlink = attr->nlink; |
| 115 | inode->i_uid = attr->uid; |
| 116 | inode->i_gid = attr->gid; |
| 117 | i_size_write(inode, attr->size); |
| 118 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 119 | inode->i_blocks = attr->blocks; |
| 120 | inode->i_atime.tv_sec = attr->atime; |
| 121 | inode->i_atime.tv_nsec = attr->atimensec; |
| 122 | inode->i_mtime.tv_sec = attr->mtime; |
| 123 | inode->i_mtime.tv_nsec = attr->mtimensec; |
| 124 | inode->i_ctime.tv_sec = attr->ctime; |
| 125 | inode->i_ctime.tv_nsec = attr->ctimensec; |
| 126 | } |
| 127 | |
| 128 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
| 129 | { |
| 130 | inode->i_mode = attr->mode & S_IFMT; |
| 131 | i_size_write(inode, attr->size); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 132 | if (S_ISREG(inode->i_mode)) { |
| 133 | fuse_init_common(inode); |
Miklos Szeredi | b6aeade | 2005-09-09 13:10:30 -0700 | [diff] [blame] | 134 | fuse_init_file_inode(inode); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 135 | } else if (S_ISDIR(inode->i_mode)) |
| 136 | fuse_init_dir(inode); |
| 137 | else if (S_ISLNK(inode->i_mode)) |
| 138 | fuse_init_symlink(inode); |
| 139 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 140 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
| 141 | fuse_init_common(inode); |
| 142 | init_special_inode(inode, inode->i_mode, |
| 143 | new_decode_dev(attr->rdev)); |
Miklos Szeredi | 39ee059 | 2006-01-06 00:19:43 -0800 | [diff] [blame] | 144 | } else |
| 145 | BUG(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
| 149 | { |
| 150 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 151 | if (get_node_id(inode) == nodeid) |
| 152 | return 1; |
| 153 | else |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 158 | { |
| 159 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 160 | get_fuse_inode(inode)->nodeid = nodeid; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 165 | int generation, struct fuse_attr *attr) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 166 | { |
| 167 | struct inode *inode; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 168 | struct fuse_inode *fi; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 169 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 170 | int retried = 0; |
| 171 | |
| 172 | retry: |
| 173 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 174 | if (!inode) |
| 175 | return NULL; |
| 176 | |
| 177 | if ((inode->i_state & I_NEW)) { |
Miklos Szeredi | b36c31b | 2005-09-09 13:10:38 -0700 | [diff] [blame] | 178 | inode->i_flags |= S_NOATIME|S_NOCMTIME; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 179 | inode->i_generation = generation; |
| 180 | inode->i_data.backing_dev_info = &fc->bdi; |
| 181 | fuse_init_inode(inode, attr); |
| 182 | unlock_new_inode(inode); |
| 183 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { |
| 184 | BUG_ON(retried); |
| 185 | /* Inode has changed type, any I/O on the old should fail */ |
| 186 | make_bad_inode(inode); |
| 187 | iput(inode); |
| 188 | retried = 1; |
| 189 | goto retry; |
| 190 | } |
| 191 | |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 192 | fi = get_fuse_inode(inode); |
| 193 | fi->nlookup ++; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 194 | fuse_change_attributes(inode, attr); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 195 | return inode; |
| 196 | } |
| 197 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 198 | static void fuse_umount_begin(struct super_block *sb) |
| 199 | { |
| 200 | fuse_abort_conn(get_fuse_conn_super(sb)); |
| 201 | } |
| 202 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 203 | static void fuse_put_super(struct super_block *sb) |
| 204 | { |
| 205 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 206 | |
Miklos Szeredi | 5a5fb1e | 2006-04-26 10:48:55 +0200 | [diff] [blame] | 207 | down_write(&fc->sbput_sem); |
| 208 | while (!list_empty(&fc->background)) |
| 209 | fuse_release_background(fc, |
| 210 | list_entry(fc->background.next, |
| 211 | struct fuse_req, bg_entry)); |
| 212 | |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 213 | spin_lock(&fc->lock); |
Miklos Szeredi | 5a5fb1e | 2006-04-26 10:48:55 +0200 | [diff] [blame] | 214 | fc->mounted = 0; |
Miklos Szeredi | 9ba7cbb | 2006-01-16 22:14:34 -0800 | [diff] [blame] | 215 | fc->connected = 0; |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 216 | spin_unlock(&fc->lock); |
Miklos Szeredi | 5a5fb1e | 2006-04-26 10:48:55 +0200 | [diff] [blame] | 217 | up_write(&fc->sbput_sem); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 218 | /* Flush all readers on this fs */ |
Jeff Dike | 385a17b | 2006-04-10 22:54:52 -0700 | [diff] [blame] | 219 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 220 | wake_up_all(&fc->waitq); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 221 | kobject_del(&fc->kobj); |
| 222 | kobject_put(&fc->kobj); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 225 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
| 226 | { |
| 227 | stbuf->f_type = FUSE_SUPER_MAGIC; |
| 228 | stbuf->f_bsize = attr->bsize; |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 229 | stbuf->f_frsize = attr->frsize; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 230 | stbuf->f_blocks = attr->blocks; |
| 231 | stbuf->f_bfree = attr->bfree; |
| 232 | stbuf->f_bavail = attr->bavail; |
| 233 | stbuf->f_files = attr->files; |
| 234 | stbuf->f_ffree = attr->ffree; |
| 235 | stbuf->f_namelen = attr->namelen; |
| 236 | /* fsid is left zero */ |
| 237 | } |
| 238 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 239 | static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 240 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 241 | struct super_block *sb = dentry->d_sb; |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 242 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
| 243 | struct fuse_req *req; |
| 244 | struct fuse_statfs_out outarg; |
| 245 | int err; |
| 246 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 247 | req = fuse_get_req(fc); |
| 248 | if (IS_ERR(req)) |
| 249 | return PTR_ERR(req); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 250 | |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 251 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 252 | req->in.numargs = 0; |
| 253 | req->in.h.opcode = FUSE_STATFS; |
| 254 | req->out.numargs = 1; |
Miklos Szeredi | de5f120 | 2006-01-06 00:19:37 -0800 | [diff] [blame] | 255 | req->out.args[0].size = |
| 256 | fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg); |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 257 | req->out.args[0].value = &outarg; |
| 258 | request_send(fc, req); |
| 259 | err = req->out.h.error; |
| 260 | if (!err) |
| 261 | convert_fuse_statfs(buf, &outarg.st); |
| 262 | fuse_put_request(fc, req); |
| 263 | return err; |
| 264 | } |
| 265 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 266 | enum { |
| 267 | OPT_FD, |
| 268 | OPT_ROOTMODE, |
| 269 | OPT_USER_ID, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 270 | OPT_GROUP_ID, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 271 | OPT_DEFAULT_PERMISSIONS, |
| 272 | OPT_ALLOW_OTHER, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 273 | OPT_MAX_READ, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 274 | OPT_ERR |
| 275 | }; |
| 276 | |
| 277 | static match_table_t tokens = { |
| 278 | {OPT_FD, "fd=%u"}, |
| 279 | {OPT_ROOTMODE, "rootmode=%o"}, |
| 280 | {OPT_USER_ID, "user_id=%u"}, |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 281 | {OPT_GROUP_ID, "group_id=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 282 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
| 283 | {OPT_ALLOW_OTHER, "allow_other"}, |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 284 | {OPT_MAX_READ, "max_read=%u"}, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 285 | {OPT_ERR, NULL} |
| 286 | }; |
| 287 | |
| 288 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) |
| 289 | { |
| 290 | char *p; |
| 291 | memset(d, 0, sizeof(struct fuse_mount_data)); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 292 | d->max_read = ~0; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 293 | |
| 294 | while ((p = strsep(&opt, ",")) != NULL) { |
| 295 | int token; |
| 296 | int value; |
| 297 | substring_t args[MAX_OPT_ARGS]; |
| 298 | if (!*p) |
| 299 | continue; |
| 300 | |
| 301 | token = match_token(p, tokens, args); |
| 302 | switch (token) { |
| 303 | case OPT_FD: |
| 304 | if (match_int(&args[0], &value)) |
| 305 | return 0; |
| 306 | d->fd = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 307 | d->fd_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 308 | break; |
| 309 | |
| 310 | case OPT_ROOTMODE: |
| 311 | if (match_octal(&args[0], &value)) |
| 312 | return 0; |
| 313 | d->rootmode = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 314 | d->rootmode_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 315 | break; |
| 316 | |
| 317 | case OPT_USER_ID: |
| 318 | if (match_int(&args[0], &value)) |
| 319 | return 0; |
| 320 | d->user_id = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 321 | d->user_id_present = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 322 | break; |
| 323 | |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 324 | case OPT_GROUP_ID: |
| 325 | if (match_int(&args[0], &value)) |
| 326 | return 0; |
| 327 | d->group_id = value; |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 328 | d->group_id_present = 1; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 329 | break; |
| 330 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 331 | case OPT_DEFAULT_PERMISSIONS: |
| 332 | d->flags |= FUSE_DEFAULT_PERMISSIONS; |
| 333 | break; |
| 334 | |
| 335 | case OPT_ALLOW_OTHER: |
| 336 | d->flags |= FUSE_ALLOW_OTHER; |
| 337 | break; |
| 338 | |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 339 | case OPT_MAX_READ: |
| 340 | if (match_int(&args[0], &value)) |
| 341 | return 0; |
| 342 | d->max_read = value; |
| 343 | break; |
| 344 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 345 | default: |
| 346 | return 0; |
| 347 | } |
| 348 | } |
Miklos Szeredi | 5a53368 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 349 | |
| 350 | if (!d->fd_present || !d->rootmode_present || |
| 351 | !d->user_id_present || !d->group_id_present) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 352 | return 0; |
| 353 | |
| 354 | return 1; |
| 355 | } |
| 356 | |
| 357 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) |
| 358 | { |
| 359 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); |
| 360 | |
| 361 | seq_printf(m, ",user_id=%u", fc->user_id); |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 362 | seq_printf(m, ",group_id=%u", fc->group_id); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 363 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
| 364 | seq_puts(m, ",default_permissions"); |
| 365 | if (fc->flags & FUSE_ALLOW_OTHER) |
| 366 | seq_puts(m, ",allow_other"); |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 367 | if (fc->max_read != ~0) |
| 368 | seq_printf(m, ",max_read=%u", fc->max_read); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 372 | static void fuse_conn_release(struct kobject *kobj) |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 373 | { |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 374 | kfree(get_fuse_conn_kobj(kobj)); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 377 | static struct fuse_conn *new_conn(void) |
| 378 | { |
| 379 | struct fuse_conn *fc; |
| 380 | |
Miklos Szeredi | 6383bda | 2006-01-16 22:14:29 -0800 | [diff] [blame] | 381 | fc = kzalloc(sizeof(*fc), GFP_KERNEL); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 382 | if (fc) { |
Miklos Szeredi | d713311 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 383 | spin_lock_init(&fc->lock); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 384 | init_waitqueue_head(&fc->waitq); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 385 | init_waitqueue_head(&fc->blocked_waitq); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 386 | INIT_LIST_HEAD(&fc->pending); |
| 387 | INIT_LIST_HEAD(&fc->processing); |
Miklos Szeredi | d77a1d5 | 2006-01-16 22:14:31 -0800 | [diff] [blame] | 388 | INIT_LIST_HEAD(&fc->io); |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 389 | INIT_LIST_HEAD(&fc->background); |
Miklos Szeredi | 5a5fb1e | 2006-04-26 10:48:55 +0200 | [diff] [blame] | 390 | init_rwsem(&fc->sbput_sem); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 391 | kobj_set_kset_s(fc, connections_subsys); |
| 392 | kobject_init(&fc->kobj); |
Miklos Szeredi | 095da6c | 2006-01-16 22:14:52 -0800 | [diff] [blame] | 393 | atomic_set(&fc->num_waiting, 0); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 394 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
| 395 | fc->bdi.unplug_io_fn = default_unplug_io_fn; |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 396 | fc->reqctr = 0; |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 397 | fc->blocked = 1; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 398 | } |
| 399 | return fc; |
| 400 | } |
| 401 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 402 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) |
| 403 | { |
| 404 | struct fuse_attr attr; |
| 405 | memset(&attr, 0, sizeof(attr)); |
| 406 | |
| 407 | attr.mode = mode; |
| 408 | attr.ino = FUSE_ROOT_ID; |
Miklos Szeredi | 9e6268d | 2005-09-09 13:10:29 -0700 | [diff] [blame] | 409 | return fuse_iget(sb, 1, 0, &attr); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static struct super_operations fuse_super_operations = { |
| 413 | .alloc_inode = fuse_alloc_inode, |
| 414 | .destroy_inode = fuse_destroy_inode, |
| 415 | .read_inode = fuse_read_inode, |
| 416 | .clear_inode = fuse_clear_inode, |
| 417 | .put_super = fuse_put_super, |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 418 | .umount_begin = fuse_umount_begin, |
Miklos Szeredi | e5e5558 | 2005-09-09 13:10:28 -0700 | [diff] [blame] | 419 | .statfs = fuse_statfs, |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 420 | .show_options = fuse_show_options, |
| 421 | }; |
| 422 | |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 423 | static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) |
| 424 | { |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 425 | struct fuse_init_out *arg = &req->misc.init_out; |
| 426 | |
| 427 | if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) |
| 428 | fc->conn_error = 1; |
| 429 | else { |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 430 | unsigned long ra_pages; |
| 431 | |
| 432 | if (arg->minor >= 6) { |
| 433 | ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; |
| 434 | if (arg->flags & FUSE_ASYNC_READ) |
| 435 | fc->async_read = 1; |
| 436 | } else |
| 437 | ra_pages = fc->max_read / PAGE_CACHE_SIZE; |
| 438 | |
| 439 | fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 440 | fc->minor = arg->minor; |
| 441 | fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; |
| 442 | } |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 443 | fuse_put_request(fc, req); |
Miklos Szeredi | 08a53cd | 2006-04-10 22:54:59 -0700 | [diff] [blame] | 444 | fc->blocked = 0; |
| 445 | wake_up_all(&fc->blocked_waitq); |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 448 | static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 449 | { |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 450 | struct fuse_init_in *arg = &req->misc.init_in; |
Miklos Szeredi | 095da6c | 2006-01-16 22:14:52 -0800 | [diff] [blame] | 451 | |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 452 | arg->major = FUSE_KERNEL_VERSION; |
| 453 | arg->minor = FUSE_KERNEL_MINOR_VERSION; |
Miklos Szeredi | 9cd6845 | 2006-02-01 03:04:40 -0800 | [diff] [blame] | 454 | arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; |
| 455 | arg->flags |= FUSE_ASYNC_READ; |
Miklos Szeredi | 9b9a046 | 2006-01-16 22:14:44 -0800 | [diff] [blame] | 456 | req->in.h.opcode = FUSE_INIT; |
| 457 | req->in.numargs = 1; |
| 458 | req->in.args[0].size = sizeof(*arg); |
| 459 | req->in.args[0].value = arg; |
| 460 | req->out.numargs = 1; |
| 461 | /* Variable length arguement used for backward compatibility |
| 462 | with interface version < 7.5. Rest of init_out is zeroed |
| 463 | by do_get_request(), so a short reply is not a problem */ |
| 464 | req->out.argvar = 1; |
| 465 | req->out.args[0].size = sizeof(struct fuse_init_out); |
| 466 | req->out.args[0].value = &req->misc.init_out; |
| 467 | req->end = process_init_reply; |
| 468 | request_send_background(fc, req); |
| 469 | } |
| 470 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 471 | static unsigned long long conn_id(void) |
| 472 | { |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 473 | /* BKL is held for ->get_sb() */ |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 474 | static unsigned long long ctr = 1; |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 475 | return ctr++; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 476 | } |
| 477 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 478 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) |
| 479 | { |
| 480 | struct fuse_conn *fc; |
| 481 | struct inode *root; |
| 482 | struct fuse_mount_data d; |
| 483 | struct file *file; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 484 | struct dentry *root_dentry; |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 485 | struct fuse_req *init_req; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 486 | int err; |
| 487 | |
| 488 | if (!parse_fuse_opt((char *) data, &d)) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 492 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 493 | sb->s_magic = FUSE_SUPER_MAGIC; |
| 494 | sb->s_op = &fuse_super_operations; |
| 495 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 496 | |
| 497 | file = fget(d.fd); |
| 498 | if (!file) |
| 499 | return -EINVAL; |
| 500 | |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 501 | if (file->f_op != &fuse_dev_operations) |
| 502 | return -EINVAL; |
| 503 | |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 504 | fc = new_conn(); |
| 505 | if (!fc) |
| 506 | return -ENOMEM; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 507 | |
Miklos Szeredi | 1e9a4ed | 2005-09-09 13:10:31 -0700 | [diff] [blame] | 508 | fc->flags = d.flags; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 509 | fc->user_id = d.user_id; |
Miklos Szeredi | 87729a5 | 2005-09-09 13:10:34 -0700 | [diff] [blame] | 510 | fc->group_id = d.group_id; |
Miklos Szeredi | db50b96 | 2005-09-09 13:10:33 -0700 | [diff] [blame] | 511 | fc->max_read = d.max_read; |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 512 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 513 | /* Used by get_root_inode() */ |
| 514 | sb->s_fs_info = fc; |
| 515 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 516 | err = -ENOMEM; |
| 517 | root = get_root_inode(sb, d.rootmode); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 518 | if (!root) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 519 | goto err; |
| 520 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 521 | root_dentry = d_alloc_root(root); |
| 522 | if (!root_dentry) { |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 523 | iput(root); |
| 524 | goto err; |
| 525 | } |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 526 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 527 | init_req = fuse_request_alloc(); |
| 528 | if (!init_req) |
| 529 | goto err_put_root; |
| 530 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 531 | err = kobject_set_name(&fc->kobj, "%llu", conn_id()); |
| 532 | if (err) |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 533 | goto err_free_req; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 534 | |
| 535 | err = kobject_add(&fc->kobj); |
| 536 | if (err) |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 537 | goto err_free_req; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 538 | |
Miklos Szeredi | 8aa09a5 | 2006-04-26 10:49:16 +0200 | [diff] [blame] | 539 | /* Setting file->private_data can't race with other mount() |
| 540 | instances, since BKL is held for ->get_sb() */ |
| 541 | err = -EINVAL; |
| 542 | if (file->private_data) |
| 543 | goto err_kobject_del; |
| 544 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 545 | sb->s_root = root_dentry; |
Miklos Szeredi | 5a5fb1e | 2006-04-26 10:48:55 +0200 | [diff] [blame] | 546 | fc->mounted = 1; |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 547 | fc->connected = 1; |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 548 | kobject_get(&fc->kobj); |
| 549 | file->private_data = fc; |
| 550 | /* |
| 551 | * atomic_dec_and_test() in fput() provides the necessary |
| 552 | * memory barrier for file->private_data to be visible on all |
| 553 | * CPUs after this |
| 554 | */ |
| 555 | fput(file); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 556 | |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 557 | fuse_send_init(fc, init_req); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 558 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | |
Miklos Szeredi | 8aa09a5 | 2006-04-26 10:49:16 +0200 | [diff] [blame] | 561 | err_kobject_del: |
| 562 | kobject_del(&fc->kobj); |
Miklos Szeredi | ce1d5a4 | 2006-04-10 22:54:58 -0700 | [diff] [blame] | 563 | err_free_req: |
| 564 | fuse_request_free(init_req); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 565 | err_put_root: |
| 566 | dput(root_dentry); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 567 | err: |
Miklos Szeredi | 0720b31 | 2006-04-10 22:54:55 -0700 | [diff] [blame] | 568 | fput(file); |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 569 | kobject_put(&fc->kobj); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 570 | return err; |
| 571 | } |
| 572 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 573 | static int fuse_get_sb(struct file_system_type *fs_type, |
| 574 | int flags, const char *dev_name, |
| 575 | void *raw_data, struct vfsmount *mnt) |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 576 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 577 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static struct file_system_type fuse_fs_type = { |
| 581 | .owner = THIS_MODULE, |
| 582 | .name = "fuse", |
| 583 | .get_sb = fuse_get_sb, |
| 584 | .kill_sb = kill_anon_super, |
| 585 | }; |
| 586 | |
Miklos Szeredi | 0cd5b88 | 2006-01-16 22:14:38 -0800 | [diff] [blame] | 587 | static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page) |
| 588 | { |
| 589 | return sprintf(page, "%i\n", atomic_read(&fc->num_waiting)); |
| 590 | } |
| 591 | |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 592 | static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page, |
| 593 | size_t count) |
| 594 | { |
| 595 | fuse_abort_conn(fc); |
| 596 | return count; |
| 597 | } |
| 598 | |
Miklos Szeredi | 0cd5b88 | 2006-01-16 22:14:38 -0800 | [diff] [blame] | 599 | static struct fuse_conn_attr fuse_conn_waiting = |
| 600 | __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL); |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 601 | static struct fuse_conn_attr fuse_conn_abort = |
| 602 | __ATTR(abort, 0600, NULL, fuse_conn_abort_store); |
Miklos Szeredi | 0cd5b88 | 2006-01-16 22:14:38 -0800 | [diff] [blame] | 603 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 604 | static struct attribute *fuse_conn_attrs[] = { |
Miklos Szeredi | 0cd5b88 | 2006-01-16 22:14:38 -0800 | [diff] [blame] | 605 | &fuse_conn_waiting.attr, |
Miklos Szeredi | 69a53bf | 2006-01-16 22:14:41 -0800 | [diff] [blame] | 606 | &fuse_conn_abort.attr, |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 607 | NULL, |
| 608 | }; |
| 609 | |
| 610 | static ssize_t fuse_conn_attr_show(struct kobject *kobj, |
| 611 | struct attribute *attr, |
| 612 | char *page) |
| 613 | { |
| 614 | struct fuse_conn_attr *fca = |
| 615 | container_of(attr, struct fuse_conn_attr, attr); |
| 616 | |
| 617 | if (fca->show) |
| 618 | return fca->show(get_fuse_conn_kobj(kobj), page); |
| 619 | else |
| 620 | return -EACCES; |
| 621 | } |
| 622 | |
| 623 | static ssize_t fuse_conn_attr_store(struct kobject *kobj, |
| 624 | struct attribute *attr, |
| 625 | const char *page, size_t count) |
| 626 | { |
| 627 | struct fuse_conn_attr *fca = |
| 628 | container_of(attr, struct fuse_conn_attr, attr); |
| 629 | |
| 630 | if (fca->store) |
| 631 | return fca->store(get_fuse_conn_kobj(kobj), page, count); |
| 632 | else |
| 633 | return -EACCES; |
| 634 | } |
| 635 | |
| 636 | static struct sysfs_ops fuse_conn_sysfs_ops = { |
| 637 | .show = &fuse_conn_attr_show, |
| 638 | .store = &fuse_conn_attr_store, |
| 639 | }; |
| 640 | |
| 641 | static struct kobj_type ktype_fuse_conn = { |
| 642 | .release = fuse_conn_release, |
| 643 | .sysfs_ops = &fuse_conn_sysfs_ops, |
| 644 | .default_attrs = fuse_conn_attrs, |
| 645 | }; |
| 646 | |
| 647 | static decl_subsys(fuse, NULL, NULL); |
| 648 | static decl_subsys(connections, &ktype_fuse_conn, NULL); |
| 649 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 650 | static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, |
| 651 | unsigned long flags) |
| 652 | { |
| 653 | struct inode * inode = foo; |
| 654 | |
| 655 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 656 | SLAB_CTOR_CONSTRUCTOR) |
| 657 | inode_init_once(inode); |
| 658 | } |
| 659 | |
| 660 | static int __init fuse_fs_init(void) |
| 661 | { |
| 662 | int err; |
| 663 | |
| 664 | err = register_filesystem(&fuse_fs_type); |
| 665 | if (err) |
| 666 | printk("fuse: failed to register filesystem\n"); |
| 667 | else { |
| 668 | fuse_inode_cachep = kmem_cache_create("fuse_inode", |
| 669 | sizeof(struct fuse_inode), |
| 670 | 0, SLAB_HWCACHE_ALIGN, |
| 671 | fuse_inode_init_once, NULL); |
| 672 | if (!fuse_inode_cachep) { |
| 673 | unregister_filesystem(&fuse_fs_type); |
| 674 | err = -ENOMEM; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return err; |
| 679 | } |
| 680 | |
| 681 | static void fuse_fs_cleanup(void) |
| 682 | { |
| 683 | unregister_filesystem(&fuse_fs_type); |
| 684 | kmem_cache_destroy(fuse_inode_cachep); |
| 685 | } |
| 686 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 687 | static int fuse_sysfs_init(void) |
| 688 | { |
| 689 | int err; |
| 690 | |
| 691 | kset_set_kset_s(&fuse_subsys, fs_subsys); |
| 692 | err = subsystem_register(&fuse_subsys); |
| 693 | if (err) |
| 694 | goto out_err; |
| 695 | |
| 696 | kset_set_kset_s(&connections_subsys, fuse_subsys); |
| 697 | err = subsystem_register(&connections_subsys); |
| 698 | if (err) |
| 699 | goto out_fuse_unregister; |
| 700 | |
| 701 | return 0; |
| 702 | |
| 703 | out_fuse_unregister: |
| 704 | subsystem_unregister(&fuse_subsys); |
| 705 | out_err: |
| 706 | return err; |
| 707 | } |
| 708 | |
| 709 | static void fuse_sysfs_cleanup(void) |
| 710 | { |
| 711 | subsystem_unregister(&connections_subsys); |
| 712 | subsystem_unregister(&fuse_subsys); |
| 713 | } |
| 714 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 715 | static int __init fuse_init(void) |
| 716 | { |
| 717 | int res; |
| 718 | |
| 719 | printk("fuse init (API version %i.%i)\n", |
| 720 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 721 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 722 | res = fuse_fs_init(); |
| 723 | if (res) |
| 724 | goto err; |
| 725 | |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 726 | res = fuse_dev_init(); |
| 727 | if (res) |
| 728 | goto err_fs_cleanup; |
| 729 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 730 | res = fuse_sysfs_init(); |
| 731 | if (res) |
| 732 | goto err_dev_cleanup; |
| 733 | |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 734 | return 0; |
| 735 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 736 | err_dev_cleanup: |
| 737 | fuse_dev_cleanup(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 738 | err_fs_cleanup: |
| 739 | fuse_fs_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 740 | err: |
| 741 | return res; |
| 742 | } |
| 743 | |
| 744 | static void __exit fuse_exit(void) |
| 745 | { |
| 746 | printk(KERN_DEBUG "fuse exit\n"); |
| 747 | |
Miklos Szeredi | f543f25 | 2006-01-16 22:14:35 -0800 | [diff] [blame] | 748 | fuse_sysfs_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 749 | fuse_fs_cleanup(); |
Miklos Szeredi | 334f485 | 2005-09-09 13:10:27 -0700 | [diff] [blame] | 750 | fuse_dev_cleanup(); |
Miklos Szeredi | d8a5ba4 | 2005-09-09 13:10:26 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | module_init(fuse_init); |
| 754 | module_exit(fuse_exit); |