4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/stat.h>
23 #include <linux/slab.h>
24 #include <linux/pagemap.h>
25 #include <linux/freezer.h>
26 #include <linux/sched/signal.h>
27 #include <linux/wait_bit.h>
29 #include <asm/div64.h>
33 #include "cifsproto.h"
34 #include "cifs_debug.h"
35 #include "cifs_fs_sb.h"
36 #include "cifs_unicode.h"
40 static void cifs_set_ops(struct inode *inode)
42 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
44 switch (inode->i_mode & S_IFMT) {
46 inode->i_op = &cifs_file_inode_ops;
47 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
48 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
49 inode->i_fop = &cifs_file_direct_nobrl_ops;
51 inode->i_fop = &cifs_file_direct_ops;
52 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
53 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
54 inode->i_fop = &cifs_file_strict_nobrl_ops;
56 inode->i_fop = &cifs_file_strict_ops;
57 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
58 inode->i_fop = &cifs_file_nobrl_ops;
59 else { /* not direct, send byte range locks */
60 inode->i_fop = &cifs_file_ops;
63 /* check if server can support readpages */
64 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
65 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
66 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
68 inode->i_data.a_ops = &cifs_addr_ops;
71 #ifdef CONFIG_CIFS_DFS_UPCALL
72 if (IS_AUTOMOUNT(inode)) {
73 inode->i_op = &cifs_dfs_referral_inode_operations;
75 #else /* NO DFS support, treat as a directory */
78 inode->i_op = &cifs_dir_inode_ops;
79 inode->i_fop = &cifs_dir_ops;
83 inode->i_op = &cifs_symlink_inode_ops;
86 init_special_inode(inode, inode->i_mode, inode->i_rdev);
91 /* check inode attributes against fattr. If they don't match, tag the
92 * inode for cache invalidation
95 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
97 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
99 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
100 __func__, cifs_i->uniqueid);
102 if (inode->i_state & I_NEW) {
103 cifs_dbg(FYI, "%s: inode %llu is new\n",
104 __func__, cifs_i->uniqueid);
108 /* don't bother with revalidation if we have an oplock */
109 if (CIFS_CACHE_READ(cifs_i)) {
110 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
111 __func__, cifs_i->uniqueid);
115 /* revalidate if mtime or size have changed */
116 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
117 cifs_i->server_eof == fattr->cf_eof) {
118 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
119 __func__, cifs_i->uniqueid);
123 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
124 __func__, cifs_i->uniqueid);
125 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
129 * copy nlink to the inode, unless it wasn't provided. Provide
130 * sane values if we don't have an existing one and none was provided
133 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
136 * if we're in a situation where we can't trust what we
137 * got from the server (readdir, some non-unix cases)
138 * fake reasonable values
140 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
141 /* only provide fake values on a new inode */
142 if (inode->i_state & I_NEW) {
143 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
151 /* we trust the server, so update it */
152 set_nlink(inode, fattr->cf_nlink);
155 /* populate an inode with info from a cifs_fattr struct */
157 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
159 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
160 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
162 cifs_revalidate_cache(inode, fattr);
164 spin_lock(&inode->i_lock);
165 inode->i_atime = fattr->cf_atime;
166 inode->i_mtime = fattr->cf_mtime;
167 inode->i_ctime = fattr->cf_ctime;
168 inode->i_rdev = fattr->cf_rdev;
169 cifs_nlink_fattr_to_inode(inode, fattr);
170 inode->i_uid = fattr->cf_uid;
171 inode->i_gid = fattr->cf_gid;
173 /* if dynperm is set, don't clobber existing mode */
174 if (inode->i_state & I_NEW ||
175 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
176 inode->i_mode = fattr->cf_mode;
178 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
180 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
183 cifs_i->time = jiffies;
185 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
186 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
188 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
190 cifs_i->server_eof = fattr->cf_eof;
192 * Can't safely change the file size here if the client is writing to
193 * it due to potential races.
195 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
196 i_size_write(inode, fattr->cf_eof);
199 * i_blocks is not related to (i_size / i_blksize),
200 * but instead 512 byte (2**9) size is required for
201 * calculating num blocks.
203 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
205 spin_unlock(&inode->i_lock);
207 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
208 inode->i_flags |= S_AUTOMOUNT;
209 if (inode->i_state & I_NEW)
214 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
216 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
218 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
221 fattr->cf_uniqueid = iunique(sb, ROOT_I);
224 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
226 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
227 struct cifs_sb_info *cifs_sb)
229 memset(fattr, 0, sizeof(*fattr));
230 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
231 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
232 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
234 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
235 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
236 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
237 /* old POSIX extensions don't get create time */
239 fattr->cf_mode = le64_to_cpu(info->Permissions);
242 * Since we set the inode type below we need to mask off
243 * to avoid strange results if bits set above.
245 fattr->cf_mode &= ~S_IFMT;
246 switch (le32_to_cpu(info->Type)) {
248 fattr->cf_mode |= S_IFREG;
249 fattr->cf_dtype = DT_REG;
252 fattr->cf_mode |= S_IFLNK;
253 fattr->cf_dtype = DT_LNK;
256 fattr->cf_mode |= S_IFDIR;
257 fattr->cf_dtype = DT_DIR;
260 fattr->cf_mode |= S_IFCHR;
261 fattr->cf_dtype = DT_CHR;
262 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
263 le64_to_cpu(info->DevMinor) & MINORMASK);
266 fattr->cf_mode |= S_IFBLK;
267 fattr->cf_dtype = DT_BLK;
268 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
269 le64_to_cpu(info->DevMinor) & MINORMASK);
272 fattr->cf_mode |= S_IFIFO;
273 fattr->cf_dtype = DT_FIFO;
276 fattr->cf_mode |= S_IFSOCK;
277 fattr->cf_dtype = DT_SOCK;
280 /* safest to call it a file if we do not know */
281 fattr->cf_mode |= S_IFREG;
282 fattr->cf_dtype = DT_REG;
283 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
287 fattr->cf_uid = cifs_sb->mnt_uid;
288 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
289 u64 id = le64_to_cpu(info->Uid);
290 if (id < ((uid_t)-1)) {
291 kuid_t uid = make_kuid(&init_user_ns, id);
297 fattr->cf_gid = cifs_sb->mnt_gid;
298 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
299 u64 id = le64_to_cpu(info->Gid);
300 if (id < ((gid_t)-1)) {
301 kgid_t gid = make_kgid(&init_user_ns, id);
307 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
311 * Fill a cifs_fattr struct with fake inode info.
313 * Needed to setup cifs_fattr data for the directory which is the
314 * junction to the new submount (ie to setup the fake directory
315 * which represents a DFS referral).
318 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
320 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
322 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
324 memset(fattr, 0, sizeof(*fattr));
325 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
326 fattr->cf_uid = cifs_sb->mnt_uid;
327 fattr->cf_gid = cifs_sb->mnt_gid;
328 ktime_get_real_ts(&fattr->cf_mtime);
329 fattr->cf_mtime = timespec_trunc(fattr->cf_mtime, sb->s_time_gran);
330 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
332 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
336 cifs_get_file_info_unix(struct file *filp)
340 FILE_UNIX_BASIC_INFO find_data;
341 struct cifs_fattr fattr;
342 struct inode *inode = file_inode(filp);
343 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
344 struct cifsFileInfo *cfile = filp->private_data;
345 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
348 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
350 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
351 } else if (rc == -EREMOTE) {
352 cifs_create_dfs_fattr(&fattr, inode->i_sb);
356 cifs_fattr_to_inode(inode, &fattr);
361 int cifs_get_inode_info_unix(struct inode **pinode,
362 const unsigned char *full_path,
363 struct super_block *sb, unsigned int xid)
366 FILE_UNIX_BASIC_INFO find_data;
367 struct cifs_fattr fattr;
368 struct cifs_tcon *tcon;
369 struct tcon_link *tlink;
370 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
372 cifs_dbg(FYI, "Getting info on %s\n", full_path);
374 tlink = cifs_sb_tlink(cifs_sb);
376 return PTR_ERR(tlink);
377 tcon = tlink_tcon(tlink);
379 /* could have done a find first instead but this returns more info */
380 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
381 cifs_sb->local_nls, cifs_remap(cifs_sb));
382 cifs_put_tlink(tlink);
385 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
386 } else if (rc == -EREMOTE) {
387 cifs_create_dfs_fattr(&fattr, sb);
393 /* check for Minshall+French symlinks */
394 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
395 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
398 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
401 if (*pinode == NULL) {
403 cifs_fill_uniqueid(sb, &fattr);
404 *pinode = cifs_iget(sb, &fattr);
408 /* we already have inode, update it */
410 /* if uniqueid is different, return error */
411 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
412 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
413 CIFS_I(*pinode)->time = 0; /* force reval */
418 /* if filetype is different, return error */
419 if (unlikely(((*pinode)->i_mode & S_IFMT) !=
420 (fattr.cf_mode & S_IFMT))) {
421 CIFS_I(*pinode)->time = 0; /* force reval */
426 cifs_fattr_to_inode(*pinode, &fattr);
434 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
435 struct cifs_sb_info *cifs_sb, unsigned int xid)
439 struct tcon_link *tlink;
440 struct cifs_tcon *tcon;
442 struct cifs_open_parms oparms;
443 struct cifs_io_parms io_parms;
445 unsigned int bytes_read;
447 int buf_type = CIFS_NO_BUFFER;
451 fattr->cf_mode &= ~S_IFMT;
453 if (fattr->cf_eof == 0) {
454 fattr->cf_mode |= S_IFIFO;
455 fattr->cf_dtype = DT_FIFO;
457 } else if (fattr->cf_eof < 8) {
458 fattr->cf_mode |= S_IFREG;
459 fattr->cf_dtype = DT_REG;
460 return -EINVAL; /* EOPNOTSUPP? */
463 tlink = cifs_sb_tlink(cifs_sb);
465 return PTR_ERR(tlink);
466 tcon = tlink_tcon(tlink);
469 oparms.cifs_sb = cifs_sb;
470 oparms.desired_access = GENERIC_READ;
471 oparms.create_options = CREATE_NOT_DIR;
472 if (backup_cred(cifs_sb))
473 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
474 oparms.disposition = FILE_OPEN;
477 oparms.reconnect = false;
479 if (tcon->ses->server->oplocks)
483 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
485 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
486 cifs_put_tlink(tlink);
491 io_parms.netfid = fid.netfid;
492 io_parms.pid = current->tgid;
493 io_parms.tcon = tcon;
495 io_parms.length = 24;
497 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
498 &bytes_read, &pbuf, &buf_type);
499 if ((rc == 0) && (bytes_read >= 8)) {
500 if (memcmp("IntxBLK", pbuf, 8) == 0) {
501 cifs_dbg(FYI, "Block device\n");
502 fattr->cf_mode |= S_IFBLK;
503 fattr->cf_dtype = DT_BLK;
504 if (bytes_read == 24) {
505 /* we have enough to decode dev num */
506 __u64 mjr; /* major */
507 __u64 mnr; /* minor */
508 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
509 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
510 fattr->cf_rdev = MKDEV(mjr, mnr);
512 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
513 cifs_dbg(FYI, "Char device\n");
514 fattr->cf_mode |= S_IFCHR;
515 fattr->cf_dtype = DT_CHR;
516 if (bytes_read == 24) {
517 /* we have enough to decode dev num */
518 __u64 mjr; /* major */
519 __u64 mnr; /* minor */
520 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
521 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
522 fattr->cf_rdev = MKDEV(mjr, mnr);
524 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
525 cifs_dbg(FYI, "Symlink\n");
526 fattr->cf_mode |= S_IFLNK;
527 fattr->cf_dtype = DT_LNK;
529 fattr->cf_mode |= S_IFREG; /* file? */
530 fattr->cf_dtype = DT_REG;
534 fattr->cf_mode |= S_IFREG; /* then it is a file */
535 fattr->cf_dtype = DT_REG;
536 rc = -EOPNOTSUPP; /* or some unknown SFU type */
539 tcon->ses->server->ops->close(xid, tcon, &fid);
540 cifs_put_tlink(tlink);
544 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
547 * Fetch mode bits as provided by SFU.
549 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
551 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
552 struct cifs_sb_info *cifs_sb, unsigned int xid)
554 #ifdef CONFIG_CIFS_XATTR
558 struct tcon_link *tlink;
559 struct cifs_tcon *tcon;
561 tlink = cifs_sb_tlink(cifs_sb);
563 return PTR_ERR(tlink);
564 tcon = tlink_tcon(tlink);
566 if (tcon->ses->server->ops->query_all_EAs == NULL) {
567 cifs_put_tlink(tlink);
571 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
572 "SETFILEBITS", ea_value, 4 /* size of buf */,
574 cifs_put_tlink(tlink);
578 mode = le32_to_cpu(*((__le32 *)ea_value));
579 fattr->cf_mode &= ~SFBITS_MASK;
580 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
581 mode, fattr->cf_mode);
582 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
583 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
592 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
594 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
595 struct super_block *sb, bool adjust_tz,
598 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
599 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
601 memset(fattr, 0, sizeof(*fattr));
602 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
603 if (info->DeletePending)
604 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
606 if (info->LastAccessTime)
607 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
609 ktime_get_real_ts(&fattr->cf_atime);
610 fattr->cf_atime = timespec_trunc(fattr->cf_atime, sb->s_time_gran);
613 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
614 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
617 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
618 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
621 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
622 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
623 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
625 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
628 fattr->cf_mode = S_IFLNK;
629 fattr->cf_dtype = DT_LNK;
630 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
631 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
632 fattr->cf_dtype = DT_DIR;
634 * Server can return wrong NumberOfLinks value for directories
635 * when Unix extensions are disabled - fake it.
638 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
640 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
641 fattr->cf_dtype = DT_REG;
643 /* clear write bits if ATTR_READONLY is set */
644 if (fattr->cf_cifsattrs & ATTR_READONLY)
645 fattr->cf_mode &= ~(S_IWUGO);
648 * Don't accept zero nlink from non-unix servers unless
649 * delete is pending. Instead mark it as unknown.
651 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
652 !info->DeletePending) {
653 cifs_dbg(1, "bogus file nlink value %u\n",
655 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
659 fattr->cf_uid = cifs_sb->mnt_uid;
660 fattr->cf_gid = cifs_sb->mnt_gid;
664 cifs_get_file_info(struct file *filp)
668 FILE_ALL_INFO find_data;
669 struct cifs_fattr fattr;
670 struct inode *inode = file_inode(filp);
671 struct cifsFileInfo *cfile = filp->private_data;
672 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
673 struct TCP_Server_Info *server = tcon->ses->server;
675 if (!server->ops->query_file_info)
679 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
682 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
686 cifs_create_dfs_fattr(&fattr, inode->i_sb);
692 * FIXME: legacy server -- fall back to path-based call?
693 * for now, just skip revalidating and mark inode for
697 CIFS_I(inode)->time = 0;
703 * don't bother with SFU junk here -- just mark inode as needing
706 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
707 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
708 cifs_fattr_to_inode(inode, &fattr);
714 /* Simple function to return a 64 bit hash of string. Rarely called */
715 static __u64 simple_hashstr(const char *str)
717 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
721 hash = (hash + (__u64) *str++) * hash_mult;
727 cifs_get_inode_info(struct inode **inode, const char *full_path,
728 FILE_ALL_INFO *data, struct super_block *sb, int xid,
729 const struct cifs_fid *fid)
731 bool validinum = false;
733 int rc = 0, tmprc = ENOSYS;
734 struct cifs_tcon *tcon;
735 struct TCP_Server_Info *server;
736 struct tcon_link *tlink;
737 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
739 bool adjust_tz = false;
740 struct cifs_fattr fattr;
741 struct cifs_search_info *srchinf = NULL;
742 bool symlink = false;
744 tlink = cifs_sb_tlink(cifs_sb);
746 return PTR_ERR(tlink);
747 tcon = tlink_tcon(tlink);
748 server = tcon->ses->server;
750 cifs_dbg(FYI, "Getting info on %s\n", full_path);
752 if ((data == NULL) && (*inode != NULL)) {
753 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
754 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
759 /* if inode info is not passed, get it from server */
761 if (!server->ops->query_path_info) {
765 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
770 data = (FILE_ALL_INFO *)buf;
771 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
772 data, &adjust_tz, &symlink);
776 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
778 } else if (rc == -EREMOTE) {
779 cifs_create_dfs_fattr(&fattr, sb);
781 } else if ((rc == -EACCES) && backup_cred(cifs_sb) &&
782 (strcmp(server->vals->version_string, SMB1_VERSION_STRING)
785 * For SMB2 and later the backup intent flag is already
786 * sent if needed on open and there is no path based
787 * FindFirst operation to use to retry with
790 srchinf = kzalloc(sizeof(struct cifs_search_info),
792 if (srchinf == NULL) {
797 srchinf->endOfSearch = false;
799 srchinf->info_level = SMB_FIND_FILE_UNIX;
800 else if ((tcon->ses->capabilities &
801 tcon->ses->server->vals->cap_nt_find) == 0)
802 srchinf->info_level = SMB_FIND_FILE_INFO_STANDARD;
803 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
804 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
805 else /* no srvino useful for fallback to some netapp */
806 srchinf->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
808 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
809 CIFS_SEARCH_CLOSE_AT_END |
810 CIFS_SEARCH_BACKUP_SEARCH;
812 rc = CIFSFindFirst(xid, tcon, full_path,
813 cifs_sb, NULL, srchflgs, srchinf, false);
815 data = (FILE_ALL_INFO *)srchinf->srch_entries_start;
817 cifs_dir_info_to_fattr(&fattr,
818 (FILE_DIRECTORY_INFO *)data, cifs_sb);
819 fattr.cf_uniqueid = le64_to_cpu(
820 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
823 cifs_buf_release(srchinf->ntwrk_buf_start);
832 * If an inode wasn't passed in, then get the inode number
834 * Is an i_ino of zero legal? Can we use that to check if the server
835 * supports returning inode numbers? Are there other sanity checks we
836 * can use to ensure that the server is really filling in that field?
838 if (*inode == NULL) {
839 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
840 if (validinum == false) {
841 if (server->ops->get_srv_inum)
842 tmprc = server->ops->get_srv_inum(xid,
843 tcon, cifs_sb, full_path,
844 &fattr.cf_uniqueid, data);
846 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
848 fattr.cf_uniqueid = iunique(sb, ROOT_I);
849 cifs_autodisable_serverino(cifs_sb);
850 } else if ((fattr.cf_uniqueid == 0) &&
851 strlen(full_path) == 0) {
852 /* some servers ret bad root ino ie 0 */
853 cifs_dbg(FYI, "Invalid (0) inodenum\n");
855 CIFS_FATTR_FAKE_ROOT_INO;
857 simple_hashstr(tcon->treeName);
861 fattr.cf_uniqueid = iunique(sb, ROOT_I);
863 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
864 validinum == false && server->ops->get_srv_inum) {
866 * Pass a NULL tcon to ensure we don't make a round
867 * trip to the server. This only works for SMB2+.
869 tmprc = server->ops->get_srv_inum(xid,
870 NULL, cifs_sb, full_path,
871 &fattr.cf_uniqueid, data);
873 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
874 else if ((fattr.cf_uniqueid == 0) &&
875 strlen(full_path) == 0) {
877 * Reuse existing root inode num since
878 * inum zero for root causes ls of . and .. to
881 cifs_dbg(FYI, "Srv ret 0 inode num for root\n");
882 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
885 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
888 /* query for SFU type info if supported and needed */
889 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
890 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
891 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
893 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
896 #ifdef CONFIG_CIFS_ACL
897 /* fill in 0777 bits from ACL */
898 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
899 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
901 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
906 #endif /* CONFIG_CIFS_ACL */
908 /* fill in remaining high mode bits e.g. SUID, VTX */
909 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
910 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
912 /* check for Minshall+French symlinks */
913 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
914 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
917 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
921 *inode = cifs_iget(sb, &fattr);
925 /* we already have inode, update it */
927 /* if uniqueid is different, return error */
928 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
929 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
930 CIFS_I(*inode)->time = 0; /* force reval */
935 /* if filetype is different, return error */
936 if (unlikely(((*inode)->i_mode & S_IFMT) !=
937 (fattr.cf_mode & S_IFMT))) {
938 CIFS_I(*inode)->time = 0; /* force reval */
943 cifs_fattr_to_inode(*inode, &fattr);
947 if ((*inode) && ((*inode)->i_ino == 0))
948 cifs_dbg(FYI, "inode number of zero returned\n");
951 cifs_put_tlink(tlink);
955 static const struct inode_operations cifs_ipc_inode_ops = {
956 .lookup = cifs_lookup,
960 cifs_find_inode(struct inode *inode, void *opaque)
962 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
964 /* don't match inode with different uniqueid */
965 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
968 /* use createtime like an i_generation field */
969 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
972 /* don't match inode of different type */
973 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
976 /* if it's not a directory or has no dentries, then flag it */
977 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
978 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
984 cifs_init_inode(struct inode *inode, void *opaque)
986 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
988 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
989 CIFS_I(inode)->createtime = fattr->cf_createtime;
994 * walk dentry list for an inode and report whether it has aliases that
995 * are hashed. We use this to determine if a directory inode can actually
999 inode_has_hashed_dentries(struct inode *inode)
1001 struct dentry *dentry;
1003 spin_lock(&inode->i_lock);
1004 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1005 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1006 spin_unlock(&inode->i_lock);
1010 spin_unlock(&inode->i_lock);
1014 /* Given fattrs, get a corresponding inode */
1016 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1019 struct inode *inode;
1022 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1024 /* hash down to 32-bits on 32-bit arch */
1025 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1027 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1029 /* was there a potentially problematic inode collision? */
1030 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1031 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1033 if (inode_has_hashed_dentries(inode)) {
1034 cifs_autodisable_serverino(CIFS_SB(sb));
1036 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1037 goto retry_iget5_locked;
1041 cifs_fattr_to_inode(inode, fattr);
1042 if (sb->s_flags & MS_NOATIME)
1043 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1044 if (inode->i_state & I_NEW) {
1045 inode->i_ino = hash;
1046 #ifdef CONFIG_CIFS_FSCACHE
1047 /* initialize per-inode cache cookie pointer */
1048 CIFS_I(inode)->fscache = NULL;
1050 unlock_new_inode(inode);
1057 /* gets root inode */
1058 struct inode *cifs_root_iget(struct super_block *sb)
1061 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1062 struct inode *inode = NULL;
1064 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1068 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1069 && cifs_sb->prepath) {
1070 len = strlen(cifs_sb->prepath);
1071 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1073 return ERR_PTR(-ENOMEM);
1075 memcpy(path+1, cifs_sb->prepath, len);
1077 path = kstrdup("", GFP_KERNEL);
1079 return ERR_PTR(-ENOMEM);
1083 if (tcon->unix_ext) {
1084 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1085 /* some servers mistakenly claim POSIX support */
1086 if (rc != -EOPNOTSUPP)
1088 cifs_dbg(VFS, "server does not support POSIX extensions");
1089 tcon->unix_ext = false;
1092 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1093 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1097 inode = ERR_PTR(rc);
1101 #ifdef CONFIG_CIFS_FSCACHE
1102 /* populate tcon->resource_id */
1103 tcon->resource_id = CIFS_I(inode)->uniqueid;
1106 if (rc && tcon->ipc) {
1107 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1108 spin_lock(&inode->i_lock);
1109 inode->i_mode |= S_IFDIR;
1110 set_nlink(inode, 2);
1111 inode->i_op = &cifs_ipc_inode_ops;
1112 inode->i_fop = &simple_dir_operations;
1113 inode->i_uid = cifs_sb->mnt_uid;
1114 inode->i_gid = cifs_sb->mnt_gid;
1115 spin_unlock(&inode->i_lock);
1118 inode = ERR_PTR(rc);
1123 /* can not call macro free_xid here since in a void func
1124 * TODO: This is no longer true
1131 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1132 char *full_path, __u32 dosattr)
1134 bool set_time = false;
1135 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1136 struct TCP_Server_Info *server;
1137 FILE_BASIC_INFO info_buf;
1142 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1143 if (!server->ops->set_file_info)
1148 if (attrs->ia_valid & ATTR_ATIME) {
1150 info_buf.LastAccessTime =
1151 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1153 info_buf.LastAccessTime = 0;
1155 if (attrs->ia_valid & ATTR_MTIME) {
1157 info_buf.LastWriteTime =
1158 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1160 info_buf.LastWriteTime = 0;
1163 * Samba throws this field away, but windows may actually use it.
1164 * Do not set ctime unless other time stamps are changed explicitly
1165 * (i.e. by utimes()) since we would then have a mix of client and
1168 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1169 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1170 info_buf.ChangeTime =
1171 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1173 info_buf.ChangeTime = 0;
1175 info_buf.CreationTime = 0; /* don't change */
1176 info_buf.Attributes = cpu_to_le32(dosattr);
1178 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1182 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1183 * and rename it to a random name that hopefully won't conflict with
1187 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1188 const unsigned int xid)
1192 struct cifs_fid fid;
1193 struct cifs_open_parms oparms;
1194 struct inode *inode = d_inode(dentry);
1195 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1196 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1197 struct tcon_link *tlink;
1198 struct cifs_tcon *tcon;
1199 __u32 dosattr, origattr;
1200 FILE_BASIC_INFO *info_buf = NULL;
1202 tlink = cifs_sb_tlink(cifs_sb);
1204 return PTR_ERR(tlink);
1205 tcon = tlink_tcon(tlink);
1208 * We cannot rename the file if the server doesn't support
1209 * CAP_INFOLEVEL_PASSTHRU
1211 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1217 oparms.cifs_sb = cifs_sb;
1218 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1219 oparms.create_options = CREATE_NOT_DIR;
1220 oparms.disposition = FILE_OPEN;
1221 oparms.path = full_path;
1223 oparms.reconnect = false;
1225 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1229 origattr = cifsInode->cifsAttrs;
1231 origattr |= ATTR_NORMAL;
1233 dosattr = origattr & ~ATTR_READONLY;
1235 dosattr |= ATTR_NORMAL;
1236 dosattr |= ATTR_HIDDEN;
1238 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1239 if (dosattr != origattr) {
1240 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1241 if (info_buf == NULL) {
1245 info_buf->Attributes = cpu_to_le32(dosattr);
1246 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1248 /* although we would like to mark the file hidden
1249 if that fails we will still try to rename it */
1251 cifsInode->cifsAttrs = dosattr;
1253 dosattr = origattr; /* since not able to change them */
1256 /* rename the file */
1257 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1259 cifs_remap(cifs_sb));
1265 /* try to set DELETE_ON_CLOSE */
1266 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1267 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1270 * some samba versions return -ENOENT when we try to set the
1271 * file disposition here. Likely a samba bug, but work around
1272 * it for now. This means that some cifsXXX files may hang
1273 * around after they shouldn't.
1275 * BB: remove this hack after more servers have the fix
1283 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1287 CIFSSMBClose(xid, tcon, fid.netfid);
1290 cifs_put_tlink(tlink);
1294 * reset everything back to the original state. Don't bother
1295 * dealing with errors here since we can't do anything about
1299 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1300 cifs_sb->local_nls, cifs_remap(cifs_sb));
1302 if (dosattr != origattr) {
1303 info_buf->Attributes = cpu_to_le32(origattr);
1304 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1306 cifsInode->cifsAttrs = origattr;
1312 /* copied from fs/nfs/dir.c with small changes */
1314 cifs_drop_nlink(struct inode *inode)
1316 spin_lock(&inode->i_lock);
1317 if (inode->i_nlink > 0)
1319 spin_unlock(&inode->i_lock);
1323 * If d_inode(dentry) is null (usually meaning the cached dentry
1324 * is a negative dentry) then we would attempt a standard SMB delete, but
1325 * if that fails we can not attempt the fall back mechanisms on EACCESS
1326 * but will return the EACCESS to the caller. Note that the VFS does not call
1327 * unlink on negative dentries currently.
1329 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1333 char *full_path = NULL;
1334 struct inode *inode = d_inode(dentry);
1335 struct cifsInodeInfo *cifs_inode;
1336 struct super_block *sb = dir->i_sb;
1337 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1338 struct tcon_link *tlink;
1339 struct cifs_tcon *tcon;
1340 struct TCP_Server_Info *server;
1341 struct iattr *attrs = NULL;
1342 __u32 dosattr = 0, origattr = 0;
1344 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1346 tlink = cifs_sb_tlink(cifs_sb);
1348 return PTR_ERR(tlink);
1349 tcon = tlink_tcon(tlink);
1350 server = tcon->ses->server;
1354 /* Unlink can be called from rename so we can not take the
1355 * sb->s_vfs_rename_mutex here */
1356 full_path = build_path_from_dentry(dentry);
1357 if (full_path == NULL) {
1362 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1363 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1364 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1365 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1366 cifs_remap(cifs_sb));
1367 cifs_dbg(FYI, "posix del rc %d\n", rc);
1368 if ((rc == 0) || (rc == -ENOENT))
1369 goto psx_del_no_retry;
1373 if (!server->ops->unlink) {
1375 goto psx_del_no_retry;
1378 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1383 cifs_drop_nlink(inode);
1384 } else if (rc == -ENOENT) {
1386 } else if (rc == -EBUSY) {
1387 if (server->ops->rename_pending_delete) {
1388 rc = server->ops->rename_pending_delete(full_path,
1391 cifs_drop_nlink(inode);
1393 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1394 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1395 if (attrs == NULL) {
1400 /* try to reset dos attributes */
1401 cifs_inode = CIFS_I(inode);
1402 origattr = cifs_inode->cifsAttrs;
1404 origattr |= ATTR_NORMAL;
1405 dosattr = origattr & ~ATTR_READONLY;
1407 dosattr |= ATTR_NORMAL;
1408 dosattr |= ATTR_HIDDEN;
1410 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1414 goto retry_std_delete;
1417 /* undo the setattr if we errored out and it's needed */
1418 if (rc != 0 && dosattr != 0)
1419 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1423 cifs_inode = CIFS_I(inode);
1424 cifs_inode->time = 0; /* will force revalidate to get info
1426 inode->i_ctime = current_time(inode);
1428 dir->i_ctime = dir->i_mtime = current_time(dir);
1429 cifs_inode = CIFS_I(dir);
1430 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1435 cifs_put_tlink(tlink);
1440 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1441 const char *full_path, struct cifs_sb_info *cifs_sb,
1442 struct cifs_tcon *tcon, const unsigned int xid)
1445 struct inode *inode = NULL;
1448 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1451 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1458 * setting nlink not necessary except in cases where we failed to get it
1459 * from the server or was set bogus. Also, since this is a brand new
1460 * inode, no need to grab the i_lock before setting the i_nlink.
1462 if (inode->i_nlink < 2)
1463 set_nlink(inode, 2);
1464 mode &= ~current_umask();
1465 /* must turn on setgid bit if parent dir has it */
1466 if (parent->i_mode & S_ISGID)
1469 if (tcon->unix_ext) {
1470 struct cifs_unix_set_info_args args = {
1472 .ctime = NO_CHANGE_64,
1473 .atime = NO_CHANGE_64,
1474 .mtime = NO_CHANGE_64,
1477 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1478 args.uid = current_fsuid();
1479 if (parent->i_mode & S_ISGID)
1480 args.gid = parent->i_gid;
1482 args.gid = current_fsgid();
1484 args.uid = INVALID_UID; /* no change */
1485 args.gid = INVALID_GID; /* no change */
1487 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1489 cifs_remap(cifs_sb));
1491 struct TCP_Server_Info *server = tcon->ses->server;
1492 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1493 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1494 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1496 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1497 inode->i_mode = (mode | S_IFDIR);
1499 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1500 inode->i_uid = current_fsuid();
1501 if (inode->i_mode & S_ISGID)
1502 inode->i_gid = parent->i_gid;
1504 inode->i_gid = current_fsgid();
1507 d_instantiate(dentry, inode);
1512 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1513 const char *full_path, struct cifs_sb_info *cifs_sb,
1514 struct cifs_tcon *tcon, const unsigned int xid)
1518 FILE_UNIX_BASIC_INFO *info = NULL;
1519 struct inode *newinode = NULL;
1520 struct cifs_fattr fattr;
1522 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1525 goto posix_mkdir_out;
1528 mode &= ~current_umask();
1529 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1530 NULL /* netfid */, info, &oplock, full_path,
1531 cifs_sb->local_nls, cifs_remap(cifs_sb));
1532 if (rc == -EOPNOTSUPP)
1533 goto posix_mkdir_out;
1535 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1537 goto posix_mkdir_out;
1540 if (info->Type == cpu_to_le32(-1))
1541 /* no return info, go query for it */
1542 goto posix_mkdir_get_info;
1544 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1545 * need to set uid/gid.
1548 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1549 cifs_fill_uniqueid(inode->i_sb, &fattr);
1550 newinode = cifs_iget(inode->i_sb, &fattr);
1552 goto posix_mkdir_get_info;
1554 d_instantiate(dentry, newinode);
1556 #ifdef CONFIG_CIFS_DEBUG2
1557 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1558 dentry, dentry, newinode);
1560 if (newinode->i_nlink != 2)
1561 cifs_dbg(FYI, "unexpected number of links %d\n",
1568 posix_mkdir_get_info:
1569 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1571 goto posix_mkdir_out;
1574 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1578 struct cifs_sb_info *cifs_sb;
1579 struct tcon_link *tlink;
1580 struct cifs_tcon *tcon;
1581 struct TCP_Server_Info *server;
1584 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1587 cifs_sb = CIFS_SB(inode->i_sb);
1588 tlink = cifs_sb_tlink(cifs_sb);
1590 return PTR_ERR(tlink);
1591 tcon = tlink_tcon(tlink);
1595 full_path = build_path_from_dentry(direntry);
1596 if (full_path == NULL) {
1601 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1602 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1603 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1605 if (rc != -EOPNOTSUPP)
1609 server = tcon->ses->server;
1611 if (!server->ops->mkdir) {
1616 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1617 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1619 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1624 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1628 * Force revalidate to get parent dir info when needed since cached
1629 * attributes are invalid now.
1631 CIFS_I(inode)->time = 0;
1634 cifs_put_tlink(tlink);
1638 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1642 struct cifs_sb_info *cifs_sb;
1643 struct tcon_link *tlink;
1644 struct cifs_tcon *tcon;
1645 struct TCP_Server_Info *server;
1646 char *full_path = NULL;
1647 struct cifsInodeInfo *cifsInode;
1649 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1653 full_path = build_path_from_dentry(direntry);
1654 if (full_path == NULL) {
1659 cifs_sb = CIFS_SB(inode->i_sb);
1660 tlink = cifs_sb_tlink(cifs_sb);
1661 if (IS_ERR(tlink)) {
1662 rc = PTR_ERR(tlink);
1665 tcon = tlink_tcon(tlink);
1666 server = tcon->ses->server;
1668 if (!server->ops->rmdir) {
1670 cifs_put_tlink(tlink);
1674 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1675 cifs_put_tlink(tlink);
1678 spin_lock(&d_inode(direntry)->i_lock);
1679 i_size_write(d_inode(direntry), 0);
1680 clear_nlink(d_inode(direntry));
1681 spin_unlock(&d_inode(direntry)->i_lock);
1684 cifsInode = CIFS_I(d_inode(direntry));
1685 /* force revalidate to go get info when needed */
1686 cifsInode->time = 0;
1688 cifsInode = CIFS_I(inode);
1690 * Force revalidate to get parent dir info when needed since cached
1691 * attributes are invalid now.
1693 cifsInode->time = 0;
1695 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1696 current_time(inode);
1705 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1706 const char *from_path, struct dentry *to_dentry,
1707 const char *to_path)
1709 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1710 struct tcon_link *tlink;
1711 struct cifs_tcon *tcon;
1712 struct TCP_Server_Info *server;
1713 struct cifs_fid fid;
1714 struct cifs_open_parms oparms;
1717 tlink = cifs_sb_tlink(cifs_sb);
1719 return PTR_ERR(tlink);
1720 tcon = tlink_tcon(tlink);
1721 server = tcon->ses->server;
1723 if (!server->ops->rename)
1726 /* try path-based rename first */
1727 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
1730 * Don't bother with rename by filehandle unless file is busy and
1731 * source. Note that cross directory moves do not work with
1732 * rename by filehandle to various Windows servers.
1734 if (rc == 0 || rc != -EBUSY)
1735 goto do_rename_exit;
1737 /* Don't fall back to using SMB on SMB 2+ mount */
1738 if (server->vals->protocol_id != 0)
1739 goto do_rename_exit;
1741 /* open-file renames don't work across directories */
1742 if (to_dentry->d_parent != from_dentry->d_parent)
1743 goto do_rename_exit;
1746 oparms.cifs_sb = cifs_sb;
1747 /* open the file to be renamed -- we need DELETE perms */
1748 oparms.desired_access = DELETE;
1749 oparms.create_options = CREATE_NOT_DIR;
1750 oparms.disposition = FILE_OPEN;
1751 oparms.path = from_path;
1753 oparms.reconnect = false;
1755 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1757 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
1758 (const char *) to_dentry->d_name.name,
1759 cifs_sb->local_nls, cifs_remap(cifs_sb));
1760 CIFSSMBClose(xid, tcon, fid.netfid);
1763 cifs_put_tlink(tlink);
1768 cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1769 struct inode *target_dir, struct dentry *target_dentry,
1772 char *from_name = NULL;
1773 char *to_name = NULL;
1774 struct cifs_sb_info *cifs_sb;
1775 struct tcon_link *tlink;
1776 struct cifs_tcon *tcon;
1777 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1778 FILE_UNIX_BASIC_INFO *info_buf_target;
1782 if (flags & ~RENAME_NOREPLACE)
1785 cifs_sb = CIFS_SB(source_dir->i_sb);
1786 tlink = cifs_sb_tlink(cifs_sb);
1788 return PTR_ERR(tlink);
1789 tcon = tlink_tcon(tlink);
1794 * we already have the rename sem so we do not need to
1795 * grab it again here to protect the path integrity
1797 from_name = build_path_from_dentry(source_dentry);
1798 if (from_name == NULL) {
1800 goto cifs_rename_exit;
1803 to_name = build_path_from_dentry(target_dentry);
1804 if (to_name == NULL) {
1806 goto cifs_rename_exit;
1809 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1813 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1815 if (flags & RENAME_NOREPLACE)
1816 goto cifs_rename_exit;
1818 if (rc == -EEXIST && tcon->unix_ext) {
1820 * Are src and dst hardlinks of same inode? We can only tell
1821 * with unix extensions enabled.
1824 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1826 if (info_buf_source == NULL) {
1828 goto cifs_rename_exit;
1831 info_buf_target = info_buf_source + 1;
1832 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1835 cifs_remap(cifs_sb));
1839 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1842 cifs_remap(cifs_sb));
1844 if (tmprc == 0 && (info_buf_source->UniqueId ==
1845 info_buf_target->UniqueId)) {
1846 /* same file, POSIX says that this is a noop */
1848 goto cifs_rename_exit;
1852 * else ... BB we could add the same check for Windows by
1853 * checking the UniqueId via FILE_INTERNAL_INFO
1857 /* Try unlinking the target dentry if it's not negative */
1858 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
1859 if (d_is_dir(target_dentry))
1860 tmprc = cifs_rmdir(target_dir, target_dentry);
1862 tmprc = cifs_unlink(target_dir, target_dentry);
1864 goto cifs_rename_exit;
1865 rc = cifs_do_rename(xid, source_dentry, from_name,
1866 target_dentry, to_name);
1869 /* force revalidate to go get info when needed */
1870 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1872 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1873 target_dir->i_mtime = current_time(source_dir);
1876 kfree(info_buf_source);
1880 cifs_put_tlink(tlink);
1885 cifs_inode_needs_reval(struct inode *inode)
1887 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1888 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1890 if (CIFS_CACHE_READ(cifs_i))
1893 if (!lookupCacheEnabled)
1896 if (cifs_i->time == 0)
1899 if (!cifs_sb->actimeo)
1902 if (!time_in_range(jiffies, cifs_i->time,
1903 cifs_i->time + cifs_sb->actimeo))
1906 /* hardlinked files w/ noserverino get "special" treatment */
1907 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1908 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1915 * Zap the cache. Called when invalid_mapping flag is set.
1918 cifs_invalidate_mapping(struct inode *inode)
1922 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1923 rc = invalidate_inode_pages2(inode->i_mapping);
1925 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1929 cifs_fscache_reset_inode_cookie(inode);
1934 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1935 * @word: long word containing the bit lock
1938 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
1940 freezable_schedule_unsafe();
1941 if (signal_pending_state(mode, current))
1942 return -ERESTARTSYS;
1947 cifs_revalidate_mapping(struct inode *inode)
1950 unsigned long *flags = &CIFS_I(inode)->flags;
1952 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1957 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1958 rc = cifs_invalidate_mapping(inode);
1960 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1963 clear_bit_unlock(CIFS_INO_LOCK, flags);
1964 smp_mb__after_atomic();
1965 wake_up_bit(flags, CIFS_INO_LOCK);
1971 cifs_zap_mapping(struct inode *inode)
1973 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1974 return cifs_revalidate_mapping(inode);
1977 int cifs_revalidate_file_attr(struct file *filp)
1980 struct inode *inode = file_inode(filp);
1981 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1983 if (!cifs_inode_needs_reval(inode))
1986 if (tlink_tcon(cfile->tlink)->unix_ext)
1987 rc = cifs_get_file_info_unix(filp);
1989 rc = cifs_get_file_info(filp);
1994 int cifs_revalidate_dentry_attr(struct dentry *dentry)
1998 struct inode *inode = d_inode(dentry);
1999 struct super_block *sb = dentry->d_sb;
2000 char *full_path = NULL;
2006 if (!cifs_inode_needs_reval(inode))
2011 /* can not safely grab the rename sem here if rename calls revalidate
2012 since that would deadlock */
2013 full_path = build_path_from_dentry(dentry);
2014 if (full_path == NULL) {
2019 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2020 full_path, inode, inode->i_count.counter,
2021 dentry, cifs_get_time(dentry), jiffies);
2024 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2025 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2027 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2029 if (rc == -EAGAIN && count++ < 10)
2038 int cifs_revalidate_file(struct file *filp)
2041 struct inode *inode = file_inode(filp);
2043 rc = cifs_revalidate_file_attr(filp);
2047 return cifs_revalidate_mapping(inode);
2050 /* revalidate a dentry's inode attributes */
2051 int cifs_revalidate_dentry(struct dentry *dentry)
2054 struct inode *inode = d_inode(dentry);
2056 rc = cifs_revalidate_dentry_attr(dentry);
2060 return cifs_revalidate_mapping(inode);
2063 int cifs_getattr(const struct path *path, struct kstat *stat,
2064 u32 request_mask, unsigned int flags)
2066 struct dentry *dentry = path->dentry;
2067 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2068 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2069 struct inode *inode = d_inode(dentry);
2073 * We need to be sure that all dirty pages are written and the server
2074 * has actual ctime, mtime and file length.
2076 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2077 inode->i_mapping->nrpages != 0) {
2078 rc = filemap_fdatawait(inode->i_mapping);
2080 mapping_set_error(inode->i_mapping, rc);
2085 rc = cifs_revalidate_dentry_attr(dentry);
2089 generic_fillattr(inode, stat);
2090 stat->blksize = CIFS_MAX_MSGSIZE;
2091 stat->ino = CIFS_I(inode)->uniqueid;
2093 /* old CIFS Unix Extensions doesn't return create time */
2094 if (CIFS_I(inode)->createtime) {
2095 stat->result_mask |= STATX_BTIME;
2097 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2100 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2101 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2102 stat->attributes |= STATX_ATTR_COMPRESSED;
2103 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2104 stat->attributes |= STATX_ATTR_ENCRYPTED;
2107 * If on a multiuser mount without unix extensions or cifsacl being
2108 * enabled, and the admin hasn't overridden them, set the ownership
2109 * to the fsuid/fsgid of the current process.
2111 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2112 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2114 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2115 stat->uid = current_fsuid();
2116 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2117 stat->gid = current_fsgid();
2122 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2124 pgoff_t index = from >> PAGE_SHIFT;
2125 unsigned offset = from & (PAGE_SIZE - 1);
2129 page = grab_cache_page(mapping, index);
2133 zero_user_segment(page, offset, PAGE_SIZE);
2139 static void cifs_setsize(struct inode *inode, loff_t offset)
2141 spin_lock(&inode->i_lock);
2142 i_size_write(inode, offset);
2143 spin_unlock(&inode->i_lock);
2145 truncate_pagecache(inode, offset);
2149 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2150 unsigned int xid, char *full_path)
2153 struct cifsFileInfo *open_file;
2154 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2156 struct tcon_link *tlink = NULL;
2157 struct cifs_tcon *tcon = NULL;
2158 struct TCP_Server_Info *server;
2161 * To avoid spurious oplock breaks from server, in the case of
2162 * inodes that we already have open, avoid doing path based
2163 * setting of file size if we can do it by handle.
2164 * This keeps our caching token (oplock) and avoids timeouts
2165 * when the local oplock break takes longer to flush
2166 * writebehind data than the SMB timeout for the SetPathInfo
2167 * request would allow
2169 open_file = find_writable_file(cifsInode, true);
2171 tcon = tlink_tcon(open_file->tlink);
2172 server = tcon->ses->server;
2173 if (server->ops->set_file_size)
2174 rc = server->ops->set_file_size(xid, tcon, open_file,
2175 attrs->ia_size, false);
2178 cifsFileInfo_put(open_file);
2179 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2187 tlink = cifs_sb_tlink(cifs_sb);
2189 return PTR_ERR(tlink);
2190 tcon = tlink_tcon(tlink);
2191 server = tcon->ses->server;
2195 * Set file size by pathname rather than by handle either because no
2196 * valid, writeable file handle for it was found or because there was
2197 * an error setting it by handle.
2199 if (server->ops->set_path_size)
2200 rc = server->ops->set_path_size(xid, tcon, full_path,
2201 attrs->ia_size, cifs_sb, false);
2204 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2207 cifs_put_tlink(tlink);
2211 cifsInode->server_eof = attrs->ia_size;
2212 cifs_setsize(inode, attrs->ia_size);
2215 * The man page of truncate says if the size changed,
2216 * then the st_ctime and st_mtime fields for the file
2219 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2220 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2222 cifs_truncate_page(inode->i_mapping, inode->i_size);
2229 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2233 char *full_path = NULL;
2234 struct inode *inode = d_inode(direntry);
2235 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2236 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2237 struct tcon_link *tlink;
2238 struct cifs_tcon *pTcon;
2239 struct cifs_unix_set_info_args *args = NULL;
2240 struct cifsFileInfo *open_file;
2242 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2243 direntry, attrs->ia_valid);
2247 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2248 attrs->ia_valid |= ATTR_FORCE;
2250 rc = setattr_prepare(direntry, attrs);
2254 full_path = build_path_from_dentry(direntry);
2255 if (full_path == NULL) {
2261 * Attempt to flush data before changing attributes. We need to do
2262 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2263 * ownership or mode then we may also need to do this. Here, we take
2264 * the safe way out and just do the flush on all setattr requests. If
2265 * the flush returns error, store it to report later and continue.
2267 * BB: This should be smarter. Why bother flushing pages that
2268 * will be truncated anyway? Also, should we error out here if
2269 * the flush returns error?
2271 rc = filemap_write_and_wait(inode->i_mapping);
2272 mapping_set_error(inode->i_mapping, rc);
2275 if (attrs->ia_valid & ATTR_SIZE) {
2276 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2281 /* skip mode change if it's just for clearing setuid/setgid */
2282 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2283 attrs->ia_valid &= ~ATTR_MODE;
2285 args = kmalloc(sizeof(*args), GFP_KERNEL);
2291 /* set up the struct */
2292 if (attrs->ia_valid & ATTR_MODE)
2293 args->mode = attrs->ia_mode;
2295 args->mode = NO_CHANGE_64;
2297 if (attrs->ia_valid & ATTR_UID)
2298 args->uid = attrs->ia_uid;
2300 args->uid = INVALID_UID; /* no change */
2302 if (attrs->ia_valid & ATTR_GID)
2303 args->gid = attrs->ia_gid;
2305 args->gid = INVALID_GID; /* no change */
2307 if (attrs->ia_valid & ATTR_ATIME)
2308 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2310 args->atime = NO_CHANGE_64;
2312 if (attrs->ia_valid & ATTR_MTIME)
2313 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2315 args->mtime = NO_CHANGE_64;
2317 if (attrs->ia_valid & ATTR_CTIME)
2318 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2320 args->ctime = NO_CHANGE_64;
2323 open_file = find_writable_file(cifsInode, true);
2325 u16 nfid = open_file->fid.netfid;
2326 u32 npid = open_file->pid;
2327 pTcon = tlink_tcon(open_file->tlink);
2328 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2329 cifsFileInfo_put(open_file);
2331 tlink = cifs_sb_tlink(cifs_sb);
2332 if (IS_ERR(tlink)) {
2333 rc = PTR_ERR(tlink);
2336 pTcon = tlink_tcon(tlink);
2337 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2339 cifs_remap(cifs_sb));
2340 cifs_put_tlink(tlink);
2346 if ((attrs->ia_valid & ATTR_SIZE) &&
2347 attrs->ia_size != i_size_read(inode))
2348 truncate_setsize(inode, attrs->ia_size);
2350 setattr_copy(inode, attrs);
2351 mark_inode_dirty(inode);
2353 /* force revalidate when any of these times are set since some
2354 of the fs types (eg ext3, fat) do not have fine enough
2355 time granularity to match protocol, and we do not have a
2356 a way (yet) to query the server fs's time granularity (and
2357 whether it rounds times down).
2359 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2360 cifsInode->time = 0;
2369 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2372 kuid_t uid = INVALID_UID;
2373 kgid_t gid = INVALID_GID;
2374 struct inode *inode = d_inode(direntry);
2375 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2376 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2377 char *full_path = NULL;
2380 __u64 mode = NO_CHANGE_64;
2384 cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
2385 direntry, attrs->ia_valid);
2387 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2388 attrs->ia_valid |= ATTR_FORCE;
2390 rc = setattr_prepare(direntry, attrs);
2396 full_path = build_path_from_dentry(direntry);
2397 if (full_path == NULL) {
2404 * Attempt to flush data before changing attributes. We need to do
2405 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2406 * ownership or mode then we may also need to do this. Here, we take
2407 * the safe way out and just do the flush on all setattr requests. If
2408 * the flush returns error, store it to report later and continue.
2410 * BB: This should be smarter. Why bother flushing pages that
2411 * will be truncated anyway? Also, should we error out here if
2412 * the flush returns error?
2414 rc = filemap_write_and_wait(inode->i_mapping);
2415 mapping_set_error(inode->i_mapping, rc);
2418 if (attrs->ia_valid & ATTR_SIZE) {
2419 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2421 goto cifs_setattr_exit;
2424 if (attrs->ia_valid & ATTR_UID)
2425 uid = attrs->ia_uid;
2427 if (attrs->ia_valid & ATTR_GID)
2428 gid = attrs->ia_gid;
2430 #ifdef CONFIG_CIFS_ACL
2431 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2432 if (uid_valid(uid) || gid_valid(gid)) {
2433 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2436 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2438 goto cifs_setattr_exit;
2442 #endif /* CONFIG_CIFS_ACL */
2443 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2444 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2446 /* skip mode change if it's just for clearing setuid/setgid */
2447 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2448 attrs->ia_valid &= ~ATTR_MODE;
2450 if (attrs->ia_valid & ATTR_MODE) {
2451 mode = attrs->ia_mode;
2453 #ifdef CONFIG_CIFS_ACL
2454 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2455 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2456 INVALID_UID, INVALID_GID);
2458 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2460 goto cifs_setattr_exit;
2463 #endif /* CONFIG_CIFS_ACL */
2464 if (((mode & S_IWUGO) == 0) &&
2465 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2467 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2469 /* fix up mode if we're not using dynperm */
2470 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2471 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2472 } else if ((mode & S_IWUGO) &&
2473 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2475 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2476 /* Attributes of 0 are ignored */
2478 dosattr |= ATTR_NORMAL;
2480 /* reset local inode permissions to normal */
2481 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2482 attrs->ia_mode &= ~(S_IALLUGO);
2483 if (S_ISDIR(inode->i_mode))
2485 cifs_sb->mnt_dir_mode;
2488 cifs_sb->mnt_file_mode;
2490 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2491 /* ignore mode change - ATTR_READONLY hasn't changed */
2492 attrs->ia_valid &= ~ATTR_MODE;
2496 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2497 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2498 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2499 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2501 /* Even if error on time set, no sense failing the call if
2502 the server would set the time to a reasonable value anyway,
2503 and this check ensures that we are not being called from
2504 sys_utimes in which case we ought to fail the call back to
2505 the user when the server rejects the call */
2506 if ((rc) && (attrs->ia_valid &
2507 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2511 /* do not need local check to inode_check_ok since the server does
2514 goto cifs_setattr_exit;
2516 if ((attrs->ia_valid & ATTR_SIZE) &&
2517 attrs->ia_size != i_size_read(inode))
2518 truncate_setsize(inode, attrs->ia_size);
2520 setattr_copy(inode, attrs);
2521 mark_inode_dirty(inode);
2530 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2532 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
2533 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2535 if (pTcon->unix_ext)
2536 return cifs_setattr_unix(direntry, attrs);
2538 return cifs_setattr_nounix(direntry, attrs);
2540 /* BB: add cifs_setattr_legacy for really old servers */
2544 void cifs_delete_inode(struct inode *inode)
2546 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2547 /* may have to add back in if and when safe distributed caching of
2548 directories added e.g. via FindNotify */