Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * vfs operations that deal with dentries |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 5 | * |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 6 | * Copyright (C) International Business Machines Corp., 2002,2009 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/stat.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/namei.h> |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 14 | #include <linux/mount.h> |
Jeff Layton | 6ca9f3b | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 15 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #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 Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 22 | #include "cifs_unicode.h" |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 23 | #include "fs_context.h" |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 24 | #include "cifs_ioctl.h" |
David Howells | 70431bf | 2020-11-17 15:56:59 +0000 | [diff] [blame] | 25 | #include "fscache.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 27 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | renew_parental_timestamps(struct dentry *direntry) |
| 29 | { |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 30 | /* BB check if there is a way to get the kernel to do this or if we |
| 31 | really need this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | do { |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 33 | cifs_set_time(direntry, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | direntry = direntry->d_parent; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 35 | } while (!IS_ROOT(direntry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 38 | char * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 39 | cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
Sachin Prabhu | 374402a | 2016-12-15 12:31:19 +0530 | [diff] [blame] | 40 | struct cifs_tcon *tcon, int add_treename) |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 41 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 42 | int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0; |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 43 | 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 Prabhu | 374402a | 2016-12-15 12:31:19 +0530 | [diff] [blame] | 52 | if (add_treename) |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 53 | 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 Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 62 | memcpy(full_path, tcon->treeName, dfsplen); |
Jeff Layton | 839db3d | 2012-12-10 06:10:45 -0500 | [diff] [blame] | 63 | full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 64 | memcpy(full_path + dfsplen + 1, ctx->prepath, pplen); |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 65 | convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 66 | return full_path; |
| 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* Note: caller must free return buffer */ |
Al Viro | 8e33cf2 | 2021-03-18 15:47:35 -0400 | [diff] [blame] | 70 | const char * |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 71 | build_path_from_dentry(struct dentry *direntry, void *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Aurelien Aptel | 268a635 | 2017-02-13 16:14:17 +0100 | [diff] [blame] | 73 | 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 Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 77 | return build_path_from_dentry_optional_prefix(direntry, page, |
Aurelien Aptel | 268a635 | 2017-02-13 16:14:17 +0100 | [diff] [blame] | 78 | prefix); |
| 79 | } |
| 80 | |
| 81 | char * |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 82 | build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page, |
| 83 | bool prefix) |
Aurelien Aptel | 268a635 | 2017-02-13 16:14:17 +0100 | [diff] [blame] | 84 | { |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 85 | int dfsplen; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 86 | int pplen = 0; |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 87 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 88 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 89 | char dirsep = CIFS_DIR_SEP(cifs_sb); |
| 90 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 92 | if (unlikely(!page)) |
| 93 | return ERR_PTR(-ENOMEM); |
| 94 | |
Aurelien Aptel | 268a635 | 2017-02-13 16:14:17 +0100 | [diff] [blame] | 95 | if (prefix) |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 96 | dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1); |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 97 | else |
| 98 | dfsplen = 0; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 99 | |
| 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 Sahlberg | 981567b | 2021-08-10 16:33:55 +1000 | [diff] [blame] | 103 | s = dentry_path_raw(direntry, page, PATH_MAX); |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 104 | 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 Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 109 | return ERR_PTR(-ENAMETOOLONG); |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 110 | if (pplen) { |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 111 | cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath); |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 112 | s -= pplen; |
| 113 | memcpy(s + 1, cifs_sb->prepath, pplen - 1); |
| 114 | *s = '/'; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 115 | } |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 116 | if (dirsep != '/') { |
| 117 | /* BB test paths to Windows with '/' in the midst of prepath */ |
| 118 | char *p; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 119 | |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 120 | for (p = s; *p; p++) |
| 121 | if (*p == '/') |
| 122 | *p = dirsep; |
| 123 | } |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 124 | if (dfsplen) { |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 125 | s -= dfsplen; |
| 126 | memcpy(s, tcon->treeName, dfsplen); |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 127 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
| 128 | int i; |
| 129 | for (i = 0; i < dfsplen; i++) { |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 130 | if (s[i] == '\\') |
| 131 | s[i] = '/'; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
Al Viro | 991e72eb | 2021-03-05 21:53:48 -0500 | [diff] [blame] | 135 | return s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 138 | /* |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 139 | * Don't allow path components longer than the server max. |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 140 | * Don't allow the separator character in a path component. |
| 141 | * The VFS will not allow "/", but "\" is allowed by posix. |
| 142 | */ |
| 143 | static int |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 144 | check_name(struct dentry *direntry, struct cifs_tcon *tcon) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 145 | { |
| 146 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
| 147 | int i; |
| 148 | |
Ronnie Sahlberg | f74bc7c | 2017-10-30 13:28:03 +1100 | [diff] [blame] | 149 | if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength && |
| 150 | direntry->d_name.len > |
Steve French | 6e3c152 | 2017-08-27 16:56:08 -0500 | [diff] [blame] | 151 | le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength))) |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 152 | return -ENAMETOOLONG; |
| 153 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 154 | 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 Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 157 | cifs_dbg(FYI, "Invalid file name\n"); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 166 | /* Inode operations in similar order to how they appear in Linux file fs.h */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 168 | static int |
| 169 | cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid, |
| 170 | struct tcon_link *tlink, unsigned oflags, umode_t mode, |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 171 | __u32 *oplock, struct cifs_fid *fid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | int rc = -ENOENT; |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 174 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 175 | int desired_access; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 176 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 177 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 178 | const char *full_path; |
| 179 | void *page = alloc_dentry_path(); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 180 | FILE_ALL_INFO *buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | struct inode *newinode = NULL; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 182 | int disposition; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 183 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 184 | struct cifs_open_parms oparms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 186 | *oplock = 0; |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 187 | if (tcon->ses->server->oplocks) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 188 | *oplock = REQ_OPLOCK; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 189 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 190 | 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 Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 195 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 196 | if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open && |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 197 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 198 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 199 | rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode, |
| 200 | oflags, oplock, &fid->netfid, xid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 201 | switch (rc) { |
| 202 | case 0: |
| 203 | if (newinode == NULL) { |
| 204 | /* query inode info */ |
Steve French | 90e4ee5 | 2009-05-08 03:04:30 +0000 | [diff] [blame] | 205 | goto cifs_create_get_file_info; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 208 | if (S_ISDIR(newinode->i_mode)) { |
| 209 | CIFSSMBClose(xid, tcon, fid->netfid); |
| 210 | iput(newinode); |
| 211 | rc = -EISDIR; |
| 212 | goto out; |
| 213 | } |
| 214 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 215 | 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 Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 223 | CIFSSMBClose(xid, tcon, fid->netfid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 224 | 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 French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 263 | } |
Steve French | f818dd5 | 2009-01-19 02:38:35 +0000 | [diff] [blame] | 264 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 265 | desired_access = 0; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 266 | if (OPEN_FMODE(oflags) & FMODE_READ) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 267 | desired_access |= GENERIC_READ; /* is this too little? */ |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 268 | if (OPEN_FMODE(oflags) & FMODE_WRITE) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 269 | desired_access |= GENERIC_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 271 | 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 Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 279 | cifs_dbg(FYI, "Create flag not set in create function\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 281 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 291 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 292 | if (buf == NULL) { |
Jeff Layton | 232341b | 2010-08-05 13:58:38 -0400 | [diff] [blame] | 293 | rc = -ENOMEM; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 294 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 296 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 297 | /* |
| 298 | * if we're not using unix extensions, see if we need to set |
| 299 | * ATTR_READONLY on the create call |
| 300 | */ |
Steve French | f818dd5 | 2009-01-19 02:38:35 +0000 | [diff] [blame] | 301 | if (!tcon->unix_ext && (mode & S_IWUGO) == 0) |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 302 | create_options |= CREATE_OPTION_READONLY; |
| 303 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 304 | oparms.tcon = tcon; |
| 305 | oparms.cifs_sb = cifs_sb; |
| 306 | oparms.desired_access = desired_access; |
Amir Goldstein | 0f06093 | 2020-02-03 21:46:43 +0200 | [diff] [blame] | 307 | oparms.create_options = cifs_create_options(cifs_sb, create_options); |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 308 | oparms.disposition = disposition; |
| 309 | oparms.path = full_path; |
| 310 | oparms.fid = fid; |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 311 | oparms.reconnect = false; |
Steve French | ce558b0 | 2018-05-31 19:16:54 -0500 | [diff] [blame] | 312 | oparms.mode = mode; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 313 | rc = server->ops->open(xid, &oparms, oplock, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 315 | cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 316 | goto out; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 319 | /* |
| 320 | * If Open reported that we actually created a file then we now have to |
| 321 | * set the mode if possible. |
| 322 | */ |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 323 | if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) { |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 324 | struct cifs_unix_set_info_args args = { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 325 | .mode = mode, |
| 326 | .ctime = NO_CHANGE_64, |
| 327 | .atime = NO_CHANGE_64, |
| 328 | .mtime = NO_CHANGE_64, |
| 329 | .device = 0, |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 330 | }; |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 331 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 332 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 333 | args.uid = current_fsuid(); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 334 | if (inode->i_mode & S_ISGID) |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 335 | args.gid = inode->i_gid; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 336 | else |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 337 | args.gid = current_fsgid(); |
Steve French | 3ce53fc | 2007-06-08 14:55:14 +0000 | [diff] [blame] | 338 | } else { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 339 | args.uid = INVALID_UID; /* no change */ |
| 340 | args.gid = INVALID_GID; /* no change */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 342 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid, |
| 343 | current->tgid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 344 | } else { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 345 | /* |
| 346 | * BB implement mode setting via Windows security |
| 347 | * descriptors e.g. |
| 348 | */ |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 349 | /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 351 | /* Could set r/o dos attribute if mode & 0222 == 0 */ |
| 352 | } |
| 353 | |
| 354 | cifs_create_get_file_info: |
| 355 | /* server might mask mode so we have to query for it */ |
| 356 | if (tcon->unix_ext) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 357 | rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb, |
| 358 | xid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 359 | else { |
Steve French | d313852 | 2020-06-11 22:43:01 -0500 | [diff] [blame] | 360 | /* TODO: Add support for calling POSIX query info here, but passing in fid */ |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 361 | rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb, |
Steve French | 42eacf9 | 2014-02-10 14:08:16 -0600 | [diff] [blame] | 362 | xid, fid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 363 | if (newinode) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 364 | if (server->ops->set_lease_key) |
| 365 | server->ops->set_lease_key(newinode, fid); |
Al Viro | 4ab5260 | 2021-02-11 15:04:35 -0500 | [diff] [blame] | 366 | 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 French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 380 | cifs_create_set_dentry: |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 381 | if (rc != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 382 | cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n", |
| 383 | rc); |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 384 | goto out_err; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 385 | } |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 386 | |
Steve French | 0917310 | 2021-06-22 19:53:08 -0500 | [diff] [blame] | 387 | if (newinode) |
| 388 | if (S_ISDIR(newinode->i_mode)) { |
| 389 | rc = -EISDIR; |
| 390 | goto out_err; |
| 391 | } |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 392 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 393 | d_drop(direntry); |
| 394 | d_add(direntry, newinode); |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 395 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 396 | out: |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 397 | kfree(buf); |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 398 | free_dentry_path(page); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 399 | return rc; |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 400 | |
| 401 | out_err: |
| 402 | if (server->ops->close) |
| 403 | server->ops->close(xid, tcon, fid); |
| 404 | if (newinode) |
| 405 | iput(newinode); |
| 406 | goto out; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 409 | int |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 410 | cifs_atomic_open(struct inode *inode, struct dentry *direntry, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 411 | struct file *file, unsigned oflags, umode_t mode) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 412 | { |
| 413 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 414 | unsigned int xid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 415 | struct tcon_link *tlink; |
| 416 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 417 | struct TCP_Server_Info *server; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 418 | struct cifs_fid fid; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 419 | struct cifs_pending_open open; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 420 | __u32 oplock; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 421 | struct cifsFileInfo *file_info; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 422 | |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 423 | if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb)))) |
| 424 | return -EIO; |
| 425 | |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 426 | /* |
| 427 | * Posix open is only called (at lookup time) for file create now. For |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 428 | * 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 Prabhu | 3798f47a | 2012-11-05 11:39:32 +0000 | [diff] [blame] | 438 | 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 Viro | 00699ad | 2016-07-05 09:44:53 -0400 | [diff] [blame] | 444 | if (!d_in_lookup(direntry)) |
Sachin Prabhu | 3798f47a | 2012-11-05 11:39:32 +0000 | [diff] [blame] | 445 | return -ENOENT; |
| 446 | |
| 447 | res = cifs_lookup(inode, direntry, 0); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 448 | if (IS_ERR(res)) |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 449 | return PTR_ERR(res); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 450 | |
Al Viro | e45198a | 2012-06-10 06:48:09 -0400 | [diff] [blame] | 451 | return finish_no_open(file, res); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 454 | xid = get_xid(); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 455 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 456 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 457 | inode, direntry, direntry); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 458 | |
| 459 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); |
Wei Yongjun | efb79f2 | 2013-04-04 14:16:21 +0800 | [diff] [blame] | 460 | if (IS_ERR(tlink)) { |
| 461 | rc = PTR_ERR(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 462 | goto out_free_xid; |
Wei Yongjun | efb79f2 | 2013-04-04 14:16:21 +0800 | [diff] [blame] | 463 | } |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 464 | |
| 465 | tcon = tlink_tcon(tlink); |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 466 | |
| 467 | rc = check_name(direntry, tcon); |
| 468 | if (rc) |
Ronnie Sahlberg | f74bc7c | 2017-10-30 13:28:03 +1100 | [diff] [blame] | 469 | goto out; |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 470 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 471 | server = tcon->ses->server; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 472 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 473 | if (server->ops->new_lease_key) |
| 474 | server->ops->new_lease_key(&fid); |
| 475 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 476 | cifs_add_pending_open(&fid, tlink, &open); |
| 477 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 478 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 479 | &oplock, &fid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 480 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 481 | if (rc) { |
| 482 | cifs_del_pending_open(&open); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 483 | goto out; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 484 | } |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 485 | |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 486 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
Al Viro | 73a09dd | 2018-06-08 13:22:02 -0400 | [diff] [blame] | 487 | file->f_mode |= FMODE_CREATED; |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 488 | |
Al Viro | be12af3 | 2018-06-08 11:44:56 -0400 | [diff] [blame] | 489 | rc = finish_open(file, direntry, generic_file_open); |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 490 | if (rc) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 491 | if (server->ops->close) |
| 492 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 493 | cifs_del_pending_open(&open); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 494 | goto out; |
| 495 | } |
| 496 | |
Namjae Jeon | 787aded | 2014-08-22 14:22:51 +0900 | [diff] [blame] | 497 | 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 Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 505 | file_info = cifs_new_fileinfo(&fid, file, tlink, oplock); |
| 506 | if (file_info == NULL) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 507 | if (server->ops->close) |
| 508 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 509 | cifs_del_pending_open(&open); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 510 | rc = -ENOMEM; |
David Howells | 70431bf | 2020-11-17 15:56:59 +0000 | [diff] [blame] | 511 | goto out; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 512 | } |
| 513 | |
David Howells | 70431bf | 2020-11-17 15:56:59 +0000 | [diff] [blame] | 514 | fscache_use_cookie(cifs_inode_cookie(file_inode(file)), |
| 515 | file->f_mode & FMODE_WRITE); |
| 516 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 517 | out: |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 518 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 519 | out_free_xid: |
| 520 | free_xid(xid); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 521 | return rc; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 524 | int cifs_create(struct user_namespace *mnt_userns, struct inode *inode, |
| 525 | struct dentry *direntry, umode_t mode, bool excl) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 526 | { |
| 527 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 528 | unsigned int xid = get_xid(); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 529 | /* |
| 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 Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 538 | struct cifs_tcon *tcon; |
| 539 | struct TCP_Server_Info *server; |
| 540 | struct cifs_fid fid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 541 | __u32 oplock; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 542 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 543 | cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 544 | inode, direntry, direntry); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 545 | |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 546 | if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb)))) |
| 547 | return -EIO; |
| 548 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 549 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); |
| 550 | rc = PTR_ERR(tlink); |
| 551 | if (IS_ERR(tlink)) |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 552 | goto out_free_xid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 553 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 554 | tcon = tlink_tcon(tlink); |
| 555 | server = tcon->ses->server; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 556 | |
| 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 Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 561 | &oplock, &fid); |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 562 | if (!rc && server->ops->close) |
| 563 | server->ops->close(xid, tcon, &fid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 564 | |
| 565 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 566 | out_free_xid: |
| 567 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return rc; |
| 569 | } |
| 570 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 571 | int cifs_mknod(struct user_namespace *mnt_userns, struct inode *inode, |
| 572 | struct dentry *direntry, umode_t mode, dev_t device_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
| 574 | int rc = -EPERM; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 575 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 577 | struct tcon_link *tlink; |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 578 | struct cifs_tcon *tcon; |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 579 | const char *full_path; |
| 580 | void *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | if (!old_valid_dev(device_number)) |
| 583 | return -EINVAL; |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | cifs_sb = CIFS_SB(inode->i_sb); |
Steve French | 087f757 | 2021-04-29 00:18:43 -0500 | [diff] [blame] | 586 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 587 | return -EIO; |
| 588 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 589 | tlink = cifs_sb_tlink(cifs_sb); |
| 590 | if (IS_ERR(tlink)) |
| 591 | return PTR_ERR(tlink); |
| 592 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 593 | page = alloc_dentry_path(); |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 594 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 595 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 597 | full_path = build_path_from_dentry(direntry, page); |
| 598 | if (IS_ERR(full_path)) { |
| 599 | rc = PTR_ERR(full_path); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 600 | goto mknod_out; |
| 601 | } |
| 602 | |
Aurelien Aptel | c847dcc | 2019-03-14 00:29:17 -0500 | [diff] [blame] | 603 | rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon, |
| 604 | full_path, mode, |
| 605 | device_number); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 606 | |
| 607 | mknod_out: |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 608 | free_dentry_path(page); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 609 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 610 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return rc; |
| 612 | } |
| 613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | struct dentry * |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 615 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 616 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 618 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ |
| 620 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 621 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 622 | struct cifs_tcon *pTcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | struct inode *newInode = NULL; |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 624 | const char *full_path; |
| 625 | void *page; |
Thiago Rafael Becker | 6efa994 | 2021-06-15 13:42:56 -0300 | [diff] [blame] | 626 | int retry_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 628 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 630 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 631 | parent_dir_inode, direntry, direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | /* check whether path exists */ |
| 634 | |
| 635 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 636 | tlink = cifs_sb_tlink(cifs_sb); |
| 637 | if (IS_ERR(tlink)) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 638 | free_xid(xid); |
Al Viro | 11f17c9 | 2018-05-14 21:42:29 -0400 | [diff] [blame] | 639 | return ERR_CAST(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 640 | } |
| 641 | pTcon = tlink_tcon(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Ronnie Sahlberg | d3edede | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 643 | rc = check_name(direntry, pTcon); |
Al Viro | 11f17c9 | 2018-05-14 21:42:29 -0400 | [diff] [blame] | 644 | if (unlikely(rc)) { |
| 645 | cifs_put_tlink(tlink); |
| 646 | free_xid(xid); |
| 647 | return ERR_PTR(rc); |
| 648 | } |
Jeff Layton | 5ddf1e0 | 2009-07-05 11:01:02 -0400 | [diff] [blame] | 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | /* 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 Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 653 | page = alloc_dentry_path(); |
| 654 | full_path = build_path_from_dentry(direntry, page); |
| 655 | if (IS_ERR(full_path)) { |
Al Viro | 11f17c9 | 2018-05-14 21:42:29 -0400 | [diff] [blame] | 656 | cifs_put_tlink(tlink); |
| 657 | free_xid(xid); |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 658 | free_dentry_path(page); |
| 659 | return ERR_CAST(full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 662 | if (d_really_is_positive(direntry)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 663 | cifs_dbg(FYI, "non-NULL inode in lookup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 665 | cifs_dbg(FYI, "NULL inode in lookup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 667 | cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 668 | full_path, d_inode(direntry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Thiago Rafael Becker | 6efa994 | 2021-06-15 13:42:56 -0300 | [diff] [blame] | 670 | again: |
Steve French | 790434f | 2020-06-11 22:28:49 -0500 | [diff] [blame] | 671 | 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 Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 674 | rc = cifs_get_inode_info_unix(&newInode, full_path, |
| 675 | parent_dir_inode->i_sb, xid); |
| 676 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | rc = cifs_get_inode_info(&newInode, full_path, NULL, |
Steve French | a6ce493 | 2009-04-09 01:14:32 +0000 | [diff] [blame] | 678 | parent_dir_inode->i_sb, xid, NULL); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
Al Viro | a8b75f6 | 2018-05-14 21:26:32 -0400 | [diff] [blame] | 681 | if (rc == 0) { |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 682 | /* since paths are not looked up by component - the parent |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 683 | directories are presumed to be good here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | renew_parental_timestamps(direntry); |
Thiago Rafael Becker | 6efa994 | 2021-06-15 13:42:56 -0300 | [diff] [blame] | 685 | } else if (rc == -EAGAIN && retry_count++ < 10) { |
| 686 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | } else if (rc == -ENOENT) { |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 688 | cifs_set_time(direntry, jiffies); |
Al Viro | 11f17c9 | 2018-05-14 21:42:29 -0400 | [diff] [blame] | 689 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | } |
Al Viro | f6a9bc3 | 2021-03-05 17:36:04 -0500 | [diff] [blame] | 698 | free_dentry_path(page); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 699 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 700 | free_xid(xid); |
Al Viro | 11f17c9 | 2018-05-14 21:42:29 -0400 | [diff] [blame] | 701 | return d_splice_alias(newInode, direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | static int |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 705 | cifs_d_revalidate(struct dentry *direntry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Pavel Shilovsky | 0b3d0ef | 2019-09-30 10:06:20 -0700 | [diff] [blame] | 707 | struct inode *inode; |
Aurelien Aptel | 21b200d | 2021-02-05 15:42:48 +0100 | [diff] [blame] | 708 | int rc; |
Pavel Shilovsky | 0b3d0ef | 2019-09-30 10:06:20 -0700 | [diff] [blame] | 709 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 710 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 711 | return -ECHILD; |
| 712 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 713 | if (d_really_is_positive(direntry)) { |
Pavel Shilovsky | 0b3d0ef | 2019-09-30 10:06:20 -0700 | [diff] [blame] | 714 | 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 Aptel | 21b200d | 2021-02-05 15:42:48 +0100 | [diff] [blame] | 718 | 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 Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 737 | else { |
| 738 | /* |
Ian Kent | 936ad90 | 2012-05-02 07:19:09 -0400 | [diff] [blame] | 739 | * 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 Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 744 | */ |
Pavel Shilovsky | 0b3d0ef | 2019-09-30 10:06:20 -0700 | [diff] [blame] | 745 | if (IS_AUTOMOUNT(inode) && |
Ian Kent | 936ad90 | 2012-05-02 07:19:09 -0400 | [diff] [blame] | 746 | !(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 Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 752 | return 1; |
Gerlando Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 756 | /* |
| 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 Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 760 | if (!flags) |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 761 | 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 Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 768 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
Al Viro | 407938e | 2011-06-25 21:37:18 -0400 | [diff] [blame] | 769 | return 0; |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 770 | |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 771 | if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled) |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 772 | return 0; |
| 773 | |
| 774 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* static int cifs_d_delete(struct dentry *direntry) |
| 778 | { |
| 779 | int rc = 0; |
| 780 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 781 | cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | return rc; |
| 784 | } */ |
| 785 | |
Al Viro | 4fd03e8 | 2009-02-20 05:57:07 +0000 | [diff] [blame] | 786 | const struct dentry_operations cifs_dentry_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | .d_revalidate = cifs_d_revalidate, |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 788 | .d_automount = cifs_dfs_d_automount, |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 789 | /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | }; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 791 | |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 792 | static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q) |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 793 | { |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 794 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 795 | unsigned long hash; |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 796 | wchar_t c; |
| 797 | int i, charlen; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 798 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 799 | hash = init_name_hash(dentry); |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 800 | 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 French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 807 | q->hash = end_name_hash(hash); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 812 | static int cifs_ci_compare(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 813 | unsigned int len, const char *str, const struct qstr *name) |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 814 | { |
Al Viro | d3fe198 | 2016-07-29 18:23:59 -0400 | [diff] [blame] | 815 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 816 | wchar_t c1, c2; |
| 817 | int i, l1, l2; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 818 | |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 819 | /* |
| 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 French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Al Viro | 4fd03e8 | 2009-02-20 05:57:07 +0000 | [diff] [blame] | 859 | const struct dentry_operations cifs_ci_dentry_ops = { |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 860 | .d_revalidate = cifs_d_revalidate, |
| 861 | .d_hash = cifs_ci_hash, |
| 862 | .d_compare = cifs_ci_compare, |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 863 | .d_automount = cifs_dfs_d_automount, |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 864 | }; |