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);
463 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
468 int cifs_get_inode_info_unix(struct inode **pinode,
469 const unsigned char *full_path,
470 struct super_block *sb, unsigned int xid)
472 struct cifs_fattr fattr = {};
475 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
479 rc = update_inode_info(sb, &fattr, pinode);
481 kfree(fattr.cf_symlink_target);
485 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
486 struct super_block *sb,
487 struct cifs_fattr *fattr,
488 struct inode **pinode,
489 const unsigned int xid)
494 int cifs_get_inode_info_unix(struct inode **pinode,
495 const unsigned char *full_path,
496 struct super_block *sb, unsigned int xid)
500 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
503 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
504 struct cifs_sb_info *cifs_sb, unsigned int xid)
508 struct tcon_link *tlink;
509 struct cifs_tcon *tcon;
511 struct cifs_open_parms oparms;
512 struct cifs_io_parms io_parms = {0};
514 unsigned int bytes_read;
516 int buf_type = CIFS_NO_BUFFER;
520 fattr->cf_mode &= ~S_IFMT;
522 if (fattr->cf_eof == 0) {
523 fattr->cf_mode |= S_IFIFO;
524 fattr->cf_dtype = DT_FIFO;
526 } else if (fattr->cf_eof < 8) {
527 fattr->cf_mode |= S_IFREG;
528 fattr->cf_dtype = DT_REG;
529 return -EINVAL; /* EOPNOTSUPP? */
532 tlink = cifs_sb_tlink(cifs_sb);
534 return PTR_ERR(tlink);
535 tcon = tlink_tcon(tlink);
537 oparms = (struct cifs_open_parms) {
540 .desired_access = GENERIC_READ,
541 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
542 .disposition = FILE_OPEN,
547 if (tcon->ses->server->oplocks)
551 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
553 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
554 cifs_put_tlink(tlink);
559 io_parms.netfid = fid.netfid;
560 io_parms.pid = current->tgid;
561 io_parms.tcon = tcon;
563 io_parms.length = 24;
565 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
566 &bytes_read, &pbuf, &buf_type);
567 if ((rc == 0) && (bytes_read >= 8)) {
568 if (memcmp("IntxBLK", pbuf, 8) == 0) {
569 cifs_dbg(FYI, "Block device\n");
570 fattr->cf_mode |= S_IFBLK;
571 fattr->cf_dtype = DT_BLK;
572 if (bytes_read == 24) {
573 /* we have enough to decode dev num */
574 __u64 mjr; /* major */
575 __u64 mnr; /* minor */
576 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
577 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
578 fattr->cf_rdev = MKDEV(mjr, mnr);
580 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
581 cifs_dbg(FYI, "Char device\n");
582 fattr->cf_mode |= S_IFCHR;
583 fattr->cf_dtype = DT_CHR;
584 if (bytes_read == 24) {
585 /* we have enough to decode dev num */
586 __u64 mjr; /* major */
587 __u64 mnr; /* minor */
588 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
589 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
590 fattr->cf_rdev = MKDEV(mjr, mnr);
592 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
593 cifs_dbg(FYI, "Symlink\n");
594 fattr->cf_mode |= S_IFLNK;
595 fattr->cf_dtype = DT_LNK;
596 } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
597 cifs_dbg(FYI, "FIFO\n");
598 fattr->cf_mode |= S_IFIFO;
599 fattr->cf_dtype = DT_FIFO;
601 fattr->cf_mode |= S_IFREG; /* file? */
602 fattr->cf_dtype = DT_REG;
606 fattr->cf_mode |= S_IFREG; /* then it is a file */
607 fattr->cf_dtype = DT_REG;
608 rc = -EOPNOTSUPP; /* or some unknown SFU type */
611 tcon->ses->server->ops->close(xid, tcon, &fid);
612 cifs_put_tlink(tlink);
616 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
619 * Fetch mode bits as provided by SFU.
621 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
623 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
624 struct cifs_sb_info *cifs_sb, unsigned int xid)
626 #ifdef CONFIG_CIFS_XATTR
630 struct tcon_link *tlink;
631 struct cifs_tcon *tcon;
633 tlink = cifs_sb_tlink(cifs_sb);
635 return PTR_ERR(tlink);
636 tcon = tlink_tcon(tlink);
638 if (tcon->ses->server->ops->query_all_EAs == NULL) {
639 cifs_put_tlink(tlink);
643 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
644 "SETFILEBITS", ea_value, 4 /* size of buf */,
646 cifs_put_tlink(tlink);
650 mode = le32_to_cpu(*((__le32 *)ea_value));
651 fattr->cf_mode &= ~SFBITS_MASK;
652 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
653 mode, fattr->cf_mode);
654 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
655 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
664 /* Fill a cifs_fattr struct with info from POSIX info struct */
665 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
666 struct cifs_open_info_data *data,
667 struct cifs_sid *owner,
668 struct cifs_sid *group,
669 struct super_block *sb)
671 struct smb311_posix_qinfo *info = &data->posix_fi;
672 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
673 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
675 memset(fattr, 0, sizeof(*fattr));
677 /* no fattr->flags to set */
678 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
679 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
681 if (info->LastAccessTime)
682 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
684 ktime_get_coarse_real_ts64(&fattr->cf_atime);
686 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
687 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
689 if (data->adjust_tz) {
690 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
691 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
694 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
695 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
696 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
698 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
699 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
700 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
701 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
704 fattr->cf_mode |= S_IFLNK;
705 fattr->cf_dtype = DT_LNK;
706 fattr->cf_symlink_target = data->symlink_target;
707 data->symlink_target = NULL;
708 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
709 fattr->cf_mode |= S_IFDIR;
710 fattr->cf_dtype = DT_DIR;
712 fattr->cf_mode |= S_IFREG;
713 fattr->cf_dtype = DT_REG;
715 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
717 sid_to_id(cifs_sb, owner, fattr, SIDOWNER);
718 sid_to_id(cifs_sb, group, fattr, SIDGROUP);
720 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
721 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
724 static inline dev_t nfs_mkdev(struct reparse_posix_data *buf)
726 u64 v = le64_to_cpu(*(__le64 *)buf->DataBuffer);
728 return MKDEV(v >> 32, v & 0xffffffff);
731 bool cifs_reparse_point_to_fattr(struct cifs_sb_info *cifs_sb,
732 struct cifs_fattr *fattr,
733 struct cifs_open_info_data *data)
735 struct reparse_posix_data *buf = data->reparse.posix;
736 u32 tag = data->reparse.tag;
738 if (tag == IO_REPARSE_TAG_NFS && buf) {
739 switch (le64_to_cpu(buf->InodeType)) {
740 case NFS_SPECFILE_CHR:
741 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
742 fattr->cf_dtype = DT_CHR;
743 fattr->cf_rdev = nfs_mkdev(buf);
745 case NFS_SPECFILE_BLK:
746 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
747 fattr->cf_dtype = DT_BLK;
748 fattr->cf_rdev = nfs_mkdev(buf);
750 case NFS_SPECFILE_FIFO:
751 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
752 fattr->cf_dtype = DT_FIFO;
754 case NFS_SPECFILE_SOCK:
755 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
756 fattr->cf_dtype = DT_SOCK;
758 case NFS_SPECFILE_LNK:
759 fattr->cf_mode = S_IFLNK | cifs_sb->ctx->file_mode;
760 fattr->cf_dtype = DT_LNK;
770 case IO_REPARSE_TAG_LX_SYMLINK:
771 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
772 fattr->cf_dtype = DT_LNK;
774 case IO_REPARSE_TAG_LX_FIFO:
775 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
776 fattr->cf_dtype = DT_FIFO;
778 case IO_REPARSE_TAG_AF_UNIX:
779 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
780 fattr->cf_dtype = DT_SOCK;
782 case IO_REPARSE_TAG_LX_CHR:
783 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
784 fattr->cf_dtype = DT_CHR;
786 case IO_REPARSE_TAG_LX_BLK:
787 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
788 fattr->cf_dtype = DT_BLK;
790 case 0: /* SMB1 symlink */
791 case IO_REPARSE_TAG_SYMLINK:
792 case IO_REPARSE_TAG_NFS:
793 fattr->cf_mode = S_IFLNK | cifs_sb->ctx->file_mode;
794 fattr->cf_dtype = DT_LNK;
802 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
803 struct cifs_open_info_data *data,
804 struct super_block *sb)
806 struct smb2_file_all_info *info = &data->fi;
807 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
808 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
810 memset(fattr, 0, sizeof(*fattr));
811 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
812 if (info->DeletePending)
813 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
815 if (info->LastAccessTime)
816 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
818 ktime_get_coarse_real_ts64(&fattr->cf_atime);
820 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
821 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
823 if (data->adjust_tz) {
824 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
825 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
828 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
829 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
830 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
831 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
833 if (cifs_open_data_reparse(data) &&
834 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
837 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
838 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
839 fattr->cf_dtype = DT_DIR;
841 * Server can return wrong NumberOfLinks value for directories
842 * when Unix extensions are disabled - fake it.
845 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
847 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
848 fattr->cf_dtype = DT_REG;
850 /* clear write bits if ATTR_READONLY is set */
851 if (fattr->cf_cifsattrs & ATTR_READONLY)
852 fattr->cf_mode &= ~(S_IWUGO);
855 * Don't accept zero nlink from non-unix servers unless
856 * delete is pending. Instead mark it as unknown.
858 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
859 !info->DeletePending) {
860 cifs_dbg(VFS, "bogus file nlink value %u\n",
862 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
867 if (S_ISLNK(fattr->cf_mode)) {
868 if (likely(data->symlink_target))
869 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
870 fattr->cf_symlink_target = data->symlink_target;
871 data->symlink_target = NULL;
874 fattr->cf_uid = cifs_sb->ctx->linux_uid;
875 fattr->cf_gid = cifs_sb->ctx->linux_gid;
879 cifs_get_file_info(struct file *filp)
883 struct cifs_open_info_data data = {};
884 struct cifs_fattr fattr;
885 struct inode *inode = file_inode(filp);
886 struct cifsFileInfo *cfile = filp->private_data;
887 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
888 struct TCP_Server_Info *server = tcon->ses->server;
890 if (!server->ops->query_file_info)
894 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
897 /* TODO: add support to query reparse tag */
898 data.adjust_tz = false;
899 if (data.symlink_target) {
901 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
903 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
906 cifs_create_junction_fattr(&fattr, inode->i_sb);
912 * FIXME: legacy server -- fall back to path-based call?
913 * for now, just skip revalidating and mark inode for
917 CIFS_I(inode)->time = 0;
924 * don't bother with SFU junk here -- just mark inode as needing
927 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
928 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
929 /* if filetype is different, return error */
930 rc = cifs_fattr_to_inode(inode, &fattr);
932 cifs_free_open_info(&data);
937 /* Simple function to return a 64 bit hash of string. Rarely called */
938 static __u64 simple_hashstr(const char *str)
940 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
944 hash = (hash + (__u64) *str++) * hash_mult;
949 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
951 * cifs_backup_query_path_info - SMB1 fallback code to get ino
953 * Fallback code to get file metadata when we don't have access to
954 * full_path (EACCES) and have backup creds.
956 * @xid: transaction id used to identify original request in logs
957 * @tcon: information about the server share we have mounted
958 * @sb: the superblock stores info such as disk space available
959 * @full_path: name of the file we are getting the metadata for
960 * @resp_buf: will be set to cifs resp buf and needs to be freed with
961 * cifs_buf_release() when done with @data
962 * @data: will be set to search info result buffer
965 cifs_backup_query_path_info(int xid,
966 struct cifs_tcon *tcon,
967 struct super_block *sb,
968 const char *full_path,
970 FILE_ALL_INFO **data)
972 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
973 struct cifs_search_info info = {0};
978 info.endOfSearch = false;
980 info.info_level = SMB_FIND_FILE_UNIX;
981 else if ((tcon->ses->capabilities &
982 tcon->ses->server->vals->cap_nt_find) == 0)
983 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
984 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
985 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
986 else /* no srvino useful for fallback to some netapp */
987 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
989 flags = CIFS_SEARCH_CLOSE_ALWAYS |
990 CIFS_SEARCH_CLOSE_AT_END |
991 CIFS_SEARCH_BACKUP_SEARCH;
993 rc = CIFSFindFirst(xid, tcon, full_path,
994 cifs_sb, NULL, flags, &info, false);
998 *resp_buf = (void *)info.ntwrk_buf_start;
999 *data = (FILE_ALL_INFO *)info.srch_entries_start;
1002 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1004 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1005 struct inode **inode, const char *full_path,
1006 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1008 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1009 struct TCP_Server_Info *server = tcon->ses->server;
1012 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1014 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1016 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1021 * If we have an inode pass a NULL tcon to ensure we don't
1022 * make a round trip to the server. This only works for SMB2+.
1024 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1025 &fattr->cf_uniqueid, data);
1028 * If that fails reuse existing ino or generate one
1029 * and disable server ones
1032 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1034 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1035 cifs_autodisable_serverino(cifs_sb);
1040 /* If no errors, check for zero root inode (invalid) */
1041 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1042 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1045 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1047 /* make an ino by hashing the UNC */
1048 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1049 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1054 static inline bool is_inode_cache_good(struct inode *ino)
1056 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1059 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1060 struct super_block *sb,
1061 const unsigned int xid,
1062 struct cifs_tcon *tcon,
1063 const char *full_path,
1064 struct cifs_fattr *fattr)
1066 struct TCP_Server_Info *server = tcon->ses->server;
1067 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1068 struct kvec rsp_iov, *iov = NULL;
1069 int rsp_buftype = CIFS_NO_BUFFER;
1070 u32 tag = data->reparse.tag;
1073 if (!tag && server->ops->query_reparse_point) {
1074 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1076 &rsp_iov, &rsp_buftype);
1082 switch ((data->reparse.tag = tag)) {
1083 case 0: /* SMB1 symlink */
1084 if (server->ops->query_symlink) {
1085 rc = server->ops->query_symlink(xid, tcon,
1087 &data->symlink_target);
1090 case IO_REPARSE_TAG_MOUNT_POINT:
1091 cifs_create_junction_fattr(fattr, sb);
1095 if (data->symlink_target) {
1097 } else if (server->ops->parse_reparse_point) {
1098 rc = server->ops->parse_reparse_point(cifs_sb,
1104 cifs_open_info_to_fattr(fattr, data, sb);
1106 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1110 static int cifs_get_fattr(struct cifs_open_info_data *data,
1111 struct super_block *sb, int xid,
1112 const struct cifs_fid *fid,
1113 struct cifs_fattr *fattr,
1114 struct inode **inode,
1115 const char *full_path)
1117 struct cifs_open_info_data tmp_data = {};
1118 struct cifs_tcon *tcon;
1119 struct TCP_Server_Info *server;
1120 struct tcon_link *tlink;
1121 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1122 void *smb1_backup_rsp_buf = NULL;
1126 tlink = cifs_sb_tlink(cifs_sb);
1128 return PTR_ERR(tlink);
1129 tcon = tlink_tcon(tlink);
1130 server = tcon->ses->server;
1133 * 1. Fetch file metadata if not provided (data)
1137 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1138 full_path, &tmp_data);
1143 * 2. Convert it to internal cifs metadata (fattr)
1149 * If the file is a reparse point, it is more complicated
1150 * since we have to check if its reparse tag matches a known
1151 * special file type e.g. symlink or fifo or char etc.
1153 if (cifs_open_data_reparse(data)) {
1154 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1157 cifs_open_info_to_fattr(fattr, data, sb);
1161 /* DFS link, no metadata available on this server */
1162 cifs_create_junction_fattr(fattr, sb);
1166 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1168 * perm errors, try again with backup flags if possible
1170 * For SMB2 and later the backup intent flag
1171 * is already sent if needed on open and there
1172 * is no path based FindFirst operation to use
1175 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1176 /* for easier reading */
1178 FILE_DIRECTORY_INFO *fdi;
1179 SEARCH_ID_FULL_DIR_INFO *si;
1181 rc = cifs_backup_query_path_info(xid, tcon, sb,
1183 &smb1_backup_rsp_buf,
1188 move_cifs_info_to_smb2(&data->fi, fi);
1189 fdi = (FILE_DIRECTORY_INFO *)fi;
1190 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1192 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1193 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1194 /* uniqueid set, skip get inum step */
1195 goto handle_mnt_opt;
1197 /* nothing we can do, bail out */
1202 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1205 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1210 * 3. Get or update inode number (fattr->cf_uniqueid)
1213 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1216 * 4. Tweak fattr based on mount options
1218 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1220 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1221 /* query for SFU type info if supported and needed */
1222 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1223 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1224 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1226 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1229 /* fill in 0777 bits from ACL */
1230 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1231 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1232 true, full_path, fid);
1236 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1240 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1241 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1242 false, full_path, fid);
1246 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1252 /* fill in remaining high mode bits e.g. SUID, VTX */
1253 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1254 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1256 /* check for Minshall+French symlinks */
1257 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1258 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1259 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1263 cifs_buf_release(smb1_backup_rsp_buf);
1264 cifs_put_tlink(tlink);
1265 cifs_free_open_info(&tmp_data);
1269 int cifs_get_inode_info(struct inode **inode,
1270 const char *full_path,
1271 struct cifs_open_info_data *data,
1272 struct super_block *sb, int xid,
1273 const struct cifs_fid *fid)
1275 struct cifs_fattr fattr = {};
1278 if (is_inode_cache_good(*inode)) {
1279 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1283 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1287 rc = update_inode_info(sb, &fattr, inode);
1289 kfree(fattr.cf_symlink_target);
1293 static int smb311_posix_get_fattr(struct cifs_fattr *fattr,
1294 const char *full_path,
1295 struct super_block *sb,
1296 const unsigned int xid)
1298 struct cifs_open_info_data data = {};
1299 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1300 struct cifs_tcon *tcon;
1301 struct tcon_link *tlink;
1302 struct cifs_sid owner, group;
1306 tlink = cifs_sb_tlink(cifs_sb);
1308 return PTR_ERR(tlink);
1309 tcon = tlink_tcon(tlink);
1312 * 1. Fetch file metadata
1315 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1320 * 2. Convert it to internal cifs metadata (fattr)
1325 smb311_posix_info_to_fattr(fattr, &data, &owner, &group, sb);
1328 /* DFS link, no metadata available on this server */
1329 cifs_create_junction_fattr(fattr, sb);
1334 * For SMB2 and later the backup intent flag
1335 * is already sent if needed on open and there
1336 * is no path based FindFirst operation to use
1337 * to retry with so nothing we can do, bail out
1341 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1346 * 3. Tweak fattr based on mount options
1348 /* check for Minshall+French symlinks */
1349 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1350 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1351 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1355 cifs_put_tlink(tlink);
1356 cifs_free_open_info(&data);
1360 int smb311_posix_get_inode_info(struct inode **inode, const char *full_path,
1361 struct super_block *sb, const unsigned int xid)
1363 struct cifs_fattr fattr = {};
1366 if (is_inode_cache_good(*inode)) {
1367 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1371 rc = smb311_posix_get_fattr(&fattr, full_path, sb, xid);
1375 rc = update_inode_info(sb, &fattr, inode);
1377 kfree(fattr.cf_symlink_target);
1381 static const struct inode_operations cifs_ipc_inode_ops = {
1382 .lookup = cifs_lookup,
1386 cifs_find_inode(struct inode *inode, void *opaque)
1388 struct cifs_fattr *fattr = opaque;
1390 /* don't match inode with different uniqueid */
1391 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1394 /* use createtime like an i_generation field */
1395 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1398 /* don't match inode of different type */
1399 if (inode_wrong_type(inode, fattr->cf_mode))
1402 /* if it's not a directory or has no dentries, then flag it */
1403 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1404 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1410 cifs_init_inode(struct inode *inode, void *opaque)
1412 struct cifs_fattr *fattr = opaque;
1414 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1415 CIFS_I(inode)->createtime = fattr->cf_createtime;
1420 * walk dentry list for an inode and report whether it has aliases that
1421 * are hashed. We use this to determine if a directory inode can actually
1425 inode_has_hashed_dentries(struct inode *inode)
1427 struct dentry *dentry;
1429 spin_lock(&inode->i_lock);
1430 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1431 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1432 spin_unlock(&inode->i_lock);
1436 spin_unlock(&inode->i_lock);
1440 /* Given fattrs, get a corresponding inode */
1442 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1445 struct inode *inode;
1448 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1450 /* hash down to 32-bits on 32-bit arch */
1451 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1453 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1455 /* was there a potentially problematic inode collision? */
1456 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1457 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1459 if (inode_has_hashed_dentries(inode)) {
1460 cifs_autodisable_serverino(CIFS_SB(sb));
1462 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1463 goto retry_iget5_locked;
1467 /* can't fail - see cifs_find_inode() */
1468 cifs_fattr_to_inode(inode, fattr);
1469 if (sb->s_flags & SB_NOATIME)
1470 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1471 if (inode->i_state & I_NEW) {
1472 inode->i_ino = hash;
1473 cifs_fscache_get_inode_cookie(inode);
1474 unlock_new_inode(inode);
1481 /* gets root inode */
1482 struct inode *cifs_root_iget(struct super_block *sb)
1484 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1485 struct cifs_fattr fattr = {};
1486 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1487 struct inode *inode = NULL;
1493 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1494 && cifs_sb->prepath) {
1495 len = strlen(cifs_sb->prepath);
1496 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1498 return ERR_PTR(-ENOMEM);
1500 memcpy(path+1, cifs_sb->prepath, len);
1502 path = kstrdup("", GFP_KERNEL);
1504 return ERR_PTR(-ENOMEM);
1508 if (tcon->unix_ext) {
1509 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1510 /* some servers mistakenly claim POSIX support */
1511 if (rc != -EOPNOTSUPP)
1513 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1514 tcon->unix_ext = false;
1517 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1518 if (tcon->posix_extensions)
1519 rc = smb311_posix_get_fattr(&fattr, path, sb, xid);
1521 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1525 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1526 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1527 cifs_autodisable_serverino(cifs_sb);
1529 inode = cifs_iget(sb, &fattr);
1533 inode = ERR_PTR(rc);
1537 if (rc && tcon->pipe) {
1538 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1539 spin_lock(&inode->i_lock);
1540 inode->i_mode |= S_IFDIR;
1541 set_nlink(inode, 2);
1542 inode->i_op = &cifs_ipc_inode_ops;
1543 inode->i_fop = &simple_dir_operations;
1544 inode->i_uid = cifs_sb->ctx->linux_uid;
1545 inode->i_gid = cifs_sb->ctx->linux_gid;
1546 spin_unlock(&inode->i_lock);
1549 inode = ERR_PTR(rc);
1555 kfree(fattr.cf_symlink_target);
1560 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1561 const char *full_path, __u32 dosattr)
1563 bool set_time = false;
1564 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1565 struct TCP_Server_Info *server;
1566 FILE_BASIC_INFO info_buf;
1571 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1572 if (!server->ops->set_file_info)
1577 if (attrs->ia_valid & ATTR_ATIME) {
1579 info_buf.LastAccessTime =
1580 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1582 info_buf.LastAccessTime = 0;
1584 if (attrs->ia_valid & ATTR_MTIME) {
1586 info_buf.LastWriteTime =
1587 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1589 info_buf.LastWriteTime = 0;
1592 * Samba throws this field away, but windows may actually use it.
1593 * Do not set ctime unless other time stamps are changed explicitly
1594 * (i.e. by utimes()) since we would then have a mix of client and
1597 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1598 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1599 info_buf.ChangeTime =
1600 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1602 info_buf.ChangeTime = 0;
1604 info_buf.CreationTime = 0; /* don't change */
1605 info_buf.Attributes = cpu_to_le32(dosattr);
1607 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1610 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1612 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1613 * and rename it to a random name that hopefully won't conflict with
1617 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1618 const unsigned int xid)
1622 struct cifs_fid fid;
1623 struct cifs_open_parms oparms;
1624 struct inode *inode = d_inode(dentry);
1625 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1626 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1627 struct tcon_link *tlink;
1628 struct cifs_tcon *tcon;
1629 __u32 dosattr, origattr;
1630 FILE_BASIC_INFO *info_buf = NULL;
1632 tlink = cifs_sb_tlink(cifs_sb);
1634 return PTR_ERR(tlink);
1635 tcon = tlink_tcon(tlink);
1638 * We cannot rename the file if the server doesn't support
1639 * CAP_INFOLEVEL_PASSTHRU
1641 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1646 oparms = (struct cifs_open_parms) {
1649 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1650 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1651 .disposition = FILE_OPEN,
1656 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1660 origattr = cifsInode->cifsAttrs;
1662 origattr |= ATTR_NORMAL;
1664 dosattr = origattr & ~ATTR_READONLY;
1666 dosattr |= ATTR_NORMAL;
1667 dosattr |= ATTR_HIDDEN;
1669 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1670 if (dosattr != origattr) {
1671 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1672 if (info_buf == NULL) {
1676 info_buf->Attributes = cpu_to_le32(dosattr);
1677 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1679 /* although we would like to mark the file hidden
1680 if that fails we will still try to rename it */
1682 cifsInode->cifsAttrs = dosattr;
1684 dosattr = origattr; /* since not able to change them */
1687 /* rename the file */
1688 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1690 cifs_remap(cifs_sb));
1696 /* try to set DELETE_ON_CLOSE */
1697 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1698 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1701 * some samba versions return -ENOENT when we try to set the
1702 * file disposition here. Likely a samba bug, but work around
1703 * it for now. This means that some cifsXXX files may hang
1704 * around after they shouldn't.
1706 * BB: remove this hack after more servers have the fix
1714 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1718 CIFSSMBClose(xid, tcon, fid.netfid);
1721 cifs_put_tlink(tlink);
1725 * reset everything back to the original state. Don't bother
1726 * dealing with errors here since we can't do anything about
1730 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1731 cifs_sb->local_nls, cifs_remap(cifs_sb));
1733 if (dosattr != origattr) {
1734 info_buf->Attributes = cpu_to_le32(origattr);
1735 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1737 cifsInode->cifsAttrs = origattr;
1742 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1744 /* copied from fs/nfs/dir.c with small changes */
1746 cifs_drop_nlink(struct inode *inode)
1748 spin_lock(&inode->i_lock);
1749 if (inode->i_nlink > 0)
1751 spin_unlock(&inode->i_lock);
1755 * If d_inode(dentry) is null (usually meaning the cached dentry
1756 * is a negative dentry) then we would attempt a standard SMB delete, but
1757 * if that fails we can not attempt the fall back mechanisms on EACCES
1758 * but will return the EACCES to the caller. Note that the VFS does not call
1759 * unlink on negative dentries currently.
1761 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1765 const char *full_path;
1767 struct inode *inode = d_inode(dentry);
1768 struct cifsInodeInfo *cifs_inode;
1769 struct super_block *sb = dir->i_sb;
1770 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1771 struct tcon_link *tlink;
1772 struct cifs_tcon *tcon;
1773 struct TCP_Server_Info *server;
1774 struct iattr *attrs = NULL;
1775 __u32 dosattr = 0, origattr = 0;
1777 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1779 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1782 tlink = cifs_sb_tlink(cifs_sb);
1784 return PTR_ERR(tlink);
1785 tcon = tlink_tcon(tlink);
1786 server = tcon->ses->server;
1789 page = alloc_dentry_path();
1791 if (tcon->nodelete) {
1796 /* Unlink can be called from rename so we can not take the
1797 * sb->s_vfs_rename_mutex here */
1798 full_path = build_path_from_dentry(dentry, page);
1799 if (IS_ERR(full_path)) {
1800 rc = PTR_ERR(full_path);
1804 cifs_close_deferred_file_under_dentry(tcon, full_path);
1805 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1806 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1807 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1808 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1809 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1810 cifs_remap(cifs_sb));
1811 cifs_dbg(FYI, "posix del rc %d\n", rc);
1812 if ((rc == 0) || (rc == -ENOENT))
1813 goto psx_del_no_retry;
1815 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1818 if (!server->ops->unlink) {
1820 goto psx_del_no_retry;
1823 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1828 cifs_drop_nlink(inode);
1829 } else if (rc == -ENOENT) {
1831 } else if (rc == -EBUSY) {
1832 if (server->ops->rename_pending_delete) {
1833 rc = server->ops->rename_pending_delete(full_path,
1836 cifs_drop_nlink(inode);
1838 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1839 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1840 if (attrs == NULL) {
1845 /* try to reset dos attributes */
1846 cifs_inode = CIFS_I(inode);
1847 origattr = cifs_inode->cifsAttrs;
1849 origattr |= ATTR_NORMAL;
1850 dosattr = origattr & ~ATTR_READONLY;
1852 dosattr |= ATTR_NORMAL;
1853 dosattr |= ATTR_HIDDEN;
1855 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1859 goto retry_std_delete;
1862 /* undo the setattr if we errored out and it's needed */
1863 if (rc != 0 && dosattr != 0)
1864 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1868 cifs_inode = CIFS_I(inode);
1869 cifs_inode->time = 0; /* will force revalidate to get info
1871 inode_set_ctime_current(inode);
1873 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1874 cifs_inode = CIFS_I(dir);
1875 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1877 free_dentry_path(page);
1880 cifs_put_tlink(tlink);
1885 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1886 const char *full_path, struct cifs_sb_info *cifs_sb,
1887 struct cifs_tcon *tcon, const unsigned int xid)
1890 struct inode *inode = NULL;
1892 if (tcon->posix_extensions)
1893 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1894 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1895 else if (tcon->unix_ext)
1896 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1898 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1900 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1906 if (!S_ISDIR(inode->i_mode)) {
1908 * mkdir succeeded, but another client has managed to remove the
1909 * sucker and replace it with non-directory. Return success,
1910 * but don't leave the child in dcache.
1917 * setting nlink not necessary except in cases where we failed to get it
1918 * from the server or was set bogus. Also, since this is a brand new
1919 * inode, no need to grab the i_lock before setting the i_nlink.
1921 if (inode->i_nlink < 2)
1922 set_nlink(inode, 2);
1923 mode &= ~current_umask();
1924 /* must turn on setgid bit if parent dir has it */
1925 if (parent->i_mode & S_ISGID)
1928 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1929 if (tcon->unix_ext) {
1930 struct cifs_unix_set_info_args args = {
1932 .ctime = NO_CHANGE_64,
1933 .atime = NO_CHANGE_64,
1934 .mtime = NO_CHANGE_64,
1937 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1938 args.uid = current_fsuid();
1939 if (parent->i_mode & S_ISGID)
1940 args.gid = parent->i_gid;
1942 args.gid = current_fsgid();
1944 args.uid = INVALID_UID; /* no change */
1945 args.gid = INVALID_GID; /* no change */
1947 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1949 cifs_remap(cifs_sb));
1953 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1954 struct TCP_Server_Info *server = tcon->ses->server;
1955 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1956 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1957 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1959 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1960 inode->i_mode = (mode | S_IFDIR);
1962 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1963 inode->i_uid = current_fsuid();
1964 if (inode->i_mode & S_ISGID)
1965 inode->i_gid = parent->i_gid;
1967 inode->i_gid = current_fsgid();
1970 d_instantiate(dentry, inode);
1974 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1976 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1977 const char *full_path, struct cifs_sb_info *cifs_sb,
1978 struct cifs_tcon *tcon, const unsigned int xid)
1982 FILE_UNIX_BASIC_INFO *info = NULL;
1983 struct inode *newinode = NULL;
1984 struct cifs_fattr fattr;
1986 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1989 goto posix_mkdir_out;
1992 mode &= ~current_umask();
1993 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1994 NULL /* netfid */, info, &oplock, full_path,
1995 cifs_sb->local_nls, cifs_remap(cifs_sb));
1996 if (rc == -EOPNOTSUPP)
1997 goto posix_mkdir_out;
1999 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
2001 goto posix_mkdir_out;
2004 if (info->Type == cpu_to_le32(-1))
2005 /* no return info, go query for it */
2006 goto posix_mkdir_get_info;
2008 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2009 * need to set uid/gid.
2012 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2013 cifs_fill_uniqueid(inode->i_sb, &fattr);
2014 newinode = cifs_iget(inode->i_sb, &fattr);
2016 goto posix_mkdir_get_info;
2018 d_instantiate(dentry, newinode);
2020 #ifdef CONFIG_CIFS_DEBUG2
2021 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2022 dentry, dentry, newinode);
2024 if (newinode->i_nlink != 2)
2025 cifs_dbg(FYI, "unexpected number of links %d\n",
2032 posix_mkdir_get_info:
2033 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2035 goto posix_mkdir_out;
2037 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2039 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2040 struct dentry *direntry, umode_t mode)
2044 struct cifs_sb_info *cifs_sb;
2045 struct tcon_link *tlink;
2046 struct cifs_tcon *tcon;
2047 struct TCP_Server_Info *server;
2048 const char *full_path;
2051 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2054 cifs_sb = CIFS_SB(inode->i_sb);
2055 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2057 tlink = cifs_sb_tlink(cifs_sb);
2059 return PTR_ERR(tlink);
2060 tcon = tlink_tcon(tlink);
2064 page = alloc_dentry_path();
2065 full_path = build_path_from_dentry(direntry, page);
2066 if (IS_ERR(full_path)) {
2067 rc = PTR_ERR(full_path);
2071 server = tcon->ses->server;
2073 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2074 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2076 d_drop(direntry); /* for time being always refresh inode info */
2080 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2081 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2082 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2083 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2085 if (rc != -EOPNOTSUPP)
2088 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2090 if (!server->ops->mkdir) {
2095 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2096 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2098 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2103 /* TODO: skip this for smb2/smb3 */
2104 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2108 * Force revalidate to get parent dir info when needed since cached
2109 * attributes are invalid now.
2111 CIFS_I(inode)->time = 0;
2112 free_dentry_path(page);
2114 cifs_put_tlink(tlink);
2118 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2122 struct cifs_sb_info *cifs_sb;
2123 struct tcon_link *tlink;
2124 struct cifs_tcon *tcon;
2125 struct TCP_Server_Info *server;
2126 const char *full_path;
2127 void *page = alloc_dentry_path();
2128 struct cifsInodeInfo *cifsInode;
2130 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2134 full_path = build_path_from_dentry(direntry, page);
2135 if (IS_ERR(full_path)) {
2136 rc = PTR_ERR(full_path);
2140 cifs_sb = CIFS_SB(inode->i_sb);
2141 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2146 tlink = cifs_sb_tlink(cifs_sb);
2147 if (IS_ERR(tlink)) {
2148 rc = PTR_ERR(tlink);
2151 tcon = tlink_tcon(tlink);
2152 server = tcon->ses->server;
2154 if (!server->ops->rmdir) {
2156 cifs_put_tlink(tlink);
2160 if (tcon->nodelete) {
2162 cifs_put_tlink(tlink);
2166 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2167 cifs_put_tlink(tlink);
2170 spin_lock(&d_inode(direntry)->i_lock);
2171 i_size_write(d_inode(direntry), 0);
2172 clear_nlink(d_inode(direntry));
2173 spin_unlock(&d_inode(direntry)->i_lock);
2176 cifsInode = CIFS_I(d_inode(direntry));
2177 /* force revalidate to go get info when needed */
2178 cifsInode->time = 0;
2180 cifsInode = CIFS_I(inode);
2182 * Force revalidate to get parent dir info when needed since cached
2183 * attributes are invalid now.
2185 cifsInode->time = 0;
2187 inode_set_ctime_current(d_inode(direntry));
2188 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2191 free_dentry_path(page);
2197 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2198 const char *from_path, struct dentry *to_dentry,
2199 const char *to_path)
2201 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2202 struct tcon_link *tlink;
2203 struct cifs_tcon *tcon;
2204 struct TCP_Server_Info *server;
2205 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2206 struct cifs_fid fid;
2207 struct cifs_open_parms oparms;
2209 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2212 tlink = cifs_sb_tlink(cifs_sb);
2214 return PTR_ERR(tlink);
2215 tcon = tlink_tcon(tlink);
2216 server = tcon->ses->server;
2218 if (!server->ops->rename)
2221 /* try path-based rename first */
2222 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2225 * Don't bother with rename by filehandle unless file is busy and
2226 * source. Note that cross directory moves do not work with
2227 * rename by filehandle to various Windows servers.
2229 if (rc == 0 || rc != -EBUSY)
2230 goto do_rename_exit;
2232 /* Don't fall back to using SMB on SMB 2+ mount */
2233 if (server->vals->protocol_id != 0)
2234 goto do_rename_exit;
2236 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2237 /* open-file renames don't work across directories */
2238 if (to_dentry->d_parent != from_dentry->d_parent)
2239 goto do_rename_exit;
2241 oparms = (struct cifs_open_parms) {
2244 /* open the file to be renamed -- we need DELETE perms */
2245 .desired_access = DELETE,
2246 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2247 .disposition = FILE_OPEN,
2252 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2254 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2255 (const char *) to_dentry->d_name.name,
2256 cifs_sb->local_nls, cifs_remap(cifs_sb));
2257 CIFSSMBClose(xid, tcon, fid.netfid);
2259 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2262 d_move(from_dentry, to_dentry);
2263 cifs_put_tlink(tlink);
2268 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2269 struct dentry *source_dentry, struct inode *target_dir,
2270 struct dentry *target_dentry, unsigned int flags)
2272 const char *from_name, *to_name;
2273 void *page1, *page2;
2274 struct cifs_sb_info *cifs_sb;
2275 struct tcon_link *tlink;
2276 struct cifs_tcon *tcon;
2279 int retry_count = 0;
2280 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2281 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2282 FILE_UNIX_BASIC_INFO *info_buf_target;
2283 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2285 if (flags & ~RENAME_NOREPLACE)
2288 cifs_sb = CIFS_SB(source_dir->i_sb);
2289 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2292 tlink = cifs_sb_tlink(cifs_sb);
2294 return PTR_ERR(tlink);
2295 tcon = tlink_tcon(tlink);
2297 page1 = alloc_dentry_path();
2298 page2 = alloc_dentry_path();
2301 from_name = build_path_from_dentry(source_dentry, page1);
2302 if (IS_ERR(from_name)) {
2303 rc = PTR_ERR(from_name);
2304 goto cifs_rename_exit;
2307 to_name = build_path_from_dentry(target_dentry, page2);
2308 if (IS_ERR(to_name)) {
2309 rc = PTR_ERR(to_name);
2310 goto cifs_rename_exit;
2313 cifs_close_deferred_file_under_dentry(tcon, from_name);
2314 if (d_inode(target_dentry) != NULL)
2315 cifs_close_deferred_file_under_dentry(tcon, to_name);
2317 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2320 if (rc == -EACCES) {
2321 while (retry_count < 3) {
2322 cifs_close_all_deferred_files(tcon);
2323 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2332 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2334 if (flags & RENAME_NOREPLACE)
2335 goto cifs_rename_exit;
2337 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2338 if (rc == -EEXIST && tcon->unix_ext) {
2340 * Are src and dst hardlinks of same inode? We can only tell
2341 * with unix extensions enabled.
2344 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2346 if (info_buf_source == NULL) {
2348 goto cifs_rename_exit;
2351 info_buf_target = info_buf_source + 1;
2352 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2355 cifs_remap(cifs_sb));
2359 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2362 cifs_remap(cifs_sb));
2364 if (tmprc == 0 && (info_buf_source->UniqueId ==
2365 info_buf_target->UniqueId)) {
2366 /* same file, POSIX says that this is a noop */
2368 goto cifs_rename_exit;
2372 * else ... BB we could add the same check for Windows by
2373 * checking the UniqueId via FILE_INTERNAL_INFO
2377 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2379 /* Try unlinking the target dentry if it's not negative */
2380 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2381 if (d_is_dir(target_dentry))
2382 tmprc = cifs_rmdir(target_dir, target_dentry);
2384 tmprc = cifs_unlink(target_dir, target_dentry);
2386 goto cifs_rename_exit;
2387 rc = cifs_do_rename(xid, source_dentry, from_name,
2388 target_dentry, to_name);
2391 /* force revalidate to go get info when needed */
2392 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2395 kfree(info_buf_source);
2396 free_dentry_path(page2);
2397 free_dentry_path(page1);
2399 cifs_put_tlink(tlink);
2404 cifs_dentry_needs_reval(struct dentry *dentry)
2406 struct inode *inode = d_inode(dentry);
2407 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2408 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2409 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2410 struct cached_fid *cfid = NULL;
2412 if (cifs_i->time == 0)
2415 if (CIFS_CACHE_READ(cifs_i))
2418 if (!lookupCacheEnabled)
2421 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2422 spin_lock(&cfid->fid_lock);
2423 if (cfid->time && cifs_i->time > cfid->time) {
2424 spin_unlock(&cfid->fid_lock);
2425 close_cached_dir(cfid);
2428 spin_unlock(&cfid->fid_lock);
2429 close_cached_dir(cfid);
2432 * depending on inode type, check if attribute caching disabled for
2433 * files or directories
2435 if (S_ISDIR(inode->i_mode)) {
2436 if (!cifs_sb->ctx->acdirmax)
2438 if (!time_in_range(jiffies, cifs_i->time,
2439 cifs_i->time + cifs_sb->ctx->acdirmax))
2442 if (!cifs_sb->ctx->acregmax)
2444 if (!time_in_range(jiffies, cifs_i->time,
2445 cifs_i->time + cifs_sb->ctx->acregmax))
2449 /* hardlinked files w/ noserverino get "special" treatment */
2450 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2451 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2458 * Zap the cache. Called when invalid_mapping flag is set.
2461 cifs_invalidate_mapping(struct inode *inode)
2465 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2466 rc = invalidate_inode_pages2(inode->i_mapping);
2468 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2469 __func__, inode, rc);
2476 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2478 * @key: currently unused
2479 * @mode: the task state to sleep in
2482 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2485 if (signal_pending_state(mode, current))
2486 return -ERESTARTSYS;
2491 cifs_revalidate_mapping(struct inode *inode)
2494 unsigned long *flags = &CIFS_I(inode)->flags;
2495 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2497 /* swapfiles are not supposed to be shared */
2498 if (IS_SWAPFILE(inode))
2501 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2502 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2506 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2507 /* for cache=singleclient, do not invalidate */
2508 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2509 goto skip_invalidate;
2511 rc = cifs_invalidate_mapping(inode);
2513 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2517 clear_bit_unlock(CIFS_INO_LOCK, flags);
2518 smp_mb__after_atomic();
2519 wake_up_bit(flags, CIFS_INO_LOCK);
2525 cifs_zap_mapping(struct inode *inode)
2527 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2528 return cifs_revalidate_mapping(inode);
2531 int cifs_revalidate_file_attr(struct file *filp)
2534 struct dentry *dentry = file_dentry(filp);
2535 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2536 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2537 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2539 if (!cifs_dentry_needs_reval(dentry))
2542 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2543 if (tlink_tcon(cfile->tlink)->unix_ext)
2544 rc = cifs_get_file_info_unix(filp);
2546 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2547 rc = cifs_get_file_info(filp);
2552 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2556 struct inode *inode = d_inode(dentry);
2557 struct super_block *sb = dentry->d_sb;
2558 const char *full_path;
2565 if (!cifs_dentry_needs_reval(dentry))
2570 page = alloc_dentry_path();
2571 full_path = build_path_from_dentry(dentry, page);
2572 if (IS_ERR(full_path)) {
2573 rc = PTR_ERR(full_path);
2577 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2578 full_path, inode, inode->i_count.counter,
2579 dentry, cifs_get_time(dentry), jiffies);
2582 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2583 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2584 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2585 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2587 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2589 if (rc == -EAGAIN && count++ < 10)
2592 free_dentry_path(page);
2598 int cifs_revalidate_file(struct file *filp)
2601 struct inode *inode = file_inode(filp);
2603 rc = cifs_revalidate_file_attr(filp);
2607 return cifs_revalidate_mapping(inode);
2610 /* revalidate a dentry's inode attributes */
2611 int cifs_revalidate_dentry(struct dentry *dentry)
2614 struct inode *inode = d_inode(dentry);
2616 rc = cifs_revalidate_dentry_attr(dentry);
2620 return cifs_revalidate_mapping(inode);
2623 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2624 struct kstat *stat, u32 request_mask, unsigned int flags)
2626 struct dentry *dentry = path->dentry;
2627 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2628 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2629 struct inode *inode = d_inode(dentry);
2632 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2636 * We need to be sure that all dirty pages are written and the server
2637 * has actual ctime, mtime and file length.
2639 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2640 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2641 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2642 rc = filemap_fdatawait(inode->i_mapping);
2644 mapping_set_error(inode->i_mapping, rc);
2649 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2650 CIFS_I(inode)->time = 0; /* force revalidate */
2653 * If the caller doesn't require syncing, only sync if
2654 * necessary (e.g. due to earlier truncate or setattr
2655 * invalidating the cached metadata)
2657 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2658 (CIFS_I(inode)->time == 0)) {
2659 rc = cifs_revalidate_dentry_attr(dentry);
2664 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2665 stat->blksize = cifs_sb->ctx->bsize;
2666 stat->ino = CIFS_I(inode)->uniqueid;
2668 /* old CIFS Unix Extensions doesn't return create time */
2669 if (CIFS_I(inode)->createtime) {
2670 stat->result_mask |= STATX_BTIME;
2672 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2675 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2676 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2677 stat->attributes |= STATX_ATTR_COMPRESSED;
2678 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2679 stat->attributes |= STATX_ATTR_ENCRYPTED;
2682 * If on a multiuser mount without unix extensions or cifsacl being
2683 * enabled, and the admin hasn't overridden them, set the ownership
2684 * to the fsuid/fsgid of the current process.
2686 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2687 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2689 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2690 stat->uid = current_fsuid();
2691 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2692 stat->gid = current_fsgid();
2697 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2700 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2701 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2702 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2703 struct TCP_Server_Info *server = tcon->ses->server;
2704 struct cifsFileInfo *cfile;
2707 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2711 * We need to be sure that all dirty pages are written as they
2712 * might fill holes on the server.
2714 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2715 inode->i_mapping->nrpages != 0) {
2716 rc = filemap_fdatawait(inode->i_mapping);
2718 mapping_set_error(inode->i_mapping, rc);
2723 cfile = find_readable_file(cifs_i, false);
2727 if (server->ops->fiemap) {
2728 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2729 cifsFileInfo_put(cfile);
2733 cifsFileInfo_put(cfile);
2737 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2739 pgoff_t index = from >> PAGE_SHIFT;
2740 unsigned offset = from & (PAGE_SIZE - 1);
2744 page = grab_cache_page(mapping, index);
2748 zero_user_segment(page, offset, PAGE_SIZE);
2754 void cifs_setsize(struct inode *inode, loff_t offset)
2756 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2758 spin_lock(&inode->i_lock);
2759 i_size_write(inode, offset);
2760 spin_unlock(&inode->i_lock);
2762 /* Cached inode must be refreshed on truncate */
2764 truncate_pagecache(inode, offset);
2768 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2769 unsigned int xid, const char *full_path)
2772 struct cifsFileInfo *open_file;
2773 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2774 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2775 struct tcon_link *tlink = NULL;
2776 struct cifs_tcon *tcon = NULL;
2777 struct TCP_Server_Info *server;
2780 * To avoid spurious oplock breaks from server, in the case of
2781 * inodes that we already have open, avoid doing path based
2782 * setting of file size if we can do it by handle.
2783 * This keeps our caching token (oplock) and avoids timeouts
2784 * when the local oplock break takes longer to flush
2785 * writebehind data than the SMB timeout for the SetPathInfo
2786 * request would allow
2788 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2790 tcon = tlink_tcon(open_file->tlink);
2791 server = tcon->ses->server;
2792 if (server->ops->set_file_size)
2793 rc = server->ops->set_file_size(xid, tcon, open_file,
2794 attrs->ia_size, false);
2797 cifsFileInfo_put(open_file);
2798 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2806 tlink = cifs_sb_tlink(cifs_sb);
2808 return PTR_ERR(tlink);
2809 tcon = tlink_tcon(tlink);
2810 server = tcon->ses->server;
2814 * Set file size by pathname rather than by handle either because no
2815 * valid, writeable file handle for it was found or because there was
2816 * an error setting it by handle.
2818 if (server->ops->set_path_size)
2819 rc = server->ops->set_path_size(xid, tcon, full_path,
2820 attrs->ia_size, cifs_sb, false);
2823 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2826 cifs_put_tlink(tlink);
2830 cifsInode->server_eof = attrs->ia_size;
2831 cifs_setsize(inode, attrs->ia_size);
2833 * i_blocks is not related to (i_size / i_blksize), but instead
2834 * 512 byte (2**9) size is required for calculating num blocks.
2835 * Until we can query the server for actual allocation size,
2836 * this is best estimate we have for blocks allocated for a file
2837 * Number of blocks must be rounded up so size 1 is not 0 blocks
2839 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2842 * The man page of truncate says if the size changed,
2843 * then the st_ctime and st_mtime fields for the file
2846 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2847 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2849 cifs_truncate_page(inode->i_mapping, inode->i_size);
2855 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2857 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2861 const char *full_path;
2862 void *page = alloc_dentry_path();
2863 struct inode *inode = d_inode(direntry);
2864 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2865 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2866 struct tcon_link *tlink;
2867 struct cifs_tcon *pTcon;
2868 struct cifs_unix_set_info_args *args = NULL;
2869 struct cifsFileInfo *open_file;
2871 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2872 direntry, attrs->ia_valid);
2876 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2877 attrs->ia_valid |= ATTR_FORCE;
2879 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2883 full_path = build_path_from_dentry(direntry, page);
2884 if (IS_ERR(full_path)) {
2885 rc = PTR_ERR(full_path);
2890 * Attempt to flush data before changing attributes. We need to do
2891 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2892 * ownership or mode then we may also need to do this. Here, we take
2893 * the safe way out and just do the flush on all setattr requests. If
2894 * the flush returns error, store it to report later and continue.
2896 * BB: This should be smarter. Why bother flushing pages that
2897 * will be truncated anyway? Also, should we error out here if
2898 * the flush returns error?
2900 rc = filemap_write_and_wait(inode->i_mapping);
2901 if (is_interrupt_error(rc)) {
2906 mapping_set_error(inode->i_mapping, rc);
2909 if (attrs->ia_valid & ATTR_SIZE) {
2910 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2915 /* skip mode change if it's just for clearing setuid/setgid */
2916 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2917 attrs->ia_valid &= ~ATTR_MODE;
2919 args = kmalloc(sizeof(*args), GFP_KERNEL);
2925 /* set up the struct */
2926 if (attrs->ia_valid & ATTR_MODE)
2927 args->mode = attrs->ia_mode;
2929 args->mode = NO_CHANGE_64;
2931 if (attrs->ia_valid & ATTR_UID)
2932 args->uid = attrs->ia_uid;
2934 args->uid = INVALID_UID; /* no change */
2936 if (attrs->ia_valid & ATTR_GID)
2937 args->gid = attrs->ia_gid;
2939 args->gid = INVALID_GID; /* no change */
2941 if (attrs->ia_valid & ATTR_ATIME)
2942 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2944 args->atime = NO_CHANGE_64;
2946 if (attrs->ia_valid & ATTR_MTIME)
2947 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2949 args->mtime = NO_CHANGE_64;
2951 if (attrs->ia_valid & ATTR_CTIME)
2952 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2954 args->ctime = NO_CHANGE_64;
2957 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2959 u16 nfid = open_file->fid.netfid;
2960 u32 npid = open_file->pid;
2961 pTcon = tlink_tcon(open_file->tlink);
2962 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2963 cifsFileInfo_put(open_file);
2965 tlink = cifs_sb_tlink(cifs_sb);
2966 if (IS_ERR(tlink)) {
2967 rc = PTR_ERR(tlink);
2970 pTcon = tlink_tcon(tlink);
2971 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2973 cifs_remap(cifs_sb));
2974 cifs_put_tlink(tlink);
2980 if ((attrs->ia_valid & ATTR_SIZE) &&
2981 attrs->ia_size != i_size_read(inode)) {
2982 truncate_setsize(inode, attrs->ia_size);
2983 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2986 setattr_copy(&nop_mnt_idmap, inode, attrs);
2987 mark_inode_dirty(inode);
2989 /* force revalidate when any of these times are set since some
2990 of the fs types (eg ext3, fat) do not have fine enough
2991 time granularity to match protocol, and we do not have a
2992 a way (yet) to query the server fs's time granularity (and
2993 whether it rounds times down).
2995 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2996 cifsInode->time = 0;
2999 free_dentry_path(page);
3003 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3006 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3009 kuid_t uid = INVALID_UID;
3010 kgid_t gid = INVALID_GID;
3011 struct inode *inode = d_inode(direntry);
3012 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3013 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3014 struct cifsFileInfo *wfile;
3015 struct cifs_tcon *tcon;
3016 const char *full_path;
3017 void *page = alloc_dentry_path();
3020 __u64 mode = NO_CHANGE_64;
3024 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3025 direntry, attrs->ia_valid);
3027 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3028 attrs->ia_valid |= ATTR_FORCE;
3030 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3032 goto cifs_setattr_exit;
3034 full_path = build_path_from_dentry(direntry, page);
3035 if (IS_ERR(full_path)) {
3036 rc = PTR_ERR(full_path);
3037 goto cifs_setattr_exit;
3041 * Attempt to flush data before changing attributes. We need to do
3042 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3043 * returns error, store it to report later and continue.
3045 * BB: This should be smarter. Why bother flushing pages that
3046 * will be truncated anyway? Also, should we error out here if
3047 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3049 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3050 rc = filemap_write_and_wait(inode->i_mapping);
3051 if (is_interrupt_error(rc)) {
3053 goto cifs_setattr_exit;
3055 mapping_set_error(inode->i_mapping, rc);
3060 if ((attrs->ia_valid & ATTR_MTIME) &&
3061 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3062 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3064 tcon = tlink_tcon(wfile->tlink);
3065 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3066 cifsFileInfo_put(wfile);
3068 goto cifs_setattr_exit;
3069 } else if (rc != -EBADF)
3070 goto cifs_setattr_exit;
3075 if (attrs->ia_valid & ATTR_SIZE) {
3076 rc = cifs_set_file_size(inode, attrs, xid, full_path);
3078 goto cifs_setattr_exit;
3081 if (attrs->ia_valid & ATTR_UID)
3082 uid = attrs->ia_uid;
3084 if (attrs->ia_valid & ATTR_GID)
3085 gid = attrs->ia_gid;
3087 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3088 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3089 if (uid_valid(uid) || gid_valid(gid)) {
3090 mode = NO_CHANGE_64;
3091 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3094 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3096 goto cifs_setattr_exit;
3100 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3101 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3103 /* skip mode change if it's just for clearing setuid/setgid */
3104 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3105 attrs->ia_valid &= ~ATTR_MODE;
3107 if (attrs->ia_valid & ATTR_MODE) {
3108 mode = attrs->ia_mode;
3110 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3111 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3112 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3113 INVALID_UID, INVALID_GID);
3115 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3117 goto cifs_setattr_exit;
3121 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3122 * Pick up the actual mode bits that were set.
3124 if (mode != attrs->ia_mode)
3125 attrs->ia_mode = mode;
3127 if (((mode & S_IWUGO) == 0) &&
3128 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3130 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3132 /* fix up mode if we're not using dynperm */
3133 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3134 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3135 } else if ((mode & S_IWUGO) &&
3136 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3138 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3139 /* Attributes of 0 are ignored */
3141 dosattr |= ATTR_NORMAL;
3143 /* reset local inode permissions to normal */
3144 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3145 attrs->ia_mode &= ~(S_IALLUGO);
3146 if (S_ISDIR(inode->i_mode))
3148 cifs_sb->ctx->dir_mode;
3151 cifs_sb->ctx->file_mode;
3153 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3154 /* ignore mode change - ATTR_READONLY hasn't changed */
3155 attrs->ia_valid &= ~ATTR_MODE;
3159 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3160 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3161 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3162 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3164 /* Even if error on time set, no sense failing the call if
3165 the server would set the time to a reasonable value anyway,
3166 and this check ensures that we are not being called from
3167 sys_utimes in which case we ought to fail the call back to
3168 the user when the server rejects the call */
3169 if ((rc) && (attrs->ia_valid &
3170 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3174 /* do not need local check to inode_check_ok since the server does
3177 goto cifs_setattr_exit;
3179 if ((attrs->ia_valid & ATTR_SIZE) &&
3180 attrs->ia_size != i_size_read(inode)) {
3181 truncate_setsize(inode, attrs->ia_size);
3182 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3185 setattr_copy(&nop_mnt_idmap, inode, attrs);
3186 mark_inode_dirty(inode);
3190 free_dentry_path(page);
3195 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3196 struct iattr *attrs)
3198 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3199 int rc, retries = 0;
3200 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3201 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3202 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3204 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3208 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3209 if (pTcon->unix_ext)
3210 rc = cifs_setattr_unix(direntry, attrs);
3212 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3213 rc = cifs_setattr_nounix(direntry, attrs);
3215 } while (is_retryable_error(rc) && retries < 2);
3217 /* BB: add cifs_setattr_legacy for really old servers */