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