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"
31 static void cifs_set_ops(struct inode *inode)
33 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
35 switch (inode->i_mode & S_IFMT) {
37 inode->i_op = &cifs_file_inode_ops;
38 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
39 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
40 inode->i_fop = &cifs_file_direct_nobrl_ops;
42 inode->i_fop = &cifs_file_direct_ops;
43 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
44 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
45 inode->i_fop = &cifs_file_strict_nobrl_ops;
47 inode->i_fop = &cifs_file_strict_ops;
48 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
49 inode->i_fop = &cifs_file_nobrl_ops;
50 else { /* not direct, send byte range locks */
51 inode->i_fop = &cifs_file_ops;
54 /* check if server can support readahead */
55 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
56 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
57 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
59 inode->i_data.a_ops = &cifs_addr_ops;
62 if (IS_AUTOMOUNT(inode)) {
63 inode->i_op = &cifs_namespace_inode_operations;
65 inode->i_op = &cifs_dir_inode_ops;
66 inode->i_fop = &cifs_dir_ops;
70 inode->i_op = &cifs_symlink_inode_ops;
73 init_special_inode(inode, inode->i_mode, inode->i_rdev);
78 /* check inode attributes against fattr. If they don't match, tag the
79 * inode for cache invalidation
82 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
84 struct cifs_fscache_inode_coherency_data cd;
85 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
86 struct timespec64 mtime;
88 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
89 __func__, cifs_i->uniqueid);
91 if (inode->i_state & I_NEW) {
92 cifs_dbg(FYI, "%s: inode %llu is new\n",
93 __func__, cifs_i->uniqueid);
97 /* don't bother with revalidation if we have an oplock */
98 if (CIFS_CACHE_READ(cifs_i)) {
99 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
100 __func__, cifs_i->uniqueid);
104 /* revalidate if mtime or size have changed */
105 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
106 mtime = inode_get_mtime(inode);
107 if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
108 cifs_i->netfs.remote_i_size == fattr->cf_eof) {
109 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
110 __func__, cifs_i->uniqueid);
114 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
115 __func__, cifs_i->uniqueid);
116 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
117 /* Invalidate fscache cookie */
118 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
119 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
123 * copy nlink to the inode, unless it wasn't provided. Provide
124 * sane values if we don't have an existing one and none was provided
127 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
130 * if we're in a situation where we can't trust what we
131 * got from the server (readdir, some non-unix cases)
132 * fake reasonable values
134 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
135 /* only provide fake values on a new inode */
136 if (inode->i_state & I_NEW) {
137 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
145 /* we trust the server, so update it */
146 set_nlink(inode, fattr->cf_nlink);
149 /* populate an inode with info from a cifs_fattr struct */
151 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
154 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
157 if (!(inode->i_state & I_NEW) &&
158 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
159 CIFS_I(inode)->time = 0; /* force reval */
163 cifs_revalidate_cache(inode, fattr);
165 spin_lock(&inode->i_lock);
166 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
167 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
168 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
169 /* we do not want atime to be less than mtime, it broke some apps */
170 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
171 inode_set_atime_to_ts(inode, fattr->cf_mtime);
173 inode_set_atime_to_ts(inode, fattr->cf_atime);
174 inode_set_mtime_to_ts(inode, fattr->cf_mtime);
175 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
176 inode->i_rdev = fattr->cf_rdev;
177 cifs_nlink_fattr_to_inode(inode, fattr);
178 inode->i_uid = fattr->cf_uid;
179 inode->i_gid = fattr->cf_gid;
181 /* if dynperm is set, don't clobber existing mode */
182 if (inode->i_state & I_NEW ||
183 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
184 inode->i_mode = fattr->cf_mode;
186 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
187 cifs_i->reparse_tag = fattr->cf_cifstag;
189 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
192 cifs_i->time = jiffies;
194 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
195 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
197 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
199 cifs_i->netfs.remote_i_size = fattr->cf_eof;
201 * Can't safely change the file size here if the client is writing to
202 * it due to potential races.
204 if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
205 i_size_write(inode, fattr->cf_eof);
208 * i_blocks is not related to (i_size / i_blksize),
209 * but instead 512 byte (2**9) size is required for
210 * calculating num blocks.
212 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
215 if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) {
216 kfree(cifs_i->symlink_target);
217 cifs_i->symlink_target = fattr->cf_symlink_target;
218 fattr->cf_symlink_target = NULL;
220 spin_unlock(&inode->i_lock);
222 if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
223 inode->i_flags |= S_AUTOMOUNT;
224 if (inode->i_state & I_NEW)
230 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
232 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
234 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
237 fattr->cf_uniqueid = iunique(sb, ROOT_I);
240 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
242 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
243 struct cifs_sb_info *cifs_sb)
245 memset(fattr, 0, sizeof(*fattr));
246 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
247 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
248 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
250 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
251 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
252 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
253 /* old POSIX extensions don't get create time */
255 fattr->cf_mode = le64_to_cpu(info->Permissions);
258 * Since we set the inode type below we need to mask off
259 * to avoid strange results if bits set above.
261 fattr->cf_mode &= ~S_IFMT;
262 switch (le32_to_cpu(info->Type)) {
264 fattr->cf_mode |= S_IFREG;
265 fattr->cf_dtype = DT_REG;
268 fattr->cf_mode |= S_IFLNK;
269 fattr->cf_dtype = DT_LNK;
272 fattr->cf_mode |= S_IFDIR;
273 fattr->cf_dtype = DT_DIR;
276 fattr->cf_mode |= S_IFCHR;
277 fattr->cf_dtype = DT_CHR;
278 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
279 le64_to_cpu(info->DevMinor) & MINORMASK);
282 fattr->cf_mode |= S_IFBLK;
283 fattr->cf_dtype = DT_BLK;
284 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
285 le64_to_cpu(info->DevMinor) & MINORMASK);
288 fattr->cf_mode |= S_IFIFO;
289 fattr->cf_dtype = DT_FIFO;
292 fattr->cf_mode |= S_IFSOCK;
293 fattr->cf_dtype = DT_SOCK;
296 /* safest to call it a file if we do not know */
297 fattr->cf_mode |= S_IFREG;
298 fattr->cf_dtype = DT_REG;
299 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
303 fattr->cf_uid = cifs_sb->ctx->linux_uid;
304 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
305 u64 id = le64_to_cpu(info->Uid);
306 if (id < ((uid_t)-1)) {
307 kuid_t uid = make_kuid(&init_user_ns, id);
313 fattr->cf_gid = cifs_sb->ctx->linux_gid;
314 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
315 u64 id = le64_to_cpu(info->Gid);
316 if (id < ((gid_t)-1)) {
317 kgid_t gid = make_kgid(&init_user_ns, id);
323 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
327 * Fill a cifs_fattr struct with fake inode info.
329 * Needed to setup cifs_fattr data for the directory which is the
330 * junction to the new submount (ie to setup the fake directory
331 * which represents a DFS referral or reparse mount point).
333 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
334 struct super_block *sb)
336 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
338 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
340 memset(fattr, 0, sizeof(*fattr));
341 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
342 fattr->cf_uid = cifs_sb->ctx->linux_uid;
343 fattr->cf_gid = cifs_sb->ctx->linux_gid;
344 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
345 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
347 fattr->cf_flags = CIFS_FATTR_JUNCTION;
350 /* Update inode with final fattr data */
351 static int update_inode_info(struct super_block *sb,
352 struct cifs_fattr *fattr,
353 struct inode **inode)
355 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
359 *inode = cifs_iget(sb, fattr);
364 /* We already have inode, update it.
366 * If file type or uniqueid is different, return error.
368 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
369 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
370 CIFS_I(*inode)->time = 0; /* force reval */
373 return cifs_fattr_to_inode(*inode, fattr, false);
376 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
378 cifs_get_file_info_unix(struct file *filp)
382 FILE_UNIX_BASIC_INFO find_data;
383 struct cifs_fattr fattr = {};
384 struct inode *inode = file_inode(filp);
385 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
386 struct cifsFileInfo *cfile = filp->private_data;
387 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
391 if (cfile->symlink_target) {
392 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
393 if (!fattr.cf_symlink_target) {
395 goto cifs_gfiunix_out;
399 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
401 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
402 } else if (rc == -EREMOTE) {
403 cifs_create_junction_fattr(&fattr, inode->i_sb);
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 void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
732 struct cifs_open_info_data *data,
733 struct super_block *sb)
735 struct smb2_file_all_info *info = &data->fi;
736 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
737 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
739 memset(fattr, 0, sizeof(*fattr));
740 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
741 if (info->DeletePending)
742 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
744 if (info->LastAccessTime)
745 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
747 ktime_get_coarse_real_ts64(&fattr->cf_atime);
749 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
750 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
752 if (data->adjust_tz) {
753 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
754 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
757 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
758 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
759 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
760 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
761 fattr->cf_uid = cifs_sb->ctx->linux_uid;
762 fattr->cf_gid = cifs_sb->ctx->linux_gid;
764 fattr->cf_mode = cifs_sb->ctx->file_mode;
765 if (cifs_open_data_reparse(data) &&
766 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
769 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
770 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
771 fattr->cf_dtype = DT_DIR;
773 * Server can return wrong NumberOfLinks value for directories
774 * when Unix extensions are disabled - fake it.
777 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
779 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
780 fattr->cf_dtype = DT_REG;
782 /* clear write bits if ATTR_READONLY is set */
783 if (fattr->cf_cifsattrs & ATTR_READONLY)
784 fattr->cf_mode &= ~(S_IWUGO);
787 * Don't accept zero nlink from non-unix servers unless
788 * delete is pending. Instead mark it as unknown.
790 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
791 !info->DeletePending) {
792 cifs_dbg(VFS, "bogus file nlink value %u\n",
794 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
799 if (S_ISLNK(fattr->cf_mode)) {
800 if (likely(data->symlink_target))
801 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
802 fattr->cf_symlink_target = data->symlink_target;
803 data->symlink_target = NULL;
808 cifs_get_file_info(struct file *filp)
812 struct cifs_open_info_data data = {};
813 struct cifs_fattr fattr;
814 struct inode *inode = file_inode(filp);
815 struct cifsFileInfo *cfile = filp->private_data;
816 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
817 struct TCP_Server_Info *server = tcon->ses->server;
818 struct dentry *dentry = filp->f_path.dentry;
819 void *page = alloc_dentry_path();
820 const unsigned char *path;
822 if (!server->ops->query_file_info) {
823 free_dentry_path(page);
828 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
831 /* TODO: add support to query reparse tag */
832 data.adjust_tz = false;
833 if (data.symlink_target) {
835 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
837 path = build_path_from_dentry(dentry, page);
842 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
843 if (fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
844 cifs_mark_open_handles_for_deleted_file(inode, path);
847 cifs_create_junction_fattr(&fattr, inode->i_sb);
852 * FIXME: legacy server -- fall back to path-based call?
853 * for now, just skip revalidating and mark inode for
857 CIFS_I(inode)->time = 0;
864 * don't bother with SFU junk here -- just mark inode as needing
867 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
868 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
869 /* if filetype is different, return error */
870 rc = cifs_fattr_to_inode(inode, &fattr, false);
872 cifs_free_open_info(&data);
873 free_dentry_path(page);
878 /* Simple function to return a 64 bit hash of string. Rarely called */
879 static __u64 simple_hashstr(const char *str)
881 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
885 hash = (hash + (__u64) *str++) * hash_mult;
890 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
892 * cifs_backup_query_path_info - SMB1 fallback code to get ino
894 * Fallback code to get file metadata when we don't have access to
895 * full_path (EACCES) and have backup creds.
897 * @xid: transaction id used to identify original request in logs
898 * @tcon: information about the server share we have mounted
899 * @sb: the superblock stores info such as disk space available
900 * @full_path: name of the file we are getting the metadata for
901 * @resp_buf: will be set to cifs resp buf and needs to be freed with
902 * cifs_buf_release() when done with @data
903 * @data: will be set to search info result buffer
906 cifs_backup_query_path_info(int xid,
907 struct cifs_tcon *tcon,
908 struct super_block *sb,
909 const char *full_path,
911 FILE_ALL_INFO **data)
913 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
914 struct cifs_search_info info = {0};
919 info.endOfSearch = false;
921 info.info_level = SMB_FIND_FILE_UNIX;
922 else if ((tcon->ses->capabilities &
923 tcon->ses->server->vals->cap_nt_find) == 0)
924 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
925 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
926 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
927 else /* no srvino useful for fallback to some netapp */
928 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
930 flags = CIFS_SEARCH_CLOSE_ALWAYS |
931 CIFS_SEARCH_CLOSE_AT_END |
932 CIFS_SEARCH_BACKUP_SEARCH;
934 rc = CIFSFindFirst(xid, tcon, full_path,
935 cifs_sb, NULL, flags, &info, false);
939 *resp_buf = (void *)info.ntwrk_buf_start;
940 *data = (FILE_ALL_INFO *)info.srch_entries_start;
943 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
945 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
946 struct inode **inode, const char *full_path,
947 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
949 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
950 struct TCP_Server_Info *server = tcon->ses->server;
953 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
955 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
957 fattr->cf_uniqueid = iunique(sb, ROOT_I);
962 * If we have an inode pass a NULL tcon to ensure we don't
963 * make a round trip to the server. This only works for SMB2+.
965 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
966 &fattr->cf_uniqueid, data);
969 * If that fails reuse existing ino or generate one
970 * and disable server ones
973 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
975 fattr->cf_uniqueid = iunique(sb, ROOT_I);
976 cifs_autodisable_serverino(cifs_sb);
981 /* If no errors, check for zero root inode (invalid) */
982 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
983 cifs_dbg(FYI, "Invalid (0) inodenum\n");
986 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
988 /* make an ino by hashing the UNC */
989 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
990 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
995 static inline bool is_inode_cache_good(struct inode *ino)
997 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1000 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1001 struct super_block *sb,
1002 const unsigned int xid,
1003 struct cifs_tcon *tcon,
1004 const char *full_path,
1005 struct cifs_fattr *fattr)
1007 struct TCP_Server_Info *server = tcon->ses->server;
1008 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1009 struct kvec rsp_iov, *iov = NULL;
1010 int rsp_buftype = CIFS_NO_BUFFER;
1011 u32 tag = data->reparse.tag;
1014 if (!tag && server->ops->query_reparse_point) {
1015 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1017 &rsp_iov, &rsp_buftype);
1020 } else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1021 data->reparse.io.iov.iov_base) {
1022 iov = &data->reparse.io.iov;
1026 switch ((data->reparse.tag = tag)) {
1027 case 0: /* SMB1 symlink */
1028 if (server->ops->query_symlink) {
1029 rc = server->ops->query_symlink(xid, tcon,
1031 &data->symlink_target);
1034 case IO_REPARSE_TAG_MOUNT_POINT:
1035 cifs_create_junction_fattr(fattr, sb);
1039 /* Check for cached reparse point data */
1040 if (data->symlink_target || data->reparse.buf) {
1042 } else if (iov && server->ops->parse_reparse_point) {
1043 rc = server->ops->parse_reparse_point(cifs_sb,
1049 if (tcon->posix_extensions)
1050 smb311_posix_info_to_fattr(fattr, data, sb);
1052 cifs_open_info_to_fattr(fattr, data, sb);
1054 fattr->cf_cifstag = data->reparse.tag;
1055 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1059 static int cifs_get_fattr(struct cifs_open_info_data *data,
1060 struct super_block *sb, int xid,
1061 const struct cifs_fid *fid,
1062 struct cifs_fattr *fattr,
1063 struct inode **inode,
1064 const char *full_path)
1066 struct cifs_open_info_data tmp_data = {};
1067 struct cifs_tcon *tcon;
1068 struct TCP_Server_Info *server;
1069 struct tcon_link *tlink;
1070 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1071 void *smb1_backup_rsp_buf = NULL;
1075 tlink = cifs_sb_tlink(cifs_sb);
1077 return PTR_ERR(tlink);
1078 tcon = tlink_tcon(tlink);
1079 server = tcon->ses->server;
1082 * 1. Fetch file metadata if not provided (data)
1086 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1087 full_path, &tmp_data);
1092 * 2. Convert it to internal cifs metadata (fattr)
1098 * If the file is a reparse point, it is more complicated
1099 * since we have to check if its reparse tag matches a known
1100 * special file type e.g. symlink or fifo or char etc.
1102 if (cifs_open_data_reparse(data)) {
1103 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1106 cifs_open_info_to_fattr(fattr, data, sb);
1108 if (!rc && fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
1109 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1112 /* DFS link, no metadata available on this server */
1113 cifs_create_junction_fattr(fattr, sb);
1117 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1119 * perm errors, try again with backup flags if possible
1121 * For SMB2 and later the backup intent flag
1122 * is already sent if needed on open and there
1123 * is no path based FindFirst operation to use
1126 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1127 /* for easier reading */
1129 FILE_DIRECTORY_INFO *fdi;
1130 SEARCH_ID_FULL_DIR_INFO *si;
1132 rc = cifs_backup_query_path_info(xid, tcon, sb,
1134 &smb1_backup_rsp_buf,
1139 move_cifs_info_to_smb2(&data->fi, fi);
1140 fdi = (FILE_DIRECTORY_INFO *)fi;
1141 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1143 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1144 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1145 /* uniqueid set, skip get inum step */
1146 goto handle_mnt_opt;
1148 /* nothing we can do, bail out */
1153 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1156 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1161 * 3. Get or update inode number (fattr->cf_uniqueid)
1164 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1167 * 4. Tweak fattr based on mount options
1169 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1171 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1172 /* query for SFU type info if supported and needed */
1173 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1174 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1175 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1177 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1180 /* fill in 0777 bits from ACL */
1181 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1182 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1183 true, full_path, fid);
1187 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1191 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1192 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1193 false, full_path, fid);
1197 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1203 /* fill in remaining high mode bits e.g. SUID, VTX */
1204 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1205 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1207 /* check for Minshall+French symlinks */
1208 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1209 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1210 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1214 cifs_buf_release(smb1_backup_rsp_buf);
1215 cifs_put_tlink(tlink);
1216 cifs_free_open_info(&tmp_data);
1220 int cifs_get_inode_info(struct inode **inode,
1221 const char *full_path,
1222 struct cifs_open_info_data *data,
1223 struct super_block *sb, int xid,
1224 const struct cifs_fid *fid)
1226 struct cifs_fattr fattr = {};
1229 if (is_inode_cache_good(*inode)) {
1230 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1234 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1238 rc = update_inode_info(sb, &fattr, inode);
1240 kfree(fattr.cf_symlink_target);
1244 static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1245 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 tmp_data = {};
1251 struct TCP_Server_Info *server;
1252 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1253 struct cifs_tcon *tcon;
1254 struct tcon_link *tlink;
1258 tlink = cifs_sb_tlink(cifs_sb);
1260 return PTR_ERR(tlink);
1261 tcon = tlink_tcon(tlink);
1262 server = tcon->ses->server;
1265 * 1. Fetch file metadata if not provided (data)
1268 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1269 full_path, &tmp_data);
1274 * 2. Convert it to internal cifs metadata (fattr)
1279 if (cifs_open_data_reparse(data)) {
1280 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1283 smb311_posix_info_to_fattr(fattr, data, sb);
1287 /* DFS link, no metadata available on this server */
1288 cifs_create_junction_fattr(fattr, sb);
1293 * For SMB2 and later the backup intent flag
1294 * is already sent if needed on open and there
1295 * is no path based FindFirst operation to use
1296 * to retry with so nothing we can do, bail out
1300 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1305 * 3. Tweak fattr based on mount options
1307 /* check for Minshall+French symlinks */
1308 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1309 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1310 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1314 cifs_put_tlink(tlink);
1315 cifs_free_open_info(data);
1319 int smb311_posix_get_inode_info(struct inode **inode,
1320 const char *full_path,
1321 struct cifs_open_info_data *data,
1322 struct super_block *sb,
1323 const unsigned int xid)
1325 struct cifs_fattr fattr = {};
1328 if (is_inode_cache_good(*inode)) {
1329 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1333 rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1337 rc = update_inode_info(sb, &fattr, inode);
1338 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1339 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1341 kfree(fattr.cf_symlink_target);
1345 static const struct inode_operations cifs_ipc_inode_ops = {
1346 .lookup = cifs_lookup,
1350 cifs_find_inode(struct inode *inode, void *opaque)
1352 struct cifs_fattr *fattr = opaque;
1354 /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1356 /* don't match inode with different uniqueid */
1357 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1360 /* use createtime like an i_generation field */
1361 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1364 /* don't match inode of different type */
1365 if (inode_wrong_type(inode, fattr->cf_mode))
1368 /* if it's not a directory or has no dentries, then flag it */
1369 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1370 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1376 cifs_init_inode(struct inode *inode, void *opaque)
1378 struct cifs_fattr *fattr = opaque;
1380 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1381 CIFS_I(inode)->createtime = fattr->cf_createtime;
1386 * walk dentry list for an inode and report whether it has aliases that
1387 * are hashed. We use this to determine if a directory inode can actually
1391 inode_has_hashed_dentries(struct inode *inode)
1393 struct dentry *dentry;
1395 spin_lock(&inode->i_lock);
1396 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1397 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1398 spin_unlock(&inode->i_lock);
1402 spin_unlock(&inode->i_lock);
1406 /* Given fattrs, get a corresponding inode */
1408 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1411 struct inode *inode;
1414 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1416 /* hash down to 32-bits on 32-bit arch */
1417 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1419 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1421 /* was there a potentially problematic inode collision? */
1422 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1423 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1425 if (inode_has_hashed_dentries(inode)) {
1426 cifs_autodisable_serverino(CIFS_SB(sb));
1428 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1429 goto retry_iget5_locked;
1433 /* can't fail - see cifs_find_inode() */
1434 cifs_fattr_to_inode(inode, fattr, false);
1435 if (sb->s_flags & SB_NOATIME)
1436 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1437 if (inode->i_state & I_NEW) {
1438 inode->i_ino = hash;
1439 cifs_fscache_get_inode_cookie(inode);
1440 unlock_new_inode(inode);
1447 /* gets root inode */
1448 struct inode *cifs_root_iget(struct super_block *sb)
1450 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1451 struct cifs_fattr fattr = {};
1452 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1453 struct inode *inode = NULL;
1459 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1460 && cifs_sb->prepath) {
1461 len = strlen(cifs_sb->prepath);
1462 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1464 return ERR_PTR(-ENOMEM);
1466 memcpy(path+1, cifs_sb->prepath, len);
1468 path = kstrdup("", GFP_KERNEL);
1470 return ERR_PTR(-ENOMEM);
1474 if (tcon->unix_ext) {
1475 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1476 /* some servers mistakenly claim POSIX support */
1477 if (rc != -EOPNOTSUPP)
1479 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1480 tcon->unix_ext = false;
1483 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1484 if (tcon->posix_extensions)
1485 rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1487 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1491 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1492 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1493 cifs_autodisable_serverino(cifs_sb);
1495 inode = cifs_iget(sb, &fattr);
1499 inode = ERR_PTR(rc);
1503 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1504 cifs_mark_open_handles_for_deleted_file(inode, path);
1506 if (rc && tcon->pipe) {
1507 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1508 spin_lock(&inode->i_lock);
1509 inode->i_mode |= S_IFDIR;
1510 set_nlink(inode, 2);
1511 inode->i_op = &cifs_ipc_inode_ops;
1512 inode->i_fop = &simple_dir_operations;
1513 inode->i_uid = cifs_sb->ctx->linux_uid;
1514 inode->i_gid = cifs_sb->ctx->linux_gid;
1515 spin_unlock(&inode->i_lock);
1518 inode = ERR_PTR(rc);
1524 kfree(fattr.cf_symlink_target);
1529 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1530 const char *full_path, __u32 dosattr)
1532 bool set_time = false;
1533 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1534 struct TCP_Server_Info *server;
1535 FILE_BASIC_INFO info_buf;
1540 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1541 if (!server->ops->set_file_info)
1546 if (attrs->ia_valid & ATTR_ATIME) {
1548 info_buf.LastAccessTime =
1549 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1551 info_buf.LastAccessTime = 0;
1553 if (attrs->ia_valid & ATTR_MTIME) {
1555 info_buf.LastWriteTime =
1556 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1558 info_buf.LastWriteTime = 0;
1561 * Samba throws this field away, but windows may actually use it.
1562 * Do not set ctime unless other time stamps are changed explicitly
1563 * (i.e. by utimes()) since we would then have a mix of client and
1566 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1567 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1568 info_buf.ChangeTime =
1569 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1571 info_buf.ChangeTime = 0;
1573 info_buf.CreationTime = 0; /* don't change */
1574 info_buf.Attributes = cpu_to_le32(dosattr);
1576 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1579 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1581 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1582 * and rename it to a random name that hopefully won't conflict with
1586 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1587 const unsigned int xid)
1591 struct cifs_fid fid;
1592 struct cifs_open_parms oparms;
1593 struct inode *inode = d_inode(dentry);
1594 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1595 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1596 struct tcon_link *tlink;
1597 struct cifs_tcon *tcon;
1598 __u32 dosattr, origattr;
1599 FILE_BASIC_INFO *info_buf = NULL;
1601 tlink = cifs_sb_tlink(cifs_sb);
1603 return PTR_ERR(tlink);
1604 tcon = tlink_tcon(tlink);
1607 * We cannot rename the file if the server doesn't support
1608 * CAP_INFOLEVEL_PASSTHRU
1610 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1615 oparms = (struct cifs_open_parms) {
1618 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1619 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1620 .disposition = FILE_OPEN,
1625 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1629 origattr = cifsInode->cifsAttrs;
1631 origattr |= ATTR_NORMAL;
1633 dosattr = origattr & ~ATTR_READONLY;
1635 dosattr |= ATTR_NORMAL;
1636 dosattr |= ATTR_HIDDEN;
1638 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1639 if (dosattr != origattr) {
1640 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1641 if (info_buf == NULL) {
1645 info_buf->Attributes = cpu_to_le32(dosattr);
1646 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1648 /* although we would like to mark the file hidden
1649 if that fails we will still try to rename it */
1651 cifsInode->cifsAttrs = dosattr;
1653 dosattr = origattr; /* since not able to change them */
1656 /* rename the file */
1657 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1659 cifs_remap(cifs_sb));
1665 /* try to set DELETE_ON_CLOSE */
1666 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1667 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1670 * some samba versions return -ENOENT when we try to set the
1671 * file disposition here. Likely a samba bug, but work around
1672 * it for now. This means that some cifsXXX files may hang
1673 * around after they shouldn't.
1675 * BB: remove this hack after more servers have the fix
1683 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1687 CIFSSMBClose(xid, tcon, fid.netfid);
1690 cifs_put_tlink(tlink);
1694 * reset everything back to the original state. Don't bother
1695 * dealing with errors here since we can't do anything about
1699 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1700 cifs_sb->local_nls, cifs_remap(cifs_sb));
1702 if (dosattr != origattr) {
1703 info_buf->Attributes = cpu_to_le32(origattr);
1704 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1706 cifsInode->cifsAttrs = origattr;
1711 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1713 /* copied from fs/nfs/dir.c with small changes */
1715 cifs_drop_nlink(struct inode *inode)
1717 spin_lock(&inode->i_lock);
1718 if (inode->i_nlink > 0)
1720 spin_unlock(&inode->i_lock);
1724 * If d_inode(dentry) is null (usually meaning the cached dentry
1725 * is a negative dentry) then we would attempt a standard SMB delete, but
1726 * if that fails we can not attempt the fall back mechanisms on EACCES
1727 * but will return the EACCES to the caller. Note that the VFS does not call
1728 * unlink on negative dentries currently.
1730 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1734 const char *full_path;
1736 struct inode *inode = d_inode(dentry);
1737 struct cifsInodeInfo *cifs_inode;
1738 struct super_block *sb = dir->i_sb;
1739 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1740 struct tcon_link *tlink;
1741 struct cifs_tcon *tcon;
1742 struct TCP_Server_Info *server;
1743 struct iattr *attrs = NULL;
1744 __u32 dosattr = 0, origattr = 0;
1746 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1748 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1751 tlink = cifs_sb_tlink(cifs_sb);
1753 return PTR_ERR(tlink);
1754 tcon = tlink_tcon(tlink);
1755 server = tcon->ses->server;
1758 page = alloc_dentry_path();
1760 if (tcon->nodelete) {
1765 /* Unlink can be called from rename so we can not take the
1766 * sb->s_vfs_rename_mutex here */
1767 full_path = build_path_from_dentry(dentry, page);
1768 if (IS_ERR(full_path)) {
1769 rc = PTR_ERR(full_path);
1773 cifs_close_deferred_file_under_dentry(tcon, full_path);
1774 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1775 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1776 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1777 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1778 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1779 cifs_remap(cifs_sb));
1780 cifs_dbg(FYI, "posix del rc %d\n", rc);
1781 if ((rc == 0) || (rc == -ENOENT))
1782 goto psx_del_no_retry;
1784 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1787 if (!server->ops->unlink) {
1789 goto psx_del_no_retry;
1792 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb, dentry);
1797 cifs_mark_open_handles_for_deleted_file(inode, full_path);
1798 cifs_drop_nlink(inode);
1800 } else if (rc == -ENOENT) {
1802 } else if (rc == -EBUSY) {
1803 if (server->ops->rename_pending_delete) {
1804 rc = server->ops->rename_pending_delete(full_path,
1807 cifs_mark_open_handles_for_deleted_file(inode, full_path);
1808 cifs_drop_nlink(inode);
1811 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1812 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1813 if (attrs == NULL) {
1818 /* try to reset dos attributes */
1819 cifs_inode = CIFS_I(inode);
1820 origattr = cifs_inode->cifsAttrs;
1822 origattr |= ATTR_NORMAL;
1823 dosattr = origattr & ~ATTR_READONLY;
1825 dosattr |= ATTR_NORMAL;
1826 dosattr |= ATTR_HIDDEN;
1828 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1832 goto retry_std_delete;
1835 /* undo the setattr if we errored out and it's needed */
1836 if (rc != 0 && dosattr != 0)
1837 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1841 cifs_inode = CIFS_I(inode);
1842 cifs_inode->time = 0; /* will force revalidate to get info
1844 inode_set_ctime_current(inode);
1846 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1847 cifs_inode = CIFS_I(dir);
1848 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1850 free_dentry_path(page);
1853 cifs_put_tlink(tlink);
1858 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1859 const char *full_path, struct cifs_sb_info *cifs_sb,
1860 struct cifs_tcon *tcon, const unsigned int xid)
1863 struct inode *inode = NULL;
1865 if (tcon->posix_extensions) {
1866 rc = smb311_posix_get_inode_info(&inode, full_path,
1867 NULL, parent->i_sb, xid);
1868 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1869 } else if (tcon->unix_ext) {
1870 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1872 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1874 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1881 if (!S_ISDIR(inode->i_mode)) {
1883 * mkdir succeeded, but another client has managed to remove the
1884 * sucker and replace it with non-directory. Return success,
1885 * but don't leave the child in dcache.
1892 * setting nlink not necessary except in cases where we failed to get it
1893 * from the server or was set bogus. Also, since this is a brand new
1894 * inode, no need to grab the i_lock before setting the i_nlink.
1896 if (inode->i_nlink < 2)
1897 set_nlink(inode, 2);
1898 mode &= ~current_umask();
1899 /* must turn on setgid bit if parent dir has it */
1900 if (parent->i_mode & S_ISGID)
1903 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1904 if (tcon->unix_ext) {
1905 struct cifs_unix_set_info_args args = {
1907 .ctime = NO_CHANGE_64,
1908 .atime = NO_CHANGE_64,
1909 .mtime = NO_CHANGE_64,
1912 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1913 args.uid = current_fsuid();
1914 if (parent->i_mode & S_ISGID)
1915 args.gid = parent->i_gid;
1917 args.gid = current_fsgid();
1919 args.uid = INVALID_UID; /* no change */
1920 args.gid = INVALID_GID; /* no change */
1922 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1924 cifs_remap(cifs_sb));
1928 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1929 struct TCP_Server_Info *server = tcon->ses->server;
1930 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1931 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1932 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1934 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1935 inode->i_mode = (mode | S_IFDIR);
1937 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1938 inode->i_uid = current_fsuid();
1939 if (inode->i_mode & S_ISGID)
1940 inode->i_gid = parent->i_gid;
1942 inode->i_gid = current_fsgid();
1945 d_instantiate(dentry, inode);
1949 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1951 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1952 const char *full_path, struct cifs_sb_info *cifs_sb,
1953 struct cifs_tcon *tcon, const unsigned int xid)
1957 FILE_UNIX_BASIC_INFO *info = NULL;
1958 struct inode *newinode = NULL;
1959 struct cifs_fattr fattr;
1961 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1964 goto posix_mkdir_out;
1967 mode &= ~current_umask();
1968 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1969 NULL /* netfid */, info, &oplock, full_path,
1970 cifs_sb->local_nls, cifs_remap(cifs_sb));
1971 if (rc == -EOPNOTSUPP)
1972 goto posix_mkdir_out;
1974 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1976 goto posix_mkdir_out;
1979 if (info->Type == cpu_to_le32(-1))
1980 /* no return info, go query for it */
1981 goto posix_mkdir_get_info;
1983 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1984 * need to set uid/gid.
1987 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1988 cifs_fill_uniqueid(inode->i_sb, &fattr);
1989 newinode = cifs_iget(inode->i_sb, &fattr);
1991 goto posix_mkdir_get_info;
1993 d_instantiate(dentry, newinode);
1995 #ifdef CONFIG_CIFS_DEBUG2
1996 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1997 dentry, dentry, newinode);
1999 if (newinode->i_nlink != 2)
2000 cifs_dbg(FYI, "unexpected number of links %d\n",
2007 posix_mkdir_get_info:
2008 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2010 goto posix_mkdir_out;
2012 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2014 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2015 struct dentry *direntry, umode_t mode)
2019 struct cifs_sb_info *cifs_sb;
2020 struct tcon_link *tlink;
2021 struct cifs_tcon *tcon;
2022 struct TCP_Server_Info *server;
2023 const char *full_path;
2026 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2029 cifs_sb = CIFS_SB(inode->i_sb);
2030 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2032 tlink = cifs_sb_tlink(cifs_sb);
2034 return PTR_ERR(tlink);
2035 tcon = tlink_tcon(tlink);
2039 page = alloc_dentry_path();
2040 full_path = build_path_from_dentry(direntry, page);
2041 if (IS_ERR(full_path)) {
2042 rc = PTR_ERR(full_path);
2046 server = tcon->ses->server;
2048 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2049 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2051 d_drop(direntry); /* for time being always refresh inode info */
2055 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2056 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2057 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2058 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2060 if (rc != -EOPNOTSUPP)
2063 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2065 if (!server->ops->mkdir) {
2070 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2071 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2073 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2078 /* TODO: skip this for smb2/smb3 */
2079 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2083 * Force revalidate to get parent dir info when needed since cached
2084 * attributes are invalid now.
2086 CIFS_I(inode)->time = 0;
2087 free_dentry_path(page);
2089 cifs_put_tlink(tlink);
2093 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2097 struct cifs_sb_info *cifs_sb;
2098 struct tcon_link *tlink;
2099 struct cifs_tcon *tcon;
2100 struct TCP_Server_Info *server;
2101 const char *full_path;
2102 void *page = alloc_dentry_path();
2103 struct cifsInodeInfo *cifsInode;
2105 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2109 full_path = build_path_from_dentry(direntry, page);
2110 if (IS_ERR(full_path)) {
2111 rc = PTR_ERR(full_path);
2115 cifs_sb = CIFS_SB(inode->i_sb);
2116 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2121 tlink = cifs_sb_tlink(cifs_sb);
2122 if (IS_ERR(tlink)) {
2123 rc = PTR_ERR(tlink);
2126 tcon = tlink_tcon(tlink);
2127 server = tcon->ses->server;
2129 if (!server->ops->rmdir) {
2131 cifs_put_tlink(tlink);
2135 if (tcon->nodelete) {
2137 cifs_put_tlink(tlink);
2141 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2142 cifs_put_tlink(tlink);
2145 spin_lock(&d_inode(direntry)->i_lock);
2146 i_size_write(d_inode(direntry), 0);
2147 clear_nlink(d_inode(direntry));
2148 spin_unlock(&d_inode(direntry)->i_lock);
2151 cifsInode = CIFS_I(d_inode(direntry));
2152 /* force revalidate to go get info when needed */
2153 cifsInode->time = 0;
2155 cifsInode = CIFS_I(inode);
2157 * Force revalidate to get parent dir info when needed since cached
2158 * attributes are invalid now.
2160 cifsInode->time = 0;
2162 inode_set_ctime_current(d_inode(direntry));
2163 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2166 free_dentry_path(page);
2172 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2173 const char *from_path, struct dentry *to_dentry,
2174 const char *to_path)
2176 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2177 struct tcon_link *tlink;
2178 struct cifs_tcon *tcon;
2179 struct TCP_Server_Info *server;
2180 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2181 struct cifs_fid fid;
2182 struct cifs_open_parms oparms;
2184 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2187 tlink = cifs_sb_tlink(cifs_sb);
2189 return PTR_ERR(tlink);
2190 tcon = tlink_tcon(tlink);
2191 server = tcon->ses->server;
2193 if (!server->ops->rename)
2196 /* try path-based rename first */
2197 rc = server->ops->rename(xid, tcon, from_dentry,
2198 from_path, to_path, cifs_sb);
2201 * Don't bother with rename by filehandle unless file is busy and
2202 * source. Note that cross directory moves do not work with
2203 * rename by filehandle to various Windows servers.
2205 if (rc == 0 || rc != -EBUSY)
2206 goto do_rename_exit;
2208 /* Don't fall back to using SMB on SMB 2+ mount */
2209 if (server->vals->protocol_id != 0)
2210 goto do_rename_exit;
2212 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2213 /* open-file renames don't work across directories */
2214 if (to_dentry->d_parent != from_dentry->d_parent)
2215 goto do_rename_exit;
2217 oparms = (struct cifs_open_parms) {
2220 /* open the file to be renamed -- we need DELETE perms */
2221 .desired_access = DELETE,
2222 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2223 .disposition = FILE_OPEN,
2228 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2230 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2231 (const char *) to_dentry->d_name.name,
2232 cifs_sb->local_nls, cifs_remap(cifs_sb));
2233 CIFSSMBClose(xid, tcon, fid.netfid);
2235 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2238 d_move(from_dentry, to_dentry);
2239 cifs_put_tlink(tlink);
2244 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2245 struct dentry *source_dentry, struct inode *target_dir,
2246 struct dentry *target_dentry, unsigned int flags)
2248 const char *from_name, *to_name;
2249 void *page1, *page2;
2250 struct cifs_sb_info *cifs_sb;
2251 struct tcon_link *tlink;
2252 struct cifs_tcon *tcon;
2255 int retry_count = 0;
2256 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2257 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2258 FILE_UNIX_BASIC_INFO *info_buf_target;
2259 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2261 if (flags & ~RENAME_NOREPLACE)
2264 cifs_sb = CIFS_SB(source_dir->i_sb);
2265 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2268 tlink = cifs_sb_tlink(cifs_sb);
2270 return PTR_ERR(tlink);
2271 tcon = tlink_tcon(tlink);
2273 page1 = alloc_dentry_path();
2274 page2 = alloc_dentry_path();
2277 from_name = build_path_from_dentry(source_dentry, page1);
2278 if (IS_ERR(from_name)) {
2279 rc = PTR_ERR(from_name);
2280 goto cifs_rename_exit;
2283 to_name = build_path_from_dentry(target_dentry, page2);
2284 if (IS_ERR(to_name)) {
2285 rc = PTR_ERR(to_name);
2286 goto cifs_rename_exit;
2289 cifs_close_deferred_file_under_dentry(tcon, from_name);
2290 if (d_inode(target_dentry) != NULL)
2291 cifs_close_deferred_file_under_dentry(tcon, to_name);
2293 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2296 if (rc == -EACCES) {
2297 while (retry_count < 3) {
2298 cifs_close_all_deferred_files(tcon);
2299 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2308 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2310 if (flags & RENAME_NOREPLACE)
2311 goto cifs_rename_exit;
2313 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2314 if (rc == -EEXIST && tcon->unix_ext) {
2316 * Are src and dst hardlinks of same inode? We can only tell
2317 * with unix extensions enabled.
2320 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2322 if (info_buf_source == NULL) {
2324 goto cifs_rename_exit;
2327 info_buf_target = info_buf_source + 1;
2328 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2331 cifs_remap(cifs_sb));
2335 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2338 cifs_remap(cifs_sb));
2340 if (tmprc == 0 && (info_buf_source->UniqueId ==
2341 info_buf_target->UniqueId)) {
2342 /* same file, POSIX says that this is a noop */
2344 goto cifs_rename_exit;
2348 * else ... BB we could add the same check for Windows by
2349 * checking the UniqueId via FILE_INTERNAL_INFO
2353 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2355 /* Try unlinking the target dentry if it's not negative */
2356 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2357 if (d_is_dir(target_dentry))
2358 tmprc = cifs_rmdir(target_dir, target_dentry);
2360 tmprc = cifs_unlink(target_dir, target_dentry);
2362 goto cifs_rename_exit;
2363 rc = cifs_do_rename(xid, source_dentry, from_name,
2364 target_dentry, to_name);
2367 /* force revalidate to go get info when needed */
2368 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2371 kfree(info_buf_source);
2372 free_dentry_path(page2);
2373 free_dentry_path(page1);
2375 cifs_put_tlink(tlink);
2380 cifs_dentry_needs_reval(struct dentry *dentry)
2382 struct inode *inode = d_inode(dentry);
2383 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2384 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2385 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2386 struct cached_fid *cfid = NULL;
2388 if (cifs_i->time == 0)
2391 if (CIFS_CACHE_READ(cifs_i))
2394 if (!lookupCacheEnabled)
2397 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2398 spin_lock(&cfid->fid_lock);
2399 if (cfid->time && cifs_i->time > cfid->time) {
2400 spin_unlock(&cfid->fid_lock);
2401 close_cached_dir(cfid);
2404 spin_unlock(&cfid->fid_lock);
2405 close_cached_dir(cfid);
2408 * depending on inode type, check if attribute caching disabled for
2409 * files or directories
2411 if (S_ISDIR(inode->i_mode)) {
2412 if (!cifs_sb->ctx->acdirmax)
2414 if (!time_in_range(jiffies, cifs_i->time,
2415 cifs_i->time + cifs_sb->ctx->acdirmax))
2418 if (!cifs_sb->ctx->acregmax)
2420 if (!time_in_range(jiffies, cifs_i->time,
2421 cifs_i->time + cifs_sb->ctx->acregmax))
2425 /* hardlinked files w/ noserverino get "special" treatment */
2426 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2427 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2434 * Zap the cache. Called when invalid_mapping flag is set.
2437 cifs_invalidate_mapping(struct inode *inode)
2441 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2442 rc = invalidate_inode_pages2(inode->i_mapping);
2444 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2445 __func__, inode, rc);
2452 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2454 * @key: currently unused
2455 * @mode: the task state to sleep in
2458 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2461 if (signal_pending_state(mode, current))
2462 return -ERESTARTSYS;
2467 cifs_revalidate_mapping(struct inode *inode)
2470 unsigned long *flags = &CIFS_I(inode)->flags;
2471 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2473 /* swapfiles are not supposed to be shared */
2474 if (IS_SWAPFILE(inode))
2477 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2478 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2482 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2483 /* for cache=singleclient, do not invalidate */
2484 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2485 goto skip_invalidate;
2487 rc = cifs_invalidate_mapping(inode);
2489 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2493 clear_bit_unlock(CIFS_INO_LOCK, flags);
2494 smp_mb__after_atomic();
2495 wake_up_bit(flags, CIFS_INO_LOCK);
2501 cifs_zap_mapping(struct inode *inode)
2503 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2504 return cifs_revalidate_mapping(inode);
2507 int cifs_revalidate_file_attr(struct file *filp)
2510 struct dentry *dentry = file_dentry(filp);
2511 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2512 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2513 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2515 if (!cifs_dentry_needs_reval(dentry))
2518 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2519 if (tlink_tcon(cfile->tlink)->unix_ext)
2520 rc = cifs_get_file_info_unix(filp);
2522 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2523 rc = cifs_get_file_info(filp);
2528 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2532 struct inode *inode = d_inode(dentry);
2533 struct super_block *sb = dentry->d_sb;
2534 const char *full_path;
2541 if (!cifs_dentry_needs_reval(dentry))
2546 page = alloc_dentry_path();
2547 full_path = build_path_from_dentry(dentry, page);
2548 if (IS_ERR(full_path)) {
2549 rc = PTR_ERR(full_path);
2553 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2554 full_path, inode, inode->i_count.counter,
2555 dentry, cifs_get_time(dentry), jiffies);
2558 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2559 rc = smb311_posix_get_inode_info(&inode, full_path,
2561 } else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2562 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2564 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2567 if (rc == -EAGAIN && count++ < 10)
2570 free_dentry_path(page);
2576 int cifs_revalidate_file(struct file *filp)
2579 struct inode *inode = file_inode(filp);
2581 rc = cifs_revalidate_file_attr(filp);
2585 return cifs_revalidate_mapping(inode);
2588 /* revalidate a dentry's inode attributes */
2589 int cifs_revalidate_dentry(struct dentry *dentry)
2592 struct inode *inode = d_inode(dentry);
2594 rc = cifs_revalidate_dentry_attr(dentry);
2598 return cifs_revalidate_mapping(inode);
2601 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2602 struct kstat *stat, u32 request_mask, unsigned int flags)
2604 struct dentry *dentry = path->dentry;
2605 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2606 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2607 struct inode *inode = d_inode(dentry);
2610 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2614 * We need to be sure that all dirty pages are written and the server
2615 * has actual ctime, mtime and file length.
2617 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2618 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2619 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2620 rc = filemap_fdatawait(inode->i_mapping);
2622 mapping_set_error(inode->i_mapping, rc);
2627 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2628 CIFS_I(inode)->time = 0; /* force revalidate */
2631 * If the caller doesn't require syncing, only sync if
2632 * necessary (e.g. due to earlier truncate or setattr
2633 * invalidating the cached metadata)
2635 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2636 (CIFS_I(inode)->time == 0)) {
2637 rc = cifs_revalidate_dentry_attr(dentry);
2642 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2643 stat->blksize = cifs_sb->ctx->bsize;
2644 stat->ino = CIFS_I(inode)->uniqueid;
2646 /* old CIFS Unix Extensions doesn't return create time */
2647 if (CIFS_I(inode)->createtime) {
2648 stat->result_mask |= STATX_BTIME;
2650 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2653 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2654 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2655 stat->attributes |= STATX_ATTR_COMPRESSED;
2656 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2657 stat->attributes |= STATX_ATTR_ENCRYPTED;
2660 * If on a multiuser mount without unix extensions or cifsacl being
2661 * enabled, and the admin hasn't overridden them, set the ownership
2662 * to the fsuid/fsgid of the current process.
2664 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2665 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2667 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2668 stat->uid = current_fsuid();
2669 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2670 stat->gid = current_fsgid();
2675 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2678 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2679 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2680 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2681 struct TCP_Server_Info *server = tcon->ses->server;
2682 struct cifsFileInfo *cfile;
2685 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2689 * We need to be sure that all dirty pages are written as they
2690 * might fill holes on the server.
2692 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2693 inode->i_mapping->nrpages != 0) {
2694 rc = filemap_fdatawait(inode->i_mapping);
2696 mapping_set_error(inode->i_mapping, rc);
2701 cfile = find_readable_file(cifs_i, false);
2705 if (server->ops->fiemap) {
2706 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2707 cifsFileInfo_put(cfile);
2711 cifsFileInfo_put(cfile);
2715 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2717 pgoff_t index = from >> PAGE_SHIFT;
2718 unsigned offset = from & (PAGE_SIZE - 1);
2722 page = grab_cache_page(mapping, index);
2726 zero_user_segment(page, offset, PAGE_SIZE);
2732 void cifs_setsize(struct inode *inode, loff_t offset)
2734 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2736 spin_lock(&inode->i_lock);
2737 i_size_write(inode, offset);
2738 spin_unlock(&inode->i_lock);
2740 /* Cached inode must be refreshed on truncate */
2742 truncate_pagecache(inode, offset);
2746 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2747 unsigned int xid, const char *full_path, struct dentry *dentry)
2750 struct cifsFileInfo *open_file;
2751 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2752 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2753 struct tcon_link *tlink = NULL;
2754 struct cifs_tcon *tcon = NULL;
2755 struct TCP_Server_Info *server;
2758 * To avoid spurious oplock breaks from server, in the case of
2759 * inodes that we already have open, avoid doing path based
2760 * setting of file size if we can do it by handle.
2761 * This keeps our caching token (oplock) and avoids timeouts
2762 * when the local oplock break takes longer to flush
2763 * writebehind data than the SMB timeout for the SetPathInfo
2764 * request would allow
2766 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2768 tcon = tlink_tcon(open_file->tlink);
2769 server = tcon->ses->server;
2770 if (server->ops->set_file_size)
2771 rc = server->ops->set_file_size(xid, tcon, open_file,
2772 attrs->ia_size, false);
2775 cifsFileInfo_put(open_file);
2776 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2784 tlink = cifs_sb_tlink(cifs_sb);
2786 return PTR_ERR(tlink);
2787 tcon = tlink_tcon(tlink);
2788 server = tcon->ses->server;
2792 * Set file size by pathname rather than by handle either because no
2793 * valid, writeable file handle for it was found or because there was
2794 * an error setting it by handle.
2796 if (server->ops->set_path_size)
2797 rc = server->ops->set_path_size(xid, tcon, full_path,
2798 attrs->ia_size, cifs_sb, false, dentry);
2801 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2804 cifs_put_tlink(tlink);
2808 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2809 cifs_setsize(inode, attrs->ia_size);
2811 * i_blocks is not related to (i_size / i_blksize), but instead
2812 * 512 byte (2**9) size is required for calculating num blocks.
2813 * Until we can query the server for actual allocation size,
2814 * this is best estimate we have for blocks allocated for a file
2815 * Number of blocks must be rounded up so size 1 is not 0 blocks
2817 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2820 * The man page of truncate says if the size changed,
2821 * then the st_ctime and st_mtime fields for the file
2824 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2825 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2827 cifs_truncate_page(inode->i_mapping, inode->i_size);
2833 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2835 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2839 const char *full_path;
2840 void *page = alloc_dentry_path();
2841 struct inode *inode = d_inode(direntry);
2842 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2843 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2844 struct tcon_link *tlink;
2845 struct cifs_tcon *pTcon;
2846 struct cifs_unix_set_info_args *args = NULL;
2847 struct cifsFileInfo *open_file;
2849 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2850 direntry, attrs->ia_valid);
2854 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2855 attrs->ia_valid |= ATTR_FORCE;
2857 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2861 full_path = build_path_from_dentry(direntry, page);
2862 if (IS_ERR(full_path)) {
2863 rc = PTR_ERR(full_path);
2868 * Attempt to flush data before changing attributes. We need to do
2869 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2870 * ownership or mode then we may also need to do this. Here, we take
2871 * the safe way out and just do the flush on all setattr requests. If
2872 * the flush returns error, store it to report later and continue.
2874 * BB: This should be smarter. Why bother flushing pages that
2875 * will be truncated anyway? Also, should we error out here if
2876 * the flush returns error?
2878 rc = filemap_write_and_wait(inode->i_mapping);
2879 if (is_interrupt_error(rc)) {
2884 mapping_set_error(inode->i_mapping, rc);
2887 if (attrs->ia_valid & ATTR_SIZE) {
2888 rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
2893 /* skip mode change if it's just for clearing setuid/setgid */
2894 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2895 attrs->ia_valid &= ~ATTR_MODE;
2897 args = kmalloc(sizeof(*args), GFP_KERNEL);
2903 /* set up the struct */
2904 if (attrs->ia_valid & ATTR_MODE)
2905 args->mode = attrs->ia_mode;
2907 args->mode = NO_CHANGE_64;
2909 if (attrs->ia_valid & ATTR_UID)
2910 args->uid = attrs->ia_uid;
2912 args->uid = INVALID_UID; /* no change */
2914 if (attrs->ia_valid & ATTR_GID)
2915 args->gid = attrs->ia_gid;
2917 args->gid = INVALID_GID; /* no change */
2919 if (attrs->ia_valid & ATTR_ATIME)
2920 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2922 args->atime = NO_CHANGE_64;
2924 if (attrs->ia_valid & ATTR_MTIME)
2925 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2927 args->mtime = NO_CHANGE_64;
2929 if (attrs->ia_valid & ATTR_CTIME)
2930 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2932 args->ctime = NO_CHANGE_64;
2935 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2937 u16 nfid = open_file->fid.netfid;
2938 u32 npid = open_file->pid;
2939 pTcon = tlink_tcon(open_file->tlink);
2940 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2941 cifsFileInfo_put(open_file);
2943 tlink = cifs_sb_tlink(cifs_sb);
2944 if (IS_ERR(tlink)) {
2945 rc = PTR_ERR(tlink);
2948 pTcon = tlink_tcon(tlink);
2949 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2951 cifs_remap(cifs_sb));
2952 cifs_put_tlink(tlink);
2958 if ((attrs->ia_valid & ATTR_SIZE) &&
2959 attrs->ia_size != i_size_read(inode)) {
2960 truncate_setsize(inode, attrs->ia_size);
2961 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2962 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2965 setattr_copy(&nop_mnt_idmap, inode, attrs);
2966 mark_inode_dirty(inode);
2968 /* force revalidate when any of these times are set since some
2969 of the fs types (eg ext3, fat) do not have fine enough
2970 time granularity to match protocol, and we do not have a
2971 a way (yet) to query the server fs's time granularity (and
2972 whether it rounds times down).
2974 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2975 cifsInode->time = 0;
2978 free_dentry_path(page);
2982 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2985 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2988 kuid_t uid = INVALID_UID;
2989 kgid_t gid = INVALID_GID;
2990 struct inode *inode = d_inode(direntry);
2991 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2992 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2993 struct cifsFileInfo *wfile;
2994 struct cifs_tcon *tcon;
2995 const char *full_path;
2996 void *page = alloc_dentry_path();
2999 __u64 mode = NO_CHANGE_64;
3003 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3004 direntry, attrs->ia_valid);
3006 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3007 attrs->ia_valid |= ATTR_FORCE;
3009 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3011 goto cifs_setattr_exit;
3013 full_path = build_path_from_dentry(direntry, page);
3014 if (IS_ERR(full_path)) {
3015 rc = PTR_ERR(full_path);
3016 goto cifs_setattr_exit;
3020 * Attempt to flush data before changing attributes. We need to do
3021 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3022 * returns error, store it to report later and continue.
3024 * BB: This should be smarter. Why bother flushing pages that
3025 * will be truncated anyway? Also, should we error out here if
3026 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3028 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3029 rc = filemap_write_and_wait(inode->i_mapping);
3030 if (is_interrupt_error(rc)) {
3032 goto cifs_setattr_exit;
3034 mapping_set_error(inode->i_mapping, rc);
3039 if ((attrs->ia_valid & ATTR_MTIME) &&
3040 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3041 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3043 tcon = tlink_tcon(wfile->tlink);
3044 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3045 cifsFileInfo_put(wfile);
3047 goto cifs_setattr_exit;
3048 } else if (rc != -EBADF)
3049 goto cifs_setattr_exit;
3054 if (attrs->ia_valid & ATTR_SIZE) {
3055 rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
3057 goto cifs_setattr_exit;
3060 if (attrs->ia_valid & ATTR_UID)
3061 uid = attrs->ia_uid;
3063 if (attrs->ia_valid & ATTR_GID)
3064 gid = attrs->ia_gid;
3066 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3067 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3068 if (uid_valid(uid) || gid_valid(gid)) {
3069 mode = NO_CHANGE_64;
3070 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3073 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3075 goto cifs_setattr_exit;
3079 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3080 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3082 /* skip mode change if it's just for clearing setuid/setgid */
3083 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3084 attrs->ia_valid &= ~ATTR_MODE;
3086 if (attrs->ia_valid & ATTR_MODE) {
3087 mode = attrs->ia_mode;
3089 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3090 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3091 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3092 INVALID_UID, INVALID_GID);
3094 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3096 goto cifs_setattr_exit;
3100 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3101 * Pick up the actual mode bits that were set.
3103 if (mode != attrs->ia_mode)
3104 attrs->ia_mode = mode;
3106 if (((mode & S_IWUGO) == 0) &&
3107 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3109 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3111 /* fix up mode if we're not using dynperm */
3112 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3113 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3114 } else if ((mode & S_IWUGO) &&
3115 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3117 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3118 /* Attributes of 0 are ignored */
3120 dosattr |= ATTR_NORMAL;
3122 /* reset local inode permissions to normal */
3123 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3124 attrs->ia_mode &= ~(S_IALLUGO);
3125 if (S_ISDIR(inode->i_mode))
3127 cifs_sb->ctx->dir_mode;
3130 cifs_sb->ctx->file_mode;
3132 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3133 /* ignore mode change - ATTR_READONLY hasn't changed */
3134 attrs->ia_valid &= ~ATTR_MODE;
3138 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3139 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3140 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3141 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3143 /* Even if error on time set, no sense failing the call if
3144 the server would set the time to a reasonable value anyway,
3145 and this check ensures that we are not being called from
3146 sys_utimes in which case we ought to fail the call back to
3147 the user when the server rejects the call */
3148 if ((rc) && (attrs->ia_valid &
3149 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3153 /* do not need local check to inode_check_ok since the server does
3156 goto cifs_setattr_exit;
3158 if ((attrs->ia_valid & ATTR_SIZE) &&
3159 attrs->ia_size != i_size_read(inode)) {
3160 truncate_setsize(inode, attrs->ia_size);
3161 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3162 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3165 setattr_copy(&nop_mnt_idmap, inode, attrs);
3166 mark_inode_dirty(inode);
3170 free_dentry_path(page);
3175 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3176 struct iattr *attrs)
3178 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3179 int rc, retries = 0;
3180 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3181 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3182 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3184 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3188 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3189 if (pTcon->unix_ext)
3190 rc = cifs_setattr_unix(direntry, attrs);
3192 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3193 rc = cifs_setattr_nounix(direntry, attrs);
3195 } while (is_retryable_error(rc) && retries < 2);
3197 /* BB: add cifs_setattr_legacy for really old servers */