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