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