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