]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/dir.c | |
3 | * | |
4 | * vfs operations that deal with dentries | |
5fdae1f6 | 5 | * |
c3b2a0c6 | 6 | * Copyright (C) International Business Machines Corp., 2002,2009 |
1da177e4 LT |
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> | |
3bc303c2 | 27 | #include <linux/mount.h> |
6ca9f3ba | 28 | #include <linux/file.h> |
1da177e4 LT |
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" | |
ec71e0e1 | 35 | #include "cifs_unicode.h" |
3fa1c6d1 | 36 | #include "fs_context.h" |
1da177e4 | 37 | |
99ee4dbd | 38 | static void |
1da177e4 LT |
39 | renew_parental_timestamps(struct dentry *direntry) |
40 | { | |
5fdae1f6 SF |
41 | /* BB check if there is a way to get the kernel to do this or if we |
42 | really need this */ | |
1da177e4 | 43 | do { |
a00be0e3 | 44 | cifs_set_time(direntry, jiffies); |
1da177e4 | 45 | direntry = direntry->d_parent; |
5fdae1f6 | 46 | } while (!IS_ROOT(direntry)); |
1da177e4 LT |
47 | } |
48 | ||
6d3ea7e4 | 49 | char * |
3fa1c6d1 | 50 | cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
374402a2 | 51 | struct cifs_tcon *tcon, int add_treename) |
6d3ea7e4 | 52 | { |
3fa1c6d1 | 53 | int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0; |
6d3ea7e4 SF |
54 | int dfsplen; |
55 | char *full_path = NULL; | |
56 | ||
57 | /* if no prefix path, simply set path to the root of share to "" */ | |
58 | if (pplen == 0) { | |
59 | full_path = kzalloc(1, GFP_KERNEL); | |
60 | return full_path; | |
61 | } | |
62 | ||
374402a2 | 63 | if (add_treename) |
6d3ea7e4 SF |
64 | dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1); |
65 | else | |
66 | dfsplen = 0; | |
67 | ||
68 | full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); | |
69 | if (full_path == NULL) | |
70 | return full_path; | |
71 | ||
72 | if (dfsplen) | |
340625e6 | 73 | memcpy(full_path, tcon->treeName, dfsplen); |
839db3d1 | 74 | full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb); |
3fa1c6d1 | 75 | memcpy(full_path + dfsplen + 1, ctx->prepath, pplen); |
6d3ea7e4 | 76 | convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); |
6d3ea7e4 SF |
77 | return full_path; |
78 | } | |
79 | ||
1da177e4 LT |
80 | /* Note: caller must free return buffer */ |
81 | char * | |
82 | build_path_from_dentry(struct dentry *direntry) | |
268a635d AA |
83 | { |
84 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); | |
85 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); | |
86 | bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS; | |
87 | ||
88 | return build_path_from_dentry_optional_prefix(direntry, | |
89 | prefix); | |
90 | } | |
91 | ||
92 | char * | |
93 | build_path_from_dentry_optional_prefix(struct dentry *direntry, bool prefix) | |
1da177e4 LT |
94 | { |
95 | struct dentry *temp; | |
2fe87f02 | 96 | int namelen; |
646dd539 | 97 | int dfsplen; |
a6b5058f | 98 | int pplen = 0; |
1da177e4 | 99 | char *full_path; |
88274815 | 100 | char dirsep; |
0d424ad0 | 101 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); |
96daf2b0 | 102 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
dc137bf5 | 103 | unsigned seq; |
1da177e4 | 104 | |
646dd539 | 105 | dirsep = CIFS_DIR_SEP(cifs_sb); |
268a635d | 106 | if (prefix) |
0d424ad0 | 107 | dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1); |
646dd539 SF |
108 | else |
109 | dfsplen = 0; | |
a6b5058f AA |
110 | |
111 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) | |
112 | pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0; | |
113 | ||
1da177e4 | 114 | cifs_bp_rename_retry: |
a6b5058f | 115 | namelen = dfsplen + pplen; |
dc137bf5 AV |
116 | seq = read_seqbegin(&rename_lock); |
117 | rcu_read_lock(); | |
1da177e4 LT |
118 | for (temp = direntry; !IS_ROOT(temp);) { |
119 | namelen += (1 + temp->d_name.len); | |
120 | temp = temp->d_parent; | |
5fdae1f6 | 121 | if (temp == NULL) { |
f96637be | 122 | cifs_dbg(VFS, "corrupt dentry\n"); |
dc137bf5 | 123 | rcu_read_unlock(); |
1da177e4 LT |
124 | return NULL; |
125 | } | |
126 | } | |
dc137bf5 | 127 | rcu_read_unlock(); |
1da177e4 | 128 | |
8de9e86c | 129 | full_path = kmalloc(namelen+1, GFP_ATOMIC); |
5fdae1f6 | 130 | if (full_path == NULL) |
1da177e4 LT |
131 | return full_path; |
132 | full_path[namelen] = 0; /* trailing null */ | |
dc137bf5 | 133 | rcu_read_lock(); |
1da177e4 | 134 | for (temp = direntry; !IS_ROOT(temp);) { |
dc137bf5 | 135 | spin_lock(&temp->d_lock); |
1da177e4 LT |
136 | namelen -= 1 + temp->d_name.len; |
137 | if (namelen < 0) { | |
dc137bf5 | 138 | spin_unlock(&temp->d_lock); |
1da177e4 LT |
139 | break; |
140 | } else { | |
7f57356b | 141 | full_path[namelen] = dirsep; |
1da177e4 LT |
142 | strncpy(full_path + namelen + 1, temp->d_name.name, |
143 | temp->d_name.len); | |
f96637be | 144 | cifs_dbg(FYI, "name: %s\n", full_path + namelen); |
1da177e4 | 145 | } |
dc137bf5 | 146 | spin_unlock(&temp->d_lock); |
1da177e4 | 147 | temp = temp->d_parent; |
5fdae1f6 | 148 | if (temp == NULL) { |
f96637be | 149 | cifs_dbg(VFS, "corrupt dentry\n"); |
dc137bf5 | 150 | rcu_read_unlock(); |
1da177e4 LT |
151 | kfree(full_path); |
152 | return NULL; | |
153 | } | |
154 | } | |
dc137bf5 | 155 | rcu_read_unlock(); |
a6b5058f | 156 | if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) { |
f96637be JP |
157 | cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n", |
158 | namelen, dfsplen); | |
5fdae1f6 | 159 | /* presumably this is only possible if racing with a rename |
1da177e4 LT |
160 | of one of the parent directories (we can not lock the dentries |
161 | above us to prevent this, but retrying should be harmless) */ | |
162 | kfree(full_path); | |
1da177e4 LT |
163 | goto cifs_bp_rename_retry; |
164 | } | |
2fe87f02 SF |
165 | /* DIR_SEP already set for byte 0 / vs \ but not for |
166 | subsequent slashes in prepath which currently must | |
167 | be entered the right way - not sure if there is an alternative | |
168 | since the '\' is a valid posix character so we can not switch | |
169 | those safely to '/' if any are found in the middle of the prepath */ | |
170 | /* BB test paths to Windows with '/' in the midst of prepath */ | |
646dd539 | 171 | |
a6b5058f AA |
172 | if (pplen) { |
173 | int i; | |
174 | ||
175 | cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath); | |
176 | memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1); | |
c988de29 | 177 | full_path[dfsplen] = dirsep; |
a6b5058f AA |
178 | for (i = 0; i < pplen-1; i++) |
179 | if (full_path[dfsplen+1+i] == '/') | |
180 | full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb); | |
181 | } | |
182 | ||
646dd539 | 183 | if (dfsplen) { |
0d424ad0 | 184 | strncpy(full_path, tcon->treeName, dfsplen); |
646dd539 SF |
185 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
186 | int i; | |
187 | for (i = 0; i < dfsplen; i++) { | |
188 | if (full_path[i] == '\\') | |
189 | full_path[i] = '/'; | |
190 | } | |
191 | } | |
192 | } | |
1da177e4 LT |
193 | return full_path; |
194 | } | |
195 | ||
d2c12719 | 196 | /* |
d3edede2 | 197 | * Don't allow path components longer than the server max. |
d2c12719 MS |
198 | * Don't allow the separator character in a path component. |
199 | * The VFS will not allow "/", but "\" is allowed by posix. | |
200 | */ | |
201 | static int | |
d3edede2 | 202 | check_name(struct dentry *direntry, struct cifs_tcon *tcon) |
d2c12719 MS |
203 | { |
204 | struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); | |
205 | int i; | |
206 | ||
f74bc7c6 RS |
207 | if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength && |
208 | direntry->d_name.len > | |
6e3c1529 | 209 | le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength))) |
d3edede2 RS |
210 | return -ENAMETOOLONG; |
211 | ||
d2c12719 MS |
212 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { |
213 | for (i = 0; i < direntry->d_name.len; i++) { | |
214 | if (direntry->d_name.name[i] == '\\') { | |
f96637be | 215 | cifs_dbg(FYI, "Invalid file name\n"); |
d2c12719 MS |
216 | return -EINVAL; |
217 | } | |
218 | } | |
219 | } | |
220 | return 0; | |
221 | } | |
222 | ||
223 | ||
3979877e | 224 | /* Inode operations in similar order to how they appear in Linux file fs.h */ |
1da177e4 | 225 | |
6d5786a3 PS |
226 | static int |
227 | cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid, | |
228 | struct tcon_link *tlink, unsigned oflags, umode_t mode, | |
f1e32681 | 229 | __u32 *oplock, struct cifs_fid *fid) |
1da177e4 LT |
230 | { |
231 | int rc = -ENOENT; | |
67750fb9 | 232 | int create_options = CREATE_NOT_DIR; |
25364138 | 233 | int desired_access; |
d2c12719 MS |
234 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
235 | struct cifs_tcon *tcon = tlink_tcon(tlink); | |
1da177e4 | 236 | char *full_path = NULL; |
fb8c4b14 | 237 | FILE_ALL_INFO *buf = NULL; |
1da177e4 | 238 | struct inode *newinode = NULL; |
d2c12719 | 239 | int disposition; |
25364138 | 240 | struct TCP_Server_Info *server = tcon->ses->server; |
226730b4 | 241 | struct cifs_open_parms oparms; |
1da177e4 | 242 | |
d2c12719 | 243 | *oplock = 0; |
10b9b98e | 244 | if (tcon->ses->server->oplocks) |
d2c12719 | 245 | *oplock = REQ_OPLOCK; |
c3b2a0c6 | 246 | |
7ffec372 | 247 | full_path = build_path_from_dentry(direntry); |
598b6c57 ME |
248 | if (!full_path) |
249 | return -ENOMEM; | |
7ffec372 | 250 | |
29e20f9c | 251 | if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open && |
c3b2a0c6 SF |
252 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
253 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { | |
25364138 PS |
254 | rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode, |
255 | oflags, oplock, &fid->netfid, xid); | |
d2c12719 MS |
256 | switch (rc) { |
257 | case 0: | |
258 | if (newinode == NULL) { | |
259 | /* query inode info */ | |
90e4ee5d | 260 | goto cifs_create_get_file_info; |
d2c12719 MS |
261 | } |
262 | ||
8d9535b6 SP |
263 | if (S_ISDIR(newinode->i_mode)) { |
264 | CIFSSMBClose(xid, tcon, fid->netfid); | |
265 | iput(newinode); | |
266 | rc = -EISDIR; | |
267 | goto out; | |
268 | } | |
269 | ||
d2c12719 MS |
270 | if (!S_ISREG(newinode->i_mode)) { |
271 | /* | |
272 | * The server may allow us to open things like | |
273 | * FIFOs, but the client isn't set up to deal | |
274 | * with that. If it's not a regular file, just | |
275 | * close it and proceed as if it were a normal | |
276 | * lookup. | |
277 | */ | |
25364138 | 278 | CIFSSMBClose(xid, tcon, fid->netfid); |
d2c12719 MS |
279 | goto cifs_create_get_file_info; |
280 | } | |
281 | /* success, no need to query */ | |
282 | goto cifs_create_set_dentry; | |
283 | ||
284 | case -ENOENT: | |
285 | goto cifs_create_get_file_info; | |
286 | ||
287 | case -EIO: | |
288 | case -EINVAL: | |
289 | /* | |
290 | * EIO could indicate that (posix open) operation is not | |
291 | * supported, despite what server claimed in capability | |
292 | * negotiation. | |
293 | * | |
294 | * POSIX open in samba versions 3.3.1 and earlier could | |
295 | * incorrectly fail with invalid parameter. | |
296 | */ | |
297 | tcon->broken_posix_open = true; | |
298 | break; | |
299 | ||
300 | case -EREMOTE: | |
301 | case -EOPNOTSUPP: | |
302 | /* | |
303 | * EREMOTE indicates DFS junction, which is not handled | |
304 | * in posix open. If either that or op not supported | |
305 | * returned, follow the normal lookup. | |
306 | */ | |
307 | break; | |
e08fc045 | 308 | |
d2c12719 MS |
309 | default: |
310 | goto out; | |
311 | } | |
312 | /* | |
313 | * fallthrough to retry, using older open call, this is case | |
314 | * where server does not support this SMB level, and falsely | |
315 | * claims capability (also get here for DFS case which should be | |
316 | * rare for path not covered on files) | |
317 | */ | |
1da177e4 LT |
318 | } |
319 | ||
25364138 | 320 | desired_access = 0; |
d2c12719 | 321 | if (OPEN_FMODE(oflags) & FMODE_READ) |
25364138 | 322 | desired_access |= GENERIC_READ; /* is this too little? */ |
d2c12719 | 323 | if (OPEN_FMODE(oflags) & FMODE_WRITE) |
25364138 | 324 | desired_access |= GENERIC_WRITE; |
d2c12719 MS |
325 | |
326 | disposition = FILE_OVERWRITE_IF; | |
327 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) | |
328 | disposition = FILE_CREATE; | |
329 | else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) | |
330 | disposition = FILE_OVERWRITE_IF; | |
331 | else if ((oflags & O_CREAT) == O_CREAT) | |
332 | disposition = FILE_OPEN_IF; | |
333 | else | |
f96637be | 334 | cifs_dbg(FYI, "Create flag not set in create function\n"); |
d2c12719 | 335 | |
25364138 PS |
336 | /* |
337 | * BB add processing to set equivalent of mode - e.g. via CreateX with | |
338 | * ACLs | |
339 | */ | |
340 | ||
341 | if (!server->ops->open) { | |
342 | rc = -ENOSYS; | |
343 | goto out; | |
344 | } | |
1da177e4 | 345 | |
5fdae1f6 SF |
346 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
347 | if (buf == NULL) { | |
232341ba | 348 | rc = -ENOMEM; |
d2c12719 | 349 | goto out; |
1da177e4 | 350 | } |
67750fb9 | 351 | |
67750fb9 JL |
352 | /* |
353 | * if we're not using unix extensions, see if we need to set | |
354 | * ATTR_READONLY on the create call | |
355 | */ | |
f818dd55 | 356 | if (!tcon->unix_ext && (mode & S_IWUGO) == 0) |
67750fb9 JL |
357 | create_options |= CREATE_OPTION_READONLY; |
358 | ||
226730b4 PS |
359 | oparms.tcon = tcon; |
360 | oparms.cifs_sb = cifs_sb; | |
361 | oparms.desired_access = desired_access; | |
0f060936 | 362 | oparms.create_options = cifs_create_options(cifs_sb, create_options); |
226730b4 PS |
363 | oparms.disposition = disposition; |
364 | oparms.path = full_path; | |
365 | oparms.fid = fid; | |
9cbc0b73 | 366 | oparms.reconnect = false; |
ce558b0e | 367 | oparms.mode = mode; |
226730b4 | 368 | rc = server->ops->open(xid, &oparms, oplock, buf); |
1da177e4 | 369 | if (rc) { |
f96637be | 370 | cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc); |
d2c12719 | 371 | goto out; |
c3b2a0c6 SF |
372 | } |
373 | ||
25364138 PS |
374 | /* |
375 | * If Open reported that we actually created a file then we now have to | |
376 | * set the mode if possible. | |
377 | */ | |
d2c12719 | 378 | if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) { |
c3b2a0c6 | 379 | struct cifs_unix_set_info_args args = { |
4e1e7fb9 JL |
380 | .mode = mode, |
381 | .ctime = NO_CHANGE_64, | |
382 | .atime = NO_CHANGE_64, | |
383 | .mtime = NO_CHANGE_64, | |
384 | .device = 0, | |
c3b2a0c6 SF |
385 | }; |
386 | ||
387 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
49418b2c | 388 | args.uid = current_fsuid(); |
c3b2a0c6 | 389 | if (inode->i_mode & S_ISGID) |
49418b2c | 390 | args.gid = inode->i_gid; |
c3b2a0c6 | 391 | else |
49418b2c | 392 | args.gid = current_fsgid(); |
3ce53fc4 | 393 | } else { |
49418b2c EB |
394 | args.uid = INVALID_UID; /* no change */ |
395 | args.gid = INVALID_GID; /* no change */ | |
1da177e4 | 396 | } |
25364138 PS |
397 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid, |
398 | current->tgid); | |
c3b2a0c6 | 399 | } else { |
25364138 PS |
400 | /* |
401 | * BB implement mode setting via Windows security | |
402 | * descriptors e.g. | |
403 | */ | |
c3b2a0c6 SF |
404 | /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ |
405 | ||
406 | /* Could set r/o dos attribute if mode & 0222 == 0 */ | |
407 | } | |
1da177e4 | 408 | |
c3b2a0c6 SF |
409 | cifs_create_get_file_info: |
410 | /* server might mask mode so we have to query for it */ | |
411 | if (tcon->unix_ext) | |
25364138 PS |
412 | rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb, |
413 | xid); | |
c3b2a0c6 | 414 | else { |
d313852d | 415 | /* TODO: Add support for calling POSIX query info here, but passing in fid */ |
25364138 | 416 | rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb, |
42eacf9e | 417 | xid, fid); |
c3b2a0c6 | 418 | if (newinode) { |
b8c32dbb PS |
419 | if (server->ops->set_lease_key) |
420 | server->ops->set_lease_key(newinode, fid); | |
c3b2a0c6 SF |
421 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) |
422 | newinode->i_mode = mode; | |
d2c12719 | 423 | if ((*oplock & CIFS_CREATE_ACTION) && |
c3b2a0c6 SF |
424 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) { |
425 | newinode->i_uid = current_fsuid(); | |
426 | if (inode->i_mode & S_ISGID) | |
427 | newinode->i_gid = inode->i_gid; | |
428 | else | |
429 | newinode->i_gid = current_fsgid(); | |
6473a559 | 430 | } |
1da177e4 | 431 | } |
c3b2a0c6 | 432 | } |
1da177e4 | 433 | |
c3b2a0c6 | 434 | cifs_create_set_dentry: |
d2c12719 | 435 | if (rc != 0) { |
f96637be JP |
436 | cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n", |
437 | rc); | |
8d9535b6 | 438 | goto out_err; |
d2c12719 | 439 | } |
8d9535b6 SP |
440 | |
441 | if (S_ISDIR(newinode->i_mode)) { | |
442 | rc = -EISDIR; | |
443 | goto out_err; | |
d2c12719 | 444 | } |
8d9535b6 | 445 | |
d2c12719 MS |
446 | d_drop(direntry); |
447 | d_add(direntry, newinode); | |
c3b2a0c6 | 448 | |
d2c12719 MS |
449 | out: |
450 | kfree(buf); | |
451 | kfree(full_path); | |
452 | return rc; | |
8d9535b6 SP |
453 | |
454 | out_err: | |
455 | if (server->ops->close) | |
456 | server->ops->close(xid, tcon, fid); | |
457 | if (newinode) | |
458 | iput(newinode); | |
459 | goto out; | |
d2c12719 | 460 | } |
6ca9f3ba | 461 | |
d9585277 | 462 | int |
d2c12719 | 463 | cifs_atomic_open(struct inode *inode, struct dentry *direntry, |
44907d79 | 464 | struct file *file, unsigned oflags, umode_t mode) |
d2c12719 MS |
465 | { |
466 | int rc; | |
6d5786a3 | 467 | unsigned int xid; |
d2c12719 MS |
468 | struct tcon_link *tlink; |
469 | struct cifs_tcon *tcon; | |
25364138 | 470 | struct TCP_Server_Info *server; |
fb1214e4 | 471 | struct cifs_fid fid; |
233839b1 | 472 | struct cifs_pending_open open; |
d2c12719 | 473 | __u32 oplock; |
fb1214e4 | 474 | struct cifsFileInfo *file_info; |
d2c12719 | 475 | |
fb1214e4 PS |
476 | /* |
477 | * Posix open is only called (at lookup time) for file create now. For | |
d2c12719 MS |
478 | * opens (rather than creates), because we do not know if it is a file |
479 | * or directory yet, and current Samba no longer allows us to do posix | |
480 | * open on dirs, we could end up wasting an open call on what turns out | |
481 | * to be a dir. For file opens, we wait to call posix open till | |
482 | * cifs_open. It could be added to atomic_open in the future but the | |
483 | * performance tradeoff of the extra network request when EISDIR or | |
484 | * EACCES is returned would have to be weighed against the 50% reduction | |
485 | * in network traffic in the other paths. | |
486 | */ | |
487 | if (!(oflags & O_CREAT)) { | |
3798f47a SP |
488 | struct dentry *res; |
489 | ||
490 | /* | |
491 | * Check for hashed negative dentry. We have already revalidated | |
492 | * the dentry and it is fine. No need to perform another lookup. | |
493 | */ | |
00699ad8 | 494 | if (!d_in_lookup(direntry)) |
3798f47a SP |
495 | return -ENOENT; |
496 | ||
497 | res = cifs_lookup(inode, direntry, 0); | |
d2c12719 | 498 | if (IS_ERR(res)) |
d9585277 | 499 | return PTR_ERR(res); |
d2c12719 | 500 | |
e45198a6 | 501 | return finish_no_open(file, res); |
d2c12719 MS |
502 | } |
503 | ||
6d5786a3 | 504 | xid = get_xid(); |
d2c12719 | 505 | |
35c265e0 AV |
506 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
507 | inode, direntry, direntry); | |
d2c12719 MS |
508 | |
509 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); | |
efb79f28 WY |
510 | if (IS_ERR(tlink)) { |
511 | rc = PTR_ERR(tlink); | |
6d5786a3 | 512 | goto out_free_xid; |
efb79f28 | 513 | } |
d2c12719 MS |
514 | |
515 | tcon = tlink_tcon(tlink); | |
d3edede2 RS |
516 | |
517 | rc = check_name(direntry, tcon); | |
518 | if (rc) | |
f74bc7c6 | 519 | goto out; |
d3edede2 | 520 | |
25364138 | 521 | server = tcon->ses->server; |
d2c12719 | 522 | |
b8c32dbb PS |
523 | if (server->ops->new_lease_key) |
524 | server->ops->new_lease_key(&fid); | |
525 | ||
233839b1 PS |
526 | cifs_add_pending_open(&fid, tlink, &open); |
527 | ||
d2c12719 | 528 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, |
f1e32681 | 529 | &oplock, &fid); |
d2c12719 | 530 | |
233839b1 PS |
531 | if (rc) { |
532 | cifs_del_pending_open(&open); | |
d2c12719 | 533 | goto out; |
233839b1 | 534 | } |
d2c12719 | 535 | |
f1e32681 | 536 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
73a09dd9 | 537 | file->f_mode |= FMODE_CREATED; |
f1e32681 | 538 | |
be12af3e | 539 | rc = finish_open(file, direntry, generic_file_open); |
30d90494 | 540 | if (rc) { |
25364138 PS |
541 | if (server->ops->close) |
542 | server->ops->close(xid, tcon, &fid); | |
233839b1 | 543 | cifs_del_pending_open(&open); |
d2c12719 | 544 | goto out; |
5fdae1f6 | 545 | } |
2422f676 | 546 | |
787aded6 NJ |
547 | if (file->f_flags & O_DIRECT && |
548 | CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { | |
549 | if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) | |
550 | file->f_op = &cifs_file_direct_nobrl_ops; | |
551 | else | |
552 | file->f_op = &cifs_file_direct_ops; | |
553 | } | |
554 | ||
fb1214e4 PS |
555 | file_info = cifs_new_fileinfo(&fid, file, tlink, oplock); |
556 | if (file_info == NULL) { | |
25364138 PS |
557 | if (server->ops->close) |
558 | server->ops->close(xid, tcon, &fid); | |
233839b1 | 559 | cifs_del_pending_open(&open); |
d9585277 | 560 | rc = -ENOMEM; |
d2c12719 MS |
561 | } |
562 | ||
563 | out: | |
564 | cifs_put_tlink(tlink); | |
6d5786a3 PS |
565 | out_free_xid: |
566 | free_xid(xid); | |
d9585277 | 567 | return rc; |
d2c12719 MS |
568 | } |
569 | ||
549c7297 CB |
570 | int cifs_create(struct user_namespace *mnt_userns, struct inode *inode, |
571 | struct dentry *direntry, umode_t mode, bool excl) | |
d2c12719 MS |
572 | { |
573 | int rc; | |
6d5786a3 | 574 | unsigned int xid = get_xid(); |
d2c12719 MS |
575 | /* |
576 | * BB below access is probably too much for mknod to request | |
577 | * but we have to do query and setpathinfo so requesting | |
578 | * less could fail (unless we want to request getatr and setatr | |
579 | * permissions (only). At least for POSIX we do not have to | |
580 | * request so much. | |
581 | */ | |
582 | unsigned oflags = O_EXCL | O_CREAT | O_RDWR; | |
583 | struct tcon_link *tlink; | |
25364138 PS |
584 | struct cifs_tcon *tcon; |
585 | struct TCP_Server_Info *server; | |
586 | struct cifs_fid fid; | |
d2c12719 | 587 | __u32 oplock; |
d2c12719 | 588 | |
35c265e0 AV |
589 | cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
590 | inode, direntry, direntry); | |
d2c12719 MS |
591 | |
592 | tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb)); | |
593 | rc = PTR_ERR(tlink); | |
594 | if (IS_ERR(tlink)) | |
6d5786a3 | 595 | goto out_free_xid; |
d2c12719 | 596 | |
25364138 PS |
597 | tcon = tlink_tcon(tlink); |
598 | server = tcon->ses->server; | |
b8c32dbb PS |
599 | |
600 | if (server->ops->new_lease_key) | |
601 | server->ops->new_lease_key(&fid); | |
602 | ||
603 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, | |
f1e32681 | 604 | &oplock, &fid); |
25364138 PS |
605 | if (!rc && server->ops->close) |
606 | server->ops->close(xid, tcon, &fid); | |
d2c12719 | 607 | |
7ffec372 | 608 | cifs_put_tlink(tlink); |
6d5786a3 PS |
609 | out_free_xid: |
610 | free_xid(xid); | |
1da177e4 LT |
611 | return rc; |
612 | } | |
613 | ||
549c7297 CB |
614 | int cifs_mknod(struct user_namespace *mnt_userns, struct inode *inode, |
615 | struct dentry *direntry, umode_t mode, dev_t device_number) | |
1da177e4 LT |
616 | { |
617 | int rc = -EPERM; | |
6d5786a3 | 618 | unsigned int xid; |
1da177e4 | 619 | struct cifs_sb_info *cifs_sb; |
7ffec372 | 620 | struct tcon_link *tlink; |
dd120671 | 621 | struct cifs_tcon *tcon; |
1da177e4 | 622 | char *full_path = NULL; |
1da177e4 LT |
623 | |
624 | if (!old_valid_dev(device_number)) | |
625 | return -EINVAL; | |
626 | ||
1da177e4 | 627 | cifs_sb = CIFS_SB(inode->i_sb); |
7ffec372 JL |
628 | tlink = cifs_sb_tlink(cifs_sb); |
629 | if (IS_ERR(tlink)) | |
630 | return PTR_ERR(tlink); | |
631 | ||
dd120671 | 632 | tcon = tlink_tcon(tlink); |
7ffec372 | 633 | |
6d5786a3 | 634 | xid = get_xid(); |
1da177e4 | 635 | |
1da177e4 | 636 | full_path = build_path_from_dentry(direntry); |
5d9ac7fd | 637 | if (full_path == NULL) { |
1da177e4 | 638 | rc = -ENOMEM; |
5d9ac7fd JL |
639 | goto mknod_out; |
640 | } | |
641 | ||
c847dccf AA |
642 | rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon, |
643 | full_path, mode, | |
644 | device_number); | |
5d9ac7fd JL |
645 | |
646 | mknod_out: | |
d14537f1 | 647 | kfree(full_path); |
6d5786a3 | 648 | free_xid(xid); |
7ffec372 | 649 | cifs_put_tlink(tlink); |
1da177e4 LT |
650 | return rc; |
651 | } | |
652 | ||
1da177e4 | 653 | struct dentry * |
5fdae1f6 | 654 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, |
00cd8dd3 | 655 | unsigned int flags) |
1da177e4 | 656 | { |
6d5786a3 | 657 | unsigned int xid; |
1da177e4 LT |
658 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ |
659 | struct cifs_sb_info *cifs_sb; | |
7ffec372 | 660 | struct tcon_link *tlink; |
96daf2b0 | 661 | struct cifs_tcon *pTcon; |
1da177e4 LT |
662 | struct inode *newInode = NULL; |
663 | char *full_path = NULL; | |
664 | ||
6d5786a3 | 665 | xid = get_xid(); |
1da177e4 | 666 | |
35c265e0 AV |
667 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n", |
668 | parent_dir_inode, direntry, direntry); | |
1da177e4 | 669 | |
1da177e4 LT |
670 | /* check whether path exists */ |
671 | ||
672 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); | |
7ffec372 JL |
673 | tlink = cifs_sb_tlink(cifs_sb); |
674 | if (IS_ERR(tlink)) { | |
6d5786a3 | 675 | free_xid(xid); |
11f17c9b | 676 | return ERR_CAST(tlink); |
7ffec372 JL |
677 | } |
678 | pTcon = tlink_tcon(tlink); | |
1da177e4 | 679 | |
d3edede2 | 680 | rc = check_name(direntry, pTcon); |
11f17c9b AV |
681 | if (unlikely(rc)) { |
682 | cifs_put_tlink(tlink); | |
683 | free_xid(xid); | |
684 | return ERR_PTR(rc); | |
685 | } | |
5ddf1e0f | 686 | |
1da177e4 LT |
687 | /* can not grab the rename sem here since it would |
688 | deadlock in the cases (beginning of sys_rename itself) | |
689 | in which we already have the sb rename sem */ | |
690 | full_path = build_path_from_dentry(direntry); | |
5fdae1f6 | 691 | if (full_path == NULL) { |
11f17c9b AV |
692 | cifs_put_tlink(tlink); |
693 | free_xid(xid); | |
694 | return ERR_PTR(-ENOMEM); | |
1da177e4 LT |
695 | } |
696 | ||
2b0143b5 | 697 | if (d_really_is_positive(direntry)) { |
f96637be | 698 | cifs_dbg(FYI, "non-NULL inode in lookup\n"); |
1da177e4 | 699 | } else { |
f96637be | 700 | cifs_dbg(FYI, "NULL inode in lookup\n"); |
1da177e4 | 701 | } |
f96637be | 702 | cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", |
2b0143b5 | 703 | full_path, d_inode(direntry)); |
1da177e4 | 704 | |
790434ff SF |
705 | if (pTcon->posix_extensions) |
706 | rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid); | |
707 | else if (pTcon->unix_ext) { | |
d2c12719 MS |
708 | rc = cifs_get_inode_info_unix(&newInode, full_path, |
709 | parent_dir_inode->i_sb, xid); | |
710 | } else { | |
1da177e4 | 711 | rc = cifs_get_inode_info(&newInode, full_path, NULL, |
a6ce4932 | 712 | parent_dir_inode->i_sb, xid, NULL); |
d2c12719 | 713 | } |
1da177e4 | 714 | |
a8b75f66 | 715 | if (rc == 0) { |
5fdae1f6 | 716 | /* since paths are not looked up by component - the parent |
3abb9272 | 717 | directories are presumed to be good here */ |
1da177e4 | 718 | renew_parental_timestamps(direntry); |
1da177e4 | 719 | } else if (rc == -ENOENT) { |
a00be0e3 | 720 | cifs_set_time(direntry, jiffies); |
11f17c9b AV |
721 | newInode = NULL; |
722 | } else { | |
723 | if (rc != -EACCES) { | |
724 | cifs_dbg(FYI, "Unexpected lookup error %d\n", rc); | |
725 | /* We special case check for Access Denied - since that | |
726 | is a common return code */ | |
727 | } | |
728 | newInode = ERR_PTR(rc); | |
1da177e4 | 729 | } |
d14537f1 | 730 | kfree(full_path); |
7ffec372 | 731 | cifs_put_tlink(tlink); |
6d5786a3 | 732 | free_xid(xid); |
11f17c9b | 733 | return d_splice_alias(newInode, direntry); |
1da177e4 LT |
734 | } |
735 | ||
1da177e4 | 736 | static int |
0b728e19 | 737 | cifs_d_revalidate(struct dentry *direntry, unsigned int flags) |
1da177e4 | 738 | { |
0b3d0ef9 PS |
739 | struct inode *inode; |
740 | ||
0b728e19 | 741 | if (flags & LOOKUP_RCU) |
34286d66 NP |
742 | return -ECHILD; |
743 | ||
2b0143b5 | 744 | if (d_really_is_positive(direntry)) { |
0b3d0ef9 PS |
745 | inode = d_inode(direntry); |
746 | if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode))) | |
747 | CIFS_I(inode)->time = 0; /* force reval */ | |
748 | ||
df2cf170 | 749 | if (cifs_revalidate_dentry(direntry)) |
1da177e4 | 750 | return 0; |
ad4778fb GF |
751 | else { |
752 | /* | |
936ad909 IK |
753 | * If the inode wasn't known to be a dfs entry when |
754 | * the dentry was instantiated, such as when created | |
755 | * via ->readdir(), it needs to be set now since the | |
756 | * attributes will have been updated by | |
757 | * cifs_revalidate_dentry(). | |
ad4778fb | 758 | */ |
0b3d0ef9 | 759 | if (IS_AUTOMOUNT(inode) && |
936ad909 IK |
760 | !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) { |
761 | spin_lock(&direntry->d_lock); | |
762 | direntry->d_flags |= DCACHE_NEED_AUTOMOUNT; | |
763 | spin_unlock(&direntry->d_lock); | |
764 | } | |
765 | ||
262f86ad | 766 | return 1; |
ad4778fb | 767 | } |
1da177e4 LT |
768 | } |
769 | ||
262f86ad NP |
770 | /* |
771 | * This may be nfsd (or something), anyway, we can't see the | |
772 | * intent of this. So, since this can be for creation, drop it. | |
773 | */ | |
0b728e19 | 774 | if (!flags) |
262f86ad NP |
775 | return 0; |
776 | ||
777 | /* | |
778 | * Drop the negative dentry, in order to make sure to use the | |
779 | * case sensitive name which is specified by user if this is | |
780 | * for creation. | |
781 | */ | |
0b728e19 | 782 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
407938e7 | 783 | return 0; |
262f86ad | 784 | |
a00be0e3 | 785 | if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled) |
262f86ad NP |
786 | return 0; |
787 | ||
788 | return 1; | |
1da177e4 LT |
789 | } |
790 | ||
791 | /* static int cifs_d_delete(struct dentry *direntry) | |
792 | { | |
793 | int rc = 0; | |
794 | ||
35c265e0 | 795 | cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry); |
1da177e4 LT |
796 | |
797 | return rc; | |
798 | } */ | |
799 | ||
4fd03e84 | 800 | const struct dentry_operations cifs_dentry_ops = { |
1da177e4 | 801 | .d_revalidate = cifs_d_revalidate, |
01c64fea | 802 | .d_automount = cifs_dfs_d_automount, |
5fdae1f6 | 803 | /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ |
1da177e4 | 804 | }; |
b92327fe | 805 | |
da53be12 | 806 | static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q) |
b92327fe | 807 | { |
b1e6a015 | 808 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
b92327fe | 809 | unsigned long hash; |
ec71e0e1 JL |
810 | wchar_t c; |
811 | int i, charlen; | |
b92327fe | 812 | |
8387ff25 | 813 | hash = init_name_hash(dentry); |
ec71e0e1 JL |
814 | for (i = 0; i < q->len; i += charlen) { |
815 | charlen = codepage->char2uni(&q->name[i], q->len - i, &c); | |
816 | /* error out if we can't convert the character */ | |
817 | if (unlikely(charlen < 0)) | |
818 | return charlen; | |
819 | hash = partial_name_hash(cifs_toupper(c), hash); | |
820 | } | |
b92327fe SF |
821 | q->hash = end_name_hash(hash); |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
6fa67e70 | 826 | static int cifs_ci_compare(const struct dentry *dentry, |
621e155a | 827 | unsigned int len, const char *str, const struct qstr *name) |
b92327fe | 828 | { |
d3fe1985 | 829 | struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; |
ec71e0e1 JL |
830 | wchar_t c1, c2; |
831 | int i, l1, l2; | |
b92327fe | 832 | |
ec71e0e1 JL |
833 | /* |
834 | * We make the assumption here that uppercase characters in the local | |
835 | * codepage are always the same length as their lowercase counterparts. | |
836 | * | |
837 | * If that's ever not the case, then this will fail to match it. | |
838 | */ | |
839 | if (name->len != len) | |
840 | return 1; | |
841 | ||
842 | for (i = 0; i < len; i += l1) { | |
843 | /* Convert characters in both strings to UTF-16. */ | |
844 | l1 = codepage->char2uni(&str[i], len - i, &c1); | |
845 | l2 = codepage->char2uni(&name->name[i], name->len - i, &c2); | |
846 | ||
847 | /* | |
848 | * If we can't convert either character, just declare it to | |
849 | * be 1 byte long and compare the original byte. | |
850 | */ | |
851 | if (unlikely(l1 < 0 && l2 < 0)) { | |
852 | if (str[i] != name->name[i]) | |
853 | return 1; | |
854 | l1 = 1; | |
855 | continue; | |
856 | } | |
857 | ||
858 | /* | |
859 | * Here, we again ass|u|me that upper/lowercase versions of | |
860 | * a character are the same length in the local NLS. | |
861 | */ | |
862 | if (l1 != l2) | |
863 | return 1; | |
864 | ||
865 | /* Now compare uppercase versions of these characters */ | |
866 | if (cifs_toupper(c1) != cifs_toupper(c2)) | |
867 | return 1; | |
868 | } | |
869 | ||
870 | return 0; | |
b92327fe SF |
871 | } |
872 | ||
4fd03e84 | 873 | const struct dentry_operations cifs_ci_dentry_ops = { |
b92327fe SF |
874 | .d_revalidate = cifs_d_revalidate, |
875 | .d_hash = cifs_ci_hash, | |
876 | .d_compare = cifs_ci_compare, | |
01c64fea | 877 | .d_automount = cifs_dfs_d_automount, |
b92327fe | 878 | }; |