| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 1 | /* | 
|  | 2 | *  linux/fs/9p/vfs_inode_dotl.c | 
|  | 3 | * | 
|  | 4 | * This file contains vfs inode ops for the 9P2000.L protocol. | 
|  | 5 | * | 
|  | 6 | *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> | 
|  | 7 | *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> | 
|  | 8 | * | 
|  | 9 | *  This program is free software; you can redistribute it and/or modify | 
|  | 10 | *  it under the terms of the GNU General Public License version 2 | 
|  | 11 | *  as published by the Free Software Foundation. | 
|  | 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/inet.h> | 
|  | 34 | #include <linux/namei.h> | 
|  | 35 | #include <linux/idr.h> | 
|  | 36 | #include <linux/sched.h> | 
|  | 37 | #include <linux/slab.h> | 
|  | 38 | #include <linux/xattr.h> | 
|  | 39 | #include <linux/posix_acl.h> | 
|  | 40 | #include <net/9p/9p.h> | 
|  | 41 | #include <net/9p/client.h> | 
|  | 42 |  | 
|  | 43 | #include "v9fs.h" | 
|  | 44 | #include "v9fs_vfs.h" | 
|  | 45 | #include "fid.h" | 
|  | 46 | #include "cache.h" | 
|  | 47 | #include "xattr.h" | 
|  | 48 | #include "acl.h" | 
|  | 49 |  | 
|  | 50 | static int | 
|  | 51 | v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode, | 
|  | 52 | dev_t rdev); | 
|  | 53 |  | 
|  | 54 | /** | 
|  | 55 | * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a | 
|  | 56 | * new file system object. This checks the S_ISGID to determine the owning | 
|  | 57 | * group of the new file system object. | 
|  | 58 | */ | 
|  | 59 |  | 
|  | 60 | static gid_t v9fs_get_fsgid_for_create(struct inode *dir_inode) | 
|  | 61 | { | 
|  | 62 | BUG_ON(dir_inode == NULL); | 
|  | 63 |  | 
|  | 64 | if (dir_inode->i_mode & S_ISGID) { | 
|  | 65 | /* set_gid bit is set.*/ | 
|  | 66 | return dir_inode->i_gid; | 
|  | 67 | } | 
|  | 68 | return current_fsgid(); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | /** | 
|  | 72 | * v9fs_dentry_from_dir_inode - helper function to get the dentry from | 
|  | 73 | * dir inode. | 
|  | 74 | * | 
|  | 75 | */ | 
|  | 76 |  | 
|  | 77 | static struct dentry *v9fs_dentry_from_dir_inode(struct inode *inode) | 
|  | 78 | { | 
|  | 79 | struct dentry *dentry; | 
|  | 80 |  | 
|  | 81 | spin_lock(&inode->i_lock); | 
|  | 82 | /* Directory should have only one entry. */ | 
|  | 83 | BUG_ON(S_ISDIR(inode->i_mode) && !list_is_singular(&inode->i_dentry)); | 
|  | 84 | dentry = list_entry(inode->i_dentry.next, struct dentry, d_alias); | 
|  | 85 | spin_unlock(&inode->i_lock); | 
|  | 86 | return dentry; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | struct inode * | 
|  | 90 | v9fs_inode_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid, | 
|  | 91 | struct super_block *sb) | 
|  | 92 | { | 
|  | 93 | struct inode *ret = NULL; | 
|  | 94 | int err; | 
|  | 95 | struct p9_stat_dotl *st; | 
|  | 96 |  | 
|  | 97 | st = p9_client_getattr_dotl(fid, P9_STATS_BASIC); | 
|  | 98 | if (IS_ERR(st)) | 
|  | 99 | return ERR_CAST(st); | 
|  | 100 |  | 
|  | 101 | ret = v9fs_get_inode(sb, st->st_mode); | 
|  | 102 | if (IS_ERR(ret)) { | 
|  | 103 | err = PTR_ERR(ret); | 
|  | 104 | goto error; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | v9fs_stat2inode_dotl(st, ret); | 
|  | 108 | ret->i_ino = v9fs_qid2ino(&st->qid); | 
|  | 109 | #ifdef CONFIG_9P_FSCACHE | 
|  | 110 | v9fs_vcookie_set_qid(ret, &st->qid); | 
|  | 111 | v9fs_cache_inode_get_cookie(ret); | 
|  | 112 | #endif | 
|  | 113 | err = v9fs_get_acl(ret, fid); | 
|  | 114 | if (err) { | 
|  | 115 | iput(ret); | 
|  | 116 | goto error; | 
|  | 117 | } | 
|  | 118 | kfree(st); | 
|  | 119 | return ret; | 
|  | 120 | error: | 
|  | 121 | kfree(st); | 
|  | 122 | return ERR_PTR(err); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | /** | 
|  | 126 | * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol. | 
|  | 127 | * @dir: directory inode that is being created | 
|  | 128 | * @dentry:  dentry that is being deleted | 
|  | 129 | * @mode: create permissions | 
|  | 130 | * @nd: path information | 
|  | 131 | * | 
|  | 132 | */ | 
|  | 133 |  | 
|  | 134 | static int | 
|  | 135 | v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, int omode, | 
|  | 136 | struct nameidata *nd) | 
|  | 137 | { | 
|  | 138 | int err = 0; | 
|  | 139 | char *name = NULL; | 
|  | 140 | gid_t gid; | 
|  | 141 | int flags; | 
|  | 142 | mode_t mode; | 
|  | 143 | struct v9fs_session_info *v9ses; | 
|  | 144 | struct p9_fid *fid = NULL; | 
|  | 145 | struct p9_fid *dfid, *ofid; | 
|  | 146 | struct file *filp; | 
|  | 147 | struct p9_qid qid; | 
|  | 148 | struct inode *inode; | 
|  | 149 | struct posix_acl *pacl = NULL, *dacl = NULL; | 
|  | 150 |  | 
|  | 151 | v9ses = v9fs_inode2v9ses(dir); | 
|  | 152 | if (nd && nd->flags & LOOKUP_OPEN) | 
|  | 153 | flags = nd->intent.open.flags - 1; | 
|  | 154 | else { | 
|  | 155 | /* | 
|  | 156 | * create call without LOOKUP_OPEN is due | 
|  | 157 | * to mknod of regular files. So use mknod | 
|  | 158 | * operation. | 
|  | 159 | */ | 
|  | 160 | return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | name = (char *) dentry->d_name.name; | 
|  | 164 | P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_create_dotl: name:%s flags:0x%x " | 
|  | 165 | "mode:0x%x\n", name, flags, omode); | 
|  | 166 |  | 
|  | 167 | dfid = v9fs_fid_lookup(dentry->d_parent); | 
|  | 168 | if (IS_ERR(dfid)) { | 
|  | 169 | err = PTR_ERR(dfid); | 
|  | 170 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | 
|  | 171 | return err; | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | /* clone a fid to use for creation */ | 
|  | 175 | ofid = p9_client_walk(dfid, 0, NULL, 1); | 
|  | 176 | if (IS_ERR(ofid)) { | 
|  | 177 | err = PTR_ERR(ofid); | 
|  | 178 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); | 
|  | 179 | return err; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | gid = v9fs_get_fsgid_for_create(dir); | 
|  | 183 |  | 
|  | 184 | mode = omode; | 
|  | 185 | /* Update mode based on ACL value */ | 
|  | 186 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | 
|  | 187 | if (err) { | 
|  | 188 | P9_DPRINTK(P9_DEBUG_VFS, | 
|  | 189 | "Failed to get acl values in creat %d\n", err); | 
|  | 190 | goto error; | 
|  | 191 | } | 
|  | 192 | err = p9_client_create_dotl(ofid, name, flags, mode, gid, &qid); | 
|  | 193 | if (err < 0) { | 
|  | 194 | P9_DPRINTK(P9_DEBUG_VFS, | 
|  | 195 | "p9_client_open_dotl failed in creat %d\n", | 
|  | 196 | err); | 
|  | 197 | goto error; | 
|  | 198 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 199 |  | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 200 | /* instantiate inode and assign the unopened fid to the dentry */ | 
|  | 201 | fid = p9_client_walk(dfid, 1, &name, 1); | 
|  | 202 | if (IS_ERR(fid)) { | 
|  | 203 | err = PTR_ERR(fid); | 
| Eric Van Hensbergen | c25a61f | 2011-01-11 09:49:03 -0600 | [diff] [blame] | 204 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 205 | fid = NULL; | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 206 | goto error; | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 207 | } | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 208 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | 
|  | 209 | if (IS_ERR(inode)) { | 
|  | 210 | err = PTR_ERR(inode); | 
|  | 211 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err); | 
|  | 212 | goto error; | 
|  | 213 | } | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 214 | d_instantiate(dentry, inode); | 
|  | 215 | err = v9fs_fid_add(dentry, fid); | 
|  | 216 | if (err < 0) | 
|  | 217 | goto error; | 
|  | 218 |  | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 219 | /* Now set the ACL based on the default value */ | 
|  | 220 | v9fs_set_create_acl(dentry, dacl, pacl); | 
|  | 221 |  | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 222 | /* Since we are opening a file, assign the open fid to the file */ | 
|  | 223 | filp = lookup_instantiate_filp(nd, dentry, generic_file_open); | 
|  | 224 | if (IS_ERR(filp)) { | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 225 | p9_client_clunk(ofid); | 
| Aneesh Kumar K.V | af7542f | 2011-01-10 14:22:21 -0600 | [diff] [blame] | 226 | return PTR_ERR(filp); | 
|  | 227 | } | 
|  | 228 | filp->private_data = ofid; | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 229 | return 0; | 
|  | 230 |  | 
|  | 231 | error: | 
|  | 232 | if (ofid) | 
|  | 233 | p9_client_clunk(ofid); | 
|  | 234 | if (fid) | 
|  | 235 | p9_client_clunk(fid); | 
|  | 236 | return err; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | /** | 
|  | 240 | * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory | 
|  | 241 | * @dir:  inode that is being unlinked | 
|  | 242 | * @dentry: dentry that is being unlinked | 
|  | 243 | * @mode: mode for new directory | 
|  | 244 | * | 
|  | 245 | */ | 
|  | 246 |  | 
|  | 247 | static int v9fs_vfs_mkdir_dotl(struct inode *dir, | 
|  | 248 | struct dentry *dentry, int omode) | 
|  | 249 | { | 
|  | 250 | int err; | 
|  | 251 | struct v9fs_session_info *v9ses; | 
|  | 252 | struct p9_fid *fid = NULL, *dfid = NULL; | 
|  | 253 | gid_t gid; | 
|  | 254 | char *name; | 
|  | 255 | mode_t mode; | 
|  | 256 | struct inode *inode; | 
|  | 257 | struct p9_qid qid; | 
|  | 258 | struct dentry *dir_dentry; | 
|  | 259 | struct posix_acl *dacl = NULL, *pacl = NULL; | 
|  | 260 |  | 
|  | 261 | P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name); | 
|  | 262 | err = 0; | 
|  | 263 | v9ses = v9fs_inode2v9ses(dir); | 
|  | 264 |  | 
|  | 265 | omode |= S_IFDIR; | 
|  | 266 | if (dir->i_mode & S_ISGID) | 
|  | 267 | omode |= S_ISGID; | 
|  | 268 |  | 
|  | 269 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | 
|  | 270 | dfid = v9fs_fid_lookup(dir_dentry); | 
|  | 271 | if (IS_ERR(dfid)) { | 
|  | 272 | err = PTR_ERR(dfid); | 
|  | 273 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | 
|  | 274 | dfid = NULL; | 
|  | 275 | goto error; | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | gid = v9fs_get_fsgid_for_create(dir); | 
|  | 279 | mode = omode; | 
|  | 280 | /* Update mode based on ACL value */ | 
|  | 281 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | 
|  | 282 | if (err) { | 
|  | 283 | P9_DPRINTK(P9_DEBUG_VFS, | 
|  | 284 | "Failed to get acl values in mkdir %d\n", err); | 
|  | 285 | goto error; | 
|  | 286 | } | 
|  | 287 | name = (char *) dentry->d_name.name; | 
|  | 288 | err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid); | 
|  | 289 | if (err < 0) | 
|  | 290 | goto error; | 
|  | 291 |  | 
|  | 292 | /* instantiate inode and assign the unopened fid to the dentry */ | 
|  | 293 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | 
|  | 294 | fid = p9_client_walk(dfid, 1, &name, 1); | 
|  | 295 | if (IS_ERR(fid)) { | 
|  | 296 | err = PTR_ERR(fid); | 
|  | 297 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | 
|  | 298 | err); | 
|  | 299 | fid = NULL; | 
|  | 300 | goto error; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | 
|  | 304 | if (IS_ERR(inode)) { | 
|  | 305 | err = PTR_ERR(inode); | 
|  | 306 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | 
|  | 307 | err); | 
|  | 308 | goto error; | 
|  | 309 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 310 | d_instantiate(dentry, inode); | 
|  | 311 | err = v9fs_fid_add(dentry, fid); | 
|  | 312 | if (err < 0) | 
|  | 313 | goto error; | 
|  | 314 | fid = NULL; | 
|  | 315 | } else { | 
|  | 316 | /* | 
|  | 317 | * Not in cached mode. No need to populate | 
|  | 318 | * inode with stat. We need to get an inode | 
|  | 319 | * so that we can set the acl with dentry | 
|  | 320 | */ | 
|  | 321 | inode = v9fs_get_inode(dir->i_sb, mode); | 
|  | 322 | if (IS_ERR(inode)) { | 
|  | 323 | err = PTR_ERR(inode); | 
|  | 324 | goto error; | 
|  | 325 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 326 | d_instantiate(dentry, inode); | 
|  | 327 | } | 
|  | 328 | /* Now set the ACL based on the default value */ | 
|  | 329 | v9fs_set_create_acl(dentry, dacl, pacl); | 
|  | 330 |  | 
|  | 331 | error: | 
|  | 332 | if (fid) | 
|  | 333 | p9_client_clunk(fid); | 
|  | 334 | return err; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | static int | 
|  | 338 | v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry, | 
|  | 339 | struct kstat *stat) | 
|  | 340 | { | 
|  | 341 | int err; | 
|  | 342 | struct v9fs_session_info *v9ses; | 
|  | 343 | struct p9_fid *fid; | 
|  | 344 | struct p9_stat_dotl *st; | 
|  | 345 |  | 
|  | 346 | P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry); | 
|  | 347 | err = -EPERM; | 
|  | 348 | v9ses = v9fs_inode2v9ses(dentry->d_inode); | 
|  | 349 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) | 
|  | 350 | return simple_getattr(mnt, dentry, stat); | 
|  | 351 |  | 
|  | 352 | fid = v9fs_fid_lookup(dentry); | 
|  | 353 | if (IS_ERR(fid)) | 
|  | 354 | return PTR_ERR(fid); | 
|  | 355 |  | 
|  | 356 | /* Ask for all the fields in stat structure. Server will return | 
|  | 357 | * whatever it supports | 
|  | 358 | */ | 
|  | 359 |  | 
|  | 360 | st = p9_client_getattr_dotl(fid, P9_STATS_ALL); | 
|  | 361 | if (IS_ERR(st)) | 
|  | 362 | return PTR_ERR(st); | 
|  | 363 |  | 
|  | 364 | v9fs_stat2inode_dotl(st, dentry->d_inode); | 
|  | 365 | generic_fillattr(dentry->d_inode, stat); | 
|  | 366 | /* Change block size to what the server returned */ | 
|  | 367 | stat->blksize = st->st_blksize; | 
|  | 368 |  | 
|  | 369 | kfree(st); | 
|  | 370 | return 0; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | /** | 
|  | 374 | * v9fs_vfs_setattr_dotl - set file metadata | 
|  | 375 | * @dentry: file whose metadata to set | 
|  | 376 | * @iattr: metadata assignment structure | 
|  | 377 | * | 
|  | 378 | */ | 
|  | 379 |  | 
|  | 380 | int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr) | 
|  | 381 | { | 
|  | 382 | int retval; | 
|  | 383 | struct v9fs_session_info *v9ses; | 
|  | 384 | struct p9_fid *fid; | 
|  | 385 | struct p9_iattr_dotl p9attr; | 
|  | 386 |  | 
|  | 387 | P9_DPRINTK(P9_DEBUG_VFS, "\n"); | 
|  | 388 |  | 
|  | 389 | retval = inode_change_ok(dentry->d_inode, iattr); | 
|  | 390 | if (retval) | 
|  | 391 | return retval; | 
|  | 392 |  | 
|  | 393 | p9attr.valid = iattr->ia_valid; | 
|  | 394 | p9attr.mode = iattr->ia_mode; | 
|  | 395 | p9attr.uid = iattr->ia_uid; | 
|  | 396 | p9attr.gid = iattr->ia_gid; | 
|  | 397 | p9attr.size = iattr->ia_size; | 
|  | 398 | p9attr.atime_sec = iattr->ia_atime.tv_sec; | 
|  | 399 | p9attr.atime_nsec = iattr->ia_atime.tv_nsec; | 
|  | 400 | p9attr.mtime_sec = iattr->ia_mtime.tv_sec; | 
|  | 401 | p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec; | 
|  | 402 |  | 
|  | 403 | retval = -EPERM; | 
|  | 404 | v9ses = v9fs_inode2v9ses(dentry->d_inode); | 
|  | 405 | fid = v9fs_fid_lookup(dentry); | 
|  | 406 | if (IS_ERR(fid)) | 
|  | 407 | return PTR_ERR(fid); | 
|  | 408 |  | 
|  | 409 | retval = p9_client_setattr(fid, &p9attr); | 
|  | 410 | if (retval < 0) | 
|  | 411 | return retval; | 
|  | 412 |  | 
|  | 413 | if ((iattr->ia_valid & ATTR_SIZE) && | 
|  | 414 | iattr->ia_size != i_size_read(dentry->d_inode)) { | 
|  | 415 | retval = vmtruncate(dentry->d_inode, iattr->ia_size); | 
|  | 416 | if (retval) | 
|  | 417 | return retval; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | setattr_copy(dentry->d_inode, iattr); | 
|  | 421 | mark_inode_dirty(dentry->d_inode); | 
|  | 422 | if (iattr->ia_valid & ATTR_MODE) { | 
|  | 423 | /* We also want to update ACL when we update mode bits */ | 
|  | 424 | retval = v9fs_acl_chmod(dentry); | 
|  | 425 | if (retval < 0) | 
|  | 426 | return retval; | 
|  | 427 | } | 
|  | 428 | return 0; | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | /** | 
|  | 432 | * v9fs_stat2inode_dotl - populate an inode structure with stat info | 
|  | 433 | * @stat: stat structure | 
|  | 434 | * @inode: inode to populate | 
|  | 435 | * @sb: superblock of filesystem | 
|  | 436 | * | 
|  | 437 | */ | 
|  | 438 |  | 
|  | 439 | void | 
|  | 440 | v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode) | 
|  | 441 | { | 
|  | 442 |  | 
|  | 443 | if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) { | 
|  | 444 | inode->i_atime.tv_sec = stat->st_atime_sec; | 
|  | 445 | inode->i_atime.tv_nsec = stat->st_atime_nsec; | 
|  | 446 | inode->i_mtime.tv_sec = stat->st_mtime_sec; | 
|  | 447 | inode->i_mtime.tv_nsec = stat->st_mtime_nsec; | 
|  | 448 | inode->i_ctime.tv_sec = stat->st_ctime_sec; | 
|  | 449 | inode->i_ctime.tv_nsec = stat->st_ctime_nsec; | 
|  | 450 | inode->i_uid = stat->st_uid; | 
|  | 451 | inode->i_gid = stat->st_gid; | 
|  | 452 | inode->i_nlink = stat->st_nlink; | 
|  | 453 | inode->i_mode = stat->st_mode; | 
|  | 454 | inode->i_rdev = new_decode_dev(stat->st_rdev); | 
|  | 455 |  | 
|  | 456 | if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) | 
|  | 457 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | 
|  | 458 |  | 
|  | 459 | i_size_write(inode, stat->st_size); | 
|  | 460 | inode->i_blocks = stat->st_blocks; | 
|  | 461 | } else { | 
|  | 462 | if (stat->st_result_mask & P9_STATS_ATIME) { | 
|  | 463 | inode->i_atime.tv_sec = stat->st_atime_sec; | 
|  | 464 | inode->i_atime.tv_nsec = stat->st_atime_nsec; | 
|  | 465 | } | 
|  | 466 | if (stat->st_result_mask & P9_STATS_MTIME) { | 
|  | 467 | inode->i_mtime.tv_sec = stat->st_mtime_sec; | 
|  | 468 | inode->i_mtime.tv_nsec = stat->st_mtime_nsec; | 
|  | 469 | } | 
|  | 470 | if (stat->st_result_mask & P9_STATS_CTIME) { | 
|  | 471 | inode->i_ctime.tv_sec = stat->st_ctime_sec; | 
|  | 472 | inode->i_ctime.tv_nsec = stat->st_ctime_nsec; | 
|  | 473 | } | 
|  | 474 | if (stat->st_result_mask & P9_STATS_UID) | 
|  | 475 | inode->i_uid = stat->st_uid; | 
|  | 476 | if (stat->st_result_mask & P9_STATS_GID) | 
|  | 477 | inode->i_gid = stat->st_gid; | 
|  | 478 | if (stat->st_result_mask & P9_STATS_NLINK) | 
|  | 479 | inode->i_nlink = stat->st_nlink; | 
|  | 480 | if (stat->st_result_mask & P9_STATS_MODE) { | 
|  | 481 | inode->i_mode = stat->st_mode; | 
|  | 482 | if ((S_ISBLK(inode->i_mode)) || | 
|  | 483 | (S_ISCHR(inode->i_mode))) | 
|  | 484 | init_special_inode(inode, inode->i_mode, | 
|  | 485 | inode->i_rdev); | 
|  | 486 | } | 
|  | 487 | if (stat->st_result_mask & P9_STATS_RDEV) | 
|  | 488 | inode->i_rdev = new_decode_dev(stat->st_rdev); | 
|  | 489 | if (stat->st_result_mask & P9_STATS_SIZE) | 
|  | 490 | i_size_write(inode, stat->st_size); | 
|  | 491 | if (stat->st_result_mask & P9_STATS_BLOCKS) | 
|  | 492 | inode->i_blocks = stat->st_blocks; | 
|  | 493 | } | 
|  | 494 | if (stat->st_result_mask & P9_STATS_GEN) | 
|  | 495 | inode->i_generation = stat->st_gen; | 
|  | 496 |  | 
|  | 497 | /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION | 
|  | 498 | * because the inode structure does not have fields for them. | 
|  | 499 | */ | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | static int | 
|  | 503 | v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry, | 
|  | 504 | const char *symname) | 
|  | 505 | { | 
|  | 506 | struct v9fs_session_info *v9ses; | 
|  | 507 | struct p9_fid *dfid; | 
|  | 508 | struct p9_fid *fid = NULL; | 
|  | 509 | struct inode *inode; | 
|  | 510 | struct p9_qid qid; | 
|  | 511 | char *name; | 
|  | 512 | int err; | 
|  | 513 | gid_t gid; | 
|  | 514 |  | 
|  | 515 | name = (char *) dentry->d_name.name; | 
|  | 516 | P9_DPRINTK(P9_DEBUG_VFS, "v9fs_vfs_symlink_dotl : %lu,%s,%s\n", | 
|  | 517 | dir->i_ino, name, symname); | 
|  | 518 | v9ses = v9fs_inode2v9ses(dir); | 
|  | 519 |  | 
|  | 520 | dfid = v9fs_fid_lookup(dentry->d_parent); | 
|  | 521 | if (IS_ERR(dfid)) { | 
|  | 522 | err = PTR_ERR(dfid); | 
|  | 523 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | 
|  | 524 | return err; | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | gid = v9fs_get_fsgid_for_create(dir); | 
|  | 528 |  | 
|  | 529 | /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */ | 
|  | 530 | err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid); | 
|  | 531 |  | 
|  | 532 | if (err < 0) { | 
|  | 533 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err); | 
|  | 534 | goto error; | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | if (v9ses->cache) { | 
|  | 538 | /* Now walk from the parent so we can get an unopened fid. */ | 
|  | 539 | fid = p9_client_walk(dfid, 1, &name, 1); | 
|  | 540 | if (IS_ERR(fid)) { | 
|  | 541 | err = PTR_ERR(fid); | 
|  | 542 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | 
|  | 543 | err); | 
|  | 544 | fid = NULL; | 
|  | 545 | goto error; | 
|  | 546 | } | 
|  | 547 |  | 
|  | 548 | /* instantiate inode and assign the unopened fid to dentry */ | 
|  | 549 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | 
|  | 550 | if (IS_ERR(inode)) { | 
|  | 551 | err = PTR_ERR(inode); | 
|  | 552 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | 
|  | 553 | err); | 
|  | 554 | goto error; | 
|  | 555 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 556 | d_instantiate(dentry, inode); | 
|  | 557 | err = v9fs_fid_add(dentry, fid); | 
|  | 558 | if (err < 0) | 
|  | 559 | goto error; | 
|  | 560 | fid = NULL; | 
|  | 561 | } else { | 
|  | 562 | /* Not in cached mode. No need to populate inode with stat */ | 
|  | 563 | inode = v9fs_get_inode(dir->i_sb, S_IFLNK); | 
|  | 564 | if (IS_ERR(inode)) { | 
|  | 565 | err = PTR_ERR(inode); | 
|  | 566 | goto error; | 
|  | 567 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 568 | d_instantiate(dentry, inode); | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | error: | 
|  | 572 | if (fid) | 
|  | 573 | p9_client_clunk(fid); | 
|  | 574 |  | 
|  | 575 | return err; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | /** | 
|  | 579 | * v9fs_vfs_link_dotl - create a hardlink for dotl | 
|  | 580 | * @old_dentry: dentry for file to link to | 
|  | 581 | * @dir: inode destination for new link | 
|  | 582 | * @dentry: dentry for link | 
|  | 583 | * | 
|  | 584 | */ | 
|  | 585 |  | 
|  | 586 | static int | 
|  | 587 | v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir, | 
|  | 588 | struct dentry *dentry) | 
|  | 589 | { | 
|  | 590 | int err; | 
|  | 591 | struct p9_fid *dfid, *oldfid; | 
|  | 592 | char *name; | 
|  | 593 | struct v9fs_session_info *v9ses; | 
|  | 594 | struct dentry *dir_dentry; | 
|  | 595 |  | 
|  | 596 | P9_DPRINTK(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n", | 
|  | 597 | dir->i_ino, old_dentry->d_name.name, | 
|  | 598 | dentry->d_name.name); | 
|  | 599 |  | 
|  | 600 | v9ses = v9fs_inode2v9ses(dir); | 
|  | 601 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | 
|  | 602 | dfid = v9fs_fid_lookup(dir_dentry); | 
|  | 603 | if (IS_ERR(dfid)) | 
|  | 604 | return PTR_ERR(dfid); | 
|  | 605 |  | 
|  | 606 | oldfid = v9fs_fid_lookup(old_dentry); | 
|  | 607 | if (IS_ERR(oldfid)) | 
|  | 608 | return PTR_ERR(oldfid); | 
|  | 609 |  | 
|  | 610 | name = (char *) dentry->d_name.name; | 
|  | 611 |  | 
|  | 612 | err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name); | 
|  | 613 |  | 
|  | 614 | if (err < 0) { | 
|  | 615 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_link failed %d\n", err); | 
|  | 616 | return err; | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | 
|  | 620 | /* Get the latest stat info from server. */ | 
|  | 621 | struct p9_fid *fid; | 
|  | 622 | struct p9_stat_dotl *st; | 
|  | 623 |  | 
|  | 624 | fid = v9fs_fid_lookup(old_dentry); | 
|  | 625 | if (IS_ERR(fid)) | 
|  | 626 | return PTR_ERR(fid); | 
|  | 627 |  | 
|  | 628 | st = p9_client_getattr_dotl(fid, P9_STATS_BASIC); | 
|  | 629 | if (IS_ERR(st)) | 
|  | 630 | return PTR_ERR(st); | 
|  | 631 |  | 
|  | 632 | v9fs_stat2inode_dotl(st, old_dentry->d_inode); | 
|  | 633 |  | 
|  | 634 | kfree(st); | 
|  | 635 | } else { | 
|  | 636 | /* Caching disabled. No need to get upto date stat info. | 
|  | 637 | * This dentry will be released immediately. So, just hold the | 
|  | 638 | * inode | 
|  | 639 | */ | 
|  | 640 | ihold(old_dentry->d_inode); | 
|  | 641 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 642 | d_instantiate(dentry, old_dentry->d_inode); | 
|  | 643 |  | 
|  | 644 | return err; | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | /** | 
|  | 648 | * v9fs_vfs_mknod_dotl - create a special file | 
|  | 649 | * @dir: inode destination for new link | 
|  | 650 | * @dentry: dentry for file | 
|  | 651 | * @mode: mode for creation | 
|  | 652 | * @rdev: device associated with special file | 
|  | 653 | * | 
|  | 654 | */ | 
|  | 655 | static int | 
|  | 656 | v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, int omode, | 
|  | 657 | dev_t rdev) | 
|  | 658 | { | 
|  | 659 | int err; | 
|  | 660 | char *name; | 
|  | 661 | mode_t mode; | 
|  | 662 | struct v9fs_session_info *v9ses; | 
|  | 663 | struct p9_fid *fid = NULL, *dfid = NULL; | 
|  | 664 | struct inode *inode; | 
|  | 665 | gid_t gid; | 
|  | 666 | struct p9_qid qid; | 
|  | 667 | struct dentry *dir_dentry; | 
|  | 668 | struct posix_acl *dacl = NULL, *pacl = NULL; | 
|  | 669 |  | 
|  | 670 | P9_DPRINTK(P9_DEBUG_VFS, | 
|  | 671 | " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino, | 
|  | 672 | dentry->d_name.name, omode, MAJOR(rdev), MINOR(rdev)); | 
|  | 673 |  | 
|  | 674 | if (!new_valid_dev(rdev)) | 
|  | 675 | return -EINVAL; | 
|  | 676 |  | 
|  | 677 | v9ses = v9fs_inode2v9ses(dir); | 
|  | 678 | dir_dentry = v9fs_dentry_from_dir_inode(dir); | 
|  | 679 | dfid = v9fs_fid_lookup(dir_dentry); | 
|  | 680 | if (IS_ERR(dfid)) { | 
|  | 681 | err = PTR_ERR(dfid); | 
|  | 682 | P9_DPRINTK(P9_DEBUG_VFS, "fid lookup failed %d\n", err); | 
|  | 683 | dfid = NULL; | 
|  | 684 | goto error; | 
|  | 685 | } | 
|  | 686 |  | 
|  | 687 | gid = v9fs_get_fsgid_for_create(dir); | 
|  | 688 | mode = omode; | 
|  | 689 | /* Update mode based on ACL value */ | 
|  | 690 | err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); | 
|  | 691 | if (err) { | 
|  | 692 | P9_DPRINTK(P9_DEBUG_VFS, | 
|  | 693 | "Failed to get acl values in mknod %d\n", err); | 
|  | 694 | goto error; | 
|  | 695 | } | 
|  | 696 | name = (char *) dentry->d_name.name; | 
|  | 697 |  | 
|  | 698 | err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid); | 
|  | 699 | if (err < 0) | 
|  | 700 | goto error; | 
|  | 701 |  | 
|  | 702 | /* instantiate inode and assign the unopened fid to the dentry */ | 
|  | 703 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) { | 
|  | 704 | fid = p9_client_walk(dfid, 1, &name, 1); | 
|  | 705 | if (IS_ERR(fid)) { | 
|  | 706 | err = PTR_ERR(fid); | 
|  | 707 | P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", | 
|  | 708 | err); | 
|  | 709 | fid = NULL; | 
|  | 710 | goto error; | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb); | 
|  | 714 | if (IS_ERR(inode)) { | 
|  | 715 | err = PTR_ERR(inode); | 
|  | 716 | P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", | 
|  | 717 | err); | 
|  | 718 | goto error; | 
|  | 719 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 720 | d_instantiate(dentry, inode); | 
|  | 721 | err = v9fs_fid_add(dentry, fid); | 
|  | 722 | if (err < 0) | 
|  | 723 | goto error; | 
|  | 724 | fid = NULL; | 
|  | 725 | } else { | 
|  | 726 | /* | 
|  | 727 | * Not in cached mode. No need to populate inode with stat. | 
|  | 728 | * socket syscall returns a fd, so we need instantiate | 
|  | 729 | */ | 
|  | 730 | inode = v9fs_get_inode(dir->i_sb, mode); | 
|  | 731 | if (IS_ERR(inode)) { | 
|  | 732 | err = PTR_ERR(inode); | 
|  | 733 | goto error; | 
|  | 734 | } | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 735 | d_instantiate(dentry, inode); | 
|  | 736 | } | 
|  | 737 | /* Now set the ACL based on the default value */ | 
|  | 738 | v9fs_set_create_acl(dentry, dacl, pacl); | 
|  | 739 | error: | 
|  | 740 | if (fid) | 
|  | 741 | p9_client_clunk(fid); | 
|  | 742 | return err; | 
|  | 743 | } | 
|  | 744 |  | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 745 | /** | 
|  | 746 | * v9fs_vfs_follow_link_dotl - follow a symlink path | 
|  | 747 | * @dentry: dentry for symlink | 
|  | 748 | * @nd: nameidata | 
|  | 749 | * | 
|  | 750 | */ | 
|  | 751 |  | 
|  | 752 | static void * | 
|  | 753 | v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd) | 
|  | 754 | { | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 755 | int retval; | 
|  | 756 | struct p9_fid *fid; | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 757 | char *link = __getname(); | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 758 | char *target; | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 759 |  | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 760 | P9_DPRINTK(P9_DEBUG_VFS, "%s\n", dentry->d_name.name); | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 761 |  | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 762 | if (!link) { | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 763 | link = ERR_PTR(-ENOMEM); | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 764 | goto ndset; | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 765 | } | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 766 | fid = v9fs_fid_lookup(dentry); | 
|  | 767 | if (IS_ERR(fid)) { | 
|  | 768 | __putname(link); | 
|  | 769 | link = ERR_PTR(PTR_ERR(fid)); | 
|  | 770 | goto ndset; | 
|  | 771 | } | 
|  | 772 | retval = p9_client_readlink(fid, &target); | 
|  | 773 | if (!retval) { | 
|  | 774 | strcpy(link, target); | 
|  | 775 | kfree(target); | 
|  | 776 | goto ndset; | 
|  | 777 | } | 
|  | 778 | __putname(link); | 
|  | 779 | link = ERR_PTR(retval); | 
|  | 780 | ndset: | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 781 | nd_set_link(nd, link); | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 782 | return NULL; | 
|  | 783 | } | 
|  | 784 |  | 
|  | 785 | const struct inode_operations v9fs_dir_inode_operations_dotl = { | 
|  | 786 | .create = v9fs_vfs_create_dotl, | 
|  | 787 | .lookup = v9fs_vfs_lookup, | 
|  | 788 | .link = v9fs_vfs_link_dotl, | 
|  | 789 | .symlink = v9fs_vfs_symlink_dotl, | 
|  | 790 | .unlink = v9fs_vfs_unlink, | 
|  | 791 | .mkdir = v9fs_vfs_mkdir_dotl, | 
|  | 792 | .rmdir = v9fs_vfs_rmdir, | 
|  | 793 | .mknod = v9fs_vfs_mknod_dotl, | 
|  | 794 | .rename = v9fs_vfs_rename, | 
|  | 795 | .getattr = v9fs_vfs_getattr_dotl, | 
|  | 796 | .setattr = v9fs_vfs_setattr_dotl, | 
|  | 797 | .setxattr = generic_setxattr, | 
|  | 798 | .getxattr = generic_getxattr, | 
|  | 799 | .removexattr = generic_removexattr, | 
|  | 800 | .listxattr = v9fs_listxattr, | 
|  | 801 | .check_acl = v9fs_check_acl, | 
|  | 802 | }; | 
|  | 803 |  | 
|  | 804 | const struct inode_operations v9fs_file_inode_operations_dotl = { | 
|  | 805 | .getattr = v9fs_vfs_getattr_dotl, | 
|  | 806 | .setattr = v9fs_vfs_setattr_dotl, | 
|  | 807 | .setxattr = generic_setxattr, | 
|  | 808 | .getxattr = generic_getxattr, | 
|  | 809 | .removexattr = generic_removexattr, | 
|  | 810 | .listxattr = v9fs_listxattr, | 
|  | 811 | .check_acl = v9fs_check_acl, | 
|  | 812 | }; | 
|  | 813 |  | 
|  | 814 | const struct inode_operations v9fs_symlink_inode_operations_dotl = { | 
| M. Mohan Kumar | 31b6cea | 2011-01-08 07:28:46 +0530 | [diff] [blame] | 815 | .readlink = generic_readlink, | 
| Aneesh Kumar K.V | 53c06f4 | 2011-01-10 13:51:47 -0600 | [diff] [blame] | 816 | .follow_link = v9fs_vfs_follow_link_dotl, | 
|  | 817 | .put_link = v9fs_vfs_put_link, | 
|  | 818 | .getattr = v9fs_vfs_getattr_dotl, | 
|  | 819 | .setattr = v9fs_vfs_setattr_dotl, | 
|  | 820 | .setxattr = generic_setxattr, | 
|  | 821 | .getxattr = generic_getxattr, | 
|  | 822 | .removexattr = generic_removexattr, | 
|  | 823 | .listxattr = v9fs_listxattr, | 
|  | 824 | }; |