1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2010
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
30 static void cifs_set_ops(struct inode *inode)
32 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
34 switch (inode->i_mode & S_IFMT) {
36 inode->i_op = &cifs_file_inode_ops;
37 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
38 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
39 inode->i_fop = &cifs_file_direct_nobrl_ops;
41 inode->i_fop = &cifs_file_direct_ops;
42 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44 inode->i_fop = &cifs_file_strict_nobrl_ops;
46 inode->i_fop = &cifs_file_strict_ops;
47 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48 inode->i_fop = &cifs_file_nobrl_ops;
49 else { /* not direct, send byte range locks */
50 inode->i_fop = &cifs_file_ops;
53 /* check if server can support readahead */
54 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
55 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
56 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
58 inode->i_data.a_ops = &cifs_addr_ops;
61 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;
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 fattr->cf_symlink_target = data->symlink_target;
869 data->symlink_target = NULL;
872 fattr->cf_uid = cifs_sb->ctx->linux_uid;
873 fattr->cf_gid = cifs_sb->ctx->linux_gid;
877 cifs_get_file_info(struct file *filp)
881 struct cifs_open_info_data data = {};
882 struct cifs_fattr fattr;
883 struct inode *inode = file_inode(filp);
884 struct cifsFileInfo *cfile = filp->private_data;
885 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
886 struct TCP_Server_Info *server = tcon->ses->server;
888 if (!server->ops->query_file_info)
892 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
895 /* TODO: add support to query reparse tag */
896 data.adjust_tz = false;
897 if (data.symlink_target) {
899 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
901 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
904 cifs_create_junction_fattr(&fattr, inode->i_sb);
910 * FIXME: legacy server -- fall back to path-based call?
911 * for now, just skip revalidating and mark inode for
915 CIFS_I(inode)->time = 0;
922 * don't bother with SFU junk here -- just mark inode as needing
925 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
926 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
927 /* if filetype is different, return error */
928 rc = cifs_fattr_to_inode(inode, &fattr);
930 cifs_free_open_info(&data);
935 /* Simple function to return a 64 bit hash of string. Rarely called */
936 static __u64 simple_hashstr(const char *str)
938 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
942 hash = (hash + (__u64) *str++) * hash_mult;
947 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
949 * cifs_backup_query_path_info - SMB1 fallback code to get ino
951 * Fallback code to get file metadata when we don't have access to
952 * full_path (EACCES) and have backup creds.
954 * @xid: transaction id used to identify original request in logs
955 * @tcon: information about the server share we have mounted
956 * @sb: the superblock stores info such as disk space available
957 * @full_path: name of the file we are getting the metadata for
958 * @resp_buf: will be set to cifs resp buf and needs to be freed with
959 * cifs_buf_release() when done with @data
960 * @data: will be set to search info result buffer
963 cifs_backup_query_path_info(int xid,
964 struct cifs_tcon *tcon,
965 struct super_block *sb,
966 const char *full_path,
968 FILE_ALL_INFO **data)
970 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
971 struct cifs_search_info info = {0};
976 info.endOfSearch = false;
978 info.info_level = SMB_FIND_FILE_UNIX;
979 else if ((tcon->ses->capabilities &
980 tcon->ses->server->vals->cap_nt_find) == 0)
981 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
982 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
983 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
984 else /* no srvino useful for fallback to some netapp */
985 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
987 flags = CIFS_SEARCH_CLOSE_ALWAYS |
988 CIFS_SEARCH_CLOSE_AT_END |
989 CIFS_SEARCH_BACKUP_SEARCH;
991 rc = CIFSFindFirst(xid, tcon, full_path,
992 cifs_sb, NULL, flags, &info, false);
996 *resp_buf = (void *)info.ntwrk_buf_start;
997 *data = (FILE_ALL_INFO *)info.srch_entries_start;
1000 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1002 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1003 struct inode **inode, const char *full_path,
1004 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1006 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1007 struct TCP_Server_Info *server = tcon->ses->server;
1010 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1012 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1014 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1019 * If we have an inode pass a NULL tcon to ensure we don't
1020 * make a round trip to the server. This only works for SMB2+.
1022 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1023 &fattr->cf_uniqueid, data);
1026 * If that fails reuse existing ino or generate one
1027 * and disable server ones
1030 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1032 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1033 cifs_autodisable_serverino(cifs_sb);
1038 /* If no errors, check for zero root inode (invalid) */
1039 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1040 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1043 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1045 /* make an ino by hashing the UNC */
1046 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1047 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1052 static inline bool is_inode_cache_good(struct inode *ino)
1054 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1057 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1058 struct super_block *sb,
1059 const unsigned int xid,
1060 struct cifs_tcon *tcon,
1061 const char *full_path,
1062 struct cifs_fattr *fattr)
1064 struct TCP_Server_Info *server = tcon->ses->server;
1065 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1066 struct kvec rsp_iov, *iov = NULL;
1067 int rsp_buftype = CIFS_NO_BUFFER;
1068 u32 tag = data->reparse.tag;
1071 if (!tag && server->ops->query_reparse_point) {
1072 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1074 &rsp_iov, &rsp_buftype);
1080 switch ((data->reparse.tag = tag)) {
1081 case 0: /* SMB1 symlink */
1082 if (server->ops->query_symlink) {
1083 rc = server->ops->query_symlink(xid, tcon,
1085 &data->symlink_target);
1088 case IO_REPARSE_TAG_MOUNT_POINT:
1089 cifs_create_junction_fattr(fattr, sb);
1093 if (data->symlink_target) {
1095 } else if (server->ops->parse_reparse_point) {
1096 rc = server->ops->parse_reparse_point(cifs_sb,
1102 cifs_open_info_to_fattr(fattr, data, sb);
1104 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1108 static int cifs_get_fattr(struct cifs_open_info_data *data,
1109 struct super_block *sb, int xid,
1110 const struct cifs_fid *fid,
1111 struct cifs_fattr *fattr,
1112 struct inode **inode,
1113 const char *full_path)
1115 struct cifs_open_info_data tmp_data = {};
1116 struct cifs_tcon *tcon;
1117 struct TCP_Server_Info *server;
1118 struct tcon_link *tlink;
1119 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1120 void *smb1_backup_rsp_buf = NULL;
1124 tlink = cifs_sb_tlink(cifs_sb);
1126 return PTR_ERR(tlink);
1127 tcon = tlink_tcon(tlink);
1128 server = tcon->ses->server;
1131 * 1. Fetch file metadata if not provided (data)
1135 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1136 full_path, &tmp_data);
1141 * 2. Convert it to internal cifs metadata (fattr)
1147 * If the file is a reparse point, it is more complicated
1148 * since we have to check if its reparse tag matches a known
1149 * special file type e.g. symlink or fifo or char etc.
1151 if (cifs_open_data_reparse(data)) {
1152 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1155 cifs_open_info_to_fattr(fattr, data, sb);
1159 /* DFS link, no metadata available on this server */
1160 cifs_create_junction_fattr(fattr, sb);
1164 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1166 * perm errors, try again with backup flags if possible
1168 * For SMB2 and later the backup intent flag
1169 * is already sent if needed on open and there
1170 * is no path based FindFirst operation to use
1173 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1174 /* for easier reading */
1176 FILE_DIRECTORY_INFO *fdi;
1177 SEARCH_ID_FULL_DIR_INFO *si;
1179 rc = cifs_backup_query_path_info(xid, tcon, sb,
1181 &smb1_backup_rsp_buf,
1186 move_cifs_info_to_smb2(&data->fi, fi);
1187 fdi = (FILE_DIRECTORY_INFO *)fi;
1188 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1190 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1191 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1192 /* uniqueid set, skip get inum step */
1193 goto handle_mnt_opt;
1195 /* nothing we can do, bail out */
1200 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1203 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1208 * 3. Get or update inode number (fattr->cf_uniqueid)
1211 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1214 * 4. Tweak fattr based on mount options
1216 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1218 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1219 /* query for SFU type info if supported and needed */
1220 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1221 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1222 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1224 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1227 /* fill in 0777 bits from ACL */
1228 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1229 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1230 true, full_path, fid);
1234 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1238 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1239 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1240 false, full_path, fid);
1244 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1250 /* fill in remaining high mode bits e.g. SUID, VTX */
1251 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1252 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1254 /* check for Minshall+French symlinks */
1255 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1256 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1257 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1261 cifs_buf_release(smb1_backup_rsp_buf);
1262 cifs_put_tlink(tlink);
1263 cifs_free_open_info(&tmp_data);
1267 int cifs_get_inode_info(struct inode **inode,
1268 const char *full_path,
1269 struct cifs_open_info_data *data,
1270 struct super_block *sb, int xid,
1271 const struct cifs_fid *fid)
1273 struct cifs_fattr fattr = {};
1276 if (is_inode_cache_good(*inode)) {
1277 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1281 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1285 rc = update_inode_info(sb, &fattr, inode);
1287 kfree(fattr.cf_symlink_target);
1291 static int smb311_posix_get_fattr(struct cifs_fattr *fattr,
1292 const char *full_path,
1293 struct super_block *sb,
1294 const unsigned int xid)
1296 struct cifs_open_info_data data = {};
1297 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1298 struct cifs_tcon *tcon;
1299 struct tcon_link *tlink;
1300 struct cifs_sid owner, group;
1304 tlink = cifs_sb_tlink(cifs_sb);
1306 return PTR_ERR(tlink);
1307 tcon = tlink_tcon(tlink);
1310 * 1. Fetch file metadata
1313 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1318 * 2. Convert it to internal cifs metadata (fattr)
1323 smb311_posix_info_to_fattr(fattr, &data, &owner, &group, sb);
1326 /* DFS link, no metadata available on this server */
1327 cifs_create_junction_fattr(fattr, sb);
1332 * For SMB2 and later the backup intent flag
1333 * is already sent if needed on open and there
1334 * is no path based FindFirst operation to use
1335 * to retry with so nothing we can do, bail out
1339 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1344 * 3. Tweak fattr based on mount options
1346 /* check for Minshall+French symlinks */
1347 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1348 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1349 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1353 cifs_put_tlink(tlink);
1354 cifs_free_open_info(&data);
1358 int smb311_posix_get_inode_info(struct inode **inode, const char *full_path,
1359 struct super_block *sb, const unsigned int xid)
1361 struct cifs_fattr fattr = {};
1364 if (is_inode_cache_good(*inode)) {
1365 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1369 rc = smb311_posix_get_fattr(&fattr, full_path, sb, xid);
1373 rc = update_inode_info(sb, &fattr, inode);
1375 kfree(fattr.cf_symlink_target);
1379 static const struct inode_operations cifs_ipc_inode_ops = {
1380 .lookup = cifs_lookup,
1384 cifs_find_inode(struct inode *inode, void *opaque)
1386 struct cifs_fattr *fattr = opaque;
1388 /* don't match inode with different uniqueid */
1389 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1392 /* use createtime like an i_generation field */
1393 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1396 /* don't match inode of different type */
1397 if (inode_wrong_type(inode, fattr->cf_mode))
1400 /* if it's not a directory or has no dentries, then flag it */
1401 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1402 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1408 cifs_init_inode(struct inode *inode, void *opaque)
1410 struct cifs_fattr *fattr = opaque;
1412 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1413 CIFS_I(inode)->createtime = fattr->cf_createtime;
1418 * walk dentry list for an inode and report whether it has aliases that
1419 * are hashed. We use this to determine if a directory inode can actually
1423 inode_has_hashed_dentries(struct inode *inode)
1425 struct dentry *dentry;
1427 spin_lock(&inode->i_lock);
1428 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1429 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1430 spin_unlock(&inode->i_lock);
1434 spin_unlock(&inode->i_lock);
1438 /* Given fattrs, get a corresponding inode */
1440 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1443 struct inode *inode;
1446 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1448 /* hash down to 32-bits on 32-bit arch */
1449 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1451 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1453 /* was there a potentially problematic inode collision? */
1454 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1455 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1457 if (inode_has_hashed_dentries(inode)) {
1458 cifs_autodisable_serverino(CIFS_SB(sb));
1460 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1461 goto retry_iget5_locked;
1465 /* can't fail - see cifs_find_inode() */
1466 cifs_fattr_to_inode(inode, fattr);
1467 if (sb->s_flags & SB_NOATIME)
1468 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1469 if (inode->i_state & I_NEW) {
1470 inode->i_ino = hash;
1471 cifs_fscache_get_inode_cookie(inode);
1472 unlock_new_inode(inode);
1479 /* gets root inode */
1480 struct inode *cifs_root_iget(struct super_block *sb)
1482 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1483 struct cifs_fattr fattr = {};
1484 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1485 struct inode *inode = NULL;
1491 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1492 && cifs_sb->prepath) {
1493 len = strlen(cifs_sb->prepath);
1494 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1496 return ERR_PTR(-ENOMEM);
1498 memcpy(path+1, cifs_sb->prepath, len);
1500 path = kstrdup("", GFP_KERNEL);
1502 return ERR_PTR(-ENOMEM);
1506 if (tcon->unix_ext) {
1507 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1508 /* some servers mistakenly claim POSIX support */
1509 if (rc != -EOPNOTSUPP)
1511 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1512 tcon->unix_ext = false;
1515 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1516 if (tcon->posix_extensions)
1517 rc = smb311_posix_get_fattr(&fattr, path, sb, xid);
1519 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1523 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1524 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1525 cifs_autodisable_serverino(cifs_sb);
1527 inode = cifs_iget(sb, &fattr);
1531 inode = ERR_PTR(rc);
1535 if (rc && tcon->pipe) {
1536 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1537 spin_lock(&inode->i_lock);
1538 inode->i_mode |= S_IFDIR;
1539 set_nlink(inode, 2);
1540 inode->i_op = &cifs_ipc_inode_ops;
1541 inode->i_fop = &simple_dir_operations;
1542 inode->i_uid = cifs_sb->ctx->linux_uid;
1543 inode->i_gid = cifs_sb->ctx->linux_gid;
1544 spin_unlock(&inode->i_lock);
1547 inode = ERR_PTR(rc);
1553 kfree(fattr.cf_symlink_target);
1558 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1559 const char *full_path, __u32 dosattr)
1561 bool set_time = false;
1562 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1563 struct TCP_Server_Info *server;
1564 FILE_BASIC_INFO info_buf;
1569 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1570 if (!server->ops->set_file_info)
1575 if (attrs->ia_valid & ATTR_ATIME) {
1577 info_buf.LastAccessTime =
1578 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1580 info_buf.LastAccessTime = 0;
1582 if (attrs->ia_valid & ATTR_MTIME) {
1584 info_buf.LastWriteTime =
1585 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1587 info_buf.LastWriteTime = 0;
1590 * Samba throws this field away, but windows may actually use it.
1591 * Do not set ctime unless other time stamps are changed explicitly
1592 * (i.e. by utimes()) since we would then have a mix of client and
1595 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1596 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1597 info_buf.ChangeTime =
1598 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1600 info_buf.ChangeTime = 0;
1602 info_buf.CreationTime = 0; /* don't change */
1603 info_buf.Attributes = cpu_to_le32(dosattr);
1605 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1608 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1610 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1611 * and rename it to a random name that hopefully won't conflict with
1615 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1616 const unsigned int xid)
1620 struct cifs_fid fid;
1621 struct cifs_open_parms oparms;
1622 struct inode *inode = d_inode(dentry);
1623 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1624 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1625 struct tcon_link *tlink;
1626 struct cifs_tcon *tcon;
1627 __u32 dosattr, origattr;
1628 FILE_BASIC_INFO *info_buf = NULL;
1630 tlink = cifs_sb_tlink(cifs_sb);
1632 return PTR_ERR(tlink);
1633 tcon = tlink_tcon(tlink);
1636 * We cannot rename the file if the server doesn't support
1637 * CAP_INFOLEVEL_PASSTHRU
1639 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1644 oparms = (struct cifs_open_parms) {
1647 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1648 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1649 .disposition = FILE_OPEN,
1654 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1658 origattr = cifsInode->cifsAttrs;
1660 origattr |= ATTR_NORMAL;
1662 dosattr = origattr & ~ATTR_READONLY;
1664 dosattr |= ATTR_NORMAL;
1665 dosattr |= ATTR_HIDDEN;
1667 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1668 if (dosattr != origattr) {
1669 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1670 if (info_buf == NULL) {
1674 info_buf->Attributes = cpu_to_le32(dosattr);
1675 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1677 /* although we would like to mark the file hidden
1678 if that fails we will still try to rename it */
1680 cifsInode->cifsAttrs = dosattr;
1682 dosattr = origattr; /* since not able to change them */
1685 /* rename the file */
1686 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1688 cifs_remap(cifs_sb));
1694 /* try to set DELETE_ON_CLOSE */
1695 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1696 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1699 * some samba versions return -ENOENT when we try to set the
1700 * file disposition here. Likely a samba bug, but work around
1701 * it for now. This means that some cifsXXX files may hang
1702 * around after they shouldn't.
1704 * BB: remove this hack after more servers have the fix
1712 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1716 CIFSSMBClose(xid, tcon, fid.netfid);
1719 cifs_put_tlink(tlink);
1723 * reset everything back to the original state. Don't bother
1724 * dealing with errors here since we can't do anything about
1728 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1729 cifs_sb->local_nls, cifs_remap(cifs_sb));
1731 if (dosattr != origattr) {
1732 info_buf->Attributes = cpu_to_le32(origattr);
1733 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1735 cifsInode->cifsAttrs = origattr;
1740 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1742 /* copied from fs/nfs/dir.c with small changes */
1744 cifs_drop_nlink(struct inode *inode)
1746 spin_lock(&inode->i_lock);
1747 if (inode->i_nlink > 0)
1749 spin_unlock(&inode->i_lock);
1753 * If d_inode(dentry) is null (usually meaning the cached dentry
1754 * is a negative dentry) then we would attempt a standard SMB delete, but
1755 * if that fails we can not attempt the fall back mechanisms on EACCES
1756 * but will return the EACCES to the caller. Note that the VFS does not call
1757 * unlink on negative dentries currently.
1759 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1763 const char *full_path;
1765 struct inode *inode = d_inode(dentry);
1766 struct cifsInodeInfo *cifs_inode;
1767 struct super_block *sb = dir->i_sb;
1768 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1769 struct tcon_link *tlink;
1770 struct cifs_tcon *tcon;
1771 struct TCP_Server_Info *server;
1772 struct iattr *attrs = NULL;
1773 __u32 dosattr = 0, origattr = 0;
1775 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1777 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1780 tlink = cifs_sb_tlink(cifs_sb);
1782 return PTR_ERR(tlink);
1783 tcon = tlink_tcon(tlink);
1784 server = tcon->ses->server;
1787 page = alloc_dentry_path();
1789 if (tcon->nodelete) {
1794 /* Unlink can be called from rename so we can not take the
1795 * sb->s_vfs_rename_mutex here */
1796 full_path = build_path_from_dentry(dentry, page);
1797 if (IS_ERR(full_path)) {
1798 rc = PTR_ERR(full_path);
1802 cifs_close_deferred_file_under_dentry(tcon, full_path);
1803 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1804 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1805 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1806 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1807 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1808 cifs_remap(cifs_sb));
1809 cifs_dbg(FYI, "posix del rc %d\n", rc);
1810 if ((rc == 0) || (rc == -ENOENT))
1811 goto psx_del_no_retry;
1813 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1816 if (!server->ops->unlink) {
1818 goto psx_del_no_retry;
1821 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1826 cifs_drop_nlink(inode);
1827 } else if (rc == -ENOENT) {
1829 } else if (rc == -EBUSY) {
1830 if (server->ops->rename_pending_delete) {
1831 rc = server->ops->rename_pending_delete(full_path,
1834 cifs_drop_nlink(inode);
1836 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1837 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1838 if (attrs == NULL) {
1843 /* try to reset dos attributes */
1844 cifs_inode = CIFS_I(inode);
1845 origattr = cifs_inode->cifsAttrs;
1847 origattr |= ATTR_NORMAL;
1848 dosattr = origattr & ~ATTR_READONLY;
1850 dosattr |= ATTR_NORMAL;
1851 dosattr |= ATTR_HIDDEN;
1853 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1857 goto retry_std_delete;
1860 /* undo the setattr if we errored out and it's needed */
1861 if (rc != 0 && dosattr != 0)
1862 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1866 cifs_inode = CIFS_I(inode);
1867 cifs_inode->time = 0; /* will force revalidate to get info
1869 inode_set_ctime_current(inode);
1871 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
1872 cifs_inode = CIFS_I(dir);
1873 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1875 free_dentry_path(page);
1878 cifs_put_tlink(tlink);
1883 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1884 const char *full_path, struct cifs_sb_info *cifs_sb,
1885 struct cifs_tcon *tcon, const unsigned int xid)
1888 struct inode *inode = NULL;
1890 if (tcon->posix_extensions)
1891 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1892 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1893 else if (tcon->unix_ext)
1894 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1896 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1898 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1904 if (!S_ISDIR(inode->i_mode)) {
1906 * mkdir succeeded, but another client has managed to remove the
1907 * sucker and replace it with non-directory. Return success,
1908 * but don't leave the child in dcache.
1915 * setting nlink not necessary except in cases where we failed to get it
1916 * from the server or was set bogus. Also, since this is a brand new
1917 * inode, no need to grab the i_lock before setting the i_nlink.
1919 if (inode->i_nlink < 2)
1920 set_nlink(inode, 2);
1921 mode &= ~current_umask();
1922 /* must turn on setgid bit if parent dir has it */
1923 if (parent->i_mode & S_ISGID)
1926 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1927 if (tcon->unix_ext) {
1928 struct cifs_unix_set_info_args args = {
1930 .ctime = NO_CHANGE_64,
1931 .atime = NO_CHANGE_64,
1932 .mtime = NO_CHANGE_64,
1935 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1936 args.uid = current_fsuid();
1937 if (parent->i_mode & S_ISGID)
1938 args.gid = parent->i_gid;
1940 args.gid = current_fsgid();
1942 args.uid = INVALID_UID; /* no change */
1943 args.gid = INVALID_GID; /* no change */
1945 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1947 cifs_remap(cifs_sb));
1951 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1952 struct TCP_Server_Info *server = tcon->ses->server;
1953 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1954 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1955 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1957 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1958 inode->i_mode = (mode | S_IFDIR);
1960 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1961 inode->i_uid = current_fsuid();
1962 if (inode->i_mode & S_ISGID)
1963 inode->i_gid = parent->i_gid;
1965 inode->i_gid = current_fsgid();
1968 d_instantiate(dentry, inode);
1972 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1974 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1975 const char *full_path, struct cifs_sb_info *cifs_sb,
1976 struct cifs_tcon *tcon, const unsigned int xid)
1980 FILE_UNIX_BASIC_INFO *info = NULL;
1981 struct inode *newinode = NULL;
1982 struct cifs_fattr fattr;
1984 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1987 goto posix_mkdir_out;
1990 mode &= ~current_umask();
1991 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1992 NULL /* netfid */, info, &oplock, full_path,
1993 cifs_sb->local_nls, cifs_remap(cifs_sb));
1994 if (rc == -EOPNOTSUPP)
1995 goto posix_mkdir_out;
1997 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1999 goto posix_mkdir_out;
2002 if (info->Type == cpu_to_le32(-1))
2003 /* no return info, go query for it */
2004 goto posix_mkdir_get_info;
2006 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2007 * need to set uid/gid.
2010 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2011 cifs_fill_uniqueid(inode->i_sb, &fattr);
2012 newinode = cifs_iget(inode->i_sb, &fattr);
2014 goto posix_mkdir_get_info;
2016 d_instantiate(dentry, newinode);
2018 #ifdef CONFIG_CIFS_DEBUG2
2019 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2020 dentry, dentry, newinode);
2022 if (newinode->i_nlink != 2)
2023 cifs_dbg(FYI, "unexpected number of links %d\n",
2030 posix_mkdir_get_info:
2031 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2033 goto posix_mkdir_out;
2035 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2037 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2038 struct dentry *direntry, umode_t mode)
2042 struct cifs_sb_info *cifs_sb;
2043 struct tcon_link *tlink;
2044 struct cifs_tcon *tcon;
2045 struct TCP_Server_Info *server;
2046 const char *full_path;
2049 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2052 cifs_sb = CIFS_SB(inode->i_sb);
2053 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2055 tlink = cifs_sb_tlink(cifs_sb);
2057 return PTR_ERR(tlink);
2058 tcon = tlink_tcon(tlink);
2062 page = alloc_dentry_path();
2063 full_path = build_path_from_dentry(direntry, page);
2064 if (IS_ERR(full_path)) {
2065 rc = PTR_ERR(full_path);
2069 server = tcon->ses->server;
2071 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2072 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2074 d_drop(direntry); /* for time being always refresh inode info */
2078 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2079 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2080 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2081 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2083 if (rc != -EOPNOTSUPP)
2086 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2088 if (!server->ops->mkdir) {
2093 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2094 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2096 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2101 /* TODO: skip this for smb2/smb3 */
2102 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2106 * Force revalidate to get parent dir info when needed since cached
2107 * attributes are invalid now.
2109 CIFS_I(inode)->time = 0;
2110 free_dentry_path(page);
2112 cifs_put_tlink(tlink);
2116 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2120 struct cifs_sb_info *cifs_sb;
2121 struct tcon_link *tlink;
2122 struct cifs_tcon *tcon;
2123 struct TCP_Server_Info *server;
2124 const char *full_path;
2125 void *page = alloc_dentry_path();
2126 struct cifsInodeInfo *cifsInode;
2128 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2132 full_path = build_path_from_dentry(direntry, page);
2133 if (IS_ERR(full_path)) {
2134 rc = PTR_ERR(full_path);
2138 cifs_sb = CIFS_SB(inode->i_sb);
2139 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2144 tlink = cifs_sb_tlink(cifs_sb);
2145 if (IS_ERR(tlink)) {
2146 rc = PTR_ERR(tlink);
2149 tcon = tlink_tcon(tlink);
2150 server = tcon->ses->server;
2152 if (!server->ops->rmdir) {
2154 cifs_put_tlink(tlink);
2158 if (tcon->nodelete) {
2160 cifs_put_tlink(tlink);
2164 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2165 cifs_put_tlink(tlink);
2168 spin_lock(&d_inode(direntry)->i_lock);
2169 i_size_write(d_inode(direntry), 0);
2170 clear_nlink(d_inode(direntry));
2171 spin_unlock(&d_inode(direntry)->i_lock);
2174 cifsInode = CIFS_I(d_inode(direntry));
2175 /* force revalidate to go get info when needed */
2176 cifsInode->time = 0;
2178 cifsInode = CIFS_I(inode);
2180 * Force revalidate to get parent dir info when needed since cached
2181 * attributes are invalid now.
2183 cifsInode->time = 0;
2185 inode_set_ctime_current(d_inode(direntry));
2186 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2189 free_dentry_path(page);
2195 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2196 const char *from_path, struct dentry *to_dentry,
2197 const char *to_path)
2199 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2200 struct tcon_link *tlink;
2201 struct cifs_tcon *tcon;
2202 struct TCP_Server_Info *server;
2203 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2204 struct cifs_fid fid;
2205 struct cifs_open_parms oparms;
2207 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2210 tlink = cifs_sb_tlink(cifs_sb);
2212 return PTR_ERR(tlink);
2213 tcon = tlink_tcon(tlink);
2214 server = tcon->ses->server;
2216 if (!server->ops->rename)
2219 /* try path-based rename first */
2220 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2223 * Don't bother with rename by filehandle unless file is busy and
2224 * source. Note that cross directory moves do not work with
2225 * rename by filehandle to various Windows servers.
2227 if (rc == 0 || rc != -EBUSY)
2228 goto do_rename_exit;
2230 /* Don't fall back to using SMB on SMB 2+ mount */
2231 if (server->vals->protocol_id != 0)
2232 goto do_rename_exit;
2234 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2235 /* open-file renames don't work across directories */
2236 if (to_dentry->d_parent != from_dentry->d_parent)
2237 goto do_rename_exit;
2239 oparms = (struct cifs_open_parms) {
2242 /* open the file to be renamed -- we need DELETE perms */
2243 .desired_access = DELETE,
2244 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2245 .disposition = FILE_OPEN,
2250 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2252 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2253 (const char *) to_dentry->d_name.name,
2254 cifs_sb->local_nls, cifs_remap(cifs_sb));
2255 CIFSSMBClose(xid, tcon, fid.netfid);
2257 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2260 d_move(from_dentry, to_dentry);
2261 cifs_put_tlink(tlink);
2266 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2267 struct dentry *source_dentry, struct inode *target_dir,
2268 struct dentry *target_dentry, unsigned int flags)
2270 const char *from_name, *to_name;
2271 void *page1, *page2;
2272 struct cifs_sb_info *cifs_sb;
2273 struct tcon_link *tlink;
2274 struct cifs_tcon *tcon;
2277 int retry_count = 0;
2278 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2279 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2280 FILE_UNIX_BASIC_INFO *info_buf_target;
2281 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2283 if (flags & ~RENAME_NOREPLACE)
2286 cifs_sb = CIFS_SB(source_dir->i_sb);
2287 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2290 tlink = cifs_sb_tlink(cifs_sb);
2292 return PTR_ERR(tlink);
2293 tcon = tlink_tcon(tlink);
2295 page1 = alloc_dentry_path();
2296 page2 = alloc_dentry_path();
2299 from_name = build_path_from_dentry(source_dentry, page1);
2300 if (IS_ERR(from_name)) {
2301 rc = PTR_ERR(from_name);
2302 goto cifs_rename_exit;
2305 to_name = build_path_from_dentry(target_dentry, page2);
2306 if (IS_ERR(to_name)) {
2307 rc = PTR_ERR(to_name);
2308 goto cifs_rename_exit;
2311 cifs_close_deferred_file_under_dentry(tcon, from_name);
2312 if (d_inode(target_dentry) != NULL)
2313 cifs_close_deferred_file_under_dentry(tcon, to_name);
2315 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2318 if (rc == -EACCES) {
2319 while (retry_count < 3) {
2320 cifs_close_all_deferred_files(tcon);
2321 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2330 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2332 if (flags & RENAME_NOREPLACE)
2333 goto cifs_rename_exit;
2335 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2336 if (rc == -EEXIST && tcon->unix_ext) {
2338 * Are src and dst hardlinks of same inode? We can only tell
2339 * with unix extensions enabled.
2342 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2344 if (info_buf_source == NULL) {
2346 goto cifs_rename_exit;
2349 info_buf_target = info_buf_source + 1;
2350 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2353 cifs_remap(cifs_sb));
2357 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2360 cifs_remap(cifs_sb));
2362 if (tmprc == 0 && (info_buf_source->UniqueId ==
2363 info_buf_target->UniqueId)) {
2364 /* same file, POSIX says that this is a noop */
2366 goto cifs_rename_exit;
2370 * else ... BB we could add the same check for Windows by
2371 * checking the UniqueId via FILE_INTERNAL_INFO
2375 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2377 /* Try unlinking the target dentry if it's not negative */
2378 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2379 if (d_is_dir(target_dentry))
2380 tmprc = cifs_rmdir(target_dir, target_dentry);
2382 tmprc = cifs_unlink(target_dir, target_dentry);
2384 goto cifs_rename_exit;
2385 rc = cifs_do_rename(xid, source_dentry, from_name,
2386 target_dentry, to_name);
2389 /* force revalidate to go get info when needed */
2390 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2393 kfree(info_buf_source);
2394 free_dentry_path(page2);
2395 free_dentry_path(page1);
2397 cifs_put_tlink(tlink);
2402 cifs_dentry_needs_reval(struct dentry *dentry)
2404 struct inode *inode = d_inode(dentry);
2405 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2406 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2407 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2408 struct cached_fid *cfid = NULL;
2410 if (cifs_i->time == 0)
2413 if (CIFS_CACHE_READ(cifs_i))
2416 if (!lookupCacheEnabled)
2419 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2420 spin_lock(&cfid->fid_lock);
2421 if (cfid->time && cifs_i->time > cfid->time) {
2422 spin_unlock(&cfid->fid_lock);
2423 close_cached_dir(cfid);
2426 spin_unlock(&cfid->fid_lock);
2427 close_cached_dir(cfid);
2430 * depending on inode type, check if attribute caching disabled for
2431 * files or directories
2433 if (S_ISDIR(inode->i_mode)) {
2434 if (!cifs_sb->ctx->acdirmax)
2436 if (!time_in_range(jiffies, cifs_i->time,
2437 cifs_i->time + cifs_sb->ctx->acdirmax))
2440 if (!cifs_sb->ctx->acregmax)
2442 if (!time_in_range(jiffies, cifs_i->time,
2443 cifs_i->time + cifs_sb->ctx->acregmax))
2447 /* hardlinked files w/ noserverino get "special" treatment */
2448 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2449 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2456 * Zap the cache. Called when invalid_mapping flag is set.
2459 cifs_invalidate_mapping(struct inode *inode)
2463 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2464 rc = invalidate_inode_pages2(inode->i_mapping);
2466 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2467 __func__, inode, rc);
2474 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2476 * @key: currently unused
2477 * @mode: the task state to sleep in
2480 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2483 if (signal_pending_state(mode, current))
2484 return -ERESTARTSYS;
2489 cifs_revalidate_mapping(struct inode *inode)
2492 unsigned long *flags = &CIFS_I(inode)->flags;
2493 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2495 /* swapfiles are not supposed to be shared */
2496 if (IS_SWAPFILE(inode))
2499 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2500 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2504 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2505 /* for cache=singleclient, do not invalidate */
2506 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2507 goto skip_invalidate;
2509 rc = cifs_invalidate_mapping(inode);
2511 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2515 clear_bit_unlock(CIFS_INO_LOCK, flags);
2516 smp_mb__after_atomic();
2517 wake_up_bit(flags, CIFS_INO_LOCK);
2523 cifs_zap_mapping(struct inode *inode)
2525 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2526 return cifs_revalidate_mapping(inode);
2529 int cifs_revalidate_file_attr(struct file *filp)
2532 struct dentry *dentry = file_dentry(filp);
2533 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2534 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2535 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2537 if (!cifs_dentry_needs_reval(dentry))
2540 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2541 if (tlink_tcon(cfile->tlink)->unix_ext)
2542 rc = cifs_get_file_info_unix(filp);
2544 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2545 rc = cifs_get_file_info(filp);
2550 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2554 struct inode *inode = d_inode(dentry);
2555 struct super_block *sb = dentry->d_sb;
2556 const char *full_path;
2563 if (!cifs_dentry_needs_reval(dentry))
2568 page = alloc_dentry_path();
2569 full_path = build_path_from_dentry(dentry, page);
2570 if (IS_ERR(full_path)) {
2571 rc = PTR_ERR(full_path);
2575 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2576 full_path, inode, inode->i_count.counter,
2577 dentry, cifs_get_time(dentry), jiffies);
2580 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2581 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2582 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2583 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2585 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2587 if (rc == -EAGAIN && count++ < 10)
2590 free_dentry_path(page);
2596 int cifs_revalidate_file(struct file *filp)
2599 struct inode *inode = file_inode(filp);
2601 rc = cifs_revalidate_file_attr(filp);
2605 return cifs_revalidate_mapping(inode);
2608 /* revalidate a dentry's inode attributes */
2609 int cifs_revalidate_dentry(struct dentry *dentry)
2612 struct inode *inode = d_inode(dentry);
2614 rc = cifs_revalidate_dentry_attr(dentry);
2618 return cifs_revalidate_mapping(inode);
2621 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2622 struct kstat *stat, u32 request_mask, unsigned int flags)
2624 struct dentry *dentry = path->dentry;
2625 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2626 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2627 struct inode *inode = d_inode(dentry);
2630 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2634 * We need to be sure that all dirty pages are written and the server
2635 * has actual ctime, mtime and file length.
2637 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2638 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2639 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2640 rc = filemap_fdatawait(inode->i_mapping);
2642 mapping_set_error(inode->i_mapping, rc);
2647 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2648 CIFS_I(inode)->time = 0; /* force revalidate */
2651 * If the caller doesn't require syncing, only sync if
2652 * necessary (e.g. due to earlier truncate or setattr
2653 * invalidating the cached metadata)
2655 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2656 (CIFS_I(inode)->time == 0)) {
2657 rc = cifs_revalidate_dentry_attr(dentry);
2662 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2663 stat->blksize = cifs_sb->ctx->bsize;
2664 stat->ino = CIFS_I(inode)->uniqueid;
2666 /* old CIFS Unix Extensions doesn't return create time */
2667 if (CIFS_I(inode)->createtime) {
2668 stat->result_mask |= STATX_BTIME;
2670 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2673 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2674 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2675 stat->attributes |= STATX_ATTR_COMPRESSED;
2676 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2677 stat->attributes |= STATX_ATTR_ENCRYPTED;
2680 * If on a multiuser mount without unix extensions or cifsacl being
2681 * enabled, and the admin hasn't overridden them, set the ownership
2682 * to the fsuid/fsgid of the current process.
2684 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2685 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2687 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2688 stat->uid = current_fsuid();
2689 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2690 stat->gid = current_fsgid();
2695 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2698 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2699 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2700 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2701 struct TCP_Server_Info *server = tcon->ses->server;
2702 struct cifsFileInfo *cfile;
2705 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2709 * We need to be sure that all dirty pages are written as they
2710 * might fill holes on the server.
2712 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2713 inode->i_mapping->nrpages != 0) {
2714 rc = filemap_fdatawait(inode->i_mapping);
2716 mapping_set_error(inode->i_mapping, rc);
2721 cfile = find_readable_file(cifs_i, false);
2725 if (server->ops->fiemap) {
2726 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2727 cifsFileInfo_put(cfile);
2731 cifsFileInfo_put(cfile);
2735 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2737 pgoff_t index = from >> PAGE_SHIFT;
2738 unsigned offset = from & (PAGE_SIZE - 1);
2742 page = grab_cache_page(mapping, index);
2746 zero_user_segment(page, offset, PAGE_SIZE);
2752 void cifs_setsize(struct inode *inode, loff_t offset)
2754 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2756 spin_lock(&inode->i_lock);
2757 i_size_write(inode, offset);
2758 spin_unlock(&inode->i_lock);
2760 /* Cached inode must be refreshed on truncate */
2762 truncate_pagecache(inode, offset);
2766 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2767 unsigned int xid, const char *full_path)
2770 struct cifsFileInfo *open_file;
2771 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2772 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2773 struct tcon_link *tlink = NULL;
2774 struct cifs_tcon *tcon = NULL;
2775 struct TCP_Server_Info *server;
2778 * To avoid spurious oplock breaks from server, in the case of
2779 * inodes that we already have open, avoid doing path based
2780 * setting of file size if we can do it by handle.
2781 * This keeps our caching token (oplock) and avoids timeouts
2782 * when the local oplock break takes longer to flush
2783 * writebehind data than the SMB timeout for the SetPathInfo
2784 * request would allow
2786 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2788 tcon = tlink_tcon(open_file->tlink);
2789 server = tcon->ses->server;
2790 if (server->ops->set_file_size)
2791 rc = server->ops->set_file_size(xid, tcon, open_file,
2792 attrs->ia_size, false);
2795 cifsFileInfo_put(open_file);
2796 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2804 tlink = cifs_sb_tlink(cifs_sb);
2806 return PTR_ERR(tlink);
2807 tcon = tlink_tcon(tlink);
2808 server = tcon->ses->server;
2812 * Set file size by pathname rather than by handle either because no
2813 * valid, writeable file handle for it was found or because there was
2814 * an error setting it by handle.
2816 if (server->ops->set_path_size)
2817 rc = server->ops->set_path_size(xid, tcon, full_path,
2818 attrs->ia_size, cifs_sb, false);
2821 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2824 cifs_put_tlink(tlink);
2828 cifsInode->server_eof = attrs->ia_size;
2829 cifs_setsize(inode, attrs->ia_size);
2831 * i_blocks is not related to (i_size / i_blksize), but instead
2832 * 512 byte (2**9) size is required for calculating num blocks.
2833 * Until we can query the server for actual allocation size,
2834 * this is best estimate we have for blocks allocated for a file
2835 * Number of blocks must be rounded up so size 1 is not 0 blocks
2837 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2840 * The man page of truncate says if the size changed,
2841 * then the st_ctime and st_mtime fields for the file
2844 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2845 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2847 cifs_truncate_page(inode->i_mapping, inode->i_size);
2853 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2855 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2859 const char *full_path;
2860 void *page = alloc_dentry_path();
2861 struct inode *inode = d_inode(direntry);
2862 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2863 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2864 struct tcon_link *tlink;
2865 struct cifs_tcon *pTcon;
2866 struct cifs_unix_set_info_args *args = NULL;
2867 struct cifsFileInfo *open_file;
2869 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2870 direntry, attrs->ia_valid);
2874 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2875 attrs->ia_valid |= ATTR_FORCE;
2877 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
2881 full_path = build_path_from_dentry(direntry, page);
2882 if (IS_ERR(full_path)) {
2883 rc = PTR_ERR(full_path);
2888 * Attempt to flush data before changing attributes. We need to do
2889 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2890 * ownership or mode then we may also need to do this. Here, we take
2891 * the safe way out and just do the flush on all setattr requests. If
2892 * the flush returns error, store it to report later and continue.
2894 * BB: This should be smarter. Why bother flushing pages that
2895 * will be truncated anyway? Also, should we error out here if
2896 * the flush returns error?
2898 rc = filemap_write_and_wait(inode->i_mapping);
2899 if (is_interrupt_error(rc)) {
2904 mapping_set_error(inode->i_mapping, rc);
2907 if (attrs->ia_valid & ATTR_SIZE) {
2908 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2913 /* skip mode change if it's just for clearing setuid/setgid */
2914 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2915 attrs->ia_valid &= ~ATTR_MODE;
2917 args = kmalloc(sizeof(*args), GFP_KERNEL);
2923 /* set up the struct */
2924 if (attrs->ia_valid & ATTR_MODE)
2925 args->mode = attrs->ia_mode;
2927 args->mode = NO_CHANGE_64;
2929 if (attrs->ia_valid & ATTR_UID)
2930 args->uid = attrs->ia_uid;
2932 args->uid = INVALID_UID; /* no change */
2934 if (attrs->ia_valid & ATTR_GID)
2935 args->gid = attrs->ia_gid;
2937 args->gid = INVALID_GID; /* no change */
2939 if (attrs->ia_valid & ATTR_ATIME)
2940 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2942 args->atime = NO_CHANGE_64;
2944 if (attrs->ia_valid & ATTR_MTIME)
2945 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2947 args->mtime = NO_CHANGE_64;
2949 if (attrs->ia_valid & ATTR_CTIME)
2950 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2952 args->ctime = NO_CHANGE_64;
2955 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2957 u16 nfid = open_file->fid.netfid;
2958 u32 npid = open_file->pid;
2959 pTcon = tlink_tcon(open_file->tlink);
2960 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2961 cifsFileInfo_put(open_file);
2963 tlink = cifs_sb_tlink(cifs_sb);
2964 if (IS_ERR(tlink)) {
2965 rc = PTR_ERR(tlink);
2968 pTcon = tlink_tcon(tlink);
2969 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2971 cifs_remap(cifs_sb));
2972 cifs_put_tlink(tlink);
2978 if ((attrs->ia_valid & ATTR_SIZE) &&
2979 attrs->ia_size != i_size_read(inode)) {
2980 truncate_setsize(inode, attrs->ia_size);
2981 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2984 setattr_copy(&nop_mnt_idmap, inode, attrs);
2985 mark_inode_dirty(inode);
2987 /* force revalidate when any of these times are set since some
2988 of the fs types (eg ext3, fat) do not have fine enough
2989 time granularity to match protocol, and we do not have a
2990 a way (yet) to query the server fs's time granularity (and
2991 whether it rounds times down).
2993 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2994 cifsInode->time = 0;
2997 free_dentry_path(page);
3001 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3004 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3007 kuid_t uid = INVALID_UID;
3008 kgid_t gid = INVALID_GID;
3009 struct inode *inode = d_inode(direntry);
3010 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3011 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3012 struct cifsFileInfo *wfile;
3013 struct cifs_tcon *tcon;
3014 const char *full_path;
3015 void *page = alloc_dentry_path();
3018 __u64 mode = NO_CHANGE_64;
3022 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3023 direntry, attrs->ia_valid);
3025 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3026 attrs->ia_valid |= ATTR_FORCE;
3028 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3030 goto cifs_setattr_exit;
3032 full_path = build_path_from_dentry(direntry, page);
3033 if (IS_ERR(full_path)) {
3034 rc = PTR_ERR(full_path);
3035 goto cifs_setattr_exit;
3039 * Attempt to flush data before changing attributes. We need to do
3040 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3041 * returns error, store it to report later and continue.
3043 * BB: This should be smarter. Why bother flushing pages that
3044 * will be truncated anyway? Also, should we error out here if
3045 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3047 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3048 rc = filemap_write_and_wait(inode->i_mapping);
3049 if (is_interrupt_error(rc)) {
3051 goto cifs_setattr_exit;
3053 mapping_set_error(inode->i_mapping, rc);
3058 if ((attrs->ia_valid & ATTR_MTIME) &&
3059 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3060 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3062 tcon = tlink_tcon(wfile->tlink);
3063 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3064 cifsFileInfo_put(wfile);
3066 goto cifs_setattr_exit;
3067 } else if (rc != -EBADF)
3068 goto cifs_setattr_exit;
3073 if (attrs->ia_valid & ATTR_SIZE) {
3074 rc = cifs_set_file_size(inode, attrs, xid, full_path);
3076 goto cifs_setattr_exit;
3079 if (attrs->ia_valid & ATTR_UID)
3080 uid = attrs->ia_uid;
3082 if (attrs->ia_valid & ATTR_GID)
3083 gid = attrs->ia_gid;
3085 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3086 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3087 if (uid_valid(uid) || gid_valid(gid)) {
3088 mode = NO_CHANGE_64;
3089 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3092 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3094 goto cifs_setattr_exit;
3098 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3099 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3101 /* skip mode change if it's just for clearing setuid/setgid */
3102 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3103 attrs->ia_valid &= ~ATTR_MODE;
3105 if (attrs->ia_valid & ATTR_MODE) {
3106 mode = attrs->ia_mode;
3108 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3109 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3110 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3111 INVALID_UID, INVALID_GID);
3113 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3115 goto cifs_setattr_exit;
3119 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3120 * Pick up the actual mode bits that were set.
3122 if (mode != attrs->ia_mode)
3123 attrs->ia_mode = mode;
3125 if (((mode & S_IWUGO) == 0) &&
3126 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3128 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3130 /* fix up mode if we're not using dynperm */
3131 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3132 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3133 } else if ((mode & S_IWUGO) &&
3134 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3136 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3137 /* Attributes of 0 are ignored */
3139 dosattr |= ATTR_NORMAL;
3141 /* reset local inode permissions to normal */
3142 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3143 attrs->ia_mode &= ~(S_IALLUGO);
3144 if (S_ISDIR(inode->i_mode))
3146 cifs_sb->ctx->dir_mode;
3149 cifs_sb->ctx->file_mode;
3151 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3152 /* ignore mode change - ATTR_READONLY hasn't changed */
3153 attrs->ia_valid &= ~ATTR_MODE;
3157 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3158 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3159 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3160 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3162 /* Even if error on time set, no sense failing the call if
3163 the server would set the time to a reasonable value anyway,
3164 and this check ensures that we are not being called from
3165 sys_utimes in which case we ought to fail the call back to
3166 the user when the server rejects the call */
3167 if ((rc) && (attrs->ia_valid &
3168 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3172 /* do not need local check to inode_check_ok since the server does
3175 goto cifs_setattr_exit;
3177 if ((attrs->ia_valid & ATTR_SIZE) &&
3178 attrs->ia_size != i_size_read(inode)) {
3179 truncate_setsize(inode, attrs->ia_size);
3180 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3183 setattr_copy(&nop_mnt_idmap, inode, attrs);
3184 mark_inode_dirty(inode);
3188 free_dentry_path(page);
3193 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3194 struct iattr *attrs)
3196 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3197 int rc, retries = 0;
3198 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3199 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3200 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3202 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3206 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3207 if (pTcon->unix_ext)
3208 rc = cifs_setattr_unix(direntry, attrs);
3210 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3211 rc = cifs_setattr_nounix(direntry, attrs);
3213 } while (is_retryable_error(rc) && retries < 2);
3215 /* BB: add cifs_setattr_legacy for really old servers */