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