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