blob: df9377828e2f435bdb32339f48c2ad08ac9bdd32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
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.
11 *
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.
16 *
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
20 */
21#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pagemap.h>
Jeff Layton4f73c7d2014-04-30 09:31:47 -040025#include <linux/freezer.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Ingo Molnar5dd43ce2017-06-20 12:19:09 +020027#include <linux/wait_bit.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/div64.h>
30#include "cifsfs.h"
31#include "cifspdu.h"
32#include "cifsglob.h"
33#include "cifsproto.h"
34#include "cifs_debug.h"
35#include "cifs_fs_sb.h"
Steve French2baa2682014-09-27 02:19:01 -050036#include "cifs_unicode.h"
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +053037#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Christoph Hellwig70eff552008-02-15 20:55:05 +000039
David Howells01c64fe2011-01-14 18:45:47 +000040static void cifs_set_ops(struct inode *inode)
Christoph Hellwig70eff552008-02-15 20:55:05 +000041{
42 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
43
44 switch (inode->i_mode & S_IFMT) {
45 case S_IFREG:
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;
50 else
51 inode->i_fop = &cifs_file_direct_ops;
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +030052 } 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;
55 else
56 inode->i_fop = &cifs_file_strict_ops;
Christoph Hellwig70eff552008-02-15 20:55:05 +000057 } 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;
61 }
62
Christoph Hellwig70eff552008-02-15 20:55:05 +000063 /* check if server can support readpages */
Jeff Layton0d424ad2010-09-20 16:01:35 -070064 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030065 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
Christoph Hellwig70eff552008-02-15 20:55:05 +000066 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
67 else
68 inode->i_data.a_ops = &cifs_addr_ops;
69 break;
70 case S_IFDIR:
Steve Frenchbc5b6e22008-03-11 21:07:48 +000071#ifdef CONFIG_CIFS_DFS_UPCALL
David Howells01c64fe2011-01-14 18:45:47 +000072 if (IS_AUTOMOUNT(inode)) {
Igor Mammedov79626702008-03-09 03:44:18 +000073 inode->i_op = &cifs_dfs_referral_inode_operations;
74 } else {
Steve Frenchbc5b6e22008-03-11 21:07:48 +000075#else /* NO DFS support, treat as a directory */
76 {
77#endif
Igor Mammedov79626702008-03-09 03:44:18 +000078 inode->i_op = &cifs_dir_inode_ops;
79 inode->i_fop = &cifs_dir_ops;
80 }
Christoph Hellwig70eff552008-02-15 20:55:05 +000081 break;
82 case S_IFLNK:
83 inode->i_op = &cifs_symlink_inode_ops;
84 break;
85 default:
86 init_special_inode(inode, inode->i_mode, inode->i_rdev);
87 break;
88 }
89}
90
Jeff Laytondf2cf172010-02-12 07:44:16 -050091/* check inode attributes against fattr. If they don't match, tag the
92 * inode for cache invalidation
93 */
94static void
95cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
96{
97 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
98
Joe Perchesf96637b2013-05-04 22:12:25 -050099 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
100 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500101
102 if (inode->i_state & I_NEW) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500103 cifs_dbg(FYI, "%s: inode %llu is new\n",
104 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500105 return;
106 }
107
108 /* don't bother with revalidation if we have an oplock */
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400109 if (CIFS_CACHE_READ(cifs_i)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500110 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
111 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500112 return;
113 }
114
115 /* revalidate if mtime or size have changed */
Arnd Bergmann95390202018-06-19 17:27:58 +0200116 if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
Jeff Laytondf2cf172010-02-12 07:44:16 -0500117 cifs_i->server_eof == fattr->cf_eof) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500118 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
119 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500120 return;
121 }
122
Joe Perchesf96637b2013-05-04 22:12:25 -0500123 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
124 __func__, cifs_i->uniqueid);
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400125 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500126}
127
Jim McDonough74d290d2013-09-21 10:36:10 -0500128/*
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
131 */
132static void
133cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
134{
135 /*
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
139 */
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)
144 set_nlink(inode, 2);
145 else
146 set_nlink(inode, 1);
147 }
148 return;
149 }
150
151 /* we trust the server, so update it */
152 set_nlink(inode, fattr->cf_nlink);
153}
154
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400155/* populate an inode with info from a cifs_fattr struct */
156void
157cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
Christoph Hellwig75f12982008-02-25 20:25:21 +0000158{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400159 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400160 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000161
Jeff Laytondf2cf172010-02-12 07:44:16 -0500162 cifs_revalidate_cache(inode, fattr);
163
Steve Frenchb7ca6922012-08-03 08:43:01 -0500164 spin_lock(&inode->i_lock);
Steve French9b9c5be2018-09-22 12:07:06 -0500165 /* we do not want atime to be less than mtime, it broke some apps */
166 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime))
167 inode->i_atime = fattr->cf_mtime;
168 else
169 inode->i_atime = fattr->cf_atime;
Arnd Bergmann95390202018-06-19 17:27:58 +0200170 inode->i_mtime = fattr->cf_mtime;
171 inode->i_ctime = fattr->cf_ctime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400172 inode->i_rdev = fattr->cf_rdev;
Jim McDonough74d290d2013-09-21 10:36:10 -0500173 cifs_nlink_fattr_to_inode(inode, fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400174 inode->i_uid = fattr->cf_uid;
175 inode->i_gid = fattr->cf_gid;
176
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400177 /* if dynperm is set, don't clobber existing mode */
178 if (inode->i_state & I_NEW ||
179 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
180 inode->i_mode = fattr->cf_mode;
181
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400182 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400183
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400184 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
185 cifs_i->time = 0;
186 else
187 cifs_i->time = jiffies;
188
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400189 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
190 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
191 else
192 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000193
Jeff Layton835a36c2010-02-10 16:21:33 -0500194 cifs_i->server_eof = fattr->cf_eof;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000195 /*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400196 * Can't safely change the file size here if the client is writing to
197 * it due to potential races.
Christoph Hellwig75f12982008-02-25 20:25:21 +0000198 */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400199 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
200 i_size_write(inode, fattr->cf_eof);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000201
202 /*
203 * i_blocks is not related to (i_size / i_blksize),
204 * but instead 512 byte (2**9) size is required for
205 * calculating num blocks.
206 */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400207 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000208 }
209 spin_unlock(&inode->i_lock);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400210
David Howells01c64fe2011-01-14 18:45:47 +0000211 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
212 inode->i_flags |= S_AUTOMOUNT;
Jeff Laytonc2b93e02013-05-07 11:28:31 -0400213 if (inode->i_state & I_NEW)
214 cifs_set_ops(inode);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000215}
216
Jeff Layton4065c802010-05-17 07:18:58 -0400217void
218cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
219{
220 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
221
222 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
223 return;
224
225 fattr->cf_uniqueid = iunique(sb, ROOT_I);
226}
227
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400228/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
229void
230cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
231 struct cifs_sb_info *cifs_sb)
232{
233 memset(fattr, 0, sizeof(*fattr));
234 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
235 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
236 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
237
238 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
239 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
240 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
Steve French6e70e262017-09-21 21:32:29 -0500241 /* old POSIX extensions don't get create time */
242
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400243 fattr->cf_mode = le64_to_cpu(info->Permissions);
244
245 /*
246 * Since we set the inode type below we need to mask off
247 * to avoid strange results if bits set above.
248 */
249 fattr->cf_mode &= ~S_IFMT;
250 switch (le32_to_cpu(info->Type)) {
251 case UNIX_FILE:
252 fattr->cf_mode |= S_IFREG;
253 fattr->cf_dtype = DT_REG;
254 break;
255 case UNIX_SYMLINK:
256 fattr->cf_mode |= S_IFLNK;
257 fattr->cf_dtype = DT_LNK;
258 break;
259 case UNIX_DIR:
260 fattr->cf_mode |= S_IFDIR;
261 fattr->cf_dtype = DT_DIR;
262 break;
263 case UNIX_CHARDEV:
264 fattr->cf_mode |= S_IFCHR;
265 fattr->cf_dtype = DT_CHR;
266 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
267 le64_to_cpu(info->DevMinor) & MINORMASK);
268 break;
269 case UNIX_BLOCKDEV:
270 fattr->cf_mode |= S_IFBLK;
271 fattr->cf_dtype = DT_BLK;
272 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
273 le64_to_cpu(info->DevMinor) & MINORMASK);
274 break;
275 case UNIX_FIFO:
276 fattr->cf_mode |= S_IFIFO;
277 fattr->cf_dtype = DT_FIFO;
278 break;
279 case UNIX_SOCKET:
280 fattr->cf_mode |= S_IFSOCK;
281 fattr->cf_dtype = DT_SOCK;
282 break;
283 default:
284 /* safest to call it a file if we do not know */
285 fattr->cf_mode |= S_IFREG;
286 fattr->cf_dtype = DT_REG;
Joe Perchesf96637b2013-05-04 22:12:25 -0500287 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400288 break;
289 }
290
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800291 fattr->cf_uid = cifs_sb->mnt_uid;
292 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
293 u64 id = le64_to_cpu(info->Uid);
Eric W. Biederman4a2c8cf2013-02-06 01:53:25 -0800294 if (id < ((uid_t)-1)) {
295 kuid_t uid = make_kuid(&init_user_ns, id);
296 if (uid_valid(uid))
297 fattr->cf_uid = uid;
298 }
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800299 }
300
301 fattr->cf_gid = cifs_sb->mnt_gid;
302 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
303 u64 id = le64_to_cpu(info->Gid);
Eric W. Biederman4a2c8cf2013-02-06 01:53:25 -0800304 if (id < ((gid_t)-1)) {
305 kgid_t gid = make_kgid(&init_user_ns, id);
306 if (gid_valid(gid))
307 fattr->cf_gid = gid;
308 }
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800309 }
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400310
311 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
312}
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000313
314/*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400315 * Fill a cifs_fattr struct with fake inode info.
316 *
317 * Needed to setup cifs_fattr data for the directory which is the
318 * junction to the new submount (ie to setup the fake directory
319 * which represents a DFS referral).
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000320 */
Steve Frenchf1230c92009-07-22 23:13:01 +0000321static void
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400322cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
Steve French0e4bbde2008-05-20 19:50:46 +0000323{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400324 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French0e4bbde2008-05-20 19:50:46 +0000325
Joe Perchesf96637b2013-05-04 22:12:25 -0500326 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
Steve French0e4bbde2008-05-20 19:50:46 +0000327
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400328 memset(fattr, 0, sizeof(*fattr));
329 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
330 fattr->cf_uid = cifs_sb->mnt_uid;
331 fattr->cf_gid = cifs_sb->mnt_gid;
Arnd Bergmann95390202018-06-19 17:27:58 +0200332 ktime_get_real_ts64(&fattr->cf_mtime);
333 fattr->cf_mtime = timespec64_trunc(fattr->cf_mtime, sb->s_time_gran);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700334 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400335 fattr->cf_nlink = 2;
Steve French97aa4952018-11-15 00:33:05 -0600336 fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
Steve French0e4bbde2008-05-20 19:50:46 +0000337}
338
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700339static int
340cifs_get_file_info_unix(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -0500341{
342 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400343 unsigned int xid;
Jeff Laytonabab0952010-02-12 07:44:18 -0500344 FILE_UNIX_BASIC_INFO find_data;
345 struct cifs_fattr fattr;
Al Viro496ad9a2013-01-23 17:07:38 -0500346 struct inode *inode = file_inode(filp);
Jeff Laytonabab0952010-02-12 07:44:18 -0500347 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Joe Perchesc21dfb62010-07-12 13:50:14 -0700348 struct cifsFileInfo *cfile = filp->private_data;
Steve French96daf2b2011-05-27 04:34:02 +0000349 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Jeff Laytonabab0952010-02-12 07:44:18 -0500350
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400351 xid = get_xid();
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700352 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
Jeff Laytonabab0952010-02-12 07:44:18 -0500353 if (!rc) {
354 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
355 } else if (rc == -EREMOTE) {
356 cifs_create_dfs_fattr(&fattr, inode->i_sb);
357 rc = 0;
358 }
359
360 cifs_fattr_to_inode(inode, &fattr);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400361 free_xid(xid);
Jeff Laytonabab0952010-02-12 07:44:18 -0500362 return rc;
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365int cifs_get_inode_info_unix(struct inode **pinode,
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400366 const unsigned char *full_path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400367 struct super_block *sb, unsigned int xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400369 int rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000370 FILE_UNIX_BASIC_INFO find_data;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400371 struct cifs_fattr fattr;
Steve French96daf2b2011-05-27 04:34:02 +0000372 struct cifs_tcon *tcon;
Jeff Layton7ffec372010-09-29 19:51:11 -0400373 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Joe Perchesf96637b2013-05-04 22:12:25 -0500376 cifs_dbg(FYI, "Getting info on %s\n", full_path);
Igor Mammedov79626702008-03-09 03:44:18 +0000377
Jeff Layton7ffec372010-09-29 19:51:11 -0400378 tlink = cifs_sb_tlink(cifs_sb);
379 if (IS_ERR(tlink))
380 return PTR_ERR(tlink);
381 tcon = tlink_tcon(tlink);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* could have done a find first instead but this returns more info */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400384 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +0900385 cifs_sb->local_nls, cifs_remap(cifs_sb));
Jeff Layton7ffec372010-09-29 19:51:11 -0400386 cifs_put_tlink(tlink);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400387
388 if (!rc) {
389 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
390 } else if (rc == -EREMOTE) {
391 cifs_create_dfs_fattr(&fattr, sb);
Jeff Laytone911d0c2008-07-12 13:47:59 -0700392 rc = 0;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400393 } else {
394 return rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000395 }
396
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200397 /* check for Minshall+French symlinks */
398 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
Sachin Prabhucb084b12013-11-25 17:09:50 +0000399 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
400 full_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200401 if (tmprc)
Sachin Prabhucb084b12013-11-25 17:09:50 +0000402 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200403 }
404
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400405 if (*pinode == NULL) {
406 /* get new inode */
Jeff Layton4065c802010-05-17 07:18:58 -0400407 cifs_fill_uniqueid(sb, &fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400408 *pinode = cifs_iget(sb, &fattr);
409 if (!*pinode)
410 rc = -ENOMEM;
411 } else {
412 /* we already have inode, update it */
Nakajima Akira7196ac12015-04-22 15:24:44 +0900413
414 /* if uniqueid is different, return error */
415 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
416 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
Pavel Shilovskyc82e5ac2019-09-30 10:06:19 -0700417 CIFS_I(*pinode)->time = 0; /* force reval */
Nakajima Akira7196ac12015-04-22 15:24:44 +0900418 rc = -ESTALE;
419 goto cgiiu_exit;
420 }
421
422 /* if filetype is different, return error */
423 if (unlikely(((*pinode)->i_mode & S_IFMT) !=
424 (fattr.cf_mode & S_IFMT))) {
Pavel Shilovskyc82e5ac2019-09-30 10:06:19 -0700425 CIFS_I(*pinode)->time = 0; /* force reval */
Nakajima Akira7196ac12015-04-22 15:24:44 +0900426 rc = -ESTALE;
427 goto cgiiu_exit;
428 }
429
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400430 cifs_fattr_to_inode(*pinode, &fattr);
431 }
Steve French0e4bbde2008-05-20 19:50:46 +0000432
Nakajima Akira7196ac12015-04-22 15:24:44 +0900433cgiiu_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return rc;
435}
436
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400437static int
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400438cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400439 struct cifs_sb_info *cifs_sb, unsigned int xid)
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800440{
441 int rc;
Steve Frenchdb8b6312014-09-22 05:13:55 -0500442 __u32 oplock;
Jeff Layton7ffec372010-09-29 19:51:11 -0400443 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000444 struct cifs_tcon *tcon;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400445 struct cifs_fid fid;
446 struct cifs_open_parms oparms;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +0000447 struct cifs_io_parms io_parms;
Steve French86c96b42005-11-18 20:25:31 -0800448 char buf[24];
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800449 unsigned int bytes_read;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000450 char *pbuf;
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400451 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800452
453 pbuf = buf;
454
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400455 fattr->cf_mode &= ~S_IFMT;
456
457 if (fattr->cf_eof == 0) {
458 fattr->cf_mode |= S_IFIFO;
459 fattr->cf_dtype = DT_FIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800460 return 0;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400461 } else if (fattr->cf_eof < 8) {
462 fattr->cf_mode |= S_IFREG;
463 fattr->cf_dtype = DT_REG;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800464 return -EINVAL; /* EOPNOTSUPP? */
465 }
Steve French50c2f752007-07-13 00:33:32 +0000466
Jeff Layton7ffec372010-09-29 19:51:11 -0400467 tlink = cifs_sb_tlink(cifs_sb);
468 if (IS_ERR(tlink))
469 return PTR_ERR(tlink);
470 tcon = tlink_tcon(tlink);
471
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400472 oparms.tcon = tcon;
473 oparms.cifs_sb = cifs_sb;
474 oparms.desired_access = GENERIC_READ;
475 oparms.create_options = CREATE_NOT_DIR;
Steve French5e196972018-08-27 17:04:13 -0500476 if (backup_cred(cifs_sb))
477 oparms.create_options |= CREATE_OPEN_BACKUP_INTENT;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400478 oparms.disposition = FILE_OPEN;
479 oparms.path = path;
480 oparms.fid = &fid;
481 oparms.reconnect = false;
482
Steve Frenchdb8b6312014-09-22 05:13:55 -0500483 if (tcon->ses->server->oplocks)
484 oplock = REQ_OPLOCK;
485 else
486 oplock = 0;
487 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400488 if (rc) {
Steve Frenchdb8b6312014-09-22 05:13:55 -0500489 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400490 cifs_put_tlink(tlink);
491 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800492 }
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400493
494 /* Read header */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400495 io_parms.netfid = fid.netfid;
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400496 io_parms.pid = current->tgid;
497 io_parms.tcon = tcon;
498 io_parms.offset = 0;
499 io_parms.length = 24;
500
Steve Frenchdb8b6312014-09-22 05:13:55 -0500501 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
502 &bytes_read, &pbuf, &buf_type);
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400503 if ((rc == 0) && (bytes_read >= 8)) {
504 if (memcmp("IntxBLK", pbuf, 8) == 0) {
505 cifs_dbg(FYI, "Block device\n");
506 fattr->cf_mode |= S_IFBLK;
507 fattr->cf_dtype = DT_BLK;
508 if (bytes_read == 24) {
509 /* we have enough to decode dev num */
510 __u64 mjr; /* major */
511 __u64 mnr; /* minor */
512 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
513 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
514 fattr->cf_rdev = MKDEV(mjr, mnr);
515 }
516 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
517 cifs_dbg(FYI, "Char device\n");
518 fattr->cf_mode |= S_IFCHR;
519 fattr->cf_dtype = DT_CHR;
520 if (bytes_read == 24) {
521 /* we have enough to decode dev num */
522 __u64 mjr; /* major */
523 __u64 mnr; /* minor */
524 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
525 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
526 fattr->cf_rdev = MKDEV(mjr, mnr);
527 }
528 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
529 cifs_dbg(FYI, "Symlink\n");
530 fattr->cf_mode |= S_IFLNK;
531 fattr->cf_dtype = DT_LNK;
532 } else {
533 fattr->cf_mode |= S_IFREG; /* file? */
534 fattr->cf_dtype = DT_REG;
535 rc = -EOPNOTSUPP;
536 }
537 } else {
538 fattr->cf_mode |= S_IFREG; /* then it is a file */
539 fattr->cf_dtype = DT_REG;
540 rc = -EOPNOTSUPP; /* or some unknown SFU type */
541 }
Steve Frenchdb8b6312014-09-22 05:13:55 -0500542
543 tcon->ses->server->ops->close(xid, tcon, &fid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400544 cifs_put_tlink(tlink);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800545 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800546}
547
Steve French9e294f12005-11-17 16:59:21 -0800548#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
549
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400550/*
551 * Fetch mode bits as provided by SFU.
552 *
553 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
554 */
555static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400556 struct cifs_sb_info *cifs_sb, unsigned int xid)
Steve French9e294f12005-11-17 16:59:21 -0800557{
Steve French3020a1f2005-11-18 11:31:10 -0800558#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800559 ssize_t rc;
560 char ea_value[4];
561 __u32 mode;
Jeff Layton7ffec372010-09-29 19:51:11 -0400562 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000563 struct cifs_tcon *tcon;
Steve French9e294f12005-11-17 16:59:21 -0800564
Jeff Layton7ffec372010-09-29 19:51:11 -0400565 tlink = cifs_sb_tlink(cifs_sb);
566 if (IS_ERR(tlink))
567 return PTR_ERR(tlink);
568 tcon = tlink_tcon(tlink);
569
Steve Frenchd979f3b2014-02-01 23:27:18 -0600570 if (tcon->ses->server->ops->query_all_EAs == NULL) {
571 cifs_put_tlink(tlink);
572 return -EOPNOTSUPP;
573 }
574
575 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
576 "SETFILEBITS", ea_value, 4 /* size of buf */,
Steve French67b4c882017-05-12 20:59:10 -0500577 cifs_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400578 cifs_put_tlink(tlink);
Steve French4523cc32007-04-30 20:13:06 +0000579 if (rc < 0)
Steve French9e294f12005-11-17 16:59:21 -0800580 return (int)rc;
581 else if (rc > 3) {
582 mode = le32_to_cpu(*((__le32 *)ea_value));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400583 fattr->cf_mode &= ~SFBITS_MASK;
Joe Perchesf96637b2013-05-04 22:12:25 -0500584 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
585 mode, fattr->cf_mode);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400586 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -0500587 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
Steve French9e294f12005-11-17 16:59:21 -0800588 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400589
590 return 0;
Steve French3020a1f2005-11-18 11:31:10 -0800591#else
592 return -EOPNOTSUPP;
593#endif
Steve French9e294f12005-11-17 16:59:21 -0800594}
595
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400596/* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
Steve Frenchf1230c92009-07-22 23:13:01 +0000597static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400598cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700599 struct super_block *sb, bool adjust_tz,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400600 bool symlink)
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000601{
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700602 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French96daf2b2011-05-27 04:34:02 +0000603 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Jeff Layton0d424ad2010-09-20 16:01:35 -0700604
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400605 memset(fattr, 0, sizeof(*fattr));
606 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
607 if (info->DeletePending)
608 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000609
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400610 if (info->LastAccessTime)
611 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700612 else {
Arnd Bergmann95390202018-06-19 17:27:58 +0200613 ktime_get_real_ts64(&fattr->cf_atime);
614 fattr->cf_atime = timespec64_trunc(fattr->cf_atime, sb->s_time_gran);
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700615 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400616
617 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
618 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
619
620 if (adjust_tz) {
Jeff Layton0d424ad2010-09-20 16:01:35 -0700621 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
622 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400623 }
624
625 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
626 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
Jeff Layton20054bd2011-01-07 11:30:27 -0500627 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400628
Jim McDonough74d290d2013-09-21 10:36:10 -0500629 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400630
631 if (symlink) {
632 fattr->cf_mode = S_IFLNK;
633 fattr->cf_dtype = DT_LNK;
634 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400635 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
636 fattr->cf_dtype = DT_DIR;
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +0300637 /*
638 * Server can return wrong NumberOfLinks value for directories
639 * when Unix extensions are disabled - fake it.
640 */
Jim McDonough74d290d2013-09-21 10:36:10 -0500641 if (!tcon->unix_ext)
642 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400643 } else {
644 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
645 fattr->cf_dtype = DT_REG;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400646
Jeff Laytond0c280d2009-07-09 01:46:44 -0400647 /* clear write bits if ATTR_READONLY is set */
648 if (fattr->cf_cifsattrs & ATTR_READONLY)
649 fattr->cf_mode &= ~(S_IWUGO);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400650
Jim McDonough74d290d2013-09-21 10:36:10 -0500651 /*
652 * Don't accept zero nlink from non-unix servers unless
653 * delete is pending. Instead mark it as unknown.
654 */
655 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
656 !info->DeletePending) {
657 cifs_dbg(1, "bogus file nlink value %u\n",
Steve French6658b9f2013-07-04 14:38:48 -0500658 fattr->cf_nlink);
Jim McDonough74d290d2013-09-21 10:36:10 -0500659 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
Steve French6658b9f2013-07-04 14:38:48 -0500660 }
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +0300661 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400662
663 fattr->cf_uid = cifs_sb->mnt_uid;
664 fattr->cf_gid = cifs_sb->mnt_gid;
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000665}
666
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700667static int
668cifs_get_file_info(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -0500669{
670 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400671 unsigned int xid;
Jeff Laytonabab0952010-02-12 07:44:18 -0500672 FILE_ALL_INFO find_data;
673 struct cifs_fattr fattr;
Al Viro496ad9a2013-01-23 17:07:38 -0500674 struct inode *inode = file_inode(filp);
Joe Perchesc21dfb62010-07-12 13:50:14 -0700675 struct cifsFileInfo *cfile = filp->private_data;
Steve French96daf2b2011-05-27 04:34:02 +0000676 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700677 struct TCP_Server_Info *server = tcon->ses->server;
678
679 if (!server->ops->query_file_info)
680 return -ENOSYS;
Jeff Laytonabab0952010-02-12 07:44:18 -0500681
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400682 xid = get_xid();
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700683 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400684 switch (rc) {
685 case 0:
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700686 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400687 false);
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400688 break;
689 case -EREMOTE:
690 cifs_create_dfs_fattr(&fattr, inode->i_sb);
691 rc = 0;
692 break;
693 case -EOPNOTSUPP:
694 case -EINVAL:
Jeff Laytonabab0952010-02-12 07:44:18 -0500695 /*
696 * FIXME: legacy server -- fall back to path-based call?
Steve Frenchff2157132010-03-09 20:30:42 +0000697 * for now, just skip revalidating and mark inode for
698 * immediate reval.
699 */
Jeff Laytonabab0952010-02-12 07:44:18 -0500700 rc = 0;
701 CIFS_I(inode)->time = 0;
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400702 default:
Jeff Laytonabab0952010-02-12 07:44:18 -0500703 goto cgfi_exit;
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400704 }
Jeff Laytonabab0952010-02-12 07:44:18 -0500705
706 /*
707 * don't bother with SFU junk here -- just mark inode as needing
708 * revalidation.
709 */
Jeff Laytonabab0952010-02-12 07:44:18 -0500710 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
711 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
712 cifs_fattr_to_inode(inode, &fattr);
713cgfi_exit:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400714 free_xid(xid);
Jeff Laytonabab0952010-02-12 07:44:18 -0500715 return rc;
716}
717
Steve French7ea884c2018-03-31 18:13:38 -0500718/* Simple function to return a 64 bit hash of string. Rarely called */
719static __u64 simple_hashstr(const char *str)
720{
Geert Uytterhoeven3995bbf2018-04-05 14:57:11 +0200721 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
Steve French7ea884c2018-03-31 18:13:38 -0500722 __u64 hash = 0;
723
724 while (*str)
725 hash = (hash + (__u64) *str++) * hash_mult;
726
727 return hash;
728}
729
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400730int
731cifs_get_inode_info(struct inode **inode, const char *full_path,
732 FILE_ALL_INFO *data, struct super_block *sb, int xid,
Steve French42eacf92014-02-10 14:08:16 -0600733 const struct cifs_fid *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500735 __u16 srchflgs;
736 int rc = 0, tmprc = ENOSYS;
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400737 struct cifs_tcon *tcon;
738 struct TCP_Server_Info *server;
Jeff Layton7ffec372010-09-29 19:51:11 -0400739 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 char *buf = NULL;
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400742 bool adjust_tz = false;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400743 struct cifs_fattr fattr;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500744 struct cifs_search_info *srchinf = NULL;
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400745 bool symlink = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Jeff Layton7ffec372010-09-29 19:51:11 -0400747 tlink = cifs_sb_tlink(cifs_sb);
748 if (IS_ERR(tlink))
749 return PTR_ERR(tlink);
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400750 tcon = tlink_tcon(tlink);
751 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -0400752
Joe Perchesf96637b2013-05-04 22:12:25 -0500753 cifs_dbg(FYI, "Getting info on %s\n", full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400755 if ((data == NULL) && (*inode != NULL)) {
Ronnie Sahlberg57c55cd2018-05-24 06:54:27 +1000756 if (CIFS_CACHE_READ(CIFS_I(*inode)) &&
757 CIFS_I(*inode)->time != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500758 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
Jeff Layton7ffec372010-09-29 19:51:11 -0400759 goto cgii_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
762
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400763 /* if inode info is not passed, get it from server */
764 if (data == NULL) {
765 if (!server->ops->query_path_info) {
766 rc = -ENOSYS;
767 goto cgii_exit;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Jeff Layton7ffec372010-09-29 19:51:11 -0400770 if (buf == NULL) {
771 rc = -ENOMEM;
772 goto cgii_exit;
773 }
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400774 data = (FILE_ALL_INFO *)buf;
775 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400776 data, &adjust_tz, &symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400778
779 if (!rc) {
Deepa Dinamanie37fea52017-05-08 15:59:16 -0700780 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400781 symlink);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400782 } else if (rc == -EREMOTE) {
783 cifs_create_dfs_fattr(&fattr, sb);
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000784 rc = 0;
Steve French1e77a8c2018-10-19 00:45:21 -0500785 } else if ((rc == -EACCES) && backup_cred(cifs_sb) &&
786 (strcmp(server->vals->version_string, SMB1_VERSION_STRING)
787 == 0)) {
Steve French3b7960c2018-10-19 01:58:22 -0500788 /*
789 * For SMB2 and later the backup intent flag is already
790 * sent if needed on open and there is no path based
791 * FindFirst operation to use to retry with
792 */
Steve French1e77a8c2018-10-19 00:45:21 -0500793
Steve French3b7960c2018-10-19 01:58:22 -0500794 srchinf = kzalloc(sizeof(struct cifs_search_info),
795 GFP_KERNEL);
796 if (srchinf == NULL) {
797 rc = -ENOMEM;
798 goto cgii_exit;
799 }
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500800
Steve French3b7960c2018-10-19 01:58:22 -0500801 srchinf->endOfSearch = false;
802 if (tcon->unix_ext)
803 srchinf->info_level = SMB_FIND_FILE_UNIX;
804 else if ((tcon->ses->capabilities &
805 tcon->ses->server->vals->cap_nt_find) == 0)
806 srchinf->info_level = SMB_FIND_FILE_INFO_STANDARD;
807 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500808 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
Steve French3b7960c2018-10-19 01:58:22 -0500809 else /* no srvino useful for fallback to some netapp */
810 srchinf->info_level = SMB_FIND_FILE_DIRECTORY_INFO;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500811
Steve French3b7960c2018-10-19 01:58:22 -0500812 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
813 CIFS_SEARCH_CLOSE_AT_END |
814 CIFS_SEARCH_BACKUP_SEARCH;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500815
Steve French3b7960c2018-10-19 01:58:22 -0500816 rc = CIFSFindFirst(xid, tcon, full_path,
817 cifs_sb, NULL, srchflgs, srchinf, false);
818 if (!rc) {
819 data = (FILE_ALL_INFO *)srchinf->srch_entries_start;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500820
Steve French3b7960c2018-10-19 01:58:22 -0500821 cifs_dir_info_to_fattr(&fattr,
822 (FILE_DIRECTORY_INFO *)data, cifs_sb);
823 fattr.cf_uniqueid = le64_to_cpu(
824 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500825
Steve French3b7960c2018-10-19 01:58:22 -0500826 cifs_buf_release(srchinf->ntwrk_buf_start);
827 }
828 kfree(srchinf);
829 if (rc)
830 goto cgii_exit;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500831 } else
Igor Mammedov79626702008-03-09 03:44:18 +0000832 goto cgii_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400834 /*
835 * If an inode wasn't passed in, then get the inode number
836 *
837 * Is an i_ino of zero legal? Can we use that to check if the server
838 * supports returning inode numbers? Are there other sanity checks we
839 * can use to ensure that the server is really filling in that field?
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400840 */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400841 if (*inode == NULL) {
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000842 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +0100843 if (server->ops->get_srv_inum)
844 tmprc = server->ops->get_srv_inum(xid,
845 tcon, cifs_sb, full_path,
846 &fattr.cf_uniqueid, data);
847 if (tmprc) {
848 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
849 tmprc);
850 fattr.cf_uniqueid = iunique(sb, ROOT_I);
851 cifs_autodisable_serverino(cifs_sb);
852 } else if ((fattr.cf_uniqueid == 0) &&
853 strlen(full_path) == 0) {
854 /* some servers ret bad root ino ie 0 */
855 cifs_dbg(FYI, "Invalid (0) inodenum\n");
856 fattr.cf_flags |=
857 CIFS_FATTR_FAKE_ROOT_INO;
858 fattr.cf_uniqueid =
859 simple_hashstr(tcon->treeName);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500860 }
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500861 } else
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400862 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Ross Lagerwalla108471b2015-12-02 14:46:08 +0000863 } else {
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +0100864 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
865 && server->ops->get_srv_inum) {
Ross Lagerwalla108471b2015-12-02 14:46:08 +0000866 /*
867 * Pass a NULL tcon to ensure we don't make a round
868 * trip to the server. This only works for SMB2+.
869 */
870 tmprc = server->ops->get_srv_inum(xid,
871 NULL, cifs_sb, full_path,
872 &fattr.cf_uniqueid, data);
873 if (tmprc)
874 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
Steve French7ea884c2018-03-31 18:13:38 -0500875 else if ((fattr.cf_uniqueid == 0) &&
876 strlen(full_path) == 0) {
877 /*
878 * Reuse existing root inode num since
879 * inum zero for root causes ls of . and .. to
880 * not be returned
881 */
882 cifs_dbg(FYI, "Srv ret 0 inode num for root\n");
883 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
884 }
Ross Lagerwalla108471b2015-12-02 14:46:08 +0000885 } else
886 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
887 }
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000888
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400889 /* query for SFU type info if supported and needed */
890 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
891 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
892 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
893 if (tmprc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500894 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000895 }
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000896
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000897 /* fill in 0777 bits from ACL */
Steve Frenche2f8fbf2019-07-19 06:30:07 +0000898 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
899 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
900 full_path, fid);
901 if (rc) {
902 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
903 __func__, rc);
904 goto cgii_exit;
905 }
906 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
907 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
908 full_path, fid);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -0600909 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500910 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
911 __func__, rc);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -0600912 goto cgii_exit;
913 }
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000914 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400915
916 /* fill in remaining high mode bits e.g. SUID, VTX */
917 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
918 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
919
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200920 /* check for Minshall+French symlinks */
921 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
Sachin Prabhucb084b12013-11-25 17:09:50 +0000922 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
923 full_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200924 if (tmprc)
Sachin Prabhucb084b12013-11-25 17:09:50 +0000925 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200926 }
927
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400928 if (!*inode) {
929 *inode = cifs_iget(sb, &fattr);
930 if (!*inode)
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400931 rc = -ENOMEM;
932 } else {
Nakajima Akira7196ac12015-04-22 15:24:44 +0900933 /* we already have inode, update it */
934
Ross Lagerwalla108471b2015-12-02 14:46:08 +0000935 /* if uniqueid is different, return error */
936 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
937 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
Pavel Shilovskyc82e5ac2019-09-30 10:06:19 -0700938 CIFS_I(*inode)->time = 0; /* force reval */
Ross Lagerwalla108471b2015-12-02 14:46:08 +0000939 rc = -ESTALE;
940 goto cgii_exit;
941 }
942
Nakajima Akira7196ac12015-04-22 15:24:44 +0900943 /* if filetype is different, return error */
944 if (unlikely(((*inode)->i_mode & S_IFMT) !=
945 (fattr.cf_mode & S_IFMT))) {
Pavel Shilovskyc82e5ac2019-09-30 10:06:19 -0700946 CIFS_I(*inode)->time = 0; /* force reval */
Nakajima Akira7196ac12015-04-22 15:24:44 +0900947 rc = -ESTALE;
948 goto cgii_exit;
949 }
950
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400951 cifs_fattr_to_inode(*inode, &fattr);
Steve Frenchb9a3260f2008-05-20 21:52:32 +0000952 }
953
Igor Mammedov79626702008-03-09 03:44:18 +0000954cgii_exit:
Steve French7ea884c2018-03-31 18:13:38 -0500955 if ((*inode) && ((*inode)->i_ino == 0))
956 cifs_dbg(FYI, "inode number of zero returned\n");
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 kfree(buf);
Jeff Layton7ffec372010-09-29 19:51:11 -0400959 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return rc;
961}
962
Steve French7f8ed422007-09-28 22:28:55 +0000963static const struct inode_operations cifs_ipc_inode_ops = {
964 .lookup = cifs_lookup,
965};
966
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400967static int
968cifs_find_inode(struct inode *inode, void *opaque)
969{
970 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
971
Jeff Laytonf30b9c12010-07-19 18:00:17 -0400972 /* don't match inode with different uniqueid */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400973 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
974 return 0;
975
Jeff Layton20054bd2011-01-07 11:30:27 -0500976 /* use createtime like an i_generation field */
977 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
978 return 0;
979
Jeff Laytonf30b9c12010-07-19 18:00:17 -0400980 /* don't match inode of different type */
981 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
982 return 0;
983
Jeff Layton5acfec22010-08-02 17:43:54 -0400984 /* if it's not a directory or has no dentries, then flag it */
Al Virob3d9b7a2012-06-09 13:51:19 -0400985 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
Jeff Layton3d694382010-05-11 14:59:55 -0400986 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
Jeff Layton3d694382010-05-11 14:59:55 -0400987
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400988 return 1;
989}
990
991static int
992cifs_init_inode(struct inode *inode, void *opaque)
993{
994 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
995
996 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
Jeff Layton20054bd2011-01-07 11:30:27 -0500997 CIFS_I(inode)->createtime = fattr->cf_createtime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400998 return 0;
999}
1000
Jeff Layton5acfec22010-08-02 17:43:54 -04001001/*
1002 * walk dentry list for an inode and report whether it has aliases that
1003 * are hashed. We use this to determine if a directory inode can actually
1004 * be used.
1005 */
1006static bool
1007inode_has_hashed_dentries(struct inode *inode)
1008{
1009 struct dentry *dentry;
1010
Nick Piggin873feea2011-01-07 17:50:06 +11001011 spin_lock(&inode->i_lock);
Al Viro946e51f2014-10-26 19:19:16 -04001012 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Jeff Layton5acfec22010-08-02 17:43:54 -04001013 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +11001014 spin_unlock(&inode->i_lock);
Jeff Layton5acfec22010-08-02 17:43:54 -04001015 return true;
1016 }
1017 }
Nick Piggin873feea2011-01-07 17:50:06 +11001018 spin_unlock(&inode->i_lock);
Jeff Layton5acfec22010-08-02 17:43:54 -04001019 return false;
1020}
1021
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001022/* Given fattrs, get a corresponding inode */
1023struct inode *
1024cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1025{
1026 unsigned long hash;
1027 struct inode *inode;
1028
Jeff Layton3d694382010-05-11 14:59:55 -04001029retry_iget5_locked:
Joe Perchesf96637b2013-05-04 22:12:25 -05001030 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001031
1032 /* hash down to 32-bits on 32-bit arch */
1033 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1034
1035 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001036 if (inode) {
Jeff Layton5acfec22010-08-02 17:43:54 -04001037 /* was there a potentially problematic inode collision? */
Jeff Layton3d694382010-05-11 14:59:55 -04001038 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
Jeff Layton3d694382010-05-11 14:59:55 -04001039 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
Jeff Layton5acfec22010-08-02 17:43:54 -04001040
1041 if (inode_has_hashed_dentries(inode)) {
1042 cifs_autodisable_serverino(CIFS_SB(sb));
1043 iput(inode);
1044 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1045 goto retry_iget5_locked;
1046 }
Jeff Layton3d694382010-05-11 14:59:55 -04001047 }
1048
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001049 cifs_fattr_to_inode(inode, fattr);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001050 if (sb->s_flags & SB_NOATIME)
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001051 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1052 if (inode->i_state & I_NEW) {
1053 inode->i_ino = hash;
Steve French0ccd4802010-07-16 04:31:02 +00001054#ifdef CONFIG_CIFS_FSCACHE
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +05301055 /* initialize per-inode cache cookie pointer */
1056 CIFS_I(inode)->fscache = NULL;
Steve French0ccd4802010-07-16 04:31:02 +00001057#endif
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001058 unlock_new_inode(inode);
1059 }
1060 }
1061
1062 return inode;
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/* gets root inode */
Shirish Pargaonkar9b6763e2011-02-21 23:56:59 -06001066struct inode *cifs_root_iget(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001068 unsigned int xid;
Jeff Layton0d424ad2010-09-20 16:01:35 -07001069 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001070 struct inode *inode = NULL;
David Howellsce634ab2008-02-07 00:15:33 -08001071 long rc;
Steve French96daf2b2011-05-27 04:34:02 +00001072 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Aurelien Aptela6b50582016-05-25 19:59:09 +02001073 char *path = NULL;
1074 int len;
1075
1076 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1077 && cifs_sb->prepath) {
1078 len = strlen(cifs_sb->prepath);
1079 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1080 if (path == NULL)
1081 return ERR_PTR(-ENOMEM);
1082 path[0] = '/';
1083 memcpy(path+1, cifs_sb->prepath, len);
1084 } else {
1085 path = kstrdup("", GFP_KERNEL);
1086 if (path == NULL)
1087 return ERR_PTR(-ENOMEM);
1088 }
David Howellsce634ab2008-02-07 00:15:33 -08001089
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001090 xid = get_xid();
Steve Frenchb5b374e2014-10-06 01:01:03 -05001091 if (tcon->unix_ext) {
Aurelien Aptela6b50582016-05-25 19:59:09 +02001092 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
Steve Frenchb5b374e2014-10-06 01:01:03 -05001093 /* some servers mistakenly claim POSIX support */
1094 if (rc != -EOPNOTSUPP)
1095 goto iget_no_retry;
1096 cifs_dbg(VFS, "server does not support POSIX extensions");
1097 tcon->unix_ext = false;
1098 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -04001099
Aurelien Aptela6b50582016-05-25 19:59:09 +02001100 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1101 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
Steve Frenchb5b374e2014-10-06 01:01:03 -05001102
1103iget_no_retry:
Oskar Schirmera7851ce2010-11-10 21:06:13 +00001104 if (!inode) {
1105 inode = ERR_PTR(rc);
1106 goto out;
1107 }
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001108
Steve French0ccd4802010-07-16 04:31:02 +00001109#ifdef CONFIG_CIFS_FSCACHE
Suresh Jayaramand03382c2010-07-05 18:12:27 +05301110 /* populate tcon->resource_id */
Jeff Layton0d424ad2010-09-20 16:01:35 -07001111 tcon->resource_id = CIFS_I(inode)->uniqueid;
Steve French0ccd4802010-07-16 04:31:02 +00001112#endif
Suresh Jayaramand03382c2010-07-05 18:12:27 +05301113
Aurelien Aptelb327a712018-01-24 13:46:10 +01001114 if (rc && tcon->pipe) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001115 cifs_dbg(FYI, "ipc connection - fake read inode\n");
Steve Frenchb7ca6922012-08-03 08:43:01 -05001116 spin_lock(&inode->i_lock);
Steve French7f8ed422007-09-28 22:28:55 +00001117 inode->i_mode |= S_IFDIR;
Miklos Szeredibfe86842011-10-28 14:13:29 +02001118 set_nlink(inode, 2);
Steve French7f8ed422007-09-28 22:28:55 +00001119 inode->i_op = &cifs_ipc_inode_ops;
1120 inode->i_fop = &simple_dir_operations;
1121 inode->i_uid = cifs_sb->mnt_uid;
1122 inode->i_gid = cifs_sb->mnt_gid;
Steve Frenchb7ca6922012-08-03 08:43:01 -05001123 spin_unlock(&inode->i_lock);
Steve Frenchad661332008-08-12 14:14:40 +00001124 } else if (rc) {
David Howellsce634ab2008-02-07 00:15:33 -08001125 iget_failed(inode);
Oskar Schirmera7851ce2010-11-10 21:06:13 +00001126 inode = ERR_PTR(rc);
Steve French7f8ed422007-09-28 22:28:55 +00001127 }
1128
Oskar Schirmera7851ce2010-11-10 21:06:13 +00001129out:
Aurelien Aptela6b50582016-05-25 19:59:09 +02001130 kfree(path);
Phillip Potter31cd1062018-03-17 21:06:56 +00001131 free_xid(xid);
David Howellsce634ab2008-02-07 00:15:33 -08001132 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001135int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001136cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001137 char *full_path, __u32 dosattr)
Steve French388e57b2008-09-16 23:50:58 +00001138{
Steve French388e57b2008-09-16 23:50:58 +00001139 bool set_time = false;
Steve French388e57b2008-09-16 23:50:58 +00001140 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001141 struct TCP_Server_Info *server;
Steve French388e57b2008-09-16 23:50:58 +00001142 FILE_BASIC_INFO info_buf;
1143
Steve French1adcb712009-02-25 14:19:56 +00001144 if (attrs == NULL)
1145 return -EINVAL;
1146
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001147 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1148 if (!server->ops->set_file_info)
1149 return -ENOSYS;
1150
Steve Frenchfd09b7d2018-08-02 20:28:18 -05001151 info_buf.Pad = 0;
1152
Steve French388e57b2008-09-16 23:50:58 +00001153 if (attrs->ia_valid & ATTR_ATIME) {
1154 set_time = true;
1155 info_buf.LastAccessTime =
Arnd Bergmann95390202018-06-19 17:27:58 +02001156 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
Steve French388e57b2008-09-16 23:50:58 +00001157 } else
1158 info_buf.LastAccessTime = 0;
1159
1160 if (attrs->ia_valid & ATTR_MTIME) {
1161 set_time = true;
1162 info_buf.LastWriteTime =
Arnd Bergmann95390202018-06-19 17:27:58 +02001163 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
Steve French388e57b2008-09-16 23:50:58 +00001164 } else
1165 info_buf.LastWriteTime = 0;
1166
1167 /*
1168 * Samba throws this field away, but windows may actually use it.
1169 * Do not set ctime unless other time stamps are changed explicitly
1170 * (i.e. by utimes()) since we would then have a mix of client and
1171 * server times.
1172 */
1173 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001174 cifs_dbg(FYI, "CIFS - CTIME changed\n");
Steve French388e57b2008-09-16 23:50:58 +00001175 info_buf.ChangeTime =
Arnd Bergmann95390202018-06-19 17:27:58 +02001176 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
Steve French388e57b2008-09-16 23:50:58 +00001177 } else
1178 info_buf.ChangeTime = 0;
1179
1180 info_buf.CreationTime = 0; /* don't change */
1181 info_buf.Attributes = cpu_to_le32(dosattr);
1182
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001183 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
Steve French388e57b2008-09-16 23:50:58 +00001184}
1185
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001186/*
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001187 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001188 * and rename it to a random name that hopefully won't conflict with
1189 * anything else.
1190 */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001191int
1192cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1193 const unsigned int xid)
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001194{
1195 int oplock = 0;
1196 int rc;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001197 struct cifs_fid fid;
1198 struct cifs_open_parms oparms;
David Howells2b0143b2015-03-17 22:25:59 +00001199 struct inode *inode = d_inode(dentry);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001200 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1201 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001202 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001203 struct cifs_tcon *tcon;
Steve French32709582008-10-20 00:44:19 +00001204 __u32 dosattr, origattr;
1205 FILE_BASIC_INFO *info_buf = NULL;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001206
Jeff Layton7ffec372010-09-29 19:51:11 -04001207 tlink = cifs_sb_tlink(cifs_sb);
1208 if (IS_ERR(tlink))
1209 return PTR_ERR(tlink);
1210 tcon = tlink_tcon(tlink);
1211
Sachin Prabhuc483a982013-03-05 19:25:56 +00001212 /*
1213 * We cannot rename the file if the server doesn't support
1214 * CAP_INFOLEVEL_PASSTHRU
1215 */
1216 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1217 rc = -EBUSY;
1218 goto out;
1219 }
1220
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001221 oparms.tcon = tcon;
1222 oparms.cifs_sb = cifs_sb;
1223 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1224 oparms.create_options = CREATE_NOT_DIR;
1225 oparms.disposition = FILE_OPEN;
1226 oparms.path = full_path;
1227 oparms.fid = &fid;
1228 oparms.reconnect = false;
1229
1230 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001231 if (rc != 0)
1232 goto out;
1233
Steve French32709582008-10-20 00:44:19 +00001234 origattr = cifsInode->cifsAttrs;
1235 if (origattr == 0)
1236 origattr |= ATTR_NORMAL;
1237
1238 dosattr = origattr & ~ATTR_READONLY;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001239 if (dosattr == 0)
1240 dosattr |= ATTR_NORMAL;
1241 dosattr |= ATTR_HIDDEN;
1242
Steve French32709582008-10-20 00:44:19 +00001243 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1244 if (dosattr != origattr) {
1245 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1246 if (info_buf == NULL) {
1247 rc = -ENOMEM;
1248 goto out_close;
1249 }
1250 info_buf->Attributes = cpu_to_le32(dosattr);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001251 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001252 current->tgid);
1253 /* although we would like to mark the file hidden
1254 if that fails we will still try to rename it */
Sachin Prabhu72d282d2013-03-05 19:25:55 +00001255 if (!rc)
Steve French32709582008-10-20 00:44:19 +00001256 cifsInode->cifsAttrs = dosattr;
1257 else
1258 dosattr = origattr; /* since not able to change them */
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001259 }
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001260
Jeff Laytondd1db2d2008-10-16 19:27:12 -04001261 /* rename the file */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001262 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1263 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05001264 cifs_remap(cifs_sb));
Steve French32709582008-10-20 00:44:19 +00001265 if (rc != 0) {
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001266 rc = -EBUSY;
Steve French32709582008-10-20 00:44:19 +00001267 goto undo_setattr;
1268 }
Jeff Layton6d22f092008-09-23 11:48:35 -04001269
Steve French32709582008-10-20 00:44:19 +00001270 /* try to set DELETE_ON_CLOSE */
Jeff Laytonaff8d5c2014-04-30 09:31:45 -04001271 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001272 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001273 current->tgid);
1274 /*
1275 * some samba versions return -ENOENT when we try to set the
1276 * file disposition here. Likely a samba bug, but work around
1277 * it for now. This means that some cifsXXX files may hang
1278 * around after they shouldn't.
1279 *
1280 * BB: remove this hack after more servers have the fix
1281 */
1282 if (rc == -ENOENT)
1283 rc = 0;
1284 else if (rc != 0) {
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001285 rc = -EBUSY;
Steve French32709582008-10-20 00:44:19 +00001286 goto undo_rename;
1287 }
Jeff Laytonaff8d5c2014-04-30 09:31:45 -04001288 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
Steve French32709582008-10-20 00:44:19 +00001289 }
Jeff Layton7ce86d52008-09-24 11:32:59 -04001290
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001291out_close:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001292 CIFSSMBClose(xid, tcon, fid.netfid);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001293out:
Steve French32709582008-10-20 00:44:19 +00001294 kfree(info_buf);
Jeff Layton7ffec372010-09-29 19:51:11 -04001295 cifs_put_tlink(tlink);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001296 return rc;
Steve French32709582008-10-20 00:44:19 +00001297
1298 /*
1299 * reset everything back to the original state. Don't bother
1300 * dealing with errors here since we can't do anything about
1301 * them anyway.
1302 */
1303undo_rename:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001304 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
Steve French2baa2682014-09-27 02:19:01 -05001305 cifs_sb->local_nls, cifs_remap(cifs_sb));
Steve French32709582008-10-20 00:44:19 +00001306undo_setattr:
1307 if (dosattr != origattr) {
1308 info_buf->Attributes = cpu_to_le32(origattr);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001309 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001310 current->tgid))
1311 cifsInode->cifsAttrs = origattr;
1312 }
1313
1314 goto out_close;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001315}
1316
Steve Frenchb7ca6922012-08-03 08:43:01 -05001317/* copied from fs/nfs/dir.c with small changes */
1318static void
1319cifs_drop_nlink(struct inode *inode)
1320{
1321 spin_lock(&inode->i_lock);
1322 if (inode->i_nlink > 0)
1323 drop_nlink(inode);
1324 spin_unlock(&inode->i_lock);
1325}
Steve Frenchff694522009-04-20 19:45:13 +00001326
1327/*
David Howells2b0143b2015-03-17 22:25:59 +00001328 * If d_inode(dentry) is null (usually meaning the cached dentry
Steve Frenchff694522009-04-20 19:45:13 +00001329 * is a negative dentry) then we would attempt a standard SMB delete, but
Colin Ian King413d6102018-10-26 19:07:21 +01001330 * if that fails we can not attempt the fall back mechanisms on EACCES
1331 * but will return the EACCES to the caller. Note that the VFS does not call
Steve Frenchff694522009-04-20 19:45:13 +00001332 * unlink on negative dentries currently.
1333 */
Jeff Layton5f0319a2008-09-16 14:05:16 -04001334int cifs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001337 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 char *full_path = NULL;
David Howells2b0143b2015-03-17 22:25:59 +00001339 struct inode *inode = d_inode(dentry);
Steve Frenchff694522009-04-20 19:45:13 +00001340 struct cifsInodeInfo *cifs_inode;
Jeff Layton5f0319a2008-09-16 14:05:16 -04001341 struct super_block *sb = dir->i_sb;
1342 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001343 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001344 struct cifs_tcon *tcon;
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001345 struct TCP_Server_Info *server;
Steve French60502472008-10-07 18:42:52 +00001346 struct iattr *attrs = NULL;
1347 __u32 dosattr = 0, origattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Joe Perchesf96637b2013-05-04 22:12:25 -05001349 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Jeff Layton7ffec372010-09-29 19:51:11 -04001351 tlink = cifs_sb_tlink(cifs_sb);
1352 if (IS_ERR(tlink))
1353 return PTR_ERR(tlink);
1354 tcon = tlink_tcon(tlink);
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001355 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -04001356
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001357 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Jeff Layton5f0319a2008-09-16 14:05:16 -04001359 /* Unlink can be called from rename so we can not take the
1360 * sb->s_vfs_rename_mutex here */
1361 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301363 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001364 goto unlink_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Steve French2d785a52007-07-15 01:48:57 +00001366
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001367 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1368 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001369 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
Steve French2d785a52007-07-15 01:48:57 +00001370 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05001371 cifs_remap(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05001372 cifs_dbg(FYI, "posix del rc %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +00001373 if ((rc == 0) || (rc == -ENOENT))
1374 goto psx_del_no_retry;
1375 }
1376
Steve French60502472008-10-07 18:42:52 +00001377retry_std_delete:
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001378 if (!server->ops->unlink) {
1379 rc = -ENOSYS;
1380 goto psx_del_no_retry;
1381 }
1382
1383 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
Steve French60502472008-10-07 18:42:52 +00001384
Steve French2d785a52007-07-15 01:48:57 +00001385psx_del_no_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!rc) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001387 if (inode)
Steve Frenchb7ca6922012-08-03 08:43:01 -05001388 cifs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 } else if (rc == -ENOENT) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001390 d_drop(dentry);
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001391 } else if (rc == -EBUSY) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001392 if (server->ops->rename_pending_delete) {
1393 rc = server->ops->rename_pending_delete(full_path,
1394 dentry, xid);
1395 if (rc == 0)
1396 cifs_drop_nlink(inode);
1397 }
Steve Frenchff694522009-04-20 19:45:13 +00001398 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
Steve French388e57b2008-09-16 23:50:58 +00001399 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1400 if (attrs == NULL) {
1401 rc = -ENOMEM;
1402 goto out_reval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Steve French388e57b2008-09-16 23:50:58 +00001404
1405 /* try to reset dos attributes */
Steve Frenchff694522009-04-20 19:45:13 +00001406 cifs_inode = CIFS_I(inode);
1407 origattr = cifs_inode->cifsAttrs;
Steve French60502472008-10-07 18:42:52 +00001408 if (origattr == 0)
1409 origattr |= ATTR_NORMAL;
1410 dosattr = origattr & ~ATTR_READONLY;
Steve French388e57b2008-09-16 23:50:58 +00001411 if (dosattr == 0)
1412 dosattr |= ATTR_NORMAL;
1413 dosattr |= ATTR_HIDDEN;
1414
1415 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
Steve French388e57b2008-09-16 23:50:58 +00001416 if (rc != 0)
1417 goto out_reval;
Steve French60502472008-10-07 18:42:52 +00001418
1419 goto retry_std_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Steve French60502472008-10-07 18:42:52 +00001421
1422 /* undo the setattr if we errored out and it's needed */
1423 if (rc != 0 && dosattr != 0)
1424 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1425
Steve French388e57b2008-09-16 23:50:58 +00001426out_reval:
Jeff Layton5f0319a2008-09-16 14:05:16 -04001427 if (inode) {
Steve Frenchff694522009-04-20 19:45:13 +00001428 cifs_inode = CIFS_I(inode);
1429 cifs_inode->time = 0; /* will force revalidate to get info
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001430 when needed */
Deepa Dinamanie37fea52017-05-08 15:59:16 -07001431 inode->i_ctime = current_time(inode);
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001432 }
Deepa Dinamanie37fea52017-05-08 15:59:16 -07001433 dir->i_ctime = dir->i_mtime = current_time(dir);
Steve Frenchff694522009-04-20 19:45:13 +00001434 cifs_inode = CIFS_I(dir);
Steve French60502472008-10-07 18:42:52 +00001435 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
Jeff Layton7ffec372010-09-29 19:51:11 -04001436unlink_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 kfree(full_path);
Steve French60502472008-10-07 18:42:52 +00001438 kfree(attrs);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001439 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001440 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return rc;
1442}
1443
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001444static int
Jeff Layton101b92d2012-09-19 06:22:45 -07001445cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001446 const char *full_path, struct cifs_sb_info *cifs_sb,
1447 struct cifs_tcon *tcon, const unsigned int xid)
1448{
1449 int rc = 0;
Jeff Layton101b92d2012-09-19 06:22:45 -07001450 struct inode *inode = NULL;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001451
1452 if (tcon->unix_ext)
Jeff Layton101b92d2012-09-19 06:22:45 -07001453 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001454 xid);
1455 else
Jeff Layton101b92d2012-09-19 06:22:45 -07001456 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1457 xid, NULL);
1458
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001459 if (rc)
1460 return rc;
1461
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001462 /*
1463 * setting nlink not necessary except in cases where we failed to get it
Jeff Layton101b92d2012-09-19 06:22:45 -07001464 * from the server or was set bogus. Also, since this is a brand new
1465 * inode, no need to grab the i_lock before setting the i_nlink.
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001466 */
Jeff Layton101b92d2012-09-19 06:22:45 -07001467 if (inode->i_nlink < 2)
1468 set_nlink(inode, 2);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001469 mode &= ~current_umask();
1470 /* must turn on setgid bit if parent dir has it */
Jeff Layton101b92d2012-09-19 06:22:45 -07001471 if (parent->i_mode & S_ISGID)
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001472 mode |= S_ISGID;
1473
1474 if (tcon->unix_ext) {
1475 struct cifs_unix_set_info_args args = {
1476 .mode = mode,
1477 .ctime = NO_CHANGE_64,
1478 .atime = NO_CHANGE_64,
1479 .mtime = NO_CHANGE_64,
1480 .device = 0,
1481 };
1482 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Eric W. Biederman49418b22013-02-06 00:57:56 -08001483 args.uid = current_fsuid();
Jeff Layton101b92d2012-09-19 06:22:45 -07001484 if (parent->i_mode & S_ISGID)
Eric W. Biederman49418b22013-02-06 00:57:56 -08001485 args.gid = parent->i_gid;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001486 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08001487 args.gid = current_fsgid();
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001488 } else {
Eric W. Biederman49418b22013-02-06 00:57:56 -08001489 args.uid = INVALID_UID; /* no change */
1490 args.gid = INVALID_GID; /* no change */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001491 }
1492 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1493 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05001494 cifs_remap(cifs_sb));
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001495 } else {
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001496 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001497 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001498 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
Jeff Layton101b92d2012-09-19 06:22:45 -07001499 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001500 tcon, xid);
Jeff Layton101b92d2012-09-19 06:22:45 -07001501 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1502 inode->i_mode = (mode | S_IFDIR);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001503
Jeff Layton101b92d2012-09-19 06:22:45 -07001504 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1505 inode->i_uid = current_fsuid();
1506 if (inode->i_mode & S_ISGID)
1507 inode->i_gid = parent->i_gid;
1508 else
1509 inode->i_gid = current_fsgid();
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001510 }
1511 }
Jeff Layton101b92d2012-09-19 06:22:45 -07001512 d_instantiate(dentry, inode);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001513 return rc;
1514}
1515
1516static int
1517cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1518 const char *full_path, struct cifs_sb_info *cifs_sb,
1519 struct cifs_tcon *tcon, const unsigned int xid)
1520{
1521 int rc = 0;
1522 u32 oplock = 0;
1523 FILE_UNIX_BASIC_INFO *info = NULL;
1524 struct inode *newinode = NULL;
1525 struct cifs_fattr fattr;
1526
1527 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1528 if (info == NULL) {
1529 rc = -ENOMEM;
1530 goto posix_mkdir_out;
1531 }
1532
1533 mode &= ~current_umask();
1534 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1535 NULL /* netfid */, info, &oplock, full_path,
Steve French2baa2682014-09-27 02:19:01 -05001536 cifs_sb->local_nls, cifs_remap(cifs_sb));
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001537 if (rc == -EOPNOTSUPP)
1538 goto posix_mkdir_out;
1539 else if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001540 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001541 d_drop(dentry);
1542 goto posix_mkdir_out;
1543 }
1544
1545 if (info->Type == cpu_to_le32(-1))
1546 /* no return info, go query for it */
1547 goto posix_mkdir_get_info;
1548 /*
1549 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1550 * need to set uid/gid.
1551 */
1552
1553 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1554 cifs_fill_uniqueid(inode->i_sb, &fattr);
1555 newinode = cifs_iget(inode->i_sb, &fattr);
1556 if (!newinode)
1557 goto posix_mkdir_get_info;
1558
1559 d_instantiate(dentry, newinode);
1560
1561#ifdef CONFIG_CIFS_DEBUG2
Al Viro35c265e2014-08-19 20:25:34 -04001562 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1563 dentry, dentry, newinode);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001564
1565 if (newinode->i_nlink != 2)
Joe Perchesf96637b2013-05-04 22:12:25 -05001566 cifs_dbg(FYI, "unexpected number of links %d\n",
1567 newinode->i_nlink);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001568#endif
1569
1570posix_mkdir_out:
1571 kfree(info);
1572 return rc;
1573posix_mkdir_get_info:
1574 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1575 xid);
1576 goto posix_mkdir_out;
1577}
1578
Al Viro18bb1db2011-07-26 01:41:39 -04001579int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001581 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001582 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001584 struct tcon_link *tlink;
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001585 struct cifs_tcon *tcon;
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001586 struct TCP_Server_Info *server;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001587 char *full_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Joe Perchesf96637b2013-05-04 22:12:25 -05001589 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1590 mode, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001593 tlink = cifs_sb_tlink(cifs_sb);
1594 if (IS_ERR(tlink))
1595 return PTR_ERR(tlink);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001596 tcon = tlink_tcon(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -04001597
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001598 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Steve French7f573562005-08-30 11:32:14 -07001600 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301602 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001603 goto mkdir_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Steve French50c2f752007-07-13 00:33:32 +00001605
Steve Frenchbea851b2018-06-14 21:56:32 -05001606 server = tcon->ses->server;
1607
Steve Frenchbea851b2018-06-14 21:56:32 -05001608 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1609 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1610 cifs_sb);
1611 d_drop(direntry); /* for time being always refresh inode info */
1612 goto mkdir_out;
1613 }
Steve Frenchbea851b2018-06-14 21:56:32 -05001614
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001615 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1616 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001617 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1618 tcon, xid);
1619 if (rc != -EOPNOTSUPP)
Steve French2dd29d32007-04-23 22:07:35 +00001620 goto mkdir_out;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001621 }
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001622
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001623 if (!server->ops->mkdir) {
1624 rc = -ENOSYS;
1625 goto mkdir_out;
1626 }
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve Frenchc3ca78e2019-09-25 00:32:13 -05001629 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001631 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 d_drop(direntry);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001633 goto mkdir_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001635
Steve Frenchc3ca78e2019-09-25 00:32:13 -05001636 /* TODO: skip this for smb2/smb3 */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001637 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1638 xid);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001639mkdir_out:
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001640 /*
1641 * Force revalidate to get parent dir info when needed since cached
1642 * attributes are invalid now.
1643 */
1644 CIFS_I(inode)->time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001646 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001647 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 return rc;
1649}
1650
1651int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1652{
1653 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001654 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001656 struct tcon_link *tlink;
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001657 struct cifs_tcon *tcon;
1658 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 char *full_path = NULL;
1660 struct cifsInodeInfo *cifsInode;
1661
Joe Perchesf96637b2013-05-04 22:12:25 -05001662 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001664 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Steve French7f573562005-08-30 11:32:14 -07001666 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301668 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001669 goto rmdir_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
Jeff Layton7ffec372010-09-29 19:51:11 -04001672 cifs_sb = CIFS_SB(inode->i_sb);
1673 tlink = cifs_sb_tlink(cifs_sb);
1674 if (IS_ERR(tlink)) {
1675 rc = PTR_ERR(tlink);
1676 goto rmdir_exit;
1677 }
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001678 tcon = tlink_tcon(tlink);
1679 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -04001680
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001681 if (!server->ops->rmdir) {
1682 rc = -ENOSYS;
1683 cifs_put_tlink(tlink);
1684 goto rmdir_exit;
1685 }
1686
1687 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001688 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (!rc) {
David Howells2b0143b2015-03-17 22:25:59 +00001691 spin_lock(&d_inode(direntry)->i_lock);
1692 i_size_write(d_inode(direntry), 0);
1693 clear_nlink(d_inode(direntry));
1694 spin_unlock(&d_inode(direntry)->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696
David Howells2b0143b2015-03-17 22:25:59 +00001697 cifsInode = CIFS_I(d_inode(direntry));
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001698 /* force revalidate to go get info when needed */
1699 cifsInode->time = 0;
Steve French42c245442009-01-13 22:03:55 +00001700
1701 cifsInode = CIFS_I(inode);
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001702 /*
1703 * Force revalidate to get parent dir info when needed since cached
1704 * attributes are invalid now.
1705 */
1706 cifsInode->time = 0;
Steve French42c245442009-01-13 22:03:55 +00001707
David Howells2b0143b2015-03-17 22:25:59 +00001708 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
Deepa Dinamanie37fea52017-05-08 15:59:16 -07001709 current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jeff Layton7ffec372010-09-29 19:51:11 -04001711rmdir_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001713 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return rc;
1715}
1716
Steve Frenchee2fd962008-09-23 18:23:33 +00001717static int
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001718cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1719 const char *from_path, struct dentry *to_dentry,
1720 const char *to_path)
Steve Frenchee2fd962008-09-23 18:23:33 +00001721{
1722 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001723 struct tcon_link *tlink;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001724 struct cifs_tcon *tcon;
1725 struct TCP_Server_Info *server;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001726 struct cifs_fid fid;
1727 struct cifs_open_parms oparms;
Steve Frenchee2fd962008-09-23 18:23:33 +00001728 int oplock, rc;
1729
Jeff Layton7ffec372010-09-29 19:51:11 -04001730 tlink = cifs_sb_tlink(cifs_sb);
1731 if (IS_ERR(tlink))
1732 return PTR_ERR(tlink);
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001733 tcon = tlink_tcon(tlink);
1734 server = tcon->ses->server;
1735
1736 if (!server->ops->rename)
1737 return -ENOSYS;
Jeff Layton7ffec372010-09-29 19:51:11 -04001738
Steve Frenchee2fd962008-09-23 18:23:33 +00001739 /* try path-based rename first */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001740 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
Steve Frenchee2fd962008-09-23 18:23:33 +00001741
1742 /*
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001743 * Don't bother with rename by filehandle unless file is busy and
1744 * source. Note that cross directory moves do not work with
Steve Frenchee2fd962008-09-23 18:23:33 +00001745 * rename by filehandle to various Windows servers.
1746 */
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001747 if (rc == 0 || rc != -EBUSY)
Jeff Layton7ffec372010-09-29 19:51:11 -04001748 goto do_rename_exit;
Steve Frenchee2fd962008-09-23 18:23:33 +00001749
Frank Sorenson652727b2019-04-16 08:37:27 -05001750 /* Don't fall back to using SMB on SMB 2+ mount */
1751 if (server->vals->protocol_id != 0)
1752 goto do_rename_exit;
1753
Jeff Laytoned0e3ac2010-06-01 16:21:01 -04001754 /* open-file renames don't work across directories */
1755 if (to_dentry->d_parent != from_dentry->d_parent)
Jeff Layton7ffec372010-09-29 19:51:11 -04001756 goto do_rename_exit;
Jeff Laytoned0e3ac2010-06-01 16:21:01 -04001757
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001758 oparms.tcon = tcon;
1759 oparms.cifs_sb = cifs_sb;
Steve Frenchee2fd962008-09-23 18:23:33 +00001760 /* open the file to be renamed -- we need DELETE perms */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001761 oparms.desired_access = DELETE;
1762 oparms.create_options = CREATE_NOT_DIR;
1763 oparms.disposition = FILE_OPEN;
1764 oparms.path = from_path;
1765 oparms.fid = &fid;
1766 oparms.reconnect = false;
1767
1768 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Steve Frenchee2fd962008-09-23 18:23:33 +00001769 if (rc == 0) {
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001770 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
Steve Frenchee2fd962008-09-23 18:23:33 +00001771 (const char *) to_dentry->d_name.name,
Steve French2baa2682014-09-27 02:19:01 -05001772 cifs_sb->local_nls, cifs_remap(cifs_sb));
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001773 CIFSSMBClose(xid, tcon, fid.netfid);
Steve Frenchee2fd962008-09-23 18:23:33 +00001774 }
Jeff Layton7ffec372010-09-29 19:51:11 -04001775do_rename_exit:
1776 cifs_put_tlink(tlink);
Steve Frenchee2fd962008-09-23 18:23:33 +00001777 return rc;
1778}
1779
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001780int
Miklos Szeredi7c33d592014-07-23 15:15:36 +02001781cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1782 struct inode *target_dir, struct dentry *target_dentry,
1783 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001785 char *from_name = NULL;
1786 char *to_name = NULL;
Jeff Layton639e7a92010-09-03 11:50:09 -04001787 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001788 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001789 struct cifs_tcon *tcon;
Steve Frenchee2fd962008-09-23 18:23:33 +00001790 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1791 FILE_UNIX_BASIC_INFO *info_buf_target;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001792 unsigned int xid;
1793 int rc, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Miklos Szeredi7c33d592014-07-23 15:15:36 +02001795 if (flags & ~RENAME_NOREPLACE)
1796 return -EINVAL;
1797
Jeff Layton639e7a92010-09-03 11:50:09 -04001798 cifs_sb = CIFS_SB(source_dir->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001799 tlink = cifs_sb_tlink(cifs_sb);
1800 if (IS_ERR(tlink))
1801 return PTR_ERR(tlink);
1802 tcon = tlink_tcon(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001804 xid = get_xid();
Steve Frenchee2fd962008-09-23 18:23:33 +00001805
1806 /*
Steve Frenchee2fd962008-09-23 18:23:33 +00001807 * we already have the rename sem so we do not need to
1808 * grab it again here to protect the path integrity
1809 */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001810 from_name = build_path_from_dentry(source_dentry);
1811 if (from_name == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 rc = -ENOMEM;
1813 goto cifs_rename_exit;
1814 }
1815
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001816 to_name = build_path_from_dentry(target_dentry);
1817 if (to_name == NULL) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001818 rc = -ENOMEM;
1819 goto cifs_rename_exit;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001822 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1823 to_name);
Steve Frenchee2fd962008-09-23 18:23:33 +00001824
Miklos Szeredi7c33d592014-07-23 15:15:36 +02001825 /*
1826 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1827 */
1828 if (flags & RENAME_NOREPLACE)
1829 goto cifs_rename_exit;
1830
Jeff Layton14121bd2008-10-20 14:45:22 -04001831 if (rc == -EEXIST && tcon->unix_ext) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001832 /*
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001833 * Are src and dst hardlinks of same inode? We can only tell
1834 * with unix extensions enabled.
Steve Frenchee2fd962008-09-23 18:23:33 +00001835 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001836 info_buf_source =
Kees Cook6da2ec52018-06-12 13:55:00 -07001837 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
Jeff Layton14121bd2008-10-20 14:45:22 -04001838 GFP_KERNEL);
1839 if (info_buf_source == NULL) {
1840 rc = -ENOMEM;
1841 goto cifs_rename_exit;
1842 }
1843
1844 info_buf_target = info_buf_source + 1;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001845 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1846 info_buf_source,
1847 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05001848 cifs_remap(cifs_sb));
Jeff Layton8d281ef2008-10-22 13:57:01 -04001849 if (tmprc != 0)
Jeff Layton14121bd2008-10-20 14:45:22 -04001850 goto unlink_target;
1851
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001852 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1853 info_buf_target,
1854 cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05001855 cifs_remap(cifs_sb));
Jeff Layton14121bd2008-10-20 14:45:22 -04001856
Jeff Layton8d281ef2008-10-22 13:57:01 -04001857 if (tmprc == 0 && (info_buf_source->UniqueId ==
Jeff Laytonae6884a2008-11-03 14:05:08 -05001858 info_buf_target->UniqueId)) {
Jeff Layton14121bd2008-10-20 14:45:22 -04001859 /* same file, POSIX says that this is a noop */
Jeff Laytonae6884a2008-11-03 14:05:08 -05001860 rc = 0;
Jeff Layton14121bd2008-10-20 14:45:22 -04001861 goto cifs_rename_exit;
Jeff Laytonae6884a2008-11-03 14:05:08 -05001862 }
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001863 }
1864 /*
1865 * else ... BB we could add the same check for Windows by
1866 * checking the UniqueId via FILE_INTERNAL_INFO
1867 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001868
Jeff Layton14121bd2008-10-20 14:45:22 -04001869unlink_target:
Jeff Laytonfc6f3942009-04-17 11:45:30 -04001870 /* Try unlinking the target dentry if it's not negative */
David Howells2b0143b2015-03-17 22:25:59 +00001871 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
Pavel Shilovskya07d3222014-08-22 13:32:09 +04001872 if (d_is_dir(target_dentry))
1873 tmprc = cifs_rmdir(target_dir, target_dentry);
1874 else
1875 tmprc = cifs_unlink(target_dir, target_dentry);
Jeff Layton14121bd2008-10-20 14:45:22 -04001876 if (tmprc)
1877 goto cifs_rename_exit;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001878 rc = cifs_do_rename(xid, source_dentry, from_name,
1879 target_dentry, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
Pavel Shilovskyb46799a2014-08-18 20:49:58 +04001882 /* force revalidate to go get info when needed */
1883 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1884
1885 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
Deepa Dinamanie37fea52017-05-08 15:59:16 -07001886 target_dir->i_mtime = current_time(source_dir);
Pavel Shilovskyb46799a2014-08-18 20:49:58 +04001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888cifs_rename_exit:
Steve Frenchee2fd962008-09-23 18:23:33 +00001889 kfree(info_buf_source);
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001890 kfree(from_name);
1891 kfree(to_name);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001892 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001893 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return rc;
1895}
1896
Jeff Laytondf2cf172010-02-12 07:44:16 -05001897static bool
1898cifs_inode_needs_reval(struct inode *inode)
1899{
1900 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301901 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Laytondf2cf172010-02-12 07:44:16 -05001902
Ronnie Sahlberg57c55cd2018-05-24 06:54:27 +10001903 if (cifs_i->time == 0)
1904 return true;
1905
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001906 if (CIFS_CACHE_READ(cifs_i))
Jeff Laytondf2cf172010-02-12 07:44:16 -05001907 return false;
1908
1909 if (!lookupCacheEnabled)
1910 return true;
1911
Jeff Laytona87c9ad2014-03-26 07:24:23 -07001912 if (!cifs_sb->actimeo)
1913 return true;
1914
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301915 if (!time_in_range(jiffies, cifs_i->time,
1916 cifs_i->time + cifs_sb->actimeo))
Jeff Laytondf2cf172010-02-12 07:44:16 -05001917 return true;
1918
Jeff Laytondb192722010-05-17 14:51:49 -04001919 /* hardlinked files w/ noserverino get "special" treatment */
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301920 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
Jeff Laytondb192722010-05-17 14:51:49 -04001921 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1922 return true;
1923
Jeff Laytondf2cf172010-02-12 07:44:16 -05001924 return false;
1925}
1926
Suresh Jayaraman523fb8c2010-11-29 22:39:47 +05301927/*
1928 * Zap the cache. Called when invalid_mapping flag is set.
1929 */
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001930int
Jeff Laytondf2cf172010-02-12 07:44:16 -05001931cifs_invalidate_mapping(struct inode *inode)
1932{
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001933 int rc = 0;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001934
Jeff Laytondf2cf172010-02-12 07:44:16 -05001935 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
Pavel Shilovsky257fb1f2011-03-16 01:55:32 +03001936 rc = invalidate_inode_pages2(inode->i_mapping);
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001937 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001938 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1939 __func__, inode);
Jeff Laytondf2cf172010-02-12 07:44:16 -05001940 }
Pavel Shilovsky257fb1f2011-03-16 01:55:32 +03001941
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +05301942 cifs_fscache_reset_inode_cookie(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001943 return rc;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001944}
1945
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001946/**
1947 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1948 * @word: long word containing the bit lock
1949 */
1950static int
Peter Zijlstradfd01f02015-12-13 22:11:16 +01001951cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001952{
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001953 freezable_schedule_unsafe();
Peter Zijlstradfd01f02015-12-13 22:11:16 +01001954 if (signal_pending_state(mode, current))
1955 return -ERESTARTSYS;
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001956 return 0;
1957}
1958
Jeff Laytone284e532014-04-30 09:31:46 -04001959int
1960cifs_revalidate_mapping(struct inode *inode)
1961{
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001962 int rc;
1963 unsigned long *flags = &CIFS_I(inode)->flags;
1964
NeilBrown74316202014-07-07 15:16:04 +10001965 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1966 TASK_KILLABLE);
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001967 if (rc)
1968 return rc;
1969
1970 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1971 rc = cifs_invalidate_mapping(inode);
1972 if (rc)
1973 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1974 }
1975
1976 clear_bit_unlock(CIFS_INO_LOCK, flags);
Linus Torvaldsb1cce802014-06-09 19:08:43 -07001977 smp_mb__after_atomic();
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001978 wake_up_bit(flags, CIFS_INO_LOCK);
1979
1980 return rc;
1981}
1982
1983int
1984cifs_zap_mapping(struct inode *inode)
1985{
1986 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1987 return cifs_revalidate_mapping(inode);
Jeff Laytone284e532014-04-30 09:31:46 -04001988}
1989
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001990int cifs_revalidate_file_attr(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -05001991{
1992 int rc = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05001993 struct inode *inode = file_inode(filp);
Jeff Laytonba00ba642010-09-20 16:01:31 -07001994 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
Jeff Laytonabab0952010-02-12 07:44:18 -05001995
1996 if (!cifs_inode_needs_reval(inode))
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001997 return rc;
Jeff Laytonabab0952010-02-12 07:44:18 -05001998
Jeff Layton13cfb732010-09-29 19:51:11 -04001999 if (tlink_tcon(cfile->tlink)->unix_ext)
Jeff Laytonabab0952010-02-12 07:44:18 -05002000 rc = cifs_get_file_info_unix(filp);
2001 else
2002 rc = cifs_get_file_info(filp);
2003
Jeff Laytonabab0952010-02-12 07:44:18 -05002004 return rc;
2005}
2006
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002007int cifs_revalidate_dentry_attr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002009 unsigned int xid;
Jeff Laytondf2cf172010-02-12 07:44:16 -05002010 int rc = 0;
David Howells2b0143b2015-03-17 22:25:59 +00002011 struct inode *inode = d_inode(dentry);
Jeff Laytondf2cf172010-02-12 07:44:16 -05002012 struct super_block *sb = dentry->d_sb;
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002013 char *full_path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Jeff Laytondf2cf172010-02-12 07:44:16 -05002015 if (inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return -ENOENT;
2017
Jeff Laytondf2cf172010-02-12 07:44:16 -05002018 if (!cifs_inode_needs_reval(inode))
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002019 return rc;
2020
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002021 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 /* can not safely grab the rename sem here if rename calls revalidate
2024 since that would deadlock */
Jeff Laytondf2cf172010-02-12 07:44:16 -05002025 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05302027 rc = -ENOMEM;
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Jeff Laytondf2cf172010-02-12 07:44:16 -05002030
Joe Perchesf96637b2013-05-04 22:12:25 -05002031 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2032 full_path, inode, inode->i_count.counter,
Miklos Szeredia00be0e2016-09-16 12:44:21 +02002033 dentry, cifs_get_time(dentry), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Jeff Layton0d424ad2010-09-20 16:01:35 -07002035 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
Jeff Laytondf2cf172010-02-12 07:44:16 -05002036 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2037 else
2038 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2039 xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002041out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002043 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return rc;
2045}
2046
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002047int cifs_revalidate_file(struct file *filp)
2048{
2049 int rc;
Al Viro496ad9a2013-01-23 17:07:38 -05002050 struct inode *inode = file_inode(filp);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002051
2052 rc = cifs_revalidate_file_attr(filp);
2053 if (rc)
2054 return rc;
2055
Jeff Laytone284e532014-04-30 09:31:46 -04002056 return cifs_revalidate_mapping(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002057}
2058
2059/* revalidate a dentry's inode attributes */
2060int cifs_revalidate_dentry(struct dentry *dentry)
2061{
2062 int rc;
David Howells2b0143b2015-03-17 22:25:59 +00002063 struct inode *inode = d_inode(dentry);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002064
2065 rc = cifs_revalidate_dentry_attr(dentry);
2066 if (rc)
2067 return rc;
2068
Jeff Laytone284e532014-04-30 09:31:46 -04002069 return cifs_revalidate_mapping(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002070}
2071
David Howellsa528d352017-01-31 16:46:22 +00002072int cifs_getattr(const struct path *path, struct kstat *stat,
2073 u32 request_mask, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
David Howellsa528d352017-01-31 16:46:22 +00002075 struct dentry *dentry = path->dentry;
Jeff Layton3aa1c8c2010-10-07 14:46:28 -04002076 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
Steve French96daf2b2011-05-27 04:34:02 +00002077 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
David Howells2b0143b2015-03-17 22:25:59 +00002078 struct inode *inode = d_inode(dentry);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002079 int rc;
Jeff Layton3aa1c8c2010-10-07 14:46:28 -04002080
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002081 /*
2082 * We need to be sure that all dirty pages are written and the server
2083 * has actual ctime, mtime and file length.
2084 */
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002085 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002086 inode->i_mapping->nrpages != 0) {
2087 rc = filemap_fdatawait(inode->i_mapping);
Steve French156ecb22011-05-20 17:00:01 +00002088 if (rc) {
2089 mapping_set_error(inode->i_mapping, rc);
2090 return rc;
2091 }
Steve French5fe14c82006-11-07 19:26:33 +00002092 }
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002093
2094 rc = cifs_revalidate_dentry_attr(dentry);
2095 if (rc)
2096 return rc;
2097
2098 generic_fillattr(inode, stat);
Steve Frenche8506d22019-02-28 21:32:15 -06002099 stat->blksize = cifs_sb->bsize;
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002100 stat->ino = CIFS_I(inode)->uniqueid;
2101
Steve French6e70e262017-09-21 21:32:29 -05002102 /* old CIFS Unix Extensions doesn't return create time */
2103 if (CIFS_I(inode)->createtime) {
2104 stat->result_mask |= STATX_BTIME;
Arnd Bergmann95390202018-06-19 17:27:58 +02002105 stat->btime =
2106 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
Steve French6e70e262017-09-21 21:32:29 -05002107 }
2108
2109 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2110 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2111 stat->attributes |= STATX_ATTR_COMPRESSED;
2112 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2113 stat->attributes |= STATX_ATTR_ENCRYPTED;
2114
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002115 /*
Jeff Laytond3d1fce2012-11-25 08:00:40 -05002116 * If on a multiuser mount without unix extensions or cifsacl being
2117 * enabled, and the admin hasn't overridden them, set the ownership
2118 * to the fsuid/fsgid of the current process.
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002119 */
2120 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
Jeff Laytond3d1fce2012-11-25 08:00:40 -05002121 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002122 !tcon->unix_ext) {
2123 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2124 stat->uid = current_fsuid();
2125 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2126 stat->gid = current_fsgid();
2127 }
2128 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10002131int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2132 u64 len)
2133{
2134 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2135 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->vfs_inode.i_sb);
2136 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2137 struct TCP_Server_Info *server = tcon->ses->server;
2138 struct cifsFileInfo *cfile;
2139 int rc;
2140
2141 /*
2142 * We need to be sure that all dirty pages are written as they
2143 * might fill holes on the server.
2144 */
2145 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2146 inode->i_mapping->nrpages != 0) {
2147 rc = filemap_fdatawait(inode->i_mapping);
2148 if (rc) {
2149 mapping_set_error(inode->i_mapping, rc);
2150 return rc;
2151 }
2152 }
2153
2154 cfile = find_readable_file(cifs_i, false);
2155 if (cfile == NULL)
2156 return -EINVAL;
2157
2158 if (server->ops->fiemap) {
2159 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2160 cifsFileInfo_put(cfile);
2161 return rc;
2162 }
2163
2164 cifsFileInfo_put(cfile);
2165 return -ENOTSUPP;
2166}
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2169{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002170 pgoff_t index = from >> PAGE_SHIFT;
2171 unsigned offset = from & (PAGE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 int rc = 0;
2174
2175 page = grab_cache_page(mapping, index);
2176 if (!page)
2177 return -ENOMEM;
2178
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 zero_user_segment(page, offset, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002181 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return rc;
2183}
2184
Christoph Hellwig1b947462010-07-18 17:51:21 -04002185static void cifs_setsize(struct inode *inode, loff_t offset)
Steve French3677db12007-02-26 16:46:11 +00002186{
Ronnie Sahlberg57c55cd2018-05-24 06:54:27 +10002187 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2188
Steve Frenchba6a46a2007-02-26 20:06:29 +00002189 spin_lock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00002190 i_size_write(inode, offset);
Steve Frenchba6a46a2007-02-26 20:06:29 +00002191 spin_unlock(&inode->i_lock);
Christoph Hellwig1b947462010-07-18 17:51:21 -04002192
Ronnie Sahlberg57c55cd2018-05-24 06:54:27 +10002193 /* Cached inode must be refreshed on truncate */
2194 cifs_i->time = 0;
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07002195 truncate_pagecache(inode, offset);
Steve French3677db12007-02-26 16:46:11 +00002196}
2197
Jeff Layton8efdbde2008-07-23 21:28:12 +00002198static int
2199cifs_set_file_size(struct inode *inode, struct iattr *attrs,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002200 unsigned int xid, char *full_path)
Jeff Layton8efdbde2008-07-23 21:28:12 +00002201{
2202 int rc;
2203 struct cifsFileInfo *open_file;
2204 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2205 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04002206 struct tcon_link *tlink = NULL;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002207 struct cifs_tcon *tcon = NULL;
2208 struct TCP_Server_Info *server;
Jeff Layton8efdbde2008-07-23 21:28:12 +00002209
2210 /*
2211 * To avoid spurious oplock breaks from server, in the case of
2212 * inodes that we already have open, avoid doing path based
2213 * setting of file size if we can do it by handle.
2214 * This keeps our caching token (oplock) and avoids timeouts
2215 * when the local oplock break takes longer to flush
2216 * writebehind data than the SMB timeout for the SetPathInfo
2217 * request would allow
2218 */
Jeff Layton6508d902010-09-29 19:51:11 -04002219 open_file = find_writable_file(cifsInode, true);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002220 if (open_file) {
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002221 tcon = tlink_tcon(open_file->tlink);
2222 server = tcon->ses->server;
2223 if (server->ops->set_file_size)
2224 rc = server->ops->set_file_size(xid, tcon, open_file,
2225 attrs->ia_size, false);
2226 else
2227 rc = -ENOSYS;
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04002228 cifsFileInfo_put(open_file);
Joe Perchesf96637b2013-05-04 22:12:25 -05002229 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002230 } else
2231 rc = -EINVAL;
2232
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002233 if (!rc)
2234 goto set_size_out;
Jeff Laytonba00ba642010-09-20 16:01:31 -07002235
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002236 if (tcon == NULL) {
2237 tlink = cifs_sb_tlink(cifs_sb);
2238 if (IS_ERR(tlink))
2239 return PTR_ERR(tlink);
2240 tcon = tlink_tcon(tlink);
2241 server = tcon->ses->server;
Jeff Layton8efdbde2008-07-23 21:28:12 +00002242 }
2243
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002244 /*
2245 * Set file size by pathname rather than by handle either because no
2246 * valid, writeable file handle for it was found or because there was
2247 * an error setting it by handle.
2248 */
2249 if (server->ops->set_path_size)
2250 rc = server->ops->set_path_size(xid, tcon, full_path,
2251 attrs->ia_size, cifs_sb, false);
2252 else
2253 rc = -ENOSYS;
Joe Perchesf96637b2013-05-04 22:12:25 -05002254 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002255
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002256 if (tlink)
2257 cifs_put_tlink(tlink);
2258
2259set_size_out:
Jeff Layton8efdbde2008-07-23 21:28:12 +00002260 if (rc == 0) {
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002261 cifsInode->server_eof = attrs->ia_size;
Christoph Hellwig1b947462010-07-18 17:51:21 -04002262 cifs_setsize(inode, attrs->ia_size);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002263 cifs_truncate_page(inode->i_mapping, inode->i_size);
2264 }
2265
2266 return rc;
2267}
2268
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002269static int
2270cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2271{
2272 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002273 unsigned int xid;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002274 char *full_path = NULL;
David Howells2b0143b2015-03-17 22:25:59 +00002275 struct inode *inode = d_inode(direntry);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002276 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2277 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04002278 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00002279 struct cifs_tcon *pTcon;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002280 struct cifs_unix_set_info_args *args = NULL;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002281 struct cifsFileInfo *open_file;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002282
Al Viro35c265e2014-08-19 20:25:34 -04002283 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2284 direntry, attrs->ia_valid);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002285
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002286 xid = get_xid();
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002287
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002288 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2289 attrs->ia_valid |= ATTR_FORCE;
2290
Jan Kara31051c82016-05-26 16:55:18 +02002291 rc = setattr_prepare(direntry, attrs);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002292 if (rc < 0)
2293 goto out;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002294
2295 full_path = build_path_from_dentry(direntry);
2296 if (full_path == NULL) {
2297 rc = -ENOMEM;
2298 goto out;
2299 }
2300
Jeff Layton0f4d6342009-03-26 13:35:37 -04002301 /*
2302 * Attempt to flush data before changing attributes. We need to do
2303 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2304 * ownership or mode then we may also need to do this. Here, we take
2305 * the safe way out and just do the flush on all setattr requests. If
2306 * the flush returns error, store it to report later and continue.
2307 *
2308 * BB: This should be smarter. Why bother flushing pages that
2309 * will be truncated anyway? Also, should we error out here if
2310 * the flush returns error?
2311 */
2312 rc = filemap_write_and_wait(inode->i_mapping);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002313 if (is_interrupt_error(rc)) {
2314 rc = -ERESTARTSYS;
2315 goto out;
2316 }
2317
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002318 mapping_set_error(inode->i_mapping, rc);
2319 rc = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002320
2321 if (attrs->ia_valid & ATTR_SIZE) {
2322 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2323 if (rc != 0)
2324 goto out;
2325 }
2326
2327 /* skip mode change if it's just for clearing setuid/setgid */
2328 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2329 attrs->ia_valid &= ~ATTR_MODE;
2330
2331 args = kmalloc(sizeof(*args), GFP_KERNEL);
2332 if (args == NULL) {
2333 rc = -ENOMEM;
2334 goto out;
2335 }
2336
2337 /* set up the struct */
2338 if (attrs->ia_valid & ATTR_MODE)
2339 args->mode = attrs->ia_mode;
2340 else
2341 args->mode = NO_CHANGE_64;
2342
2343 if (attrs->ia_valid & ATTR_UID)
2344 args->uid = attrs->ia_uid;
2345 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08002346 args->uid = INVALID_UID; /* no change */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002347
2348 if (attrs->ia_valid & ATTR_GID)
2349 args->gid = attrs->ia_gid;
2350 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08002351 args->gid = INVALID_GID; /* no change */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002352
2353 if (attrs->ia_valid & ATTR_ATIME)
Arnd Bergmann95390202018-06-19 17:27:58 +02002354 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002355 else
2356 args->atime = NO_CHANGE_64;
2357
2358 if (attrs->ia_valid & ATTR_MTIME)
Arnd Bergmann95390202018-06-19 17:27:58 +02002359 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002360 else
2361 args->mtime = NO_CHANGE_64;
2362
2363 if (attrs->ia_valid & ATTR_CTIME)
Arnd Bergmann95390202018-06-19 17:27:58 +02002364 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002365 else
2366 args->ctime = NO_CHANGE_64;
2367
2368 args->device = 0;
Jeff Layton6508d902010-09-29 19:51:11 -04002369 open_file = find_writable_file(cifsInode, true);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002370 if (open_file) {
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002371 u16 nfid = open_file->fid.netfid;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002372 u32 npid = open_file->pid;
Jeff Layton13cfb732010-09-29 19:51:11 -04002373 pTcon = tlink_tcon(open_file->tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002374 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04002375 cifsFileInfo_put(open_file);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002376 } else {
Jeff Layton7ffec372010-09-29 19:51:11 -04002377 tlink = cifs_sb_tlink(cifs_sb);
2378 if (IS_ERR(tlink)) {
2379 rc = PTR_ERR(tlink);
2380 goto out;
2381 }
2382 pTcon = tlink_tcon(tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002383 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
Jeff Layton01ea95e2009-07-09 20:02:49 -04002384 cifs_sb->local_nls,
Nakajima Akirabc8ebdc42015-02-13 15:35:58 +09002385 cifs_remap(cifs_sb));
Jeff Layton7ffec372010-09-29 19:51:11 -04002386 cifs_put_tlink(tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002387 }
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002388
Christoph Hellwig10257742010-06-04 11:30:02 +02002389 if (rc)
2390 goto out;
Steve Frenchccd4bb12010-02-08 17:39:58 +00002391
Christoph Hellwig10257742010-06-04 11:30:02 +02002392 if ((attrs->ia_valid & ATTR_SIZE) &&
Christoph Hellwig1b947462010-07-18 17:51:21 -04002393 attrs->ia_size != i_size_read(inode))
2394 truncate_setsize(inode, attrs->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +02002395
2396 setattr_copy(inode, attrs);
2397 mark_inode_dirty(inode);
2398
2399 /* force revalidate when any of these times are set since some
2400 of the fs types (eg ext3, fat) do not have fine enough
2401 time granularity to match protocol, and we do not have a
2402 a way (yet) to query the server fs's time granularity (and
2403 whether it rounds times down).
2404 */
2405 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2406 cifsInode->time = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002407out:
2408 kfree(args);
2409 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002410 free_xid(xid);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002411 return rc;
2412}
2413
Jeff Layton0510eeb2008-08-02 07:26:12 -04002414static int
2415cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002417 unsigned int xid;
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002418 kuid_t uid = INVALID_UID;
2419 kgid_t gid = INVALID_GID;
David Howells2b0143b2015-03-17 22:25:59 +00002420 struct inode *inode = d_inode(direntry);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002421 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002422 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
Ronnie Sahlbergaa081852019-07-19 08:12:11 +10002423 struct cifsFileInfo *wfile;
2424 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 char *full_path = NULL;
2426 int rc = -EACCES;
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002427 __u32 dosattr = 0;
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04002428 __u64 mode = NO_CHANGE_64;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002429
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002430 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Colin Ian King63d614a2019-06-24 17:39:43 +01002432 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
Al Viro35c265e2014-08-19 20:25:34 -04002433 direntry, attrs->ia_valid);
Steve French6473a552005-11-29 20:20:10 -08002434
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002435 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2436 attrs->ia_valid |= ATTR_FORCE;
2437
Jan Kara31051c82016-05-26 16:55:18 +02002438 rc = setattr_prepare(direntry, attrs);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002439 if (rc < 0) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002440 free_xid(xid);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002441 return rc;
Steve French6473a552005-11-29 20:20:10 -08002442 }
Steve French50c2f752007-07-13 00:33:32 +00002443
Steve French7f573562005-08-30 11:32:14 -07002444 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05302446 rc = -ENOMEM;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002447 free_xid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05302448 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Jeff Layton0f4d6342009-03-26 13:35:37 -04002451 /*
2452 * Attempt to flush data before changing attributes. We need to do
2453 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2454 * ownership or mode then we may also need to do this. Here, we take
2455 * the safe way out and just do the flush on all setattr requests. If
2456 * the flush returns error, store it to report later and continue.
2457 *
2458 * BB: This should be smarter. Why bother flushing pages that
2459 * will be truncated anyway? Also, should we error out here if
2460 * the flush returns error?
2461 */
2462 rc = filemap_write_and_wait(inode->i_mapping);
Pavel Shilovsky9a663962019-01-08 11:15:28 -08002463 if (is_interrupt_error(rc)) {
2464 rc = -ERESTARTSYS;
2465 goto cifs_setattr_exit;
2466 }
2467
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002468 mapping_set_error(inode->i_mapping, rc);
2469 rc = 0;
Jeff Laytoncea21802007-11-20 23:19:03 +00002470
Ronnie Sahlbergaa081852019-07-19 08:12:11 +10002471 if (attrs->ia_valid & ATTR_MTIME) {
2472 rc = cifs_get_writable_file(cifsInode, false, &wfile);
2473 if (!rc) {
2474 tcon = tlink_tcon(wfile->tlink);
2475 rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2476 cifsFileInfo_put(wfile);
2477 if (rc)
Chuhong Yuan783bf7b2019-10-14 15:15:31 +08002478 goto cifs_setattr_exit;
Ronnie Sahlbergaa081852019-07-19 08:12:11 +10002479 } else if (rc != -EBADF)
Chuhong Yuan783bf7b2019-10-14 15:15:31 +08002480 goto cifs_setattr_exit;
Ronnie Sahlbergaa081852019-07-19 08:12:11 +10002481 else
2482 rc = 0;
2483 }
2484
Steve French50531442008-03-14 19:21:31 +00002485 if (attrs->ia_valid & ATTR_SIZE) {
Jeff Layton8efdbde2008-07-23 21:28:12 +00002486 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2487 if (rc != 0)
Steve Frenche30dcf32005-09-20 20:49:16 -07002488 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Jeff Layton4ca691a2008-05-22 09:33:34 -04002490
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002491 if (attrs->ia_valid & ATTR_UID)
2492 uid = attrs->ia_uid;
2493
2494 if (attrs->ia_valid & ATTR_GID)
2495 gid = attrs->ia_gid;
2496
Steve French22442172019-07-19 08:15:55 +00002497 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2498 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002499 if (uid_valid(uid) || gid_valid(gid)) {
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002500 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2501 uid, gid);
2502 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002503 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2504 __func__, rc);
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002505 goto cifs_setattr_exit;
2506 }
2507 }
2508 } else
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002509 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
Jeff Layton4ca691a2008-05-22 09:33:34 -04002510 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Jeff Laytond32c4f22007-10-18 03:05:22 -07002512 /* skip mode change if it's just for clearing setuid/setgid */
2513 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2514 attrs->ia_valid &= ~ATTR_MODE;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (attrs->ia_valid & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 mode = attrs->ia_mode;
Steve Frenchcdbce9c82005-11-19 21:04:52 -08002518 rc = 0;
Steve French22442172019-07-19 08:15:55 +00002519 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2520 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002521 rc = id_mode_to_cifs_acl(inode, full_path, mode,
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002522 INVALID_UID, INVALID_GID);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -06002523 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002524 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2525 __func__, rc);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -06002526 goto cifs_setattr_exit;
2527 }
2528 } else
Jeff Layton51328612008-05-22 09:33:34 -04002529 if (((mode & S_IWUGO) == 0) &&
2530 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002531
2532 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2533
Jeff Layton51328612008-05-22 09:33:34 -04002534 /* fix up mode if we're not using dynperm */
2535 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2536 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2537 } else if ((mode & S_IWUGO) &&
2538 (cifsInode->cifsAttrs & ATTR_READONLY)) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002539
2540 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2541 /* Attributes of 0 are ignored */
2542 if (dosattr == 0)
2543 dosattr |= ATTR_NORMAL;
Jeff Layton51328612008-05-22 09:33:34 -04002544
2545 /* reset local inode permissions to normal */
2546 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2547 attrs->ia_mode &= ~(S_IALLUGO);
2548 if (S_ISDIR(inode->i_mode))
2549 attrs->ia_mode |=
2550 cifs_sb->mnt_dir_mode;
2551 else
2552 attrs->ia_mode |=
2553 cifs_sb->mnt_file_mode;
2554 }
2555 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2556 /* ignore mode change - ATTR_READONLY hasn't changed */
2557 attrs->ia_valid &= ~ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002561 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2562 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2563 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2564 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Steve Frenche30dcf32005-09-20 20:49:16 -07002566 /* Even if error on time set, no sense failing the call if
2567 the server would set the time to a reasonable value anyway,
2568 and this check ensures that we are not being called from
2569 sys_utimes in which case we ought to fail the call back to
2570 the user when the server rejects the call */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002571 if ((rc) && (attrs->ia_valid &
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002572 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
Steve Frenche30dcf32005-09-20 20:49:16 -07002573 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
2576 /* do not need local check to inode_check_ok since the server does
2577 that */
Christoph Hellwig10257742010-06-04 11:30:02 +02002578 if (rc)
2579 goto cifs_setattr_exit;
2580
2581 if ((attrs->ia_valid & ATTR_SIZE) &&
Christoph Hellwig1b947462010-07-18 17:51:21 -04002582 attrs->ia_size != i_size_read(inode))
2583 truncate_setsize(inode, attrs->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +02002584
2585 setattr_copy(inode, attrs);
2586 mark_inode_dirty(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +02002587
Steve Frenche30dcf32005-09-20 20:49:16 -07002588cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002590 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return rc;
2592}
2593
Jeff Layton0510eeb2008-08-02 07:26:12 -04002594int
2595cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2596{
Al Virofc640052016-04-10 01:33:30 -04002597 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
Steve French96daf2b2011-05-27 04:34:02 +00002598 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
Jeff Layton0510eeb2008-08-02 07:26:12 -04002599
2600 if (pTcon->unix_ext)
2601 return cifs_setattr_unix(direntry, attrs);
2602
2603 return cifs_setattr_nounix(direntry, attrs);
2604
2605 /* BB: add cifs_setattr_legacy for really old servers */
2606}
2607
Steve French99ee4db2007-02-27 05:35:17 +00002608#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609void cifs_delete_inode(struct inode *inode)
2610{
Joe Perchesf96637b2013-05-04 22:12:25 -05002611 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /* may have to add back in if and when safe distributed caching of
2613 directories added e.g. via FindNotify */
2614}
Steve French99ee4db2007-02-27 05:35:17 +00002615#endif