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