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