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);
86 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
87 __func__, cifs_i->uniqueid);
89 if (inode->i_state & I_NEW) {
90 cifs_dbg(FYI, "%s: inode %llu is new\n",
91 __func__, cifs_i->uniqueid);
95 /* don't bother with revalidation if we have an oplock */
96 if (CIFS_CACHE_READ(cifs_i)) {
97 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
98 __func__, cifs_i->uniqueid);
102 /* revalidate if mtime or size have changed */
103 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
104 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
105 cifs_i->server_eof == fattr->cf_eof) {
106 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
107 __func__, cifs_i->uniqueid);
111 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
112 __func__, cifs_i->uniqueid);
113 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
114 /* Invalidate fscache cookie */
115 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
116 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
120 * copy nlink to the inode, unless it wasn't provided. Provide
121 * sane values if we don't have an existing one and none was provided
124 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
127 * if we're in a situation where we can't trust what we
128 * got from the server (readdir, some non-unix cases)
129 * fake reasonable values
131 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
132 /* only provide fake values on a new inode */
133 if (inode->i_state & I_NEW) {
134 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
142 /* we trust the server, so update it */
143 set_nlink(inode, fattr->cf_nlink);
146 /* populate an inode with info from a cifs_fattr struct */
148 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
150 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
151 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
153 if (!(inode->i_state & I_NEW) &&
154 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
155 CIFS_I(inode)->time = 0; /* force reval */
159 cifs_revalidate_cache(inode, fattr);
161 spin_lock(&inode->i_lock);
162 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
163 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
164 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
165 /* we do not want atime to be less than mtime, it broke some apps */
166 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
167 inode->i_atime = fattr->cf_mtime;
169 inode->i_atime = fattr->cf_atime;
170 inode->i_mtime = fattr->cf_mtime;
171 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
172 inode->i_rdev = fattr->cf_rdev;
173 cifs_nlink_fattr_to_inode(inode, fattr);
174 inode->i_uid = fattr->cf_uid;
175 inode->i_gid = fattr->cf_gid;
177 /* if dynperm is set, don't clobber existing mode */
178 if (inode->i_state & I_NEW ||
179 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
180 inode->i_mode = fattr->cf_mode;
182 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
184 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
187 cifs_i->time = jiffies;
189 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
190 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
192 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
194 cifs_i->server_eof = fattr->cf_eof;
196 * Can't safely change the file size here if the client is writing to
197 * it due to potential races.
199 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
200 i_size_write(inode, fattr->cf_eof);
203 * i_blocks is not related to (i_size / i_blksize),
204 * but instead 512 byte (2**9) size is required for
205 * calculating num blocks.
207 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
210 if (S_ISLNK(fattr->cf_mode)) {
211 kfree(cifs_i->symlink_target);
212 cifs_i->symlink_target = fattr->cf_symlink_target;
213 fattr->cf_symlink_target = NULL;
215 spin_unlock(&inode->i_lock);
217 if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
218 inode->i_flags |= S_AUTOMOUNT;
219 if (inode->i_state & I_NEW)
225 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
227 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
229 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
232 fattr->cf_uniqueid = iunique(sb, ROOT_I);
235 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
237 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
238 struct cifs_sb_info *cifs_sb)
240 memset(fattr, 0, sizeof(*fattr));
241 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
242 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
243 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
245 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
246 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
247 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
248 /* old POSIX extensions don't get create time */
250 fattr->cf_mode = le64_to_cpu(info->Permissions);
253 * Since we set the inode type below we need to mask off
254 * to avoid strange results if bits set above.
256 fattr->cf_mode &= ~S_IFMT;
257 switch (le32_to_cpu(info->Type)) {
259 fattr->cf_mode |= S_IFREG;
260 fattr->cf_dtype = DT_REG;
263 fattr->cf_mode |= S_IFLNK;
264 fattr->cf_dtype = DT_LNK;
267 fattr->cf_mode |= S_IFDIR;
268 fattr->cf_dtype = DT_DIR;
271 fattr->cf_mode |= S_IFCHR;
272 fattr->cf_dtype = DT_CHR;
273 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
274 le64_to_cpu(info->DevMinor) & MINORMASK);
277 fattr->cf_mode |= S_IFBLK;
278 fattr->cf_dtype = DT_BLK;
279 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
280 le64_to_cpu(info->DevMinor) & MINORMASK);
283 fattr->cf_mode |= S_IFIFO;
284 fattr->cf_dtype = DT_FIFO;
287 fattr->cf_mode |= S_IFSOCK;
288 fattr->cf_dtype = DT_SOCK;
291 /* safest to call it a file if we do not know */
292 fattr->cf_mode |= S_IFREG;
293 fattr->cf_dtype = DT_REG;
294 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
298 fattr->cf_uid = cifs_sb->ctx->linux_uid;
299 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
300 u64 id = le64_to_cpu(info->Uid);
301 if (id < ((uid_t)-1)) {
302 kuid_t uid = make_kuid(&init_user_ns, id);
308 fattr->cf_gid = cifs_sb->ctx->linux_gid;
309 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
310 u64 id = le64_to_cpu(info->Gid);
311 if (id < ((gid_t)-1)) {
312 kgid_t gid = make_kgid(&init_user_ns, id);
318 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
322 * Fill a cifs_fattr struct with fake inode info.
324 * Needed to setup cifs_fattr data for the directory which is the
325 * junction to the new submount (ie to setup the fake directory
326 * which represents a DFS referral or reparse mount point).
328 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
329 struct super_block *sb)
331 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
333 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
335 memset(fattr, 0, sizeof(*fattr));
336 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
337 fattr->cf_uid = cifs_sb->ctx->linux_uid;
338 fattr->cf_gid = cifs_sb->ctx->linux_gid;
339 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
340 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
342 fattr->cf_flags = CIFS_FATTR_JUNCTION;
345 /* Update inode with final fattr data */
346 static int update_inode_info(struct super_block *sb,
347 struct cifs_fattr *fattr,
348 struct inode **inode)
350 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
354 *inode = cifs_iget(sb, fattr);
359 /* We already have inode, update it.
361 * If file type or uniqueid is different, return error.
363 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
364 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
365 CIFS_I(*inode)->time = 0; /* force reval */
368 return cifs_fattr_to_inode(*inode, fattr);
371 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
373 cifs_get_file_info_unix(struct file *filp)
377 FILE_UNIX_BASIC_INFO find_data;
378 struct cifs_fattr fattr = {};
379 struct inode *inode = file_inode(filp);
380 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
381 struct cifsFileInfo *cfile = filp->private_data;
382 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
386 if (cfile->symlink_target) {
387 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
388 if (!fattr.cf_symlink_target) {
390 goto cifs_gfiunix_out;
394 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
396 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
397 } else if (rc == -EREMOTE) {
398 cifs_create_junction_fattr(&fattr, inode->i_sb);
401 goto cifs_gfiunix_out;
403 rc = cifs_fattr_to_inode(inode, &fattr);
410 static int cifs_get_unix_fattr(const unsigned char *full_path,
411 struct super_block *sb,
412 struct cifs_fattr *fattr,
413 struct inode **pinode,
414 const unsigned int xid)
416 struct TCP_Server_Info *server;
417 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
418 FILE_UNIX_BASIC_INFO find_data;
419 struct cifs_tcon *tcon;
420 struct tcon_link *tlink;
423 cifs_dbg(FYI, "Getting info on %s\n", full_path);
425 tlink = cifs_sb_tlink(cifs_sb);
427 return PTR_ERR(tlink);
428 tcon = tlink_tcon(tlink);
429 server = tcon->ses->server;
431 /* could have done a find first instead but this returns more info */
432 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
433 cifs_sb->local_nls, cifs_remap(cifs_sb));
434 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
435 cifs_put_tlink(tlink);
438 cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
439 } else if (rc == -EREMOTE) {
440 cifs_create_junction_fattr(fattr, sb);
447 cifs_fill_uniqueid(sb, fattr);
449 /* check for Minshall+French symlinks */
450 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
451 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
452 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
455 if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
456 if (!server->ops->query_symlink)
458 rc = server->ops->query_symlink(xid, tcon,
460 &fattr->cf_symlink_target,
462 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
467 int cifs_get_inode_info_unix(struct inode **pinode,
468 const unsigned char *full_path,
469 struct super_block *sb, unsigned int xid)
471 struct cifs_fattr fattr = {};
474 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
478 rc = update_inode_info(sb, &fattr, pinode);
480 kfree(fattr.cf_symlink_target);
484 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
485 struct super_block *sb,
486 struct cifs_fattr *fattr,
487 struct inode **pinode,
488 const unsigned int xid)
493 int cifs_get_inode_info_unix(struct inode **pinode,
494 const unsigned char *full_path,
495 struct super_block *sb, unsigned int xid)
499 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
502 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
503 struct cifs_sb_info *cifs_sb, unsigned int xid)
507 struct tcon_link *tlink;
508 struct cifs_tcon *tcon;
510 struct cifs_open_parms oparms;
511 struct cifs_io_parms io_parms = {0};
513 unsigned int bytes_read;
515 int buf_type = CIFS_NO_BUFFER;
519 fattr->cf_mode &= ~S_IFMT;
521 if (fattr->cf_eof == 0) {
522 fattr->cf_mode |= S_IFIFO;
523 fattr->cf_dtype = DT_FIFO;
525 } else if (fattr->cf_eof < 8) {
526 fattr->cf_mode |= S_IFREG;
527 fattr->cf_dtype = DT_REG;
528 return -EINVAL; /* EOPNOTSUPP? */
531 tlink = cifs_sb_tlink(cifs_sb);
533 return PTR_ERR(tlink);
534 tcon = tlink_tcon(tlink);
536 oparms = (struct cifs_open_parms) {
539 .desired_access = GENERIC_READ,
540 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
541 .disposition = FILE_OPEN,
546 if (tcon->ses->server->oplocks)
550 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
552 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
553 cifs_put_tlink(tlink);
558 io_parms.netfid = fid.netfid;
559 io_parms.pid = current->tgid;
560 io_parms.tcon = tcon;
562 io_parms.length = 24;
564 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
565 &bytes_read, &pbuf, &buf_type);
566 if ((rc == 0) && (bytes_read >= 8)) {
567 if (memcmp("IntxBLK", pbuf, 8) == 0) {
568 cifs_dbg(FYI, "Block device\n");
569 fattr->cf_mode |= S_IFBLK;
570 fattr->cf_dtype = DT_BLK;
571 if (bytes_read == 24) {
572 /* we have enough to decode dev num */
573 __u64 mjr; /* major */
574 __u64 mnr; /* minor */
575 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
576 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
577 fattr->cf_rdev = MKDEV(mjr, mnr);
579 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
580 cifs_dbg(FYI, "Char device\n");
581 fattr->cf_mode |= S_IFCHR;
582 fattr->cf_dtype = DT_CHR;
583 if (bytes_read == 24) {
584 /* we have enough to decode dev num */
585 __u64 mjr; /* major */
586 __u64 mnr; /* minor */
587 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
588 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
589 fattr->cf_rdev = MKDEV(mjr, mnr);
591 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
592 cifs_dbg(FYI, "Symlink\n");
593 fattr->cf_mode |= S_IFLNK;
594 fattr->cf_dtype = DT_LNK;
595 } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
596 cifs_dbg(FYI, "FIFO\n");
597 fattr->cf_mode |= S_IFIFO;
598 fattr->cf_dtype = DT_FIFO;
600 fattr->cf_mode |= S_IFREG; /* file? */
601 fattr->cf_dtype = DT_REG;
605 fattr->cf_mode |= S_IFREG; /* then it is a file */
606 fattr->cf_dtype = DT_REG;
607 rc = -EOPNOTSUPP; /* or some unknown SFU type */
610 tcon->ses->server->ops->close(xid, tcon, &fid);
611 cifs_put_tlink(tlink);
615 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
618 * Fetch mode bits as provided by SFU.
620 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
622 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
623 struct cifs_sb_info *cifs_sb, unsigned int xid)
625 #ifdef CONFIG_CIFS_XATTR
629 struct tcon_link *tlink;
630 struct cifs_tcon *tcon;
632 tlink = cifs_sb_tlink(cifs_sb);
634 return PTR_ERR(tlink);
635 tcon = tlink_tcon(tlink);
637 if (tcon->ses->server->ops->query_all_EAs == NULL) {
638 cifs_put_tlink(tlink);
642 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
643 "SETFILEBITS", ea_value, 4 /* size of buf */,
645 cifs_put_tlink(tlink);
649 mode = le32_to_cpu(*((__le32 *)ea_value));
650 fattr->cf_mode &= ~SFBITS_MASK;
651 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
652 mode, fattr->cf_mode);
653 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
654 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
663 /* Fill a cifs_fattr struct with info from POSIX info struct */
664 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
665 struct cifs_open_info_data *data,
666 struct cifs_sid *owner,
667 struct cifs_sid *group,
668 struct super_block *sb)
670 struct smb311_posix_qinfo *info = &data->posix_fi;
671 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
672 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
674 memset(fattr, 0, sizeof(*fattr));
676 /* no fattr->flags to set */
677 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
678 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
680 if (info->LastAccessTime)
681 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
683 ktime_get_coarse_real_ts64(&fattr->cf_atime);
685 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
686 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
688 if (data->adjust_tz) {
689 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
690 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
693 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
694 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
695 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
697 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
698 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
699 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
700 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
703 fattr->cf_mode |= S_IFLNK;
704 fattr->cf_dtype = DT_LNK;
705 fattr->cf_symlink_target = data->symlink_target;
706 data->symlink_target = NULL;
707 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
708 fattr->cf_mode |= S_IFDIR;
709 fattr->cf_dtype = DT_DIR;
711 fattr->cf_mode |= S_IFREG;
712 fattr->cf_dtype = DT_REG;
714 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
716 sid_to_id(cifs_sb, owner, fattr, SIDOWNER);
717 sid_to_id(cifs_sb, group, fattr, SIDGROUP);
719 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
720 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
723 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
724 struct cifs_fattr *fattr,
728 case IO_REPARSE_TAG_LX_SYMLINK:
729 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
730 fattr->cf_dtype = DT_LNK;
732 case IO_REPARSE_TAG_LX_FIFO:
733 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
734 fattr->cf_dtype = DT_FIFO;
736 case IO_REPARSE_TAG_AF_UNIX:
737 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
738 fattr->cf_dtype = DT_SOCK;
740 case IO_REPARSE_TAG_LX_CHR:
741 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
742 fattr->cf_dtype = DT_CHR;
744 case IO_REPARSE_TAG_LX_BLK:
745 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
746 fattr->cf_dtype = DT_BLK;
748 case 0: /* SMB1 symlink */
749 case IO_REPARSE_TAG_SYMLINK:
750 case IO_REPARSE_TAG_NFS:
751 fattr->cf_mode = S_IFLNK | cifs_sb->ctx->file_mode;
752 fattr->cf_dtype = DT_LNK;
760 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
761 struct cifs_open_info_data *data,
762 struct super_block *sb)
764 struct smb2_file_all_info *info = &data->fi;
765 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
766 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
768 memset(fattr, 0, sizeof(*fattr));
769 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
770 if (info->DeletePending)
771 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
773 if (info->LastAccessTime)
774 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
776 ktime_get_coarse_real_ts64(&fattr->cf_atime);
778 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
779 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
781 if (data->adjust_tz) {
782 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
783 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
786 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
787 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
788 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
789 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
791 if (cifs_open_data_reparse(data) &&
792 cifs_reparse_point_to_fattr(cifs_sb, fattr, data->reparse_tag))
795 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
796 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
797 fattr->cf_dtype = DT_DIR;
799 * Server can return wrong NumberOfLinks value for directories
800 * when Unix extensions are disabled - fake it.
803 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
805 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
806 fattr->cf_dtype = DT_REG;
808 /* clear write bits if ATTR_READONLY is set */
809 if (fattr->cf_cifsattrs & ATTR_READONLY)
810 fattr->cf_mode &= ~(S_IWUGO);
813 * Don't accept zero nlink from non-unix servers unless
814 * delete is pending. Instead mark it as unknown.
816 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
817 !info->DeletePending) {
818 cifs_dbg(VFS, "bogus file nlink value %u\n",
820 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
825 if (S_ISLNK(fattr->cf_mode)) {
826 if (likely(data->symlink_target))
827 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
828 fattr->cf_symlink_target = data->symlink_target;
829 data->symlink_target = NULL;
832 fattr->cf_uid = cifs_sb->ctx->linux_uid;
833 fattr->cf_gid = cifs_sb->ctx->linux_gid;
837 cifs_get_file_info(struct file *filp)
841 struct cifs_open_info_data data = {};
842 struct cifs_fattr fattr;
843 struct inode *inode = file_inode(filp);
844 struct cifsFileInfo *cfile = filp->private_data;
845 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
846 struct TCP_Server_Info *server = tcon->ses->server;
848 if (!server->ops->query_file_info)
852 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
855 /* TODO: add support to query reparse tag */
856 data.adjust_tz = false;
857 if (data.symlink_target) {
859 data.reparse_tag = IO_REPARSE_TAG_SYMLINK;
861 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
864 cifs_create_junction_fattr(&fattr, inode->i_sb);
870 * FIXME: legacy server -- fall back to path-based call?
871 * for now, just skip revalidating and mark inode for
875 CIFS_I(inode)->time = 0;
882 * don't bother with SFU junk here -- just mark inode as needing
885 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
886 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
887 /* if filetype is different, return error */
888 rc = cifs_fattr_to_inode(inode, &fattr);
890 cifs_free_open_info(&data);
895 /* Simple function to return a 64 bit hash of string. Rarely called */
896 static __u64 simple_hashstr(const char *str)
898 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
902 hash = (hash + (__u64) *str++) * hash_mult;
907 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
909 * cifs_backup_query_path_info - SMB1 fallback code to get ino
911 * Fallback code to get file metadata when we don't have access to
912 * full_path (EACCES) and have backup creds.
914 * @xid: transaction id used to identify original request in logs
915 * @tcon: information about the server share we have mounted
916 * @sb: the superblock stores info such as disk space available
917 * @full_path: name of the file we are getting the metadata for
918 * @resp_buf: will be set to cifs resp buf and needs to be freed with
919 * cifs_buf_release() when done with @data
920 * @data: will be set to search info result buffer
923 cifs_backup_query_path_info(int xid,
924 struct cifs_tcon *tcon,
925 struct super_block *sb,
926 const char *full_path,
928 FILE_ALL_INFO **data)
930 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
931 struct cifs_search_info info = {0};
936 info.endOfSearch = false;
938 info.info_level = SMB_FIND_FILE_UNIX;
939 else if ((tcon->ses->capabilities &
940 tcon->ses->server->vals->cap_nt_find) == 0)
941 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
942 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
943 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
944 else /* no srvino useful for fallback to some netapp */
945 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
947 flags = CIFS_SEARCH_CLOSE_ALWAYS |
948 CIFS_SEARCH_CLOSE_AT_END |
949 CIFS_SEARCH_BACKUP_SEARCH;
951 rc = CIFSFindFirst(xid, tcon, full_path,
952 cifs_sb, NULL, flags, &info, false);
956 *resp_buf = (void *)info.ntwrk_buf_start;
957 *data = (FILE_ALL_INFO *)info.srch_entries_start;
960 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
962 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
963 struct inode **inode, const char *full_path,
964 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
966 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
967 struct TCP_Server_Info *server = tcon->ses->server;
970 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
972 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
974 fattr->cf_uniqueid = iunique(sb, ROOT_I);
979 * If we have an inode pass a NULL tcon to ensure we don't
980 * make a round trip to the server. This only works for SMB2+.
982 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
983 &fattr->cf_uniqueid, data);
986 * If that fails reuse existing ino or generate one
987 * and disable server ones
990 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
992 fattr->cf_uniqueid = iunique(sb, ROOT_I);
993 cifs_autodisable_serverino(cifs_sb);
998 /* If no errors, check for zero root inode (invalid) */
999 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1000 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1003 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1005 /* make an ino by hashing the UNC */
1006 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1007 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1012 static inline bool is_inode_cache_good(struct inode *ino)
1014 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1017 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1018 struct super_block *sb,
1019 const unsigned int xid,
1020 struct cifs_tcon *tcon,
1021 const char *full_path,
1022 struct cifs_fattr *fattr)
1024 struct TCP_Server_Info *server = tcon->ses->server;
1025 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1026 struct kvec rsp_iov, *iov = NULL;
1027 int rsp_buftype = CIFS_NO_BUFFER;
1028 u32 tag = data->reparse_tag;
1031 if (!tag && server->ops->query_reparse_point) {
1032 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1034 &rsp_iov, &rsp_buftype);
1038 switch ((data->reparse_tag = tag)) {
1039 case 0: /* SMB1 symlink */
1042 case IO_REPARSE_TAG_NFS:
1043 case IO_REPARSE_TAG_SYMLINK:
1044 if (!data->symlink_target && server->ops->query_symlink) {
1045 rc = server->ops->query_symlink(xid, tcon,
1047 &data->symlink_target,
1051 case IO_REPARSE_TAG_MOUNT_POINT:
1052 cifs_create_junction_fattr(fattr, sb);
1056 cifs_open_info_to_fattr(fattr, data, sb);
1058 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1062 static int cifs_get_fattr(struct cifs_open_info_data *data,
1063 struct super_block *sb, int xid,
1064 const struct cifs_fid *fid,
1065 struct cifs_fattr *fattr,
1066 struct inode **inode,
1067 const char *full_path)
1069 struct cifs_open_info_data tmp_data = {};
1070 struct cifs_tcon *tcon;
1071 struct TCP_Server_Info *server;
1072 struct tcon_link *tlink;
1073 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1074 void *smb1_backup_rsp_buf = NULL;
1078 tlink = cifs_sb_tlink(cifs_sb);
1080 return PTR_ERR(tlink);
1081 tcon = tlink_tcon(tlink);
1082 server = tcon->ses->server;
1085 * 1. Fetch file metadata if not provided (data)
1089 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1090 full_path, &tmp_data);
1095 * 2. Convert it to internal cifs metadata (fattr)
1101 * If the file is a reparse point, it is more complicated
1102 * since we have to check if its reparse tag matches a known
1103 * special file type e.g. symlink or fifo or char etc.
1105 if (cifs_open_data_reparse(data)) {
1106 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1109 cifs_open_info_to_fattr(fattr, data, sb);
1113 /* DFS link, no metadata available on this server */
1114 cifs_create_junction_fattr(fattr, sb);
1118 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1120 * perm errors, try again with backup flags if possible
1122 * For SMB2 and later the backup intent flag
1123 * is already sent if needed on open and there
1124 * is no path based FindFirst operation to use
1127 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1128 /* for easier reading */
1130 FILE_DIRECTORY_INFO *fdi;
1131 SEARCH_ID_FULL_DIR_INFO *si;
1133 rc = cifs_backup_query_path_info(xid, tcon, sb,
1135 &smb1_backup_rsp_buf,
1140 move_cifs_info_to_smb2(&data->fi, fi);
1141 fdi = (FILE_DIRECTORY_INFO *)fi;
1142 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1144 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1145 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1146 /* uniqueid set, skip get inum step */
1147 goto handle_mnt_opt;
1149 /* nothing we can do, bail out */
1154 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1157 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1162 * 3. Get or update inode number (fattr->cf_uniqueid)
1165 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1168 * 4. Tweak fattr based on mount options
1170 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1172 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1173 /* query for SFU type info if supported and needed */
1174 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1175 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1176 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1178 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1181 /* fill in 0777 bits from ACL */
1182 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1183 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1184 true, full_path, fid);
1188 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1192 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1193 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1194 false, full_path, fid);
1198 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1204 /* fill in remaining high mode bits e.g. SUID, VTX */
1205 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1206 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1208 /* check for Minshall+French symlinks */
1209 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1210 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1211 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1215 cifs_buf_release(smb1_backup_rsp_buf);
1216 cifs_put_tlink(tlink);
1217 cifs_free_open_info(&tmp_data);
1221 int cifs_get_inode_info(struct inode **inode,
1222 const char *full_path,
1223 struct cifs_open_info_data *data,
1224 struct super_block *sb, int xid,
1225 const struct cifs_fid *fid)
1227 struct cifs_fattr fattr = {};
1230 if (is_inode_cache_good(*inode)) {
1231 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1235 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1239 rc = update_inode_info(sb, &fattr, inode);
1241 kfree(fattr.cf_symlink_target);
1245 static int smb311_posix_get_fattr(struct cifs_fattr *fattr,
1246 const char *full_path,
1247 struct super_block *sb,
1248 const unsigned int xid)
1250 struct cifs_open_info_data data = {};
1251 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1252 struct cifs_tcon *tcon;
1253 struct tcon_link *tlink;
1254 struct cifs_sid owner, group;
1258 tlink = cifs_sb_tlink(cifs_sb);
1260 return PTR_ERR(tlink);
1261 tcon = tlink_tcon(tlink);
1264 * 1. Fetch file metadata
1267 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1272 * 2. Convert it to internal cifs metadata (fattr)
1277 smb311_posix_info_to_fattr(fattr, &data, &owner, &group, sb);
1280 /* DFS link, no metadata available on this server */
1281 cifs_create_junction_fattr(fattr, sb);
1286 * For SMB2 and later the backup intent flag
1287 * is already sent if needed on open and there
1288 * is no path based FindFirst operation to use
1289 * to retry with so nothing we can do, bail out
1293 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1298 * 3. Tweak fattr based on mount options
1300 /* check for Minshall+French symlinks */
1301 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1302 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1303 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1307 cifs_put_tlink(tlink);
1308 cifs_free_open_info(&data);
1312 int smb311_posix_get_inode_info(struct inode **inode, const char *full_path,
1313 struct super_block *sb, const unsigned int xid)
1315 struct cifs_fattr fattr = {};
1318 if (is_inode_cache_good(*inode)) {
1319 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1323 rc = smb311_posix_get_fattr(&fattr, full_path, sb, xid);
1327 rc = update_inode_info(sb, &fattr, inode);
1329 kfree(fattr.cf_symlink_target);
1333 static const struct inode_operations cifs_ipc_inode_ops = {
1334 .lookup = cifs_lookup,
1338 cifs_find_inode(struct inode *inode, void *opaque)
1340 struct cifs_fattr *fattr = opaque;
1342 /* don't match inode with different uniqueid */
1343 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1346 /* use createtime like an i_generation field */
1347 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1350 /* don't match inode of different type */
1351 if (inode_wrong_type(inode, fattr->cf_mode))
1354 /* if it's not a directory or has no dentries, then flag it */
1355 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1356 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1362 cifs_init_inode(struct inode *inode, void *opaque)
1364 struct cifs_fattr *fattr = opaque;
1366 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1367 CIFS_I(inode)->createtime = fattr->cf_createtime;
1372 * walk dentry list for an inode and report whether it has aliases that
1373 * are hashed. We use this to determine if a directory inode can actually
1377 inode_has_hashed_dentries(struct inode *inode)
1379 struct dentry *dentry;
1381 spin_lock(&inode->i_lock);
1382 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1383 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1384 spin_unlock(&inode->i_lock);
1388 spin_unlock(&inode->i_lock);
1392 /* Given fattrs, get a corresponding inode */
1394 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1397 struct inode *inode;
1400 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1402 /* hash down to 32-bits on 32-bit arch */
1403 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1405 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1407 /* was there a potentially problematic inode collision? */
1408 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1409 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1411 if (inode_has_hashed_dentries(inode)) {
1412 cifs_autodisable_serverino(CIFS_SB(sb));
1414 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1415 goto retry_iget5_locked;
1419 /* can't fail - see cifs_find_inode() */
1420 cifs_fattr_to_inode(inode, fattr);
1421 if (sb->s_flags & SB_NOATIME)
1422 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1423 if (inode->i_state & I_NEW) {
1424 inode->i_ino = hash;
1425 cifs_fscache_get_inode_cookie(inode);
1426 unlock_new_inode(inode);
1433 /* gets root inode */
1434 struct inode *cifs_root_iget(struct super_block *sb)
1436 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1437 struct cifs_fattr fattr = {};
1438 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1439 struct inode *inode = NULL;
1445 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1446 && cifs_sb->prepath) {
1447 len = strlen(cifs_sb->prepath);
1448 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1450 return ERR_PTR(-ENOMEM);
1452 memcpy(path+1, cifs_sb->prepath, len);
1454 path = kstrdup("", GFP_KERNEL);
1456 return ERR_PTR(-ENOMEM);
1460 if (tcon->unix_ext) {
1461 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1462 /* some servers mistakenly claim POSIX support */
1463 if (rc != -EOPNOTSUPP)
1465 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1466 tcon->unix_ext = false;
1469 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1470 if (tcon->posix_extensions)
1471 rc = smb311_posix_get_fattr(&fattr, path, sb, xid);
1473 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1477 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1478 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1479 cifs_autodisable_serverino(cifs_sb);
1481 inode = cifs_iget(sb, &fattr);
1485 inode = ERR_PTR(rc);
1489 if (rc && tcon->pipe) {
1490 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1491 spin_lock(&inode->i_lock);
1492 inode->i_mode |= S_IFDIR;
1493 set_nlink(inode, 2);
1494 inode->i_op = &cifs_ipc_inode_ops;
1495 inode->i_fop = &simple_dir_operations;
1496 inode->i_uid = cifs_sb->ctx->linux_uid;
1497 inode->i_gid = cifs_sb->ctx->linux_gid;
1498 spin_unlock(&inode->i_lock);
1501 inode = ERR_PTR(rc);
1507 kfree(fattr.cf_symlink_target);
1512 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1513 const char *full_path, __u32 dosattr)
1515 bool set_time = false;
1516 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1517 struct TCP_Server_Info *server;
1518 FILE_BASIC_INFO info_buf;
1523 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1524 if (!server->ops->set_file_info)
1529 if (attrs->ia_valid & ATTR_ATIME) {
1531 info_buf.LastAccessTime =
1532 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1534 info_buf.LastAccessTime = 0;
1536 if (attrs->ia_valid & ATTR_MTIME) {
1538 info_buf.LastWriteTime =
1539 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1541 info_buf.LastWriteTime = 0;
1544 * Samba throws this field away, but windows may actually use it.
1545 * Do not set ctime unless other time stamps are changed explicitly
1546 * (i.e. by utimes()) since we would then have a mix of client and
1549 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1550 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1551 info_buf.ChangeTime =
1552 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1554 info_buf.ChangeTime = 0;
1556 info_buf.CreationTime = 0; /* don't change */
1557 info_buf.Attributes = cpu_to_le32(dosattr);
1559 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1562 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1564 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1565 * and rename it to a random name that hopefully won't conflict with
1569 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1570 const unsigned int xid)
1574 struct cifs_fid fid;
1575 struct cifs_open_parms oparms;
1576 struct inode *inode = d_inode(dentry);
1577 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1578 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1579 struct tcon_link *tlink;
1580 struct cifs_tcon *tcon;
1581 __u32 dosattr, origattr;
1582 FILE_BASIC_INFO *info_buf = NULL;
1584 tlink = cifs_sb_tlink(cifs_sb);
1586 return PTR_ERR(tlink);
1587 tcon = tlink_tcon(tlink);
1590 * We cannot rename the file if the server doesn't support
1591 * CAP_INFOLEVEL_PASSTHRU
1593 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1598 oparms = (struct cifs_open_parms) {
1601 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1602 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1603 .disposition = FILE_OPEN,
1608 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1612 origattr = cifsInode->cifsAttrs;
1614 origattr |= ATTR_NORMAL;
1616 dosattr = origattr & ~ATTR_READONLY;
1618 dosattr |= ATTR_NORMAL;
1619 dosattr |= ATTR_HIDDEN;
1621 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1622 if (dosattr != origattr) {
1623 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1624 if (info_buf == NULL) {
1628 info_buf->Attributes = cpu_to_le32(dosattr);
1629 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1631 /* although we would like to mark the file hidden
1632 if that fails we will still try to rename it */
1634 cifsInode->cifsAttrs = dosattr;
1636 dosattr = origattr; /* since not able to change them */
1639 /* rename the file */
1640 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1642 cifs_remap(cifs_sb));
1648 /* try to set DELETE_ON_CLOSE */
1649 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1650 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1653 * some samba versions return -ENOENT when we try to set the
1654 * file disposition here. Likely a samba bug, but work around
1655 * it for now. This means that some cifsXXX files may hang
1656 * around after they shouldn't.
1658 * BB: remove this hack after more servers have the fix
1666 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1670 CIFSSMBClose(xid, tcon, fid.netfid);
1673 cifs_put_tlink(tlink);
1677 * reset everything back to the original state. Don't bother
1678 * dealing with errors here since we can't do anything about
1682 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1683 cifs_sb->local_nls, cifs_remap(cifs_sb));
1685 if (dosattr != origattr) {
1686 info_buf->Attributes = cpu_to_le32(origattr);
1687 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1689 cifsInode->cifsAttrs = origattr;
1694 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1696 /* copied from fs/nfs/dir.c with small changes */
1698 cifs_drop_nlink(struct inode *inode)
1700 spin_lock(&inode->i_lock);
1701 if (inode->i_nlink > 0)
1703 spin_unlock(&inode->i_lock);
1707 * If d_inode(dentry) is null (usually meaning the cached dentry
1708 * is a negative dentry) then we would attempt a standard SMB delete, but
1709 * if that fails we can not attempt the fall back mechanisms on EACCES
1710 * but will return the EACCES to the caller. Note that the VFS does not call
1711 * unlink on negative dentries currently.
1713 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1717 const char *full_path;
1719 struct inode *inode = d_inode(dentry);
1720 struct cifsInodeInfo *cifs_inode;
1721 struct super_block *sb = dir->i_sb;
1722 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1723 struct tcon_link *tlink;
1724 struct cifs_tcon *tcon;
1725 struct TCP_Server_Info *server;
1726 struct iattr *attrs = NULL;
1727 __u32 dosattr = 0, origattr = 0;
1729 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1731 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1734 tlink = cifs_sb_tlink(cifs_sb);
1736 return PTR_ERR(tlink);
1737 tcon = tlink_tcon(tlink);
1738 server = tcon->ses->server;
1741 page = alloc_dentry_path();
1743 if (tcon->nodelete) {
1748 /* Unlink can be called from rename so we can not take the
1749 * sb->s_vfs_rename_mutex here */
1750 full_path = build_path_from_dentry(dentry, page);
1751 if (IS_ERR(full_path)) {
1752 rc = PTR_ERR(full_path);
1756 cifs_close_deferred_file_under_dentry(tcon, full_path);
1757 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1758 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1759 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1760 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1761 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1762 cifs_remap(cifs_sb));
1763 cifs_dbg(FYI, "posix del rc %d\n", rc);
1764 if ((rc == 0) || (rc == -ENOENT))
1765 goto psx_del_no_retry;
1767 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1770 if (!server->ops->unlink) {
1772 goto psx_del_no_retry;
1775 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1780 cifs_drop_nlink(inode);
1781 } else if (rc == -ENOENT) {
1783 } else if (rc == -EBUSY) {
1784 if (server->ops->rename_pending_delete) {
1785 rc = server->ops->rename_pending_delete(full_path,
1788 cifs_drop_nlink(inode);
1790 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1791 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1792 if (attrs == NULL) {
1797 /* try to reset dos attributes */
1798 cifs_inode = CIFS_I(inode);
1799 origattr = cifs_inode->cifsAttrs;
1801 origattr |= ATTR_NORMAL;
1802 dosattr = origattr & ~ATTR_READONLY;
1804 dosattr |= ATTR_NORMAL;
1805 dosattr |= ATTR_HIDDEN;
1807 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1811 goto retry_std_delete;
1814 /* undo the setattr if we errored out and it's needed */
1815 if (rc != 0 && dosattr != 0)
1816 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1820 cifs_inode = CIFS_I(inode);
1821 cifs_inode->time = 0; /* will force revalidate to get info
1823 inode_set_ctime_current(inode);
1825 dir->i_mtime = inode_set_ctime_current(dir);
1826 cifs_inode = CIFS_I(dir);
1827 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1829 free_dentry_path(page);
1832 cifs_put_tlink(tlink);
1837 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1838 const char *full_path, struct cifs_sb_info *cifs_sb,
1839 struct cifs_tcon *tcon, const unsigned int xid)
1842 struct inode *inode = NULL;
1844 if (tcon->posix_extensions)
1845 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1846 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1847 else if (tcon->unix_ext)
1848 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1850 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1852 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1858 if (!S_ISDIR(inode->i_mode)) {
1860 * mkdir succeeded, but another client has managed to remove the
1861 * sucker and replace it with non-directory. Return success,
1862 * but don't leave the child in dcache.
1869 * setting nlink not necessary except in cases where we failed to get it
1870 * from the server or was set bogus. Also, since this is a brand new
1871 * inode, no need to grab the i_lock before setting the i_nlink.
1873 if (inode->i_nlink < 2)
1874 set_nlink(inode, 2);
1875 mode &= ~current_umask();
1876 /* must turn on setgid bit if parent dir has it */
1877 if (parent->i_mode & S_ISGID)
1880 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1881 if (tcon->unix_ext) {
1882 struct cifs_unix_set_info_args args = {
1884 .ctime = NO_CHANGE_64,
1885 .atime = NO_CHANGE_64,
1886 .mtime = NO_CHANGE_64,
1889 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1890 args.uid = current_fsuid();
1891 if (parent->i_mode & S_ISGID)
1892 args.gid = parent->i_gid;
1894 args.gid = current_fsgid();
1896 args.uid = INVALID_UID; /* no change */
1897 args.gid = INVALID_GID; /* no change */
1899 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1901 cifs_remap(cifs_sb));
1905 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1906 struct TCP_Server_Info *server = tcon->ses->server;
1907 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1908 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1909 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1911 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1912 inode->i_mode = (mode | S_IFDIR);
1914 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1915 inode->i_uid = current_fsuid();
1916 if (inode->i_mode & S_ISGID)
1917 inode->i_gid = parent->i_gid;
1919 inode->i_gid = current_fsgid();
1922 d_instantiate(dentry, inode);
1926 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1928 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1929 const char *full_path, struct cifs_sb_info *cifs_sb,
1930 struct cifs_tcon *tcon, const unsigned int xid)
1934 FILE_UNIX_BASIC_INFO *info = NULL;
1935 struct inode *newinode = NULL;
1936 struct cifs_fattr fattr;
1938 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1941 goto posix_mkdir_out;
1944 mode &= ~current_umask();
1945 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1946 NULL /* netfid */, info, &oplock, full_path,
1947 cifs_sb->local_nls, cifs_remap(cifs_sb));
1948 if (rc == -EOPNOTSUPP)
1949 goto posix_mkdir_out;
1951 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1953 goto posix_mkdir_out;
1956 if (info->Type == cpu_to_le32(-1))
1957 /* no return info, go query for it */
1958 goto posix_mkdir_get_info;
1960 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1961 * need to set uid/gid.
1964 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1965 cifs_fill_uniqueid(inode->i_sb, &fattr);
1966 newinode = cifs_iget(inode->i_sb, &fattr);
1968 goto posix_mkdir_get_info;
1970 d_instantiate(dentry, newinode);
1972 #ifdef CONFIG_CIFS_DEBUG2
1973 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1974 dentry, dentry, newinode);
1976 if (newinode->i_nlink != 2)
1977 cifs_dbg(FYI, "unexpected number of links %d\n",
1984 posix_mkdir_get_info:
1985 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1987 goto posix_mkdir_out;
1989 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1991 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
1992 struct dentry *direntry, umode_t mode)
1996 struct cifs_sb_info *cifs_sb;
1997 struct tcon_link *tlink;
1998 struct cifs_tcon *tcon;
1999 struct TCP_Server_Info *server;
2000 const char *full_path;
2003 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2006 cifs_sb = CIFS_SB(inode->i_sb);
2007 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2009 tlink = cifs_sb_tlink(cifs_sb);
2011 return PTR_ERR(tlink);
2012 tcon = tlink_tcon(tlink);
2016 page = alloc_dentry_path();
2017 full_path = build_path_from_dentry(direntry, page);
2018 if (IS_ERR(full_path)) {
2019 rc = PTR_ERR(full_path);
2023 server = tcon->ses->server;
2025 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2026 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2028 d_drop(direntry); /* for time being always refresh inode info */
2032 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2033 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2034 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2035 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2037 if (rc != -EOPNOTSUPP)
2040 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2042 if (!server->ops->mkdir) {
2047 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2048 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2050 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2055 /* TODO: skip this for smb2/smb3 */
2056 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2060 * Force revalidate to get parent dir info when needed since cached
2061 * attributes are invalid now.
2063 CIFS_I(inode)->time = 0;
2064 free_dentry_path(page);
2066 cifs_put_tlink(tlink);
2070 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2074 struct cifs_sb_info *cifs_sb;
2075 struct tcon_link *tlink;
2076 struct cifs_tcon *tcon;
2077 struct TCP_Server_Info *server;
2078 const char *full_path;
2079 void *page = alloc_dentry_path();
2080 struct cifsInodeInfo *cifsInode;
2082 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2086 full_path = build_path_from_dentry(direntry, page);
2087 if (IS_ERR(full_path)) {
2088 rc = PTR_ERR(full_path);
2092 cifs_sb = CIFS_SB(inode->i_sb);
2093 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2098 tlink = cifs_sb_tlink(cifs_sb);
2099 if (IS_ERR(tlink)) {
2100 rc = PTR_ERR(tlink);
2103 tcon = tlink_tcon(tlink);
2104 server = tcon->ses->server;
2106 if (!server->ops->rmdir) {
2108 cifs_put_tlink(tlink);
2112 if (tcon->nodelete) {
2114 cifs_put_tlink(tlink);
2118 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2119 cifs_put_tlink(tlink);
2122 spin_lock(&d_inode(direntry)->i_lock);
2123 i_size_write(d_inode(direntry), 0);
2124 clear_nlink(d_inode(direntry));
2125 spin_unlock(&d_inode(direntry)->i_lock);
2128 cifsInode = CIFS_I(d_inode(direntry));
2129 /* force revalidate to go get info when needed */
2130 cifsInode->time = 0;
2132 cifsInode = CIFS_I(inode);
2134 * Force revalidate to get parent dir info when needed since cached
2135 * attributes are invalid now.
2137 cifsInode->time = 0;
2139 inode_set_ctime_current(d_inode(direntry));
2140 inode->i_mtime = inode_set_ctime_current(inode);
2143 free_dentry_path(page);
2149 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2150 const char *from_path, struct dentry *to_dentry,
2151 const char *to_path)
2153 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2154 struct tcon_link *tlink;
2155 struct cifs_tcon *tcon;
2156 struct TCP_Server_Info *server;
2157 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2158 struct cifs_fid fid;
2159 struct cifs_open_parms oparms;
2161 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2164 tlink = cifs_sb_tlink(cifs_sb);
2166 return PTR_ERR(tlink);
2167 tcon = tlink_tcon(tlink);
2168 server = tcon->ses->server;
2170 if (!server->ops->rename)
2173 /* try path-based rename first */
2174 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2177 * Don't bother with rename by filehandle unless file is busy and
2178 * source. Note that cross directory moves do not work with
2179 * rename by filehandle to various Windows servers.
2181 if (rc == 0 || rc != -EBUSY)
2182 goto do_rename_exit;
2184 /* Don't fall back to using SMB on SMB 2+ mount */
2185 if (server->vals->protocol_id != 0)
2186 goto do_rename_exit;
2188 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2189 /* open-file renames don't work across directories */
2190 if (to_dentry->d_parent != from_dentry->d_parent)
2191 goto do_rename_exit;
2193 oparms = (struct cifs_open_parms) {
2196 /* open the file to be renamed -- we need DELETE perms */
2197 .desired_access = DELETE,
2198 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2199 .disposition = FILE_OPEN,
2204 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2206 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2207 (const char *) to_dentry->d_name.name,
2208 cifs_sb->local_nls, cifs_remap(cifs_sb));
2209 CIFSSMBClose(xid, tcon, fid.netfid);
2211 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2214 d_move(from_dentry, to_dentry);
2215 cifs_put_tlink(tlink);
2220 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2221 struct dentry *source_dentry, struct inode *target_dir,
2222 struct dentry *target_dentry, unsigned int flags)
2224 const char *from_name, *to_name;
2225 void *page1, *page2;
2226 struct cifs_sb_info *cifs_sb;
2227 struct tcon_link *tlink;
2228 struct cifs_tcon *tcon;
2231 int retry_count = 0;
2232 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2233 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2234 FILE_UNIX_BASIC_INFO *info_buf_target;
2235 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2237 if (flags & ~RENAME_NOREPLACE)
2240 cifs_sb = CIFS_SB(source_dir->i_sb);
2241 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2244 tlink = cifs_sb_tlink(cifs_sb);
2246 return PTR_ERR(tlink);
2247 tcon = tlink_tcon(tlink);
2249 page1 = alloc_dentry_path();
2250 page2 = alloc_dentry_path();
2253 from_name = build_path_from_dentry(source_dentry, page1);
2254 if (IS_ERR(from_name)) {
2255 rc = PTR_ERR(from_name);
2256 goto cifs_rename_exit;
2259 to_name = build_path_from_dentry(target_dentry, page2);
2260 if (IS_ERR(to_name)) {
2261 rc = PTR_ERR(to_name);
2262 goto cifs_rename_exit;
2265 cifs_close_deferred_file_under_dentry(tcon, from_name);
2266 if (d_inode(target_dentry) != NULL)
2267 cifs_close_deferred_file_under_dentry(tcon, to_name);
2269 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2272 if (rc == -EACCES) {
2273 while (retry_count < 3) {
2274 cifs_close_all_deferred_files(tcon);
2275 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2284 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2286 if (flags & RENAME_NOREPLACE)
2287 goto cifs_rename_exit;
2289 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2290 if (rc == -EEXIST && tcon->unix_ext) {
2292 * Are src and dst hardlinks of same inode? We can only tell
2293 * with unix extensions enabled.
2296 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2298 if (info_buf_source == NULL) {
2300 goto cifs_rename_exit;
2303 info_buf_target = info_buf_source + 1;
2304 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2307 cifs_remap(cifs_sb));
2311 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2314 cifs_remap(cifs_sb));
2316 if (tmprc == 0 && (info_buf_source->UniqueId ==
2317 info_buf_target->UniqueId)) {
2318 /* same file, POSIX says that this is a noop */
2320 goto cifs_rename_exit;
2324 * else ... BB we could add the same check for Windows by
2325 * checking the UniqueId via FILE_INTERNAL_INFO
2329 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2331 /* Try unlinking the target dentry if it's not negative */
2332 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2333 if (d_is_dir(target_dentry))
2334 tmprc = cifs_rmdir(target_dir, target_dentry);
2336 tmprc = cifs_unlink(target_dir, target_dentry);
2338 goto cifs_rename_exit;
2339 rc = cifs_do_rename(xid, source_dentry, from_name,
2340 target_dentry, to_name);
2343 /* force revalidate to go get info when needed */
2344 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2346 source_dir->i_mtime = target_dir->i_mtime = inode_set_ctime_to_ts(source_dir,
2347 inode_set_ctime_current(target_dir));
2350 kfree(info_buf_source);
2351 free_dentry_path(page2);
2352 free_dentry_path(page1);
2354 cifs_put_tlink(tlink);
2359 cifs_dentry_needs_reval(struct dentry *dentry)
2361 struct inode *inode = d_inode(dentry);
2362 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2363 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2364 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2365 struct cached_fid *cfid = NULL;
2367 if (cifs_i->time == 0)
2370 if (CIFS_CACHE_READ(cifs_i))
2373 if (!lookupCacheEnabled)
2376 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2377 spin_lock(&cfid->fid_lock);
2378 if (cfid->time && cifs_i->time > cfid->time) {
2379 spin_unlock(&cfid->fid_lock);
2380 close_cached_dir(cfid);
2383 spin_unlock(&cfid->fid_lock);
2384 close_cached_dir(cfid);
2387 * depending on inode type, check if attribute caching disabled for
2388 * files or directories
2390 if (S_ISDIR(inode->i_mode)) {
2391 if (!cifs_sb->ctx->acdirmax)
2393 if (!time_in_range(jiffies, cifs_i->time,
2394 cifs_i->time + cifs_sb->ctx->acdirmax))
2397 if (!cifs_sb->ctx->acregmax)
2399 if (!time_in_range(jiffies, cifs_i->time,
2400 cifs_i->time + cifs_sb->ctx->acregmax))
2404 /* hardlinked files w/ noserverino get "special" treatment */
2405 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2406 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2413 * Zap the cache. Called when invalid_mapping flag is set.
2416 cifs_invalidate_mapping(struct inode *inode)
2420 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2421 rc = invalidate_inode_pages2(inode->i_mapping);
2423 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2424 __func__, inode, rc);
2431 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2433 * @key: currently unused
2434 * @mode: the task state to sleep in
2437 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2440 if (signal_pending_state(mode, current))
2441 return -ERESTARTSYS;
2446 cifs_revalidate_mapping(struct inode *inode)
2449 unsigned long *flags = &CIFS_I(inode)->flags;
2450 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2452 /* swapfiles are not supposed to be shared */
2453 if (IS_SWAPFILE(inode))
2456 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2457 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2461 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2462 /* for cache=singleclient, do not invalidate */
2463 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2464 goto skip_invalidate;
2466 rc = cifs_invalidate_mapping(inode);
2468 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2472 clear_bit_unlock(CIFS_INO_LOCK, flags);
2473 smp_mb__after_atomic();
2474 wake_up_bit(flags, CIFS_INO_LOCK);
2480 cifs_zap_mapping(struct inode *inode)
2482 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2483 return cifs_revalidate_mapping(inode);
2486 int cifs_revalidate_file_attr(struct file *filp)
2489 struct dentry *dentry = file_dentry(filp);
2490 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2491 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2492 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2494 if (!cifs_dentry_needs_reval(dentry))
2497 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2498 if (tlink_tcon(cfile->tlink)->unix_ext)
2499 rc = cifs_get_file_info_unix(filp);
2501 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2502 rc = cifs_get_file_info(filp);
2507 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2511 struct inode *inode = d_inode(dentry);
2512 struct super_block *sb = dentry->d_sb;
2513 const char *full_path;
2520 if (!cifs_dentry_needs_reval(dentry))
2525 page = alloc_dentry_path();
2526 full_path = build_path_from_dentry(dentry, page);
2527 if (IS_ERR(full_path)) {
2528 rc = PTR_ERR(full_path);
2532 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2533 full_path, inode, inode->i_count.counter,
2534 dentry, cifs_get_time(dentry), jiffies);
2537 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2538 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2539 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2540 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2542 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2544 if (rc == -EAGAIN && count++ < 10)
2547 free_dentry_path(page);
2553 int cifs_revalidate_file(struct file *filp)
2556 struct inode *inode = file_inode(filp);
2558 rc = cifs_revalidate_file_attr(filp);
2562 return cifs_revalidate_mapping(inode);
2565 /* revalidate a dentry's inode attributes */
2566 int cifs_revalidate_dentry(struct dentry *dentry)
2569 struct inode *inode = d_inode(dentry);
2571 rc = cifs_revalidate_dentry_attr(dentry);
2575 return cifs_revalidate_mapping(inode);
2578 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2579 struct kstat *stat, u32 request_mask, unsigned int flags)
2581 struct dentry *dentry = path->dentry;
2582 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2583 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2584 struct inode *inode = d_inode(dentry);
2587 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2591 * We need to be sure that all dirty pages are written and the server
2592 * has actual ctime, mtime and file length.
2594 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2595 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2596 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2597 rc = filemap_fdatawait(inode->i_mapping);
2599 mapping_set_error(inode->i_mapping, rc);
2604 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2605 CIFS_I(inode)->time = 0; /* force revalidate */
2608 * If the caller doesn't require syncing, only sync if
2609 * necessary (e.g. due to earlier truncate or setattr
2610 * invalidating the cached metadata)
2612 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2613 (CIFS_I(inode)->time == 0)) {
2614 rc = cifs_revalidate_dentry_attr(dentry);
2619 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2620 stat->blksize = cifs_sb->ctx->bsize;
2621 stat->ino = CIFS_I(inode)->uniqueid;
2623 /* old CIFS Unix Extensions doesn't return create time */
2624 if (CIFS_I(inode)->createtime) {
2625 stat->result_mask |= STATX_BTIME;
2627 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2630 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2631 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2632 stat->attributes |= STATX_ATTR_COMPRESSED;
2633 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2634 stat->attributes |= STATX_ATTR_ENCRYPTED;
2637 * If on a multiuser mount without unix extensions or cifsacl being
2638 * enabled, and the admin hasn't overridden them, set the ownership
2639 * to the fsuid/fsgid of the current process.
2641 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2642 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2644 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2645 stat->uid = current_fsuid();
2646 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2647 stat->gid = current_fsgid();
2652 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2655 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2656 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2657 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2658 struct TCP_Server_Info *server = tcon->ses->server;
2659 struct cifsFileInfo *cfile;
2662 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2666 * We need to be sure that all dirty pages are written as they
2667 * might fill holes on the server.
2669 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2670 inode->i_mapping->nrpages != 0) {
2671 rc = filemap_fdatawait(inode->i_mapping);
2673 mapping_set_error(inode->i_mapping, rc);
2678 cfile = find_readable_file(cifs_i, false);
2682 if (server->ops->fiemap) {
2683 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2684 cifsFileInfo_put(cfile);
2688 cifsFileInfo_put(cfile);
2692 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2694 pgoff_t index = from >> PAGE_SHIFT;
2695 unsigned offset = from & (PAGE_SIZE - 1);
2699 page = grab_cache_page(mapping, index);
2703 zero_user_segment(page, offset, PAGE_SIZE);
2709 void cifs_setsize(struct inode *inode, loff_t offset)
2711 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2713 spin_lock(&inode->i_lock);
2714 i_size_write(inode, offset);
2715 spin_unlock(&inode->i_lock);
2717 /* Cached inode must be refreshed on truncate */
2719 truncate_pagecache(inode, offset);
2723 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2724 unsigned int xid, const char *full_path)
2727 struct cifsFileInfo *open_file;
2728 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2729 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2730 struct tcon_link *tlink = NULL;
2731 struct cifs_tcon *tcon = NULL;
2732 struct TCP_Server_Info *server;
2735 * To avoid spurious oplock breaks from server, in the case of
2736 * inodes that we already have open, avoid doing path based
2737 * setting of file size if we can do it by handle.
2738 * This keeps our caching token (oplock) and avoids timeouts
2739 * when the local oplock break takes longer to flush
2740 * writebehind data than the SMB timeout for the SetPathInfo
2741 * request would allow
2743 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2745 tcon = tlink_tcon(open_file->tlink);
2746 server = tcon->ses->server;
2747 if (server->ops->set_file_size)
2748 rc = server->ops->set_file_size(xid, tcon, open_file,
2749 attrs->ia_size, false);
2752 cifsFileInfo_put(open_file);
2753 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2761 tlink = cifs_sb_tlink(cifs_sb);
2763 return PTR_ERR(tlink);
2764 tcon = tlink_tcon(tlink);
2765 server = tcon->ses->server;
2769 * Set file size by pathname rather than by handle either because no
2770 * valid, writeable file handle for it was found or because there was
2771 * an error setting it by handle.
2773 if (server->ops->set_path_size)
2774 rc = server->ops->set_path_size(xid, tcon, full_path,
2775 attrs->ia_size, cifs_sb, false);
2778 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2781 cifs_put_tlink(tlink);
2785 cifsInode->server_eof = attrs->ia_size;
2786 cifs_setsize(inode, attrs->ia_size);
2788 * i_blocks is not related to (i_size / i_blksize), but instead
2789 * 512 byte (2**9) size is required for calculating num blocks.
2790 * Until we can query the server for actual allocation size,
2791 * this is best estimate we have for blocks allocated for a file
2792 * Number of blocks must be rounded up so size 1 is not 0 blocks
2794 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2797 * The man page of truncate says if the size changed,
2798 * then the st_ctime and st_mtime fields for the file
2801 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2802 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2804 cifs_truncate_page(inode->i_mapping, inode->i_size);
2810 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2812 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2816 const char *full_path;
2817 void *page = alloc_dentry_path();
2818 struct inode *inode = d_inode(direntry);
2819 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2820 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2821 struct tcon_link *tlink;
2822 struct cifs_tcon *pTcon;
2823 struct cifs_unix_set_info_args *args = NULL;
2824 struct cifsFileInfo *open_file;
2826 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2827 direntry, attrs->ia_valid);
2831 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2832 attrs->ia_valid |= ATTR_FORCE;
2834 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2838 full_path = build_path_from_dentry(direntry, page);
2839 if (IS_ERR(full_path)) {
2840 rc = PTR_ERR(full_path);
2845 * Attempt to flush data before changing attributes. We need to do
2846 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2847 * ownership or mode then we may also need to do this. Here, we take
2848 * the safe way out and just do the flush on all setattr requests. If
2849 * the flush returns error, store it to report later and continue.
2851 * BB: This should be smarter. Why bother flushing pages that
2852 * will be truncated anyway? Also, should we error out here if
2853 * the flush returns error?
2855 rc = filemap_write_and_wait(inode->i_mapping);
2856 if (is_interrupt_error(rc)) {
2861 mapping_set_error(inode->i_mapping, rc);
2864 if (attrs->ia_valid & ATTR_SIZE) {
2865 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2870 /* skip mode change if it's just for clearing setuid/setgid */
2871 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2872 attrs->ia_valid &= ~ATTR_MODE;
2874 args = kmalloc(sizeof(*args), GFP_KERNEL);
2880 /* set up the struct */
2881 if (attrs->ia_valid & ATTR_MODE)
2882 args->mode = attrs->ia_mode;
2884 args->mode = NO_CHANGE_64;
2886 if (attrs->ia_valid & ATTR_UID)
2887 args->uid = attrs->ia_uid;
2889 args->uid = INVALID_UID; /* no change */
2891 if (attrs->ia_valid & ATTR_GID)
2892 args->gid = attrs->ia_gid;
2894 args->gid = INVALID_GID; /* no change */
2896 if (attrs->ia_valid & ATTR_ATIME)
2897 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2899 args->atime = NO_CHANGE_64;
2901 if (attrs->ia_valid & ATTR_MTIME)
2902 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2904 args->mtime = NO_CHANGE_64;
2906 if (attrs->ia_valid & ATTR_CTIME)
2907 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2909 args->ctime = NO_CHANGE_64;
2912 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2914 u16 nfid = open_file->fid.netfid;
2915 u32 npid = open_file->pid;
2916 pTcon = tlink_tcon(open_file->tlink);
2917 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2918 cifsFileInfo_put(open_file);
2920 tlink = cifs_sb_tlink(cifs_sb);
2921 if (IS_ERR(tlink)) {
2922 rc = PTR_ERR(tlink);
2925 pTcon = tlink_tcon(tlink);
2926 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2928 cifs_remap(cifs_sb));
2929 cifs_put_tlink(tlink);
2935 if ((attrs->ia_valid & ATTR_SIZE) &&
2936 attrs->ia_size != i_size_read(inode)) {
2937 truncate_setsize(inode, attrs->ia_size);
2938 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2941 setattr_copy(&nop_mnt_idmap, inode, attrs);
2942 mark_inode_dirty(inode);
2944 /* force revalidate when any of these times are set since some
2945 of the fs types (eg ext3, fat) do not have fine enough
2946 time granularity to match protocol, and we do not have a
2947 a way (yet) to query the server fs's time granularity (and
2948 whether it rounds times down).
2950 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2951 cifsInode->time = 0;
2954 free_dentry_path(page);
2958 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2961 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2964 kuid_t uid = INVALID_UID;
2965 kgid_t gid = INVALID_GID;
2966 struct inode *inode = d_inode(direntry);
2967 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2968 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2969 struct cifsFileInfo *wfile;
2970 struct cifs_tcon *tcon;
2971 const char *full_path;
2972 void *page = alloc_dentry_path();
2975 __u64 mode = NO_CHANGE_64;
2979 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2980 direntry, attrs->ia_valid);
2982 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2983 attrs->ia_valid |= ATTR_FORCE;
2985 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2987 goto cifs_setattr_exit;
2989 full_path = build_path_from_dentry(direntry, page);
2990 if (IS_ERR(full_path)) {
2991 rc = PTR_ERR(full_path);
2992 goto cifs_setattr_exit;
2996 * Attempt to flush data before changing attributes. We need to do
2997 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2998 * returns error, store it to report later and continue.
3000 * BB: This should be smarter. Why bother flushing pages that
3001 * will be truncated anyway? Also, should we error out here if
3002 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3004 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3005 rc = filemap_write_and_wait(inode->i_mapping);
3006 if (is_interrupt_error(rc)) {
3008 goto cifs_setattr_exit;
3010 mapping_set_error(inode->i_mapping, rc);
3015 if ((attrs->ia_valid & ATTR_MTIME) &&
3016 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3017 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3019 tcon = tlink_tcon(wfile->tlink);
3020 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3021 cifsFileInfo_put(wfile);
3023 goto cifs_setattr_exit;
3024 } else if (rc != -EBADF)
3025 goto cifs_setattr_exit;
3030 if (attrs->ia_valid & ATTR_SIZE) {
3031 rc = cifs_set_file_size(inode, attrs, xid, full_path);
3033 goto cifs_setattr_exit;
3036 if (attrs->ia_valid & ATTR_UID)
3037 uid = attrs->ia_uid;
3039 if (attrs->ia_valid & ATTR_GID)
3040 gid = attrs->ia_gid;
3042 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3043 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3044 if (uid_valid(uid) || gid_valid(gid)) {
3045 mode = NO_CHANGE_64;
3046 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3049 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3051 goto cifs_setattr_exit;
3055 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3056 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3058 /* skip mode change if it's just for clearing setuid/setgid */
3059 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3060 attrs->ia_valid &= ~ATTR_MODE;
3062 if (attrs->ia_valid & ATTR_MODE) {
3063 mode = attrs->ia_mode;
3065 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3066 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3067 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3068 INVALID_UID, INVALID_GID);
3070 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3072 goto cifs_setattr_exit;
3076 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3077 * Pick up the actual mode bits that were set.
3079 if (mode != attrs->ia_mode)
3080 attrs->ia_mode = mode;
3082 if (((mode & S_IWUGO) == 0) &&
3083 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3085 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3087 /* fix up mode if we're not using dynperm */
3088 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3089 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3090 } else if ((mode & S_IWUGO) &&
3091 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3093 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3094 /* Attributes of 0 are ignored */
3096 dosattr |= ATTR_NORMAL;
3098 /* reset local inode permissions to normal */
3099 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3100 attrs->ia_mode &= ~(S_IALLUGO);
3101 if (S_ISDIR(inode->i_mode))
3103 cifs_sb->ctx->dir_mode;
3106 cifs_sb->ctx->file_mode;
3108 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3109 /* ignore mode change - ATTR_READONLY hasn't changed */
3110 attrs->ia_valid &= ~ATTR_MODE;
3114 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3115 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3116 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3117 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3119 /* Even if error on time set, no sense failing the call if
3120 the server would set the time to a reasonable value anyway,
3121 and this check ensures that we are not being called from
3122 sys_utimes in which case we ought to fail the call back to
3123 the user when the server rejects the call */
3124 if ((rc) && (attrs->ia_valid &
3125 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3129 /* do not need local check to inode_check_ok since the server does
3132 goto cifs_setattr_exit;
3134 if ((attrs->ia_valid & ATTR_SIZE) &&
3135 attrs->ia_size != i_size_read(inode)) {
3136 truncate_setsize(inode, attrs->ia_size);
3137 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3140 setattr_copy(&nop_mnt_idmap, inode, attrs);
3141 mark_inode_dirty(inode);
3145 free_dentry_path(page);
3150 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3151 struct iattr *attrs)
3153 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3154 int rc, retries = 0;
3155 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3156 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3157 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3159 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3163 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3164 if (pTcon->unix_ext)
3165 rc = cifs_setattr_unix(direntry, attrs);
3167 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3168 rc = cifs_setattr_nounix(direntry, attrs);
3170 } while (is_retryable_error(rc) && retries < 2);
3172 /* BB: add cifs_setattr_legacy for really old servers */