Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.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 | |
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 | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 13 | #include <linux/file.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 14 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 15 | static struct inode_operations fuse_dir_inode_operations; |
| 16 | static struct inode_operations fuse_file_inode_operations; |
| 17 | static struct inode_operations fuse_symlink_inode_operations; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 18 | |
| 19 | static struct file_operations fuse_dir_operations; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 20 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 21 | static struct dentry_operations fuse_dentry_operations; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 22 | |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 23 | #ifndef KERNEL_2_6 |
| 24 | #define new_decode_dev(x) (x) |
| 25 | #define new_encode_dev(x) (x) |
| 26 | #endif |
| 27 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 28 | static void change_attributes(struct inode *inode, struct fuse_attr *attr) |
| 29 | { |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 30 | if(S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 31 | #ifdef KERNEL_2_6 |
| 32 | invalidate_inode_pages(inode->i_mapping); |
| 33 | #else |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 34 | invalidate_inode_pages(inode); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 35 | #endif |
| 36 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 37 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 38 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 39 | inode->i_nlink = attr->nlink; |
| 40 | inode->i_uid = attr->uid; |
| 41 | inode->i_gid = attr->gid; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 42 | i_size_write(inode, attr->size); |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 43 | inode->i_blksize = PAGE_CACHE_SIZE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 44 | inode->i_blocks = attr->blocks; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 45 | #ifdef KERNEL_2_6 |
| 46 | inode->i_atime.tv_sec = attr->atime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 47 | inode->i_atime.tv_nsec = attr->atimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 48 | inode->i_mtime.tv_sec = attr->mtime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 49 | inode->i_mtime.tv_nsec = attr->mtimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 50 | inode->i_ctime.tv_sec = attr->ctime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 51 | inode->i_ctime.tv_nsec = attr->ctimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 52 | #else |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 53 | inode->i_atime = attr->atime; |
| 54 | inode->i_mtime = attr->mtime; |
| 55 | inode->i_ctime = attr->ctime; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 56 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 59 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 60 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 61 | inode->i_mode = attr->mode & S_IFMT; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 62 | i_size_write(inode, attr->size); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 63 | if(S_ISREG(inode->i_mode)) { |
| 64 | inode->i_op = &fuse_file_inode_operations; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 65 | fuse_init_file_inode(inode); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 66 | } |
| 67 | else if(S_ISDIR(inode->i_mode)) { |
| 68 | inode->i_op = &fuse_dir_inode_operations; |
| 69 | inode->i_fop = &fuse_dir_operations; |
| 70 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 71 | else if(S_ISLNK(inode->i_mode)) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 72 | inode->i_op = &fuse_symlink_inode_operations; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 73 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 74 | else if(S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
| 75 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)){ |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 76 | inode->i_op = &fuse_file_inode_operations; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 77 | init_special_inode(inode, inode->i_mode, |
| 78 | new_decode_dev(attr->rdev)); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 79 | } else |
| 80 | printk("fuse_init_inode: bad file type: %o\n", inode->i_mode); |
| 81 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 82 | inode->u.generic_ip = inode; |
| 83 | } |
| 84 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 85 | struct inode *fuse_iget(struct super_block *sb, ino_t ino, int generation, |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 86 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 87 | { |
| 88 | struct inode *inode; |
| 89 | |
| 90 | inode = iget(sb, ino); |
| 91 | if(inode) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 92 | if(!inode->u.generic_ip) { |
| 93 | inode->i_generation = generation; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 94 | fuse_init_inode(inode, attr); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 95 | } else if(inode->i_generation != generation) |
| 96 | printk("fuse_iget: bad generation for ino %lu\n", ino); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 97 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 98 | change_attributes(inode, attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 99 | inode->i_version = version; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return inode; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 105 | static int fuse_do_lookup(struct inode *dir, struct dentry *entry, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 106 | struct fuse_entry_out *outarg, int *version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 107 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 108 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 109 | struct fuse_in in = FUSE_IN_INIT; |
| 110 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 111 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 112 | in.h.opcode = FUSE_LOOKUP; |
| 113 | in.h.ino = dir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 114 | in.numargs = 1; |
| 115 | in.args[0].size = entry->d_name.len + 1; |
| 116 | in.args[0].value = entry->d_name.name; |
| 117 | out.numargs = 1; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 118 | out.args[0].size = sizeof(struct fuse_entry_out); |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 119 | out.args[0].value = outarg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 120 | request_send(fc, &in, &out); |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 121 | |
| 122 | *version = out.h.unique; |
| 123 | return out.h.error; |
| 124 | } |
| 125 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 126 | static inline unsigned long time_to_jiffies(unsigned long sec, |
| 127 | unsigned long nsec) |
| 128 | { |
| 129 | /* prevent wrapping of jiffies */ |
| 130 | if(sec + 1 >= LONG_MAX / HZ) |
| 131 | return 0; |
| 132 | |
| 133 | return jiffies + sec * HZ + nsec / (1000000000 / HZ); |
| 134 | } |
| 135 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 136 | static int fuse_lookup_iget(struct inode *dir, struct dentry *entry, |
| 137 | struct inode **inodep) |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 138 | { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 139 | int err; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 140 | struct fuse_entry_out outarg; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 141 | int version; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 142 | struct inode *inode = NULL; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 143 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 144 | err = fuse_do_lookup(dir, entry, &outarg, &version); |
| 145 | if(!err) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 146 | inode = fuse_iget(dir->i_sb, outarg.ino, outarg.generation, |
| 147 | &outarg.attr, version); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 148 | if(!inode) |
| 149 | return -ENOMEM; |
| 150 | } else if(err != -ENOENT) |
| 151 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 152 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 153 | entry->d_time = time_to_jiffies(outarg.entry_valid, |
| 154 | outarg.entry_valid_nsec); |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 155 | entry->d_op = &fuse_dentry_operations; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 156 | *inodep = inode; |
| 157 | return 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 160 | static int lookup_new_entry(struct inode *dir, struct dentry *entry, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 161 | struct fuse_entry_out *outarg, int version, |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 162 | int mode) |
| 163 | { |
| 164 | struct inode *inode; |
| 165 | inode = fuse_iget(dir->i_sb, outarg->ino, outarg->generation, |
| 166 | &outarg->attr, version); |
| 167 | if(!inode) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | /* Don't allow userspace to do really stupid things... */ |
| 171 | if((inode->i_mode ^ mode) & S_IFMT) { |
| 172 | iput(inode); |
| 173 | printk("fuse_mknod: inode has wrong type\n"); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | d_instantiate(entry, inode); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 182 | static int _fuse_mknod(struct inode *dir, struct dentry *entry, int mode, |
| 183 | dev_t rdev) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 184 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 185 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 186 | struct fuse_in in = FUSE_IN_INIT; |
| 187 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 188 | struct fuse_mknod_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 189 | struct fuse_entry_out outarg; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 190 | |
| 191 | memset(&inarg, 0, sizeof(inarg)); |
| 192 | inarg.mode = mode; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 193 | inarg.rdev = new_encode_dev(rdev); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 194 | |
| 195 | in.h.opcode = FUSE_MKNOD; |
| 196 | in.h.ino = dir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 197 | in.numargs = 2; |
| 198 | in.args[0].size = sizeof(inarg); |
| 199 | in.args[0].value = &inarg; |
| 200 | in.args[1].size = entry->d_name.len + 1; |
| 201 | in.args[1].value = entry->d_name.name; |
| 202 | out.numargs = 1; |
| 203 | out.args[0].size = sizeof(outarg); |
| 204 | out.args[0].value = &outarg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 205 | request_send(fc, &in, &out); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 206 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 207 | if(out.h.error) |
| 208 | return out.h.error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 209 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 210 | return lookup_new_entry(dir, entry, &outarg, out.h.unique, mode); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 213 | static int _fuse_create(struct inode *dir, struct dentry *entry, int mode) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 214 | { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 215 | return _fuse_mknod(dir, entry, mode, 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 218 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 219 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) |
| 220 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 221 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 222 | struct fuse_in in = FUSE_IN_INIT; |
| 223 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 224 | struct fuse_mkdir_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 225 | struct fuse_entry_out outarg; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 226 | |
| 227 | memset(&inarg, 0, sizeof(inarg)); |
| 228 | inarg.mode = mode; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 229 | |
| 230 | in.h.opcode = FUSE_MKDIR; |
| 231 | in.h.ino = dir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 232 | in.numargs = 2; |
| 233 | in.args[0].size = sizeof(inarg); |
| 234 | in.args[0].value = &inarg; |
| 235 | in.args[1].size = entry->d_name.len + 1; |
| 236 | in.args[1].value = entry->d_name.name; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 237 | out.numargs = 1; |
| 238 | out.args[0].size = sizeof(outarg); |
| 239 | out.args[0].value = &outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 240 | request_send(fc, &in, &out); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 241 | if(out.h.error) |
| 242 | return out.h.error; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 243 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 244 | return lookup_new_entry(dir, entry, &outarg, out.h.unique, S_IFDIR); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static int fuse_symlink(struct inode *dir, struct dentry *entry, |
| 248 | const char *link) |
| 249 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 250 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 251 | struct fuse_in in = FUSE_IN_INIT; |
| 252 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 253 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 254 | |
| 255 | in.h.opcode = FUSE_SYMLINK; |
| 256 | in.h.ino = dir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 257 | in.numargs = 2; |
| 258 | in.args[0].size = entry->d_name.len + 1; |
| 259 | in.args[0].value = entry->d_name.name; |
| 260 | in.args[1].size = strlen(link) + 1; |
| 261 | in.args[1].value = link; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 262 | out.numargs = 1; |
| 263 | out.args[0].size = sizeof(outarg); |
| 264 | out.args[0].value = &outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 265 | request_send(fc, &in, &out); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 266 | if(out.h.error) |
| 267 | return out.h.error; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 268 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 269 | return lookup_new_entry(dir, entry, &outarg, out.h.unique, S_IFLNK); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 272 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 273 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 274 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 275 | struct fuse_in in = FUSE_IN_INIT; |
| 276 | struct fuse_out out = FUSE_OUT_INIT; |
| 277 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 278 | in.h.opcode = FUSE_UNLINK; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 279 | in.h.ino = dir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 280 | in.numargs = 1; |
| 281 | in.args[0].size = entry->d_name.len + 1; |
| 282 | in.args[0].value = entry->d_name.name; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 283 | request_send(fc, &in, &out); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 285 | if(!out.h.error) { |
| 286 | /* Set nlink to zero so the inode can be cleared, if |
| 287 | the inode does have more links this will be |
| 288 | discovered at the next lookup/getattr */ |
| 289 | /* FIXME: mark inode "not uptodate" */ |
| 290 | entry->d_inode->i_nlink = 0; |
Miklos Szeredi | f4f8b89 | 2004-01-27 17:04:59 +0000 | [diff] [blame] | 291 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 292 | |
| 293 | return out.h.error; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) |
| 297 | { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 298 | struct fuse_conn *fc = INO_FC(dir); |
| 299 | struct fuse_in in = FUSE_IN_INIT; |
| 300 | struct fuse_out out = FUSE_OUT_INIT; |
| 301 | |
| 302 | in.h.opcode = FUSE_RMDIR; |
| 303 | in.h.ino = dir->i_ino; |
| 304 | in.numargs = 1; |
| 305 | in.args[0].size = entry->d_name.len + 1; |
| 306 | in.args[0].value = entry->d_name.name; |
| 307 | request_send(fc, &in, &out); |
| 308 | if(!out.h.error) |
Miklos Szeredi | f4f8b89 | 2004-01-27 17:04:59 +0000 | [diff] [blame] | 309 | entry->d_inode->i_nlink = 0; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 310 | |
| 311 | return out.h.error; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, |
| 315 | struct inode *newdir, struct dentry *newent) |
| 316 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 317 | struct fuse_conn *fc = INO_FC(olddir); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 318 | struct fuse_in in = FUSE_IN_INIT; |
| 319 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 320 | struct fuse_rename_in inarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 321 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 322 | memset(&inarg, 0, sizeof(inarg)); |
| 323 | inarg.newdir = newdir->i_ino; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 324 | |
| 325 | in.h.opcode = FUSE_RENAME; |
| 326 | in.h.ino = olddir->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 327 | in.numargs = 3; |
| 328 | in.args[0].size = sizeof(inarg); |
| 329 | in.args[0].value = &inarg; |
| 330 | in.args[1].size = oldent->d_name.len + 1; |
| 331 | in.args[1].value = oldent->d_name.name; |
| 332 | in.args[2].size = newent->d_name.len + 1; |
| 333 | in.args[2].value = newent->d_name.name; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 334 | request_send(fc, &in, &out); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 335 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 336 | return out.h.error; |
| 337 | } |
| 338 | |
| 339 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
| 340 | struct dentry *newent) |
| 341 | { |
| 342 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 343 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 344 | struct fuse_in in = FUSE_IN_INIT; |
| 345 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 346 | struct fuse_link_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 347 | struct fuse_entry_out outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 348 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 349 | memset(&inarg, 0, sizeof(inarg)); |
| 350 | inarg.newdir = newdir->i_ino; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 351 | |
| 352 | in.h.opcode = FUSE_LINK; |
| 353 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 354 | in.numargs = 2; |
| 355 | in.args[0].size = sizeof(inarg); |
| 356 | in.args[0].value = &inarg; |
| 357 | in.args[1].size = newent->d_name.len + 1; |
| 358 | in.args[1].value = newent->d_name.name; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 359 | out.numargs = 1; |
| 360 | out.args[0].size = sizeof(outarg); |
| 361 | out.args[0].value = &outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 362 | request_send(fc, &in, &out); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 363 | if(out.h.error) |
| 364 | return out.h.error; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 365 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 366 | /* Invalidate old entry, so attributes are refreshed */ |
| 367 | d_invalidate(entry); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 368 | return lookup_new_entry(newdir, newent, &outarg, out.h.unique, |
| 369 | inode->i_mode); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 372 | int fuse_do_getattr(struct inode *inode) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 373 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 374 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 375 | struct fuse_in in = FUSE_IN_INIT; |
| 376 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 377 | struct fuse_attr_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 378 | |
| 379 | in.h.opcode = FUSE_GETATTR; |
| 380 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 381 | out.numargs = 1; |
| 382 | out.args[0].size = sizeof(arg); |
| 383 | out.args[0].value = &arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 384 | request_send(fc, &in, &out); |
| 385 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 386 | if(!out.h.error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 387 | change_attributes(inode, &arg.attr); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 388 | |
| 389 | return out.h.error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 392 | static int fuse_revalidate(struct dentry *entry) |
| 393 | { |
| 394 | struct inode *inode = entry->d_inode; |
| 395 | struct fuse_conn *fc = INO_FC(inode); |
| 396 | |
| 397 | if(inode->i_ino == FUSE_ROOT_INO) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 398 | if(!(fc->flags & FUSE_ALLOW_OTHER) && |
| 399 | current->fsuid != fc->uid) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 400 | return -EACCES; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 401 | } else if(!entry->d_time || time_before_eq(jiffies, entry->d_time)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 402 | return 0; |
| 403 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 404 | return fuse_do_getattr(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 407 | static int _fuse_permission(struct inode *inode, int mask) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 408 | { |
| 409 | struct fuse_conn *fc = INO_FC(inode); |
| 410 | |
| 411 | if(!(fc->flags & FUSE_ALLOW_OTHER) && current->fsuid != fc->uid) |
| 412 | return -EACCES; |
| 413 | else if(fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
| 414 | int err = vfs_permission(inode, mask); |
| 415 | |
| 416 | /* If permission is denied, try to refresh file |
| 417 | attributes. This is also needed, because the root |
| 418 | node will at first have no permissions */ |
| 419 | |
| 420 | if(err == -EACCES) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 421 | err = fuse_do_getattr(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 422 | if(!err) |
| 423 | err = vfs_permission(inode, mask); |
| 424 | } |
| 425 | |
| 426 | /* FIXME: Need some mechanism to revoke permissions: |
| 427 | currently if the filesystem suddenly changes the |
| 428 | file mode, we will not be informed abot that, and |
| 429 | continue to allow access to the file/directory. |
| 430 | |
| 431 | This is actually not so grave, since the user can |
| 432 | simply keep access to the file/directory anyway by |
| 433 | keeping it open... */ |
| 434 | |
| 435 | return err; |
Miklos Szeredi | 69cc79a | 2004-02-17 08:57:29 +0000 | [diff] [blame] | 436 | } else |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 440 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, |
| 441 | void *dstbuf, filldir_t filldir) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 442 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 443 | while(nbytes >= FUSE_NAME_OFFSET) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 444 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 445 | size_t reclen = FUSE_DIRENT_SIZE(dirent); |
| 446 | int over; |
| 447 | if(dirent->namelen > NAME_MAX) { |
| 448 | printk("fuse_readdir: name too long\n"); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 449 | return -EPROTO; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 450 | } |
| 451 | if(reclen > nbytes) |
| 452 | break; |
| 453 | |
| 454 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
| 455 | file->f_pos, dirent->ino, dirent->type); |
| 456 | if(over) |
| 457 | break; |
| 458 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 459 | buf += reclen; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 460 | file->f_pos += reclen; |
| 461 | nbytes -= reclen; |
| 462 | } |
| 463 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | #define DIR_BUFSIZE 2048 |
| 468 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
| 469 | { |
| 470 | struct file *cfile = file->private_data; |
| 471 | char *buf; |
| 472 | int ret; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 473 | |
| 474 | if(!cfile) |
| 475 | return -EISDIR; |
| 476 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 477 | buf = kmalloc(DIR_BUFSIZE, GFP_KERNEL); |
| 478 | if(!buf) |
| 479 | return -ENOMEM; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 480 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 481 | ret = kernel_read(cfile, file->f_pos, buf, DIR_BUFSIZE); |
| 482 | if(ret < 0) |
| 483 | printk("fuse_readdir: failed to read container file\n"); |
| 484 | else |
| 485 | ret = parse_dirfile(buf, ret, file, dstbuf, filldir); |
| 486 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 487 | kfree(buf); |
| 488 | return ret; |
| 489 | } |
| 490 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 491 | static char *read_link(struct dentry *dentry) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 492 | { |
| 493 | struct inode *inode = dentry->d_inode; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 494 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 495 | struct fuse_in in = FUSE_IN_INIT; |
| 496 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 497 | char *link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 498 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 499 | link = (char *) __get_free_page(GFP_KERNEL); |
| 500 | if(!link) |
| 501 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 502 | |
| 503 | in.h.opcode = FUSE_READLINK; |
| 504 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 505 | out.argvar = 1; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 506 | out.numargs = 1; |
| 507 | out.args[0].size = PAGE_SIZE - 1; |
| 508 | out.args[0].value = link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 509 | request_send(fc, &in, &out); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 510 | if(out.h.error) { |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 511 | free_page((unsigned long) link); |
| 512 | return ERR_PTR(out.h.error); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 515 | link[out.args[0].size] = '\0'; |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 516 | return link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void free_link(char *link) |
| 520 | { |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 521 | if(!IS_ERR(link)) |
| 522 | free_page((unsigned long) link); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static int fuse_readlink(struct dentry *dentry, char *buffer, int buflen) |
| 526 | { |
| 527 | int ret; |
| 528 | char *link; |
| 529 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 530 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 531 | ret = vfs_readlink(dentry, buffer, buflen, link); |
| 532 | free_link(link); |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 537 | { |
| 538 | int ret; |
| 539 | char *link; |
| 540 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 541 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 542 | ret = vfs_follow_link(nd, link); |
| 543 | free_link(link); |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static int fuse_dir_open(struct inode *inode, struct file *file) |
| 548 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 549 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 550 | struct fuse_in in = FUSE_IN_INIT; |
| 551 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 552 | struct fuse_getdir_out_i outarg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 553 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 554 | in.h.opcode = FUSE_GETDIR; |
| 555 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 556 | out.numargs = 1; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 557 | out.args[0].size = sizeof(struct fuse_getdir_out); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 558 | out.args[0].value = &outarg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 559 | request_send(fc, &in, &out); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 560 | if(!out.h.error) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 561 | struct file *cfile = outarg.file; |
| 562 | struct inode *inode; |
| 563 | if(!cfile) { |
| 564 | printk("fuse_getdir: invalid file\n"); |
| 565 | return -EPROTO; |
| 566 | } |
| 567 | inode = cfile->f_dentry->d_inode; |
| 568 | if(!S_ISREG(inode->i_mode)) { |
| 569 | printk("fuse_getdir: not a regular file\n"); |
| 570 | fput(cfile); |
| 571 | return -EPROTO; |
| 572 | } |
| 573 | |
| 574 | file->private_data = cfile; |
| 575 | } |
| 576 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 577 | return out.h.error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static int fuse_dir_release(struct inode *inode, struct file *file) |
| 581 | { |
| 582 | struct file *cfile = file->private_data; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 583 | |
| 584 | if(cfile) |
| 585 | fput(cfile); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 590 | static unsigned int iattr_to_fattr(struct iattr *iattr, |
| 591 | struct fuse_attr *fattr) |
| 592 | { |
| 593 | unsigned int ivalid = iattr->ia_valid; |
| 594 | unsigned int fvalid = 0; |
| 595 | |
| 596 | memset(fattr, 0, sizeof(*fattr)); |
| 597 | |
| 598 | if(ivalid & ATTR_MODE) |
| 599 | fvalid |= FATTR_MODE, fattr->mode = iattr->ia_mode; |
| 600 | if(ivalid & ATTR_UID) |
| 601 | fvalid |= FATTR_UID, fattr->uid = iattr->ia_uid; |
| 602 | if(ivalid & ATTR_GID) |
| 603 | fvalid |= FATTR_GID, fattr->gid = iattr->ia_gid; |
| 604 | if(ivalid & ATTR_SIZE) |
| 605 | fvalid |= FATTR_SIZE, fattr->size = iattr->ia_size; |
| 606 | /* You can only _set_ these together (they may change by themselves) */ |
| 607 | if((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 608 | fvalid |= FATTR_ATIME | FATTR_MTIME; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 609 | #ifdef KERNEL_2_6 |
| 610 | fattr->atime = iattr->ia_atime.tv_sec; |
| 611 | fattr->mtime = iattr->ia_mtime.tv_sec; |
| 612 | #else |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 613 | fattr->atime = iattr->ia_atime; |
| 614 | fattr->mtime = iattr->ia_mtime; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 615 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | return fvalid; |
| 619 | } |
| 620 | |
| 621 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
| 622 | { |
| 623 | struct inode *inode = entry->d_inode; |
| 624 | struct fuse_conn *fc = INO_FC(inode); |
| 625 | struct fuse_in in = FUSE_IN_INIT; |
| 626 | struct fuse_out out = FUSE_OUT_INIT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 627 | struct fuse_setattr_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 628 | struct fuse_attr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 629 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 630 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 631 | inarg.valid = iattr_to_fattr(attr, &inarg.attr); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 632 | |
| 633 | in.h.opcode = FUSE_SETATTR; |
| 634 | in.h.ino = inode->i_ino; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 635 | in.numargs = 1; |
| 636 | in.args[0].size = sizeof(inarg); |
| 637 | in.args[0].value = &inarg; |
| 638 | out.numargs = 1; |
| 639 | out.args[0].size = sizeof(outarg); |
| 640 | out.args[0].value = &outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 641 | request_send(fc, &in, &out); |
| 642 | |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 643 | if(!out.h.error) { |
| 644 | if(attr->ia_valid & ATTR_SIZE && |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 645 | outarg.attr.size < i_size_read(inode)) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 646 | vmtruncate(inode, outarg.attr.size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 647 | |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 648 | change_attributes(inode, &outarg.attr); |
| 649 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 650 | return out.h.error; |
| 651 | } |
| 652 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 653 | static int _fuse_dentry_revalidate(struct dentry *entry) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 654 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 655 | if(!entry->d_inode) |
| 656 | return 0; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 657 | else if(entry->d_time && time_after(jiffies, entry->d_time)) { |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 658 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 659 | struct fuse_entry_out outarg; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 660 | int version; |
| 661 | int ret; |
| 662 | |
| 663 | ret = fuse_do_lookup(entry->d_parent->d_inode, entry, &outarg, |
| 664 | &version); |
| 665 | if(ret) |
| 666 | return 0; |
| 667 | |
| 668 | if(outarg.ino != inode->i_ino) |
| 669 | return 0; |
| 670 | |
| 671 | change_attributes(inode, &outarg.attr); |
| 672 | inode->i_version = version; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame^] | 673 | entry->d_time = time_to_jiffies(outarg.entry_valid, |
| 674 | outarg.entry_valid_nsec); |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 675 | } |
| 676 | return 1; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 679 | #ifdef KERNEL_2_6 |
| 680 | |
| 681 | #define fuse_mknod _fuse_mknod |
| 682 | |
| 683 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
| 684 | struct kstat *stat) |
| 685 | { |
| 686 | struct inode *inode = entry->d_inode; |
| 687 | int err = fuse_revalidate(entry); |
| 688 | if(!err) |
| 689 | generic_fillattr(inode, stat); |
| 690 | |
| 691 | return err; |
| 692 | } |
| 693 | |
| 694 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
| 695 | struct nameidata *nd) |
| 696 | { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 697 | struct inode *inode; |
| 698 | int err = fuse_lookup_iget(dir, entry, &inode); |
| 699 | if (err) |
| 700 | return ERR_PTR(err); |
| 701 | return d_splice_alias(inode, entry); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, |
| 705 | struct nameidata *nd) |
| 706 | { |
| 707 | return _fuse_create(dir, entry, mode); |
| 708 | } |
| 709 | |
| 710 | static int fuse_permission(struct inode *inode, int mask, |
| 711 | struct nameidata *nd) |
| 712 | { |
| 713 | return _fuse_permission(inode, mask); |
| 714 | } |
| 715 | |
| 716 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
| 717 | { |
| 718 | return _fuse_dentry_revalidate(entry); |
| 719 | } |
| 720 | #else /* KERNEL_2_6 */ |
| 721 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 722 | #define fuse_create _fuse_create |
| 723 | #define fuse_permission _fuse_permission |
| 724 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 725 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry) |
| 726 | { |
| 727 | struct inode *inode; |
| 728 | struct dentry *alias; |
| 729 | |
| 730 | int err = fuse_lookup_iget(dir, entry, &inode); |
| 731 | if(err) |
| 732 | return ERR_PTR(err); |
| 733 | |
| 734 | if(inode && S_ISDIR(inode->i_mode) && |
| 735 | (alias = d_find_alias(inode)) != NULL) { |
| 736 | dput(alias); |
| 737 | iput(inode); |
| 738 | printk("fuse: cannot assign an existing directory\n"); |
Miklos Szeredi | d23b70a | 2004-01-26 12:17:53 +0000 | [diff] [blame] | 739 | return ERR_PTR(-EPROTO); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | d_add(entry, inode); |
| 743 | return NULL; |
| 744 | } |
| 745 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 746 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, |
| 747 | int rdev) |
| 748 | { |
| 749 | return fuse_mknod(dir, entry, mode, rdev); |
| 750 | } |
| 751 | |
| 752 | static int fuse_dentry_revalidate(struct dentry *entry, int flags) |
| 753 | { |
| 754 | return _fuse_dentry_revalidate(entry); |
| 755 | } |
| 756 | #endif /* KERNEL_2_6 */ |
| 757 | |
| 758 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 759 | static struct inode_operations fuse_dir_inode_operations = |
| 760 | { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 761 | .lookup = fuse_lookup, |
| 762 | .create = fuse_create, |
| 763 | .mknod = fuse_mknod, |
| 764 | .mkdir = fuse_mkdir, |
| 765 | .symlink = fuse_symlink, |
| 766 | .unlink = fuse_unlink, |
| 767 | .rmdir = fuse_rmdir, |
| 768 | .rename = fuse_rename, |
| 769 | .link = fuse_link, |
| 770 | .setattr = fuse_setattr, |
| 771 | .permission = fuse_permission, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 772 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 773 | .getattr = fuse_getattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 774 | #else |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 775 | .revalidate = fuse_revalidate, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 776 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 777 | }; |
| 778 | |
| 779 | static struct file_operations fuse_dir_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 780 | .read = generic_read_dir, |
| 781 | .readdir = fuse_readdir, |
| 782 | .open = fuse_dir_open, |
| 783 | .release = fuse_dir_release, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 784 | }; |
| 785 | |
| 786 | static struct inode_operations fuse_file_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 787 | .setattr = fuse_setattr, |
| 788 | .permission = fuse_permission, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 789 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 790 | .getattr = fuse_getattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 791 | #else |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 792 | .revalidate = fuse_revalidate, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 793 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 794 | }; |
| 795 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 796 | static struct inode_operations fuse_symlink_inode_operations = |
| 797 | { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 798 | .setattr = fuse_setattr, |
| 799 | .readlink = fuse_readlink, |
| 800 | .follow_link = fuse_follow_link, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 801 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 802 | .getattr = fuse_getattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 803 | #else |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 804 | .revalidate = fuse_revalidate, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 805 | #endif |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 806 | }; |
| 807 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 808 | static struct dentry_operations fuse_dentry_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 809 | .d_revalidate = fuse_dentry_revalidate, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 810 | }; |
| 811 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 812 | /* |
| 813 | * Local Variables: |
| 814 | * indent-tabs-mode: t |
| 815 | * c-basic-offset: 8 |
| 816 | * End: |
| 817 | */ |