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