Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: LGPL-2.1
   2/*
   3 *
   4 *   Copyright (C) International Business Machines  Corp., 2002,2010
   5 *   Author(s): Steve French (sfrench@us.ibm.com)
   6 *
   7 */
   8#include <linux/fs.h>
   9#include <linux/stat.h>
  10#include <linux/slab.h>
  11#include <linux/pagemap.h>
  12#include <linux/freezer.h>
  13#include <linux/sched/signal.h>
  14#include <linux/wait_bit.h>
  15#include <linux/fiemap.h>
  16#include <asm/div64.h>
  17#include "cifsfs.h"
  18#include "cifspdu.h"
  19#include "cifsglob.h"
  20#include "cifsproto.h"
  21#include "smb2proto.h"
  22#include "cifs_debug.h"
  23#include "cifs_fs_sb.h"
  24#include "cifs_unicode.h"
  25#include "fscache.h"
  26#include "fs_context.h"
  27#include "cifs_ioctl.h"
  28#include "cached_dir.h"
 
  29
  30static void cifs_set_ops(struct inode *inode)
  31{
  32	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  33
  34	switch (inode->i_mode & S_IFMT) {
  35	case S_IFREG:
  36		inode->i_op = &cifs_file_inode_ops;
  37		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  38			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  39				inode->i_fop = &cifs_file_direct_nobrl_ops;
  40			else
  41				inode->i_fop = &cifs_file_direct_ops;
  42		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  43			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  44				inode->i_fop = &cifs_file_strict_nobrl_ops;
  45			else
  46				inode->i_fop = &cifs_file_strict_ops;
  47		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  48			inode->i_fop = &cifs_file_nobrl_ops;
  49		else { /* not direct, send byte range locks */
  50			inode->i_fop = &cifs_file_ops;
  51		}
  52
  53		/* check if server can support readahead */
  54		if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
  55				PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  56			inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  57		else
  58			inode->i_data.a_ops = &cifs_addr_ops;
  59		break;
  60	case S_IFDIR:
  61		if (IS_AUTOMOUNT(inode)) {
  62			inode->i_op = &cifs_namespace_inode_operations;
  63		} else {
  64			inode->i_op = &cifs_dir_inode_ops;
  65			inode->i_fop = &cifs_dir_ops;
  66		}
  67		break;
  68	case S_IFLNK:
  69		inode->i_op = &cifs_symlink_inode_ops;
  70		break;
  71	default:
  72		init_special_inode(inode, inode->i_mode, inode->i_rdev);
  73		break;
  74	}
  75}
  76
  77/* check inode attributes against fattr. If they don't match, tag the
  78 * inode for cache invalidation
  79 */
  80static void
  81cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  82{
  83	struct cifs_fscache_inode_coherency_data cd;
  84	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  85	struct timespec64 mtime;
  86
  87	cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  88		 __func__, cifs_i->uniqueid);
  89
  90	if (inode->i_state & I_NEW) {
  91		cifs_dbg(FYI, "%s: inode %llu is new\n",
  92			 __func__, cifs_i->uniqueid);
  93		return;
  94	}
  95
  96	/* don't bother with revalidation if we have an oplock */
  97	if (CIFS_CACHE_READ(cifs_i)) {
  98		cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  99			 __func__, cifs_i->uniqueid);
 100		return;
 101	}
 102
 103	 /* revalidate if mtime or size have changed */
 104	fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
 105	mtime = inode_get_mtime(inode);
 106	if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
 107	    cifs_i->netfs.remote_i_size == fattr->cf_eof) {
 108		cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
 109			 __func__, cifs_i->uniqueid);
 110		return;
 111	}
 112
 113	cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
 114		 __func__, cifs_i->uniqueid);
 115	set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 116	/* Invalidate fscache cookie */
 117	cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
 118	fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
 119}
 120
 121/*
 122 * copy nlink to the inode, unless it wasn't provided.  Provide
 123 * sane values if we don't have an existing one and none was provided
 124 */
 125static void
 126cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 127{
 128	/*
 129	 * if we're in a situation where we can't trust what we
 130	 * got from the server (readdir, some non-unix cases)
 131	 * fake reasonable values
 132	 */
 133	if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
 134		/* only provide fake values on a new inode */
 135		if (inode->i_state & I_NEW) {
 136			if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
 137				set_nlink(inode, 2);
 138			else
 139				set_nlink(inode, 1);
 140		}
 141		return;
 142	}
 143
 144	/* we trust the server, so update it */
 145	set_nlink(inode, fattr->cf_nlink);
 146}
 147
 148/* populate an inode with info from a cifs_fattr struct */
 149int
 150cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 
 151{
 152	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 153	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 154
 155	if (!(inode->i_state & I_NEW) &&
 156	    unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
 157		CIFS_I(inode)->time = 0; /* force reval */
 158		return -ESTALE;
 159	}
 160
 161	cifs_revalidate_cache(inode, fattr);
 162
 163	spin_lock(&inode->i_lock);
 164	fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
 165	fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
 166	fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
 167	/* we do not want atime to be less than mtime, it broke some apps */
 168	if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
 169		inode_set_atime_to_ts(inode, fattr->cf_mtime);
 170	else
 171		inode_set_atime_to_ts(inode, fattr->cf_atime);
 172	inode_set_mtime_to_ts(inode, fattr->cf_mtime);
 173	inode_set_ctime_to_ts(inode, fattr->cf_ctime);
 174	inode->i_rdev = fattr->cf_rdev;
 175	cifs_nlink_fattr_to_inode(inode, fattr);
 176	inode->i_uid = fattr->cf_uid;
 177	inode->i_gid = fattr->cf_gid;
 178
 179	/* if dynperm is set, don't clobber existing mode */
 180	if (inode->i_state & I_NEW ||
 181	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
 182		inode->i_mode = fattr->cf_mode;
 183
 184	cifs_i->cifsAttrs = fattr->cf_cifsattrs;
 185	cifs_i->reparse_tag = fattr->cf_cifstag;
 186
 187	if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
 188		cifs_i->time = 0;
 189	else
 190		cifs_i->time = jiffies;
 191
 192	if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
 193		set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 194	else
 195		clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 196
 197	cifs_i->netfs.remote_i_size = fattr->cf_eof;
 198	/*
 199	 * Can't safely change the file size here if the client is writing to
 200	 * it due to potential races.
 201	 */
 202	if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
 203		i_size_write(inode, fattr->cf_eof);
 204
 205		/*
 206		 * i_blocks is not related to (i_size / i_blksize),
 207		 * but instead 512 byte (2**9) size is required for
 208		 * calculating num blocks.
 209		 */
 210		inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
 211	}
 212
 213	if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) {
 214		kfree(cifs_i->symlink_target);
 215		cifs_i->symlink_target = fattr->cf_symlink_target;
 216		fattr->cf_symlink_target = NULL;
 217	}
 218	spin_unlock(&inode->i_lock);
 219
 220	if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
 221		inode->i_flags |= S_AUTOMOUNT;
 222	if (inode->i_state & I_NEW)
 223		cifs_set_ops(inode);
 224	return 0;
 225}
 226
 227void
 228cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
 229{
 230	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 231
 232	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
 233		return;
 234
 235	fattr->cf_uniqueid = iunique(sb, ROOT_I);
 236}
 237
 238/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
 239void
 240cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
 241			 struct cifs_sb_info *cifs_sb)
 242{
 243	memset(fattr, 0, sizeof(*fattr));
 244	fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
 245	fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
 246	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 247
 248	fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 249	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
 250	fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
 251	/* old POSIX extensions don't get create time */
 252
 253	fattr->cf_mode = le64_to_cpu(info->Permissions);
 254
 255	/*
 256	 * Since we set the inode type below we need to mask off
 257	 * to avoid strange results if bits set above.
 258	 */
 259	fattr->cf_mode &= ~S_IFMT;
 260	switch (le32_to_cpu(info->Type)) {
 261	case UNIX_FILE:
 262		fattr->cf_mode |= S_IFREG;
 263		fattr->cf_dtype = DT_REG;
 264		break;
 265	case UNIX_SYMLINK:
 266		fattr->cf_mode |= S_IFLNK;
 267		fattr->cf_dtype = DT_LNK;
 268		break;
 269	case UNIX_DIR:
 270		fattr->cf_mode |= S_IFDIR;
 271		fattr->cf_dtype = DT_DIR;
 272		break;
 273	case UNIX_CHARDEV:
 274		fattr->cf_mode |= S_IFCHR;
 275		fattr->cf_dtype = DT_CHR;
 276		fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
 277				       le64_to_cpu(info->DevMinor) & MINORMASK);
 278		break;
 279	case UNIX_BLOCKDEV:
 280		fattr->cf_mode |= S_IFBLK;
 281		fattr->cf_dtype = DT_BLK;
 282		fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
 283				       le64_to_cpu(info->DevMinor) & MINORMASK);
 284		break;
 285	case UNIX_FIFO:
 286		fattr->cf_mode |= S_IFIFO;
 287		fattr->cf_dtype = DT_FIFO;
 288		break;
 289	case UNIX_SOCKET:
 290		fattr->cf_mode |= S_IFSOCK;
 291		fattr->cf_dtype = DT_SOCK;
 292		break;
 293	default:
 294		/* safest to call it a file if we do not know */
 295		fattr->cf_mode |= S_IFREG;
 296		fattr->cf_dtype = DT_REG;
 297		cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
 298		break;
 299	}
 300
 301	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 302	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
 303		u64 id = le64_to_cpu(info->Uid);
 304		if (id < ((uid_t)-1)) {
 305			kuid_t uid = make_kuid(&init_user_ns, id);
 306			if (uid_valid(uid))
 307				fattr->cf_uid = uid;
 308		}
 309	}
 310	
 311	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 312	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
 313		u64 id = le64_to_cpu(info->Gid);
 314		if (id < ((gid_t)-1)) {
 315			kgid_t gid = make_kgid(&init_user_ns, id);
 316			if (gid_valid(gid))
 317				fattr->cf_gid = gid;
 318		}
 319	}
 320
 321	fattr->cf_nlink = le64_to_cpu(info->Nlinks);
 322}
 323
 324/*
 325 * Fill a cifs_fattr struct with fake inode info.
 326 *
 327 * Needed to setup cifs_fattr data for the directory which is the
 328 * junction to the new submount (ie to setup the fake directory
 329 * which represents a DFS referral or reparse mount point).
 330 */
 331static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
 332				       struct super_block *sb)
 333{
 334	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 335
 336	cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
 337
 338	memset(fattr, 0, sizeof(*fattr));
 339	fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
 340	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 341	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 342	ktime_get_coarse_real_ts64(&fattr->cf_mtime);
 343	fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
 344	fattr->cf_nlink = 2;
 345	fattr->cf_flags = CIFS_FATTR_JUNCTION;
 346}
 347
 348/* Update inode with final fattr data */
 349static int update_inode_info(struct super_block *sb,
 350			     struct cifs_fattr *fattr,
 351			     struct inode **inode)
 352{
 353	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 354	int rc = 0;
 355
 356	if (!*inode) {
 357		*inode = cifs_iget(sb, fattr);
 358		if (!*inode)
 359			rc = -ENOMEM;
 360		return rc;
 361	}
 362	/* We already have inode, update it.
 363	 *
 364	 * If file type or uniqueid is different, return error.
 365	 */
 366	if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
 367		     CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
 368		CIFS_I(*inode)->time = 0; /* force reval */
 369		return -ESTALE;
 370	}
 371	return cifs_fattr_to_inode(*inode, fattr);
 372}
 373
 374#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
 375static int
 376cifs_get_file_info_unix(struct file *filp)
 377{
 378	int rc;
 379	unsigned int xid;
 380	FILE_UNIX_BASIC_INFO find_data;
 381	struct cifs_fattr fattr = {};
 382	struct inode *inode = file_inode(filp);
 383	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 384	struct cifsFileInfo *cfile = filp->private_data;
 385	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 386
 387	xid = get_xid();
 388
 389	if (cfile->symlink_target) {
 390		fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
 391		if (!fattr.cf_symlink_target) {
 392			rc = -ENOMEM;
 393			goto cifs_gfiunix_out;
 394		}
 395	}
 396
 397	rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
 398	if (!rc) {
 399		cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
 400	} else if (rc == -EREMOTE) {
 401		cifs_create_junction_fattr(&fattr, inode->i_sb);
 402		rc = 0;
 403	} else
 404		goto cifs_gfiunix_out;
 405
 406	rc = cifs_fattr_to_inode(inode, &fattr);
 407
 408cifs_gfiunix_out:
 409	free_xid(xid);
 410	return rc;
 411}
 412
 413static int cifs_get_unix_fattr(const unsigned char *full_path,
 414			       struct super_block *sb,
 415			       struct cifs_fattr *fattr,
 416			       struct inode **pinode,
 417			       const unsigned int xid)
 418{
 419	struct TCP_Server_Info *server;
 420	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 421	FILE_UNIX_BASIC_INFO find_data;
 422	struct cifs_tcon *tcon;
 423	struct tcon_link *tlink;
 424	int rc, tmprc;
 425
 426	cifs_dbg(FYI, "Getting info on %s\n", full_path);
 427
 428	tlink = cifs_sb_tlink(cifs_sb);
 429	if (IS_ERR(tlink))
 430		return PTR_ERR(tlink);
 431	tcon = tlink_tcon(tlink);
 432	server = tcon->ses->server;
 433
 434	/* could have done a find first instead but this returns more info */
 435	rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
 436				  cifs_sb->local_nls, cifs_remap(cifs_sb));
 437	cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
 438	cifs_put_tlink(tlink);
 439
 440	if (!rc) {
 441		cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
 442	} else if (rc == -EREMOTE) {
 443		cifs_create_junction_fattr(fattr, sb);
 444		rc = 0;
 445	} else {
 446		return rc;
 447	}
 448
 449	if (!*pinode)
 450		cifs_fill_uniqueid(sb, fattr);
 451
 452	/* check for Minshall+French symlinks */
 453	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
 454		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
 455		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
 456	}
 457
 458	if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
 459		if (!server->ops->query_symlink)
 460			return -EOPNOTSUPP;
 461		rc = server->ops->query_symlink(xid, tcon,
 462						cifs_sb, full_path,
 463						&fattr->cf_symlink_target);
 464		cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
 465	}
 466	return rc;
 467}
 468
 469int cifs_get_inode_info_unix(struct inode **pinode,
 470			     const unsigned char *full_path,
 471			     struct super_block *sb, unsigned int xid)
 472{
 473	struct cifs_fattr fattr = {};
 474	int rc;
 475
 476	rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
 477	if (rc)
 478		goto out;
 479
 480	rc = update_inode_info(sb, &fattr, pinode);
 481out:
 482	kfree(fattr.cf_symlink_target);
 483	return rc;
 484}
 485#else
 486static inline int cifs_get_unix_fattr(const unsigned char *full_path,
 487				      struct super_block *sb,
 488				      struct cifs_fattr *fattr,
 489				      struct inode **pinode,
 490				      const unsigned int xid)
 491{
 492	return -EOPNOTSUPP;
 493}
 494
 495int cifs_get_inode_info_unix(struct inode **pinode,
 496			     const unsigned char *full_path,
 497			     struct super_block *sb, unsigned int xid)
 498{
 499	return -EOPNOTSUPP;
 500}
 501#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
 502
 503static int
 504cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
 505	      struct cifs_sb_info *cifs_sb, unsigned int xid)
 506{
 507	int rc;
 508	__u32 oplock;
 509	struct tcon_link *tlink;
 510	struct cifs_tcon *tcon;
 511	struct cifs_fid fid;
 512	struct cifs_open_parms oparms;
 513	struct cifs_io_parms io_parms = {0};
 514	char buf[24];
 515	unsigned int bytes_read;
 516	char *pbuf;
 517	int buf_type = CIFS_NO_BUFFER;
 518
 519	pbuf = buf;
 520
 521	fattr->cf_mode &= ~S_IFMT;
 522
 523	if (fattr->cf_eof == 0) {
 524		fattr->cf_mode |= S_IFIFO;
 525		fattr->cf_dtype = DT_FIFO;
 526		return 0;
 527	} else if (fattr->cf_eof < 8) {
 528		fattr->cf_mode |= S_IFREG;
 529		fattr->cf_dtype = DT_REG;
 530		return -EINVAL;	 /* EOPNOTSUPP? */
 531	}
 532
 533	tlink = cifs_sb_tlink(cifs_sb);
 534	if (IS_ERR(tlink))
 535		return PTR_ERR(tlink);
 536	tcon = tlink_tcon(tlink);
 537
 538	oparms = (struct cifs_open_parms) {
 539		.tcon = tcon,
 540		.cifs_sb = cifs_sb,
 541		.desired_access = GENERIC_READ,
 542		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
 543		.disposition = FILE_OPEN,
 544		.path = path,
 545		.fid = &fid,
 546	};
 547
 548	if (tcon->ses->server->oplocks)
 549		oplock = REQ_OPLOCK;
 550	else
 551		oplock = 0;
 552	rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
 553	if (rc) {
 554		cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
 555		cifs_put_tlink(tlink);
 556		return rc;
 557	}
 558
 559	/* Read header */
 560	io_parms.netfid = fid.netfid;
 561	io_parms.pid = current->tgid;
 562	io_parms.tcon = tcon;
 563	io_parms.offset = 0;
 564	io_parms.length = 24;
 565
 566	rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
 567					&bytes_read, &pbuf, &buf_type);
 568	if ((rc == 0) && (bytes_read >= 8)) {
 569		if (memcmp("IntxBLK", pbuf, 8) == 0) {
 570			cifs_dbg(FYI, "Block device\n");
 571			fattr->cf_mode |= S_IFBLK;
 572			fattr->cf_dtype = DT_BLK;
 573			if (bytes_read == 24) {
 574				/* we have enough to decode dev num */
 575				__u64 mjr; /* major */
 576				__u64 mnr; /* minor */
 577				mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
 578				mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
 579				fattr->cf_rdev = MKDEV(mjr, mnr);
 580			}
 581		} else if (memcmp("IntxCHR", pbuf, 8) == 0) {
 582			cifs_dbg(FYI, "Char device\n");
 583			fattr->cf_mode |= S_IFCHR;
 584			fattr->cf_dtype = DT_CHR;
 585			if (bytes_read == 24) {
 586				/* we have enough to decode dev num */
 587				__u64 mjr; /* major */
 588				__u64 mnr; /* minor */
 589				mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
 590				mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
 591				fattr->cf_rdev = MKDEV(mjr, mnr);
 592			}
 593		} else if (memcmp("IntxLNK", pbuf, 7) == 0) {
 594			cifs_dbg(FYI, "Symlink\n");
 595			fattr->cf_mode |= S_IFLNK;
 596			fattr->cf_dtype = DT_LNK;
 597		} else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
 598			cifs_dbg(FYI, "FIFO\n");
 599			fattr->cf_mode |= S_IFIFO;
 600			fattr->cf_dtype = DT_FIFO;
 601		} else {
 602			fattr->cf_mode |= S_IFREG; /* file? */
 603			fattr->cf_dtype = DT_REG;
 604			rc = -EOPNOTSUPP;
 605		}
 606	} else {
 607		fattr->cf_mode |= S_IFREG; /* then it is a file */
 608		fattr->cf_dtype = DT_REG;
 609		rc = -EOPNOTSUPP; /* or some unknown SFU type */
 610	}
 611
 612	tcon->ses->server->ops->close(xid, tcon, &fid);
 613	cifs_put_tlink(tlink);
 614	return rc;
 615}
 616
 617#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
 618
 619/*
 620 * Fetch mode bits as provided by SFU.
 621 *
 622 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
 623 */
 624static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
 625			 struct cifs_sb_info *cifs_sb, unsigned int xid)
 626{
 627#ifdef CONFIG_CIFS_XATTR
 628	ssize_t rc;
 629	char ea_value[4];
 630	__u32 mode;
 631	struct tcon_link *tlink;
 632	struct cifs_tcon *tcon;
 633
 634	tlink = cifs_sb_tlink(cifs_sb);
 635	if (IS_ERR(tlink))
 636		return PTR_ERR(tlink);
 637	tcon = tlink_tcon(tlink);
 638
 639	if (tcon->ses->server->ops->query_all_EAs == NULL) {
 640		cifs_put_tlink(tlink);
 641		return -EOPNOTSUPP;
 642	}
 643
 644	rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
 645			"SETFILEBITS", ea_value, 4 /* size of buf */,
 646			cifs_sb);
 647	cifs_put_tlink(tlink);
 648	if (rc < 0)
 649		return (int)rc;
 650	else if (rc > 3) {
 651		mode = le32_to_cpu(*((__le32 *)ea_value));
 652		fattr->cf_mode &= ~SFBITS_MASK;
 653		cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
 654			 mode, fattr->cf_mode);
 655		fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
 656		cifs_dbg(FYI, "special mode bits 0%o\n", mode);
 657	}
 658
 659	return 0;
 660#else
 661	return -EOPNOTSUPP;
 662#endif
 663}
 664
 665/* Fill a cifs_fattr struct with info from POSIX info struct */
 666static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
 667				       struct cifs_open_info_data *data,
 668				       struct super_block *sb)
 669{
 670	struct smb311_posix_qinfo *info = &data->posix_fi;
 671	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 672	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 673
 674	memset(fattr, 0, sizeof(*fattr));
 675
 676	/* no fattr->flags to set */
 677	fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
 678	fattr->cf_uniqueid = le64_to_cpu(info->Inode);
 679
 680	if (info->LastAccessTime)
 681		fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 682	else
 683		ktime_get_coarse_real_ts64(&fattr->cf_atime);
 684
 685	fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
 686	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
 687
 688	if (data->adjust_tz) {
 689		fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
 690		fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
 691	}
 692
 693	/*
 694	 * The srv fs device id is overridden on network mount so setting
 695	 * @fattr->cf_rdev isn't needed here.
 696	 */
 697	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 698	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 699	fattr->cf_createtime = le64_to_cpu(info->CreationTime);
 700	fattr->cf_nlink = le32_to_cpu(info->HardLinks);
 701	fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
 702
 703	if (cifs_open_data_reparse(data) &&
 704	    cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
 705		goto out_reparse;
 706
 707	fattr->cf_mode &= ~S_IFMT;
 708	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
 709		fattr->cf_mode |= S_IFDIR;
 710		fattr->cf_dtype = DT_DIR;
 711	} else { /* file */
 712		fattr->cf_mode |= S_IFREG;
 713		fattr->cf_dtype = DT_REG;
 714	}
 715
 716out_reparse:
 717	if (S_ISLNK(fattr->cf_mode)) {
 718		if (likely(data->symlink_target))
 719			fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
 720		fattr->cf_symlink_target = data->symlink_target;
 721		data->symlink_target = NULL;
 722	}
 723	sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER);
 724	sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP);
 725
 726	cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
 727		fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
 728}
 729
 730static inline dev_t nfs_mkdev(struct reparse_posix_data *buf)
 731{
 732	u64 v = le64_to_cpu(*(__le64 *)buf->DataBuffer);
 733
 734	return MKDEV(v >> 32, v & 0xffffffff);
 735}
 736
 737bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
 738				 struct cifs_fattr *fattr,
 739				 struct cifs_open_info_data *data)
 740{
 741	struct reparse_posix_data *buf = data->reparse.posix;
 742	u32 tag = data->reparse.tag;
 743
 744	if (tag == IO_REPARSE_TAG_NFS && buf) {
 745		switch (le64_to_cpu(buf->InodeType)) {
 746		case NFS_SPECFILE_CHR:
 747			fattr->cf_mode |= S_IFCHR;
 748			fattr->cf_dtype = DT_CHR;
 749			fattr->cf_rdev = nfs_mkdev(buf);
 750			break;
 751		case NFS_SPECFILE_BLK:
 752			fattr->cf_mode |= S_IFBLK;
 753			fattr->cf_dtype = DT_BLK;
 754			fattr->cf_rdev = nfs_mkdev(buf);
 755			break;
 756		case NFS_SPECFILE_FIFO:
 757			fattr->cf_mode |= S_IFIFO;
 758			fattr->cf_dtype = DT_FIFO;
 759			break;
 760		case NFS_SPECFILE_SOCK:
 761			fattr->cf_mode |= S_IFSOCK;
 762			fattr->cf_dtype = DT_SOCK;
 763			break;
 764		case NFS_SPECFILE_LNK:
 765			fattr->cf_mode |= S_IFLNK;
 766			fattr->cf_dtype = DT_LNK;
 767			break;
 768		default:
 769			WARN_ON_ONCE(1);
 770			return false;
 771		}
 772		return true;
 773	}
 774
 775	switch (tag) {
 776	case IO_REPARSE_TAG_LX_SYMLINK:
 777		fattr->cf_mode |= S_IFLNK;
 778		fattr->cf_dtype = DT_LNK;
 779		break;
 780	case IO_REPARSE_TAG_LX_FIFO:
 781		fattr->cf_mode |= S_IFIFO;
 782		fattr->cf_dtype = DT_FIFO;
 783		break;
 784	case IO_REPARSE_TAG_AF_UNIX:
 785		fattr->cf_mode |= S_IFSOCK;
 786		fattr->cf_dtype = DT_SOCK;
 787		break;
 788	case IO_REPARSE_TAG_LX_CHR:
 789		fattr->cf_mode |= S_IFCHR;
 790		fattr->cf_dtype = DT_CHR;
 791		break;
 792	case IO_REPARSE_TAG_LX_BLK:
 793		fattr->cf_mode |= S_IFBLK;
 794		fattr->cf_dtype = DT_BLK;
 795		break;
 796	case 0: /* SMB1 symlink */
 797	case IO_REPARSE_TAG_SYMLINK:
 798	case IO_REPARSE_TAG_NFS:
 799		fattr->cf_mode |= S_IFLNK;
 800		fattr->cf_dtype = DT_LNK;
 801		break;
 802	default:
 803		return false;
 804	}
 805	return true;
 806}
 807
 808static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
 809				    struct cifs_open_info_data *data,
 810				    struct super_block *sb)
 811{
 812	struct smb2_file_all_info *info = &data->fi;
 813	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 814	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 815
 816	memset(fattr, 0, sizeof(*fattr));
 817	fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
 818	if (info->DeletePending)
 819		fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
 820
 821	if (info->LastAccessTime)
 822		fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 823	else
 824		ktime_get_coarse_real_ts64(&fattr->cf_atime);
 825
 826	fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
 827	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
 828
 829	if (data->adjust_tz) {
 830		fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
 831		fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
 832	}
 833
 834	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 835	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 836	fattr->cf_createtime = le64_to_cpu(info->CreationTime);
 837	fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
 
 
 838
 839	fattr->cf_mode = cifs_sb->ctx->file_mode;
 840	if (cifs_open_data_reparse(data) &&
 841	    cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
 842		goto out_reparse;
 843
 844	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
 845		fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
 846		fattr->cf_dtype = DT_DIR;
 847		/*
 848		 * Server can return wrong NumberOfLinks value for directories
 849		 * when Unix extensions are disabled - fake it.
 850		 */
 851		if (!tcon->unix_ext)
 852			fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
 853	} else {
 854		fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
 855		fattr->cf_dtype = DT_REG;
 856
 857		/* clear write bits if ATTR_READONLY is set */
 858		if (fattr->cf_cifsattrs & ATTR_READONLY)
 859			fattr->cf_mode &= ~(S_IWUGO);
 860
 861		/*
 862		 * Don't accept zero nlink from non-unix servers unless
 863		 * delete is pending.  Instead mark it as unknown.
 864		 */
 865		if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
 866		    !info->DeletePending) {
 867			cifs_dbg(VFS, "bogus file nlink value %u\n",
 868				 fattr->cf_nlink);
 869			fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
 870		}
 871	}
 872
 873out_reparse:
 874	if (S_ISLNK(fattr->cf_mode)) {
 875		if (likely(data->symlink_target))
 876			fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
 877		fattr->cf_symlink_target = data->symlink_target;
 878		data->symlink_target = NULL;
 879	}
 880
 881	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 882	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 883}
 884
 885static int
 886cifs_get_file_info(struct file *filp)
 887{
 888	int rc;
 889	unsigned int xid;
 890	struct cifs_open_info_data data = {};
 891	struct cifs_fattr fattr;
 892	struct inode *inode = file_inode(filp);
 893	struct cifsFileInfo *cfile = filp->private_data;
 894	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 895	struct TCP_Server_Info *server = tcon->ses->server;
 
 
 
 896
 897	if (!server->ops->query_file_info)
 
 898		return -ENOSYS;
 
 899
 900	xid = get_xid();
 901	rc = server->ops->query_file_info(xid, tcon, cfile, &data);
 902	switch (rc) {
 903	case 0:
 904		/* TODO: add support to query reparse tag */
 905		data.adjust_tz = false;
 906		if (data.symlink_target) {
 907			data.symlink = true;
 908			data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
 909		}
 
 
 
 
 
 910		cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
 
 
 911		break;
 912	case -EREMOTE:
 913		cifs_create_junction_fattr(&fattr, inode->i_sb);
 914		rc = 0;
 915		break;
 916	case -EOPNOTSUPP:
 917	case -EINVAL:
 918		/*
 919		 * FIXME: legacy server -- fall back to path-based call?
 920		 * for now, just skip revalidating and mark inode for
 921		 * immediate reval.
 922		 */
 923		rc = 0;
 924		CIFS_I(inode)->time = 0;
 925		goto cgfi_exit;
 926	default:
 927		goto cgfi_exit;
 928	}
 929
 930	/*
 931	 * don't bother with SFU junk here -- just mark inode as needing
 932	 * revalidation.
 933	 */
 934	fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
 935	fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
 936	/* if filetype is different, return error */
 937	rc = cifs_fattr_to_inode(inode, &fattr);
 938cgfi_exit:
 939	cifs_free_open_info(&data);
 
 940	free_xid(xid);
 941	return rc;
 942}
 943
 944/* Simple function to return a 64 bit hash of string.  Rarely called */
 945static __u64 simple_hashstr(const char *str)
 946{
 947	const __u64 hash_mult =  1125899906842597ULL; /* a big enough prime */
 948	__u64 hash = 0;
 949
 950	while (*str)
 951		hash = (hash + (__u64) *str++) * hash_mult;
 952
 953	return hash;
 954}
 955
 956#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
 957/**
 958 * cifs_backup_query_path_info - SMB1 fallback code to get ino
 959 *
 960 * Fallback code to get file metadata when we don't have access to
 961 * full_path (EACCES) and have backup creds.
 962 *
 963 * @xid:	transaction id used to identify original request in logs
 964 * @tcon:	information about the server share we have mounted
 965 * @sb:	the superblock stores info such as disk space available
 966 * @full_path:	name of the file we are getting the metadata for
 967 * @resp_buf:	will be set to cifs resp buf and needs to be freed with
 968 * 		cifs_buf_release() when done with @data
 969 * @data:	will be set to search info result buffer
 970 */
 971static int
 972cifs_backup_query_path_info(int xid,
 973			    struct cifs_tcon *tcon,
 974			    struct super_block *sb,
 975			    const char *full_path,
 976			    void **resp_buf,
 977			    FILE_ALL_INFO **data)
 978{
 979	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 980	struct cifs_search_info info = {0};
 981	u16 flags;
 982	int rc;
 983
 984	*resp_buf = NULL;
 985	info.endOfSearch = false;
 986	if (tcon->unix_ext)
 987		info.info_level = SMB_FIND_FILE_UNIX;
 988	else if ((tcon->ses->capabilities &
 989		  tcon->ses->server->vals->cap_nt_find) == 0)
 990		info.info_level = SMB_FIND_FILE_INFO_STANDARD;
 991	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
 992		info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
 993	else /* no srvino useful for fallback to some netapp */
 994		info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
 995
 996	flags = CIFS_SEARCH_CLOSE_ALWAYS |
 997		CIFS_SEARCH_CLOSE_AT_END |
 998		CIFS_SEARCH_BACKUP_SEARCH;
 999
1000	rc = CIFSFindFirst(xid, tcon, full_path,
1001			   cifs_sb, NULL, flags, &info, false);
1002	if (rc)
1003		return rc;
1004
1005	*resp_buf = (void *)info.ntwrk_buf_start;
1006	*data = (FILE_ALL_INFO *)info.srch_entries_start;
1007	return 0;
1008}
1009#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1010
1011static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1012			       struct inode **inode, const char *full_path,
1013			       struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1014{
1015	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1016	struct TCP_Server_Info *server = tcon->ses->server;
1017	int rc;
1018
1019	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1020		if (*inode)
1021			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1022		else
1023			fattr->cf_uniqueid = iunique(sb, ROOT_I);
1024		return;
1025	}
1026
1027	/*
1028	 * If we have an inode pass a NULL tcon to ensure we don't
1029	 * make a round trip to the server. This only works for SMB2+.
1030	 */
1031	rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1032				       &fattr->cf_uniqueid, data);
1033	if (rc) {
1034		/*
1035		 * If that fails reuse existing ino or generate one
1036		 * and disable server ones
1037		 */
1038		if (*inode)
1039			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1040		else {
1041			fattr->cf_uniqueid = iunique(sb, ROOT_I);
1042			cifs_autodisable_serverino(cifs_sb);
1043		}
1044		return;
1045	}
1046
1047	/* If no errors, check for zero root inode (invalid) */
1048	if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1049		cifs_dbg(FYI, "Invalid (0) inodenum\n");
1050		if (*inode) {
1051			/* reuse */
1052			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1053		} else {
1054			/* make an ino by hashing the UNC */
1055			fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1056			fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1057		}
1058	}
1059}
1060
1061static inline bool is_inode_cache_good(struct inode *ino)
1062{
1063	return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1064}
1065
1066static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1067				 struct super_block *sb,
1068				 const unsigned int xid,
1069				 struct cifs_tcon *tcon,
1070				 const char *full_path,
1071				 struct cifs_fattr *fattr)
1072{
1073	struct TCP_Server_Info *server = tcon->ses->server;
1074	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1075	struct kvec rsp_iov, *iov = NULL;
1076	int rsp_buftype = CIFS_NO_BUFFER;
1077	u32 tag = data->reparse.tag;
1078	int rc = 0;
1079
1080	if (!tag && server->ops->query_reparse_point) {
1081		rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1082						      full_path, &tag,
1083						      &rsp_iov, &rsp_buftype);
1084		if (!rc)
1085			iov = &rsp_iov;
1086	} else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1087		   data->reparse.io.iov.iov_base) {
1088		iov = &data->reparse.io.iov;
1089	}
1090
1091	rc = -EOPNOTSUPP;
1092	switch ((data->reparse.tag = tag)) {
1093	case 0: /* SMB1 symlink */
1094		if (server->ops->query_symlink) {
1095			rc = server->ops->query_symlink(xid, tcon,
1096							cifs_sb, full_path,
1097							&data->symlink_target);
1098		}
1099		break;
1100	case IO_REPARSE_TAG_MOUNT_POINT:
1101		cifs_create_junction_fattr(fattr, sb);
1102		rc = 0;
1103		goto out;
1104	default:
1105		/* Check for cached reparse point data */
1106		if (data->symlink_target || data->reparse.buf) {
1107			rc = 0;
1108		} else if (iov && server->ops->parse_reparse_point) {
1109			rc = server->ops->parse_reparse_point(cifs_sb,
1110							      iov, data);
1111		}
1112		break;
1113	}
1114
1115	if (tcon->posix_extensions)
1116		smb311_posix_info_to_fattr(fattr, data, sb);
1117	else
1118		cifs_open_info_to_fattr(fattr, data, sb);
1119out:
1120	fattr->cf_cifstag = data->reparse.tag;
1121	free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1122	return rc;
1123}
1124
1125static int cifs_get_fattr(struct cifs_open_info_data *data,
1126			  struct super_block *sb, int xid,
1127			  const struct cifs_fid *fid,
1128			  struct cifs_fattr *fattr,
1129			  struct inode **inode,
1130			  const char *full_path)
1131{
1132	struct cifs_open_info_data tmp_data = {};
1133	struct cifs_tcon *tcon;
1134	struct TCP_Server_Info *server;
1135	struct tcon_link *tlink;
1136	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1137	void *smb1_backup_rsp_buf = NULL;
1138	int rc = 0;
1139	int tmprc = 0;
1140
1141	tlink = cifs_sb_tlink(cifs_sb);
1142	if (IS_ERR(tlink))
1143		return PTR_ERR(tlink);
1144	tcon = tlink_tcon(tlink);
1145	server = tcon->ses->server;
1146
1147	/*
1148	 * 1. Fetch file metadata if not provided (data)
1149	 */
1150
1151	if (!data) {
1152		rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1153						  full_path, &tmp_data);
1154		data = &tmp_data;
1155	}
1156
1157	/*
1158	 * 2. Convert it to internal cifs metadata (fattr)
1159	 */
1160
1161	switch (rc) {
1162	case 0:
1163		/*
1164		 * If the file is a reparse point, it is more complicated
1165		 * since we have to check if its reparse tag matches a known
1166		 * special file type e.g. symlink or fifo or char etc.
1167		 */
1168		if (cifs_open_data_reparse(data)) {
1169			rc = reparse_info_to_fattr(data, sb, xid, tcon,
1170						   full_path, fattr);
1171		} else {
1172			cifs_open_info_to_fattr(fattr, data, sb);
1173		}
 
 
 
1174		break;
1175	case -EREMOTE:
1176		/* DFS link, no metadata available on this server */
1177		cifs_create_junction_fattr(fattr, sb);
1178		rc = 0;
1179		break;
1180	case -EACCES:
1181#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1182		/*
1183		 * perm errors, try again with backup flags if possible
1184		 *
1185		 * For SMB2 and later the backup intent flag
1186		 * is already sent if needed on open and there
1187		 * is no path based FindFirst operation to use
1188		 * to retry with
1189		 */
1190		if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1191			/* for easier reading */
1192			FILE_ALL_INFO *fi;
1193			FILE_DIRECTORY_INFO *fdi;
1194			SEARCH_ID_FULL_DIR_INFO *si;
1195
1196			rc = cifs_backup_query_path_info(xid, tcon, sb,
1197							 full_path,
1198							 &smb1_backup_rsp_buf,
1199							 &fi);
1200			if (rc)
1201				goto out;
1202
1203			move_cifs_info_to_smb2(&data->fi, fi);
1204			fdi = (FILE_DIRECTORY_INFO *)fi;
1205			si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1206
1207			cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1208			fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1209			/* uniqueid set, skip get inum step */
1210			goto handle_mnt_opt;
1211		} else {
1212			/* nothing we can do, bail out */
1213			goto out;
1214		}
1215#else
1216		goto out;
1217#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1218		break;
1219	default:
1220		cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1221		goto out;
1222	}
1223
1224	/*
1225	 * 3. Get or update inode number (fattr->cf_uniqueid)
1226	 */
1227
1228	cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1229
1230	/*
1231	 * 4. Tweak fattr based on mount options
1232	 */
1233#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1234handle_mnt_opt:
1235#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1236	/* query for SFU type info if supported and needed */
1237	if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1238	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1239		tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1240		if (tmprc)
1241			cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1242	}
1243
1244	/* fill in 0777 bits from ACL */
1245	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1246		rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1247				       true, full_path, fid);
1248		if (rc == -EREMOTE)
1249			rc = 0;
1250		if (rc) {
1251			cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1252				 __func__, rc);
1253			goto out;
1254		}
1255	} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1256		rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1257				       false, full_path, fid);
1258		if (rc == -EREMOTE)
1259			rc = 0;
1260		if (rc) {
1261			cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1262				 __func__, rc);
1263			goto out;
1264		}
1265	}
1266
1267	/* fill in remaining high mode bits e.g. SUID, VTX */
1268	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1269		cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1270
1271	/* check for Minshall+French symlinks */
1272	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1273		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1274		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1275	}
1276
1277out:
1278	cifs_buf_release(smb1_backup_rsp_buf);
1279	cifs_put_tlink(tlink);
1280	cifs_free_open_info(&tmp_data);
1281	return rc;
1282}
1283
1284int cifs_get_inode_info(struct inode **inode,
1285			const char *full_path,
1286			struct cifs_open_info_data *data,
1287			struct super_block *sb, int xid,
1288			const struct cifs_fid *fid)
1289{
1290	struct cifs_fattr fattr = {};
1291	int rc;
1292
1293	if (is_inode_cache_good(*inode)) {
1294		cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1295		return 0;
1296	}
1297
1298	rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1299	if (rc)
1300		goto out;
1301
1302	rc = update_inode_info(sb, &fattr, inode);
1303out:
1304	kfree(fattr.cf_symlink_target);
1305	return rc;
1306}
1307
1308static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1309				  struct cifs_fattr *fattr,
1310				  const char *full_path,
1311				  struct super_block *sb,
1312				  const unsigned int xid)
1313{
1314	struct cifs_open_info_data tmp_data = {};
1315	struct TCP_Server_Info *server;
1316	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1317	struct cifs_tcon *tcon;
1318	struct tcon_link *tlink;
1319	int tmprc;
1320	int rc = 0;
1321
1322	tlink = cifs_sb_tlink(cifs_sb);
1323	if (IS_ERR(tlink))
1324		return PTR_ERR(tlink);
1325	tcon = tlink_tcon(tlink);
1326	server = tcon->ses->server;
1327
1328	/*
1329	 * 1. Fetch file metadata if not provided (data)
1330	 */
1331	if (!data) {
1332		rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1333						  full_path, &tmp_data);
1334		data = &tmp_data;
1335	}
1336
1337	/*
1338	 * 2. Convert it to internal cifs metadata (fattr)
1339	 */
1340
1341	switch (rc) {
1342	case 0:
1343		if (cifs_open_data_reparse(data)) {
1344			rc = reparse_info_to_fattr(data, sb, xid, tcon,
1345						   full_path, fattr);
1346		} else {
1347			smb311_posix_info_to_fattr(fattr, data, sb);
1348		}
1349		break;
1350	case -EREMOTE:
1351		/* DFS link, no metadata available on this server */
1352		cifs_create_junction_fattr(fattr, sb);
1353		rc = 0;
1354		break;
1355	case -EACCES:
1356		/*
1357		 * For SMB2 and later the backup intent flag
1358		 * is already sent if needed on open and there
1359		 * is no path based FindFirst operation to use
1360		 * to retry with so nothing we can do, bail out
1361		 */
1362		goto out;
1363	default:
1364		cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1365		goto out;
1366	}
1367
1368	/*
1369	 * 3. Tweak fattr based on mount options
1370	 */
1371	/* check for Minshall+French symlinks */
1372	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1373		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1374		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1375	}
1376
1377out:
1378	cifs_put_tlink(tlink);
1379	cifs_free_open_info(data);
1380	return rc;
1381}
1382
1383int smb311_posix_get_inode_info(struct inode **inode,
1384				const char *full_path,
1385				struct cifs_open_info_data *data,
1386				struct super_block *sb,
1387				const unsigned int xid)
1388{
1389	struct cifs_fattr fattr = {};
1390	int rc;
1391
1392	if (is_inode_cache_good(*inode)) {
1393		cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1394		return 0;
1395	}
1396
1397	rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1398	if (rc)
1399		goto out;
1400
1401	rc = update_inode_info(sb, &fattr, inode);
 
 
1402out:
1403	kfree(fattr.cf_symlink_target);
1404	return rc;
1405}
1406
1407static const struct inode_operations cifs_ipc_inode_ops = {
1408	.lookup = cifs_lookup,
1409};
1410
1411static int
1412cifs_find_inode(struct inode *inode, void *opaque)
1413{
1414	struct cifs_fattr *fattr = opaque;
1415
 
 
1416	/* don't match inode with different uniqueid */
1417	if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1418		return 0;
1419
1420	/* use createtime like an i_generation field */
1421	if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1422		return 0;
1423
1424	/* don't match inode of different type */
1425	if (inode_wrong_type(inode, fattr->cf_mode))
1426		return 0;
1427
1428	/* if it's not a directory or has no dentries, then flag it */
1429	if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1430		fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1431
1432	return 1;
1433}
1434
1435static int
1436cifs_init_inode(struct inode *inode, void *opaque)
1437{
1438	struct cifs_fattr *fattr = opaque;
1439
1440	CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1441	CIFS_I(inode)->createtime = fattr->cf_createtime;
1442	return 0;
1443}
1444
1445/*
1446 * walk dentry list for an inode and report whether it has aliases that
1447 * are hashed. We use this to determine if a directory inode can actually
1448 * be used.
1449 */
1450static bool
1451inode_has_hashed_dentries(struct inode *inode)
1452{
1453	struct dentry *dentry;
1454
1455	spin_lock(&inode->i_lock);
1456	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1457		if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1458			spin_unlock(&inode->i_lock);
1459			return true;
1460		}
1461	}
1462	spin_unlock(&inode->i_lock);
1463	return false;
1464}
1465
1466/* Given fattrs, get a corresponding inode */
1467struct inode *
1468cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1469{
1470	unsigned long hash;
1471	struct inode *inode;
1472
1473retry_iget5_locked:
1474	cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1475
1476	/* hash down to 32-bits on 32-bit arch */
1477	hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1478
1479	inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1480	if (inode) {
1481		/* was there a potentially problematic inode collision? */
1482		if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1483			fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1484
1485			if (inode_has_hashed_dentries(inode)) {
1486				cifs_autodisable_serverino(CIFS_SB(sb));
1487				iput(inode);
1488				fattr->cf_uniqueid = iunique(sb, ROOT_I);
1489				goto retry_iget5_locked;
1490			}
1491		}
1492
1493		/* can't fail - see cifs_find_inode() */
1494		cifs_fattr_to_inode(inode, fattr);
1495		if (sb->s_flags & SB_NOATIME)
1496			inode->i_flags |= S_NOATIME | S_NOCMTIME;
1497		if (inode->i_state & I_NEW) {
1498			inode->i_ino = hash;
1499			cifs_fscache_get_inode_cookie(inode);
1500			unlock_new_inode(inode);
1501		}
1502	}
1503
1504	return inode;
1505}
1506
1507/* gets root inode */
1508struct inode *cifs_root_iget(struct super_block *sb)
1509{
1510	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1511	struct cifs_fattr fattr = {};
1512	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1513	struct inode *inode = NULL;
1514	unsigned int xid;
1515	char *path = NULL;
1516	int len;
1517	int rc;
1518
1519	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1520	    && cifs_sb->prepath) {
1521		len = strlen(cifs_sb->prepath);
1522		path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1523		if (path == NULL)
1524			return ERR_PTR(-ENOMEM);
1525		path[0] = '/';
1526		memcpy(path+1, cifs_sb->prepath, len);
1527	} else {
1528		path = kstrdup("", GFP_KERNEL);
1529		if (path == NULL)
1530			return ERR_PTR(-ENOMEM);
1531	}
1532
1533	xid = get_xid();
1534	if (tcon->unix_ext) {
1535		rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1536		/* some servers mistakenly claim POSIX support */
1537		if (rc != -EOPNOTSUPP)
1538			goto iget_root;
1539		cifs_dbg(VFS, "server does not support POSIX extensions\n");
1540		tcon->unix_ext = false;
1541	}
1542
1543	convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1544	if (tcon->posix_extensions)
1545		rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1546	else
1547		rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1548
1549iget_root:
1550	if (!rc) {
1551		if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1552			fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1553			cifs_autodisable_serverino(cifs_sb);
1554		}
1555		inode = cifs_iget(sb, &fattr);
1556	}
1557
1558	if (!inode) {
1559		inode = ERR_PTR(rc);
1560		goto out;
1561	}
1562
 
 
 
1563	if (rc && tcon->pipe) {
1564		cifs_dbg(FYI, "ipc connection - fake read inode\n");
1565		spin_lock(&inode->i_lock);
1566		inode->i_mode |= S_IFDIR;
1567		set_nlink(inode, 2);
1568		inode->i_op = &cifs_ipc_inode_ops;
1569		inode->i_fop = &simple_dir_operations;
1570		inode->i_uid = cifs_sb->ctx->linux_uid;
1571		inode->i_gid = cifs_sb->ctx->linux_gid;
1572		spin_unlock(&inode->i_lock);
1573	} else if (rc) {
1574		iget_failed(inode);
1575		inode = ERR_PTR(rc);
1576	}
1577
1578out:
1579	kfree(path);
1580	free_xid(xid);
1581	kfree(fattr.cf_symlink_target);
1582	return inode;
1583}
1584
1585int
1586cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1587		   const char *full_path, __u32 dosattr)
1588{
1589	bool set_time = false;
1590	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1591	struct TCP_Server_Info *server;
1592	FILE_BASIC_INFO	info_buf;
1593
1594	if (attrs == NULL)
1595		return -EINVAL;
1596
1597	server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1598	if (!server->ops->set_file_info)
1599		return -ENOSYS;
1600
1601	info_buf.Pad = 0;
1602
1603	if (attrs->ia_valid & ATTR_ATIME) {
1604		set_time = true;
1605		info_buf.LastAccessTime =
1606			cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1607	} else
1608		info_buf.LastAccessTime = 0;
1609
1610	if (attrs->ia_valid & ATTR_MTIME) {
1611		set_time = true;
1612		info_buf.LastWriteTime =
1613		    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1614	} else
1615		info_buf.LastWriteTime = 0;
1616
1617	/*
1618	 * Samba throws this field away, but windows may actually use it.
1619	 * Do not set ctime unless other time stamps are changed explicitly
1620	 * (i.e. by utimes()) since we would then have a mix of client and
1621	 * server times.
1622	 */
1623	if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1624		cifs_dbg(FYI, "CIFS - CTIME changed\n");
1625		info_buf.ChangeTime =
1626		    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1627	} else
1628		info_buf.ChangeTime = 0;
1629
1630	info_buf.CreationTime = 0;	/* don't change */
1631	info_buf.Attributes = cpu_to_le32(dosattr);
1632
1633	return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1634}
1635
1636#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1637/*
1638 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1639 * and rename it to a random name that hopefully won't conflict with
1640 * anything else.
1641 */
1642int
1643cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1644			   const unsigned int xid)
1645{
1646	int oplock = 0;
1647	int rc;
1648	struct cifs_fid fid;
1649	struct cifs_open_parms oparms;
1650	struct inode *inode = d_inode(dentry);
1651	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1652	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1653	struct tcon_link *tlink;
1654	struct cifs_tcon *tcon;
1655	__u32 dosattr, origattr;
1656	FILE_BASIC_INFO *info_buf = NULL;
1657
1658	tlink = cifs_sb_tlink(cifs_sb);
1659	if (IS_ERR(tlink))
1660		return PTR_ERR(tlink);
1661	tcon = tlink_tcon(tlink);
1662
1663	/*
1664	 * We cannot rename the file if the server doesn't support
1665	 * CAP_INFOLEVEL_PASSTHRU
1666	 */
1667	if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1668		rc = -EBUSY;
1669		goto out;
1670	}
1671
1672	oparms = (struct cifs_open_parms) {
1673		.tcon = tcon,
1674		.cifs_sb = cifs_sb,
1675		.desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1676		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1677		.disposition = FILE_OPEN,
1678		.path = full_path,
1679		.fid = &fid,
1680	};
1681
1682	rc = CIFS_open(xid, &oparms, &oplock, NULL);
1683	if (rc != 0)
1684		goto out;
1685
1686	origattr = cifsInode->cifsAttrs;
1687	if (origattr == 0)
1688		origattr |= ATTR_NORMAL;
1689
1690	dosattr = origattr & ~ATTR_READONLY;
1691	if (dosattr == 0)
1692		dosattr |= ATTR_NORMAL;
1693	dosattr |= ATTR_HIDDEN;
1694
1695	/* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1696	if (dosattr != origattr) {
1697		info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1698		if (info_buf == NULL) {
1699			rc = -ENOMEM;
1700			goto out_close;
1701		}
1702		info_buf->Attributes = cpu_to_le32(dosattr);
1703		rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1704					current->tgid);
1705		/* although we would like to mark the file hidden
1706 		   if that fails we will still try to rename it */
1707		if (!rc)
1708			cifsInode->cifsAttrs = dosattr;
1709		else
1710			dosattr = origattr; /* since not able to change them */
1711	}
1712
1713	/* rename the file */
1714	rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1715				   cifs_sb->local_nls,
1716				   cifs_remap(cifs_sb));
1717	if (rc != 0) {
1718		rc = -EBUSY;
1719		goto undo_setattr;
1720	}
1721
1722	/* try to set DELETE_ON_CLOSE */
1723	if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1724		rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1725					       current->tgid);
1726		/*
1727		 * some samba versions return -ENOENT when we try to set the
1728		 * file disposition here. Likely a samba bug, but work around
1729		 * it for now. This means that some cifsXXX files may hang
1730		 * around after they shouldn't.
1731		 *
1732		 * BB: remove this hack after more servers have the fix
1733		 */
1734		if (rc == -ENOENT)
1735			rc = 0;
1736		else if (rc != 0) {
1737			rc = -EBUSY;
1738			goto undo_rename;
1739		}
1740		set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1741	}
1742
1743out_close:
1744	CIFSSMBClose(xid, tcon, fid.netfid);
1745out:
1746	kfree(info_buf);
1747	cifs_put_tlink(tlink);
1748	return rc;
1749
1750	/*
1751	 * reset everything back to the original state. Don't bother
1752	 * dealing with errors here since we can't do anything about
1753	 * them anyway.
1754	 */
1755undo_rename:
1756	CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1757				cifs_sb->local_nls, cifs_remap(cifs_sb));
1758undo_setattr:
1759	if (dosattr != origattr) {
1760		info_buf->Attributes = cpu_to_le32(origattr);
1761		if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1762					current->tgid))
1763			cifsInode->cifsAttrs = origattr;
1764	}
1765
1766	goto out_close;
1767}
1768#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1769
1770/* copied from fs/nfs/dir.c with small changes */
1771static void
1772cifs_drop_nlink(struct inode *inode)
1773{
1774	spin_lock(&inode->i_lock);
1775	if (inode->i_nlink > 0)
1776		drop_nlink(inode);
1777	spin_unlock(&inode->i_lock);
1778}
1779
1780/*
1781 * If d_inode(dentry) is null (usually meaning the cached dentry
1782 * is a negative dentry) then we would attempt a standard SMB delete, but
1783 * if that fails we can not attempt the fall back mechanisms on EACCES
1784 * but will return the EACCES to the caller. Note that the VFS does not call
1785 * unlink on negative dentries currently.
1786 */
1787int cifs_unlink(struct inode *dir, struct dentry *dentry)
1788{
1789	int rc = 0;
1790	unsigned int xid;
1791	const char *full_path;
1792	void *page;
1793	struct inode *inode = d_inode(dentry);
1794	struct cifsInodeInfo *cifs_inode;
1795	struct super_block *sb = dir->i_sb;
1796	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1797	struct tcon_link *tlink;
1798	struct cifs_tcon *tcon;
1799	struct TCP_Server_Info *server;
1800	struct iattr *attrs = NULL;
1801	__u32 dosattr = 0, origattr = 0;
1802
1803	cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1804
1805	if (unlikely(cifs_forced_shutdown(cifs_sb)))
1806		return -EIO;
1807
1808	tlink = cifs_sb_tlink(cifs_sb);
1809	if (IS_ERR(tlink))
1810		return PTR_ERR(tlink);
1811	tcon = tlink_tcon(tlink);
1812	server = tcon->ses->server;
1813
1814	xid = get_xid();
1815	page = alloc_dentry_path();
1816
1817	if (tcon->nodelete) {
1818		rc = -EACCES;
1819		goto unlink_out;
1820	}
1821
1822	/* Unlink can be called from rename so we can not take the
1823	 * sb->s_vfs_rename_mutex here */
1824	full_path = build_path_from_dentry(dentry, page);
1825	if (IS_ERR(full_path)) {
1826		rc = PTR_ERR(full_path);
1827		goto unlink_out;
1828	}
1829
1830	cifs_close_deferred_file_under_dentry(tcon, full_path);
1831#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1832	if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1833				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1834		rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1835			SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1836			cifs_remap(cifs_sb));
1837		cifs_dbg(FYI, "posix del rc %d\n", rc);
1838		if ((rc == 0) || (rc == -ENOENT))
1839			goto psx_del_no_retry;
1840	}
1841#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1842
1843retry_std_delete:
1844	if (!server->ops->unlink) {
1845		rc = -ENOSYS;
1846		goto psx_del_no_retry;
1847	}
1848
1849	rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1850
1851psx_del_no_retry:
1852	if (!rc) {
1853		if (inode)
 
1854			cifs_drop_nlink(inode);
 
1855	} else if (rc == -ENOENT) {
1856		d_drop(dentry);
1857	} else if (rc == -EBUSY) {
1858		if (server->ops->rename_pending_delete) {
1859			rc = server->ops->rename_pending_delete(full_path,
1860								dentry, xid);
1861			if (rc == 0)
 
1862				cifs_drop_nlink(inode);
 
1863		}
1864	} else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1865		attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1866		if (attrs == NULL) {
1867			rc = -ENOMEM;
1868			goto out_reval;
1869		}
1870
1871		/* try to reset dos attributes */
1872		cifs_inode = CIFS_I(inode);
1873		origattr = cifs_inode->cifsAttrs;
1874		if (origattr == 0)
1875			origattr |= ATTR_NORMAL;
1876		dosattr = origattr & ~ATTR_READONLY;
1877		if (dosattr == 0)
1878			dosattr |= ATTR_NORMAL;
1879		dosattr |= ATTR_HIDDEN;
1880
1881		rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1882		if (rc != 0)
1883			goto out_reval;
1884
1885		goto retry_std_delete;
1886	}
1887
1888	/* undo the setattr if we errored out and it's needed */
1889	if (rc != 0 && dosattr != 0)
1890		cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1891
1892out_reval:
1893	if (inode) {
1894		cifs_inode = CIFS_I(inode);
1895		cifs_inode->time = 0;	/* will force revalidate to get info
1896					   when needed */
1897		inode_set_ctime_current(inode);
1898	}
1899	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1900	cifs_inode = CIFS_I(dir);
1901	CIFS_I(dir)->time = 0;	/* force revalidate of dir as well */
1902unlink_out:
1903	free_dentry_path(page);
1904	kfree(attrs);
1905	free_xid(xid);
1906	cifs_put_tlink(tlink);
1907	return rc;
1908}
1909
1910static int
1911cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1912		 const char *full_path, struct cifs_sb_info *cifs_sb,
1913		 struct cifs_tcon *tcon, const unsigned int xid)
1914{
1915	int rc = 0;
1916	struct inode *inode = NULL;
1917
1918	if (tcon->posix_extensions) {
1919		rc = smb311_posix_get_inode_info(&inode, full_path,
1920						 NULL, parent->i_sb, xid);
1921#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1922	} else if (tcon->unix_ext) {
1923		rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1924					      xid);
1925#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1926	} else {
1927		rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1928					 xid, NULL);
1929	}
1930
1931	if (rc)
1932		return rc;
1933
1934	if (!S_ISDIR(inode->i_mode)) {
1935		/*
1936		 * mkdir succeeded, but another client has managed to remove the
1937		 * sucker and replace it with non-directory.  Return success,
1938		 * but don't leave the child in dcache.
1939		 */
1940		 iput(inode);
1941		 d_drop(dentry);
1942		 return 0;
1943	}
1944	/*
1945	 * setting nlink not necessary except in cases where we failed to get it
1946	 * from the server or was set bogus. Also, since this is a brand new
1947	 * inode, no need to grab the i_lock before setting the i_nlink.
1948	 */
1949	if (inode->i_nlink < 2)
1950		set_nlink(inode, 2);
1951	mode &= ~current_umask();
1952	/* must turn on setgid bit if parent dir has it */
1953	if (parent->i_mode & S_ISGID)
1954		mode |= S_ISGID;
1955
1956#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1957	if (tcon->unix_ext) {
1958		struct cifs_unix_set_info_args args = {
1959			.mode	= mode,
1960			.ctime	= NO_CHANGE_64,
1961			.atime	= NO_CHANGE_64,
1962			.mtime	= NO_CHANGE_64,
1963			.device	= 0,
1964		};
1965		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1966			args.uid = current_fsuid();
1967			if (parent->i_mode & S_ISGID)
1968				args.gid = parent->i_gid;
1969			else
1970				args.gid = current_fsgid();
1971		} else {
1972			args.uid = INVALID_UID; /* no change */
1973			args.gid = INVALID_GID; /* no change */
1974		}
1975		CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1976				       cifs_sb->local_nls,
1977				       cifs_remap(cifs_sb));
1978	} else {
1979#else
1980	{
1981#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1982		struct TCP_Server_Info *server = tcon->ses->server;
1983		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1984		    (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1985			server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1986						   tcon, xid);
1987		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1988			inode->i_mode = (mode | S_IFDIR);
1989
1990		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1991			inode->i_uid = current_fsuid();
1992			if (inode->i_mode & S_ISGID)
1993				inode->i_gid = parent->i_gid;
1994			else
1995				inode->i_gid = current_fsgid();
1996		}
1997	}
1998	d_instantiate(dentry, inode);
1999	return 0;
2000}
2001
2002#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2003static int
2004cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
2005		 const char *full_path, struct cifs_sb_info *cifs_sb,
2006		 struct cifs_tcon *tcon, const unsigned int xid)
2007{
2008	int rc = 0;
2009	u32 oplock = 0;
2010	FILE_UNIX_BASIC_INFO *info = NULL;
2011	struct inode *newinode = NULL;
2012	struct cifs_fattr fattr;
2013
2014	info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
2015	if (info == NULL) {
2016		rc = -ENOMEM;
2017		goto posix_mkdir_out;
2018	}
2019
2020	mode &= ~current_umask();
2021	rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
2022			     NULL /* netfid */, info, &oplock, full_path,
2023			     cifs_sb->local_nls, cifs_remap(cifs_sb));
2024	if (rc == -EOPNOTSUPP)
2025		goto posix_mkdir_out;
2026	else if (rc) {
2027		cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
2028		d_drop(dentry);
2029		goto posix_mkdir_out;
2030	}
2031
2032	if (info->Type == cpu_to_le32(-1))
2033		/* no return info, go query for it */
2034		goto posix_mkdir_get_info;
2035	/*
2036	 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2037	 * need to set uid/gid.
2038	 */
2039
2040	cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2041	cifs_fill_uniqueid(inode->i_sb, &fattr);
2042	newinode = cifs_iget(inode->i_sb, &fattr);
2043	if (!newinode)
2044		goto posix_mkdir_get_info;
2045
2046	d_instantiate(dentry, newinode);
2047
2048#ifdef CONFIG_CIFS_DEBUG2
2049	cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2050		 dentry, dentry, newinode);
2051
2052	if (newinode->i_nlink != 2)
2053		cifs_dbg(FYI, "unexpected number of links %d\n",
2054			 newinode->i_nlink);
2055#endif
2056
2057posix_mkdir_out:
2058	kfree(info);
2059	return rc;
2060posix_mkdir_get_info:
2061	rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2062			      xid);
2063	goto posix_mkdir_out;
2064}
2065#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2066
2067int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2068	       struct dentry *direntry, umode_t mode)
2069{
2070	int rc = 0;
2071	unsigned int xid;
2072	struct cifs_sb_info *cifs_sb;
2073	struct tcon_link *tlink;
2074	struct cifs_tcon *tcon;
2075	struct TCP_Server_Info *server;
2076	const char *full_path;
2077	void *page;
2078
2079	cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2080		 mode, inode);
2081
2082	cifs_sb = CIFS_SB(inode->i_sb);
2083	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2084		return -EIO;
2085	tlink = cifs_sb_tlink(cifs_sb);
2086	if (IS_ERR(tlink))
2087		return PTR_ERR(tlink);
2088	tcon = tlink_tcon(tlink);
2089
2090	xid = get_xid();
2091
2092	page = alloc_dentry_path();
2093	full_path = build_path_from_dentry(direntry, page);
2094	if (IS_ERR(full_path)) {
2095		rc = PTR_ERR(full_path);
2096		goto mkdir_out;
2097	}
2098
2099	server = tcon->ses->server;
2100
2101	if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2102		rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2103					      cifs_sb);
2104		d_drop(direntry); /* for time being always refresh inode info */
2105		goto mkdir_out;
2106	}
2107
2108#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2109	if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2110				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2111		rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2112				      tcon, xid);
2113		if (rc != -EOPNOTSUPP)
2114			goto mkdir_out;
2115	}
2116#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2117
2118	if (!server->ops->mkdir) {
2119		rc = -ENOSYS;
2120		goto mkdir_out;
2121	}
2122
2123	/* BB add setting the equivalent of mode via CreateX w/ACLs */
2124	rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2125	if (rc) {
2126		cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2127		d_drop(direntry);
2128		goto mkdir_out;
2129	}
2130
2131	/* TODO: skip this for smb2/smb3 */
2132	rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2133			      xid);
2134mkdir_out:
2135	/*
2136	 * Force revalidate to get parent dir info when needed since cached
2137	 * attributes are invalid now.
2138	 */
2139	CIFS_I(inode)->time = 0;
2140	free_dentry_path(page);
2141	free_xid(xid);
2142	cifs_put_tlink(tlink);
2143	return rc;
2144}
2145
2146int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2147{
2148	int rc = 0;
2149	unsigned int xid;
2150	struct cifs_sb_info *cifs_sb;
2151	struct tcon_link *tlink;
2152	struct cifs_tcon *tcon;
2153	struct TCP_Server_Info *server;
2154	const char *full_path;
2155	void *page = alloc_dentry_path();
2156	struct cifsInodeInfo *cifsInode;
2157
2158	cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2159
2160	xid = get_xid();
2161
2162	full_path = build_path_from_dentry(direntry, page);
2163	if (IS_ERR(full_path)) {
2164		rc = PTR_ERR(full_path);
2165		goto rmdir_exit;
2166	}
2167
2168	cifs_sb = CIFS_SB(inode->i_sb);
2169	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2170		rc = -EIO;
2171		goto rmdir_exit;
2172	}
2173
2174	tlink = cifs_sb_tlink(cifs_sb);
2175	if (IS_ERR(tlink)) {
2176		rc = PTR_ERR(tlink);
2177		goto rmdir_exit;
2178	}
2179	tcon = tlink_tcon(tlink);
2180	server = tcon->ses->server;
2181
2182	if (!server->ops->rmdir) {
2183		rc = -ENOSYS;
2184		cifs_put_tlink(tlink);
2185		goto rmdir_exit;
2186	}
2187
2188	if (tcon->nodelete) {
2189		rc = -EACCES;
2190		cifs_put_tlink(tlink);
2191		goto rmdir_exit;
2192	}
2193
2194	rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2195	cifs_put_tlink(tlink);
2196
2197	if (!rc) {
2198		spin_lock(&d_inode(direntry)->i_lock);
2199		i_size_write(d_inode(direntry), 0);
2200		clear_nlink(d_inode(direntry));
2201		spin_unlock(&d_inode(direntry)->i_lock);
2202	}
2203
2204	cifsInode = CIFS_I(d_inode(direntry));
2205	/* force revalidate to go get info when needed */
2206	cifsInode->time = 0;
2207
2208	cifsInode = CIFS_I(inode);
2209	/*
2210	 * Force revalidate to get parent dir info when needed since cached
2211	 * attributes are invalid now.
2212	 */
2213	cifsInode->time = 0;
2214
2215	inode_set_ctime_current(d_inode(direntry));
2216	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2217
2218rmdir_exit:
2219	free_dentry_path(page);
2220	free_xid(xid);
2221	return rc;
2222}
2223
2224static int
2225cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2226	       const char *from_path, struct dentry *to_dentry,
2227	       const char *to_path)
2228{
2229	struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2230	struct tcon_link *tlink;
2231	struct cifs_tcon *tcon;
2232	struct TCP_Server_Info *server;
2233#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2234	struct cifs_fid fid;
2235	struct cifs_open_parms oparms;
2236	int oplock;
2237#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2238	int rc;
2239
2240	tlink = cifs_sb_tlink(cifs_sb);
2241	if (IS_ERR(tlink))
2242		return PTR_ERR(tlink);
2243	tcon = tlink_tcon(tlink);
2244	server = tcon->ses->server;
2245
2246	if (!server->ops->rename)
2247		return -ENOSYS;
2248
2249	/* try path-based rename first */
2250	rc = server->ops->rename(xid, tcon, from_dentry,
2251				 from_path, to_path, cifs_sb);
2252
2253	/*
2254	 * Don't bother with rename by filehandle unless file is busy and
2255	 * source. Note that cross directory moves do not work with
2256	 * rename by filehandle to various Windows servers.
2257	 */
2258	if (rc == 0 || rc != -EBUSY)
2259		goto do_rename_exit;
2260
2261	/* Don't fall back to using SMB on SMB 2+ mount */
2262	if (server->vals->protocol_id != 0)
2263		goto do_rename_exit;
2264
2265#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2266	/* open-file renames don't work across directories */
2267	if (to_dentry->d_parent != from_dentry->d_parent)
2268		goto do_rename_exit;
2269
2270	oparms = (struct cifs_open_parms) {
2271		.tcon = tcon,
2272		.cifs_sb = cifs_sb,
2273		/* open the file to be renamed -- we need DELETE perms */
2274		.desired_access = DELETE,
2275		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2276		.disposition = FILE_OPEN,
2277		.path = from_path,
2278		.fid = &fid,
2279	};
2280
2281	rc = CIFS_open(xid, &oparms, &oplock, NULL);
2282	if (rc == 0) {
2283		rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2284				(const char *) to_dentry->d_name.name,
2285				cifs_sb->local_nls, cifs_remap(cifs_sb));
2286		CIFSSMBClose(xid, tcon, fid.netfid);
2287	}
2288#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2289do_rename_exit:
2290	if (rc == 0)
2291		d_move(from_dentry, to_dentry);
2292	cifs_put_tlink(tlink);
2293	return rc;
2294}
2295
2296int
2297cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2298	     struct dentry *source_dentry, struct inode *target_dir,
2299	     struct dentry *target_dentry, unsigned int flags)
2300{
2301	const char *from_name, *to_name;
2302	void *page1, *page2;
2303	struct cifs_sb_info *cifs_sb;
2304	struct tcon_link *tlink;
2305	struct cifs_tcon *tcon;
2306	unsigned int xid;
2307	int rc, tmprc;
2308	int retry_count = 0;
2309	FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2310#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2311	FILE_UNIX_BASIC_INFO *info_buf_target;
2312#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2313
2314	if (flags & ~RENAME_NOREPLACE)
2315		return -EINVAL;
2316
2317	cifs_sb = CIFS_SB(source_dir->i_sb);
2318	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2319		return -EIO;
2320
2321	tlink = cifs_sb_tlink(cifs_sb);
2322	if (IS_ERR(tlink))
2323		return PTR_ERR(tlink);
2324	tcon = tlink_tcon(tlink);
2325
2326	page1 = alloc_dentry_path();
2327	page2 = alloc_dentry_path();
2328	xid = get_xid();
2329
2330	from_name = build_path_from_dentry(source_dentry, page1);
2331	if (IS_ERR(from_name)) {
2332		rc = PTR_ERR(from_name);
2333		goto cifs_rename_exit;
2334	}
2335
2336	to_name = build_path_from_dentry(target_dentry, page2);
2337	if (IS_ERR(to_name)) {
2338		rc = PTR_ERR(to_name);
2339		goto cifs_rename_exit;
2340	}
2341
2342	cifs_close_deferred_file_under_dentry(tcon, from_name);
2343	if (d_inode(target_dentry) != NULL)
2344		cifs_close_deferred_file_under_dentry(tcon, to_name);
2345
2346	rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2347			    to_name);
2348
2349	if (rc == -EACCES) {
2350		while (retry_count < 3) {
2351			cifs_close_all_deferred_files(tcon);
2352			rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2353					    to_name);
2354			if (rc != -EACCES)
2355				break;
2356			retry_count++;
2357		}
2358	}
2359
2360	/*
2361	 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2362	 */
2363	if (flags & RENAME_NOREPLACE)
2364		goto cifs_rename_exit;
2365
2366#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2367	if (rc == -EEXIST && tcon->unix_ext) {
2368		/*
2369		 * Are src and dst hardlinks of same inode? We can only tell
2370		 * with unix extensions enabled.
2371		 */
2372		info_buf_source =
2373			kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2374					GFP_KERNEL);
2375		if (info_buf_source == NULL) {
2376			rc = -ENOMEM;
2377			goto cifs_rename_exit;
2378		}
2379
2380		info_buf_target = info_buf_source + 1;
2381		tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2382					     info_buf_source,
2383					     cifs_sb->local_nls,
2384					     cifs_remap(cifs_sb));
2385		if (tmprc != 0)
2386			goto unlink_target;
2387
2388		tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2389					     info_buf_target,
2390					     cifs_sb->local_nls,
2391					     cifs_remap(cifs_sb));
2392
2393		if (tmprc == 0 && (info_buf_source->UniqueId ==
2394				   info_buf_target->UniqueId)) {
2395			/* same file, POSIX says that this is a noop */
2396			rc = 0;
2397			goto cifs_rename_exit;
2398		}
2399	}
2400	/*
2401	 * else ... BB we could add the same check for Windows by
2402	 * checking the UniqueId via FILE_INTERNAL_INFO
2403	 */
2404
2405unlink_target:
2406#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2407
2408	/* Try unlinking the target dentry if it's not negative */
2409	if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2410		if (d_is_dir(target_dentry))
2411			tmprc = cifs_rmdir(target_dir, target_dentry);
2412		else
2413			tmprc = cifs_unlink(target_dir, target_dentry);
2414		if (tmprc)
2415			goto cifs_rename_exit;
2416		rc = cifs_do_rename(xid, source_dentry, from_name,
2417				    target_dentry, to_name);
2418	}
2419
2420	/* force revalidate to go get info when needed */
2421	CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2422
2423cifs_rename_exit:
2424	kfree(info_buf_source);
2425	free_dentry_path(page2);
2426	free_dentry_path(page1);
2427	free_xid(xid);
2428	cifs_put_tlink(tlink);
2429	return rc;
2430}
2431
2432static bool
2433cifs_dentry_needs_reval(struct dentry *dentry)
2434{
2435	struct inode *inode = d_inode(dentry);
2436	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2437	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2438	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2439	struct cached_fid *cfid = NULL;
2440
2441	if (cifs_i->time == 0)
2442		return true;
2443
2444	if (CIFS_CACHE_READ(cifs_i))
2445		return false;
2446
2447	if (!lookupCacheEnabled)
2448		return true;
2449
2450	if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2451		spin_lock(&cfid->fid_lock);
2452		if (cfid->time && cifs_i->time > cfid->time) {
2453			spin_unlock(&cfid->fid_lock);
2454			close_cached_dir(cfid);
2455			return false;
2456		}
2457		spin_unlock(&cfid->fid_lock);
2458		close_cached_dir(cfid);
2459	}
2460	/*
2461	 * depending on inode type, check if attribute caching disabled for
2462	 * files or directories
2463	 */
2464	if (S_ISDIR(inode->i_mode)) {
2465		if (!cifs_sb->ctx->acdirmax)
2466			return true;
2467		if (!time_in_range(jiffies, cifs_i->time,
2468				   cifs_i->time + cifs_sb->ctx->acdirmax))
2469			return true;
2470	} else { /* file */
2471		if (!cifs_sb->ctx->acregmax)
2472			return true;
2473		if (!time_in_range(jiffies, cifs_i->time,
2474				   cifs_i->time + cifs_sb->ctx->acregmax))
2475			return true;
2476	}
2477
2478	/* hardlinked files w/ noserverino get "special" treatment */
2479	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2480	    S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2481		return true;
2482
2483	return false;
2484}
2485
2486/*
2487 * Zap the cache. Called when invalid_mapping flag is set.
2488 */
2489int
2490cifs_invalidate_mapping(struct inode *inode)
2491{
2492	int rc = 0;
2493
2494	if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2495		rc = invalidate_inode_pages2(inode->i_mapping);
2496		if (rc)
2497			cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2498				 __func__, inode, rc);
2499	}
2500
2501	return rc;
2502}
2503
2504/**
2505 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2506 *
2507 * @key:	currently unused
2508 * @mode:	the task state to sleep in
2509 */
2510static int
2511cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2512{
2513	schedule();
2514	if (signal_pending_state(mode, current))
2515		return -ERESTARTSYS;
2516	return 0;
2517}
2518
2519int
2520cifs_revalidate_mapping(struct inode *inode)
2521{
2522	int rc;
2523	unsigned long *flags = &CIFS_I(inode)->flags;
2524	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2525
2526	/* swapfiles are not supposed to be shared */
2527	if (IS_SWAPFILE(inode))
2528		return 0;
2529
2530	rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2531				     TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2532	if (rc)
2533		return rc;
2534
2535	if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2536		/* for cache=singleclient, do not invalidate */
2537		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2538			goto skip_invalidate;
2539
2540		rc = cifs_invalidate_mapping(inode);
2541		if (rc)
2542			set_bit(CIFS_INO_INVALID_MAPPING, flags);
2543	}
2544
2545skip_invalidate:
2546	clear_bit_unlock(CIFS_INO_LOCK, flags);
2547	smp_mb__after_atomic();
2548	wake_up_bit(flags, CIFS_INO_LOCK);
2549
2550	return rc;
2551}
2552
2553int
2554cifs_zap_mapping(struct inode *inode)
2555{
2556	set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2557	return cifs_revalidate_mapping(inode);
2558}
2559
2560int cifs_revalidate_file_attr(struct file *filp)
2561{
2562	int rc = 0;
2563	struct dentry *dentry = file_dentry(filp);
2564#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2565	struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2566#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2567
2568	if (!cifs_dentry_needs_reval(dentry))
2569		return rc;
2570
2571#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2572	if (tlink_tcon(cfile->tlink)->unix_ext)
2573		rc = cifs_get_file_info_unix(filp);
2574	else
2575#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2576		rc = cifs_get_file_info(filp);
2577
2578	return rc;
2579}
2580
2581int cifs_revalidate_dentry_attr(struct dentry *dentry)
2582{
2583	unsigned int xid;
2584	int rc = 0;
2585	struct inode *inode = d_inode(dentry);
2586	struct super_block *sb = dentry->d_sb;
2587	const char *full_path;
2588	void *page;
2589	int count = 0;
2590
2591	if (inode == NULL)
2592		return -ENOENT;
2593
2594	if (!cifs_dentry_needs_reval(dentry))
2595		return rc;
2596
2597	xid = get_xid();
2598
2599	page = alloc_dentry_path();
2600	full_path = build_path_from_dentry(dentry, page);
2601	if (IS_ERR(full_path)) {
2602		rc = PTR_ERR(full_path);
2603		goto out;
2604	}
2605
2606	cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2607		 full_path, inode, inode->i_count.counter,
2608		 dentry, cifs_get_time(dentry), jiffies);
2609
2610again:
2611	if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2612		rc = smb311_posix_get_inode_info(&inode, full_path,
2613						 NULL, sb, xid);
2614	} else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2615		rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2616	} else {
2617		rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2618					 xid, NULL);
2619	}
2620	if (rc == -EAGAIN && count++ < 10)
2621		goto again;
2622out:
2623	free_dentry_path(page);
2624	free_xid(xid);
2625
2626	return rc;
2627}
2628
2629int cifs_revalidate_file(struct file *filp)
2630{
2631	int rc;
2632	struct inode *inode = file_inode(filp);
2633
2634	rc = cifs_revalidate_file_attr(filp);
2635	if (rc)
2636		return rc;
2637
2638	return cifs_revalidate_mapping(inode);
2639}
2640
2641/* revalidate a dentry's inode attributes */
2642int cifs_revalidate_dentry(struct dentry *dentry)
2643{
2644	int rc;
2645	struct inode *inode = d_inode(dentry);
2646
2647	rc = cifs_revalidate_dentry_attr(dentry);
2648	if (rc)
2649		return rc;
2650
2651	return cifs_revalidate_mapping(inode);
2652}
2653
2654int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2655		 struct kstat *stat, u32 request_mask, unsigned int flags)
2656{
2657	struct dentry *dentry = path->dentry;
2658	struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2659	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2660	struct inode *inode = d_inode(dentry);
2661	int rc;
2662
2663	if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2664		return -EIO;
2665
2666	/*
2667	 * We need to be sure that all dirty pages are written and the server
2668	 * has actual ctime, mtime and file length.
2669	 */
2670	if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2671	    !CIFS_CACHE_READ(CIFS_I(inode)) &&
2672	    inode->i_mapping && inode->i_mapping->nrpages != 0) {
2673		rc = filemap_fdatawait(inode->i_mapping);
2674		if (rc) {
2675			mapping_set_error(inode->i_mapping, rc);
2676			return rc;
2677		}
2678	}
2679
2680	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2681		CIFS_I(inode)->time = 0; /* force revalidate */
2682
2683	/*
2684	 * If the caller doesn't require syncing, only sync if
2685	 * necessary (e.g. due to earlier truncate or setattr
2686	 * invalidating the cached metadata)
2687	 */
2688	if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2689	    (CIFS_I(inode)->time == 0)) {
2690		rc = cifs_revalidate_dentry_attr(dentry);
2691		if (rc)
2692			return rc;
2693	}
2694
2695	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2696	stat->blksize = cifs_sb->ctx->bsize;
2697	stat->ino = CIFS_I(inode)->uniqueid;
2698
2699	/* old CIFS Unix Extensions doesn't return create time */
2700	if (CIFS_I(inode)->createtime) {
2701		stat->result_mask |= STATX_BTIME;
2702		stat->btime =
2703		      cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2704	}
2705
2706	stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2707	if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2708		stat->attributes |= STATX_ATTR_COMPRESSED;
2709	if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2710		stat->attributes |= STATX_ATTR_ENCRYPTED;
2711
2712	/*
2713	 * If on a multiuser mount without unix extensions or cifsacl being
2714	 * enabled, and the admin hasn't overridden them, set the ownership
2715	 * to the fsuid/fsgid of the current process.
2716	 */
2717	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2718	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2719	    !tcon->unix_ext) {
2720		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2721			stat->uid = current_fsuid();
2722		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2723			stat->gid = current_fsgid();
2724	}
2725	return 0;
2726}
2727
2728int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2729		u64 len)
2730{
2731	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2732	struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2733	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2734	struct TCP_Server_Info *server = tcon->ses->server;
2735	struct cifsFileInfo *cfile;
2736	int rc;
2737
2738	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2739		return -EIO;
2740
2741	/*
2742	 * We need to be sure that all dirty pages are written as they
2743	 * might fill holes on the server.
2744	 */
2745	if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2746	    inode->i_mapping->nrpages != 0) {
2747		rc = filemap_fdatawait(inode->i_mapping);
2748		if (rc) {
2749			mapping_set_error(inode->i_mapping, rc);
2750			return rc;
2751		}
2752	}
2753
2754	cfile = find_readable_file(cifs_i, false);
2755	if (cfile == NULL)
2756		return -EINVAL;
2757
2758	if (server->ops->fiemap) {
2759		rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2760		cifsFileInfo_put(cfile);
2761		return rc;
2762	}
2763
2764	cifsFileInfo_put(cfile);
2765	return -EOPNOTSUPP;
2766}
2767
2768int cifs_truncate_page(struct address_space *mapping, loff_t from)
2769{
2770	pgoff_t index = from >> PAGE_SHIFT;
2771	unsigned offset = from & (PAGE_SIZE - 1);
2772	struct page *page;
2773	int rc = 0;
2774
2775	page = grab_cache_page(mapping, index);
2776	if (!page)
2777		return -ENOMEM;
2778
2779	zero_user_segment(page, offset, PAGE_SIZE);
2780	unlock_page(page);
2781	put_page(page);
2782	return rc;
2783}
2784
2785void cifs_setsize(struct inode *inode, loff_t offset)
2786{
2787	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2788
2789	spin_lock(&inode->i_lock);
2790	i_size_write(inode, offset);
2791	spin_unlock(&inode->i_lock);
2792
2793	/* Cached inode must be refreshed on truncate */
2794	cifs_i->time = 0;
2795	truncate_pagecache(inode, offset);
2796}
2797
2798static int
2799cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2800		   unsigned int xid, const char *full_path)
2801{
2802	int rc;
2803	struct cifsFileInfo *open_file;
2804	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2805	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2806	struct tcon_link *tlink = NULL;
2807	struct cifs_tcon *tcon = NULL;
2808	struct TCP_Server_Info *server;
2809
2810	/*
2811	 * To avoid spurious oplock breaks from server, in the case of
2812	 * inodes that we already have open, avoid doing path based
2813	 * setting of file size if we can do it by handle.
2814	 * This keeps our caching token (oplock) and avoids timeouts
2815	 * when the local oplock break takes longer to flush
2816	 * writebehind data than the SMB timeout for the SetPathInfo
2817	 * request would allow
2818	 */
2819	open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2820	if (open_file) {
2821		tcon = tlink_tcon(open_file->tlink);
2822		server = tcon->ses->server;
2823		if (server->ops->set_file_size)
2824			rc = server->ops->set_file_size(xid, tcon, open_file,
2825							attrs->ia_size, false);
2826		else
2827			rc = -ENOSYS;
2828		cifsFileInfo_put(open_file);
2829		cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2830	} else
2831		rc = -EINVAL;
2832
2833	if (!rc)
2834		goto set_size_out;
2835
2836	if (tcon == NULL) {
2837		tlink = cifs_sb_tlink(cifs_sb);
2838		if (IS_ERR(tlink))
2839			return PTR_ERR(tlink);
2840		tcon = tlink_tcon(tlink);
2841		server = tcon->ses->server;
2842	}
2843
2844	/*
2845	 * Set file size by pathname rather than by handle either because no
2846	 * valid, writeable file handle for it was found or because there was
2847	 * an error setting it by handle.
2848	 */
2849	if (server->ops->set_path_size)
2850		rc = server->ops->set_path_size(xid, tcon, full_path,
2851						attrs->ia_size, cifs_sb, false);
2852	else
2853		rc = -ENOSYS;
2854	cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2855
2856	if (tlink)
2857		cifs_put_tlink(tlink);
2858
2859set_size_out:
2860	if (rc == 0) {
2861		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2862		cifs_setsize(inode, attrs->ia_size);
2863		/*
2864		 * i_blocks is not related to (i_size / i_blksize), but instead
2865		 * 512 byte (2**9) size is required for calculating num blocks.
2866		 * Until we can query the server for actual allocation size,
2867		 * this is best estimate we have for blocks allocated for a file
2868		 * Number of blocks must be rounded up so size 1 is not 0 blocks
2869		 */
2870		inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2871
2872		/*
2873		 * The man page of truncate says if the size changed,
2874		 * then the st_ctime and st_mtime fields for the file
2875		 * are updated.
2876		 */
2877		attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2878		attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2879
2880		cifs_truncate_page(inode->i_mapping, inode->i_size);
2881	}
2882
2883	return rc;
2884}
2885
2886#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2887static int
2888cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2889{
2890	int rc;
2891	unsigned int xid;
2892	const char *full_path;
2893	void *page = alloc_dentry_path();
2894	struct inode *inode = d_inode(direntry);
2895	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2896	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2897	struct tcon_link *tlink;
2898	struct cifs_tcon *pTcon;
2899	struct cifs_unix_set_info_args *args = NULL;
2900	struct cifsFileInfo *open_file;
2901
2902	cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2903		 direntry, attrs->ia_valid);
2904
2905	xid = get_xid();
2906
2907	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2908		attrs->ia_valid |= ATTR_FORCE;
2909
2910	rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2911	if (rc < 0)
2912		goto out;
2913
2914	full_path = build_path_from_dentry(direntry, page);
2915	if (IS_ERR(full_path)) {
2916		rc = PTR_ERR(full_path);
2917		goto out;
2918	}
2919
2920	/*
2921	 * Attempt to flush data before changing attributes. We need to do
2922	 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2923	 * ownership or mode then we may also need to do this. Here, we take
2924	 * the safe way out and just do the flush on all setattr requests. If
2925	 * the flush returns error, store it to report later and continue.
2926	 *
2927	 * BB: This should be smarter. Why bother flushing pages that
2928	 * will be truncated anyway? Also, should we error out here if
2929	 * the flush returns error?
2930	 */
2931	rc = filemap_write_and_wait(inode->i_mapping);
2932	if (is_interrupt_error(rc)) {
2933		rc = -ERESTARTSYS;
2934		goto out;
2935	}
2936
2937	mapping_set_error(inode->i_mapping, rc);
2938	rc = 0;
2939
2940	if (attrs->ia_valid & ATTR_SIZE) {
2941		rc = cifs_set_file_size(inode, attrs, xid, full_path);
2942		if (rc != 0)
2943			goto out;
2944	}
2945
2946	/* skip mode change if it's just for clearing setuid/setgid */
2947	if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2948		attrs->ia_valid &= ~ATTR_MODE;
2949
2950	args = kmalloc(sizeof(*args), GFP_KERNEL);
2951	if (args == NULL) {
2952		rc = -ENOMEM;
2953		goto out;
2954	}
2955
2956	/* set up the struct */
2957	if (attrs->ia_valid & ATTR_MODE)
2958		args->mode = attrs->ia_mode;
2959	else
2960		args->mode = NO_CHANGE_64;
2961
2962	if (attrs->ia_valid & ATTR_UID)
2963		args->uid = attrs->ia_uid;
2964	else
2965		args->uid = INVALID_UID; /* no change */
2966
2967	if (attrs->ia_valid & ATTR_GID)
2968		args->gid = attrs->ia_gid;
2969	else
2970		args->gid = INVALID_GID; /* no change */
2971
2972	if (attrs->ia_valid & ATTR_ATIME)
2973		args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2974	else
2975		args->atime = NO_CHANGE_64;
2976
2977	if (attrs->ia_valid & ATTR_MTIME)
2978		args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2979	else
2980		args->mtime = NO_CHANGE_64;
2981
2982	if (attrs->ia_valid & ATTR_CTIME)
2983		args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2984	else
2985		args->ctime = NO_CHANGE_64;
2986
2987	args->device = 0;
2988	open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2989	if (open_file) {
2990		u16 nfid = open_file->fid.netfid;
2991		u32 npid = open_file->pid;
2992		pTcon = tlink_tcon(open_file->tlink);
2993		rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2994		cifsFileInfo_put(open_file);
2995	} else {
2996		tlink = cifs_sb_tlink(cifs_sb);
2997		if (IS_ERR(tlink)) {
2998			rc = PTR_ERR(tlink);
2999			goto out;
3000		}
3001		pTcon = tlink_tcon(tlink);
3002		rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
3003				    cifs_sb->local_nls,
3004				    cifs_remap(cifs_sb));
3005		cifs_put_tlink(tlink);
3006	}
3007
3008	if (rc)
3009		goto out;
3010
3011	if ((attrs->ia_valid & ATTR_SIZE) &&
3012	    attrs->ia_size != i_size_read(inode)) {
3013		truncate_setsize(inode, attrs->ia_size);
3014		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3015		fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3016	}
3017
3018	setattr_copy(&nop_mnt_idmap, inode, attrs);
3019	mark_inode_dirty(inode);
3020
3021	/* force revalidate when any of these times are set since some
3022	   of the fs types (eg ext3, fat) do not have fine enough
3023	   time granularity to match protocol, and we do not have a
3024	   a way (yet) to query the server fs's time granularity (and
3025	   whether it rounds times down).
3026	*/
3027	if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
3028		cifsInode->time = 0;
3029out:
3030	kfree(args);
3031	free_dentry_path(page);
3032	free_xid(xid);
3033	return rc;
3034}
3035#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3036
3037static int
3038cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3039{
3040	unsigned int xid;
3041	kuid_t uid = INVALID_UID;
3042	kgid_t gid = INVALID_GID;
3043	struct inode *inode = d_inode(direntry);
3044	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3045	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3046	struct cifsFileInfo *wfile;
3047	struct cifs_tcon *tcon;
3048	const char *full_path;
3049	void *page = alloc_dentry_path();
3050	int rc = -EACCES;
3051	__u32 dosattr = 0;
3052	__u64 mode = NO_CHANGE_64;
3053
3054	xid = get_xid();
3055
3056	cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3057		 direntry, attrs->ia_valid);
3058
3059	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3060		attrs->ia_valid |= ATTR_FORCE;
3061
3062	rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3063	if (rc < 0)
3064		goto cifs_setattr_exit;
3065
3066	full_path = build_path_from_dentry(direntry, page);
3067	if (IS_ERR(full_path)) {
3068		rc = PTR_ERR(full_path);
3069		goto cifs_setattr_exit;
3070	}
3071
3072	/*
3073	 * Attempt to flush data before changing attributes. We need to do
3074	 * this for ATTR_SIZE and ATTR_MTIME.  If the flush of the data
3075	 * returns error, store it to report later and continue.
3076	 *
3077	 * BB: This should be smarter. Why bother flushing pages that
3078	 * will be truncated anyway? Also, should we error out here if
3079	 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3080	 */
3081	if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3082		rc = filemap_write_and_wait(inode->i_mapping);
3083		if (is_interrupt_error(rc)) {
3084			rc = -ERESTARTSYS;
3085			goto cifs_setattr_exit;
3086		}
3087		mapping_set_error(inode->i_mapping, rc);
3088	}
3089
3090	rc = 0;
3091
3092	if ((attrs->ia_valid & ATTR_MTIME) &&
3093	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3094		rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3095		if (!rc) {
3096			tcon = tlink_tcon(wfile->tlink);
3097			rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3098			cifsFileInfo_put(wfile);
3099			if (rc)
3100				goto cifs_setattr_exit;
3101		} else if (rc != -EBADF)
3102			goto cifs_setattr_exit;
3103		else
3104			rc = 0;
3105	}
3106
3107	if (attrs->ia_valid & ATTR_SIZE) {
3108		rc = cifs_set_file_size(inode, attrs, xid, full_path);
3109		if (rc != 0)
3110			goto cifs_setattr_exit;
3111	}
3112
3113	if (attrs->ia_valid & ATTR_UID)
3114		uid = attrs->ia_uid;
3115
3116	if (attrs->ia_valid & ATTR_GID)
3117		gid = attrs->ia_gid;
3118
3119	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3120	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3121		if (uid_valid(uid) || gid_valid(gid)) {
3122			mode = NO_CHANGE_64;
3123			rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3124							uid, gid);
3125			if (rc) {
3126				cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3127					 __func__, rc);
3128				goto cifs_setattr_exit;
3129			}
3130		}
3131	} else
3132	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3133		attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3134
3135	/* skip mode change if it's just for clearing setuid/setgid */
3136	if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3137		attrs->ia_valid &= ~ATTR_MODE;
3138
3139	if (attrs->ia_valid & ATTR_MODE) {
3140		mode = attrs->ia_mode;
3141		rc = 0;
3142		if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3143		    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3144			rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3145						INVALID_UID, INVALID_GID);
3146			if (rc) {
3147				cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3148					 __func__, rc);
3149				goto cifs_setattr_exit;
3150			}
3151
3152			/*
3153			 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3154			 * Pick up the actual mode bits that were set.
3155			 */
3156			if (mode != attrs->ia_mode)
3157				attrs->ia_mode = mode;
3158		} else
3159		if (((mode & S_IWUGO) == 0) &&
3160		    (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3161
3162			dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3163
3164			/* fix up mode if we're not using dynperm */
3165			if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3166				attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3167		} else if ((mode & S_IWUGO) &&
3168			   (cifsInode->cifsAttrs & ATTR_READONLY)) {
3169
3170			dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3171			/* Attributes of 0 are ignored */
3172			if (dosattr == 0)
3173				dosattr |= ATTR_NORMAL;
3174
3175			/* reset local inode permissions to normal */
3176			if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3177				attrs->ia_mode &= ~(S_IALLUGO);
3178				if (S_ISDIR(inode->i_mode))
3179					attrs->ia_mode |=
3180						cifs_sb->ctx->dir_mode;
3181				else
3182					attrs->ia_mode |=
3183						cifs_sb->ctx->file_mode;
3184			}
3185		} else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3186			/* ignore mode change - ATTR_READONLY hasn't changed */
3187			attrs->ia_valid &= ~ATTR_MODE;
3188		}
3189	}
3190
3191	if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3192	    ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3193		rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3194		/* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3195
3196		/* Even if error on time set, no sense failing the call if
3197		the server would set the time to a reasonable value anyway,
3198		and this check ensures that we are not being called from
3199		sys_utimes in which case we ought to fail the call back to
3200		the user when the server rejects the call */
3201		if ((rc) && (attrs->ia_valid &
3202				(ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3203			rc = 0;
3204	}
3205
3206	/* do not need local check to inode_check_ok since the server does
3207	   that */
3208	if (rc)
3209		goto cifs_setattr_exit;
3210
3211	if ((attrs->ia_valid & ATTR_SIZE) &&
3212	    attrs->ia_size != i_size_read(inode)) {
3213		truncate_setsize(inode, attrs->ia_size);
3214		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3215		fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3216	}
3217
3218	setattr_copy(&nop_mnt_idmap, inode, attrs);
3219	mark_inode_dirty(inode);
3220
3221cifs_setattr_exit:
3222	free_xid(xid);
3223	free_dentry_path(page);
3224	return rc;
3225}
3226
3227int
3228cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3229	     struct iattr *attrs)
3230{
3231	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3232	int rc, retries = 0;
3233#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3234	struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3235#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3236
3237	if (unlikely(cifs_forced_shutdown(cifs_sb)))
3238		return -EIO;
3239
3240	do {
3241#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3242		if (pTcon->unix_ext)
3243			rc = cifs_setattr_unix(direntry, attrs);
3244		else
3245#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3246			rc = cifs_setattr_nounix(direntry, attrs);
3247		retries++;
3248	} while (is_retryable_error(rc) && retries < 2);
3249
3250	/* BB: add cifs_setattr_legacy for really old servers */
3251	return rc;
3252}
v6.9.4
   1// SPDX-License-Identifier: LGPL-2.1
   2/*
   3 *
   4 *   Copyright (C) International Business Machines  Corp., 2002,2010
   5 *   Author(s): Steve French (sfrench@us.ibm.com)
   6 *
   7 */
   8#include <linux/fs.h>
   9#include <linux/stat.h>
  10#include <linux/slab.h>
  11#include <linux/pagemap.h>
  12#include <linux/freezer.h>
  13#include <linux/sched/signal.h>
  14#include <linux/wait_bit.h>
  15#include <linux/fiemap.h>
  16#include <asm/div64.h>
  17#include "cifsfs.h"
  18#include "cifspdu.h"
  19#include "cifsglob.h"
  20#include "cifsproto.h"
  21#include "smb2proto.h"
  22#include "cifs_debug.h"
  23#include "cifs_fs_sb.h"
  24#include "cifs_unicode.h"
  25#include "fscache.h"
  26#include "fs_context.h"
  27#include "cifs_ioctl.h"
  28#include "cached_dir.h"
  29#include "reparse.h"
  30
  31static void cifs_set_ops(struct inode *inode)
  32{
  33	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  34
  35	switch (inode->i_mode & S_IFMT) {
  36	case S_IFREG:
  37		inode->i_op = &cifs_file_inode_ops;
  38		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  39			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  40				inode->i_fop = &cifs_file_direct_nobrl_ops;
  41			else
  42				inode->i_fop = &cifs_file_direct_ops;
  43		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  44			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  45				inode->i_fop = &cifs_file_strict_nobrl_ops;
  46			else
  47				inode->i_fop = &cifs_file_strict_ops;
  48		} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  49			inode->i_fop = &cifs_file_nobrl_ops;
  50		else { /* not direct, send byte range locks */
  51			inode->i_fop = &cifs_file_ops;
  52		}
  53
  54		/* check if server can support readahead */
  55		if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
  56				PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  57			inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  58		else
  59			inode->i_data.a_ops = &cifs_addr_ops;
  60		break;
  61	case S_IFDIR:
  62		if (IS_AUTOMOUNT(inode)) {
  63			inode->i_op = &cifs_namespace_inode_operations;
  64		} else {
  65			inode->i_op = &cifs_dir_inode_ops;
  66			inode->i_fop = &cifs_dir_ops;
  67		}
  68		break;
  69	case S_IFLNK:
  70		inode->i_op = &cifs_symlink_inode_ops;
  71		break;
  72	default:
  73		init_special_inode(inode, inode->i_mode, inode->i_rdev);
  74		break;
  75	}
  76}
  77
  78/* check inode attributes against fattr. If they don't match, tag the
  79 * inode for cache invalidation
  80 */
  81static void
  82cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  83{
  84	struct cifs_fscache_inode_coherency_data cd;
  85	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  86	struct timespec64 mtime;
  87
  88	cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  89		 __func__, cifs_i->uniqueid);
  90
  91	if (inode->i_state & I_NEW) {
  92		cifs_dbg(FYI, "%s: inode %llu is new\n",
  93			 __func__, cifs_i->uniqueid);
  94		return;
  95	}
  96
  97	/* don't bother with revalidation if we have an oplock */
  98	if (CIFS_CACHE_READ(cifs_i)) {
  99		cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
 100			 __func__, cifs_i->uniqueid);
 101		return;
 102	}
 103
 104	 /* revalidate if mtime or size have changed */
 105	fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
 106	mtime = inode_get_mtime(inode);
 107	if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
 108	    cifs_i->netfs.remote_i_size == fattr->cf_eof) {
 109		cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
 110			 __func__, cifs_i->uniqueid);
 111		return;
 112	}
 113
 114	cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
 115		 __func__, cifs_i->uniqueid);
 116	set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
 117	/* Invalidate fscache cookie */
 118	cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
 119	fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
 120}
 121
 122/*
 123 * copy nlink to the inode, unless it wasn't provided.  Provide
 124 * sane values if we don't have an existing one and none was provided
 125 */
 126static void
 127cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
 128{
 129	/*
 130	 * if we're in a situation where we can't trust what we
 131	 * got from the server (readdir, some non-unix cases)
 132	 * fake reasonable values
 133	 */
 134	if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
 135		/* only provide fake values on a new inode */
 136		if (inode->i_state & I_NEW) {
 137			if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
 138				set_nlink(inode, 2);
 139			else
 140				set_nlink(inode, 1);
 141		}
 142		return;
 143	}
 144
 145	/* we trust the server, so update it */
 146	set_nlink(inode, fattr->cf_nlink);
 147}
 148
 149/* populate an inode with info from a cifs_fattr struct */
 150int
 151cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
 152		    bool from_readdir)
 153{
 154	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
 155	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 156
 157	if (!(inode->i_state & I_NEW) &&
 158	    unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
 159		CIFS_I(inode)->time = 0; /* force reval */
 160		return -ESTALE;
 161	}
 162
 163	cifs_revalidate_cache(inode, fattr);
 164
 165	spin_lock(&inode->i_lock);
 166	fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
 167	fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
 168	fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
 169	/* we do not want atime to be less than mtime, it broke some apps */
 170	if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
 171		inode_set_atime_to_ts(inode, fattr->cf_mtime);
 172	else
 173		inode_set_atime_to_ts(inode, fattr->cf_atime);
 174	inode_set_mtime_to_ts(inode, fattr->cf_mtime);
 175	inode_set_ctime_to_ts(inode, fattr->cf_ctime);
 176	inode->i_rdev = fattr->cf_rdev;
 177	cifs_nlink_fattr_to_inode(inode, fattr);
 178	inode->i_uid = fattr->cf_uid;
 179	inode->i_gid = fattr->cf_gid;
 180
 181	/* if dynperm is set, don't clobber existing mode */
 182	if (inode->i_state & I_NEW ||
 183	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
 184		inode->i_mode = fattr->cf_mode;
 185
 186	cifs_i->cifsAttrs = fattr->cf_cifsattrs;
 187	cifs_i->reparse_tag = fattr->cf_cifstag;
 188
 189	if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
 190		cifs_i->time = 0;
 191	else
 192		cifs_i->time = jiffies;
 193
 194	if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
 195		set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 196	else
 197		clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
 198
 199	cifs_i->netfs.remote_i_size = fattr->cf_eof;
 200	/*
 201	 * Can't safely change the file size here if the client is writing to
 202	 * it due to potential races.
 203	 */
 204	if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
 205		i_size_write(inode, fattr->cf_eof);
 206
 207		/*
 208		 * i_blocks is not related to (i_size / i_blksize),
 209		 * but instead 512 byte (2**9) size is required for
 210		 * calculating num blocks.
 211		 */
 212		inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
 213	}
 214
 215	if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) {
 216		kfree(cifs_i->symlink_target);
 217		cifs_i->symlink_target = fattr->cf_symlink_target;
 218		fattr->cf_symlink_target = NULL;
 219	}
 220	spin_unlock(&inode->i_lock);
 221
 222	if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
 223		inode->i_flags |= S_AUTOMOUNT;
 224	if (inode->i_state & I_NEW)
 225		cifs_set_ops(inode);
 226	return 0;
 227}
 228
 229void
 230cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
 231{
 232	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 233
 234	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
 235		return;
 236
 237	fattr->cf_uniqueid = iunique(sb, ROOT_I);
 238}
 239
 240/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
 241void
 242cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
 243			 struct cifs_sb_info *cifs_sb)
 244{
 245	memset(fattr, 0, sizeof(*fattr));
 246	fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
 247	fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
 248	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 249
 250	fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 251	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
 252	fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
 253	/* old POSIX extensions don't get create time */
 254
 255	fattr->cf_mode = le64_to_cpu(info->Permissions);
 256
 257	/*
 258	 * Since we set the inode type below we need to mask off
 259	 * to avoid strange results if bits set above.
 260	 */
 261	fattr->cf_mode &= ~S_IFMT;
 262	switch (le32_to_cpu(info->Type)) {
 263	case UNIX_FILE:
 264		fattr->cf_mode |= S_IFREG;
 265		fattr->cf_dtype = DT_REG;
 266		break;
 267	case UNIX_SYMLINK:
 268		fattr->cf_mode |= S_IFLNK;
 269		fattr->cf_dtype = DT_LNK;
 270		break;
 271	case UNIX_DIR:
 272		fattr->cf_mode |= S_IFDIR;
 273		fattr->cf_dtype = DT_DIR;
 274		break;
 275	case UNIX_CHARDEV:
 276		fattr->cf_mode |= S_IFCHR;
 277		fattr->cf_dtype = DT_CHR;
 278		fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
 279				       le64_to_cpu(info->DevMinor) & MINORMASK);
 280		break;
 281	case UNIX_BLOCKDEV:
 282		fattr->cf_mode |= S_IFBLK;
 283		fattr->cf_dtype = DT_BLK;
 284		fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
 285				       le64_to_cpu(info->DevMinor) & MINORMASK);
 286		break;
 287	case UNIX_FIFO:
 288		fattr->cf_mode |= S_IFIFO;
 289		fattr->cf_dtype = DT_FIFO;
 290		break;
 291	case UNIX_SOCKET:
 292		fattr->cf_mode |= S_IFSOCK;
 293		fattr->cf_dtype = DT_SOCK;
 294		break;
 295	default:
 296		/* safest to call it a file if we do not know */
 297		fattr->cf_mode |= S_IFREG;
 298		fattr->cf_dtype = DT_REG;
 299		cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
 300		break;
 301	}
 302
 303	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 304	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
 305		u64 id = le64_to_cpu(info->Uid);
 306		if (id < ((uid_t)-1)) {
 307			kuid_t uid = make_kuid(&init_user_ns, id);
 308			if (uid_valid(uid))
 309				fattr->cf_uid = uid;
 310		}
 311	}
 312	
 313	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 314	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
 315		u64 id = le64_to_cpu(info->Gid);
 316		if (id < ((gid_t)-1)) {
 317			kgid_t gid = make_kgid(&init_user_ns, id);
 318			if (gid_valid(gid))
 319				fattr->cf_gid = gid;
 320		}
 321	}
 322
 323	fattr->cf_nlink = le64_to_cpu(info->Nlinks);
 324}
 325
 326/*
 327 * Fill a cifs_fattr struct with fake inode info.
 328 *
 329 * Needed to setup cifs_fattr data for the directory which is the
 330 * junction to the new submount (ie to setup the fake directory
 331 * which represents a DFS referral or reparse mount point).
 332 */
 333static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
 334				       struct super_block *sb)
 335{
 336	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 337
 338	cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
 339
 340	memset(fattr, 0, sizeof(*fattr));
 341	fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
 342	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 343	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 344	ktime_get_coarse_real_ts64(&fattr->cf_mtime);
 345	fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
 346	fattr->cf_nlink = 2;
 347	fattr->cf_flags = CIFS_FATTR_JUNCTION;
 348}
 349
 350/* Update inode with final fattr data */
 351static int update_inode_info(struct super_block *sb,
 352			     struct cifs_fattr *fattr,
 353			     struct inode **inode)
 354{
 355	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 356	int rc = 0;
 357
 358	if (!*inode) {
 359		*inode = cifs_iget(sb, fattr);
 360		if (!*inode)
 361			rc = -ENOMEM;
 362		return rc;
 363	}
 364	/* We already have inode, update it.
 365	 *
 366	 * If file type or uniqueid is different, return error.
 367	 */
 368	if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
 369		     CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
 370		CIFS_I(*inode)->time = 0; /* force reval */
 371		return -ESTALE;
 372	}
 373	return cifs_fattr_to_inode(*inode, fattr, false);
 374}
 375
 376#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
 377static int
 378cifs_get_file_info_unix(struct file *filp)
 379{
 380	int rc;
 381	unsigned int xid;
 382	FILE_UNIX_BASIC_INFO find_data;
 383	struct cifs_fattr fattr = {};
 384	struct inode *inode = file_inode(filp);
 385	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
 386	struct cifsFileInfo *cfile = filp->private_data;
 387	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 388
 389	xid = get_xid();
 390
 391	if (cfile->symlink_target) {
 392		fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
 393		if (!fattr.cf_symlink_target) {
 394			rc = -ENOMEM;
 395			goto cifs_gfiunix_out;
 396		}
 397	}
 398
 399	rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
 400	if (!rc) {
 401		cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
 402	} else if (rc == -EREMOTE) {
 403		cifs_create_junction_fattr(&fattr, inode->i_sb);
 
 404	} else
 405		goto cifs_gfiunix_out;
 406
 407	rc = cifs_fattr_to_inode(inode, &fattr, false);
 408
 409cifs_gfiunix_out:
 410	free_xid(xid);
 411	return rc;
 412}
 413
 414static int cifs_get_unix_fattr(const unsigned char *full_path,
 415			       struct super_block *sb,
 416			       struct cifs_fattr *fattr,
 417			       struct inode **pinode,
 418			       const unsigned int xid)
 419{
 420	struct TCP_Server_Info *server;
 421	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 422	FILE_UNIX_BASIC_INFO find_data;
 423	struct cifs_tcon *tcon;
 424	struct tcon_link *tlink;
 425	int rc, tmprc;
 426
 427	cifs_dbg(FYI, "Getting info on %s\n", full_path);
 428
 429	tlink = cifs_sb_tlink(cifs_sb);
 430	if (IS_ERR(tlink))
 431		return PTR_ERR(tlink);
 432	tcon = tlink_tcon(tlink);
 433	server = tcon->ses->server;
 434
 435	/* could have done a find first instead but this returns more info */
 436	rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
 437				  cifs_sb->local_nls, cifs_remap(cifs_sb));
 438	cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
 439	cifs_put_tlink(tlink);
 440
 441	if (!rc) {
 442		cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
 443	} else if (rc == -EREMOTE) {
 444		cifs_create_junction_fattr(fattr, sb);
 445		rc = 0;
 446	} else {
 447		return rc;
 448	}
 449
 450	if (!*pinode)
 451		cifs_fill_uniqueid(sb, fattr);
 452
 453	/* check for Minshall+French symlinks */
 454	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
 455		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
 456		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
 457	}
 458
 459	if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
 460		if (!server->ops->query_symlink)
 461			return -EOPNOTSUPP;
 462		rc = server->ops->query_symlink(xid, tcon,
 463						cifs_sb, full_path,
 464						&fattr->cf_symlink_target);
 465		cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
 466	}
 467	return rc;
 468}
 469
 470int cifs_get_inode_info_unix(struct inode **pinode,
 471			     const unsigned char *full_path,
 472			     struct super_block *sb, unsigned int xid)
 473{
 474	struct cifs_fattr fattr = {};
 475	int rc;
 476
 477	rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
 478	if (rc)
 479		goto out;
 480
 481	rc = update_inode_info(sb, &fattr, pinode);
 482out:
 483	kfree(fattr.cf_symlink_target);
 484	return rc;
 485}
 486#else
 487static inline int cifs_get_unix_fattr(const unsigned char *full_path,
 488				      struct super_block *sb,
 489				      struct cifs_fattr *fattr,
 490				      struct inode **pinode,
 491				      const unsigned int xid)
 492{
 493	return -EOPNOTSUPP;
 494}
 495
 496int cifs_get_inode_info_unix(struct inode **pinode,
 497			     const unsigned char *full_path,
 498			     struct super_block *sb, unsigned int xid)
 499{
 500	return -EOPNOTSUPP;
 501}
 502#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
 503
 504static int
 505cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
 506	      struct cifs_sb_info *cifs_sb, unsigned int xid)
 507{
 508	int rc;
 509	__u32 oplock;
 510	struct tcon_link *tlink;
 511	struct cifs_tcon *tcon;
 512	struct cifs_fid fid;
 513	struct cifs_open_parms oparms;
 514	struct cifs_io_parms io_parms = {0};
 515	char buf[24];
 516	unsigned int bytes_read;
 517	char *pbuf;
 518	int buf_type = CIFS_NO_BUFFER;
 519
 520	pbuf = buf;
 521
 522	fattr->cf_mode &= ~S_IFMT;
 523
 524	if (fattr->cf_eof == 0) {
 525		fattr->cf_mode |= S_IFIFO;
 526		fattr->cf_dtype = DT_FIFO;
 527		return 0;
 528	} else if (fattr->cf_eof < 8) {
 529		fattr->cf_mode |= S_IFREG;
 530		fattr->cf_dtype = DT_REG;
 531		return -EINVAL;	 /* EOPNOTSUPP? */
 532	}
 533
 534	tlink = cifs_sb_tlink(cifs_sb);
 535	if (IS_ERR(tlink))
 536		return PTR_ERR(tlink);
 537	tcon = tlink_tcon(tlink);
 538
 539	oparms = (struct cifs_open_parms) {
 540		.tcon = tcon,
 541		.cifs_sb = cifs_sb,
 542		.desired_access = GENERIC_READ,
 543		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
 544		.disposition = FILE_OPEN,
 545		.path = path,
 546		.fid = &fid,
 547	};
 548
 549	if (tcon->ses->server->oplocks)
 550		oplock = REQ_OPLOCK;
 551	else
 552		oplock = 0;
 553	rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
 554	if (rc) {
 555		cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
 556		cifs_put_tlink(tlink);
 557		return rc;
 558	}
 559
 560	/* Read header */
 561	io_parms.netfid = fid.netfid;
 562	io_parms.pid = current->tgid;
 563	io_parms.tcon = tcon;
 564	io_parms.offset = 0;
 565	io_parms.length = 24;
 566
 567	rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
 568					&bytes_read, &pbuf, &buf_type);
 569	if ((rc == 0) && (bytes_read >= 8)) {
 570		if (memcmp("IntxBLK", pbuf, 8) == 0) {
 571			cifs_dbg(FYI, "Block device\n");
 572			fattr->cf_mode |= S_IFBLK;
 573			fattr->cf_dtype = DT_BLK;
 574			if (bytes_read == 24) {
 575				/* we have enough to decode dev num */
 576				__u64 mjr; /* major */
 577				__u64 mnr; /* minor */
 578				mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
 579				mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
 580				fattr->cf_rdev = MKDEV(mjr, mnr);
 581			}
 582		} else if (memcmp("IntxCHR", pbuf, 8) == 0) {
 583			cifs_dbg(FYI, "Char device\n");
 584			fattr->cf_mode |= S_IFCHR;
 585			fattr->cf_dtype = DT_CHR;
 586			if (bytes_read == 24) {
 587				/* we have enough to decode dev num */
 588				__u64 mjr; /* major */
 589				__u64 mnr; /* minor */
 590				mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
 591				mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
 592				fattr->cf_rdev = MKDEV(mjr, mnr);
 593			}
 594		} else if (memcmp("IntxLNK", pbuf, 7) == 0) {
 595			cifs_dbg(FYI, "Symlink\n");
 596			fattr->cf_mode |= S_IFLNK;
 597			fattr->cf_dtype = DT_LNK;
 598		} else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
 599			cifs_dbg(FYI, "FIFO\n");
 600			fattr->cf_mode |= S_IFIFO;
 601			fattr->cf_dtype = DT_FIFO;
 602		} else {
 603			fattr->cf_mode |= S_IFREG; /* file? */
 604			fattr->cf_dtype = DT_REG;
 605			rc = -EOPNOTSUPP;
 606		}
 607	} else {
 608		fattr->cf_mode |= S_IFREG; /* then it is a file */
 609		fattr->cf_dtype = DT_REG;
 610		rc = -EOPNOTSUPP; /* or some unknown SFU type */
 611	}
 612
 613	tcon->ses->server->ops->close(xid, tcon, &fid);
 614	cifs_put_tlink(tlink);
 615	return rc;
 616}
 617
 618#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
 619
 620/*
 621 * Fetch mode bits as provided by SFU.
 622 *
 623 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
 624 */
 625static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
 626			 struct cifs_sb_info *cifs_sb, unsigned int xid)
 627{
 628#ifdef CONFIG_CIFS_XATTR
 629	ssize_t rc;
 630	char ea_value[4];
 631	__u32 mode;
 632	struct tcon_link *tlink;
 633	struct cifs_tcon *tcon;
 634
 635	tlink = cifs_sb_tlink(cifs_sb);
 636	if (IS_ERR(tlink))
 637		return PTR_ERR(tlink);
 638	tcon = tlink_tcon(tlink);
 639
 640	if (tcon->ses->server->ops->query_all_EAs == NULL) {
 641		cifs_put_tlink(tlink);
 642		return -EOPNOTSUPP;
 643	}
 644
 645	rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
 646			"SETFILEBITS", ea_value, 4 /* size of buf */,
 647			cifs_sb);
 648	cifs_put_tlink(tlink);
 649	if (rc < 0)
 650		return (int)rc;
 651	else if (rc > 3) {
 652		mode = le32_to_cpu(*((__le32 *)ea_value));
 653		fattr->cf_mode &= ~SFBITS_MASK;
 654		cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
 655			 mode, fattr->cf_mode);
 656		fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
 657		cifs_dbg(FYI, "special mode bits 0%o\n", mode);
 658	}
 659
 660	return 0;
 661#else
 662	return -EOPNOTSUPP;
 663#endif
 664}
 665
 666/* Fill a cifs_fattr struct with info from POSIX info struct */
 667static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
 668				       struct cifs_open_info_data *data,
 669				       struct super_block *sb)
 670{
 671	struct smb311_posix_qinfo *info = &data->posix_fi;
 672	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 673	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 674
 675	memset(fattr, 0, sizeof(*fattr));
 676
 677	/* no fattr->flags to set */
 678	fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
 679	fattr->cf_uniqueid = le64_to_cpu(info->Inode);
 680
 681	if (info->LastAccessTime)
 682		fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 683	else
 684		ktime_get_coarse_real_ts64(&fattr->cf_atime);
 685
 686	fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
 687	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
 688
 689	if (data->adjust_tz) {
 690		fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
 691		fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
 692	}
 693
 694	/*
 695	 * The srv fs device id is overridden on network mount so setting
 696	 * @fattr->cf_rdev isn't needed here.
 697	 */
 698	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 699	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 700	fattr->cf_createtime = le64_to_cpu(info->CreationTime);
 701	fattr->cf_nlink = le32_to_cpu(info->HardLinks);
 702	fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
 703
 704	if (cifs_open_data_reparse(data) &&
 705	    cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
 706		goto out_reparse;
 707
 708	fattr->cf_mode &= ~S_IFMT;
 709	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
 710		fattr->cf_mode |= S_IFDIR;
 711		fattr->cf_dtype = DT_DIR;
 712	} else { /* file */
 713		fattr->cf_mode |= S_IFREG;
 714		fattr->cf_dtype = DT_REG;
 715	}
 716
 717out_reparse:
 718	if (S_ISLNK(fattr->cf_mode)) {
 719		if (likely(data->symlink_target))
 720			fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
 721		fattr->cf_symlink_target = data->symlink_target;
 722		data->symlink_target = NULL;
 723	}
 724	sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER);
 725	sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP);
 726
 727	cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
 728		fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
 729}
 730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 731static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
 732				    struct cifs_open_info_data *data,
 733				    struct super_block *sb)
 734{
 735	struct smb2_file_all_info *info = &data->fi;
 736	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 737	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
 738
 739	memset(fattr, 0, sizeof(*fattr));
 740	fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
 741	if (info->DeletePending)
 742		fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
 743
 744	if (info->LastAccessTime)
 745		fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
 746	else
 747		ktime_get_coarse_real_ts64(&fattr->cf_atime);
 748
 749	fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
 750	fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
 751
 752	if (data->adjust_tz) {
 753		fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
 754		fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
 755	}
 756
 757	fattr->cf_eof = le64_to_cpu(info->EndOfFile);
 758	fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
 759	fattr->cf_createtime = le64_to_cpu(info->CreationTime);
 760	fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
 761	fattr->cf_uid = cifs_sb->ctx->linux_uid;
 762	fattr->cf_gid = cifs_sb->ctx->linux_gid;
 763
 764	fattr->cf_mode = cifs_sb->ctx->file_mode;
 765	if (cifs_open_data_reparse(data) &&
 766	    cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
 767		goto out_reparse;
 768
 769	if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
 770		fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
 771		fattr->cf_dtype = DT_DIR;
 772		/*
 773		 * Server can return wrong NumberOfLinks value for directories
 774		 * when Unix extensions are disabled - fake it.
 775		 */
 776		if (!tcon->unix_ext)
 777			fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
 778	} else {
 779		fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
 780		fattr->cf_dtype = DT_REG;
 781
 782		/* clear write bits if ATTR_READONLY is set */
 783		if (fattr->cf_cifsattrs & ATTR_READONLY)
 784			fattr->cf_mode &= ~(S_IWUGO);
 785
 786		/*
 787		 * Don't accept zero nlink from non-unix servers unless
 788		 * delete is pending.  Instead mark it as unknown.
 789		 */
 790		if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
 791		    !info->DeletePending) {
 792			cifs_dbg(VFS, "bogus file nlink value %u\n",
 793				 fattr->cf_nlink);
 794			fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
 795		}
 796	}
 797
 798out_reparse:
 799	if (S_ISLNK(fattr->cf_mode)) {
 800		if (likely(data->symlink_target))
 801			fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
 802		fattr->cf_symlink_target = data->symlink_target;
 803		data->symlink_target = NULL;
 804	}
 
 
 
 805}
 806
 807static int
 808cifs_get_file_info(struct file *filp)
 809{
 810	int rc;
 811	unsigned int xid;
 812	struct cifs_open_info_data data = {};
 813	struct cifs_fattr fattr;
 814	struct inode *inode = file_inode(filp);
 815	struct cifsFileInfo *cfile = filp->private_data;
 816	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
 817	struct TCP_Server_Info *server = tcon->ses->server;
 818	struct dentry *dentry = filp->f_path.dentry;
 819	void *page = alloc_dentry_path();
 820	const unsigned char *path;
 821
 822	if (!server->ops->query_file_info) {
 823		free_dentry_path(page);
 824		return -ENOSYS;
 825	}
 826
 827	xid = get_xid();
 828	rc = server->ops->query_file_info(xid, tcon, cfile, &data);
 829	switch (rc) {
 830	case 0:
 831		/* TODO: add support to query reparse tag */
 832		data.adjust_tz = false;
 833		if (data.symlink_target) {
 834			data.symlink = true;
 835			data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
 836		}
 837		path = build_path_from_dentry(dentry, page);
 838		if (IS_ERR(path)) {
 839			rc = PTR_ERR(path);
 840			goto cgfi_exit;
 841		}
 842		cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
 843		if (fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
 844			cifs_mark_open_handles_for_deleted_file(inode, path);
 845		break;
 846	case -EREMOTE:
 847		cifs_create_junction_fattr(&fattr, inode->i_sb);
 
 848		break;
 849	case -EOPNOTSUPP:
 850	case -EINVAL:
 851		/*
 852		 * FIXME: legacy server -- fall back to path-based call?
 853		 * for now, just skip revalidating and mark inode for
 854		 * immediate reval.
 855		 */
 856		rc = 0;
 857		CIFS_I(inode)->time = 0;
 858		goto cgfi_exit;
 859	default:
 860		goto cgfi_exit;
 861	}
 862
 863	/*
 864	 * don't bother with SFU junk here -- just mark inode as needing
 865	 * revalidation.
 866	 */
 867	fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
 868	fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
 869	/* if filetype is different, return error */
 870	rc = cifs_fattr_to_inode(inode, &fattr, false);
 871cgfi_exit:
 872	cifs_free_open_info(&data);
 873	free_dentry_path(page);
 874	free_xid(xid);
 875	return rc;
 876}
 877
 878/* Simple function to return a 64 bit hash of string.  Rarely called */
 879static __u64 simple_hashstr(const char *str)
 880{
 881	const __u64 hash_mult =  1125899906842597ULL; /* a big enough prime */
 882	__u64 hash = 0;
 883
 884	while (*str)
 885		hash = (hash + (__u64) *str++) * hash_mult;
 886
 887	return hash;
 888}
 889
 890#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
 891/**
 892 * cifs_backup_query_path_info - SMB1 fallback code to get ino
 893 *
 894 * Fallback code to get file metadata when we don't have access to
 895 * full_path (EACCES) and have backup creds.
 896 *
 897 * @xid:	transaction id used to identify original request in logs
 898 * @tcon:	information about the server share we have mounted
 899 * @sb:	the superblock stores info such as disk space available
 900 * @full_path:	name of the file we are getting the metadata for
 901 * @resp_buf:	will be set to cifs resp buf and needs to be freed with
 902 * 		cifs_buf_release() when done with @data
 903 * @data:	will be set to search info result buffer
 904 */
 905static int
 906cifs_backup_query_path_info(int xid,
 907			    struct cifs_tcon *tcon,
 908			    struct super_block *sb,
 909			    const char *full_path,
 910			    void **resp_buf,
 911			    FILE_ALL_INFO **data)
 912{
 913	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 914	struct cifs_search_info info = {0};
 915	u16 flags;
 916	int rc;
 917
 918	*resp_buf = NULL;
 919	info.endOfSearch = false;
 920	if (tcon->unix_ext)
 921		info.info_level = SMB_FIND_FILE_UNIX;
 922	else if ((tcon->ses->capabilities &
 923		  tcon->ses->server->vals->cap_nt_find) == 0)
 924		info.info_level = SMB_FIND_FILE_INFO_STANDARD;
 925	else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
 926		info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
 927	else /* no srvino useful for fallback to some netapp */
 928		info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
 929
 930	flags = CIFS_SEARCH_CLOSE_ALWAYS |
 931		CIFS_SEARCH_CLOSE_AT_END |
 932		CIFS_SEARCH_BACKUP_SEARCH;
 933
 934	rc = CIFSFindFirst(xid, tcon, full_path,
 935			   cifs_sb, NULL, flags, &info, false);
 936	if (rc)
 937		return rc;
 938
 939	*resp_buf = (void *)info.ntwrk_buf_start;
 940	*data = (FILE_ALL_INFO *)info.srch_entries_start;
 941	return 0;
 942}
 943#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
 944
 945static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
 946			       struct inode **inode, const char *full_path,
 947			       struct cifs_open_info_data *data, struct cifs_fattr *fattr)
 948{
 949	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
 950	struct TCP_Server_Info *server = tcon->ses->server;
 951	int rc;
 952
 953	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
 954		if (*inode)
 955			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
 956		else
 957			fattr->cf_uniqueid = iunique(sb, ROOT_I);
 958		return;
 959	}
 960
 961	/*
 962	 * If we have an inode pass a NULL tcon to ensure we don't
 963	 * make a round trip to the server. This only works for SMB2+.
 964	 */
 965	rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
 966				       &fattr->cf_uniqueid, data);
 967	if (rc) {
 968		/*
 969		 * If that fails reuse existing ino or generate one
 970		 * and disable server ones
 971		 */
 972		if (*inode)
 973			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
 974		else {
 975			fattr->cf_uniqueid = iunique(sb, ROOT_I);
 976			cifs_autodisable_serverino(cifs_sb);
 977		}
 978		return;
 979	}
 980
 981	/* If no errors, check for zero root inode (invalid) */
 982	if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
 983		cifs_dbg(FYI, "Invalid (0) inodenum\n");
 984		if (*inode) {
 985			/* reuse */
 986			fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
 987		} else {
 988			/* make an ino by hashing the UNC */
 989			fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
 990			fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
 991		}
 992	}
 993}
 994
 995static inline bool is_inode_cache_good(struct inode *ino)
 996{
 997	return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
 998}
 999
1000static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1001				 struct super_block *sb,
1002				 const unsigned int xid,
1003				 struct cifs_tcon *tcon,
1004				 const char *full_path,
1005				 struct cifs_fattr *fattr)
1006{
1007	struct TCP_Server_Info *server = tcon->ses->server;
1008	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1009	struct kvec rsp_iov, *iov = NULL;
1010	int rsp_buftype = CIFS_NO_BUFFER;
1011	u32 tag = data->reparse.tag;
1012	int rc = 0;
1013
1014	if (!tag && server->ops->query_reparse_point) {
1015		rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1016						      full_path, &tag,
1017						      &rsp_iov, &rsp_buftype);
1018		if (!rc)
1019			iov = &rsp_iov;
1020	} else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1021		   data->reparse.io.iov.iov_base) {
1022		iov = &data->reparse.io.iov;
1023	}
1024
1025	rc = -EOPNOTSUPP;
1026	switch ((data->reparse.tag = tag)) {
1027	case 0: /* SMB1 symlink */
1028		if (server->ops->query_symlink) {
1029			rc = server->ops->query_symlink(xid, tcon,
1030							cifs_sb, full_path,
1031							&data->symlink_target);
1032		}
1033		break;
1034	case IO_REPARSE_TAG_MOUNT_POINT:
1035		cifs_create_junction_fattr(fattr, sb);
1036		rc = 0;
1037		goto out;
1038	default:
1039		/* Check for cached reparse point data */
1040		if (data->symlink_target || data->reparse.buf) {
1041			rc = 0;
1042		} else if (iov && server->ops->parse_reparse_point) {
1043			rc = server->ops->parse_reparse_point(cifs_sb,
1044							      iov, data);
1045		}
1046		break;
1047	}
1048
1049	if (tcon->posix_extensions)
1050		smb311_posix_info_to_fattr(fattr, data, sb);
1051	else
1052		cifs_open_info_to_fattr(fattr, data, sb);
1053out:
1054	fattr->cf_cifstag = data->reparse.tag;
1055	free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1056	return rc;
1057}
1058
1059static int cifs_get_fattr(struct cifs_open_info_data *data,
1060			  struct super_block *sb, int xid,
1061			  const struct cifs_fid *fid,
1062			  struct cifs_fattr *fattr,
1063			  struct inode **inode,
1064			  const char *full_path)
1065{
1066	struct cifs_open_info_data tmp_data = {};
1067	struct cifs_tcon *tcon;
1068	struct TCP_Server_Info *server;
1069	struct tcon_link *tlink;
1070	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1071	void *smb1_backup_rsp_buf = NULL;
1072	int rc = 0;
1073	int tmprc = 0;
1074
1075	tlink = cifs_sb_tlink(cifs_sb);
1076	if (IS_ERR(tlink))
1077		return PTR_ERR(tlink);
1078	tcon = tlink_tcon(tlink);
1079	server = tcon->ses->server;
1080
1081	/*
1082	 * 1. Fetch file metadata if not provided (data)
1083	 */
1084
1085	if (!data) {
1086		rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1087						  full_path, &tmp_data);
1088		data = &tmp_data;
1089	}
1090
1091	/*
1092	 * 2. Convert it to internal cifs metadata (fattr)
1093	 */
1094
1095	switch (rc) {
1096	case 0:
1097		/*
1098		 * If the file is a reparse point, it is more complicated
1099		 * since we have to check if its reparse tag matches a known
1100		 * special file type e.g. symlink or fifo or char etc.
1101		 */
1102		if (cifs_open_data_reparse(data)) {
1103			rc = reparse_info_to_fattr(data, sb, xid, tcon,
1104						   full_path, fattr);
1105		} else {
1106			cifs_open_info_to_fattr(fattr, data, sb);
1107		}
1108		if (!rc && *inode &&
1109		    (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING))
1110			cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1111		break;
1112	case -EREMOTE:
1113		/* DFS link, no metadata available on this server */
1114		cifs_create_junction_fattr(fattr, sb);
1115		rc = 0;
1116		break;
1117	case -EACCES:
1118#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1119		/*
1120		 * perm errors, try again with backup flags if possible
1121		 *
1122		 * For SMB2 and later the backup intent flag
1123		 * is already sent if needed on open and there
1124		 * is no path based FindFirst operation to use
1125		 * to retry with
1126		 */
1127		if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1128			/* for easier reading */
1129			FILE_ALL_INFO *fi;
1130			FILE_DIRECTORY_INFO *fdi;
1131			SEARCH_ID_FULL_DIR_INFO *si;
1132
1133			rc = cifs_backup_query_path_info(xid, tcon, sb,
1134							 full_path,
1135							 &smb1_backup_rsp_buf,
1136							 &fi);
1137			if (rc)
1138				goto out;
1139
1140			move_cifs_info_to_smb2(&data->fi, fi);
1141			fdi = (FILE_DIRECTORY_INFO *)fi;
1142			si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1143
1144			cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1145			fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1146			/* uniqueid set, skip get inum step */
1147			goto handle_mnt_opt;
1148		} else {
1149			/* nothing we can do, bail out */
1150			goto out;
1151		}
1152#else
1153		goto out;
1154#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1155		break;
1156	default:
1157		cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1158		goto out;
1159	}
1160
1161	/*
1162	 * 3. Get or update inode number (fattr->cf_uniqueid)
1163	 */
1164
1165	cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1166
1167	/*
1168	 * 4. Tweak fattr based on mount options
1169	 */
1170#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1171handle_mnt_opt:
1172#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1173	/* query for SFU type info if supported and needed */
1174	if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1175	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1176		tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1177		if (tmprc)
1178			cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1179	}
1180
1181	/* fill in 0777 bits from ACL */
1182	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1183		rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1184				       true, full_path, fid);
1185		if (rc == -EREMOTE)
1186			rc = 0;
1187		if (rc) {
1188			cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1189				 __func__, rc);
1190			goto out;
1191		}
1192	} else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1193		rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1194				       false, full_path, fid);
1195		if (rc == -EREMOTE)
1196			rc = 0;
1197		if (rc) {
1198			cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1199				 __func__, rc);
1200			goto out;
1201		}
1202	}
1203
1204	/* fill in remaining high mode bits e.g. SUID, VTX */
1205	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1206		cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1207
1208	/* check for Minshall+French symlinks */
1209	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1210		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1211		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1212	}
1213
1214out:
1215	cifs_buf_release(smb1_backup_rsp_buf);
1216	cifs_put_tlink(tlink);
1217	cifs_free_open_info(&tmp_data);
1218	return rc;
1219}
1220
1221int cifs_get_inode_info(struct inode **inode,
1222			const char *full_path,
1223			struct cifs_open_info_data *data,
1224			struct super_block *sb, int xid,
1225			const struct cifs_fid *fid)
1226{
1227	struct cifs_fattr fattr = {};
1228	int rc;
1229
1230	if (is_inode_cache_good(*inode)) {
1231		cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1232		return 0;
1233	}
1234
1235	rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1236	if (rc)
1237		goto out;
1238
1239	rc = update_inode_info(sb, &fattr, inode);
1240out:
1241	kfree(fattr.cf_symlink_target);
1242	return rc;
1243}
1244
1245static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1246				  struct cifs_fattr *fattr,
1247				  const char *full_path,
1248				  struct super_block *sb,
1249				  const unsigned int xid)
1250{
1251	struct cifs_open_info_data tmp_data = {};
1252	struct TCP_Server_Info *server;
1253	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1254	struct cifs_tcon *tcon;
1255	struct tcon_link *tlink;
1256	int tmprc;
1257	int rc = 0;
1258
1259	tlink = cifs_sb_tlink(cifs_sb);
1260	if (IS_ERR(tlink))
1261		return PTR_ERR(tlink);
1262	tcon = tlink_tcon(tlink);
1263	server = tcon->ses->server;
1264
1265	/*
1266	 * 1. Fetch file metadata if not provided (data)
1267	 */
1268	if (!data) {
1269		rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1270						  full_path, &tmp_data);
1271		data = &tmp_data;
1272	}
1273
1274	/*
1275	 * 2. Convert it to internal cifs metadata (fattr)
1276	 */
1277
1278	switch (rc) {
1279	case 0:
1280		if (cifs_open_data_reparse(data)) {
1281			rc = reparse_info_to_fattr(data, sb, xid, tcon,
1282						   full_path, fattr);
1283		} else {
1284			smb311_posix_info_to_fattr(fattr, data, sb);
1285		}
1286		break;
1287	case -EREMOTE:
1288		/* DFS link, no metadata available on this server */
1289		cifs_create_junction_fattr(fattr, sb);
1290		rc = 0;
1291		break;
1292	case -EACCES:
1293		/*
1294		 * For SMB2 and later the backup intent flag
1295		 * is already sent if needed on open and there
1296		 * is no path based FindFirst operation to use
1297		 * to retry with so nothing we can do, bail out
1298		 */
1299		goto out;
1300	default:
1301		cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1302		goto out;
1303	}
1304
1305	/*
1306	 * 3. Tweak fattr based on mount options
1307	 */
1308	/* check for Minshall+French symlinks */
1309	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1310		tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1311		cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1312	}
1313
1314out:
1315	cifs_put_tlink(tlink);
1316	cifs_free_open_info(data);
1317	return rc;
1318}
1319
1320int smb311_posix_get_inode_info(struct inode **inode,
1321				const char *full_path,
1322				struct cifs_open_info_data *data,
1323				struct super_block *sb,
1324				const unsigned int xid)
1325{
1326	struct cifs_fattr fattr = {};
1327	int rc;
1328
1329	if (is_inode_cache_good(*inode)) {
1330		cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1331		return 0;
1332	}
1333
1334	rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1335	if (rc)
1336		goto out;
1337
1338	rc = update_inode_info(sb, &fattr, inode);
1339	if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1340		cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1341out:
1342	kfree(fattr.cf_symlink_target);
1343	return rc;
1344}
1345
1346static const struct inode_operations cifs_ipc_inode_ops = {
1347	.lookup = cifs_lookup,
1348};
1349
1350static int
1351cifs_find_inode(struct inode *inode, void *opaque)
1352{
1353	struct cifs_fattr *fattr = opaque;
1354
1355	/* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1356
1357	/* don't match inode with different uniqueid */
1358	if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1359		return 0;
1360
1361	/* use createtime like an i_generation field */
1362	if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1363		return 0;
1364
1365	/* don't match inode of different type */
1366	if (inode_wrong_type(inode, fattr->cf_mode))
1367		return 0;
1368
1369	/* if it's not a directory or has no dentries, then flag it */
1370	if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1371		fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1372
1373	return 1;
1374}
1375
1376static int
1377cifs_init_inode(struct inode *inode, void *opaque)
1378{
1379	struct cifs_fattr *fattr = opaque;
1380
1381	CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1382	CIFS_I(inode)->createtime = fattr->cf_createtime;
1383	return 0;
1384}
1385
1386/*
1387 * walk dentry list for an inode and report whether it has aliases that
1388 * are hashed. We use this to determine if a directory inode can actually
1389 * be used.
1390 */
1391static bool
1392inode_has_hashed_dentries(struct inode *inode)
1393{
1394	struct dentry *dentry;
1395
1396	spin_lock(&inode->i_lock);
1397	hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1398		if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1399			spin_unlock(&inode->i_lock);
1400			return true;
1401		}
1402	}
1403	spin_unlock(&inode->i_lock);
1404	return false;
1405}
1406
1407/* Given fattrs, get a corresponding inode */
1408struct inode *
1409cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1410{
1411	unsigned long hash;
1412	struct inode *inode;
1413
1414retry_iget5_locked:
1415	cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1416
1417	/* hash down to 32-bits on 32-bit arch */
1418	hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1419
1420	inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1421	if (inode) {
1422		/* was there a potentially problematic inode collision? */
1423		if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1424			fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1425
1426			if (inode_has_hashed_dentries(inode)) {
1427				cifs_autodisable_serverino(CIFS_SB(sb));
1428				iput(inode);
1429				fattr->cf_uniqueid = iunique(sb, ROOT_I);
1430				goto retry_iget5_locked;
1431			}
1432		}
1433
1434		/* can't fail - see cifs_find_inode() */
1435		cifs_fattr_to_inode(inode, fattr, false);
1436		if (sb->s_flags & SB_NOATIME)
1437			inode->i_flags |= S_NOATIME | S_NOCMTIME;
1438		if (inode->i_state & I_NEW) {
1439			inode->i_ino = hash;
1440			cifs_fscache_get_inode_cookie(inode);
1441			unlock_new_inode(inode);
1442		}
1443	}
1444
1445	return inode;
1446}
1447
1448/* gets root inode */
1449struct inode *cifs_root_iget(struct super_block *sb)
1450{
1451	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1452	struct cifs_fattr fattr = {};
1453	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1454	struct inode *inode = NULL;
1455	unsigned int xid;
1456	char *path = NULL;
1457	int len;
1458	int rc;
1459
1460	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1461	    && cifs_sb->prepath) {
1462		len = strlen(cifs_sb->prepath);
1463		path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1464		if (path == NULL)
1465			return ERR_PTR(-ENOMEM);
1466		path[0] = '/';
1467		memcpy(path+1, cifs_sb->prepath, len);
1468	} else {
1469		path = kstrdup("", GFP_KERNEL);
1470		if (path == NULL)
1471			return ERR_PTR(-ENOMEM);
1472	}
1473
1474	xid = get_xid();
1475	if (tcon->unix_ext) {
1476		rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1477		/* some servers mistakenly claim POSIX support */
1478		if (rc != -EOPNOTSUPP)
1479			goto iget_root;
1480		cifs_dbg(VFS, "server does not support POSIX extensions\n");
1481		tcon->unix_ext = false;
1482	}
1483
1484	convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1485	if (tcon->posix_extensions)
1486		rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1487	else
1488		rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1489
1490iget_root:
1491	if (!rc) {
1492		if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1493			fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1494			cifs_autodisable_serverino(cifs_sb);
1495		}
1496		inode = cifs_iget(sb, &fattr);
1497	}
1498
1499	if (!inode) {
1500		inode = ERR_PTR(rc);
1501		goto out;
1502	}
1503
1504	if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1505		cifs_mark_open_handles_for_deleted_file(inode, path);
1506
1507	if (rc && tcon->pipe) {
1508		cifs_dbg(FYI, "ipc connection - fake read inode\n");
1509		spin_lock(&inode->i_lock);
1510		inode->i_mode |= S_IFDIR;
1511		set_nlink(inode, 2);
1512		inode->i_op = &cifs_ipc_inode_ops;
1513		inode->i_fop = &simple_dir_operations;
1514		inode->i_uid = cifs_sb->ctx->linux_uid;
1515		inode->i_gid = cifs_sb->ctx->linux_gid;
1516		spin_unlock(&inode->i_lock);
1517	} else if (rc) {
1518		iget_failed(inode);
1519		inode = ERR_PTR(rc);
1520	}
1521
1522out:
1523	kfree(path);
1524	free_xid(xid);
1525	kfree(fattr.cf_symlink_target);
1526	return inode;
1527}
1528
1529int
1530cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1531		   const char *full_path, __u32 dosattr)
1532{
1533	bool set_time = false;
1534	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1535	struct TCP_Server_Info *server;
1536	FILE_BASIC_INFO	info_buf;
1537
1538	if (attrs == NULL)
1539		return -EINVAL;
1540
1541	server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1542	if (!server->ops->set_file_info)
1543		return -ENOSYS;
1544
1545	info_buf.Pad = 0;
1546
1547	if (attrs->ia_valid & ATTR_ATIME) {
1548		set_time = true;
1549		info_buf.LastAccessTime =
1550			cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1551	} else
1552		info_buf.LastAccessTime = 0;
1553
1554	if (attrs->ia_valid & ATTR_MTIME) {
1555		set_time = true;
1556		info_buf.LastWriteTime =
1557		    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1558	} else
1559		info_buf.LastWriteTime = 0;
1560
1561	/*
1562	 * Samba throws this field away, but windows may actually use it.
1563	 * Do not set ctime unless other time stamps are changed explicitly
1564	 * (i.e. by utimes()) since we would then have a mix of client and
1565	 * server times.
1566	 */
1567	if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1568		cifs_dbg(FYI, "CIFS - CTIME changed\n");
1569		info_buf.ChangeTime =
1570		    cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1571	} else
1572		info_buf.ChangeTime = 0;
1573
1574	info_buf.CreationTime = 0;	/* don't change */
1575	info_buf.Attributes = cpu_to_le32(dosattr);
1576
1577	return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1578}
1579
1580#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1581/*
1582 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1583 * and rename it to a random name that hopefully won't conflict with
1584 * anything else.
1585 */
1586int
1587cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1588			   const unsigned int xid)
1589{
1590	int oplock = 0;
1591	int rc;
1592	struct cifs_fid fid;
1593	struct cifs_open_parms oparms;
1594	struct inode *inode = d_inode(dentry);
1595	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1596	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1597	struct tcon_link *tlink;
1598	struct cifs_tcon *tcon;
1599	__u32 dosattr, origattr;
1600	FILE_BASIC_INFO *info_buf = NULL;
1601
1602	tlink = cifs_sb_tlink(cifs_sb);
1603	if (IS_ERR(tlink))
1604		return PTR_ERR(tlink);
1605	tcon = tlink_tcon(tlink);
1606
1607	/*
1608	 * We cannot rename the file if the server doesn't support
1609	 * CAP_INFOLEVEL_PASSTHRU
1610	 */
1611	if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1612		rc = -EBUSY;
1613		goto out;
1614	}
1615
1616	oparms = (struct cifs_open_parms) {
1617		.tcon = tcon,
1618		.cifs_sb = cifs_sb,
1619		.desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1620		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1621		.disposition = FILE_OPEN,
1622		.path = full_path,
1623		.fid = &fid,
1624	};
1625
1626	rc = CIFS_open(xid, &oparms, &oplock, NULL);
1627	if (rc != 0)
1628		goto out;
1629
1630	origattr = cifsInode->cifsAttrs;
1631	if (origattr == 0)
1632		origattr |= ATTR_NORMAL;
1633
1634	dosattr = origattr & ~ATTR_READONLY;
1635	if (dosattr == 0)
1636		dosattr |= ATTR_NORMAL;
1637	dosattr |= ATTR_HIDDEN;
1638
1639	/* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1640	if (dosattr != origattr) {
1641		info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1642		if (info_buf == NULL) {
1643			rc = -ENOMEM;
1644			goto out_close;
1645		}
1646		info_buf->Attributes = cpu_to_le32(dosattr);
1647		rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1648					current->tgid);
1649		/* although we would like to mark the file hidden
1650 		   if that fails we will still try to rename it */
1651		if (!rc)
1652			cifsInode->cifsAttrs = dosattr;
1653		else
1654			dosattr = origattr; /* since not able to change them */
1655	}
1656
1657	/* rename the file */
1658	rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1659				   cifs_sb->local_nls,
1660				   cifs_remap(cifs_sb));
1661	if (rc != 0) {
1662		rc = -EBUSY;
1663		goto undo_setattr;
1664	}
1665
1666	/* try to set DELETE_ON_CLOSE */
1667	if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1668		rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1669					       current->tgid);
1670		/*
1671		 * some samba versions return -ENOENT when we try to set the
1672		 * file disposition here. Likely a samba bug, but work around
1673		 * it for now. This means that some cifsXXX files may hang
1674		 * around after they shouldn't.
1675		 *
1676		 * BB: remove this hack after more servers have the fix
1677		 */
1678		if (rc == -ENOENT)
1679			rc = 0;
1680		else if (rc != 0) {
1681			rc = -EBUSY;
1682			goto undo_rename;
1683		}
1684		set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1685	}
1686
1687out_close:
1688	CIFSSMBClose(xid, tcon, fid.netfid);
1689out:
1690	kfree(info_buf);
1691	cifs_put_tlink(tlink);
1692	return rc;
1693
1694	/*
1695	 * reset everything back to the original state. Don't bother
1696	 * dealing with errors here since we can't do anything about
1697	 * them anyway.
1698	 */
1699undo_rename:
1700	CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1701				cifs_sb->local_nls, cifs_remap(cifs_sb));
1702undo_setattr:
1703	if (dosattr != origattr) {
1704		info_buf->Attributes = cpu_to_le32(origattr);
1705		if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1706					current->tgid))
1707			cifsInode->cifsAttrs = origattr;
1708	}
1709
1710	goto out_close;
1711}
1712#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1713
1714/* copied from fs/nfs/dir.c with small changes */
1715static void
1716cifs_drop_nlink(struct inode *inode)
1717{
1718	spin_lock(&inode->i_lock);
1719	if (inode->i_nlink > 0)
1720		drop_nlink(inode);
1721	spin_unlock(&inode->i_lock);
1722}
1723
1724/*
1725 * If d_inode(dentry) is null (usually meaning the cached dentry
1726 * is a negative dentry) then we would attempt a standard SMB delete, but
1727 * if that fails we can not attempt the fall back mechanisms on EACCES
1728 * but will return the EACCES to the caller. Note that the VFS does not call
1729 * unlink on negative dentries currently.
1730 */
1731int cifs_unlink(struct inode *dir, struct dentry *dentry)
1732{
1733	int rc = 0;
1734	unsigned int xid;
1735	const char *full_path;
1736	void *page;
1737	struct inode *inode = d_inode(dentry);
1738	struct cifsInodeInfo *cifs_inode;
1739	struct super_block *sb = dir->i_sb;
1740	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1741	struct tcon_link *tlink;
1742	struct cifs_tcon *tcon;
1743	struct TCP_Server_Info *server;
1744	struct iattr *attrs = NULL;
1745	__u32 dosattr = 0, origattr = 0;
1746
1747	cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1748
1749	if (unlikely(cifs_forced_shutdown(cifs_sb)))
1750		return -EIO;
1751
1752	tlink = cifs_sb_tlink(cifs_sb);
1753	if (IS_ERR(tlink))
1754		return PTR_ERR(tlink);
1755	tcon = tlink_tcon(tlink);
1756	server = tcon->ses->server;
1757
1758	xid = get_xid();
1759	page = alloc_dentry_path();
1760
1761	if (tcon->nodelete) {
1762		rc = -EACCES;
1763		goto unlink_out;
1764	}
1765
1766	/* Unlink can be called from rename so we can not take the
1767	 * sb->s_vfs_rename_mutex here */
1768	full_path = build_path_from_dentry(dentry, page);
1769	if (IS_ERR(full_path)) {
1770		rc = PTR_ERR(full_path);
1771		goto unlink_out;
1772	}
1773
1774	cifs_close_deferred_file_under_dentry(tcon, full_path);
1775#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1776	if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1777				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1778		rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1779			SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1780			cifs_remap(cifs_sb));
1781		cifs_dbg(FYI, "posix del rc %d\n", rc);
1782		if ((rc == 0) || (rc == -ENOENT))
1783			goto psx_del_no_retry;
1784	}
1785#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1786
1787retry_std_delete:
1788	if (!server->ops->unlink) {
1789		rc = -ENOSYS;
1790		goto psx_del_no_retry;
1791	}
1792
1793	rc = server->ops->unlink(xid, tcon, full_path, cifs_sb, dentry);
1794
1795psx_del_no_retry:
1796	if (!rc) {
1797		if (inode) {
1798			cifs_mark_open_handles_for_deleted_file(inode, full_path);
1799			cifs_drop_nlink(inode);
1800		}
1801	} else if (rc == -ENOENT) {
1802		d_drop(dentry);
1803	} else if (rc == -EBUSY) {
1804		if (server->ops->rename_pending_delete) {
1805			rc = server->ops->rename_pending_delete(full_path,
1806								dentry, xid);
1807			if (rc == 0) {
1808				cifs_mark_open_handles_for_deleted_file(inode, full_path);
1809				cifs_drop_nlink(inode);
1810			}
1811		}
1812	} else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1813		attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1814		if (attrs == NULL) {
1815			rc = -ENOMEM;
1816			goto out_reval;
1817		}
1818
1819		/* try to reset dos attributes */
1820		cifs_inode = CIFS_I(inode);
1821		origattr = cifs_inode->cifsAttrs;
1822		if (origattr == 0)
1823			origattr |= ATTR_NORMAL;
1824		dosattr = origattr & ~ATTR_READONLY;
1825		if (dosattr == 0)
1826			dosattr |= ATTR_NORMAL;
1827		dosattr |= ATTR_HIDDEN;
1828
1829		rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1830		if (rc != 0)
1831			goto out_reval;
1832
1833		goto retry_std_delete;
1834	}
1835
1836	/* undo the setattr if we errored out and it's needed */
1837	if (rc != 0 && dosattr != 0)
1838		cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1839
1840out_reval:
1841	if (inode) {
1842		cifs_inode = CIFS_I(inode);
1843		cifs_inode->time = 0;	/* will force revalidate to get info
1844					   when needed */
1845		inode_set_ctime_current(inode);
1846	}
1847	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1848	cifs_inode = CIFS_I(dir);
1849	CIFS_I(dir)->time = 0;	/* force revalidate of dir as well */
1850unlink_out:
1851	free_dentry_path(page);
1852	kfree(attrs);
1853	free_xid(xid);
1854	cifs_put_tlink(tlink);
1855	return rc;
1856}
1857
1858static int
1859cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1860		 const char *full_path, struct cifs_sb_info *cifs_sb,
1861		 struct cifs_tcon *tcon, const unsigned int xid)
1862{
1863	int rc = 0;
1864	struct inode *inode = NULL;
1865
1866	if (tcon->posix_extensions) {
1867		rc = smb311_posix_get_inode_info(&inode, full_path,
1868						 NULL, parent->i_sb, xid);
1869#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1870	} else if (tcon->unix_ext) {
1871		rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1872					      xid);
1873#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1874	} else {
1875		rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1876					 xid, NULL);
1877	}
1878
1879	if (rc)
1880		return rc;
1881
1882	if (!S_ISDIR(inode->i_mode)) {
1883		/*
1884		 * mkdir succeeded, but another client has managed to remove the
1885		 * sucker and replace it with non-directory.  Return success,
1886		 * but don't leave the child in dcache.
1887		 */
1888		 iput(inode);
1889		 d_drop(dentry);
1890		 return 0;
1891	}
1892	/*
1893	 * setting nlink not necessary except in cases where we failed to get it
1894	 * from the server or was set bogus. Also, since this is a brand new
1895	 * inode, no need to grab the i_lock before setting the i_nlink.
1896	 */
1897	if (inode->i_nlink < 2)
1898		set_nlink(inode, 2);
1899	mode &= ~current_umask();
1900	/* must turn on setgid bit if parent dir has it */
1901	if (parent->i_mode & S_ISGID)
1902		mode |= S_ISGID;
1903
1904#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1905	if (tcon->unix_ext) {
1906		struct cifs_unix_set_info_args args = {
1907			.mode	= mode,
1908			.ctime	= NO_CHANGE_64,
1909			.atime	= NO_CHANGE_64,
1910			.mtime	= NO_CHANGE_64,
1911			.device	= 0,
1912		};
1913		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1914			args.uid = current_fsuid();
1915			if (parent->i_mode & S_ISGID)
1916				args.gid = parent->i_gid;
1917			else
1918				args.gid = current_fsgid();
1919		} else {
1920			args.uid = INVALID_UID; /* no change */
1921			args.gid = INVALID_GID; /* no change */
1922		}
1923		CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1924				       cifs_sb->local_nls,
1925				       cifs_remap(cifs_sb));
1926	} else {
1927#else
1928	{
1929#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1930		struct TCP_Server_Info *server = tcon->ses->server;
1931		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1932		    (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1933			server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1934						   tcon, xid);
1935		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1936			inode->i_mode = (mode | S_IFDIR);
1937
1938		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1939			inode->i_uid = current_fsuid();
1940			if (inode->i_mode & S_ISGID)
1941				inode->i_gid = parent->i_gid;
1942			else
1943				inode->i_gid = current_fsgid();
1944		}
1945	}
1946	d_instantiate(dentry, inode);
1947	return 0;
1948}
1949
1950#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1951static int
1952cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1953		 const char *full_path, struct cifs_sb_info *cifs_sb,
1954		 struct cifs_tcon *tcon, const unsigned int xid)
1955{
1956	int rc = 0;
1957	u32 oplock = 0;
1958	FILE_UNIX_BASIC_INFO *info = NULL;
1959	struct inode *newinode = NULL;
1960	struct cifs_fattr fattr;
1961
1962	info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1963	if (info == NULL) {
1964		rc = -ENOMEM;
1965		goto posix_mkdir_out;
1966	}
1967
1968	mode &= ~current_umask();
1969	rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1970			     NULL /* netfid */, info, &oplock, full_path,
1971			     cifs_sb->local_nls, cifs_remap(cifs_sb));
1972	if (rc == -EOPNOTSUPP)
1973		goto posix_mkdir_out;
1974	else if (rc) {
1975		cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1976		d_drop(dentry);
1977		goto posix_mkdir_out;
1978	}
1979
1980	if (info->Type == cpu_to_le32(-1))
1981		/* no return info, go query for it */
1982		goto posix_mkdir_get_info;
1983	/*
1984	 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1985	 * need to set uid/gid.
1986	 */
1987
1988	cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1989	cifs_fill_uniqueid(inode->i_sb, &fattr);
1990	newinode = cifs_iget(inode->i_sb, &fattr);
1991	if (!newinode)
1992		goto posix_mkdir_get_info;
1993
1994	d_instantiate(dentry, newinode);
1995
1996#ifdef CONFIG_CIFS_DEBUG2
1997	cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1998		 dentry, dentry, newinode);
1999
2000	if (newinode->i_nlink != 2)
2001		cifs_dbg(FYI, "unexpected number of links %d\n",
2002			 newinode->i_nlink);
2003#endif
2004
2005posix_mkdir_out:
2006	kfree(info);
2007	return rc;
2008posix_mkdir_get_info:
2009	rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2010			      xid);
2011	goto posix_mkdir_out;
2012}
2013#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2014
2015int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2016	       struct dentry *direntry, umode_t mode)
2017{
2018	int rc = 0;
2019	unsigned int xid;
2020	struct cifs_sb_info *cifs_sb;
2021	struct tcon_link *tlink;
2022	struct cifs_tcon *tcon;
2023	struct TCP_Server_Info *server;
2024	const char *full_path;
2025	void *page;
2026
2027	cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2028		 mode, inode);
2029
2030	cifs_sb = CIFS_SB(inode->i_sb);
2031	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2032		return -EIO;
2033	tlink = cifs_sb_tlink(cifs_sb);
2034	if (IS_ERR(tlink))
2035		return PTR_ERR(tlink);
2036	tcon = tlink_tcon(tlink);
2037
2038	xid = get_xid();
2039
2040	page = alloc_dentry_path();
2041	full_path = build_path_from_dentry(direntry, page);
2042	if (IS_ERR(full_path)) {
2043		rc = PTR_ERR(full_path);
2044		goto mkdir_out;
2045	}
2046
2047	server = tcon->ses->server;
2048
2049	if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2050		rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2051					      cifs_sb);
2052		d_drop(direntry); /* for time being always refresh inode info */
2053		goto mkdir_out;
2054	}
2055
2056#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2057	if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2058				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2059		rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2060				      tcon, xid);
2061		if (rc != -EOPNOTSUPP)
2062			goto mkdir_out;
2063	}
2064#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2065
2066	if (!server->ops->mkdir) {
2067		rc = -ENOSYS;
2068		goto mkdir_out;
2069	}
2070
2071	/* BB add setting the equivalent of mode via CreateX w/ACLs */
2072	rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2073	if (rc) {
2074		cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2075		d_drop(direntry);
2076		goto mkdir_out;
2077	}
2078
2079	/* TODO: skip this for smb2/smb3 */
2080	rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2081			      xid);
2082mkdir_out:
2083	/*
2084	 * Force revalidate to get parent dir info when needed since cached
2085	 * attributes are invalid now.
2086	 */
2087	CIFS_I(inode)->time = 0;
2088	free_dentry_path(page);
2089	free_xid(xid);
2090	cifs_put_tlink(tlink);
2091	return rc;
2092}
2093
2094int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2095{
2096	int rc = 0;
2097	unsigned int xid;
2098	struct cifs_sb_info *cifs_sb;
2099	struct tcon_link *tlink;
2100	struct cifs_tcon *tcon;
2101	struct TCP_Server_Info *server;
2102	const char *full_path;
2103	void *page = alloc_dentry_path();
2104	struct cifsInodeInfo *cifsInode;
2105
2106	cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2107
2108	xid = get_xid();
2109
2110	full_path = build_path_from_dentry(direntry, page);
2111	if (IS_ERR(full_path)) {
2112		rc = PTR_ERR(full_path);
2113		goto rmdir_exit;
2114	}
2115
2116	cifs_sb = CIFS_SB(inode->i_sb);
2117	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2118		rc = -EIO;
2119		goto rmdir_exit;
2120	}
2121
2122	tlink = cifs_sb_tlink(cifs_sb);
2123	if (IS_ERR(tlink)) {
2124		rc = PTR_ERR(tlink);
2125		goto rmdir_exit;
2126	}
2127	tcon = tlink_tcon(tlink);
2128	server = tcon->ses->server;
2129
2130	if (!server->ops->rmdir) {
2131		rc = -ENOSYS;
2132		cifs_put_tlink(tlink);
2133		goto rmdir_exit;
2134	}
2135
2136	if (tcon->nodelete) {
2137		rc = -EACCES;
2138		cifs_put_tlink(tlink);
2139		goto rmdir_exit;
2140	}
2141
2142	rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2143	cifs_put_tlink(tlink);
2144
2145	if (!rc) {
2146		spin_lock(&d_inode(direntry)->i_lock);
2147		i_size_write(d_inode(direntry), 0);
2148		clear_nlink(d_inode(direntry));
2149		spin_unlock(&d_inode(direntry)->i_lock);
2150	}
2151
2152	cifsInode = CIFS_I(d_inode(direntry));
2153	/* force revalidate to go get info when needed */
2154	cifsInode->time = 0;
2155
2156	cifsInode = CIFS_I(inode);
2157	/*
2158	 * Force revalidate to get parent dir info when needed since cached
2159	 * attributes are invalid now.
2160	 */
2161	cifsInode->time = 0;
2162
2163	inode_set_ctime_current(d_inode(direntry));
2164	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2165
2166rmdir_exit:
2167	free_dentry_path(page);
2168	free_xid(xid);
2169	return rc;
2170}
2171
2172static int
2173cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2174	       const char *from_path, struct dentry *to_dentry,
2175	       const char *to_path)
2176{
2177	struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2178	struct tcon_link *tlink;
2179	struct cifs_tcon *tcon;
2180	struct TCP_Server_Info *server;
2181#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2182	struct cifs_fid fid;
2183	struct cifs_open_parms oparms;
2184	int oplock;
2185#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2186	int rc;
2187
2188	tlink = cifs_sb_tlink(cifs_sb);
2189	if (IS_ERR(tlink))
2190		return PTR_ERR(tlink);
2191	tcon = tlink_tcon(tlink);
2192	server = tcon->ses->server;
2193
2194	if (!server->ops->rename)
2195		return -ENOSYS;
2196
2197	/* try path-based rename first */
2198	rc = server->ops->rename(xid, tcon, from_dentry,
2199				 from_path, to_path, cifs_sb);
2200
2201	/*
2202	 * Don't bother with rename by filehandle unless file is busy and
2203	 * source. Note that cross directory moves do not work with
2204	 * rename by filehandle to various Windows servers.
2205	 */
2206	if (rc == 0 || rc != -EBUSY)
2207		goto do_rename_exit;
2208
2209	/* Don't fall back to using SMB on SMB 2+ mount */
2210	if (server->vals->protocol_id != 0)
2211		goto do_rename_exit;
2212
2213#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2214	/* open-file renames don't work across directories */
2215	if (to_dentry->d_parent != from_dentry->d_parent)
2216		goto do_rename_exit;
2217
2218	oparms = (struct cifs_open_parms) {
2219		.tcon = tcon,
2220		.cifs_sb = cifs_sb,
2221		/* open the file to be renamed -- we need DELETE perms */
2222		.desired_access = DELETE,
2223		.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2224		.disposition = FILE_OPEN,
2225		.path = from_path,
2226		.fid = &fid,
2227	};
2228
2229	rc = CIFS_open(xid, &oparms, &oplock, NULL);
2230	if (rc == 0) {
2231		rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2232				(const char *) to_dentry->d_name.name,
2233				cifs_sb->local_nls, cifs_remap(cifs_sb));
2234		CIFSSMBClose(xid, tcon, fid.netfid);
2235	}
2236#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2237do_rename_exit:
2238	if (rc == 0)
2239		d_move(from_dentry, to_dentry);
2240	cifs_put_tlink(tlink);
2241	return rc;
2242}
2243
2244int
2245cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2246	     struct dentry *source_dentry, struct inode *target_dir,
2247	     struct dentry *target_dentry, unsigned int flags)
2248{
2249	const char *from_name, *to_name;
2250	void *page1, *page2;
2251	struct cifs_sb_info *cifs_sb;
2252	struct tcon_link *tlink;
2253	struct cifs_tcon *tcon;
2254	unsigned int xid;
2255	int rc, tmprc;
2256	int retry_count = 0;
2257	FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2258#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2259	FILE_UNIX_BASIC_INFO *info_buf_target;
2260#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2261
2262	if (flags & ~RENAME_NOREPLACE)
2263		return -EINVAL;
2264
2265	cifs_sb = CIFS_SB(source_dir->i_sb);
2266	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2267		return -EIO;
2268
2269	tlink = cifs_sb_tlink(cifs_sb);
2270	if (IS_ERR(tlink))
2271		return PTR_ERR(tlink);
2272	tcon = tlink_tcon(tlink);
2273
2274	page1 = alloc_dentry_path();
2275	page2 = alloc_dentry_path();
2276	xid = get_xid();
2277
2278	from_name = build_path_from_dentry(source_dentry, page1);
2279	if (IS_ERR(from_name)) {
2280		rc = PTR_ERR(from_name);
2281		goto cifs_rename_exit;
2282	}
2283
2284	to_name = build_path_from_dentry(target_dentry, page2);
2285	if (IS_ERR(to_name)) {
2286		rc = PTR_ERR(to_name);
2287		goto cifs_rename_exit;
2288	}
2289
2290	cifs_close_deferred_file_under_dentry(tcon, from_name);
2291	if (d_inode(target_dentry) != NULL)
2292		cifs_close_deferred_file_under_dentry(tcon, to_name);
2293
2294	rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2295			    to_name);
2296
2297	if (rc == -EACCES) {
2298		while (retry_count < 3) {
2299			cifs_close_all_deferred_files(tcon);
2300			rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2301					    to_name);
2302			if (rc != -EACCES)
2303				break;
2304			retry_count++;
2305		}
2306	}
2307
2308	/*
2309	 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2310	 */
2311	if (flags & RENAME_NOREPLACE)
2312		goto cifs_rename_exit;
2313
2314#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2315	if (rc == -EEXIST && tcon->unix_ext) {
2316		/*
2317		 * Are src and dst hardlinks of same inode? We can only tell
2318		 * with unix extensions enabled.
2319		 */
2320		info_buf_source =
2321			kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2322					GFP_KERNEL);
2323		if (info_buf_source == NULL) {
2324			rc = -ENOMEM;
2325			goto cifs_rename_exit;
2326		}
2327
2328		info_buf_target = info_buf_source + 1;
2329		tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2330					     info_buf_source,
2331					     cifs_sb->local_nls,
2332					     cifs_remap(cifs_sb));
2333		if (tmprc != 0)
2334			goto unlink_target;
2335
2336		tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2337					     info_buf_target,
2338					     cifs_sb->local_nls,
2339					     cifs_remap(cifs_sb));
2340
2341		if (tmprc == 0 && (info_buf_source->UniqueId ==
2342				   info_buf_target->UniqueId)) {
2343			/* same file, POSIX says that this is a noop */
2344			rc = 0;
2345			goto cifs_rename_exit;
2346		}
2347	}
2348	/*
2349	 * else ... BB we could add the same check for Windows by
2350	 * checking the UniqueId via FILE_INTERNAL_INFO
2351	 */
2352
2353unlink_target:
2354#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2355
2356	/* Try unlinking the target dentry if it's not negative */
2357	if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2358		if (d_is_dir(target_dentry))
2359			tmprc = cifs_rmdir(target_dir, target_dentry);
2360		else
2361			tmprc = cifs_unlink(target_dir, target_dentry);
2362		if (tmprc)
2363			goto cifs_rename_exit;
2364		rc = cifs_do_rename(xid, source_dentry, from_name,
2365				    target_dentry, to_name);
2366	}
2367
2368	/* force revalidate to go get info when needed */
2369	CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2370
2371cifs_rename_exit:
2372	kfree(info_buf_source);
2373	free_dentry_path(page2);
2374	free_dentry_path(page1);
2375	free_xid(xid);
2376	cifs_put_tlink(tlink);
2377	return rc;
2378}
2379
2380static bool
2381cifs_dentry_needs_reval(struct dentry *dentry)
2382{
2383	struct inode *inode = d_inode(dentry);
2384	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2385	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2386	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2387	struct cached_fid *cfid = NULL;
2388
2389	if (cifs_i->time == 0)
2390		return true;
2391
2392	if (CIFS_CACHE_READ(cifs_i))
2393		return false;
2394
2395	if (!lookupCacheEnabled)
2396		return true;
2397
2398	if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2399		spin_lock(&cfid->fid_lock);
2400		if (cfid->time && cifs_i->time > cfid->time) {
2401			spin_unlock(&cfid->fid_lock);
2402			close_cached_dir(cfid);
2403			return false;
2404		}
2405		spin_unlock(&cfid->fid_lock);
2406		close_cached_dir(cfid);
2407	}
2408	/*
2409	 * depending on inode type, check if attribute caching disabled for
2410	 * files or directories
2411	 */
2412	if (S_ISDIR(inode->i_mode)) {
2413		if (!cifs_sb->ctx->acdirmax)
2414			return true;
2415		if (!time_in_range(jiffies, cifs_i->time,
2416				   cifs_i->time + cifs_sb->ctx->acdirmax))
2417			return true;
2418	} else { /* file */
2419		if (!cifs_sb->ctx->acregmax)
2420			return true;
2421		if (!time_in_range(jiffies, cifs_i->time,
2422				   cifs_i->time + cifs_sb->ctx->acregmax))
2423			return true;
2424	}
2425
2426	/* hardlinked files w/ noserverino get "special" treatment */
2427	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2428	    S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2429		return true;
2430
2431	return false;
2432}
2433
2434/*
2435 * Zap the cache. Called when invalid_mapping flag is set.
2436 */
2437int
2438cifs_invalidate_mapping(struct inode *inode)
2439{
2440	int rc = 0;
2441
2442	if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2443		rc = invalidate_inode_pages2(inode->i_mapping);
2444		if (rc)
2445			cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2446				 __func__, inode, rc);
2447	}
2448
2449	return rc;
2450}
2451
2452/**
2453 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2454 *
2455 * @key:	currently unused
2456 * @mode:	the task state to sleep in
2457 */
2458static int
2459cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2460{
2461	schedule();
2462	if (signal_pending_state(mode, current))
2463		return -ERESTARTSYS;
2464	return 0;
2465}
2466
2467int
2468cifs_revalidate_mapping(struct inode *inode)
2469{
2470	int rc;
2471	unsigned long *flags = &CIFS_I(inode)->flags;
2472	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2473
2474	/* swapfiles are not supposed to be shared */
2475	if (IS_SWAPFILE(inode))
2476		return 0;
2477
2478	rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2479				     TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2480	if (rc)
2481		return rc;
2482
2483	if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2484		/* for cache=singleclient, do not invalidate */
2485		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2486			goto skip_invalidate;
2487
2488		rc = cifs_invalidate_mapping(inode);
2489		if (rc)
2490			set_bit(CIFS_INO_INVALID_MAPPING, flags);
2491	}
2492
2493skip_invalidate:
2494	clear_bit_unlock(CIFS_INO_LOCK, flags);
2495	smp_mb__after_atomic();
2496	wake_up_bit(flags, CIFS_INO_LOCK);
2497
2498	return rc;
2499}
2500
2501int
2502cifs_zap_mapping(struct inode *inode)
2503{
2504	set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2505	return cifs_revalidate_mapping(inode);
2506}
2507
2508int cifs_revalidate_file_attr(struct file *filp)
2509{
2510	int rc = 0;
2511	struct dentry *dentry = file_dentry(filp);
2512#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2513	struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2514#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2515
2516	if (!cifs_dentry_needs_reval(dentry))
2517		return rc;
2518
2519#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2520	if (tlink_tcon(cfile->tlink)->unix_ext)
2521		rc = cifs_get_file_info_unix(filp);
2522	else
2523#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2524		rc = cifs_get_file_info(filp);
2525
2526	return rc;
2527}
2528
2529int cifs_revalidate_dentry_attr(struct dentry *dentry)
2530{
2531	unsigned int xid;
2532	int rc = 0;
2533	struct inode *inode = d_inode(dentry);
2534	struct super_block *sb = dentry->d_sb;
2535	const char *full_path;
2536	void *page;
2537	int count = 0;
2538
2539	if (inode == NULL)
2540		return -ENOENT;
2541
2542	if (!cifs_dentry_needs_reval(dentry))
2543		return rc;
2544
2545	xid = get_xid();
2546
2547	page = alloc_dentry_path();
2548	full_path = build_path_from_dentry(dentry, page);
2549	if (IS_ERR(full_path)) {
2550		rc = PTR_ERR(full_path);
2551		goto out;
2552	}
2553
2554	cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2555		 full_path, inode, inode->i_count.counter,
2556		 dentry, cifs_get_time(dentry), jiffies);
2557
2558again:
2559	if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2560		rc = smb311_posix_get_inode_info(&inode, full_path,
2561						 NULL, sb, xid);
2562	} else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2563		rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2564	} else {
2565		rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2566					 xid, NULL);
2567	}
2568	if (rc == -EAGAIN && count++ < 10)
2569		goto again;
2570out:
2571	free_dentry_path(page);
2572	free_xid(xid);
2573
2574	return rc;
2575}
2576
2577int cifs_revalidate_file(struct file *filp)
2578{
2579	int rc;
2580	struct inode *inode = file_inode(filp);
2581
2582	rc = cifs_revalidate_file_attr(filp);
2583	if (rc)
2584		return rc;
2585
2586	return cifs_revalidate_mapping(inode);
2587}
2588
2589/* revalidate a dentry's inode attributes */
2590int cifs_revalidate_dentry(struct dentry *dentry)
2591{
2592	int rc;
2593	struct inode *inode = d_inode(dentry);
2594
2595	rc = cifs_revalidate_dentry_attr(dentry);
2596	if (rc)
2597		return rc;
2598
2599	return cifs_revalidate_mapping(inode);
2600}
2601
2602int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2603		 struct kstat *stat, u32 request_mask, unsigned int flags)
2604{
2605	struct dentry *dentry = path->dentry;
2606	struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2607	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2608	struct inode *inode = d_inode(dentry);
2609	int rc;
2610
2611	if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2612		return -EIO;
2613
2614	/*
2615	 * We need to be sure that all dirty pages are written and the server
2616	 * has actual ctime, mtime and file length.
2617	 */
2618	if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2619	    !CIFS_CACHE_READ(CIFS_I(inode)) &&
2620	    inode->i_mapping && inode->i_mapping->nrpages != 0) {
2621		rc = filemap_fdatawait(inode->i_mapping);
2622		if (rc) {
2623			mapping_set_error(inode->i_mapping, rc);
2624			return rc;
2625		}
2626	}
2627
2628	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2629		CIFS_I(inode)->time = 0; /* force revalidate */
2630
2631	/*
2632	 * If the caller doesn't require syncing, only sync if
2633	 * necessary (e.g. due to earlier truncate or setattr
2634	 * invalidating the cached metadata)
2635	 */
2636	if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2637	    (CIFS_I(inode)->time == 0)) {
2638		rc = cifs_revalidate_dentry_attr(dentry);
2639		if (rc)
2640			return rc;
2641	}
2642
2643	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2644	stat->blksize = cifs_sb->ctx->bsize;
2645	stat->ino = CIFS_I(inode)->uniqueid;
2646
2647	/* old CIFS Unix Extensions doesn't return create time */
2648	if (CIFS_I(inode)->createtime) {
2649		stat->result_mask |= STATX_BTIME;
2650		stat->btime =
2651		      cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2652	}
2653
2654	stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2655	if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2656		stat->attributes |= STATX_ATTR_COMPRESSED;
2657	if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2658		stat->attributes |= STATX_ATTR_ENCRYPTED;
2659
2660	/*
2661	 * If on a multiuser mount without unix extensions or cifsacl being
2662	 * enabled, and the admin hasn't overridden them, set the ownership
2663	 * to the fsuid/fsgid of the current process.
2664	 */
2665	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2666	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2667	    !tcon->unix_ext) {
2668		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2669			stat->uid = current_fsuid();
2670		if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2671			stat->gid = current_fsgid();
2672	}
2673	return 0;
2674}
2675
2676int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2677		u64 len)
2678{
2679	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2680	struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2681	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2682	struct TCP_Server_Info *server = tcon->ses->server;
2683	struct cifsFileInfo *cfile;
2684	int rc;
2685
2686	if (unlikely(cifs_forced_shutdown(cifs_sb)))
2687		return -EIO;
2688
2689	/*
2690	 * We need to be sure that all dirty pages are written as they
2691	 * might fill holes on the server.
2692	 */
2693	if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2694	    inode->i_mapping->nrpages != 0) {
2695		rc = filemap_fdatawait(inode->i_mapping);
2696		if (rc) {
2697			mapping_set_error(inode->i_mapping, rc);
2698			return rc;
2699		}
2700	}
2701
2702	cfile = find_readable_file(cifs_i, false);
2703	if (cfile == NULL)
2704		return -EINVAL;
2705
2706	if (server->ops->fiemap) {
2707		rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2708		cifsFileInfo_put(cfile);
2709		return rc;
2710	}
2711
2712	cifsFileInfo_put(cfile);
2713	return -EOPNOTSUPP;
2714}
2715
2716int cifs_truncate_page(struct address_space *mapping, loff_t from)
2717{
2718	pgoff_t index = from >> PAGE_SHIFT;
2719	unsigned offset = from & (PAGE_SIZE - 1);
2720	struct page *page;
2721	int rc = 0;
2722
2723	page = grab_cache_page(mapping, index);
2724	if (!page)
2725		return -ENOMEM;
2726
2727	zero_user_segment(page, offset, PAGE_SIZE);
2728	unlock_page(page);
2729	put_page(page);
2730	return rc;
2731}
2732
2733void cifs_setsize(struct inode *inode, loff_t offset)
2734{
2735	struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2736
2737	spin_lock(&inode->i_lock);
2738	i_size_write(inode, offset);
2739	spin_unlock(&inode->i_lock);
2740
2741	/* Cached inode must be refreshed on truncate */
2742	cifs_i->time = 0;
2743	truncate_pagecache(inode, offset);
2744}
2745
2746static int
2747cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2748		   unsigned int xid, const char *full_path, struct dentry *dentry)
2749{
2750	int rc;
2751	struct cifsFileInfo *open_file;
2752	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2753	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2754	struct tcon_link *tlink = NULL;
2755	struct cifs_tcon *tcon = NULL;
2756	struct TCP_Server_Info *server;
2757
2758	/*
2759	 * To avoid spurious oplock breaks from server, in the case of
2760	 * inodes that we already have open, avoid doing path based
2761	 * setting of file size if we can do it by handle.
2762	 * This keeps our caching token (oplock) and avoids timeouts
2763	 * when the local oplock break takes longer to flush
2764	 * writebehind data than the SMB timeout for the SetPathInfo
2765	 * request would allow
2766	 */
2767	open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2768	if (open_file) {
2769		tcon = tlink_tcon(open_file->tlink);
2770		server = tcon->ses->server;
2771		if (server->ops->set_file_size)
2772			rc = server->ops->set_file_size(xid, tcon, open_file,
2773							attrs->ia_size, false);
2774		else
2775			rc = -ENOSYS;
2776		cifsFileInfo_put(open_file);
2777		cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2778	} else
2779		rc = -EINVAL;
2780
2781	if (!rc)
2782		goto set_size_out;
2783
2784	if (tcon == NULL) {
2785		tlink = cifs_sb_tlink(cifs_sb);
2786		if (IS_ERR(tlink))
2787			return PTR_ERR(tlink);
2788		tcon = tlink_tcon(tlink);
2789		server = tcon->ses->server;
2790	}
2791
2792	/*
2793	 * Set file size by pathname rather than by handle either because no
2794	 * valid, writeable file handle for it was found or because there was
2795	 * an error setting it by handle.
2796	 */
2797	if (server->ops->set_path_size)
2798		rc = server->ops->set_path_size(xid, tcon, full_path,
2799						attrs->ia_size, cifs_sb, false, dentry);
2800	else
2801		rc = -ENOSYS;
2802	cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2803
2804	if (tlink)
2805		cifs_put_tlink(tlink);
2806
2807set_size_out:
2808	if (rc == 0) {
2809		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2810		cifs_setsize(inode, attrs->ia_size);
2811		/*
2812		 * i_blocks is not related to (i_size / i_blksize), but instead
2813		 * 512 byte (2**9) size is required for calculating num blocks.
2814		 * Until we can query the server for actual allocation size,
2815		 * this is best estimate we have for blocks allocated for a file
2816		 * Number of blocks must be rounded up so size 1 is not 0 blocks
2817		 */
2818		inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2819
2820		/*
2821		 * The man page of truncate says if the size changed,
2822		 * then the st_ctime and st_mtime fields for the file
2823		 * are updated.
2824		 */
2825		attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2826		attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2827
2828		cifs_truncate_page(inode->i_mapping, inode->i_size);
2829	}
2830
2831	return rc;
2832}
2833
2834#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2835static int
2836cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2837{
2838	int rc;
2839	unsigned int xid;
2840	const char *full_path;
2841	void *page = alloc_dentry_path();
2842	struct inode *inode = d_inode(direntry);
2843	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2844	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2845	struct tcon_link *tlink;
2846	struct cifs_tcon *pTcon;
2847	struct cifs_unix_set_info_args *args = NULL;
2848	struct cifsFileInfo *open_file;
2849
2850	cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2851		 direntry, attrs->ia_valid);
2852
2853	xid = get_xid();
2854
2855	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2856		attrs->ia_valid |= ATTR_FORCE;
2857
2858	rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2859	if (rc < 0)
2860		goto out;
2861
2862	full_path = build_path_from_dentry(direntry, page);
2863	if (IS_ERR(full_path)) {
2864		rc = PTR_ERR(full_path);
2865		goto out;
2866	}
2867
2868	/*
2869	 * Attempt to flush data before changing attributes. We need to do
2870	 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2871	 * ownership or mode then we may also need to do this. Here, we take
2872	 * the safe way out and just do the flush on all setattr requests. If
2873	 * the flush returns error, store it to report later and continue.
2874	 *
2875	 * BB: This should be smarter. Why bother flushing pages that
2876	 * will be truncated anyway? Also, should we error out here if
2877	 * the flush returns error?
2878	 */
2879	rc = filemap_write_and_wait(inode->i_mapping);
2880	if (is_interrupt_error(rc)) {
2881		rc = -ERESTARTSYS;
2882		goto out;
2883	}
2884
2885	mapping_set_error(inode->i_mapping, rc);
2886	rc = 0;
2887
2888	if (attrs->ia_valid & ATTR_SIZE) {
2889		rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
2890		if (rc != 0)
2891			goto out;
2892	}
2893
2894	/* skip mode change if it's just for clearing setuid/setgid */
2895	if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2896		attrs->ia_valid &= ~ATTR_MODE;
2897
2898	args = kmalloc(sizeof(*args), GFP_KERNEL);
2899	if (args == NULL) {
2900		rc = -ENOMEM;
2901		goto out;
2902	}
2903
2904	/* set up the struct */
2905	if (attrs->ia_valid & ATTR_MODE)
2906		args->mode = attrs->ia_mode;
2907	else
2908		args->mode = NO_CHANGE_64;
2909
2910	if (attrs->ia_valid & ATTR_UID)
2911		args->uid = attrs->ia_uid;
2912	else
2913		args->uid = INVALID_UID; /* no change */
2914
2915	if (attrs->ia_valid & ATTR_GID)
2916		args->gid = attrs->ia_gid;
2917	else
2918		args->gid = INVALID_GID; /* no change */
2919
2920	if (attrs->ia_valid & ATTR_ATIME)
2921		args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2922	else
2923		args->atime = NO_CHANGE_64;
2924
2925	if (attrs->ia_valid & ATTR_MTIME)
2926		args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2927	else
2928		args->mtime = NO_CHANGE_64;
2929
2930	if (attrs->ia_valid & ATTR_CTIME)
2931		args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2932	else
2933		args->ctime = NO_CHANGE_64;
2934
2935	args->device = 0;
2936	open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2937	if (open_file) {
2938		u16 nfid = open_file->fid.netfid;
2939		u32 npid = open_file->pid;
2940		pTcon = tlink_tcon(open_file->tlink);
2941		rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2942		cifsFileInfo_put(open_file);
2943	} else {
2944		tlink = cifs_sb_tlink(cifs_sb);
2945		if (IS_ERR(tlink)) {
2946			rc = PTR_ERR(tlink);
2947			goto out;
2948		}
2949		pTcon = tlink_tcon(tlink);
2950		rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2951				    cifs_sb->local_nls,
2952				    cifs_remap(cifs_sb));
2953		cifs_put_tlink(tlink);
2954	}
2955
2956	if (rc)
2957		goto out;
2958
2959	if ((attrs->ia_valid & ATTR_SIZE) &&
2960	    attrs->ia_size != i_size_read(inode)) {
2961		truncate_setsize(inode, attrs->ia_size);
2962		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2963		fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2964	}
2965
2966	setattr_copy(&nop_mnt_idmap, inode, attrs);
2967	mark_inode_dirty(inode);
2968
2969	/* force revalidate when any of these times are set since some
2970	   of the fs types (eg ext3, fat) do not have fine enough
2971	   time granularity to match protocol, and we do not have a
2972	   a way (yet) to query the server fs's time granularity (and
2973	   whether it rounds times down).
2974	*/
2975	if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2976		cifsInode->time = 0;
2977out:
2978	kfree(args);
2979	free_dentry_path(page);
2980	free_xid(xid);
2981	return rc;
2982}
2983#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2984
2985static int
2986cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2987{
2988	unsigned int xid;
2989	kuid_t uid = INVALID_UID;
2990	kgid_t gid = INVALID_GID;
2991	struct inode *inode = d_inode(direntry);
2992	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2993	struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2994	struct cifsFileInfo *wfile;
2995	struct cifs_tcon *tcon;
2996	const char *full_path;
2997	void *page = alloc_dentry_path();
2998	int rc = -EACCES;
2999	__u32 dosattr = 0;
3000	__u64 mode = NO_CHANGE_64;
3001
3002	xid = get_xid();
3003
3004	cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3005		 direntry, attrs->ia_valid);
3006
3007	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3008		attrs->ia_valid |= ATTR_FORCE;
3009
3010	rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3011	if (rc < 0)
3012		goto cifs_setattr_exit;
3013
3014	full_path = build_path_from_dentry(direntry, page);
3015	if (IS_ERR(full_path)) {
3016		rc = PTR_ERR(full_path);
3017		goto cifs_setattr_exit;
3018	}
3019
3020	/*
3021	 * Attempt to flush data before changing attributes. We need to do
3022	 * this for ATTR_SIZE and ATTR_MTIME.  If the flush of the data
3023	 * returns error, store it to report later and continue.
3024	 *
3025	 * BB: This should be smarter. Why bother flushing pages that
3026	 * will be truncated anyway? Also, should we error out here if
3027	 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3028	 */
3029	if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3030		rc = filemap_write_and_wait(inode->i_mapping);
3031		if (is_interrupt_error(rc)) {
3032			rc = -ERESTARTSYS;
3033			goto cifs_setattr_exit;
3034		}
3035		mapping_set_error(inode->i_mapping, rc);
3036	}
3037
3038	rc = 0;
3039
3040	if ((attrs->ia_valid & ATTR_MTIME) &&
3041	    !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3042		rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3043		if (!rc) {
3044			tcon = tlink_tcon(wfile->tlink);
3045			rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3046			cifsFileInfo_put(wfile);
3047			if (rc)
3048				goto cifs_setattr_exit;
3049		} else if (rc != -EBADF)
3050			goto cifs_setattr_exit;
3051		else
3052			rc = 0;
3053	}
3054
3055	if (attrs->ia_valid & ATTR_SIZE) {
3056		rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
3057		if (rc != 0)
3058			goto cifs_setattr_exit;
3059	}
3060
3061	if (attrs->ia_valid & ATTR_UID)
3062		uid = attrs->ia_uid;
3063
3064	if (attrs->ia_valid & ATTR_GID)
3065		gid = attrs->ia_gid;
3066
3067	if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3068	    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3069		if (uid_valid(uid) || gid_valid(gid)) {
3070			mode = NO_CHANGE_64;
3071			rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3072							uid, gid);
3073			if (rc) {
3074				cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3075					 __func__, rc);
3076				goto cifs_setattr_exit;
3077			}
3078		}
3079	} else
3080	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3081		attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3082
3083	/* skip mode change if it's just for clearing setuid/setgid */
3084	if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3085		attrs->ia_valid &= ~ATTR_MODE;
3086
3087	if (attrs->ia_valid & ATTR_MODE) {
3088		mode = attrs->ia_mode;
3089		rc = 0;
3090		if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3091		    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3092			rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3093						INVALID_UID, INVALID_GID);
3094			if (rc) {
3095				cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3096					 __func__, rc);
3097				goto cifs_setattr_exit;
3098			}
3099
3100			/*
3101			 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3102			 * Pick up the actual mode bits that were set.
3103			 */
3104			if (mode != attrs->ia_mode)
3105				attrs->ia_mode = mode;
3106		} else
3107		if (((mode & S_IWUGO) == 0) &&
3108		    (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3109
3110			dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3111
3112			/* fix up mode if we're not using dynperm */
3113			if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3114				attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3115		} else if ((mode & S_IWUGO) &&
3116			   (cifsInode->cifsAttrs & ATTR_READONLY)) {
3117
3118			dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3119			/* Attributes of 0 are ignored */
3120			if (dosattr == 0)
3121				dosattr |= ATTR_NORMAL;
3122
3123			/* reset local inode permissions to normal */
3124			if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3125				attrs->ia_mode &= ~(S_IALLUGO);
3126				if (S_ISDIR(inode->i_mode))
3127					attrs->ia_mode |=
3128						cifs_sb->ctx->dir_mode;
3129				else
3130					attrs->ia_mode |=
3131						cifs_sb->ctx->file_mode;
3132			}
3133		} else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3134			/* ignore mode change - ATTR_READONLY hasn't changed */
3135			attrs->ia_valid &= ~ATTR_MODE;
3136		}
3137	}
3138
3139	if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3140	    ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3141		rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3142		/* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3143
3144		/* Even if error on time set, no sense failing the call if
3145		the server would set the time to a reasonable value anyway,
3146		and this check ensures that we are not being called from
3147		sys_utimes in which case we ought to fail the call back to
3148		the user when the server rejects the call */
3149		if ((rc) && (attrs->ia_valid &
3150				(ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3151			rc = 0;
3152	}
3153
3154	/* do not need local check to inode_check_ok since the server does
3155	   that */
3156	if (rc)
3157		goto cifs_setattr_exit;
3158
3159	if ((attrs->ia_valid & ATTR_SIZE) &&
3160	    attrs->ia_size != i_size_read(inode)) {
3161		truncate_setsize(inode, attrs->ia_size);
3162		netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3163		fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3164	}
3165
3166	setattr_copy(&nop_mnt_idmap, inode, attrs);
3167	mark_inode_dirty(inode);
3168
3169cifs_setattr_exit:
3170	free_xid(xid);
3171	free_dentry_path(page);
3172	return rc;
3173}
3174
3175int
3176cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3177	     struct iattr *attrs)
3178{
3179	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3180	int rc, retries = 0;
3181#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3182	struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3183#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3184
3185	if (unlikely(cifs_forced_shutdown(cifs_sb)))
3186		return -EIO;
3187
3188	do {
3189#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3190		if (pTcon->unix_ext)
3191			rc = cifs_setattr_unix(direntry, attrs);
3192		else
3193#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3194			rc = cifs_setattr_nounix(direntry, attrs);
3195		retries++;
3196	} while (is_retryable_error(rc) && retries < 2);
3197
3198	/* BB: add cifs_setattr_legacy for really old servers */
3199	return rc;
3200}