1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
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>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
30 static void cifs_set_ops(struct inode *inode)
32 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
34 switch (inode->i_mode & S_IFMT) {
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;
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;
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;
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;
58 inode->i_data.a_ops = &cifs_addr_ops;
61 #ifdef CONFIG_CIFS_DFS_UPCALL
62 if (IS_AUTOMOUNT(inode)) {
63 inode->i_op = &cifs_dfs_referral_inode_operations;
65 #else /* NO DFS support, treat as a directory */
68 inode->i_op = &cifs_dir_inode_ops;
69 inode->i_fop = &cifs_dir_ops;
73 inode->i_op = &cifs_symlink_inode_ops;
76 init_special_inode(inode, inode->i_mode, inode->i_rdev);
81 /* check inode attributes against fattr. If they don't match, tag the
82 * inode for cache invalidation
85 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
87 struct cifs_fscache_inode_coherency_data cd;
88 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
90 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
91 __func__, cifs_i->uniqueid);
93 if (inode->i_state & I_NEW) {
94 cifs_dbg(FYI, "%s: inode %llu is new\n",
95 __func__, cifs_i->uniqueid);
99 /* don't bother with revalidation if we have an oplock */
100 if (CIFS_CACHE_READ(cifs_i)) {
101 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
102 __func__, cifs_i->uniqueid);
106 /* revalidate if mtime or size have changed */
107 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
108 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
109 cifs_i->server_eof == fattr->cf_eof) {
110 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
111 __func__, cifs_i->uniqueid);
115 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
116 __func__, cifs_i->uniqueid);
117 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
118 /* Invalidate fscache cookie */
119 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
120 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
124 * copy nlink to the inode, unless it wasn't provided. Provide
125 * sane values if we don't have an existing one and none was provided
128 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
131 * if we're in a situation where we can't trust what we
132 * got from the server (readdir, some non-unix cases)
133 * fake reasonable values
135 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
136 /* only provide fake values on a new inode */
137 if (inode->i_state & I_NEW) {
138 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
146 /* we trust the server, so update it */
147 set_nlink(inode, fattr->cf_nlink);
150 /* populate an inode with info from a cifs_fattr struct */
152 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
154 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
157 if (!(inode->i_state & I_NEW) &&
158 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
159 CIFS_I(inode)->time = 0; /* force reval */
163 cifs_revalidate_cache(inode, fattr);
165 spin_lock(&inode->i_lock);
166 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
167 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
168 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
169 /* we do not want atime to be less than mtime, it broke some apps */
170 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
171 inode->i_atime = fattr->cf_mtime;
173 inode->i_atime = fattr->cf_atime;
174 inode->i_mtime = fattr->cf_mtime;
175 inode->i_ctime = fattr->cf_ctime;
176 inode->i_rdev = fattr->cf_rdev;
177 cifs_nlink_fattr_to_inode(inode, fattr);
178 inode->i_uid = fattr->cf_uid;
179 inode->i_gid = fattr->cf_gid;
181 /* if dynperm is set, don't clobber existing mode */
182 if (inode->i_state & I_NEW ||
183 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
184 inode->i_mode = fattr->cf_mode;
186 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
188 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
191 cifs_i->time = jiffies;
193 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
194 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
196 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
198 cifs_i->server_eof = fattr->cf_eof;
200 * Can't safely change the file size here if the client is writing to
201 * it due to potential races.
203 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
204 i_size_write(inode, fattr->cf_eof);
207 * i_blocks is not related to (i_size / i_blksize),
208 * but instead 512 byte (2**9) size is required for
209 * calculating num blocks.
211 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
214 if (S_ISLNK(fattr->cf_mode)) {
215 kfree(cifs_i->symlink_target);
216 cifs_i->symlink_target = fattr->cf_symlink_target;
217 fattr->cf_symlink_target = NULL;
219 spin_unlock(&inode->i_lock);
221 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
222 inode->i_flags |= S_AUTOMOUNT;
223 if (inode->i_state & I_NEW)
229 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
231 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
233 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
236 fattr->cf_uniqueid = iunique(sb, ROOT_I);
239 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
241 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
242 struct cifs_sb_info *cifs_sb)
244 memset(fattr, 0, sizeof(*fattr));
245 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
246 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
247 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
249 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
250 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
251 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
252 /* old POSIX extensions don't get create time */
254 fattr->cf_mode = le64_to_cpu(info->Permissions);
257 * Since we set the inode type below we need to mask off
258 * to avoid strange results if bits set above.
260 fattr->cf_mode &= ~S_IFMT;
261 switch (le32_to_cpu(info->Type)) {
263 fattr->cf_mode |= S_IFREG;
264 fattr->cf_dtype = DT_REG;
267 fattr->cf_mode |= S_IFLNK;
268 fattr->cf_dtype = DT_LNK;
271 fattr->cf_mode |= S_IFDIR;
272 fattr->cf_dtype = DT_DIR;
275 fattr->cf_mode |= S_IFCHR;
276 fattr->cf_dtype = DT_CHR;
277 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
278 le64_to_cpu(info->DevMinor) & MINORMASK);
281 fattr->cf_mode |= S_IFBLK;
282 fattr->cf_dtype = DT_BLK;
283 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
284 le64_to_cpu(info->DevMinor) & MINORMASK);
287 fattr->cf_mode |= S_IFIFO;
288 fattr->cf_dtype = DT_FIFO;
291 fattr->cf_mode |= S_IFSOCK;
292 fattr->cf_dtype = DT_SOCK;
295 /* safest to call it a file if we do not know */
296 fattr->cf_mode |= S_IFREG;
297 fattr->cf_dtype = DT_REG;
298 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
302 fattr->cf_uid = cifs_sb->ctx->linux_uid;
303 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
304 u64 id = le64_to_cpu(info->Uid);
305 if (id < ((uid_t)-1)) {
306 kuid_t uid = make_kuid(&init_user_ns, id);
312 fattr->cf_gid = cifs_sb->ctx->linux_gid;
313 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
314 u64 id = le64_to_cpu(info->Gid);
315 if (id < ((gid_t)-1)) {
316 kgid_t gid = make_kgid(&init_user_ns, id);
322 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
326 * Fill a cifs_fattr struct with fake inode info.
328 * Needed to setup cifs_fattr data for the directory which is the
329 * junction to the new submount (ie to setup the fake directory
330 * which represents a DFS referral).
333 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
337 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
339 memset(fattr, 0, sizeof(*fattr));
340 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
341 fattr->cf_uid = cifs_sb->ctx->linux_uid;
342 fattr->cf_gid = cifs_sb->ctx->linux_gid;
343 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
344 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
346 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
349 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
351 cifs_get_file_info_unix(struct file *filp)
355 FILE_UNIX_BASIC_INFO find_data;
356 struct cifs_fattr fattr = {};
357 struct inode *inode = file_inode(filp);
358 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
359 struct cifsFileInfo *cfile = filp->private_data;
360 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
364 if (cfile->symlink_target) {
365 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
366 if (!fattr.cf_symlink_target) {
368 goto cifs_gfiunix_out;
372 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
374 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
375 } else if (rc == -EREMOTE) {
376 cifs_create_dfs_fattr(&fattr, inode->i_sb);
379 goto cifs_gfiunix_out;
381 rc = cifs_fattr_to_inode(inode, &fattr);
388 int cifs_get_inode_info_unix(struct inode **pinode,
389 const unsigned char *full_path,
390 struct super_block *sb, unsigned int xid)
393 FILE_UNIX_BASIC_INFO find_data;
394 struct cifs_fattr fattr;
395 struct cifs_tcon *tcon;
396 struct TCP_Server_Info *server;
397 struct tcon_link *tlink;
398 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
400 cifs_dbg(FYI, "Getting info on %s\n", full_path);
402 tlink = cifs_sb_tlink(cifs_sb);
404 return PTR_ERR(tlink);
405 tcon = tlink_tcon(tlink);
406 server = tcon->ses->server;
408 /* could have done a find first instead but this returns more info */
409 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
410 cifs_sb->local_nls, cifs_remap(cifs_sb));
411 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
412 cifs_put_tlink(tlink);
415 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
416 } else if (rc == -EREMOTE) {
417 cifs_create_dfs_fattr(&fattr, sb);
423 /* check for Minshall+French symlinks */
424 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
425 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
428 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
431 if (S_ISLNK(fattr.cf_mode) && !fattr.cf_symlink_target) {
432 if (!server->ops->query_symlink)
434 rc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
435 &fattr.cf_symlink_target, false);
437 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
442 if (*pinode == NULL) {
444 cifs_fill_uniqueid(sb, &fattr);
445 *pinode = cifs_iget(sb, &fattr);
449 /* we already have inode, update it */
451 /* if uniqueid is different, return error */
452 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
453 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
454 CIFS_I(*pinode)->time = 0; /* force reval */
459 /* if filetype is different, return error */
460 rc = cifs_fattr_to_inode(*pinode, &fattr);
464 kfree(fattr.cf_symlink_target);
468 int cifs_get_inode_info_unix(struct inode **pinode,
469 const unsigned char *full_path,
470 struct super_block *sb, unsigned int xid)
474 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
477 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
478 struct cifs_sb_info *cifs_sb, unsigned int xid)
482 struct tcon_link *tlink;
483 struct cifs_tcon *tcon;
485 struct cifs_open_parms oparms;
486 struct cifs_io_parms io_parms = {0};
488 unsigned int bytes_read;
490 int buf_type = CIFS_NO_BUFFER;
494 fattr->cf_mode &= ~S_IFMT;
496 if (fattr->cf_eof == 0) {
497 fattr->cf_mode |= S_IFIFO;
498 fattr->cf_dtype = DT_FIFO;
500 } else if (fattr->cf_eof < 8) {
501 fattr->cf_mode |= S_IFREG;
502 fattr->cf_dtype = DT_REG;
503 return -EINVAL; /* EOPNOTSUPP? */
506 tlink = cifs_sb_tlink(cifs_sb);
508 return PTR_ERR(tlink);
509 tcon = tlink_tcon(tlink);
512 oparms.cifs_sb = cifs_sb;
513 oparms.desired_access = GENERIC_READ;
514 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
515 oparms.disposition = FILE_OPEN;
518 oparms.reconnect = false;
520 if (tcon->ses->server->oplocks)
524 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
526 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
527 cifs_put_tlink(tlink);
532 io_parms.netfid = fid.netfid;
533 io_parms.pid = current->tgid;
534 io_parms.tcon = tcon;
536 io_parms.length = 24;
538 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
539 &bytes_read, &pbuf, &buf_type);
540 if ((rc == 0) && (bytes_read >= 8)) {
541 if (memcmp("IntxBLK", pbuf, 8) == 0) {
542 cifs_dbg(FYI, "Block device\n");
543 fattr->cf_mode |= S_IFBLK;
544 fattr->cf_dtype = DT_BLK;
545 if (bytes_read == 24) {
546 /* we have enough to decode dev num */
547 __u64 mjr; /* major */
548 __u64 mnr; /* minor */
549 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
550 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
551 fattr->cf_rdev = MKDEV(mjr, mnr);
553 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
554 cifs_dbg(FYI, "Char device\n");
555 fattr->cf_mode |= S_IFCHR;
556 fattr->cf_dtype = DT_CHR;
557 if (bytes_read == 24) {
558 /* we have enough to decode dev num */
559 __u64 mjr; /* major */
560 __u64 mnr; /* minor */
561 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
562 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
563 fattr->cf_rdev = MKDEV(mjr, mnr);
565 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
566 cifs_dbg(FYI, "Symlink\n");
567 fattr->cf_mode |= S_IFLNK;
568 fattr->cf_dtype = DT_LNK;
570 fattr->cf_mode |= S_IFREG; /* file? */
571 fattr->cf_dtype = DT_REG;
575 fattr->cf_mode |= S_IFREG; /* then it is a file */
576 fattr->cf_dtype = DT_REG;
577 rc = -EOPNOTSUPP; /* or some unknown SFU type */
580 tcon->ses->server->ops->close(xid, tcon, &fid);
581 cifs_put_tlink(tlink);
585 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
588 * Fetch mode bits as provided by SFU.
590 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
592 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
593 struct cifs_sb_info *cifs_sb, unsigned int xid)
595 #ifdef CONFIG_CIFS_XATTR
599 struct tcon_link *tlink;
600 struct cifs_tcon *tcon;
602 tlink = cifs_sb_tlink(cifs_sb);
604 return PTR_ERR(tlink);
605 tcon = tlink_tcon(tlink);
607 if (tcon->ses->server->ops->query_all_EAs == NULL) {
608 cifs_put_tlink(tlink);
612 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
613 "SETFILEBITS", ea_value, 4 /* size of buf */,
615 cifs_put_tlink(tlink);
619 mode = le32_to_cpu(*((__le32 *)ea_value));
620 fattr->cf_mode &= ~SFBITS_MASK;
621 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
622 mode, fattr->cf_mode);
623 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
624 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
633 /* Fill a cifs_fattr struct with info from POSIX info struct */
634 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct cifs_open_info_data *data,
635 struct super_block *sb, bool adjust_tz, bool symlink)
637 struct smb311_posix_qinfo *info = &data->posix_fi;
638 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
639 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
641 memset(fattr, 0, sizeof(*fattr));
643 /* no fattr->flags to set */
644 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
645 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
647 if (info->LastAccessTime)
648 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
650 ktime_get_coarse_real_ts64(&fattr->cf_atime);
652 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
653 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
656 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
657 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
660 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
661 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
662 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
664 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
665 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
666 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
667 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
670 fattr->cf_mode |= S_IFLNK;
671 fattr->cf_dtype = DT_LNK;
672 fattr->cf_symlink_target = data->symlink_target;
673 data->symlink_target = NULL;
674 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
675 fattr->cf_mode |= S_IFDIR;
676 fattr->cf_dtype = DT_DIR;
678 fattr->cf_mode |= S_IFREG;
679 fattr->cf_dtype = DT_REG;
681 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
683 fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
684 fattr->cf_gid = cifs_sb->ctx->linux_gid;
686 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
687 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
690 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr, struct cifs_open_info_data *data,
691 struct super_block *sb, bool adjust_tz, bool symlink,
694 struct smb2_file_all_info *info = &data->fi;
695 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
696 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
698 memset(fattr, 0, sizeof(*fattr));
699 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
700 if (info->DeletePending)
701 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
703 if (info->LastAccessTime)
704 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
706 ktime_get_coarse_real_ts64(&fattr->cf_atime);
708 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
709 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
712 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
713 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
716 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
717 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
718 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
720 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
721 if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
722 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
723 fattr->cf_dtype = DT_LNK;
724 } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
725 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
726 fattr->cf_dtype = DT_FIFO;
727 } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
728 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
729 fattr->cf_dtype = DT_SOCK;
730 } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
731 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
732 fattr->cf_dtype = DT_CHR;
733 } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
734 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
735 fattr->cf_dtype = DT_BLK;
736 } else if (symlink || reparse_tag == IO_REPARSE_TAG_SYMLINK ||
737 reparse_tag == IO_REPARSE_TAG_NFS) {
738 fattr->cf_mode = S_IFLNK;
739 fattr->cf_dtype = DT_LNK;
740 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
741 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
742 fattr->cf_dtype = DT_DIR;
744 * Server can return wrong NumberOfLinks value for directories
745 * when Unix extensions are disabled - fake it.
748 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
750 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
751 fattr->cf_dtype = DT_REG;
753 /* clear write bits if ATTR_READONLY is set */
754 if (fattr->cf_cifsattrs & ATTR_READONLY)
755 fattr->cf_mode &= ~(S_IWUGO);
758 * Don't accept zero nlink from non-unix servers unless
759 * delete is pending. Instead mark it as unknown.
761 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
762 !info->DeletePending) {
763 cifs_dbg(VFS, "bogus file nlink value %u\n",
765 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
769 if (S_ISLNK(fattr->cf_mode)) {
770 fattr->cf_symlink_target = data->symlink_target;
771 data->symlink_target = NULL;
774 fattr->cf_uid = cifs_sb->ctx->linux_uid;
775 fattr->cf_gid = cifs_sb->ctx->linux_gid;
779 cifs_get_file_info(struct file *filp)
783 struct cifs_open_info_data data = {};
784 struct cifs_fattr fattr;
785 struct inode *inode = file_inode(filp);
786 struct cifsFileInfo *cfile = filp->private_data;
787 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
788 struct TCP_Server_Info *server = tcon->ses->server;
789 bool symlink = false;
792 if (!server->ops->query_file_info)
796 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
799 /* TODO: add support to query reparse tag */
800 if (data.symlink_target) {
802 tag = IO_REPARSE_TAG_SYMLINK;
804 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb, false, symlink, tag);
807 cifs_create_dfs_fattr(&fattr, inode->i_sb);
813 * FIXME: legacy server -- fall back to path-based call?
814 * for now, just skip revalidating and mark inode for
818 CIFS_I(inode)->time = 0;
825 * don't bother with SFU junk here -- just mark inode as needing
828 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
829 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
830 /* if filetype is different, return error */
831 rc = cifs_fattr_to_inode(inode, &fattr);
833 cifs_free_open_info(&data);
838 /* Simple function to return a 64 bit hash of string. Rarely called */
839 static __u64 simple_hashstr(const char *str)
841 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
845 hash = (hash + (__u64) *str++) * hash_mult;
850 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
852 * cifs_backup_query_path_info - SMB1 fallback code to get ino
854 * Fallback code to get file metadata when we don't have access to
855 * full_path (EACCES) and have backup creds.
857 * @xid: transaction id used to identify original request in logs
858 * @tcon: information about the server share we have mounted
859 * @sb: the superblock stores info such as disk space available
860 * @full_path: name of the file we are getting the metadata for
861 * @resp_buf: will be set to cifs resp buf and needs to be freed with
862 * cifs_buf_release() when done with @data
863 * @data: will be set to search info result buffer
866 cifs_backup_query_path_info(int xid,
867 struct cifs_tcon *tcon,
868 struct super_block *sb,
869 const char *full_path,
871 FILE_ALL_INFO **data)
873 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
874 struct cifs_search_info info = {0};
879 info.endOfSearch = false;
881 info.info_level = SMB_FIND_FILE_UNIX;
882 else if ((tcon->ses->capabilities &
883 tcon->ses->server->vals->cap_nt_find) == 0)
884 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
885 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
886 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
887 else /* no srvino useful for fallback to some netapp */
888 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
890 flags = CIFS_SEARCH_CLOSE_ALWAYS |
891 CIFS_SEARCH_CLOSE_AT_END |
892 CIFS_SEARCH_BACKUP_SEARCH;
894 rc = CIFSFindFirst(xid, tcon, full_path,
895 cifs_sb, NULL, flags, &info, false);
899 *resp_buf = (void *)info.ntwrk_buf_start;
900 *data = (FILE_ALL_INFO *)info.srch_entries_start;
903 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
905 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
906 struct inode **inode, const char *full_path,
907 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
909 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
910 struct TCP_Server_Info *server = tcon->ses->server;
913 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
915 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
917 fattr->cf_uniqueid = iunique(sb, ROOT_I);
922 * If we have an inode pass a NULL tcon to ensure we don't
923 * make a round trip to the server. This only works for SMB2+.
925 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
926 &fattr->cf_uniqueid, data);
929 * If that fails reuse existing ino or generate one
930 * and disable server ones
933 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
935 fattr->cf_uniqueid = iunique(sb, ROOT_I);
936 cifs_autodisable_serverino(cifs_sb);
941 /* If no errors, check for zero root inode (invalid) */
942 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
943 cifs_dbg(FYI, "Invalid (0) inodenum\n");
946 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
948 /* make an ino by hashing the UNC */
949 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
950 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
955 static inline bool is_inode_cache_good(struct inode *ino)
957 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
960 int cifs_get_inode_info(struct inode **inode, const char *full_path,
961 struct cifs_open_info_data *data, struct super_block *sb, int xid,
962 const struct cifs_fid *fid)
964 struct cifs_tcon *tcon;
965 struct TCP_Server_Info *server;
966 struct tcon_link *tlink;
967 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
968 bool adjust_tz = false;
969 struct cifs_fattr fattr = {0};
970 bool is_reparse_point = false;
971 struct cifs_open_info_data tmp_data = {};
972 void *smb1_backup_rsp_buf = NULL;
975 __u32 reparse_tag = 0;
977 tlink = cifs_sb_tlink(cifs_sb);
979 return PTR_ERR(tlink);
980 tcon = tlink_tcon(tlink);
981 server = tcon->ses->server;
984 * 1. Fetch file metadata if not provided (data)
988 if (is_inode_cache_good(*inode)) {
989 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
992 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path, &tmp_data,
993 &adjust_tz, &is_reparse_point);
994 #ifdef CONFIG_CIFS_DFS_UPCALL
995 if (rc == -ENOENT && is_tcon_dfs(tcon))
996 rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon,
1004 * 2. Convert it to internal cifs metadata (fattr)
1010 * If the file is a reparse point, it is more complicated
1011 * since we have to check if its reparse tag matches a known
1012 * special file type e.g. symlink or fifo or char etc.
1014 if (is_reparse_point && data->symlink_target) {
1015 reparse_tag = IO_REPARSE_TAG_SYMLINK;
1016 } else if ((le32_to_cpu(data->fi.Attributes) & ATTR_REPARSE) &&
1017 server->ops->query_reparse_tag) {
1018 tmprc = server->ops->query_reparse_tag(xid, tcon, cifs_sb, full_path,
1021 cifs_dbg(FYI, "%s: query_reparse_tag: rc = %d\n", __func__, tmprc);
1022 if (server->ops->query_symlink) {
1023 tmprc = server->ops->query_symlink(xid, tcon, cifs_sb, full_path,
1024 &data->symlink_target,
1027 cifs_dbg(FYI, "%s: query_symlink: rc = %d\n", __func__,
1031 cifs_open_info_to_fattr(&fattr, data, sb, adjust_tz, is_reparse_point, reparse_tag);
1034 /* DFS link, no metadata available on this server */
1035 cifs_create_dfs_fattr(&fattr, sb);
1039 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1041 * perm errors, try again with backup flags if possible
1043 * For SMB2 and later the backup intent flag
1044 * is already sent if needed on open and there
1045 * is no path based FindFirst operation to use
1048 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1049 /* for easier reading */
1051 FILE_DIRECTORY_INFO *fdi;
1052 SEARCH_ID_FULL_DIR_INFO *si;
1054 rc = cifs_backup_query_path_info(xid, tcon, sb,
1056 &smb1_backup_rsp_buf,
1061 move_cifs_info_to_smb2(&data->fi, fi);
1062 fdi = (FILE_DIRECTORY_INFO *)fi;
1063 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1065 cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1066 fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1067 /* uniqueid set, skip get inum step */
1068 goto handle_mnt_opt;
1070 /* nothing we can do, bail out */
1075 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1078 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1083 * 3. Get or update inode number (fattr.cf_uniqueid)
1086 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1089 * 4. Tweak fattr based on mount options
1091 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1093 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1094 /* query for SFU type info if supported and needed */
1095 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1096 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1097 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1099 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1102 /* fill in 0777 bits from ACL */
1103 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1104 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1109 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1113 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1114 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1119 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1125 /* fill in remaining high mode bits e.g. SUID, VTX */
1126 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1127 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1129 /* check for Minshall+French symlinks */
1130 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1131 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1134 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1138 * 5. Update inode with final fattr data
1142 *inode = cifs_iget(sb, &fattr);
1146 /* we already have inode, update it */
1148 /* if uniqueid is different, return error */
1149 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1150 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1151 CIFS_I(*inode)->time = 0; /* force reval */
1155 /* if filetype is different, return error */
1156 rc = cifs_fattr_to_inode(*inode, &fattr);
1159 cifs_buf_release(smb1_backup_rsp_buf);
1160 cifs_put_tlink(tlink);
1161 cifs_free_open_info(&tmp_data);
1162 kfree(fattr.cf_symlink_target);
1167 smb311_posix_get_inode_info(struct inode **inode,
1168 const char *full_path,
1169 struct super_block *sb, unsigned int xid)
1171 struct cifs_tcon *tcon;
1172 struct tcon_link *tlink;
1173 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1174 bool adjust_tz = false;
1175 struct cifs_fattr fattr = {0};
1176 bool symlink = false;
1177 struct cifs_open_info_data data = {};
1181 tlink = cifs_sb_tlink(cifs_sb);
1183 return PTR_ERR(tlink);
1184 tcon = tlink_tcon(tlink);
1187 * 1. Fetch file metadata
1190 if (is_inode_cache_good(*inode)) {
1191 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1195 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb, full_path, &data, &adjust_tz,
1199 * 2. Convert it to internal cifs metadata (fattr)
1204 smb311_posix_info_to_fattr(&fattr, &data, sb, adjust_tz, symlink);
1207 /* DFS link, no metadata available on this server */
1208 cifs_create_dfs_fattr(&fattr, sb);
1213 * For SMB2 and later the backup intent flag
1214 * is already sent if needed on open and there
1215 * is no path based FindFirst operation to use
1216 * to retry with so nothing we can do, bail out
1220 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1226 * 3. Tweak fattr based on mount options
1229 /* check for Minshall+French symlinks */
1230 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1231 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1234 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1238 * 4. Update inode with final fattr data
1242 *inode = cifs_iget(sb, &fattr);
1246 /* we already have inode, update it */
1248 /* if uniqueid is different, return error */
1249 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1250 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1251 CIFS_I(*inode)->time = 0; /* force reval */
1256 /* if filetype is different, return error */
1257 rc = cifs_fattr_to_inode(*inode, &fattr);
1260 cifs_put_tlink(tlink);
1261 cifs_free_open_info(&data);
1262 kfree(fattr.cf_symlink_target);
1267 static const struct inode_operations cifs_ipc_inode_ops = {
1268 .lookup = cifs_lookup,
1272 cifs_find_inode(struct inode *inode, void *opaque)
1274 struct cifs_fattr *fattr = opaque;
1276 /* don't match inode with different uniqueid */
1277 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1280 /* use createtime like an i_generation field */
1281 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1284 /* don't match inode of different type */
1285 if (inode_wrong_type(inode, fattr->cf_mode))
1288 /* if it's not a directory or has no dentries, then flag it */
1289 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1290 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1296 cifs_init_inode(struct inode *inode, void *opaque)
1298 struct cifs_fattr *fattr = opaque;
1300 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1301 CIFS_I(inode)->createtime = fattr->cf_createtime;
1306 * walk dentry list for an inode and report whether it has aliases that
1307 * are hashed. We use this to determine if a directory inode can actually
1311 inode_has_hashed_dentries(struct inode *inode)
1313 struct dentry *dentry;
1315 spin_lock(&inode->i_lock);
1316 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1317 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1318 spin_unlock(&inode->i_lock);
1322 spin_unlock(&inode->i_lock);
1326 /* Given fattrs, get a corresponding inode */
1328 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1331 struct inode *inode;
1334 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1336 /* hash down to 32-bits on 32-bit arch */
1337 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1339 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1341 /* was there a potentially problematic inode collision? */
1342 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1343 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1345 if (inode_has_hashed_dentries(inode)) {
1346 cifs_autodisable_serverino(CIFS_SB(sb));
1348 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1349 goto retry_iget5_locked;
1353 /* can't fail - see cifs_find_inode() */
1354 cifs_fattr_to_inode(inode, fattr);
1355 if (sb->s_flags & SB_NOATIME)
1356 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1357 if (inode->i_state & I_NEW) {
1358 inode->i_ino = hash;
1359 cifs_fscache_get_inode_cookie(inode);
1360 unlock_new_inode(inode);
1367 /* gets root inode */
1368 struct inode *cifs_root_iget(struct super_block *sb)
1371 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1372 struct inode *inode = NULL;
1374 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1378 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1379 && cifs_sb->prepath) {
1380 len = strlen(cifs_sb->prepath);
1381 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1383 return ERR_PTR(-ENOMEM);
1385 memcpy(path+1, cifs_sb->prepath, len);
1387 path = kstrdup("", GFP_KERNEL);
1389 return ERR_PTR(-ENOMEM);
1393 if (tcon->unix_ext) {
1394 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1395 /* some servers mistakenly claim POSIX support */
1396 if (rc != -EOPNOTSUPP)
1398 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1399 tcon->unix_ext = false;
1402 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1403 if (tcon->posix_extensions)
1404 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1406 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1410 inode = ERR_PTR(rc);
1414 if (rc && tcon->pipe) {
1415 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1416 spin_lock(&inode->i_lock);
1417 inode->i_mode |= S_IFDIR;
1418 set_nlink(inode, 2);
1419 inode->i_op = &cifs_ipc_inode_ops;
1420 inode->i_fop = &simple_dir_operations;
1421 inode->i_uid = cifs_sb->ctx->linux_uid;
1422 inode->i_gid = cifs_sb->ctx->linux_gid;
1423 spin_unlock(&inode->i_lock);
1426 inode = ERR_PTR(rc);
1436 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1437 const char *full_path, __u32 dosattr)
1439 bool set_time = false;
1440 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1441 struct TCP_Server_Info *server;
1442 FILE_BASIC_INFO info_buf;
1447 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1448 if (!server->ops->set_file_info)
1453 if (attrs->ia_valid & ATTR_ATIME) {
1455 info_buf.LastAccessTime =
1456 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1458 info_buf.LastAccessTime = 0;
1460 if (attrs->ia_valid & ATTR_MTIME) {
1462 info_buf.LastWriteTime =
1463 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1465 info_buf.LastWriteTime = 0;
1468 * Samba throws this field away, but windows may actually use it.
1469 * Do not set ctime unless other time stamps are changed explicitly
1470 * (i.e. by utimes()) since we would then have a mix of client and
1473 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1474 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1475 info_buf.ChangeTime =
1476 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1478 info_buf.ChangeTime = 0;
1480 info_buf.CreationTime = 0; /* don't change */
1481 info_buf.Attributes = cpu_to_le32(dosattr);
1483 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1486 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1488 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1489 * and rename it to a random name that hopefully won't conflict with
1493 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1494 const unsigned int xid)
1498 struct cifs_fid fid;
1499 struct cifs_open_parms oparms;
1500 struct inode *inode = d_inode(dentry);
1501 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1502 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1503 struct tcon_link *tlink;
1504 struct cifs_tcon *tcon;
1505 __u32 dosattr, origattr;
1506 FILE_BASIC_INFO *info_buf = NULL;
1508 tlink = cifs_sb_tlink(cifs_sb);
1510 return PTR_ERR(tlink);
1511 tcon = tlink_tcon(tlink);
1514 * We cannot rename the file if the server doesn't support
1515 * CAP_INFOLEVEL_PASSTHRU
1517 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1523 oparms.cifs_sb = cifs_sb;
1524 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1525 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1526 oparms.disposition = FILE_OPEN;
1527 oparms.path = full_path;
1529 oparms.reconnect = false;
1531 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1535 origattr = cifsInode->cifsAttrs;
1537 origattr |= ATTR_NORMAL;
1539 dosattr = origattr & ~ATTR_READONLY;
1541 dosattr |= ATTR_NORMAL;
1542 dosattr |= ATTR_HIDDEN;
1544 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1545 if (dosattr != origattr) {
1546 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1547 if (info_buf == NULL) {
1551 info_buf->Attributes = cpu_to_le32(dosattr);
1552 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1554 /* although we would like to mark the file hidden
1555 if that fails we will still try to rename it */
1557 cifsInode->cifsAttrs = dosattr;
1559 dosattr = origattr; /* since not able to change them */
1562 /* rename the file */
1563 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1565 cifs_remap(cifs_sb));
1571 /* try to set DELETE_ON_CLOSE */
1572 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1573 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1576 * some samba versions return -ENOENT when we try to set the
1577 * file disposition here. Likely a samba bug, but work around
1578 * it for now. This means that some cifsXXX files may hang
1579 * around after they shouldn't.
1581 * BB: remove this hack after more servers have the fix
1589 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1593 CIFSSMBClose(xid, tcon, fid.netfid);
1596 cifs_put_tlink(tlink);
1600 * reset everything back to the original state. Don't bother
1601 * dealing with errors here since we can't do anything about
1605 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1606 cifs_sb->local_nls, cifs_remap(cifs_sb));
1608 if (dosattr != origattr) {
1609 info_buf->Attributes = cpu_to_le32(origattr);
1610 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1612 cifsInode->cifsAttrs = origattr;
1617 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1619 /* copied from fs/nfs/dir.c with small changes */
1621 cifs_drop_nlink(struct inode *inode)
1623 spin_lock(&inode->i_lock);
1624 if (inode->i_nlink > 0)
1626 spin_unlock(&inode->i_lock);
1630 * If d_inode(dentry) is null (usually meaning the cached dentry
1631 * is a negative dentry) then we would attempt a standard SMB delete, but
1632 * if that fails we can not attempt the fall back mechanisms on EACCES
1633 * but will return the EACCES to the caller. Note that the VFS does not call
1634 * unlink on negative dentries currently.
1636 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1640 const char *full_path;
1642 struct inode *inode = d_inode(dentry);
1643 struct cifsInodeInfo *cifs_inode;
1644 struct super_block *sb = dir->i_sb;
1645 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1646 struct tcon_link *tlink;
1647 struct cifs_tcon *tcon;
1648 struct TCP_Server_Info *server;
1649 struct iattr *attrs = NULL;
1650 __u32 dosattr = 0, origattr = 0;
1652 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1654 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1657 tlink = cifs_sb_tlink(cifs_sb);
1659 return PTR_ERR(tlink);
1660 tcon = tlink_tcon(tlink);
1661 server = tcon->ses->server;
1664 page = alloc_dentry_path();
1666 if (tcon->nodelete) {
1671 /* Unlink can be called from rename so we can not take the
1672 * sb->s_vfs_rename_mutex here */
1673 full_path = build_path_from_dentry(dentry, page);
1674 if (IS_ERR(full_path)) {
1675 rc = PTR_ERR(full_path);
1679 cifs_close_deferred_file_under_dentry(tcon, full_path);
1680 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1681 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1682 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1683 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1684 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1685 cifs_remap(cifs_sb));
1686 cifs_dbg(FYI, "posix del rc %d\n", rc);
1687 if ((rc == 0) || (rc == -ENOENT))
1688 goto psx_del_no_retry;
1690 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1693 if (!server->ops->unlink) {
1695 goto psx_del_no_retry;
1698 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1703 cifs_drop_nlink(inode);
1704 } else if (rc == -ENOENT) {
1706 } else if (rc == -EBUSY) {
1707 if (server->ops->rename_pending_delete) {
1708 rc = server->ops->rename_pending_delete(full_path,
1711 cifs_drop_nlink(inode);
1713 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1714 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1715 if (attrs == NULL) {
1720 /* try to reset dos attributes */
1721 cifs_inode = CIFS_I(inode);
1722 origattr = cifs_inode->cifsAttrs;
1724 origattr |= ATTR_NORMAL;
1725 dosattr = origattr & ~ATTR_READONLY;
1727 dosattr |= ATTR_NORMAL;
1728 dosattr |= ATTR_HIDDEN;
1730 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1734 goto retry_std_delete;
1737 /* undo the setattr if we errored out and it's needed */
1738 if (rc != 0 && dosattr != 0)
1739 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1743 cifs_inode = CIFS_I(inode);
1744 cifs_inode->time = 0; /* will force revalidate to get info
1746 inode->i_ctime = current_time(inode);
1748 dir->i_ctime = dir->i_mtime = current_time(dir);
1749 cifs_inode = CIFS_I(dir);
1750 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1752 free_dentry_path(page);
1755 cifs_put_tlink(tlink);
1760 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1761 const char *full_path, struct cifs_sb_info *cifs_sb,
1762 struct cifs_tcon *tcon, const unsigned int xid)
1765 struct inode *inode = NULL;
1767 if (tcon->posix_extensions)
1768 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1769 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1770 else if (tcon->unix_ext)
1771 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1773 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1775 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1781 if (!S_ISDIR(inode->i_mode)) {
1783 * mkdir succeeded, but another client has managed to remove the
1784 * sucker and replace it with non-directory. Return success,
1785 * but don't leave the child in dcache.
1792 * setting nlink not necessary except in cases where we failed to get it
1793 * from the server or was set bogus. Also, since this is a brand new
1794 * inode, no need to grab the i_lock before setting the i_nlink.
1796 if (inode->i_nlink < 2)
1797 set_nlink(inode, 2);
1798 mode &= ~current_umask();
1799 /* must turn on setgid bit if parent dir has it */
1800 if (parent->i_mode & S_ISGID)
1803 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1804 if (tcon->unix_ext) {
1805 struct cifs_unix_set_info_args args = {
1807 .ctime = NO_CHANGE_64,
1808 .atime = NO_CHANGE_64,
1809 .mtime = NO_CHANGE_64,
1812 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1813 args.uid = current_fsuid();
1814 if (parent->i_mode & S_ISGID)
1815 args.gid = parent->i_gid;
1817 args.gid = current_fsgid();
1819 args.uid = INVALID_UID; /* no change */
1820 args.gid = INVALID_GID; /* no change */
1822 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1824 cifs_remap(cifs_sb));
1828 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1829 struct TCP_Server_Info *server = tcon->ses->server;
1830 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1831 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1832 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1834 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1835 inode->i_mode = (mode | S_IFDIR);
1837 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1838 inode->i_uid = current_fsuid();
1839 if (inode->i_mode & S_ISGID)
1840 inode->i_gid = parent->i_gid;
1842 inode->i_gid = current_fsgid();
1845 d_instantiate(dentry, inode);
1849 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1851 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1852 const char *full_path, struct cifs_sb_info *cifs_sb,
1853 struct cifs_tcon *tcon, const unsigned int xid)
1857 FILE_UNIX_BASIC_INFO *info = NULL;
1858 struct inode *newinode = NULL;
1859 struct cifs_fattr fattr;
1861 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1864 goto posix_mkdir_out;
1867 mode &= ~current_umask();
1868 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1869 NULL /* netfid */, info, &oplock, full_path,
1870 cifs_sb->local_nls, cifs_remap(cifs_sb));
1871 if (rc == -EOPNOTSUPP)
1872 goto posix_mkdir_out;
1874 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1876 goto posix_mkdir_out;
1879 if (info->Type == cpu_to_le32(-1))
1880 /* no return info, go query for it */
1881 goto posix_mkdir_get_info;
1883 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1884 * need to set uid/gid.
1887 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1888 cifs_fill_uniqueid(inode->i_sb, &fattr);
1889 newinode = cifs_iget(inode->i_sb, &fattr);
1891 goto posix_mkdir_get_info;
1893 d_instantiate(dentry, newinode);
1895 #ifdef CONFIG_CIFS_DEBUG2
1896 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1897 dentry, dentry, newinode);
1899 if (newinode->i_nlink != 2)
1900 cifs_dbg(FYI, "unexpected number of links %d\n",
1907 posix_mkdir_get_info:
1908 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1910 goto posix_mkdir_out;
1912 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1914 int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
1915 struct dentry *direntry, umode_t mode)
1919 struct cifs_sb_info *cifs_sb;
1920 struct tcon_link *tlink;
1921 struct cifs_tcon *tcon;
1922 struct TCP_Server_Info *server;
1923 const char *full_path;
1926 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1929 cifs_sb = CIFS_SB(inode->i_sb);
1930 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1932 tlink = cifs_sb_tlink(cifs_sb);
1934 return PTR_ERR(tlink);
1935 tcon = tlink_tcon(tlink);
1939 page = alloc_dentry_path();
1940 full_path = build_path_from_dentry(direntry, page);
1941 if (IS_ERR(full_path)) {
1942 rc = PTR_ERR(full_path);
1946 server = tcon->ses->server;
1948 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1949 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1951 d_drop(direntry); /* for time being always refresh inode info */
1955 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1956 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1957 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1958 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1960 if (rc != -EOPNOTSUPP)
1963 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1965 if (!server->ops->mkdir) {
1970 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1971 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1973 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1978 /* TODO: skip this for smb2/smb3 */
1979 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1983 * Force revalidate to get parent dir info when needed since cached
1984 * attributes are invalid now.
1986 CIFS_I(inode)->time = 0;
1987 free_dentry_path(page);
1989 cifs_put_tlink(tlink);
1993 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1997 struct cifs_sb_info *cifs_sb;
1998 struct tcon_link *tlink;
1999 struct cifs_tcon *tcon;
2000 struct TCP_Server_Info *server;
2001 const char *full_path;
2002 void *page = alloc_dentry_path();
2003 struct cifsInodeInfo *cifsInode;
2005 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2009 full_path = build_path_from_dentry(direntry, page);
2010 if (IS_ERR(full_path)) {
2011 rc = PTR_ERR(full_path);
2015 cifs_sb = CIFS_SB(inode->i_sb);
2016 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2021 tlink = cifs_sb_tlink(cifs_sb);
2022 if (IS_ERR(tlink)) {
2023 rc = PTR_ERR(tlink);
2026 tcon = tlink_tcon(tlink);
2027 server = tcon->ses->server;
2029 if (!server->ops->rmdir) {
2031 cifs_put_tlink(tlink);
2035 if (tcon->nodelete) {
2037 cifs_put_tlink(tlink);
2041 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2042 cifs_put_tlink(tlink);
2045 spin_lock(&d_inode(direntry)->i_lock);
2046 i_size_write(d_inode(direntry), 0);
2047 clear_nlink(d_inode(direntry));
2048 spin_unlock(&d_inode(direntry)->i_lock);
2051 cifsInode = CIFS_I(d_inode(direntry));
2052 /* force revalidate to go get info when needed */
2053 cifsInode->time = 0;
2055 cifsInode = CIFS_I(inode);
2057 * Force revalidate to get parent dir info when needed since cached
2058 * attributes are invalid now.
2060 cifsInode->time = 0;
2062 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
2063 current_time(inode);
2066 free_dentry_path(page);
2072 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2073 const char *from_path, struct dentry *to_dentry,
2074 const char *to_path)
2076 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2077 struct tcon_link *tlink;
2078 struct cifs_tcon *tcon;
2079 struct TCP_Server_Info *server;
2080 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2081 struct cifs_fid fid;
2082 struct cifs_open_parms oparms;
2084 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2087 tlink = cifs_sb_tlink(cifs_sb);
2089 return PTR_ERR(tlink);
2090 tcon = tlink_tcon(tlink);
2091 server = tcon->ses->server;
2093 if (!server->ops->rename)
2096 /* try path-based rename first */
2097 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2100 * Don't bother with rename by filehandle unless file is busy and
2101 * source. Note that cross directory moves do not work with
2102 * rename by filehandle to various Windows servers.
2104 if (rc == 0 || rc != -EBUSY)
2105 goto do_rename_exit;
2107 /* Don't fall back to using SMB on SMB 2+ mount */
2108 if (server->vals->protocol_id != 0)
2109 goto do_rename_exit;
2111 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2112 /* open-file renames don't work across directories */
2113 if (to_dentry->d_parent != from_dentry->d_parent)
2114 goto do_rename_exit;
2117 oparms.cifs_sb = cifs_sb;
2118 /* open the file to be renamed -- we need DELETE perms */
2119 oparms.desired_access = DELETE;
2120 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2121 oparms.disposition = FILE_OPEN;
2122 oparms.path = from_path;
2124 oparms.reconnect = false;
2126 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2128 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2129 (const char *) to_dentry->d_name.name,
2130 cifs_sb->local_nls, cifs_remap(cifs_sb));
2131 CIFSSMBClose(xid, tcon, fid.netfid);
2133 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2136 d_move(from_dentry, to_dentry);
2137 cifs_put_tlink(tlink);
2142 cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
2143 struct dentry *source_dentry, struct inode *target_dir,
2144 struct dentry *target_dentry, unsigned int flags)
2146 const char *from_name, *to_name;
2147 void *page1, *page2;
2148 struct cifs_sb_info *cifs_sb;
2149 struct tcon_link *tlink;
2150 struct cifs_tcon *tcon;
2153 int retry_count = 0;
2154 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2155 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2156 FILE_UNIX_BASIC_INFO *info_buf_target;
2157 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2159 if (flags & ~RENAME_NOREPLACE)
2162 cifs_sb = CIFS_SB(source_dir->i_sb);
2163 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2166 tlink = cifs_sb_tlink(cifs_sb);
2168 return PTR_ERR(tlink);
2169 tcon = tlink_tcon(tlink);
2171 page1 = alloc_dentry_path();
2172 page2 = alloc_dentry_path();
2175 from_name = build_path_from_dentry(source_dentry, page1);
2176 if (IS_ERR(from_name)) {
2177 rc = PTR_ERR(from_name);
2178 goto cifs_rename_exit;
2181 to_name = build_path_from_dentry(target_dentry, page2);
2182 if (IS_ERR(to_name)) {
2183 rc = PTR_ERR(to_name);
2184 goto cifs_rename_exit;
2187 cifs_close_deferred_file_under_dentry(tcon, from_name);
2188 if (d_inode(target_dentry) != NULL)
2189 cifs_close_deferred_file_under_dentry(tcon, to_name);
2191 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2194 if (rc == -EACCES) {
2195 while (retry_count < 3) {
2196 cifs_close_all_deferred_files(tcon);
2197 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2206 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2208 if (flags & RENAME_NOREPLACE)
2209 goto cifs_rename_exit;
2211 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2212 if (rc == -EEXIST && tcon->unix_ext) {
2214 * Are src and dst hardlinks of same inode? We can only tell
2215 * with unix extensions enabled.
2218 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2220 if (info_buf_source == NULL) {
2222 goto cifs_rename_exit;
2225 info_buf_target = info_buf_source + 1;
2226 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2229 cifs_remap(cifs_sb));
2233 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2236 cifs_remap(cifs_sb));
2238 if (tmprc == 0 && (info_buf_source->UniqueId ==
2239 info_buf_target->UniqueId)) {
2240 /* same file, POSIX says that this is a noop */
2242 goto cifs_rename_exit;
2246 * else ... BB we could add the same check for Windows by
2247 * checking the UniqueId via FILE_INTERNAL_INFO
2251 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2253 /* Try unlinking the target dentry if it's not negative */
2254 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2255 if (d_is_dir(target_dentry))
2256 tmprc = cifs_rmdir(target_dir, target_dentry);
2258 tmprc = cifs_unlink(target_dir, target_dentry);
2260 goto cifs_rename_exit;
2261 rc = cifs_do_rename(xid, source_dentry, from_name,
2262 target_dentry, to_name);
2265 /* force revalidate to go get info when needed */
2266 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2268 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2269 target_dir->i_mtime = current_time(source_dir);
2272 kfree(info_buf_source);
2273 free_dentry_path(page2);
2274 free_dentry_path(page1);
2276 cifs_put_tlink(tlink);
2281 cifs_dentry_needs_reval(struct dentry *dentry)
2283 struct inode *inode = d_inode(dentry);
2284 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2285 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2286 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2287 struct cached_fid *cfid = NULL;
2289 if (cifs_i->time == 0)
2292 if (CIFS_CACHE_READ(cifs_i))
2295 if (!lookupCacheEnabled)
2298 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2299 spin_lock(&cfid->fid_lock);
2300 if (cfid->time && cifs_i->time > cfid->time) {
2301 spin_unlock(&cfid->fid_lock);
2302 close_cached_dir(cfid);
2305 spin_unlock(&cfid->fid_lock);
2306 close_cached_dir(cfid);
2309 * depending on inode type, check if attribute caching disabled for
2310 * files or directories
2312 if (S_ISDIR(inode->i_mode)) {
2313 if (!cifs_sb->ctx->acdirmax)
2315 if (!time_in_range(jiffies, cifs_i->time,
2316 cifs_i->time + cifs_sb->ctx->acdirmax))
2319 if (!cifs_sb->ctx->acregmax)
2321 if (!time_in_range(jiffies, cifs_i->time,
2322 cifs_i->time + cifs_sb->ctx->acregmax))
2326 /* hardlinked files w/ noserverino get "special" treatment */
2327 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2328 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2335 * Zap the cache. Called when invalid_mapping flag is set.
2338 cifs_invalidate_mapping(struct inode *inode)
2342 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2343 rc = invalidate_inode_pages2(inode->i_mapping);
2345 cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2353 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2355 * @key: currently unused
2356 * @mode: the task state to sleep in
2359 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2362 if (signal_pending_state(mode, current))
2363 return -ERESTARTSYS;
2368 cifs_revalidate_mapping(struct inode *inode)
2371 unsigned long *flags = &CIFS_I(inode)->flags;
2372 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2374 /* swapfiles are not supposed to be shared */
2375 if (IS_SWAPFILE(inode))
2378 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2379 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2383 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2384 /* for cache=singleclient, do not invalidate */
2385 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2386 goto skip_invalidate;
2388 rc = cifs_invalidate_mapping(inode);
2390 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2394 clear_bit_unlock(CIFS_INO_LOCK, flags);
2395 smp_mb__after_atomic();
2396 wake_up_bit(flags, CIFS_INO_LOCK);
2402 cifs_zap_mapping(struct inode *inode)
2404 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2405 return cifs_revalidate_mapping(inode);
2408 int cifs_revalidate_file_attr(struct file *filp)
2411 struct dentry *dentry = file_dentry(filp);
2412 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2413 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2414 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2416 if (!cifs_dentry_needs_reval(dentry))
2419 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2420 if (tlink_tcon(cfile->tlink)->unix_ext)
2421 rc = cifs_get_file_info_unix(filp);
2423 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2424 rc = cifs_get_file_info(filp);
2429 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2433 struct inode *inode = d_inode(dentry);
2434 struct super_block *sb = dentry->d_sb;
2435 const char *full_path;
2442 if (!cifs_dentry_needs_reval(dentry))
2447 page = alloc_dentry_path();
2448 full_path = build_path_from_dentry(dentry, page);
2449 if (IS_ERR(full_path)) {
2450 rc = PTR_ERR(full_path);
2454 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2455 full_path, inode, inode->i_count.counter,
2456 dentry, cifs_get_time(dentry), jiffies);
2459 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2460 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2461 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2462 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2464 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2466 if (rc == -EAGAIN && count++ < 10)
2469 free_dentry_path(page);
2475 int cifs_revalidate_file(struct file *filp)
2478 struct inode *inode = file_inode(filp);
2480 rc = cifs_revalidate_file_attr(filp);
2484 return cifs_revalidate_mapping(inode);
2487 /* revalidate a dentry's inode attributes */
2488 int cifs_revalidate_dentry(struct dentry *dentry)
2491 struct inode *inode = d_inode(dentry);
2493 rc = cifs_revalidate_dentry_attr(dentry);
2497 return cifs_revalidate_mapping(inode);
2500 int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
2501 struct kstat *stat, u32 request_mask, unsigned int flags)
2503 struct dentry *dentry = path->dentry;
2504 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2505 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2506 struct inode *inode = d_inode(dentry);
2509 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2513 * We need to be sure that all dirty pages are written and the server
2514 * has actual ctime, mtime and file length.
2516 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2517 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2518 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2519 rc = filemap_fdatawait(inode->i_mapping);
2521 mapping_set_error(inode->i_mapping, rc);
2526 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2527 CIFS_I(inode)->time = 0; /* force revalidate */
2530 * If the caller doesn't require syncing, only sync if
2531 * necessary (e.g. due to earlier truncate or setattr
2532 * invalidating the cached metadata)
2534 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2535 (CIFS_I(inode)->time == 0)) {
2536 rc = cifs_revalidate_dentry_attr(dentry);
2541 generic_fillattr(&init_user_ns, inode, stat);
2542 stat->blksize = cifs_sb->ctx->bsize;
2543 stat->ino = CIFS_I(inode)->uniqueid;
2545 /* old CIFS Unix Extensions doesn't return create time */
2546 if (CIFS_I(inode)->createtime) {
2547 stat->result_mask |= STATX_BTIME;
2549 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2552 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2553 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2554 stat->attributes |= STATX_ATTR_COMPRESSED;
2555 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2556 stat->attributes |= STATX_ATTR_ENCRYPTED;
2559 * If on a multiuser mount without unix extensions or cifsacl being
2560 * enabled, and the admin hasn't overridden them, set the ownership
2561 * to the fsuid/fsgid of the current process.
2563 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2564 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2566 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2567 stat->uid = current_fsuid();
2568 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2569 stat->gid = current_fsgid();
2574 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2577 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2578 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2579 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2580 struct TCP_Server_Info *server = tcon->ses->server;
2581 struct cifsFileInfo *cfile;
2584 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2588 * We need to be sure that all dirty pages are written as they
2589 * might fill holes on the server.
2591 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2592 inode->i_mapping->nrpages != 0) {
2593 rc = filemap_fdatawait(inode->i_mapping);
2595 mapping_set_error(inode->i_mapping, rc);
2600 cfile = find_readable_file(cifs_i, false);
2604 if (server->ops->fiemap) {
2605 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2606 cifsFileInfo_put(cfile);
2610 cifsFileInfo_put(cfile);
2614 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2616 pgoff_t index = from >> PAGE_SHIFT;
2617 unsigned offset = from & (PAGE_SIZE - 1);
2621 page = grab_cache_page(mapping, index);
2625 zero_user_segment(page, offset, PAGE_SIZE);
2631 void cifs_setsize(struct inode *inode, loff_t offset)
2633 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2635 spin_lock(&inode->i_lock);
2636 i_size_write(inode, offset);
2637 spin_unlock(&inode->i_lock);
2639 /* Cached inode must be refreshed on truncate */
2641 truncate_pagecache(inode, offset);
2645 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2646 unsigned int xid, const char *full_path)
2649 struct cifsFileInfo *open_file;
2650 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2651 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2652 struct tcon_link *tlink = NULL;
2653 struct cifs_tcon *tcon = NULL;
2654 struct TCP_Server_Info *server;
2657 * To avoid spurious oplock breaks from server, in the case of
2658 * inodes that we already have open, avoid doing path based
2659 * setting of file size if we can do it by handle.
2660 * This keeps our caching token (oplock) and avoids timeouts
2661 * when the local oplock break takes longer to flush
2662 * writebehind data than the SMB timeout for the SetPathInfo
2663 * request would allow
2665 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2667 tcon = tlink_tcon(open_file->tlink);
2668 server = tcon->ses->server;
2669 if (server->ops->set_file_size)
2670 rc = server->ops->set_file_size(xid, tcon, open_file,
2671 attrs->ia_size, false);
2674 cifsFileInfo_put(open_file);
2675 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2683 tlink = cifs_sb_tlink(cifs_sb);
2685 return PTR_ERR(tlink);
2686 tcon = tlink_tcon(tlink);
2687 server = tcon->ses->server;
2691 * Set file size by pathname rather than by handle either because no
2692 * valid, writeable file handle for it was found or because there was
2693 * an error setting it by handle.
2695 if (server->ops->set_path_size)
2696 rc = server->ops->set_path_size(xid, tcon, full_path,
2697 attrs->ia_size, cifs_sb, false);
2700 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2703 cifs_put_tlink(tlink);
2707 cifsInode->server_eof = attrs->ia_size;
2708 cifs_setsize(inode, attrs->ia_size);
2710 * i_blocks is not related to (i_size / i_blksize), but instead
2711 * 512 byte (2**9) size is required for calculating num blocks.
2712 * Until we can query the server for actual allocation size,
2713 * this is best estimate we have for blocks allocated for a file
2714 * Number of blocks must be rounded up so size 1 is not 0 blocks
2716 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2719 * The man page of truncate says if the size changed,
2720 * then the st_ctime and st_mtime fields for the file
2723 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2724 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2726 cifs_truncate_page(inode->i_mapping, inode->i_size);
2732 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2734 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2738 const char *full_path;
2739 void *page = alloc_dentry_path();
2740 struct inode *inode = d_inode(direntry);
2741 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2742 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2743 struct tcon_link *tlink;
2744 struct cifs_tcon *pTcon;
2745 struct cifs_unix_set_info_args *args = NULL;
2746 struct cifsFileInfo *open_file;
2748 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2749 direntry, attrs->ia_valid);
2753 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2754 attrs->ia_valid |= ATTR_FORCE;
2756 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2760 full_path = build_path_from_dentry(direntry, page);
2761 if (IS_ERR(full_path)) {
2762 rc = PTR_ERR(full_path);
2767 * Attempt to flush data before changing attributes. We need to do
2768 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2769 * ownership or mode then we may also need to do this. Here, we take
2770 * the safe way out and just do the flush on all setattr requests. If
2771 * the flush returns error, store it to report later and continue.
2773 * BB: This should be smarter. Why bother flushing pages that
2774 * will be truncated anyway? Also, should we error out here if
2775 * the flush returns error?
2777 rc = filemap_write_and_wait(inode->i_mapping);
2778 if (is_interrupt_error(rc)) {
2783 mapping_set_error(inode->i_mapping, rc);
2786 if (attrs->ia_valid & ATTR_SIZE) {
2787 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2792 /* skip mode change if it's just for clearing setuid/setgid */
2793 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2794 attrs->ia_valid &= ~ATTR_MODE;
2796 args = kmalloc(sizeof(*args), GFP_KERNEL);
2802 /* set up the struct */
2803 if (attrs->ia_valid & ATTR_MODE)
2804 args->mode = attrs->ia_mode;
2806 args->mode = NO_CHANGE_64;
2808 if (attrs->ia_valid & ATTR_UID)
2809 args->uid = attrs->ia_uid;
2811 args->uid = INVALID_UID; /* no change */
2813 if (attrs->ia_valid & ATTR_GID)
2814 args->gid = attrs->ia_gid;
2816 args->gid = INVALID_GID; /* no change */
2818 if (attrs->ia_valid & ATTR_ATIME)
2819 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2821 args->atime = NO_CHANGE_64;
2823 if (attrs->ia_valid & ATTR_MTIME)
2824 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2826 args->mtime = NO_CHANGE_64;
2828 if (attrs->ia_valid & ATTR_CTIME)
2829 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2831 args->ctime = NO_CHANGE_64;
2834 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2836 u16 nfid = open_file->fid.netfid;
2837 u32 npid = open_file->pid;
2838 pTcon = tlink_tcon(open_file->tlink);
2839 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2840 cifsFileInfo_put(open_file);
2842 tlink = cifs_sb_tlink(cifs_sb);
2843 if (IS_ERR(tlink)) {
2844 rc = PTR_ERR(tlink);
2847 pTcon = tlink_tcon(tlink);
2848 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2850 cifs_remap(cifs_sb));
2851 cifs_put_tlink(tlink);
2857 if ((attrs->ia_valid & ATTR_SIZE) &&
2858 attrs->ia_size != i_size_read(inode)) {
2859 truncate_setsize(inode, attrs->ia_size);
2860 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2863 setattr_copy(&init_user_ns, inode, attrs);
2864 mark_inode_dirty(inode);
2866 /* force revalidate when any of these times are set since some
2867 of the fs types (eg ext3, fat) do not have fine enough
2868 time granularity to match protocol, and we do not have a
2869 a way (yet) to query the server fs's time granularity (and
2870 whether it rounds times down).
2872 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2873 cifsInode->time = 0;
2876 free_dentry_path(page);
2880 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2883 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2886 kuid_t uid = INVALID_UID;
2887 kgid_t gid = INVALID_GID;
2888 struct inode *inode = d_inode(direntry);
2889 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2890 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2891 struct cifsFileInfo *wfile;
2892 struct cifs_tcon *tcon;
2893 const char *full_path;
2894 void *page = alloc_dentry_path();
2897 __u64 mode = NO_CHANGE_64;
2901 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2902 direntry, attrs->ia_valid);
2904 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2905 attrs->ia_valid |= ATTR_FORCE;
2907 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2909 goto cifs_setattr_exit;
2911 full_path = build_path_from_dentry(direntry, page);
2912 if (IS_ERR(full_path)) {
2913 rc = PTR_ERR(full_path);
2914 goto cifs_setattr_exit;
2918 * Attempt to flush data before changing attributes. We need to do
2919 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2920 * returns error, store it to report later and continue.
2922 * BB: This should be smarter. Why bother flushing pages that
2923 * will be truncated anyway? Also, should we error out here if
2924 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2926 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2927 rc = filemap_write_and_wait(inode->i_mapping);
2928 if (is_interrupt_error(rc)) {
2930 goto cifs_setattr_exit;
2932 mapping_set_error(inode->i_mapping, rc);
2937 if ((attrs->ia_valid & ATTR_MTIME) &&
2938 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2939 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2941 tcon = tlink_tcon(wfile->tlink);
2942 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2943 cifsFileInfo_put(wfile);
2945 goto cifs_setattr_exit;
2946 } else if (rc != -EBADF)
2947 goto cifs_setattr_exit;
2952 if (attrs->ia_valid & ATTR_SIZE) {
2953 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2955 goto cifs_setattr_exit;
2958 if (attrs->ia_valid & ATTR_UID)
2959 uid = attrs->ia_uid;
2961 if (attrs->ia_valid & ATTR_GID)
2962 gid = attrs->ia_gid;
2964 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2965 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2966 if (uid_valid(uid) || gid_valid(gid)) {
2967 mode = NO_CHANGE_64;
2968 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2971 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2973 goto cifs_setattr_exit;
2977 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2978 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2980 /* skip mode change if it's just for clearing setuid/setgid */
2981 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2982 attrs->ia_valid &= ~ATTR_MODE;
2984 if (attrs->ia_valid & ATTR_MODE) {
2985 mode = attrs->ia_mode;
2987 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2988 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2989 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2990 INVALID_UID, INVALID_GID);
2992 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2994 goto cifs_setattr_exit;
2998 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
2999 * Pick up the actual mode bits that were set.
3001 if (mode != attrs->ia_mode)
3002 attrs->ia_mode = mode;
3004 if (((mode & S_IWUGO) == 0) &&
3005 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3007 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3009 /* fix up mode if we're not using dynperm */
3010 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3011 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3012 } else if ((mode & S_IWUGO) &&
3013 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3015 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3016 /* Attributes of 0 are ignored */
3018 dosattr |= ATTR_NORMAL;
3020 /* reset local inode permissions to normal */
3021 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3022 attrs->ia_mode &= ~(S_IALLUGO);
3023 if (S_ISDIR(inode->i_mode))
3025 cifs_sb->ctx->dir_mode;
3028 cifs_sb->ctx->file_mode;
3030 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3031 /* ignore mode change - ATTR_READONLY hasn't changed */
3032 attrs->ia_valid &= ~ATTR_MODE;
3036 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3037 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3038 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3039 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3041 /* Even if error on time set, no sense failing the call if
3042 the server would set the time to a reasonable value anyway,
3043 and this check ensures that we are not being called from
3044 sys_utimes in which case we ought to fail the call back to
3045 the user when the server rejects the call */
3046 if ((rc) && (attrs->ia_valid &
3047 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3051 /* do not need local check to inode_check_ok since the server does
3054 goto cifs_setattr_exit;
3056 if ((attrs->ia_valid & ATTR_SIZE) &&
3057 attrs->ia_size != i_size_read(inode)) {
3058 truncate_setsize(inode, attrs->ia_size);
3059 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3062 setattr_copy(&init_user_ns, inode, attrs);
3063 mark_inode_dirty(inode);
3067 free_dentry_path(page);
3072 cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
3073 struct iattr *attrs)
3075 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3076 int rc, retries = 0;
3077 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3078 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3079 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3081 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3085 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3086 if (pTcon->unix_ext)
3087 rc = cifs_setattr_unix(direntry, attrs);
3089 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3090 rc = cifs_setattr_nounix(direntry, attrs);
3092 } while (is_retryable_error(rc) && retries < 2);
3094 /* BB: add cifs_setattr_legacy for really old servers */