Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/9p/vfs_inode.c |
| 3 | * |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 4 | * This file contains vfs inode ops for the 9P2000 protocol. |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> |
| 7 | * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to: |
| 21 | * Free Software Foundation |
| 22 | * 51 Franklin Street, Fifth Floor |
| 23 | * Boston, MA 02111-1301 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/file.h> |
| 31 | #include <linux/pagemap.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/smp_lock.h> |
| 35 | #include <linux/inet.h> |
| 36 | #include <linux/namei.h> |
| 37 | #include <linux/idr.h> |
| 38 | |
| 39 | #include "debug.h" |
| 40 | #include "v9fs.h" |
| 41 | #include "9p.h" |
| 42 | #include "v9fs_vfs.h" |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 43 | #include "fid.h" |
| 44 | |
| 45 | static struct inode_operations v9fs_dir_inode_operations; |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 46 | static struct inode_operations v9fs_dir_inode_operations_ext; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 47 | static struct inode_operations v9fs_file_inode_operations; |
| 48 | static struct inode_operations v9fs_symlink_inode_operations; |
| 49 | |
| 50 | /** |
| 51 | * unixmode2p9mode - convert unix mode bits to plan 9 |
| 52 | * @v9ses: v9fs session information |
| 53 | * @mode: mode to convert |
| 54 | * |
| 55 | */ |
| 56 | |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 57 | static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 58 | { |
| 59 | int res; |
| 60 | res = mode & 0777; |
| 61 | if (S_ISDIR(mode)) |
| 62 | res |= V9FS_DMDIR; |
| 63 | if (v9ses->extended) { |
| 64 | if (S_ISLNK(mode)) |
| 65 | res |= V9FS_DMSYMLINK; |
| 66 | if (v9ses->nodev == 0) { |
| 67 | if (S_ISSOCK(mode)) |
| 68 | res |= V9FS_DMSOCKET; |
| 69 | if (S_ISFIFO(mode)) |
| 70 | res |= V9FS_DMNAMEDPIPE; |
| 71 | if (S_ISBLK(mode)) |
| 72 | res |= V9FS_DMDEVICE; |
| 73 | if (S_ISCHR(mode)) |
| 74 | res |= V9FS_DMDEVICE; |
| 75 | } |
| 76 | |
| 77 | if ((mode & S_ISUID) == S_ISUID) |
| 78 | res |= V9FS_DMSETUID; |
| 79 | if ((mode & S_ISGID) == S_ISGID) |
| 80 | res |= V9FS_DMSETGID; |
| 81 | if ((mode & V9FS_DMLINK)) |
| 82 | res |= V9FS_DMLINK; |
| 83 | } |
| 84 | |
| 85 | return res; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * p9mode2unixmode- convert plan9 mode bits to unix mode bits |
| 90 | * @v9ses: v9fs session information |
| 91 | * @mode: mode to convert |
| 92 | * |
| 93 | */ |
| 94 | |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 95 | static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 96 | { |
| 97 | int res; |
| 98 | |
| 99 | res = mode & 0777; |
| 100 | |
| 101 | if ((mode & V9FS_DMDIR) == V9FS_DMDIR) |
| 102 | res |= S_IFDIR; |
| 103 | else if ((mode & V9FS_DMSYMLINK) && (v9ses->extended)) |
| 104 | res |= S_IFLNK; |
| 105 | else if ((mode & V9FS_DMSOCKET) && (v9ses->extended) |
| 106 | && (v9ses->nodev == 0)) |
| 107 | res |= S_IFSOCK; |
| 108 | else if ((mode & V9FS_DMNAMEDPIPE) && (v9ses->extended) |
| 109 | && (v9ses->nodev == 0)) |
| 110 | res |= S_IFIFO; |
| 111 | else if ((mode & V9FS_DMDEVICE) && (v9ses->extended) |
| 112 | && (v9ses->nodev == 0)) |
| 113 | res |= S_IFBLK; |
| 114 | else |
| 115 | res |= S_IFREG; |
| 116 | |
| 117 | if (v9ses->extended) { |
| 118 | if ((mode & V9FS_DMSETUID) == V9FS_DMSETUID) |
| 119 | res |= S_ISUID; |
| 120 | |
| 121 | if ((mode & V9FS_DMSETGID) == V9FS_DMSETGID) |
| 122 | res |= S_ISGID; |
| 123 | } |
| 124 | |
| 125 | return res; |
| 126 | } |
| 127 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 128 | int v9fs_uflags2omode(int uflags) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | ret = 0; |
| 133 | switch (uflags&3) { |
| 134 | default: |
| 135 | case O_RDONLY: |
| 136 | ret = V9FS_OREAD; |
| 137 | break; |
| 138 | |
| 139 | case O_WRONLY: |
| 140 | ret = V9FS_OWRITE; |
| 141 | break; |
| 142 | |
| 143 | case O_RDWR: |
| 144 | ret = V9FS_ORDWR; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (uflags & O_EXCL) |
| 149 | ret |= V9FS_OEXCL; |
| 150 | |
| 151 | if (uflags & O_TRUNC) |
| 152 | ret |= V9FS_OTRUNC; |
| 153 | |
| 154 | if (uflags & O_APPEND) |
| 155 | ret |= V9FS_OAPPEND; |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 160 | /** |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 161 | * v9fs_blank_wstat - helper function to setup a 9P stat structure |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 162 | * @v9ses: 9P session info (for determining extended mode) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 163 | * @wstat: structure to initialize |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 164 | * |
| 165 | */ |
| 166 | |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 167 | static void |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 168 | v9fs_blank_wstat(struct v9fs_wstat *wstat) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 169 | { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 170 | wstat->type = ~0; |
| 171 | wstat->dev = ~0; |
| 172 | wstat->qid.type = ~0; |
| 173 | wstat->qid.version = ~0; |
| 174 | *((long long *)&wstat->qid.path) = ~0; |
| 175 | wstat->mode = ~0; |
| 176 | wstat->atime = ~0; |
| 177 | wstat->mtime = ~0; |
| 178 | wstat->length = ~0; |
| 179 | wstat->name = NULL; |
| 180 | wstat->uid = NULL; |
| 181 | wstat->gid = NULL; |
| 182 | wstat->muid = NULL; |
| 183 | wstat->n_uid = ~0; |
| 184 | wstat->n_gid = ~0; |
| 185 | wstat->n_muid = ~0; |
| 186 | wstat->extension = NULL; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
| 190 | * v9fs_get_inode - helper function to setup an inode |
| 191 | * @sb: superblock |
| 192 | * @mode: mode to setup inode with |
| 193 | * |
| 194 | */ |
| 195 | |
| 196 | struct inode *v9fs_get_inode(struct super_block *sb, int mode) |
| 197 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 198 | struct inode *inode; |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 199 | struct v9fs_session_info *v9ses = sb->s_fs_info; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 200 | |
| 201 | dprintk(DEBUG_VFS, "super block: %p mode: %o\n", sb, mode); |
| 202 | |
| 203 | inode = new_inode(sb); |
| 204 | if (inode) { |
| 205 | inode->i_mode = mode; |
| 206 | inode->i_uid = current->fsuid; |
| 207 | inode->i_gid = current->fsgid; |
| 208 | inode->i_blksize = sb->s_blocksize; |
| 209 | inode->i_blocks = 0; |
| 210 | inode->i_rdev = 0; |
| 211 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Eric Van Hensbergen | 147b31c | 2006-01-18 17:43:02 -0800 | [diff] [blame] | 212 | inode->i_mapping->a_ops = &v9fs_addr_operations; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 213 | |
| 214 | switch (mode & S_IFMT) { |
| 215 | case S_IFIFO: |
| 216 | case S_IFBLK: |
| 217 | case S_IFCHR: |
| 218 | case S_IFSOCK: |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 219 | if(!v9ses->extended) { |
| 220 | dprintk(DEBUG_ERROR, "special files without extended mode\n"); |
| 221 | return ERR_PTR(-EINVAL); |
| 222 | } |
Eric Van Hensbergen | 5d58bec | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 223 | init_special_inode(inode, inode->i_mode, |
| 224 | inode->i_rdev); |
| 225 | break; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 226 | case S_IFREG: |
| 227 | inode->i_op = &v9fs_file_inode_operations; |
| 228 | inode->i_fop = &v9fs_file_operations; |
| 229 | break; |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 230 | case S_IFLNK: |
| 231 | if(!v9ses->extended) { |
| 232 | dprintk(DEBUG_ERROR, "extended modes used w/o 9P2000.u\n"); |
| 233 | return ERR_PTR(-EINVAL); |
| 234 | } |
| 235 | inode->i_op = &v9fs_symlink_inode_operations; |
| 236 | break; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 237 | case S_IFDIR: |
| 238 | inode->i_nlink++; |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 239 | if(v9ses->extended) |
| 240 | inode->i_op = &v9fs_dir_inode_operations_ext; |
| 241 | else |
| 242 | inode->i_op = &v9fs_dir_inode_operations; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 243 | inode->i_fop = &v9fs_dir_operations; |
| 244 | break; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 245 | default: |
| 246 | dprintk(DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n", |
| 247 | mode, mode & S_IFMT); |
| 248 | return ERR_PTR(-EINVAL); |
| 249 | } |
| 250 | } else { |
| 251 | eprintk(KERN_WARNING, "Problem allocating inode\n"); |
| 252 | return ERR_PTR(-ENOMEM); |
| 253 | } |
| 254 | return inode; |
| 255 | } |
| 256 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 257 | static int |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 258 | v9fs_create(struct v9fs_session_info *v9ses, u32 pfid, char *name, u32 perm, |
| 259 | u8 mode, char *extension, u32 *fidp, struct v9fs_qid *qid, u32 *iounit) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 260 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 261 | u32 fid; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 262 | int err; |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 263 | struct v9fs_fcall *fcall; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 264 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 265 | fid = v9fs_get_idpool(&v9ses->fidpool); |
| 266 | if (fid < 0) { |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 267 | eprintk(KERN_WARNING, "no free fids available\n"); |
Latchesar Ionkov | 731805b | 2006-03-07 21:55:42 -0800 | [diff] [blame] | 268 | return -ENOSPC; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 271 | err = v9fs_t_walk(v9ses, pfid, fid, NULL, &fcall); |
| 272 | if (err < 0) { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 273 | PRINT_FCALL_ERROR("clone error", fcall); |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 274 | goto put_fid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 275 | } |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 276 | kfree(fcall); |
| 277 | |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 278 | err = v9fs_t_create(v9ses, fid, name, perm, mode, extension, &fcall); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 279 | if (err < 0) { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 280 | PRINT_FCALL_ERROR("create fails", fcall); |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 281 | goto clunk_fid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 284 | if (iounit) |
| 285 | *iounit = fcall->params.rcreate.iounit; |
| 286 | |
| 287 | if (qid) |
| 288 | *qid = fcall->params.rcreate.qid; |
| 289 | |
| 290 | if (fidp) |
| 291 | *fidp = fid; |
| 292 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 293 | kfree(fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 296 | clunk_fid: |
| 297 | v9fs_t_clunk(v9ses, fid); |
| 298 | fid = V9FS_NOFID; |
| 299 | |
| 300 | put_fid: |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 301 | if (fid >= 0) |
| 302 | v9fs_put_idpool(fid, &v9ses->fidpool); |
| 303 | |
| 304 | kfree(fcall); |
| 305 | return err; |
| 306 | } |
| 307 | |
| 308 | static struct v9fs_fid* |
| 309 | v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry) |
| 310 | { |
| 311 | int err; |
| 312 | u32 nfid; |
| 313 | struct v9fs_fid *ret; |
| 314 | struct v9fs_fcall *fcall; |
| 315 | |
| 316 | nfid = v9fs_get_idpool(&v9ses->fidpool); |
| 317 | if (nfid < 0) { |
| 318 | eprintk(KERN_WARNING, "no free fids available\n"); |
Latchesar Ionkov | 731805b | 2006-03-07 21:55:42 -0800 | [diff] [blame] | 319 | return ERR_PTR(-ENOSPC); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name, |
| 323 | &fcall); |
| 324 | |
| 325 | if (err < 0) { |
| 326 | PRINT_FCALL_ERROR("walk error", fcall); |
| 327 | v9fs_put_idpool(nfid, &v9ses->fidpool); |
| 328 | goto error; |
| 329 | } |
| 330 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 331 | kfree(fcall); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 332 | fcall = NULL; |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 333 | ret = v9fs_fid_create(v9ses, nfid); |
| 334 | if (!ret) { |
| 335 | err = -ENOMEM; |
| 336 | goto clunk_fid; |
| 337 | } |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 338 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 339 | err = v9fs_fid_insert(ret, dentry); |
| 340 | if (err < 0) { |
| 341 | v9fs_fid_destroy(ret); |
| 342 | goto clunk_fid; |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 343 | } |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 344 | |
| 345 | return ret; |
| 346 | |
| 347 | clunk_fid: |
| 348 | v9fs_t_clunk(v9ses, nfid); |
| 349 | |
| 350 | error: |
| 351 | kfree(fcall); |
| 352 | return ERR_PTR(err); |
| 353 | } |
| 354 | |
Latchesar Ionkov | 5174fda | 2006-03-25 03:07:25 -0800 | [diff] [blame] | 355 | static struct inode * |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 356 | v9fs_inode_from_fid(struct v9fs_session_info *v9ses, u32 fid, |
| 357 | struct super_block *sb) |
| 358 | { |
| 359 | int err, umode; |
| 360 | struct inode *ret; |
| 361 | struct v9fs_fcall *fcall; |
| 362 | |
| 363 | ret = NULL; |
| 364 | err = v9fs_t_stat(v9ses, fid, &fcall); |
| 365 | if (err) { |
| 366 | PRINT_FCALL_ERROR("stat error", fcall); |
| 367 | goto error; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 368 | } |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 369 | |
| 370 | umode = p9mode2unixmode(v9ses, fcall->params.rstat.stat.mode); |
| 371 | ret = v9fs_get_inode(sb, umode); |
| 372 | if (IS_ERR(ret)) { |
| 373 | err = PTR_ERR(ret); |
| 374 | ret = NULL; |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | v9fs_stat2inode(&fcall->params.rstat.stat, ret, sb); |
| 379 | kfree(fcall); |
| 380 | return ret; |
| 381 | |
| 382 | error: |
| 383 | kfree(fcall); |
| 384 | if (ret) |
| 385 | iput(ret); |
| 386 | |
| 387 | return ERR_PTR(err); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * v9fs_remove - helper function to remove files and directories |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 392 | * @dir: directory inode that is being deleted |
| 393 | * @file: dentry that is being deleted |
| 394 | * @rmdir: removing a directory |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 395 | * |
| 396 | */ |
| 397 | |
| 398 | static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir) |
| 399 | { |
| 400 | struct v9fs_fcall *fcall = NULL; |
| 401 | struct super_block *sb = NULL; |
| 402 | struct v9fs_session_info *v9ses = NULL; |
| 403 | struct v9fs_fid *v9fid = NULL; |
| 404 | struct inode *file_inode = NULL; |
| 405 | int fid = -1; |
| 406 | int result = 0; |
| 407 | |
| 408 | dprintk(DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file, |
| 409 | rmdir); |
| 410 | |
| 411 | file_inode = file->d_inode; |
| 412 | sb = file_inode->i_sb; |
| 413 | v9ses = v9fs_inode2v9ses(file_inode); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 414 | v9fid = v9fs_fid_lookup(file); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 415 | |
| 416 | if (!v9fid) { |
| 417 | dprintk(DEBUG_ERROR, |
| 418 | "no v9fs_fid\n"); |
| 419 | return -EBADF; |
| 420 | } |
| 421 | |
| 422 | fid = v9fid->fid; |
| 423 | if (fid < 0) { |
| 424 | dprintk(DEBUG_ERROR, "inode #%lu, no fid!\n", |
| 425 | file_inode->i_ino); |
| 426 | return -EBADF; |
| 427 | } |
| 428 | |
| 429 | result = v9fs_t_remove(v9ses, fid, &fcall); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 430 | if (result < 0) { |
| 431 | PRINT_FCALL_ERROR("remove fails", fcall); |
| 432 | } else { |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 433 | v9fs_put_idpool(fid, &v9ses->fidpool); |
| 434 | v9fs_fid_destroy(v9fid); |
| 435 | } |
| 436 | |
| 437 | kfree(fcall); |
| 438 | return result; |
| 439 | } |
| 440 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 441 | static int |
| 442 | v9fs_open_created(struct inode *inode, struct file *file) |
| 443 | { |
| 444 | return 0; |
| 445 | } |
| 446 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 447 | /** |
| 448 | * v9fs_vfs_create - VFS hook to create files |
| 449 | * @inode: directory inode that is being deleted |
| 450 | * @dentry: dentry that is being deleted |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 451 | * @mode: create permissions |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 452 | * @nd: path information |
| 453 | * |
| 454 | */ |
| 455 | |
| 456 | static int |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 457 | v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode, |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 458 | struct nameidata *nd) |
| 459 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 460 | int err; |
| 461 | u32 fid, perm, iounit; |
| 462 | int flags; |
| 463 | struct v9fs_session_info *v9ses; |
| 464 | struct v9fs_fid *dfid, *vfid, *ffid; |
| 465 | struct inode *inode; |
| 466 | struct v9fs_qid qid; |
| 467 | struct file *filp; |
| 468 | |
| 469 | inode = NULL; |
| 470 | vfid = NULL; |
| 471 | v9ses = v9fs_inode2v9ses(dir); |
| 472 | dfid = v9fs_fid_lookup(dentry->d_parent); |
| 473 | perm = unixmode2p9mode(v9ses, mode); |
| 474 | |
| 475 | if (nd && nd->flags & LOOKUP_OPEN) |
| 476 | flags = nd->intent.open.flags - 1; |
| 477 | else |
| 478 | flags = O_RDWR; |
| 479 | |
| 480 | err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name, |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 481 | perm, v9fs_uflags2omode(flags), NULL, &fid, &qid, &iounit); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 482 | |
| 483 | if (err) |
| 484 | goto error; |
| 485 | |
| 486 | vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry); |
| 487 | if (IS_ERR(vfid)) { |
| 488 | err = PTR_ERR(vfid); |
| 489 | vfid = NULL; |
| 490 | goto error; |
| 491 | } |
| 492 | |
| 493 | inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb); |
| 494 | if (IS_ERR(inode)) { |
| 495 | err = PTR_ERR(inode); |
| 496 | inode = NULL; |
| 497 | goto error; |
| 498 | } |
| 499 | |
| 500 | dentry->d_op = &v9fs_dentry_operations; |
| 501 | d_instantiate(dentry, inode); |
| 502 | |
| 503 | if (nd && nd->flags & LOOKUP_OPEN) { |
| 504 | ffid = v9fs_fid_create(v9ses, fid); |
| 505 | if (!ffid) |
| 506 | return -ENOMEM; |
| 507 | |
| 508 | filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created); |
| 509 | if (IS_ERR(filp)) { |
| 510 | v9fs_fid_destroy(ffid); |
| 511 | return PTR_ERR(filp); |
| 512 | } |
| 513 | |
| 514 | ffid->rdir_pos = 0; |
| 515 | ffid->rdir_fcall = NULL; |
| 516 | ffid->fidopen = 1; |
| 517 | ffid->iounit = iounit; |
| 518 | ffid->filp = filp; |
| 519 | filp->private_data = ffid; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | |
| 524 | error: |
| 525 | if (vfid) |
| 526 | v9fs_fid_destroy(vfid); |
| 527 | |
| 528 | if (inode) |
| 529 | iput(inode); |
| 530 | |
| 531 | return err; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
| 535 | * v9fs_vfs_mkdir - VFS mkdir hook to create a directory |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 536 | * @inode: inode that is being unlinked |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 537 | * @dentry: dentry that is being unlinked |
| 538 | * @mode: mode for new directory |
| 539 | * |
| 540 | */ |
| 541 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 542 | static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 543 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 544 | int err; |
| 545 | u32 fid, perm; |
| 546 | struct v9fs_session_info *v9ses; |
| 547 | struct v9fs_fid *dfid, *vfid; |
| 548 | struct inode *inode; |
| 549 | |
| 550 | inode = NULL; |
| 551 | vfid = NULL; |
| 552 | v9ses = v9fs_inode2v9ses(dir); |
| 553 | dfid = v9fs_fid_lookup(dentry->d_parent); |
| 554 | perm = unixmode2p9mode(v9ses, mode | S_IFDIR); |
| 555 | |
| 556 | err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name, |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 557 | perm, V9FS_OREAD, NULL, &fid, NULL, NULL); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 558 | |
| 559 | if (err) { |
| 560 | dprintk(DEBUG_ERROR, "create error %d\n", err); |
| 561 | goto error; |
| 562 | } |
| 563 | |
| 564 | err = v9fs_t_clunk(v9ses, fid); |
| 565 | if (err) { |
| 566 | dprintk(DEBUG_ERROR, "clunk error %d\n", err); |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry); |
| 571 | if (IS_ERR(vfid)) { |
| 572 | err = PTR_ERR(vfid); |
| 573 | vfid = NULL; |
| 574 | goto error; |
| 575 | } |
| 576 | |
| 577 | inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb); |
| 578 | if (IS_ERR(inode)) { |
| 579 | err = PTR_ERR(inode); |
| 580 | inode = NULL; |
| 581 | goto error; |
| 582 | } |
| 583 | |
| 584 | dentry->d_op = &v9fs_dentry_operations; |
| 585 | d_instantiate(dentry, inode); |
| 586 | return 0; |
| 587 | |
| 588 | error: |
| 589 | if (vfid) |
| 590 | v9fs_fid_destroy(vfid); |
| 591 | |
| 592 | return err; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /** |
| 596 | * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode |
| 597 | * @dir: inode that is being walked from |
| 598 | * @dentry: dentry that is being walked to? |
| 599 | * @nameidata: path data |
| 600 | * |
| 601 | */ |
| 602 | |
| 603 | static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry, |
| 604 | struct nameidata *nameidata) |
| 605 | { |
| 606 | struct super_block *sb; |
| 607 | struct v9fs_session_info *v9ses; |
| 608 | struct v9fs_fid *dirfid; |
| 609 | struct v9fs_fid *fid; |
| 610 | struct inode *inode; |
| 611 | struct v9fs_fcall *fcall = NULL; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 612 | int dirfidnum = -1; |
| 613 | int newfid = -1; |
| 614 | int result = 0; |
| 615 | |
| 616 | dprintk(DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n", |
Latchesar Ionkov | 731805b | 2006-03-07 21:55:42 -0800 | [diff] [blame] | 617 | dir, dentry->d_name.name, dentry, nameidata); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 618 | |
| 619 | sb = dir->i_sb; |
| 620 | v9ses = v9fs_inode2v9ses(dir); |
Latchesar Ionkov | 5e7a99a | 2006-03-22 00:07:37 -0800 | [diff] [blame] | 621 | dentry->d_op = &v9fs_dentry_operations; |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 622 | dirfid = v9fs_fid_lookup(dentry->d_parent); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 623 | |
| 624 | if (!dirfid) { |
| 625 | dprintk(DEBUG_ERROR, "no dirfid\n"); |
| 626 | return ERR_PTR(-EINVAL); |
| 627 | } |
| 628 | |
| 629 | dirfidnum = dirfid->fid; |
| 630 | |
| 631 | if (dirfidnum < 0) { |
| 632 | dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n", |
| 633 | dir, dir->i_ino); |
| 634 | return ERR_PTR(-EBADF); |
| 635 | } |
| 636 | |
| 637 | newfid = v9fs_get_idpool(&v9ses->fidpool); |
| 638 | if (newfid < 0) { |
| 639 | eprintk(KERN_WARNING, "newfid fails!\n"); |
| 640 | return ERR_PTR(-ENOSPC); |
| 641 | } |
| 642 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 643 | result = v9fs_t_walk(v9ses, dirfidnum, newfid, |
| 644 | (char *)dentry->d_name.name, NULL); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 645 | if (result < 0) { |
| 646 | v9fs_put_idpool(newfid, &v9ses->fidpool); |
| 647 | if (result == -ENOENT) { |
| 648 | d_add(dentry, NULL); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 649 | dprintk(DEBUG_VFS, |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 650 | "Return negative dentry %p count %d\n", |
| 651 | dentry, atomic_read(&dentry->d_count)); |
| 652 | return NULL; |
| 653 | } |
| 654 | dprintk(DEBUG_ERROR, "walk error:%d\n", result); |
| 655 | goto FreeFcall; |
| 656 | } |
| 657 | |
| 658 | result = v9fs_t_stat(v9ses, newfid, &fcall); |
| 659 | if (result < 0) { |
| 660 | dprintk(DEBUG_ERROR, "stat error\n"); |
| 661 | goto FreeFcall; |
| 662 | } |
| 663 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 664 | inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses, |
| 665 | fcall->params.rstat.stat.mode)); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 666 | |
| 667 | if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) { |
| 668 | eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n", |
| 669 | PTR_ERR(inode)); |
| 670 | |
| 671 | result = -ENOSPC; |
| 672 | goto FreeFcall; |
| 673 | } |
| 674 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 675 | inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 676 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 677 | fid = v9fs_fid_create(v9ses, newfid); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 678 | if (fid == NULL) { |
| 679 | dprintk(DEBUG_ERROR, "couldn't insert\n"); |
| 680 | result = -ENOMEM; |
| 681 | goto FreeFcall; |
| 682 | } |
| 683 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 684 | result = v9fs_fid_insert(fid, dentry); |
| 685 | if (result < 0) |
| 686 | goto FreeFcall; |
| 687 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 688 | fid->qid = fcall->params.rstat.stat.qid; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 689 | v9fs_stat2inode(&fcall->params.rstat.stat, inode, inode->i_sb); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 690 | |
| 691 | d_add(dentry, inode); |
| 692 | kfree(fcall); |
| 693 | |
| 694 | return NULL; |
| 695 | |
| 696 | FreeFcall: |
| 697 | kfree(fcall); |
| 698 | return ERR_PTR(result); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * v9fs_vfs_unlink - VFS unlink hook to delete an inode |
| 703 | * @i: inode that is being unlinked |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 704 | * @d: dentry that is being unlinked |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 705 | * |
| 706 | */ |
| 707 | |
| 708 | static int v9fs_vfs_unlink(struct inode *i, struct dentry *d) |
| 709 | { |
| 710 | return v9fs_remove(i, d, 0); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * v9fs_vfs_rmdir - VFS unlink hook to delete a directory |
| 715 | * @i: inode that is being unlinked |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 716 | * @d: dentry that is being unlinked |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 717 | * |
| 718 | */ |
| 719 | |
| 720 | static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d) |
| 721 | { |
| 722 | return v9fs_remove(i, d, 1); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * v9fs_vfs_rename - VFS hook to rename an inode |
| 727 | * @old_dir: old dir inode |
| 728 | * @old_dentry: old dentry |
| 729 | * @new_dir: new dir inode |
| 730 | * @new_dentry: new dentry |
| 731 | * |
| 732 | */ |
| 733 | |
| 734 | static int |
| 735 | v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 736 | struct inode *new_dir, struct dentry *new_dentry) |
| 737 | { |
| 738 | struct inode *old_inode = old_dentry->d_inode; |
| 739 | struct v9fs_session_info *v9ses = v9fs_inode2v9ses(old_inode); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 740 | struct v9fs_fid *oldfid = v9fs_fid_lookup(old_dentry); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 741 | struct v9fs_fid *olddirfid = |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 742 | v9fs_fid_lookup(old_dentry->d_parent); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 743 | struct v9fs_fid *newdirfid = |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 744 | v9fs_fid_lookup(new_dentry->d_parent); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 745 | struct v9fs_wstat wstat; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 746 | struct v9fs_fcall *fcall = NULL; |
| 747 | int fid = -1; |
| 748 | int olddirfidnum = -1; |
| 749 | int newdirfidnum = -1; |
| 750 | int retval = 0; |
| 751 | |
| 752 | dprintk(DEBUG_VFS, "\n"); |
| 753 | |
| 754 | if ((!oldfid) || (!olddirfid) || (!newdirfid)) { |
| 755 | dprintk(DEBUG_ERROR, "problem with arguments\n"); |
| 756 | return -EBADF; |
| 757 | } |
| 758 | |
| 759 | /* 9P can only handle file rename in the same directory */ |
| 760 | if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) { |
| 761 | dprintk(DEBUG_ERROR, "old dir and new dir are different\n"); |
| 762 | retval = -EPERM; |
| 763 | goto FreeFcallnBail; |
| 764 | } |
| 765 | |
| 766 | fid = oldfid->fid; |
| 767 | olddirfidnum = olddirfid->fid; |
| 768 | newdirfidnum = newdirfid->fid; |
| 769 | |
| 770 | if (fid < 0) { |
| 771 | dprintk(DEBUG_ERROR, "no fid for old file #%lu\n", |
| 772 | old_inode->i_ino); |
| 773 | retval = -EBADF; |
| 774 | goto FreeFcallnBail; |
| 775 | } |
| 776 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 777 | v9fs_blank_wstat(&wstat); |
| 778 | wstat.muid = v9ses->name; |
| 779 | wstat.name = (char *) new_dentry->d_name.name; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 780 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 781 | retval = v9fs_t_wstat(v9ses, fid, &wstat, &fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 782 | |
| 783 | FreeFcallnBail: |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 784 | if (retval < 0) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 785 | PRINT_FCALL_ERROR("wstat error", fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 786 | |
| 787 | kfree(fcall); |
| 788 | return retval; |
| 789 | } |
| 790 | |
| 791 | /** |
Adrian Bunk | 943ffb5 | 2006-01-10 00:10:13 +0100 | [diff] [blame] | 792 | * v9fs_vfs_getattr - retrieve file metadata |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 793 | * @mnt - mount information |
| 794 | * @dentry - file to get attributes on |
| 795 | * @stat - metadata structure to populate |
| 796 | * |
| 797 | */ |
| 798 | |
| 799 | static int |
| 800 | v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 801 | struct kstat *stat) |
| 802 | { |
| 803 | struct v9fs_fcall *fcall = NULL; |
| 804 | struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 805 | struct v9fs_fid *fid = v9fs_fid_lookup(dentry); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 806 | int err = -EPERM; |
| 807 | |
| 808 | dprintk(DEBUG_VFS, "dentry: %p\n", dentry); |
| 809 | if (!fid) { |
| 810 | dprintk(DEBUG_ERROR, |
| 811 | "couldn't find fid associated with dentry\n"); |
| 812 | return -EBADF; |
| 813 | } |
| 814 | |
| 815 | err = v9fs_t_stat(v9ses, fid->fid, &fcall); |
| 816 | |
| 817 | if (err < 0) |
| 818 | dprintk(DEBUG_ERROR, "stat error\n"); |
| 819 | else { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 820 | v9fs_stat2inode(&fcall->params.rstat.stat, dentry->d_inode, |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 821 | dentry->d_inode->i_sb); |
| 822 | generic_fillattr(dentry->d_inode, stat); |
| 823 | } |
| 824 | |
| 825 | kfree(fcall); |
| 826 | return err; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * v9fs_vfs_setattr - set file metadata |
| 831 | * @dentry: file whose metadata to set |
| 832 | * @iattr: metadata assignment structure |
| 833 | * |
| 834 | */ |
| 835 | |
| 836 | static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 837 | { |
| 838 | struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 839 | struct v9fs_fid *fid = v9fs_fid_lookup(dentry); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 840 | struct v9fs_fcall *fcall = NULL; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 841 | struct v9fs_wstat wstat; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 842 | int res = -EPERM; |
| 843 | |
| 844 | dprintk(DEBUG_VFS, "\n"); |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 845 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 846 | if (!fid) { |
| 847 | dprintk(DEBUG_ERROR, |
| 848 | "Couldn't find fid associated with dentry\n"); |
| 849 | return -EBADF; |
| 850 | } |
| 851 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 852 | v9fs_blank_wstat(&wstat); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 853 | if (iattr->ia_valid & ATTR_MODE) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 854 | wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 855 | |
| 856 | if (iattr->ia_valid & ATTR_MTIME) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 857 | wstat.mtime = iattr->ia_mtime.tv_sec; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 858 | |
| 859 | if (iattr->ia_valid & ATTR_ATIME) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 860 | wstat.atime = iattr->ia_atime.tv_sec; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 861 | |
| 862 | if (iattr->ia_valid & ATTR_SIZE) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 863 | wstat.length = iattr->ia_size; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 864 | |
| 865 | if (v9ses->extended) { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 866 | if (iattr->ia_valid & ATTR_UID) |
| 867 | wstat.n_uid = iattr->ia_uid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 868 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 869 | if (iattr->ia_valid & ATTR_GID) |
| 870 | wstat.n_gid = iattr->ia_gid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 873 | res = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 874 | |
| 875 | if (res < 0) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 876 | PRINT_FCALL_ERROR("wstat error", fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 877 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 878 | kfree(fcall); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 879 | if (res >= 0) |
| 880 | res = inode_setattr(dentry->d_inode, iattr); |
| 881 | |
| 882 | return res; |
| 883 | } |
| 884 | |
| 885 | /** |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 886 | * v9fs_stat2inode - populate an inode structure with mistat info |
| 887 | * @stat: Plan 9 metadata (mistat) structure |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 888 | * @inode: inode to populate |
| 889 | * @sb: superblock of filesystem |
| 890 | * |
| 891 | */ |
| 892 | |
| 893 | void |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 894 | v9fs_stat2inode(struct v9fs_stat *stat, struct inode *inode, |
| 895 | struct super_block *sb) |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 896 | { |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 897 | int n; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 898 | char ext[32]; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 899 | struct v9fs_session_info *v9ses = sb->s_fs_info; |
| 900 | |
| 901 | inode->i_nlink = 1; |
| 902 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 903 | inode->i_atime.tv_sec = stat->atime; |
| 904 | inode->i_mtime.tv_sec = stat->mtime; |
| 905 | inode->i_ctime.tv_sec = stat->mtime; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 906 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 907 | inode->i_uid = v9ses->uid; |
| 908 | inode->i_gid = v9ses->gid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 909 | |
| 910 | if (v9ses->extended) { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 911 | inode->i_uid = stat->n_uid; |
| 912 | inode->i_gid = stat->n_gid; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 915 | inode->i_mode = p9mode2unixmode(v9ses, stat->mode); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 916 | if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) { |
| 917 | char type = 0; |
| 918 | int major = -1; |
| 919 | int minor = -1; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 920 | |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 921 | n = stat->extension.len; |
| 922 | if (n > sizeof(ext)-1) |
| 923 | n = sizeof(ext)-1; |
| 924 | memmove(ext, stat->extension.str, n); |
| 925 | ext[n] = 0; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 926 | sscanf(ext, "%c %u %u", &type, &major, &minor); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 927 | switch (type) { |
| 928 | case 'c': |
| 929 | inode->i_mode &= ~S_IFBLK; |
| 930 | inode->i_mode |= S_IFCHR; |
| 931 | break; |
| 932 | case 'b': |
| 933 | break; |
| 934 | default: |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 935 | dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n", |
| 936 | type, stat->extension.len, stat->extension.str); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 937 | }; |
| 938 | inode->i_rdev = MKDEV(major, minor); |
| 939 | } else |
| 940 | inode->i_rdev = 0; |
| 941 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 942 | inode->i_size = stat->length; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 943 | |
| 944 | inode->i_blksize = sb->s_blocksize; |
| 945 | inode->i_blocks = |
| 946 | (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * v9fs_qid2ino - convert qid into inode number |
| 951 | * @qid: qid to hash |
| 952 | * |
| 953 | * BUG: potential for inode number collisions? |
| 954 | */ |
| 955 | |
| 956 | ino_t v9fs_qid2ino(struct v9fs_qid *qid) |
| 957 | { |
| 958 | u64 path = qid->path + 2; |
| 959 | ino_t i = 0; |
| 960 | |
| 961 | if (sizeof(ino_t) == sizeof(path)) |
| 962 | memcpy(&i, &path, sizeof(ino_t)); |
| 963 | else |
| 964 | i = (ino_t) (path ^ (path >> 32)); |
| 965 | |
| 966 | return i; |
| 967 | } |
| 968 | |
| 969 | /** |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 970 | * v9fs_readlink - read a symlink's location (internal version) |
| 971 | * @dentry: dentry for symlink |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 972 | * @buffer: buffer to load symlink location into |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 973 | * @buflen: length of buffer |
| 974 | * |
| 975 | */ |
| 976 | |
| 977 | static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen) |
| 978 | { |
| 979 | int retval = -EPERM; |
| 980 | |
| 981 | struct v9fs_fcall *fcall = NULL; |
| 982 | struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode); |
Latchesar Ionkov | 0b8dd17 | 2005-09-27 21:45:24 -0700 | [diff] [blame] | 983 | struct v9fs_fid *fid = v9fs_fid_lookup(dentry); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 984 | |
| 985 | if (!fid) { |
| 986 | dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n"); |
| 987 | retval = -EBADF; |
| 988 | goto FreeFcall; |
| 989 | } |
| 990 | |
| 991 | if (!v9ses->extended) { |
| 992 | retval = -EBADF; |
| 993 | dprintk(DEBUG_ERROR, "not extended\n"); |
| 994 | goto FreeFcall; |
| 995 | } |
| 996 | |
| 997 | dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name); |
| 998 | retval = v9fs_t_stat(v9ses, fid->fid, &fcall); |
| 999 | |
| 1000 | if (retval < 0) { |
| 1001 | dprintk(DEBUG_ERROR, "stat error\n"); |
| 1002 | goto FreeFcall; |
| 1003 | } |
| 1004 | |
| 1005 | if (!fcall) |
| 1006 | return -EIO; |
| 1007 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1008 | if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) { |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1009 | retval = -EINVAL; |
| 1010 | goto FreeFcall; |
| 1011 | } |
| 1012 | |
| 1013 | /* copy extension buffer into buffer */ |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1014 | if (fcall->params.rstat.stat.extension.len < buflen) |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1015 | buflen = fcall->params.rstat.stat.extension.len + 1; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1016 | |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1017 | memmove(buffer, fcall->params.rstat.stat.extension.str, buflen - 1); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1018 | buffer[buflen-1] = 0; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1019 | |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1020 | dprintk(DEBUG_ERROR, "%s -> %.*s (%s)\n", dentry->d_name.name, fcall->params.rstat.stat.extension.len, |
| 1021 | fcall->params.rstat.stat.extension.str, buffer); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1022 | retval = buflen; |
| 1023 | |
| 1024 | FreeFcall: |
| 1025 | kfree(fcall); |
| 1026 | |
| 1027 | return retval; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
| 1031 | * v9fs_vfs_readlink - read a symlink's location |
| 1032 | * @dentry: dentry for symlink |
| 1033 | * @buf: buffer to load symlink location into |
| 1034 | * @buflen: length of buffer |
| 1035 | * |
| 1036 | */ |
| 1037 | |
| 1038 | static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer, |
| 1039 | int buflen) |
| 1040 | { |
| 1041 | int retval; |
| 1042 | int ret; |
| 1043 | char *link = __getname(); |
| 1044 | |
Latchesar Ionkov | a1f9d8d | 2005-09-22 21:43:52 -0700 | [diff] [blame] | 1045 | if (buflen > PATH_MAX) |
| 1046 | buflen = PATH_MAX; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1047 | |
| 1048 | dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry); |
| 1049 | |
| 1050 | retval = v9fs_readlink(dentry, link, buflen); |
| 1051 | |
| 1052 | if (retval > 0) { |
| 1053 | if ((ret = copy_to_user(buffer, link, retval)) != 0) { |
| 1054 | dprintk(DEBUG_ERROR, "problem copying to user: %d\n", |
| 1055 | ret); |
| 1056 | retval = ret; |
| 1057 | } |
| 1058 | } |
| 1059 | |
Davi Arnaut | ce44eeb | 2005-11-07 00:59:36 -0800 | [diff] [blame] | 1060 | __putname(link); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1061 | return retval; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * v9fs_vfs_follow_link - follow a symlink path |
| 1066 | * @dentry: dentry for symlink |
| 1067 | * @nd: nameidata |
| 1068 | * |
| 1069 | */ |
| 1070 | |
| 1071 | static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1072 | { |
| 1073 | int len = 0; |
| 1074 | char *link = __getname(); |
| 1075 | |
| 1076 | dprintk(DEBUG_VFS, "%s n", dentry->d_name.name); |
| 1077 | |
| 1078 | if (!link) |
| 1079 | link = ERR_PTR(-ENOMEM); |
| 1080 | else { |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1081 | len = v9fs_readlink(dentry, link, PATH_MAX); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1082 | |
| 1083 | if (len < 0) { |
Davi Arnaut | ce44eeb | 2005-11-07 00:59:36 -0800 | [diff] [blame] | 1084 | __putname(link); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1085 | link = ERR_PTR(len); |
| 1086 | } else |
| 1087 | link[len] = 0; |
| 1088 | } |
| 1089 | nd_set_link(nd, link); |
| 1090 | |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * v9fs_vfs_put_link - release a symlink path |
| 1096 | * @dentry: dentry for symlink |
| 1097 | * @nd: nameidata |
| 1098 | * |
| 1099 | */ |
| 1100 | |
| 1101 | static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p) |
| 1102 | { |
| 1103 | char *s = nd_get_link(nd); |
| 1104 | |
| 1105 | dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s); |
| 1106 | if (!IS_ERR(s)) |
Davi Arnaut | ce44eeb | 2005-11-07 00:59:36 -0800 | [diff] [blame] | 1107 | __putname(s); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1110 | static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry, |
| 1111 | int mode, const char *extension) |
| 1112 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1113 | int err; |
| 1114 | u32 fid, perm; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1115 | struct v9fs_session_info *v9ses; |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1116 | struct v9fs_fid *dfid, *vfid; |
| 1117 | struct inode *inode; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1118 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1119 | inode = NULL; |
| 1120 | vfid = NULL; |
| 1121 | v9ses = v9fs_inode2v9ses(dir); |
| 1122 | dfid = v9fs_fid_lookup(dentry->d_parent); |
| 1123 | perm = unixmode2p9mode(v9ses, mode); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1124 | |
| 1125 | if (!v9ses->extended) { |
| 1126 | dprintk(DEBUG_ERROR, "not extended\n"); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1127 | return -EPERM; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1130 | err = v9fs_create(v9ses, dfid->fid, (char *) dentry->d_name.name, |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1131 | perm, V9FS_OREAD, (char *) extension, &fid, NULL, NULL); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1132 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1133 | if (err) |
| 1134 | goto error; |
| 1135 | |
| 1136 | err = v9fs_t_clunk(v9ses, fid); |
| 1137 | if (err) |
| 1138 | goto error; |
| 1139 | |
| 1140 | vfid = v9fs_clone_walk(v9ses, dfid->fid, dentry); |
| 1141 | if (IS_ERR(vfid)) { |
| 1142 | err = PTR_ERR(vfid); |
| 1143 | vfid = NULL; |
| 1144 | goto error; |
| 1145 | } |
| 1146 | |
| 1147 | inode = v9fs_inode_from_fid(v9ses, vfid->fid, dir->i_sb); |
| 1148 | if (IS_ERR(inode)) { |
| 1149 | err = PTR_ERR(inode); |
| 1150 | inode = NULL; |
| 1151 | goto error; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1154 | dentry->d_op = &v9fs_dentry_operations; |
| 1155 | d_instantiate(dentry, inode); |
| 1156 | return 0; |
| 1157 | |
| 1158 | error: |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 1159 | if (vfid) |
| 1160 | v9fs_fid_destroy(vfid); |
| 1161 | |
| 1162 | if (inode) |
| 1163 | iput(inode); |
| 1164 | |
| 1165 | return err; |
| 1166 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * v9fs_vfs_symlink - helper function to create symlinks |
| 1171 | * @dir: directory inode containing symlink |
| 1172 | * @dentry: dentry for symlink |
| 1173 | * @symname: symlink data |
| 1174 | * |
| 1175 | * See 9P2000.u RFC for more information |
| 1176 | * |
| 1177 | */ |
| 1178 | |
| 1179 | static int |
| 1180 | v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 1181 | { |
| 1182 | dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name, |
| 1183 | symname); |
| 1184 | |
| 1185 | return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname); |
| 1186 | } |
| 1187 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * v9fs_vfs_link - create a hardlink |
| 1190 | * @old_dentry: dentry for file to link to |
| 1191 | * @dir: inode destination for new link |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 1192 | * @dentry: dentry for link |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1193 | * |
| 1194 | */ |
| 1195 | |
| 1196 | /* XXX - lots of code dup'd from symlink and creates, |
| 1197 | * figure out a better reuse strategy |
| 1198 | */ |
| 1199 | |
| 1200 | static int |
| 1201 | v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1202 | struct dentry *dentry) |
| 1203 | { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1204 | int retval; |
| 1205 | struct v9fs_fid *oldfid; |
| 1206 | char *name; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1207 | |
| 1208 | dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name, |
| 1209 | old_dentry->d_name.name); |
| 1210 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1211 | oldfid = v9fs_fid_lookup(old_dentry); |
| 1212 | if (!oldfid) { |
| 1213 | dprintk(DEBUG_ERROR, "can't find oldfid\n"); |
| 1214 | return -EPERM; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1217 | name = __getname(); |
Latchesar Ionkov | 16cce6d | 2006-03-25 03:07:26 -0800 | [diff] [blame] | 1218 | sprintf(name, "%d\n", oldfid->fid); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1219 | retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name); |
| 1220 | __putname(name); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1221 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1222 | return retval; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * v9fs_vfs_mknod - create a special file |
| 1227 | * @dir: inode destination for new link |
| 1228 | * @dentry: dentry for file |
| 1229 | * @mode: mode for creation |
| 1230 | * @dev_t: device associated with special file |
| 1231 | * |
| 1232 | */ |
| 1233 | |
| 1234 | static int |
| 1235 | v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) |
| 1236 | { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1237 | int retval; |
| 1238 | char *name; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1239 | |
| 1240 | dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino, |
| 1241 | dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev)); |
| 1242 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1243 | if (!new_valid_dev(rdev)) |
| 1244 | return -EINVAL; |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 1245 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1246 | name = __getname(); |
Eugene Teo | c0291a0 | 2006-03-25 03:07:27 -0800 | [diff] [blame^] | 1247 | if (!name) |
| 1248 | return -ENOMEM; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1249 | /* build extension */ |
| 1250 | if (S_ISBLK(mode)) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1251 | sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev)); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1252 | else if (S_ISCHR(mode)) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1253 | sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev)); |
Eric Van Hensbergen | 73c592b | 2005-09-09 13:04:26 -0700 | [diff] [blame] | 1254 | else if (S_ISFIFO(mode)) |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1255 | *name = 0; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1256 | else { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1257 | __putname(name); |
| 1258 | return -EINVAL; |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 1261 | retval = v9fs_vfs_mkspecial(dir, dentry, mode, name); |
| 1262 | __putname(name); |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1263 | |
| 1264 | return retval; |
| 1265 | } |
| 1266 | |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 1267 | static struct inode_operations v9fs_dir_inode_operations_ext = { |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1268 | .create = v9fs_vfs_create, |
| 1269 | .lookup = v9fs_vfs_lookup, |
| 1270 | .symlink = v9fs_vfs_symlink, |
| 1271 | .link = v9fs_vfs_link, |
| 1272 | .unlink = v9fs_vfs_unlink, |
| 1273 | .mkdir = v9fs_vfs_mkdir, |
| 1274 | .rmdir = v9fs_vfs_rmdir, |
| 1275 | .mknod = v9fs_vfs_mknod, |
| 1276 | .rename = v9fs_vfs_rename, |
| 1277 | .readlink = v9fs_vfs_readlink, |
| 1278 | .getattr = v9fs_vfs_getattr, |
| 1279 | .setattr = v9fs_vfs_setattr, |
| 1280 | }; |
| 1281 | |
Eric Van Hensbergen | b501611 | 2005-09-09 13:04:27 -0700 | [diff] [blame] | 1282 | static struct inode_operations v9fs_dir_inode_operations = { |
| 1283 | .create = v9fs_vfs_create, |
| 1284 | .lookup = v9fs_vfs_lookup, |
| 1285 | .unlink = v9fs_vfs_unlink, |
| 1286 | .mkdir = v9fs_vfs_mkdir, |
| 1287 | .rmdir = v9fs_vfs_rmdir, |
| 1288 | .mknod = v9fs_vfs_mknod, |
| 1289 | .rename = v9fs_vfs_rename, |
| 1290 | .getattr = v9fs_vfs_getattr, |
| 1291 | .setattr = v9fs_vfs_setattr, |
| 1292 | }; |
| 1293 | |
Eric Van Hensbergen | 2bad847 | 2005-09-09 13:04:19 -0700 | [diff] [blame] | 1294 | static struct inode_operations v9fs_file_inode_operations = { |
| 1295 | .getattr = v9fs_vfs_getattr, |
| 1296 | .setattr = v9fs_vfs_setattr, |
| 1297 | }; |
| 1298 | |
| 1299 | static struct inode_operations v9fs_symlink_inode_operations = { |
| 1300 | .readlink = v9fs_vfs_readlink, |
| 1301 | .follow_link = v9fs_vfs_follow_link, |
| 1302 | .put_link = v9fs_vfs_put_link, |
| 1303 | .getattr = v9fs_vfs_getattr, |
| 1304 | .setattr = v9fs_vfs_setattr, |
| 1305 | }; |