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