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