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