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