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