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