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