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