]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/dir.c | |
3 | * | |
4 | * vfs operations that deal with dentries | |
5 | * | |
6 | * Copyright (C) International Business Machines Corp., 2002,2003 | |
7 | * Author(s): Steve French ([email protected]) | |
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> | |
27 | #include "cifsfs.h" | |
28 | #include "cifspdu.h" | |
29 | #include "cifsglob.h" | |
30 | #include "cifsproto.h" | |
31 | #include "cifs_debug.h" | |
32 | #include "cifs_fs_sb.h" | |
33 | ||
34 | void | |
35 | renew_parental_timestamps(struct dentry *direntry) | |
36 | { | |
37 | /* BB check if there is a way to get the kernel to do this or if we really need this */ | |
38 | do { | |
39 | direntry->d_time = jiffies; | |
40 | direntry = direntry->d_parent; | |
41 | } while (!IS_ROOT(direntry)); | |
42 | } | |
43 | ||
44 | /* Note: caller must free return buffer */ | |
45 | char * | |
46 | build_path_from_dentry(struct dentry *direntry) | |
47 | { | |
48 | struct dentry *temp; | |
49 | int namelen = 0; | |
50 | char *full_path; | |
51 | ||
52 | if(direntry == NULL) | |
53 | return NULL; /* not much we can do if dentry is freed and | |
54 | we need to reopen the file after it was closed implicitly | |
55 | when the server crashed */ | |
56 | ||
57 | cifs_bp_rename_retry: | |
58 | for (temp = direntry; !IS_ROOT(temp);) { | |
59 | namelen += (1 + temp->d_name.len); | |
60 | temp = temp->d_parent; | |
61 | if(temp == NULL) { | |
62 | cERROR(1,("corrupt dentry")); | |
63 | return NULL; | |
64 | } | |
65 | } | |
66 | ||
67 | full_path = kmalloc(namelen+1, GFP_KERNEL); | |
68 | if(full_path == NULL) | |
69 | return full_path; | |
70 | full_path[namelen] = 0; /* trailing null */ | |
71 | ||
72 | for (temp = direntry; !IS_ROOT(temp);) { | |
73 | namelen -= 1 + temp->d_name.len; | |
74 | if (namelen < 0) { | |
75 | break; | |
76 | } else { | |
77 | full_path[namelen] = '\\'; | |
78 | strncpy(full_path + namelen + 1, temp->d_name.name, | |
79 | temp->d_name.len); | |
80 | cFYI(0, (" name: %s ", full_path + namelen)); | |
81 | } | |
82 | temp = temp->d_parent; | |
83 | if(temp == NULL) { | |
84 | cERROR(1,("corrupt dentry")); | |
85 | kfree(full_path); | |
86 | return NULL; | |
87 | } | |
88 | } | |
89 | if (namelen != 0) { | |
90 | cERROR(1, | |
91 | ("We did not end path lookup where we expected namelen is %d", | |
92 | namelen)); | |
93 | /* presumably this is only possible if we were racing with a rename | |
94 | of one of the parent directories (we can not lock the dentries | |
95 | above us to prevent this, but retrying should be harmless) */ | |
96 | kfree(full_path); | |
97 | namelen = 0; | |
98 | goto cifs_bp_rename_retry; | |
99 | } | |
100 | ||
101 | return full_path; | |
102 | } | |
103 | ||
104 | /* Note: caller must free return buffer */ | |
105 | char * | |
106 | build_wildcard_path_from_dentry(struct dentry *direntry) | |
107 | { | |
108 | struct dentry *temp; | |
109 | int namelen = 0; | |
110 | char *full_path; | |
111 | ||
112 | if(direntry == NULL) | |
113 | return NULL; /* not much we can do if dentry is freed and | |
114 | we need to reopen the file after it was closed implicitly | |
115 | when the server crashed */ | |
116 | ||
117 | cifs_bwp_rename_retry: | |
118 | for (temp = direntry; !IS_ROOT(temp);) { | |
119 | namelen += (1 + temp->d_name.len); | |
120 | temp = temp->d_parent; | |
121 | if(temp == NULL) { | |
122 | cERROR(1,("corrupt dentry")); | |
123 | return NULL; | |
124 | } | |
125 | } | |
126 | ||
127 | full_path = kmalloc(namelen+3, GFP_KERNEL); | |
128 | if(full_path == NULL) | |
129 | return full_path; | |
130 | ||
131 | full_path[namelen] = '\\'; | |
132 | full_path[namelen+1] = '*'; | |
133 | full_path[namelen+2] = 0; /* trailing null */ | |
134 | ||
135 | for (temp = direntry; !IS_ROOT(temp);) { | |
136 | namelen -= 1 + temp->d_name.len; | |
137 | if (namelen < 0) { | |
138 | break; | |
139 | } else { | |
140 | full_path[namelen] = '\\'; | |
141 | strncpy(full_path + namelen + 1, temp->d_name.name, | |
142 | temp->d_name.len); | |
143 | cFYI(0, (" name: %s ", full_path + namelen)); | |
144 | } | |
145 | temp = temp->d_parent; | |
146 | if(temp == NULL) { | |
147 | cERROR(1,("corrupt dentry")); | |
148 | kfree(full_path); | |
149 | return NULL; | |
150 | } | |
151 | } | |
152 | if (namelen != 0) { | |
153 | cERROR(1, | |
154 | ("We did not end path lookup where we expected namelen is %d", | |
155 | namelen)); | |
156 | /* presumably this is only possible if we were racing with a rename | |
157 | of one of the parent directories (we can not lock the dentries | |
158 | above us to prevent this, but retrying should be harmless) */ | |
159 | kfree(full_path); | |
160 | namelen = 0; | |
161 | goto cifs_bwp_rename_retry; | |
162 | } | |
163 | ||
164 | return full_path; | |
165 | } | |
166 | ||
167 | /* Inode operations in similar order to how they appear in the Linux file fs.h */ | |
168 | ||
169 | int | |
170 | cifs_create(struct inode *inode, struct dentry *direntry, int mode, | |
171 | struct nameidata *nd) | |
172 | { | |
173 | int rc = -ENOENT; | |
174 | int xid; | |
175 | int oplock = 0; | |
176 | int desiredAccess = GENERIC_READ | GENERIC_WRITE; | |
177 | __u16 fileHandle; | |
178 | struct cifs_sb_info *cifs_sb; | |
179 | struct cifsTconInfo *pTcon; | |
180 | char *full_path = NULL; | |
181 | FILE_ALL_INFO * buf = NULL; | |
182 | struct inode *newinode = NULL; | |
183 | struct cifsFileInfo * pCifsFile = NULL; | |
184 | struct cifsInodeInfo * pCifsInode; | |
185 | int disposition = FILE_OVERWRITE_IF; | |
186 | int write_only = FALSE; | |
187 | ||
188 | xid = GetXid(); | |
189 | ||
190 | cifs_sb = CIFS_SB(inode->i_sb); | |
191 | pTcon = cifs_sb->tcon; | |
192 | ||
193 | down(&direntry->d_sb->s_vfs_rename_sem); | |
194 | full_path = build_path_from_dentry(direntry); | |
195 | up(&direntry->d_sb->s_vfs_rename_sem); | |
196 | if(full_path == NULL) { | |
197 | FreeXid(xid); | |
198 | return -ENOMEM; | |
199 | } | |
200 | ||
201 | if(nd) { | |
202 | if ((nd->intent.open.flags & O_ACCMODE) == O_RDONLY) | |
203 | desiredAccess = GENERIC_READ; | |
204 | else if ((nd->intent.open.flags & O_ACCMODE) == O_WRONLY) { | |
205 | desiredAccess = GENERIC_WRITE; | |
206 | write_only = TRUE; | |
207 | } else if ((nd->intent.open.flags & O_ACCMODE) == O_RDWR) { | |
208 | /* GENERIC_ALL is too much permission to request */ | |
209 | /* can cause unnecessary access denied on create */ | |
210 | /* desiredAccess = GENERIC_ALL; */ | |
211 | desiredAccess = GENERIC_READ | GENERIC_WRITE; | |
212 | } | |
213 | ||
214 | if((nd->intent.open.flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) | |
215 | disposition = FILE_CREATE; | |
216 | else if((nd->intent.open.flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) | |
217 | disposition = FILE_OVERWRITE_IF; | |
218 | else if((nd->intent.open.flags & O_CREAT) == O_CREAT) | |
219 | disposition = FILE_OPEN_IF; | |
220 | else { | |
221 | cFYI(1,("Create flag not set in create function")); | |
222 | } | |
223 | } | |
224 | ||
225 | /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */ | |
226 | if (oplockEnabled) | |
227 | oplock = REQ_OPLOCK; | |
228 | ||
229 | buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL); | |
230 | if(buf == NULL) { | |
231 | kfree(full_path); | |
232 | FreeXid(xid); | |
233 | return -ENOMEM; | |
234 | } | |
235 | ||
236 | rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, | |
237 | desiredAccess, CREATE_NOT_DIR, | |
238 | &fileHandle, &oplock, buf, cifs_sb->local_nls); | |
239 | if (rc) { | |
240 | cFYI(1, ("cifs_create returned 0x%x ", rc)); | |
241 | } else { | |
242 | /* If Open reported that we actually created a file | |
243 | then we now have to set the mode if possible */ | |
244 | if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) && | |
245 | (oplock & CIFS_CREATE_ACTION)) | |
246 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
247 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, | |
248 | (__u64)current->euid, | |
249 | (__u64)current->egid, | |
250 | 0 /* dev */, | |
251 | cifs_sb->local_nls); | |
252 | } else { | |
253 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, | |
254 | (__u64)-1, | |
255 | (__u64)-1, | |
256 | 0 /* dev */, | |
257 | cifs_sb->local_nls); | |
258 | } | |
259 | else { | |
260 | /* BB implement via Windows security descriptors */ | |
261 | /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/ | |
262 | /* could set r/o dos attribute if mode & 0222 == 0 */ | |
263 | } | |
264 | ||
265 | /* BB server might mask mode so we have to query for Unix case*/ | |
266 | if (pTcon->ses->capabilities & CAP_UNIX) | |
267 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
268 | inode->i_sb,xid); | |
269 | else { | |
270 | rc = cifs_get_inode_info(&newinode, full_path, | |
271 | buf, inode->i_sb,xid); | |
272 | if(newinode) | |
273 | newinode->i_mode = mode; | |
274 | } | |
275 | ||
276 | if (rc != 0) { | |
277 | cFYI(1,("Create worked but get_inode_info failed with rc = %d", | |
278 | rc)); | |
279 | } else { | |
280 | direntry->d_op = &cifs_dentry_ops; | |
281 | d_instantiate(direntry, newinode); | |
282 | } | |
283 | if((nd->flags & LOOKUP_OPEN) == FALSE) { | |
284 | /* mknod case - do not leave file open */ | |
285 | CIFSSMBClose(xid, pTcon, fileHandle); | |
286 | } else if(newinode) { | |
d14537f1 | 287 | pCifsFile = |
1da177e4 | 288 | kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL); |
d14537f1 SF |
289 | |
290 | if(pCifsFile == NULL) | |
291 | goto cifs_create_out; | |
292 | memset((char *)pCifsFile, 0, | |
293 | sizeof (struct cifsFileInfo)); | |
294 | pCifsFile->netfid = fileHandle; | |
295 | pCifsFile->pid = current->tgid; | |
296 | pCifsFile->pInode = newinode; | |
297 | pCifsFile->invalidHandle = FALSE; | |
298 | pCifsFile->closePend = FALSE; | |
299 | init_MUTEX(&pCifsFile->fh_sem); | |
300 | /* set the following in open now | |
301 | pCifsFile->pfile = file; */ | |
302 | write_lock(&GlobalSMBSeslock); | |
303 | list_add(&pCifsFile->tlist,&pTcon->openFileList); | |
304 | pCifsInode = CIFS_I(newinode); | |
305 | if(pCifsInode) { | |
1da177e4 | 306 | /* if readable file instance put first in list*/ |
d14537f1 SF |
307 | if (write_only == TRUE) { |
308 | list_add_tail(&pCifsFile->flist, | |
309 | &pCifsInode->openFileList); | |
310 | } else { | |
311 | list_add(&pCifsFile->flist, | |
312 | &pCifsInode->openFileList); | |
1da177e4 | 313 | } |
d14537f1 SF |
314 | if((oplock & 0xF) == OPLOCK_EXCLUSIVE) { |
315 | pCifsInode->clientCanCacheAll = TRUE; | |
316 | pCifsInode->clientCanCacheRead = TRUE; | |
317 | cFYI(1,("Exclusive Oplock for inode %p", | |
318 | newinode)); | |
319 | } else if((oplock & 0xF) == OPLOCK_READ) | |
320 | pCifsInode->clientCanCacheRead = TRUE; | |
1da177e4 | 321 | } |
d14537f1 | 322 | write_unlock(&GlobalSMBSeslock); |
1da177e4 LT |
323 | } |
324 | } | |
d14537f1 SF |
325 | cifs_create_out: |
326 | kfree(buf); | |
327 | kfree(full_path); | |
1da177e4 | 328 | FreeXid(xid); |
1da177e4 LT |
329 | return rc; |
330 | } | |
331 | ||
332 | int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number) | |
333 | { | |
334 | int rc = -EPERM; | |
335 | int xid; | |
336 | struct cifs_sb_info *cifs_sb; | |
337 | struct cifsTconInfo *pTcon; | |
338 | char *full_path = NULL; | |
339 | struct inode * newinode = NULL; | |
340 | ||
341 | if (!old_valid_dev(device_number)) | |
342 | return -EINVAL; | |
343 | ||
344 | xid = GetXid(); | |
345 | ||
346 | cifs_sb = CIFS_SB(inode->i_sb); | |
347 | pTcon = cifs_sb->tcon; | |
348 | ||
349 | down(&direntry->d_sb->s_vfs_rename_sem); | |
350 | full_path = build_path_from_dentry(direntry); | |
351 | up(&direntry->d_sb->s_vfs_rename_sem); | |
352 | if(full_path == NULL) | |
353 | rc = -ENOMEM; | |
354 | ||
355 | if (full_path && (pTcon->ses->capabilities & CAP_UNIX)) { | |
356 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { | |
357 | rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, | |
358 | mode,(__u64)current->euid,(__u64)current->egid, | |
359 | device_number, cifs_sb->local_nls); | |
360 | } else { | |
361 | rc = CIFSSMBUnixSetPerms(xid, pTcon, | |
362 | full_path, mode, (__u64)-1, (__u64)-1, | |
363 | device_number, cifs_sb->local_nls); | |
364 | } | |
365 | ||
366 | if(!rc) { | |
367 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
368 | inode->i_sb,xid); | |
369 | direntry->d_op = &cifs_dentry_ops; | |
370 | if(rc == 0) | |
371 | d_instantiate(direntry, newinode); | |
372 | } | |
373 | } | |
374 | ||
d14537f1 | 375 | kfree(full_path); |
1da177e4 | 376 | FreeXid(xid); |
1da177e4 LT |
377 | return rc; |
378 | } | |
379 | ||
380 | ||
381 | struct dentry * | |
382 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd) | |
383 | { | |
384 | int xid; | |
385 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ | |
386 | struct cifs_sb_info *cifs_sb; | |
387 | struct cifsTconInfo *pTcon; | |
388 | struct inode *newInode = NULL; | |
389 | char *full_path = NULL; | |
390 | ||
391 | xid = GetXid(); | |
392 | ||
393 | cFYI(1, | |
394 | (" parent inode = 0x%p name is: %s and dentry = 0x%p", | |
395 | parent_dir_inode, direntry->d_name.name, direntry)); | |
396 | ||
397 | /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */ | |
398 | ||
399 | /* check whether path exists */ | |
400 | ||
401 | cifs_sb = CIFS_SB(parent_dir_inode->i_sb); | |
402 | pTcon = cifs_sb->tcon; | |
403 | ||
404 | /* can not grab the rename sem here since it would | |
405 | deadlock in the cases (beginning of sys_rename itself) | |
406 | in which we already have the sb rename sem */ | |
407 | full_path = build_path_from_dentry(direntry); | |
408 | if(full_path == NULL) { | |
409 | FreeXid(xid); | |
410 | return ERR_PTR(-ENOMEM); | |
411 | } | |
412 | ||
413 | if (direntry->d_inode != NULL) { | |
414 | cFYI(1, (" non-NULL inode in lookup")); | |
415 | } else { | |
416 | cFYI(1, (" NULL inode in lookup")); | |
417 | } | |
418 | cFYI(1, | |
419 | (" Full path: %s inode = 0x%p", full_path, direntry->d_inode)); | |
420 | ||
421 | if (pTcon->ses->capabilities & CAP_UNIX) | |
422 | rc = cifs_get_inode_info_unix(&newInode, full_path, | |
423 | parent_dir_inode->i_sb,xid); | |
424 | else | |
425 | rc = cifs_get_inode_info(&newInode, full_path, NULL, | |
426 | parent_dir_inode->i_sb,xid); | |
427 | ||
428 | if ((rc == 0) && (newInode != NULL)) { | |
429 | direntry->d_op = &cifs_dentry_ops; | |
430 | d_add(direntry, newInode); | |
431 | ||
432 | /* since paths are not looked up by component - the parent directories are presumed to be good here */ | |
433 | renew_parental_timestamps(direntry); | |
434 | ||
435 | } else if (rc == -ENOENT) { | |
436 | rc = 0; | |
437 | d_add(direntry, NULL); | |
438 | } else { | |
439 | cERROR(1,("Error 0x%x or on cifs_get_inode_info in lookup",rc)); | |
440 | /* BB special case check for Access Denied - watch security | |
441 | exposure of returning dir info implicitly via different rc | |
442 | if file exists or not but no access BB */ | |
443 | } | |
444 | ||
d14537f1 | 445 | kfree(full_path); |
1da177e4 LT |
446 | FreeXid(xid); |
447 | return ERR_PTR(rc); | |
448 | } | |
449 | ||
450 | int | |
451 | cifs_dir_open(struct inode *inode, struct file *file) | |
452 | { /* NB: currently unused since searches are opened in readdir */ | |
453 | int rc = 0; | |
454 | int xid; | |
455 | struct cifs_sb_info *cifs_sb; | |
456 | struct cifsTconInfo *pTcon; | |
457 | char *full_path = NULL; | |
458 | ||
459 | xid = GetXid(); | |
460 | ||
461 | cifs_sb = CIFS_SB(inode->i_sb); | |
462 | pTcon = cifs_sb->tcon; | |
463 | ||
464 | if(file->f_dentry) { | |
465 | down(&file->f_dentry->d_sb->s_vfs_rename_sem); | |
466 | full_path = build_wildcard_path_from_dentry(file->f_dentry); | |
467 | up(&file->f_dentry->d_sb->s_vfs_rename_sem); | |
468 | } else { | |
469 | FreeXid(xid); | |
470 | return -EIO; | |
471 | } | |
472 | ||
473 | cFYI(1, ("inode = 0x%p and full path is %s", inode, full_path)); | |
474 | ||
d14537f1 | 475 | kfree(full_path); |
1da177e4 LT |
476 | FreeXid(xid); |
477 | return rc; | |
478 | } | |
479 | ||
480 | static int | |
481 | cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) | |
482 | { | |
483 | int isValid = 1; | |
484 | ||
485 | /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */ | |
486 | ||
487 | if (direntry->d_inode) { | |
488 | if (cifs_revalidate(direntry)) { | |
489 | /* unlock_kernel(); */ | |
490 | return 0; | |
491 | } | |
492 | } else { | |
493 | cFYI(1, | |
494 | ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p", | |
495 | direntry->d_name.name, direntry)); | |
496 | } | |
497 | ||
498 | /* unlock_kernel(); */ | |
499 | ||
500 | return isValid; | |
501 | } | |
502 | ||
503 | /* static int cifs_d_delete(struct dentry *direntry) | |
504 | { | |
505 | int rc = 0; | |
506 | ||
507 | cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name)); | |
508 | ||
509 | return rc; | |
510 | } */ | |
511 | ||
512 | struct dentry_operations cifs_dentry_ops = { | |
513 | .d_revalidate = cifs_d_revalidate, | |
514 | /* d_delete: cifs_d_delete, *//* not needed except for debugging */ | |
515 | /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */ | |
516 | }; |