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