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