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