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