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