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