1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
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 if (IS_AUTOMOUNT(inode)) {
62 inode->i_op = &cifs_namespace_inode_operations;
64 inode->i_op = &cifs_dir_inode_ops;
65 inode->i_fop = &cifs_dir_ops;
69 inode->i_op = &cifs_symlink_inode_ops;
72 init_special_inode(inode, inode->i_mode, inode->i_rdev);
77 /* check inode attributes against fattr. If they don't match, tag the
78 * inode for cache invalidation
81 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
83 struct cifs_fscache_inode_coherency_data cd;
84 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
85 struct timespec64 mtime;
87 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
88 __func__, cifs_i->uniqueid);
90 if (inode->i_state & I_NEW) {
91 cifs_dbg(FYI, "%s: inode %llu is new\n",
92 __func__, cifs_i->uniqueid);
96 /* don't bother with revalidation if we have an oplock */
97 if (CIFS_CACHE_READ(cifs_i)) {
98 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
99 __func__, cifs_i->uniqueid);
103 /* revalidate if mtime or size have changed */
104 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
105 mtime = inode_get_mtime(inode);
106 if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
107 cifs_i->netfs.remote_i_size == fattr->cf_eof) {
108 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
109 __func__, cifs_i->uniqueid);
113 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
114 __func__, cifs_i->uniqueid);
115 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
116 /* Invalidate fscache cookie */
117 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
118 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
122 * copy nlink to the inode, unless it wasn't provided. Provide
123 * sane values if we don't have an existing one and none was provided
126 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
129 * if we're in a situation where we can't trust what we
130 * got from the server (readdir, some non-unix cases)
131 * fake reasonable values
133 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
134 /* only provide fake values on a new inode */
135 if (inode->i_state & I_NEW) {
136 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
144 /* we trust the server, so update it */
145 set_nlink(inode, fattr->cf_nlink);
148 /* populate an inode with info from a cifs_fattr struct */
150 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
153 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
154 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
156 if (!(inode->i_state & I_NEW) &&
157 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
158 CIFS_I(inode)->time = 0; /* force reval */
162 cifs_revalidate_cache(inode, fattr);
164 spin_lock(&inode->i_lock);
165 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
166 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
167 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
168 /* we do not want atime to be less than mtime, it broke some apps */
169 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
170 inode_set_atime_to_ts(inode, fattr->cf_mtime);
172 inode_set_atime_to_ts(inode, fattr->cf_atime);
173 inode_set_mtime_to_ts(inode, fattr->cf_mtime);
174 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
175 inode->i_rdev = fattr->cf_rdev;
176 cifs_nlink_fattr_to_inode(inode, fattr);
177 inode->i_uid = fattr->cf_uid;
178 inode->i_gid = fattr->cf_gid;
180 /* if dynperm is set, don't clobber existing mode */
181 if (inode->i_state & I_NEW ||
182 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
183 inode->i_mode = fattr->cf_mode;
185 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
186 cifs_i->reparse_tag = fattr->cf_cifstag;
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->netfs.remote_i_size = 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, from_readdir)) {
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) && fattr->cf_symlink_target) {
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_JUNCTION)
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 or reparse mount point).
332 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
333 struct super_block *sb)
335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
337 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
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_JUNCTION;
349 /* Update inode with final fattr data */
350 static int update_inode_info(struct super_block *sb,
351 struct cifs_fattr *fattr,
352 struct inode **inode)
354 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
358 *inode = cifs_iget(sb, fattr);
363 /* We already have inode, update it.
365 * If file type or uniqueid is different, return error.
367 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
368 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
369 CIFS_I(*inode)->time = 0; /* force reval */
372 return cifs_fattr_to_inode(*inode, fattr, false);
375 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
377 cifs_get_file_info_unix(struct file *filp)
381 FILE_UNIX_BASIC_INFO find_data;
382 struct cifs_fattr fattr = {};
383 struct inode *inode = file_inode(filp);
384 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
385 struct cifsFileInfo *cfile = filp->private_data;
386 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
390 if (cfile->symlink_target) {
391 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
392 if (!fattr.cf_symlink_target) {
394 goto cifs_gfiunix_out;
398 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
400 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
401 } else if (rc == -EREMOTE) {
402 cifs_create_junction_fattr(&fattr, inode->i_sb);
405 goto cifs_gfiunix_out;
407 rc = cifs_fattr_to_inode(inode, &fattr, false);
414 static int cifs_get_unix_fattr(const unsigned char *full_path,
415 struct super_block *sb,
416 struct cifs_fattr *fattr,
417 struct inode **pinode,
418 const unsigned int xid)
420 struct TCP_Server_Info *server;
421 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
422 FILE_UNIX_BASIC_INFO find_data;
423 struct cifs_tcon *tcon;
424 struct tcon_link *tlink;
427 cifs_dbg(FYI, "Getting info on %s\n", full_path);
429 tlink = cifs_sb_tlink(cifs_sb);
431 return PTR_ERR(tlink);
432 tcon = tlink_tcon(tlink);
433 server = tcon->ses->server;
435 /* could have done a find first instead but this returns more info */
436 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
437 cifs_sb->local_nls, cifs_remap(cifs_sb));
438 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
439 cifs_put_tlink(tlink);
442 cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
443 } else if (rc == -EREMOTE) {
444 cifs_create_junction_fattr(fattr, sb);
451 cifs_fill_uniqueid(sb, fattr);
453 /* check for Minshall+French symlinks */
454 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
455 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
456 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
459 if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
460 if (!server->ops->query_symlink)
462 rc = server->ops->query_symlink(xid, tcon,
464 &fattr->cf_symlink_target);
465 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
470 int cifs_get_inode_info_unix(struct inode **pinode,
471 const unsigned char *full_path,
472 struct super_block *sb, unsigned int xid)
474 struct cifs_fattr fattr = {};
477 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
481 rc = update_inode_info(sb, &fattr, pinode);
483 kfree(fattr.cf_symlink_target);
487 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
488 struct super_block *sb,
489 struct cifs_fattr *fattr,
490 struct inode **pinode,
491 const unsigned int xid)
496 int cifs_get_inode_info_unix(struct inode **pinode,
497 const unsigned char *full_path,
498 struct super_block *sb, unsigned int xid)
502 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
505 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
506 struct cifs_sb_info *cifs_sb, unsigned int xid)
510 struct tcon_link *tlink;
511 struct cifs_tcon *tcon;
513 struct cifs_open_parms oparms;
514 struct cifs_io_parms io_parms = {0};
516 unsigned int bytes_read;
518 int buf_type = CIFS_NO_BUFFER;
522 fattr->cf_mode &= ~S_IFMT;
524 if (fattr->cf_eof == 0) {
525 fattr->cf_mode |= S_IFIFO;
526 fattr->cf_dtype = DT_FIFO;
528 } else if (fattr->cf_eof < 8) {
529 fattr->cf_mode |= S_IFREG;
530 fattr->cf_dtype = DT_REG;
531 return -EINVAL; /* EOPNOTSUPP? */
534 tlink = cifs_sb_tlink(cifs_sb);
536 return PTR_ERR(tlink);
537 tcon = tlink_tcon(tlink);
539 oparms = (struct cifs_open_parms) {
542 .desired_access = GENERIC_READ,
543 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
544 .disposition = FILE_OPEN,
549 if (tcon->ses->server->oplocks)
553 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
555 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
556 cifs_put_tlink(tlink);
561 io_parms.netfid = fid.netfid;
562 io_parms.pid = current->tgid;
563 io_parms.tcon = tcon;
565 io_parms.length = 24;
567 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
568 &bytes_read, &pbuf, &buf_type);
569 if ((rc == 0) && (bytes_read >= 8)) {
570 if (memcmp("IntxBLK", pbuf, 8) == 0) {
571 cifs_dbg(FYI, "Block device\n");
572 fattr->cf_mode |= S_IFBLK;
573 fattr->cf_dtype = DT_BLK;
574 if (bytes_read == 24) {
575 /* we have enough to decode dev num */
576 __u64 mjr; /* major */
577 __u64 mnr; /* minor */
578 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
579 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
580 fattr->cf_rdev = MKDEV(mjr, mnr);
582 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
583 cifs_dbg(FYI, "Char device\n");
584 fattr->cf_mode |= S_IFCHR;
585 fattr->cf_dtype = DT_CHR;
586 if (bytes_read == 24) {
587 /* we have enough to decode dev num */
588 __u64 mjr; /* major */
589 __u64 mnr; /* minor */
590 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
591 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
592 fattr->cf_rdev = MKDEV(mjr, mnr);
594 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
595 cifs_dbg(FYI, "Symlink\n");
596 fattr->cf_mode |= S_IFLNK;
597 fattr->cf_dtype = DT_LNK;
598 } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
599 cifs_dbg(FYI, "FIFO\n");
600 fattr->cf_mode |= S_IFIFO;
601 fattr->cf_dtype = DT_FIFO;
603 fattr->cf_mode |= S_IFREG; /* file? */
604 fattr->cf_dtype = DT_REG;
608 fattr->cf_mode |= S_IFREG; /* then it is a file */
609 fattr->cf_dtype = DT_REG;
610 rc = -EOPNOTSUPP; /* or some unknown SFU type */
613 tcon->ses->server->ops->close(xid, tcon, &fid);
614 cifs_put_tlink(tlink);
618 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
621 * Fetch mode bits as provided by SFU.
623 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
625 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
626 struct cifs_sb_info *cifs_sb, unsigned int xid)
628 #ifdef CONFIG_CIFS_XATTR
632 struct tcon_link *tlink;
633 struct cifs_tcon *tcon;
635 tlink = cifs_sb_tlink(cifs_sb);
637 return PTR_ERR(tlink);
638 tcon = tlink_tcon(tlink);
640 if (tcon->ses->server->ops->query_all_EAs == NULL) {
641 cifs_put_tlink(tlink);
645 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
646 "SETFILEBITS", ea_value, 4 /* size of buf */,
648 cifs_put_tlink(tlink);
652 mode = le32_to_cpu(*((__le32 *)ea_value));
653 fattr->cf_mode &= ~SFBITS_MASK;
654 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
655 mode, fattr->cf_mode);
656 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
657 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
666 /* Fill a cifs_fattr struct with info from POSIX info struct */
667 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
668 struct cifs_open_info_data *data,
669 struct super_block *sb)
671 struct smb311_posix_qinfo *info = &data->posix_fi;
672 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
673 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
675 memset(fattr, 0, sizeof(*fattr));
677 /* no fattr->flags to set */
678 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
679 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
681 if (info->LastAccessTime)
682 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
684 ktime_get_coarse_real_ts64(&fattr->cf_atime);
686 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
687 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
689 if (data->adjust_tz) {
690 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
691 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
695 * The srv fs device id is overridden on network mount so setting
696 * @fattr->cf_rdev isn't needed here.
698 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
699 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
700 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
701 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
702 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
704 if (cifs_open_data_reparse(data) &&
705 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
708 fattr->cf_mode &= ~S_IFMT;
709 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
710 fattr->cf_mode |= S_IFDIR;
711 fattr->cf_dtype = DT_DIR;
713 fattr->cf_mode |= S_IFREG;
714 fattr->cf_dtype = DT_REG;
718 if (S_ISLNK(fattr->cf_mode)) {
719 if (likely(data->symlink_target))
720 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
721 fattr->cf_symlink_target = data->symlink_target;
722 data->symlink_target = NULL;
724 sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER);
725 sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP);
727 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
728 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
731 static inline dev_t nfs_mkdev(struct reparse_posix_data *buf)
733 u64 v = le64_to_cpu(*(__le64 *)buf->DataBuffer);
735 return MKDEV(v >> 32, v & 0xffffffff);
738 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
739 struct cifs_fattr *fattr,
740 struct cifs_open_info_data *data)
742 struct reparse_posix_data *buf = data->reparse.posix;
743 u32 tag = data->reparse.tag;
745 if (tag == IO_REPARSE_TAG_NFS && buf) {
746 switch (le64_to_cpu(buf->InodeType)) {
747 case NFS_SPECFILE_CHR:
748 fattr->cf_mode |= S_IFCHR;
749 fattr->cf_dtype = DT_CHR;
750 fattr->cf_rdev = nfs_mkdev(buf);
752 case NFS_SPECFILE_BLK:
753 fattr->cf_mode |= S_IFBLK;
754 fattr->cf_dtype = DT_BLK;
755 fattr->cf_rdev = nfs_mkdev(buf);
757 case NFS_SPECFILE_FIFO:
758 fattr->cf_mode |= S_IFIFO;
759 fattr->cf_dtype = DT_FIFO;
761 case NFS_SPECFILE_SOCK:
762 fattr->cf_mode |= S_IFSOCK;
763 fattr->cf_dtype = DT_SOCK;
765 case NFS_SPECFILE_LNK:
766 fattr->cf_mode |= S_IFLNK;
767 fattr->cf_dtype = DT_LNK;
777 case IO_REPARSE_TAG_LX_SYMLINK:
778 fattr->cf_mode |= S_IFLNK;
779 fattr->cf_dtype = DT_LNK;
781 case IO_REPARSE_TAG_LX_FIFO:
782 fattr->cf_mode |= S_IFIFO;
783 fattr->cf_dtype = DT_FIFO;
785 case IO_REPARSE_TAG_AF_UNIX:
786 fattr->cf_mode |= S_IFSOCK;
787 fattr->cf_dtype = DT_SOCK;
789 case IO_REPARSE_TAG_LX_CHR:
790 fattr->cf_mode |= S_IFCHR;
791 fattr->cf_dtype = DT_CHR;
793 case IO_REPARSE_TAG_LX_BLK:
794 fattr->cf_mode |= S_IFBLK;
795 fattr->cf_dtype = DT_BLK;
797 case 0: /* SMB1 symlink */
798 case IO_REPARSE_TAG_SYMLINK:
799 case IO_REPARSE_TAG_NFS:
800 fattr->cf_mode |= S_IFLNK;
801 fattr->cf_dtype = DT_LNK;
809 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
810 struct cifs_open_info_data *data,
811 struct super_block *sb)
813 struct smb2_file_all_info *info = &data->fi;
814 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
815 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
817 memset(fattr, 0, sizeof(*fattr));
818 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
819 if (info->DeletePending)
820 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
822 if (info->LastAccessTime)
823 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
825 ktime_get_coarse_real_ts64(&fattr->cf_atime);
827 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
828 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
830 if (data->adjust_tz) {
831 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
832 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
835 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
836 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
837 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
838 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
840 fattr->cf_mode = cifs_sb->ctx->file_mode;
841 if (cifs_open_data_reparse(data) &&
842 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
845 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
846 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
847 fattr->cf_dtype = DT_DIR;
849 * Server can return wrong NumberOfLinks value for directories
850 * when Unix extensions are disabled - fake it.
853 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
855 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
856 fattr->cf_dtype = DT_REG;
858 /* clear write bits if ATTR_READONLY is set */
859 if (fattr->cf_cifsattrs & ATTR_READONLY)
860 fattr->cf_mode &= ~(S_IWUGO);
863 * Don't accept zero nlink from non-unix servers unless
864 * delete is pending. Instead mark it as unknown.
866 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
867 !info->DeletePending) {
868 cifs_dbg(VFS, "bogus file nlink value %u\n",
870 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
875 if (S_ISLNK(fattr->cf_mode)) {
876 if (likely(data->symlink_target))
877 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
878 fattr->cf_symlink_target = data->symlink_target;
879 data->symlink_target = NULL;
882 fattr->cf_uid = cifs_sb->ctx->linux_uid;
883 fattr->cf_gid = cifs_sb->ctx->linux_gid;
887 cifs_get_file_info(struct file *filp)
891 struct cifs_open_info_data data = {};
892 struct cifs_fattr fattr;
893 struct inode *inode = file_inode(filp);
894 struct cifsFileInfo *cfile = filp->private_data;
895 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
896 struct TCP_Server_Info *server = tcon->ses->server;
898 if (!server->ops->query_file_info)
902 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
905 /* TODO: add support to query reparse tag */
906 data.adjust_tz = false;
907 if (data.symlink_target) {
909 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
911 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
914 cifs_create_junction_fattr(&fattr, inode->i_sb);
920 * FIXME: legacy server -- fall back to path-based call?
921 * for now, just skip revalidating and mark inode for
925 CIFS_I(inode)->time = 0;
932 * don't bother with SFU junk here -- just mark inode as needing
935 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
936 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
937 /* if filetype is different, return error */
938 rc = cifs_fattr_to_inode(inode, &fattr, false);
940 cifs_free_open_info(&data);
945 /* Simple function to return a 64 bit hash of string. Rarely called */
946 static __u64 simple_hashstr(const char *str)
948 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
952 hash = (hash + (__u64) *str++) * hash_mult;
957 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
959 * cifs_backup_query_path_info - SMB1 fallback code to get ino
961 * Fallback code to get file metadata when we don't have access to
962 * full_path (EACCES) and have backup creds.
964 * @xid: transaction id used to identify original request in logs
965 * @tcon: information about the server share we have mounted
966 * @sb: the superblock stores info such as disk space available
967 * @full_path: name of the file we are getting the metadata for
968 * @resp_buf: will be set to cifs resp buf and needs to be freed with
969 * cifs_buf_release() when done with @data
970 * @data: will be set to search info result buffer
973 cifs_backup_query_path_info(int xid,
974 struct cifs_tcon *tcon,
975 struct super_block *sb,
976 const char *full_path,
978 FILE_ALL_INFO **data)
980 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
981 struct cifs_search_info info = {0};
986 info.endOfSearch = false;
988 info.info_level = SMB_FIND_FILE_UNIX;
989 else if ((tcon->ses->capabilities &
990 tcon->ses->server->vals->cap_nt_find) == 0)
991 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
992 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
993 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
994 else /* no srvino useful for fallback to some netapp */
995 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
997 flags = CIFS_SEARCH_CLOSE_ALWAYS |
998 CIFS_SEARCH_CLOSE_AT_END |
999 CIFS_SEARCH_BACKUP_SEARCH;
1001 rc = CIFSFindFirst(xid, tcon, full_path,
1002 cifs_sb, NULL, flags, &info, false);
1006 *resp_buf = (void *)info.ntwrk_buf_start;
1007 *data = (FILE_ALL_INFO *)info.srch_entries_start;
1010 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1012 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1013 struct inode **inode, const char *full_path,
1014 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1016 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1017 struct TCP_Server_Info *server = tcon->ses->server;
1020 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1022 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1024 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1029 * If we have an inode pass a NULL tcon to ensure we don't
1030 * make a round trip to the server. This only works for SMB2+.
1032 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1033 &fattr->cf_uniqueid, data);
1036 * If that fails reuse existing ino or generate one
1037 * and disable server ones
1040 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1042 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1043 cifs_autodisable_serverino(cifs_sb);
1048 /* If no errors, check for zero root inode (invalid) */
1049 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1050 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1053 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1055 /* make an ino by hashing the UNC */
1056 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1057 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1062 static inline bool is_inode_cache_good(struct inode *ino)
1064 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1067 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1068 struct super_block *sb,
1069 const unsigned int xid,
1070 struct cifs_tcon *tcon,
1071 const char *full_path,
1072 struct cifs_fattr *fattr)
1074 struct TCP_Server_Info *server = tcon->ses->server;
1075 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1076 struct kvec rsp_iov, *iov = NULL;
1077 int rsp_buftype = CIFS_NO_BUFFER;
1078 u32 tag = data->reparse.tag;
1081 if (!tag && server->ops->query_reparse_point) {
1082 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1084 &rsp_iov, &rsp_buftype);
1087 } else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1088 data->reparse.io.iov.iov_base) {
1089 iov = &data->reparse.io.iov;
1093 switch ((data->reparse.tag = tag)) {
1094 case 0: /* SMB1 symlink */
1095 if (server->ops->query_symlink) {
1096 rc = server->ops->query_symlink(xid, tcon,
1098 &data->symlink_target);
1101 case IO_REPARSE_TAG_MOUNT_POINT:
1102 cifs_create_junction_fattr(fattr, sb);
1106 /* Check for cached reparse point data */
1107 if (data->symlink_target || data->reparse.buf) {
1109 } else if (iov && server->ops->parse_reparse_point) {
1110 rc = server->ops->parse_reparse_point(cifs_sb,
1116 if (tcon->posix_extensions)
1117 smb311_posix_info_to_fattr(fattr, data, sb);
1119 cifs_open_info_to_fattr(fattr, data, sb);
1121 fattr->cf_cifstag = data->reparse.tag;
1122 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1126 static int cifs_get_fattr(struct cifs_open_info_data *data,
1127 struct super_block *sb, int xid,
1128 const struct cifs_fid *fid,
1129 struct cifs_fattr *fattr,
1130 struct inode **inode,
1131 const char *full_path)
1133 struct cifs_open_info_data tmp_data = {};
1134 struct cifs_tcon *tcon;
1135 struct TCP_Server_Info *server;
1136 struct tcon_link *tlink;
1137 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1138 void *smb1_backup_rsp_buf = NULL;
1142 tlink = cifs_sb_tlink(cifs_sb);
1144 return PTR_ERR(tlink);
1145 tcon = tlink_tcon(tlink);
1146 server = tcon->ses->server;
1149 * 1. Fetch file metadata if not provided (data)
1153 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1154 full_path, &tmp_data);
1159 * 2. Convert it to internal cifs metadata (fattr)
1165 * If the file is a reparse point, it is more complicated
1166 * since we have to check if its reparse tag matches a known
1167 * special file type e.g. symlink or fifo or char etc.
1169 if (cifs_open_data_reparse(data)) {
1170 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1173 cifs_open_info_to_fattr(fattr, data, sb);
1177 /* DFS link, no metadata available on this server */
1178 cifs_create_junction_fattr(fattr, sb);
1182 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1184 * perm errors, try again with backup flags if possible
1186 * For SMB2 and later the backup intent flag
1187 * is already sent if needed on open and there
1188 * is no path based FindFirst operation to use
1191 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1192 /* for easier reading */
1194 FILE_DIRECTORY_INFO *fdi;
1195 SEARCH_ID_FULL_DIR_INFO *si;
1197 rc = cifs_backup_query_path_info(xid, tcon, sb,
1199 &smb1_backup_rsp_buf,
1204 move_cifs_info_to_smb2(&data->fi, fi);
1205 fdi = (FILE_DIRECTORY_INFO *)fi;
1206 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1208 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1209 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1210 /* uniqueid set, skip get inum step */
1211 goto handle_mnt_opt;
1213 /* nothing we can do, bail out */
1218 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1221 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1226 * 3. Get or update inode number (fattr->cf_uniqueid)
1229 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1232 * 4. Tweak fattr based on mount options
1234 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1236 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1237 /* query for SFU type info if supported and needed */
1238 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1239 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1240 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1242 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1245 /* fill in 0777 bits from ACL */
1246 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1247 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1248 true, full_path, fid);
1252 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1256 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1257 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1258 false, full_path, fid);
1262 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1268 /* fill in remaining high mode bits e.g. SUID, VTX */
1269 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1270 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1272 /* check for Minshall+French symlinks */
1273 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1274 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1275 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1279 cifs_buf_release(smb1_backup_rsp_buf);
1280 cifs_put_tlink(tlink);
1281 cifs_free_open_info(&tmp_data);
1285 int cifs_get_inode_info(struct inode **inode,
1286 const char *full_path,
1287 struct cifs_open_info_data *data,
1288 struct super_block *sb, int xid,
1289 const struct cifs_fid *fid)
1291 struct cifs_fattr fattr = {};
1294 if (is_inode_cache_good(*inode)) {
1295 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1299 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1303 rc = update_inode_info(sb, &fattr, inode);
1305 kfree(fattr.cf_symlink_target);
1309 static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1310 struct cifs_fattr *fattr,
1311 const char *full_path,
1312 struct super_block *sb,
1313 const unsigned int xid)
1315 struct cifs_open_info_data tmp_data = {};
1316 struct TCP_Server_Info *server;
1317 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1318 struct cifs_tcon *tcon;
1319 struct tcon_link *tlink;
1323 tlink = cifs_sb_tlink(cifs_sb);
1325 return PTR_ERR(tlink);
1326 tcon = tlink_tcon(tlink);
1327 server = tcon->ses->server;
1330 * 1. Fetch file metadata if not provided (data)
1333 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1334 full_path, &tmp_data);
1339 * 2. Convert it to internal cifs metadata (fattr)
1344 if (cifs_open_data_reparse(data)) {
1345 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1348 smb311_posix_info_to_fattr(fattr, data, sb);
1352 /* DFS link, no metadata available on this server */
1353 cifs_create_junction_fattr(fattr, sb);
1358 * For SMB2 and later the backup intent flag
1359 * is already sent if needed on open and there
1360 * is no path based FindFirst operation to use
1361 * to retry with so nothing we can do, bail out
1365 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1370 * 3. Tweak fattr based on mount options
1372 /* check for Minshall+French symlinks */
1373 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1374 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1375 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1379 cifs_put_tlink(tlink);
1380 cifs_free_open_info(data);
1384 int smb311_posix_get_inode_info(struct inode **inode,
1385 const char *full_path,
1386 struct cifs_open_info_data *data,
1387 struct super_block *sb,
1388 const unsigned int xid)
1390 struct cifs_fattr fattr = {};
1393 if (is_inode_cache_good(*inode)) {
1394 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1398 rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1402 rc = update_inode_info(sb, &fattr, inode);
1404 kfree(fattr.cf_symlink_target);
1408 static const struct inode_operations cifs_ipc_inode_ops = {
1409 .lookup = cifs_lookup,
1413 cifs_find_inode(struct inode *inode, void *opaque)
1415 struct cifs_fattr *fattr = opaque;
1417 /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1419 /* don't match inode with different uniqueid */
1420 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1423 /* use createtime like an i_generation field */
1424 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1427 /* don't match inode of different type */
1428 if (inode_wrong_type(inode, fattr->cf_mode))
1431 /* if it's not a directory or has no dentries, then flag it */
1432 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1433 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1439 cifs_init_inode(struct inode *inode, void *opaque)
1441 struct cifs_fattr *fattr = opaque;
1443 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1444 CIFS_I(inode)->createtime = fattr->cf_createtime;
1449 * walk dentry list for an inode and report whether it has aliases that
1450 * are hashed. We use this to determine if a directory inode can actually
1454 inode_has_hashed_dentries(struct inode *inode)
1456 struct dentry *dentry;
1458 spin_lock(&inode->i_lock);
1459 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1460 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1461 spin_unlock(&inode->i_lock);
1465 spin_unlock(&inode->i_lock);
1469 /* Given fattrs, get a corresponding inode */
1471 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1474 struct inode *inode;
1477 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1479 /* hash down to 32-bits on 32-bit arch */
1480 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1482 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1484 /* was there a potentially problematic inode collision? */
1485 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1486 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1488 if (inode_has_hashed_dentries(inode)) {
1489 cifs_autodisable_serverino(CIFS_SB(sb));
1491 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1492 goto retry_iget5_locked;
1496 /* can't fail - see cifs_find_inode() */
1497 cifs_fattr_to_inode(inode, fattr, false);
1498 if (sb->s_flags & SB_NOATIME)
1499 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1500 if (inode->i_state & I_NEW) {
1501 inode->i_ino = hash;
1502 cifs_fscache_get_inode_cookie(inode);
1503 unlock_new_inode(inode);
1510 /* gets root inode */
1511 struct inode *cifs_root_iget(struct super_block *sb)
1513 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1514 struct cifs_fattr fattr = {};
1515 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1516 struct inode *inode = NULL;
1522 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1523 && cifs_sb->prepath) {
1524 len = strlen(cifs_sb->prepath);
1525 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1527 return ERR_PTR(-ENOMEM);
1529 memcpy(path+1, cifs_sb->prepath, len);
1531 path = kstrdup("", GFP_KERNEL);
1533 return ERR_PTR(-ENOMEM);
1537 if (tcon->unix_ext) {
1538 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1539 /* some servers mistakenly claim POSIX support */
1540 if (rc != -EOPNOTSUPP)
1542 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1543 tcon->unix_ext = false;
1546 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1547 if (tcon->posix_extensions)
1548 rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1550 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1554 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1555 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1556 cifs_autodisable_serverino(cifs_sb);
1558 inode = cifs_iget(sb, &fattr);
1562 inode = ERR_PTR(rc);
1566 if (rc && tcon->pipe) {
1567 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1568 spin_lock(&inode->i_lock);
1569 inode->i_mode |= S_IFDIR;
1570 set_nlink(inode, 2);
1571 inode->i_op = &cifs_ipc_inode_ops;
1572 inode->i_fop = &simple_dir_operations;
1573 inode->i_uid = cifs_sb->ctx->linux_uid;
1574 inode->i_gid = cifs_sb->ctx->linux_gid;
1575 spin_unlock(&inode->i_lock);
1578 inode = ERR_PTR(rc);
1584 kfree(fattr.cf_symlink_target);
1589 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1590 const char *full_path, __u32 dosattr)
1592 bool set_time = false;
1593 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1594 struct TCP_Server_Info *server;
1595 FILE_BASIC_INFO info_buf;
1600 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1601 if (!server->ops->set_file_info)
1606 if (attrs->ia_valid & ATTR_ATIME) {
1608 info_buf.LastAccessTime =
1609 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1611 info_buf.LastAccessTime = 0;
1613 if (attrs->ia_valid & ATTR_MTIME) {
1615 info_buf.LastWriteTime =
1616 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1618 info_buf.LastWriteTime = 0;
1621 * Samba throws this field away, but windows may actually use it.
1622 * Do not set ctime unless other time stamps are changed explicitly
1623 * (i.e. by utimes()) since we would then have a mix of client and
1626 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1627 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1628 info_buf.ChangeTime =
1629 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1631 info_buf.ChangeTime = 0;
1633 info_buf.CreationTime = 0; /* don't change */
1634 info_buf.Attributes = cpu_to_le32(dosattr);
1636 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1639 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1641 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1642 * and rename it to a random name that hopefully won't conflict with
1646 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1647 const unsigned int xid)
1651 struct cifs_fid fid;
1652 struct cifs_open_parms oparms;
1653 struct inode *inode = d_inode(dentry);
1654 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1655 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1656 struct tcon_link *tlink;
1657 struct cifs_tcon *tcon;
1658 __u32 dosattr, origattr;
1659 FILE_BASIC_INFO *info_buf = NULL;
1661 tlink = cifs_sb_tlink(cifs_sb);
1663 return PTR_ERR(tlink);
1664 tcon = tlink_tcon(tlink);
1667 * We cannot rename the file if the server doesn't support
1668 * CAP_INFOLEVEL_PASSTHRU
1670 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1675 oparms = (struct cifs_open_parms) {
1678 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1679 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1680 .disposition = FILE_OPEN,
1685 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1689 origattr = cifsInode->cifsAttrs;
1691 origattr |= ATTR_NORMAL;
1693 dosattr = origattr & ~ATTR_READONLY;
1695 dosattr |= ATTR_NORMAL;
1696 dosattr |= ATTR_HIDDEN;
1698 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1699 if (dosattr != origattr) {
1700 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1701 if (info_buf == NULL) {
1705 info_buf->Attributes = cpu_to_le32(dosattr);
1706 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1708 /* although we would like to mark the file hidden
1709 if that fails we will still try to rename it */
1711 cifsInode->cifsAttrs = dosattr;
1713 dosattr = origattr; /* since not able to change them */
1716 /* rename the file */
1717 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1719 cifs_remap(cifs_sb));
1725 /* try to set DELETE_ON_CLOSE */
1726 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1727 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1730 * some samba versions return -ENOENT when we try to set the
1731 * file disposition here. Likely a samba bug, but work around
1732 * it for now. This means that some cifsXXX files may hang
1733 * around after they shouldn't.
1735 * BB: remove this hack after more servers have the fix
1743 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1747 CIFSSMBClose(xid, tcon, fid.netfid);
1750 cifs_put_tlink(tlink);
1754 * reset everything back to the original state. Don't bother
1755 * dealing with errors here since we can't do anything about
1759 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1760 cifs_sb->local_nls, cifs_remap(cifs_sb));
1762 if (dosattr != origattr) {
1763 info_buf->Attributes = cpu_to_le32(origattr);
1764 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1766 cifsInode->cifsAttrs = origattr;
1771 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1773 /* copied from fs/nfs/dir.c with small changes */
1775 cifs_drop_nlink(struct inode *inode)
1777 spin_lock(&inode->i_lock);
1778 if (inode->i_nlink > 0)
1780 spin_unlock(&inode->i_lock);
1784 * If d_inode(dentry) is null (usually meaning the cached dentry
1785 * is a negative dentry) then we would attempt a standard SMB delete, but
1786 * if that fails we can not attempt the fall back mechanisms on EACCES
1787 * but will return the EACCES to the caller. Note that the VFS does not call
1788 * unlink on negative dentries currently.
1790 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1794 const char *full_path;
1796 struct inode *inode = d_inode(dentry);
1797 struct cifsInodeInfo *cifs_inode;
1798 struct super_block *sb = dir->i_sb;
1799 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1800 struct tcon_link *tlink;
1801 struct cifs_tcon *tcon;
1802 struct TCP_Server_Info *server;
1803 struct iattr *attrs = NULL;
1804 __u32 dosattr = 0, origattr = 0;
1806 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1808 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1811 tlink = cifs_sb_tlink(cifs_sb);
1813 return PTR_ERR(tlink);
1814 tcon = tlink_tcon(tlink);
1815 server = tcon->ses->server;
1818 page = alloc_dentry_path();
1820 if (tcon->nodelete) {
1825 /* Unlink can be called from rename so we can not take the
1826 * sb->s_vfs_rename_mutex here */
1827 full_path = build_path_from_dentry(dentry, page);
1828 if (IS_ERR(full_path)) {
1829 rc = PTR_ERR(full_path);
1833 cifs_close_deferred_file_under_dentry(tcon, full_path);
1834 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1835 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1836 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1837 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1838 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1839 cifs_remap(cifs_sb));
1840 cifs_dbg(FYI, "posix del rc %d\n", rc);
1841 if ((rc == 0) || (rc == -ENOENT))
1842 goto psx_del_no_retry;
1844 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1847 if (!server->ops->unlink) {
1849 goto psx_del_no_retry;
1852 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1857 cifs_drop_nlink(inode);
1858 } else if (rc == -ENOENT) {
1860 } else if (rc == -EBUSY) {
1861 if (server->ops->rename_pending_delete) {
1862 rc = server->ops->rename_pending_delete(full_path,
1865 cifs_drop_nlink(inode);
1867 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1868 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1869 if (attrs == NULL) {
1874 /* try to reset dos attributes */
1875 cifs_inode = CIFS_I(inode);
1876 origattr = cifs_inode->cifsAttrs;
1878 origattr |= ATTR_NORMAL;
1879 dosattr = origattr & ~ATTR_READONLY;
1881 dosattr |= ATTR_NORMAL;
1882 dosattr |= ATTR_HIDDEN;
1884 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1888 goto retry_std_delete;
1891 /* undo the setattr if we errored out and it's needed */
1892 if (rc != 0 && dosattr != 0)
1893 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1897 cifs_inode = CIFS_I(inode);
1898 cifs_inode->time = 0; /* will force revalidate to get info
1900 inode_set_ctime_current(inode);
1902 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1903 cifs_inode = CIFS_I(dir);
1904 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1906 free_dentry_path(page);
1909 cifs_put_tlink(tlink);
1914 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1915 const char *full_path, struct cifs_sb_info *cifs_sb,
1916 struct cifs_tcon *tcon, const unsigned int xid)
1919 struct inode *inode = NULL;
1921 if (tcon->posix_extensions) {
1922 rc = smb311_posix_get_inode_info(&inode, full_path,
1923 NULL, parent->i_sb, xid);
1924 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1925 } else if (tcon->unix_ext) {
1926 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1928 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1930 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1937 if (!S_ISDIR(inode->i_mode)) {
1939 * mkdir succeeded, but another client has managed to remove the
1940 * sucker and replace it with non-directory. Return success,
1941 * but don't leave the child in dcache.
1948 * setting nlink not necessary except in cases where we failed to get it
1949 * from the server or was set bogus. Also, since this is a brand new
1950 * inode, no need to grab the i_lock before setting the i_nlink.
1952 if (inode->i_nlink < 2)
1953 set_nlink(inode, 2);
1954 mode &= ~current_umask();
1955 /* must turn on setgid bit if parent dir has it */
1956 if (parent->i_mode & S_ISGID)
1959 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1960 if (tcon->unix_ext) {
1961 struct cifs_unix_set_info_args args = {
1963 .ctime = NO_CHANGE_64,
1964 .atime = NO_CHANGE_64,
1965 .mtime = NO_CHANGE_64,
1968 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1969 args.uid = current_fsuid();
1970 if (parent->i_mode & S_ISGID)
1971 args.gid = parent->i_gid;
1973 args.gid = current_fsgid();
1975 args.uid = INVALID_UID; /* no change */
1976 args.gid = INVALID_GID; /* no change */
1978 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1980 cifs_remap(cifs_sb));
1984 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1985 struct TCP_Server_Info *server = tcon->ses->server;
1986 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1987 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1988 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1990 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1991 inode->i_mode = (mode | S_IFDIR);
1993 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1994 inode->i_uid = current_fsuid();
1995 if (inode->i_mode & S_ISGID)
1996 inode->i_gid = parent->i_gid;
1998 inode->i_gid = current_fsgid();
2001 d_instantiate(dentry, inode);
2005 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2007 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
2008 const char *full_path, struct cifs_sb_info *cifs_sb,
2009 struct cifs_tcon *tcon, const unsigned int xid)
2013 FILE_UNIX_BASIC_INFO *info = NULL;
2014 struct inode *newinode = NULL;
2015 struct cifs_fattr fattr;
2017 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
2020 goto posix_mkdir_out;
2023 mode &= ~current_umask();
2024 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
2025 NULL /* netfid */, info, &oplock, full_path,
2026 cifs_sb->local_nls, cifs_remap(cifs_sb));
2027 if (rc == -EOPNOTSUPP)
2028 goto posix_mkdir_out;
2030 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
2032 goto posix_mkdir_out;
2035 if (info->Type == cpu_to_le32(-1))
2036 /* no return info, go query for it */
2037 goto posix_mkdir_get_info;
2039 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2040 * need to set uid/gid.
2043 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2044 cifs_fill_uniqueid(inode->i_sb, &fattr);
2045 newinode = cifs_iget(inode->i_sb, &fattr);
2047 goto posix_mkdir_get_info;
2049 d_instantiate(dentry, newinode);
2051 #ifdef CONFIG_CIFS_DEBUG2
2052 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2053 dentry, dentry, newinode);
2055 if (newinode->i_nlink != 2)
2056 cifs_dbg(FYI, "unexpected number of links %d\n",
2063 posix_mkdir_get_info:
2064 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2066 goto posix_mkdir_out;
2068 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2070 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2071 struct dentry *direntry, umode_t mode)
2075 struct cifs_sb_info *cifs_sb;
2076 struct tcon_link *tlink;
2077 struct cifs_tcon *tcon;
2078 struct TCP_Server_Info *server;
2079 const char *full_path;
2082 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2085 cifs_sb = CIFS_SB(inode->i_sb);
2086 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2088 tlink = cifs_sb_tlink(cifs_sb);
2090 return PTR_ERR(tlink);
2091 tcon = tlink_tcon(tlink);
2095 page = alloc_dentry_path();
2096 full_path = build_path_from_dentry(direntry, page);
2097 if (IS_ERR(full_path)) {
2098 rc = PTR_ERR(full_path);
2102 server = tcon->ses->server;
2104 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2105 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2107 d_drop(direntry); /* for time being always refresh inode info */
2111 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2112 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2113 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2114 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2116 if (rc != -EOPNOTSUPP)
2119 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2121 if (!server->ops->mkdir) {
2126 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2127 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2129 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2134 /* TODO: skip this for smb2/smb3 */
2135 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2139 * Force revalidate to get parent dir info when needed since cached
2140 * attributes are invalid now.
2142 CIFS_I(inode)->time = 0;
2143 free_dentry_path(page);
2145 cifs_put_tlink(tlink);
2149 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2153 struct cifs_sb_info *cifs_sb;
2154 struct tcon_link *tlink;
2155 struct cifs_tcon *tcon;
2156 struct TCP_Server_Info *server;
2157 const char *full_path;
2158 void *page = alloc_dentry_path();
2159 struct cifsInodeInfo *cifsInode;
2161 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2165 full_path = build_path_from_dentry(direntry, page);
2166 if (IS_ERR(full_path)) {
2167 rc = PTR_ERR(full_path);
2171 cifs_sb = CIFS_SB(inode->i_sb);
2172 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2177 tlink = cifs_sb_tlink(cifs_sb);
2178 if (IS_ERR(tlink)) {
2179 rc = PTR_ERR(tlink);
2182 tcon = tlink_tcon(tlink);
2183 server = tcon->ses->server;
2185 if (!server->ops->rmdir) {
2187 cifs_put_tlink(tlink);
2191 if (tcon->nodelete) {
2193 cifs_put_tlink(tlink);
2197 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2198 cifs_put_tlink(tlink);
2201 spin_lock(&d_inode(direntry)->i_lock);
2202 i_size_write(d_inode(direntry), 0);
2203 clear_nlink(d_inode(direntry));
2204 spin_unlock(&d_inode(direntry)->i_lock);
2207 cifsInode = CIFS_I(d_inode(direntry));
2208 /* force revalidate to go get info when needed */
2209 cifsInode->time = 0;
2211 cifsInode = CIFS_I(inode);
2213 * Force revalidate to get parent dir info when needed since cached
2214 * attributes are invalid now.
2216 cifsInode->time = 0;
2218 inode_set_ctime_current(d_inode(direntry));
2219 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2222 free_dentry_path(page);
2228 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2229 const char *from_path, struct dentry *to_dentry,
2230 const char *to_path)
2232 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2233 struct tcon_link *tlink;
2234 struct cifs_tcon *tcon;
2235 struct TCP_Server_Info *server;
2236 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2237 struct cifs_fid fid;
2238 struct cifs_open_parms oparms;
2240 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2243 tlink = cifs_sb_tlink(cifs_sb);
2245 return PTR_ERR(tlink);
2246 tcon = tlink_tcon(tlink);
2247 server = tcon->ses->server;
2249 if (!server->ops->rename)
2252 /* try path-based rename first */
2253 rc = server->ops->rename(xid, tcon, from_dentry,
2254 from_path, to_path, cifs_sb);
2257 * Don't bother with rename by filehandle unless file is busy and
2258 * source. Note that cross directory moves do not work with
2259 * rename by filehandle to various Windows servers.
2261 if (rc == 0 || rc != -EBUSY)
2262 goto do_rename_exit;
2264 /* Don't fall back to using SMB on SMB 2+ mount */
2265 if (server->vals->protocol_id != 0)
2266 goto do_rename_exit;
2268 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2269 /* open-file renames don't work across directories */
2270 if (to_dentry->d_parent != from_dentry->d_parent)
2271 goto do_rename_exit;
2273 oparms = (struct cifs_open_parms) {
2276 /* open the file to be renamed -- we need DELETE perms */
2277 .desired_access = DELETE,
2278 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2279 .disposition = FILE_OPEN,
2284 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2286 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2287 (const char *) to_dentry->d_name.name,
2288 cifs_sb->local_nls, cifs_remap(cifs_sb));
2289 CIFSSMBClose(xid, tcon, fid.netfid);
2291 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2294 d_move(from_dentry, to_dentry);
2295 cifs_put_tlink(tlink);
2300 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2301 struct dentry *source_dentry, struct inode *target_dir,
2302 struct dentry *target_dentry, unsigned int flags)
2304 const char *from_name, *to_name;
2305 void *page1, *page2;
2306 struct cifs_sb_info *cifs_sb;
2307 struct tcon_link *tlink;
2308 struct cifs_tcon *tcon;
2311 int retry_count = 0;
2312 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2313 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2314 FILE_UNIX_BASIC_INFO *info_buf_target;
2315 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2317 if (flags & ~RENAME_NOREPLACE)
2320 cifs_sb = CIFS_SB(source_dir->i_sb);
2321 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2324 tlink = cifs_sb_tlink(cifs_sb);
2326 return PTR_ERR(tlink);
2327 tcon = tlink_tcon(tlink);
2329 page1 = alloc_dentry_path();
2330 page2 = alloc_dentry_path();
2333 from_name = build_path_from_dentry(source_dentry, page1);
2334 if (IS_ERR(from_name)) {
2335 rc = PTR_ERR(from_name);
2336 goto cifs_rename_exit;
2339 to_name = build_path_from_dentry(target_dentry, page2);
2340 if (IS_ERR(to_name)) {
2341 rc = PTR_ERR(to_name);
2342 goto cifs_rename_exit;
2345 cifs_close_deferred_file_under_dentry(tcon, from_name);
2346 if (d_inode(target_dentry) != NULL)
2347 cifs_close_deferred_file_under_dentry(tcon, to_name);
2349 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2352 if (rc == -EACCES) {
2353 while (retry_count < 3) {
2354 cifs_close_all_deferred_files(tcon);
2355 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2364 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2366 if (flags & RENAME_NOREPLACE)
2367 goto cifs_rename_exit;
2369 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2370 if (rc == -EEXIST && tcon->unix_ext) {
2372 * Are src and dst hardlinks of same inode? We can only tell
2373 * with unix extensions enabled.
2376 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2378 if (info_buf_source == NULL) {
2380 goto cifs_rename_exit;
2383 info_buf_target = info_buf_source + 1;
2384 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2387 cifs_remap(cifs_sb));
2391 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2394 cifs_remap(cifs_sb));
2396 if (tmprc == 0 && (info_buf_source->UniqueId ==
2397 info_buf_target->UniqueId)) {
2398 /* same file, POSIX says that this is a noop */
2400 goto cifs_rename_exit;
2404 * else ... BB we could add the same check for Windows by
2405 * checking the UniqueId via FILE_INTERNAL_INFO
2409 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2411 /* Try unlinking the target dentry if it's not negative */
2412 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2413 if (d_is_dir(target_dentry))
2414 tmprc = cifs_rmdir(target_dir, target_dentry);
2416 tmprc = cifs_unlink(target_dir, target_dentry);
2418 goto cifs_rename_exit;
2419 rc = cifs_do_rename(xid, source_dentry, from_name,
2420 target_dentry, to_name);
2423 /* force revalidate to go get info when needed */
2424 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2427 kfree(info_buf_source);
2428 free_dentry_path(page2);
2429 free_dentry_path(page1);
2431 cifs_put_tlink(tlink);
2436 cifs_dentry_needs_reval(struct dentry *dentry)
2438 struct inode *inode = d_inode(dentry);
2439 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2440 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2441 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2442 struct cached_fid *cfid = NULL;
2444 if (cifs_i->time == 0)
2447 if (CIFS_CACHE_READ(cifs_i))
2450 if (!lookupCacheEnabled)
2453 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2454 spin_lock(&cfid->fid_lock);
2455 if (cfid->time && cifs_i->time > cfid->time) {
2456 spin_unlock(&cfid->fid_lock);
2457 close_cached_dir(cfid);
2460 spin_unlock(&cfid->fid_lock);
2461 close_cached_dir(cfid);
2464 * depending on inode type, check if attribute caching disabled for
2465 * files or directories
2467 if (S_ISDIR(inode->i_mode)) {
2468 if (!cifs_sb->ctx->acdirmax)
2470 if (!time_in_range(jiffies, cifs_i->time,
2471 cifs_i->time + cifs_sb->ctx->acdirmax))
2474 if (!cifs_sb->ctx->acregmax)
2476 if (!time_in_range(jiffies, cifs_i->time,
2477 cifs_i->time + cifs_sb->ctx->acregmax))
2481 /* hardlinked files w/ noserverino get "special" treatment */
2482 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2483 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2490 * Zap the cache. Called when invalid_mapping flag is set.
2493 cifs_invalidate_mapping(struct inode *inode)
2497 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2498 rc = invalidate_inode_pages2(inode->i_mapping);
2500 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2501 __func__, inode, rc);
2508 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2510 * @key: currently unused
2511 * @mode: the task state to sleep in
2514 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2517 if (signal_pending_state(mode, current))
2518 return -ERESTARTSYS;
2523 cifs_revalidate_mapping(struct inode *inode)
2526 unsigned long *flags = &CIFS_I(inode)->flags;
2527 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2529 /* swapfiles are not supposed to be shared */
2530 if (IS_SWAPFILE(inode))
2533 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2534 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2538 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2539 /* for cache=singleclient, do not invalidate */
2540 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2541 goto skip_invalidate;
2543 rc = cifs_invalidate_mapping(inode);
2545 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2549 clear_bit_unlock(CIFS_INO_LOCK, flags);
2550 smp_mb__after_atomic();
2551 wake_up_bit(flags, CIFS_INO_LOCK);
2557 cifs_zap_mapping(struct inode *inode)
2559 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2560 return cifs_revalidate_mapping(inode);
2563 int cifs_revalidate_file_attr(struct file *filp)
2566 struct dentry *dentry = file_dentry(filp);
2567 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2568 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2569 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2571 if (!cifs_dentry_needs_reval(dentry))
2574 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2575 if (tlink_tcon(cfile->tlink)->unix_ext)
2576 rc = cifs_get_file_info_unix(filp);
2578 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2579 rc = cifs_get_file_info(filp);
2584 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2588 struct inode *inode = d_inode(dentry);
2589 struct super_block *sb = dentry->d_sb;
2590 const char *full_path;
2597 if (!cifs_dentry_needs_reval(dentry))
2602 page = alloc_dentry_path();
2603 full_path = build_path_from_dentry(dentry, page);
2604 if (IS_ERR(full_path)) {
2605 rc = PTR_ERR(full_path);
2609 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2610 full_path, inode, inode->i_count.counter,
2611 dentry, cifs_get_time(dentry), jiffies);
2614 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2615 rc = smb311_posix_get_inode_info(&inode, full_path,
2617 } else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2618 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2620 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2623 if (rc == -EAGAIN && count++ < 10)
2626 free_dentry_path(page);
2632 int cifs_revalidate_file(struct file *filp)
2635 struct inode *inode = file_inode(filp);
2637 rc = cifs_revalidate_file_attr(filp);
2641 return cifs_revalidate_mapping(inode);
2644 /* revalidate a dentry's inode attributes */
2645 int cifs_revalidate_dentry(struct dentry *dentry)
2648 struct inode *inode = d_inode(dentry);
2650 rc = cifs_revalidate_dentry_attr(dentry);
2654 return cifs_revalidate_mapping(inode);
2657 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2658 struct kstat *stat, u32 request_mask, unsigned int flags)
2660 struct dentry *dentry = path->dentry;
2661 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2662 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2663 struct inode *inode = d_inode(dentry);
2666 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2670 * We need to be sure that all dirty pages are written and the server
2671 * has actual ctime, mtime and file length.
2673 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2674 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2675 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2676 rc = filemap_fdatawait(inode->i_mapping);
2678 mapping_set_error(inode->i_mapping, rc);
2683 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2684 CIFS_I(inode)->time = 0; /* force revalidate */
2687 * If the caller doesn't require syncing, only sync if
2688 * necessary (e.g. due to earlier truncate or setattr
2689 * invalidating the cached metadata)
2691 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2692 (CIFS_I(inode)->time == 0)) {
2693 rc = cifs_revalidate_dentry_attr(dentry);
2698 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2699 stat->blksize = cifs_sb->ctx->bsize;
2700 stat->ino = CIFS_I(inode)->uniqueid;
2702 /* old CIFS Unix Extensions doesn't return create time */
2703 if (CIFS_I(inode)->createtime) {
2704 stat->result_mask |= STATX_BTIME;
2706 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2709 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2710 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2711 stat->attributes |= STATX_ATTR_COMPRESSED;
2712 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2713 stat->attributes |= STATX_ATTR_ENCRYPTED;
2716 * If on a multiuser mount without unix extensions or cifsacl being
2717 * enabled, and the admin hasn't overridden them, set the ownership
2718 * to the fsuid/fsgid of the current process.
2720 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2721 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2723 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2724 stat->uid = current_fsuid();
2725 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2726 stat->gid = current_fsgid();
2731 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2734 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2735 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2736 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2737 struct TCP_Server_Info *server = tcon->ses->server;
2738 struct cifsFileInfo *cfile;
2741 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2745 * We need to be sure that all dirty pages are written as they
2746 * might fill holes on the server.
2748 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2749 inode->i_mapping->nrpages != 0) {
2750 rc = filemap_fdatawait(inode->i_mapping);
2752 mapping_set_error(inode->i_mapping, rc);
2757 cfile = find_readable_file(cifs_i, false);
2761 if (server->ops->fiemap) {
2762 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2763 cifsFileInfo_put(cfile);
2767 cifsFileInfo_put(cfile);
2771 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2773 pgoff_t index = from >> PAGE_SHIFT;
2774 unsigned offset = from & (PAGE_SIZE - 1);
2778 page = grab_cache_page(mapping, index);
2782 zero_user_segment(page, offset, PAGE_SIZE);
2788 void cifs_setsize(struct inode *inode, loff_t offset)
2790 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2792 spin_lock(&inode->i_lock);
2793 i_size_write(inode, offset);
2794 spin_unlock(&inode->i_lock);
2796 /* Cached inode must be refreshed on truncate */
2798 truncate_pagecache(inode, offset);
2802 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2803 unsigned int xid, const char *full_path)
2806 struct cifsFileInfo *open_file;
2807 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2808 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2809 struct tcon_link *tlink = NULL;
2810 struct cifs_tcon *tcon = NULL;
2811 struct TCP_Server_Info *server;
2814 * To avoid spurious oplock breaks from server, in the case of
2815 * inodes that we already have open, avoid doing path based
2816 * setting of file size if we can do it by handle.
2817 * This keeps our caching token (oplock) and avoids timeouts
2818 * when the local oplock break takes longer to flush
2819 * writebehind data than the SMB timeout for the SetPathInfo
2820 * request would allow
2822 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2824 tcon = tlink_tcon(open_file->tlink);
2825 server = tcon->ses->server;
2826 if (server->ops->set_file_size)
2827 rc = server->ops->set_file_size(xid, tcon, open_file,
2828 attrs->ia_size, false);
2831 cifsFileInfo_put(open_file);
2832 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2840 tlink = cifs_sb_tlink(cifs_sb);
2842 return PTR_ERR(tlink);
2843 tcon = tlink_tcon(tlink);
2844 server = tcon->ses->server;
2848 * Set file size by pathname rather than by handle either because no
2849 * valid, writeable file handle for it was found or because there was
2850 * an error setting it by handle.
2852 if (server->ops->set_path_size)
2853 rc = server->ops->set_path_size(xid, tcon, full_path,
2854 attrs->ia_size, cifs_sb, false);
2857 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2860 cifs_put_tlink(tlink);
2864 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2865 cifs_setsize(inode, attrs->ia_size);
2867 * i_blocks is not related to (i_size / i_blksize), but instead
2868 * 512 byte (2**9) size is required for calculating num blocks.
2869 * Until we can query the server for actual allocation size,
2870 * this is best estimate we have for blocks allocated for a file
2871 * Number of blocks must be rounded up so size 1 is not 0 blocks
2873 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2876 * The man page of truncate says if the size changed,
2877 * then the st_ctime and st_mtime fields for the file
2880 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2881 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2883 cifs_truncate_page(inode->i_mapping, inode->i_size);
2889 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2891 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2895 const char *full_path;
2896 void *page = alloc_dentry_path();
2897 struct inode *inode = d_inode(direntry);
2898 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2899 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2900 struct tcon_link *tlink;
2901 struct cifs_tcon *pTcon;
2902 struct cifs_unix_set_info_args *args = NULL;
2903 struct cifsFileInfo *open_file;
2905 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2906 direntry, attrs->ia_valid);
2910 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2911 attrs->ia_valid |= ATTR_FORCE;
2913 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2917 full_path = build_path_from_dentry(direntry, page);
2918 if (IS_ERR(full_path)) {
2919 rc = PTR_ERR(full_path);
2924 * Attempt to flush data before changing attributes. We need to do
2925 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2926 * ownership or mode then we may also need to do this. Here, we take
2927 * the safe way out and just do the flush on all setattr requests. If
2928 * the flush returns error, store it to report later and continue.
2930 * BB: This should be smarter. Why bother flushing pages that
2931 * will be truncated anyway? Also, should we error out here if
2932 * the flush returns error?
2934 rc = filemap_write_and_wait(inode->i_mapping);
2935 if (is_interrupt_error(rc)) {
2940 mapping_set_error(inode->i_mapping, rc);
2943 if (attrs->ia_valid & ATTR_SIZE) {
2944 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2949 /* skip mode change if it's just for clearing setuid/setgid */
2950 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2951 attrs->ia_valid &= ~ATTR_MODE;
2953 args = kmalloc(sizeof(*args), GFP_KERNEL);
2959 /* set up the struct */
2960 if (attrs->ia_valid & ATTR_MODE)
2961 args->mode = attrs->ia_mode;
2963 args->mode = NO_CHANGE_64;
2965 if (attrs->ia_valid & ATTR_UID)
2966 args->uid = attrs->ia_uid;
2968 args->uid = INVALID_UID; /* no change */
2970 if (attrs->ia_valid & ATTR_GID)
2971 args->gid = attrs->ia_gid;
2973 args->gid = INVALID_GID; /* no change */
2975 if (attrs->ia_valid & ATTR_ATIME)
2976 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2978 args->atime = NO_CHANGE_64;
2980 if (attrs->ia_valid & ATTR_MTIME)
2981 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2983 args->mtime = NO_CHANGE_64;
2985 if (attrs->ia_valid & ATTR_CTIME)
2986 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2988 args->ctime = NO_CHANGE_64;
2991 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2993 u16 nfid = open_file->fid.netfid;
2994 u32 npid = open_file->pid;
2995 pTcon = tlink_tcon(open_file->tlink);
2996 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2997 cifsFileInfo_put(open_file);
2999 tlink = cifs_sb_tlink(cifs_sb);
3000 if (IS_ERR(tlink)) {
3001 rc = PTR_ERR(tlink);
3004 pTcon = tlink_tcon(tlink);
3005 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
3007 cifs_remap(cifs_sb));
3008 cifs_put_tlink(tlink);
3014 if ((attrs->ia_valid & ATTR_SIZE) &&
3015 attrs->ia_size != i_size_read(inode)) {
3016 truncate_setsize(inode, attrs->ia_size);
3017 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3018 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3021 setattr_copy(&nop_mnt_idmap, inode, attrs);
3022 mark_inode_dirty(inode);
3024 /* force revalidate when any of these times are set since some
3025 of the fs types (eg ext3, fat) do not have fine enough
3026 time granularity to match protocol, and we do not have a
3027 a way (yet) to query the server fs's time granularity (and
3028 whether it rounds times down).
3030 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
3031 cifsInode->time = 0;
3034 free_dentry_path(page);
3038 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3041 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3044 kuid_t uid = INVALID_UID;
3045 kgid_t gid = INVALID_GID;
3046 struct inode *inode = d_inode(direntry);
3047 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3048 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3049 struct cifsFileInfo *wfile;
3050 struct cifs_tcon *tcon;
3051 const char *full_path;
3052 void *page = alloc_dentry_path();
3055 __u64 mode = NO_CHANGE_64;
3059 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3060 direntry, attrs->ia_valid);
3062 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3063 attrs->ia_valid |= ATTR_FORCE;
3065 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3067 goto cifs_setattr_exit;
3069 full_path = build_path_from_dentry(direntry, page);
3070 if (IS_ERR(full_path)) {
3071 rc = PTR_ERR(full_path);
3072 goto cifs_setattr_exit;
3076 * Attempt to flush data before changing attributes. We need to do
3077 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3078 * returns error, store it to report later and continue.
3080 * BB: This should be smarter. Why bother flushing pages that
3081 * will be truncated anyway? Also, should we error out here if
3082 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3084 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3085 rc = filemap_write_and_wait(inode->i_mapping);
3086 if (is_interrupt_error(rc)) {
3088 goto cifs_setattr_exit;
3090 mapping_set_error(inode->i_mapping, rc);
3095 if ((attrs->ia_valid & ATTR_MTIME) &&
3096 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3097 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3099 tcon = tlink_tcon(wfile->tlink);
3100 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3101 cifsFileInfo_put(wfile);
3103 goto cifs_setattr_exit;
3104 } else if (rc != -EBADF)
3105 goto cifs_setattr_exit;
3110 if (attrs->ia_valid & ATTR_SIZE) {
3111 rc = cifs_set_file_size(inode, attrs, xid, full_path);
3113 goto cifs_setattr_exit;
3116 if (attrs->ia_valid & ATTR_UID)
3117 uid = attrs->ia_uid;
3119 if (attrs->ia_valid & ATTR_GID)
3120 gid = attrs->ia_gid;
3122 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3123 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3124 if (uid_valid(uid) || gid_valid(gid)) {
3125 mode = NO_CHANGE_64;
3126 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3129 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3131 goto cifs_setattr_exit;
3135 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3136 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3138 /* skip mode change if it's just for clearing setuid/setgid */
3139 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3140 attrs->ia_valid &= ~ATTR_MODE;
3142 if (attrs->ia_valid & ATTR_MODE) {
3143 mode = attrs->ia_mode;
3145 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3146 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3147 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3148 INVALID_UID, INVALID_GID);
3150 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3152 goto cifs_setattr_exit;
3156 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3157 * Pick up the actual mode bits that were set.
3159 if (mode != attrs->ia_mode)
3160 attrs->ia_mode = mode;
3162 if (((mode & S_IWUGO) == 0) &&
3163 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3165 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3167 /* fix up mode if we're not using dynperm */
3168 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3169 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3170 } else if ((mode & S_IWUGO) &&
3171 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3173 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3174 /* Attributes of 0 are ignored */
3176 dosattr |= ATTR_NORMAL;
3178 /* reset local inode permissions to normal */
3179 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3180 attrs->ia_mode &= ~(S_IALLUGO);
3181 if (S_ISDIR(inode->i_mode))
3183 cifs_sb->ctx->dir_mode;
3186 cifs_sb->ctx->file_mode;
3188 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3189 /* ignore mode change - ATTR_READONLY hasn't changed */
3190 attrs->ia_valid &= ~ATTR_MODE;
3194 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3195 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3196 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3197 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3199 /* Even if error on time set, no sense failing the call if
3200 the server would set the time to a reasonable value anyway,
3201 and this check ensures that we are not being called from
3202 sys_utimes in which case we ought to fail the call back to
3203 the user when the server rejects the call */
3204 if ((rc) && (attrs->ia_valid &
3205 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3209 /* do not need local check to inode_check_ok since the server does
3212 goto cifs_setattr_exit;
3214 if ((attrs->ia_valid & ATTR_SIZE) &&
3215 attrs->ia_size != i_size_read(inode)) {
3216 truncate_setsize(inode, attrs->ia_size);
3217 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3218 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3221 setattr_copy(&nop_mnt_idmap, inode, attrs);
3222 mark_inode_dirty(inode);
3226 free_dentry_path(page);
3231 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3232 struct iattr *attrs)
3234 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3235 int rc, retries = 0;
3236 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3237 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3238 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3240 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3244 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3245 if (pTcon->unix_ext)
3246 rc = cifs_setattr_unix(direntry, attrs);
3248 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3249 rc = cifs_setattr_nounix(direntry, attrs);
3251 } while (is_retryable_error(rc) && retries < 2);
3253 /* BB: add cifs_setattr_legacy for really old servers */