1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
30 static void cifs_set_ops(struct inode *inode)
32 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
34 switch (inode->i_mode & S_IFMT) {
36 inode->i_op = &cifs_file_inode_ops;
37 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
38 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
39 inode->i_fop = &cifs_file_direct_nobrl_ops;
41 inode->i_fop = &cifs_file_direct_ops;
42 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44 inode->i_fop = &cifs_file_strict_nobrl_ops;
46 inode->i_fop = &cifs_file_strict_ops;
47 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48 inode->i_fop = &cifs_file_nobrl_ops;
49 else { /* not direct, send byte range locks */
50 inode->i_fop = &cifs_file_ops;
53 /* check if server can support readahead */
54 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
55 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
56 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
58 inode->i_data.a_ops = &cifs_addr_ops;
61 if (IS_AUTOMOUNT(inode)) {
62 inode->i_op = &cifs_namespace_inode_operations;
64 inode->i_op = &cifs_dir_inode_ops;
65 inode->i_fop = &cifs_dir_ops;
69 inode->i_op = &cifs_symlink_inode_ops;
72 init_special_inode(inode, inode->i_mode, inode->i_rdev);
77 /* check inode attributes against fattr. If they don't match, tag the
78 * inode for cache invalidation
81 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
83 struct cifs_fscache_inode_coherency_data cd;
84 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
85 struct timespec64 mtime;
87 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
88 __func__, cifs_i->uniqueid);
90 if (inode->i_state & I_NEW) {
91 cifs_dbg(FYI, "%s: inode %llu is new\n",
92 __func__, cifs_i->uniqueid);
96 /* don't bother with revalidation if we have an oplock */
97 if (CIFS_CACHE_READ(cifs_i)) {
98 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
99 __func__, cifs_i->uniqueid);
103 /* revalidate if mtime or size have changed */
104 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
105 mtime = inode_get_mtime(inode);
106 if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
107 cifs_i->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);
116 /* Invalidate fscache cookie */
117 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
118 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
122 * copy nlink to the inode, unless it wasn't provided. Provide
123 * sane values if we don't have an existing one and none was provided
126 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
129 * if we're in a situation where we can't trust what we
130 * got from the server (readdir, some non-unix cases)
131 * fake reasonable values
133 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
134 /* only provide fake values on a new inode */
135 if (inode->i_state & I_NEW) {
136 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
144 /* we trust the server, so update it */
145 set_nlink(inode, fattr->cf_nlink);
148 /* populate an inode with info from a cifs_fattr struct */
150 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
152 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
153 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
155 if (!(inode->i_state & I_NEW) &&
156 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
157 CIFS_I(inode)->time = 0; /* force reval */
161 cifs_revalidate_cache(inode, fattr);
163 spin_lock(&inode->i_lock);
164 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
165 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
166 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
167 /* we do not want atime to be less than mtime, it broke some apps */
168 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
169 inode_set_atime_to_ts(inode, fattr->cf_mtime);
171 inode_set_atime_to_ts(inode, fattr->cf_atime);
172 inode_set_mtime_to_ts(inode, fattr->cf_mtime);
173 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
174 inode->i_rdev = fattr->cf_rdev;
175 cifs_nlink_fattr_to_inode(inode, fattr);
176 inode->i_uid = fattr->cf_uid;
177 inode->i_gid = fattr->cf_gid;
179 /* if dynperm is set, don't clobber existing mode */
180 if (inode->i_state & I_NEW ||
181 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
182 inode->i_mode = fattr->cf_mode;
184 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
186 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
189 cifs_i->time = jiffies;
191 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
192 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
194 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
196 cifs_i->server_eof = fattr->cf_eof;
198 * Can't safely change the file size here if the client is writing to
199 * it due to potential races.
201 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
202 i_size_write(inode, fattr->cf_eof);
205 * i_blocks is not related to (i_size / i_blksize),
206 * but instead 512 byte (2**9) size is required for
207 * calculating num blocks.
209 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
212 if (S_ISLNK(fattr->cf_mode)) {
213 kfree(cifs_i->symlink_target);
214 cifs_i->symlink_target = fattr->cf_symlink_target;
215 fattr->cf_symlink_target = NULL;
217 spin_unlock(&inode->i_lock);
219 if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
220 inode->i_flags |= S_AUTOMOUNT;
221 if (inode->i_state & I_NEW)
227 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
229 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
231 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
234 fattr->cf_uniqueid = iunique(sb, ROOT_I);
237 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
239 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
240 struct cifs_sb_info *cifs_sb)
242 memset(fattr, 0, sizeof(*fattr));
243 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
244 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
245 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
247 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
248 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
249 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
250 /* old POSIX extensions don't get create time */
252 fattr->cf_mode = le64_to_cpu(info->Permissions);
255 * Since we set the inode type below we need to mask off
256 * to avoid strange results if bits set above.
258 fattr->cf_mode &= ~S_IFMT;
259 switch (le32_to_cpu(info->Type)) {
261 fattr->cf_mode |= S_IFREG;
262 fattr->cf_dtype = DT_REG;
265 fattr->cf_mode |= S_IFLNK;
266 fattr->cf_dtype = DT_LNK;
269 fattr->cf_mode |= S_IFDIR;
270 fattr->cf_dtype = DT_DIR;
273 fattr->cf_mode |= S_IFCHR;
274 fattr->cf_dtype = DT_CHR;
275 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
276 le64_to_cpu(info->DevMinor) & MINORMASK);
279 fattr->cf_mode |= S_IFBLK;
280 fattr->cf_dtype = DT_BLK;
281 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
282 le64_to_cpu(info->DevMinor) & MINORMASK);
285 fattr->cf_mode |= S_IFIFO;
286 fattr->cf_dtype = DT_FIFO;
289 fattr->cf_mode |= S_IFSOCK;
290 fattr->cf_dtype = DT_SOCK;
293 /* safest to call it a file if we do not know */
294 fattr->cf_mode |= S_IFREG;
295 fattr->cf_dtype = DT_REG;
296 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
300 fattr->cf_uid = cifs_sb->ctx->linux_uid;
301 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
302 u64 id = le64_to_cpu(info->Uid);
303 if (id < ((uid_t)-1)) {
304 kuid_t uid = make_kuid(&init_user_ns, id);
310 fattr->cf_gid = cifs_sb->ctx->linux_gid;
311 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
312 u64 id = le64_to_cpu(info->Gid);
313 if (id < ((gid_t)-1)) {
314 kgid_t gid = make_kgid(&init_user_ns, id);
320 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
324 * Fill a cifs_fattr struct with fake inode info.
326 * Needed to setup cifs_fattr data for the directory which is the
327 * junction to the new submount (ie to setup the fake directory
328 * which represents a DFS referral or reparse mount point).
330 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
331 struct super_block *sb)
333 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
335 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
337 memset(fattr, 0, sizeof(*fattr));
338 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
339 fattr->cf_uid = cifs_sb->ctx->linux_uid;
340 fattr->cf_gid = cifs_sb->ctx->linux_gid;
341 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
342 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
344 fattr->cf_flags = CIFS_FATTR_JUNCTION;
347 /* Update inode with final fattr data */
348 static int update_inode_info(struct super_block *sb,
349 struct cifs_fattr *fattr,
350 struct inode **inode)
352 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
356 *inode = cifs_iget(sb, fattr);
361 /* We already have inode, update it.
363 * If file type or uniqueid is different, return error.
365 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
366 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
367 CIFS_I(*inode)->time = 0; /* force reval */
370 return cifs_fattr_to_inode(*inode, fattr);
373 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
375 cifs_get_file_info_unix(struct file *filp)
379 FILE_UNIX_BASIC_INFO find_data;
380 struct cifs_fattr fattr = {};
381 struct inode *inode = file_inode(filp);
382 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
383 struct cifsFileInfo *cfile = filp->private_data;
384 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
388 if (cfile->symlink_target) {
389 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
390 if (!fattr.cf_symlink_target) {
392 goto cifs_gfiunix_out;
396 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
398 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
399 } else if (rc == -EREMOTE) {
400 cifs_create_junction_fattr(&fattr, inode->i_sb);
403 goto cifs_gfiunix_out;
405 rc = cifs_fattr_to_inode(inode, &fattr);
412 static int cifs_get_unix_fattr(const unsigned char *full_path,
413 struct super_block *sb,
414 struct cifs_fattr *fattr,
415 struct inode **pinode,
416 const unsigned int xid)
418 struct TCP_Server_Info *server;
419 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
420 FILE_UNIX_BASIC_INFO find_data;
421 struct cifs_tcon *tcon;
422 struct tcon_link *tlink;
425 cifs_dbg(FYI, "Getting info on %s\n", full_path);
427 tlink = cifs_sb_tlink(cifs_sb);
429 return PTR_ERR(tlink);
430 tcon = tlink_tcon(tlink);
431 server = tcon->ses->server;
433 /* could have done a find first instead but this returns more info */
434 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
435 cifs_sb->local_nls, cifs_remap(cifs_sb));
436 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
437 cifs_put_tlink(tlink);
440 cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
441 } else if (rc == -EREMOTE) {
442 cifs_create_junction_fattr(fattr, sb);
449 cifs_fill_uniqueid(sb, fattr);
451 /* check for Minshall+French symlinks */
452 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
453 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
454 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
457 if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
458 if (!server->ops->query_symlink)
460 rc = server->ops->query_symlink(xid, tcon,
462 &fattr->cf_symlink_target,
464 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
469 int cifs_get_inode_info_unix(struct inode **pinode,
470 const unsigned char *full_path,
471 struct super_block *sb, unsigned int xid)
473 struct cifs_fattr fattr = {};
476 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
480 rc = update_inode_info(sb, &fattr, pinode);
482 kfree(fattr.cf_symlink_target);
486 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
487 struct super_block *sb,
488 struct cifs_fattr *fattr,
489 struct inode **pinode,
490 const unsigned int xid)
495 int cifs_get_inode_info_unix(struct inode **pinode,
496 const unsigned char *full_path,
497 struct super_block *sb, unsigned int xid)
501 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
504 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
505 struct cifs_sb_info *cifs_sb, unsigned int xid)
509 struct tcon_link *tlink;
510 struct cifs_tcon *tcon;
512 struct cifs_open_parms oparms;
513 struct cifs_io_parms io_parms = {0};
515 unsigned int bytes_read;
517 int buf_type = CIFS_NO_BUFFER;
521 fattr->cf_mode &= ~S_IFMT;
523 if (fattr->cf_eof == 0) {
524 fattr->cf_mode |= S_IFIFO;
525 fattr->cf_dtype = DT_FIFO;
527 } else if (fattr->cf_eof < 8) {
528 fattr->cf_mode |= S_IFREG;
529 fattr->cf_dtype = DT_REG;
530 return -EINVAL; /* EOPNOTSUPP? */
533 tlink = cifs_sb_tlink(cifs_sb);
535 return PTR_ERR(tlink);
536 tcon = tlink_tcon(tlink);
538 oparms = (struct cifs_open_parms) {
541 .desired_access = GENERIC_READ,
542 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
543 .disposition = FILE_OPEN,
548 if (tcon->ses->server->oplocks)
552 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
554 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
555 cifs_put_tlink(tlink);
560 io_parms.netfid = fid.netfid;
561 io_parms.pid = current->tgid;
562 io_parms.tcon = tcon;
564 io_parms.length = 24;
566 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
567 &bytes_read, &pbuf, &buf_type);
568 if ((rc == 0) && (bytes_read >= 8)) {
569 if (memcmp("IntxBLK", pbuf, 8) == 0) {
570 cifs_dbg(FYI, "Block device\n");
571 fattr->cf_mode |= S_IFBLK;
572 fattr->cf_dtype = DT_BLK;
573 if (bytes_read == 24) {
574 /* we have enough to decode dev num */
575 __u64 mjr; /* major */
576 __u64 mnr; /* minor */
577 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
578 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
579 fattr->cf_rdev = MKDEV(mjr, mnr);
581 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
582 cifs_dbg(FYI, "Char device\n");
583 fattr->cf_mode |= S_IFCHR;
584 fattr->cf_dtype = DT_CHR;
585 if (bytes_read == 24) {
586 /* we have enough to decode dev num */
587 __u64 mjr; /* major */
588 __u64 mnr; /* minor */
589 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
590 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
591 fattr->cf_rdev = MKDEV(mjr, mnr);
593 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
594 cifs_dbg(FYI, "Symlink\n");
595 fattr->cf_mode |= S_IFLNK;
596 fattr->cf_dtype = DT_LNK;
598 fattr->cf_mode |= S_IFREG; /* file? */
599 fattr->cf_dtype = DT_REG;
603 fattr->cf_mode |= S_IFREG; /* then it is a file */
604 fattr->cf_dtype = DT_REG;
605 rc = -EOPNOTSUPP; /* or some unknown SFU type */
608 tcon->ses->server->ops->close(xid, tcon, &fid);
609 cifs_put_tlink(tlink);
613 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
616 * Fetch mode bits as provided by SFU.
618 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
620 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
621 struct cifs_sb_info *cifs_sb, unsigned int xid)
623 #ifdef CONFIG_CIFS_XATTR
627 struct tcon_link *tlink;
628 struct cifs_tcon *tcon;
630 tlink = cifs_sb_tlink(cifs_sb);
632 return PTR_ERR(tlink);
633 tcon = tlink_tcon(tlink);
635 if (tcon->ses->server->ops->query_all_EAs == NULL) {
636 cifs_put_tlink(tlink);
640 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
641 "SETFILEBITS", ea_value, 4 /* size of buf */,
643 cifs_put_tlink(tlink);
647 mode = le32_to_cpu(*((__le32 *)ea_value));
648 fattr->cf_mode &= ~SFBITS_MASK;
649 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
650 mode, fattr->cf_mode);
651 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
652 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
661 /* Fill a cifs_fattr struct with info from POSIX info struct */
662 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
663 struct cifs_open_info_data *data,
664 struct cifs_sid *owner,
665 struct cifs_sid *group,
666 struct super_block *sb)
668 struct smb311_posix_qinfo *info = &data->posix_fi;
669 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
670 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
672 memset(fattr, 0, sizeof(*fattr));
674 /* no fattr->flags to set */
675 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
676 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
678 if (info->LastAccessTime)
679 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
681 ktime_get_coarse_real_ts64(&fattr->cf_atime);
683 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
684 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
686 if (data->adjust_tz) {
687 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
688 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
691 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
692 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
693 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
695 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
696 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
697 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
698 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
701 fattr->cf_mode |= S_IFLNK;
702 fattr->cf_dtype = DT_LNK;
703 fattr->cf_symlink_target = data->symlink_target;
704 data->symlink_target = NULL;
705 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
706 fattr->cf_mode |= S_IFDIR;
707 fattr->cf_dtype = DT_DIR;
709 fattr->cf_mode |= S_IFREG;
710 fattr->cf_dtype = DT_REG;
712 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
714 sid_to_id(cifs_sb, owner, fattr, SIDOWNER);
715 sid_to_id(cifs_sb, group, fattr, SIDGROUP);
717 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
718 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
721 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
722 struct cifs_fattr *fattr,
726 case IO_REPARSE_TAG_LX_SYMLINK:
727 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
728 fattr->cf_dtype = DT_LNK;
730 case IO_REPARSE_TAG_LX_FIFO:
731 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
732 fattr->cf_dtype = DT_FIFO;
734 case IO_REPARSE_TAG_AF_UNIX:
735 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
736 fattr->cf_dtype = DT_SOCK;
738 case IO_REPARSE_TAG_LX_CHR:
739 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
740 fattr->cf_dtype = DT_CHR;
742 case IO_REPARSE_TAG_LX_BLK:
743 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
744 fattr->cf_dtype = DT_BLK;
746 case 0: /* SMB1 symlink */
747 case IO_REPARSE_TAG_SYMLINK:
748 case IO_REPARSE_TAG_NFS:
749 fattr->cf_mode = S_IFLNK;
750 fattr->cf_dtype = DT_LNK;
758 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
759 struct cifs_open_info_data *data,
760 struct super_block *sb)
762 struct smb2_file_all_info *info = &data->fi;
763 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
764 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
766 memset(fattr, 0, sizeof(*fattr));
767 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
768 if (info->DeletePending)
769 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
771 if (info->LastAccessTime)
772 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
774 ktime_get_coarse_real_ts64(&fattr->cf_atime);
776 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
777 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
779 if (data->adjust_tz) {
780 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
781 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
784 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
785 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
786 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
787 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
789 if (cifs_open_data_reparse(data) &&
790 cifs_reparse_point_to_fattr(cifs_sb, fattr, data->reparse_tag))
793 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
794 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
795 fattr->cf_dtype = DT_DIR;
797 * Server can return wrong NumberOfLinks value for directories
798 * when Unix extensions are disabled - fake it.
801 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
803 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
804 fattr->cf_dtype = DT_REG;
806 /* clear write bits if ATTR_READONLY is set */
807 if (fattr->cf_cifsattrs & ATTR_READONLY)
808 fattr->cf_mode &= ~(S_IWUGO);
811 * Don't accept zero nlink from non-unix servers unless
812 * delete is pending. Instead mark it as unknown.
814 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
815 !info->DeletePending) {
816 cifs_dbg(VFS, "bogus file nlink value %u\n",
818 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
823 if (S_ISLNK(fattr->cf_mode)) {
824 fattr->cf_symlink_target = data->symlink_target;
825 data->symlink_target = NULL;
828 fattr->cf_uid = cifs_sb->ctx->linux_uid;
829 fattr->cf_gid = cifs_sb->ctx->linux_gid;
833 cifs_get_file_info(struct file *filp)
837 struct cifs_open_info_data data = {};
838 struct cifs_fattr fattr;
839 struct inode *inode = file_inode(filp);
840 struct cifsFileInfo *cfile = filp->private_data;
841 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
842 struct TCP_Server_Info *server = tcon->ses->server;
844 if (!server->ops->query_file_info)
848 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
851 /* TODO: add support to query reparse tag */
852 data.adjust_tz = false;
853 if (data.symlink_target) {
855 data.reparse_tag = IO_REPARSE_TAG_SYMLINK;
857 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
860 cifs_create_junction_fattr(&fattr, inode->i_sb);
866 * FIXME: legacy server -- fall back to path-based call?
867 * for now, just skip revalidating and mark inode for
871 CIFS_I(inode)->time = 0;
878 * don't bother with SFU junk here -- just mark inode as needing
881 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
882 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
883 /* if filetype is different, return error */
884 rc = cifs_fattr_to_inode(inode, &fattr);
886 cifs_free_open_info(&data);
891 /* Simple function to return a 64 bit hash of string. Rarely called */
892 static __u64 simple_hashstr(const char *str)
894 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
898 hash = (hash + (__u64) *str++) * hash_mult;
903 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
905 * cifs_backup_query_path_info - SMB1 fallback code to get ino
907 * Fallback code to get file metadata when we don't have access to
908 * full_path (EACCES) and have backup creds.
910 * @xid: transaction id used to identify original request in logs
911 * @tcon: information about the server share we have mounted
912 * @sb: the superblock stores info such as disk space available
913 * @full_path: name of the file we are getting the metadata for
914 * @resp_buf: will be set to cifs resp buf and needs to be freed with
915 * cifs_buf_release() when done with @data
916 * @data: will be set to search info result buffer
919 cifs_backup_query_path_info(int xid,
920 struct cifs_tcon *tcon,
921 struct super_block *sb,
922 const char *full_path,
924 FILE_ALL_INFO **data)
926 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
927 struct cifs_search_info info = {0};
932 info.endOfSearch = false;
934 info.info_level = SMB_FIND_FILE_UNIX;
935 else if ((tcon->ses->capabilities &
936 tcon->ses->server->vals->cap_nt_find) == 0)
937 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
938 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
939 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
940 else /* no srvino useful for fallback to some netapp */
941 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
943 flags = CIFS_SEARCH_CLOSE_ALWAYS |
944 CIFS_SEARCH_CLOSE_AT_END |
945 CIFS_SEARCH_BACKUP_SEARCH;
947 rc = CIFSFindFirst(xid, tcon, full_path,
948 cifs_sb, NULL, flags, &info, false);
952 *resp_buf = (void *)info.ntwrk_buf_start;
953 *data = (FILE_ALL_INFO *)info.srch_entries_start;
956 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
958 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
959 struct inode **inode, const char *full_path,
960 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
962 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
963 struct TCP_Server_Info *server = tcon->ses->server;
966 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
968 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
970 fattr->cf_uniqueid = iunique(sb, ROOT_I);
975 * If we have an inode pass a NULL tcon to ensure we don't
976 * make a round trip to the server. This only works for SMB2+.
978 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
979 &fattr->cf_uniqueid, data);
982 * If that fails reuse existing ino or generate one
983 * and disable server ones
986 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
988 fattr->cf_uniqueid = iunique(sb, ROOT_I);
989 cifs_autodisable_serverino(cifs_sb);
994 /* If no errors, check for zero root inode (invalid) */
995 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
996 cifs_dbg(FYI, "Invalid (0) inodenum\n");
999 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1001 /* make an ino by hashing the UNC */
1002 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1003 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1008 static inline bool is_inode_cache_good(struct inode *ino)
1010 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1013 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1014 struct super_block *sb,
1015 const unsigned int xid,
1016 struct cifs_tcon *tcon,
1017 const char *full_path,
1018 struct cifs_fattr *fattr)
1020 struct TCP_Server_Info *server = tcon->ses->server;
1021 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1022 struct kvec rsp_iov, *iov = NULL;
1023 int rsp_buftype = CIFS_NO_BUFFER;
1024 u32 tag = data->reparse_tag;
1027 if (!tag && server->ops->query_reparse_point) {
1028 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1030 &rsp_iov, &rsp_buftype);
1034 switch ((data->reparse_tag = tag)) {
1035 case 0: /* SMB1 symlink */
1038 case IO_REPARSE_TAG_NFS:
1039 case IO_REPARSE_TAG_SYMLINK:
1040 if (!data->symlink_target && server->ops->query_symlink) {
1041 rc = server->ops->query_symlink(xid, tcon,
1043 &data->symlink_target,
1047 case IO_REPARSE_TAG_MOUNT_POINT:
1048 cifs_create_junction_fattr(fattr, sb);
1052 cifs_open_info_to_fattr(fattr, data, sb);
1054 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1058 static int cifs_get_fattr(struct cifs_open_info_data *data,
1059 struct super_block *sb, int xid,
1060 const struct cifs_fid *fid,
1061 struct cifs_fattr *fattr,
1062 struct inode **inode,
1063 const char *full_path)
1065 struct cifs_open_info_data tmp_data = {};
1066 struct cifs_tcon *tcon;
1067 struct TCP_Server_Info *server;
1068 struct tcon_link *tlink;
1069 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1070 void *smb1_backup_rsp_buf = NULL;
1074 tlink = cifs_sb_tlink(cifs_sb);
1076 return PTR_ERR(tlink);
1077 tcon = tlink_tcon(tlink);
1078 server = tcon->ses->server;
1081 * 1. Fetch file metadata if not provided (data)
1085 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1086 full_path, &tmp_data);
1091 * 2. Convert it to internal cifs metadata (fattr)
1097 * If the file is a reparse point, it is more complicated
1098 * since we have to check if its reparse tag matches a known
1099 * special file type e.g. symlink or fifo or char etc.
1101 if (cifs_open_data_reparse(data)) {
1102 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1105 cifs_open_info_to_fattr(fattr, data, sb);
1109 /* DFS link, no metadata available on this server */
1110 cifs_create_junction_fattr(fattr, sb);
1114 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1116 * perm errors, try again with backup flags if possible
1118 * For SMB2 and later the backup intent flag
1119 * is already sent if needed on open and there
1120 * is no path based FindFirst operation to use
1123 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1124 /* for easier reading */
1126 FILE_DIRECTORY_INFO *fdi;
1127 SEARCH_ID_FULL_DIR_INFO *si;
1129 rc = cifs_backup_query_path_info(xid, tcon, sb,
1131 &smb1_backup_rsp_buf,
1136 move_cifs_info_to_smb2(&data->fi, fi);
1137 fdi = (FILE_DIRECTORY_INFO *)fi;
1138 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1140 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1141 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1142 /* uniqueid set, skip get inum step */
1143 goto handle_mnt_opt;
1145 /* nothing we can do, bail out */
1150 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1153 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1158 * 3. Get or update inode number (fattr->cf_uniqueid)
1161 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1164 * 4. Tweak fattr based on mount options
1166 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1168 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1169 /* query for SFU type info if supported and needed */
1170 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1171 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1172 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1174 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1177 /* fill in 0777 bits from ACL */
1178 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1179 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1180 true, full_path, fid);
1184 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1188 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1189 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1190 false, full_path, fid);
1194 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1200 /* fill in remaining high mode bits e.g. SUID, VTX */
1201 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1202 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1204 /* check for Minshall+French symlinks */
1205 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1206 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1207 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1211 cifs_buf_release(smb1_backup_rsp_buf);
1212 cifs_put_tlink(tlink);
1213 cifs_free_open_info(&tmp_data);
1217 int cifs_get_inode_info(struct inode **inode,
1218 const char *full_path,
1219 struct cifs_open_info_data *data,
1220 struct super_block *sb, int xid,
1221 const struct cifs_fid *fid)
1223 struct cifs_fattr fattr = {};
1226 if (is_inode_cache_good(*inode)) {
1227 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1231 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1235 rc = update_inode_info(sb, &fattr, inode);
1237 kfree(fattr.cf_symlink_target);
1241 static int smb311_posix_get_fattr(struct cifs_fattr *fattr,
1242 const char *full_path,
1243 struct super_block *sb,
1244 const unsigned int xid)
1246 struct cifs_open_info_data data = {};
1247 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1248 struct cifs_tcon *tcon;
1249 struct tcon_link *tlink;
1250 struct cifs_sid owner, group;
1254 tlink = cifs_sb_tlink(cifs_sb);
1256 return PTR_ERR(tlink);
1257 tcon = tlink_tcon(tlink);
1260 * 1. Fetch file metadata
1263 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1268 * 2. Convert it to internal cifs metadata (fattr)
1273 smb311_posix_info_to_fattr(fattr, &data, &owner, &group, sb);
1276 /* DFS link, no metadata available on this server */
1277 cifs_create_junction_fattr(fattr, sb);
1282 * For SMB2 and later the backup intent flag
1283 * is already sent if needed on open and there
1284 * is no path based FindFirst operation to use
1285 * to retry with so nothing we can do, bail out
1289 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1294 * 3. Tweak fattr based on mount options
1296 /* check for Minshall+French symlinks */
1297 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1298 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1299 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1303 cifs_put_tlink(tlink);
1304 cifs_free_open_info(&data);
1308 int smb311_posix_get_inode_info(struct inode **inode, const char *full_path,
1309 struct super_block *sb, const unsigned int xid)
1311 struct cifs_fattr fattr = {};
1314 if (is_inode_cache_good(*inode)) {
1315 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1319 rc = smb311_posix_get_fattr(&fattr, full_path, sb, xid);
1323 rc = update_inode_info(sb, &fattr, inode);
1325 kfree(fattr.cf_symlink_target);
1329 static const struct inode_operations cifs_ipc_inode_ops = {
1330 .lookup = cifs_lookup,
1334 cifs_find_inode(struct inode *inode, void *opaque)
1336 struct cifs_fattr *fattr = opaque;
1338 /* don't match inode with different uniqueid */
1339 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1342 /* use createtime like an i_generation field */
1343 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1346 /* don't match inode of different type */
1347 if (inode_wrong_type(inode, fattr->cf_mode))
1350 /* if it's not a directory or has no dentries, then flag it */
1351 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1352 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1358 cifs_init_inode(struct inode *inode, void *opaque)
1360 struct cifs_fattr *fattr = opaque;
1362 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1363 CIFS_I(inode)->createtime = fattr->cf_createtime;
1368 * walk dentry list for an inode and report whether it has aliases that
1369 * are hashed. We use this to determine if a directory inode can actually
1373 inode_has_hashed_dentries(struct inode *inode)
1375 struct dentry *dentry;
1377 spin_lock(&inode->i_lock);
1378 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1379 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1380 spin_unlock(&inode->i_lock);
1384 spin_unlock(&inode->i_lock);
1388 /* Given fattrs, get a corresponding inode */
1390 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1393 struct inode *inode;
1396 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1398 /* hash down to 32-bits on 32-bit arch */
1399 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1401 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1403 /* was there a potentially problematic inode collision? */
1404 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1405 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1407 if (inode_has_hashed_dentries(inode)) {
1408 cifs_autodisable_serverino(CIFS_SB(sb));
1410 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1411 goto retry_iget5_locked;
1415 /* can't fail - see cifs_find_inode() */
1416 cifs_fattr_to_inode(inode, fattr);
1417 if (sb->s_flags & SB_NOATIME)
1418 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1419 if (inode->i_state & I_NEW) {
1420 inode->i_ino = hash;
1421 cifs_fscache_get_inode_cookie(inode);
1422 unlock_new_inode(inode);
1429 /* gets root inode */
1430 struct inode *cifs_root_iget(struct super_block *sb)
1432 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1433 struct cifs_fattr fattr = {};
1434 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1435 struct inode *inode = NULL;
1441 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1442 && cifs_sb->prepath) {
1443 len = strlen(cifs_sb->prepath);
1444 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1446 return ERR_PTR(-ENOMEM);
1448 memcpy(path+1, cifs_sb->prepath, len);
1450 path = kstrdup("", GFP_KERNEL);
1452 return ERR_PTR(-ENOMEM);
1456 if (tcon->unix_ext) {
1457 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1458 /* some servers mistakenly claim POSIX support */
1459 if (rc != -EOPNOTSUPP)
1461 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1462 tcon->unix_ext = false;
1465 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1466 if (tcon->posix_extensions)
1467 rc = smb311_posix_get_fattr(&fattr, path, sb, xid);
1469 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1473 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1474 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1475 cifs_autodisable_serverino(cifs_sb);
1477 inode = cifs_iget(sb, &fattr);
1481 inode = ERR_PTR(rc);
1485 if (rc && tcon->pipe) {
1486 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1487 spin_lock(&inode->i_lock);
1488 inode->i_mode |= S_IFDIR;
1489 set_nlink(inode, 2);
1490 inode->i_op = &cifs_ipc_inode_ops;
1491 inode->i_fop = &simple_dir_operations;
1492 inode->i_uid = cifs_sb->ctx->linux_uid;
1493 inode->i_gid = cifs_sb->ctx->linux_gid;
1494 spin_unlock(&inode->i_lock);
1497 inode = ERR_PTR(rc);
1503 kfree(fattr.cf_symlink_target);
1508 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1509 const char *full_path, __u32 dosattr)
1511 bool set_time = false;
1512 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1513 struct TCP_Server_Info *server;
1514 FILE_BASIC_INFO info_buf;
1519 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1520 if (!server->ops->set_file_info)
1525 if (attrs->ia_valid & ATTR_ATIME) {
1527 info_buf.LastAccessTime =
1528 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1530 info_buf.LastAccessTime = 0;
1532 if (attrs->ia_valid & ATTR_MTIME) {
1534 info_buf.LastWriteTime =
1535 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1537 info_buf.LastWriteTime = 0;
1540 * Samba throws this field away, but windows may actually use it.
1541 * Do not set ctime unless other time stamps are changed explicitly
1542 * (i.e. by utimes()) since we would then have a mix of client and
1545 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1546 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1547 info_buf.ChangeTime =
1548 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1550 info_buf.ChangeTime = 0;
1552 info_buf.CreationTime = 0; /* don't change */
1553 info_buf.Attributes = cpu_to_le32(dosattr);
1555 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1558 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1560 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1561 * and rename it to a random name that hopefully won't conflict with
1565 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1566 const unsigned int xid)
1570 struct cifs_fid fid;
1571 struct cifs_open_parms oparms;
1572 struct inode *inode = d_inode(dentry);
1573 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1574 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1575 struct tcon_link *tlink;
1576 struct cifs_tcon *tcon;
1577 __u32 dosattr, origattr;
1578 FILE_BASIC_INFO *info_buf = NULL;
1580 tlink = cifs_sb_tlink(cifs_sb);
1582 return PTR_ERR(tlink);
1583 tcon = tlink_tcon(tlink);
1586 * We cannot rename the file if the server doesn't support
1587 * CAP_INFOLEVEL_PASSTHRU
1589 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1594 oparms = (struct cifs_open_parms) {
1597 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1598 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1599 .disposition = FILE_OPEN,
1604 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1608 origattr = cifsInode->cifsAttrs;
1610 origattr |= ATTR_NORMAL;
1612 dosattr = origattr & ~ATTR_READONLY;
1614 dosattr |= ATTR_NORMAL;
1615 dosattr |= ATTR_HIDDEN;
1617 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1618 if (dosattr != origattr) {
1619 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1620 if (info_buf == NULL) {
1624 info_buf->Attributes = cpu_to_le32(dosattr);
1625 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1627 /* although we would like to mark the file hidden
1628 if that fails we will still try to rename it */
1630 cifsInode->cifsAttrs = dosattr;
1632 dosattr = origattr; /* since not able to change them */
1635 /* rename the file */
1636 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1638 cifs_remap(cifs_sb));
1644 /* try to set DELETE_ON_CLOSE */
1645 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1646 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1649 * some samba versions return -ENOENT when we try to set the
1650 * file disposition here. Likely a samba bug, but work around
1651 * it for now. This means that some cifsXXX files may hang
1652 * around after they shouldn't.
1654 * BB: remove this hack after more servers have the fix
1662 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1666 CIFSSMBClose(xid, tcon, fid.netfid);
1669 cifs_put_tlink(tlink);
1673 * reset everything back to the original state. Don't bother
1674 * dealing with errors here since we can't do anything about
1678 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1679 cifs_sb->local_nls, cifs_remap(cifs_sb));
1681 if (dosattr != origattr) {
1682 info_buf->Attributes = cpu_to_le32(origattr);
1683 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1685 cifsInode->cifsAttrs = origattr;
1690 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1692 /* copied from fs/nfs/dir.c with small changes */
1694 cifs_drop_nlink(struct inode *inode)
1696 spin_lock(&inode->i_lock);
1697 if (inode->i_nlink > 0)
1699 spin_unlock(&inode->i_lock);
1703 * If d_inode(dentry) is null (usually meaning the cached dentry
1704 * is a negative dentry) then we would attempt a standard SMB delete, but
1705 * if that fails we can not attempt the fall back mechanisms on EACCES
1706 * but will return the EACCES to the caller. Note that the VFS does not call
1707 * unlink on negative dentries currently.
1709 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1713 const char *full_path;
1715 struct inode *inode = d_inode(dentry);
1716 struct cifsInodeInfo *cifs_inode;
1717 struct super_block *sb = dir->i_sb;
1718 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1719 struct tcon_link *tlink;
1720 struct cifs_tcon *tcon;
1721 struct TCP_Server_Info *server;
1722 struct iattr *attrs = NULL;
1723 __u32 dosattr = 0, origattr = 0;
1725 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1727 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1730 tlink = cifs_sb_tlink(cifs_sb);
1732 return PTR_ERR(tlink);
1733 tcon = tlink_tcon(tlink);
1734 server = tcon->ses->server;
1737 page = alloc_dentry_path();
1739 if (tcon->nodelete) {
1744 /* Unlink can be called from rename so we can not take the
1745 * sb->s_vfs_rename_mutex here */
1746 full_path = build_path_from_dentry(dentry, page);
1747 if (IS_ERR(full_path)) {
1748 rc = PTR_ERR(full_path);
1752 cifs_close_deferred_file_under_dentry(tcon, full_path);
1753 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1754 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1755 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1756 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1757 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1758 cifs_remap(cifs_sb));
1759 cifs_dbg(FYI, "posix del rc %d\n", rc);
1760 if ((rc == 0) || (rc == -ENOENT))
1761 goto psx_del_no_retry;
1763 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1766 if (!server->ops->unlink) {
1768 goto psx_del_no_retry;
1771 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1776 cifs_drop_nlink(inode);
1777 } else if (rc == -ENOENT) {
1779 } else if (rc == -EBUSY) {
1780 if (server->ops->rename_pending_delete) {
1781 rc = server->ops->rename_pending_delete(full_path,
1784 cifs_drop_nlink(inode);
1786 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1787 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1788 if (attrs == NULL) {
1793 /* try to reset dos attributes */
1794 cifs_inode = CIFS_I(inode);
1795 origattr = cifs_inode->cifsAttrs;
1797 origattr |= ATTR_NORMAL;
1798 dosattr = origattr & ~ATTR_READONLY;
1800 dosattr |= ATTR_NORMAL;
1801 dosattr |= ATTR_HIDDEN;
1803 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1807 goto retry_std_delete;
1810 /* undo the setattr if we errored out and it's needed */
1811 if (rc != 0 && dosattr != 0)
1812 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1816 cifs_inode = CIFS_I(inode);
1817 cifs_inode->time = 0; /* will force revalidate to get info
1819 inode_set_ctime_current(inode);
1821 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1822 cifs_inode = CIFS_I(dir);
1823 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1825 free_dentry_path(page);
1828 cifs_put_tlink(tlink);
1833 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1834 const char *full_path, struct cifs_sb_info *cifs_sb,
1835 struct cifs_tcon *tcon, const unsigned int xid)
1838 struct inode *inode = NULL;
1840 if (tcon->posix_extensions)
1841 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1842 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1843 else if (tcon->unix_ext)
1844 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1846 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1848 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1854 if (!S_ISDIR(inode->i_mode)) {
1856 * mkdir succeeded, but another client has managed to remove the
1857 * sucker and replace it with non-directory. Return success,
1858 * but don't leave the child in dcache.
1865 * setting nlink not necessary except in cases where we failed to get it
1866 * from the server or was set bogus. Also, since this is a brand new
1867 * inode, no need to grab the i_lock before setting the i_nlink.
1869 if (inode->i_nlink < 2)
1870 set_nlink(inode, 2);
1871 mode &= ~current_umask();
1872 /* must turn on setgid bit if parent dir has it */
1873 if (parent->i_mode & S_ISGID)
1876 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1877 if (tcon->unix_ext) {
1878 struct cifs_unix_set_info_args args = {
1880 .ctime = NO_CHANGE_64,
1881 .atime = NO_CHANGE_64,
1882 .mtime = NO_CHANGE_64,
1885 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1886 args.uid = current_fsuid();
1887 if (parent->i_mode & S_ISGID)
1888 args.gid = parent->i_gid;
1890 args.gid = current_fsgid();
1892 args.uid = INVALID_UID; /* no change */
1893 args.gid = INVALID_GID; /* no change */
1895 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1897 cifs_remap(cifs_sb));
1901 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1902 struct TCP_Server_Info *server = tcon->ses->server;
1903 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1904 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1905 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1907 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1908 inode->i_mode = (mode | S_IFDIR);
1910 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1911 inode->i_uid = current_fsuid();
1912 if (inode->i_mode & S_ISGID)
1913 inode->i_gid = parent->i_gid;
1915 inode->i_gid = current_fsgid();
1918 d_instantiate(dentry, inode);
1922 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1924 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1925 const char *full_path, struct cifs_sb_info *cifs_sb,
1926 struct cifs_tcon *tcon, const unsigned int xid)
1930 FILE_UNIX_BASIC_INFO *info = NULL;
1931 struct inode *newinode = NULL;
1932 struct cifs_fattr fattr;
1934 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1937 goto posix_mkdir_out;
1940 mode &= ~current_umask();
1941 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1942 NULL /* netfid */, info, &oplock, full_path,
1943 cifs_sb->local_nls, cifs_remap(cifs_sb));
1944 if (rc == -EOPNOTSUPP)
1945 goto posix_mkdir_out;
1947 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1949 goto posix_mkdir_out;
1952 if (info->Type == cpu_to_le32(-1))
1953 /* no return info, go query for it */
1954 goto posix_mkdir_get_info;
1956 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1957 * need to set uid/gid.
1960 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1961 cifs_fill_uniqueid(inode->i_sb, &fattr);
1962 newinode = cifs_iget(inode->i_sb, &fattr);
1964 goto posix_mkdir_get_info;
1966 d_instantiate(dentry, newinode);
1968 #ifdef CONFIG_CIFS_DEBUG2
1969 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1970 dentry, dentry, newinode);
1972 if (newinode->i_nlink != 2)
1973 cifs_dbg(FYI, "unexpected number of links %d\n",
1980 posix_mkdir_get_info:
1981 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1983 goto posix_mkdir_out;
1985 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1987 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
1988 struct dentry *direntry, umode_t mode)
1992 struct cifs_sb_info *cifs_sb;
1993 struct tcon_link *tlink;
1994 struct cifs_tcon *tcon;
1995 struct TCP_Server_Info *server;
1996 const char *full_path;
1999 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2002 cifs_sb = CIFS_SB(inode->i_sb);
2003 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2005 tlink = cifs_sb_tlink(cifs_sb);
2007 return PTR_ERR(tlink);
2008 tcon = tlink_tcon(tlink);
2012 page = alloc_dentry_path();
2013 full_path = build_path_from_dentry(direntry, page);
2014 if (IS_ERR(full_path)) {
2015 rc = PTR_ERR(full_path);
2019 server = tcon->ses->server;
2021 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2022 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2024 d_drop(direntry); /* for time being always refresh inode info */
2028 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2029 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2030 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2031 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2033 if (rc != -EOPNOTSUPP)
2036 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2038 if (!server->ops->mkdir) {
2043 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2044 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2046 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2051 /* TODO: skip this for smb2/smb3 */
2052 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2056 * Force revalidate to get parent dir info when needed since cached
2057 * attributes are invalid now.
2059 CIFS_I(inode)->time = 0;
2060 free_dentry_path(page);
2062 cifs_put_tlink(tlink);
2066 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2070 struct cifs_sb_info *cifs_sb;
2071 struct tcon_link *tlink;
2072 struct cifs_tcon *tcon;
2073 struct TCP_Server_Info *server;
2074 const char *full_path;
2075 void *page = alloc_dentry_path();
2076 struct cifsInodeInfo *cifsInode;
2078 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2082 full_path = build_path_from_dentry(direntry, page);
2083 if (IS_ERR(full_path)) {
2084 rc = PTR_ERR(full_path);
2088 cifs_sb = CIFS_SB(inode->i_sb);
2089 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2094 tlink = cifs_sb_tlink(cifs_sb);
2095 if (IS_ERR(tlink)) {
2096 rc = PTR_ERR(tlink);
2099 tcon = tlink_tcon(tlink);
2100 server = tcon->ses->server;
2102 if (!server->ops->rmdir) {
2104 cifs_put_tlink(tlink);
2108 if (tcon->nodelete) {
2110 cifs_put_tlink(tlink);
2114 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2115 cifs_put_tlink(tlink);
2118 spin_lock(&d_inode(direntry)->i_lock);
2119 i_size_write(d_inode(direntry), 0);
2120 clear_nlink(d_inode(direntry));
2121 spin_unlock(&d_inode(direntry)->i_lock);
2124 cifsInode = CIFS_I(d_inode(direntry));
2125 /* force revalidate to go get info when needed */
2126 cifsInode->time = 0;
2128 cifsInode = CIFS_I(inode);
2130 * Force revalidate to get parent dir info when needed since cached
2131 * attributes are invalid now.
2133 cifsInode->time = 0;
2135 inode_set_ctime_current(d_inode(direntry));
2136 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2139 free_dentry_path(page);
2145 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2146 const char *from_path, struct dentry *to_dentry,
2147 const char *to_path)
2149 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2150 struct tcon_link *tlink;
2151 struct cifs_tcon *tcon;
2152 struct TCP_Server_Info *server;
2153 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2154 struct cifs_fid fid;
2155 struct cifs_open_parms oparms;
2157 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2160 tlink = cifs_sb_tlink(cifs_sb);
2162 return PTR_ERR(tlink);
2163 tcon = tlink_tcon(tlink);
2164 server = tcon->ses->server;
2166 if (!server->ops->rename)
2169 /* try path-based rename first */
2170 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2173 * Don't bother with rename by filehandle unless file is busy and
2174 * source. Note that cross directory moves do not work with
2175 * rename by filehandle to various Windows servers.
2177 if (rc == 0 || rc != -EBUSY)
2178 goto do_rename_exit;
2180 /* Don't fall back to using SMB on SMB 2+ mount */
2181 if (server->vals->protocol_id != 0)
2182 goto do_rename_exit;
2184 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2185 /* open-file renames don't work across directories */
2186 if (to_dentry->d_parent != from_dentry->d_parent)
2187 goto do_rename_exit;
2189 oparms = (struct cifs_open_parms) {
2192 /* open the file to be renamed -- we need DELETE perms */
2193 .desired_access = DELETE,
2194 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2195 .disposition = FILE_OPEN,
2200 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2202 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2203 (const char *) to_dentry->d_name.name,
2204 cifs_sb->local_nls, cifs_remap(cifs_sb));
2205 CIFSSMBClose(xid, tcon, fid.netfid);
2207 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2210 d_move(from_dentry, to_dentry);
2211 cifs_put_tlink(tlink);
2216 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2217 struct dentry *source_dentry, struct inode *target_dir,
2218 struct dentry *target_dentry, unsigned int flags)
2220 const char *from_name, *to_name;
2221 void *page1, *page2;
2222 struct cifs_sb_info *cifs_sb;
2223 struct tcon_link *tlink;
2224 struct cifs_tcon *tcon;
2227 int retry_count = 0;
2228 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2229 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2230 FILE_UNIX_BASIC_INFO *info_buf_target;
2231 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2233 if (flags & ~RENAME_NOREPLACE)
2236 cifs_sb = CIFS_SB(source_dir->i_sb);
2237 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2240 tlink = cifs_sb_tlink(cifs_sb);
2242 return PTR_ERR(tlink);
2243 tcon = tlink_tcon(tlink);
2245 page1 = alloc_dentry_path();
2246 page2 = alloc_dentry_path();
2249 from_name = build_path_from_dentry(source_dentry, page1);
2250 if (IS_ERR(from_name)) {
2251 rc = PTR_ERR(from_name);
2252 goto cifs_rename_exit;
2255 to_name = build_path_from_dentry(target_dentry, page2);
2256 if (IS_ERR(to_name)) {
2257 rc = PTR_ERR(to_name);
2258 goto cifs_rename_exit;
2261 cifs_close_deferred_file_under_dentry(tcon, from_name);
2262 if (d_inode(target_dentry) != NULL)
2263 cifs_close_deferred_file_under_dentry(tcon, to_name);
2265 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2268 if (rc == -EACCES) {
2269 while (retry_count < 3) {
2270 cifs_close_all_deferred_files(tcon);
2271 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2280 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2282 if (flags & RENAME_NOREPLACE)
2283 goto cifs_rename_exit;
2285 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2286 if (rc == -EEXIST && tcon->unix_ext) {
2288 * Are src and dst hardlinks of same inode? We can only tell
2289 * with unix extensions enabled.
2292 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2294 if (info_buf_source == NULL) {
2296 goto cifs_rename_exit;
2299 info_buf_target = info_buf_source + 1;
2300 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2303 cifs_remap(cifs_sb));
2307 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2310 cifs_remap(cifs_sb));
2312 if (tmprc == 0 && (info_buf_source->UniqueId ==
2313 info_buf_target->UniqueId)) {
2314 /* same file, POSIX says that this is a noop */
2316 goto cifs_rename_exit;
2320 * else ... BB we could add the same check for Windows by
2321 * checking the UniqueId via FILE_INTERNAL_INFO
2325 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2327 /* Try unlinking the target dentry if it's not negative */
2328 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2329 if (d_is_dir(target_dentry))
2330 tmprc = cifs_rmdir(target_dir, target_dentry);
2332 tmprc = cifs_unlink(target_dir, target_dentry);
2334 goto cifs_rename_exit;
2335 rc = cifs_do_rename(xid, source_dentry, from_name,
2336 target_dentry, to_name);
2339 /* force revalidate to go get info when needed */
2340 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2343 kfree(info_buf_source);
2344 free_dentry_path(page2);
2345 free_dentry_path(page1);
2347 cifs_put_tlink(tlink);
2352 cifs_dentry_needs_reval(struct dentry *dentry)
2354 struct inode *inode = d_inode(dentry);
2355 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2356 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2357 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2358 struct cached_fid *cfid = NULL;
2360 if (cifs_i->time == 0)
2363 if (CIFS_CACHE_READ(cifs_i))
2366 if (!lookupCacheEnabled)
2369 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2370 spin_lock(&cfid->fid_lock);
2371 if (cfid->time && cifs_i->time > cfid->time) {
2372 spin_unlock(&cfid->fid_lock);
2373 close_cached_dir(cfid);
2376 spin_unlock(&cfid->fid_lock);
2377 close_cached_dir(cfid);
2380 * depending on inode type, check if attribute caching disabled for
2381 * files or directories
2383 if (S_ISDIR(inode->i_mode)) {
2384 if (!cifs_sb->ctx->acdirmax)
2386 if (!time_in_range(jiffies, cifs_i->time,
2387 cifs_i->time + cifs_sb->ctx->acdirmax))
2390 if (!cifs_sb->ctx->acregmax)
2392 if (!time_in_range(jiffies, cifs_i->time,
2393 cifs_i->time + cifs_sb->ctx->acregmax))
2397 /* hardlinked files w/ noserverino get "special" treatment */
2398 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2399 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2406 * Zap the cache. Called when invalid_mapping flag is set.
2409 cifs_invalidate_mapping(struct inode *inode)
2413 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2414 rc = invalidate_inode_pages2(inode->i_mapping);
2416 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2417 __func__, inode, rc);
2424 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2426 * @key: currently unused
2427 * @mode: the task state to sleep in
2430 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2433 if (signal_pending_state(mode, current))
2434 return -ERESTARTSYS;
2439 cifs_revalidate_mapping(struct inode *inode)
2442 unsigned long *flags = &CIFS_I(inode)->flags;
2443 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2445 /* swapfiles are not supposed to be shared */
2446 if (IS_SWAPFILE(inode))
2449 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2450 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2454 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2455 /* for cache=singleclient, do not invalidate */
2456 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2457 goto skip_invalidate;
2459 rc = cifs_invalidate_mapping(inode);
2461 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2465 clear_bit_unlock(CIFS_INO_LOCK, flags);
2466 smp_mb__after_atomic();
2467 wake_up_bit(flags, CIFS_INO_LOCK);
2473 cifs_zap_mapping(struct inode *inode)
2475 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2476 return cifs_revalidate_mapping(inode);
2479 int cifs_revalidate_file_attr(struct file *filp)
2482 struct dentry *dentry = file_dentry(filp);
2483 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2484 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2485 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2487 if (!cifs_dentry_needs_reval(dentry))
2490 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2491 if (tlink_tcon(cfile->tlink)->unix_ext)
2492 rc = cifs_get_file_info_unix(filp);
2494 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2495 rc = cifs_get_file_info(filp);
2500 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2504 struct inode *inode = d_inode(dentry);
2505 struct super_block *sb = dentry->d_sb;
2506 const char *full_path;
2513 if (!cifs_dentry_needs_reval(dentry))
2518 page = alloc_dentry_path();
2519 full_path = build_path_from_dentry(dentry, page);
2520 if (IS_ERR(full_path)) {
2521 rc = PTR_ERR(full_path);
2525 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2526 full_path, inode, inode->i_count.counter,
2527 dentry, cifs_get_time(dentry), jiffies);
2530 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2531 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2532 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2533 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2535 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2537 if (rc == -EAGAIN && count++ < 10)
2540 free_dentry_path(page);
2546 int cifs_revalidate_file(struct file *filp)
2549 struct inode *inode = file_inode(filp);
2551 rc = cifs_revalidate_file_attr(filp);
2555 return cifs_revalidate_mapping(inode);
2558 /* revalidate a dentry's inode attributes */
2559 int cifs_revalidate_dentry(struct dentry *dentry)
2562 struct inode *inode = d_inode(dentry);
2564 rc = cifs_revalidate_dentry_attr(dentry);
2568 return cifs_revalidate_mapping(inode);
2571 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2572 struct kstat *stat, u32 request_mask, unsigned int flags)
2574 struct dentry *dentry = path->dentry;
2575 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2576 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2577 struct inode *inode = d_inode(dentry);
2580 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2584 * We need to be sure that all dirty pages are written and the server
2585 * has actual ctime, mtime and file length.
2587 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2588 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2589 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2590 rc = filemap_fdatawait(inode->i_mapping);
2592 mapping_set_error(inode->i_mapping, rc);
2597 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2598 CIFS_I(inode)->time = 0; /* force revalidate */
2601 * If the caller doesn't require syncing, only sync if
2602 * necessary (e.g. due to earlier truncate or setattr
2603 * invalidating the cached metadata)
2605 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2606 (CIFS_I(inode)->time == 0)) {
2607 rc = cifs_revalidate_dentry_attr(dentry);
2612 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2613 stat->blksize = cifs_sb->ctx->bsize;
2614 stat->ino = CIFS_I(inode)->uniqueid;
2616 /* old CIFS Unix Extensions doesn't return create time */
2617 if (CIFS_I(inode)->createtime) {
2618 stat->result_mask |= STATX_BTIME;
2620 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2623 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2624 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2625 stat->attributes |= STATX_ATTR_COMPRESSED;
2626 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2627 stat->attributes |= STATX_ATTR_ENCRYPTED;
2630 * If on a multiuser mount without unix extensions or cifsacl being
2631 * enabled, and the admin hasn't overridden them, set the ownership
2632 * to the fsuid/fsgid of the current process.
2634 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2635 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2637 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2638 stat->uid = current_fsuid();
2639 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2640 stat->gid = current_fsgid();
2645 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2648 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2649 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2650 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2651 struct TCP_Server_Info *server = tcon->ses->server;
2652 struct cifsFileInfo *cfile;
2655 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2659 * We need to be sure that all dirty pages are written as they
2660 * might fill holes on the server.
2662 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2663 inode->i_mapping->nrpages != 0) {
2664 rc = filemap_fdatawait(inode->i_mapping);
2666 mapping_set_error(inode->i_mapping, rc);
2671 cfile = find_readable_file(cifs_i, false);
2675 if (server->ops->fiemap) {
2676 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2677 cifsFileInfo_put(cfile);
2681 cifsFileInfo_put(cfile);
2685 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2687 pgoff_t index = from >> PAGE_SHIFT;
2688 unsigned offset = from & (PAGE_SIZE - 1);
2692 page = grab_cache_page(mapping, index);
2696 zero_user_segment(page, offset, PAGE_SIZE);
2702 void cifs_setsize(struct inode *inode, loff_t offset)
2704 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2706 spin_lock(&inode->i_lock);
2707 i_size_write(inode, offset);
2708 spin_unlock(&inode->i_lock);
2710 /* Cached inode must be refreshed on truncate */
2712 truncate_pagecache(inode, offset);
2716 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2717 unsigned int xid, const char *full_path)
2720 struct cifsFileInfo *open_file;
2721 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2722 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2723 struct tcon_link *tlink = NULL;
2724 struct cifs_tcon *tcon = NULL;
2725 struct TCP_Server_Info *server;
2728 * To avoid spurious oplock breaks from server, in the case of
2729 * inodes that we already have open, avoid doing path based
2730 * setting of file size if we can do it by handle.
2731 * This keeps our caching token (oplock) and avoids timeouts
2732 * when the local oplock break takes longer to flush
2733 * writebehind data than the SMB timeout for the SetPathInfo
2734 * request would allow
2736 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2738 tcon = tlink_tcon(open_file->tlink);
2739 server = tcon->ses->server;
2740 if (server->ops->set_file_size)
2741 rc = server->ops->set_file_size(xid, tcon, open_file,
2742 attrs->ia_size, false);
2745 cifsFileInfo_put(open_file);
2746 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2754 tlink = cifs_sb_tlink(cifs_sb);
2756 return PTR_ERR(tlink);
2757 tcon = tlink_tcon(tlink);
2758 server = tcon->ses->server;
2762 * Set file size by pathname rather than by handle either because no
2763 * valid, writeable file handle for it was found or because there was
2764 * an error setting it by handle.
2766 if (server->ops->set_path_size)
2767 rc = server->ops->set_path_size(xid, tcon, full_path,
2768 attrs->ia_size, cifs_sb, false);
2771 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2774 cifs_put_tlink(tlink);
2778 cifsInode->server_eof = attrs->ia_size;
2779 cifs_setsize(inode, attrs->ia_size);
2781 * i_blocks is not related to (i_size / i_blksize), but instead
2782 * 512 byte (2**9) size is required for calculating num blocks.
2783 * Until we can query the server for actual allocation size,
2784 * this is best estimate we have for blocks allocated for a file
2785 * Number of blocks must be rounded up so size 1 is not 0 blocks
2787 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2790 * The man page of truncate says if the size changed,
2791 * then the st_ctime and st_mtime fields for the file
2794 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2795 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2797 cifs_truncate_page(inode->i_mapping, inode->i_size);
2803 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2805 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2809 const char *full_path;
2810 void *page = alloc_dentry_path();
2811 struct inode *inode = d_inode(direntry);
2812 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2813 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2814 struct tcon_link *tlink;
2815 struct cifs_tcon *pTcon;
2816 struct cifs_unix_set_info_args *args = NULL;
2817 struct cifsFileInfo *open_file;
2819 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2820 direntry, attrs->ia_valid);
2824 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2825 attrs->ia_valid |= ATTR_FORCE;
2827 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2831 full_path = build_path_from_dentry(direntry, page);
2832 if (IS_ERR(full_path)) {
2833 rc = PTR_ERR(full_path);
2838 * Attempt to flush data before changing attributes. We need to do
2839 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2840 * ownership or mode then we may also need to do this. Here, we take
2841 * the safe way out and just do the flush on all setattr requests. If
2842 * the flush returns error, store it to report later and continue.
2844 * BB: This should be smarter. Why bother flushing pages that
2845 * will be truncated anyway? Also, should we error out here if
2846 * the flush returns error?
2848 rc = filemap_write_and_wait(inode->i_mapping);
2849 if (is_interrupt_error(rc)) {
2854 mapping_set_error(inode->i_mapping, rc);
2857 if (attrs->ia_valid & ATTR_SIZE) {
2858 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2863 /* skip mode change if it's just for clearing setuid/setgid */
2864 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2865 attrs->ia_valid &= ~ATTR_MODE;
2867 args = kmalloc(sizeof(*args), GFP_KERNEL);
2873 /* set up the struct */
2874 if (attrs->ia_valid & ATTR_MODE)
2875 args->mode = attrs->ia_mode;
2877 args->mode = NO_CHANGE_64;
2879 if (attrs->ia_valid & ATTR_UID)
2880 args->uid = attrs->ia_uid;
2882 args->uid = INVALID_UID; /* no change */
2884 if (attrs->ia_valid & ATTR_GID)
2885 args->gid = attrs->ia_gid;
2887 args->gid = INVALID_GID; /* no change */
2889 if (attrs->ia_valid & ATTR_ATIME)
2890 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2892 args->atime = NO_CHANGE_64;
2894 if (attrs->ia_valid & ATTR_MTIME)
2895 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2897 args->mtime = NO_CHANGE_64;
2899 if (attrs->ia_valid & ATTR_CTIME)
2900 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2902 args->ctime = NO_CHANGE_64;
2905 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2907 u16 nfid = open_file->fid.netfid;
2908 u32 npid = open_file->pid;
2909 pTcon = tlink_tcon(open_file->tlink);
2910 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2911 cifsFileInfo_put(open_file);
2913 tlink = cifs_sb_tlink(cifs_sb);
2914 if (IS_ERR(tlink)) {
2915 rc = PTR_ERR(tlink);
2918 pTcon = tlink_tcon(tlink);
2919 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2921 cifs_remap(cifs_sb));
2922 cifs_put_tlink(tlink);
2928 if ((attrs->ia_valid & ATTR_SIZE) &&
2929 attrs->ia_size != i_size_read(inode)) {
2930 truncate_setsize(inode, attrs->ia_size);
2931 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2934 setattr_copy(&nop_mnt_idmap, inode, attrs);
2935 mark_inode_dirty(inode);
2937 /* force revalidate when any of these times are set since some
2938 of the fs types (eg ext3, fat) do not have fine enough
2939 time granularity to match protocol, and we do not have a
2940 a way (yet) to query the server fs's time granularity (and
2941 whether it rounds times down).
2943 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2944 cifsInode->time = 0;
2947 free_dentry_path(page);
2951 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2954 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2957 kuid_t uid = INVALID_UID;
2958 kgid_t gid = INVALID_GID;
2959 struct inode *inode = d_inode(direntry);
2960 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2961 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2962 struct cifsFileInfo *wfile;
2963 struct cifs_tcon *tcon;
2964 const char *full_path;
2965 void *page = alloc_dentry_path();
2968 __u64 mode = NO_CHANGE_64;
2972 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2973 direntry, attrs->ia_valid);
2975 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2976 attrs->ia_valid |= ATTR_FORCE;
2978 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2980 goto cifs_setattr_exit;
2982 full_path = build_path_from_dentry(direntry, page);
2983 if (IS_ERR(full_path)) {
2984 rc = PTR_ERR(full_path);
2985 goto cifs_setattr_exit;
2989 * Attempt to flush data before changing attributes. We need to do
2990 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2991 * returns error, store it to report later and continue.
2993 * BB: This should be smarter. Why bother flushing pages that
2994 * will be truncated anyway? Also, should we error out here if
2995 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2997 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2998 rc = filemap_write_and_wait(inode->i_mapping);
2999 if (is_interrupt_error(rc)) {
3001 goto cifs_setattr_exit;
3003 mapping_set_error(inode->i_mapping, rc);
3008 if ((attrs->ia_valid & ATTR_MTIME) &&
3009 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3010 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3012 tcon = tlink_tcon(wfile->tlink);
3013 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3014 cifsFileInfo_put(wfile);
3016 goto cifs_setattr_exit;
3017 } else if (rc != -EBADF)
3018 goto cifs_setattr_exit;
3023 if (attrs->ia_valid & ATTR_SIZE) {
3024 rc = cifs_set_file_size(inode, attrs, xid, full_path);
3026 goto cifs_setattr_exit;
3029 if (attrs->ia_valid & ATTR_UID)
3030 uid = attrs->ia_uid;
3032 if (attrs->ia_valid & ATTR_GID)
3033 gid = attrs->ia_gid;
3035 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3036 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3037 if (uid_valid(uid) || gid_valid(gid)) {
3038 mode = NO_CHANGE_64;
3039 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3042 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3044 goto cifs_setattr_exit;
3048 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3049 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3051 /* skip mode change if it's just for clearing setuid/setgid */
3052 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3053 attrs->ia_valid &= ~ATTR_MODE;
3055 if (attrs->ia_valid & ATTR_MODE) {
3056 mode = attrs->ia_mode;
3058 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3059 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3060 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3061 INVALID_UID, INVALID_GID);
3063 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3065 goto cifs_setattr_exit;
3069 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3070 * Pick up the actual mode bits that were set.
3072 if (mode != attrs->ia_mode)
3073 attrs->ia_mode = mode;
3075 if (((mode & S_IWUGO) == 0) &&
3076 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3078 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3080 /* fix up mode if we're not using dynperm */
3081 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3082 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3083 } else if ((mode & S_IWUGO) &&
3084 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3086 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3087 /* Attributes of 0 are ignored */
3089 dosattr |= ATTR_NORMAL;
3091 /* reset local inode permissions to normal */
3092 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3093 attrs->ia_mode &= ~(S_IALLUGO);
3094 if (S_ISDIR(inode->i_mode))
3096 cifs_sb->ctx->dir_mode;
3099 cifs_sb->ctx->file_mode;
3101 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3102 /* ignore mode change - ATTR_READONLY hasn't changed */
3103 attrs->ia_valid &= ~ATTR_MODE;
3107 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3108 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3109 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3110 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3112 /* Even if error on time set, no sense failing the call if
3113 the server would set the time to a reasonable value anyway,
3114 and this check ensures that we are not being called from
3115 sys_utimes in which case we ought to fail the call back to
3116 the user when the server rejects the call */
3117 if ((rc) && (attrs->ia_valid &
3118 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3122 /* do not need local check to inode_check_ok since the server does
3125 goto cifs_setattr_exit;
3127 if ((attrs->ia_valid & ATTR_SIZE) &&
3128 attrs->ia_size != i_size_read(inode)) {
3129 truncate_setsize(inode, attrs->ia_size);
3130 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3133 setattr_copy(&nop_mnt_idmap, inode, attrs);
3134 mark_inode_dirty(inode);
3138 free_dentry_path(page);
3143 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3144 struct iattr *attrs)
3146 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3147 int rc, retries = 0;
3148 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3149 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3150 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3152 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3156 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3157 if (pTcon->unix_ext)
3158 rc = cifs_setattr_unix(direntry, attrs);
3160 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3161 rc = cifs_setattr_nounix(direntry, attrs);
3163 } while (is_retryable_error(rc) && retries < 2);
3165 /* BB: add cifs_setattr_legacy for really old servers */