patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / cifs / dir.c
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 (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>
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 char *
105 build_wildcard_path_from_dentry(struct dentry *direntry)
106 {
107         struct dentry *temp;
108         int namelen = 0;
109         char *full_path;
110
111         for (temp = direntry; !IS_ROOT(temp);) {
112                 namelen += (1 + temp->d_name.len);
113                 temp = temp->d_parent;
114         }
115         namelen += 3;           /* allow for trailing null and wildcard (slash and *) */
116         full_path = kmalloc(namelen, GFP_KERNEL);
117         namelen--;
118         full_path[namelen] = 0; /* trailing null */
119         namelen--;
120         full_path[namelen] = '*';
121         namelen--;
122         full_path[namelen] = '\\';
123
124         for (temp = direntry; !IS_ROOT(temp);) {
125                 namelen -= 1 + temp->d_name.len;
126                 if (namelen < 0) {
127                         break;
128                 } else {
129                         full_path[namelen] = '\\';
130                         strncpy(full_path + namelen + 1, temp->d_name.name,
131                                 temp->d_name.len);
132                 }
133                 temp = temp->d_parent;
134         }
135         if (namelen != 0)
136                 cERROR(1,
137                        ("We did not end path lookup where we expected namelen is %d",
138                         namelen));
139
140         return full_path;
141 }
142
143 /* Inode operations in similar order to how they appear in the Linux file fs.h */
144
145 int
146 cifs_create(struct inode *inode, struct dentry *direntry, int mode,
147                 struct nameidata *nd)
148 {
149         int rc = -ENOENT;
150         int xid;
151         int oplock = 0;
152         int desiredAccess = GENERIC_READ | GENERIC_WRITE;
153         __u16 fileHandle;
154         struct cifs_sb_info *cifs_sb;
155         struct cifsTconInfo *pTcon;
156         char *full_path = NULL;
157         FILE_ALL_INFO * buf = NULL;
158         struct inode *newinode = NULL;
159         struct cifsFileInfo * pCifsFile = NULL;
160         struct cifsInodeInfo * pCifsInode;
161         int disposition = FILE_OVERWRITE_IF;
162         int write_only = FALSE;
163
164         xid = GetXid();
165
166         cifs_sb = CIFS_SB(inode->i_sb);
167         pTcon = cifs_sb->tcon;
168
169         down(&direntry->d_sb->s_vfs_rename_sem);
170         full_path = build_path_from_dentry(direntry);
171         up(&direntry->d_sb->s_vfs_rename_sem);
172         if(full_path == NULL) {
173                 FreeXid(xid);
174                 return -ENOMEM;
175         }
176
177         if(nd) {
178                 if ((nd->intent.open.flags & O_ACCMODE) == O_RDONLY)
179                         desiredAccess = GENERIC_READ;
180                 else if ((nd->intent.open.flags & O_ACCMODE) == O_WRONLY) {
181                         desiredAccess = GENERIC_WRITE;
182                         write_only = TRUE;
183                 } else if ((nd->intent.open.flags & O_ACCMODE) == O_RDWR) {
184                         /* GENERIC_ALL is too much permission to request */
185                         /* can cause unnecessary access denied on create */
186                         /* desiredAccess = GENERIC_ALL; */
187                         desiredAccess = GENERIC_READ | GENERIC_WRITE;
188                 }
189
190                 if((nd->intent.open.flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
191                         disposition = FILE_CREATE;
192                 else if((nd->intent.open.flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
193                         disposition = FILE_OVERWRITE_IF;
194                 else if((nd->intent.open.flags & O_CREAT) == O_CREAT)
195                         disposition = FILE_OPEN_IF;
196                 else {
197                         cFYI(1,("Create flag not set in create function"));
198                 }
199         }
200
201         /* BB add processing to set equivalent of mode - e.g. via CreateX with ACLs */
202         if (oplockEnabled)
203                 oplock = REQ_OPLOCK;
204
205         buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
206         if(buf == NULL) {
207                 kfree(full_path);
208                 FreeXid(xid);
209                 return -ENOMEM;
210         }
211
212         rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
213                          desiredAccess, CREATE_NOT_DIR,
214                          &fileHandle, &oplock, buf, cifs_sb->local_nls);
215         if (rc) {
216                 cFYI(1, ("cifs_create returned 0x%x ", rc));
217         } else {
218                 /* If Open reported that we actually created a file
219                 then we now have to set the mode if possible */
220                 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) &&
221                         (oplock & CIFS_CREATE_ACTION))
222                         CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
223                                         (__u64)-1,
224                                         (__u64)-1,
225                                         0 /* dev */,
226                                         cifs_sb->local_nls);
227                 else {
228                         /* BB implement via Windows security descriptors */
229                         /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
230                         /* could set r/o dos attribute if mode & 0222 == 0 */
231                 }
232
233         /* BB server might mask mode so we have to query for Unix case*/
234                 if (pTcon->ses->capabilities & CAP_UNIX)
235                         rc = cifs_get_inode_info_unix(&newinode, full_path,
236                                                  inode->i_sb);
237                 else {
238                         rc = cifs_get_inode_info(&newinode, full_path,
239                                                  buf, inode->i_sb);
240                         if(newinode)
241                                 newinode->i_mode = mode;
242                 }
243
244                 if (rc != 0) {
245                         cFYI(1,("Create worked but get_inode_info failed with rc = %d",
246                               rc));
247                 } else {
248                         direntry->d_op = &cifs_dentry_ops;
249                         d_instantiate(direntry, newinode);
250                 }
251                 if((nd->flags & LOOKUP_OPEN) == FALSE) {
252                         /* mknod case - do not leave file open */
253                         CIFSSMBClose(xid, pTcon, fileHandle);
254                 } else if(newinode) {
255                         pCifsFile = (struct cifsFileInfo *)
256                            kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
257                 
258                         if (pCifsFile) {
259                                 memset((char *)pCifsFile, 0,
260                                        sizeof (struct cifsFileInfo));
261                                 pCifsFile->netfid = fileHandle;
262                                 pCifsFile->pid = current->tgid;
263                                 pCifsFile->pInode = newinode;
264                                 pCifsFile->invalidHandle = FALSE;
265                                 pCifsFile->closePend     = FALSE;
266                                 init_MUTEX(&pCifsFile->fh_sem);
267                                 /* put the following in at open now */
268                                 /* pCifsFile->pfile = file; */ 
269                                 write_lock(&GlobalSMBSeslock);
270                                 list_add(&pCifsFile->tlist,&pTcon->openFileList);
271                                 pCifsInode = CIFS_I(newinode);
272                                 if(pCifsInode) {
273                                 /* if readable file instance put first in list*/
274                                         if (write_only == TRUE) {
275                                                 list_add_tail(&pCifsFile->flist,
276                                                         &pCifsInode->openFileList);
277                                         } else {
278                                                 list_add(&pCifsFile->flist,
279                                                         &pCifsInode->openFileList);
280                                         }
281                                         if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
282                                                 pCifsInode->clientCanCacheAll = TRUE;
283                                                 pCifsInode->clientCanCacheRead = TRUE;
284                                                 cFYI(1,("Exclusive Oplock granted on inode %p",
285                                                         newinode));
286                                         } else if((oplock & 0xF) == OPLOCK_READ)
287                                                 pCifsInode->clientCanCacheRead = TRUE;
288                                 }
289                                 write_unlock(&GlobalSMBSeslock);
290                         }
291                 }
292         } 
293
294         if (buf)
295             kfree(buf);
296         if (full_path)
297             kfree(full_path);
298         FreeXid(xid);
299
300         return rc;
301 }
302
303 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number) 
304 {
305         int rc = -EPERM;
306         int xid;
307         struct cifs_sb_info *cifs_sb;
308         struct cifsTconInfo *pTcon;
309         char *full_path = NULL;
310         struct inode * newinode = NULL;
311
312         if (!old_valid_dev(device_number))
313                 return -EINVAL;
314
315         xid = GetXid();
316
317         cifs_sb = CIFS_SB(inode->i_sb);
318         pTcon = cifs_sb->tcon;
319
320         down(&direntry->d_sb->s_vfs_rename_sem);
321         full_path = build_path_from_dentry(direntry);
322         up(&direntry->d_sb->s_vfs_rename_sem);
323         if(full_path == NULL)
324                 rc = -ENOMEM;
325         
326         if (full_path && (pTcon->ses->capabilities & CAP_UNIX)) {
327                 rc = CIFSSMBUnixSetPerms(xid, pTcon,
328                         full_path, mode, current->euid, current->egid,
329                         device_number, cifs_sb->local_nls);
330                 if(!rc) {
331                         rc = cifs_get_inode_info_unix(&newinode, full_path,
332                                                 inode->i_sb);
333                         direntry->d_op = &cifs_dentry_ops;
334                         if(rc == 0)
335                                 d_instantiate(direntry, newinode);
336                 }
337         }
338
339         if (full_path)
340                 kfree(full_path);
341         FreeXid(xid);
342
343         return rc;
344 }
345
346
347 struct dentry *
348 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
349 {
350         int xid;
351         int rc = 0; /* to get around spurious gcc warning, set to zero here */
352         struct cifs_sb_info *cifs_sb;
353         struct cifsTconInfo *pTcon;
354         struct inode *newInode = NULL;
355         char *full_path = NULL;
356
357         xid = GetXid();
358
359         cFYI(1,
360              (" parent inode = 0x%p name is: %s and dentry = 0x%p",
361               parent_dir_inode, direntry->d_name.name, direntry));
362
363         if(nd) {  /* BB removeme */
364                 cFYI(1,("In lookup nd flags 0x%x open intent flags 0x%x",nd->flags,nd->intent.open.flags));
365         } /* BB removeme BB */
366         /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
367
368         /* check whether path exists */
369
370         cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
371         pTcon = cifs_sb->tcon;
372
373         /* can not grab the rename sem here since it would
374         deadlock in the cases (beginning of sys_rename itself)
375         in which we already have the sb rename sem */
376         full_path = build_path_from_dentry(direntry);
377         if(full_path == NULL) {
378                 FreeXid(xid);
379                 return ERR_PTR(-ENOMEM);
380         }
381
382         if (direntry->d_inode != NULL) {
383                 cFYI(1, (" non-NULL inode in lookup"));
384         } else {
385                 cFYI(1, (" NULL inode in lookup"));
386         }
387         cFYI(1,
388              (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
389
390         if (pTcon->ses->capabilities & CAP_UNIX)
391                 rc = cifs_get_inode_info_unix(&newInode, full_path,
392                                               parent_dir_inode->i_sb);
393         else
394                 rc = cifs_get_inode_info(&newInode, full_path, NULL,
395                                          parent_dir_inode->i_sb);
396
397         if ((rc == 0) && (newInode != NULL)) {
398                 direntry->d_op = &cifs_dentry_ops;
399                 d_add(direntry, newInode);
400
401                 /* since paths are not looked up by component - the parent directories are presumed to be good here */
402                 renew_parental_timestamps(direntry);
403
404         } else if (rc == -ENOENT) {
405                 rc = 0;
406                 d_add(direntry, NULL);
407         } else {
408                 cERROR(1,("Error 0x%x or on cifs_get_inode_info in lookup",rc));
409                 /* BB special case check for Access Denied - watch security 
410                 exposure of returning dir info implicitly via different rc 
411                 if file exists or not but no access BB */
412         }
413
414         if (full_path)
415                 kfree(full_path);
416         FreeXid(xid);
417         return ERR_PTR(rc);
418 }
419
420 int
421 cifs_dir_open(struct inode *inode, struct file *file)
422 {                               /* NB: currently unused since searches are opened in readdir */
423         int rc = 0;
424         int xid;
425         struct cifs_sb_info *cifs_sb;
426         struct cifsTconInfo *pTcon;
427         char *full_path = NULL;
428
429         xid = GetXid();
430
431         cifs_sb = CIFS_SB(inode->i_sb);
432         pTcon = cifs_sb->tcon;
433
434         full_path = build_wildcard_path_from_dentry(file->f_dentry);
435
436         cFYI(1, (" inode = 0x%p and full path is %s", inode, full_path));
437
438         if (full_path)
439                 kfree(full_path);
440         FreeXid(xid);
441         return rc;
442 }
443
444 static int
445 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
446 {
447         int isValid = 1;
448
449 /*      lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
450
451         if (direntry->d_inode) {
452                 if (cifs_revalidate(direntry)) {
453                         /* unlock_kernel(); */
454                         return 0;
455                 }
456         } else {
457                 cFYI(1,
458                      ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
459                       direntry->d_name.name, direntry));
460         }
461
462 /*    unlock_kernel(); */
463
464         return isValid;
465 }
466
467 /* static int cifs_d_delete(struct dentry *direntry)
468 {
469         int rc = 0;
470
471         cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
472
473         return rc;
474 }     */
475
476 struct dentry_operations cifs_dentry_ops = {
477         .d_revalidate = cifs_d_revalidate,
478 /* d_delete:       cifs_d_delete,       *//* not needed except for debugging */
479         /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
480 };