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>
28 #include <asm/div64.h>
32 #include "cifsproto.h"
33 #include "cifs_debug.h"
34 #include "cifs_fs_sb.h"
35 #include "cifs_unicode.h"
39 static void cifs_set_ops(struct inode *inode)
41 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
43 switch (inode->i_mode & S_IFMT) {
45 inode->i_op = &cifs_file_inode_ops;
46 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
47 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48 inode->i_fop = &cifs_file_direct_nobrl_ops;
50 inode->i_fop = &cifs_file_direct_ops;
51 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
52 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
53 inode->i_fop = &cifs_file_strict_nobrl_ops;
55 inode->i_fop = &cifs_file_strict_ops;
56 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
57 inode->i_fop = &cifs_file_nobrl_ops;
58 else { /* not direct, send byte range locks */
59 inode->i_fop = &cifs_file_ops;
62 /* check if server can support readpages */
63 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
64 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
65 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
67 inode->i_data.a_ops = &cifs_addr_ops;
70 #ifdef CONFIG_CIFS_DFS_UPCALL
71 if (IS_AUTOMOUNT(inode)) {
72 inode->i_op = &cifs_dfs_referral_inode_operations;
74 #else /* NO DFS support, treat as a directory */
77 inode->i_op = &cifs_dir_inode_ops;
78 inode->i_fop = &cifs_dir_ops;
82 inode->i_op = &cifs_symlink_inode_ops;
85 init_special_inode(inode, inode->i_mode, inode->i_rdev);
90 /* check inode attributes against fattr. If they don't match, tag the
91 * inode for cache invalidation
94 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
96 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
98 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
99 __func__, cifs_i->uniqueid);
101 if (inode->i_state & I_NEW) {
102 cifs_dbg(FYI, "%s: inode %llu is new\n",
103 __func__, cifs_i->uniqueid);
107 /* don't bother with revalidation if we have an oplock */
108 if (CIFS_CACHE_READ(cifs_i)) {
109 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
110 __func__, cifs_i->uniqueid);
114 /* revalidate if mtime or size have changed */
115 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
116 cifs_i->server_eof == fattr->cf_eof) {
117 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
118 __func__, cifs_i->uniqueid);
122 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
123 __func__, cifs_i->uniqueid);
124 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
128 * copy nlink to the inode, unless it wasn't provided. Provide
129 * sane values if we don't have an existing one and none was provided
132 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
135 * if we're in a situation where we can't trust what we
136 * got from the server (readdir, some non-unix cases)
137 * fake reasonable values
139 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
140 /* only provide fake values on a new inode */
141 if (inode->i_state & I_NEW) {
142 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
150 /* we trust the server, so update it */
151 set_nlink(inode, fattr->cf_nlink);
154 /* populate an inode with info from a cifs_fattr struct */
156 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
158 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
159 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
161 cifs_revalidate_cache(inode, fattr);
163 spin_lock(&inode->i_lock);
164 inode->i_atime = fattr->cf_atime;
165 inode->i_mtime = fattr->cf_mtime;
166 inode->i_ctime = fattr->cf_ctime;
167 inode->i_rdev = fattr->cf_rdev;
168 cifs_nlink_fattr_to_inode(inode, fattr);
169 inode->i_uid = fattr->cf_uid;
170 inode->i_gid = fattr->cf_gid;
172 /* if dynperm is set, don't clobber existing mode */
173 if (inode->i_state & I_NEW ||
174 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
175 inode->i_mode = fattr->cf_mode;
177 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
179 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
182 cifs_i->time = jiffies;
184 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
185 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
187 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
189 cifs_i->server_eof = fattr->cf_eof;
191 * Can't safely change the file size here if the client is writing to
192 * it due to potential races.
194 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
195 i_size_write(inode, fattr->cf_eof);
198 * i_blocks is not related to (i_size / i_blksize),
199 * but instead 512 byte (2**9) size is required for
200 * calculating num blocks.
202 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
204 spin_unlock(&inode->i_lock);
206 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
207 inode->i_flags |= S_AUTOMOUNT;
208 if (inode->i_state & I_NEW)
213 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
215 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
217 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
220 fattr->cf_uniqueid = iunique(sb, ROOT_I);
223 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
225 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
226 struct cifs_sb_info *cifs_sb)
228 memset(fattr, 0, sizeof(*fattr));
229 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
230 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
231 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
233 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
234 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
235 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
236 fattr->cf_mode = le64_to_cpu(info->Permissions);
239 * Since we set the inode type below we need to mask off
240 * to avoid strange results if bits set above.
242 fattr->cf_mode &= ~S_IFMT;
243 switch (le32_to_cpu(info->Type)) {
245 fattr->cf_mode |= S_IFREG;
246 fattr->cf_dtype = DT_REG;
249 fattr->cf_mode |= S_IFLNK;
250 fattr->cf_dtype = DT_LNK;
253 fattr->cf_mode |= S_IFDIR;
254 fattr->cf_dtype = DT_DIR;
257 fattr->cf_mode |= S_IFCHR;
258 fattr->cf_dtype = DT_CHR;
259 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
260 le64_to_cpu(info->DevMinor) & MINORMASK);
263 fattr->cf_mode |= S_IFBLK;
264 fattr->cf_dtype = DT_BLK;
265 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
266 le64_to_cpu(info->DevMinor) & MINORMASK);
269 fattr->cf_mode |= S_IFIFO;
270 fattr->cf_dtype = DT_FIFO;
273 fattr->cf_mode |= S_IFSOCK;
274 fattr->cf_dtype = DT_SOCK;
277 /* safest to call it a file if we do not know */
278 fattr->cf_mode |= S_IFREG;
279 fattr->cf_dtype = DT_REG;
280 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
284 fattr->cf_uid = cifs_sb->mnt_uid;
285 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
286 u64 id = le64_to_cpu(info->Uid);
287 if (id < ((uid_t)-1)) {
288 kuid_t uid = make_kuid(&init_user_ns, id);
294 fattr->cf_gid = cifs_sb->mnt_gid;
295 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
296 u64 id = le64_to_cpu(info->Gid);
297 if (id < ((gid_t)-1)) {
298 kgid_t gid = make_kgid(&init_user_ns, id);
304 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
308 * Fill a cifs_fattr struct with fake inode info.
310 * Needed to setup cifs_fattr data for the directory which is the
311 * junction to the new submount (ie to setup the fake directory
312 * which represents a DFS referral).
315 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
317 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
319 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
321 memset(fattr, 0, sizeof(*fattr));
322 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
323 fattr->cf_uid = cifs_sb->mnt_uid;
324 fattr->cf_gid = cifs_sb->mnt_gid;
325 ktime_get_real_ts(&fattr->cf_mtime);
326 fattr->cf_mtime = timespec_trunc(fattr->cf_mtime, sb->s_time_gran);
327 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
329 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
333 cifs_get_file_info_unix(struct file *filp)
337 FILE_UNIX_BASIC_INFO find_data;
338 struct cifs_fattr fattr;
339 struct inode *inode = file_inode(filp);
340 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
341 struct cifsFileInfo *cfile = filp->private_data;
342 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
345 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
347 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
348 } else if (rc == -EREMOTE) {
349 cifs_create_dfs_fattr(&fattr, inode->i_sb);
353 cifs_fattr_to_inode(inode, &fattr);
358 int cifs_get_inode_info_unix(struct inode **pinode,
359 const unsigned char *full_path,
360 struct super_block *sb, unsigned int xid)
363 FILE_UNIX_BASIC_INFO find_data;
364 struct cifs_fattr fattr;
365 struct cifs_tcon *tcon;
366 struct tcon_link *tlink;
367 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
369 cifs_dbg(FYI, "Getting info on %s\n", full_path);
371 tlink = cifs_sb_tlink(cifs_sb);
373 return PTR_ERR(tlink);
374 tcon = tlink_tcon(tlink);
376 /* could have done a find first instead but this returns more info */
377 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
378 cifs_sb->local_nls, cifs_remap(cifs_sb));
379 cifs_put_tlink(tlink);
382 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
383 } else if (rc == -EREMOTE) {
384 cifs_create_dfs_fattr(&fattr, sb);
390 /* check for Minshall+French symlinks */
391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
392 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
395 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
398 if (*pinode == NULL) {
400 cifs_fill_uniqueid(sb, &fattr);
401 *pinode = cifs_iget(sb, &fattr);
405 /* we already have inode, update it */
407 /* if uniqueid is different, return error */
408 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
409 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
414 /* if filetype is different, return error */
415 if (unlikely(((*pinode)->i_mode & S_IFMT) !=
416 (fattr.cf_mode & S_IFMT))) {
421 cifs_fattr_to_inode(*pinode, &fattr);
429 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
430 struct cifs_sb_info *cifs_sb, unsigned int xid)
434 struct tcon_link *tlink;
435 struct cifs_tcon *tcon;
437 struct cifs_open_parms oparms;
438 struct cifs_io_parms io_parms;
440 unsigned int bytes_read;
442 int buf_type = CIFS_NO_BUFFER;
446 fattr->cf_mode &= ~S_IFMT;
448 if (fattr->cf_eof == 0) {
449 fattr->cf_mode |= S_IFIFO;
450 fattr->cf_dtype = DT_FIFO;
452 } else if (fattr->cf_eof < 8) {
453 fattr->cf_mode |= S_IFREG;
454 fattr->cf_dtype = DT_REG;
455 return -EINVAL; /* EOPNOTSUPP? */
458 tlink = cifs_sb_tlink(cifs_sb);
460 return PTR_ERR(tlink);
461 tcon = tlink_tcon(tlink);
464 oparms.cifs_sb = cifs_sb;
465 oparms.desired_access = GENERIC_READ;
466 oparms.create_options = CREATE_NOT_DIR;
467 oparms.disposition = FILE_OPEN;
470 oparms.reconnect = false;
472 if (tcon->ses->server->oplocks)
476 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
478 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
479 cifs_put_tlink(tlink);
484 io_parms.netfid = fid.netfid;
485 io_parms.pid = current->tgid;
486 io_parms.tcon = tcon;
488 io_parms.length = 24;
490 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
491 &bytes_read, &pbuf, &buf_type);
492 if ((rc == 0) && (bytes_read >= 8)) {
493 if (memcmp("IntxBLK", pbuf, 8) == 0) {
494 cifs_dbg(FYI, "Block device\n");
495 fattr->cf_mode |= S_IFBLK;
496 fattr->cf_dtype = DT_BLK;
497 if (bytes_read == 24) {
498 /* we have enough to decode dev num */
499 __u64 mjr; /* major */
500 __u64 mnr; /* minor */
501 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
502 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
503 fattr->cf_rdev = MKDEV(mjr, mnr);
505 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
506 cifs_dbg(FYI, "Char device\n");
507 fattr->cf_mode |= S_IFCHR;
508 fattr->cf_dtype = DT_CHR;
509 if (bytes_read == 24) {
510 /* we have enough to decode dev num */
511 __u64 mjr; /* major */
512 __u64 mnr; /* minor */
513 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
514 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
515 fattr->cf_rdev = MKDEV(mjr, mnr);
517 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
518 cifs_dbg(FYI, "Symlink\n");
519 fattr->cf_mode |= S_IFLNK;
520 fattr->cf_dtype = DT_LNK;
522 fattr->cf_mode |= S_IFREG; /* file? */
523 fattr->cf_dtype = DT_REG;
527 fattr->cf_mode |= S_IFREG; /* then it is a file */
528 fattr->cf_dtype = DT_REG;
529 rc = -EOPNOTSUPP; /* or some unknown SFU type */
532 tcon->ses->server->ops->close(xid, tcon, &fid);
533 cifs_put_tlink(tlink);
537 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
540 * Fetch mode bits as provided by SFU.
542 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
544 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
545 struct cifs_sb_info *cifs_sb, unsigned int xid)
547 #ifdef CONFIG_CIFS_XATTR
551 struct tcon_link *tlink;
552 struct cifs_tcon *tcon;
554 tlink = cifs_sb_tlink(cifs_sb);
556 return PTR_ERR(tlink);
557 tcon = tlink_tcon(tlink);
559 if (tcon->ses->server->ops->query_all_EAs == NULL) {
560 cifs_put_tlink(tlink);
564 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
565 "SETFILEBITS", ea_value, 4 /* size of buf */,
567 cifs_put_tlink(tlink);
571 mode = le32_to_cpu(*((__le32 *)ea_value));
572 fattr->cf_mode &= ~SFBITS_MASK;
573 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
574 mode, fattr->cf_mode);
575 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
576 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
585 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
587 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
588 struct super_block *sb, bool adjust_tz,
591 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
592 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
594 memset(fattr, 0, sizeof(*fattr));
595 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
596 if (info->DeletePending)
597 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
599 if (info->LastAccessTime)
600 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
602 ktime_get_real_ts(&fattr->cf_atime);
603 fattr->cf_atime = timespec_trunc(fattr->cf_atime, sb->s_time_gran);
606 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
607 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
610 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
611 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
614 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
615 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
616 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
618 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
621 fattr->cf_mode = S_IFLNK;
622 fattr->cf_dtype = DT_LNK;
623 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
624 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
625 fattr->cf_dtype = DT_DIR;
627 * Server can return wrong NumberOfLinks value for directories
628 * when Unix extensions are disabled - fake it.
631 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
633 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
634 fattr->cf_dtype = DT_REG;
636 /* clear write bits if ATTR_READONLY is set */
637 if (fattr->cf_cifsattrs & ATTR_READONLY)
638 fattr->cf_mode &= ~(S_IWUGO);
641 * Don't accept zero nlink from non-unix servers unless
642 * delete is pending. Instead mark it as unknown.
644 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
645 !info->DeletePending) {
646 cifs_dbg(1, "bogus file nlink value %u\n",
648 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
652 fattr->cf_uid = cifs_sb->mnt_uid;
653 fattr->cf_gid = cifs_sb->mnt_gid;
657 cifs_get_file_info(struct file *filp)
661 FILE_ALL_INFO find_data;
662 struct cifs_fattr fattr;
663 struct inode *inode = file_inode(filp);
664 struct cifsFileInfo *cfile = filp->private_data;
665 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
666 struct TCP_Server_Info *server = tcon->ses->server;
668 if (!server->ops->query_file_info)
672 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
675 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
679 cifs_create_dfs_fattr(&fattr, inode->i_sb);
685 * FIXME: legacy server -- fall back to path-based call?
686 * for now, just skip revalidating and mark inode for
690 CIFS_I(inode)->time = 0;
696 * don't bother with SFU junk here -- just mark inode as needing
699 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
700 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
701 cifs_fattr_to_inode(inode, &fattr);
708 cifs_get_inode_info(struct inode **inode, const char *full_path,
709 FILE_ALL_INFO *data, struct super_block *sb, int xid,
710 const struct cifs_fid *fid)
712 bool validinum = false;
714 int rc = 0, tmprc = ENOSYS;
715 struct cifs_tcon *tcon;
716 struct TCP_Server_Info *server;
717 struct tcon_link *tlink;
718 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
720 bool adjust_tz = false;
721 struct cifs_fattr fattr;
722 struct cifs_search_info *srchinf = NULL;
723 bool symlink = false;
725 tlink = cifs_sb_tlink(cifs_sb);
727 return PTR_ERR(tlink);
728 tcon = tlink_tcon(tlink);
729 server = tcon->ses->server;
731 cifs_dbg(FYI, "Getting info on %s\n", full_path);
733 if ((data == NULL) && (*inode != NULL)) {
734 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
735 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
740 /* if inode info is not passed, get it from server */
742 if (!server->ops->query_path_info) {
746 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
751 data = (FILE_ALL_INFO *)buf;
752 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
753 data, &adjust_tz, &symlink);
757 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
759 } else if (rc == -EREMOTE) {
760 cifs_create_dfs_fattr(&fattr, sb);
762 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
763 srchinf = kzalloc(sizeof(struct cifs_search_info),
765 if (srchinf == NULL) {
770 srchinf->endOfSearch = false;
771 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
773 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
774 CIFS_SEARCH_CLOSE_AT_END |
775 CIFS_SEARCH_BACKUP_SEARCH;
777 rc = CIFSFindFirst(xid, tcon, full_path,
778 cifs_sb, NULL, srchflgs, srchinf, false);
781 (FILE_ALL_INFO *)srchinf->srch_entries_start;
783 cifs_dir_info_to_fattr(&fattr,
784 (FILE_DIRECTORY_INFO *)data, cifs_sb);
785 fattr.cf_uniqueid = le64_to_cpu(
786 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
789 cifs_buf_release(srchinf->ntwrk_buf_start);
798 * If an inode wasn't passed in, then get the inode number
800 * Is an i_ino of zero legal? Can we use that to check if the server
801 * supports returning inode numbers? Are there other sanity checks we
802 * can use to ensure that the server is really filling in that field?
804 if (*inode == NULL) {
805 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
806 if (validinum == false) {
807 if (server->ops->get_srv_inum)
808 tmprc = server->ops->get_srv_inum(xid,
809 tcon, cifs_sb, full_path,
810 &fattr.cf_uniqueid, data);
812 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
814 fattr.cf_uniqueid = iunique(sb, ROOT_I);
815 cifs_autodisable_serverino(cifs_sb);
819 fattr.cf_uniqueid = iunique(sb, ROOT_I);
821 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
822 validinum == false && server->ops->get_srv_inum) {
824 * Pass a NULL tcon to ensure we don't make a round
825 * trip to the server. This only works for SMB2+.
827 tmprc = server->ops->get_srv_inum(xid,
828 NULL, cifs_sb, full_path,
829 &fattr.cf_uniqueid, data);
831 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
833 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
836 /* query for SFU type info if supported and needed */
837 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
838 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
839 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
841 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
844 #ifdef CONFIG_CIFS_ACL
845 /* fill in 0777 bits from ACL */
846 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
847 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
849 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
854 #endif /* CONFIG_CIFS_ACL */
856 /* fill in remaining high mode bits e.g. SUID, VTX */
857 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
858 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
860 /* check for Minshall+French symlinks */
861 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
862 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
865 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
869 *inode = cifs_iget(sb, &fattr);
873 /* we already have inode, update it */
875 /* if uniqueid is different, return error */
876 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
877 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
882 /* if filetype is different, return error */
883 if (unlikely(((*inode)->i_mode & S_IFMT) !=
884 (fattr.cf_mode & S_IFMT))) {
889 cifs_fattr_to_inode(*inode, &fattr);
894 cifs_put_tlink(tlink);
898 static const struct inode_operations cifs_ipc_inode_ops = {
899 .lookup = cifs_lookup,
903 cifs_find_inode(struct inode *inode, void *opaque)
905 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
907 /* don't match inode with different uniqueid */
908 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
911 /* use createtime like an i_generation field */
912 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
915 /* don't match inode of different type */
916 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
919 /* if it's not a directory or has no dentries, then flag it */
920 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
921 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
927 cifs_init_inode(struct inode *inode, void *opaque)
929 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
931 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
932 CIFS_I(inode)->createtime = fattr->cf_createtime;
937 * walk dentry list for an inode and report whether it has aliases that
938 * are hashed. We use this to determine if a directory inode can actually
942 inode_has_hashed_dentries(struct inode *inode)
944 struct dentry *dentry;
946 spin_lock(&inode->i_lock);
947 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
948 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
949 spin_unlock(&inode->i_lock);
953 spin_unlock(&inode->i_lock);
957 /* Given fattrs, get a corresponding inode */
959 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
965 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
967 /* hash down to 32-bits on 32-bit arch */
968 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
970 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
972 /* was there a potentially problematic inode collision? */
973 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
974 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
976 if (inode_has_hashed_dentries(inode)) {
977 cifs_autodisable_serverino(CIFS_SB(sb));
979 fattr->cf_uniqueid = iunique(sb, ROOT_I);
980 goto retry_iget5_locked;
984 cifs_fattr_to_inode(inode, fattr);
985 if (sb->s_flags & MS_NOATIME)
986 inode->i_flags |= S_NOATIME | S_NOCMTIME;
987 if (inode->i_state & I_NEW) {
989 #ifdef CONFIG_CIFS_FSCACHE
990 /* initialize per-inode cache cookie pointer */
991 CIFS_I(inode)->fscache = NULL;
993 unlock_new_inode(inode);
1000 /* gets root inode */
1001 struct inode *cifs_root_iget(struct super_block *sb)
1004 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1005 struct inode *inode = NULL;
1007 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1011 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1012 && cifs_sb->prepath) {
1013 len = strlen(cifs_sb->prepath);
1014 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1016 return ERR_PTR(-ENOMEM);
1018 memcpy(path+1, cifs_sb->prepath, len);
1020 path = kstrdup("", GFP_KERNEL);
1022 return ERR_PTR(-ENOMEM);
1026 if (tcon->unix_ext) {
1027 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1028 /* some servers mistakenly claim POSIX support */
1029 if (rc != -EOPNOTSUPP)
1031 cifs_dbg(VFS, "server does not support POSIX extensions");
1032 tcon->unix_ext = false;
1035 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1036 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1040 inode = ERR_PTR(rc);
1044 #ifdef CONFIG_CIFS_FSCACHE
1045 /* populate tcon->resource_id */
1046 tcon->resource_id = CIFS_I(inode)->uniqueid;
1049 if (rc && tcon->ipc) {
1050 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1051 spin_lock(&inode->i_lock);
1052 inode->i_mode |= S_IFDIR;
1053 set_nlink(inode, 2);
1054 inode->i_op = &cifs_ipc_inode_ops;
1055 inode->i_fop = &simple_dir_operations;
1056 inode->i_uid = cifs_sb->mnt_uid;
1057 inode->i_gid = cifs_sb->mnt_gid;
1058 spin_unlock(&inode->i_lock);
1061 inode = ERR_PTR(rc);
1066 /* can not call macro free_xid here since in a void func
1067 * TODO: This is no longer true
1074 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1075 char *full_path, __u32 dosattr)
1077 bool set_time = false;
1078 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1079 struct TCP_Server_Info *server;
1080 FILE_BASIC_INFO info_buf;
1085 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1086 if (!server->ops->set_file_info)
1089 if (attrs->ia_valid & ATTR_ATIME) {
1091 info_buf.LastAccessTime =
1092 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1094 info_buf.LastAccessTime = 0;
1096 if (attrs->ia_valid & ATTR_MTIME) {
1098 info_buf.LastWriteTime =
1099 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1101 info_buf.LastWriteTime = 0;
1104 * Samba throws this field away, but windows may actually use it.
1105 * Do not set ctime unless other time stamps are changed explicitly
1106 * (i.e. by utimes()) since we would then have a mix of client and
1109 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1110 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1111 info_buf.ChangeTime =
1112 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1114 info_buf.ChangeTime = 0;
1116 info_buf.CreationTime = 0; /* don't change */
1117 info_buf.Attributes = cpu_to_le32(dosattr);
1119 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1123 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1124 * and rename it to a random name that hopefully won't conflict with
1128 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1129 const unsigned int xid)
1133 struct cifs_fid fid;
1134 struct cifs_open_parms oparms;
1135 struct inode *inode = d_inode(dentry);
1136 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1137 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1138 struct tcon_link *tlink;
1139 struct cifs_tcon *tcon;
1140 __u32 dosattr, origattr;
1141 FILE_BASIC_INFO *info_buf = NULL;
1143 tlink = cifs_sb_tlink(cifs_sb);
1145 return PTR_ERR(tlink);
1146 tcon = tlink_tcon(tlink);
1149 * We cannot rename the file if the server doesn't support
1150 * CAP_INFOLEVEL_PASSTHRU
1152 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1158 oparms.cifs_sb = cifs_sb;
1159 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1160 oparms.create_options = CREATE_NOT_DIR;
1161 oparms.disposition = FILE_OPEN;
1162 oparms.path = full_path;
1164 oparms.reconnect = false;
1166 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1170 origattr = cifsInode->cifsAttrs;
1172 origattr |= ATTR_NORMAL;
1174 dosattr = origattr & ~ATTR_READONLY;
1176 dosattr |= ATTR_NORMAL;
1177 dosattr |= ATTR_HIDDEN;
1179 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1180 if (dosattr != origattr) {
1181 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1182 if (info_buf == NULL) {
1186 info_buf->Attributes = cpu_to_le32(dosattr);
1187 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1189 /* although we would like to mark the file hidden
1190 if that fails we will still try to rename it */
1192 cifsInode->cifsAttrs = dosattr;
1194 dosattr = origattr; /* since not able to change them */
1197 /* rename the file */
1198 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1200 cifs_remap(cifs_sb));
1206 /* try to set DELETE_ON_CLOSE */
1207 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1208 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1211 * some samba versions return -ENOENT when we try to set the
1212 * file disposition here. Likely a samba bug, but work around
1213 * it for now. This means that some cifsXXX files may hang
1214 * around after they shouldn't.
1216 * BB: remove this hack after more servers have the fix
1224 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1228 CIFSSMBClose(xid, tcon, fid.netfid);
1231 cifs_put_tlink(tlink);
1235 * reset everything back to the original state. Don't bother
1236 * dealing with errors here since we can't do anything about
1240 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1241 cifs_sb->local_nls, cifs_remap(cifs_sb));
1243 if (dosattr != origattr) {
1244 info_buf->Attributes = cpu_to_le32(origattr);
1245 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1247 cifsInode->cifsAttrs = origattr;
1253 /* copied from fs/nfs/dir.c with small changes */
1255 cifs_drop_nlink(struct inode *inode)
1257 spin_lock(&inode->i_lock);
1258 if (inode->i_nlink > 0)
1260 spin_unlock(&inode->i_lock);
1264 * If d_inode(dentry) is null (usually meaning the cached dentry
1265 * is a negative dentry) then we would attempt a standard SMB delete, but
1266 * if that fails we can not attempt the fall back mechanisms on EACCESS
1267 * but will return the EACCESS to the caller. Note that the VFS does not call
1268 * unlink on negative dentries currently.
1270 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1274 char *full_path = NULL;
1275 struct inode *inode = d_inode(dentry);
1276 struct cifsInodeInfo *cifs_inode;
1277 struct super_block *sb = dir->i_sb;
1278 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1279 struct tcon_link *tlink;
1280 struct cifs_tcon *tcon;
1281 struct TCP_Server_Info *server;
1282 struct iattr *attrs = NULL;
1283 __u32 dosattr = 0, origattr = 0;
1285 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1287 tlink = cifs_sb_tlink(cifs_sb);
1289 return PTR_ERR(tlink);
1290 tcon = tlink_tcon(tlink);
1291 server = tcon->ses->server;
1295 /* Unlink can be called from rename so we can not take the
1296 * sb->s_vfs_rename_mutex here */
1297 full_path = build_path_from_dentry(dentry);
1298 if (full_path == NULL) {
1303 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1304 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1305 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1306 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1307 cifs_remap(cifs_sb));
1308 cifs_dbg(FYI, "posix del rc %d\n", rc);
1309 if ((rc == 0) || (rc == -ENOENT))
1310 goto psx_del_no_retry;
1314 if (!server->ops->unlink) {
1316 goto psx_del_no_retry;
1319 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1324 cifs_drop_nlink(inode);
1325 } else if (rc == -ENOENT) {
1327 } else if (rc == -EBUSY) {
1328 if (server->ops->rename_pending_delete) {
1329 rc = server->ops->rename_pending_delete(full_path,
1332 cifs_drop_nlink(inode);
1334 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1335 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1336 if (attrs == NULL) {
1341 /* try to reset dos attributes */
1342 cifs_inode = CIFS_I(inode);
1343 origattr = cifs_inode->cifsAttrs;
1345 origattr |= ATTR_NORMAL;
1346 dosattr = origattr & ~ATTR_READONLY;
1348 dosattr |= ATTR_NORMAL;
1349 dosattr |= ATTR_HIDDEN;
1351 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1355 goto retry_std_delete;
1358 /* undo the setattr if we errored out and it's needed */
1359 if (rc != 0 && dosattr != 0)
1360 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1364 cifs_inode = CIFS_I(inode);
1365 cifs_inode->time = 0; /* will force revalidate to get info
1367 inode->i_ctime = current_time(inode);
1369 dir->i_ctime = dir->i_mtime = current_time(dir);
1370 cifs_inode = CIFS_I(dir);
1371 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1376 cifs_put_tlink(tlink);
1381 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1382 const char *full_path, struct cifs_sb_info *cifs_sb,
1383 struct cifs_tcon *tcon, const unsigned int xid)
1386 struct inode *inode = NULL;
1389 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1392 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1399 * setting nlink not necessary except in cases where we failed to get it
1400 * from the server or was set bogus. Also, since this is a brand new
1401 * inode, no need to grab the i_lock before setting the i_nlink.
1403 if (inode->i_nlink < 2)
1404 set_nlink(inode, 2);
1405 mode &= ~current_umask();
1406 /* must turn on setgid bit if parent dir has it */
1407 if (parent->i_mode & S_ISGID)
1410 if (tcon->unix_ext) {
1411 struct cifs_unix_set_info_args args = {
1413 .ctime = NO_CHANGE_64,
1414 .atime = NO_CHANGE_64,
1415 .mtime = NO_CHANGE_64,
1418 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1419 args.uid = current_fsuid();
1420 if (parent->i_mode & S_ISGID)
1421 args.gid = parent->i_gid;
1423 args.gid = current_fsgid();
1425 args.uid = INVALID_UID; /* no change */
1426 args.gid = INVALID_GID; /* no change */
1428 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1430 cifs_remap(cifs_sb));
1432 struct TCP_Server_Info *server = tcon->ses->server;
1433 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1434 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1435 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1437 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1438 inode->i_mode = (mode | S_IFDIR);
1440 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1441 inode->i_uid = current_fsuid();
1442 if (inode->i_mode & S_ISGID)
1443 inode->i_gid = parent->i_gid;
1445 inode->i_gid = current_fsgid();
1448 d_instantiate(dentry, inode);
1453 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1454 const char *full_path, struct cifs_sb_info *cifs_sb,
1455 struct cifs_tcon *tcon, const unsigned int xid)
1459 FILE_UNIX_BASIC_INFO *info = NULL;
1460 struct inode *newinode = NULL;
1461 struct cifs_fattr fattr;
1463 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1466 goto posix_mkdir_out;
1469 mode &= ~current_umask();
1470 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1471 NULL /* netfid */, info, &oplock, full_path,
1472 cifs_sb->local_nls, cifs_remap(cifs_sb));
1473 if (rc == -EOPNOTSUPP)
1474 goto posix_mkdir_out;
1476 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1478 goto posix_mkdir_out;
1481 if (info->Type == cpu_to_le32(-1))
1482 /* no return info, go query for it */
1483 goto posix_mkdir_get_info;
1485 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1486 * need to set uid/gid.
1489 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1490 cifs_fill_uniqueid(inode->i_sb, &fattr);
1491 newinode = cifs_iget(inode->i_sb, &fattr);
1493 goto posix_mkdir_get_info;
1495 d_instantiate(dentry, newinode);
1497 #ifdef CONFIG_CIFS_DEBUG2
1498 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1499 dentry, dentry, newinode);
1501 if (newinode->i_nlink != 2)
1502 cifs_dbg(FYI, "unexpected number of links %d\n",
1509 posix_mkdir_get_info:
1510 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1512 goto posix_mkdir_out;
1515 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1519 struct cifs_sb_info *cifs_sb;
1520 struct tcon_link *tlink;
1521 struct cifs_tcon *tcon;
1522 struct TCP_Server_Info *server;
1525 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1528 cifs_sb = CIFS_SB(inode->i_sb);
1529 tlink = cifs_sb_tlink(cifs_sb);
1531 return PTR_ERR(tlink);
1532 tcon = tlink_tcon(tlink);
1536 full_path = build_path_from_dentry(direntry);
1537 if (full_path == NULL) {
1542 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1543 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1544 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1546 if (rc != -EOPNOTSUPP)
1550 server = tcon->ses->server;
1552 if (!server->ops->mkdir) {
1557 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1558 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1560 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1565 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1569 * Force revalidate to get parent dir info when needed since cached
1570 * attributes are invalid now.
1572 CIFS_I(inode)->time = 0;
1575 cifs_put_tlink(tlink);
1579 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1583 struct cifs_sb_info *cifs_sb;
1584 struct tcon_link *tlink;
1585 struct cifs_tcon *tcon;
1586 struct TCP_Server_Info *server;
1587 char *full_path = NULL;
1588 struct cifsInodeInfo *cifsInode;
1590 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1594 full_path = build_path_from_dentry(direntry);
1595 if (full_path == NULL) {
1600 cifs_sb = CIFS_SB(inode->i_sb);
1601 tlink = cifs_sb_tlink(cifs_sb);
1602 if (IS_ERR(tlink)) {
1603 rc = PTR_ERR(tlink);
1606 tcon = tlink_tcon(tlink);
1607 server = tcon->ses->server;
1609 if (!server->ops->rmdir) {
1611 cifs_put_tlink(tlink);
1615 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1616 cifs_put_tlink(tlink);
1619 spin_lock(&d_inode(direntry)->i_lock);
1620 i_size_write(d_inode(direntry), 0);
1621 clear_nlink(d_inode(direntry));
1622 spin_unlock(&d_inode(direntry)->i_lock);
1625 cifsInode = CIFS_I(d_inode(direntry));
1626 /* force revalidate to go get info when needed */
1627 cifsInode->time = 0;
1629 cifsInode = CIFS_I(inode);
1631 * Force revalidate to get parent dir info when needed since cached
1632 * attributes are invalid now.
1634 cifsInode->time = 0;
1636 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1637 current_time(inode);
1646 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1647 const char *from_path, struct dentry *to_dentry,
1648 const char *to_path)
1650 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1651 struct tcon_link *tlink;
1652 struct cifs_tcon *tcon;
1653 struct TCP_Server_Info *server;
1654 struct cifs_fid fid;
1655 struct cifs_open_parms oparms;
1658 tlink = cifs_sb_tlink(cifs_sb);
1660 return PTR_ERR(tlink);
1661 tcon = tlink_tcon(tlink);
1662 server = tcon->ses->server;
1664 if (!server->ops->rename)
1667 /* try path-based rename first */
1668 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
1671 * Don't bother with rename by filehandle unless file is busy and
1672 * source. Note that cross directory moves do not work with
1673 * rename by filehandle to various Windows servers.
1675 if (rc == 0 || rc != -EBUSY)
1676 goto do_rename_exit;
1678 /* open-file renames don't work across directories */
1679 if (to_dentry->d_parent != from_dentry->d_parent)
1680 goto do_rename_exit;
1683 oparms.cifs_sb = cifs_sb;
1684 /* open the file to be renamed -- we need DELETE perms */
1685 oparms.desired_access = DELETE;
1686 oparms.create_options = CREATE_NOT_DIR;
1687 oparms.disposition = FILE_OPEN;
1688 oparms.path = from_path;
1690 oparms.reconnect = false;
1692 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1694 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
1695 (const char *) to_dentry->d_name.name,
1696 cifs_sb->local_nls, cifs_remap(cifs_sb));
1697 CIFSSMBClose(xid, tcon, fid.netfid);
1700 cifs_put_tlink(tlink);
1705 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1706 struct inode *target_dir, struct dentry *target_dentry,
1709 char *from_name = NULL;
1710 char *to_name = NULL;
1711 struct cifs_sb_info *cifs_sb;
1712 struct tcon_link *tlink;
1713 struct cifs_tcon *tcon;
1714 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1715 FILE_UNIX_BASIC_INFO *info_buf_target;
1719 if (flags & ~RENAME_NOREPLACE)
1722 cifs_sb = CIFS_SB(source_dir->i_sb);
1723 tlink = cifs_sb_tlink(cifs_sb);
1725 return PTR_ERR(tlink);
1726 tcon = tlink_tcon(tlink);
1731 * we already have the rename sem so we do not need to
1732 * grab it again here to protect the path integrity
1734 from_name = build_path_from_dentry(source_dentry);
1735 if (from_name == NULL) {
1737 goto cifs_rename_exit;
1740 to_name = build_path_from_dentry(target_dentry);
1741 if (to_name == NULL) {
1743 goto cifs_rename_exit;
1746 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1750 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1752 if (flags & RENAME_NOREPLACE)
1753 goto cifs_rename_exit;
1755 if (rc == -EEXIST && tcon->unix_ext) {
1757 * Are src and dst hardlinks of same inode? We can only tell
1758 * with unix extensions enabled.
1761 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1763 if (info_buf_source == NULL) {
1765 goto cifs_rename_exit;
1768 info_buf_target = info_buf_source + 1;
1769 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1772 cifs_remap(cifs_sb));
1776 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1779 cifs_remap(cifs_sb));
1781 if (tmprc == 0 && (info_buf_source->UniqueId ==
1782 info_buf_target->UniqueId)) {
1783 /* same file, POSIX says that this is a noop */
1785 goto cifs_rename_exit;
1789 * else ... BB we could add the same check for Windows by
1790 * checking the UniqueId via FILE_INTERNAL_INFO
1794 /* Try unlinking the target dentry if it's not negative */
1795 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
1796 if (d_is_dir(target_dentry))
1797 tmprc = cifs_rmdir(target_dir, target_dentry);
1799 tmprc = cifs_unlink(target_dir, target_dentry);
1801 goto cifs_rename_exit;
1802 rc = cifs_do_rename(xid, source_dentry, from_name,
1803 target_dentry, to_name);
1806 /* force revalidate to go get info when needed */
1807 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1809 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1810 target_dir->i_mtime = current_time(source_dir);
1813 kfree(info_buf_source);
1817 cifs_put_tlink(tlink);
1822 cifs_inode_needs_reval(struct inode *inode)
1824 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1825 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1827 if (CIFS_CACHE_READ(cifs_i))
1830 if (!lookupCacheEnabled)
1833 if (cifs_i->time == 0)
1836 if (!cifs_sb->actimeo)
1839 if (!time_in_range(jiffies, cifs_i->time,
1840 cifs_i->time + cifs_sb->actimeo))
1843 /* hardlinked files w/ noserverino get "special" treatment */
1844 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1845 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1852 * Zap the cache. Called when invalid_mapping flag is set.
1855 cifs_invalidate_mapping(struct inode *inode)
1859 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1860 rc = invalidate_inode_pages2(inode->i_mapping);
1862 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1866 cifs_fscache_reset_inode_cookie(inode);
1871 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1872 * @word: long word containing the bit lock
1875 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
1877 freezable_schedule_unsafe();
1878 if (signal_pending_state(mode, current))
1879 return -ERESTARTSYS;
1884 cifs_revalidate_mapping(struct inode *inode)
1887 unsigned long *flags = &CIFS_I(inode)->flags;
1889 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1894 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1895 rc = cifs_invalidate_mapping(inode);
1897 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1900 clear_bit_unlock(CIFS_INO_LOCK, flags);
1901 smp_mb__after_atomic();
1902 wake_up_bit(flags, CIFS_INO_LOCK);
1908 cifs_zap_mapping(struct inode *inode)
1910 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1911 return cifs_revalidate_mapping(inode);
1914 int cifs_revalidate_file_attr(struct file *filp)
1917 struct inode *inode = file_inode(filp);
1918 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1920 if (!cifs_inode_needs_reval(inode))
1923 if (tlink_tcon(cfile->tlink)->unix_ext)
1924 rc = cifs_get_file_info_unix(filp);
1926 rc = cifs_get_file_info(filp);
1931 int cifs_revalidate_dentry_attr(struct dentry *dentry)
1935 struct inode *inode = d_inode(dentry);
1936 struct super_block *sb = dentry->d_sb;
1937 char *full_path = NULL;
1942 if (!cifs_inode_needs_reval(inode))
1947 /* can not safely grab the rename sem here if rename calls revalidate
1948 since that would deadlock */
1949 full_path = build_path_from_dentry(dentry);
1950 if (full_path == NULL) {
1955 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1956 full_path, inode, inode->i_count.counter,
1957 dentry, cifs_get_time(dentry), jiffies);
1959 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
1960 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1962 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1971 int cifs_revalidate_file(struct file *filp)
1974 struct inode *inode = file_inode(filp);
1976 rc = cifs_revalidate_file_attr(filp);
1980 return cifs_revalidate_mapping(inode);
1983 /* revalidate a dentry's inode attributes */
1984 int cifs_revalidate_dentry(struct dentry *dentry)
1987 struct inode *inode = d_inode(dentry);
1989 rc = cifs_revalidate_dentry_attr(dentry);
1993 return cifs_revalidate_mapping(inode);
1996 int cifs_getattr(const struct path *path, struct kstat *stat,
1997 u32 request_mask, unsigned int flags)
1999 struct dentry *dentry = path->dentry;
2000 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2001 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2002 struct inode *inode = d_inode(dentry);
2006 * We need to be sure that all dirty pages are written and the server
2007 * has actual ctime, mtime and file length.
2009 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2010 inode->i_mapping->nrpages != 0) {
2011 rc = filemap_fdatawait(inode->i_mapping);
2013 mapping_set_error(inode->i_mapping, rc);
2018 rc = cifs_revalidate_dentry_attr(dentry);
2022 generic_fillattr(inode, stat);
2023 stat->blksize = CIFS_MAX_MSGSIZE;
2024 stat->ino = CIFS_I(inode)->uniqueid;
2027 * If on a multiuser mount without unix extensions or cifsacl being
2028 * enabled, and the admin hasn't overridden them, set the ownership
2029 * to the fsuid/fsgid of the current process.
2031 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2032 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2034 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2035 stat->uid = current_fsuid();
2036 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2037 stat->gid = current_fsgid();
2042 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2044 pgoff_t index = from >> PAGE_SHIFT;
2045 unsigned offset = from & (PAGE_SIZE - 1);
2049 page = grab_cache_page(mapping, index);
2053 zero_user_segment(page, offset, PAGE_SIZE);
2059 static void cifs_setsize(struct inode *inode, loff_t offset)
2061 spin_lock(&inode->i_lock);
2062 i_size_write(inode, offset);
2063 spin_unlock(&inode->i_lock);
2065 truncate_pagecache(inode, offset);
2069 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2070 unsigned int xid, char *full_path)
2073 struct cifsFileInfo *open_file;
2074 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2075 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2076 struct tcon_link *tlink = NULL;
2077 struct cifs_tcon *tcon = NULL;
2078 struct TCP_Server_Info *server;
2081 * To avoid spurious oplock breaks from server, in the case of
2082 * inodes that we already have open, avoid doing path based
2083 * setting of file size if we can do it by handle.
2084 * This keeps our caching token (oplock) and avoids timeouts
2085 * when the local oplock break takes longer to flush
2086 * writebehind data than the SMB timeout for the SetPathInfo
2087 * request would allow
2089 open_file = find_writable_file(cifsInode, true);
2091 tcon = tlink_tcon(open_file->tlink);
2092 server = tcon->ses->server;
2093 if (server->ops->set_file_size)
2094 rc = server->ops->set_file_size(xid, tcon, open_file,
2095 attrs->ia_size, false);
2098 cifsFileInfo_put(open_file);
2099 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2107 tlink = cifs_sb_tlink(cifs_sb);
2109 return PTR_ERR(tlink);
2110 tcon = tlink_tcon(tlink);
2111 server = tcon->ses->server;
2115 * Set file size by pathname rather than by handle either because no
2116 * valid, writeable file handle for it was found or because there was
2117 * an error setting it by handle.
2119 if (server->ops->set_path_size)
2120 rc = server->ops->set_path_size(xid, tcon, full_path,
2121 attrs->ia_size, cifs_sb, false);
2124 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2127 cifs_put_tlink(tlink);
2131 cifsInode->server_eof = attrs->ia_size;
2132 cifs_setsize(inode, attrs->ia_size);
2133 cifs_truncate_page(inode->i_mapping, inode->i_size);
2140 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2144 char *full_path = NULL;
2145 struct inode *inode = d_inode(direntry);
2146 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2147 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2148 struct tcon_link *tlink;
2149 struct cifs_tcon *pTcon;
2150 struct cifs_unix_set_info_args *args = NULL;
2151 struct cifsFileInfo *open_file;
2153 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2154 direntry, attrs->ia_valid);
2158 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2159 attrs->ia_valid |= ATTR_FORCE;
2161 rc = setattr_prepare(direntry, attrs);
2165 full_path = build_path_from_dentry(direntry);
2166 if (full_path == NULL) {
2172 * Attempt to flush data before changing attributes. We need to do
2173 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2174 * ownership or mode then we may also need to do this. Here, we take
2175 * the safe way out and just do the flush on all setattr requests. If
2176 * the flush returns error, store it to report later and continue.
2178 * BB: This should be smarter. Why bother flushing pages that
2179 * will be truncated anyway? Also, should we error out here if
2180 * the flush returns error?
2182 rc = filemap_write_and_wait(inode->i_mapping);
2183 mapping_set_error(inode->i_mapping, rc);
2186 if (attrs->ia_valid & ATTR_SIZE) {
2187 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2192 /* skip mode change if it's just for clearing setuid/setgid */
2193 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2194 attrs->ia_valid &= ~ATTR_MODE;
2196 args = kmalloc(sizeof(*args), GFP_KERNEL);
2202 /* set up the struct */
2203 if (attrs->ia_valid & ATTR_MODE)
2204 args->mode = attrs->ia_mode;
2206 args->mode = NO_CHANGE_64;
2208 if (attrs->ia_valid & ATTR_UID)
2209 args->uid = attrs->ia_uid;
2211 args->uid = INVALID_UID; /* no change */
2213 if (attrs->ia_valid & ATTR_GID)
2214 args->gid = attrs->ia_gid;
2216 args->gid = INVALID_GID; /* no change */
2218 if (attrs->ia_valid & ATTR_ATIME)
2219 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2221 args->atime = NO_CHANGE_64;
2223 if (attrs->ia_valid & ATTR_MTIME)
2224 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2226 args->mtime = NO_CHANGE_64;
2228 if (attrs->ia_valid & ATTR_CTIME)
2229 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2231 args->ctime = NO_CHANGE_64;
2234 open_file = find_writable_file(cifsInode, true);
2236 u16 nfid = open_file->fid.netfid;
2237 u32 npid = open_file->pid;
2238 pTcon = tlink_tcon(open_file->tlink);
2239 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2240 cifsFileInfo_put(open_file);
2242 tlink = cifs_sb_tlink(cifs_sb);
2243 if (IS_ERR(tlink)) {
2244 rc = PTR_ERR(tlink);
2247 pTcon = tlink_tcon(tlink);
2248 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2250 cifs_remap(cifs_sb));
2251 cifs_put_tlink(tlink);
2257 if ((attrs->ia_valid & ATTR_SIZE) &&
2258 attrs->ia_size != i_size_read(inode))
2259 truncate_setsize(inode, attrs->ia_size);
2261 setattr_copy(inode, attrs);
2262 mark_inode_dirty(inode);
2264 /* force revalidate when any of these times are set since some
2265 of the fs types (eg ext3, fat) do not have fine enough
2266 time granularity to match protocol, and we do not have a
2267 a way (yet) to query the server fs's time granularity (and
2268 whether it rounds times down).
2270 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2271 cifsInode->time = 0;
2280 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2283 kuid_t uid = INVALID_UID;
2284 kgid_t gid = INVALID_GID;
2285 struct inode *inode = d_inode(direntry);
2286 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2287 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2288 char *full_path = NULL;
2291 __u64 mode = NO_CHANGE_64;
2295 cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
2296 direntry, attrs->ia_valid);
2298 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2299 attrs->ia_valid |= ATTR_FORCE;
2301 rc = setattr_prepare(direntry, attrs);
2307 full_path = build_path_from_dentry(direntry);
2308 if (full_path == NULL) {
2315 * Attempt to flush data before changing attributes. We need to do
2316 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2317 * ownership or mode then we may also need to do this. Here, we take
2318 * the safe way out and just do the flush on all setattr requests. If
2319 * the flush returns error, store it to report later and continue.
2321 * BB: This should be smarter. Why bother flushing pages that
2322 * will be truncated anyway? Also, should we error out here if
2323 * the flush returns error?
2325 rc = filemap_write_and_wait(inode->i_mapping);
2326 mapping_set_error(inode->i_mapping, rc);
2329 if (attrs->ia_valid & ATTR_SIZE) {
2330 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2332 goto cifs_setattr_exit;
2335 if (attrs->ia_valid & ATTR_UID)
2336 uid = attrs->ia_uid;
2338 if (attrs->ia_valid & ATTR_GID)
2339 gid = attrs->ia_gid;
2341 #ifdef CONFIG_CIFS_ACL
2342 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2343 if (uid_valid(uid) || gid_valid(gid)) {
2344 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2347 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2349 goto cifs_setattr_exit;
2353 #endif /* CONFIG_CIFS_ACL */
2354 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2355 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2357 /* skip mode change if it's just for clearing setuid/setgid */
2358 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2359 attrs->ia_valid &= ~ATTR_MODE;
2361 if (attrs->ia_valid & ATTR_MODE) {
2362 mode = attrs->ia_mode;
2364 #ifdef CONFIG_CIFS_ACL
2365 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2366 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2367 INVALID_UID, INVALID_GID);
2369 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2371 goto cifs_setattr_exit;
2374 #endif /* CONFIG_CIFS_ACL */
2375 if (((mode & S_IWUGO) == 0) &&
2376 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2378 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2380 /* fix up mode if we're not using dynperm */
2381 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2382 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2383 } else if ((mode & S_IWUGO) &&
2384 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2386 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2387 /* Attributes of 0 are ignored */
2389 dosattr |= ATTR_NORMAL;
2391 /* reset local inode permissions to normal */
2392 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2393 attrs->ia_mode &= ~(S_IALLUGO);
2394 if (S_ISDIR(inode->i_mode))
2396 cifs_sb->mnt_dir_mode;
2399 cifs_sb->mnt_file_mode;
2401 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2402 /* ignore mode change - ATTR_READONLY hasn't changed */
2403 attrs->ia_valid &= ~ATTR_MODE;
2407 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2408 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2409 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2410 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2412 /* Even if error on time set, no sense failing the call if
2413 the server would set the time to a reasonable value anyway,
2414 and this check ensures that we are not being called from
2415 sys_utimes in which case we ought to fail the call back to
2416 the user when the server rejects the call */
2417 if ((rc) && (attrs->ia_valid &
2418 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2422 /* do not need local check to inode_check_ok since the server does
2425 goto cifs_setattr_exit;
2427 if ((attrs->ia_valid & ATTR_SIZE) &&
2428 attrs->ia_size != i_size_read(inode))
2429 truncate_setsize(inode, attrs->ia_size);
2431 setattr_copy(inode, attrs);
2432 mark_inode_dirty(inode);
2441 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2443 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2444 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2446 if (pTcon->unix_ext)
2447 return cifs_setattr_unix(direntry, attrs);
2449 return cifs_setattr_nounix(direntry, attrs);
2451 /* BB: add cifs_setattr_legacy for really old servers */
2455 void cifs_delete_inode(struct inode *inode)
2457 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2458 /* may have to add back in if and when safe distributed caching of
2459 directories added e.g. via FindNotify */