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