1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
30 static void cifs_set_ops(struct inode *inode)
32 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
34 switch (inode->i_mode & S_IFMT) {
36 inode->i_op = &cifs_file_inode_ops;
37 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
38 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
39 inode->i_fop = &cifs_file_direct_nobrl_ops;
41 inode->i_fop = &cifs_file_direct_ops;
42 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44 inode->i_fop = &cifs_file_strict_nobrl_ops;
46 inode->i_fop = &cifs_file_strict_ops;
47 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48 inode->i_fop = &cifs_file_nobrl_ops;
49 else { /* not direct, send byte range locks */
50 inode->i_fop = &cifs_file_ops;
53 /* check if server can support readahead */
54 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
55 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
56 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
58 inode->i_data.a_ops = &cifs_addr_ops;
61 #ifdef CONFIG_CIFS_DFS_UPCALL
62 if (IS_AUTOMOUNT(inode)) {
63 inode->i_op = &cifs_dfs_referral_inode_operations;
65 #else /* NO DFS support, treat as a directory */
68 inode->i_op = &cifs_dir_inode_ops;
69 inode->i_fop = &cifs_dir_ops;
73 inode->i_op = &cifs_symlink_inode_ops;
76 init_special_inode(inode, inode->i_mode, inode->i_rdev);
81 /* check inode attributes against fattr. If they don't match, tag the
82 * inode for cache invalidation
85 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
87 struct cifs_fscache_inode_coherency_data cd;
88 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
90 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
91 __func__, cifs_i->uniqueid);
93 if (inode->i_state & I_NEW) {
94 cifs_dbg(FYI, "%s: inode %llu is new\n",
95 __func__, cifs_i->uniqueid);
99 /* don't bother with revalidation if we have an oplock */
100 if (CIFS_CACHE_READ(cifs_i)) {
101 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
102 __func__, cifs_i->uniqueid);
106 /* revalidate if mtime or size have changed */
107 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
108 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
109 cifs_i->server_eof == fattr->cf_eof) {
110 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
111 __func__, cifs_i->uniqueid);
115 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
116 __func__, cifs_i->uniqueid);
117 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
118 /* Invalidate fscache cookie */
119 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
120 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
124 * copy nlink to the inode, unless it wasn't provided. Provide
125 * sane values if we don't have an existing one and none was provided
128 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
131 * if we're in a situation where we can't trust what we
132 * got from the server (readdir, some non-unix cases)
133 * fake reasonable values
135 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
136 /* only provide fake values on a new inode */
137 if (inode->i_state & I_NEW) {
138 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
146 /* we trust the server, so update it */
147 set_nlink(inode, fattr->cf_nlink);
150 /* populate an inode with info from a cifs_fattr struct */
152 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
154 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
157 if (!(inode->i_state & I_NEW) &&
158 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
159 CIFS_I(inode)->time = 0; /* force reval */
163 cifs_revalidate_cache(inode, fattr);
165 spin_lock(&inode->i_lock);
166 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
167 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
168 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
169 /* we do not want atime to be less than mtime, it broke some apps */
170 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
171 inode->i_atime = fattr->cf_mtime;
173 inode->i_atime = fattr->cf_atime;
174 inode->i_mtime = fattr->cf_mtime;
175 inode->i_ctime = fattr->cf_ctime;
176 inode->i_rdev = fattr->cf_rdev;
177 cifs_nlink_fattr_to_inode(inode, fattr);
178 inode->i_uid = fattr->cf_uid;
179 inode->i_gid = fattr->cf_gid;
181 /* if dynperm is set, don't clobber existing mode */
182 if (inode->i_state & I_NEW ||
183 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
184 inode->i_mode = fattr->cf_mode;
186 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
188 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
191 cifs_i->time = jiffies;
193 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
194 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
196 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
198 cifs_i->server_eof = fattr->cf_eof;
200 * Can't safely change the file size here if the client is writing to
201 * it due to potential races.
203 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
204 i_size_write(inode, fattr->cf_eof);
207 * i_blocks is not related to (i_size / i_blksize),
208 * but instead 512 byte (2**9) size is required for
209 * calculating num blocks.
211 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
213 spin_unlock(&inode->i_lock);
215 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
216 inode->i_flags |= S_AUTOMOUNT;
217 if (inode->i_state & I_NEW)
223 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
225 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
227 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
230 fattr->cf_uniqueid = iunique(sb, ROOT_I);
233 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
235 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
236 struct cifs_sb_info *cifs_sb)
238 memset(fattr, 0, sizeof(*fattr));
239 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
240 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
241 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
243 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
244 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
245 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
246 /* old POSIX extensions don't get create time */
248 fattr->cf_mode = le64_to_cpu(info->Permissions);
251 * Since we set the inode type below we need to mask off
252 * to avoid strange results if bits set above.
254 fattr->cf_mode &= ~S_IFMT;
255 switch (le32_to_cpu(info->Type)) {
257 fattr->cf_mode |= S_IFREG;
258 fattr->cf_dtype = DT_REG;
261 fattr->cf_mode |= S_IFLNK;
262 fattr->cf_dtype = DT_LNK;
265 fattr->cf_mode |= S_IFDIR;
266 fattr->cf_dtype = DT_DIR;
269 fattr->cf_mode |= S_IFCHR;
270 fattr->cf_dtype = DT_CHR;
271 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
272 le64_to_cpu(info->DevMinor) & MINORMASK);
275 fattr->cf_mode |= S_IFBLK;
276 fattr->cf_dtype = DT_BLK;
277 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
278 le64_to_cpu(info->DevMinor) & MINORMASK);
281 fattr->cf_mode |= S_IFIFO;
282 fattr->cf_dtype = DT_FIFO;
285 fattr->cf_mode |= S_IFSOCK;
286 fattr->cf_dtype = DT_SOCK;
289 /* safest to call it a file if we do not know */
290 fattr->cf_mode |= S_IFREG;
291 fattr->cf_dtype = DT_REG;
292 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
296 fattr->cf_uid = cifs_sb->ctx->linux_uid;
297 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
298 u64 id = le64_to_cpu(info->Uid);
299 if (id < ((uid_t)-1)) {
300 kuid_t uid = make_kuid(&init_user_ns, id);
306 fattr->cf_gid = cifs_sb->ctx->linux_gid;
307 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
308 u64 id = le64_to_cpu(info->Gid);
309 if (id < ((gid_t)-1)) {
310 kgid_t gid = make_kgid(&init_user_ns, id);
316 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
320 * Fill a cifs_fattr struct with fake inode info.
322 * Needed to setup cifs_fattr data for the directory which is the
323 * junction to the new submount (ie to setup the fake directory
324 * which represents a DFS referral).
327 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
329 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
331 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
333 memset(fattr, 0, sizeof(*fattr));
334 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
335 fattr->cf_uid = cifs_sb->ctx->linux_uid;
336 fattr->cf_gid = cifs_sb->ctx->linux_gid;
337 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
338 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
340 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
343 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
345 cifs_get_file_info_unix(struct file *filp)
349 FILE_UNIX_BASIC_INFO find_data;
350 struct cifs_fattr fattr;
351 struct inode *inode = file_inode(filp);
352 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
353 struct cifsFileInfo *cfile = filp->private_data;
354 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
357 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
359 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
360 } else if (rc == -EREMOTE) {
361 cifs_create_dfs_fattr(&fattr, inode->i_sb);
364 goto cifs_gfiunix_out;
366 rc = cifs_fattr_to_inode(inode, &fattr);
373 int cifs_get_inode_info_unix(struct inode **pinode,
374 const unsigned char *full_path,
375 struct super_block *sb, unsigned int xid)
378 FILE_UNIX_BASIC_INFO find_data;
379 struct cifs_fattr fattr;
380 struct cifs_tcon *tcon;
381 struct tcon_link *tlink;
382 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
384 cifs_dbg(FYI, "Getting info on %s\n", full_path);
386 tlink = cifs_sb_tlink(cifs_sb);
388 return PTR_ERR(tlink);
389 tcon = tlink_tcon(tlink);
391 /* could have done a find first instead but this returns more info */
392 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
393 cifs_sb->local_nls, cifs_remap(cifs_sb));
394 cifs_put_tlink(tlink);
397 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
398 } else if (rc == -EREMOTE) {
399 cifs_create_dfs_fattr(&fattr, sb);
405 /* check for Minshall+French symlinks */
406 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
407 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
410 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
413 if (*pinode == NULL) {
415 cifs_fill_uniqueid(sb, &fattr);
416 *pinode = cifs_iget(sb, &fattr);
420 /* we already have inode, update it */
422 /* if uniqueid is different, return error */
423 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
424 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
425 CIFS_I(*pinode)->time = 0; /* force reval */
430 /* if filetype is different, return error */
431 rc = cifs_fattr_to_inode(*pinode, &fattr);
438 int cifs_get_inode_info_unix(struct inode **pinode,
439 const unsigned char *full_path,
440 struct super_block *sb, unsigned int xid)
444 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
447 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
448 struct cifs_sb_info *cifs_sb, unsigned int xid)
452 struct tcon_link *tlink;
453 struct cifs_tcon *tcon;
455 struct cifs_open_parms oparms;
456 struct cifs_io_parms io_parms = {0};
458 unsigned int bytes_read;
460 int buf_type = CIFS_NO_BUFFER;
464 fattr->cf_mode &= ~S_IFMT;
466 if (fattr->cf_eof == 0) {
467 fattr->cf_mode |= S_IFIFO;
468 fattr->cf_dtype = DT_FIFO;
470 } else if (fattr->cf_eof < 8) {
471 fattr->cf_mode |= S_IFREG;
472 fattr->cf_dtype = DT_REG;
473 return -EINVAL; /* EOPNOTSUPP? */
476 tlink = cifs_sb_tlink(cifs_sb);
478 return PTR_ERR(tlink);
479 tcon = tlink_tcon(tlink);
482 oparms.cifs_sb = cifs_sb;
483 oparms.desired_access = GENERIC_READ;
484 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
485 oparms.disposition = FILE_OPEN;
488 oparms.reconnect = false;
490 if (tcon->ses->server->oplocks)
494 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
496 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
497 cifs_put_tlink(tlink);
502 io_parms.netfid = fid.netfid;
503 io_parms.pid = current->tgid;
504 io_parms.tcon = tcon;
506 io_parms.length = 24;
508 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
509 &bytes_read, &pbuf, &buf_type);
510 if ((rc == 0) && (bytes_read >= 8)) {
511 if (memcmp("IntxBLK", pbuf, 8) == 0) {
512 cifs_dbg(FYI, "Block device\n");
513 fattr->cf_mode |= S_IFBLK;
514 fattr->cf_dtype = DT_BLK;
515 if (bytes_read == 24) {
516 /* we have enough to decode dev num */
517 __u64 mjr; /* major */
518 __u64 mnr; /* minor */
519 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
520 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
521 fattr->cf_rdev = MKDEV(mjr, mnr);
523 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
524 cifs_dbg(FYI, "Char device\n");
525 fattr->cf_mode |= S_IFCHR;
526 fattr->cf_dtype = DT_CHR;
527 if (bytes_read == 24) {
528 /* we have enough to decode dev num */
529 __u64 mjr; /* major */
530 __u64 mnr; /* minor */
531 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
532 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
533 fattr->cf_rdev = MKDEV(mjr, mnr);
535 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
536 cifs_dbg(FYI, "Symlink\n");
537 fattr->cf_mode |= S_IFLNK;
538 fattr->cf_dtype = DT_LNK;
540 fattr->cf_mode |= S_IFREG; /* file? */
541 fattr->cf_dtype = DT_REG;
545 fattr->cf_mode |= S_IFREG; /* then it is a file */
546 fattr->cf_dtype = DT_REG;
547 rc = -EOPNOTSUPP; /* or some unknown SFU type */
550 tcon->ses->server->ops->close(xid, tcon, &fid);
551 cifs_put_tlink(tlink);
555 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
558 * Fetch mode bits as provided by SFU.
560 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
562 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
563 struct cifs_sb_info *cifs_sb, unsigned int xid)
565 #ifdef CONFIG_CIFS_XATTR
569 struct tcon_link *tlink;
570 struct cifs_tcon *tcon;
572 tlink = cifs_sb_tlink(cifs_sb);
574 return PTR_ERR(tlink);
575 tcon = tlink_tcon(tlink);
577 if (tcon->ses->server->ops->query_all_EAs == NULL) {
578 cifs_put_tlink(tlink);
582 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
583 "SETFILEBITS", ea_value, 4 /* size of buf */,
585 cifs_put_tlink(tlink);
589 mode = le32_to_cpu(*((__le32 *)ea_value));
590 fattr->cf_mode &= ~SFBITS_MASK;
591 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
592 mode, fattr->cf_mode);
593 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
594 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
603 /* Fill a cifs_fattr struct with info from POSIX info struct */
605 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
606 struct super_block *sb, bool adjust_tz, bool symlink)
608 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
609 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
611 memset(fattr, 0, sizeof(*fattr));
613 /* no fattr->flags to set */
614 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
615 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
617 if (info->LastAccessTime)
618 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
620 ktime_get_coarse_real_ts64(&fattr->cf_atime);
622 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
623 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
626 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
627 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
630 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
631 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
632 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
634 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
635 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
636 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
637 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
640 fattr->cf_mode |= S_IFLNK;
641 fattr->cf_dtype = DT_LNK;
642 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
643 fattr->cf_mode |= S_IFDIR;
644 fattr->cf_dtype = DT_DIR;
646 fattr->cf_mode |= S_IFREG;
647 fattr->cf_dtype = DT_REG;
649 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
651 fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
652 fattr->cf_gid = cifs_sb->ctx->linux_gid;
654 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
655 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
659 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
661 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
662 struct super_block *sb, bool adjust_tz,
663 bool symlink, u32 reparse_tag)
665 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
666 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
668 memset(fattr, 0, sizeof(*fattr));
669 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
670 if (info->DeletePending)
671 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
673 if (info->LastAccessTime)
674 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
676 ktime_get_coarse_real_ts64(&fattr->cf_atime);
678 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
679 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
682 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
683 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
686 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
687 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
688 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
690 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
691 if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
692 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
693 fattr->cf_dtype = DT_LNK;
694 } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
695 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
696 fattr->cf_dtype = DT_FIFO;
697 } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
698 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
699 fattr->cf_dtype = DT_SOCK;
700 } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
701 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
702 fattr->cf_dtype = DT_CHR;
703 } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
704 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
705 fattr->cf_dtype = DT_BLK;
706 } else if (symlink) { /* TODO add more reparse tag checks */
707 fattr->cf_mode = S_IFLNK;
708 fattr->cf_dtype = DT_LNK;
709 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
710 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
711 fattr->cf_dtype = DT_DIR;
713 * Server can return wrong NumberOfLinks value for directories
714 * when Unix extensions are disabled - fake it.
717 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
719 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
720 fattr->cf_dtype = DT_REG;
722 /* clear write bits if ATTR_READONLY is set */
723 if (fattr->cf_cifsattrs & ATTR_READONLY)
724 fattr->cf_mode &= ~(S_IWUGO);
727 * Don't accept zero nlink from non-unix servers unless
728 * delete is pending. Instead mark it as unknown.
730 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
731 !info->DeletePending) {
732 cifs_dbg(VFS, "bogus file nlink value %u\n",
734 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
738 fattr->cf_uid = cifs_sb->ctx->linux_uid;
739 fattr->cf_gid = cifs_sb->ctx->linux_gid;
743 cifs_get_file_info(struct file *filp)
747 FILE_ALL_INFO find_data;
748 struct cifs_fattr fattr;
749 struct inode *inode = file_inode(filp);
750 struct cifsFileInfo *cfile = filp->private_data;
751 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
752 struct TCP_Server_Info *server = tcon->ses->server;
754 if (!server->ops->query_file_info)
758 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
761 /* TODO: add support to query reparse tag */
762 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
763 false, 0 /* no reparse tag */);
766 cifs_create_dfs_fattr(&fattr, inode->i_sb);
772 * FIXME: legacy server -- fall back to path-based call?
773 * for now, just skip revalidating and mark inode for
777 CIFS_I(inode)->time = 0;
784 * don't bother with SFU junk here -- just mark inode as needing
787 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
788 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
789 /* if filetype is different, return error */
790 rc = cifs_fattr_to_inode(inode, &fattr);
796 /* Simple function to return a 64 bit hash of string. Rarely called */
797 static __u64 simple_hashstr(const char *str)
799 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
803 hash = (hash + (__u64) *str++) * hash_mult;
808 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
810 * cifs_backup_query_path_info - SMB1 fallback code to get ino
812 * Fallback code to get file metadata when we don't have access to
813 * full_path (EACCES) and have backup creds.
815 * @xid: transaction id used to identify original request in logs
816 * @tcon: information about the server share we have mounted
817 * @sb: the superblock stores info such as disk space available
818 * @full_path: name of the file we are getting the metadata for
819 * @resp_buf: will be set to cifs resp buf and needs to be freed with
820 * cifs_buf_release() when done with @data
821 * @data: will be set to search info result buffer
824 cifs_backup_query_path_info(int xid,
825 struct cifs_tcon *tcon,
826 struct super_block *sb,
827 const char *full_path,
829 FILE_ALL_INFO **data)
831 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
832 struct cifs_search_info info = {0};
837 info.endOfSearch = false;
839 info.info_level = SMB_FIND_FILE_UNIX;
840 else if ((tcon->ses->capabilities &
841 tcon->ses->server->vals->cap_nt_find) == 0)
842 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
843 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
844 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
845 else /* no srvino useful for fallback to some netapp */
846 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
848 flags = CIFS_SEARCH_CLOSE_ALWAYS |
849 CIFS_SEARCH_CLOSE_AT_END |
850 CIFS_SEARCH_BACKUP_SEARCH;
852 rc = CIFSFindFirst(xid, tcon, full_path,
853 cifs_sb, NULL, flags, &info, false);
857 *resp_buf = (void *)info.ntwrk_buf_start;
858 *data = (FILE_ALL_INFO *)info.srch_entries_start;
861 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
864 cifs_set_fattr_ino(int xid,
865 struct cifs_tcon *tcon,
866 struct super_block *sb,
867 struct inode **inode,
868 const char *full_path,
870 struct cifs_fattr *fattr)
872 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
873 struct TCP_Server_Info *server = tcon->ses->server;
876 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
878 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
880 fattr->cf_uniqueid = iunique(sb, ROOT_I);
885 * If we have an inode pass a NULL tcon to ensure we don't
886 * make a round trip to the server. This only works for SMB2+.
888 rc = server->ops->get_srv_inum(xid,
889 *inode ? NULL : tcon,
895 * If that fails reuse existing ino or generate one
896 * and disable server ones
899 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
901 fattr->cf_uniqueid = iunique(sb, ROOT_I);
902 cifs_autodisable_serverino(cifs_sb);
907 /* If no errors, check for zero root inode (invalid) */
908 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
909 cifs_dbg(FYI, "Invalid (0) inodenum\n");
912 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
914 /* make an ino by hashing the UNC */
915 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
916 fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
921 static inline bool is_inode_cache_good(struct inode *ino)
923 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
927 cifs_get_inode_info(struct inode **inode,
928 const char *full_path,
929 FILE_ALL_INFO *in_data,
930 struct super_block *sb, int xid,
931 const struct cifs_fid *fid)
934 struct cifs_tcon *tcon;
935 struct TCP_Server_Info *server;
936 struct tcon_link *tlink;
937 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
938 bool adjust_tz = false;
939 struct cifs_fattr fattr = {0};
940 bool is_reparse_point = false;
941 FILE_ALL_INFO *data = in_data;
942 FILE_ALL_INFO *tmp_data = NULL;
943 void *smb1_backup_rsp_buf = NULL;
946 __u32 reparse_tag = 0;
948 tlink = cifs_sb_tlink(cifs_sb);
950 return PTR_ERR(tlink);
951 tcon = tlink_tcon(tlink);
952 server = tcon->ses->server;
955 * 1. Fetch file metadata if not provided (data)
959 if (is_inode_cache_good(*inode)) {
960 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
963 tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
968 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
970 &adjust_tz, &is_reparse_point);
971 #ifdef CONFIG_CIFS_DFS_UPCALL
972 if (rc == -ENOENT && is_tcon_dfs(tcon))
973 rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon,
981 * 2. Convert it to internal cifs metadata (fattr)
987 * If the file is a reparse point, it is more complicated
988 * since we have to check if its reparse tag matches a known
989 * special file type e.g. symlink or fifo or char etc.
991 if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
992 server->ops->query_reparse_tag) {
993 rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
994 full_path, &reparse_tag);
995 cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
997 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
998 is_reparse_point, reparse_tag);
1001 /* DFS link, no metadata available on this server */
1002 cifs_create_dfs_fattr(&fattr, sb);
1006 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1008 * perm errors, try again with backup flags if possible
1010 * For SMB2 and later the backup intent flag
1011 * is already sent if needed on open and there
1012 * is no path based FindFirst operation to use
1015 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1016 /* for easier reading */
1017 FILE_DIRECTORY_INFO *fdi;
1018 SEARCH_ID_FULL_DIR_INFO *si;
1020 rc = cifs_backup_query_path_info(xid, tcon, sb,
1022 &smb1_backup_rsp_buf,
1027 fdi = (FILE_DIRECTORY_INFO *)data;
1028 si = (SEARCH_ID_FULL_DIR_INFO *)data;
1030 cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1031 fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1032 /* uniqueid set, skip get inum step */
1033 goto handle_mnt_opt;
1035 /* nothing we can do, bail out */
1040 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1043 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1048 * 3. Get or update inode number (fattr.cf_uniqueid)
1051 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1054 * 4. Tweak fattr based on mount options
1056 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1059 /* query for SFU type info if supported and needed */
1060 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1061 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1062 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1064 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1067 /* fill in 0777 bits from ACL */
1068 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1069 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1074 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1078 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1079 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1084 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1090 /* fill in remaining high mode bits e.g. SUID, VTX */
1091 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1092 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1094 /* check for Minshall+French symlinks */
1095 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1096 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1099 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1103 * 5. Update inode with final fattr data
1107 *inode = cifs_iget(sb, &fattr);
1111 /* we already have inode, update it */
1113 /* if uniqueid is different, return error */
1114 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1115 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1116 CIFS_I(*inode)->time = 0; /* force reval */
1120 /* if filetype is different, return error */
1121 rc = cifs_fattr_to_inode(*inode, &fattr);
1124 cifs_buf_release(smb1_backup_rsp_buf);
1125 cifs_put_tlink(tlink);
1131 smb311_posix_get_inode_info(struct inode **inode,
1132 const char *full_path,
1133 struct super_block *sb, unsigned int xid)
1135 struct cifs_tcon *tcon;
1136 struct tcon_link *tlink;
1137 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1138 bool adjust_tz = false;
1139 struct cifs_fattr fattr = {0};
1140 bool symlink = false;
1141 struct smb311_posix_qinfo *data = NULL;
1145 tlink = cifs_sb_tlink(cifs_sb);
1147 return PTR_ERR(tlink);
1148 tcon = tlink_tcon(tlink);
1151 * 1. Fetch file metadata
1154 if (is_inode_cache_good(*inode)) {
1155 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1158 data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1164 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1166 &adjust_tz, &symlink);
1169 * 2. Convert it to internal cifs metadata (fattr)
1174 smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1177 /* DFS link, no metadata available on this server */
1178 cifs_create_dfs_fattr(&fattr, sb);
1183 * For SMB2 and later the backup intent flag
1184 * is already sent if needed on open and there
1185 * is no path based FindFirst operation to use
1186 * to retry with so nothing we can do, bail out
1190 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1196 * 3. Tweak fattr based on mount options
1199 /* check for Minshall+French symlinks */
1200 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1201 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1204 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1208 * 4. Update inode with final fattr data
1212 *inode = cifs_iget(sb, &fattr);
1216 /* we already have inode, update it */
1218 /* if uniqueid is different, return error */
1219 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1220 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1221 CIFS_I(*inode)->time = 0; /* force reval */
1226 /* if filetype is different, return error */
1227 rc = cifs_fattr_to_inode(*inode, &fattr);
1230 cifs_put_tlink(tlink);
1236 static const struct inode_operations cifs_ipc_inode_ops = {
1237 .lookup = cifs_lookup,
1241 cifs_find_inode(struct inode *inode, void *opaque)
1243 struct cifs_fattr *fattr = opaque;
1245 /* don't match inode with different uniqueid */
1246 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1249 /* use createtime like an i_generation field */
1250 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1253 /* don't match inode of different type */
1254 if (inode_wrong_type(inode, fattr->cf_mode))
1257 /* if it's not a directory or has no dentries, then flag it */
1258 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1259 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1265 cifs_init_inode(struct inode *inode, void *opaque)
1267 struct cifs_fattr *fattr = opaque;
1269 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1270 CIFS_I(inode)->createtime = fattr->cf_createtime;
1275 * walk dentry list for an inode and report whether it has aliases that
1276 * are hashed. We use this to determine if a directory inode can actually
1280 inode_has_hashed_dentries(struct inode *inode)
1282 struct dentry *dentry;
1284 spin_lock(&inode->i_lock);
1285 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1286 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1287 spin_unlock(&inode->i_lock);
1291 spin_unlock(&inode->i_lock);
1295 /* Given fattrs, get a corresponding inode */
1297 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1300 struct inode *inode;
1303 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1305 /* hash down to 32-bits on 32-bit arch */
1306 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1308 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1310 /* was there a potentially problematic inode collision? */
1311 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1312 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1314 if (inode_has_hashed_dentries(inode)) {
1315 cifs_autodisable_serverino(CIFS_SB(sb));
1317 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1318 goto retry_iget5_locked;
1322 /* can't fail - see cifs_find_inode() */
1323 cifs_fattr_to_inode(inode, fattr);
1324 if (sb->s_flags & SB_NOATIME)
1325 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1326 if (inode->i_state & I_NEW) {
1327 inode->i_ino = hash;
1328 cifs_fscache_get_inode_cookie(inode);
1329 unlock_new_inode(inode);
1336 /* gets root inode */
1337 struct inode *cifs_root_iget(struct super_block *sb)
1340 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1341 struct inode *inode = NULL;
1343 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1347 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1348 && cifs_sb->prepath) {
1349 len = strlen(cifs_sb->prepath);
1350 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1352 return ERR_PTR(-ENOMEM);
1354 memcpy(path+1, cifs_sb->prepath, len);
1356 path = kstrdup("", GFP_KERNEL);
1358 return ERR_PTR(-ENOMEM);
1362 if (tcon->unix_ext) {
1363 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1364 /* some servers mistakenly claim POSIX support */
1365 if (rc != -EOPNOTSUPP)
1367 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1368 tcon->unix_ext = false;
1371 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1372 if (tcon->posix_extensions)
1373 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1375 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1379 inode = ERR_PTR(rc);
1383 if (rc && tcon->pipe) {
1384 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1385 spin_lock(&inode->i_lock);
1386 inode->i_mode |= S_IFDIR;
1387 set_nlink(inode, 2);
1388 inode->i_op = &cifs_ipc_inode_ops;
1389 inode->i_fop = &simple_dir_operations;
1390 inode->i_uid = cifs_sb->ctx->linux_uid;
1391 inode->i_gid = cifs_sb->ctx->linux_gid;
1392 spin_unlock(&inode->i_lock);
1395 inode = ERR_PTR(rc);
1405 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1406 const char *full_path, __u32 dosattr)
1408 bool set_time = false;
1409 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1410 struct TCP_Server_Info *server;
1411 FILE_BASIC_INFO info_buf;
1416 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1417 if (!server->ops->set_file_info)
1422 if (attrs->ia_valid & ATTR_ATIME) {
1424 info_buf.LastAccessTime =
1425 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1427 info_buf.LastAccessTime = 0;
1429 if (attrs->ia_valid & ATTR_MTIME) {
1431 info_buf.LastWriteTime =
1432 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1434 info_buf.LastWriteTime = 0;
1437 * Samba throws this field away, but windows may actually use it.
1438 * Do not set ctime unless other time stamps are changed explicitly
1439 * (i.e. by utimes()) since we would then have a mix of client and
1442 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1443 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1444 info_buf.ChangeTime =
1445 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1447 info_buf.ChangeTime = 0;
1449 info_buf.CreationTime = 0; /* don't change */
1450 info_buf.Attributes = cpu_to_le32(dosattr);
1452 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1455 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1457 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1458 * and rename it to a random name that hopefully won't conflict with
1462 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1463 const unsigned int xid)
1467 struct cifs_fid fid;
1468 struct cifs_open_parms oparms;
1469 struct inode *inode = d_inode(dentry);
1470 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1471 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1472 struct tcon_link *tlink;
1473 struct cifs_tcon *tcon;
1474 __u32 dosattr, origattr;
1475 FILE_BASIC_INFO *info_buf = NULL;
1477 tlink = cifs_sb_tlink(cifs_sb);
1479 return PTR_ERR(tlink);
1480 tcon = tlink_tcon(tlink);
1483 * We cannot rename the file if the server doesn't support
1484 * CAP_INFOLEVEL_PASSTHRU
1486 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1492 oparms.cifs_sb = cifs_sb;
1493 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1494 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1495 oparms.disposition = FILE_OPEN;
1496 oparms.path = full_path;
1498 oparms.reconnect = false;
1500 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1504 origattr = cifsInode->cifsAttrs;
1506 origattr |= ATTR_NORMAL;
1508 dosattr = origattr & ~ATTR_READONLY;
1510 dosattr |= ATTR_NORMAL;
1511 dosattr |= ATTR_HIDDEN;
1513 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1514 if (dosattr != origattr) {
1515 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1516 if (info_buf == NULL) {
1520 info_buf->Attributes = cpu_to_le32(dosattr);
1521 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1523 /* although we would like to mark the file hidden
1524 if that fails we will still try to rename it */
1526 cifsInode->cifsAttrs = dosattr;
1528 dosattr = origattr; /* since not able to change them */
1531 /* rename the file */
1532 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1534 cifs_remap(cifs_sb));
1540 /* try to set DELETE_ON_CLOSE */
1541 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1542 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1545 * some samba versions return -ENOENT when we try to set the
1546 * file disposition here. Likely a samba bug, but work around
1547 * it for now. This means that some cifsXXX files may hang
1548 * around after they shouldn't.
1550 * BB: remove this hack after more servers have the fix
1558 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1562 CIFSSMBClose(xid, tcon, fid.netfid);
1565 cifs_put_tlink(tlink);
1569 * reset everything back to the original state. Don't bother
1570 * dealing with errors here since we can't do anything about
1574 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1575 cifs_sb->local_nls, cifs_remap(cifs_sb));
1577 if (dosattr != origattr) {
1578 info_buf->Attributes = cpu_to_le32(origattr);
1579 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1581 cifsInode->cifsAttrs = origattr;
1586 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1588 /* copied from fs/nfs/dir.c with small changes */
1590 cifs_drop_nlink(struct inode *inode)
1592 spin_lock(&inode->i_lock);
1593 if (inode->i_nlink > 0)
1595 spin_unlock(&inode->i_lock);
1599 * If d_inode(dentry) is null (usually meaning the cached dentry
1600 * is a negative dentry) then we would attempt a standard SMB delete, but
1601 * if that fails we can not attempt the fall back mechanisms on EACCES
1602 * but will return the EACCES to the caller. Note that the VFS does not call
1603 * unlink on negative dentries currently.
1605 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1609 const char *full_path;
1611 struct inode *inode = d_inode(dentry);
1612 struct cifsInodeInfo *cifs_inode;
1613 struct super_block *sb = dir->i_sb;
1614 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1615 struct tcon_link *tlink;
1616 struct cifs_tcon *tcon;
1617 struct TCP_Server_Info *server;
1618 struct iattr *attrs = NULL;
1619 __u32 dosattr = 0, origattr = 0;
1621 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1623 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1626 tlink = cifs_sb_tlink(cifs_sb);
1628 return PTR_ERR(tlink);
1629 tcon = tlink_tcon(tlink);
1630 server = tcon->ses->server;
1633 page = alloc_dentry_path();
1635 if (tcon->nodelete) {
1640 /* Unlink can be called from rename so we can not take the
1641 * sb->s_vfs_rename_mutex here */
1642 full_path = build_path_from_dentry(dentry, page);
1643 if (IS_ERR(full_path)) {
1644 rc = PTR_ERR(full_path);
1648 cifs_close_deferred_file_under_dentry(tcon, full_path);
1649 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1650 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1651 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1652 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1653 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1654 cifs_remap(cifs_sb));
1655 cifs_dbg(FYI, "posix del rc %d\n", rc);
1656 if ((rc == 0) || (rc == -ENOENT))
1657 goto psx_del_no_retry;
1659 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1662 if (!server->ops->unlink) {
1664 goto psx_del_no_retry;
1667 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1672 cifs_drop_nlink(inode);
1673 } else if (rc == -ENOENT) {
1675 } else if (rc == -EBUSY) {
1676 if (server->ops->rename_pending_delete) {
1677 rc = server->ops->rename_pending_delete(full_path,
1680 cifs_drop_nlink(inode);
1682 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1683 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1684 if (attrs == NULL) {
1689 /* try to reset dos attributes */
1690 cifs_inode = CIFS_I(inode);
1691 origattr = cifs_inode->cifsAttrs;
1693 origattr |= ATTR_NORMAL;
1694 dosattr = origattr & ~ATTR_READONLY;
1696 dosattr |= ATTR_NORMAL;
1697 dosattr |= ATTR_HIDDEN;
1699 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1703 goto retry_std_delete;
1706 /* undo the setattr if we errored out and it's needed */
1707 if (rc != 0 && dosattr != 0)
1708 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1712 cifs_inode = CIFS_I(inode);
1713 cifs_inode->time = 0; /* will force revalidate to get info
1715 inode->i_ctime = current_time(inode);
1717 dir->i_ctime = dir->i_mtime = current_time(dir);
1718 cifs_inode = CIFS_I(dir);
1719 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1721 free_dentry_path(page);
1724 cifs_put_tlink(tlink);
1729 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1730 const char *full_path, struct cifs_sb_info *cifs_sb,
1731 struct cifs_tcon *tcon, const unsigned int xid)
1734 struct inode *inode = NULL;
1736 if (tcon->posix_extensions)
1737 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1738 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1739 else if (tcon->unix_ext)
1740 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1742 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1744 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1750 if (!S_ISDIR(inode->i_mode)) {
1752 * mkdir succeeded, but another client has managed to remove the
1753 * sucker and replace it with non-directory. Return success,
1754 * but don't leave the child in dcache.
1761 * setting nlink not necessary except in cases where we failed to get it
1762 * from the server or was set bogus. Also, since this is a brand new
1763 * inode, no need to grab the i_lock before setting the i_nlink.
1765 if (inode->i_nlink < 2)
1766 set_nlink(inode, 2);
1767 mode &= ~current_umask();
1768 /* must turn on setgid bit if parent dir has it */
1769 if (parent->i_mode & S_ISGID)
1772 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1773 if (tcon->unix_ext) {
1774 struct cifs_unix_set_info_args args = {
1776 .ctime = NO_CHANGE_64,
1777 .atime = NO_CHANGE_64,
1778 .mtime = NO_CHANGE_64,
1781 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1782 args.uid = current_fsuid();
1783 if (parent->i_mode & S_ISGID)
1784 args.gid = parent->i_gid;
1786 args.gid = current_fsgid();
1788 args.uid = INVALID_UID; /* no change */
1789 args.gid = INVALID_GID; /* no change */
1791 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1793 cifs_remap(cifs_sb));
1797 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1798 struct TCP_Server_Info *server = tcon->ses->server;
1799 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1800 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1801 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1803 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1804 inode->i_mode = (mode | S_IFDIR);
1806 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1807 inode->i_uid = current_fsuid();
1808 if (inode->i_mode & S_ISGID)
1809 inode->i_gid = parent->i_gid;
1811 inode->i_gid = current_fsgid();
1814 d_instantiate(dentry, inode);
1818 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1820 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1821 const char *full_path, struct cifs_sb_info *cifs_sb,
1822 struct cifs_tcon *tcon, const unsigned int xid)
1826 FILE_UNIX_BASIC_INFO *info = NULL;
1827 struct inode *newinode = NULL;
1828 struct cifs_fattr fattr;
1830 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1833 goto posix_mkdir_out;
1836 mode &= ~current_umask();
1837 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1838 NULL /* netfid */, info, &oplock, full_path,
1839 cifs_sb->local_nls, cifs_remap(cifs_sb));
1840 if (rc == -EOPNOTSUPP)
1841 goto posix_mkdir_out;
1843 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1845 goto posix_mkdir_out;
1848 if (info->Type == cpu_to_le32(-1))
1849 /* no return info, go query for it */
1850 goto posix_mkdir_get_info;
1852 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1853 * need to set uid/gid.
1856 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1857 cifs_fill_uniqueid(inode->i_sb, &fattr);
1858 newinode = cifs_iget(inode->i_sb, &fattr);
1860 goto posix_mkdir_get_info;
1862 d_instantiate(dentry, newinode);
1864 #ifdef CONFIG_CIFS_DEBUG2
1865 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1866 dentry, dentry, newinode);
1868 if (newinode->i_nlink != 2)
1869 cifs_dbg(FYI, "unexpected number of links %d\n",
1876 posix_mkdir_get_info:
1877 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1879 goto posix_mkdir_out;
1881 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1883 int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
1884 struct dentry *direntry, umode_t mode)
1888 struct cifs_sb_info *cifs_sb;
1889 struct tcon_link *tlink;
1890 struct cifs_tcon *tcon;
1891 struct TCP_Server_Info *server;
1892 const char *full_path;
1895 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1898 cifs_sb = CIFS_SB(inode->i_sb);
1899 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1901 tlink = cifs_sb_tlink(cifs_sb);
1903 return PTR_ERR(tlink);
1904 tcon = tlink_tcon(tlink);
1908 page = alloc_dentry_path();
1909 full_path = build_path_from_dentry(direntry, page);
1910 if (IS_ERR(full_path)) {
1911 rc = PTR_ERR(full_path);
1915 server = tcon->ses->server;
1917 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1918 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1920 d_drop(direntry); /* for time being always refresh inode info */
1924 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1925 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1926 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1927 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1929 if (rc != -EOPNOTSUPP)
1932 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1934 if (!server->ops->mkdir) {
1939 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1940 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1942 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1947 /* TODO: skip this for smb2/smb3 */
1948 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1952 * Force revalidate to get parent dir info when needed since cached
1953 * attributes are invalid now.
1955 CIFS_I(inode)->time = 0;
1956 free_dentry_path(page);
1958 cifs_put_tlink(tlink);
1962 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1966 struct cifs_sb_info *cifs_sb;
1967 struct tcon_link *tlink;
1968 struct cifs_tcon *tcon;
1969 struct TCP_Server_Info *server;
1970 const char *full_path;
1971 void *page = alloc_dentry_path();
1972 struct cifsInodeInfo *cifsInode;
1974 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1978 full_path = build_path_from_dentry(direntry, page);
1979 if (IS_ERR(full_path)) {
1980 rc = PTR_ERR(full_path);
1984 cifs_sb = CIFS_SB(inode->i_sb);
1985 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
1990 tlink = cifs_sb_tlink(cifs_sb);
1991 if (IS_ERR(tlink)) {
1992 rc = PTR_ERR(tlink);
1995 tcon = tlink_tcon(tlink);
1996 server = tcon->ses->server;
1998 if (!server->ops->rmdir) {
2000 cifs_put_tlink(tlink);
2004 if (tcon->nodelete) {
2006 cifs_put_tlink(tlink);
2010 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2011 cifs_put_tlink(tlink);
2014 spin_lock(&d_inode(direntry)->i_lock);
2015 i_size_write(d_inode(direntry), 0);
2016 clear_nlink(d_inode(direntry));
2017 spin_unlock(&d_inode(direntry)->i_lock);
2020 cifsInode = CIFS_I(d_inode(direntry));
2021 /* force revalidate to go get info when needed */
2022 cifsInode->time = 0;
2024 cifsInode = CIFS_I(inode);
2026 * Force revalidate to get parent dir info when needed since cached
2027 * attributes are invalid now.
2029 cifsInode->time = 0;
2031 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
2032 current_time(inode);
2035 free_dentry_path(page);
2041 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2042 const char *from_path, struct dentry *to_dentry,
2043 const char *to_path)
2045 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2046 struct tcon_link *tlink;
2047 struct cifs_tcon *tcon;
2048 struct TCP_Server_Info *server;
2049 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2050 struct cifs_fid fid;
2051 struct cifs_open_parms oparms;
2053 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2056 tlink = cifs_sb_tlink(cifs_sb);
2058 return PTR_ERR(tlink);
2059 tcon = tlink_tcon(tlink);
2060 server = tcon->ses->server;
2062 if (!server->ops->rename)
2065 /* try path-based rename first */
2066 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2069 * Don't bother with rename by filehandle unless file is busy and
2070 * source. Note that cross directory moves do not work with
2071 * rename by filehandle to various Windows servers.
2073 if (rc == 0 || rc != -EBUSY)
2074 goto do_rename_exit;
2076 /* Don't fall back to using SMB on SMB 2+ mount */
2077 if (server->vals->protocol_id != 0)
2078 goto do_rename_exit;
2080 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2081 /* open-file renames don't work across directories */
2082 if (to_dentry->d_parent != from_dentry->d_parent)
2083 goto do_rename_exit;
2086 oparms.cifs_sb = cifs_sb;
2087 /* open the file to be renamed -- we need DELETE perms */
2088 oparms.desired_access = DELETE;
2089 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2090 oparms.disposition = FILE_OPEN;
2091 oparms.path = from_path;
2093 oparms.reconnect = false;
2095 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2097 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2098 (const char *) to_dentry->d_name.name,
2099 cifs_sb->local_nls, cifs_remap(cifs_sb));
2100 CIFSSMBClose(xid, tcon, fid.netfid);
2102 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2105 d_move(from_dentry, to_dentry);
2106 cifs_put_tlink(tlink);
2111 cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
2112 struct dentry *source_dentry, struct inode *target_dir,
2113 struct dentry *target_dentry, unsigned int flags)
2115 const char *from_name, *to_name;
2116 void *page1, *page2;
2117 struct cifs_sb_info *cifs_sb;
2118 struct tcon_link *tlink;
2119 struct cifs_tcon *tcon;
2122 int retry_count = 0;
2123 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2124 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2125 FILE_UNIX_BASIC_INFO *info_buf_target;
2126 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2128 if (flags & ~RENAME_NOREPLACE)
2131 cifs_sb = CIFS_SB(source_dir->i_sb);
2132 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2135 tlink = cifs_sb_tlink(cifs_sb);
2137 return PTR_ERR(tlink);
2138 tcon = tlink_tcon(tlink);
2140 page1 = alloc_dentry_path();
2141 page2 = alloc_dentry_path();
2144 from_name = build_path_from_dentry(source_dentry, page1);
2145 if (IS_ERR(from_name)) {
2146 rc = PTR_ERR(from_name);
2147 goto cifs_rename_exit;
2150 to_name = build_path_from_dentry(target_dentry, page2);
2151 if (IS_ERR(to_name)) {
2152 rc = PTR_ERR(to_name);
2153 goto cifs_rename_exit;
2156 cifs_close_deferred_file_under_dentry(tcon, from_name);
2157 if (d_inode(target_dentry) != NULL)
2158 cifs_close_deferred_file_under_dentry(tcon, to_name);
2160 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2163 if (rc == -EACCES) {
2164 while (retry_count < 3) {
2165 cifs_close_all_deferred_files(tcon);
2166 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2175 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2177 if (flags & RENAME_NOREPLACE)
2178 goto cifs_rename_exit;
2180 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2181 if (rc == -EEXIST && tcon->unix_ext) {
2183 * Are src and dst hardlinks of same inode? We can only tell
2184 * with unix extensions enabled.
2187 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2189 if (info_buf_source == NULL) {
2191 goto cifs_rename_exit;
2194 info_buf_target = info_buf_source + 1;
2195 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2198 cifs_remap(cifs_sb));
2202 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2205 cifs_remap(cifs_sb));
2207 if (tmprc == 0 && (info_buf_source->UniqueId ==
2208 info_buf_target->UniqueId)) {
2209 /* same file, POSIX says that this is a noop */
2211 goto cifs_rename_exit;
2215 * else ... BB we could add the same check for Windows by
2216 * checking the UniqueId via FILE_INTERNAL_INFO
2220 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2222 /* Try unlinking the target dentry if it's not negative */
2223 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2224 if (d_is_dir(target_dentry))
2225 tmprc = cifs_rmdir(target_dir, target_dentry);
2227 tmprc = cifs_unlink(target_dir, target_dentry);
2229 goto cifs_rename_exit;
2230 rc = cifs_do_rename(xid, source_dentry, from_name,
2231 target_dentry, to_name);
2234 /* force revalidate to go get info when needed */
2235 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2237 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2238 target_dir->i_mtime = current_time(source_dir);
2241 kfree(info_buf_source);
2242 free_dentry_path(page2);
2243 free_dentry_path(page1);
2245 cifs_put_tlink(tlink);
2250 cifs_dentry_needs_reval(struct dentry *dentry)
2252 struct inode *inode = d_inode(dentry);
2253 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2254 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2255 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2256 struct cached_fid *cfid = NULL;
2258 if (cifs_i->time == 0)
2261 if (CIFS_CACHE_READ(cifs_i))
2264 if (!lookupCacheEnabled)
2267 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2268 mutex_lock(&cfid->fid_mutex);
2269 if (cfid->time && cifs_i->time > cfid->time) {
2270 mutex_unlock(&cfid->fid_mutex);
2271 close_cached_dir(cfid);
2274 mutex_unlock(&cfid->fid_mutex);
2275 close_cached_dir(cfid);
2278 * depending on inode type, check if attribute caching disabled for
2279 * files or directories
2281 if (S_ISDIR(inode->i_mode)) {
2282 if (!cifs_sb->ctx->acdirmax)
2284 if (!time_in_range(jiffies, cifs_i->time,
2285 cifs_i->time + cifs_sb->ctx->acdirmax))
2288 if (!cifs_sb->ctx->acregmax)
2290 if (!time_in_range(jiffies, cifs_i->time,
2291 cifs_i->time + cifs_sb->ctx->acregmax))
2295 /* hardlinked files w/ noserverino get "special" treatment */
2296 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2297 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2304 * Zap the cache. Called when invalid_mapping flag is set.
2307 cifs_invalidate_mapping(struct inode *inode)
2311 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2312 rc = invalidate_inode_pages2(inode->i_mapping);
2314 cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2322 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2324 * @key: currently unused
2325 * @mode: the task state to sleep in
2328 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2330 freezable_schedule_unsafe();
2331 if (signal_pending_state(mode, current))
2332 return -ERESTARTSYS;
2337 cifs_revalidate_mapping(struct inode *inode)
2340 unsigned long *flags = &CIFS_I(inode)->flags;
2341 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2343 /* swapfiles are not supposed to be shared */
2344 if (IS_SWAPFILE(inode))
2347 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2352 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2353 /* for cache=singleclient, do not invalidate */
2354 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2355 goto skip_invalidate;
2357 rc = cifs_invalidate_mapping(inode);
2359 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2363 clear_bit_unlock(CIFS_INO_LOCK, flags);
2364 smp_mb__after_atomic();
2365 wake_up_bit(flags, CIFS_INO_LOCK);
2371 cifs_zap_mapping(struct inode *inode)
2373 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2374 return cifs_revalidate_mapping(inode);
2377 int cifs_revalidate_file_attr(struct file *filp)
2380 struct dentry *dentry = file_dentry(filp);
2381 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2382 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2383 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2385 if (!cifs_dentry_needs_reval(dentry))
2388 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2389 if (tlink_tcon(cfile->tlink)->unix_ext)
2390 rc = cifs_get_file_info_unix(filp);
2392 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2393 rc = cifs_get_file_info(filp);
2398 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2402 struct inode *inode = d_inode(dentry);
2403 struct super_block *sb = dentry->d_sb;
2404 const char *full_path;
2411 if (!cifs_dentry_needs_reval(dentry))
2416 page = alloc_dentry_path();
2417 full_path = build_path_from_dentry(dentry, page);
2418 if (IS_ERR(full_path)) {
2419 rc = PTR_ERR(full_path);
2423 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2424 full_path, inode, inode->i_count.counter,
2425 dentry, cifs_get_time(dentry), jiffies);
2428 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2429 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2430 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2431 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2433 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2435 if (rc == -EAGAIN && count++ < 10)
2438 free_dentry_path(page);
2444 int cifs_revalidate_file(struct file *filp)
2447 struct inode *inode = file_inode(filp);
2449 rc = cifs_revalidate_file_attr(filp);
2453 return cifs_revalidate_mapping(inode);
2456 /* revalidate a dentry's inode attributes */
2457 int cifs_revalidate_dentry(struct dentry *dentry)
2460 struct inode *inode = d_inode(dentry);
2462 rc = cifs_revalidate_dentry_attr(dentry);
2466 return cifs_revalidate_mapping(inode);
2469 int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
2470 struct kstat *stat, u32 request_mask, unsigned int flags)
2472 struct dentry *dentry = path->dentry;
2473 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2474 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2475 struct inode *inode = d_inode(dentry);
2478 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2482 * We need to be sure that all dirty pages are written and the server
2483 * has actual ctime, mtime and file length.
2485 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2486 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2487 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2488 rc = filemap_fdatawait(inode->i_mapping);
2490 mapping_set_error(inode->i_mapping, rc);
2495 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2496 CIFS_I(inode)->time = 0; /* force revalidate */
2499 * If the caller doesn't require syncing, only sync if
2500 * necessary (e.g. due to earlier truncate or setattr
2501 * invalidating the cached metadata)
2503 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2504 (CIFS_I(inode)->time == 0)) {
2505 rc = cifs_revalidate_dentry_attr(dentry);
2510 generic_fillattr(&init_user_ns, inode, stat);
2511 stat->blksize = cifs_sb->ctx->bsize;
2512 stat->ino = CIFS_I(inode)->uniqueid;
2514 /* old CIFS Unix Extensions doesn't return create time */
2515 if (CIFS_I(inode)->createtime) {
2516 stat->result_mask |= STATX_BTIME;
2518 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2521 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2522 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2523 stat->attributes |= STATX_ATTR_COMPRESSED;
2524 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2525 stat->attributes |= STATX_ATTR_ENCRYPTED;
2528 * If on a multiuser mount without unix extensions or cifsacl being
2529 * enabled, and the admin hasn't overridden them, set the ownership
2530 * to the fsuid/fsgid of the current process.
2532 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2533 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2535 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2536 stat->uid = current_fsuid();
2537 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2538 stat->gid = current_fsgid();
2543 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2546 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2547 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2548 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2549 struct TCP_Server_Info *server = tcon->ses->server;
2550 struct cifsFileInfo *cfile;
2553 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2557 * We need to be sure that all dirty pages are written as they
2558 * might fill holes on the server.
2560 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2561 inode->i_mapping->nrpages != 0) {
2562 rc = filemap_fdatawait(inode->i_mapping);
2564 mapping_set_error(inode->i_mapping, rc);
2569 cfile = find_readable_file(cifs_i, false);
2573 if (server->ops->fiemap) {
2574 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2575 cifsFileInfo_put(cfile);
2579 cifsFileInfo_put(cfile);
2583 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2585 pgoff_t index = from >> PAGE_SHIFT;
2586 unsigned offset = from & (PAGE_SIZE - 1);
2590 page = grab_cache_page(mapping, index);
2594 zero_user_segment(page, offset, PAGE_SIZE);
2600 void cifs_setsize(struct inode *inode, loff_t offset)
2602 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2604 spin_lock(&inode->i_lock);
2605 i_size_write(inode, offset);
2606 spin_unlock(&inode->i_lock);
2608 /* Cached inode must be refreshed on truncate */
2610 truncate_pagecache(inode, offset);
2614 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2615 unsigned int xid, const char *full_path)
2618 struct cifsFileInfo *open_file;
2619 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2620 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2621 struct tcon_link *tlink = NULL;
2622 struct cifs_tcon *tcon = NULL;
2623 struct TCP_Server_Info *server;
2626 * To avoid spurious oplock breaks from server, in the case of
2627 * inodes that we already have open, avoid doing path based
2628 * setting of file size if we can do it by handle.
2629 * This keeps our caching token (oplock) and avoids timeouts
2630 * when the local oplock break takes longer to flush
2631 * writebehind data than the SMB timeout for the SetPathInfo
2632 * request would allow
2634 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2636 tcon = tlink_tcon(open_file->tlink);
2637 server = tcon->ses->server;
2638 if (server->ops->set_file_size)
2639 rc = server->ops->set_file_size(xid, tcon, open_file,
2640 attrs->ia_size, false);
2643 cifsFileInfo_put(open_file);
2644 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2652 tlink = cifs_sb_tlink(cifs_sb);
2654 return PTR_ERR(tlink);
2655 tcon = tlink_tcon(tlink);
2656 server = tcon->ses->server;
2660 * Set file size by pathname rather than by handle either because no
2661 * valid, writeable file handle for it was found or because there was
2662 * an error setting it by handle.
2664 if (server->ops->set_path_size)
2665 rc = server->ops->set_path_size(xid, tcon, full_path,
2666 attrs->ia_size, cifs_sb, false);
2669 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2672 cifs_put_tlink(tlink);
2676 cifsInode->server_eof = attrs->ia_size;
2677 cifs_setsize(inode, attrs->ia_size);
2679 * i_blocks is not related to (i_size / i_blksize), but instead
2680 * 512 byte (2**9) size is required for calculating num blocks.
2681 * Until we can query the server for actual allocation size,
2682 * this is best estimate we have for blocks allocated for a file
2683 * Number of blocks must be rounded up so size 1 is not 0 blocks
2685 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2688 * The man page of truncate says if the size changed,
2689 * then the st_ctime and st_mtime fields for the file
2692 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2693 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2695 cifs_truncate_page(inode->i_mapping, inode->i_size);
2701 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2703 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2707 const char *full_path;
2708 void *page = alloc_dentry_path();
2709 struct inode *inode = d_inode(direntry);
2710 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2711 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2712 struct tcon_link *tlink;
2713 struct cifs_tcon *pTcon;
2714 struct cifs_unix_set_info_args *args = NULL;
2715 struct cifsFileInfo *open_file;
2717 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2718 direntry, attrs->ia_valid);
2722 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2723 attrs->ia_valid |= ATTR_FORCE;
2725 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2729 full_path = build_path_from_dentry(direntry, page);
2730 if (IS_ERR(full_path)) {
2731 rc = PTR_ERR(full_path);
2736 * Attempt to flush data before changing attributes. We need to do
2737 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2738 * ownership or mode then we may also need to do this. Here, we take
2739 * the safe way out and just do the flush on all setattr requests. If
2740 * the flush returns error, store it to report later and continue.
2742 * BB: This should be smarter. Why bother flushing pages that
2743 * will be truncated anyway? Also, should we error out here if
2744 * the flush returns error?
2746 rc = filemap_write_and_wait(inode->i_mapping);
2747 if (is_interrupt_error(rc)) {
2752 mapping_set_error(inode->i_mapping, rc);
2755 if (attrs->ia_valid & ATTR_SIZE) {
2756 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2761 /* skip mode change if it's just for clearing setuid/setgid */
2762 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2763 attrs->ia_valid &= ~ATTR_MODE;
2765 args = kmalloc(sizeof(*args), GFP_KERNEL);
2771 /* set up the struct */
2772 if (attrs->ia_valid & ATTR_MODE)
2773 args->mode = attrs->ia_mode;
2775 args->mode = NO_CHANGE_64;
2777 if (attrs->ia_valid & ATTR_UID)
2778 args->uid = attrs->ia_uid;
2780 args->uid = INVALID_UID; /* no change */
2782 if (attrs->ia_valid & ATTR_GID)
2783 args->gid = attrs->ia_gid;
2785 args->gid = INVALID_GID; /* no change */
2787 if (attrs->ia_valid & ATTR_ATIME)
2788 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2790 args->atime = NO_CHANGE_64;
2792 if (attrs->ia_valid & ATTR_MTIME)
2793 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2795 args->mtime = NO_CHANGE_64;
2797 if (attrs->ia_valid & ATTR_CTIME)
2798 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2800 args->ctime = NO_CHANGE_64;
2803 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2805 u16 nfid = open_file->fid.netfid;
2806 u32 npid = open_file->pid;
2807 pTcon = tlink_tcon(open_file->tlink);
2808 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2809 cifsFileInfo_put(open_file);
2811 tlink = cifs_sb_tlink(cifs_sb);
2812 if (IS_ERR(tlink)) {
2813 rc = PTR_ERR(tlink);
2816 pTcon = tlink_tcon(tlink);
2817 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2819 cifs_remap(cifs_sb));
2820 cifs_put_tlink(tlink);
2826 if ((attrs->ia_valid & ATTR_SIZE) &&
2827 attrs->ia_size != i_size_read(inode)) {
2828 truncate_setsize(inode, attrs->ia_size);
2829 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2832 setattr_copy(&init_user_ns, inode, attrs);
2833 mark_inode_dirty(inode);
2835 /* force revalidate when any of these times are set since some
2836 of the fs types (eg ext3, fat) do not have fine enough
2837 time granularity to match protocol, and we do not have a
2838 a way (yet) to query the server fs's time granularity (and
2839 whether it rounds times down).
2841 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2842 cifsInode->time = 0;
2845 free_dentry_path(page);
2849 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2852 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2855 kuid_t uid = INVALID_UID;
2856 kgid_t gid = INVALID_GID;
2857 struct inode *inode = d_inode(direntry);
2858 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2859 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2860 struct cifsFileInfo *wfile;
2861 struct cifs_tcon *tcon;
2862 const char *full_path;
2863 void *page = alloc_dentry_path();
2866 __u64 mode = NO_CHANGE_64;
2870 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2871 direntry, attrs->ia_valid);
2873 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2874 attrs->ia_valid |= ATTR_FORCE;
2876 rc = setattr_prepare(&init_user_ns, direntry, attrs);
2878 goto cifs_setattr_exit;
2880 full_path = build_path_from_dentry(direntry, page);
2881 if (IS_ERR(full_path)) {
2882 rc = PTR_ERR(full_path);
2883 goto cifs_setattr_exit;
2887 * Attempt to flush data before changing attributes. We need to do
2888 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2889 * returns error, store it to report later and continue.
2891 * BB: This should be smarter. Why bother flushing pages that
2892 * will be truncated anyway? Also, should we error out here if
2893 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2895 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2896 rc = filemap_write_and_wait(inode->i_mapping);
2897 if (is_interrupt_error(rc)) {
2899 goto cifs_setattr_exit;
2901 mapping_set_error(inode->i_mapping, rc);
2906 if ((attrs->ia_valid & ATTR_MTIME) &&
2907 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2908 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2910 tcon = tlink_tcon(wfile->tlink);
2911 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2912 cifsFileInfo_put(wfile);
2914 goto cifs_setattr_exit;
2915 } else if (rc != -EBADF)
2916 goto cifs_setattr_exit;
2921 if (attrs->ia_valid & ATTR_SIZE) {
2922 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2924 goto cifs_setattr_exit;
2927 if (attrs->ia_valid & ATTR_UID)
2928 uid = attrs->ia_uid;
2930 if (attrs->ia_valid & ATTR_GID)
2931 gid = attrs->ia_gid;
2933 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2934 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2935 if (uid_valid(uid) || gid_valid(gid)) {
2936 mode = NO_CHANGE_64;
2937 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2940 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2942 goto cifs_setattr_exit;
2946 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2947 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2949 /* skip mode change if it's just for clearing setuid/setgid */
2950 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2951 attrs->ia_valid &= ~ATTR_MODE;
2953 if (attrs->ia_valid & ATTR_MODE) {
2954 mode = attrs->ia_mode;
2956 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2957 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2958 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2959 INVALID_UID, INVALID_GID);
2961 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2963 goto cifs_setattr_exit;
2967 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
2968 * Pick up the actual mode bits that were set.
2970 if (mode != attrs->ia_mode)
2971 attrs->ia_mode = mode;
2973 if (((mode & S_IWUGO) == 0) &&
2974 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2976 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2978 /* fix up mode if we're not using dynperm */
2979 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2980 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2981 } else if ((mode & S_IWUGO) &&
2982 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2984 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2985 /* Attributes of 0 are ignored */
2987 dosattr |= ATTR_NORMAL;
2989 /* reset local inode permissions to normal */
2990 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2991 attrs->ia_mode &= ~(S_IALLUGO);
2992 if (S_ISDIR(inode->i_mode))
2994 cifs_sb->ctx->dir_mode;
2997 cifs_sb->ctx->file_mode;
2999 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3000 /* ignore mode change - ATTR_READONLY hasn't changed */
3001 attrs->ia_valid &= ~ATTR_MODE;
3005 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3006 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3007 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3008 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3010 /* Even if error on time set, no sense failing the call if
3011 the server would set the time to a reasonable value anyway,
3012 and this check ensures that we are not being called from
3013 sys_utimes in which case we ought to fail the call back to
3014 the user when the server rejects the call */
3015 if ((rc) && (attrs->ia_valid &
3016 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3020 /* do not need local check to inode_check_ok since the server does
3023 goto cifs_setattr_exit;
3025 if ((attrs->ia_valid & ATTR_SIZE) &&
3026 attrs->ia_size != i_size_read(inode)) {
3027 truncate_setsize(inode, attrs->ia_size);
3028 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3031 setattr_copy(&init_user_ns, inode, attrs);
3032 mark_inode_dirty(inode);
3036 free_dentry_path(page);
3041 cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
3042 struct iattr *attrs)
3044 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3045 int rc, retries = 0;
3046 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3047 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3048 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3050 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3054 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3055 if (pTcon->unix_ext)
3056 rc = cifs_setattr_unix(direntry, attrs);
3058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3059 rc = cifs_setattr_nounix(direntry, attrs);
3061 } while (is_retryable_error(rc) && retries < 2);
3063 /* BB: add cifs_setattr_legacy for really old servers */