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