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