4 * Copyright (C) International Business Machines Corp., 2002,2010
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 fattr->cf_atime = CURRENT_TIME;
326 fattr->cf_ctime = CURRENT_TIME;
327 fattr->cf_mtime = CURRENT_TIME;
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_remap(cifs_sb));
568 cifs_put_tlink(tlink);
572 mode = le32_to_cpu(*((__le32 *)ea_value));
573 fattr->cf_mode &= ~SFBITS_MASK;
574 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
575 mode, fattr->cf_mode);
576 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
577 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
586 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
588 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
589 struct cifs_sb_info *cifs_sb, bool adjust_tz,
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 fattr->cf_atime = CURRENT_TIME;
604 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
605 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
608 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
609 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
612 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
613 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
614 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
616 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
619 fattr->cf_mode = S_IFLNK;
620 fattr->cf_dtype = DT_LNK;
621 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
622 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
623 fattr->cf_dtype = DT_DIR;
625 * Server can return wrong NumberOfLinks value for directories
626 * when Unix extensions are disabled - fake it.
629 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
631 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
632 fattr->cf_dtype = DT_REG;
634 /* clear write bits if ATTR_READONLY is set */
635 if (fattr->cf_cifsattrs & ATTR_READONLY)
636 fattr->cf_mode &= ~(S_IWUGO);
639 * Don't accept zero nlink from non-unix servers unless
640 * delete is pending. Instead mark it as unknown.
642 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
643 !info->DeletePending) {
644 cifs_dbg(1, "bogus file nlink value %u\n",
646 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
650 fattr->cf_uid = cifs_sb->mnt_uid;
651 fattr->cf_gid = cifs_sb->mnt_gid;
655 cifs_get_file_info(struct file *filp)
659 FILE_ALL_INFO find_data;
660 struct cifs_fattr fattr;
661 struct inode *inode = file_inode(filp);
662 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
663 struct cifsFileInfo *cfile = filp->private_data;
664 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
665 struct TCP_Server_Info *server = tcon->ses->server;
667 if (!server->ops->query_file_info)
671 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
674 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false,
678 cifs_create_dfs_fattr(&fattr, inode->i_sb);
684 * FIXME: legacy server -- fall back to path-based call?
685 * for now, just skip revalidating and mark inode for
689 CIFS_I(inode)->time = 0;
695 * don't bother with SFU junk here -- just mark inode as needing
698 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
699 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
700 cifs_fattr_to_inode(inode, &fattr);
707 cifs_get_inode_info(struct inode **inode, const char *full_path,
708 FILE_ALL_INFO *data, struct super_block *sb, int xid,
709 const struct cifs_fid *fid)
711 bool validinum = false;
713 int rc = 0, tmprc = ENOSYS;
714 struct cifs_tcon *tcon;
715 struct TCP_Server_Info *server;
716 struct tcon_link *tlink;
717 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
719 bool adjust_tz = false;
720 struct cifs_fattr fattr;
721 struct cifs_search_info *srchinf = NULL;
722 bool symlink = false;
724 tlink = cifs_sb_tlink(cifs_sb);
726 return PTR_ERR(tlink);
727 tcon = tlink_tcon(tlink);
728 server = tcon->ses->server;
730 cifs_dbg(FYI, "Getting info on %s\n", full_path);
732 if ((data == NULL) && (*inode != NULL)) {
733 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
734 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
739 /* if inode info is not passed, get it from server */
741 if (!server->ops->query_path_info) {
745 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
750 data = (FILE_ALL_INFO *)buf;
751 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
752 data, &adjust_tz, &symlink);
756 cifs_all_info_to_fattr(&fattr, data, cifs_sb, adjust_tz,
758 } else if (rc == -EREMOTE) {
759 cifs_create_dfs_fattr(&fattr, sb);
761 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
762 srchinf = kzalloc(sizeof(struct cifs_search_info),
764 if (srchinf == NULL) {
769 srchinf->endOfSearch = false;
770 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
772 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
773 CIFS_SEARCH_CLOSE_AT_END |
774 CIFS_SEARCH_BACKUP_SEARCH;
776 rc = CIFSFindFirst(xid, tcon, full_path,
777 cifs_sb, NULL, srchflgs, srchinf, false);
780 (FILE_ALL_INFO *)srchinf->srch_entries_start;
782 cifs_dir_info_to_fattr(&fattr,
783 (FILE_DIRECTORY_INFO *)data, cifs_sb);
784 fattr.cf_uniqueid = le64_to_cpu(
785 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
788 cifs_buf_release(srchinf->ntwrk_buf_start);
797 * If an inode wasn't passed in, then get the inode number
799 * Is an i_ino of zero legal? Can we use that to check if the server
800 * supports returning inode numbers? Are there other sanity checks we
801 * can use to ensure that the server is really filling in that field?
803 if (*inode == NULL) {
804 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
805 if (validinum == false) {
806 if (server->ops->get_srv_inum)
807 tmprc = server->ops->get_srv_inum(xid,
808 tcon, cifs_sb, full_path,
809 &fattr.cf_uniqueid, data);
811 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
813 fattr.cf_uniqueid = iunique(sb, ROOT_I);
814 cifs_autodisable_serverino(cifs_sb);
818 fattr.cf_uniqueid = iunique(sb, ROOT_I);
820 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
821 validinum == false && server->ops->get_srv_inum) {
823 * Pass a NULL tcon to ensure we don't make a round
824 * trip to the server. This only works for SMB2+.
826 tmprc = server->ops->get_srv_inum(xid,
827 NULL, cifs_sb, full_path,
828 &fattr.cf_uniqueid, data);
830 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
832 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
835 /* query for SFU type info if supported and needed */
836 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
837 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
838 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
840 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
843 #ifdef CONFIG_CIFS_ACL
844 /* fill in 0777 bits from ACL */
845 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
846 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
848 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
853 #endif /* CONFIG_CIFS_ACL */
855 /* fill in remaining high mode bits e.g. SUID, VTX */
856 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
857 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
859 /* check for Minshall+French symlinks */
860 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
861 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
864 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
868 *inode = cifs_iget(sb, &fattr);
872 /* we already have inode, update it */
874 /* if uniqueid is different, return error */
875 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
876 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
881 /* if filetype is different, return error */
882 if (unlikely(((*inode)->i_mode & S_IFMT) !=
883 (fattr.cf_mode & S_IFMT))) {
888 cifs_fattr_to_inode(*inode, &fattr);
893 cifs_put_tlink(tlink);
897 static const struct inode_operations cifs_ipc_inode_ops = {
898 .lookup = cifs_lookup,
902 cifs_find_inode(struct inode *inode, void *opaque)
904 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
906 /* don't match inode with different uniqueid */
907 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
910 /* use createtime like an i_generation field */
911 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
914 /* don't match inode of different type */
915 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
918 /* if it's not a directory or has no dentries, then flag it */
919 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
920 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
926 cifs_init_inode(struct inode *inode, void *opaque)
928 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
930 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
931 CIFS_I(inode)->createtime = fattr->cf_createtime;
936 * walk dentry list for an inode and report whether it has aliases that
937 * are hashed. We use this to determine if a directory inode can actually
941 inode_has_hashed_dentries(struct inode *inode)
943 struct dentry *dentry;
945 spin_lock(&inode->i_lock);
946 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
947 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
948 spin_unlock(&inode->i_lock);
952 spin_unlock(&inode->i_lock);
956 /* Given fattrs, get a corresponding inode */
958 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
964 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
966 /* hash down to 32-bits on 32-bit arch */
967 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
969 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
971 /* was there a potentially problematic inode collision? */
972 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
973 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
975 if (inode_has_hashed_dentries(inode)) {
976 cifs_autodisable_serverino(CIFS_SB(sb));
978 fattr->cf_uniqueid = iunique(sb, ROOT_I);
979 goto retry_iget5_locked;
983 cifs_fattr_to_inode(inode, fattr);
984 if (sb->s_flags & MS_NOATIME)
985 inode->i_flags |= S_NOATIME | S_NOCMTIME;
986 if (inode->i_state & I_NEW) {
988 #ifdef CONFIG_CIFS_FSCACHE
989 /* initialize per-inode cache cookie pointer */
990 CIFS_I(inode)->fscache = NULL;
992 unlock_new_inode(inode);
999 /* gets root inode */
1000 struct inode *cifs_root_iget(struct super_block *sb)
1003 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1004 struct inode *inode = NULL;
1006 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1010 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1011 && cifs_sb->prepath) {
1012 len = strlen(cifs_sb->prepath);
1013 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1015 return ERR_PTR(-ENOMEM);
1017 memcpy(path+1, cifs_sb->prepath, len);
1019 path = kstrdup("", GFP_KERNEL);
1021 return ERR_PTR(-ENOMEM);
1025 if (tcon->unix_ext) {
1026 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1027 /* some servers mistakenly claim POSIX support */
1028 if (rc != -EOPNOTSUPP)
1030 cifs_dbg(VFS, "server does not support POSIX extensions");
1031 tcon->unix_ext = false;
1034 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1035 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1039 inode = ERR_PTR(rc);
1043 #ifdef CONFIG_CIFS_FSCACHE
1044 /* populate tcon->resource_id */
1045 tcon->resource_id = CIFS_I(inode)->uniqueid;
1048 if (rc && tcon->ipc) {
1049 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1050 spin_lock(&inode->i_lock);
1051 inode->i_mode |= S_IFDIR;
1052 set_nlink(inode, 2);
1053 inode->i_op = &cifs_ipc_inode_ops;
1054 inode->i_fop = &simple_dir_operations;
1055 inode->i_uid = cifs_sb->mnt_uid;
1056 inode->i_gid = cifs_sb->mnt_gid;
1057 spin_unlock(&inode->i_lock);
1060 inode = ERR_PTR(rc);
1065 /* can not call macro free_xid here since in a void func
1066 * TODO: This is no longer true
1073 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1074 char *full_path, __u32 dosattr)
1076 bool set_time = false;
1077 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1078 struct TCP_Server_Info *server;
1079 FILE_BASIC_INFO info_buf;
1084 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1085 if (!server->ops->set_file_info)
1088 if (attrs->ia_valid & ATTR_ATIME) {
1090 info_buf.LastAccessTime =
1091 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1093 info_buf.LastAccessTime = 0;
1095 if (attrs->ia_valid & ATTR_MTIME) {
1097 info_buf.LastWriteTime =
1098 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1100 info_buf.LastWriteTime = 0;
1103 * Samba throws this field away, but windows may actually use it.
1104 * Do not set ctime unless other time stamps are changed explicitly
1105 * (i.e. by utimes()) since we would then have a mix of client and
1108 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1109 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1110 info_buf.ChangeTime =
1111 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1113 info_buf.ChangeTime = 0;
1115 info_buf.CreationTime = 0; /* don't change */
1116 info_buf.Attributes = cpu_to_le32(dosattr);
1118 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1122 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1123 * and rename it to a random name that hopefully won't conflict with
1127 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1128 const unsigned int xid)
1132 struct cifs_fid fid;
1133 struct cifs_open_parms oparms;
1134 struct inode *inode = d_inode(dentry);
1135 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1136 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1137 struct tcon_link *tlink;
1138 struct cifs_tcon *tcon;
1139 __u32 dosattr, origattr;
1140 FILE_BASIC_INFO *info_buf = NULL;
1142 tlink = cifs_sb_tlink(cifs_sb);
1144 return PTR_ERR(tlink);
1145 tcon = tlink_tcon(tlink);
1148 * We cannot rename the file if the server doesn't support
1149 * CAP_INFOLEVEL_PASSTHRU
1151 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1157 oparms.cifs_sb = cifs_sb;
1158 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1159 oparms.create_options = CREATE_NOT_DIR;
1160 oparms.disposition = FILE_OPEN;
1161 oparms.path = full_path;
1163 oparms.reconnect = false;
1165 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1169 origattr = cifsInode->cifsAttrs;
1171 origattr |= ATTR_NORMAL;
1173 dosattr = origattr & ~ATTR_READONLY;
1175 dosattr |= ATTR_NORMAL;
1176 dosattr |= ATTR_HIDDEN;
1178 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1179 if (dosattr != origattr) {
1180 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1181 if (info_buf == NULL) {
1185 info_buf->Attributes = cpu_to_le32(dosattr);
1186 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1188 /* although we would like to mark the file hidden
1189 if that fails we will still try to rename it */
1191 cifsInode->cifsAttrs = dosattr;
1193 dosattr = origattr; /* since not able to change them */
1196 /* rename the file */
1197 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1199 cifs_remap(cifs_sb));
1205 /* try to set DELETE_ON_CLOSE */
1206 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1207 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1210 * some samba versions return -ENOENT when we try to set the
1211 * file disposition here. Likely a samba bug, but work around
1212 * it for now. This means that some cifsXXX files may hang
1213 * around after they shouldn't.
1215 * BB: remove this hack after more servers have the fix
1223 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1227 CIFSSMBClose(xid, tcon, fid.netfid);
1230 cifs_put_tlink(tlink);
1234 * reset everything back to the original state. Don't bother
1235 * dealing with errors here since we can't do anything about
1239 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1240 cifs_sb->local_nls, cifs_remap(cifs_sb));
1242 if (dosattr != origattr) {
1243 info_buf->Attributes = cpu_to_le32(origattr);
1244 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1246 cifsInode->cifsAttrs = origattr;
1252 /* copied from fs/nfs/dir.c with small changes */
1254 cifs_drop_nlink(struct inode *inode)
1256 spin_lock(&inode->i_lock);
1257 if (inode->i_nlink > 0)
1259 spin_unlock(&inode->i_lock);
1263 * If d_inode(dentry) is null (usually meaning the cached dentry
1264 * is a negative dentry) then we would attempt a standard SMB delete, but
1265 * if that fails we can not attempt the fall back mechanisms on EACCESS
1266 * but will return the EACCESS to the caller. Note that the VFS does not call
1267 * unlink on negative dentries currently.
1269 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1273 char *full_path = NULL;
1274 struct inode *inode = d_inode(dentry);
1275 struct cifsInodeInfo *cifs_inode;
1276 struct super_block *sb = dir->i_sb;
1277 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1278 struct tcon_link *tlink;
1279 struct cifs_tcon *tcon;
1280 struct TCP_Server_Info *server;
1281 struct iattr *attrs = NULL;
1282 __u32 dosattr = 0, origattr = 0;
1284 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1286 tlink = cifs_sb_tlink(cifs_sb);
1288 return PTR_ERR(tlink);
1289 tcon = tlink_tcon(tlink);
1290 server = tcon->ses->server;
1294 /* Unlink can be called from rename so we can not take the
1295 * sb->s_vfs_rename_mutex here */
1296 full_path = build_path_from_dentry(dentry);
1297 if (full_path == NULL) {
1302 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1303 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1304 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1305 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1306 cifs_remap(cifs_sb));
1307 cifs_dbg(FYI, "posix del rc %d\n", rc);
1308 if ((rc == 0) || (rc == -ENOENT))
1309 goto psx_del_no_retry;
1313 if (!server->ops->unlink) {
1315 goto psx_del_no_retry;
1318 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1323 cifs_drop_nlink(inode);
1324 } else if (rc == -ENOENT) {
1326 } else if (rc == -EBUSY) {
1327 if (server->ops->rename_pending_delete) {
1328 rc = server->ops->rename_pending_delete(full_path,
1331 cifs_drop_nlink(inode);
1333 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1334 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1335 if (attrs == NULL) {
1340 /* try to reset dos attributes */
1341 cifs_inode = CIFS_I(inode);
1342 origattr = cifs_inode->cifsAttrs;
1344 origattr |= ATTR_NORMAL;
1345 dosattr = origattr & ~ATTR_READONLY;
1347 dosattr |= ATTR_NORMAL;
1348 dosattr |= ATTR_HIDDEN;
1350 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1354 goto retry_std_delete;
1357 /* undo the setattr if we errored out and it's needed */
1358 if (rc != 0 && dosattr != 0)
1359 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1363 cifs_inode = CIFS_I(inode);
1364 cifs_inode->time = 0; /* will force revalidate to get info
1366 inode->i_ctime = current_fs_time(sb);
1368 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
1369 cifs_inode = CIFS_I(dir);
1370 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1375 cifs_put_tlink(tlink);
1380 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1381 const char *full_path, struct cifs_sb_info *cifs_sb,
1382 struct cifs_tcon *tcon, const unsigned int xid)
1385 struct inode *inode = NULL;
1388 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1391 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1398 * setting nlink not necessary except in cases where we failed to get it
1399 * from the server or was set bogus. Also, since this is a brand new
1400 * inode, no need to grab the i_lock before setting the i_nlink.
1402 if (inode->i_nlink < 2)
1403 set_nlink(inode, 2);
1404 mode &= ~current_umask();
1405 /* must turn on setgid bit if parent dir has it */
1406 if (parent->i_mode & S_ISGID)
1409 if (tcon->unix_ext) {
1410 struct cifs_unix_set_info_args args = {
1412 .ctime = NO_CHANGE_64,
1413 .atime = NO_CHANGE_64,
1414 .mtime = NO_CHANGE_64,
1417 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1418 args.uid = current_fsuid();
1419 if (parent->i_mode & S_ISGID)
1420 args.gid = parent->i_gid;
1422 args.gid = current_fsgid();
1424 args.uid = INVALID_UID; /* no change */
1425 args.gid = INVALID_GID; /* no change */
1427 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1429 cifs_remap(cifs_sb));
1431 struct TCP_Server_Info *server = tcon->ses->server;
1432 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1433 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1434 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1436 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1437 inode->i_mode = (mode | S_IFDIR);
1439 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1440 inode->i_uid = current_fsuid();
1441 if (inode->i_mode & S_ISGID)
1442 inode->i_gid = parent->i_gid;
1444 inode->i_gid = current_fsgid();
1447 d_instantiate(dentry, inode);
1452 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1453 const char *full_path, struct cifs_sb_info *cifs_sb,
1454 struct cifs_tcon *tcon, const unsigned int xid)
1458 FILE_UNIX_BASIC_INFO *info = NULL;
1459 struct inode *newinode = NULL;
1460 struct cifs_fattr fattr;
1462 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1465 goto posix_mkdir_out;
1468 mode &= ~current_umask();
1469 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1470 NULL /* netfid */, info, &oplock, full_path,
1471 cifs_sb->local_nls, cifs_remap(cifs_sb));
1472 if (rc == -EOPNOTSUPP)
1473 goto posix_mkdir_out;
1475 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1477 goto posix_mkdir_out;
1480 if (info->Type == cpu_to_le32(-1))
1481 /* no return info, go query for it */
1482 goto posix_mkdir_get_info;
1484 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1485 * need to set uid/gid.
1488 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1489 cifs_fill_uniqueid(inode->i_sb, &fattr);
1490 newinode = cifs_iget(inode->i_sb, &fattr);
1492 goto posix_mkdir_get_info;
1494 d_instantiate(dentry, newinode);
1496 #ifdef CONFIG_CIFS_DEBUG2
1497 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1498 dentry, dentry, newinode);
1500 if (newinode->i_nlink != 2)
1501 cifs_dbg(FYI, "unexpected number of links %d\n",
1508 posix_mkdir_get_info:
1509 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1511 goto posix_mkdir_out;
1514 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1518 struct cifs_sb_info *cifs_sb;
1519 struct tcon_link *tlink;
1520 struct cifs_tcon *tcon;
1521 struct TCP_Server_Info *server;
1524 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1527 cifs_sb = CIFS_SB(inode->i_sb);
1528 tlink = cifs_sb_tlink(cifs_sb);
1530 return PTR_ERR(tlink);
1531 tcon = tlink_tcon(tlink);
1535 full_path = build_path_from_dentry(direntry);
1536 if (full_path == NULL) {
1541 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1542 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1543 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1545 if (rc != -EOPNOTSUPP)
1549 server = tcon->ses->server;
1551 if (!server->ops->mkdir) {
1556 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1557 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1559 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1564 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1568 * Force revalidate to get parent dir info when needed since cached
1569 * attributes are invalid now.
1571 CIFS_I(inode)->time = 0;
1574 cifs_put_tlink(tlink);
1578 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1582 struct cifs_sb_info *cifs_sb;
1583 struct tcon_link *tlink;
1584 struct cifs_tcon *tcon;
1585 struct TCP_Server_Info *server;
1586 char *full_path = NULL;
1587 struct cifsInodeInfo *cifsInode;
1589 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1593 full_path = build_path_from_dentry(direntry);
1594 if (full_path == NULL) {
1599 cifs_sb = CIFS_SB(inode->i_sb);
1600 tlink = cifs_sb_tlink(cifs_sb);
1601 if (IS_ERR(tlink)) {
1602 rc = PTR_ERR(tlink);
1605 tcon = tlink_tcon(tlink);
1606 server = tcon->ses->server;
1608 if (!server->ops->rmdir) {
1610 cifs_put_tlink(tlink);
1614 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1615 cifs_put_tlink(tlink);
1618 spin_lock(&d_inode(direntry)->i_lock);
1619 i_size_write(d_inode(direntry), 0);
1620 clear_nlink(d_inode(direntry));
1621 spin_unlock(&d_inode(direntry)->i_lock);
1624 cifsInode = CIFS_I(d_inode(direntry));
1625 /* force revalidate to go get info when needed */
1626 cifsInode->time = 0;
1628 cifsInode = CIFS_I(inode);
1630 * Force revalidate to get parent dir info when needed since cached
1631 * attributes are invalid now.
1633 cifsInode->time = 0;
1635 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1636 current_fs_time(inode->i_sb);
1645 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1646 const char *from_path, struct dentry *to_dentry,
1647 const char *to_path)
1649 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1650 struct tcon_link *tlink;
1651 struct cifs_tcon *tcon;
1652 struct TCP_Server_Info *server;
1653 struct cifs_fid fid;
1654 struct cifs_open_parms oparms;
1657 tlink = cifs_sb_tlink(cifs_sb);
1659 return PTR_ERR(tlink);
1660 tcon = tlink_tcon(tlink);
1661 server = tcon->ses->server;
1663 if (!server->ops->rename)
1666 /* try path-based rename first */
1667 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
1670 * Don't bother with rename by filehandle unless file is busy and
1671 * source. Note that cross directory moves do not work with
1672 * rename by filehandle to various Windows servers.
1674 if (rc == 0 || rc != -EBUSY)
1675 goto do_rename_exit;
1677 /* open-file renames don't work across directories */
1678 if (to_dentry->d_parent != from_dentry->d_parent)
1679 goto do_rename_exit;
1682 oparms.cifs_sb = cifs_sb;
1683 /* open the file to be renamed -- we need DELETE perms */
1684 oparms.desired_access = DELETE;
1685 oparms.create_options = CREATE_NOT_DIR;
1686 oparms.disposition = FILE_OPEN;
1687 oparms.path = from_path;
1689 oparms.reconnect = false;
1691 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1693 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
1694 (const char *) to_dentry->d_name.name,
1695 cifs_sb->local_nls, cifs_remap(cifs_sb));
1696 CIFSSMBClose(xid, tcon, fid.netfid);
1699 cifs_put_tlink(tlink);
1704 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1705 struct inode *target_dir, struct dentry *target_dentry,
1708 char *from_name = NULL;
1709 char *to_name = NULL;
1710 struct cifs_sb_info *cifs_sb;
1711 struct tcon_link *tlink;
1712 struct cifs_tcon *tcon;
1713 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1714 FILE_UNIX_BASIC_INFO *info_buf_target;
1718 if (flags & ~RENAME_NOREPLACE)
1721 cifs_sb = CIFS_SB(source_dir->i_sb);
1722 tlink = cifs_sb_tlink(cifs_sb);
1724 return PTR_ERR(tlink);
1725 tcon = tlink_tcon(tlink);
1730 * we already have the rename sem so we do not need to
1731 * grab it again here to protect the path integrity
1733 from_name = build_path_from_dentry(source_dentry);
1734 if (from_name == NULL) {
1736 goto cifs_rename_exit;
1739 to_name = build_path_from_dentry(target_dentry);
1740 if (to_name == NULL) {
1742 goto cifs_rename_exit;
1745 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1749 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1751 if (flags & RENAME_NOREPLACE)
1752 goto cifs_rename_exit;
1754 if (rc == -EEXIST && tcon->unix_ext) {
1756 * Are src and dst hardlinks of same inode? We can only tell
1757 * with unix extensions enabled.
1760 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1762 if (info_buf_source == NULL) {
1764 goto cifs_rename_exit;
1767 info_buf_target = info_buf_source + 1;
1768 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1771 cifs_remap(cifs_sb));
1775 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1778 cifs_remap(cifs_sb));
1780 if (tmprc == 0 && (info_buf_source->UniqueId ==
1781 info_buf_target->UniqueId)) {
1782 /* same file, POSIX says that this is a noop */
1784 goto cifs_rename_exit;
1788 * else ... BB we could add the same check for Windows by
1789 * checking the UniqueId via FILE_INTERNAL_INFO
1793 /* Try unlinking the target dentry if it's not negative */
1794 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
1795 if (d_is_dir(target_dentry))
1796 tmprc = cifs_rmdir(target_dir, target_dentry);
1798 tmprc = cifs_unlink(target_dir, target_dentry);
1800 goto cifs_rename_exit;
1801 rc = cifs_do_rename(xid, source_dentry, from_name,
1802 target_dentry, to_name);
1805 /* force revalidate to go get info when needed */
1806 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1808 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1809 target_dir->i_mtime = current_fs_time(source_dir->i_sb);
1812 kfree(info_buf_source);
1816 cifs_put_tlink(tlink);
1821 cifs_inode_needs_reval(struct inode *inode)
1823 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1824 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1826 if (CIFS_CACHE_READ(cifs_i))
1829 if (!lookupCacheEnabled)
1832 if (cifs_i->time == 0)
1835 if (!cifs_sb->actimeo)
1838 if (!time_in_range(jiffies, cifs_i->time,
1839 cifs_i->time + cifs_sb->actimeo))
1842 /* hardlinked files w/ noserverino get "special" treatment */
1843 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1844 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1851 * Zap the cache. Called when invalid_mapping flag is set.
1854 cifs_invalidate_mapping(struct inode *inode)
1858 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1859 rc = invalidate_inode_pages2(inode->i_mapping);
1861 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1865 cifs_fscache_reset_inode_cookie(inode);
1870 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1871 * @word: long word containing the bit lock
1874 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
1876 freezable_schedule_unsafe();
1877 if (signal_pending_state(mode, current))
1878 return -ERESTARTSYS;
1883 cifs_revalidate_mapping(struct inode *inode)
1886 unsigned long *flags = &CIFS_I(inode)->flags;
1888 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1893 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1894 rc = cifs_invalidate_mapping(inode);
1896 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1899 clear_bit_unlock(CIFS_INO_LOCK, flags);
1900 smp_mb__after_atomic();
1901 wake_up_bit(flags, CIFS_INO_LOCK);
1907 cifs_zap_mapping(struct inode *inode)
1909 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1910 return cifs_revalidate_mapping(inode);
1913 int cifs_revalidate_file_attr(struct file *filp)
1916 struct inode *inode = file_inode(filp);
1917 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1919 if (!cifs_inode_needs_reval(inode))
1922 if (tlink_tcon(cfile->tlink)->unix_ext)
1923 rc = cifs_get_file_info_unix(filp);
1925 rc = cifs_get_file_info(filp);
1930 int cifs_revalidate_dentry_attr(struct dentry *dentry)
1934 struct inode *inode = d_inode(dentry);
1935 struct super_block *sb = dentry->d_sb;
1936 char *full_path = NULL;
1941 if (!cifs_inode_needs_reval(inode))
1946 /* can not safely grab the rename sem here if rename calls revalidate
1947 since that would deadlock */
1948 full_path = build_path_from_dentry(dentry);
1949 if (full_path == NULL) {
1954 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1955 full_path, inode, inode->i_count.counter,
1956 dentry, cifs_get_time(dentry), jiffies);
1958 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
1959 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1961 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1970 int cifs_revalidate_file(struct file *filp)
1973 struct inode *inode = file_inode(filp);
1975 rc = cifs_revalidate_file_attr(filp);
1979 return cifs_revalidate_mapping(inode);
1982 /* revalidate a dentry's inode attributes */
1983 int cifs_revalidate_dentry(struct dentry *dentry)
1986 struct inode *inode = d_inode(dentry);
1988 rc = cifs_revalidate_dentry_attr(dentry);
1992 return cifs_revalidate_mapping(inode);
1995 int cifs_getattr(const struct path *path, struct kstat *stat,
1996 u32 request_mask, unsigned int flags)
1998 struct dentry *dentry = path->dentry;
1999 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2000 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2001 struct inode *inode = d_inode(dentry);
2005 * We need to be sure that all dirty pages are written and the server
2006 * has actual ctime, mtime and file length.
2008 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2009 inode->i_mapping->nrpages != 0) {
2010 rc = filemap_fdatawait(inode->i_mapping);
2012 mapping_set_error(inode->i_mapping, rc);
2017 rc = cifs_revalidate_dentry_attr(dentry);
2021 generic_fillattr(inode, stat);
2022 stat->blksize = CIFS_MAX_MSGSIZE;
2023 stat->ino = CIFS_I(inode)->uniqueid;
2026 * If on a multiuser mount without unix extensions or cifsacl being
2027 * enabled, and the admin hasn't overridden them, set the ownership
2028 * to the fsuid/fsgid of the current process.
2030 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2031 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2033 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2034 stat->uid = current_fsuid();
2035 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2036 stat->gid = current_fsgid();
2041 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2043 pgoff_t index = from >> PAGE_SHIFT;
2044 unsigned offset = from & (PAGE_SIZE - 1);
2048 page = grab_cache_page(mapping, index);
2052 zero_user_segment(page, offset, PAGE_SIZE);
2058 static void cifs_setsize(struct inode *inode, loff_t offset)
2060 spin_lock(&inode->i_lock);
2061 i_size_write(inode, offset);
2062 spin_unlock(&inode->i_lock);
2064 truncate_pagecache(inode, offset);
2068 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2069 unsigned int xid, char *full_path)
2072 struct cifsFileInfo *open_file;
2073 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2074 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2075 struct tcon_link *tlink = NULL;
2076 struct cifs_tcon *tcon = NULL;
2077 struct TCP_Server_Info *server;
2080 * To avoid spurious oplock breaks from server, in the case of
2081 * inodes that we already have open, avoid doing path based
2082 * setting of file size if we can do it by handle.
2083 * This keeps our caching token (oplock) and avoids timeouts
2084 * when the local oplock break takes longer to flush
2085 * writebehind data than the SMB timeout for the SetPathInfo
2086 * request would allow
2088 open_file = find_writable_file(cifsInode, true);
2090 tcon = tlink_tcon(open_file->tlink);
2091 server = tcon->ses->server;
2092 if (server->ops->set_file_size)
2093 rc = server->ops->set_file_size(xid, tcon, open_file,
2094 attrs->ia_size, false);
2097 cifsFileInfo_put(open_file);
2098 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2106 tlink = cifs_sb_tlink(cifs_sb);
2108 return PTR_ERR(tlink);
2109 tcon = tlink_tcon(tlink);
2110 server = tcon->ses->server;
2114 * Set file size by pathname rather than by handle either because no
2115 * valid, writeable file handle for it was found or because there was
2116 * an error setting it by handle.
2118 if (server->ops->set_path_size)
2119 rc = server->ops->set_path_size(xid, tcon, full_path,
2120 attrs->ia_size, cifs_sb, false);
2123 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2126 cifs_put_tlink(tlink);
2130 cifsInode->server_eof = attrs->ia_size;
2131 cifs_setsize(inode, attrs->ia_size);
2132 cifs_truncate_page(inode->i_mapping, inode->i_size);
2139 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2143 char *full_path = NULL;
2144 struct inode *inode = d_inode(direntry);
2145 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2146 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2147 struct tcon_link *tlink;
2148 struct cifs_tcon *pTcon;
2149 struct cifs_unix_set_info_args *args = NULL;
2150 struct cifsFileInfo *open_file;
2152 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2153 direntry, attrs->ia_valid);
2157 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2158 attrs->ia_valid |= ATTR_FORCE;
2160 rc = setattr_prepare(direntry, attrs);
2164 full_path = build_path_from_dentry(direntry);
2165 if (full_path == NULL) {
2171 * Attempt to flush data before changing attributes. We need to do
2172 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2173 * ownership or mode then we may also need to do this. Here, we take
2174 * the safe way out and just do the flush on all setattr requests. If
2175 * the flush returns error, store it to report later and continue.
2177 * BB: This should be smarter. Why bother flushing pages that
2178 * will be truncated anyway? Also, should we error out here if
2179 * the flush returns error?
2181 rc = filemap_write_and_wait(inode->i_mapping);
2182 mapping_set_error(inode->i_mapping, rc);
2185 if (attrs->ia_valid & ATTR_SIZE) {
2186 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2191 /* skip mode change if it's just for clearing setuid/setgid */
2192 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2193 attrs->ia_valid &= ~ATTR_MODE;
2195 args = kmalloc(sizeof(*args), GFP_KERNEL);
2201 /* set up the struct */
2202 if (attrs->ia_valid & ATTR_MODE)
2203 args->mode = attrs->ia_mode;
2205 args->mode = NO_CHANGE_64;
2207 if (attrs->ia_valid & ATTR_UID)
2208 args->uid = attrs->ia_uid;
2210 args->uid = INVALID_UID; /* no change */
2212 if (attrs->ia_valid & ATTR_GID)
2213 args->gid = attrs->ia_gid;
2215 args->gid = INVALID_GID; /* no change */
2217 if (attrs->ia_valid & ATTR_ATIME)
2218 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2220 args->atime = NO_CHANGE_64;
2222 if (attrs->ia_valid & ATTR_MTIME)
2223 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2225 args->mtime = NO_CHANGE_64;
2227 if (attrs->ia_valid & ATTR_CTIME)
2228 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2230 args->ctime = NO_CHANGE_64;
2233 open_file = find_writable_file(cifsInode, true);
2235 u16 nfid = open_file->fid.netfid;
2236 u32 npid = open_file->pid;
2237 pTcon = tlink_tcon(open_file->tlink);
2238 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2239 cifsFileInfo_put(open_file);
2241 tlink = cifs_sb_tlink(cifs_sb);
2242 if (IS_ERR(tlink)) {
2243 rc = PTR_ERR(tlink);
2246 pTcon = tlink_tcon(tlink);
2247 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2249 cifs_remap(cifs_sb));
2250 cifs_put_tlink(tlink);
2256 if ((attrs->ia_valid & ATTR_SIZE) &&
2257 attrs->ia_size != i_size_read(inode))
2258 truncate_setsize(inode, attrs->ia_size);
2260 setattr_copy(inode, attrs);
2261 mark_inode_dirty(inode);
2263 /* force revalidate when any of these times are set since some
2264 of the fs types (eg ext3, fat) do not have fine enough
2265 time granularity to match protocol, and we do not have a
2266 a way (yet) to query the server fs's time granularity (and
2267 whether it rounds times down).
2269 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2270 cifsInode->time = 0;
2279 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2282 kuid_t uid = INVALID_UID;
2283 kgid_t gid = INVALID_GID;
2284 struct inode *inode = d_inode(direntry);
2285 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2286 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2287 char *full_path = NULL;
2290 __u64 mode = NO_CHANGE_64;
2294 cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
2295 direntry, attrs->ia_valid);
2297 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2298 attrs->ia_valid |= ATTR_FORCE;
2300 rc = setattr_prepare(direntry, attrs);
2306 full_path = build_path_from_dentry(direntry);
2307 if (full_path == NULL) {
2314 * Attempt to flush data before changing attributes. We need to do
2315 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2316 * ownership or mode then we may also need to do this. Here, we take
2317 * the safe way out and just do the flush on all setattr requests. If
2318 * the flush returns error, store it to report later and continue.
2320 * BB: This should be smarter. Why bother flushing pages that
2321 * will be truncated anyway? Also, should we error out here if
2322 * the flush returns error?
2324 rc = filemap_write_and_wait(inode->i_mapping);
2325 mapping_set_error(inode->i_mapping, rc);
2328 if (attrs->ia_valid & ATTR_SIZE) {
2329 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2331 goto cifs_setattr_exit;
2334 if (attrs->ia_valid & ATTR_UID)
2335 uid = attrs->ia_uid;
2337 if (attrs->ia_valid & ATTR_GID)
2338 gid = attrs->ia_gid;
2340 #ifdef CONFIG_CIFS_ACL
2341 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2342 if (uid_valid(uid) || gid_valid(gid)) {
2343 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2346 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2348 goto cifs_setattr_exit;
2352 #endif /* CONFIG_CIFS_ACL */
2353 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2354 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2356 /* skip mode change if it's just for clearing setuid/setgid */
2357 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2358 attrs->ia_valid &= ~ATTR_MODE;
2360 if (attrs->ia_valid & ATTR_MODE) {
2361 mode = attrs->ia_mode;
2363 #ifdef CONFIG_CIFS_ACL
2364 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2365 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2366 INVALID_UID, INVALID_GID);
2368 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2370 goto cifs_setattr_exit;
2373 #endif /* CONFIG_CIFS_ACL */
2374 if (((mode & S_IWUGO) == 0) &&
2375 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2377 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2379 /* fix up mode if we're not using dynperm */
2380 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2381 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2382 } else if ((mode & S_IWUGO) &&
2383 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2385 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2386 /* Attributes of 0 are ignored */
2388 dosattr |= ATTR_NORMAL;
2390 /* reset local inode permissions to normal */
2391 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2392 attrs->ia_mode &= ~(S_IALLUGO);
2393 if (S_ISDIR(inode->i_mode))
2395 cifs_sb->mnt_dir_mode;
2398 cifs_sb->mnt_file_mode;
2400 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2401 /* ignore mode change - ATTR_READONLY hasn't changed */
2402 attrs->ia_valid &= ~ATTR_MODE;
2406 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2407 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2408 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2409 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2411 /* Even if error on time set, no sense failing the call if
2412 the server would set the time to a reasonable value anyway,
2413 and this check ensures that we are not being called from
2414 sys_utimes in which case we ought to fail the call back to
2415 the user when the server rejects the call */
2416 if ((rc) && (attrs->ia_valid &
2417 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2421 /* do not need local check to inode_check_ok since the server does
2424 goto cifs_setattr_exit;
2426 if ((attrs->ia_valid & ATTR_SIZE) &&
2427 attrs->ia_size != i_size_read(inode))
2428 truncate_setsize(inode, attrs->ia_size);
2430 setattr_copy(inode, attrs);
2431 mark_inode_dirty(inode);
2440 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2442 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2443 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2445 if (pTcon->unix_ext)
2446 return cifs_setattr_unix(direntry, attrs);
2448 return cifs_setattr_nounix(direntry, attrs);
2450 /* BB: add cifs_setattr_legacy for really old servers */
2454 void cifs_delete_inode(struct inode *inode)
2456 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2457 /* may have to add back in if and when safe distributed caching of
2458 directories added e.g. via FindNotify */