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