File Index
Files and directories
Sign in
FILE

inode.c

File preview with syntax highlighting.

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