Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 2e6b6f2 | 2004-07-07 19:19:53 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 12 | #include <linux/file.h> |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 13 | #ifdef KERNEL_2_6 |
| 14 | #include <linux/gfp.h> |
| 15 | #else |
| 16 | #include <linux/mm.h> |
| 17 | #endif |
| 18 | #include <linux/sched.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 20 | static struct inode_operations fuse_dir_inode_operations; |
| 21 | static struct inode_operations fuse_file_inode_operations; |
| 22 | static struct inode_operations fuse_symlink_inode_operations; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 23 | static struct file_operations fuse_dir_operations; |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 24 | static struct dentry_operations fuse_dentry_operations; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +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 |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 30 | static void change_attributes(struct inode *inode, struct fuse_attr *attr) |
| 31 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 32 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 33 | #ifdef KERNEL_2_6 |
| 34 | invalidate_inode_pages(inode->i_mapping); |
| 35 | #else |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 36 | invalidate_inode_pages(inode); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 37 | #endif |
| 38 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 39 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 40 | inode->i_ino = attr->ino; |
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; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 45 | i_size_write(inode, 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; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 50 | inode->i_atime.tv_nsec = attr->atimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 51 | inode->i_mtime.tv_sec = attr->mtime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 52 | inode->i_mtime.tv_nsec = attr->mtimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 53 | inode->i_ctime.tv_sec = attr->ctime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 54 | inode->i_ctime.tv_nsec = attr->ctimensec; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 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; |
Miklos Szeredi | d1199f8 | 2004-02-06 15:29:22 +0000 | [diff] [blame] | 65 | i_size_write(inode, attr->size); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 66 | if (S_ISREG(inode->i_mode)) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 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 | } |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 70 | else if (S_ISDIR(inode->i_mode)) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 71 | inode->i_op = &fuse_dir_inode_operations; |
| 72 | inode->i_fop = &fuse_dir_operations; |
| 73 | } |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +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 | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 77 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 78 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 79 | inode->i_op = &fuse_file_inode_operations; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 80 | init_special_inode(inode, inode->i_mode, |
| 81 | new_decode_dev(attr->rdev)); |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 82 | } else { |
| 83 | /* Don't let user create weird files */ |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 84 | printk("fuse_init_inode: bad file type: %o\n", inode->i_mode); |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 85 | inode->i_mode = S_IFREG; |
| 86 | inode->i_op = &fuse_file_inode_operations; |
| 87 | fuse_init_file_inode(inode); |
| 88 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 91 | #ifdef KERNEL_2_6 |
| 92 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) |
| 93 | { |
| 94 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 95 | struct fuse_inode *fi = INO_FI(inode); |
| 96 | if (fi->nodeid == nodeid) |
| 97 | return 1; |
| 98 | else |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) |
| 103 | { |
| 104 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 105 | struct fuse_inode *fi = INO_FI(inode); |
| 106 | fi->nodeid = nodeid; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
| 111 | int generation, struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 112 | { |
| 113 | struct inode *inode; |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 114 | struct fuse_conn *fc = SB_FC(sb); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 115 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 116 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); |
| 117 | if (!inode) |
| 118 | return NULL; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 119 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 120 | if ((inode->i_state & I_NEW)) { |
| 121 | inode->i_generation = generation; |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 122 | inode->i_data.backing_dev_info = &fc->bdi; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 123 | fuse_init_inode(inode, attr); |
| 124 | unlock_new_inode(inode); |
| 125 | } else if (inode->i_generation != generation) |
| 126 | printk("fuse_iget: bad generation for node %lu\n", nodeid); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 127 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 128 | change_attributes(inode, attr); |
| 129 | inode->i_version = version; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 130 | return inode; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 133 | struct inode *fuse_ilookup(struct super_block *sb, unsigned long nodeid) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 134 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 135 | return ilookup5(sb, nodeid, fuse_inode_eq, &nodeid); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 136 | } |
| 137 | #else |
| 138 | static int fuse_inode_eq(struct inode *inode, unsigned long ino, void *_nodeidp){ |
| 139 | unsigned long nodeid = *(unsigned long *) _nodeidp; |
| 140 | struct fuse_inode *fi = INO_FI(inode); |
| 141 | if (inode->u.generic_ip && fi->nodeid == nodeid) |
| 142 | return 1; |
| 143 | else |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, |
| 148 | int generation, struct fuse_attr *attr, int version) |
| 149 | { |
| 150 | struct inode *inode; |
| 151 | |
| 152 | inode = iget4(sb, attr->ino, fuse_inode_eq, &nodeid); |
| 153 | if (!inode) |
| 154 | return NULL; |
| 155 | |
| 156 | if (!inode->u.generic_ip) { |
| 157 | struct fuse_inode *fi = INO_FI(inode); |
| 158 | fi->nodeid = nodeid; |
| 159 | inode->u.generic_ip = inode; |
| 160 | inode->i_generation = generation; |
| 161 | fuse_init_inode(inode, attr); |
| 162 | } else if (inode->i_generation != generation) |
| 163 | printk("fuse_iget: bad generation for node %lu\n", nodeid); |
| 164 | |
| 165 | change_attributes(inode, attr); |
| 166 | inode->i_version = version; |
| 167 | return inode; |
| 168 | } |
| 169 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 170 | struct inode *fuse_ilookup(struct super_block *sb, ino_t ino, unsigned long nodeid) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 171 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 172 | struct inode *inode = iget4(sb, ino, fuse_inode_eq, &nodeid); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 173 | if (inode && !inode->u.generic_ip) { |
| 174 | iput(inode); |
| 175 | inode = NULL; |
| 176 | } |
| 177 | return inode; |
| 178 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 181 | static int fuse_send_lookup(struct fuse_conn *fc, struct fuse_req *req, |
| 182 | struct inode *dir, struct dentry *entry, |
| 183 | struct fuse_entry_out *outarg, int *version) |
| 184 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 185 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 186 | req->in.h.opcode = FUSE_LOOKUP; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 187 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 188 | req->in.numargs = 1; |
| 189 | req->in.args[0].size = entry->d_name.len + 1; |
| 190 | req->in.args[0].value = entry->d_name.name; |
| 191 | req->out.numargs = 1; |
| 192 | req->out.args[0].size = sizeof(struct fuse_entry_out); |
| 193 | req->out.args[0].value = outarg; |
| 194 | request_send(fc, req); |
| 195 | *version = req->out.h.unique; |
| 196 | return req->out.h.error; |
| 197 | } |
| 198 | |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 199 | static int fuse_do_lookup(struct inode *dir, struct dentry *entry, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 200 | struct fuse_entry_out *outarg, int *version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 201 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 202 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 203 | struct fuse_req *req; |
| 204 | int err; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 205 | |
| 206 | if (entry->d_name.len > FUSE_NAME_MAX) |
| 207 | return -ENAMETOOLONG; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 208 | req = fuse_get_request(fc); |
| 209 | if (!req) |
| 210 | return -ERESTARTSYS; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 211 | |
| 212 | err = fuse_send_lookup(fc, req, dir, entry, outarg, version); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 213 | fuse_put_request(fc, req); |
| 214 | return err; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 217 | static inline unsigned long time_to_jiffies(unsigned long sec, |
| 218 | unsigned long nsec) |
| 219 | { |
| 220 | /* prevent wrapping of jiffies */ |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 221 | if (sec + 1 >= LONG_MAX / HZ) |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | |
| 224 | return jiffies + sec * HZ + nsec / (1000000000 / HZ); |
| 225 | } |
| 226 | |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 227 | static int fuse_lookup_iget(struct inode *dir, struct dentry *entry, |
| 228 | struct inode **inodep) |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 229 | { |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 230 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 231 | int err; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 232 | struct fuse_entry_out outarg; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 233 | int version; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 234 | struct inode *inode = NULL; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 235 | struct fuse_req *req; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 236 | |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 237 | if (entry->d_name.len > FUSE_NAME_MAX) |
| 238 | return -ENAMETOOLONG; |
| 239 | req = fuse_get_request(fc); |
| 240 | if (!req) |
| 241 | return -ERESTARTSYS; |
| 242 | |
| 243 | err = fuse_send_lookup(fc, req, dir, entry, &outarg, &version); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 244 | if (!err) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 245 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 246 | &outarg.attr, version); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 247 | if (!inode) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 248 | fuse_send_forget(fc, req, outarg.nodeid, version); |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 249 | return -ENOMEM; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | fuse_put_request(fc, req); |
| 253 | if (err && err != -ENOENT) |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 254 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 255 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 256 | if (inode) { |
| 257 | struct fuse_inode *fi = INO_FI(inode); |
| 258 | entry->d_time = time_to_jiffies(outarg.entry_valid, |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 259 | outarg.entry_valid_nsec); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 260 | fi->i_time = time_to_jiffies(outarg.attr_valid, |
| 261 | outarg.attr_valid_nsec); |
| 262 | } |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 263 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 264 | entry->d_op = &fuse_dentry_operations; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 265 | *inodep = inode; |
| 266 | return 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 269 | static void fuse_invalidate_attr(struct inode *inode) |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 270 | { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 271 | struct fuse_inode *fi = INO_FI(inode); |
| 272 | fi->i_time = jiffies - 1; |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 275 | static int lookup_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
| 276 | struct inode *dir, struct dentry *entry, |
| 277 | struct fuse_entry_out *outarg, int version, |
| 278 | int mode) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 279 | { |
| 280 | struct inode *inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 281 | struct fuse_inode *fi; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 282 | inode = fuse_iget(dir->i_sb, outarg->nodeid, outarg->generation, |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 283 | &outarg->attr, version); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 284 | if (!inode) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 285 | fuse_send_forget(fc, req, outarg->nodeid, version); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 286 | return -ENOMEM; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 287 | } |
| 288 | fuse_put_request(fc, req); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 289 | |
| 290 | /* Don't allow userspace to do really stupid things... */ |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 291 | if ((inode->i_mode ^ mode) & S_IFMT) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 292 | iput(inode); |
| 293 | printk("fuse_mknod: inode has wrong type\n"); |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 294 | return -EPROTO; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 297 | entry->d_time = time_to_jiffies(outarg->entry_valid, |
| 298 | outarg->entry_valid_nsec); |
| 299 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 300 | fi = INO_FI(inode); |
| 301 | fi->i_time = time_to_jiffies(outarg->attr_valid, |
| 302 | outarg->attr_valid_nsec); |
| 303 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 304 | d_instantiate(entry, inode); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 305 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 309 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 310 | dev_t rdev) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 311 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 312 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 313 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 314 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 315 | struct fuse_mknod_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 316 | struct fuse_entry_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 317 | int err; |
| 318 | |
| 319 | if (!req) |
| 320 | return -ERESTARTSYS; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 321 | |
| 322 | memset(&inarg, 0, sizeof(inarg)); |
| 323 | inarg.mode = mode; |
Miklos Szeredi | 7c35cf9 | 2004-01-14 16:56:49 +0000 | [diff] [blame] | 324 | inarg.rdev = new_encode_dev(rdev); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 325 | req->in.h.opcode = FUSE_MKNOD; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 326 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 327 | req->in.numargs = 2; |
| 328 | req->in.args[0].size = sizeof(inarg); |
| 329 | req->in.args[0].value = &inarg; |
| 330 | req->in.args[1].size = entry->d_name.len + 1; |
| 331 | req->in.args[1].value = entry->d_name.name; |
| 332 | req->out.numargs = 1; |
| 333 | req->out.args[0].size = sizeof(outarg); |
| 334 | req->out.args[0].value = &outarg; |
| 335 | request_send(fc, req); |
| 336 | err = req->out.h.error; |
| 337 | if (!err) |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 338 | err = lookup_new_entry(fc, req, dir, entry, &outarg, |
| 339 | req->out.h.unique, mode); |
| 340 | else |
| 341 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 342 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 345 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, |
| 346 | struct nameidata *nd) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 347 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 348 | return fuse_mknod(dir, entry, mode, 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 351 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) |
| 352 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 353 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 354 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 355 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 356 | struct fuse_mkdir_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 357 | struct fuse_entry_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 358 | int err; |
| 359 | |
| 360 | if (!req) |
| 361 | return -ERESTARTSYS; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 362 | |
| 363 | memset(&inarg, 0, sizeof(inarg)); |
| 364 | inarg.mode = mode; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 365 | req->in.h.opcode = FUSE_MKDIR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 366 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 367 | req->in.numargs = 2; |
| 368 | req->in.args[0].size = sizeof(inarg); |
| 369 | req->in.args[0].value = &inarg; |
| 370 | req->in.args[1].size = entry->d_name.len + 1; |
| 371 | req->in.args[1].value = entry->d_name.name; |
| 372 | req->out.numargs = 1; |
| 373 | req->out.args[0].size = sizeof(outarg); |
| 374 | req->out.args[0].value = &outarg; |
| 375 | request_send(fc, req); |
| 376 | err = req->out.h.error; |
| 377 | if (!err) |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 378 | err = lookup_new_entry(fc, req, dir, entry, &outarg, |
| 379 | req->out.h.unique, S_IFDIR); |
| 380 | else |
| 381 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 382 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static int fuse_symlink(struct inode *dir, struct dentry *entry, |
| 386 | const char *link) |
| 387 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 388 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 389 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 390 | struct fuse_req *req; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 391 | struct fuse_entry_out outarg; |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 392 | unsigned len = strlen(link) + 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 393 | int err; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 394 | |
| 395 | if (len > FUSE_SYMLINK_MAX) |
| 396 | return -ENAMETOOLONG; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 397 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 398 | req = fuse_get_request(fc); |
| 399 | if (!req) |
| 400 | return -ERESTARTSYS; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 401 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 402 | req->in.h.opcode = FUSE_SYMLINK; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 403 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 404 | req->in.numargs = 2; |
| 405 | req->in.args[0].size = entry->d_name.len + 1; |
| 406 | req->in.args[0].value = entry->d_name.name; |
| 407 | req->in.args[1].size = len; |
| 408 | req->in.args[1].value = link; |
| 409 | req->out.numargs = 1; |
| 410 | req->out.args[0].size = sizeof(outarg); |
| 411 | req->out.args[0].value = &outarg; |
| 412 | request_send(fc, req); |
| 413 | err = req->out.h.error; |
| 414 | if (!err) |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 415 | err = lookup_new_entry(fc, req, dir, entry, &outarg, |
| 416 | req->out.h.unique, S_IFLNK); |
| 417 | else |
| 418 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 419 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 422 | static int fuse_unlink(struct inode *dir, struct dentry *entry) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 423 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 424 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 425 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 426 | struct fuse_req *req = fuse_get_request(fc); |
| 427 | int err; |
| 428 | |
| 429 | if (!req) |
| 430 | return -ERESTARTSYS; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 431 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 432 | req->in.h.opcode = FUSE_UNLINK; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 433 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 434 | req->in.numargs = 1; |
| 435 | req->in.args[0].size = entry->d_name.len + 1; |
| 436 | req->in.args[0].value = entry->d_name.name; |
| 437 | request_send(fc, req); |
| 438 | err = req->out.h.error; |
| 439 | if (!err) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 440 | struct inode *inode = entry->d_inode; |
| 441 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 442 | /* Set nlink to zero so the inode can be cleared, if |
| 443 | the inode does have more links this will be |
| 444 | discovered at the next lookup/getattr */ |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 445 | inode->i_nlink = 0; |
| 446 | fuse_invalidate_attr(inode); |
| 447 | fuse_invalidate_attr(dir); |
Miklos Szeredi | f4f8b89 | 2004-01-27 17:04:59 +0000 | [diff] [blame] | 448 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 449 | fuse_put_request(fc, req); |
| 450 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) |
| 454 | { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 455 | struct fuse_conn *fc = INO_FC(dir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 456 | struct fuse_inode *fi = INO_FI(dir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 457 | struct fuse_req *req = fuse_get_request(fc); |
| 458 | int err; |
| 459 | |
| 460 | if (!req) |
| 461 | return -ERESTARTSYS; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 462 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 463 | req->in.h.opcode = FUSE_RMDIR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 464 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 465 | req->in.numargs = 1; |
| 466 | req->in.args[0].size = entry->d_name.len + 1; |
| 467 | req->in.args[0].value = entry->d_name.name; |
| 468 | request_send(fc, req); |
| 469 | err = req->out.h.error; |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 470 | if (!err) { |
Miklos Szeredi | f4f8b89 | 2004-01-27 17:04:59 +0000 | [diff] [blame] | 471 | entry->d_inode->i_nlink = 0; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 472 | fuse_invalidate_attr(dir); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 473 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 474 | fuse_put_request(fc, req); |
| 475 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, |
| 479 | struct inode *newdir, struct dentry *newent) |
| 480 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 481 | struct fuse_conn *fc = INO_FC(olddir); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 482 | struct fuse_inode *oldfi = INO_FI(olddir); |
| 483 | struct fuse_inode *newfi = INO_FI(newdir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 484 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 485 | struct fuse_rename_in inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 486 | int err; |
| 487 | |
| 488 | if (!req) |
| 489 | return -ERESTARTSYS; |
| 490 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 491 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 492 | inarg.newdir = newfi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 493 | req->in.h.opcode = FUSE_RENAME; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 494 | req->in.h.nodeid = oldfi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 495 | req->in.numargs = 3; |
| 496 | req->in.args[0].size = sizeof(inarg); |
| 497 | req->in.args[0].value = &inarg; |
| 498 | req->in.args[1].size = oldent->d_name.len + 1; |
| 499 | req->in.args[1].value = oldent->d_name.name; |
| 500 | req->in.args[2].size = newent->d_name.len + 1; |
| 501 | req->in.args[2].value = newent->d_name.name; |
| 502 | request_send(fc, req); |
| 503 | err = req->out.h.error; |
| 504 | fuse_put_request(fc, req); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 505 | if (!err) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 506 | fuse_invalidate_attr(olddir); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 507 | if (olddir != newdir) |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 508 | fuse_invalidate_attr(newdir); |
Miklos Szeredi | 015fe70 | 2004-07-12 11:52:24 +0000 | [diff] [blame] | 509 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 510 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int fuse_link(struct dentry *entry, struct inode *newdir, |
| 514 | struct dentry *newent) |
| 515 | { |
| 516 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 517 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 518 | struct fuse_inode *fi = INO_FI(inode); |
| 519 | struct fuse_inode *newfi = INO_FI(newdir); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 520 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 521 | struct fuse_link_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 522 | struct fuse_entry_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 523 | int err; |
| 524 | |
| 525 | if (!req) |
| 526 | return -ERESTARTSYS; |
| 527 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 528 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 529 | inarg.newdir = newfi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 530 | req->in.h.opcode = FUSE_LINK; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 531 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 532 | req->in.numargs = 2; |
| 533 | req->in.args[0].size = sizeof(inarg); |
| 534 | req->in.args[0].value = &inarg; |
| 535 | req->in.args[1].size = newent->d_name.len + 1; |
| 536 | req->in.args[1].value = newent->d_name.name; |
| 537 | req->out.numargs = 1; |
| 538 | req->out.args[0].size = sizeof(outarg); |
| 539 | req->out.args[0].value = &outarg; |
| 540 | request_send(fc, req); |
| 541 | err = req->out.h.error; |
| 542 | if (!err) { |
| 543 | /* Invalidate old entry, so attributes are refreshed */ |
| 544 | d_invalidate(entry); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 545 | err = lookup_new_entry(fc, req, newdir, newent, &outarg, |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 546 | req->out.h.unique, inode->i_mode); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 547 | } else |
| 548 | fuse_put_request(fc, req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 549 | return err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 552 | int fuse_do_getattr(struct inode *inode) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 553 | { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 554 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 555 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 556 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 557 | struct fuse_attr_out arg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 558 | int err; |
| 559 | |
| 560 | if (!req) |
| 561 | return -ERESTARTSYS; |
| 562 | |
| 563 | req->in.h.opcode = FUSE_GETATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 564 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 565 | req->out.numargs = 1; |
| 566 | req->out.args[0].size = sizeof(arg); |
| 567 | req->out.args[0].value = &arg; |
| 568 | request_send(fc, req); |
| 569 | err = req->out.h.error; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 570 | if (!err) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 571 | change_attributes(inode, &arg.attr); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 572 | fi->i_time = time_to_jiffies(arg.attr_valid, |
| 573 | arg.attr_valid_nsec); |
| 574 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 575 | fuse_put_request(fc, req); |
| 576 | return err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 579 | static int fuse_revalidate(struct dentry *entry) |
| 580 | { |
| 581 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 582 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 583 | struct fuse_conn *fc = INO_FC(inode); |
| 584 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 585 | if (fi->nodeid == FUSE_ROOT_ID) { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 586 | if (!(fc->flags & FUSE_ALLOW_OTHER) && |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 587 | current->fsuid != fc->uid && |
| 588 | (!(fc->flags & FUSE_ALLOW_ROOT) || |
| 589 | current->fsuid != 0)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 590 | return -EACCES; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 591 | } else if (!fi->i_time || time_before_eq(jiffies, fi->i_time)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 592 | return 0; |
| 593 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 594 | return fuse_do_getattr(inode); |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 597 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 598 | { |
| 599 | struct fuse_conn *fc = INO_FC(inode); |
| 600 | |
Miklos Szeredi | 180ff69 | 2004-11-01 16:01:05 +0000 | [diff] [blame] | 601 | if (!(fc->flags & FUSE_ALLOW_OTHER) && current->fsuid != fc->uid && |
| 602 | (!(fc->flags & FUSE_ALLOW_ROOT) || current->fsuid != 0)) |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 603 | return -EACCES; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 604 | else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 605 | int err; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 606 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 607 | err = generic_permission(inode, mask, NULL); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 608 | #else |
| 609 | err = vfs_permission(inode, mask); |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 610 | #endif |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 611 | |
| 612 | /* If permission is denied, try to refresh file |
| 613 | attributes. This is also needed, because the root |
| 614 | node will at first have no permissions */ |
| 615 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 616 | if (err == -EACCES) { |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 617 | err = fuse_do_getattr(inode); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 618 | if (!err) |
| 619 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 620 | err = generic_permission(inode, mask, NULL); |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 621 | #else |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 622 | err = vfs_permission(inode, mask); |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 623 | #endif |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | /* FIXME: Need some mechanism to revoke permissions: |
| 627 | currently if the filesystem suddenly changes the |
Miklos Szeredi | 064efb0 | 2004-11-09 17:30:29 +0000 | [diff] [blame] | 628 | file mode, we will not be informed about it, and |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 629 | continue to allow access to the file/directory. |
| 630 | |
| 631 | This is actually not so grave, since the user can |
| 632 | simply keep access to the file/directory anyway by |
| 633 | keeping it open... */ |
| 634 | |
| 635 | return err; |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 636 | } else { |
| 637 | int mode = inode->i_mode; |
| 638 | if ((mask & MAY_WRITE) && IS_RDONLY(inode) && |
| 639 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 640 | return -EROFS; |
| 641 | if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO)) |
| 642 | return -EACCES; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 643 | return 0; |
Miklos Szeredi | 96dfad7 | 2004-11-30 00:00:02 +0000 | [diff] [blame] | 644 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 647 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, |
| 648 | void *dstbuf, filldir_t filldir) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 649 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 650 | while (nbytes >= FUSE_NAME_OFFSET) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 651 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 652 | size_t reclen = FUSE_DIRENT_SIZE(dirent); |
| 653 | int over; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 654 | if (dirent->namelen > NAME_MAX) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 655 | printk("fuse_readdir: name too long\n"); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 656 | return -EPROTO; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 657 | } |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 658 | if (reclen > nbytes) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 659 | break; |
| 660 | |
| 661 | over = filldir(dstbuf, dirent->name, dirent->namelen, |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 662 | file->f_pos, dirent->ino, dirent->type); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 663 | if (over) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 664 | break; |
| 665 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 666 | buf += reclen; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 667 | file->f_pos += reclen; |
| 668 | nbytes -= reclen; |
| 669 | } |
| 670 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 671 | return 0; |
| 672 | } |
| 673 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 674 | static int fuse_checkdir(struct file *cfile, struct file *file) |
| 675 | { |
| 676 | struct inode *inode; |
| 677 | if (!cfile) { |
| 678 | printk("fuse_getdir: invalid file\n"); |
| 679 | return -EPROTO; |
| 680 | } |
| 681 | inode = cfile->f_dentry->d_inode; |
| 682 | if (!S_ISREG(inode->i_mode)) { |
| 683 | printk("fuse_getdir: not a regular file\n"); |
| 684 | fput(cfile); |
| 685 | return -EPROTO; |
| 686 | } |
| 687 | |
| 688 | file->private_data = cfile; |
| 689 | return 0; |
| 690 | } |
| 691 | |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 692 | static int fuse_getdir(struct file *file) |
| 693 | { |
| 694 | struct inode *inode = file->f_dentry->d_inode; |
| 695 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 696 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 697 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 698 | struct fuse_getdir_out_i outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 699 | int err; |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 700 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 701 | if (!req) |
| 702 | return -ERESTARTSYS; |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 703 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 704 | req->in.h.opcode = FUSE_GETDIR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 705 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 706 | req->out.numargs = 1; |
| 707 | req->out.args[0].size = sizeof(struct fuse_getdir_out); |
| 708 | req->out.args[0].value = &outarg; |
| 709 | request_send(fc, req); |
| 710 | err = req->out.h.error; |
| 711 | if (!err) |
| 712 | err = fuse_checkdir(outarg.file, file); |
| 713 | fuse_put_request(fc, req); |
| 714 | return err; |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 717 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
| 718 | { |
| 719 | struct file *cfile = file->private_data; |
| 720 | char *buf; |
| 721 | int ret; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 722 | |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 723 | if (!cfile) { |
| 724 | ret = fuse_getdir(file); |
| 725 | if (ret) |
| 726 | return ret; |
| 727 | |
| 728 | cfile = file->private_data; |
| 729 | } |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 730 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 731 | buf = (char *) __get_free_page(GFP_KERNEL); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 732 | if (!buf) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 733 | return -ENOMEM; |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 734 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 735 | ret = kernel_read(cfile, file->f_pos, buf, PAGE_SIZE); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 736 | if (ret < 0) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 737 | printk("fuse_readdir: failed to read container file\n"); |
| 738 | else |
| 739 | ret = parse_dirfile(buf, ret, file, dstbuf, filldir); |
| 740 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 741 | free_page((unsigned long) buf); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 742 | return ret; |
| 743 | } |
| 744 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 745 | static char *read_link(struct dentry *dentry) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 746 | { |
| 747 | struct inode *inode = dentry->d_inode; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 748 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 749 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 750 | struct fuse_req *req = fuse_get_request(fc); |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 751 | char *link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 752 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 753 | if (!req) |
| 754 | return ERR_PTR(-ERESTARTSYS); |
| 755 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 756 | link = (char *) __get_free_page(GFP_KERNEL); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 757 | if (!link) { |
| 758 | link = ERR_PTR(-ENOMEM); |
| 759 | goto out; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 760 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 761 | req->in.h.opcode = FUSE_READLINK; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 762 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 763 | req->out.argvar = 1; |
| 764 | req->out.numargs = 1; |
| 765 | req->out.args[0].size = PAGE_SIZE - 1; |
| 766 | req->out.args[0].value = link; |
| 767 | request_send(fc, req); |
| 768 | if (req->out.h.error) { |
| 769 | free_page((unsigned long) link); |
| 770 | link = ERR_PTR(req->out.h.error); |
| 771 | } else |
| 772 | link[req->out.args[0].size] = '\0'; |
| 773 | out: |
| 774 | fuse_put_request(fc, req); |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 775 | return link; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static void free_link(char *link) |
| 779 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 780 | if (!IS_ERR(link)) |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 781 | free_page((unsigned long) link); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 784 | static int fuse_readlink(struct dentry *dentry, char __user *buffer, |
| 785 | int buflen) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 786 | { |
| 787 | int ret; |
| 788 | char *link; |
| 789 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 790 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 791 | ret = vfs_readlink(dentry, buffer, buflen, link); |
| 792 | free_link(link); |
| 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | static int fuse_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 797 | { |
| 798 | int ret; |
| 799 | char *link; |
| 800 | |
Miklos Szeredi | 0503304 | 2001-11-13 16:11:35 +0000 | [diff] [blame] | 801 | link = read_link(dentry); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 802 | ret = vfs_follow_link(nd, link); |
| 803 | free_link(link); |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | static int fuse_dir_open(struct inode *inode, struct file *file) |
| 808 | { |
Miklos Szeredi | 152f29e | 2004-06-03 17:52:32 +0000 | [diff] [blame] | 809 | file->private_data = NULL; |
| 810 | return 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static int fuse_dir_release(struct inode *inode, struct file *file) |
| 814 | { |
| 815 | struct file *cfile = file->private_data; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 816 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 817 | if (cfile) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 818 | fput(cfile); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 823 | static unsigned iattr_to_fattr(struct iattr *iattr, struct fuse_attr *fattr) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 824 | { |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 825 | unsigned ivalid = iattr->ia_valid; |
| 826 | unsigned fvalid = 0; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 827 | |
| 828 | memset(fattr, 0, sizeof(*fattr)); |
| 829 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 830 | if (ivalid & ATTR_MODE) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 831 | fvalid |= FATTR_MODE, fattr->mode = iattr->ia_mode; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 832 | if (ivalid & ATTR_UID) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 833 | fvalid |= FATTR_UID, fattr->uid = iattr->ia_uid; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 834 | if (ivalid & ATTR_GID) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 835 | fvalid |= FATTR_GID, fattr->gid = iattr->ia_gid; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 836 | if (ivalid & ATTR_SIZE) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 837 | fvalid |= FATTR_SIZE, fattr->size = iattr->ia_size; |
| 838 | /* You can only _set_ these together (they may change by themselves) */ |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 839 | if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 840 | fvalid |= FATTR_ATIME | FATTR_MTIME; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 841 | #ifdef KERNEL_2_6 |
| 842 | fattr->atime = iattr->ia_atime.tv_sec; |
| 843 | fattr->mtime = iattr->ia_mtime.tv_sec; |
| 844 | #else |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 845 | fattr->atime = iattr->ia_atime; |
| 846 | fattr->mtime = iattr->ia_mtime; |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 847 | #endif |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | return fvalid; |
| 851 | } |
| 852 | |
| 853 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
| 854 | { |
| 855 | struct inode *inode = entry->d_inode; |
| 856 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 857 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 858 | struct fuse_req *req; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 859 | struct fuse_setattr_in inarg; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 860 | struct fuse_attr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 861 | int err; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 862 | int is_truncate = 0; |
| 863 | |
Miklos Szeredi | 94ed76a | 2004-07-26 19:38:45 +0000 | [diff] [blame] | 864 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
| 865 | err = inode_change_ok(inode, attr); |
| 866 | if (err) |
| 867 | return err; |
| 868 | } |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 869 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 870 | if (attr->ia_valid & ATTR_SIZE) { |
| 871 | unsigned long limit; |
| 872 | is_truncate = 1; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 873 | #ifdef KERNEL_2_6_10_PLUS |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 874 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 875 | #else |
| 876 | limit = current->rlim[RLIMIT_FSIZE].rlim_cur; |
Miklos Szeredi | 08ddb8e | 2004-11-14 09:19:51 +0000 | [diff] [blame] | 877 | #endif |
| 878 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 879 | send_sig(SIGXFSZ, current, 0); |
| 880 | return -EFBIG; |
| 881 | } |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 882 | } |
Miklos Szeredi | 58615e0 | 2004-07-04 21:21:08 +0000 | [diff] [blame] | 883 | |
| 884 | req = fuse_get_request(fc); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 885 | if (!req) |
| 886 | return -ERESTARTSYS; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 887 | |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 888 | if (is_truncate) |
| 889 | down_write(&fi->write_sem); |
| 890 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 891 | memset(&inarg, 0, sizeof(inarg)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 892 | inarg.valid = iattr_to_fattr(attr, &inarg.attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 893 | req->in.h.opcode = FUSE_SETATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 894 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 895 | req->in.numargs = 1; |
| 896 | req->in.args[0].size = sizeof(inarg); |
| 897 | req->in.args[0].value = &inarg; |
| 898 | req->out.numargs = 1; |
| 899 | req->out.args[0].size = sizeof(outarg); |
| 900 | req->out.args[0].value = &outarg; |
| 901 | request_send(fc, req); |
| 902 | err = req->out.h.error; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 903 | fuse_put_request(fc, req); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 904 | |
| 905 | if (!err) { |
| 906 | if (is_truncate) { |
| 907 | loff_t origsize = i_size_read(inode); |
| 908 | i_size_write(inode, outarg.attr.size); |
| 909 | up_write(&fi->write_sem); |
| 910 | if (origsize > outarg.attr.size) |
| 911 | vmtruncate(inode, outarg.attr.size); |
| 912 | } |
| 913 | change_attributes(inode, &outarg.attr); |
| 914 | fi->i_time = time_to_jiffies(outarg.attr_valid, |
| 915 | outarg.attr_valid_nsec); |
| 916 | } else if (is_truncate) |
| 917 | up_write(&fi->write_sem); |
| 918 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 919 | return err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 922 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 923 | { |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 924 | if (!entry->d_inode) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 925 | return 0; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 926 | else if (entry->d_time && time_after(jiffies, entry->d_time)) { |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 927 | struct inode *inode = entry->d_inode; |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 928 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 929 | struct fuse_entry_out outarg; |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 930 | int version; |
| 931 | int ret; |
| 932 | |
| 933 | ret = fuse_do_lookup(entry->d_parent->d_inode, entry, &outarg, |
| 934 | &version); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 935 | if (ret) |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 936 | return 0; |
| 937 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 938 | if (outarg.nodeid != fi->nodeid) |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 939 | return 0; |
| 940 | |
| 941 | change_attributes(inode, &outarg.attr); |
| 942 | inode->i_version = version; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 943 | entry->d_time = time_to_jiffies(outarg.entry_valid, |
| 944 | outarg.entry_valid_nsec); |
Miklos Szeredi | 069c950 | 2004-07-16 16:17:02 +0000 | [diff] [blame] | 945 | fi->i_time = time_to_jiffies(outarg.attr_valid, |
| 946 | outarg.attr_valid_nsec); |
Miklos Szeredi | 5e5d61f | 2002-10-24 11:50:33 +0000 | [diff] [blame] | 947 | } |
| 948 | return 1; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 951 | #ifdef KERNEL_2_6 |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 952 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
| 953 | struct kstat *stat) |
| 954 | { |
| 955 | struct inode *inode = entry->d_inode; |
| 956 | int err = fuse_revalidate(entry); |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 957 | if (!err) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 958 | generic_fillattr(inode, stat); |
| 959 | |
| 960 | return err; |
| 961 | } |
| 962 | |
| 963 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 964 | struct nameidata *nd) |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 965 | { |
Miklos Szeredi | e815c03 | 2004-01-19 18:20:49 +0000 | [diff] [blame] | 966 | struct inode *inode; |
| 967 | int err = fuse_lookup_iget(dir, entry, &inode); |
| 968 | if (err) |
| 969 | return ERR_PTR(err); |
| 970 | return d_splice_alias(inode, entry); |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 971 | } |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 972 | #else /* KERNEL_2_6 */ |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 973 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry) |
| 974 | { |
| 975 | struct inode *inode; |
| 976 | struct dentry *alias; |
| 977 | |
| 978 | int err = fuse_lookup_iget(dir, entry, &inode); |
| 979 | if (err) |
| 980 | return ERR_PTR(err); |
| 981 | |
| 982 | if (inode && S_ISDIR(inode->i_mode) && |
| 983 | (alias = d_find_alias(inode)) != NULL) { |
| 984 | dput(alias); |
| 985 | iput(inode); |
| 986 | printk("fuse: cannot assign an existing directory\n"); |
| 987 | return ERR_PTR(-EPROTO); |
| 988 | } |
| 989 | |
| 990 | d_add(entry, inode); |
| 991 | return NULL; |
| 992 | } |
| 993 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 994 | static int fuse_mknod_2_4(struct inode *dir, struct dentry *entry, int mode, |
Miklos Szeredi | 83a0744 | 2004-11-30 18:25:20 +0000 | [diff] [blame^] | 995 | int rdev) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 996 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 997 | return fuse_mknod(dir, entry, mode, rdev); |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1000 | static int fuse_dentry_revalidate_2_4(struct dentry *entry, int flags) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1001 | { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1002 | return fuse_dentry_revalidate(entry, NULL); |
| 1003 | } |
| 1004 | |
| 1005 | static int fuse_create_2_4(struct inode *dir, struct dentry *entry, int mode) |
| 1006 | { |
| 1007 | return fuse_create(dir, entry, mode, NULL); |
| 1008 | } |
| 1009 | |
| 1010 | static int fuse_permission_2_4(struct inode *inode, int mask) |
| 1011 | { |
| 1012 | return fuse_permission(inode, mask, NULL); |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1013 | } |
| 1014 | #endif /* KERNEL_2_6 */ |
| 1015 | |
| 1016 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1017 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1018 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1019 | const void *value, size_t size, int flags) |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1020 | #else |
| 1021 | static int fuse_setxattr(struct dentry *entry, const char *name, |
| 1022 | void *value, size_t size, int flags) |
| 1023 | #endif |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1024 | { |
| 1025 | struct inode *inode = entry->d_inode; |
| 1026 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1027 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1028 | struct fuse_req *req; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1029 | struct fuse_setxattr_in inarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1030 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1031 | |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1032 | if (size > FUSE_XATTR_SIZE_MAX) |
| 1033 | return -E2BIG; |
| 1034 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1035 | if (fc->no_setxattr) |
| 1036 | return -EOPNOTSUPP; |
| 1037 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1038 | req = fuse_get_request(fc); |
| 1039 | if (!req) |
| 1040 | return -ERESTARTSYS; |
| 1041 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1042 | memset(&inarg, 0, sizeof(inarg)); |
| 1043 | inarg.size = size; |
| 1044 | inarg.flags = flags; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1045 | req->in.h.opcode = FUSE_SETXATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1046 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1047 | req->in.numargs = 3; |
| 1048 | req->in.args[0].size = sizeof(inarg); |
| 1049 | req->in.args[0].value = &inarg; |
| 1050 | req->in.args[1].size = strlen(name) + 1; |
| 1051 | req->in.args[1].value = name; |
| 1052 | req->in.args[2].size = size; |
| 1053 | req->in.args[2].value = value; |
| 1054 | request_send(fc, req); |
| 1055 | err = req->out.h.error; |
| 1056 | if (err == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1057 | fc->no_setxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1058 | err = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1059 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1060 | fuse_put_request(fc, req); |
| 1061 | return err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, |
| 1065 | void *value, size_t size) |
| 1066 | { |
| 1067 | struct inode *inode = entry->d_inode; |
| 1068 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1069 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1070 | struct fuse_req *req; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1071 | struct fuse_getxattr_in inarg; |
| 1072 | struct fuse_getxattr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1073 | ssize_t ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1074 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1075 | if (fc->no_getxattr) |
| 1076 | return -EOPNOTSUPP; |
| 1077 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1078 | req = fuse_get_request(fc); |
| 1079 | if (!req) |
| 1080 | return -ERESTARTSYS; |
| 1081 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1082 | memset(&inarg, 0, sizeof(inarg)); |
| 1083 | inarg.size = size; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1084 | req->in.h.opcode = FUSE_GETXATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1085 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1086 | req->in.numargs = 2; |
| 1087 | req->in.args[0].size = sizeof(inarg); |
| 1088 | req->in.args[0].value = &inarg; |
| 1089 | req->in.args[1].size = strlen(name) + 1; |
| 1090 | req->in.args[1].value = name; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1091 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1092 | req->out.numargs = 1; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1093 | if (size) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1094 | req->out.argvar = 1; |
| 1095 | req->out.args[0].size = size; |
| 1096 | req->out.args[0].value = value; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1097 | } else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1098 | req->out.args[0].size = sizeof(outarg); |
| 1099 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1100 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1101 | request_send(fc, req); |
| 1102 | ret = req->out.h.error; |
| 1103 | if (!ret) |
| 1104 | ret = size ? req->out.args[0].size : outarg.size; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1105 | else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1106 | if (ret == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1107 | fc->no_getxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1108 | ret = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1109 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1110 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1111 | fuse_put_request(fc, req); |
| 1112 | return ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) |
| 1116 | { |
| 1117 | struct inode *inode = entry->d_inode; |
| 1118 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1119 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1120 | struct fuse_req *req; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1121 | struct fuse_getxattr_in inarg; |
| 1122 | struct fuse_getxattr_out outarg; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1123 | ssize_t ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1124 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1125 | if (fc->no_listxattr) |
| 1126 | return -EOPNOTSUPP; |
| 1127 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1128 | req = fuse_get_request(fc); |
| 1129 | if (!req) |
| 1130 | return -ERESTARTSYS; |
| 1131 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1132 | memset(&inarg, 0, sizeof(inarg)); |
| 1133 | inarg.size = size; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1134 | req->in.h.opcode = FUSE_LISTXATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1135 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1136 | req->in.numargs = 1; |
| 1137 | req->in.args[0].size = sizeof(inarg); |
| 1138 | req->in.args[0].value = &inarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1139 | /* This is really two different operations rolled into one */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1140 | req->out.numargs = 1; |
Miklos Szeredi | c26c14d | 2004-04-09 17:48:32 +0000 | [diff] [blame] | 1141 | if (size) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1142 | req->out.argvar = 1; |
| 1143 | req->out.args[0].size = size; |
| 1144 | req->out.args[0].value = list; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1145 | } else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1146 | req->out.args[0].size = sizeof(outarg); |
| 1147 | req->out.args[0].value = &outarg; |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1148 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1149 | request_send(fc, req); |
| 1150 | ret = req->out.h.error; |
| 1151 | if (!ret) |
| 1152 | ret = size ? req->out.args[0].size : outarg.size; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1153 | else { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1154 | if (ret == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1155 | fc->no_listxattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1156 | ret = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1157 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1158 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1159 | fuse_put_request(fc, req); |
| 1160 | return ret; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static int fuse_removexattr(struct dentry *entry, const char *name) |
| 1164 | { |
| 1165 | struct inode *inode = entry->d_inode; |
| 1166 | struct fuse_conn *fc = INO_FC(inode); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1167 | struct fuse_inode *fi = INO_FI(inode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1168 | struct fuse_req *req; |
| 1169 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1170 | |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1171 | if (fc->no_removexattr) |
| 1172 | return -EOPNOTSUPP; |
| 1173 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1174 | req = fuse_get_request(fc); |
| 1175 | if (!req) |
| 1176 | return -ERESTARTSYS; |
| 1177 | |
| 1178 | req->in.h.opcode = FUSE_REMOVEXATTR; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1179 | req->in.h.nodeid = fi->nodeid; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1180 | req->in.numargs = 1; |
| 1181 | req->in.args[0].size = strlen(name) + 1; |
| 1182 | req->in.args[0].value = name; |
| 1183 | request_send(fc, req); |
| 1184 | err = req->out.h.error; |
| 1185 | if (err == -ENOSYS) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1186 | fc->no_removexattr = 1; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1187 | err = -EOPNOTSUPP; |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1188 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1189 | fuse_put_request(fc, req); |
| 1190 | return err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1191 | } |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1192 | #endif |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1193 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1194 | static struct inode_operations fuse_dir_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1195 | .lookup = fuse_lookup, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1196 | .mkdir = fuse_mkdir, |
| 1197 | .symlink = fuse_symlink, |
| 1198 | .unlink = fuse_unlink, |
| 1199 | .rmdir = fuse_rmdir, |
| 1200 | .rename = fuse_rename, |
| 1201 | .link = fuse_link, |
| 1202 | .setattr = fuse_setattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1203 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1204 | .create = fuse_create, |
| 1205 | .mknod = fuse_mknod, |
| 1206 | .permission = fuse_permission, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1207 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1208 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1209 | .create = fuse_create_2_4, |
| 1210 | .mknod = fuse_mknod_2_4, |
| 1211 | .permission = fuse_permission_2_4, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1212 | .revalidate = fuse_revalidate, |
| 1213 | #endif |
| 1214 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1215 | .setxattr = fuse_setxattr, |
| 1216 | .getxattr = fuse_getxattr, |
| 1217 | .listxattr = fuse_listxattr, |
| 1218 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1219 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1220 | }; |
| 1221 | |
| 1222 | static struct file_operations fuse_dir_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1223 | .read = generic_read_dir, |
| 1224 | .readdir = fuse_readdir, |
| 1225 | .open = fuse_dir_open, |
| 1226 | .release = fuse_dir_release, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1227 | }; |
| 1228 | |
| 1229 | static struct inode_operations fuse_file_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1230 | .setattr = fuse_setattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1231 | #ifdef KERNEL_2_6 |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1232 | .permission = fuse_permission, |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1233 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1234 | #else |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1235 | .permission = fuse_permission_2_4, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1236 | .revalidate = fuse_revalidate, |
| 1237 | #endif |
| 1238 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1239 | .setxattr = fuse_setxattr, |
| 1240 | .getxattr = fuse_getxattr, |
| 1241 | .listxattr = fuse_listxattr, |
| 1242 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1243 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1244 | }; |
| 1245 | |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1246 | static struct inode_operations fuse_symlink_inode_operations = { |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1247 | .setattr = fuse_setattr, |
| 1248 | .readlink = fuse_readlink, |
| 1249 | .follow_link = fuse_follow_link, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1250 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1251 | .getattr = fuse_getattr, |
Miklos Szeredi | 689f563 | 2004-05-04 08:49:16 +0000 | [diff] [blame] | 1252 | #else |
| 1253 | .revalidate = fuse_revalidate, |
| 1254 | #endif |
| 1255 | #ifdef HAVE_KERNEL_XATTR |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1256 | .setxattr = fuse_setxattr, |
| 1257 | .getxattr = fuse_getxattr, |
| 1258 | .listxattr = fuse_listxattr, |
| 1259 | .removexattr = fuse_removexattr, |
Miklos Szeredi | f85ab24 | 2004-01-07 12:16:45 +0000 | [diff] [blame] | 1260 | #endif |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1261 | }; |
| 1262 | |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 1263 | static struct dentry_operations fuse_dentry_operations = { |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1264 | #ifdef KERNEL_2_6 |
Miklos Szeredi | e8663f3 | 2004-01-13 15:33:12 +0000 | [diff] [blame] | 1265 | .d_revalidate = fuse_dentry_revalidate, |
Miklos Szeredi | 13ed482 | 2004-11-20 11:12:21 +0000 | [diff] [blame] | 1266 | #else |
| 1267 | .d_revalidate = fuse_dentry_revalidate_2_4, |
| 1268 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1269 | }; |