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