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