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