4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/stat.h>
23 #include <linux/slab.h>
24 #include <linux/pagemap.h>
25 #include <linux/freezer.h>
26 #include <linux/sched/signal.h>
27 #include <linux/wait_bit.h>
28 #include <linux/fiemap.h>
30 #include <asm/div64.h>
34 #include "cifsproto.h"
35 #include "smb2proto.h"
36 #include "cifs_debug.h"
37 #include "cifs_fs_sb.h"
38 #include "cifs_unicode.h"
42 static void cifs_set_ops(struct inode *inode)
44 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
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 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
51 inode->i_fop = &cifs_file_direct_nobrl_ops;
53 inode->i_fop = &cifs_file_direct_ops;
54 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
55 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
56 inode->i_fop = &cifs_file_strict_nobrl_ops;
58 inode->i_fop = &cifs_file_strict_ops;
59 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
60 inode->i_fop = &cifs_file_nobrl_ops;
61 else { /* not direct, send byte range locks */
62 inode->i_fop = &cifs_file_ops;
65 /* check if server can support readpages */
66 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
67 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
68 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
70 inode->i_data.a_ops = &cifs_addr_ops;
73 #ifdef CONFIG_CIFS_DFS_UPCALL
74 if (IS_AUTOMOUNT(inode)) {
75 inode->i_op = &cifs_dfs_referral_inode_operations;
77 #else /* NO DFS support, treat as a directory */
80 inode->i_op = &cifs_dir_inode_ops;
81 inode->i_fop = &cifs_dir_ops;
85 inode->i_op = &cifs_symlink_inode_ops;
88 init_special_inode(inode, inode->i_mode, inode->i_rdev);
93 /* check inode attributes against fattr. If they don't match, tag the
94 * inode for cache invalidation
97 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
99 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
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 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
120 cifs_i->server_eof == fattr->cf_eof) {
121 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
122 __func__, cifs_i->uniqueid);
126 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
127 __func__, cifs_i->uniqueid);
128 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
132 * copy nlink to the inode, unless it wasn't provided. Provide
133 * sane values if we don't have an existing one and none was provided
136 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
139 * if we're in a situation where we can't trust what we
140 * got from the server (readdir, some non-unix cases)
141 * fake reasonable values
143 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
144 /* only provide fake values on a new inode */
145 if (inode->i_state & I_NEW) {
146 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
154 /* we trust the server, so update it */
155 set_nlink(inode, fattr->cf_nlink);
158 /* populate an inode with info from a cifs_fattr struct */
160 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
162 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
163 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
165 cifs_revalidate_cache(inode, fattr);
167 spin_lock(&inode->i_lock);
168 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
169 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
170 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
171 /* we do not want atime to be less than mtime, it broke some apps */
172 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
173 inode->i_atime = fattr->cf_mtime;
175 inode->i_atime = fattr->cf_atime;
176 inode->i_mtime = fattr->cf_mtime;
177 inode->i_ctime = fattr->cf_ctime;
178 inode->i_rdev = fattr->cf_rdev;
179 cifs_nlink_fattr_to_inode(inode, fattr);
180 inode->i_uid = fattr->cf_uid;
181 inode->i_gid = fattr->cf_gid;
183 /* if dynperm is set, don't clobber existing mode */
184 if (inode->i_state & I_NEW ||
185 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
186 inode->i_mode = fattr->cf_mode;
188 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
190 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
193 cifs_i->time = jiffies;
195 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
196 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
198 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
200 cifs_i->server_eof = fattr->cf_eof;
202 * Can't safely change the file size here if the client is writing to
203 * it due to potential races.
205 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
206 i_size_write(inode, fattr->cf_eof);
209 * i_blocks is not related to (i_size / i_blksize),
210 * but instead 512 byte (2**9) size is required for
211 * calculating num blocks.
213 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
215 spin_unlock(&inode->i_lock);
217 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
218 inode->i_flags |= S_AUTOMOUNT;
219 if (inode->i_state & I_NEW)
224 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
226 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
228 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
231 fattr->cf_uniqueid = iunique(sb, ROOT_I);
234 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
236 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
237 struct cifs_sb_info *cifs_sb)
239 memset(fattr, 0, sizeof(*fattr));
240 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
241 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
242 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
244 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
245 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
246 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
247 /* old POSIX extensions don't get create time */
249 fattr->cf_mode = le64_to_cpu(info->Permissions);
252 * Since we set the inode type below we need to mask off
253 * to avoid strange results if bits set above.
255 fattr->cf_mode &= ~S_IFMT;
256 switch (le32_to_cpu(info->Type)) {
258 fattr->cf_mode |= S_IFREG;
259 fattr->cf_dtype = DT_REG;
262 fattr->cf_mode |= S_IFLNK;
263 fattr->cf_dtype = DT_LNK;
266 fattr->cf_mode |= S_IFDIR;
267 fattr->cf_dtype = DT_DIR;
270 fattr->cf_mode |= S_IFCHR;
271 fattr->cf_dtype = DT_CHR;
272 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
273 le64_to_cpu(info->DevMinor) & MINORMASK);
276 fattr->cf_mode |= S_IFBLK;
277 fattr->cf_dtype = DT_BLK;
278 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
279 le64_to_cpu(info->DevMinor) & MINORMASK);
282 fattr->cf_mode |= S_IFIFO;
283 fattr->cf_dtype = DT_FIFO;
286 fattr->cf_mode |= S_IFSOCK;
287 fattr->cf_dtype = DT_SOCK;
290 /* safest to call it a file if we do not know */
291 fattr->cf_mode |= S_IFREG;
292 fattr->cf_dtype = DT_REG;
293 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
297 fattr->cf_uid = cifs_sb->mnt_uid;
298 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
299 u64 id = le64_to_cpu(info->Uid);
300 if (id < ((uid_t)-1)) {
301 kuid_t uid = make_kuid(&init_user_ns, id);
307 fattr->cf_gid = cifs_sb->mnt_gid;
308 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
309 u64 id = le64_to_cpu(info->Gid);
310 if (id < ((gid_t)-1)) {
311 kgid_t gid = make_kgid(&init_user_ns, id);
317 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
321 * Fill a cifs_fattr struct with fake inode info.
323 * Needed to setup cifs_fattr data for the directory which is the
324 * junction to the new submount (ie to setup the fake directory
325 * which represents a DFS referral).
328 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
330 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
332 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
334 memset(fattr, 0, sizeof(*fattr));
335 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
336 fattr->cf_uid = cifs_sb->mnt_uid;
337 fattr->cf_gid = cifs_sb->mnt_gid;
338 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
339 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
341 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
345 cifs_get_file_info_unix(struct file *filp)
349 FILE_UNIX_BASIC_INFO find_data;
350 struct cifs_fattr fattr;
351 struct inode *inode = file_inode(filp);
352 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
353 struct cifsFileInfo *cfile = filp->private_data;
354 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
357 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
359 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
360 } else if (rc == -EREMOTE) {
361 cifs_create_dfs_fattr(&fattr, inode->i_sb);
365 cifs_fattr_to_inode(inode, &fattr);
370 int cifs_get_inode_info_unix(struct inode **pinode,
371 const unsigned char *full_path,
372 struct super_block *sb, unsigned int xid)
375 FILE_UNIX_BASIC_INFO find_data;
376 struct cifs_fattr fattr;
377 struct cifs_tcon *tcon;
378 struct tcon_link *tlink;
379 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
381 cifs_dbg(FYI, "Getting info on %s\n", full_path);
383 tlink = cifs_sb_tlink(cifs_sb);
385 return PTR_ERR(tlink);
386 tcon = tlink_tcon(tlink);
388 /* could have done a find first instead but this returns more info */
389 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
390 cifs_sb->local_nls, cifs_remap(cifs_sb));
391 cifs_put_tlink(tlink);
394 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
395 } else if (rc == -EREMOTE) {
396 cifs_create_dfs_fattr(&fattr, sb);
402 /* check for Minshall+French symlinks */
403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
404 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
407 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
410 if (*pinode == NULL) {
412 cifs_fill_uniqueid(sb, &fattr);
413 *pinode = cifs_iget(sb, &fattr);
417 /* we already have inode, update it */
419 /* if uniqueid is different, return error */
420 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
421 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
422 CIFS_I(*pinode)->time = 0; /* force reval */
427 /* if filetype is different, return error */
428 if (unlikely(((*pinode)->i_mode & S_IFMT) !=
429 (fattr.cf_mode & S_IFMT))) {
430 CIFS_I(*pinode)->time = 0; /* force reval */
435 cifs_fattr_to_inode(*pinode, &fattr);
443 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
444 struct cifs_sb_info *cifs_sb, unsigned int xid)
448 struct tcon_link *tlink;
449 struct cifs_tcon *tcon;
451 struct cifs_open_parms oparms;
452 struct cifs_io_parms io_parms = {0};
454 unsigned int bytes_read;
456 int buf_type = CIFS_NO_BUFFER;
460 fattr->cf_mode &= ~S_IFMT;
462 if (fattr->cf_eof == 0) {
463 fattr->cf_mode |= S_IFIFO;
464 fattr->cf_dtype = DT_FIFO;
466 } else if (fattr->cf_eof < 8) {
467 fattr->cf_mode |= S_IFREG;
468 fattr->cf_dtype = DT_REG;
469 return -EINVAL; /* EOPNOTSUPP? */
472 tlink = cifs_sb_tlink(cifs_sb);
474 return PTR_ERR(tlink);
475 tcon = tlink_tcon(tlink);
478 oparms.cifs_sb = cifs_sb;
479 oparms.desired_access = GENERIC_READ;
480 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
481 oparms.disposition = FILE_OPEN;
484 oparms.reconnect = false;
486 if (tcon->ses->server->oplocks)
490 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
492 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
493 cifs_put_tlink(tlink);
498 io_parms.netfid = fid.netfid;
499 io_parms.pid = current->tgid;
500 io_parms.tcon = tcon;
502 io_parms.length = 24;
504 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
505 &bytes_read, &pbuf, &buf_type);
506 if ((rc == 0) && (bytes_read >= 8)) {
507 if (memcmp("IntxBLK", pbuf, 8) == 0) {
508 cifs_dbg(FYI, "Block device\n");
509 fattr->cf_mode |= S_IFBLK;
510 fattr->cf_dtype = DT_BLK;
511 if (bytes_read == 24) {
512 /* we have enough to decode dev num */
513 __u64 mjr; /* major */
514 __u64 mnr; /* minor */
515 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
516 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
517 fattr->cf_rdev = MKDEV(mjr, mnr);
519 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
520 cifs_dbg(FYI, "Char device\n");
521 fattr->cf_mode |= S_IFCHR;
522 fattr->cf_dtype = DT_CHR;
523 if (bytes_read == 24) {
524 /* we have enough to decode dev num */
525 __u64 mjr; /* major */
526 __u64 mnr; /* minor */
527 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
528 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
529 fattr->cf_rdev = MKDEV(mjr, mnr);
531 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
532 cifs_dbg(FYI, "Symlink\n");
533 fattr->cf_mode |= S_IFLNK;
534 fattr->cf_dtype = DT_LNK;
536 fattr->cf_mode |= S_IFREG; /* file? */
537 fattr->cf_dtype = DT_REG;
541 fattr->cf_mode |= S_IFREG; /* then it is a file */
542 fattr->cf_dtype = DT_REG;
543 rc = -EOPNOTSUPP; /* or some unknown SFU type */
546 tcon->ses->server->ops->close(xid, tcon, &fid);
547 cifs_put_tlink(tlink);
551 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
554 * Fetch mode bits as provided by SFU.
556 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
558 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
559 struct cifs_sb_info *cifs_sb, unsigned int xid)
561 #ifdef CONFIG_CIFS_XATTR
565 struct tcon_link *tlink;
566 struct cifs_tcon *tcon;
568 tlink = cifs_sb_tlink(cifs_sb);
570 return PTR_ERR(tlink);
571 tcon = tlink_tcon(tlink);
573 if (tcon->ses->server->ops->query_all_EAs == NULL) {
574 cifs_put_tlink(tlink);
578 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
579 "SETFILEBITS", ea_value, 4 /* size of buf */,
581 cifs_put_tlink(tlink);
585 mode = le32_to_cpu(*((__le32 *)ea_value));
586 fattr->cf_mode &= ~SFBITS_MASK;
587 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
588 mode, fattr->cf_mode);
589 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
590 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
599 /* Fill a cifs_fattr struct with info from POSIX info struct */
601 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
602 struct super_block *sb, bool adjust_tz, bool symlink)
604 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
605 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
607 memset(fattr, 0, sizeof(*fattr));
609 /* no fattr->flags to set */
610 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
611 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
613 if (info->LastAccessTime)
614 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
616 ktime_get_coarse_real_ts64(&fattr->cf_atime);
618 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
619 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
622 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
623 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
626 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
627 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
628 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
630 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
631 fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
632 /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
633 /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
636 fattr->cf_mode |= S_IFLNK;
637 fattr->cf_dtype = DT_LNK;
638 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
639 fattr->cf_mode |= S_IFDIR;
640 fattr->cf_dtype = DT_DIR;
642 fattr->cf_mode |= S_IFREG;
643 fattr->cf_dtype = DT_REG;
645 /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
647 fattr->cf_uid = cifs_sb->mnt_uid; /* TODO: map uid and gid from SID */
648 fattr->cf_gid = cifs_sb->mnt_gid;
650 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
651 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
655 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
657 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
658 struct super_block *sb, bool adjust_tz,
659 bool symlink, u32 reparse_tag)
661 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
662 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
664 memset(fattr, 0, sizeof(*fattr));
665 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
666 if (info->DeletePending)
667 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
669 if (info->LastAccessTime)
670 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
672 ktime_get_coarse_real_ts64(&fattr->cf_atime);
674 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
675 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
678 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
679 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
682 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
683 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
684 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
686 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
687 if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
688 fattr->cf_mode |= S_IFLNK | cifs_sb->mnt_file_mode;
689 fattr->cf_dtype = DT_LNK;
690 } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
691 fattr->cf_mode |= S_IFIFO | cifs_sb->mnt_file_mode;
692 fattr->cf_dtype = DT_FIFO;
693 } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
694 fattr->cf_mode |= S_IFSOCK | cifs_sb->mnt_file_mode;
695 fattr->cf_dtype = DT_SOCK;
696 } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
697 fattr->cf_mode |= S_IFCHR | cifs_sb->mnt_file_mode;
698 fattr->cf_dtype = DT_CHR;
699 } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
700 fattr->cf_mode |= S_IFBLK | cifs_sb->mnt_file_mode;
701 fattr->cf_dtype = DT_BLK;
702 } else if (symlink) { /* TODO add more reparse tag checks */
703 fattr->cf_mode = S_IFLNK;
704 fattr->cf_dtype = DT_LNK;
705 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
706 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
707 fattr->cf_dtype = DT_DIR;
709 * Server can return wrong NumberOfLinks value for directories
710 * when Unix extensions are disabled - fake it.
713 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
715 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
716 fattr->cf_dtype = DT_REG;
718 /* clear write bits if ATTR_READONLY is set */
719 if (fattr->cf_cifsattrs & ATTR_READONLY)
720 fattr->cf_mode &= ~(S_IWUGO);
723 * Don't accept zero nlink from non-unix servers unless
724 * delete is pending. Instead mark it as unknown.
726 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
727 !info->DeletePending) {
728 cifs_dbg(VFS, "bogus file nlink value %u\n",
730 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
734 fattr->cf_uid = cifs_sb->mnt_uid;
735 fattr->cf_gid = cifs_sb->mnt_gid;
739 cifs_get_file_info(struct file *filp)
743 FILE_ALL_INFO find_data;
744 struct cifs_fattr fattr;
745 struct inode *inode = file_inode(filp);
746 struct cifsFileInfo *cfile = filp->private_data;
747 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
748 struct TCP_Server_Info *server = tcon->ses->server;
750 if (!server->ops->query_file_info)
754 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
757 /* TODO: add support to query reparse tag */
758 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
759 false, 0 /* no reparse tag */);
762 cifs_create_dfs_fattr(&fattr, inode->i_sb);
768 * FIXME: legacy server -- fall back to path-based call?
769 * for now, just skip revalidating and mark inode for
773 CIFS_I(inode)->time = 0;
780 * don't bother with SFU junk here -- just mark inode as needing
783 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
784 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
785 cifs_fattr_to_inode(inode, &fattr);
791 /* Simple function to return a 64 bit hash of string. Rarely called */
792 static __u64 simple_hashstr(const char *str)
794 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
798 hash = (hash + (__u64) *str++) * hash_mult;
804 * cifs_backup_query_path_info - SMB1 fallback code to get ino
806 * Fallback code to get file metadata when we don't have access to
807 * @full_path (EACCES) and have backup creds.
809 * @data will be set to search info result buffer
810 * @resp_buf will be set to cifs resp buf and needs to be freed with
811 * cifs_buf_release() when done with @data.
814 cifs_backup_query_path_info(int xid,
815 struct cifs_tcon *tcon,
816 struct super_block *sb,
817 const char *full_path,
819 FILE_ALL_INFO **data)
821 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
822 struct cifs_search_info info = {0};
827 info.endOfSearch = false;
829 info.info_level = SMB_FIND_FILE_UNIX;
830 else if ((tcon->ses->capabilities &
831 tcon->ses->server->vals->cap_nt_find) == 0)
832 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
833 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
834 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
835 else /* no srvino useful for fallback to some netapp */
836 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
838 flags = CIFS_SEARCH_CLOSE_ALWAYS |
839 CIFS_SEARCH_CLOSE_AT_END |
840 CIFS_SEARCH_BACKUP_SEARCH;
842 rc = CIFSFindFirst(xid, tcon, full_path,
843 cifs_sb, NULL, flags, &info, false);
847 *resp_buf = (void *)info.ntwrk_buf_start;
848 *data = (FILE_ALL_INFO *)info.srch_entries_start;
853 cifs_set_fattr_ino(int xid,
854 struct cifs_tcon *tcon,
855 struct super_block *sb,
856 struct inode **inode,
857 const char *full_path,
859 struct cifs_fattr *fattr)
861 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
862 struct TCP_Server_Info *server = tcon->ses->server;
865 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
867 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
869 fattr->cf_uniqueid = iunique(sb, ROOT_I);
874 * If we have an inode pass a NULL tcon to ensure we don't
875 * make a round trip to the server. This only works for SMB2+.
877 rc = server->ops->get_srv_inum(xid,
878 *inode ? NULL : tcon,
884 * If that fails reuse existing ino or generate one
885 * and disable server ones
888 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
890 fattr->cf_uniqueid = iunique(sb, ROOT_I);
891 cifs_autodisable_serverino(cifs_sb);
896 /* If no errors, check for zero root inode (invalid) */
897 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
898 cifs_dbg(FYI, "Invalid (0) inodenum\n");
901 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
903 /* make an ino by hashing the UNC */
904 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
905 fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
910 static inline bool is_inode_cache_good(struct inode *ino)
912 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
916 cifs_get_inode_info(struct inode **inode,
917 const char *full_path,
918 FILE_ALL_INFO *in_data,
919 struct super_block *sb, int xid,
920 const struct cifs_fid *fid)
923 struct cifs_tcon *tcon;
924 struct TCP_Server_Info *server;
925 struct tcon_link *tlink;
926 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
927 bool adjust_tz = false;
928 struct cifs_fattr fattr = {0};
929 bool is_reparse_point = false;
930 FILE_ALL_INFO *data = in_data;
931 FILE_ALL_INFO *tmp_data = NULL;
932 void *smb1_backup_rsp_buf = NULL;
935 __u32 reparse_tag = 0;
937 tlink = cifs_sb_tlink(cifs_sb);
939 return PTR_ERR(tlink);
940 tcon = tlink_tcon(tlink);
941 server = tcon->ses->server;
944 * 1. Fetch file metadata if not provided (data)
948 if (is_inode_cache_good(*inode)) {
949 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
952 tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
957 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
959 &adjust_tz, &is_reparse_point);
964 * 2. Convert it to internal cifs metadata (fattr)
970 * If the file is a reparse point, it is more complicated
971 * since we have to check if its reparse tag matches a known
972 * special file type e.g. symlink or fifo or char etc.
974 if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
975 server->ops->query_reparse_tag) {
976 rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
977 full_path, &reparse_tag);
978 cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
980 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
981 is_reparse_point, reparse_tag);
984 /* DFS link, no metadata available on this server */
985 cifs_create_dfs_fattr(&fattr, sb);
990 * perm errors, try again with backup flags if possible
992 * For SMB2 and later the backup intent flag
993 * is already sent if needed on open and there
994 * is no path based FindFirst operation to use
997 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
998 /* for easier reading */
999 FILE_DIRECTORY_INFO *fdi;
1000 SEARCH_ID_FULL_DIR_INFO *si;
1002 rc = cifs_backup_query_path_info(xid, tcon, sb,
1004 &smb1_backup_rsp_buf,
1009 fdi = (FILE_DIRECTORY_INFO *)data;
1010 si = (SEARCH_ID_FULL_DIR_INFO *)data;
1012 cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1013 fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1014 /* uniqueid set, skip get inum step */
1015 goto handle_mnt_opt;
1017 /* nothing we can do, bail out */
1022 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1027 * 3. Get or update inode number (fattr.cf_uniqueid)
1030 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1033 * 4. Tweak fattr based on mount options
1037 /* query for SFU type info if supported and needed */
1038 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1039 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1040 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1042 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1045 /* fill in 0777 bits from ACL */
1046 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1047 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1052 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1056 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1057 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1062 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1068 /* fill in remaining high mode bits e.g. SUID, VTX */
1069 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1070 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1072 /* check for Minshall+French symlinks */
1073 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1074 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1077 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1081 * 5. Update inode with final fattr data
1085 *inode = cifs_iget(sb, &fattr);
1089 /* we already have inode, update it */
1091 /* if uniqueid is different, return error */
1092 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1093 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1094 CIFS_I(*inode)->time = 0; /* force reval */
1099 /* if filetype is different, return error */
1100 if (unlikely(((*inode)->i_mode & S_IFMT) !=
1101 (fattr.cf_mode & S_IFMT))) {
1102 CIFS_I(*inode)->time = 0; /* force reval */
1107 cifs_fattr_to_inode(*inode, &fattr);
1110 cifs_buf_release(smb1_backup_rsp_buf);
1111 cifs_put_tlink(tlink);
1117 smb311_posix_get_inode_info(struct inode **inode,
1118 const char *full_path,
1119 struct super_block *sb, unsigned int xid)
1121 struct cifs_tcon *tcon;
1122 struct tcon_link *tlink;
1123 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1124 bool adjust_tz = false;
1125 struct cifs_fattr fattr = {0};
1126 bool symlink = false;
1127 struct smb311_posix_qinfo *data = NULL;
1131 tlink = cifs_sb_tlink(cifs_sb);
1133 return PTR_ERR(tlink);
1134 tcon = tlink_tcon(tlink);
1137 * 1. Fetch file metadata
1140 if (is_inode_cache_good(*inode)) {
1141 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1144 data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1150 rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1152 &adjust_tz, &symlink);
1155 * 2. Convert it to internal cifs metadata (fattr)
1160 smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1163 /* DFS link, no metadata available on this server */
1164 cifs_create_dfs_fattr(&fattr, sb);
1169 * For SMB2 and later the backup intent flag
1170 * is already sent if needed on open and there
1171 * is no path based FindFirst operation to use
1172 * to retry with so nothing we can do, bail out
1176 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1182 * 3. Tweak fattr based on mount options
1185 /* check for Minshall+French symlinks */
1186 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1187 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1190 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1194 * 4. Update inode with final fattr data
1198 *inode = cifs_iget(sb, &fattr);
1202 /* we already have inode, update it */
1204 /* if uniqueid is different, return error */
1205 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1206 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1207 CIFS_I(*inode)->time = 0; /* force reval */
1212 /* if filetype is different, return error */
1213 if (unlikely(((*inode)->i_mode & S_IFMT) !=
1214 (fattr.cf_mode & S_IFMT))) {
1215 CIFS_I(*inode)->time = 0; /* force reval */
1220 cifs_fattr_to_inode(*inode, &fattr);
1223 cifs_put_tlink(tlink);
1229 static const struct inode_operations cifs_ipc_inode_ops = {
1230 .lookup = cifs_lookup,
1234 cifs_find_inode(struct inode *inode, void *opaque)
1236 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1238 /* don't match inode with different uniqueid */
1239 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1242 /* use createtime like an i_generation field */
1243 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1246 /* don't match inode of different type */
1247 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
1250 /* if it's not a directory or has no dentries, then flag it */
1251 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1252 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1258 cifs_init_inode(struct inode *inode, void *opaque)
1260 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
1262 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1263 CIFS_I(inode)->createtime = fattr->cf_createtime;
1268 * walk dentry list for an inode and report whether it has aliases that
1269 * are hashed. We use this to determine if a directory inode can actually
1273 inode_has_hashed_dentries(struct inode *inode)
1275 struct dentry *dentry;
1277 spin_lock(&inode->i_lock);
1278 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1279 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1280 spin_unlock(&inode->i_lock);
1284 spin_unlock(&inode->i_lock);
1288 /* Given fattrs, get a corresponding inode */
1290 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1293 struct inode *inode;
1296 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1298 /* hash down to 32-bits on 32-bit arch */
1299 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1301 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1303 /* was there a potentially problematic inode collision? */
1304 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1305 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1307 if (inode_has_hashed_dentries(inode)) {
1308 cifs_autodisable_serverino(CIFS_SB(sb));
1310 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1311 goto retry_iget5_locked;
1315 cifs_fattr_to_inode(inode, fattr);
1316 if (sb->s_flags & SB_NOATIME)
1317 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1318 if (inode->i_state & I_NEW) {
1319 inode->i_ino = hash;
1320 #ifdef CONFIG_CIFS_FSCACHE
1321 /* initialize per-inode cache cookie pointer */
1322 CIFS_I(inode)->fscache = NULL;
1324 unlock_new_inode(inode);
1331 /* gets root inode */
1332 struct inode *cifs_root_iget(struct super_block *sb)
1335 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1336 struct inode *inode = NULL;
1338 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1342 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1343 && cifs_sb->prepath) {
1344 len = strlen(cifs_sb->prepath);
1345 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1347 return ERR_PTR(-ENOMEM);
1349 memcpy(path+1, cifs_sb->prepath, len);
1351 path = kstrdup("", GFP_KERNEL);
1353 return ERR_PTR(-ENOMEM);
1357 if (tcon->unix_ext) {
1358 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1359 /* some servers mistakenly claim POSIX support */
1360 if (rc != -EOPNOTSUPP)
1362 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1363 tcon->unix_ext = false;
1366 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1367 if (tcon->posix_extensions)
1368 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1370 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1374 inode = ERR_PTR(rc);
1378 #ifdef CONFIG_CIFS_FSCACHE
1379 /* populate tcon->resource_id */
1380 tcon->resource_id = CIFS_I(inode)->uniqueid;
1383 if (rc && tcon->pipe) {
1384 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1385 spin_lock(&inode->i_lock);
1386 inode->i_mode |= S_IFDIR;
1387 set_nlink(inode, 2);
1388 inode->i_op = &cifs_ipc_inode_ops;
1389 inode->i_fop = &simple_dir_operations;
1390 inode->i_uid = cifs_sb->mnt_uid;
1391 inode->i_gid = cifs_sb->mnt_gid;
1392 spin_unlock(&inode->i_lock);
1395 inode = ERR_PTR(rc);
1405 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1406 char *full_path, __u32 dosattr)
1408 bool set_time = false;
1409 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1410 struct TCP_Server_Info *server;
1411 FILE_BASIC_INFO info_buf;
1416 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1417 if (!server->ops->set_file_info)
1422 if (attrs->ia_valid & ATTR_ATIME) {
1424 info_buf.LastAccessTime =
1425 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1427 info_buf.LastAccessTime = 0;
1429 if (attrs->ia_valid & ATTR_MTIME) {
1431 info_buf.LastWriteTime =
1432 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1434 info_buf.LastWriteTime = 0;
1437 * Samba throws this field away, but windows may actually use it.
1438 * Do not set ctime unless other time stamps are changed explicitly
1439 * (i.e. by utimes()) since we would then have a mix of client and
1442 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1443 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1444 info_buf.ChangeTime =
1445 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1447 info_buf.ChangeTime = 0;
1449 info_buf.CreationTime = 0; /* don't change */
1450 info_buf.Attributes = cpu_to_le32(dosattr);
1452 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1456 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1457 * and rename it to a random name that hopefully won't conflict with
1461 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1462 const unsigned int xid)
1466 struct cifs_fid fid;
1467 struct cifs_open_parms oparms;
1468 struct inode *inode = d_inode(dentry);
1469 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1470 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1471 struct tcon_link *tlink;
1472 struct cifs_tcon *tcon;
1473 __u32 dosattr, origattr;
1474 FILE_BASIC_INFO *info_buf = NULL;
1476 tlink = cifs_sb_tlink(cifs_sb);
1478 return PTR_ERR(tlink);
1479 tcon = tlink_tcon(tlink);
1482 * We cannot rename the file if the server doesn't support
1483 * CAP_INFOLEVEL_PASSTHRU
1485 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1491 oparms.cifs_sb = cifs_sb;
1492 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1493 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1494 oparms.disposition = FILE_OPEN;
1495 oparms.path = full_path;
1497 oparms.reconnect = false;
1499 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1503 origattr = cifsInode->cifsAttrs;
1505 origattr |= ATTR_NORMAL;
1507 dosattr = origattr & ~ATTR_READONLY;
1509 dosattr |= ATTR_NORMAL;
1510 dosattr |= ATTR_HIDDEN;
1512 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1513 if (dosattr != origattr) {
1514 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1515 if (info_buf == NULL) {
1519 info_buf->Attributes = cpu_to_le32(dosattr);
1520 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1522 /* although we would like to mark the file hidden
1523 if that fails we will still try to rename it */
1525 cifsInode->cifsAttrs = dosattr;
1527 dosattr = origattr; /* since not able to change them */
1530 /* rename the file */
1531 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1533 cifs_remap(cifs_sb));
1539 /* try to set DELETE_ON_CLOSE */
1540 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1541 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1544 * some samba versions return -ENOENT when we try to set the
1545 * file disposition here. Likely a samba bug, but work around
1546 * it for now. This means that some cifsXXX files may hang
1547 * around after they shouldn't.
1549 * BB: remove this hack after more servers have the fix
1557 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1561 CIFSSMBClose(xid, tcon, fid.netfid);
1564 cifs_put_tlink(tlink);
1568 * reset everything back to the original state. Don't bother
1569 * dealing with errors here since we can't do anything about
1573 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1574 cifs_sb->local_nls, cifs_remap(cifs_sb));
1576 if (dosattr != origattr) {
1577 info_buf->Attributes = cpu_to_le32(origattr);
1578 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1580 cifsInode->cifsAttrs = origattr;
1586 /* copied from fs/nfs/dir.c with small changes */
1588 cifs_drop_nlink(struct inode *inode)
1590 spin_lock(&inode->i_lock);
1591 if (inode->i_nlink > 0)
1593 spin_unlock(&inode->i_lock);
1597 * If d_inode(dentry) is null (usually meaning the cached dentry
1598 * is a negative dentry) then we would attempt a standard SMB delete, but
1599 * if that fails we can not attempt the fall back mechanisms on EACCES
1600 * but will return the EACCES to the caller. Note that the VFS does not call
1601 * unlink on negative dentries currently.
1603 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1607 char *full_path = NULL;
1608 struct inode *inode = d_inode(dentry);
1609 struct cifsInodeInfo *cifs_inode;
1610 struct super_block *sb = dir->i_sb;
1611 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1612 struct tcon_link *tlink;
1613 struct cifs_tcon *tcon;
1614 struct TCP_Server_Info *server;
1615 struct iattr *attrs = NULL;
1616 __u32 dosattr = 0, origattr = 0;
1618 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1620 tlink = cifs_sb_tlink(cifs_sb);
1622 return PTR_ERR(tlink);
1623 tcon = tlink_tcon(tlink);
1624 server = tcon->ses->server;
1628 if (tcon->nodelete) {
1633 /* Unlink can be called from rename so we can not take the
1634 * sb->s_vfs_rename_mutex here */
1635 full_path = build_path_from_dentry(dentry);
1636 if (full_path == NULL) {
1641 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1642 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1643 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1644 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1645 cifs_remap(cifs_sb));
1646 cifs_dbg(FYI, "posix del rc %d\n", rc);
1647 if ((rc == 0) || (rc == -ENOENT))
1648 goto psx_del_no_retry;
1652 if (!server->ops->unlink) {
1654 goto psx_del_no_retry;
1657 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1662 cifs_drop_nlink(inode);
1663 } else if (rc == -ENOENT) {
1665 } else if (rc == -EBUSY) {
1666 if (server->ops->rename_pending_delete) {
1667 rc = server->ops->rename_pending_delete(full_path,
1670 cifs_drop_nlink(inode);
1672 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1673 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1674 if (attrs == NULL) {
1679 /* try to reset dos attributes */
1680 cifs_inode = CIFS_I(inode);
1681 origattr = cifs_inode->cifsAttrs;
1683 origattr |= ATTR_NORMAL;
1684 dosattr = origattr & ~ATTR_READONLY;
1686 dosattr |= ATTR_NORMAL;
1687 dosattr |= ATTR_HIDDEN;
1689 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1693 goto retry_std_delete;
1696 /* undo the setattr if we errored out and it's needed */
1697 if (rc != 0 && dosattr != 0)
1698 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1702 cifs_inode = CIFS_I(inode);
1703 cifs_inode->time = 0; /* will force revalidate to get info
1705 inode->i_ctime = current_time(inode);
1707 dir->i_ctime = dir->i_mtime = current_time(dir);
1708 cifs_inode = CIFS_I(dir);
1709 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1714 cifs_put_tlink(tlink);
1719 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1720 const char *full_path, struct cifs_sb_info *cifs_sb,
1721 struct cifs_tcon *tcon, const unsigned int xid)
1724 struct inode *inode = NULL;
1726 if (tcon->posix_extensions)
1727 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1728 else if (tcon->unix_ext)
1729 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1732 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1739 * setting nlink not necessary except in cases where we failed to get it
1740 * from the server or was set bogus. Also, since this is a brand new
1741 * inode, no need to grab the i_lock before setting the i_nlink.
1743 if (inode->i_nlink < 2)
1744 set_nlink(inode, 2);
1745 mode &= ~current_umask();
1746 /* must turn on setgid bit if parent dir has it */
1747 if (parent->i_mode & S_ISGID)
1750 if (tcon->unix_ext) {
1751 struct cifs_unix_set_info_args args = {
1753 .ctime = NO_CHANGE_64,
1754 .atime = NO_CHANGE_64,
1755 .mtime = NO_CHANGE_64,
1758 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1759 args.uid = current_fsuid();
1760 if (parent->i_mode & S_ISGID)
1761 args.gid = parent->i_gid;
1763 args.gid = current_fsgid();
1765 args.uid = INVALID_UID; /* no change */
1766 args.gid = INVALID_GID; /* no change */
1768 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1770 cifs_remap(cifs_sb));
1772 struct TCP_Server_Info *server = tcon->ses->server;
1773 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1774 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1775 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1777 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1778 inode->i_mode = (mode | S_IFDIR);
1780 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1781 inode->i_uid = current_fsuid();
1782 if (inode->i_mode & S_ISGID)
1783 inode->i_gid = parent->i_gid;
1785 inode->i_gid = current_fsgid();
1788 d_instantiate(dentry, inode);
1793 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1794 const char *full_path, struct cifs_sb_info *cifs_sb,
1795 struct cifs_tcon *tcon, const unsigned int xid)
1799 FILE_UNIX_BASIC_INFO *info = NULL;
1800 struct inode *newinode = NULL;
1801 struct cifs_fattr fattr;
1803 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1806 goto posix_mkdir_out;
1809 mode &= ~current_umask();
1810 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1811 NULL /* netfid */, info, &oplock, full_path,
1812 cifs_sb->local_nls, cifs_remap(cifs_sb));
1813 if (rc == -EOPNOTSUPP)
1814 goto posix_mkdir_out;
1816 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1818 goto posix_mkdir_out;
1821 if (info->Type == cpu_to_le32(-1))
1822 /* no return info, go query for it */
1823 goto posix_mkdir_get_info;
1825 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1826 * need to set uid/gid.
1829 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1830 cifs_fill_uniqueid(inode->i_sb, &fattr);
1831 newinode = cifs_iget(inode->i_sb, &fattr);
1833 goto posix_mkdir_get_info;
1835 d_instantiate(dentry, newinode);
1837 #ifdef CONFIG_CIFS_DEBUG2
1838 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1839 dentry, dentry, newinode);
1841 if (newinode->i_nlink != 2)
1842 cifs_dbg(FYI, "unexpected number of links %d\n",
1849 posix_mkdir_get_info:
1850 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1852 goto posix_mkdir_out;
1855 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1859 struct cifs_sb_info *cifs_sb;
1860 struct tcon_link *tlink;
1861 struct cifs_tcon *tcon;
1862 struct TCP_Server_Info *server;
1865 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1868 cifs_sb = CIFS_SB(inode->i_sb);
1869 tlink = cifs_sb_tlink(cifs_sb);
1871 return PTR_ERR(tlink);
1872 tcon = tlink_tcon(tlink);
1876 full_path = build_path_from_dentry(direntry);
1877 if (full_path == NULL) {
1882 server = tcon->ses->server;
1884 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1885 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1887 d_drop(direntry); /* for time being always refresh inode info */
1891 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1892 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1893 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1895 if (rc != -EOPNOTSUPP)
1899 if (!server->ops->mkdir) {
1904 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1905 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1907 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1912 /* TODO: skip this for smb2/smb3 */
1913 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1917 * Force revalidate to get parent dir info when needed since cached
1918 * attributes are invalid now.
1920 CIFS_I(inode)->time = 0;
1923 cifs_put_tlink(tlink);
1927 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1931 struct cifs_sb_info *cifs_sb;
1932 struct tcon_link *tlink;
1933 struct cifs_tcon *tcon;
1934 struct TCP_Server_Info *server;
1935 char *full_path = NULL;
1936 struct cifsInodeInfo *cifsInode;
1938 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1942 full_path = build_path_from_dentry(direntry);
1943 if (full_path == NULL) {
1948 cifs_sb = CIFS_SB(inode->i_sb);
1949 tlink = cifs_sb_tlink(cifs_sb);
1950 if (IS_ERR(tlink)) {
1951 rc = PTR_ERR(tlink);
1954 tcon = tlink_tcon(tlink);
1955 server = tcon->ses->server;
1957 if (!server->ops->rmdir) {
1959 cifs_put_tlink(tlink);
1963 if (tcon->nodelete) {
1965 cifs_put_tlink(tlink);
1969 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1970 cifs_put_tlink(tlink);
1973 spin_lock(&d_inode(direntry)->i_lock);
1974 i_size_write(d_inode(direntry), 0);
1975 clear_nlink(d_inode(direntry));
1976 spin_unlock(&d_inode(direntry)->i_lock);
1979 cifsInode = CIFS_I(d_inode(direntry));
1980 /* force revalidate to go get info when needed */
1981 cifsInode->time = 0;
1983 cifsInode = CIFS_I(inode);
1985 * Force revalidate to get parent dir info when needed since cached
1986 * attributes are invalid now.
1988 cifsInode->time = 0;
1990 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1991 current_time(inode);
2000 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2001 const char *from_path, struct dentry *to_dentry,
2002 const char *to_path)
2004 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2005 struct tcon_link *tlink;
2006 struct cifs_tcon *tcon;
2007 struct TCP_Server_Info *server;
2008 struct cifs_fid fid;
2009 struct cifs_open_parms oparms;
2012 tlink = cifs_sb_tlink(cifs_sb);
2014 return PTR_ERR(tlink);
2015 tcon = tlink_tcon(tlink);
2016 server = tcon->ses->server;
2018 if (!server->ops->rename)
2021 /* try path-based rename first */
2022 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2025 * Don't bother with rename by filehandle unless file is busy and
2026 * source. Note that cross directory moves do not work with
2027 * rename by filehandle to various Windows servers.
2029 if (rc == 0 || rc != -EBUSY)
2030 goto do_rename_exit;
2032 /* Don't fall back to using SMB on SMB 2+ mount */
2033 if (server->vals->protocol_id != 0)
2034 goto do_rename_exit;
2036 /* open-file renames don't work across directories */
2037 if (to_dentry->d_parent != from_dentry->d_parent)
2038 goto do_rename_exit;
2041 oparms.cifs_sb = cifs_sb;
2042 /* open the file to be renamed -- we need DELETE perms */
2043 oparms.desired_access = DELETE;
2044 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2045 oparms.disposition = FILE_OPEN;
2046 oparms.path = from_path;
2048 oparms.reconnect = false;
2050 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2052 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2053 (const char *) to_dentry->d_name.name,
2054 cifs_sb->local_nls, cifs_remap(cifs_sb));
2055 CIFSSMBClose(xid, tcon, fid.netfid);
2059 d_move(from_dentry, to_dentry);
2060 cifs_put_tlink(tlink);
2065 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
2066 struct inode *target_dir, struct dentry *target_dentry,
2069 char *from_name = NULL;
2070 char *to_name = NULL;
2071 struct cifs_sb_info *cifs_sb;
2072 struct tcon_link *tlink;
2073 struct cifs_tcon *tcon;
2074 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2075 FILE_UNIX_BASIC_INFO *info_buf_target;
2079 if (flags & ~RENAME_NOREPLACE)
2082 cifs_sb = CIFS_SB(source_dir->i_sb);
2083 tlink = cifs_sb_tlink(cifs_sb);
2085 return PTR_ERR(tlink);
2086 tcon = tlink_tcon(tlink);
2091 * we already have the rename sem so we do not need to
2092 * grab it again here to protect the path integrity
2094 from_name = build_path_from_dentry(source_dentry);
2095 if (from_name == NULL) {
2097 goto cifs_rename_exit;
2100 to_name = build_path_from_dentry(target_dentry);
2101 if (to_name == NULL) {
2103 goto cifs_rename_exit;
2106 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2110 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2112 if (flags & RENAME_NOREPLACE)
2113 goto cifs_rename_exit;
2115 if (rc == -EEXIST && tcon->unix_ext) {
2117 * Are src and dst hardlinks of same inode? We can only tell
2118 * with unix extensions enabled.
2121 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2123 if (info_buf_source == NULL) {
2125 goto cifs_rename_exit;
2128 info_buf_target = info_buf_source + 1;
2129 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2132 cifs_remap(cifs_sb));
2136 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2139 cifs_remap(cifs_sb));
2141 if (tmprc == 0 && (info_buf_source->UniqueId ==
2142 info_buf_target->UniqueId)) {
2143 /* same file, POSIX says that this is a noop */
2145 goto cifs_rename_exit;
2149 * else ... BB we could add the same check for Windows by
2150 * checking the UniqueId via FILE_INTERNAL_INFO
2154 /* Try unlinking the target dentry if it's not negative */
2155 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2156 if (d_is_dir(target_dentry))
2157 tmprc = cifs_rmdir(target_dir, target_dentry);
2159 tmprc = cifs_unlink(target_dir, target_dentry);
2161 goto cifs_rename_exit;
2162 rc = cifs_do_rename(xid, source_dentry, from_name,
2163 target_dentry, to_name);
2166 /* force revalidate to go get info when needed */
2167 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2169 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2170 target_dir->i_mtime = current_time(source_dir);
2173 kfree(info_buf_source);
2177 cifs_put_tlink(tlink);
2182 cifs_inode_needs_reval(struct inode *inode)
2184 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2185 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2187 if (cifs_i->time == 0)
2190 if (CIFS_CACHE_READ(cifs_i))
2193 if (!lookupCacheEnabled)
2196 if (!cifs_sb->actimeo)
2199 if (!time_in_range(jiffies, cifs_i->time,
2200 cifs_i->time + cifs_sb->actimeo))
2203 /* hardlinked files w/ noserverino get "special" treatment */
2204 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2205 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2212 * Zap the cache. Called when invalid_mapping flag is set.
2215 cifs_invalidate_mapping(struct inode *inode)
2219 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2220 rc = invalidate_inode_pages2(inode->i_mapping);
2222 cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2226 cifs_fscache_reset_inode_cookie(inode);
2231 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2232 * @word: long word containing the bit lock
2235 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2237 freezable_schedule_unsafe();
2238 if (signal_pending_state(mode, current))
2239 return -ERESTARTSYS;
2244 cifs_revalidate_mapping(struct inode *inode)
2247 unsigned long *flags = &CIFS_I(inode)->flags;
2249 /* swapfiles are not supposed to be shared */
2250 if (IS_SWAPFILE(inode))
2253 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2258 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2259 rc = cifs_invalidate_mapping(inode);
2261 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2264 clear_bit_unlock(CIFS_INO_LOCK, flags);
2265 smp_mb__after_atomic();
2266 wake_up_bit(flags, CIFS_INO_LOCK);
2272 cifs_zap_mapping(struct inode *inode)
2274 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2275 return cifs_revalidate_mapping(inode);
2278 int cifs_revalidate_file_attr(struct file *filp)
2281 struct inode *inode = file_inode(filp);
2282 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2284 if (!cifs_inode_needs_reval(inode))
2287 if (tlink_tcon(cfile->tlink)->unix_ext)
2288 rc = cifs_get_file_info_unix(filp);
2290 rc = cifs_get_file_info(filp);
2295 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2299 struct inode *inode = d_inode(dentry);
2300 struct super_block *sb = dentry->d_sb;
2301 char *full_path = NULL;
2307 if (!cifs_inode_needs_reval(inode))
2312 /* can not safely grab the rename sem here if rename calls revalidate
2313 since that would deadlock */
2314 full_path = build_path_from_dentry(dentry);
2315 if (full_path == NULL) {
2320 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2321 full_path, inode, inode->i_count.counter,
2322 dentry, cifs_get_time(dentry), jiffies);
2325 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2326 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2327 else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2328 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2330 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2332 if (rc == -EAGAIN && count++ < 10)
2341 int cifs_revalidate_file(struct file *filp)
2344 struct inode *inode = file_inode(filp);
2346 rc = cifs_revalidate_file_attr(filp);
2350 return cifs_revalidate_mapping(inode);
2353 /* revalidate a dentry's inode attributes */
2354 int cifs_revalidate_dentry(struct dentry *dentry)
2357 struct inode *inode = d_inode(dentry);
2359 rc = cifs_revalidate_dentry_attr(dentry);
2363 return cifs_revalidate_mapping(inode);
2366 int cifs_getattr(const struct path *path, struct kstat *stat,
2367 u32 request_mask, unsigned int flags)
2369 struct dentry *dentry = path->dentry;
2370 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2371 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2372 struct inode *inode = d_inode(dentry);
2376 * We need to be sure that all dirty pages are written and the server
2377 * has actual ctime, mtime and file length.
2379 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE)) &&
2380 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2381 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2382 rc = filemap_fdatawait(inode->i_mapping);
2384 mapping_set_error(inode->i_mapping, rc);
2389 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2390 CIFS_I(inode)->time = 0; /* force revalidate */
2393 * If the caller doesn't require syncing, only sync if
2394 * necessary (e.g. due to earlier truncate or setattr
2395 * invalidating the cached metadata)
2397 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2398 (CIFS_I(inode)->time == 0)) {
2399 rc = cifs_revalidate_dentry_attr(dentry);
2404 generic_fillattr(inode, stat);
2405 stat->blksize = cifs_sb->bsize;
2406 stat->ino = CIFS_I(inode)->uniqueid;
2408 /* old CIFS Unix Extensions doesn't return create time */
2409 if (CIFS_I(inode)->createtime) {
2410 stat->result_mask |= STATX_BTIME;
2412 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2415 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2416 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2417 stat->attributes |= STATX_ATTR_COMPRESSED;
2418 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2419 stat->attributes |= STATX_ATTR_ENCRYPTED;
2422 * If on a multiuser mount without unix extensions or cifsacl being
2423 * enabled, and the admin hasn't overridden them, set the ownership
2424 * to the fsuid/fsgid of the current process.
2426 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2427 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2429 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2430 stat->uid = current_fsuid();
2431 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2432 stat->gid = current_fsgid();
2437 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2440 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2441 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->vfs_inode.i_sb);
2442 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2443 struct TCP_Server_Info *server = tcon->ses->server;
2444 struct cifsFileInfo *cfile;
2448 * We need to be sure that all dirty pages are written as they
2449 * might fill holes on the server.
2451 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2452 inode->i_mapping->nrpages != 0) {
2453 rc = filemap_fdatawait(inode->i_mapping);
2455 mapping_set_error(inode->i_mapping, rc);
2460 cfile = find_readable_file(cifs_i, false);
2464 if (server->ops->fiemap) {
2465 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2466 cifsFileInfo_put(cfile);
2470 cifsFileInfo_put(cfile);
2474 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2476 pgoff_t index = from >> PAGE_SHIFT;
2477 unsigned offset = from & (PAGE_SIZE - 1);
2481 page = grab_cache_page(mapping, index);
2485 zero_user_segment(page, offset, PAGE_SIZE);
2491 void cifs_setsize(struct inode *inode, loff_t offset)
2493 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2495 spin_lock(&inode->i_lock);
2496 i_size_write(inode, offset);
2497 spin_unlock(&inode->i_lock);
2499 /* Cached inode must be refreshed on truncate */
2501 truncate_pagecache(inode, offset);
2505 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2506 unsigned int xid, char *full_path)
2509 struct cifsFileInfo *open_file;
2510 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2511 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2512 struct tcon_link *tlink = NULL;
2513 struct cifs_tcon *tcon = NULL;
2514 struct TCP_Server_Info *server;
2517 * To avoid spurious oplock breaks from server, in the case of
2518 * inodes that we already have open, avoid doing path based
2519 * setting of file size if we can do it by handle.
2520 * This keeps our caching token (oplock) and avoids timeouts
2521 * when the local oplock break takes longer to flush
2522 * writebehind data than the SMB timeout for the SetPathInfo
2523 * request would allow
2525 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2527 tcon = tlink_tcon(open_file->tlink);
2528 server = tcon->ses->server;
2529 if (server->ops->set_file_size)
2530 rc = server->ops->set_file_size(xid, tcon, open_file,
2531 attrs->ia_size, false);
2534 cifsFileInfo_put(open_file);
2535 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2543 tlink = cifs_sb_tlink(cifs_sb);
2545 return PTR_ERR(tlink);
2546 tcon = tlink_tcon(tlink);
2547 server = tcon->ses->server;
2551 * Set file size by pathname rather than by handle either because no
2552 * valid, writeable file handle for it was found or because there was
2553 * an error setting it by handle.
2555 if (server->ops->set_path_size)
2556 rc = server->ops->set_path_size(xid, tcon, full_path,
2557 attrs->ia_size, cifs_sb, false);
2560 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2563 cifs_put_tlink(tlink);
2567 cifsInode->server_eof = attrs->ia_size;
2568 cifs_setsize(inode, attrs->ia_size);
2571 * The man page of truncate says if the size changed,
2572 * then the st_ctime and st_mtime fields for the file
2575 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2576 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2578 cifs_truncate_page(inode->i_mapping, inode->i_size);
2585 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2589 char *full_path = NULL;
2590 struct inode *inode = d_inode(direntry);
2591 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2592 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2593 struct tcon_link *tlink;
2594 struct cifs_tcon *pTcon;
2595 struct cifs_unix_set_info_args *args = NULL;
2596 struct cifsFileInfo *open_file;
2598 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2599 direntry, attrs->ia_valid);
2603 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2604 attrs->ia_valid |= ATTR_FORCE;
2606 rc = setattr_prepare(direntry, attrs);
2610 full_path = build_path_from_dentry(direntry);
2611 if (full_path == NULL) {
2617 * Attempt to flush data before changing attributes. We need to do
2618 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2619 * ownership or mode then we may also need to do this. Here, we take
2620 * the safe way out and just do the flush on all setattr requests. If
2621 * the flush returns error, store it to report later and continue.
2623 * BB: This should be smarter. Why bother flushing pages that
2624 * will be truncated anyway? Also, should we error out here if
2625 * the flush returns error?
2627 rc = filemap_write_and_wait(inode->i_mapping);
2628 if (is_interrupt_error(rc)) {
2633 mapping_set_error(inode->i_mapping, rc);
2636 if (attrs->ia_valid & ATTR_SIZE) {
2637 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2642 /* skip mode change if it's just for clearing setuid/setgid */
2643 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2644 attrs->ia_valid &= ~ATTR_MODE;
2646 args = kmalloc(sizeof(*args), GFP_KERNEL);
2652 /* set up the struct */
2653 if (attrs->ia_valid & ATTR_MODE)
2654 args->mode = attrs->ia_mode;
2656 args->mode = NO_CHANGE_64;
2658 if (attrs->ia_valid & ATTR_UID)
2659 args->uid = attrs->ia_uid;
2661 args->uid = INVALID_UID; /* no change */
2663 if (attrs->ia_valid & ATTR_GID)
2664 args->gid = attrs->ia_gid;
2666 args->gid = INVALID_GID; /* no change */
2668 if (attrs->ia_valid & ATTR_ATIME)
2669 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2671 args->atime = NO_CHANGE_64;
2673 if (attrs->ia_valid & ATTR_MTIME)
2674 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2676 args->mtime = NO_CHANGE_64;
2678 if (attrs->ia_valid & ATTR_CTIME)
2679 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2681 args->ctime = NO_CHANGE_64;
2684 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2686 u16 nfid = open_file->fid.netfid;
2687 u32 npid = open_file->pid;
2688 pTcon = tlink_tcon(open_file->tlink);
2689 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2690 cifsFileInfo_put(open_file);
2692 tlink = cifs_sb_tlink(cifs_sb);
2693 if (IS_ERR(tlink)) {
2694 rc = PTR_ERR(tlink);
2697 pTcon = tlink_tcon(tlink);
2698 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2700 cifs_remap(cifs_sb));
2701 cifs_put_tlink(tlink);
2707 if ((attrs->ia_valid & ATTR_SIZE) &&
2708 attrs->ia_size != i_size_read(inode))
2709 truncate_setsize(inode, attrs->ia_size);
2711 setattr_copy(inode, attrs);
2712 mark_inode_dirty(inode);
2714 /* force revalidate when any of these times are set since some
2715 of the fs types (eg ext3, fat) do not have fine enough
2716 time granularity to match protocol, and we do not have a
2717 a way (yet) to query the server fs's time granularity (and
2718 whether it rounds times down).
2720 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2721 cifsInode->time = 0;
2730 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2733 kuid_t uid = INVALID_UID;
2734 kgid_t gid = INVALID_GID;
2735 struct inode *inode = d_inode(direntry);
2736 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2737 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2738 struct cifsFileInfo *wfile;
2739 struct cifs_tcon *tcon;
2740 char *full_path = NULL;
2743 __u64 mode = NO_CHANGE_64;
2747 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2748 direntry, attrs->ia_valid);
2750 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2751 attrs->ia_valid |= ATTR_FORCE;
2753 rc = setattr_prepare(direntry, attrs);
2759 full_path = build_path_from_dentry(direntry);
2760 if (full_path == NULL) {
2767 * Attempt to flush data before changing attributes. We need to do
2768 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
2769 * returns error, store it to report later and continue.
2771 * BB: This should be smarter. Why bother flushing pages that
2772 * will be truncated anyway? Also, should we error out here if
2773 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2775 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2776 rc = filemap_write_and_wait(inode->i_mapping);
2777 if (is_interrupt_error(rc)) {
2779 goto cifs_setattr_exit;
2781 mapping_set_error(inode->i_mapping, rc);
2786 if ((attrs->ia_valid & ATTR_MTIME) &&
2787 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2788 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2790 tcon = tlink_tcon(wfile->tlink);
2791 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2792 cifsFileInfo_put(wfile);
2794 goto cifs_setattr_exit;
2795 } else if (rc != -EBADF)
2796 goto cifs_setattr_exit;
2801 if (attrs->ia_valid & ATTR_SIZE) {
2802 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2804 goto cifs_setattr_exit;
2807 if (attrs->ia_valid & ATTR_UID)
2808 uid = attrs->ia_uid;
2810 if (attrs->ia_valid & ATTR_GID)
2811 gid = attrs->ia_gid;
2813 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2814 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2815 if (uid_valid(uid) || gid_valid(gid)) {
2816 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2819 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2821 goto cifs_setattr_exit;
2825 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2826 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2828 /* skip mode change if it's just for clearing setuid/setgid */
2829 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2830 attrs->ia_valid &= ~ATTR_MODE;
2832 if (attrs->ia_valid & ATTR_MODE) {
2833 mode = attrs->ia_mode;
2835 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2836 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2837 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2838 INVALID_UID, INVALID_GID);
2840 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2842 goto cifs_setattr_exit;
2845 if (((mode & S_IWUGO) == 0) &&
2846 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2848 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2850 /* fix up mode if we're not using dynperm */
2851 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2852 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2853 } else if ((mode & S_IWUGO) &&
2854 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2856 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2857 /* Attributes of 0 are ignored */
2859 dosattr |= ATTR_NORMAL;
2861 /* reset local inode permissions to normal */
2862 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2863 attrs->ia_mode &= ~(S_IALLUGO);
2864 if (S_ISDIR(inode->i_mode))
2866 cifs_sb->mnt_dir_mode;
2869 cifs_sb->mnt_file_mode;
2871 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2872 /* ignore mode change - ATTR_READONLY hasn't changed */
2873 attrs->ia_valid &= ~ATTR_MODE;
2877 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2878 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2879 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2880 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2882 /* Even if error on time set, no sense failing the call if
2883 the server would set the time to a reasonable value anyway,
2884 and this check ensures that we are not being called from
2885 sys_utimes in which case we ought to fail the call back to
2886 the user when the server rejects the call */
2887 if ((rc) && (attrs->ia_valid &
2888 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2892 /* do not need local check to inode_check_ok since the server does
2895 goto cifs_setattr_exit;
2897 if ((attrs->ia_valid & ATTR_SIZE) &&
2898 attrs->ia_size != i_size_read(inode))
2899 truncate_setsize(inode, attrs->ia_size);
2901 setattr_copy(inode, attrs);
2902 mark_inode_dirty(inode);
2911 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2913 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2914 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2915 int rc, retries = 0;
2918 if (pTcon->unix_ext)
2919 rc = cifs_setattr_unix(direntry, attrs);
2921 rc = cifs_setattr_nounix(direntry, attrs);
2923 } while (is_retryable_error(rc) && retries < 2);
2925 /* BB: add cifs_setattr_legacy for really old servers */
2930 void cifs_delete_inode(struct inode *inode)
2932 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2933 /* may have to add back in if and when safe distributed caching of
2934 directories added e.g. via FindNotify */