blob: ce9b22aecfbad9f02bc76b92376288d0efa486a8 [file] [log] [blame]
Steve French929be902021-06-18 00:31:49 -05001// SPDX-License-Identifier: LGPL-2.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * vfs operations that deal with dentries
Steve French5fdae1f2007-06-05 18:30:44 +00005 *
Steve Frenchc3b2a0c2009-02-20 04:32:45 +00006 * Copyright (C) International Business Machines Corp., 2002,2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/fs.h>
11#include <linux/stat.h>
12#include <linux/slab.h>
13#include <linux/namei.h>
Jeff Layton3bc303c2009-09-21 06:47:50 -040014#include <linux/mount.h>
Jeff Layton6ca9f3b2010-06-16 13:40:16 -040015#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "cifsfs.h"
17#include "cifspdu.h"
18#include "cifsglob.h"
19#include "cifsproto.h"
20#include "cifs_debug.h"
21#include "cifs_fs_sb.h"
Jeff Laytonec71e0e2013-09-05 08:38:11 -040022#include "cifs_unicode.h"
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060023#include "fs_context.h"
Steve French087f7572021-04-29 00:18:43 -050024#include "cifs_ioctl.h"
David Howells70431bf2020-11-17 15:56:59 +000025#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Steve French99ee4db2007-02-27 05:35:17 +000027static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070028renew_parental_timestamps(struct dentry *direntry)
29{
Steve French5fdae1f2007-06-05 18:30:44 +000030 /* BB check if there is a way to get the kernel to do this or if we
31 really need this */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 do {
Miklos Szeredia00be0e2016-09-16 12:44:21 +020033 cifs_set_time(direntry, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 direntry = direntry->d_parent;
Steve French5fdae1f2007-06-05 18:30:44 +000035 } while (!IS_ROOT(direntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Steve French6d3ea7e2012-11-28 22:34:41 -060038char *
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060039cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
Sachin Prabhu374402a2016-12-15 12:31:19 +053040 struct cifs_tcon *tcon, int add_treename)
Steve French6d3ea7e2012-11-28 22:34:41 -060041{
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060042 int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
Steve French6d3ea7e2012-11-28 22:34:41 -060043 int dfsplen;
44 char *full_path = NULL;
45
46 /* if no prefix path, simply set path to the root of share to "" */
47 if (pplen == 0) {
48 full_path = kzalloc(1, GFP_KERNEL);
49 return full_path;
50 }
51
Sachin Prabhu374402a2016-12-15 12:31:19 +053052 if (add_treename)
Steve French6d3ea7e2012-11-28 22:34:41 -060053 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
54 else
55 dfsplen = 0;
56
57 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
58 if (full_path == NULL)
59 return full_path;
60
61 if (dfsplen)
Ronnie Sahlberg340625e2019-08-27 09:30:14 +100062 memcpy(full_path, tcon->treeName, dfsplen);
Jeff Layton839db3d2012-12-10 06:10:45 -050063 full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060064 memcpy(full_path + dfsplen + 1, ctx->prepath, pplen);
Steve French6d3ea7e2012-11-28 22:34:41 -060065 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Steve French6d3ea7e2012-11-28 22:34:41 -060066 return full_path;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Note: caller must free return buffer */
Al Viro8e33cf22021-03-18 15:47:35 -040070const char *
Al Virof6a9bc32021-03-05 17:36:04 -050071build_path_from_dentry(struct dentry *direntry, void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Aurelien Aptel268a6352017-02-13 16:14:17 +010073 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
74 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
75 bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
76
Al Virof6a9bc32021-03-05 17:36:04 -050077 return build_path_from_dentry_optional_prefix(direntry, page,
Aurelien Aptel268a6352017-02-13 16:14:17 +010078 prefix);
79}
80
81char *
Al Virof6a9bc32021-03-05 17:36:04 -050082build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page,
83 bool prefix)
Aurelien Aptel268a6352017-02-13 16:14:17 +010084{
Steve French646dd532008-05-15 01:50:56 +000085 int dfsplen;
Aurelien Aptela6b50582016-05-25 19:59:09 +020086 int pplen = 0;
Jeff Layton0d424ad2010-09-20 16:01:35 -070087 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
Steve French96daf2b2011-05-27 04:34:02 +000088 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Al Viro991e72eb2021-03-05 21:53:48 -050089 char dirsep = CIFS_DIR_SEP(cifs_sb);
90 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Al Virof6a9bc32021-03-05 17:36:04 -050092 if (unlikely(!page))
93 return ERR_PTR(-ENOMEM);
94
Aurelien Aptel268a6352017-02-13 16:14:17 +010095 if (prefix)
Jeff Layton0d424ad2010-09-20 16:01:35 -070096 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
Steve French646dd532008-05-15 01:50:56 +000097 else
98 dfsplen = 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +020099
100 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
101 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
102
Ronnie Sahlberg981567b2021-08-10 16:33:55 +1000103 s = dentry_path_raw(direntry, page, PATH_MAX);
Al Viro991e72eb2021-03-05 21:53:48 -0500104 if (IS_ERR(s))
105 return s;
106 if (!s[1]) // for root we want "", not "/"
107 s++;
108 if (s < (char *)page + pplen + dfsplen)
Al Virof6a9bc32021-03-05 17:36:04 -0500109 return ERR_PTR(-ENAMETOOLONG);
Aurelien Aptela6b50582016-05-25 19:59:09 +0200110 if (pplen) {
Aurelien Aptela6b50582016-05-25 19:59:09 +0200111 cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
Al Viro991e72eb2021-03-05 21:53:48 -0500112 s -= pplen;
113 memcpy(s + 1, cifs_sb->prepath, pplen - 1);
114 *s = '/';
Aurelien Aptela6b50582016-05-25 19:59:09 +0200115 }
Al Viro991e72eb2021-03-05 21:53:48 -0500116 if (dirsep != '/') {
117 /* BB test paths to Windows with '/' in the midst of prepath */
118 char *p;
Aurelien Aptela6b50582016-05-25 19:59:09 +0200119
Al Viro991e72eb2021-03-05 21:53:48 -0500120 for (p = s; *p; p++)
121 if (*p == '/')
122 *p = dirsep;
123 }
Steve French646dd532008-05-15 01:50:56 +0000124 if (dfsplen) {
Al Viro991e72eb2021-03-05 21:53:48 -0500125 s -= dfsplen;
126 memcpy(s, tcon->treeName, dfsplen);
Steve French646dd532008-05-15 01:50:56 +0000127 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
128 int i;
129 for (i = 0; i < dfsplen; i++) {
Al Viro991e72eb2021-03-05 21:53:48 -0500130 if (s[i] == '\\')
131 s[i] = '/';
Steve French646dd532008-05-15 01:50:56 +0000132 }
133 }
134 }
Al Viro991e72eb2021-03-05 21:53:48 -0500135 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Miklos Szeredid2c12712012-06-05 15:10:23 +0200138/*
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000139 * Don't allow path components longer than the server max.
Miklos Szeredid2c12712012-06-05 15:10:23 +0200140 * Don't allow the separator character in a path component.
141 * The VFS will not allow "/", but "\" is allowed by posix.
142 */
143static int
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000144check_name(struct dentry *direntry, struct cifs_tcon *tcon)
Miklos Szeredid2c12712012-06-05 15:10:23 +0200145{
146 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
147 int i;
148
Ronnie Sahlbergf74bc7c2017-10-30 13:28:03 +1100149 if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
150 direntry->d_name.len >
Steve French6e3c1522017-08-27 16:56:08 -0500151 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000152 return -ENAMETOOLONG;
153
Miklos Szeredid2c12712012-06-05 15:10:23 +0200154 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
155 for (i = 0; i < direntry->d_name.len; i++) {
156 if (direntry->d_name.name[i] == '\\') {
Joe Perchesf96637b2013-05-04 22:12:25 -0500157 cifs_dbg(FYI, "Invalid file name\n");
Miklos Szeredid2c12712012-06-05 15:10:23 +0200158 return -EINVAL;
159 }
160 }
161 }
162 return 0;
163}
164
165
Steve French39798772006-05-31 22:40:51 +0000166/* Inode operations in similar order to how they appear in Linux file fs.h */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400168static int
169cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
170 struct tcon_link *tlink, unsigned oflags, umode_t mode,
Shirish Pargaonkarf1e32682013-12-11 16:29:53 -0600171 __u32 *oplock, struct cifs_fid *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int rc = -ENOENT;
Jeff Layton67750fb2008-05-09 22:28:02 +0000174 int create_options = CREATE_NOT_DIR;
Pavel Shilovsky25364132012-09-18 16:20:27 -0700175 int desired_access;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200176 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
177 struct cifs_tcon *tcon = tlink_tcon(tlink);
Al Virof6a9bc32021-03-05 17:36:04 -0500178 const char *full_path;
179 void *page = alloc_dentry_path();
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000180 FILE_ALL_INFO *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct inode *newinode = NULL;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200182 int disposition;
Pavel Shilovsky25364132012-09-18 16:20:27 -0700183 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400184 struct cifs_open_parms oparms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Miklos Szeredid2c12712012-06-05 15:10:23 +0200186 *oplock = 0;
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300187 if (tcon->ses->server->oplocks)
Miklos Szeredid2c12712012-06-05 15:10:23 +0200188 *oplock = REQ_OPLOCK;
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000189
Al Virof6a9bc32021-03-05 17:36:04 -0500190 full_path = build_path_from_dentry(direntry, page);
191 if (IS_ERR(full_path)) {
192 free_dentry_path(page);
193 return PTR_ERR(full_path);
194 }
Jeff Layton7ffec372010-09-29 19:51:11 -0400195
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400196 if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000197 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
198 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Pavel Shilovsky25364132012-09-18 16:20:27 -0700199 rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
200 oflags, oplock, &fid->netfid, xid);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200201 switch (rc) {
202 case 0:
203 if (newinode == NULL) {
204 /* query inode info */
Steve French90e4ee52009-05-08 03:04:30 +0000205 goto cifs_create_get_file_info;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200206 }
207
Sachin Prabhu8d9535b2016-07-07 21:28:27 +0100208 if (S_ISDIR(newinode->i_mode)) {
209 CIFSSMBClose(xid, tcon, fid->netfid);
210 iput(newinode);
211 rc = -EISDIR;
212 goto out;
213 }
214
Miklos Szeredid2c12712012-06-05 15:10:23 +0200215 if (!S_ISREG(newinode->i_mode)) {
216 /*
217 * The server may allow us to open things like
218 * FIFOs, but the client isn't set up to deal
219 * with that. If it's not a regular file, just
220 * close it and proceed as if it were a normal
221 * lookup.
222 */
Pavel Shilovsky25364132012-09-18 16:20:27 -0700223 CIFSSMBClose(xid, tcon, fid->netfid);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200224 goto cifs_create_get_file_info;
225 }
226 /* success, no need to query */
227 goto cifs_create_set_dentry;
228
229 case -ENOENT:
230 goto cifs_create_get_file_info;
231
232 case -EIO:
233 case -EINVAL:
234 /*
235 * EIO could indicate that (posix open) operation is not
236 * supported, despite what server claimed in capability
237 * negotiation.
238 *
239 * POSIX open in samba versions 3.3.1 and earlier could
240 * incorrectly fail with invalid parameter.
241 */
242 tcon->broken_posix_open = true;
243 break;
244
245 case -EREMOTE:
246 case -EOPNOTSUPP:
247 /*
248 * EREMOTE indicates DFS junction, which is not handled
249 * in posix open. If either that or op not supported
250 * returned, follow the normal lookup.
251 */
252 break;
253
254 default:
255 goto out;
256 }
257 /*
258 * fallthrough to retry, using older open call, this is case
259 * where server does not support this SMB level, and falsely
260 * claims capability (also get here for DFS case which should be
261 * rare for path not covered on files)
262 */
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000263 }
Steve Frenchf818dd52009-01-19 02:38:35 +0000264
Pavel Shilovsky25364132012-09-18 16:20:27 -0700265 desired_access = 0;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200266 if (OPEN_FMODE(oflags) & FMODE_READ)
Pavel Shilovsky25364132012-09-18 16:20:27 -0700267 desired_access |= GENERIC_READ; /* is this too little? */
Miklos Szeredid2c12712012-06-05 15:10:23 +0200268 if (OPEN_FMODE(oflags) & FMODE_WRITE)
Pavel Shilovsky25364132012-09-18 16:20:27 -0700269 desired_access |= GENERIC_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Miklos Szeredid2c12712012-06-05 15:10:23 +0200271 disposition = FILE_OVERWRITE_IF;
272 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
273 disposition = FILE_CREATE;
274 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
275 disposition = FILE_OVERWRITE_IF;
276 else if ((oflags & O_CREAT) == O_CREAT)
277 disposition = FILE_OPEN_IF;
278 else
Joe Perchesf96637b2013-05-04 22:12:25 -0500279 cifs_dbg(FYI, "Create flag not set in create function\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Pavel Shilovsky25364132012-09-18 16:20:27 -0700281 /*
282 * BB add processing to set equivalent of mode - e.g. via CreateX with
283 * ACLs
284 */
285
286 if (!server->ops->open) {
287 rc = -ENOSYS;
288 goto out;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Steve French5fdae1f2007-06-05 18:30:44 +0000291 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
292 if (buf == NULL) {
Jeff Layton232341b2010-08-05 13:58:38 -0400293 rc = -ENOMEM;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Jeff Layton67750fb2008-05-09 22:28:02 +0000296
Jeff Layton67750fb2008-05-09 22:28:02 +0000297 /*
298 * if we're not using unix extensions, see if we need to set
299 * ATTR_READONLY on the create call
300 */
Steve Frenchf818dd52009-01-19 02:38:35 +0000301 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
Jeff Layton67750fb2008-05-09 22:28:02 +0000302 create_options |= CREATE_OPTION_READONLY;
303
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400304 oparms.tcon = tcon;
305 oparms.cifs_sb = cifs_sb;
306 oparms.desired_access = desired_access;
Amir Goldstein0f060932020-02-03 21:46:43 +0200307 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400308 oparms.disposition = disposition;
309 oparms.path = full_path;
310 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400311 oparms.reconnect = false;
Steve Frenchce558b02018-05-31 19:16:54 -0500312 oparms.mode = mode;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400313 rc = server->ops->open(xid, &oparms, oplock, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500315 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200316 goto out;
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000317 }
318
Pavel Shilovsky25364132012-09-18 16:20:27 -0700319 /*
320 * If Open reported that we actually created a file then we now have to
321 * set the mode if possible.
322 */
Miklos Szeredid2c12712012-06-05 15:10:23 +0200323 if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000324 struct cifs_unix_set_info_args args = {
Jeff Layton4e1e7fb2008-08-02 07:26:12 -0400325 .mode = mode,
326 .ctime = NO_CHANGE_64,
327 .atime = NO_CHANGE_64,
328 .mtime = NO_CHANGE_64,
329 .device = 0,
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000330 };
Jeff Layton4e1e7fb2008-08-02 07:26:12 -0400331
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000332 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Eric W. Biederman49418b22013-02-06 00:57:56 -0800333 args.uid = current_fsuid();
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000334 if (inode->i_mode & S_ISGID)
Eric W. Biederman49418b22013-02-06 00:57:56 -0800335 args.gid = inode->i_gid;
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000336 else
Eric W. Biederman49418b22013-02-06 00:57:56 -0800337 args.gid = current_fsgid();
Steve French3ce53fc2007-06-08 14:55:14 +0000338 } else {
Eric W. Biederman49418b22013-02-06 00:57:56 -0800339 args.uid = INVALID_UID; /* no change */
340 args.gid = INVALID_GID; /* no change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Pavel Shilovsky25364132012-09-18 16:20:27 -0700342 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
343 current->tgid);
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000344 } else {
Pavel Shilovsky25364132012-09-18 16:20:27 -0700345 /*
346 * BB implement mode setting via Windows security
347 * descriptors e.g.
348 */
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000349 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000351 /* Could set r/o dos attribute if mode & 0222 == 0 */
352 }
353
354cifs_create_get_file_info:
355 /* server might mask mode so we have to query for it */
356 if (tcon->unix_ext)
Pavel Shilovsky25364132012-09-18 16:20:27 -0700357 rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
358 xid);
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000359 else {
Steve Frenchd3138522020-06-11 22:43:01 -0500360 /* TODO: Add support for calling POSIX query info here, but passing in fid */
Pavel Shilovsky25364132012-09-18 16:20:27 -0700361 rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
Steve French42eacf92014-02-10 14:08:16 -0600362 xid, fid);
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000363 if (newinode) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700364 if (server->ops->set_lease_key)
365 server->ops->set_lease_key(newinode, fid);
Al Viro4ab52602021-02-11 15:04:35 -0500366 if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) {
367 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
368 newinode->i_mode = mode;
369 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
370 newinode->i_uid = current_fsuid();
371 if (inode->i_mode & S_ISGID)
372 newinode->i_gid = inode->i_gid;
373 else
374 newinode->i_gid = current_fsgid();
375 }
Steve French6473a552005-11-29 20:20:10 -0800376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Steve Frenchc3b2a0c2009-02-20 04:32:45 +0000380cifs_create_set_dentry:
Miklos Szeredid2c12712012-06-05 15:10:23 +0200381 if (rc != 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500382 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
383 rc);
Sachin Prabhu8d9535b2016-07-07 21:28:27 +0100384 goto out_err;
Steve French5fdae1f2007-06-05 18:30:44 +0000385 }
Sachin Prabhu8d9535b2016-07-07 21:28:27 +0100386
Steve French09173102021-06-22 19:53:08 -0500387 if (newinode)
388 if (S_ISDIR(newinode->i_mode)) {
389 rc = -EISDIR;
390 goto out_err;
391 }
Sachin Prabhu8d9535b2016-07-07 21:28:27 +0100392
Miklos Szeredid2c12712012-06-05 15:10:23 +0200393 d_drop(direntry);
394 d_add(direntry, newinode);
Jeff Layton2422f672010-06-16 13:40:16 -0400395
Miklos Szeredid2c12712012-06-05 15:10:23 +0200396out:
Steve Frenchd14537f12005-04-28 22:41:05 -0700397 kfree(buf);
Al Virof6a9bc32021-03-05 17:36:04 -0500398 free_dentry_path(page);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200399 return rc;
Sachin Prabhu8d9535b2016-07-07 21:28:27 +0100400
401out_err:
402 if (server->ops->close)
403 server->ops->close(xid, tcon, fid);
404 if (newinode)
405 iput(newinode);
406 goto out;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200407}
408
Al Virod9585272012-06-22 12:39:14 +0400409int
Miklos Szeredid2c12712012-06-05 15:10:23 +0200410cifs_atomic_open(struct inode *inode, struct dentry *direntry,
Al Viro44907d72018-06-08 13:32:02 -0400411 struct file *file, unsigned oflags, umode_t mode)
Miklos Szeredid2c12712012-06-05 15:10:23 +0200412{
413 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400414 unsigned int xid;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200415 struct tcon_link *tlink;
416 struct cifs_tcon *tcon;
Pavel Shilovsky25364132012-09-18 16:20:27 -0700417 struct TCP_Server_Info *server;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700418 struct cifs_fid fid;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700419 struct cifs_pending_open open;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200420 __u32 oplock;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700421 struct cifsFileInfo *file_info;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200422
Steve French087f7572021-04-29 00:18:43 -0500423 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
424 return -EIO;
425
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700426 /*
427 * Posix open is only called (at lookup time) for file create now. For
Miklos Szeredid2c12712012-06-05 15:10:23 +0200428 * opens (rather than creates), because we do not know if it is a file
429 * or directory yet, and current Samba no longer allows us to do posix
430 * open on dirs, we could end up wasting an open call on what turns out
431 * to be a dir. For file opens, we wait to call posix open till
432 * cifs_open. It could be added to atomic_open in the future but the
433 * performance tradeoff of the extra network request when EISDIR or
434 * EACCES is returned would have to be weighed against the 50% reduction
435 * in network traffic in the other paths.
436 */
437 if (!(oflags & O_CREAT)) {
Sachin Prabhu3798f47a2012-11-05 11:39:32 +0000438 struct dentry *res;
439
440 /*
441 * Check for hashed negative dentry. We have already revalidated
442 * the dentry and it is fine. No need to perform another lookup.
443 */
Al Viro00699ad2016-07-05 09:44:53 -0400444 if (!d_in_lookup(direntry))
Sachin Prabhu3798f47a2012-11-05 11:39:32 +0000445 return -ENOENT;
446
447 res = cifs_lookup(inode, direntry, 0);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200448 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400449 return PTR_ERR(res);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200450
Al Viroe45198a2012-06-10 06:48:09 -0400451 return finish_no_open(file, res);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200452 }
453
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400454 xid = get_xid();
Miklos Szeredid2c12712012-06-05 15:10:23 +0200455
Al Viro35c265e2014-08-19 20:25:34 -0400456 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
457 inode, direntry, direntry);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200458
459 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
Wei Yongjunefb79f22013-04-04 14:16:21 +0800460 if (IS_ERR(tlink)) {
461 rc = PTR_ERR(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400462 goto out_free_xid;
Wei Yongjunefb79f22013-04-04 14:16:21 +0800463 }
Miklos Szeredid2c12712012-06-05 15:10:23 +0200464
465 tcon = tlink_tcon(tlink);
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000466
467 rc = check_name(direntry, tcon);
468 if (rc)
Ronnie Sahlbergf74bc7c2017-10-30 13:28:03 +1100469 goto out;
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000470
Pavel Shilovsky25364132012-09-18 16:20:27 -0700471 server = tcon->ses->server;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200472
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700473 if (server->ops->new_lease_key)
474 server->ops->new_lease_key(&fid);
475
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700476 cifs_add_pending_open(&fid, tlink, &open);
477
Miklos Szeredid2c12712012-06-05 15:10:23 +0200478 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
Shirish Pargaonkarf1e32682013-12-11 16:29:53 -0600479 &oplock, &fid);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200480
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700481 if (rc) {
482 cifs_del_pending_open(&open);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200483 goto out;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700484 }
Miklos Szeredid2c12712012-06-05 15:10:23 +0200485
Shirish Pargaonkarf1e32682013-12-11 16:29:53 -0600486 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
Al Viro73a09dd2018-06-08 13:22:02 -0400487 file->f_mode |= FMODE_CREATED;
Shirish Pargaonkarf1e32682013-12-11 16:29:53 -0600488
Al Virobe12af32018-06-08 11:44:56 -0400489 rc = finish_open(file, direntry, generic_file_open);
Al Viro30d90492012-06-22 12:40:19 +0400490 if (rc) {
Pavel Shilovsky25364132012-09-18 16:20:27 -0700491 if (server->ops->close)
492 server->ops->close(xid, tcon, &fid);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700493 cifs_del_pending_open(&open);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200494 goto out;
495 }
496
Namjae Jeon787aded2014-08-22 14:22:51 +0900497 if (file->f_flags & O_DIRECT &&
498 CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
499 if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
500 file->f_op = &cifs_file_direct_nobrl_ops;
501 else
502 file->f_op = &cifs_file_direct_ops;
503 }
504
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700505 file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
506 if (file_info == NULL) {
Pavel Shilovsky25364132012-09-18 16:20:27 -0700507 if (server->ops->close)
508 server->ops->close(xid, tcon, &fid);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700509 cifs_del_pending_open(&open);
Al Virod9585272012-06-22 12:39:14 +0400510 rc = -ENOMEM;
David Howells70431bf2020-11-17 15:56:59 +0000511 goto out;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200512 }
513
David Howells70431bf2020-11-17 15:56:59 +0000514 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
515 file->f_mode & FMODE_WRITE);
516
Miklos Szeredid2c12712012-06-05 15:10:23 +0200517out:
Jeff Layton7ffec372010-09-29 19:51:11 -0400518 cifs_put_tlink(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400519out_free_xid:
520 free_xid(xid);
Al Virod9585272012-06-22 12:39:14 +0400521 return rc;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200522}
523
Christian Brauner549c7292021-01-21 14:19:43 +0100524int cifs_create(struct user_namespace *mnt_userns, struct inode *inode,
525 struct dentry *direntry, umode_t mode, bool excl)
Miklos Szeredid2c12712012-06-05 15:10:23 +0200526{
527 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400528 unsigned int xid = get_xid();
Miklos Szeredid2c12712012-06-05 15:10:23 +0200529 /*
530 * BB below access is probably too much for mknod to request
531 * but we have to do query and setpathinfo so requesting
532 * less could fail (unless we want to request getatr and setatr
533 * permissions (only). At least for POSIX we do not have to
534 * request so much.
535 */
536 unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
537 struct tcon_link *tlink;
Pavel Shilovsky25364132012-09-18 16:20:27 -0700538 struct cifs_tcon *tcon;
539 struct TCP_Server_Info *server;
540 struct cifs_fid fid;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200541 __u32 oplock;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200542
Al Viro35c265e2014-08-19 20:25:34 -0400543 cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
544 inode, direntry, direntry);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200545
Steve French087f7572021-04-29 00:18:43 -0500546 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
547 return -EIO;
548
Miklos Szeredid2c12712012-06-05 15:10:23 +0200549 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
550 rc = PTR_ERR(tlink);
551 if (IS_ERR(tlink))
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400552 goto out_free_xid;
Miklos Szeredid2c12712012-06-05 15:10:23 +0200553
Pavel Shilovsky25364132012-09-18 16:20:27 -0700554 tcon = tlink_tcon(tlink);
555 server = tcon->ses->server;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700556
557 if (server->ops->new_lease_key)
558 server->ops->new_lease_key(&fid);
559
560 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
Shirish Pargaonkarf1e32682013-12-11 16:29:53 -0600561 &oplock, &fid);
Pavel Shilovsky25364132012-09-18 16:20:27 -0700562 if (!rc && server->ops->close)
563 server->ops->close(xid, tcon, &fid);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200564
565 cifs_put_tlink(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400566out_free_xid:
567 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return rc;
569}
570
Christian Brauner549c7292021-01-21 14:19:43 +0100571int cifs_mknod(struct user_namespace *mnt_userns, struct inode *inode,
572 struct dentry *direntry, umode_t mode, dev_t device_number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int rc = -EPERM;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400575 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -0400577 struct tcon_link *tlink;
Pavel Shilovskydd120672014-01-16 15:53:34 +0400578 struct cifs_tcon *tcon;
Al Virof6a9bc32021-03-05 17:36:04 -0500579 const char *full_path;
580 void *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (!old_valid_dev(device_number))
583 return -EINVAL;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 cifs_sb = CIFS_SB(inode->i_sb);
Steve French087f7572021-04-29 00:18:43 -0500586 if (unlikely(cifs_forced_shutdown(cifs_sb)))
587 return -EIO;
588
Jeff Layton7ffec372010-09-29 19:51:11 -0400589 tlink = cifs_sb_tlink(cifs_sb);
590 if (IS_ERR(tlink))
591 return PTR_ERR(tlink);
592
Al Virof6a9bc32021-03-05 17:36:04 -0500593 page = alloc_dentry_path();
Pavel Shilovskydd120672014-01-16 15:53:34 +0400594 tcon = tlink_tcon(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400595 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Al Virof6a9bc32021-03-05 17:36:04 -0500597 full_path = build_path_from_dentry(direntry, page);
598 if (IS_ERR(full_path)) {
599 rc = PTR_ERR(full_path);
Jeff Layton5d9ac7f2010-08-05 13:58:22 -0400600 goto mknod_out;
601 }
602
Aurelien Aptelc847dcc2019-03-14 00:29:17 -0500603 rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
604 full_path, mode,
605 device_number);
Jeff Layton5d9ac7f2010-08-05 13:58:22 -0400606
607mknod_out:
Al Virof6a9bc32021-03-05 17:36:04 -0500608 free_dentry_path(page);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400609 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400610 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return rc;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614struct dentry *
Steve French5fdae1f2007-06-05 18:30:44 +0000615cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400616 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400618 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int rc = 0; /* to get around spurious gcc warning, set to zero here */
620 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -0400621 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000622 struct cifs_tcon *pTcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 struct inode *newInode = NULL;
Al Virof6a9bc32021-03-05 17:36:04 -0500624 const char *full_path;
625 void *page;
Thiago Rafael Becker6efa9942021-06-15 13:42:56 -0300626 int retry_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400628 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Al Viro35c265e2014-08-19 20:25:34 -0400630 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
631 parent_dir_inode, direntry, direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* check whether path exists */
634
635 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400636 tlink = cifs_sb_tlink(cifs_sb);
637 if (IS_ERR(tlink)) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400638 free_xid(xid);
Al Viro11f17c92018-05-14 21:42:29 -0400639 return ERR_CAST(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -0400640 }
641 pTcon = tlink_tcon(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ronnie Sahlbergd3edede2017-08-23 14:48:14 +1000643 rc = check_name(direntry, pTcon);
Al Viro11f17c92018-05-14 21:42:29 -0400644 if (unlikely(rc)) {
645 cifs_put_tlink(tlink);
646 free_xid(xid);
647 return ERR_PTR(rc);
648 }
Jeff Layton5ddf1e02009-07-05 11:01:02 -0400649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 /* can not grab the rename sem here since it would
651 deadlock in the cases (beginning of sys_rename itself)
652 in which we already have the sb rename sem */
Al Virof6a9bc32021-03-05 17:36:04 -0500653 page = alloc_dentry_path();
654 full_path = build_path_from_dentry(direntry, page);
655 if (IS_ERR(full_path)) {
Al Viro11f17c92018-05-14 21:42:29 -0400656 cifs_put_tlink(tlink);
657 free_xid(xid);
Al Virof6a9bc32021-03-05 17:36:04 -0500658 free_dentry_path(page);
659 return ERR_CAST(full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
David Howells2b0143b2015-03-17 22:25:59 +0000662 if (d_really_is_positive(direntry)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500663 cifs_dbg(FYI, "non-NULL inode in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -0500665 cifs_dbg(FYI, "NULL inode in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Joe Perchesf96637b2013-05-04 22:12:25 -0500667 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000668 full_path, d_inode(direntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Thiago Rafael Becker6efa9942021-06-15 13:42:56 -0300670again:
Steve French790434f2020-06-11 22:28:49 -0500671 if (pTcon->posix_extensions)
672 rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
673 else if (pTcon->unix_ext) {
Miklos Szeredid2c12712012-06-05 15:10:23 +0200674 rc = cifs_get_inode_info_unix(&newInode, full_path,
675 parent_dir_inode->i_sb, xid);
676 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 rc = cifs_get_inode_info(&newInode, full_path, NULL,
Steve Frencha6ce4932009-04-09 01:14:32 +0000678 parent_dir_inode->i_sb, xid, NULL);
Miklos Szeredid2c12712012-06-05 15:10:23 +0200679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Al Viroa8b75f62018-05-14 21:26:32 -0400681 if (rc == 0) {
Steve French5fdae1f2007-06-05 18:30:44 +0000682 /* since paths are not looked up by component - the parent
Steve French3abb9272005-11-28 08:16:13 -0800683 directories are presumed to be good here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 renew_parental_timestamps(direntry);
Thiago Rafael Becker6efa9942021-06-15 13:42:56 -0300685 } else if (rc == -EAGAIN && retry_count++ < 10) {
686 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else if (rc == -ENOENT) {
Miklos Szeredia00be0e2016-09-16 12:44:21 +0200688 cifs_set_time(direntry, jiffies);
Al Viro11f17c92018-05-14 21:42:29 -0400689 newInode = NULL;
690 } else {
691 if (rc != -EACCES) {
692 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
693 /* We special case check for Access Denied - since that
694 is a common return code */
695 }
696 newInode = ERR_PTR(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Al Virof6a9bc32021-03-05 17:36:04 -0500698 free_dentry_path(page);
Jeff Layton7ffec372010-09-29 19:51:11 -0400699 cifs_put_tlink(tlink);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400700 free_xid(xid);
Al Viro11f17c92018-05-14 21:42:29 -0400701 return d_splice_alias(newInode, direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static int
Al Viro0b728e12012-06-10 16:03:43 -0400705cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Pavel Shilovsky0b3d0ef2019-09-30 10:06:20 -0700707 struct inode *inode;
Aurelien Aptel21b200d2021-02-05 15:42:48 +0100708 int rc;
Pavel Shilovsky0b3d0ef2019-09-30 10:06:20 -0700709
Al Viro0b728e12012-06-10 16:03:43 -0400710 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100711 return -ECHILD;
712
David Howells2b0143b2015-03-17 22:25:59 +0000713 if (d_really_is_positive(direntry)) {
Pavel Shilovsky0b3d0ef2019-09-30 10:06:20 -0700714 inode = d_inode(direntry);
715 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
716 CIFS_I(inode)->time = 0; /* force reval */
717
Aurelien Aptel21b200d2021-02-05 15:42:48 +0100718 rc = cifs_revalidate_dentry(direntry);
719 if (rc) {
720 cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
721 switch (rc) {
722 case -ENOENT:
723 case -ESTALE:
724 /*
725 * Those errors mean the dentry is invalid
726 * (file was deleted or recreated)
727 */
728 return 0;
729 default:
730 /*
731 * Otherwise some unexpected error happened
732 * report it as-is to VFS layer
733 */
734 return rc;
735 }
736 }
Gerlando Falautoad4778f2011-10-18 10:58:50 +0200737 else {
738 /*
Ian Kent936ad902012-05-02 07:19:09 -0400739 * If the inode wasn't known to be a dfs entry when
740 * the dentry was instantiated, such as when created
741 * via ->readdir(), it needs to be set now since the
742 * attributes will have been updated by
743 * cifs_revalidate_dentry().
Gerlando Falautoad4778f2011-10-18 10:58:50 +0200744 */
Pavel Shilovsky0b3d0ef2019-09-30 10:06:20 -0700745 if (IS_AUTOMOUNT(inode) &&
Ian Kent936ad902012-05-02 07:19:09 -0400746 !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
747 spin_lock(&direntry->d_lock);
748 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
749 spin_unlock(&direntry->d_lock);
750 }
751
Nick Piggin262f86a2010-11-11 18:42:16 +1100752 return 1;
Gerlando Falautoad4778f2011-10-18 10:58:50 +0200753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
Nick Piggin262f86a2010-11-11 18:42:16 +1100756 /*
757 * This may be nfsd (or something), anyway, we can't see the
758 * intent of this. So, since this can be for creation, drop it.
759 */
Al Viro0b728e12012-06-10 16:03:43 -0400760 if (!flags)
Nick Piggin262f86a2010-11-11 18:42:16 +1100761 return 0;
762
763 /*
764 * Drop the negative dentry, in order to make sure to use the
765 * case sensitive name which is specified by user if this is
766 * for creation.
767 */
Al Viro0b728e12012-06-10 16:03:43 -0400768 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
Al Viro407938e2011-06-25 21:37:18 -0400769 return 0;
Nick Piggin262f86a2010-11-11 18:42:16 +1100770
Miklos Szeredia00be0e2016-09-16 12:44:21 +0200771 if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
Nick Piggin262f86a2010-11-11 18:42:16 +1100772 return 0;
773
774 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/* static int cifs_d_delete(struct dentry *direntry)
778{
779 int rc = 0;
780
Al Viro35c265e2014-08-19 20:25:34 -0400781 cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 return rc;
784} */
785
Al Viro4fd03e82009-02-20 05:57:07 +0000786const struct dentry_operations cifs_dentry_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 .d_revalidate = cifs_d_revalidate,
David Howells01c64fe2011-01-14 18:45:47 +0000788 .d_automount = cifs_dfs_d_automount,
Steve French5fdae1f2007-06-05 18:30:44 +0000789/* d_delete: cifs_d_delete, */ /* not needed except for debugging */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790};
Steve Frenchb92327f2005-08-22 20:09:43 -0700791
Linus Torvaldsda53be12013-05-21 15:22:44 -0700792static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
Steve Frenchb92327f2005-08-22 20:09:43 -0700793{
Nick Pigginb1e6a012011-01-07 17:49:28 +1100794 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
Steve Frenchb92327f2005-08-22 20:09:43 -0700795 unsigned long hash;
Jeff Laytonec71e0e2013-09-05 08:38:11 -0400796 wchar_t c;
797 int i, charlen;
Steve Frenchb92327f2005-08-22 20:09:43 -0700798
Linus Torvalds8387ff22016-06-10 07:51:30 -0700799 hash = init_name_hash(dentry);
Jeff Laytonec71e0e2013-09-05 08:38:11 -0400800 for (i = 0; i < q->len; i += charlen) {
801 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
802 /* error out if we can't convert the character */
803 if (unlikely(charlen < 0))
804 return charlen;
805 hash = partial_name_hash(cifs_toupper(c), hash);
806 }
Steve Frenchb92327f2005-08-22 20:09:43 -0700807 q->hash = end_name_hash(hash);
808
809 return 0;
810}
811
Al Viro6fa67e72016-07-31 16:37:25 -0400812static int cifs_ci_compare(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100813 unsigned int len, const char *str, const struct qstr *name)
Steve Frenchb92327f2005-08-22 20:09:43 -0700814{
Al Virod3fe1982016-07-29 18:23:59 -0400815 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
Jeff Laytonec71e0e2013-09-05 08:38:11 -0400816 wchar_t c1, c2;
817 int i, l1, l2;
Steve Frenchb92327f2005-08-22 20:09:43 -0700818
Jeff Laytonec71e0e2013-09-05 08:38:11 -0400819 /*
820 * We make the assumption here that uppercase characters in the local
821 * codepage are always the same length as their lowercase counterparts.
822 *
823 * If that's ever not the case, then this will fail to match it.
824 */
825 if (name->len != len)
826 return 1;
827
828 for (i = 0; i < len; i += l1) {
829 /* Convert characters in both strings to UTF-16. */
830 l1 = codepage->char2uni(&str[i], len - i, &c1);
831 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
832
833 /*
834 * If we can't convert either character, just declare it to
835 * be 1 byte long and compare the original byte.
836 */
837 if (unlikely(l1 < 0 && l2 < 0)) {
838 if (str[i] != name->name[i])
839 return 1;
840 l1 = 1;
841 continue;
842 }
843
844 /*
845 * Here, we again ass|u|me that upper/lowercase versions of
846 * a character are the same length in the local NLS.
847 */
848 if (l1 != l2)
849 return 1;
850
851 /* Now compare uppercase versions of these characters */
852 if (cifs_toupper(c1) != cifs_toupper(c2))
853 return 1;
854 }
855
856 return 0;
Steve Frenchb92327f2005-08-22 20:09:43 -0700857}
858
Al Viro4fd03e82009-02-20 05:57:07 +0000859const struct dentry_operations cifs_ci_dentry_ops = {
Steve Frenchb92327f2005-08-22 20:09:43 -0700860 .d_revalidate = cifs_d_revalidate,
861 .d_hash = cifs_ci_hash,
862 .d_compare = cifs_ci_compare,
David Howells01c64fe2011-01-14 18:45:47 +0000863 .d_automount = cifs_dfs_d_automount,
Steve Frenchb92327f2005-08-22 20:09:43 -0700864};