vfs_inode_dotl.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 *  linux/fs/9p/vfs_inode_dotl.c
 *
 * This file contains vfs inode ops for the 9P2000.L protocol.
 *
 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/pagemap.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/inet.h>
#include <linux/namei.h>
#include <linux/idr.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/xattr.h>
#include <linux/posix_acl.h>
#include <net/9p/9p.h>
#include <net/9p/client.h>

#include "v9fs.h"
#include "v9fs_vfs.h"
#include "fid.h"
#include "cache.h"
#include "xattr.h"
#include "acl.h"

static int
A
Al Viro 已提交
51
v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
52 53 54 55 56 57 58 59
		    dev_t rdev);

/**
 * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
 * new file system object. This checks the S_ISGID to determine the owning
 * group of the new file system object.
 */

60
static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
61 62 63 64 65 66 67 68 69 70
{
	BUG_ON(dir_inode == NULL);

	if (dir_inode->i_mode & S_ISGID) {
		/* set_gid bit is set.*/
		return dir_inode->i_gid;
	}
	return current_fsgid();
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static int v9fs_test_inode_dotl(struct inode *inode, void *data)
{
	struct v9fs_inode *v9inode = V9FS_I(inode);
	struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;

	/* don't match inode of different type */
	if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
		return 0;

	if (inode->i_generation != st->st_gen)
		return 0;

	/* compare qid details */
	if (memcmp(&v9inode->qid.version,
		   &st->qid.version, sizeof(v9inode->qid.version)))
		return 0;

	if (v9inode->qid.type != st->qid.type)
		return 0;
90 91 92

	if (v9inode->qid.path != st->qid.path)
		return 0;
93 94 95
	return 1;
}

96 97 98 99 100 101
/* Always get a new inode */
static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
{
	return 0;
}

102 103 104 105 106 107 108 109 110 111
static int v9fs_set_inode_dotl(struct inode *inode,  void *data)
{
	struct v9fs_inode *v9inode = V9FS_I(inode);
	struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;

	memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
	inode->i_generation = st->st_gen;
	return 0;
}

A
Aneesh Kumar K.V 已提交
112 113 114
static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
					struct p9_qid *qid,
					struct p9_fid *fid,
115 116
					struct p9_stat_dotl *st,
					int new)
A
Aneesh Kumar K.V 已提交
117 118 119 120 121
{
	int retval;
	unsigned long i_ino;
	struct inode *inode;
	struct v9fs_session_info *v9ses = sb->s_fs_info;
122 123 124 125 126 127
	int (*test)(struct inode *, void *);

	if (new)
		test = v9fs_test_new_inode_dotl;
	else
		test = v9fs_test_inode_dotl;
A
Aneesh Kumar K.V 已提交
128 129

	i_ino = v9fs_qid2ino(qid);
130
	inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
A
Aneesh Kumar K.V 已提交
131 132 133 134 135 136 137 138 139
	if (!inode)
		return ERR_PTR(-ENOMEM);
	if (!(inode->i_state & I_NEW))
		return inode;
	/*
	 * initialize the inode with the stat info
	 * FIXME!! we may need support for stale inodes
	 * later.
	 */
140
	inode->i_ino = i_ino;
141 142
	retval = v9fs_init_inode(v9ses, inode,
				 st->st_mode, new_decode_dev(st->st_rdev));
A
Aneesh Kumar K.V 已提交
143 144 145 146 147 148 149 150 151 152 153 154
	if (retval)
		goto error;

	v9fs_stat2inode_dotl(st, inode);
	v9fs_cache_inode_get_cookie(inode);
	retval = v9fs_get_acl(inode, fid);
	if (retval)
		goto error;

	unlock_new_inode(inode);
	return inode;
error:
155
	iget_failed(inode);
A
Aneesh Kumar K.V 已提交
156 157 158 159
	return ERR_PTR(retval);

}

160
struct inode *
A
Aneesh Kumar K.V 已提交
161
v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
162
			 struct super_block *sb, int new)
163 164
{
	struct p9_stat_dotl *st;
A
Aneesh Kumar K.V 已提交
165
	struct inode *inode = NULL;
166

167
	st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
168 169 170
	if (IS_ERR(st))
		return ERR_CAST(st);

171
	inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
172
	kfree(st);
A
Aneesh Kumar K.V 已提交
173
	return inode;
174 175
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
struct dotl_openflag_map {
	int open_flag;
	int dotl_flag;
};

static int v9fs_mapped_dotl_flags(int flags)
{
	int i;
	int rflags = 0;
	struct dotl_openflag_map dotl_oflag_map[] = {
		{ O_CREAT,	P9_DOTL_CREATE },
		{ O_EXCL,	P9_DOTL_EXCL },
		{ O_NOCTTY,	P9_DOTL_NOCTTY },
		{ O_APPEND,	P9_DOTL_APPEND },
		{ O_NONBLOCK,	P9_DOTL_NONBLOCK },
		{ O_DSYNC,	P9_DOTL_DSYNC },
		{ FASYNC,	P9_DOTL_FASYNC },
		{ O_DIRECT,	P9_DOTL_DIRECT },
		{ O_LARGEFILE,	P9_DOTL_LARGEFILE },
		{ O_DIRECTORY,	P9_DOTL_DIRECTORY },
		{ O_NOFOLLOW,	P9_DOTL_NOFOLLOW },
		{ O_NOATIME,	P9_DOTL_NOATIME },
		{ O_CLOEXEC,	P9_DOTL_CLOEXEC },
		{ O_SYNC,	P9_DOTL_SYNC},
	};
	for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
		if (flags & dotl_oflag_map[i].open_flag)
			rflags |= dotl_oflag_map[i].dotl_flag;
	}
	return rflags;
}

/**
 * v9fs_open_to_dotl_flags- convert Linux specific open flags to
 * plan 9 open flag.
 * @flags: flags to convert
 */
int v9fs_open_to_dotl_flags(int flags)
{
	int rflags = 0;

	/*
	 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
	 * and P9_DOTL_NOACCESS
	 */
	rflags |= flags & O_ACCMODE;
	rflags |= v9fs_mapped_dotl_flags(flags);

	return rflags;
}

227 228 229 230
/**
 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
 * @dir: directory inode that is being created
 * @dentry:  dentry that is being deleted
F
Fabian Frederick 已提交
231
 * @omode: create permissions
232 233 234 235
 *
 */

static int
A
Al Viro 已提交
236
v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
A
Al Viro 已提交
237
		bool excl)
238 239 240 241
{
	return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
}

A
Al Viro 已提交
242
static int
243
v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
244
			  struct file *file, unsigned flags, umode_t omode)
245 246
{
	int err = 0;
247
	kgid_t gid;
A
Al Viro 已提交
248
	umode_t mode;
A
Al Viro 已提交
249
	const unsigned char *name = NULL;
250 251
	struct p9_qid qid;
	struct inode *inode;
252 253 254 255
	struct p9_fid *fid = NULL;
	struct v9fs_inode *v9inode;
	struct p9_fid *dfid, *ofid, *inode_fid;
	struct v9fs_session_info *v9ses;
256
	struct posix_acl *pacl = NULL, *dacl = NULL;
257
	struct dentry *res = NULL;
258

259
	if (d_in_lookup(dentry)) {
A
Al Viro 已提交
260
		res = v9fs_vfs_lookup(dir, dentry, 0);
261
		if (IS_ERR(res))
A
Al Viro 已提交
262
			return PTR_ERR(res);
263 264 265 266 267 268

		if (res)
			dentry = res;
	}

	/* Only creates */
269
	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
M
M. Mohan Kumar 已提交
270
		return	finish_no_open(file, res);
271

272 273
	v9ses = v9fs_inode2v9ses(dir);

A
Al Viro 已提交
274
	name = dentry->d_name.name;
275
	p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%hx\n",
276
		 name, flags, omode);
277

A
Al Viro 已提交
278
	dfid = v9fs_parent_fid(dentry);
279 280
	if (IS_ERR(dfid)) {
		err = PTR_ERR(dfid);
281
		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
A
Al Viro 已提交
282
		goto out;
283 284 285
	}

	/* clone a fid to use for creation */
A
Al Viro 已提交
286
	ofid = clone_fid(dfid);
287 288
	if (IS_ERR(ofid)) {
		err = PTR_ERR(ofid);
289
		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
A
Al Viro 已提交
290
		goto out;
291 292 293 294 295 296 297 298
	}

	gid = v9fs_get_fsgid_for_create(dir);

	mode = omode;
	/* Update mode based on ACL value */
	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
	if (err) {
299 300
		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
			 err);
301 302
		goto error;
	}
303 304
	err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
				    mode, gid, &qid);
305
	if (err < 0) {
306 307
		p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
			 err);
308 309
		goto error;
	}
310
	v9fs_invalidate_inode_attr(dir);
311

312 313 314 315
	/* instantiate inode and assign the unopened fid to the dentry */
	fid = p9_client_walk(dfid, 1, &name, 1);
	if (IS_ERR(fid)) {
		err = PTR_ERR(fid);
316
		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
317
		fid = NULL;
318
		goto error;
319
	}
320
	inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
321 322
	if (IS_ERR(inode)) {
		err = PTR_ERR(inode);
323
		p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
324 325
		goto error;
	}
326 327 328
	/* Now set the ACL based on the default value */
	v9fs_set_create_acl(inode, fid, dacl, pacl);

A
Al Viro 已提交
329
	v9fs_fid_add(dentry, fid);
330
	d_instantiate(dentry, inode);
331

332
	v9inode = V9FS_I(inode);
333
	mutex_lock(&v9inode->v_mutex);
334 335
	if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
	    !v9inode->writeback_fid &&
336
	    ((flags & O_ACCMODE) != O_RDONLY)) {
337
		/*
338
		 * clone a fid and add it to writeback_fid
339 340 341 342 343 344 345 346
		 * we do it during open time instead of
		 * page dirty time via write_begin/page_mkwrite
		 * because we want write after unlink usecase
		 * to work.
		 */
		inode_fid = v9fs_writeback_fid(dentry);
		if (IS_ERR(inode_fid)) {
			err = PTR_ERR(inode_fid);
347
			mutex_unlock(&v9inode->v_mutex);
348
			goto err_clunk_old_fid;
349
		}
350
		v9inode->writeback_fid = (void *) inode_fid;
351
	}
352
	mutex_unlock(&v9inode->v_mutex);
353
	/* Since we are opening a file, assign the open fid to the file */
354
	err = finish_open(file, dentry, generic_file_open);
A
Al Viro 已提交
355
	if (err)
356
		goto err_clunk_old_fid;
A
Al Viro 已提交
357
	file->private_data = ofid;
358
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
A
Al Viro 已提交
359
		v9fs_cache_inode_set_cookie(inode, file);
360
	file->f_mode |= FMODE_CREATED;
361
out:
362
	v9fs_put_acl(dacl, pacl);
363
	dput(res);
A
Al Viro 已提交
364
	return err;
365 366 367 368

error:
	if (fid)
		p9_client_clunk(fid);
369 370 371
err_clunk_old_fid:
	if (ofid)
		p9_client_clunk(ofid);
372
	goto out;
373 374 375 376 377 378
}

/**
 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
 * @dir:  inode that is being unlinked
 * @dentry: dentry that is being unlinked
F
Fabian Frederick 已提交
379
 * @omode: mode for new directory
380 381 382 383
 *
 */

static int v9fs_vfs_mkdir_dotl(struct inode *dir,
384
			       struct dentry *dentry, umode_t omode)
385 386 387 388
{
	int err;
	struct v9fs_session_info *v9ses;
	struct p9_fid *fid = NULL, *dfid = NULL;
389
	kgid_t gid;
A
Al Viro 已提交
390
	const unsigned char *name;
A
Al Viro 已提交
391
	umode_t mode;
392 393 394 395
	struct inode *inode;
	struct p9_qid qid;
	struct posix_acl *dacl = NULL, *pacl = NULL;

A
Al Viro 已提交
396
	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
397 398 399 400 401 402 403
	err = 0;
	v9ses = v9fs_inode2v9ses(dir);

	omode |= S_IFDIR;
	if (dir->i_mode & S_ISGID)
		omode |= S_ISGID;

A
Al Viro 已提交
404
	dfid = v9fs_parent_fid(dentry);
405 406
	if (IS_ERR(dfid)) {
		err = PTR_ERR(dfid);
407
		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
408 409 410 411 412 413 414 415 416
		dfid = NULL;
		goto error;
	}

	gid = v9fs_get_fsgid_for_create(dir);
	mode = omode;
	/* Update mode based on ACL value */
	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
	if (err) {
417 418
		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
			 err);
419 420
		goto error;
	}
A
Al Viro 已提交
421
	name = dentry->d_name.name;
422 423 424 425
	err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
	if (err < 0)
		goto error;

426 427 428 429 430 431 432 433 434
	fid = p9_client_walk(dfid, 1, &name, 1);
	if (IS_ERR(fid)) {
		err = PTR_ERR(fid);
		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
			 err);
		fid = NULL;
		goto error;
	}

435 436
	/* instantiate inode and assign the unopened fid to the dentry */
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
437
		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
438 439
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
440 441
			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
				 err);
442 443
			goto error;
		}
A
Al Viro 已提交
444
		v9fs_fid_add(dentry, fid);
445
		v9fs_set_create_acl(inode, fid, dacl, pacl);
446
		d_instantiate(dentry, inode);
447
		fid = NULL;
A
Al Viro 已提交
448
		err = 0;
449 450 451 452 453 454
	} else {
		/*
		 * Not in cached mode. No need to populate
		 * inode with stat. We need to get an inode
		 * so that we can set the acl with dentry
		 */
455
		inode = v9fs_get_inode(dir->i_sb, mode, 0);
456 457 458 459
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
			goto error;
		}
460
		v9fs_set_create_acl(inode, fid, dacl, pacl);
461 462
		d_instantiate(dentry, inode);
	}
463
	inc_nlink(dir);
464
	v9fs_invalidate_inode_attr(dir);
465 466 467
error:
	if (fid)
		p9_client_clunk(fid);
468
	v9fs_put_acl(dacl, pacl);
469 470 471 472
	return err;
}

static int
473 474
v9fs_vfs_getattr_dotl(const struct path *path, struct kstat *stat,
		 u32 request_mask, unsigned int flags)
475
{
476
	struct dentry *dentry = path->dentry;
477 478 479 480
	struct v9fs_session_info *v9ses;
	struct p9_fid *fid;
	struct p9_stat_dotl *st;

481
	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
A
Aneesh Kumar K.V 已提交
482
	v9ses = v9fs_dentry2v9ses(dentry);
483
	if (!d_really_is_negative(dentry) || v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
484
		generic_fillattr(d_inode(dentry), stat);
485 486
		return 0;
	}
487 488 489 490 491 492 493 494 495 496 497 498
	fid = v9fs_fid_lookup(dentry);
	if (IS_ERR(fid))
		return PTR_ERR(fid);

	/* Ask for all the fields in stat structure. Server will return
	 * whatever it supports
	 */

	st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
	if (IS_ERR(st))
		return PTR_ERR(st);

499 500
	v9fs_stat2inode_dotl(st, d_inode(dentry));
	generic_fillattr(d_inode(dentry), stat);
501 502 503 504 505 506 507
	/* Change block size to what the server returned */
	stat->blksize = st->st_blksize;

	kfree(st);
	return 0;
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/*
 * Attribute flags.
 */
#define P9_ATTR_MODE		(1 << 0)
#define P9_ATTR_UID		(1 << 1)
#define P9_ATTR_GID		(1 << 2)
#define P9_ATTR_SIZE		(1 << 3)
#define P9_ATTR_ATIME		(1 << 4)
#define P9_ATTR_MTIME		(1 << 5)
#define P9_ATTR_CTIME		(1 << 6)
#define P9_ATTR_ATIME_SET	(1 << 7)
#define P9_ATTR_MTIME_SET	(1 << 8)

struct dotl_iattr_map {
	int iattr_valid;
	int p9_iattr_valid;
};

static int v9fs_mapped_iattr_valid(int iattr_valid)
{
	int i;
	int p9_iattr_valid = 0;
	struct dotl_iattr_map dotl_iattr_map[] = {
		{ ATTR_MODE,		P9_ATTR_MODE },
		{ ATTR_UID,		P9_ATTR_UID },
		{ ATTR_GID,		P9_ATTR_GID },
		{ ATTR_SIZE,		P9_ATTR_SIZE },
		{ ATTR_ATIME,		P9_ATTR_ATIME },
		{ ATTR_MTIME,		P9_ATTR_MTIME },
		{ ATTR_CTIME,		P9_ATTR_CTIME },
		{ ATTR_ATIME_SET,	P9_ATTR_ATIME_SET },
		{ ATTR_MTIME_SET,	P9_ATTR_MTIME_SET },
	};
	for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
		if (iattr_valid & dotl_iattr_map[i].iattr_valid)
			p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
	}
	return p9_iattr_valid;
}

548 549 550 551 552 553 554 555 556 557 558 559
/**
 * v9fs_vfs_setattr_dotl - set file metadata
 * @dentry: file whose metadata to set
 * @iattr: metadata assignment structure
 *
 */

int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
{
	int retval;
	struct p9_fid *fid;
	struct p9_iattr_dotl p9attr;
560
	struct inode *inode = d_inode(dentry);
561

562
	p9_debug(P9_DEBUG_VFS, "\n");
563

564
	retval = setattr_prepare(dentry, iattr);
565 566 567
	if (retval)
		return retval;

568
	p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
569 570 571 572 573 574 575 576 577 578 579 580 581
	p9attr.mode = iattr->ia_mode;
	p9attr.uid = iattr->ia_uid;
	p9attr.gid = iattr->ia_gid;
	p9attr.size = iattr->ia_size;
	p9attr.atime_sec = iattr->ia_atime.tv_sec;
	p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
	p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
	p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;

	fid = v9fs_fid_lookup(dentry);
	if (IS_ERR(fid))
		return PTR_ERR(fid);

582
	/* Write all dirty data */
583 584
	if (S_ISREG(inode->i_mode))
		filemap_write_and_wait(inode->i_mapping);
585

586 587 588
	retval = p9_client_setattr(fid, &p9attr);
	if (retval < 0)
		return retval;
589

590
	if ((iattr->ia_valid & ATTR_SIZE) &&
591 592
	    iattr->ia_size != i_size_read(inode))
		truncate_setsize(inode, iattr->ia_size);
593

594 595 596
	v9fs_invalidate_inode_attr(inode);
	setattr_copy(inode, iattr);
	mark_inode_dirty(inode);
597 598
	if (iattr->ia_valid & ATTR_MODE) {
		/* We also want to update ACL when we update mode bits */
599
		retval = v9fs_acl_chmod(inode, fid);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		if (retval < 0)
			return retval;
	}
	return 0;
}

/**
 * v9fs_stat2inode_dotl - populate an inode structure with stat info
 * @stat: stat structure
 * @inode: inode to populate
 *
 */

void
v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
{
A
Al Viro 已提交
616
	umode_t mode;
617
	struct v9fs_inode *v9inode = V9FS_I(inode);
618 619 620 621 622 623 624 625 626 627

	if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
		inode->i_atime.tv_sec = stat->st_atime_sec;
		inode->i_atime.tv_nsec = stat->st_atime_nsec;
		inode->i_mtime.tv_sec = stat->st_mtime_sec;
		inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
		inode->i_ctime.tv_sec = stat->st_ctime_sec;
		inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
		inode->i_uid = stat->st_uid;
		inode->i_gid = stat->st_gid;
M
Miklos Szeredi 已提交
628
		set_nlink(inode, stat->st_nlink);
629

630 631 632
		mode = stat->st_mode & S_IALLUGO;
		mode |= inode->i_mode & ~S_IALLUGO;
		inode->i_mode = mode;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

		i_size_write(inode, stat->st_size);
		inode->i_blocks = stat->st_blocks;
	} else {
		if (stat->st_result_mask & P9_STATS_ATIME) {
			inode->i_atime.tv_sec = stat->st_atime_sec;
			inode->i_atime.tv_nsec = stat->st_atime_nsec;
		}
		if (stat->st_result_mask & P9_STATS_MTIME) {
			inode->i_mtime.tv_sec = stat->st_mtime_sec;
			inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
		}
		if (stat->st_result_mask & P9_STATS_CTIME) {
			inode->i_ctime.tv_sec = stat->st_ctime_sec;
			inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
		}
		if (stat->st_result_mask & P9_STATS_UID)
			inode->i_uid = stat->st_uid;
		if (stat->st_result_mask & P9_STATS_GID)
			inode->i_gid = stat->st_gid;
		if (stat->st_result_mask & P9_STATS_NLINK)
M
Miklos Szeredi 已提交
654
			set_nlink(inode, stat->st_nlink);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
		if (stat->st_result_mask & P9_STATS_MODE) {
			inode->i_mode = stat->st_mode;
			if ((S_ISBLK(inode->i_mode)) ||
						(S_ISCHR(inode->i_mode)))
				init_special_inode(inode, inode->i_mode,
								inode->i_rdev);
		}
		if (stat->st_result_mask & P9_STATS_RDEV)
			inode->i_rdev = new_decode_dev(stat->st_rdev);
		if (stat->st_result_mask & P9_STATS_SIZE)
			i_size_write(inode, stat->st_size);
		if (stat->st_result_mask & P9_STATS_BLOCKS)
			inode->i_blocks = stat->st_blocks;
	}
	if (stat->st_result_mask & P9_STATS_GEN)
670
		inode->i_generation = stat->st_gen;
671 672 673 674

	/* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
	 * because the inode structure does not have fields for them.
	 */
675
	v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
676 677 678 679 680 681 682
}

static int
v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
		const char *symname)
{
	int err;
683
	kgid_t gid;
A
Al Viro 已提交
684
	const unsigned char *name;
685 686 687 688 689
	struct p9_qid qid;
	struct inode *inode;
	struct p9_fid *dfid;
	struct p9_fid *fid = NULL;
	struct v9fs_session_info *v9ses;
690

A
Al Viro 已提交
691
	name = dentry->d_name.name;
692
	p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
693 694
	v9ses = v9fs_inode2v9ses(dir);

A
Al Viro 已提交
695
	dfid = v9fs_parent_fid(dentry);
696 697
	if (IS_ERR(dfid)) {
		err = PTR_ERR(dfid);
698
		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
699 700 701 702 703 704
		return err;
	}

	gid = v9fs_get_fsgid_for_create(dir);

	/* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
A
Al Viro 已提交
705
	err = p9_client_symlink(dfid, name, symname, gid, &qid);
706 707

	if (err < 0) {
708
		p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
709 710 711
		goto error;
	}

712
	v9fs_invalidate_inode_attr(dir);
713
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
714 715 716 717
		/* Now walk from the parent so we can get an unopened fid. */
		fid = p9_client_walk(dfid, 1, &name, 1);
		if (IS_ERR(fid)) {
			err = PTR_ERR(fid);
718 719
			p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
				 err);
720 721 722 723 724
			fid = NULL;
			goto error;
		}

		/* instantiate inode and assign the unopened fid to dentry */
725
		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
726 727
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
728 729
			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
				 err);
730 731
			goto error;
		}
A
Al Viro 已提交
732
		v9fs_fid_add(dentry, fid);
733
		d_instantiate(dentry, inode);
734
		fid = NULL;
A
Al Viro 已提交
735
		err = 0;
736 737
	} else {
		/* Not in cached mode. No need to populate inode with stat */
738
		inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
			goto error;
		}
		d_instantiate(dentry, inode);
	}

error:
	if (fid)
		p9_client_clunk(fid);

	return err;
}

/**
 * v9fs_vfs_link_dotl - create a hardlink for dotl
 * @old_dentry: dentry for file to link to
 * @dir: inode destination for new link
 * @dentry: dentry for link
 *
 */

static int
v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
		struct dentry *dentry)
{
	int err;
766 767
	struct p9_fid *dfid, *oldfid;
	struct v9fs_session_info *v9ses;
768

A
Al Viro 已提交
769 770
	p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
		 dir->i_ino, old_dentry, dentry);
771 772

	v9ses = v9fs_inode2v9ses(dir);
A
Al Viro 已提交
773
	dfid = v9fs_parent_fid(dentry);
774 775 776 777 778 779 780
	if (IS_ERR(dfid))
		return PTR_ERR(dfid);

	oldfid = v9fs_fid_lookup(old_dentry);
	if (IS_ERR(oldfid))
		return PTR_ERR(oldfid);

A
Al Viro 已提交
781
	err = p9_client_link(dfid, oldfid, dentry->d_name.name);
782 783

	if (err < 0) {
784
		p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
785 786 787
		return err;
	}

788
	v9fs_invalidate_inode_attr(dir);
789 790 791 792 793 794 795
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
		/* Get the latest stat info from server. */
		struct p9_fid *fid;
		fid = v9fs_fid_lookup(old_dentry);
		if (IS_ERR(fid))
			return PTR_ERR(fid);

796
		v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
797
	}
798 799
	ihold(d_inode(old_dentry));
	d_instantiate(dentry, d_inode(old_dentry));
800 801 802 803 804 805 806 807

	return err;
}

/**
 * v9fs_vfs_mknod_dotl - create a special file
 * @dir: inode destination for new link
 * @dentry: dentry for file
F
Fabian Frederick 已提交
808
 * @omode: mode for creation
809 810 811 812
 * @rdev: device associated with special file
 *
 */
static int
A
Al Viro 已提交
813
v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
814 815 816
		dev_t rdev)
{
	int err;
817
	kgid_t gid;
A
Al Viro 已提交
818
	const unsigned char *name;
A
Al Viro 已提交
819
	umode_t mode;
820 821 822 823 824 825
	struct v9fs_session_info *v9ses;
	struct p9_fid *fid = NULL, *dfid = NULL;
	struct inode *inode;
	struct p9_qid qid;
	struct posix_acl *dacl = NULL, *pacl = NULL;

A
Al Viro 已提交
826 827
	p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
		 dir->i_ino, dentry, omode,
828
		 MAJOR(rdev), MINOR(rdev));
829 830

	v9ses = v9fs_inode2v9ses(dir);
A
Al Viro 已提交
831
	dfid = v9fs_parent_fid(dentry);
832 833
	if (IS_ERR(dfid)) {
		err = PTR_ERR(dfid);
834
		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
835 836 837 838 839 840 841 842 843
		dfid = NULL;
		goto error;
	}

	gid = v9fs_get_fsgid_for_create(dir);
	mode = omode;
	/* Update mode based on ACL value */
	err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
	if (err) {
844 845
		p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
			 err);
846 847
		goto error;
	}
A
Al Viro 已提交
848
	name = dentry->d_name.name;
849 850 851 852 853

	err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
	if (err < 0)
		goto error;

854
	v9fs_invalidate_inode_attr(dir);
855 856 857 858 859 860 861 862 863
	fid = p9_client_walk(dfid, 1, &name, 1);
	if (IS_ERR(fid)) {
		err = PTR_ERR(fid);
		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
			 err);
		fid = NULL;
		goto error;
	}

864 865
	/* instantiate inode and assign the unopened fid to the dentry */
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
866
		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
867 868
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
869 870
			p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
				 err);
871 872
			goto error;
		}
873
		v9fs_set_create_acl(inode, fid, dacl, pacl);
A
Al Viro 已提交
874
		v9fs_fid_add(dentry, fid);
875
		d_instantiate(dentry, inode);
876
		fid = NULL;
A
Al Viro 已提交
877
		err = 0;
878 879 880 881 882
	} else {
		/*
		 * Not in cached mode. No need to populate inode with stat.
		 * socket syscall returns a fd, so we need instantiate
		 */
883
		inode = v9fs_get_inode(dir->i_sb, mode, rdev);
884 885 886 887
		if (IS_ERR(inode)) {
			err = PTR_ERR(inode);
			goto error;
		}
888
		v9fs_set_create_acl(inode, fid, dacl, pacl);
889 890 891 892 893
		d_instantiate(dentry, inode);
	}
error:
	if (fid)
		p9_client_clunk(fid);
894
	v9fs_put_acl(dacl, pacl);
895 896 897 898
	return err;
}

/**
899
 * v9fs_vfs_get_link_dotl - follow a symlink path
900
 * @dentry: dentry for symlink
901
 * @inode: inode for symlink
902
 * @done: destructor for return value
903 904
 */

905
static const char *
906
v9fs_vfs_get_link_dotl(struct dentry *dentry,
907 908
		       struct inode *inode,
		       struct delayed_call *done)
909
{
910
	struct p9_fid *fid;
M
M. Mohan Kumar 已提交
911
	char *target;
912
	int retval;
913

914 915 916
	if (!dentry)
		return ERR_PTR(-ECHILD);

A
Al Viro 已提交
917
	p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
918

919
	fid = v9fs_fid_lookup(dentry);
920 921
	if (IS_ERR(fid))
		return ERR_CAST(fid);
M
M. Mohan Kumar 已提交
922
	retval = p9_client_readlink(fid, &target);
923 924
	if (retval)
		return ERR_PTR(retval);
925 926
	set_delayed_call(done, kfree_link, target);
	return target;
927 928
}

929 930 931 932 933 934 935 936 937 938
int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
{
	loff_t i_size;
	struct p9_stat_dotl *st;
	struct v9fs_session_info *v9ses;

	v9ses = v9fs_inode2v9ses(inode);
	st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
	if (IS_ERR(st))
		return PTR_ERR(st);
939 940 941 942 943
	/*
	 * Don't update inode if the file type is different
	 */
	if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
		goto out;
944 945 946 947 948 949 950 951

	spin_lock(&inode->i_lock);
	/*
	 * We don't want to refresh inode->i_size,
	 * because we may have cached data
	 */
	i_size = inode->i_size;
	v9fs_stat2inode_dotl(st, inode);
952
	if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
953 954
		inode->i_size = i_size;
	spin_unlock(&inode->i_lock);
955
out:
956 957 958 959
	kfree(st);
	return 0;
}

960 961
const struct inode_operations v9fs_dir_inode_operations_dotl = {
	.create = v9fs_vfs_create_dotl,
962
	.atomic_open = v9fs_vfs_atomic_open_dotl,
963 964 965 966 967 968 969 970 971 972 973
	.lookup = v9fs_vfs_lookup,
	.link = v9fs_vfs_link_dotl,
	.symlink = v9fs_vfs_symlink_dotl,
	.unlink = v9fs_vfs_unlink,
	.mkdir = v9fs_vfs_mkdir_dotl,
	.rmdir = v9fs_vfs_rmdir,
	.mknod = v9fs_vfs_mknod_dotl,
	.rename = v9fs_vfs_rename,
	.getattr = v9fs_vfs_getattr_dotl,
	.setattr = v9fs_vfs_setattr_dotl,
	.listxattr = v9fs_listxattr,
974
	.get_acl = v9fs_iop_get_acl,
975 976 977 978 979 980
};

const struct inode_operations v9fs_file_inode_operations_dotl = {
	.getattr = v9fs_vfs_getattr_dotl,
	.setattr = v9fs_vfs_setattr_dotl,
	.listxattr = v9fs_listxattr,
981
	.get_acl = v9fs_iop_get_acl,
982 983 984
};

const struct inode_operations v9fs_symlink_inode_operations_dotl = {
985
	.get_link = v9fs_vfs_get_link_dotl,
986 987 988 989
	.getattr = v9fs_vfs_getattr_dotl,
	.setattr = v9fs_vfs_setattr_dotl,
	.listxattr = v9fs_listxattr,
};