Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/dir.c |
| 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 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/stat.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/namei.h> |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 27 | #include <linux/mount.h> |
Jeff Layton | 6ca9f3b | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 28 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "cifsfs.h" |
| 30 | #include "cifspdu.h" |
| 31 | #include "cifsglob.h" |
| 32 | #include "cifsproto.h" |
| 33 | #include "cifs_debug.h" |
| 34 | #include "cifs_fs_sb.h" |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 35 | #include "cifs_unicode.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 37 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | renew_parental_timestamps(struct dentry *direntry) |
| 39 | { |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 40 | /* BB check if there is a way to get the kernel to do this or if we |
| 41 | really need this */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | do { |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 43 | cifs_set_time(direntry, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | direntry = direntry->d_parent; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 45 | } while (!IS_ROOT(direntry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 48 | char * |
| 49 | cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb, |
| 50 | struct cifs_tcon *tcon) |
| 51 | { |
Jeff Layton | 839db3d | 2012-12-10 06:10:45 -0500 | [diff] [blame] | 52 | int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0; |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 53 | int dfsplen; |
| 54 | char *full_path = NULL; |
| 55 | |
| 56 | /* if no prefix path, simply set path to the root of share to "" */ |
| 57 | if (pplen == 0) { |
| 58 | full_path = kzalloc(1, GFP_KERNEL); |
| 59 | return full_path; |
| 60 | } |
| 61 | |
| 62 | if (tcon->Flags & SMB_SHARE_IS_IN_DFS) |
| 63 | dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1); |
| 64 | else |
| 65 | dfsplen = 0; |
| 66 | |
| 67 | full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); |
| 68 | if (full_path == NULL) |
| 69 | return full_path; |
| 70 | |
| 71 | if (dfsplen) |
| 72 | strncpy(full_path, tcon->treeName, dfsplen); |
Jeff Layton | 839db3d | 2012-12-10 06:10:45 -0500 | [diff] [blame] | 73 | full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb); |
| 74 | strncpy(full_path + dfsplen + 1, vol->prepath, pplen); |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 75 | convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); |
| 76 | full_path[dfsplen + pplen] = 0; /* add trailing null */ |
| 77 | return full_path; |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* Note: caller must free return buffer */ |
| 81 | char * |
| 82 | build_path_from_dentry(struct dentry *direntry) |
| 83 | { |
| 84 | struct dentry *temp; |
Steve French | 2fe87f0 | 2006-09-21 07:02:52 +0000 | [diff] [blame] | 85 | int namelen; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 86 | int dfsplen; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 87 | int pplen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | char *full_path; |
Steve French | 8827481 | 2006-03-09 22:21:45 +0000 | [diff] [blame] | 89 | char dirsep; |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 90 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 91 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 92 | unsigned seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 94 | dirsep = CIFS_DIR_SEP(cifs_sb); |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 95 | if (tcon->Flags & SMB_SHARE_IS_IN_DFS) |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | cifs_bp_rename_retry: |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 104 | namelen = dfsplen + pplen; |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 105 | seq = read_seqbegin(&rename_lock); |
| 106 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | for (temp = direntry; !IS_ROOT(temp);) { |
| 108 | namelen += (1 + temp->d_name.len); |
| 109 | temp = temp->d_parent; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 110 | if (temp == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 111 | cifs_dbg(VFS, "corrupt dentry\n"); |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 112 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return NULL; |
| 114 | } |
| 115 | } |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 116 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | full_path = kmalloc(namelen+1, GFP_KERNEL); |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 119 | if (full_path == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return full_path; |
| 121 | full_path[namelen] = 0; /* trailing null */ |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 122 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | for (temp = direntry; !IS_ROOT(temp);) { |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 124 | spin_lock(&temp->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | namelen -= 1 + temp->d_name.len; |
| 126 | if (namelen < 0) { |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 127 | spin_unlock(&temp->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | break; |
| 129 | } else { |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 130 | full_path[namelen] = dirsep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | strncpy(full_path + namelen + 1, temp->d_name.name, |
| 132 | temp->d_name.len); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 133 | cifs_dbg(FYI, "name: %s\n", full_path + namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 135 | spin_unlock(&temp->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | temp = temp->d_parent; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 137 | if (temp == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 138 | cifs_dbg(VFS, "corrupt dentry\n"); |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 139 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | kfree(full_path); |
| 141 | return NULL; |
| 142 | } |
| 143 | } |
Al Viro | dc137bf | 2011-07-16 23:37:20 -0400 | [diff] [blame] | 144 | rcu_read_unlock(); |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 145 | if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 146 | cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n", |
| 147 | namelen, dfsplen); |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 148 | /* presumably this is only possible if racing with a rename |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | of one of the parent directories (we can not lock the dentries |
| 150 | above us to prevent this, but retrying should be harmless) */ |
| 151 | kfree(full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | goto cifs_bp_rename_retry; |
| 153 | } |
Steve French | 2fe87f0 | 2006-09-21 07:02:52 +0000 | [diff] [blame] | 154 | /* DIR_SEP already set for byte 0 / vs \ but not for |
| 155 | subsequent slashes in prepath which currently must |
| 156 | be entered the right way - not sure if there is an alternative |
| 157 | since the '\' is a valid posix character so we can not switch |
| 158 | those safely to '/' if any are found in the middle of the prepath */ |
| 159 | /* BB test paths to Windows with '/' in the midst of prepath */ |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 160 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 161 | if (pplen) { |
| 162 | int i; |
| 163 | |
| 164 | cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath); |
| 165 | memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1); |
| 166 | full_path[dfsplen] = '\\'; |
| 167 | for (i = 0; i < pplen-1; i++) |
| 168 | if (full_path[dfsplen+1+i] == '/') |
| 169 | full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb); |
| 170 | } |
| 171 | |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 172 | if (dfsplen) { |
Jeff Layton | 0d424ad | 2010-09-20 16:01:35 -0700 | [diff] [blame] | 173 | strncpy(full_path, tcon->treeName, dfsplen); |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 174 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
| 175 | int i; |
| 176 | for (i = 0; i < dfsplen; i++) { |
| 177 | if (full_path[i] == '\\') |
| 178 | full_path[i] = '/'; |
| 179 | } |
| 180 | } |
| 181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return full_path; |
| 183 | } |
| 184 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 185 | /* |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 186 | * Don't allow path components longer than the server max. |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 187 | * Don't allow the separator character in a path component. |
| 188 | * The VFS will not allow "/", but "\" is allowed by posix. |
| 189 | */ |
| 190 | static int |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 191 | check_name(struct dentry *direntry, struct cifs_tcon *tcon) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 192 | { |
| 193 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
| 194 | int i; |
| 195 | |
Ronnie Sahlberg | 5e8b028 | 2017-10-30 13:28:03 +1100 | [diff] [blame^] | 196 | if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength && |
| 197 | direntry->d_name.len > |
Steve French | e2ae90b | 2017-08-27 16:56:08 -0500 | [diff] [blame] | 198 | le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength))) |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 199 | return -ENAMETOOLONG; |
| 200 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 201 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { |
| 202 | for (i = 0; i < direntry->d_name.len; i++) { |
| 203 | if (direntry->d_name.name[i] == '\\') { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 204 | cifs_dbg(FYI, "Invalid file name\n"); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 205 | return -EINVAL; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 213 | /* 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] | 214 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 215 | static int |
| 216 | cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid, |
| 217 | struct tcon_link *tlink, unsigned oflags, umode_t mode, |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 218 | __u32 *oplock, struct cifs_fid *fid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
| 220 | int rc = -ENOENT; |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 221 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 222 | int desired_access; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 223 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 224 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | char *full_path = NULL; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 226 | FILE_ALL_INFO *buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct inode *newinode = NULL; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 228 | int disposition; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 229 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 230 | struct cifs_open_parms oparms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 232 | *oplock = 0; |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 233 | if (tcon->ses->server->oplocks) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 234 | *oplock = REQ_OPLOCK; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 235 | |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 236 | full_path = build_path_from_dentry(direntry); |
| 237 | if (full_path == NULL) { |
| 238 | rc = -ENOMEM; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 239 | goto out; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 242 | if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open && |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 243 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 244 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 245 | rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode, |
| 246 | oflags, oplock, &fid->netfid, xid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 247 | switch (rc) { |
| 248 | case 0: |
| 249 | if (newinode == NULL) { |
| 250 | /* query inode info */ |
Steve French | 90e4ee5 | 2009-05-08 03:04:30 +0000 | [diff] [blame] | 251 | goto cifs_create_get_file_info; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 254 | if (S_ISDIR(newinode->i_mode)) { |
| 255 | CIFSSMBClose(xid, tcon, fid->netfid); |
| 256 | iput(newinode); |
| 257 | rc = -EISDIR; |
| 258 | goto out; |
| 259 | } |
| 260 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 261 | if (!S_ISREG(newinode->i_mode)) { |
| 262 | /* |
| 263 | * The server may allow us to open things like |
| 264 | * FIFOs, but the client isn't set up to deal |
| 265 | * with that. If it's not a regular file, just |
| 266 | * close it and proceed as if it were a normal |
| 267 | * lookup. |
| 268 | */ |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 269 | CIFSSMBClose(xid, tcon, fid->netfid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 270 | goto cifs_create_get_file_info; |
| 271 | } |
| 272 | /* success, no need to query */ |
| 273 | goto cifs_create_set_dentry; |
| 274 | |
| 275 | case -ENOENT: |
| 276 | goto cifs_create_get_file_info; |
| 277 | |
| 278 | case -EIO: |
| 279 | case -EINVAL: |
| 280 | /* |
| 281 | * EIO could indicate that (posix open) operation is not |
| 282 | * supported, despite what server claimed in capability |
| 283 | * negotiation. |
| 284 | * |
| 285 | * POSIX open in samba versions 3.3.1 and earlier could |
| 286 | * incorrectly fail with invalid parameter. |
| 287 | */ |
| 288 | tcon->broken_posix_open = true; |
| 289 | break; |
| 290 | |
| 291 | case -EREMOTE: |
| 292 | case -EOPNOTSUPP: |
| 293 | /* |
| 294 | * EREMOTE indicates DFS junction, which is not handled |
| 295 | * in posix open. If either that or op not supported |
| 296 | * returned, follow the normal lookup. |
| 297 | */ |
| 298 | break; |
| 299 | |
| 300 | default: |
| 301 | goto out; |
| 302 | } |
| 303 | /* |
| 304 | * fallthrough to retry, using older open call, this is case |
| 305 | * where server does not support this SMB level, and falsely |
| 306 | * claims capability (also get here for DFS case which should be |
| 307 | * rare for path not covered on files) |
| 308 | */ |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 309 | } |
Steve French | f818dd5 | 2009-01-19 02:38:35 +0000 | [diff] [blame] | 310 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 311 | desired_access = 0; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 312 | if (OPEN_FMODE(oflags) & FMODE_READ) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 313 | desired_access |= GENERIC_READ; /* is this too little? */ |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 314 | if (OPEN_FMODE(oflags) & FMODE_WRITE) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 315 | desired_access |= GENERIC_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 317 | disposition = FILE_OVERWRITE_IF; |
| 318 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 319 | disposition = FILE_CREATE; |
| 320 | else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
| 321 | disposition = FILE_OVERWRITE_IF; |
| 322 | else if ((oflags & O_CREAT) == O_CREAT) |
| 323 | disposition = FILE_OPEN_IF; |
| 324 | else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 325 | cifs_dbg(FYI, "Create flag not set in create function\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 327 | /* |
| 328 | * BB add processing to set equivalent of mode - e.g. via CreateX with |
| 329 | * ACLs |
| 330 | */ |
| 331 | |
| 332 | if (!server->ops->open) { |
| 333 | rc = -ENOSYS; |
| 334 | goto out; |
| 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 337 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 338 | if (buf == NULL) { |
Jeff Layton | 232341b | 2010-08-05 13:58:38 -0400 | [diff] [blame] | 339 | rc = -ENOMEM; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 340 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 342 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 343 | /* |
| 344 | * if we're not using unix extensions, see if we need to set |
| 345 | * ATTR_READONLY on the create call |
| 346 | */ |
Steve French | f818dd5 | 2009-01-19 02:38:35 +0000 | [diff] [blame] | 347 | if (!tcon->unix_ext && (mode & S_IWUGO) == 0) |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 348 | create_options |= CREATE_OPTION_READONLY; |
| 349 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 350 | if (backup_cred(cifs_sb)) |
| 351 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 352 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 353 | oparms.tcon = tcon; |
| 354 | oparms.cifs_sb = cifs_sb; |
| 355 | oparms.desired_access = desired_access; |
| 356 | oparms.create_options = create_options; |
| 357 | oparms.disposition = disposition; |
| 358 | oparms.path = full_path; |
| 359 | oparms.fid = fid; |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 360 | oparms.reconnect = false; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 361 | |
| 362 | rc = server->ops->open(xid, &oparms, oplock, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 364 | cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 365 | goto out; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 368 | /* |
| 369 | * If Open reported that we actually created a file then we now have to |
| 370 | * set the mode if possible. |
| 371 | */ |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 372 | if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) { |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 373 | struct cifs_unix_set_info_args args = { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 374 | .mode = mode, |
| 375 | .ctime = NO_CHANGE_64, |
| 376 | .atime = NO_CHANGE_64, |
| 377 | .mtime = NO_CHANGE_64, |
| 378 | .device = 0, |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 379 | }; |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 380 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 381 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 382 | args.uid = current_fsuid(); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 383 | if (inode->i_mode & S_ISGID) |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 384 | args.gid = inode->i_gid; |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 385 | else |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 386 | args.gid = current_fsgid(); |
Steve French | 3ce53fc | 2007-06-08 14:55:14 +0000 | [diff] [blame] | 387 | } else { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 388 | args.uid = INVALID_UID; /* no change */ |
| 389 | args.gid = INVALID_GID; /* no change */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 391 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid, |
| 392 | current->tgid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 393 | } else { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 394 | /* |
| 395 | * BB implement mode setting via Windows security |
| 396 | * descriptors e.g. |
| 397 | */ |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 398 | /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 400 | /* Could set r/o dos attribute if mode & 0222 == 0 */ |
| 401 | } |
| 402 | |
| 403 | cifs_create_get_file_info: |
| 404 | /* server might mask mode so we have to query for it */ |
| 405 | if (tcon->unix_ext) |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 406 | rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb, |
| 407 | xid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 408 | else { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 409 | rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb, |
Steve French | 42eacf9 | 2014-02-10 14:08:16 -0600 | [diff] [blame] | 410 | xid, fid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 411 | if (newinode) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 412 | if (server->ops->set_lease_key) |
| 413 | server->ops->set_lease_key(newinode, fid); |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 414 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) |
| 415 | newinode->i_mode = mode; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 416 | if ((*oplock & CIFS_CREATE_ACTION) && |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 417 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) { |
| 418 | newinode->i_uid = current_fsuid(); |
| 419 | if (inode->i_mode & S_ISGID) |
| 420 | newinode->i_gid = inode->i_gid; |
| 421 | else |
| 422 | newinode->i_gid = current_fsgid(); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Steve French | c3b2a0c | 2009-02-20 04:32:45 +0000 | [diff] [blame] | 427 | cifs_create_set_dentry: |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 428 | if (rc != 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 429 | cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n", |
| 430 | rc); |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 431 | goto out_err; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 432 | } |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 433 | |
| 434 | if (S_ISDIR(newinode->i_mode)) { |
| 435 | rc = -EISDIR; |
| 436 | goto out_err; |
| 437 | } |
| 438 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 439 | d_drop(direntry); |
| 440 | d_add(direntry, newinode); |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 441 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 442 | out: |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 443 | kfree(buf); |
| 444 | kfree(full_path); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 445 | return rc; |
Sachin Prabhu | 8d9535b | 2016-07-07 21:28:27 +0100 | [diff] [blame] | 446 | |
| 447 | out_err: |
| 448 | if (server->ops->close) |
| 449 | server->ops->close(xid, tcon, fid); |
| 450 | if (newinode) |
| 451 | iput(newinode); |
| 452 | goto out; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 455 | int |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 456 | cifs_atomic_open(struct inode *inode, struct dentry *direntry, |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 457 | struct file *file, unsigned oflags, umode_t mode, |
Al Viro | 4723768 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 458 | int *opened) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 459 | { |
| 460 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 461 | unsigned int xid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 462 | struct tcon_link *tlink; |
| 463 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 464 | struct TCP_Server_Info *server; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 465 | struct cifs_fid fid; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 466 | struct cifs_pending_open open; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 467 | __u32 oplock; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 468 | struct cifsFileInfo *file_info; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 469 | |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 470 | /* |
| 471 | * 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] | 472 | * opens (rather than creates), because we do not know if it is a file |
| 473 | * or directory yet, and current Samba no longer allows us to do posix |
| 474 | * open on dirs, we could end up wasting an open call on what turns out |
| 475 | * to be a dir. For file opens, we wait to call posix open till |
| 476 | * cifs_open. It could be added to atomic_open in the future but the |
| 477 | * performance tradeoff of the extra network request when EISDIR or |
| 478 | * EACCES is returned would have to be weighed against the 50% reduction |
| 479 | * in network traffic in the other paths. |
| 480 | */ |
| 481 | if (!(oflags & O_CREAT)) { |
Sachin Prabhu | 3798f47 | 2012-11-05 11:39:32 +0000 | [diff] [blame] | 482 | struct dentry *res; |
| 483 | |
| 484 | /* |
| 485 | * Check for hashed negative dentry. We have already revalidated |
| 486 | * the dentry and it is fine. No need to perform another lookup. |
| 487 | */ |
Al Viro | 00699ad | 2016-07-05 09:44:53 -0400 | [diff] [blame] | 488 | if (!d_in_lookup(direntry)) |
Sachin Prabhu | 3798f47 | 2012-11-05 11:39:32 +0000 | [diff] [blame] | 489 | return -ENOENT; |
| 490 | |
| 491 | res = cifs_lookup(inode, direntry, 0); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 492 | if (IS_ERR(res)) |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 493 | return PTR_ERR(res); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 494 | |
Al Viro | e45198a | 2012-06-10 06:48:09 -0400 | [diff] [blame] | 495 | return finish_no_open(file, res); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 498 | xid = get_xid(); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 499 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 500 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 501 | inode, direntry, direntry); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 502 | |
| 503 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); |
Wei Yongjun | efb79f2 | 2013-04-04 14:16:21 +0800 | [diff] [blame] | 504 | if (IS_ERR(tlink)) { |
| 505 | rc = PTR_ERR(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 506 | goto out_free_xid; |
Wei Yongjun | efb79f2 | 2013-04-04 14:16:21 +0800 | [diff] [blame] | 507 | } |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 508 | |
| 509 | tcon = tlink_tcon(tlink); |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 510 | |
| 511 | rc = check_name(direntry, tcon); |
| 512 | if (rc) |
Ronnie Sahlberg | 5e8b028 | 2017-10-30 13:28:03 +1100 | [diff] [blame^] | 513 | goto out; |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 514 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 515 | server = tcon->ses->server; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 516 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 517 | if (server->ops->new_lease_key) |
| 518 | server->ops->new_lease_key(&fid); |
| 519 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 520 | cifs_add_pending_open(&fid, tlink, &open); |
| 521 | |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 522 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 523 | &oplock, &fid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 524 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 525 | if (rc) { |
| 526 | cifs_del_pending_open(&open); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 527 | goto out; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 528 | } |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 529 | |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 530 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 531 | *opened |= FILE_CREATED; |
| 532 | |
Al Viro | 30d9049 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 533 | rc = finish_open(file, direntry, generic_file_open, opened); |
| 534 | if (rc) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 535 | if (server->ops->close) |
| 536 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 537 | cifs_del_pending_open(&open); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 538 | goto out; |
| 539 | } |
| 540 | |
Namjae Jeon | 787aded | 2014-08-22 14:22:51 +0900 | [diff] [blame] | 541 | if (file->f_flags & O_DIRECT && |
| 542 | CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { |
| 543 | if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 544 | file->f_op = &cifs_file_direct_nobrl_ops; |
| 545 | else |
| 546 | file->f_op = &cifs_file_direct_ops; |
| 547 | } |
| 548 | |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 549 | file_info = cifs_new_fileinfo(&fid, file, tlink, oplock); |
| 550 | if (file_info == NULL) { |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 551 | if (server->ops->close) |
| 552 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 553 | cifs_del_pending_open(&open); |
Miklos Szeredi | dfb1d61 | 2013-09-16 14:51:59 +0200 | [diff] [blame] | 554 | fput(file); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 555 | rc = -ENOMEM; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | out: |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 559 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 560 | out_free_xid: |
| 561 | free_xid(xid); |
Al Viro | d958527 | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 562 | return rc; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 566 | bool excl) |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 567 | { |
| 568 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 569 | unsigned int xid = get_xid(); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 570 | /* |
| 571 | * BB below access is probably too much for mknod to request |
| 572 | * but we have to do query and setpathinfo so requesting |
| 573 | * less could fail (unless we want to request getatr and setatr |
| 574 | * permissions (only). At least for POSIX we do not have to |
| 575 | * request so much. |
| 576 | */ |
| 577 | unsigned oflags = O_EXCL | O_CREAT | O_RDWR; |
| 578 | struct tcon_link *tlink; |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 579 | struct cifs_tcon *tcon; |
| 580 | struct TCP_Server_Info *server; |
| 581 | struct cifs_fid fid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 582 | __u32 oplock; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 583 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 584 | cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 585 | inode, direntry, direntry); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 586 | |
| 587 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); |
| 588 | rc = PTR_ERR(tlink); |
| 589 | if (IS_ERR(tlink)) |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 590 | goto out_free_xid; |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 591 | |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 592 | tcon = tlink_tcon(tlink); |
| 593 | server = tcon->ses->server; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 594 | |
| 595 | if (server->ops->new_lease_key) |
| 596 | server->ops->new_lease_key(&fid); |
| 597 | |
| 598 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, |
Shirish Pargaonkar | f1e3268 | 2013-12-11 16:29:53 -0600 | [diff] [blame] | 599 | &oplock, &fid); |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 600 | if (!rc && server->ops->close) |
| 601 | server->ops->close(xid, tcon, &fid); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 602 | |
| 603 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 604 | out_free_xid: |
| 605 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | return rc; |
| 607 | } |
| 608 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 609 | int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode, |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 610 | dev_t device_number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
| 612 | int rc = -EPERM; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 613 | unsigned int xid; |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 614 | int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 616 | struct tcon_link *tlink; |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 617 | struct cifs_tcon *tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 618 | struct cifs_io_parms io_parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | char *full_path = NULL; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 620 | struct inode *newinode = NULL; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 621 | __u32 oplock = 0; |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 622 | struct cifs_fid fid; |
| 623 | struct cifs_open_parms oparms; |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 624 | FILE_ALL_INFO *buf = NULL; |
| 625 | unsigned int bytes_written; |
| 626 | struct win_dev *pdev; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 627 | struct kvec iov[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | if (!old_valid_dev(device_number)) |
| 630 | return -EINVAL; |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 633 | tlink = cifs_sb_tlink(cifs_sb); |
| 634 | if (IS_ERR(tlink)) |
| 635 | return PTR_ERR(tlink); |
| 636 | |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 637 | tcon = tlink_tcon(tlink); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 638 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 639 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | full_path = build_path_from_dentry(direntry); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 642 | if (full_path == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | rc = -ENOMEM; |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 644 | goto mknod_out; |
| 645 | } |
| 646 | |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 647 | if (tcon->unix_ext) { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 648 | struct cifs_unix_set_info_args args = { |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 649 | .mode = mode & ~current_umask(), |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 650 | .ctime = NO_CHANGE_64, |
| 651 | .atime = NO_CHANGE_64, |
| 652 | .mtime = NO_CHANGE_64, |
| 653 | .device = device_number, |
| 654 | }; |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 655 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 656 | args.uid = current_fsuid(); |
| 657 | args.gid = current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } else { |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 659 | args.uid = INVALID_UID; /* no change */ |
| 660 | args.gid = INVALID_GID; /* no change */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 662 | rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args, |
Jeff Layton | 01ea95e | 2009-07-09 20:02:49 -0400 | [diff] [blame] | 663 | cifs_sb->local_nls, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 664 | cifs_remap(cifs_sb)); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 665 | if (rc) |
| 666 | goto mknod_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 668 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 669 | inode->i_sb, xid); |
Steve French | d7245c2 | 2005-07-14 18:25:12 -0500 | [diff] [blame] | 670 | |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 671 | if (rc == 0) |
| 672 | d_instantiate(direntry, newinode); |
| 673 | goto mknod_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 676 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) |
| 677 | goto mknod_out; |
| 678 | |
| 679 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 680 | cifs_dbg(FYI, "sfu compat create special file\n"); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 681 | |
| 682 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 683 | if (buf == NULL) { |
| 684 | kfree(full_path); |
| 685 | rc = -ENOMEM; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 686 | free_xid(xid); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 687 | return rc; |
| 688 | } |
| 689 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 690 | if (backup_cred(cifs_sb)) |
| 691 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 692 | |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 693 | oparms.tcon = tcon; |
| 694 | oparms.cifs_sb = cifs_sb; |
| 695 | oparms.desired_access = GENERIC_WRITE; |
| 696 | oparms.create_options = create_options; |
| 697 | oparms.disposition = FILE_CREATE; |
| 698 | oparms.path = full_path; |
| 699 | oparms.fid = &fid; |
| 700 | oparms.reconnect = false; |
| 701 | |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 702 | if (tcon->ses->server->oplocks) |
| 703 | oplock = REQ_OPLOCK; |
| 704 | else |
| 705 | oplock = 0; |
| 706 | rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 707 | if (rc) |
| 708 | goto mknod_out; |
| 709 | |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 710 | /* |
| 711 | * BB Do not bother to decode buf since no local inode yet to put |
| 712 | * timestamps in, but we can reuse it safely. |
| 713 | */ |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 714 | |
| 715 | pdev = (struct win_dev *)buf; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 716 | io_parms.pid = current->tgid; |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 717 | io_parms.tcon = tcon; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 718 | io_parms.offset = 0; |
| 719 | io_parms.length = sizeof(struct win_dev); |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 720 | iov[1].iov_base = buf; |
| 721 | iov[1].iov_len = sizeof(struct win_dev); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 722 | if (S_ISCHR(mode)) { |
| 723 | memcpy(pdev->type, "IntxCHR", 8); |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 724 | pdev->major = cpu_to_le64(MAJOR(device_number)); |
| 725 | pdev->minor = cpu_to_le64(MINOR(device_number)); |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 726 | rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms, |
| 727 | &bytes_written, iov, 1); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 728 | } else if (S_ISBLK(mode)) { |
| 729 | memcpy(pdev->type, "IntxBLK", 8); |
Pavel Shilovsky | dd12067 | 2014-01-16 15:53:34 +0400 | [diff] [blame] | 730 | pdev->major = cpu_to_le64(MAJOR(device_number)); |
| 731 | pdev->minor = cpu_to_le64(MINOR(device_number)); |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 732 | rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms, |
| 733 | &bytes_written, iov, 1); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 734 | } /* else if (S_ISFIFO) */ |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 735 | tcon->ses->server->ops->close(xid, tcon, &fid); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 736 | d_drop(direntry); |
| 737 | |
| 738 | /* FIXME: add code here to set EAs */ |
| 739 | |
| 740 | mknod_out: |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 741 | kfree(full_path); |
Jeff Layton | 5d9ac7f | 2010-08-05 13:58:22 -0400 | [diff] [blame] | 742 | kfree(buf); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 743 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 744 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | return rc; |
| 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | struct dentry * |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 749 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 750 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 752 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ |
| 754 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 755 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 756 | struct cifs_tcon *pTcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | struct inode *newInode = NULL; |
| 758 | char *full_path = NULL; |
| 759 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 760 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 762 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
| 763 | parent_dir_inode, direntry, direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | /* check whether path exists */ |
| 766 | |
| 767 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 768 | tlink = cifs_sb_tlink(cifs_sb); |
| 769 | if (IS_ERR(tlink)) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 770 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 771 | return (struct dentry *)tlink; |
| 772 | } |
| 773 | pTcon = tlink_tcon(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
Ronnie Sahlberg | 5ed70f7 | 2017-08-23 14:48:14 +1000 | [diff] [blame] | 775 | rc = check_name(direntry, pTcon); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 776 | if (rc) |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 777 | goto lookup_out; |
Jeff Layton | 5ddf1e0 | 2009-07-05 11:01:02 -0400 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* can not grab the rename sem here since it would |
| 780 | deadlock in the cases (beginning of sys_rename itself) |
| 781 | in which we already have the sb rename sem */ |
| 782 | full_path = build_path_from_dentry(direntry); |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 783 | if (full_path == NULL) { |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 784 | rc = -ENOMEM; |
| 785 | goto lookup_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 788 | if (d_really_is_positive(direntry)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 789 | cifs_dbg(FYI, "non-NULL inode in lookup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 791 | cifs_dbg(FYI, "NULL inode in lookup\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 793 | cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 794 | full_path, d_inode(direntry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Steve French | a6ce493 | 2009-04-09 01:14:32 +0000 | [diff] [blame] | 796 | if (pTcon->unix_ext) { |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 797 | rc = cifs_get_inode_info_unix(&newInode, full_path, |
| 798 | parent_dir_inode->i_sb, xid); |
| 799 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | rc = cifs_get_inode_info(&newInode, full_path, NULL, |
Steve French | a6ce493 | 2009-04-09 01:14:32 +0000 | [diff] [blame] | 801 | parent_dir_inode->i_sb, xid, NULL); |
Miklos Szeredi | d2c1271 | 2012-06-05 15:10:23 +0200 | [diff] [blame] | 802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | if ((rc == 0) && (newInode != NULL)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | d_add(direntry, newInode); |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 806 | /* since paths are not looked up by component - the parent |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 807 | directories are presumed to be good here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | renew_parental_timestamps(direntry); |
| 809 | |
| 810 | } else if (rc == -ENOENT) { |
| 811 | rc = 0; |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 812 | cifs_set_time(direntry, jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | d_add(direntry, NULL); |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 814 | /* if it was once a directory (but how can we tell?) we could do |
| 815 | shrink_dcache_parent(direntry); */ |
Steve French | ed2b917 | 2008-01-20 00:30:29 +0000 | [diff] [blame] | 816 | } else if (rc != -EACCES) { |
Jeff Layton | a858215 | 2013-11-07 15:03:39 -0500 | [diff] [blame] | 817 | cifs_dbg(FYI, "Unexpected lookup error %d\n", rc); |
Steve French | ed2b917 | 2008-01-20 00:30:29 +0000 | [diff] [blame] | 818 | /* We special case check for Access Denied - since that |
| 819 | is a common return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 822 | lookup_out: |
Steve French | d14537f1 | 2005-04-28 22:41:05 -0700 | [diff] [blame] | 823 | kfree(full_path); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 824 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 825 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | return ERR_PTR(rc); |
| 827 | } |
| 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | static int |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 830 | cifs_d_revalidate(struct dentry *direntry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 832 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 833 | return -ECHILD; |
| 834 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 835 | if (d_really_is_positive(direntry)) { |
Jeff Layton | df2cf17 | 2010-02-12 07:44:16 -0500 | [diff] [blame] | 836 | if (cifs_revalidate_dentry(direntry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return 0; |
Gerlando Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 838 | else { |
| 839 | /* |
Ian Kent | 936ad90 | 2012-05-02 07:19:09 -0400 | [diff] [blame] | 840 | * If the inode wasn't known to be a dfs entry when |
| 841 | * the dentry was instantiated, such as when created |
| 842 | * via ->readdir(), it needs to be set now since the |
| 843 | * attributes will have been updated by |
| 844 | * cifs_revalidate_dentry(). |
Gerlando Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 845 | */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 846 | if (IS_AUTOMOUNT(d_inode(direntry)) && |
Ian Kent | 936ad90 | 2012-05-02 07:19:09 -0400 | [diff] [blame] | 847 | !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) { |
| 848 | spin_lock(&direntry->d_lock); |
| 849 | direntry->d_flags |= DCACHE_NEED_AUTOMOUNT; |
| 850 | spin_unlock(&direntry->d_lock); |
| 851 | } |
| 852 | |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 853 | return 1; |
Gerlando Falauto | ad4778f | 2011-10-18 10:58:50 +0200 | [diff] [blame] | 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 857 | /* |
| 858 | * This may be nfsd (or something), anyway, we can't see the |
| 859 | * intent of this. So, since this can be for creation, drop it. |
| 860 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 861 | if (!flags) |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 862 | return 0; |
| 863 | |
| 864 | /* |
| 865 | * Drop the negative dentry, in order to make sure to use the |
| 866 | * case sensitive name which is specified by user if this is |
| 867 | * for creation. |
| 868 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 869 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
Al Viro | 407938e | 2011-06-25 21:37:18 -0400 | [diff] [blame] | 870 | return 0; |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 871 | |
Miklos Szeredi | a00be0e | 2016-09-16 12:44:21 +0200 | [diff] [blame] | 872 | if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled) |
Nick Piggin | 262f86a | 2010-11-11 18:42:16 +1100 | [diff] [blame] | 873 | return 0; |
| 874 | |
| 875 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* static int cifs_d_delete(struct dentry *direntry) |
| 879 | { |
| 880 | int rc = 0; |
| 881 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 882 | cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
| 884 | return rc; |
| 885 | } */ |
| 886 | |
Al Viro | 4fd03e8 | 2009-02-20 05:57:07 +0000 | [diff] [blame] | 887 | const struct dentry_operations cifs_dentry_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | .d_revalidate = cifs_d_revalidate, |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 889 | .d_automount = cifs_dfs_d_automount, |
Steve French | 5fdae1f | 2007-06-05 18:30:44 +0000 | [diff] [blame] | 890 | /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | }; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 892 | |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 893 | static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q) |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 894 | { |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 895 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 896 | unsigned long hash; |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 897 | wchar_t c; |
| 898 | int i, charlen; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 899 | |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 900 | hash = init_name_hash(dentry); |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 901 | for (i = 0; i < q->len; i += charlen) { |
| 902 | charlen = codepage->char2uni(&q->name[i], q->len - i, &c); |
| 903 | /* error out if we can't convert the character */ |
| 904 | if (unlikely(charlen < 0)) |
| 905 | return charlen; |
| 906 | hash = partial_name_hash(cifs_toupper(c), hash); |
| 907 | } |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 908 | q->hash = end_name_hash(hash); |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 913 | static int cifs_ci_compare(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 914 | unsigned int len, const char *str, const struct qstr *name) |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 915 | { |
Al Viro | d3fe198 | 2016-07-29 18:23:59 -0400 | [diff] [blame] | 916 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 917 | wchar_t c1, c2; |
| 918 | int i, l1, l2; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 919 | |
Jeff Layton | ec71e0e | 2013-09-05 08:38:11 -0400 | [diff] [blame] | 920 | /* |
| 921 | * We make the assumption here that uppercase characters in the local |
| 922 | * codepage are always the same length as their lowercase counterparts. |
| 923 | * |
| 924 | * If that's ever not the case, then this will fail to match it. |
| 925 | */ |
| 926 | if (name->len != len) |
| 927 | return 1; |
| 928 | |
| 929 | for (i = 0; i < len; i += l1) { |
| 930 | /* Convert characters in both strings to UTF-16. */ |
| 931 | l1 = codepage->char2uni(&str[i], len - i, &c1); |
| 932 | l2 = codepage->char2uni(&name->name[i], name->len - i, &c2); |
| 933 | |
| 934 | /* |
| 935 | * If we can't convert either character, just declare it to |
| 936 | * be 1 byte long and compare the original byte. |
| 937 | */ |
| 938 | if (unlikely(l1 < 0 && l2 < 0)) { |
| 939 | if (str[i] != name->name[i]) |
| 940 | return 1; |
| 941 | l1 = 1; |
| 942 | continue; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Here, we again ass|u|me that upper/lowercase versions of |
| 947 | * a character are the same length in the local NLS. |
| 948 | */ |
| 949 | if (l1 != l2) |
| 950 | return 1; |
| 951 | |
| 952 | /* Now compare uppercase versions of these characters */ |
| 953 | if (cifs_toupper(c1) != cifs_toupper(c2)) |
| 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | return 0; |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Al Viro | 4fd03e8 | 2009-02-20 05:57:07 +0000 | [diff] [blame] | 960 | const struct dentry_operations cifs_ci_dentry_ops = { |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 961 | .d_revalidate = cifs_d_revalidate, |
| 962 | .d_hash = cifs_ci_hash, |
| 963 | .d_compare = cifs_ci_compare, |
David Howells | 01c64fe | 2011-01-14 18:45:47 +0000 | [diff] [blame] | 964 | .d_automount = cifs_dfs_d_automount, |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 965 | }; |