Linux Audio

Check our new training course

Loading...
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This file contains vfs inode ops for the 9P2000 protocol.
   4 *
   5 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
   6 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
   7 */
   8
   9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10
  11#include <linux/module.h>
  12#include <linux/errno.h>
  13#include <linux/fs.h>
  14#include <linux/file.h>
  15#include <linux/pagemap.h>
  16#include <linux/stat.h>
  17#include <linux/string.h>
  18#include <linux/namei.h>
  19#include <linux/sched.h>
  20#include <linux/slab.h>
  21#include <linux/xattr.h>
  22#include <linux/posix_acl.h>
  23#include <net/9p/9p.h>
  24#include <net/9p/client.h>
  25
  26#include "v9fs.h"
  27#include "v9fs_vfs.h"
  28#include "fid.h"
  29#include "cache.h"
  30#include "xattr.h"
  31#include "acl.h"
  32
  33static const struct inode_operations v9fs_dir_inode_operations;
  34static const struct inode_operations v9fs_dir_inode_operations_dotu;
  35static const struct inode_operations v9fs_file_inode_operations;
  36static const struct inode_operations v9fs_symlink_inode_operations;
  37
  38/**
  39 * unixmode2p9mode - convert unix mode bits to plan 9
  40 * @v9ses: v9fs session information
  41 * @mode: mode to convert
  42 *
  43 */
  44
  45static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
  46{
  47	int res;
  48
  49	res = mode & 0777;
  50	if (S_ISDIR(mode))
  51		res |= P9_DMDIR;
  52	if (v9fs_proto_dotu(v9ses)) {
  53		if (v9ses->nodev == 0) {
  54			if (S_ISSOCK(mode))
  55				res |= P9_DMSOCKET;
  56			if (S_ISFIFO(mode))
  57				res |= P9_DMNAMEDPIPE;
  58			if (S_ISBLK(mode))
  59				res |= P9_DMDEVICE;
  60			if (S_ISCHR(mode))
  61				res |= P9_DMDEVICE;
  62		}
  63
  64		if ((mode & S_ISUID) == S_ISUID)
  65			res |= P9_DMSETUID;
  66		if ((mode & S_ISGID) == S_ISGID)
  67			res |= P9_DMSETGID;
  68		if ((mode & S_ISVTX) == S_ISVTX)
  69			res |= P9_DMSETVTX;
  70	}
  71	return res;
  72}
  73
  74/**
  75 * p9mode2perm- convert plan9 mode bits to unix permission bits
  76 * @v9ses: v9fs session information
  77 * @stat: p9_wstat from which mode need to be derived
  78 *
  79 */
  80static int p9mode2perm(struct v9fs_session_info *v9ses,
  81		       struct p9_wstat *stat)
  82{
  83	int res;
  84	int mode = stat->mode;
  85
  86	res = mode & 0777; /* S_IRWXUGO */
  87	if (v9fs_proto_dotu(v9ses)) {
  88		if ((mode & P9_DMSETUID) == P9_DMSETUID)
  89			res |= S_ISUID;
  90
  91		if ((mode & P9_DMSETGID) == P9_DMSETGID)
  92			res |= S_ISGID;
  93
  94		if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  95			res |= S_ISVTX;
  96	}
  97	return res;
  98}
  99
 100/**
 101 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
 102 * @v9ses: v9fs session information
 103 * @stat: p9_wstat from which mode need to be derived
 104 * @rdev: major number, minor number in case of device files.
 105 *
 106 */
 107static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
 108			       struct p9_wstat *stat, dev_t *rdev)
 109{
 110	int res, r;
 111	u32 mode = stat->mode;
 112
 113	*rdev = 0;
 114	res = p9mode2perm(v9ses, stat);
 115
 116	if ((mode & P9_DMDIR) == P9_DMDIR)
 117		res |= S_IFDIR;
 118	else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
 119		res |= S_IFLNK;
 120	else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
 121		 && (v9ses->nodev == 0))
 122		res |= S_IFSOCK;
 123	else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
 124		 && (v9ses->nodev == 0))
 125		res |= S_IFIFO;
 126	else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
 127		 && (v9ses->nodev == 0)) {
 128		char type = 0;
 129		int major = -1, minor = -1;
 130
 131		r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
 132		if (r != 3) {
 133			p9_debug(P9_DEBUG_ERROR,
 134				 "invalid device string, umode will be bogus: %s\n",
 135				 stat->extension);
 136			return res;
 137		}
 138		switch (type) {
 139		case 'c':
 140			res |= S_IFCHR;
 141			break;
 142		case 'b':
 143			res |= S_IFBLK;
 144			break;
 145		default:
 146			p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
 147				 type, stat->extension);
 148		}
 149		*rdev = MKDEV(major, minor);
 150	} else
 151		res |= S_IFREG;
 152
 153	return res;
 154}
 155
 156/**
 157 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
 158 * @uflags: flags to convert
 159 * @extended: if .u extensions are active
 160 */
 161
 162int v9fs_uflags2omode(int uflags, int extended)
 163{
 164	int ret;
 165
 166	switch (uflags&3) {
 167	default:
 168	case O_RDONLY:
 169		ret = P9_OREAD;
 170		break;
 171
 172	case O_WRONLY:
 173		ret = P9_OWRITE;
 174		break;
 175
 176	case O_RDWR:
 177		ret = P9_ORDWR;
 178		break;
 179	}
 180
 181	if (uflags & O_TRUNC)
 182		ret |= P9_OTRUNC;
 183
 184	if (extended) {
 185		if (uflags & O_EXCL)
 186			ret |= P9_OEXCL;
 187
 188		if (uflags & O_APPEND)
 189			ret |= P9_OAPPEND;
 190	}
 191
 192	return ret;
 193}
 194
 195/**
 196 * v9fs_blank_wstat - helper function to setup a 9P stat structure
 197 * @wstat: structure to initialize
 198 *
 199 */
 200
 201void
 202v9fs_blank_wstat(struct p9_wstat *wstat)
 203{
 204	wstat->type = ~0;
 205	wstat->dev = ~0;
 206	wstat->qid.type = ~0;
 207	wstat->qid.version = ~0;
 208	*((long long *)&wstat->qid.path) = ~0;
 209	wstat->mode = ~0;
 210	wstat->atime = ~0;
 211	wstat->mtime = ~0;
 212	wstat->length = ~0;
 213	wstat->name = NULL;
 214	wstat->uid = NULL;
 215	wstat->gid = NULL;
 216	wstat->muid = NULL;
 217	wstat->n_uid = INVALID_UID;
 218	wstat->n_gid = INVALID_GID;
 219	wstat->n_muid = INVALID_UID;
 220	wstat->extension = NULL;
 221}
 222
 223/**
 224 * v9fs_alloc_inode - helper function to allocate an inode
 225 * @sb: The superblock to allocate the inode from
 226 */
 227struct inode *v9fs_alloc_inode(struct super_block *sb)
 228{
 229	struct v9fs_inode *v9inode;
 230
 231	v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
 232	if (!v9inode)
 233		return NULL;
 234	v9inode->cache_validity = 0;
 235	mutex_init(&v9inode->v_mutex);
 236	return &v9inode->netfs.inode;
 237}
 238
 239/**
 240 * v9fs_free_inode - destroy an inode
 241 * @inode: The inode to be freed
 242 */
 243
 244void v9fs_free_inode(struct inode *inode)
 245{
 246	kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
 247}
 248
 249/*
 250 * Set parameters for the netfs library
 251 */
 252void v9fs_set_netfs_context(struct inode *inode)
 253{
 254	struct v9fs_inode *v9inode = V9FS_I(inode);
 255	netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true);
 256}
 257
 258int v9fs_init_inode(struct v9fs_session_info *v9ses,
 259		    struct inode *inode, struct p9_qid *qid, umode_t mode, dev_t rdev)
 260{
 261	int err = 0;
 262	struct v9fs_inode *v9inode = V9FS_I(inode);
 263
 264	memcpy(&v9inode->qid, qid, sizeof(struct p9_qid));
 265
 266	inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
 267	inode->i_blocks = 0;
 268	inode->i_rdev = rdev;
 269	simple_inode_init_ts(inode);
 270	inode->i_mapping->a_ops = &v9fs_addr_operations;
 271	inode->i_private = NULL;
 272
 273	switch (mode & S_IFMT) {
 274	case S_IFIFO:
 275	case S_IFBLK:
 276	case S_IFCHR:
 277	case S_IFSOCK:
 278		if (v9fs_proto_dotl(v9ses)) {
 279			inode->i_op = &v9fs_file_inode_operations_dotl;
 280		} else if (v9fs_proto_dotu(v9ses)) {
 281			inode->i_op = &v9fs_file_inode_operations;
 282		} else {
 283			p9_debug(P9_DEBUG_ERROR,
 284				 "special files without extended mode\n");
 285			err = -EINVAL;
 286			goto error;
 287		}
 288		init_special_inode(inode, inode->i_mode, inode->i_rdev);
 289		break;
 290	case S_IFREG:
 291		if (v9fs_proto_dotl(v9ses)) {
 292			inode->i_op = &v9fs_file_inode_operations_dotl;
 293			inode->i_fop = &v9fs_file_operations_dotl;
 294		} else {
 295			inode->i_op = &v9fs_file_inode_operations;
 296			inode->i_fop = &v9fs_file_operations;
 297		}
 298
 299		break;
 300	case S_IFLNK:
 301		if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
 302			p9_debug(P9_DEBUG_ERROR,
 303				 "extended modes used with legacy protocol\n");
 304			err = -EINVAL;
 305			goto error;
 306		}
 307
 308		if (v9fs_proto_dotl(v9ses))
 309			inode->i_op = &v9fs_symlink_inode_operations_dotl;
 310		else
 311			inode->i_op = &v9fs_symlink_inode_operations;
 312
 313		break;
 314	case S_IFDIR:
 315		inc_nlink(inode);
 316		if (v9fs_proto_dotl(v9ses))
 317			inode->i_op = &v9fs_dir_inode_operations_dotl;
 318		else if (v9fs_proto_dotu(v9ses))
 319			inode->i_op = &v9fs_dir_inode_operations_dotu;
 320		else
 321			inode->i_op = &v9fs_dir_inode_operations;
 322
 323		if (v9fs_proto_dotl(v9ses))
 324			inode->i_fop = &v9fs_dir_operations_dotl;
 325		else
 326			inode->i_fop = &v9fs_dir_operations;
 327
 328		break;
 329	default:
 330		p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
 331			 mode, mode & S_IFMT);
 332		err = -EINVAL;
 333		goto error;
 334	}
 335error:
 336	return err;
 337
 338}
 339
 340/**
 341 * v9fs_evict_inode - Remove an inode from the inode cache
 342 * @inode: inode to release
 343 *
 344 */
 345void v9fs_evict_inode(struct inode *inode)
 346{
 347	struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
 348	__le32 __maybe_unused version;
 349
 350	if (!is_bad_inode(inode)) {
 
 351		truncate_inode_pages_final(&inode->i_data);
 352
 353		version = cpu_to_le32(v9inode->qid.version);
 354		netfs_clear_inode_writeback(inode, &version);
 355
 356		clear_inode(inode);
 357		filemap_fdatawrite(&inode->i_data);
 358
 359#ifdef CONFIG_9P_FSCACHE
 360		if (v9fs_inode_cookie(v9inode))
 361			fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
 362#endif
 363	} else
 364		clear_inode(inode);
 365}
 366
 367struct inode *
 368v9fs_fid_iget(struct super_block *sb, struct p9_fid *fid, bool new)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 369{
 370	dev_t rdev;
 371	int retval;
 372	umode_t umode;
 373	struct inode *inode;
 374	struct p9_wstat *st;
 375	struct v9fs_session_info *v9ses = sb->s_fs_info;
 
 376
 377	inode = iget_locked(sb, QID2INO(&fid->qid));
 378	if (unlikely(!inode))
 379		return ERR_PTR(-ENOMEM);
 380	if (!(inode->i_state & I_NEW)) {
 381		if (!new) {
 382			goto done;
 383		} else {
 384			p9_debug(P9_DEBUG_VFS, "WARNING: Inode collision %ld\n",
 385						inode->i_ino);
 386			iput(inode);
 387			remove_inode_hash(inode);
 388			inode = iget_locked(sb, QID2INO(&fid->qid));
 389			WARN_ON(!(inode->i_state & I_NEW));
 390		}
 391	}
 392
 
 
 
 
 
 393	/*
 394	 * initialize the inode with the stat info
 395	 * FIXME!! we may need support for stale inodes
 396	 * later.
 397	 */
 398	st = p9_client_stat(fid);
 399	if (IS_ERR(st)) {
 400		retval = PTR_ERR(st);
 401		goto error;
 402	}
 403
 404	umode = p9mode2unixmode(v9ses, st, &rdev);
 405	retval = v9fs_init_inode(v9ses, inode, &fid->qid, umode, rdev);
 406	v9fs_stat2inode(st, inode, sb, 0);
 407	p9stat_free(st);
 408	kfree(st);
 409	if (retval)
 410		goto error;
 411
 
 412	v9fs_set_netfs_context(inode);
 413	v9fs_cache_inode_get_cookie(inode);
 414	unlock_new_inode(inode);
 415done:
 416	return inode;
 417error:
 418	iget_failed(inode);
 419	return ERR_PTR(retval);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 420}
 421
 422/**
 423 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
 424 * plan 9 AT flag.
 425 * @flags: flags to convert
 426 */
 427static int v9fs_at_to_dotl_flags(int flags)
 428{
 429	int rflags = 0;
 430
 431	if (flags & AT_REMOVEDIR)
 432		rflags |= P9_DOTL_AT_REMOVEDIR;
 433
 434	return rflags;
 435}
 436
 437/**
 438 * v9fs_dec_count - helper functon to drop i_nlink.
 439 *
 440 * If a directory had nlink <= 2 (including . and ..), then we should not drop
 441 * the link count, which indicates the underlying exported fs doesn't maintain
 442 * nlink accurately. e.g.
 443 * - overlayfs sets nlink to 1 for merged dir
 444 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
 445 *   than EXT4_LINK_MAX (65000) links.
 446 *
 447 * @inode: inode whose nlink is being dropped
 448 */
 449static void v9fs_dec_count(struct inode *inode)
 450{
 451	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) {
 452		if (inode->i_nlink) {
 453			drop_nlink(inode);
 454		} else {
 455			p9_debug(P9_DEBUG_VFS,
 456						"WARNING: unexpected i_nlink zero %d inode %ld\n",
 457						inode->i_nlink, inode->i_ino);
 458		}
 459	}
 460}
 461
 462/**
 463 * v9fs_remove - helper function to remove files and directories
 464 * @dir: directory inode that is being deleted
 465 * @dentry:  dentry that is being deleted
 466 * @flags: removing a directory
 467 *
 468 */
 469
 470static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
 471{
 472	struct inode *inode;
 473	int retval = -EOPNOTSUPP;
 474	struct p9_fid *v9fid, *dfid;
 475	struct v9fs_session_info *v9ses;
 476
 477	p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
 478		 dir, dentry, flags);
 479
 480	v9ses = v9fs_inode2v9ses(dir);
 481	inode = d_inode(dentry);
 482	dfid = v9fs_parent_fid(dentry);
 483	if (IS_ERR(dfid)) {
 484		retval = PTR_ERR(dfid);
 485		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
 486		return retval;
 487	}
 488	if (v9fs_proto_dotl(v9ses))
 489		retval = p9_client_unlinkat(dfid, dentry->d_name.name,
 490					    v9fs_at_to_dotl_flags(flags));
 491	p9_fid_put(dfid);
 492	if (retval == -EOPNOTSUPP) {
 493		/* Try the one based on path */
 494		v9fid = v9fs_fid_clone(dentry);
 495		if (IS_ERR(v9fid))
 496			return PTR_ERR(v9fid);
 497		retval = p9_client_remove(v9fid);
 498	}
 499	if (!retval) {
 500		/*
 501		 * directories on unlink should have zero
 502		 * link count
 503		 */
 504		if (flags & AT_REMOVEDIR) {
 505			clear_nlink(inode);
 506			v9fs_dec_count(dir);
 507		} else
 508			v9fs_dec_count(inode);
 509
 510		if (inode->i_nlink <= 0)	/* no more refs unhash it */
 511			remove_inode_hash(inode);
 512
 513		v9fs_invalidate_inode_attr(inode);
 514		v9fs_invalidate_inode_attr(dir);
 515
 516		/* invalidate all fids associated with dentry */
 517		/* NOTE: This will not include open fids */
 518		dentry->d_op->d_release(dentry);
 519	}
 520	return retval;
 521}
 522
 523/**
 524 * v9fs_create - Create a file
 525 * @v9ses: session information
 526 * @dir: directory that dentry is being created in
 527 * @dentry:  dentry that is being created
 528 * @extension: 9p2000.u extension string to support devices, etc.
 529 * @perm: create permissions
 530 * @mode: open mode
 531 *
 532 */
 533static struct p9_fid *
 534v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
 535		struct dentry *dentry, char *extension, u32 perm, u8 mode)
 536{
 537	int err;
 538	const unsigned char *name;
 539	struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
 540	struct inode *inode;
 541
 542	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
 543
 544	name = dentry->d_name.name;
 545	dfid = v9fs_parent_fid(dentry);
 546	if (IS_ERR(dfid)) {
 547		err = PTR_ERR(dfid);
 548		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
 549		return ERR_PTR(err);
 550	}
 551
 552	/* clone a fid to use for creation */
 553	ofid = clone_fid(dfid);
 554	if (IS_ERR(ofid)) {
 555		err = PTR_ERR(ofid);
 556		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
 557		goto error;
 558	}
 559
 560	err = p9_client_fcreate(ofid, name, perm, mode, extension);
 561	if (err < 0) {
 562		p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
 563		goto error;
 564	}
 565
 566	if (!(perm & P9_DMLINK)) {
 567		/* now walk from the parent so we can get unopened fid */
 568		fid = p9_client_walk(dfid, 1, &name, 1);
 569		if (IS_ERR(fid)) {
 570			err = PTR_ERR(fid);
 571			p9_debug(P9_DEBUG_VFS,
 572				   "p9_client_walk failed %d\n", err);
 573			goto error;
 574		}
 575		/*
 576		 * instantiate inode and assign the unopened fid to the dentry
 577		 */
 578		inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb, true);
 579		if (IS_ERR(inode)) {
 580			err = PTR_ERR(inode);
 581			p9_debug(P9_DEBUG_VFS,
 582				   "inode creation failed %d\n", err);
 583			goto error;
 584		}
 585		v9fs_fid_add(dentry, &fid);
 586		d_instantiate(dentry, inode);
 587	}
 588	p9_fid_put(dfid);
 589	return ofid;
 590error:
 591	p9_fid_put(dfid);
 592	p9_fid_put(ofid);
 593	p9_fid_put(fid);
 594	return ERR_PTR(err);
 595}
 596
 597/**
 598 * v9fs_vfs_create - VFS hook to create a regular file
 599 * @idmap: idmap of the mount
 600 * @dir: The parent directory
 601 * @dentry: The name of file to be created
 602 * @mode: The UNIX file mode to set
 603 * @excl: True if the file must not yet exist
 604 *
 605 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open().  This is only called
 606 * for mknod(2).
 607 *
 608 */
 609
 610static int
 611v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
 612		struct dentry *dentry, umode_t mode, bool excl)
 613{
 614	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
 615	u32 perm = unixmode2p9mode(v9ses, mode);
 616	struct p9_fid *fid;
 617
 618	/* P9_OEXCL? */
 619	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
 620	if (IS_ERR(fid))
 621		return PTR_ERR(fid);
 622
 623	v9fs_invalidate_inode_attr(dir);
 624	p9_fid_put(fid);
 625
 626	return 0;
 627}
 628
 629/**
 630 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
 631 * @idmap: idmap of the mount
 632 * @dir:  inode that is being unlinked
 633 * @dentry: dentry that is being unlinked
 634 * @mode: mode for new directory
 635 *
 636 */
 637
 638static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
 639			  struct dentry *dentry, umode_t mode)
 640{
 641	int err;
 642	u32 perm;
 643	struct p9_fid *fid;
 644	struct v9fs_session_info *v9ses;
 645
 646	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
 647	err = 0;
 648	v9ses = v9fs_inode2v9ses(dir);
 649	perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
 650	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
 651	if (IS_ERR(fid)) {
 652		err = PTR_ERR(fid);
 653		fid = NULL;
 654	} else {
 655		inc_nlink(dir);
 656		v9fs_invalidate_inode_attr(dir);
 657	}
 658
 659	if (fid)
 660		p9_fid_put(fid);
 661
 662	return err;
 663}
 664
 665/**
 666 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
 667 * @dir:  inode that is being walked from
 668 * @dentry: dentry that is being walked to?
 669 * @flags: lookup flags (unused)
 670 *
 671 */
 672
 673struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
 674				      unsigned int flags)
 675{
 676	struct dentry *res;
 677	struct v9fs_session_info *v9ses;
 678	struct p9_fid *dfid, *fid;
 679	struct inode *inode;
 680	const unsigned char *name;
 681
 682	p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
 683		 dir, dentry, dentry, flags);
 684
 685	if (dentry->d_name.len > NAME_MAX)
 686		return ERR_PTR(-ENAMETOOLONG);
 687
 688	v9ses = v9fs_inode2v9ses(dir);
 689	/* We can walk d_parent because we hold the dir->i_mutex */
 690	dfid = v9fs_parent_fid(dentry);
 691	if (IS_ERR(dfid))
 692		return ERR_CAST(dfid);
 693
 694	/*
 695	 * Make sure we don't use a wrong inode due to parallel
 696	 * unlink. For cached mode create calls request for new
 697	 * inode. But with cache disabled, lookup should do this.
 698	 */
 699	name = dentry->d_name.name;
 700	fid = p9_client_walk(dfid, 1, &name, 1);
 701	p9_fid_put(dfid);
 702	if (fid == ERR_PTR(-ENOENT))
 703		inode = NULL;
 704	else if (IS_ERR(fid))
 705		inode = ERR_CAST(fid);
 
 
 706	else
 707		inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb, false);
 708	/*
 709	 * If we had a rename on the server and a parallel lookup
 710	 * for the new name, then make sure we instantiate with
 711	 * the new name. ie look up for a/b, while on server somebody
 712	 * moved b under k and client parallely did a lookup for
 713	 * k/b.
 714	 */
 715	res = d_splice_alias(inode, dentry);
 716	if (!IS_ERR(fid)) {
 717		if (!res)
 718			v9fs_fid_add(dentry, &fid);
 719		else if (!IS_ERR(res))
 720			v9fs_fid_add(res, &fid);
 721		else
 722			p9_fid_put(fid);
 723	}
 724	return res;
 725}
 726
 727static int
 728v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
 729		     struct file *file, unsigned int flags, umode_t mode)
 730{
 731	int err;
 732	u32 perm;
 733	struct v9fs_inode __maybe_unused *v9inode;
 734	struct v9fs_session_info *v9ses;
 735	struct p9_fid *fid;
 736	struct dentry *res = NULL;
 737	struct inode *inode;
 738	int p9_omode;
 739
 740	if (d_in_lookup(dentry)) {
 741		res = v9fs_vfs_lookup(dir, dentry, 0);
 742		if (IS_ERR(res))
 743			return PTR_ERR(res);
 744
 745		if (res)
 746			dentry = res;
 747	}
 748
 749	/* Only creates */
 750	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
 751		return finish_no_open(file, res);
 752
 753	v9ses = v9fs_inode2v9ses(dir);
 754	perm = unixmode2p9mode(v9ses, mode);
 755	p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
 756
 757	if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
 758		p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
 759		p9_debug(P9_DEBUG_CACHE,
 760			"write-only file with writeback enabled, creating w/ O_RDWR\n");
 761	}
 762	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
 763	if (IS_ERR(fid)) {
 764		err = PTR_ERR(fid);
 765		goto error;
 766	}
 767
 768	v9fs_invalidate_inode_attr(dir);
 769	inode = d_inode(dentry);
 770	v9inode = V9FS_I(inode);
 771	err = finish_open(file, dentry, generic_file_open);
 772	if (err)
 773		goto error;
 774
 775	file->private_data = fid;
 776#ifdef CONFIG_9P_FSCACHE
 777	if (v9ses->cache & CACHE_FSCACHE)
 778		fscache_use_cookie(v9fs_inode_cookie(v9inode),
 779				   file->f_mode & FMODE_WRITE);
 780#endif
 781
 782	v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
 783	v9fs_open_fid_add(inode, &fid);
 784
 785	file->f_mode |= FMODE_CREATED;
 786out:
 787	dput(res);
 788	return err;
 789
 790error:
 791	p9_fid_put(fid);
 792	goto out;
 793}
 794
 795/**
 796 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
 797 * @i:  inode that is being unlinked
 798 * @d: dentry that is being unlinked
 799 *
 800 */
 801
 802int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
 803{
 804	return v9fs_remove(i, d, 0);
 805}
 806
 807/**
 808 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
 809 * @i:  inode that is being unlinked
 810 * @d: dentry that is being unlinked
 811 *
 812 */
 813
 814int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
 815{
 816	return v9fs_remove(i, d, AT_REMOVEDIR);
 817}
 818
 819/**
 820 * v9fs_vfs_rename - VFS hook to rename an inode
 821 * @idmap: The idmap of the mount
 822 * @old_dir:  old dir inode
 823 * @old_dentry: old dentry
 824 * @new_dir: new dir inode
 825 * @new_dentry: new dentry
 826 * @flags: RENAME_* flags
 827 *
 828 */
 829
 830int
 831v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 832		struct dentry *old_dentry, struct inode *new_dir,
 833		struct dentry *new_dentry, unsigned int flags)
 834{
 835	int retval;
 836	struct inode *old_inode;
 837	struct inode *new_inode;
 838	struct v9fs_session_info *v9ses;
 839	struct p9_fid *oldfid = NULL, *dfid = NULL;
 840	struct p9_fid *olddirfid = NULL;
 841	struct p9_fid *newdirfid = NULL;
 842	struct p9_wstat wstat;
 843
 844	if (flags)
 845		return -EINVAL;
 846
 847	p9_debug(P9_DEBUG_VFS, "\n");
 848	old_inode = d_inode(old_dentry);
 849	new_inode = d_inode(new_dentry);
 850	v9ses = v9fs_inode2v9ses(old_inode);
 851	oldfid = v9fs_fid_lookup(old_dentry);
 852	if (IS_ERR(oldfid))
 853		return PTR_ERR(oldfid);
 854
 855	dfid = v9fs_parent_fid(old_dentry);
 856	olddirfid = clone_fid(dfid);
 857	p9_fid_put(dfid);
 858	dfid = NULL;
 859
 860	if (IS_ERR(olddirfid)) {
 861		retval = PTR_ERR(olddirfid);
 862		goto error;
 863	}
 864
 865	dfid = v9fs_parent_fid(new_dentry);
 866	newdirfid = clone_fid(dfid);
 867	p9_fid_put(dfid);
 868	dfid = NULL;
 869
 870	if (IS_ERR(newdirfid)) {
 871		retval = PTR_ERR(newdirfid);
 872		goto error;
 873	}
 874
 875	down_write(&v9ses->rename_sem);
 876	if (v9fs_proto_dotl(v9ses)) {
 877		retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
 878					    newdirfid, new_dentry->d_name.name);
 879		if (retval == -EOPNOTSUPP)
 880			retval = p9_client_rename(oldfid, newdirfid,
 881						  new_dentry->d_name.name);
 882		if (retval != -EOPNOTSUPP)
 883			goto error_locked;
 884	}
 885	if (old_dentry->d_parent != new_dentry->d_parent) {
 886		/*
 887		 * 9P .u can only handle file rename in the same directory
 888		 */
 889
 890		p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
 891		retval = -EXDEV;
 892		goto error_locked;
 893	}
 894	v9fs_blank_wstat(&wstat);
 895	wstat.muid = v9ses->uname;
 896	wstat.name = new_dentry->d_name.name;
 897	retval = p9_client_wstat(oldfid, &wstat);
 898
 899error_locked:
 900	if (!retval) {
 901		if (new_inode) {
 902			if (S_ISDIR(new_inode->i_mode))
 903				clear_nlink(new_inode);
 904			else
 905				v9fs_dec_count(new_inode);
 906		}
 907		if (S_ISDIR(old_inode->i_mode)) {
 908			if (!new_inode)
 909				inc_nlink(new_dir);
 910			v9fs_dec_count(old_dir);
 911		}
 912		v9fs_invalidate_inode_attr(old_inode);
 913		v9fs_invalidate_inode_attr(old_dir);
 914		v9fs_invalidate_inode_attr(new_dir);
 915
 916		/* successful rename */
 917		d_move(old_dentry, new_dentry);
 918	}
 919	up_write(&v9ses->rename_sem);
 920
 921error:
 922	p9_fid_put(newdirfid);
 923	p9_fid_put(olddirfid);
 924	p9_fid_put(oldfid);
 925	return retval;
 926}
 927
 928/**
 929 * v9fs_vfs_getattr - retrieve file metadata
 930 * @idmap: idmap of the mount
 931 * @path: Object to query
 932 * @stat: metadata structure to populate
 933 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
 934 * @flags: AT_STATX_xxx setting
 935 *
 936 */
 937
 938static int
 939v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
 940		 struct kstat *stat, u32 request_mask, unsigned int flags)
 941{
 942	struct dentry *dentry = path->dentry;
 943	struct inode *inode = d_inode(dentry);
 944	struct v9fs_session_info *v9ses;
 945	struct p9_fid *fid;
 946	struct p9_wstat *st;
 947
 948	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
 949	v9ses = v9fs_dentry2v9ses(dentry);
 950	if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
 951		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
 952		return 0;
 953	} else if (v9ses->cache & CACHE_WRITEBACK) {
 954		if (S_ISREG(inode->i_mode)) {
 955			int retval = filemap_fdatawrite(inode->i_mapping);
 956
 957			if (retval)
 958				p9_debug(P9_DEBUG_ERROR,
 959				    "flushing writeback during getattr returned %d\n", retval);
 960		}
 961	}
 962	fid = v9fs_fid_lookup(dentry);
 963	if (IS_ERR(fid))
 964		return PTR_ERR(fid);
 965
 966	st = p9_client_stat(fid);
 967	p9_fid_put(fid);
 968	if (IS_ERR(st))
 969		return PTR_ERR(st);
 970
 971	v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
 972	generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
 973
 974	p9stat_free(st);
 975	kfree(st);
 976	return 0;
 977}
 978
 979/**
 980 * v9fs_vfs_setattr - set file metadata
 981 * @idmap: idmap of the mount
 982 * @dentry: file whose metadata to set
 983 * @iattr: metadata assignment structure
 984 *
 985 */
 986
 987static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
 988			    struct dentry *dentry, struct iattr *iattr)
 989{
 990	int retval, use_dentry = 0;
 991	struct inode *inode = d_inode(dentry);
 992	struct v9fs_session_info *v9ses;
 993	struct p9_fid *fid = NULL;
 994	struct p9_wstat wstat;
 995
 996	p9_debug(P9_DEBUG_VFS, "\n");
 997	retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
 998	if (retval)
 999		return retval;
1000
1001	v9ses = v9fs_dentry2v9ses(dentry);
1002	if (iattr->ia_valid & ATTR_FILE) {
1003		fid = iattr->ia_file->private_data;
1004		WARN_ON(!fid);
1005	}
1006	if (!fid) {
1007		fid = v9fs_fid_lookup(dentry);
1008		use_dentry = 1;
1009	}
1010	if (IS_ERR(fid))
1011		return PTR_ERR(fid);
1012
1013	v9fs_blank_wstat(&wstat);
1014	if (iattr->ia_valid & ATTR_MODE)
1015		wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1016
1017	if (iattr->ia_valid & ATTR_MTIME)
1018		wstat.mtime = iattr->ia_mtime.tv_sec;
1019
1020	if (iattr->ia_valid & ATTR_ATIME)
1021		wstat.atime = iattr->ia_atime.tv_sec;
1022
1023	if (iattr->ia_valid & ATTR_SIZE)
1024		wstat.length = iattr->ia_size;
1025
1026	if (v9fs_proto_dotu(v9ses)) {
1027		if (iattr->ia_valid & ATTR_UID)
1028			wstat.n_uid = iattr->ia_uid;
1029
1030		if (iattr->ia_valid & ATTR_GID)
1031			wstat.n_gid = iattr->ia_gid;
1032	}
1033
1034	/* Write all dirty data */
1035	if (d_is_reg(dentry)) {
1036		retval = filemap_fdatawrite(inode->i_mapping);
1037		if (retval)
1038			p9_debug(P9_DEBUG_ERROR,
1039			    "flushing writeback during setattr returned %d\n", retval);
1040	}
1041
1042	retval = p9_client_wstat(fid, &wstat);
1043
1044	if (use_dentry)
1045		p9_fid_put(fid);
1046
1047	if (retval < 0)
1048		return retval;
1049
1050	if ((iattr->ia_valid & ATTR_SIZE) &&
1051		 iattr->ia_size != i_size_read(inode)) {
1052		truncate_setsize(inode, iattr->ia_size);
1053		netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
1054
1055#ifdef CONFIG_9P_FSCACHE
1056		if (v9ses->cache & CACHE_FSCACHE) {
1057			struct v9fs_inode *v9inode = V9FS_I(inode);
1058
1059			fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1060		}
1061#endif
1062	}
1063
1064	v9fs_invalidate_inode_attr(inode);
1065
1066	setattr_copy(&nop_mnt_idmap, inode, iattr);
1067	mark_inode_dirty(inode);
1068	return 0;
1069}
1070
1071/**
1072 * v9fs_stat2inode - populate an inode structure with mistat info
1073 * @stat: Plan 9 metadata (mistat) structure
1074 * @inode: inode to populate
1075 * @sb: superblock of filesystem
1076 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1077 *
1078 */
1079
1080void
1081v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1082		 struct super_block *sb, unsigned int flags)
1083{
1084	umode_t mode;
1085	struct v9fs_session_info *v9ses = sb->s_fs_info;
1086	struct v9fs_inode *v9inode = V9FS_I(inode);
1087
1088	inode_set_atime(inode, stat->atime, 0);
1089	inode_set_mtime(inode, stat->mtime, 0);
1090	inode_set_ctime(inode, stat->mtime, 0);
1091
1092	inode->i_uid = v9ses->dfltuid;
1093	inode->i_gid = v9ses->dfltgid;
1094
1095	if (v9fs_proto_dotu(v9ses)) {
1096		inode->i_uid = stat->n_uid;
1097		inode->i_gid = stat->n_gid;
1098	}
1099	if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1100		if (v9fs_proto_dotu(v9ses)) {
1101			unsigned int i_nlink;
1102			/*
1103			 * Hadlink support got added later to the .u extension.
1104			 * So there can be a server out there that doesn't
1105			 * support this even with .u extension. That would
1106			 * just leave us with stat->extension being an empty
1107			 * string, though.
1108			 */
1109			/* HARDLINKCOUNT %u */
1110			if (sscanf(stat->extension,
1111				   " HARDLINKCOUNT %u", &i_nlink) == 1)
1112				set_nlink(inode, i_nlink);
1113		}
1114	}
1115	mode = p9mode2perm(v9ses, stat);
1116	mode |= inode->i_mode & ~S_IALLUGO;
1117	inode->i_mode = mode;
1118
1119	v9inode->netfs.remote_i_size = stat->length;
1120	if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1121		v9fs_i_size_write(inode, stat->length);
1122	/* not real number of blocks, but 512 byte ones ... */
1123	inode->i_blocks = (stat->length + 512 - 1) >> 9;
1124	v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1125}
1126
1127/**
1128 * v9fs_vfs_get_link - follow a symlink path
1129 * @dentry: dentry for symlink
1130 * @inode: inode for symlink
1131 * @done: delayed call for when we are done with the return value
1132 */
1133
1134static const char *v9fs_vfs_get_link(struct dentry *dentry,
1135				     struct inode *inode,
1136				     struct delayed_call *done)
1137{
1138	struct v9fs_session_info *v9ses;
1139	struct p9_fid *fid;
1140	struct p9_wstat *st;
1141	char *res;
1142
1143	if (!dentry)
1144		return ERR_PTR(-ECHILD);
1145
1146	v9ses = v9fs_dentry2v9ses(dentry);
1147	if (!v9fs_proto_dotu(v9ses))
1148		return ERR_PTR(-EBADF);
1149
1150	p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1151	fid = v9fs_fid_lookup(dentry);
1152
1153	if (IS_ERR(fid))
1154		return ERR_CAST(fid);
1155
1156	st = p9_client_stat(fid);
1157	p9_fid_put(fid);
1158	if (IS_ERR(st))
1159		return ERR_CAST(st);
1160
1161	if (!(st->mode & P9_DMSYMLINK)) {
1162		p9stat_free(st);
1163		kfree(st);
1164		return ERR_PTR(-EINVAL);
1165	}
1166	res = st->extension;
1167	st->extension = NULL;
1168	if (strlen(res) >= PATH_MAX)
1169		res[PATH_MAX - 1] = '\0';
1170
1171	p9stat_free(st);
1172	kfree(st);
1173	set_delayed_call(done, kfree_link, res);
1174	return res;
1175}
1176
1177/**
1178 * v9fs_vfs_mkspecial - create a special file
1179 * @dir: inode to create special file in
1180 * @dentry: dentry to create
1181 * @perm: mode to create special file
1182 * @extension: 9p2000.u format extension string representing special file
1183 *
1184 */
1185
1186static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1187	u32 perm, const char *extension)
1188{
1189	struct p9_fid *fid;
1190	struct v9fs_session_info *v9ses;
1191
1192	v9ses = v9fs_inode2v9ses(dir);
1193	if (!v9fs_proto_dotu(v9ses)) {
1194		p9_debug(P9_DEBUG_ERROR, "not extended\n");
1195		return -EPERM;
1196	}
1197
1198	fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1199								P9_OREAD);
1200	if (IS_ERR(fid))
1201		return PTR_ERR(fid);
1202
1203	v9fs_invalidate_inode_attr(dir);
1204	p9_fid_put(fid);
1205	return 0;
1206}
1207
1208/**
1209 * v9fs_vfs_symlink - helper function to create symlinks
1210 * @idmap: idmap of the mount
1211 * @dir: directory inode containing symlink
1212 * @dentry: dentry for symlink
1213 * @symname: symlink data
1214 *
1215 * See Also: 9P2000.u RFC for more information
1216 *
1217 */
1218
1219static int
1220v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1221		 struct dentry *dentry, const char *symname)
1222{
1223	p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1224		 dir->i_ino, dentry, symname);
1225
1226	return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1227}
1228
1229#define U32_MAX_DIGITS 10
1230
1231/**
1232 * v9fs_vfs_link - create a hardlink
1233 * @old_dentry: dentry for file to link to
1234 * @dir: inode destination for new link
1235 * @dentry: dentry for link
1236 *
1237 */
1238
1239static int
1240v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1241	      struct dentry *dentry)
1242{
1243	int retval;
1244	char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1245	struct p9_fid *oldfid;
1246
1247	p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1248		 dir->i_ino, dentry, old_dentry);
1249
1250	oldfid = v9fs_fid_clone(old_dentry);
1251	if (IS_ERR(oldfid))
1252		return PTR_ERR(oldfid);
1253
1254	sprintf(name, "%d\n", oldfid->fid);
1255	retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1256	if (!retval) {
1257		v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1258		v9fs_invalidate_inode_attr(dir);
1259	}
1260	p9_fid_put(oldfid);
1261	return retval;
1262}
1263
1264/**
1265 * v9fs_vfs_mknod - create a special file
1266 * @idmap: idmap of the mount
1267 * @dir: inode destination for new link
1268 * @dentry: dentry for file
1269 * @mode: mode for creation
1270 * @rdev: device associated with special file
1271 *
1272 */
1273
1274static int
1275v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1276	       struct dentry *dentry, umode_t mode, dev_t rdev)
1277{
1278	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1279	int retval;
1280	char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1281	u32 perm;
1282
1283	p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1284		 dir->i_ino, dentry, mode,
1285		 MAJOR(rdev), MINOR(rdev));
1286
1287	/* build extension */
1288	if (S_ISBLK(mode))
1289		sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1290	else if (S_ISCHR(mode))
1291		sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1292	else
1293		*name = 0;
1294
1295	perm = unixmode2p9mode(v9ses, mode);
1296	retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1297
1298	return retval;
1299}
1300
1301int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1302{
1303	int umode;
1304	dev_t rdev;
1305	struct p9_wstat *st;
1306	struct v9fs_session_info *v9ses;
1307	unsigned int flags;
1308
1309	v9ses = v9fs_inode2v9ses(inode);
1310	st = p9_client_stat(fid);
1311	if (IS_ERR(st))
1312		return PTR_ERR(st);
1313	/*
1314	 * Don't update inode if the file type is different
1315	 */
1316	umode = p9mode2unixmode(v9ses, st, &rdev);
1317	if (inode_wrong_type(inode, umode))
1318		goto out;
1319
1320	/*
1321	 * We don't want to refresh inode->i_size,
1322	 * because we may have cached data
1323	 */
1324	flags = (v9ses->cache & CACHE_LOOSE) ?
1325		V9FS_STAT2INODE_KEEP_ISIZE : 0;
1326	v9fs_stat2inode(st, inode, inode->i_sb, flags);
1327out:
1328	p9stat_free(st);
1329	kfree(st);
1330	return 0;
1331}
1332
1333static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1334	.create = v9fs_vfs_create,
1335	.lookup = v9fs_vfs_lookup,
1336	.atomic_open = v9fs_vfs_atomic_open,
1337	.symlink = v9fs_vfs_symlink,
1338	.link = v9fs_vfs_link,
1339	.unlink = v9fs_vfs_unlink,
1340	.mkdir = v9fs_vfs_mkdir,
1341	.rmdir = v9fs_vfs_rmdir,
1342	.mknod = v9fs_vfs_mknod,
1343	.rename = v9fs_vfs_rename,
1344	.getattr = v9fs_vfs_getattr,
1345	.setattr = v9fs_vfs_setattr,
1346};
1347
1348static const struct inode_operations v9fs_dir_inode_operations = {
1349	.create = v9fs_vfs_create,
1350	.lookup = v9fs_vfs_lookup,
1351	.atomic_open = v9fs_vfs_atomic_open,
1352	.unlink = v9fs_vfs_unlink,
1353	.mkdir = v9fs_vfs_mkdir,
1354	.rmdir = v9fs_vfs_rmdir,
1355	.mknod = v9fs_vfs_mknod,
1356	.rename = v9fs_vfs_rename,
1357	.getattr = v9fs_vfs_getattr,
1358	.setattr = v9fs_vfs_setattr,
1359};
1360
1361static const struct inode_operations v9fs_file_inode_operations = {
1362	.getattr = v9fs_vfs_getattr,
1363	.setattr = v9fs_vfs_setattr,
1364};
1365
1366static const struct inode_operations v9fs_symlink_inode_operations = {
1367	.get_link = v9fs_vfs_get_link,
1368	.getattr = v9fs_vfs_getattr,
1369	.setattr = v9fs_vfs_setattr,
1370};
1371
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This file contains vfs inode ops for the 9P2000 protocol.
   4 *
   5 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
   6 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
   7 */
   8
   9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10
  11#include <linux/module.h>
  12#include <linux/errno.h>
  13#include <linux/fs.h>
  14#include <linux/file.h>
  15#include <linux/pagemap.h>
  16#include <linux/stat.h>
  17#include <linux/string.h>
  18#include <linux/namei.h>
  19#include <linux/sched.h>
  20#include <linux/slab.h>
  21#include <linux/xattr.h>
  22#include <linux/posix_acl.h>
  23#include <net/9p/9p.h>
  24#include <net/9p/client.h>
  25
  26#include "v9fs.h"
  27#include "v9fs_vfs.h"
  28#include "fid.h"
  29#include "cache.h"
  30#include "xattr.h"
  31#include "acl.h"
  32
  33static const struct inode_operations v9fs_dir_inode_operations;
  34static const struct inode_operations v9fs_dir_inode_operations_dotu;
  35static const struct inode_operations v9fs_file_inode_operations;
  36static const struct inode_operations v9fs_symlink_inode_operations;
  37
  38/**
  39 * unixmode2p9mode - convert unix mode bits to plan 9
  40 * @v9ses: v9fs session information
  41 * @mode: mode to convert
  42 *
  43 */
  44
  45static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
  46{
  47	int res;
  48
  49	res = mode & 0777;
  50	if (S_ISDIR(mode))
  51		res |= P9_DMDIR;
  52	if (v9fs_proto_dotu(v9ses)) {
  53		if (v9ses->nodev == 0) {
  54			if (S_ISSOCK(mode))
  55				res |= P9_DMSOCKET;
  56			if (S_ISFIFO(mode))
  57				res |= P9_DMNAMEDPIPE;
  58			if (S_ISBLK(mode))
  59				res |= P9_DMDEVICE;
  60			if (S_ISCHR(mode))
  61				res |= P9_DMDEVICE;
  62		}
  63
  64		if ((mode & S_ISUID) == S_ISUID)
  65			res |= P9_DMSETUID;
  66		if ((mode & S_ISGID) == S_ISGID)
  67			res |= P9_DMSETGID;
  68		if ((mode & S_ISVTX) == S_ISVTX)
  69			res |= P9_DMSETVTX;
  70	}
  71	return res;
  72}
  73
  74/**
  75 * p9mode2perm- convert plan9 mode bits to unix permission bits
  76 * @v9ses: v9fs session information
  77 * @stat: p9_wstat from which mode need to be derived
  78 *
  79 */
  80static int p9mode2perm(struct v9fs_session_info *v9ses,
  81		       struct p9_wstat *stat)
  82{
  83	int res;
  84	int mode = stat->mode;
  85
  86	res = mode & 0777; /* S_IRWXUGO */
  87	if (v9fs_proto_dotu(v9ses)) {
  88		if ((mode & P9_DMSETUID) == P9_DMSETUID)
  89			res |= S_ISUID;
  90
  91		if ((mode & P9_DMSETGID) == P9_DMSETGID)
  92			res |= S_ISGID;
  93
  94		if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  95			res |= S_ISVTX;
  96	}
  97	return res;
  98}
  99
 100/**
 101 * p9mode2unixmode- convert plan9 mode bits to unix mode bits
 102 * @v9ses: v9fs session information
 103 * @stat: p9_wstat from which mode need to be derived
 104 * @rdev: major number, minor number in case of device files.
 105 *
 106 */
 107static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
 108			       struct p9_wstat *stat, dev_t *rdev)
 109{
 110	int res, r;
 111	u32 mode = stat->mode;
 112
 113	*rdev = 0;
 114	res = p9mode2perm(v9ses, stat);
 115
 116	if ((mode & P9_DMDIR) == P9_DMDIR)
 117		res |= S_IFDIR;
 118	else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
 119		res |= S_IFLNK;
 120	else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
 121		 && (v9ses->nodev == 0))
 122		res |= S_IFSOCK;
 123	else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
 124		 && (v9ses->nodev == 0))
 125		res |= S_IFIFO;
 126	else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
 127		 && (v9ses->nodev == 0)) {
 128		char type = 0;
 129		int major = -1, minor = -1;
 130
 131		r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor);
 132		if (r != 3) {
 133			p9_debug(P9_DEBUG_ERROR,
 134				 "invalid device string, umode will be bogus: %s\n",
 135				 stat->extension);
 136			return res;
 137		}
 138		switch (type) {
 139		case 'c':
 140			res |= S_IFCHR;
 141			break;
 142		case 'b':
 143			res |= S_IFBLK;
 144			break;
 145		default:
 146			p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
 147				 type, stat->extension);
 148		}
 149		*rdev = MKDEV(major, minor);
 150	} else
 151		res |= S_IFREG;
 152
 153	return res;
 154}
 155
 156/**
 157 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
 158 * @uflags: flags to convert
 159 * @extended: if .u extensions are active
 160 */
 161
 162int v9fs_uflags2omode(int uflags, int extended)
 163{
 164	int ret;
 165
 166	switch (uflags&3) {
 167	default:
 168	case O_RDONLY:
 169		ret = P9_OREAD;
 170		break;
 171
 172	case O_WRONLY:
 173		ret = P9_OWRITE;
 174		break;
 175
 176	case O_RDWR:
 177		ret = P9_ORDWR;
 178		break;
 179	}
 180
 181	if (uflags & O_TRUNC)
 182		ret |= P9_OTRUNC;
 183
 184	if (extended) {
 185		if (uflags & O_EXCL)
 186			ret |= P9_OEXCL;
 187
 188		if (uflags & O_APPEND)
 189			ret |= P9_OAPPEND;
 190	}
 191
 192	return ret;
 193}
 194
 195/**
 196 * v9fs_blank_wstat - helper function to setup a 9P stat structure
 197 * @wstat: structure to initialize
 198 *
 199 */
 200
 201void
 202v9fs_blank_wstat(struct p9_wstat *wstat)
 203{
 204	wstat->type = ~0;
 205	wstat->dev = ~0;
 206	wstat->qid.type = ~0;
 207	wstat->qid.version = ~0;
 208	*((long long *)&wstat->qid.path) = ~0;
 209	wstat->mode = ~0;
 210	wstat->atime = ~0;
 211	wstat->mtime = ~0;
 212	wstat->length = ~0;
 213	wstat->name = NULL;
 214	wstat->uid = NULL;
 215	wstat->gid = NULL;
 216	wstat->muid = NULL;
 217	wstat->n_uid = INVALID_UID;
 218	wstat->n_gid = INVALID_GID;
 219	wstat->n_muid = INVALID_UID;
 220	wstat->extension = NULL;
 221}
 222
 223/**
 224 * v9fs_alloc_inode - helper function to allocate an inode
 225 * @sb: The superblock to allocate the inode from
 226 */
 227struct inode *v9fs_alloc_inode(struct super_block *sb)
 228{
 229	struct v9fs_inode *v9inode;
 230
 231	v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL);
 232	if (!v9inode)
 233		return NULL;
 234	v9inode->cache_validity = 0;
 235	mutex_init(&v9inode->v_mutex);
 236	return &v9inode->netfs.inode;
 237}
 238
 239/**
 240 * v9fs_free_inode - destroy an inode
 241 * @inode: The inode to be freed
 242 */
 243
 244void v9fs_free_inode(struct inode *inode)
 245{
 246	kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
 247}
 248
 249/*
 250 * Set parameters for the netfs library
 251 */
 252void v9fs_set_netfs_context(struct inode *inode)
 253{
 254	struct v9fs_inode *v9inode = V9FS_I(inode);
 255	netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true);
 256}
 257
 258int v9fs_init_inode(struct v9fs_session_info *v9ses,
 259		    struct inode *inode, umode_t mode, dev_t rdev)
 260{
 261	int err = 0;
 
 
 
 262
 263	inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
 264	inode->i_blocks = 0;
 265	inode->i_rdev = rdev;
 266	simple_inode_init_ts(inode);
 267	inode->i_mapping->a_ops = &v9fs_addr_operations;
 268	inode->i_private = NULL;
 269
 270	switch (mode & S_IFMT) {
 271	case S_IFIFO:
 272	case S_IFBLK:
 273	case S_IFCHR:
 274	case S_IFSOCK:
 275		if (v9fs_proto_dotl(v9ses)) {
 276			inode->i_op = &v9fs_file_inode_operations_dotl;
 277		} else if (v9fs_proto_dotu(v9ses)) {
 278			inode->i_op = &v9fs_file_inode_operations;
 279		} else {
 280			p9_debug(P9_DEBUG_ERROR,
 281				 "special files without extended mode\n");
 282			err = -EINVAL;
 283			goto error;
 284		}
 285		init_special_inode(inode, inode->i_mode, inode->i_rdev);
 286		break;
 287	case S_IFREG:
 288		if (v9fs_proto_dotl(v9ses)) {
 289			inode->i_op = &v9fs_file_inode_operations_dotl;
 290			inode->i_fop = &v9fs_file_operations_dotl;
 291		} else {
 292			inode->i_op = &v9fs_file_inode_operations;
 293			inode->i_fop = &v9fs_file_operations;
 294		}
 295
 296		break;
 297	case S_IFLNK:
 298		if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
 299			p9_debug(P9_DEBUG_ERROR,
 300				 "extended modes used with legacy protocol\n");
 301			err = -EINVAL;
 302			goto error;
 303		}
 304
 305		if (v9fs_proto_dotl(v9ses))
 306			inode->i_op = &v9fs_symlink_inode_operations_dotl;
 307		else
 308			inode->i_op = &v9fs_symlink_inode_operations;
 309
 310		break;
 311	case S_IFDIR:
 312		inc_nlink(inode);
 313		if (v9fs_proto_dotl(v9ses))
 314			inode->i_op = &v9fs_dir_inode_operations_dotl;
 315		else if (v9fs_proto_dotu(v9ses))
 316			inode->i_op = &v9fs_dir_inode_operations_dotu;
 317		else
 318			inode->i_op = &v9fs_dir_inode_operations;
 319
 320		if (v9fs_proto_dotl(v9ses))
 321			inode->i_fop = &v9fs_dir_operations_dotl;
 322		else
 323			inode->i_fop = &v9fs_dir_operations;
 324
 325		break;
 326	default:
 327		p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
 328			 mode, mode & S_IFMT);
 329		err = -EINVAL;
 330		goto error;
 331	}
 332error:
 333	return err;
 334
 335}
 336
 337/**
 338 * v9fs_evict_inode - Remove an inode from the inode cache
 339 * @inode: inode to release
 340 *
 341 */
 342void v9fs_evict_inode(struct inode *inode)
 343{
 344	struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode);
 345	__le32 __maybe_unused version;
 346
 347	if (!is_bad_inode(inode)) {
 348		netfs_wait_for_outstanding_io(inode);
 349		truncate_inode_pages_final(&inode->i_data);
 350
 351		version = cpu_to_le32(v9inode->qid.version);
 352		netfs_clear_inode_writeback(inode, &version);
 353
 354		clear_inode(inode);
 355		filemap_fdatawrite(&inode->i_data);
 356
 357#ifdef CONFIG_9P_FSCACHE
 358		if (v9fs_inode_cookie(v9inode))
 359			fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false);
 360#endif
 361	} else
 362		clear_inode(inode);
 363}
 364
 365static int v9fs_test_inode(struct inode *inode, void *data)
 366{
 367	int umode;
 368	dev_t rdev;
 369	struct v9fs_inode *v9inode = V9FS_I(inode);
 370	struct p9_wstat *st = (struct p9_wstat *)data;
 371	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
 372
 373	umode = p9mode2unixmode(v9ses, st, &rdev);
 374	/* don't match inode of different type */
 375	if (inode_wrong_type(inode, umode))
 376		return 0;
 377
 378	/* compare qid details */
 379	if (memcmp(&v9inode->qid.version,
 380		   &st->qid.version, sizeof(v9inode->qid.version)))
 381		return 0;
 382
 383	if (v9inode->qid.type != st->qid.type)
 384		return 0;
 385
 386	if (v9inode->qid.path != st->qid.path)
 387		return 0;
 388	return 1;
 389}
 390
 391static int v9fs_test_new_inode(struct inode *inode, void *data)
 392{
 393	return 0;
 394}
 395
 396static int v9fs_set_inode(struct inode *inode,  void *data)
 397{
 398	struct v9fs_inode *v9inode = V9FS_I(inode);
 399	struct p9_wstat *st = (struct p9_wstat *)data;
 400
 401	memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
 402	return 0;
 403}
 404
 405static struct inode *v9fs_qid_iget(struct super_block *sb,
 406				   struct p9_qid *qid,
 407				   struct p9_wstat *st,
 408				   int new)
 409{
 410	dev_t rdev;
 411	int retval;
 412	umode_t umode;
 413	struct inode *inode;
 
 414	struct v9fs_session_info *v9ses = sb->s_fs_info;
 415	int (*test)(struct inode *inode, void *data);
 416
 417	if (new)
 418		test = v9fs_test_new_inode;
 419	else
 420		test = v9fs_test_inode;
 
 
 
 
 
 
 
 
 
 
 
 421
 422	inode = iget5_locked(sb, QID2INO(qid), test, v9fs_set_inode, st);
 423	if (!inode)
 424		return ERR_PTR(-ENOMEM);
 425	if (!(inode->i_state & I_NEW))
 426		return inode;
 427	/*
 428	 * initialize the inode with the stat info
 429	 * FIXME!! we may need support for stale inodes
 430	 * later.
 431	 */
 432	inode->i_ino = QID2INO(qid);
 
 
 
 
 
 433	umode = p9mode2unixmode(v9ses, st, &rdev);
 434	retval = v9fs_init_inode(v9ses, inode, umode, rdev);
 
 
 
 435	if (retval)
 436		goto error;
 437
 438	v9fs_stat2inode(st, inode, sb, 0);
 439	v9fs_set_netfs_context(inode);
 440	v9fs_cache_inode_get_cookie(inode);
 441	unlock_new_inode(inode);
 
 442	return inode;
 443error:
 444	iget_failed(inode);
 445	return ERR_PTR(retval);
 446
 447}
 448
 449struct inode *
 450v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
 451		    struct super_block *sb, int new)
 452{
 453	struct p9_wstat *st;
 454	struct inode *inode = NULL;
 455
 456	st = p9_client_stat(fid);
 457	if (IS_ERR(st))
 458		return ERR_CAST(st);
 459
 460	inode = v9fs_qid_iget(sb, &st->qid, st, new);
 461	p9stat_free(st);
 462	kfree(st);
 463	return inode;
 464}
 465
 466/**
 467 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
 468 * plan 9 AT flag.
 469 * @flags: flags to convert
 470 */
 471static int v9fs_at_to_dotl_flags(int flags)
 472{
 473	int rflags = 0;
 474
 475	if (flags & AT_REMOVEDIR)
 476		rflags |= P9_DOTL_AT_REMOVEDIR;
 477
 478	return rflags;
 479}
 480
 481/**
 482 * v9fs_dec_count - helper functon to drop i_nlink.
 483 *
 484 * If a directory had nlink <= 2 (including . and ..), then we should not drop
 485 * the link count, which indicates the underlying exported fs doesn't maintain
 486 * nlink accurately. e.g.
 487 * - overlayfs sets nlink to 1 for merged dir
 488 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more
 489 *   than EXT4_LINK_MAX (65000) links.
 490 *
 491 * @inode: inode whose nlink is being dropped
 492 */
 493static void v9fs_dec_count(struct inode *inode)
 494{
 495	if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
 496		drop_nlink(inode);
 
 
 
 
 
 
 
 497}
 498
 499/**
 500 * v9fs_remove - helper function to remove files and directories
 501 * @dir: directory inode that is being deleted
 502 * @dentry:  dentry that is being deleted
 503 * @flags: removing a directory
 504 *
 505 */
 506
 507static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
 508{
 509	struct inode *inode;
 510	int retval = -EOPNOTSUPP;
 511	struct p9_fid *v9fid, *dfid;
 512	struct v9fs_session_info *v9ses;
 513
 514	p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
 515		 dir, dentry, flags);
 516
 517	v9ses = v9fs_inode2v9ses(dir);
 518	inode = d_inode(dentry);
 519	dfid = v9fs_parent_fid(dentry);
 520	if (IS_ERR(dfid)) {
 521		retval = PTR_ERR(dfid);
 522		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
 523		return retval;
 524	}
 525	if (v9fs_proto_dotl(v9ses))
 526		retval = p9_client_unlinkat(dfid, dentry->d_name.name,
 527					    v9fs_at_to_dotl_flags(flags));
 528	p9_fid_put(dfid);
 529	if (retval == -EOPNOTSUPP) {
 530		/* Try the one based on path */
 531		v9fid = v9fs_fid_clone(dentry);
 532		if (IS_ERR(v9fid))
 533			return PTR_ERR(v9fid);
 534		retval = p9_client_remove(v9fid);
 535	}
 536	if (!retval) {
 537		/*
 538		 * directories on unlink should have zero
 539		 * link count
 540		 */
 541		if (flags & AT_REMOVEDIR) {
 542			clear_nlink(inode);
 543			v9fs_dec_count(dir);
 544		} else
 545			v9fs_dec_count(inode);
 546
 
 
 
 547		v9fs_invalidate_inode_attr(inode);
 548		v9fs_invalidate_inode_attr(dir);
 549
 550		/* invalidate all fids associated with dentry */
 551		/* NOTE: This will not include open fids */
 552		dentry->d_op->d_release(dentry);
 553	}
 554	return retval;
 555}
 556
 557/**
 558 * v9fs_create - Create a file
 559 * @v9ses: session information
 560 * @dir: directory that dentry is being created in
 561 * @dentry:  dentry that is being created
 562 * @extension: 9p2000.u extension string to support devices, etc.
 563 * @perm: create permissions
 564 * @mode: open mode
 565 *
 566 */
 567static struct p9_fid *
 568v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
 569		struct dentry *dentry, char *extension, u32 perm, u8 mode)
 570{
 571	int err;
 572	const unsigned char *name;
 573	struct p9_fid *dfid, *ofid = NULL, *fid = NULL;
 574	struct inode *inode;
 575
 576	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
 577
 578	name = dentry->d_name.name;
 579	dfid = v9fs_parent_fid(dentry);
 580	if (IS_ERR(dfid)) {
 581		err = PTR_ERR(dfid);
 582		p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
 583		return ERR_PTR(err);
 584	}
 585
 586	/* clone a fid to use for creation */
 587	ofid = clone_fid(dfid);
 588	if (IS_ERR(ofid)) {
 589		err = PTR_ERR(ofid);
 590		p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
 591		goto error;
 592	}
 593
 594	err = p9_client_fcreate(ofid, name, perm, mode, extension);
 595	if (err < 0) {
 596		p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
 597		goto error;
 598	}
 599
 600	if (!(perm & P9_DMLINK)) {
 601		/* now walk from the parent so we can get unopened fid */
 602		fid = p9_client_walk(dfid, 1, &name, 1);
 603		if (IS_ERR(fid)) {
 604			err = PTR_ERR(fid);
 605			p9_debug(P9_DEBUG_VFS,
 606				   "p9_client_walk failed %d\n", err);
 607			goto error;
 608		}
 609		/*
 610		 * instantiate inode and assign the unopened fid to the dentry
 611		 */
 612		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
 613		if (IS_ERR(inode)) {
 614			err = PTR_ERR(inode);
 615			p9_debug(P9_DEBUG_VFS,
 616				   "inode creation failed %d\n", err);
 617			goto error;
 618		}
 619		v9fs_fid_add(dentry, &fid);
 620		d_instantiate(dentry, inode);
 621	}
 622	p9_fid_put(dfid);
 623	return ofid;
 624error:
 625	p9_fid_put(dfid);
 626	p9_fid_put(ofid);
 627	p9_fid_put(fid);
 628	return ERR_PTR(err);
 629}
 630
 631/**
 632 * v9fs_vfs_create - VFS hook to create a regular file
 633 * @idmap: idmap of the mount
 634 * @dir: The parent directory
 635 * @dentry: The name of file to be created
 636 * @mode: The UNIX file mode to set
 637 * @excl: True if the file must not yet exist
 638 *
 639 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open().  This is only called
 640 * for mknod(2).
 641 *
 642 */
 643
 644static int
 645v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir,
 646		struct dentry *dentry, umode_t mode, bool excl)
 647{
 648	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
 649	u32 perm = unixmode2p9mode(v9ses, mode);
 650	struct p9_fid *fid;
 651
 652	/* P9_OEXCL? */
 653	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
 654	if (IS_ERR(fid))
 655		return PTR_ERR(fid);
 656
 657	v9fs_invalidate_inode_attr(dir);
 658	p9_fid_put(fid);
 659
 660	return 0;
 661}
 662
 663/**
 664 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
 665 * @idmap: idmap of the mount
 666 * @dir:  inode that is being unlinked
 667 * @dentry: dentry that is being unlinked
 668 * @mode: mode for new directory
 669 *
 670 */
 671
 672static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
 673			  struct dentry *dentry, umode_t mode)
 674{
 675	int err;
 676	u32 perm;
 677	struct p9_fid *fid;
 678	struct v9fs_session_info *v9ses;
 679
 680	p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
 681	err = 0;
 682	v9ses = v9fs_inode2v9ses(dir);
 683	perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
 684	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
 685	if (IS_ERR(fid)) {
 686		err = PTR_ERR(fid);
 687		fid = NULL;
 688	} else {
 689		inc_nlink(dir);
 690		v9fs_invalidate_inode_attr(dir);
 691	}
 692
 693	if (fid)
 694		p9_fid_put(fid);
 695
 696	return err;
 697}
 698
 699/**
 700 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
 701 * @dir:  inode that is being walked from
 702 * @dentry: dentry that is being walked to?
 703 * @flags: lookup flags (unused)
 704 *
 705 */
 706
 707struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
 708				      unsigned int flags)
 709{
 710	struct dentry *res;
 711	struct v9fs_session_info *v9ses;
 712	struct p9_fid *dfid, *fid;
 713	struct inode *inode;
 714	const unsigned char *name;
 715
 716	p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
 717		 dir, dentry, dentry, flags);
 718
 719	if (dentry->d_name.len > NAME_MAX)
 720		return ERR_PTR(-ENAMETOOLONG);
 721
 722	v9ses = v9fs_inode2v9ses(dir);
 723	/* We can walk d_parent because we hold the dir->i_mutex */
 724	dfid = v9fs_parent_fid(dentry);
 725	if (IS_ERR(dfid))
 726		return ERR_CAST(dfid);
 727
 728	/*
 729	 * Make sure we don't use a wrong inode due to parallel
 730	 * unlink. For cached mode create calls request for new
 731	 * inode. But with cache disabled, lookup should do this.
 732	 */
 733	name = dentry->d_name.name;
 734	fid = p9_client_walk(dfid, 1, &name, 1);
 735	p9_fid_put(dfid);
 736	if (fid == ERR_PTR(-ENOENT))
 737		inode = NULL;
 738	else if (IS_ERR(fid))
 739		inode = ERR_CAST(fid);
 740	else if (v9ses->cache & (CACHE_META|CACHE_LOOSE))
 741		inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
 742	else
 743		inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
 744	/*
 745	 * If we had a rename on the server and a parallel lookup
 746	 * for the new name, then make sure we instantiate with
 747	 * the new name. ie look up for a/b, while on server somebody
 748	 * moved b under k and client parallely did a lookup for
 749	 * k/b.
 750	 */
 751	res = d_splice_alias(inode, dentry);
 752	if (!IS_ERR(fid)) {
 753		if (!res)
 754			v9fs_fid_add(dentry, &fid);
 755		else if (!IS_ERR(res))
 756			v9fs_fid_add(res, &fid);
 757		else
 758			p9_fid_put(fid);
 759	}
 760	return res;
 761}
 762
 763static int
 764v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
 765		     struct file *file, unsigned int flags, umode_t mode)
 766{
 767	int err;
 768	u32 perm;
 769	struct v9fs_inode __maybe_unused *v9inode;
 770	struct v9fs_session_info *v9ses;
 771	struct p9_fid *fid;
 772	struct dentry *res = NULL;
 773	struct inode *inode;
 774	int p9_omode;
 775
 776	if (d_in_lookup(dentry)) {
 777		res = v9fs_vfs_lookup(dir, dentry, 0);
 778		if (IS_ERR(res))
 779			return PTR_ERR(res);
 780
 781		if (res)
 782			dentry = res;
 783	}
 784
 785	/* Only creates */
 786	if (!(flags & O_CREAT) || d_really_is_positive(dentry))
 787		return finish_no_open(file, res);
 788
 789	v9ses = v9fs_inode2v9ses(dir);
 790	perm = unixmode2p9mode(v9ses, mode);
 791	p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses));
 792
 793	if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
 794		p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR;
 795		p9_debug(P9_DEBUG_CACHE,
 796			"write-only file with writeback enabled, creating w/ O_RDWR\n");
 797	}
 798	fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode);
 799	if (IS_ERR(fid)) {
 800		err = PTR_ERR(fid);
 801		goto error;
 802	}
 803
 804	v9fs_invalidate_inode_attr(dir);
 805	inode = d_inode(dentry);
 806	v9inode = V9FS_I(inode);
 807	err = finish_open(file, dentry, generic_file_open);
 808	if (err)
 809		goto error;
 810
 811	file->private_data = fid;
 812#ifdef CONFIG_9P_FSCACHE
 813	if (v9ses->cache & CACHE_FSCACHE)
 814		fscache_use_cookie(v9fs_inode_cookie(v9inode),
 815				   file->f_mode & FMODE_WRITE);
 816#endif
 817
 818	v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
 819	v9fs_open_fid_add(inode, &fid);
 820
 821	file->f_mode |= FMODE_CREATED;
 822out:
 823	dput(res);
 824	return err;
 825
 826error:
 827	p9_fid_put(fid);
 828	goto out;
 829}
 830
 831/**
 832 * v9fs_vfs_unlink - VFS unlink hook to delete an inode
 833 * @i:  inode that is being unlinked
 834 * @d: dentry that is being unlinked
 835 *
 836 */
 837
 838int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
 839{
 840	return v9fs_remove(i, d, 0);
 841}
 842
 843/**
 844 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
 845 * @i:  inode that is being unlinked
 846 * @d: dentry that is being unlinked
 847 *
 848 */
 849
 850int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
 851{
 852	return v9fs_remove(i, d, AT_REMOVEDIR);
 853}
 854
 855/**
 856 * v9fs_vfs_rename - VFS hook to rename an inode
 857 * @idmap: The idmap of the mount
 858 * @old_dir:  old dir inode
 859 * @old_dentry: old dentry
 860 * @new_dir: new dir inode
 861 * @new_dentry: new dentry
 862 * @flags: RENAME_* flags
 863 *
 864 */
 865
 866int
 867v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 868		struct dentry *old_dentry, struct inode *new_dir,
 869		struct dentry *new_dentry, unsigned int flags)
 870{
 871	int retval;
 872	struct inode *old_inode;
 873	struct inode *new_inode;
 874	struct v9fs_session_info *v9ses;
 875	struct p9_fid *oldfid = NULL, *dfid = NULL;
 876	struct p9_fid *olddirfid = NULL;
 877	struct p9_fid *newdirfid = NULL;
 878	struct p9_wstat wstat;
 879
 880	if (flags)
 881		return -EINVAL;
 882
 883	p9_debug(P9_DEBUG_VFS, "\n");
 884	old_inode = d_inode(old_dentry);
 885	new_inode = d_inode(new_dentry);
 886	v9ses = v9fs_inode2v9ses(old_inode);
 887	oldfid = v9fs_fid_lookup(old_dentry);
 888	if (IS_ERR(oldfid))
 889		return PTR_ERR(oldfid);
 890
 891	dfid = v9fs_parent_fid(old_dentry);
 892	olddirfid = clone_fid(dfid);
 893	p9_fid_put(dfid);
 894	dfid = NULL;
 895
 896	if (IS_ERR(olddirfid)) {
 897		retval = PTR_ERR(olddirfid);
 898		goto error;
 899	}
 900
 901	dfid = v9fs_parent_fid(new_dentry);
 902	newdirfid = clone_fid(dfid);
 903	p9_fid_put(dfid);
 904	dfid = NULL;
 905
 906	if (IS_ERR(newdirfid)) {
 907		retval = PTR_ERR(newdirfid);
 908		goto error;
 909	}
 910
 911	down_write(&v9ses->rename_sem);
 912	if (v9fs_proto_dotl(v9ses)) {
 913		retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
 914					    newdirfid, new_dentry->d_name.name);
 915		if (retval == -EOPNOTSUPP)
 916			retval = p9_client_rename(oldfid, newdirfid,
 917						  new_dentry->d_name.name);
 918		if (retval != -EOPNOTSUPP)
 919			goto error_locked;
 920	}
 921	if (old_dentry->d_parent != new_dentry->d_parent) {
 922		/*
 923		 * 9P .u can only handle file rename in the same directory
 924		 */
 925
 926		p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
 927		retval = -EXDEV;
 928		goto error_locked;
 929	}
 930	v9fs_blank_wstat(&wstat);
 931	wstat.muid = v9ses->uname;
 932	wstat.name = new_dentry->d_name.name;
 933	retval = p9_client_wstat(oldfid, &wstat);
 934
 935error_locked:
 936	if (!retval) {
 937		if (new_inode) {
 938			if (S_ISDIR(new_inode->i_mode))
 939				clear_nlink(new_inode);
 940			else
 941				v9fs_dec_count(new_inode);
 942		}
 943		if (S_ISDIR(old_inode->i_mode)) {
 944			if (!new_inode)
 945				inc_nlink(new_dir);
 946			v9fs_dec_count(old_dir);
 947		}
 948		v9fs_invalidate_inode_attr(old_inode);
 949		v9fs_invalidate_inode_attr(old_dir);
 950		v9fs_invalidate_inode_attr(new_dir);
 951
 952		/* successful rename */
 953		d_move(old_dentry, new_dentry);
 954	}
 955	up_write(&v9ses->rename_sem);
 956
 957error:
 958	p9_fid_put(newdirfid);
 959	p9_fid_put(olddirfid);
 960	p9_fid_put(oldfid);
 961	return retval;
 962}
 963
 964/**
 965 * v9fs_vfs_getattr - retrieve file metadata
 966 * @idmap: idmap of the mount
 967 * @path: Object to query
 968 * @stat: metadata structure to populate
 969 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
 970 * @flags: AT_STATX_xxx setting
 971 *
 972 */
 973
 974static int
 975v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path,
 976		 struct kstat *stat, u32 request_mask, unsigned int flags)
 977{
 978	struct dentry *dentry = path->dentry;
 979	struct inode *inode = d_inode(dentry);
 980	struct v9fs_session_info *v9ses;
 981	struct p9_fid *fid;
 982	struct p9_wstat *st;
 983
 984	p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
 985	v9ses = v9fs_dentry2v9ses(dentry);
 986	if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
 987		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
 988		return 0;
 989	} else if (v9ses->cache & CACHE_WRITEBACK) {
 990		if (S_ISREG(inode->i_mode)) {
 991			int retval = filemap_fdatawrite(inode->i_mapping);
 992
 993			if (retval)
 994				p9_debug(P9_DEBUG_ERROR,
 995				    "flushing writeback during getattr returned %d\n", retval);
 996		}
 997	}
 998	fid = v9fs_fid_lookup(dentry);
 999	if (IS_ERR(fid))
1000		return PTR_ERR(fid);
1001
1002	st = p9_client_stat(fid);
1003	p9_fid_put(fid);
1004	if (IS_ERR(st))
1005		return PTR_ERR(st);
1006
1007	v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0);
1008	generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
1009
1010	p9stat_free(st);
1011	kfree(st);
1012	return 0;
1013}
1014
1015/**
1016 * v9fs_vfs_setattr - set file metadata
1017 * @idmap: idmap of the mount
1018 * @dentry: file whose metadata to set
1019 * @iattr: metadata assignment structure
1020 *
1021 */
1022
1023static int v9fs_vfs_setattr(struct mnt_idmap *idmap,
1024			    struct dentry *dentry, struct iattr *iattr)
1025{
1026	int retval, use_dentry = 0;
1027	struct inode *inode = d_inode(dentry);
1028	struct v9fs_session_info *v9ses;
1029	struct p9_fid *fid = NULL;
1030	struct p9_wstat wstat;
1031
1032	p9_debug(P9_DEBUG_VFS, "\n");
1033	retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
1034	if (retval)
1035		return retval;
1036
1037	v9ses = v9fs_dentry2v9ses(dentry);
1038	if (iattr->ia_valid & ATTR_FILE) {
1039		fid = iattr->ia_file->private_data;
1040		WARN_ON(!fid);
1041	}
1042	if (!fid) {
1043		fid = v9fs_fid_lookup(dentry);
1044		use_dentry = 1;
1045	}
1046	if (IS_ERR(fid))
1047		return PTR_ERR(fid);
1048
1049	v9fs_blank_wstat(&wstat);
1050	if (iattr->ia_valid & ATTR_MODE)
1051		wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
1052
1053	if (iattr->ia_valid & ATTR_MTIME)
1054		wstat.mtime = iattr->ia_mtime.tv_sec;
1055
1056	if (iattr->ia_valid & ATTR_ATIME)
1057		wstat.atime = iattr->ia_atime.tv_sec;
1058
1059	if (iattr->ia_valid & ATTR_SIZE)
1060		wstat.length = iattr->ia_size;
1061
1062	if (v9fs_proto_dotu(v9ses)) {
1063		if (iattr->ia_valid & ATTR_UID)
1064			wstat.n_uid = iattr->ia_uid;
1065
1066		if (iattr->ia_valid & ATTR_GID)
1067			wstat.n_gid = iattr->ia_gid;
1068	}
1069
1070	/* Write all dirty data */
1071	if (d_is_reg(dentry)) {
1072		retval = filemap_fdatawrite(inode->i_mapping);
1073		if (retval)
1074			p9_debug(P9_DEBUG_ERROR,
1075			    "flushing writeback during setattr returned %d\n", retval);
1076	}
1077
1078	retval = p9_client_wstat(fid, &wstat);
1079
1080	if (use_dentry)
1081		p9_fid_put(fid);
1082
1083	if (retval < 0)
1084		return retval;
1085
1086	if ((iattr->ia_valid & ATTR_SIZE) &&
1087		 iattr->ia_size != i_size_read(inode)) {
1088		truncate_setsize(inode, iattr->ia_size);
1089		netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
1090
1091#ifdef CONFIG_9P_FSCACHE
1092		if (v9ses->cache & CACHE_FSCACHE) {
1093			struct v9fs_inode *v9inode = V9FS_I(inode);
1094
1095			fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size);
1096		}
1097#endif
1098	}
1099
1100	v9fs_invalidate_inode_attr(inode);
1101
1102	setattr_copy(&nop_mnt_idmap, inode, iattr);
1103	mark_inode_dirty(inode);
1104	return 0;
1105}
1106
1107/**
1108 * v9fs_stat2inode - populate an inode structure with mistat info
1109 * @stat: Plan 9 metadata (mistat) structure
1110 * @inode: inode to populate
1111 * @sb: superblock of filesystem
1112 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
1113 *
1114 */
1115
1116void
1117v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
1118		 struct super_block *sb, unsigned int flags)
1119{
1120	umode_t mode;
1121	struct v9fs_session_info *v9ses = sb->s_fs_info;
1122	struct v9fs_inode *v9inode = V9FS_I(inode);
1123
1124	inode_set_atime(inode, stat->atime, 0);
1125	inode_set_mtime(inode, stat->mtime, 0);
1126	inode_set_ctime(inode, stat->mtime, 0);
1127
1128	inode->i_uid = v9ses->dfltuid;
1129	inode->i_gid = v9ses->dfltgid;
1130
1131	if (v9fs_proto_dotu(v9ses)) {
1132		inode->i_uid = stat->n_uid;
1133		inode->i_gid = stat->n_gid;
1134	}
1135	if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
1136		if (v9fs_proto_dotu(v9ses)) {
1137			unsigned int i_nlink;
1138			/*
1139			 * Hadlink support got added later to the .u extension.
1140			 * So there can be a server out there that doesn't
1141			 * support this even with .u extension. That would
1142			 * just leave us with stat->extension being an empty
1143			 * string, though.
1144			 */
1145			/* HARDLINKCOUNT %u */
1146			if (sscanf(stat->extension,
1147				   " HARDLINKCOUNT %u", &i_nlink) == 1)
1148				set_nlink(inode, i_nlink);
1149		}
1150	}
1151	mode = p9mode2perm(v9ses, stat);
1152	mode |= inode->i_mode & ~S_IALLUGO;
1153	inode->i_mode = mode;
1154
1155	v9inode->netfs.remote_i_size = stat->length;
1156	if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
1157		v9fs_i_size_write(inode, stat->length);
1158	/* not real number of blocks, but 512 byte ones ... */
1159	inode->i_blocks = (stat->length + 512 - 1) >> 9;
1160	v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
1161}
1162
1163/**
1164 * v9fs_vfs_get_link - follow a symlink path
1165 * @dentry: dentry for symlink
1166 * @inode: inode for symlink
1167 * @done: delayed call for when we are done with the return value
1168 */
1169
1170static const char *v9fs_vfs_get_link(struct dentry *dentry,
1171				     struct inode *inode,
1172				     struct delayed_call *done)
1173{
1174	struct v9fs_session_info *v9ses;
1175	struct p9_fid *fid;
1176	struct p9_wstat *st;
1177	char *res;
1178
1179	if (!dentry)
1180		return ERR_PTR(-ECHILD);
1181
1182	v9ses = v9fs_dentry2v9ses(dentry);
1183	if (!v9fs_proto_dotu(v9ses))
1184		return ERR_PTR(-EBADF);
1185
1186	p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
1187	fid = v9fs_fid_lookup(dentry);
1188
1189	if (IS_ERR(fid))
1190		return ERR_CAST(fid);
1191
1192	st = p9_client_stat(fid);
1193	p9_fid_put(fid);
1194	if (IS_ERR(st))
1195		return ERR_CAST(st);
1196
1197	if (!(st->mode & P9_DMSYMLINK)) {
1198		p9stat_free(st);
1199		kfree(st);
1200		return ERR_PTR(-EINVAL);
1201	}
1202	res = st->extension;
1203	st->extension = NULL;
1204	if (strlen(res) >= PATH_MAX)
1205		res[PATH_MAX - 1] = '\0';
1206
1207	p9stat_free(st);
1208	kfree(st);
1209	set_delayed_call(done, kfree_link, res);
1210	return res;
1211}
1212
1213/**
1214 * v9fs_vfs_mkspecial - create a special file
1215 * @dir: inode to create special file in
1216 * @dentry: dentry to create
1217 * @perm: mode to create special file
1218 * @extension: 9p2000.u format extension string representing special file
1219 *
1220 */
1221
1222static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
1223	u32 perm, const char *extension)
1224{
1225	struct p9_fid *fid;
1226	struct v9fs_session_info *v9ses;
1227
1228	v9ses = v9fs_inode2v9ses(dir);
1229	if (!v9fs_proto_dotu(v9ses)) {
1230		p9_debug(P9_DEBUG_ERROR, "not extended\n");
1231		return -EPERM;
1232	}
1233
1234	fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
1235								P9_OREAD);
1236	if (IS_ERR(fid))
1237		return PTR_ERR(fid);
1238
1239	v9fs_invalidate_inode_attr(dir);
1240	p9_fid_put(fid);
1241	return 0;
1242}
1243
1244/**
1245 * v9fs_vfs_symlink - helper function to create symlinks
1246 * @idmap: idmap of the mount
1247 * @dir: directory inode containing symlink
1248 * @dentry: dentry for symlink
1249 * @symname: symlink data
1250 *
1251 * See Also: 9P2000.u RFC for more information
1252 *
1253 */
1254
1255static int
1256v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
1257		 struct dentry *dentry, const char *symname)
1258{
1259	p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
1260		 dir->i_ino, dentry, symname);
1261
1262	return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
1263}
1264
1265#define U32_MAX_DIGITS 10
1266
1267/**
1268 * v9fs_vfs_link - create a hardlink
1269 * @old_dentry: dentry for file to link to
1270 * @dir: inode destination for new link
1271 * @dentry: dentry for link
1272 *
1273 */
1274
1275static int
1276v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
1277	      struct dentry *dentry)
1278{
1279	int retval;
1280	char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
1281	struct p9_fid *oldfid;
1282
1283	p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
1284		 dir->i_ino, dentry, old_dentry);
1285
1286	oldfid = v9fs_fid_clone(old_dentry);
1287	if (IS_ERR(oldfid))
1288		return PTR_ERR(oldfid);
1289
1290	sprintf(name, "%d\n", oldfid->fid);
1291	retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
1292	if (!retval) {
1293		v9fs_refresh_inode(oldfid, d_inode(old_dentry));
1294		v9fs_invalidate_inode_attr(dir);
1295	}
1296	p9_fid_put(oldfid);
1297	return retval;
1298}
1299
1300/**
1301 * v9fs_vfs_mknod - create a special file
1302 * @idmap: idmap of the mount
1303 * @dir: inode destination for new link
1304 * @dentry: dentry for file
1305 * @mode: mode for creation
1306 * @rdev: device associated with special file
1307 *
1308 */
1309
1310static int
1311v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
1312	       struct dentry *dentry, umode_t mode, dev_t rdev)
1313{
1314	struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
1315	int retval;
1316	char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
1317	u32 perm;
1318
1319	p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
1320		 dir->i_ino, dentry, mode,
1321		 MAJOR(rdev), MINOR(rdev));
1322
1323	/* build extension */
1324	if (S_ISBLK(mode))
1325		sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
1326	else if (S_ISCHR(mode))
1327		sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
1328	else
1329		*name = 0;
1330
1331	perm = unixmode2p9mode(v9ses, mode);
1332	retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
1333
1334	return retval;
1335}
1336
1337int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
1338{
1339	int umode;
1340	dev_t rdev;
1341	struct p9_wstat *st;
1342	struct v9fs_session_info *v9ses;
1343	unsigned int flags;
1344
1345	v9ses = v9fs_inode2v9ses(inode);
1346	st = p9_client_stat(fid);
1347	if (IS_ERR(st))
1348		return PTR_ERR(st);
1349	/*
1350	 * Don't update inode if the file type is different
1351	 */
1352	umode = p9mode2unixmode(v9ses, st, &rdev);
1353	if (inode_wrong_type(inode, umode))
1354		goto out;
1355
1356	/*
1357	 * We don't want to refresh inode->i_size,
1358	 * because we may have cached data
1359	 */
1360	flags = (v9ses->cache & CACHE_LOOSE) ?
1361		V9FS_STAT2INODE_KEEP_ISIZE : 0;
1362	v9fs_stat2inode(st, inode, inode->i_sb, flags);
1363out:
1364	p9stat_free(st);
1365	kfree(st);
1366	return 0;
1367}
1368
1369static const struct inode_operations v9fs_dir_inode_operations_dotu = {
1370	.create = v9fs_vfs_create,
1371	.lookup = v9fs_vfs_lookup,
1372	.atomic_open = v9fs_vfs_atomic_open,
1373	.symlink = v9fs_vfs_symlink,
1374	.link = v9fs_vfs_link,
1375	.unlink = v9fs_vfs_unlink,
1376	.mkdir = v9fs_vfs_mkdir,
1377	.rmdir = v9fs_vfs_rmdir,
1378	.mknod = v9fs_vfs_mknod,
1379	.rename = v9fs_vfs_rename,
1380	.getattr = v9fs_vfs_getattr,
1381	.setattr = v9fs_vfs_setattr,
1382};
1383
1384static const struct inode_operations v9fs_dir_inode_operations = {
1385	.create = v9fs_vfs_create,
1386	.lookup = v9fs_vfs_lookup,
1387	.atomic_open = v9fs_vfs_atomic_open,
1388	.unlink = v9fs_vfs_unlink,
1389	.mkdir = v9fs_vfs_mkdir,
1390	.rmdir = v9fs_vfs_rmdir,
1391	.mknod = v9fs_vfs_mknod,
1392	.rename = v9fs_vfs_rename,
1393	.getattr = v9fs_vfs_getattr,
1394	.setattr = v9fs_vfs_setattr,
1395};
1396
1397static const struct inode_operations v9fs_file_inode_operations = {
1398	.getattr = v9fs_vfs_getattr,
1399	.setattr = v9fs_vfs_setattr,
1400};
1401
1402static const struct inode_operations v9fs_symlink_inode_operations = {
1403	.get_link = v9fs_vfs_get_link,
1404	.getattr = v9fs_vfs_getattr,
1405	.setattr = v9fs_vfs_setattr,
1406};
1407