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"
32 * Set parameters for the netfs library
34 static void cifs_set_netfs_context(struct inode *inode)
36 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
38 netfs_inode_init(&cifs_i->netfs, &cifs_req_ops, true);
41 static void cifs_set_ops(struct inode *inode)
43 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
44 struct netfs_inode *ictx = netfs_inode(inode);
46 switch (inode->i_mode & S_IFMT) {
48 inode->i_op = &cifs_file_inode_ops;
49 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
50 set_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags);
51 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
52 inode->i_fop = &cifs_file_direct_nobrl_ops;
54 inode->i_fop = &cifs_file_direct_ops;
55 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
56 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
57 inode->i_fop = &cifs_file_strict_nobrl_ops;
59 inode->i_fop = &cifs_file_strict_ops;
60 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
61 inode->i_fop = &cifs_file_nobrl_ops;
62 else { /* not direct, send byte range locks */
63 inode->i_fop = &cifs_file_ops;
66 /* check if server can support readahead */
67 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
68 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
69 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
71 inode->i_data.a_ops = &cifs_addr_ops;
72 mapping_set_large_folios(inode->i_mapping);
75 if (IS_AUTOMOUNT(inode)) {
76 inode->i_op = &cifs_namespace_inode_operations;
78 inode->i_op = &cifs_dir_inode_ops;
79 inode->i_fop = &cifs_dir_ops;
83 inode->i_op = &cifs_symlink_inode_ops;
86 init_special_inode(inode, inode->i_mode, inode->i_rdev);
91 /* check inode attributes against fattr. If they don't match, tag the
92 * inode for cache invalidation
95 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
97 struct cifs_fscache_inode_coherency_data cd;
98 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
99 struct timespec64 mtime;
101 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
102 __func__, cifs_i->uniqueid);
104 if (inode->i_state & I_NEW) {
105 cifs_dbg(FYI, "%s: inode %llu is new\n",
106 __func__, cifs_i->uniqueid);
110 /* don't bother with revalidation if we have an oplock */
111 if (CIFS_CACHE_READ(cifs_i)) {
112 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
113 __func__, cifs_i->uniqueid);
117 /* revalidate if mtime or size have changed */
118 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
119 mtime = inode_get_mtime(inode);
120 if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
121 cifs_i->netfs.remote_i_size == fattr->cf_eof) {
122 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
123 __func__, cifs_i->uniqueid);
127 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
128 __func__, cifs_i->uniqueid);
129 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
130 /* Invalidate fscache cookie */
131 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
132 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
136 * copy nlink to the inode, unless it wasn't provided. Provide
137 * sane values if we don't have an existing one and none was provided
140 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
143 * if we're in a situation where we can't trust what we
144 * got from the server (readdir, some non-unix cases)
145 * fake reasonable values
147 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
148 /* only provide fake values on a new inode */
149 if (inode->i_state & I_NEW) {
150 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
158 /* we trust the server, so update it */
159 set_nlink(inode, fattr->cf_nlink);
162 /* populate an inode with info from a cifs_fattr struct */
164 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
167 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
168 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
170 if (!(inode->i_state & I_NEW) &&
171 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
172 CIFS_I(inode)->time = 0; /* force reval */
175 if (inode->i_state & I_NEW)
176 CIFS_I(inode)->netfs.zero_point = fattr->cf_eof;
178 cifs_revalidate_cache(inode, fattr);
180 spin_lock(&inode->i_lock);
181 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
182 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
183 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
184 /* we do not want atime to be less than mtime, it broke some apps */
185 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
186 inode_set_atime_to_ts(inode, fattr->cf_mtime);
188 inode_set_atime_to_ts(inode, fattr->cf_atime);
189 inode_set_mtime_to_ts(inode, fattr->cf_mtime);
190 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
191 inode->i_rdev = fattr->cf_rdev;
192 cifs_nlink_fattr_to_inode(inode, fattr);
193 inode->i_uid = fattr->cf_uid;
194 inode->i_gid = fattr->cf_gid;
196 /* if dynperm is set, don't clobber existing mode */
197 if (inode->i_state & I_NEW ||
198 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
199 inode->i_mode = fattr->cf_mode;
201 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
202 cifs_i->reparse_tag = fattr->cf_cifstag;
204 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
207 cifs_i->time = jiffies;
209 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
210 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
212 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
214 cifs_i->netfs.remote_i_size = fattr->cf_eof;
216 * Can't safely change the file size here if the client is writing to
217 * it due to potential races.
219 if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
220 i_size_write(inode, fattr->cf_eof);
223 * i_blocks is not related to (i_size / i_blksize),
224 * but instead 512 byte (2**9) size is required for
225 * calculating num blocks.
227 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
230 if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) {
231 kfree(cifs_i->symlink_target);
232 cifs_i->symlink_target = fattr->cf_symlink_target;
233 fattr->cf_symlink_target = NULL;
235 spin_unlock(&inode->i_lock);
237 if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
238 inode->i_flags |= S_AUTOMOUNT;
239 if (inode->i_state & I_NEW) {
240 cifs_set_netfs_context(inode);
247 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
249 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
251 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
254 fattr->cf_uniqueid = iunique(sb, ROOT_I);
257 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
259 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
260 struct cifs_sb_info *cifs_sb)
262 memset(fattr, 0, sizeof(*fattr));
263 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
264 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
265 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
267 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
268 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
269 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
270 /* old POSIX extensions don't get create time */
272 fattr->cf_mode = le64_to_cpu(info->Permissions);
275 * Since we set the inode type below we need to mask off
276 * to avoid strange results if bits set above.
278 fattr->cf_mode &= ~S_IFMT;
279 switch (le32_to_cpu(info->Type)) {
281 fattr->cf_mode |= S_IFREG;
282 fattr->cf_dtype = DT_REG;
285 fattr->cf_mode |= S_IFLNK;
286 fattr->cf_dtype = DT_LNK;
289 fattr->cf_mode |= S_IFDIR;
290 fattr->cf_dtype = DT_DIR;
293 fattr->cf_mode |= S_IFCHR;
294 fattr->cf_dtype = DT_CHR;
295 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
296 le64_to_cpu(info->DevMinor) & MINORMASK);
299 fattr->cf_mode |= S_IFBLK;
300 fattr->cf_dtype = DT_BLK;
301 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
302 le64_to_cpu(info->DevMinor) & MINORMASK);
305 fattr->cf_mode |= S_IFIFO;
306 fattr->cf_dtype = DT_FIFO;
309 fattr->cf_mode |= S_IFSOCK;
310 fattr->cf_dtype = DT_SOCK;
313 /* safest to call it a file if we do not know */
314 fattr->cf_mode |= S_IFREG;
315 fattr->cf_dtype = DT_REG;
316 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
320 fattr->cf_uid = cifs_sb->ctx->linux_uid;
321 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
322 u64 id = le64_to_cpu(info->Uid);
323 if (id < ((uid_t)-1)) {
324 kuid_t uid = make_kuid(&init_user_ns, id);
330 fattr->cf_gid = cifs_sb->ctx->linux_gid;
331 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
332 u64 id = le64_to_cpu(info->Gid);
333 if (id < ((gid_t)-1)) {
334 kgid_t gid = make_kgid(&init_user_ns, id);
340 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
344 * Fill a cifs_fattr struct with fake inode info.
346 * Needed to setup cifs_fattr data for the directory which is the
347 * junction to the new submount (ie to setup the fake directory
348 * which represents a DFS referral or reparse mount point).
350 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
351 struct super_block *sb)
353 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
355 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
357 memset(fattr, 0, sizeof(*fattr));
358 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
359 fattr->cf_uid = cifs_sb->ctx->linux_uid;
360 fattr->cf_gid = cifs_sb->ctx->linux_gid;
361 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
362 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
364 fattr->cf_flags = CIFS_FATTR_JUNCTION;
367 /* Update inode with final fattr data */
368 static int update_inode_info(struct super_block *sb,
369 struct cifs_fattr *fattr,
370 struct inode **inode)
372 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
376 *inode = cifs_iget(sb, fattr);
381 /* We already have inode, update it.
383 * If file type or uniqueid is different, return error.
385 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
386 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
387 CIFS_I(*inode)->time = 0; /* force reval */
390 return cifs_fattr_to_inode(*inode, fattr, false);
393 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
395 cifs_get_file_info_unix(struct file *filp)
399 FILE_UNIX_BASIC_INFO find_data;
400 struct cifs_fattr fattr = {};
401 struct inode *inode = file_inode(filp);
402 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
403 struct cifsFileInfo *cfile = filp->private_data;
404 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
408 if (cfile->symlink_target) {
409 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
410 if (!fattr.cf_symlink_target) {
412 goto cifs_gfiunix_out;
416 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
418 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
419 } else if (rc == -EREMOTE) {
420 cifs_create_junction_fattr(&fattr, inode->i_sb);
422 goto cifs_gfiunix_out;
424 rc = cifs_fattr_to_inode(inode, &fattr, false);
431 static int cifs_get_unix_fattr(const unsigned char *full_path,
432 struct super_block *sb,
433 struct cifs_fattr *fattr,
434 struct inode **pinode,
435 const unsigned int xid)
437 struct TCP_Server_Info *server;
438 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
439 FILE_UNIX_BASIC_INFO find_data;
440 struct cifs_tcon *tcon;
441 struct tcon_link *tlink;
444 cifs_dbg(FYI, "Getting info on %s\n", full_path);
446 tlink = cifs_sb_tlink(cifs_sb);
448 return PTR_ERR(tlink);
449 tcon = tlink_tcon(tlink);
450 server = tcon->ses->server;
452 /* could have done a find first instead but this returns more info */
453 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
454 cifs_sb->local_nls, cifs_remap(cifs_sb));
455 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
456 cifs_put_tlink(tlink);
459 cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
460 } else if (rc == -EREMOTE) {
461 cifs_create_junction_fattr(fattr, sb);
468 cifs_fill_uniqueid(sb, fattr);
470 /* check for Minshall+French symlinks */
471 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
472 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
473 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
476 if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
477 if (!server->ops->query_symlink)
479 rc = server->ops->query_symlink(xid, tcon,
481 &fattr->cf_symlink_target);
482 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
487 int cifs_get_inode_info_unix(struct inode **pinode,
488 const unsigned char *full_path,
489 struct super_block *sb, unsigned int xid)
491 struct cifs_fattr fattr = {};
494 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
498 rc = update_inode_info(sb, &fattr, pinode);
500 kfree(fattr.cf_symlink_target);
504 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
505 struct super_block *sb,
506 struct cifs_fattr *fattr,
507 struct inode **pinode,
508 const unsigned int xid)
513 int cifs_get_inode_info_unix(struct inode **pinode,
514 const unsigned char *full_path,
515 struct super_block *sb, unsigned int xid)
519 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
522 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
523 struct cifs_sb_info *cifs_sb, unsigned int xid)
527 struct tcon_link *tlink;
528 struct cifs_tcon *tcon;
530 struct cifs_open_parms oparms;
531 struct cifs_io_parms io_parms = {0};
532 char *symlink_buf_utf16;
533 unsigned int symlink_len_utf16;
535 unsigned int bytes_read;
537 int buf_type = CIFS_NO_BUFFER;
541 fattr->cf_mode &= ~S_IFMT;
543 if (fattr->cf_eof == 0) {
544 cifs_dbg(FYI, "Fifo\n");
545 fattr->cf_mode |= S_IFIFO;
546 fattr->cf_dtype = DT_FIFO;
548 } else if (fattr->cf_eof > 1 && fattr->cf_eof < 8) {
549 fattr->cf_mode |= S_IFREG;
550 fattr->cf_dtype = DT_REG;
551 return -EINVAL; /* EOPNOTSUPP? */
554 tlink = cifs_sb_tlink(cifs_sb);
556 return PTR_ERR(tlink);
557 tcon = tlink_tcon(tlink);
559 oparms = (struct cifs_open_parms) {
562 .desired_access = GENERIC_READ,
563 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
564 .disposition = FILE_OPEN,
569 if (tcon->ses->server->oplocks)
573 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
575 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
576 cifs_put_tlink(tlink);
581 io_parms.netfid = fid.netfid;
582 io_parms.pid = current->tgid;
583 io_parms.tcon = tcon;
585 io_parms.length = 24;
587 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
588 &bytes_read, &pbuf, &buf_type);
589 if ((rc == 0) && (bytes_read >= 8)) {
590 if (memcmp("IntxBLK\0", pbuf, 8) == 0) {
591 cifs_dbg(FYI, "Block device\n");
592 fattr->cf_mode |= S_IFBLK;
593 fattr->cf_dtype = DT_BLK;
594 if (bytes_read == 24) {
595 /* we have enough to decode dev num */
596 __u64 mjr; /* major */
597 __u64 mnr; /* minor */
598 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
599 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
600 fattr->cf_rdev = MKDEV(mjr, mnr);
601 } else if (bytes_read == 16) {
603 * Windows NFS server before Windows Server 2012
604 * stores major and minor number in SFU-modified
605 * style, just as 32-bit numbers. Recognize it.
607 __u32 mjr; /* major */
608 __u32 mnr; /* minor */
609 mjr = le32_to_cpu(*(__le32 *)(pbuf+8));
610 mnr = le32_to_cpu(*(__le32 *)(pbuf+12));
611 fattr->cf_rdev = MKDEV(mjr, mnr);
613 } else if (memcmp("IntxCHR\0", pbuf, 8) == 0) {
614 cifs_dbg(FYI, "Char device\n");
615 fattr->cf_mode |= S_IFCHR;
616 fattr->cf_dtype = DT_CHR;
617 if (bytes_read == 24) {
618 /* we have enough to decode dev num */
619 __u64 mjr; /* major */
620 __u64 mnr; /* minor */
621 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
622 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
623 fattr->cf_rdev = MKDEV(mjr, mnr);
624 } else if (bytes_read == 16) {
626 * Windows NFS server before Windows Server 2012
627 * stores major and minor number in SFU-modified
628 * style, just as 32-bit numbers. Recognize it.
630 __u32 mjr; /* major */
631 __u32 mnr; /* minor */
632 mjr = le32_to_cpu(*(__le32 *)(pbuf+8));
633 mnr = le32_to_cpu(*(__le32 *)(pbuf+12));
634 fattr->cf_rdev = MKDEV(mjr, mnr);
636 } else if (memcmp("LnxSOCK", pbuf, 8) == 0) {
637 cifs_dbg(FYI, "Socket\n");
638 fattr->cf_mode |= S_IFSOCK;
639 fattr->cf_dtype = DT_SOCK;
640 } else if (memcmp("IntxLNK\1", pbuf, 8) == 0) {
641 cifs_dbg(FYI, "Symlink\n");
642 fattr->cf_mode |= S_IFLNK;
643 fattr->cf_dtype = DT_LNK;
644 if ((fattr->cf_eof > 8) && (fattr->cf_eof % 2 == 0)) {
645 symlink_buf_utf16 = kmalloc(fattr->cf_eof-8 + 1, GFP_KERNEL);
646 if (symlink_buf_utf16) {
648 io_parms.length = fattr->cf_eof-8 + 1;
649 buf_type = CIFS_NO_BUFFER;
650 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
655 * Check that read buffer has valid length and does not
656 * contain UTF-16 null codepoint (via UniStrnlen() call)
657 * because Linux cannot process symlink with null byte.
660 (symlink_len_utf16 > 0) &&
661 (symlink_len_utf16 < fattr->cf_eof-8 + 1) &&
662 (symlink_len_utf16 % 2 == 0) &&
663 (UniStrnlen((wchar_t *)symlink_buf_utf16, symlink_len_utf16/2) == symlink_len_utf16/2)) {
664 fattr->cf_symlink_target =
665 cifs_strndup_from_utf16(symlink_buf_utf16,
669 if (!fattr->cf_symlink_target)
672 kfree(symlink_buf_utf16);
677 } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
678 cifs_dbg(FYI, "FIFO\n");
679 fattr->cf_mode |= S_IFIFO;
680 fattr->cf_dtype = DT_FIFO;
682 fattr->cf_mode |= S_IFREG; /* file? */
683 fattr->cf_dtype = DT_REG;
686 } else if ((rc == 0) && (bytes_read == 1) && (pbuf[0] == '\0')) {
687 cifs_dbg(FYI, "Socket\n");
688 fattr->cf_mode |= S_IFSOCK;
689 fattr->cf_dtype = DT_SOCK;
691 fattr->cf_mode |= S_IFREG; /* then it is a file */
692 fattr->cf_dtype = DT_REG;
693 rc = -EOPNOTSUPP; /* or some unknown SFU type */
696 tcon->ses->server->ops->close(xid, tcon, &fid);
697 cifs_put_tlink(tlink);
701 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
704 * Fetch mode bits as provided by SFU.
706 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
708 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
709 struct cifs_sb_info *cifs_sb, unsigned int xid)
711 #ifdef CONFIG_CIFS_XATTR
715 struct tcon_link *tlink;
716 struct cifs_tcon *tcon;
718 tlink = cifs_sb_tlink(cifs_sb);
720 return PTR_ERR(tlink);
721 tcon = tlink_tcon(tlink);
723 if (tcon->ses->server->ops->query_all_EAs == NULL) {
724 cifs_put_tlink(tlink);
728 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
729 "SETFILEBITS", ea_value, 4 /* size of buf */,
731 cifs_put_tlink(tlink);
735 mode = le32_to_cpu(*((__le32 *)ea_value));
736 fattr->cf_mode &= ~SFBITS_MASK;
737 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
738 mode, fattr->cf_mode);
739 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
740 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
749 #define POSIX_TYPE_FILE 0
750 #define POSIX_TYPE_DIR 1
751 #define POSIX_TYPE_SYMLINK 2
752 #define POSIX_TYPE_CHARDEV 3
753 #define POSIX_TYPE_BLKDEV 4
754 #define POSIX_TYPE_FIFO 5
755 #define POSIX_TYPE_SOCKET 6
757 #define POSIX_X_OTH 0000001
758 #define POSIX_W_OTH 0000002
759 #define POSIX_R_OTH 0000004
760 #define POSIX_X_GRP 0000010
761 #define POSIX_W_GRP 0000020
762 #define POSIX_R_GRP 0000040
763 #define POSIX_X_USR 0000100
764 #define POSIX_W_USR 0000200
765 #define POSIX_R_USR 0000400
766 #define POSIX_STICKY 0001000
767 #define POSIX_SET_GID 0002000
768 #define POSIX_SET_UID 0004000
770 #define POSIX_OTH_MASK 0000007
771 #define POSIX_GRP_MASK 0000070
772 #define POSIX_USR_MASK 0000700
773 #define POSIX_PERM_MASK 0000777
774 #define POSIX_FILETYPE_MASK 0070000
776 #define POSIX_FILETYPE_SHIFT 12
778 static u32 wire_perms_to_posix(u32 wire)
782 mode |= (wire & POSIX_X_OTH) ? S_IXOTH : 0;
783 mode |= (wire & POSIX_W_OTH) ? S_IWOTH : 0;
784 mode |= (wire & POSIX_R_OTH) ? S_IROTH : 0;
785 mode |= (wire & POSIX_X_GRP) ? S_IXGRP : 0;
786 mode |= (wire & POSIX_W_GRP) ? S_IWGRP : 0;
787 mode |= (wire & POSIX_R_GRP) ? S_IRGRP : 0;
788 mode |= (wire & POSIX_X_USR) ? S_IXUSR : 0;
789 mode |= (wire & POSIX_W_USR) ? S_IWUSR : 0;
790 mode |= (wire & POSIX_R_USR) ? S_IRUSR : 0;
791 mode |= (wire & POSIX_STICKY) ? S_ISVTX : 0;
792 mode |= (wire & POSIX_SET_GID) ? S_ISGID : 0;
793 mode |= (wire & POSIX_SET_UID) ? S_ISUID : 0;
798 static u32 posix_filetypes[] = {
808 static u32 wire_filetype_to_posix(u32 wire_type)
810 if (wire_type >= ARRAY_SIZE(posix_filetypes)) {
811 pr_warn("Unexpected type %u", wire_type);
814 return posix_filetypes[wire_type];
817 umode_t wire_mode_to_posix(u32 wire, bool is_dir)
822 wire_type = (wire & POSIX_FILETYPE_MASK) >> POSIX_FILETYPE_SHIFT;
823 /* older servers do not set POSIX file type in the mode field in the response */
824 if ((wire_type == 0) && is_dir)
825 mode = wire_perms_to_posix(wire) | S_IFDIR;
827 mode = (wire_perms_to_posix(wire) | wire_filetype_to_posix(wire_type));
828 return (umode_t)mode;
831 /* Fill a cifs_fattr struct with info from POSIX info struct */
832 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
833 struct cifs_open_info_data *data,
834 struct super_block *sb)
836 struct smb311_posix_qinfo *info = &data->posix_fi;
837 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
838 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
840 memset(fattr, 0, sizeof(*fattr));
842 /* no fattr->flags to set */
843 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
844 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
846 if (info->LastAccessTime)
847 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
849 ktime_get_coarse_real_ts64(&fattr->cf_atime);
851 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
852 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
854 if (data->adjust_tz) {
855 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
856 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
860 * The srv fs device id is overridden on network mount so setting
861 * @fattr->cf_rdev isn't needed here.
863 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
864 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
865 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
866 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
867 fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode),
868 fattr->cf_cifsattrs & ATTR_DIRECTORY);
870 if (cifs_open_data_reparse(data) &&
871 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
874 fattr->cf_dtype = S_DT(fattr->cf_mode);
877 if (S_ISLNK(fattr->cf_mode)) {
878 if (likely(data->symlink_target))
879 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
880 fattr->cf_symlink_target = data->symlink_target;
881 data->symlink_target = NULL;
883 sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER);
884 sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP);
886 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
887 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
890 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
891 struct cifs_open_info_data *data,
892 struct super_block *sb)
894 struct smb2_file_all_info *info = &data->fi;
895 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
896 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
898 memset(fattr, 0, sizeof(*fattr));
899 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
900 if (info->DeletePending)
901 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
903 if (info->LastAccessTime)
904 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
906 ktime_get_coarse_real_ts64(&fattr->cf_atime);
908 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
909 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
911 if (data->adjust_tz) {
912 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
913 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
916 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
917 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
918 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
919 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
920 fattr->cf_uid = cifs_sb->ctx->linux_uid;
921 fattr->cf_gid = cifs_sb->ctx->linux_gid;
923 fattr->cf_mode = cifs_sb->ctx->file_mode;
924 if (cifs_open_data_reparse(data) &&
925 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
928 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
929 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
930 fattr->cf_dtype = DT_DIR;
932 * Server can return wrong NumberOfLinks value for directories
933 * when Unix extensions are disabled - fake it.
936 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
938 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
939 fattr->cf_dtype = DT_REG;
942 * Don't accept zero nlink from non-unix servers unless
943 * delete is pending. Instead mark it as unknown.
945 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
946 !info->DeletePending) {
947 cifs_dbg(VFS, "bogus file nlink value %u\n",
949 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
953 /* clear write bits if ATTR_READONLY is set */
954 if (fattr->cf_cifsattrs & ATTR_READONLY)
955 fattr->cf_mode &= ~(S_IWUGO);
958 if (S_ISLNK(fattr->cf_mode)) {
959 if (likely(data->symlink_target))
960 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
961 fattr->cf_symlink_target = data->symlink_target;
962 data->symlink_target = NULL;
967 cifs_get_file_info(struct file *filp)
971 struct cifs_open_info_data data = {};
972 struct cifs_fattr fattr;
973 struct inode *inode = file_inode(filp);
974 struct cifsFileInfo *cfile = filp->private_data;
975 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
976 struct TCP_Server_Info *server = tcon->ses->server;
977 struct dentry *dentry = filp->f_path.dentry;
978 void *page = alloc_dentry_path();
979 const unsigned char *path;
981 if (!server->ops->query_file_info) {
982 free_dentry_path(page);
987 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
990 /* TODO: add support to query reparse tag */
991 data.adjust_tz = false;
992 if (data.symlink_target) {
994 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
996 path = build_path_from_dentry(dentry, page);
1001 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
1002 if (fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1003 cifs_mark_open_handles_for_deleted_file(inode, path);
1006 cifs_create_junction_fattr(&fattr, inode->i_sb);
1011 * FIXME: legacy server -- fall back to path-based call?
1012 * for now, just skip revalidating and mark inode for
1016 CIFS_I(inode)->time = 0;
1023 * don't bother with SFU junk here -- just mark inode as needing
1026 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
1027 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1028 /* if filetype is different, return error */
1029 rc = cifs_fattr_to_inode(inode, &fattr, false);
1031 cifs_free_open_info(&data);
1032 free_dentry_path(page);
1037 /* Simple function to return a 64 bit hash of string. Rarely called */
1038 static __u64 simple_hashstr(const char *str)
1040 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
1044 hash = (hash + (__u64) *str++) * hash_mult;
1049 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1051 * cifs_backup_query_path_info - SMB1 fallback code to get ino
1053 * Fallback code to get file metadata when we don't have access to
1054 * full_path (EACCES) and have backup creds.
1056 * @xid: transaction id used to identify original request in logs
1057 * @tcon: information about the server share we have mounted
1058 * @sb: the superblock stores info such as disk space available
1059 * @full_path: name of the file we are getting the metadata for
1060 * @resp_buf: will be set to cifs resp buf and needs to be freed with
1061 * cifs_buf_release() when done with @data
1062 * @data: will be set to search info result buffer
1065 cifs_backup_query_path_info(int xid,
1066 struct cifs_tcon *tcon,
1067 struct super_block *sb,
1068 const char *full_path,
1070 FILE_ALL_INFO **data)
1072 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1073 struct cifs_search_info info = {0};
1078 info.endOfSearch = false;
1080 info.info_level = SMB_FIND_FILE_UNIX;
1081 else if ((tcon->ses->capabilities &
1082 tcon->ses->server->vals->cap_nt_find) == 0)
1083 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
1084 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
1085 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
1086 else /* no srvino useful for fallback to some netapp */
1087 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
1089 flags = CIFS_SEARCH_CLOSE_ALWAYS |
1090 CIFS_SEARCH_CLOSE_AT_END |
1091 CIFS_SEARCH_BACKUP_SEARCH;
1093 rc = CIFSFindFirst(xid, tcon, full_path,
1094 cifs_sb, NULL, flags, &info, false);
1098 *resp_buf = (void *)info.ntwrk_buf_start;
1099 *data = (FILE_ALL_INFO *)info.srch_entries_start;
1102 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1104 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1105 struct inode **inode, const char *full_path,
1106 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1108 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1109 struct TCP_Server_Info *server = tcon->ses->server;
1112 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1114 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1116 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1121 * If we have an inode pass a NULL tcon to ensure we don't
1122 * make a round trip to the server. This only works for SMB2+.
1124 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1125 &fattr->cf_uniqueid, data);
1128 * If that fails reuse existing ino or generate one
1129 * and disable server ones
1132 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1134 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1135 cifs_autodisable_serverino(cifs_sb);
1140 /* If no errors, check for zero root inode (invalid) */
1141 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1142 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1145 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1147 /* make an ino by hashing the UNC */
1148 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1149 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1154 static inline bool is_inode_cache_good(struct inode *ino)
1156 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1159 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1160 struct super_block *sb,
1161 const unsigned int xid,
1162 struct cifs_tcon *tcon,
1163 const char *full_path,
1164 struct cifs_fattr *fattr)
1166 struct TCP_Server_Info *server = tcon->ses->server;
1167 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1168 struct kvec rsp_iov, *iov = NULL;
1169 int rsp_buftype = CIFS_NO_BUFFER;
1170 u32 tag = data->reparse.tag;
1173 if (!tag && server->ops->query_reparse_point) {
1174 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1176 &rsp_iov, &rsp_buftype);
1179 } else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1180 data->reparse.io.iov.iov_base) {
1181 iov = &data->reparse.io.iov;
1185 data->reparse.tag = tag;
1186 if (!data->reparse.tag) {
1187 if (server->ops->query_symlink) {
1188 rc = server->ops->query_symlink(xid, tcon,
1190 &data->symlink_target);
1192 if (rc == -EOPNOTSUPP)
1193 data->reparse.tag = IO_REPARSE_TAG_INTERNAL;
1196 switch (data->reparse.tag) {
1197 case 0: /* SMB1 symlink */
1199 case IO_REPARSE_TAG_INTERNAL:
1201 if (le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY) {
1202 cifs_create_junction_fattr(fattr, sb);
1206 case IO_REPARSE_TAG_MOUNT_POINT:
1207 cifs_create_junction_fattr(fattr, sb);
1211 /* Check for cached reparse point data */
1212 if (data->symlink_target || data->reparse.buf) {
1214 } else if (iov && server->ops->parse_reparse_point) {
1215 rc = server->ops->parse_reparse_point(cifs_sb,
1222 if (tcon->posix_extensions)
1223 smb311_posix_info_to_fattr(fattr, data, sb);
1225 cifs_open_info_to_fattr(fattr, data, sb);
1227 fattr->cf_cifstag = data->reparse.tag;
1228 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1232 static int cifs_get_fattr(struct cifs_open_info_data *data,
1233 struct super_block *sb, int xid,
1234 const struct cifs_fid *fid,
1235 struct cifs_fattr *fattr,
1236 struct inode **inode,
1237 const char *full_path)
1239 struct cifs_open_info_data tmp_data = {};
1240 struct cifs_tcon *tcon;
1241 struct TCP_Server_Info *server;
1242 struct tcon_link *tlink;
1243 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1244 void *smb1_backup_rsp_buf = NULL;
1248 tlink = cifs_sb_tlink(cifs_sb);
1250 return PTR_ERR(tlink);
1251 tcon = tlink_tcon(tlink);
1252 server = tcon->ses->server;
1255 * 1. Fetch file metadata if not provided (data)
1259 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1260 full_path, &tmp_data);
1265 * 2. Convert it to internal cifs metadata (fattr)
1271 * If the file is a reparse point, it is more complicated
1272 * since we have to check if its reparse tag matches a known
1273 * special file type e.g. symlink or fifo or char etc.
1275 if (cifs_open_data_reparse(data)) {
1276 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1279 cifs_open_info_to_fattr(fattr, data, sb);
1281 if (!rc && *inode &&
1282 (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING))
1283 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1286 /* DFS link, no metadata available on this server */
1287 cifs_create_junction_fattr(fattr, sb);
1291 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1293 * perm errors, try again with backup flags if possible
1295 * For SMB2 and later the backup intent flag
1296 * is already sent if needed on open and there
1297 * is no path based FindFirst operation to use
1300 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1301 /* for easier reading */
1303 FILE_DIRECTORY_INFO *fdi;
1304 SEARCH_ID_FULL_DIR_INFO *si;
1306 rc = cifs_backup_query_path_info(xid, tcon, sb,
1308 &smb1_backup_rsp_buf,
1313 move_cifs_info_to_smb2(&data->fi, fi);
1314 fdi = (FILE_DIRECTORY_INFO *)fi;
1315 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1317 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1318 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1319 /* uniqueid set, skip get inum step */
1320 goto handle_mnt_opt;
1322 /* nothing we can do, bail out */
1327 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1330 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1335 * 3. Get or update inode number (fattr->cf_uniqueid)
1338 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1341 * 4. Tweak fattr based on mount options
1343 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1345 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1346 /* query for SFU type info if supported and needed */
1347 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1348 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1349 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1351 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1354 /* fill in 0777 bits from ACL */
1355 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1356 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1357 true, full_path, fid);
1361 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1365 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1366 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1367 false, full_path, fid);
1371 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1375 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1376 /* fill in remaining high mode bits e.g. SUID, VTX */
1377 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1378 else if (!(tcon->posix_extensions))
1379 /* clear write bits if ATTR_READONLY is set */
1380 if (fattr->cf_cifsattrs & ATTR_READONLY)
1381 fattr->cf_mode &= ~(S_IWUGO);
1384 /* check for Minshall+French symlinks */
1385 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1386 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1387 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1391 cifs_buf_release(smb1_backup_rsp_buf);
1392 cifs_put_tlink(tlink);
1393 cifs_free_open_info(&tmp_data);
1397 int cifs_get_inode_info(struct inode **inode,
1398 const char *full_path,
1399 struct cifs_open_info_data *data,
1400 struct super_block *sb, int xid,
1401 const struct cifs_fid *fid)
1403 struct cifs_fattr fattr = {};
1406 if (is_inode_cache_good(*inode)) {
1407 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1411 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1415 rc = update_inode_info(sb, &fattr, inode);
1417 kfree(fattr.cf_symlink_target);
1421 static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1422 struct cifs_fattr *fattr,
1423 const char *full_path,
1424 struct super_block *sb,
1425 const unsigned int xid)
1427 struct cifs_open_info_data tmp_data = {};
1428 struct TCP_Server_Info *server;
1429 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1430 struct cifs_tcon *tcon;
1431 struct tcon_link *tlink;
1435 tlink = cifs_sb_tlink(cifs_sb);
1437 return PTR_ERR(tlink);
1438 tcon = tlink_tcon(tlink);
1439 server = tcon->ses->server;
1442 * 1. Fetch file metadata if not provided (data)
1445 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1446 full_path, &tmp_data);
1451 * 2. Convert it to internal cifs metadata (fattr)
1456 if (cifs_open_data_reparse(data)) {
1457 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1460 smb311_posix_info_to_fattr(fattr, data, sb);
1464 /* DFS link, no metadata available on this server */
1465 cifs_create_junction_fattr(fattr, sb);
1470 * For SMB2 and later the backup intent flag
1471 * is already sent if needed on open and there
1472 * is no path based FindFirst operation to use
1473 * to retry with so nothing we can do, bail out
1477 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1482 * 3. Tweak fattr based on mount options
1484 /* check for Minshall+French symlinks */
1485 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1486 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1487 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1491 cifs_put_tlink(tlink);
1492 cifs_free_open_info(data);
1496 int smb311_posix_get_inode_info(struct inode **inode,
1497 const char *full_path,
1498 struct cifs_open_info_data *data,
1499 struct super_block *sb,
1500 const unsigned int xid)
1502 struct cifs_fattr fattr = {};
1505 if (is_inode_cache_good(*inode)) {
1506 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1510 rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1514 rc = update_inode_info(sb, &fattr, inode);
1515 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1516 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1518 kfree(fattr.cf_symlink_target);
1522 static const struct inode_operations cifs_ipc_inode_ops = {
1523 .lookup = cifs_lookup,
1527 cifs_find_inode(struct inode *inode, void *opaque)
1529 struct cifs_fattr *fattr = opaque;
1531 /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1533 /* don't match inode with different uniqueid */
1534 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1537 /* use createtime like an i_generation field */
1538 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1541 /* don't match inode of different type */
1542 if (inode_wrong_type(inode, fattr->cf_mode))
1545 /* if it's not a directory or has no dentries, then flag it */
1546 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1547 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1553 cifs_init_inode(struct inode *inode, void *opaque)
1555 struct cifs_fattr *fattr = opaque;
1557 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1558 CIFS_I(inode)->createtime = fattr->cf_createtime;
1563 * walk dentry list for an inode and report whether it has aliases that
1564 * are hashed. We use this to determine if a directory inode can actually
1568 inode_has_hashed_dentries(struct inode *inode)
1570 struct dentry *dentry;
1572 spin_lock(&inode->i_lock);
1573 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1574 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1575 spin_unlock(&inode->i_lock);
1579 spin_unlock(&inode->i_lock);
1583 /* Given fattrs, get a corresponding inode */
1585 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1588 struct inode *inode;
1591 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1593 /* hash down to 32-bits on 32-bit arch */
1594 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1596 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1598 /* was there a potentially problematic inode collision? */
1599 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1600 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1602 if (inode_has_hashed_dentries(inode)) {
1603 cifs_autodisable_serverino(CIFS_SB(sb));
1605 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1606 goto retry_iget5_locked;
1610 /* can't fail - see cifs_find_inode() */
1611 cifs_fattr_to_inode(inode, fattr, false);
1612 if (sb->s_flags & SB_NOATIME)
1613 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1614 if (inode->i_state & I_NEW) {
1615 inode->i_ino = hash;
1616 cifs_fscache_get_inode_cookie(inode);
1617 unlock_new_inode(inode);
1624 /* gets root inode */
1625 struct inode *cifs_root_iget(struct super_block *sb)
1627 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1628 struct cifs_fattr fattr = {};
1629 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1630 struct inode *inode = NULL;
1636 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1637 && cifs_sb->prepath) {
1638 len = strlen(cifs_sb->prepath);
1639 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1641 return ERR_PTR(-ENOMEM);
1643 memcpy(path+1, cifs_sb->prepath, len);
1645 path = kstrdup("", GFP_KERNEL);
1647 return ERR_PTR(-ENOMEM);
1651 if (tcon->unix_ext) {
1652 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1653 /* some servers mistakenly claim POSIX support */
1654 if (rc != -EOPNOTSUPP)
1656 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1657 tcon->unix_ext = false;
1660 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1661 if (tcon->posix_extensions)
1662 rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1664 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1668 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1669 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1670 cifs_autodisable_serverino(cifs_sb);
1672 inode = cifs_iget(sb, &fattr);
1676 inode = ERR_PTR(rc);
1680 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1681 cifs_mark_open_handles_for_deleted_file(inode, path);
1683 if (rc && tcon->pipe) {
1684 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1685 spin_lock(&inode->i_lock);
1686 inode->i_mode |= S_IFDIR;
1687 set_nlink(inode, 2);
1688 inode->i_op = &cifs_ipc_inode_ops;
1689 inode->i_fop = &simple_dir_operations;
1690 inode->i_uid = cifs_sb->ctx->linux_uid;
1691 inode->i_gid = cifs_sb->ctx->linux_gid;
1692 spin_unlock(&inode->i_lock);
1695 inode = ERR_PTR(rc);
1701 kfree(fattr.cf_symlink_target);
1706 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1707 const char *full_path, __u32 dosattr)
1709 bool set_time = false;
1710 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1711 struct TCP_Server_Info *server;
1712 FILE_BASIC_INFO info_buf;
1717 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1718 if (!server->ops->set_file_info)
1723 if (attrs->ia_valid & ATTR_ATIME) {
1725 info_buf.LastAccessTime =
1726 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1728 info_buf.LastAccessTime = 0;
1730 if (attrs->ia_valid & ATTR_MTIME) {
1732 info_buf.LastWriteTime =
1733 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1735 info_buf.LastWriteTime = 0;
1738 * Samba throws this field away, but windows may actually use it.
1739 * Do not set ctime unless other time stamps are changed explicitly
1740 * (i.e. by utimes()) since we would then have a mix of client and
1743 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1744 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1745 info_buf.ChangeTime =
1746 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1748 info_buf.ChangeTime = 0;
1750 info_buf.CreationTime = 0; /* don't change */
1751 info_buf.Attributes = cpu_to_le32(dosattr);
1753 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1756 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1758 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1759 * and rename it to a random name that hopefully won't conflict with
1763 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1764 const unsigned int xid)
1768 struct cifs_fid fid;
1769 struct cifs_open_parms oparms;
1770 struct inode *inode = d_inode(dentry);
1771 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1772 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1773 struct tcon_link *tlink;
1774 struct cifs_tcon *tcon;
1775 __u32 dosattr, origattr;
1776 FILE_BASIC_INFO *info_buf = NULL;
1778 tlink = cifs_sb_tlink(cifs_sb);
1780 return PTR_ERR(tlink);
1781 tcon = tlink_tcon(tlink);
1784 * We cannot rename the file if the server doesn't support
1785 * CAP_INFOLEVEL_PASSTHRU
1787 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1792 oparms = (struct cifs_open_parms) {
1795 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1796 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1797 .disposition = FILE_OPEN,
1802 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1806 origattr = cifsInode->cifsAttrs;
1808 origattr |= ATTR_NORMAL;
1810 dosattr = origattr & ~ATTR_READONLY;
1812 dosattr |= ATTR_NORMAL;
1813 dosattr |= ATTR_HIDDEN;
1815 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1816 if (dosattr != origattr) {
1817 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1818 if (info_buf == NULL) {
1822 info_buf->Attributes = cpu_to_le32(dosattr);
1823 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1825 /* although we would like to mark the file hidden
1826 if that fails we will still try to rename it */
1828 cifsInode->cifsAttrs = dosattr;
1830 dosattr = origattr; /* since not able to change them */
1833 /* rename the file */
1834 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1836 cifs_remap(cifs_sb));
1842 /* try to set DELETE_ON_CLOSE */
1843 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1844 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1847 * some samba versions return -ENOENT when we try to set the
1848 * file disposition here. Likely a samba bug, but work around
1849 * it for now. This means that some cifsXXX files may hang
1850 * around after they shouldn't.
1852 * BB: remove this hack after more servers have the fix
1860 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1864 CIFSSMBClose(xid, tcon, fid.netfid);
1867 cifs_put_tlink(tlink);
1871 * reset everything back to the original state. Don't bother
1872 * dealing with errors here since we can't do anything about
1876 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1877 cifs_sb->local_nls, cifs_remap(cifs_sb));
1879 if (dosattr != origattr) {
1880 info_buf->Attributes = cpu_to_le32(origattr);
1881 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1883 cifsInode->cifsAttrs = origattr;
1888 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1890 /* copied from fs/nfs/dir.c with small changes */
1892 cifs_drop_nlink(struct inode *inode)
1894 spin_lock(&inode->i_lock);
1895 if (inode->i_nlink > 0)
1897 spin_unlock(&inode->i_lock);
1901 * If d_inode(dentry) is null (usually meaning the cached dentry
1902 * is a negative dentry) then we would attempt a standard SMB delete, but
1903 * if that fails we can not attempt the fall back mechanisms on EACCES
1904 * but will return the EACCES to the caller. Note that the VFS does not call
1905 * unlink on negative dentries currently.
1907 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1911 const char *full_path;
1913 struct inode *inode = d_inode(dentry);
1914 struct cifsInodeInfo *cifs_inode;
1915 struct super_block *sb = dir->i_sb;
1916 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1917 struct tcon_link *tlink;
1918 struct cifs_tcon *tcon;
1919 struct TCP_Server_Info *server;
1920 struct iattr *attrs = NULL;
1921 __u32 dosattr = 0, origattr = 0;
1923 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1925 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1928 tlink = cifs_sb_tlink(cifs_sb);
1930 return PTR_ERR(tlink);
1931 tcon = tlink_tcon(tlink);
1932 server = tcon->ses->server;
1935 page = alloc_dentry_path();
1937 if (tcon->nodelete) {
1942 /* Unlink can be called from rename so we can not take the
1943 * sb->s_vfs_rename_mutex here */
1944 full_path = build_path_from_dentry(dentry, page);
1945 if (IS_ERR(full_path)) {
1946 rc = PTR_ERR(full_path);
1950 netfs_wait_for_outstanding_io(inode);
1951 cifs_close_deferred_file_under_dentry(tcon, full_path);
1952 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1953 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1954 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1955 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1956 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1957 cifs_remap(cifs_sb));
1958 cifs_dbg(FYI, "posix del rc %d\n", rc);
1959 if ((rc == 0) || (rc == -ENOENT))
1960 goto psx_del_no_retry;
1962 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1965 if (!server->ops->unlink) {
1967 goto psx_del_no_retry;
1970 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb, dentry);
1975 cifs_mark_open_handles_for_deleted_file(inode, full_path);
1976 cifs_drop_nlink(inode);
1978 } else if (rc == -ENOENT) {
1980 } else if (rc == -EBUSY) {
1981 if (server->ops->rename_pending_delete) {
1982 rc = server->ops->rename_pending_delete(full_path,
1985 cifs_mark_open_handles_for_deleted_file(inode, full_path);
1986 cifs_drop_nlink(inode);
1989 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1990 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1991 if (attrs == NULL) {
1996 /* try to reset dos attributes */
1997 cifs_inode = CIFS_I(inode);
1998 origattr = cifs_inode->cifsAttrs;
2000 origattr |= ATTR_NORMAL;
2001 dosattr = origattr & ~ATTR_READONLY;
2003 dosattr |= ATTR_NORMAL;
2004 dosattr |= ATTR_HIDDEN;
2006 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2010 goto retry_std_delete;
2013 /* undo the setattr if we errored out and it's needed */
2014 if (rc != 0 && dosattr != 0)
2015 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
2019 cifs_inode = CIFS_I(inode);
2020 cifs_inode->time = 0; /* will force revalidate to get info
2022 inode_set_ctime_current(inode);
2024 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
2025 cifs_inode = CIFS_I(dir);
2026 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
2028 free_dentry_path(page);
2031 cifs_put_tlink(tlink);
2036 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
2037 const char *full_path, struct cifs_sb_info *cifs_sb,
2038 struct cifs_tcon *tcon, const unsigned int xid)
2041 struct inode *inode = NULL;
2043 if (tcon->posix_extensions) {
2044 rc = smb311_posix_get_inode_info(&inode, full_path,
2045 NULL, parent->i_sb, xid);
2046 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2047 } else if (tcon->unix_ext) {
2048 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
2050 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2052 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
2059 if (!S_ISDIR(inode->i_mode)) {
2061 * mkdir succeeded, but another client has managed to remove the
2062 * sucker and replace it with non-directory. Return success,
2063 * but don't leave the child in dcache.
2070 * setting nlink not necessary except in cases where we failed to get it
2071 * from the server or was set bogus. Also, since this is a brand new
2072 * inode, no need to grab the i_lock before setting the i_nlink.
2074 if (inode->i_nlink < 2)
2075 set_nlink(inode, 2);
2076 mode &= ~current_umask();
2077 /* must turn on setgid bit if parent dir has it */
2078 if (parent->i_mode & S_ISGID)
2081 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2082 if (tcon->unix_ext) {
2083 struct cifs_unix_set_info_args args = {
2085 .ctime = NO_CHANGE_64,
2086 .atime = NO_CHANGE_64,
2087 .mtime = NO_CHANGE_64,
2090 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
2091 args.uid = current_fsuid();
2092 if (parent->i_mode & S_ISGID)
2093 args.gid = parent->i_gid;
2095 args.gid = current_fsgid();
2097 args.uid = INVALID_UID; /* no change */
2098 args.gid = INVALID_GID; /* no change */
2100 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
2102 cifs_remap(cifs_sb));
2106 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2107 struct TCP_Server_Info *server = tcon->ses->server;
2108 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2109 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
2110 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
2112 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
2113 inode->i_mode = (mode | S_IFDIR);
2115 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
2116 inode->i_uid = current_fsuid();
2117 if (inode->i_mode & S_ISGID)
2118 inode->i_gid = parent->i_gid;
2120 inode->i_gid = current_fsgid();
2123 d_instantiate(dentry, inode);
2127 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2129 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
2130 const char *full_path, struct cifs_sb_info *cifs_sb,
2131 struct cifs_tcon *tcon, const unsigned int xid)
2135 FILE_UNIX_BASIC_INFO *info = NULL;
2136 struct inode *newinode = NULL;
2137 struct cifs_fattr fattr;
2139 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
2142 goto posix_mkdir_out;
2145 mode &= ~current_umask();
2146 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
2147 NULL /* netfid */, info, &oplock, full_path,
2148 cifs_sb->local_nls, cifs_remap(cifs_sb));
2149 if (rc == -EOPNOTSUPP)
2150 goto posix_mkdir_out;
2152 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
2154 goto posix_mkdir_out;
2157 if (info->Type == cpu_to_le32(-1))
2158 /* no return info, go query for it */
2159 goto posix_mkdir_get_info;
2161 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2162 * need to set uid/gid.
2165 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2166 cifs_fill_uniqueid(inode->i_sb, &fattr);
2167 newinode = cifs_iget(inode->i_sb, &fattr);
2169 goto posix_mkdir_get_info;
2171 d_instantiate(dentry, newinode);
2173 #ifdef CONFIG_CIFS_DEBUG2
2174 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2175 dentry, dentry, newinode);
2177 if (newinode->i_nlink != 2)
2178 cifs_dbg(FYI, "unexpected number of links %d\n",
2185 posix_mkdir_get_info:
2186 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2188 goto posix_mkdir_out;
2190 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2192 int cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2193 struct dentry *direntry, umode_t mode)
2197 struct cifs_sb_info *cifs_sb;
2198 struct tcon_link *tlink;
2199 struct cifs_tcon *tcon;
2200 struct TCP_Server_Info *server;
2201 const char *full_path;
2204 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2207 cifs_sb = CIFS_SB(inode->i_sb);
2208 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2210 tlink = cifs_sb_tlink(cifs_sb);
2212 return PTR_ERR(tlink);
2213 tcon = tlink_tcon(tlink);
2217 page = alloc_dentry_path();
2218 full_path = build_path_from_dentry(direntry, page);
2219 if (IS_ERR(full_path)) {
2220 rc = PTR_ERR(full_path);
2224 server = tcon->ses->server;
2226 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2227 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2229 d_drop(direntry); /* for time being always refresh inode info */
2233 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2234 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2235 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2236 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2238 if (rc != -EOPNOTSUPP)
2241 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2243 if (!server->ops->mkdir) {
2248 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2249 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2251 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2256 /* TODO: skip this for smb2/smb3 */
2257 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2261 * Force revalidate to get parent dir info when needed since cached
2262 * attributes are invalid now.
2264 CIFS_I(inode)->time = 0;
2265 free_dentry_path(page);
2267 cifs_put_tlink(tlink);
2271 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2275 struct cifs_sb_info *cifs_sb;
2276 struct tcon_link *tlink;
2277 struct cifs_tcon *tcon;
2278 struct TCP_Server_Info *server;
2279 const char *full_path;
2280 void *page = alloc_dentry_path();
2281 struct cifsInodeInfo *cifsInode;
2283 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2287 full_path = build_path_from_dentry(direntry, page);
2288 if (IS_ERR(full_path)) {
2289 rc = PTR_ERR(full_path);
2293 cifs_sb = CIFS_SB(inode->i_sb);
2294 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2299 tlink = cifs_sb_tlink(cifs_sb);
2300 if (IS_ERR(tlink)) {
2301 rc = PTR_ERR(tlink);
2304 tcon = tlink_tcon(tlink);
2305 server = tcon->ses->server;
2307 if (!server->ops->rmdir) {
2309 cifs_put_tlink(tlink);
2313 if (tcon->nodelete) {
2315 cifs_put_tlink(tlink);
2319 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2320 cifs_put_tlink(tlink);
2323 spin_lock(&d_inode(direntry)->i_lock);
2324 i_size_write(d_inode(direntry), 0);
2325 clear_nlink(d_inode(direntry));
2326 spin_unlock(&d_inode(direntry)->i_lock);
2329 cifsInode = CIFS_I(d_inode(direntry));
2330 /* force revalidate to go get info when needed */
2331 cifsInode->time = 0;
2333 cifsInode = CIFS_I(inode);
2335 * Force revalidate to get parent dir info when needed since cached
2336 * attributes are invalid now.
2338 cifsInode->time = 0;
2340 inode_set_ctime_current(d_inode(direntry));
2341 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2344 free_dentry_path(page);
2350 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2351 const char *from_path, struct dentry *to_dentry,
2352 const char *to_path)
2354 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2355 struct tcon_link *tlink;
2356 struct cifs_tcon *tcon;
2357 struct TCP_Server_Info *server;
2358 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2359 struct cifs_fid fid;
2360 struct cifs_open_parms oparms;
2362 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2365 tlink = cifs_sb_tlink(cifs_sb);
2367 return PTR_ERR(tlink);
2368 tcon = tlink_tcon(tlink);
2369 server = tcon->ses->server;
2371 if (!server->ops->rename)
2374 /* try path-based rename first */
2375 rc = server->ops->rename(xid, tcon, from_dentry,
2376 from_path, to_path, cifs_sb);
2379 * Don't bother with rename by filehandle unless file is busy and
2380 * source. Note that cross directory moves do not work with
2381 * rename by filehandle to various Windows servers.
2383 if (rc == 0 || rc != -EBUSY)
2384 goto do_rename_exit;
2386 /* Don't fall back to using SMB on SMB 2+ mount */
2387 if (server->vals->protocol_id != 0)
2388 goto do_rename_exit;
2390 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2391 /* open-file renames don't work across directories */
2392 if (to_dentry->d_parent != from_dentry->d_parent)
2393 goto do_rename_exit;
2395 oparms = (struct cifs_open_parms) {
2398 /* open the file to be renamed -- we need DELETE perms */
2399 .desired_access = DELETE,
2400 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2401 .disposition = FILE_OPEN,
2406 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2408 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2409 (const char *) to_dentry->d_name.name,
2410 cifs_sb->local_nls, cifs_remap(cifs_sb));
2411 CIFSSMBClose(xid, tcon, fid.netfid);
2413 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2416 d_move(from_dentry, to_dentry);
2417 cifs_put_tlink(tlink);
2422 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2423 struct dentry *source_dentry, struct inode *target_dir,
2424 struct dentry *target_dentry, unsigned int flags)
2426 const char *from_name, *to_name;
2427 void *page1, *page2;
2428 struct cifs_sb_info *cifs_sb;
2429 struct tcon_link *tlink;
2430 struct cifs_tcon *tcon;
2433 int retry_count = 0;
2434 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2435 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2436 FILE_UNIX_BASIC_INFO *info_buf_target;
2437 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2439 if (flags & ~RENAME_NOREPLACE)
2442 cifs_sb = CIFS_SB(source_dir->i_sb);
2443 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2446 tlink = cifs_sb_tlink(cifs_sb);
2448 return PTR_ERR(tlink);
2449 tcon = tlink_tcon(tlink);
2451 page1 = alloc_dentry_path();
2452 page2 = alloc_dentry_path();
2455 from_name = build_path_from_dentry(source_dentry, page1);
2456 if (IS_ERR(from_name)) {
2457 rc = PTR_ERR(from_name);
2458 goto cifs_rename_exit;
2461 to_name = build_path_from_dentry(target_dentry, page2);
2462 if (IS_ERR(to_name)) {
2463 rc = PTR_ERR(to_name);
2464 goto cifs_rename_exit;
2467 cifs_close_deferred_file_under_dentry(tcon, from_name);
2468 if (d_inode(target_dentry) != NULL) {
2469 netfs_wait_for_outstanding_io(d_inode(target_dentry));
2470 cifs_close_deferred_file_under_dentry(tcon, to_name);
2473 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2476 if (rc == -EACCES) {
2477 while (retry_count < 3) {
2478 cifs_close_all_deferred_files(tcon);
2479 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2488 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2490 if (flags & RENAME_NOREPLACE)
2491 goto cifs_rename_exit;
2493 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2494 if (rc == -EEXIST && tcon->unix_ext) {
2496 * Are src and dst hardlinks of same inode? We can only tell
2497 * with unix extensions enabled.
2500 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2502 if (info_buf_source == NULL) {
2504 goto cifs_rename_exit;
2507 info_buf_target = info_buf_source + 1;
2508 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2511 cifs_remap(cifs_sb));
2515 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2518 cifs_remap(cifs_sb));
2520 if (tmprc == 0 && (info_buf_source->UniqueId ==
2521 info_buf_target->UniqueId)) {
2522 /* same file, POSIX says that this is a noop */
2524 goto cifs_rename_exit;
2528 * else ... BB we could add the same check for Windows by
2529 * checking the UniqueId via FILE_INTERNAL_INFO
2533 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2535 /* Try unlinking the target dentry if it's not negative */
2536 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2537 if (d_is_dir(target_dentry))
2538 tmprc = cifs_rmdir(target_dir, target_dentry);
2540 tmprc = cifs_unlink(target_dir, target_dentry);
2542 goto cifs_rename_exit;
2543 rc = cifs_do_rename(xid, source_dentry, from_name,
2544 target_dentry, to_name);
2547 /* force revalidate to go get info when needed */
2548 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2551 kfree(info_buf_source);
2552 free_dentry_path(page2);
2553 free_dentry_path(page1);
2555 cifs_put_tlink(tlink);
2560 cifs_dentry_needs_reval(struct dentry *dentry)
2562 struct inode *inode = d_inode(dentry);
2563 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2564 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2565 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2566 struct cached_fid *cfid = NULL;
2568 if (cifs_i->time == 0)
2571 if (CIFS_CACHE_READ(cifs_i))
2574 if (!lookupCacheEnabled)
2577 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2578 if (cfid->time && cifs_i->time > cfid->time) {
2579 close_cached_dir(cfid);
2582 close_cached_dir(cfid);
2585 * depending on inode type, check if attribute caching disabled for
2586 * files or directories
2588 if (S_ISDIR(inode->i_mode)) {
2589 if (!cifs_sb->ctx->acdirmax)
2591 if (!time_in_range(jiffies, cifs_i->time,
2592 cifs_i->time + cifs_sb->ctx->acdirmax))
2595 if (!cifs_sb->ctx->acregmax)
2597 if (!time_in_range(jiffies, cifs_i->time,
2598 cifs_i->time + cifs_sb->ctx->acregmax))
2602 /* hardlinked files w/ noserverino get "special" treatment */
2603 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2604 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2611 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2613 * @key: currently unused
2614 * @mode: the task state to sleep in
2617 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2620 if (signal_pending_state(mode, current))
2621 return -ERESTARTSYS;
2626 cifs_revalidate_mapping(struct inode *inode)
2629 struct cifsInodeInfo *cifs_inode = CIFS_I(inode);
2630 unsigned long *flags = &cifs_inode->flags;
2631 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2633 /* swapfiles are not supposed to be shared */
2634 if (IS_SWAPFILE(inode))
2637 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2638 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2642 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2643 /* for cache=singleclient, do not invalidate */
2644 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2645 goto skip_invalidate;
2647 cifs_inode->netfs.zero_point = cifs_inode->netfs.remote_i_size;
2648 rc = filemap_invalidate_inode(inode, true, 0, LLONG_MAX);
2650 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2651 __func__, inode, rc);
2652 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2657 clear_bit_unlock(CIFS_INO_LOCK, flags);
2658 smp_mb__after_atomic();
2659 wake_up_bit(flags, CIFS_INO_LOCK);
2665 cifs_zap_mapping(struct inode *inode)
2667 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2668 return cifs_revalidate_mapping(inode);
2671 int cifs_revalidate_file_attr(struct file *filp)
2674 struct dentry *dentry = file_dentry(filp);
2675 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2676 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2677 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2679 if (!cifs_dentry_needs_reval(dentry))
2682 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2683 if (tlink_tcon(cfile->tlink)->unix_ext)
2684 rc = cifs_get_file_info_unix(filp);
2686 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2687 rc = cifs_get_file_info(filp);
2692 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2696 struct inode *inode = d_inode(dentry);
2697 struct super_block *sb = dentry->d_sb;
2698 const char *full_path;
2705 if (!cifs_dentry_needs_reval(dentry))
2710 page = alloc_dentry_path();
2711 full_path = build_path_from_dentry(dentry, page);
2712 if (IS_ERR(full_path)) {
2713 rc = PTR_ERR(full_path);
2717 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2718 full_path, inode, inode->i_count.counter,
2719 dentry, cifs_get_time(dentry), jiffies);
2722 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2723 rc = smb311_posix_get_inode_info(&inode, full_path,
2725 } else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2726 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2728 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2731 if (rc == -EAGAIN && count++ < 10)
2734 free_dentry_path(page);
2740 int cifs_revalidate_file(struct file *filp)
2743 struct inode *inode = file_inode(filp);
2745 rc = cifs_revalidate_file_attr(filp);
2749 return cifs_revalidate_mapping(inode);
2752 /* revalidate a dentry's inode attributes */
2753 int cifs_revalidate_dentry(struct dentry *dentry)
2756 struct inode *inode = d_inode(dentry);
2758 rc = cifs_revalidate_dentry_attr(dentry);
2762 return cifs_revalidate_mapping(inode);
2765 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2766 struct kstat *stat, u32 request_mask, unsigned int flags)
2768 struct dentry *dentry = path->dentry;
2769 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2770 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2771 struct inode *inode = d_inode(dentry);
2774 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2778 * We need to be sure that all dirty pages are written and the server
2779 * has actual ctime, mtime and file length.
2781 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2782 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2783 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2784 rc = filemap_fdatawait(inode->i_mapping);
2786 mapping_set_error(inode->i_mapping, rc);
2791 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2792 CIFS_I(inode)->time = 0; /* force revalidate */
2795 * If the caller doesn't require syncing, only sync if
2796 * necessary (e.g. due to earlier truncate or setattr
2797 * invalidating the cached metadata)
2799 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2800 (CIFS_I(inode)->time == 0)) {
2801 rc = cifs_revalidate_dentry_attr(dentry);
2806 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2807 stat->blksize = cifs_sb->ctx->bsize;
2808 stat->ino = CIFS_I(inode)->uniqueid;
2810 /* old CIFS Unix Extensions doesn't return create time */
2811 if (CIFS_I(inode)->createtime) {
2812 stat->result_mask |= STATX_BTIME;
2814 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2817 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2818 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2819 stat->attributes |= STATX_ATTR_COMPRESSED;
2820 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2821 stat->attributes |= STATX_ATTR_ENCRYPTED;
2824 * If on a multiuser mount without unix extensions or cifsacl being
2825 * enabled, and the admin hasn't overridden them, set the ownership
2826 * to the fsuid/fsgid of the current process.
2828 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2829 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2831 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2832 stat->uid = current_fsuid();
2833 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2834 stat->gid = current_fsgid();
2839 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2842 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2843 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2844 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2845 struct TCP_Server_Info *server = tcon->ses->server;
2846 struct cifsFileInfo *cfile;
2849 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2853 * We need to be sure that all dirty pages are written as they
2854 * might fill holes on the server.
2856 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2857 inode->i_mapping->nrpages != 0) {
2858 rc = filemap_fdatawait(inode->i_mapping);
2860 mapping_set_error(inode->i_mapping, rc);
2865 cfile = find_readable_file(cifs_i, false);
2869 if (server->ops->fiemap) {
2870 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2871 cifsFileInfo_put(cfile);
2875 cifsFileInfo_put(cfile);
2879 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2881 pgoff_t index = from >> PAGE_SHIFT;
2882 unsigned offset = from & (PAGE_SIZE - 1);
2886 page = grab_cache_page(mapping, index);
2890 zero_user_segment(page, offset, PAGE_SIZE);
2896 void cifs_setsize(struct inode *inode, loff_t offset)
2898 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2900 spin_lock(&inode->i_lock);
2901 i_size_write(inode, offset);
2902 spin_unlock(&inode->i_lock);
2904 /* Cached inode must be refreshed on truncate */
2906 truncate_pagecache(inode, offset);
2910 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2911 unsigned int xid, const char *full_path, struct dentry *dentry)
2914 struct cifsFileInfo *open_file;
2915 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2916 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2917 struct tcon_link *tlink = NULL;
2918 struct cifs_tcon *tcon = NULL;
2919 struct TCP_Server_Info *server;
2922 * To avoid spurious oplock breaks from server, in the case of
2923 * inodes that we already have open, avoid doing path based
2924 * setting of file size if we can do it by handle.
2925 * This keeps our caching token (oplock) and avoids timeouts
2926 * when the local oplock break takes longer to flush
2927 * writebehind data than the SMB timeout for the SetPathInfo
2928 * request would allow
2930 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2932 tcon = tlink_tcon(open_file->tlink);
2933 server = tcon->ses->server;
2934 if (server->ops->set_file_size)
2935 rc = server->ops->set_file_size(xid, tcon, open_file,
2936 attrs->ia_size, false);
2939 cifsFileInfo_put(open_file);
2940 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2948 tlink = cifs_sb_tlink(cifs_sb);
2950 return PTR_ERR(tlink);
2951 tcon = tlink_tcon(tlink);
2952 server = tcon->ses->server;
2956 * Set file size by pathname rather than by handle either because no
2957 * valid, writeable file handle for it was found or because there was
2958 * an error setting it by handle.
2960 if (server->ops->set_path_size)
2961 rc = server->ops->set_path_size(xid, tcon, full_path,
2962 attrs->ia_size, cifs_sb, false, dentry);
2965 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2968 cifs_put_tlink(tlink);
2972 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
2973 cifs_setsize(inode, attrs->ia_size);
2975 * i_blocks is not related to (i_size / i_blksize), but instead
2976 * 512 byte (2**9) size is required for calculating num blocks.
2977 * Until we can query the server for actual allocation size,
2978 * this is best estimate we have for blocks allocated for a file
2979 * Number of blocks must be rounded up so size 1 is not 0 blocks
2981 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2984 * The man page of truncate says if the size changed,
2985 * then the st_ctime and st_mtime fields for the file
2988 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2989 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2991 cifs_truncate_page(inode->i_mapping, inode->i_size);
2997 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2999 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
3003 const char *full_path;
3004 void *page = alloc_dentry_path();
3005 struct inode *inode = d_inode(direntry);
3006 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3007 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3008 struct tcon_link *tlink;
3009 struct cifs_tcon *pTcon;
3010 struct cifs_unix_set_info_args *args = NULL;
3011 struct cifsFileInfo *open_file;
3013 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
3014 direntry, attrs->ia_valid);
3018 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3019 attrs->ia_valid |= ATTR_FORCE;
3021 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3025 full_path = build_path_from_dentry(direntry, page);
3026 if (IS_ERR(full_path)) {
3027 rc = PTR_ERR(full_path);
3032 * Attempt to flush data before changing attributes. We need to do
3033 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
3034 * ownership or mode then we may also need to do this. Here, we take
3035 * the safe way out and just do the flush on all setattr requests. If
3036 * the flush returns error, store it to report later and continue.
3038 * BB: This should be smarter. Why bother flushing pages that
3039 * will be truncated anyway? Also, should we error out here if
3040 * the flush returns error?
3042 rc = filemap_write_and_wait(inode->i_mapping);
3043 if (is_interrupt_error(rc)) {
3048 mapping_set_error(inode->i_mapping, rc);
3051 if (attrs->ia_valid & ATTR_SIZE) {
3052 rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
3057 /* skip mode change if it's just for clearing setuid/setgid */
3058 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3059 attrs->ia_valid &= ~ATTR_MODE;
3061 args = kmalloc(sizeof(*args), GFP_KERNEL);
3067 /* set up the struct */
3068 if (attrs->ia_valid & ATTR_MODE)
3069 args->mode = attrs->ia_mode;
3071 args->mode = NO_CHANGE_64;
3073 if (attrs->ia_valid & ATTR_UID)
3074 args->uid = attrs->ia_uid;
3076 args->uid = INVALID_UID; /* no change */
3078 if (attrs->ia_valid & ATTR_GID)
3079 args->gid = attrs->ia_gid;
3081 args->gid = INVALID_GID; /* no change */
3083 if (attrs->ia_valid & ATTR_ATIME)
3084 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
3086 args->atime = NO_CHANGE_64;
3088 if (attrs->ia_valid & ATTR_MTIME)
3089 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
3091 args->mtime = NO_CHANGE_64;
3093 if (attrs->ia_valid & ATTR_CTIME)
3094 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
3096 args->ctime = NO_CHANGE_64;
3099 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
3101 u16 nfid = open_file->fid.netfid;
3102 u32 npid = open_file->pid;
3103 pTcon = tlink_tcon(open_file->tlink);
3104 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
3105 cifsFileInfo_put(open_file);
3107 tlink = cifs_sb_tlink(cifs_sb);
3108 if (IS_ERR(tlink)) {
3109 rc = PTR_ERR(tlink);
3112 pTcon = tlink_tcon(tlink);
3113 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
3115 cifs_remap(cifs_sb));
3116 cifs_put_tlink(tlink);
3122 if ((attrs->ia_valid & ATTR_SIZE) &&
3123 attrs->ia_size != i_size_read(inode)) {
3124 truncate_setsize(inode, attrs->ia_size);
3125 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3126 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3129 setattr_copy(&nop_mnt_idmap, inode, attrs);
3130 mark_inode_dirty(inode);
3132 /* force revalidate when any of these times are set since some
3133 of the fs types (eg ext3, fat) do not have fine enough
3134 time granularity to match protocol, and we do not have a
3135 a way (yet) to query the server fs's time granularity (and
3136 whether it rounds times down).
3138 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
3139 cifsInode->time = 0;
3142 free_dentry_path(page);
3146 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3149 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3152 kuid_t uid = INVALID_UID;
3153 kgid_t gid = INVALID_GID;
3154 struct inode *inode = d_inode(direntry);
3155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3156 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3157 struct cifsFileInfo *wfile;
3158 struct cifs_tcon *tcon;
3159 const char *full_path;
3160 void *page = alloc_dentry_path();
3163 __u64 mode = NO_CHANGE_64;
3164 bool posix = cifs_sb_master_tcon(cifs_sb)->posix_extensions;
3168 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3169 direntry, attrs->ia_valid);
3171 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3172 attrs->ia_valid |= ATTR_FORCE;
3174 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3176 goto cifs_setattr_exit;
3178 full_path = build_path_from_dentry(direntry, page);
3179 if (IS_ERR(full_path)) {
3180 rc = PTR_ERR(full_path);
3181 goto cifs_setattr_exit;
3185 * Attempt to flush data before changing attributes. We need to do
3186 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3187 * returns error, store it to report later and continue.
3189 * BB: This should be smarter. Why bother flushing pages that
3190 * will be truncated anyway? Also, should we error out here if
3191 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3193 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3194 rc = filemap_write_and_wait(inode->i_mapping);
3195 if (is_interrupt_error(rc)) {
3197 goto cifs_setattr_exit;
3199 mapping_set_error(inode->i_mapping, rc);
3204 if ((attrs->ia_valid & ATTR_MTIME) &&
3205 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3206 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
3208 tcon = tlink_tcon(wfile->tlink);
3209 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
3210 cifsFileInfo_put(wfile);
3212 goto cifs_setattr_exit;
3213 } else if (rc != -EBADF)
3214 goto cifs_setattr_exit;
3219 if (attrs->ia_valid & ATTR_SIZE) {
3220 rc = cifs_set_file_size(inode, attrs, xid, full_path, direntry);
3222 goto cifs_setattr_exit;
3225 if (attrs->ia_valid & ATTR_UID)
3226 uid = attrs->ia_uid;
3228 if (attrs->ia_valid & ATTR_GID)
3229 gid = attrs->ia_gid;
3231 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3232 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3233 if (uid_valid(uid) || gid_valid(gid)) {
3234 mode = NO_CHANGE_64;
3235 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3238 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3240 goto cifs_setattr_exit;
3244 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3245 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3247 /* skip mode change if it's just for clearing setuid/setgid */
3248 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3249 attrs->ia_valid &= ~ATTR_MODE;
3251 if (attrs->ia_valid & ATTR_MODE) {
3252 mode = attrs->ia_mode;
3254 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3255 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) ||
3257 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3258 INVALID_UID, INVALID_GID);
3260 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3262 goto cifs_setattr_exit;
3266 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3267 * Pick up the actual mode bits that were set.
3269 if (mode != attrs->ia_mode)
3270 attrs->ia_mode = mode;
3272 if (((mode & S_IWUGO) == 0) &&
3273 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3275 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3277 /* fix up mode if we're not using dynperm */
3278 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3279 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3280 } else if ((mode & S_IWUGO) &&
3281 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3283 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3284 /* Attributes of 0 are ignored */
3286 dosattr |= ATTR_NORMAL;
3288 /* reset local inode permissions to normal */
3289 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3290 attrs->ia_mode &= ~(S_IALLUGO);
3291 if (S_ISDIR(inode->i_mode))
3293 cifs_sb->ctx->dir_mode;
3296 cifs_sb->ctx->file_mode;
3298 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3299 /* ignore mode change - ATTR_READONLY hasn't changed */
3300 attrs->ia_valid &= ~ATTR_MODE;
3304 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3305 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3306 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3307 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3309 /* Even if error on time set, no sense failing the call if
3310 the server would set the time to a reasonable value anyway,
3311 and this check ensures that we are not being called from
3312 sys_utimes in which case we ought to fail the call back to
3313 the user when the server rejects the call */
3314 if ((rc) && (attrs->ia_valid &
3315 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3319 /* do not need local check to inode_check_ok since the server does
3322 goto cifs_setattr_exit;
3324 if ((attrs->ia_valid & ATTR_SIZE) &&
3325 attrs->ia_size != i_size_read(inode)) {
3326 truncate_setsize(inode, attrs->ia_size);
3327 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3328 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3331 setattr_copy(&nop_mnt_idmap, inode, attrs);
3332 mark_inode_dirty(inode);
3336 free_dentry_path(page);
3341 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3342 struct iattr *attrs)
3344 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3345 int rc, retries = 0;
3346 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3347 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3348 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3350 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3354 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3355 if (pTcon->unix_ext)
3356 rc = cifs_setattr_unix(direntry, attrs);
3358 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3359 rc = cifs_setattr_nounix(direntry, attrs);
3361 } while (is_retryable_error(rc) && retries < 2);
3363 /* BB: add cifs_setattr_legacy for really old servers */