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