1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
29 static void cifs_set_ops(struct inode *inode)
31 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
33 switch (inode->i_mode & S_IFMT) {
35 inode->i_op = &cifs_file_inode_ops;
36 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
37 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
38 inode->i_fop = &cifs_file_direct_nobrl_ops;
40 inode->i_fop = &cifs_file_direct_ops;
41 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
42 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
43 inode->i_fop = &cifs_file_strict_nobrl_ops;
45 inode->i_fop = &cifs_file_strict_ops;
46 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
47 inode->i_fop = &cifs_file_nobrl_ops;
48 else { /* not direct, send byte range locks */
49 inode->i_fop = &cifs_file_ops;
52 /* check if server can support readpages */
53 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
54 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
55 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
57 inode->i_data.a_ops = &cifs_addr_ops;
60 #ifdef CONFIG_CIFS_DFS_UPCALL
61 if (IS_AUTOMOUNT(inode)) {
62 inode->i_op = &cifs_dfs_referral_inode_operations;
64 #else /* NO DFS support, treat as a directory */
67 inode->i_op = &cifs_dir_inode_ops;
68 inode->i_fop = &cifs_dir_ops;
72 inode->i_op = &cifs_symlink_inode_ops;
75 init_special_inode(inode, inode->i_mode, inode->i_rdev);
80 /* check inode attributes against fattr. If they don't match, tag the
81 * inode for cache invalidation
84 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
86 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
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 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
107 cifs_i->server_eof == fattr->cf_eof) {
108 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
109 __func__, cifs_i->uniqueid);
113 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
114 __func__, cifs_i->uniqueid);
115 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
119 * copy nlink to the inode, unless it wasn't provided. Provide
120 * sane values if we don't have an existing one and none was provided
123 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
126 * if we're in a situation where we can't trust what we
127 * got from the server (readdir, some non-unix cases)
128 * fake reasonable values
130 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
131 /* only provide fake values on a new inode */
132 if (inode->i_state & I_NEW) {
133 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
141 /* we trust the server, so update it */
142 set_nlink(inode, fattr->cf_nlink);
145 /* populate an inode with info from a cifs_fattr struct */
147 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
149 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
150 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
152 if (!(inode->i_state & I_NEW) &&
153 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
154 CIFS_I(inode)->time = 0; /* force reval */
158 cifs_revalidate_cache(inode, fattr);
160 spin_lock(&inode->i_lock);
161 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
162 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
163 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
164 /* we do not want atime to be less than mtime, it broke some apps */
165 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
166 inode->i_atime = fattr->cf_mtime;
168 inode->i_atime = fattr->cf_atime;
169 inode->i_mtime = fattr->cf_mtime;
170 inode->i_ctime = fattr->cf_ctime;
171 inode->i_rdev = fattr->cf_rdev;
172 cifs_nlink_fattr_to_inode(inode, fattr);
173 inode->i_uid = fattr->cf_uid;
174 inode->i_gid = fattr->cf_gid;
176 /* if dynperm is set, don't clobber existing mode */
177 if (inode->i_state & I_NEW ||
178 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
179 inode->i_mode = fattr->cf_mode;
181 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
183 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
186 cifs_i->time = jiffies;
188 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
189 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
191 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
193 cifs_i->server_eof = fattr->cf_eof;
195 * Can't safely change the file size here if the client is writing to
196 * it due to potential races.
198 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
199 i_size_write(inode, fattr->cf_eof);
202 * i_blocks is not related to (i_size / i_blksize),
203 * but instead 512 byte (2**9) size is required for
204 * calculating num blocks.
206 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
208 spin_unlock(&inode->i_lock);
210 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
211 inode->i_flags |= S_AUTOMOUNT;
212 if (inode->i_state & I_NEW)
218 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
220 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
222 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
225 fattr->cf_uniqueid = iunique(sb, ROOT_I);
228 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
230 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
231 struct cifs_sb_info *cifs_sb)
233 memset(fattr, 0, sizeof(*fattr));
234 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
235 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
236 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
238 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
239 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
240 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
241 /* old POSIX extensions don't get create time */
243 fattr->cf_mode = le64_to_cpu(info->Permissions);
246 * Since we set the inode type below we need to mask off
247 * to avoid strange results if bits set above.
249 fattr->cf_mode &= ~S_IFMT;
250 switch (le32_to_cpu(info->Type)) {
252 fattr->cf_mode |= S_IFREG;
253 fattr->cf_dtype = DT_REG;
256 fattr->cf_mode |= S_IFLNK;
257 fattr->cf_dtype = DT_LNK;
260 fattr->cf_mode |= S_IFDIR;
261 fattr->cf_dtype = DT_DIR;
264 fattr->cf_mode |= S_IFCHR;
265 fattr->cf_dtype = DT_CHR;
266 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
267 le64_to_cpu(info->DevMinor) & MINORMASK);
270 fattr->cf_mode |= S_IFBLK;
271 fattr->cf_dtype = DT_BLK;
272 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
273 le64_to_cpu(info->DevMinor) & MINORMASK);
276 fattr->cf_mode |= S_IFIFO;
277 fattr->cf_dtype = DT_FIFO;
280 fattr->cf_mode |= S_IFSOCK;
281 fattr->cf_dtype = DT_SOCK;
284 /* safest to call it a file if we do not know */
285 fattr->cf_mode |= S_IFREG;
286 fattr->cf_dtype = DT_REG;
287 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
291 fattr->cf_uid = cifs_sb->ctx->linux_uid;
292 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
293 u64 id = le64_to_cpu(info->Uid);
294 if (id < ((uid_t)-1)) {
295 kuid_t uid = make_kuid(&init_user_ns, id);
301 fattr->cf_gid = cifs_sb->ctx->linux_gid;
302 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
303 u64 id = le64_to_cpu(info->Gid);
304 if (id < ((gid_t)-1)) {
305 kgid_t gid = make_kgid(&init_user_ns, id);
311 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
315 * Fill a cifs_fattr struct with fake inode info.
317 * Needed to setup cifs_fattr data for the directory which is the
318 * junction to the new submount (ie to setup the fake directory
319 * which represents a DFS referral).
322 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
324 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
326 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
328 memset(fattr, 0, sizeof(*fattr));
329 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
330 fattr->cf_uid = cifs_sb->ctx->linux_uid;
331 fattr->cf_gid = cifs_sb->ctx->linux_gid;
332 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
333 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
335 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
339 cifs_get_file_info_unix(struct file *filp)
343 FILE_UNIX_BASIC_INFO find_data;
344 struct cifs_fattr fattr;
345 struct inode *inode = file_inode(filp);
346 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
347 struct cifsFileInfo *cfile = filp->private_data;
348 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
351 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
353 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
354 } else if (rc == -EREMOTE) {
355 cifs_create_dfs_fattr(&fattr, inode->i_sb);
358 goto cifs_gfiunix_out;
360 rc = cifs_fattr_to_inode(inode, &fattr);
367 int cifs_get_inode_info_unix(struct inode **pinode,
368 const unsigned char *full_path,
369 struct super_block *sb, unsigned int xid)
372 FILE_UNIX_BASIC_INFO find_data;
373 struct cifs_fattr fattr;
374 struct cifs_tcon *tcon;
375 struct tcon_link *tlink;
376 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
378 cifs_dbg(FYI, "Getting info on %s\n", full_path);
380 tlink = cifs_sb_tlink(cifs_sb);
382 return PTR_ERR(tlink);
383 tcon = tlink_tcon(tlink);
385 /* could have done a find first instead but this returns more info */
386 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
387 cifs_sb->local_nls, cifs_remap(cifs_sb));
388 cifs_put_tlink(tlink);
391 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
392 } else if (rc == -EREMOTE) {
393 cifs_create_dfs_fattr(&fattr, sb);
399 /* check for Minshall+French symlinks */
400 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
401 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
404 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
407 if (*pinode == NULL) {
409 cifs_fill_uniqueid(sb, &fattr);
410 *pinode = cifs_iget(sb, &fattr);
414 /* we already have inode, update it */
416 /* if uniqueid is different, return error */
417 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
418 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
419 CIFS_I(*pinode)->time = 0; /* force reval */
424 /* if filetype is different, return error */
425 rc = cifs_fattr_to_inode(*pinode, &fattr);
433 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
434 struct cifs_sb_info *cifs_sb, unsigned int xid)
438 struct tcon_link *tlink;
439 struct cifs_tcon *tcon;
441 struct cifs_open_parms oparms;
442 struct cifs_io_parms io_parms = {0};
444 unsigned int bytes_read;
446 int buf_type = CIFS_NO_BUFFER;
450 fattr->cf_mode &= ~S_IFMT;
452 if (fattr->cf_eof == 0) {
453 fattr->cf_mode |= S_IFIFO;
454 fattr->cf_dtype = DT_FIFO;
456 } else if (fattr->cf_eof < 8) {
457 fattr->cf_mode |= S_IFREG;
458 fattr->cf_dtype = DT_REG;
459 return -EINVAL; /* EOPNOTSUPP? */
462 tlink = cifs_sb_tlink(cifs_sb);
464 return PTR_ERR(tlink);
465 tcon = tlink_tcon(tlink);
468 oparms.cifs_sb = cifs_sb;
469 oparms.desired_access = GENERIC_READ;
470 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
471 oparms.disposition = FILE_OPEN;
474 oparms.reconnect = false;
476 if (tcon->ses->server->oplocks)
480 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
482 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
483 cifs_put_tlink(tlink);
488 io_parms.netfid = fid.netfid;
489 io_parms.pid = current->tgid;
490 io_parms.tcon = tcon;
492 io_parms.length = 24;
494 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
495 &bytes_read, &pbuf, &buf_type);
496 if ((rc == 0) && (bytes_read >= 8)) {
497 if (memcmp("IntxBLK", pbuf, 8) == 0) {
498 cifs_dbg(FYI, "Block device\n");
499 fattr->cf_mode |= S_IFBLK;
500 fattr->cf_dtype = DT_BLK;
501 if (bytes_read == 24) {
502 /* we have enough to decode dev num */
503 __u64 mjr; /* major */
504 __u64 mnr; /* minor */
505 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
506 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
507 fattr->cf_rdev = MKDEV(mjr, mnr);
509 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
510 cifs_dbg(FYI, "Char device\n");
511 fattr->cf_mode |= S_IFCHR;
512 fattr->cf_dtype = DT_CHR;
513 if (bytes_read == 24) {
514 /* we have enough to decode dev num */
515 __u64 mjr; /* major */
516 __u64 mnr; /* minor */
517 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
518 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
519 fattr->cf_rdev = MKDEV(mjr, mnr);
521 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
522 cifs_dbg(FYI, "Symlink\n");
523 fattr->cf_mode |= S_IFLNK;
524 fattr->cf_dtype = DT_LNK;
526 fattr->cf_mode |= S_IFREG; /* file? */
527 fattr->cf_dtype = DT_REG;
531 fattr->cf_mode |= S_IFREG; /* then it is a file */
532 fattr->cf_dtype = DT_REG;
533 rc = -EOPNOTSUPP; /* or some unknown SFU type */
536 tcon->ses->server->ops->close(xid, tcon, &fid);
537 cifs_put_tlink(tlink);
541 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
544 * Fetch mode bits as provided by SFU.
546 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
548 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
549 struct cifs_sb_info *cifs_sb, unsigned int xid)
551 #ifdef CONFIG_CIFS_XATTR
555 struct tcon_link *tlink;
556 struct cifs_tcon *tcon;
558 tlink = cifs_sb_tlink(cifs_sb);
560 return PTR_ERR(tlink);
561 tcon = tlink_tcon(tlink);
563 if (tcon->ses->server->ops->query_all_EAs == NULL) {
564 cifs_put_tlink(tlink);
568 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
569 "SETFILEBITS", ea_value, 4 /* size of buf */,
571 cifs_put_tlink(tlink);
575 mode = le32_to_cpu(*((__le32 *)ea_value));
576 fattr->cf_mode &= ~SFBITS_MASK;
577 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
578 mode, fattr->cf_mode);
579 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
580 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
589 /* Fill a cifs_fattr struct with info from POSIX info struct */
591 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
592 struct super_block *sb, bool adjust_tz, bool symlink)
594 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
595 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
597 memset(fattr, 0, sizeof(*fattr));
599 /* no fattr->flags to set */
600 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
601 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
603 if (info->LastAccessTime)
604 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
606 ktime_get_coarse_real_ts64(&fattr->cf_atime);
608 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
609 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
612 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
613 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
616 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
617 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
618 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
620 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
621 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
622 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
623 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
626 fattr->cf_mode |= S_IFLNK;
627 fattr->cf_dtype = DT_LNK;
628 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
629 fattr->cf_mode |= S_IFDIR;
630 fattr->cf_dtype = DT_DIR;
632 fattr->cf_mode |= S_IFREG;
633 fattr->cf_dtype = DT_REG;
635 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
637 fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
638 fattr->cf_gid = cifs_sb->ctx->linux_gid;
640 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
641 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
645 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
647 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
648 struct super_block *sb, bool adjust_tz,
649 bool symlink, u32 reparse_tag)
651 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
652 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
654 memset(fattr, 0, sizeof(*fattr));
655 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
656 if (info->DeletePending)
657 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
659 if (info->LastAccessTime)
660 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
662 ktime_get_coarse_real_ts64(&fattr->cf_atime);
664 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
665 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
668 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
669 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
672 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
673 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
674 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
676 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
677 if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
678 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
679 fattr->cf_dtype = DT_LNK;
680 } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
681 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
682 fattr->cf_dtype = DT_FIFO;
683 } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
684 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
685 fattr->cf_dtype = DT_SOCK;
686 } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
687 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
688 fattr->cf_dtype = DT_CHR;
689 } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
690 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
691 fattr->cf_dtype = DT_BLK;
692 } else if (symlink) { /* TODO add more reparse tag checks */
693 fattr->cf_mode = S_IFLNK;
694 fattr->cf_dtype = DT_LNK;
695 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
696 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
697 fattr->cf_dtype = DT_DIR;
699 * Server can return wrong NumberOfLinks value for directories
700 * when Unix extensions are disabled - fake it.
703 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
705 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
706 fattr->cf_dtype = DT_REG;
708 /* clear write bits if ATTR_READONLY is set */
709 if (fattr->cf_cifsattrs & ATTR_READONLY)
710 fattr->cf_mode &= ~(S_IWUGO);
713 * Don't accept zero nlink from non-unix servers unless
714 * delete is pending. Instead mark it as unknown.
716 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
717 !info->DeletePending) {
718 cifs_dbg(VFS, "bogus file nlink value %u\n",
720 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
724 fattr->cf_uid = cifs_sb->ctx->linux_uid;
725 fattr->cf_gid = cifs_sb->ctx->linux_gid;
729 cifs_get_file_info(struct file *filp)
733 FILE_ALL_INFO find_data;
734 struct cifs_fattr fattr;
735 struct inode *inode = file_inode(filp);
736 struct cifsFileInfo *cfile = filp->private_data;
737 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
738 struct TCP_Server_Info *server = tcon->ses->server;
740 if (!server->ops->query_file_info)
744 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
747 /* TODO: add support to query reparse tag */
748 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
749 false, 0 /* no reparse tag */);
752 cifs_create_dfs_fattr(&fattr, inode->i_sb);
758 * FIXME: legacy server -- fall back to path-based call?
759 * for now, just skip revalidating and mark inode for
763 CIFS_I(inode)->time = 0;
770 * don't bother with SFU junk here -- just mark inode as needing
773 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
774 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
775 /* if filetype is different, return error */
776 rc = cifs_fattr_to_inode(inode, &fattr);
782 /* Simple function to return a 64 bit hash of string. Rarely called */
783 static __u64 simple_hashstr(const char *str)
785 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
789 hash = (hash + (__u64) *str++) * hash_mult;
795 * cifs_backup_query_path_info - SMB1 fallback code to get ino
797 * Fallback code to get file metadata when we don't have access to
798 * full_path (EACCES) and have backup creds.
800 * @xid: transaction id used to identify original request in logs
801 * @tcon: information about the server share we have mounted
802 * @sb: the superblock stores info such as disk space available
803 * @full_path: name of the file we are getting the metadata for
804 * @resp_buf: will be set to cifs resp buf and needs to be freed with
805 * cifs_buf_release() when done with @data
806 * @data: will be set to search info result buffer
809 cifs_backup_query_path_info(int xid,
810 struct cifs_tcon *tcon,
811 struct super_block *sb,
812 const char *full_path,
814 FILE_ALL_INFO **data)
816 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
817 struct cifs_search_info info = {0};
822 info.endOfSearch = false;
824 info.info_level = SMB_FIND_FILE_UNIX;
825 else if ((tcon->ses->capabilities &
826 tcon->ses->server->vals->cap_nt_find) == 0)
827 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
828 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
829 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
830 else /* no srvino useful for fallback to some netapp */
831 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
833 flags = CIFS_SEARCH_CLOSE_ALWAYS |
834 CIFS_SEARCH_CLOSE_AT_END |
835 CIFS_SEARCH_BACKUP_SEARCH;
837 rc = CIFSFindFirst(xid, tcon, full_path,
838 cifs_sb, NULL, flags, &info, false);
842 *resp_buf = (void *)info.ntwrk_buf_start;
843 *data = (FILE_ALL_INFO *)info.srch_entries_start;
848 cifs_set_fattr_ino(int xid,
849 struct cifs_tcon *tcon,
850 struct super_block *sb,
851 struct inode **inode,
852 const char *full_path,
854 struct cifs_fattr *fattr)
856 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
857 struct TCP_Server_Info *server = tcon->ses->server;
860 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
862 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
864 fattr->cf_uniqueid = iunique(sb, ROOT_I);
869 * If we have an inode pass a NULL tcon to ensure we don't
870 * make a round trip to the server. This only works for SMB2+.
872 rc = server->ops->get_srv_inum(xid,
873 *inode ? NULL : tcon,
879 * If that fails reuse existing ino or generate one
880 * and disable server ones
883 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
885 fattr->cf_uniqueid = iunique(sb, ROOT_I);
886 cifs_autodisable_serverino(cifs_sb);
891 /* If no errors, check for zero root inode (invalid) */
892 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
893 cifs_dbg(FYI, "Invalid (0) inodenum\n");
896 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
898 /* make an ino by hashing the UNC */
899 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
900 fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
905 static inline bool is_inode_cache_good(struct inode *ino)
907 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
911 cifs_get_inode_info(struct inode **inode,
912 const char *full_path,
913 FILE_ALL_INFO *in_data,
914 struct super_block *sb, int xid,
915 const struct cifs_fid *fid)
918 struct cifs_tcon *tcon;
919 struct TCP_Server_Info *server;
920 struct tcon_link *tlink;
921 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
922 bool adjust_tz = false;
923 struct cifs_fattr fattr = {0};
924 bool is_reparse_point = false;
925 FILE_ALL_INFO *data = in_data;
926 FILE_ALL_INFO *tmp_data = NULL;
927 void *smb1_backup_rsp_buf = NULL;
930 __u32 reparse_tag = 0;
932 tlink = cifs_sb_tlink(cifs_sb);
934 return PTR_ERR(tlink);
935 tcon = tlink_tcon(tlink);
936 server = tcon->ses->server;
939 * 1. Fetch file metadata if not provided (data)
943 if (is_inode_cache_good(*inode)) {
944 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
947 tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
952 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
954 &adjust_tz, &is_reparse_point);
959 * 2. Convert it to internal cifs metadata (fattr)
965 * If the file is a reparse point, it is more complicated
966 * since we have to check if its reparse tag matches a known
967 * special file type e.g. symlink or fifo or char etc.
969 if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
970 server->ops->query_reparse_tag) {
971 rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
972 full_path, &reparse_tag);
973 cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
975 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
976 is_reparse_point, reparse_tag);
979 /* DFS link, no metadata available on this server */
980 cifs_create_dfs_fattr(&fattr, sb);
985 * perm errors, try again with backup flags if possible
987 * For SMB2 and later the backup intent flag
988 * is already sent if needed on open and there
989 * is no path based FindFirst operation to use
992 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
993 /* for easier reading */
994 FILE_DIRECTORY_INFO *fdi;
995 SEARCH_ID_FULL_DIR_INFO *si;
997 rc = cifs_backup_query_path_info(xid, tcon, sb,
999 &smb1_backup_rsp_buf,
1004 fdi = (FILE_DIRECTORY_INFO *)data;
1005 si = (SEARCH_ID_FULL_DIR_INFO *)data;
1007 cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1008 fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1009 /* uniqueid set, skip get inum step */
1010 goto handle_mnt_opt;
1012 /* nothing we can do, bail out */
1017 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1022 * 3. Get or update inode number (fattr.cf_uniqueid)
1025 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1028 * 4. Tweak fattr based on mount options
1032 /* query for SFU type info if supported and needed */
1033 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1034 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1035 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1037 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1040 /* fill in 0777 bits from ACL */
1041 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1042 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1047 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1051 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1052 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1057 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1063 /* fill in remaining high mode bits e.g. SUID, VTX */
1064 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1065 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1067 /* check for Minshall+French symlinks */
1068 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1069 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1072 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1076 * 5. Update inode with final fattr data
1080 *inode = cifs_iget(sb, &fattr);
1084 /* we already have inode, update it */
1086 /* if uniqueid is different, return error */
1087 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1088 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1089 CIFS_I(*inode)->time = 0; /* force reval */
1093 /* if filetype is different, return error */
1094 rc = cifs_fattr_to_inode(*inode, &fattr);
1097 cifs_buf_release(smb1_backup_rsp_buf);
1098 cifs_put_tlink(tlink);
1104 smb311_posix_get_inode_info(struct inode **inode,
1105 const char *full_path,
1106 struct super_block *sb, unsigned int xid)
1108 struct cifs_tcon *tcon;
1109 struct tcon_link *tlink;
1110 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1111 bool adjust_tz = false;
1112 struct cifs_fattr fattr = {0};
1113 bool symlink = false;
1114 struct smb311_posix_qinfo *data = NULL;
1118 tlink = cifs_sb_tlink(cifs_sb);
1120 return PTR_ERR(tlink);
1121 tcon = tlink_tcon(tlink);
1124 * 1. Fetch file metadata
1127 if (is_inode_cache_good(*inode)) {
1128 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1131 data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1137 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1139 &adjust_tz, &symlink);
1142 * 2. Convert it to internal cifs metadata (fattr)
1147 smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1150 /* DFS link, no metadata available on this server */
1151 cifs_create_dfs_fattr(&fattr, sb);
1156 * For SMB2 and later the backup intent flag
1157 * is already sent if needed on open and there
1158 * is no path based FindFirst operation to use
1159 * to retry with so nothing we can do, bail out
1163 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1169 * 3. Tweak fattr based on mount options
1172 /* check for Minshall+French symlinks */
1173 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1174 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1177 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1181 * 4. Update inode with final fattr data
1185 *inode = cifs_iget(sb, &fattr);
1189 /* we already have inode, update it */
1191 /* if uniqueid is different, return error */
1192 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1193 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1194 CIFS_I(*inode)->time = 0; /* force reval */
1199 /* if filetype is different, return error */
1200 rc = cifs_fattr_to_inode(*inode, &fattr);
1203 cifs_put_tlink(tlink);
1209 static const struct inode_operations cifs_ipc_inode_ops = {
1210 .lookup = cifs_lookup,
1214 cifs_find_inode(struct inode *inode, void *opaque)
1216 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1218 /* don't match inode with different uniqueid */
1219 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1222 /* use createtime like an i_generation field */
1223 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1226 /* don't match inode of different type */
1227 if (inode_wrong_type(inode, fattr->cf_mode))
1230 /* if it's not a directory or has no dentries, then flag it */
1231 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1232 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1238 cifs_init_inode(struct inode *inode, void *opaque)
1240 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1242 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1243 CIFS_I(inode)->createtime = fattr->cf_createtime;
1248 * walk dentry list for an inode and report whether it has aliases that
1249 * are hashed. We use this to determine if a directory inode can actually
1253 inode_has_hashed_dentries(struct inode *inode)
1255 struct dentry *dentry;
1257 spin_lock(&inode->i_lock);
1258 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1259 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1260 spin_unlock(&inode->i_lock);
1264 spin_unlock(&inode->i_lock);
1268 /* Given fattrs, get a corresponding inode */
1270 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1273 struct inode *inode;
1276 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1278 /* hash down to 32-bits on 32-bit arch */
1279 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1281 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1283 /* was there a potentially problematic inode collision? */
1284 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1285 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1287 if (inode_has_hashed_dentries(inode)) {
1288 cifs_autodisable_serverino(CIFS_SB(sb));
1290 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1291 goto retry_iget5_locked;
1295 /* can't fail - see cifs_find_inode() */
1296 cifs_fattr_to_inode(inode, fattr);
1297 if (sb->s_flags & SB_NOATIME)
1298 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1299 if (inode->i_state & I_NEW) {
1300 inode->i_ino = hash;
1301 #ifdef CONFIG_CIFS_FSCACHE
1302 /* initialize per-inode cache cookie pointer */
1303 CIFS_I(inode)->fscache = NULL;
1305 unlock_new_inode(inode);
1312 /* gets root inode */
1313 struct inode *cifs_root_iget(struct super_block *sb)
1316 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1317 struct inode *inode = NULL;
1319 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1323 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1324 && cifs_sb->prepath) {
1325 len = strlen(cifs_sb->prepath);
1326 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1328 return ERR_PTR(-ENOMEM);
1330 memcpy(path+1, cifs_sb->prepath, len);
1332 path = kstrdup("", GFP_KERNEL);
1334 return ERR_PTR(-ENOMEM);
1338 if (tcon->unix_ext) {
1339 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1340 /* some servers mistakenly claim POSIX support */
1341 if (rc != -EOPNOTSUPP)
1343 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1344 tcon->unix_ext = false;
1347 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1348 if (tcon->posix_extensions)
1349 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1351 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1355 inode = ERR_PTR(rc);
1359 #ifdef CONFIG_CIFS_FSCACHE
1360 /* populate tcon->resource_id */
1361 tcon->resource_id = CIFS_I(inode)->uniqueid;
1364 if (rc && tcon->pipe) {
1365 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1366 spin_lock(&inode->i_lock);
1367 inode->i_mode |= S_IFDIR;
1368 set_nlink(inode, 2);
1369 inode->i_op = &cifs_ipc_inode_ops;
1370 inode->i_fop = &simple_dir_operations;
1371 inode->i_uid = cifs_sb->ctx->linux_uid;
1372 inode->i_gid = cifs_sb->ctx->linux_gid;
1373 spin_unlock(&inode->i_lock);
1376 inode = ERR_PTR(rc);
1386 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1387 const char *full_path, __u32 dosattr)
1389 bool set_time = false;
1390 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1391 struct TCP_Server_Info *server;
1392 FILE_BASIC_INFO info_buf;
1397 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1398 if (!server->ops->set_file_info)
1403 if (attrs->ia_valid & ATTR_ATIME) {
1405 info_buf.LastAccessTime =
1406 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1408 info_buf.LastAccessTime = 0;
1410 if (attrs->ia_valid & ATTR_MTIME) {
1412 info_buf.LastWriteTime =
1413 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1415 info_buf.LastWriteTime = 0;
1418 * Samba throws this field away, but windows may actually use it.
1419 * Do not set ctime unless other time stamps are changed explicitly
1420 * (i.e. by utimes()) since we would then have a mix of client and
1423 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1424 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1425 info_buf.ChangeTime =
1426 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1428 info_buf.ChangeTime = 0;
1430 info_buf.CreationTime = 0; /* don't change */
1431 info_buf.Attributes = cpu_to_le32(dosattr);
1433 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1437 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1438 * and rename it to a random name that hopefully won't conflict with
1442 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1443 const unsigned int xid)
1447 struct cifs_fid fid;
1448 struct cifs_open_parms oparms;
1449 struct inode *inode = d_inode(dentry);
1450 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1451 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1452 struct tcon_link *tlink;
1453 struct cifs_tcon *tcon;
1454 __u32 dosattr, origattr;
1455 FILE_BASIC_INFO *info_buf = NULL;
1457 tlink = cifs_sb_tlink(cifs_sb);
1459 return PTR_ERR(tlink);
1460 tcon = tlink_tcon(tlink);
1463 * We cannot rename the file if the server doesn't support
1464 * CAP_INFOLEVEL_PASSTHRU
1466 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1472 oparms.cifs_sb = cifs_sb;
1473 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1474 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1475 oparms.disposition = FILE_OPEN;
1476 oparms.path = full_path;
1478 oparms.reconnect = false;
1480 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1484 origattr = cifsInode->cifsAttrs;
1486 origattr |= ATTR_NORMAL;
1488 dosattr = origattr & ~ATTR_READONLY;
1490 dosattr |= ATTR_NORMAL;
1491 dosattr |= ATTR_HIDDEN;
1493 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1494 if (dosattr != origattr) {
1495 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1496 if (info_buf == NULL) {
1500 info_buf->Attributes = cpu_to_le32(dosattr);
1501 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1503 /* although we would like to mark the file hidden
1504 if that fails we will still try to rename it */
1506 cifsInode->cifsAttrs = dosattr;
1508 dosattr = origattr; /* since not able to change them */
1511 /* rename the file */
1512 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1514 cifs_remap(cifs_sb));
1520 /* try to set DELETE_ON_CLOSE */
1521 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1522 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1525 * some samba versions return -ENOENT when we try to set the
1526 * file disposition here. Likely a samba bug, but work around
1527 * it for now. This means that some cifsXXX files may hang
1528 * around after they shouldn't.
1530 * BB: remove this hack after more servers have the fix
1538 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1542 CIFSSMBClose(xid, tcon, fid.netfid);
1545 cifs_put_tlink(tlink);
1549 * reset everything back to the original state. Don't bother
1550 * dealing with errors here since we can't do anything about
1554 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1555 cifs_sb->local_nls, cifs_remap(cifs_sb));
1557 if (dosattr != origattr) {
1558 info_buf->Attributes = cpu_to_le32(origattr);
1559 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1561 cifsInode->cifsAttrs = origattr;
1567 /* copied from fs/nfs/dir.c with small changes */
1569 cifs_drop_nlink(struct inode *inode)
1571 spin_lock(&inode->i_lock);
1572 if (inode->i_nlink > 0)
1574 spin_unlock(&inode->i_lock);
1578 * If d_inode(dentry) is null (usually meaning the cached dentry
1579 * is a negative dentry) then we would attempt a standard SMB delete, but
1580 * if that fails we can not attempt the fall back mechanisms on EACCES
1581 * but will return the EACCES to the caller. Note that the VFS does not call
1582 * unlink on negative dentries currently.
1584 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1588 const char *full_path;
1590 struct inode *inode = d_inode(dentry);
1591 struct cifsInodeInfo *cifs_inode;
1592 struct super_block *sb = dir->i_sb;
1593 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1594 struct tcon_link *tlink;
1595 struct cifs_tcon *tcon;
1596 struct TCP_Server_Info *server;
1597 struct iattr *attrs = NULL;
1598 __u32 dosattr = 0, origattr = 0;
1600 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1602 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1605 tlink = cifs_sb_tlink(cifs_sb);
1607 return PTR_ERR(tlink);
1608 tcon = tlink_tcon(tlink);
1609 server = tcon->ses->server;
1612 page = alloc_dentry_path();
1614 if (tcon->nodelete) {
1619 /* Unlink can be called from rename so we can not take the
1620 * sb->s_vfs_rename_mutex here */
1621 full_path = build_path_from_dentry(dentry, page);
1622 if (IS_ERR(full_path)) {
1623 rc = PTR_ERR(full_path);
1627 cifs_close_deferred_file_under_dentry(tcon, full_path);
1628 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1629 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1630 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1631 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1632 cifs_remap(cifs_sb));
1633 cifs_dbg(FYI, "posix del rc %d\n", rc);
1634 if ((rc == 0) || (rc == -ENOENT))
1635 goto psx_del_no_retry;
1639 if (!server->ops->unlink) {
1641 goto psx_del_no_retry;
1644 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1649 cifs_drop_nlink(inode);
1650 } else if (rc == -ENOENT) {
1652 } else if (rc == -EBUSY) {
1653 if (server->ops->rename_pending_delete) {
1654 rc = server->ops->rename_pending_delete(full_path,
1657 cifs_drop_nlink(inode);
1659 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1660 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1661 if (attrs == NULL) {
1666 /* try to reset dos attributes */
1667 cifs_inode = CIFS_I(inode);
1668 origattr = cifs_inode->cifsAttrs;
1670 origattr |= ATTR_NORMAL;
1671 dosattr = origattr & ~ATTR_READONLY;
1673 dosattr |= ATTR_NORMAL;
1674 dosattr |= ATTR_HIDDEN;
1676 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1680 goto retry_std_delete;
1683 /* undo the setattr if we errored out and it's needed */
1684 if (rc != 0 && dosattr != 0)
1685 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1689 cifs_inode = CIFS_I(inode);
1690 cifs_inode->time = 0; /* will force revalidate to get info
1692 inode->i_ctime = current_time(inode);
1694 dir->i_ctime = dir->i_mtime = current_time(dir);
1695 cifs_inode = CIFS_I(dir);
1696 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1698 free_dentry_path(page);
1701 cifs_put_tlink(tlink);
1706 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1707 const char *full_path, struct cifs_sb_info *cifs_sb,
1708 struct cifs_tcon *tcon, const unsigned int xid)
1711 struct inode *inode = NULL;
1713 if (tcon->posix_extensions)
1714 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1715 else if (tcon->unix_ext)
1716 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1719 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1725 if (!S_ISDIR(inode->i_mode)) {
1727 * mkdir succeeded, but another client has managed to remove the
1728 * sucker and replace it with non-directory. Return success,
1729 * but don't leave the child in dcache.
1736 * setting nlink not necessary except in cases where we failed to get it
1737 * from the server or was set bogus. Also, since this is a brand new
1738 * inode, no need to grab the i_lock before setting the i_nlink.
1740 if (inode->i_nlink < 2)
1741 set_nlink(inode, 2);
1742 mode &= ~current_umask();
1743 /* must turn on setgid bit if parent dir has it */
1744 if (parent->i_mode & S_ISGID)
1747 if (tcon->unix_ext) {
1748 struct cifs_unix_set_info_args args = {
1750 .ctime = NO_CHANGE_64,
1751 .atime = NO_CHANGE_64,
1752 .mtime = NO_CHANGE_64,
1755 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1756 args.uid = current_fsuid();
1757 if (parent->i_mode & S_ISGID)
1758 args.gid = parent->i_gid;
1760 args.gid = current_fsgid();
1762 args.uid = INVALID_UID; /* no change */
1763 args.gid = INVALID_GID; /* no change */
1765 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1767 cifs_remap(cifs_sb));
1769 struct TCP_Server_Info *server = tcon->ses->server;
1770 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1771 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1772 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1774 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1775 inode->i_mode = (mode | S_IFDIR);
1777 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1778 inode->i_uid = current_fsuid();
1779 if (inode->i_mode & S_ISGID)
1780 inode->i_gid = parent->i_gid;
1782 inode->i_gid = current_fsgid();
1785 d_instantiate(dentry, inode);
1790 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1791 const char *full_path, struct cifs_sb_info *cifs_sb,
1792 struct cifs_tcon *tcon, const unsigned int xid)
1796 FILE_UNIX_BASIC_INFO *info = NULL;
1797 struct inode *newinode = NULL;
1798 struct cifs_fattr fattr;
1800 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1803 goto posix_mkdir_out;
1806 mode &= ~current_umask();
1807 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1808 NULL /* netfid */, info, &oplock, full_path,
1809 cifs_sb->local_nls, cifs_remap(cifs_sb));
1810 if (rc == -EOPNOTSUPP)
1811 goto posix_mkdir_out;
1813 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1815 goto posix_mkdir_out;
1818 if (info->Type == cpu_to_le32(-1))
1819 /* no return info, go query for it */
1820 goto posix_mkdir_get_info;
1822 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1823 * need to set uid/gid.
1826 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1827 cifs_fill_uniqueid(inode->i_sb, &fattr);
1828 newinode = cifs_iget(inode->i_sb, &fattr);
1830 goto posix_mkdir_get_info;
1832 d_instantiate(dentry, newinode);
1834 #ifdef CONFIG_CIFS_DEBUG2
1835 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1836 dentry, dentry, newinode);
1838 if (newinode->i_nlink != 2)
1839 cifs_dbg(FYI, "unexpected number of links %d\n",
1846 posix_mkdir_get_info:
1847 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1849 goto posix_mkdir_out;
1852 int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
1853 struct dentry *direntry, umode_t mode)
1857 struct cifs_sb_info *cifs_sb;
1858 struct tcon_link *tlink;
1859 struct cifs_tcon *tcon;
1860 struct TCP_Server_Info *server;
1861 const char *full_path;
1864 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1867 cifs_sb = CIFS_SB(inode->i_sb);
1868 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1870 tlink = cifs_sb_tlink(cifs_sb);
1872 return PTR_ERR(tlink);
1873 tcon = tlink_tcon(tlink);
1877 page = alloc_dentry_path();
1878 full_path = build_path_from_dentry(direntry, page);
1879 if (IS_ERR(full_path)) {
1880 rc = PTR_ERR(full_path);
1884 server = tcon->ses->server;
1886 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1887 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1889 d_drop(direntry); /* for time being always refresh inode info */
1893 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1894 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1895 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1897 if (rc != -EOPNOTSUPP)
1901 if (!server->ops->mkdir) {
1906 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1907 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1909 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1914 /* TODO: skip this for smb2/smb3 */
1915 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1919 * Force revalidate to get parent dir info when needed since cached
1920 * attributes are invalid now.
1922 CIFS_I(inode)->time = 0;
1923 free_dentry_path(page);
1925 cifs_put_tlink(tlink);
1929 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1933 struct cifs_sb_info *cifs_sb;
1934 struct tcon_link *tlink;
1935 struct cifs_tcon *tcon;
1936 struct TCP_Server_Info *server;
1937 const char *full_path;
1938 void *page = alloc_dentry_path();
1939 struct cifsInodeInfo *cifsInode;
1941 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1945 full_path = build_path_from_dentry(direntry, page);
1946 if (IS_ERR(full_path)) {
1947 rc = PTR_ERR(full_path);
1951 cifs_sb = CIFS_SB(inode->i_sb);
1952 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
1957 tlink = cifs_sb_tlink(cifs_sb);
1958 if (IS_ERR(tlink)) {
1959 rc = PTR_ERR(tlink);
1962 tcon = tlink_tcon(tlink);
1963 server = tcon->ses->server;
1965 if (!server->ops->rmdir) {
1967 cifs_put_tlink(tlink);
1971 if (tcon->nodelete) {
1973 cifs_put_tlink(tlink);
1977 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1978 cifs_put_tlink(tlink);
1981 spin_lock(&d_inode(direntry)->i_lock);
1982 i_size_write(d_inode(direntry), 0);
1983 clear_nlink(d_inode(direntry));
1984 spin_unlock(&d_inode(direntry)->i_lock);
1987 cifsInode = CIFS_I(d_inode(direntry));
1988 /* force revalidate to go get info when needed */
1989 cifsInode->time = 0;
1991 cifsInode = CIFS_I(inode);
1993 * Force revalidate to get parent dir info when needed since cached
1994 * attributes are invalid now.
1996 cifsInode->time = 0;
1998 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1999 current_time(inode);
2002 free_dentry_path(page);
2008 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2009 const char *from_path, struct dentry *to_dentry,
2010 const char *to_path)
2012 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2013 struct tcon_link *tlink;
2014 struct cifs_tcon *tcon;
2015 struct TCP_Server_Info *server;
2016 struct cifs_fid fid;
2017 struct cifs_open_parms oparms;
2020 tlink = cifs_sb_tlink(cifs_sb);
2022 return PTR_ERR(tlink);
2023 tcon = tlink_tcon(tlink);
2024 server = tcon->ses->server;
2026 if (!server->ops->rename)
2029 /* try path-based rename first */
2030 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2033 * Don't bother with rename by filehandle unless file is busy and
2034 * source. Note that cross directory moves do not work with
2035 * rename by filehandle to various Windows servers.
2037 if (rc == 0 || rc != -EBUSY)
2038 goto do_rename_exit;
2040 /* Don't fall back to using SMB on SMB 2+ mount */
2041 if (server->vals->protocol_id != 0)
2042 goto do_rename_exit;
2044 /* open-file renames don't work across directories */
2045 if (to_dentry->d_parent != from_dentry->d_parent)
2046 goto do_rename_exit;
2049 oparms.cifs_sb = cifs_sb;
2050 /* open the file to be renamed -- we need DELETE perms */
2051 oparms.desired_access = DELETE;
2052 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2053 oparms.disposition = FILE_OPEN;
2054 oparms.path = from_path;
2056 oparms.reconnect = false;
2058 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2060 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2061 (const char *) to_dentry->d_name.name,
2062 cifs_sb->local_nls, cifs_remap(cifs_sb));
2063 CIFSSMBClose(xid, tcon, fid.netfid);
2067 d_move(from_dentry, to_dentry);
2068 cifs_put_tlink(tlink);
2073 cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
2074 struct dentry *source_dentry, struct inode *target_dir,
2075 struct dentry *target_dentry, unsigned int flags)
2077 const char *from_name, *to_name;
2078 void *page1, *page2;
2079 struct cifs_sb_info *cifs_sb;
2080 struct tcon_link *tlink;
2081 struct cifs_tcon *tcon;
2082 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2083 FILE_UNIX_BASIC_INFO *info_buf_target;
2086 int retry_count = 0;
2088 if (flags & ~RENAME_NOREPLACE)
2091 cifs_sb = CIFS_SB(source_dir->i_sb);
2092 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2095 tlink = cifs_sb_tlink(cifs_sb);
2097 return PTR_ERR(tlink);
2098 tcon = tlink_tcon(tlink);
2100 page1 = alloc_dentry_path();
2101 page2 = alloc_dentry_path();
2104 from_name = build_path_from_dentry(source_dentry, page1);
2105 if (IS_ERR(from_name)) {
2106 rc = PTR_ERR(from_name);
2107 goto cifs_rename_exit;
2110 to_name = build_path_from_dentry(target_dentry, page2);
2111 if (IS_ERR(to_name)) {
2112 rc = PTR_ERR(to_name);
2113 goto cifs_rename_exit;
2116 cifs_close_deferred_file_under_dentry(tcon, from_name);
2117 if (d_inode(target_dentry) != NULL)
2118 cifs_close_deferred_file_under_dentry(tcon, to_name);
2120 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2123 if (rc == -EACCES) {
2124 while (retry_count < 3) {
2125 cifs_close_all_deferred_files(tcon);
2126 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2135 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2137 if (flags & RENAME_NOREPLACE)
2138 goto cifs_rename_exit;
2140 if (rc == -EEXIST && tcon->unix_ext) {
2142 * Are src and dst hardlinks of same inode? We can only tell
2143 * with unix extensions enabled.
2146 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2148 if (info_buf_source == NULL) {
2150 goto cifs_rename_exit;
2153 info_buf_target = info_buf_source + 1;
2154 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2157 cifs_remap(cifs_sb));
2161 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2164 cifs_remap(cifs_sb));
2166 if (tmprc == 0 && (info_buf_source->UniqueId ==
2167 info_buf_target->UniqueId)) {
2168 /* same file, POSIX says that this is a noop */
2170 goto cifs_rename_exit;
2174 * else ... BB we could add the same check for Windows by
2175 * checking the UniqueId via FILE_INTERNAL_INFO
2179 /* Try unlinking the target dentry if it's not negative */
2180 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2181 if (d_is_dir(target_dentry))
2182 tmprc = cifs_rmdir(target_dir, target_dentry);
2184 tmprc = cifs_unlink(target_dir, target_dentry);
2186 goto cifs_rename_exit;
2187 rc = cifs_do_rename(xid, source_dentry, from_name,
2188 target_dentry, to_name);
2191 /* force revalidate to go get info when needed */
2192 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2194 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2195 target_dir->i_mtime = current_time(source_dir);
2198 kfree(info_buf_source);
2199 free_dentry_path(page2);
2200 free_dentry_path(page1);
2202 cifs_put_tlink(tlink);
2207 cifs_dentry_needs_reval(struct dentry *dentry)
2209 struct inode *inode = d_inode(dentry);
2210 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2211 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2212 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2213 struct cached_fid *cfid = NULL;
2215 if (cifs_i->time == 0)
2218 if (CIFS_CACHE_READ(cifs_i))
2221 if (!lookupCacheEnabled)
2224 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2225 mutex_lock(&cfid->fid_mutex);
2226 if (cfid->time && cifs_i->time > cfid->time) {
2227 mutex_unlock(&cfid->fid_mutex);
2228 close_cached_dir(cfid);
2231 mutex_unlock(&cfid->fid_mutex);
2232 close_cached_dir(cfid);
2235 * depending on inode type, check if attribute caching disabled for
2236 * files or directories
2238 if (S_ISDIR(inode->i_mode)) {
2239 if (!cifs_sb->ctx->acdirmax)
2241 if (!time_in_range(jiffies, cifs_i->time,
2242 cifs_i->time + cifs_sb->ctx->acdirmax))
2245 if (!cifs_sb->ctx->acregmax)
2247 if (!time_in_range(jiffies, cifs_i->time,
2248 cifs_i->time + cifs_sb->ctx->acregmax))
2252 /* hardlinked files w/ noserverino get "special" treatment */
2253 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2254 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2261 * Zap the cache. Called when invalid_mapping flag is set.
2264 cifs_invalidate_mapping(struct inode *inode)
2268 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2269 rc = invalidate_inode_pages2(inode->i_mapping);
2271 cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2275 cifs_fscache_reset_inode_cookie(inode);
2280 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2282 * @key: currently unused
2283 * @mode: the task state to sleep in
2286 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2288 freezable_schedule_unsafe();
2289 if (signal_pending_state(mode, current))
2290 return -ERESTARTSYS;
2295 cifs_revalidate_mapping(struct inode *inode)
2298 unsigned long *flags = &CIFS_I(inode)->flags;
2299 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2301 /* swapfiles are not supposed to be shared */
2302 if (IS_SWAPFILE(inode))
2305 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2310 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2311 /* for cache=singleclient, do not invalidate */
2312 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2313 goto skip_invalidate;
2315 rc = cifs_invalidate_mapping(inode);
2317 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2321 clear_bit_unlock(CIFS_INO_LOCK, flags);
2322 smp_mb__after_atomic();
2323 wake_up_bit(flags, CIFS_INO_LOCK);
2329 cifs_zap_mapping(struct inode *inode)
2331 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2332 return cifs_revalidate_mapping(inode);
2335 int cifs_revalidate_file_attr(struct file *filp)
2338 struct dentry *dentry = file_dentry(filp);
2339 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2341 if (!cifs_dentry_needs_reval(dentry))
2344 if (tlink_tcon(cfile->tlink)->unix_ext)
2345 rc = cifs_get_file_info_unix(filp);
2347 rc = cifs_get_file_info(filp);
2352 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2356 struct inode *inode = d_inode(dentry);
2357 struct super_block *sb = dentry->d_sb;
2358 const char *full_path;
2365 if (!cifs_dentry_needs_reval(dentry))
2370 page = alloc_dentry_path();
2371 full_path = build_path_from_dentry(dentry, page);
2372 if (IS_ERR(full_path)) {
2373 rc = PTR_ERR(full_path);
2377 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2378 full_path, inode, inode->i_count.counter,
2379 dentry, cifs_get_time(dentry), jiffies);
2382 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2383 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2384 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2385 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2387 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2389 if (rc == -EAGAIN && count++ < 10)
2392 free_dentry_path(page);
2398 int cifs_revalidate_file(struct file *filp)
2401 struct inode *inode = file_inode(filp);
2403 rc = cifs_revalidate_file_attr(filp);
2407 return cifs_revalidate_mapping(inode);
2410 /* revalidate a dentry's inode attributes */
2411 int cifs_revalidate_dentry(struct dentry *dentry)
2414 struct inode *inode = d_inode(dentry);
2416 rc = cifs_revalidate_dentry_attr(dentry);
2420 return cifs_revalidate_mapping(inode);
2423 int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
2424 struct kstat *stat, u32 request_mask, unsigned int flags)
2426 struct dentry *dentry = path->dentry;
2427 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2428 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2429 struct inode *inode = d_inode(dentry);
2432 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2436 * We need to be sure that all dirty pages are written and the server
2437 * has actual ctime, mtime and file length.
2439 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2440 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2441 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2442 rc = filemap_fdatawait(inode->i_mapping);
2444 mapping_set_error(inode->i_mapping, rc);
2449 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2450 CIFS_I(inode)->time = 0; /* force revalidate */
2453 * If the caller doesn't require syncing, only sync if
2454 * necessary (e.g. due to earlier truncate or setattr
2455 * invalidating the cached metadata)
2457 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2458 (CIFS_I(inode)->time == 0)) {
2459 rc = cifs_revalidate_dentry_attr(dentry);
2464 generic_fillattr(&init_user_ns, inode, stat);
2465 stat->blksize = cifs_sb->ctx->bsize;
2466 stat->ino = CIFS_I(inode)->uniqueid;
2468 /* old CIFS Unix Extensions doesn't return create time */
2469 if (CIFS_I(inode)->createtime) {
2470 stat->result_mask |= STATX_BTIME;
2472 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2475 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2476 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2477 stat->attributes |= STATX_ATTR_COMPRESSED;
2478 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2479 stat->attributes |= STATX_ATTR_ENCRYPTED;
2482 * If on a multiuser mount without unix extensions or cifsacl being
2483 * enabled, and the admin hasn't overridden them, set the ownership
2484 * to the fsuid/fsgid of the current process.
2486 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2487 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2489 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2490 stat->uid = current_fsuid();
2491 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2492 stat->gid = current_fsgid();
2497 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2500 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2501 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->vfs_inode.i_sb);
2502 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2503 struct TCP_Server_Info *server = tcon->ses->server;
2504 struct cifsFileInfo *cfile;
2507 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2511 * We need to be sure that all dirty pages are written as they
2512 * might fill holes on the server.
2514 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2515 inode->i_mapping->nrpages != 0) {
2516 rc = filemap_fdatawait(inode->i_mapping);
2518 mapping_set_error(inode->i_mapping, rc);
2523 cfile = find_readable_file(cifs_i, false);
2527 if (server->ops->fiemap) {
2528 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2529 cifsFileInfo_put(cfile);
2533 cifsFileInfo_put(cfile);
2537 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2539 pgoff_t index = from >> PAGE_SHIFT;
2540 unsigned offset = from & (PAGE_SIZE - 1);
2544 page = grab_cache_page(mapping, index);
2548 zero_user_segment(page, offset, PAGE_SIZE);
2554 void cifs_setsize(struct inode *inode, loff_t offset)
2556 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2558 spin_lock(&inode->i_lock);
2559 i_size_write(inode, offset);
2560 spin_unlock(&inode->i_lock);
2562 /* Cached inode must be refreshed on truncate */
2564 truncate_pagecache(inode, offset);
2568 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2569 unsigned int xid, const char *full_path)
2572 struct cifsFileInfo *open_file;
2573 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2574 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2575 struct tcon_link *tlink = NULL;
2576 struct cifs_tcon *tcon = NULL;
2577 struct TCP_Server_Info *server;
2580 * To avoid spurious oplock breaks from server, in the case of
2581 * inodes that we already have open, avoid doing path based
2582 * setting of file size if we can do it by handle.
2583 * This keeps our caching token (oplock) and avoids timeouts
2584 * when the local oplock break takes longer to flush
2585 * writebehind data than the SMB timeout for the SetPathInfo
2586 * request would allow
2588 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2590 tcon = tlink_tcon(open_file->tlink);
2591 server = tcon->ses->server;
2592 if (server->ops->set_file_size)
2593 rc = server->ops->set_file_size(xid, tcon, open_file,
2594 attrs->ia_size, false);
2597 cifsFileInfo_put(open_file);
2598 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2606 tlink = cifs_sb_tlink(cifs_sb);
2608 return PTR_ERR(tlink);
2609 tcon = tlink_tcon(tlink);
2610 server = tcon->ses->server;
2614 * Set file size by pathname rather than by handle either because no
2615 * valid, writeable file handle for it was found or because there was
2616 * an error setting it by handle.
2618 if (server->ops->set_path_size)
2619 rc = server->ops->set_path_size(xid, tcon, full_path,
2620 attrs->ia_size, cifs_sb, false);
2623 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2626 cifs_put_tlink(tlink);
2630 cifsInode->server_eof = attrs->ia_size;
2631 cifs_setsize(inode, attrs->ia_size);
2633 * i_blocks is not related to (i_size / i_blksize), but instead
2634 * 512 byte (2**9) size is required for calculating num blocks.
2635 * Until we can query the server for actual allocation size,
2636 * this is best estimate we have for blocks allocated for a file
2637 * Number of blocks must be rounded up so size 1 is not 0 blocks
2639 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2642 * The man page of truncate says if the size changed,
2643 * then the st_ctime and st_mtime fields for the file
2646 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2647 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2649 cifs_truncate_page(inode->i_mapping, inode->i_size);
2656 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2660 const char *full_path;
2661 void *page = alloc_dentry_path();
2662 struct inode *inode = d_inode(direntry);
2663 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2664 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2665 struct tcon_link *tlink;
2666 struct cifs_tcon *pTcon;
2667 struct cifs_unix_set_info_args *args = NULL;
2668 struct cifsFileInfo *open_file;
2670 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2671 direntry, attrs->ia_valid);
2675 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2676 attrs->ia_valid |= ATTR_FORCE;
2678 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2682 full_path = build_path_from_dentry(direntry, page);
2683 if (IS_ERR(full_path)) {
2684 rc = PTR_ERR(full_path);
2689 * Attempt to flush data before changing attributes. We need to do
2690 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2691 * ownership or mode then we may also need to do this. Here, we take
2692 * the safe way out and just do the flush on all setattr requests. If
2693 * the flush returns error, store it to report later and continue.
2695 * BB: This should be smarter. Why bother flushing pages that
2696 * will be truncated anyway? Also, should we error out here if
2697 * the flush returns error?
2699 rc = filemap_write_and_wait(inode->i_mapping);
2700 if (is_interrupt_error(rc)) {
2705 mapping_set_error(inode->i_mapping, rc);
2708 if (attrs->ia_valid & ATTR_SIZE) {
2709 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2714 /* skip mode change if it's just for clearing setuid/setgid */
2715 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2716 attrs->ia_valid &= ~ATTR_MODE;
2718 args = kmalloc(sizeof(*args), GFP_KERNEL);
2724 /* set up the struct */
2725 if (attrs->ia_valid & ATTR_MODE)
2726 args->mode = attrs->ia_mode;
2728 args->mode = NO_CHANGE_64;
2730 if (attrs->ia_valid & ATTR_UID)
2731 args->uid = attrs->ia_uid;
2733 args->uid = INVALID_UID; /* no change */
2735 if (attrs->ia_valid & ATTR_GID)
2736 args->gid = attrs->ia_gid;
2738 args->gid = INVALID_GID; /* no change */
2740 if (attrs->ia_valid & ATTR_ATIME)
2741 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2743 args->atime = NO_CHANGE_64;
2745 if (attrs->ia_valid & ATTR_MTIME)
2746 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2748 args->mtime = NO_CHANGE_64;
2750 if (attrs->ia_valid & ATTR_CTIME)
2751 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2753 args->ctime = NO_CHANGE_64;
2756 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2758 u16 nfid = open_file->fid.netfid;
2759 u32 npid = open_file->pid;
2760 pTcon = tlink_tcon(open_file->tlink);
2761 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2762 cifsFileInfo_put(open_file);
2764 tlink = cifs_sb_tlink(cifs_sb);
2765 if (IS_ERR(tlink)) {
2766 rc = PTR_ERR(tlink);
2769 pTcon = tlink_tcon(tlink);
2770 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2772 cifs_remap(cifs_sb));
2773 cifs_put_tlink(tlink);
2779 if ((attrs->ia_valid & ATTR_SIZE) &&
2780 attrs->ia_size != i_size_read(inode))
2781 truncate_setsize(inode, attrs->ia_size);
2783 setattr_copy(&init_user_ns, inode, attrs);
2784 mark_inode_dirty(inode);
2786 /* force revalidate when any of these times are set since some
2787 of the fs types (eg ext3, fat) do not have fine enough
2788 time granularity to match protocol, and we do not have a
2789 a way (yet) to query the server fs's time granularity (and
2790 whether it rounds times down).
2792 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2793 cifsInode->time = 0;
2796 free_dentry_path(page);
2802 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2805 kuid_t uid = INVALID_UID;
2806 kgid_t gid = INVALID_GID;
2807 struct inode *inode = d_inode(direntry);
2808 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2809 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2810 struct cifsFileInfo *wfile;
2811 struct cifs_tcon *tcon;
2812 const char *full_path;
2813 void *page = alloc_dentry_path();
2816 __u64 mode = NO_CHANGE_64;
2820 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2821 direntry, attrs->ia_valid);
2823 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2824 attrs->ia_valid |= ATTR_FORCE;
2826 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2828 goto cifs_setattr_exit;
2830 full_path = build_path_from_dentry(direntry, page);
2831 if (IS_ERR(full_path)) {
2832 rc = PTR_ERR(full_path);
2833 goto cifs_setattr_exit;
2837 * Attempt to flush data before changing attributes. We need to do
2838 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2839 * returns error, store it to report later and continue.
2841 * BB: This should be smarter. Why bother flushing pages that
2842 * will be truncated anyway? Also, should we error out here if
2843 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2845 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2846 rc = filemap_write_and_wait(inode->i_mapping);
2847 if (is_interrupt_error(rc)) {
2849 goto cifs_setattr_exit;
2851 mapping_set_error(inode->i_mapping, rc);
2856 if ((attrs->ia_valid & ATTR_MTIME) &&
2857 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2858 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2860 tcon = tlink_tcon(wfile->tlink);
2861 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2862 cifsFileInfo_put(wfile);
2864 goto cifs_setattr_exit;
2865 } else if (rc != -EBADF)
2866 goto cifs_setattr_exit;
2871 if (attrs->ia_valid & ATTR_SIZE) {
2872 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2874 goto cifs_setattr_exit;
2877 if (attrs->ia_valid & ATTR_UID)
2878 uid = attrs->ia_uid;
2880 if (attrs->ia_valid & ATTR_GID)
2881 gid = attrs->ia_gid;
2883 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2884 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2885 if (uid_valid(uid) || gid_valid(gid)) {
2886 mode = NO_CHANGE_64;
2887 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2890 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2892 goto cifs_setattr_exit;
2896 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2897 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2899 /* skip mode change if it's just for clearing setuid/setgid */
2900 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2901 attrs->ia_valid &= ~ATTR_MODE;
2903 if (attrs->ia_valid & ATTR_MODE) {
2904 mode = attrs->ia_mode;
2906 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2907 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2908 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2909 INVALID_UID, INVALID_GID);
2911 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2913 goto cifs_setattr_exit;
2917 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
2918 * Pick up the actual mode bits that were set.
2920 if (mode != attrs->ia_mode)
2921 attrs->ia_mode = mode;
2923 if (((mode & S_IWUGO) == 0) &&
2924 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2926 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2928 /* fix up mode if we're not using dynperm */
2929 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2930 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2931 } else if ((mode & S_IWUGO) &&
2932 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2934 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2935 /* Attributes of 0 are ignored */
2937 dosattr |= ATTR_NORMAL;
2939 /* reset local inode permissions to normal */
2940 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2941 attrs->ia_mode &= ~(S_IALLUGO);
2942 if (S_ISDIR(inode->i_mode))
2944 cifs_sb->ctx->dir_mode;
2947 cifs_sb->ctx->file_mode;
2949 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2950 /* ignore mode change - ATTR_READONLY hasn't changed */
2951 attrs->ia_valid &= ~ATTR_MODE;
2955 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2956 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2957 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2958 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2960 /* Even if error on time set, no sense failing the call if
2961 the server would set the time to a reasonable value anyway,
2962 and this check ensures that we are not being called from
2963 sys_utimes in which case we ought to fail the call back to
2964 the user when the server rejects the call */
2965 if ((rc) && (attrs->ia_valid &
2966 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2970 /* do not need local check to inode_check_ok since the server does
2973 goto cifs_setattr_exit;
2975 if ((attrs->ia_valid & ATTR_SIZE) &&
2976 attrs->ia_size != i_size_read(inode))
2977 truncate_setsize(inode, attrs->ia_size);
2979 setattr_copy(&init_user_ns, inode, attrs);
2980 mark_inode_dirty(inode);
2984 free_dentry_path(page);
2989 cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
2990 struct iattr *attrs)
2992 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2993 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2994 int rc, retries = 0;
2996 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3000 if (pTcon->unix_ext)
3001 rc = cifs_setattr_unix(direntry, attrs);
3003 rc = cifs_setattr_nounix(direntry, attrs);
3005 } while (is_retryable_error(rc) && retries < 2);
3007 /* BB: add cifs_setattr_legacy for really old servers */