This commit was manufactured by cvs2svn to create tag
[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 /* 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                         CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
247                                         (__u64)-1,
248                                         (__u64)-1,
249                                         0 /* dev */,
250                                         cifs_sb->local_nls);
251                 else {
252                         /* BB implement via Windows security descriptors */
253                         /* eg CIFSSMBWinSetPerms(xid,pTcon,full_path,mode,-1,-1,local_nls);*/
254                         /* could set r/o dos attribute if mode & 0222 == 0 */
255                 }
256
257         /* BB server might mask mode so we have to query for Unix case*/
258                 if (pTcon->ses->capabilities & CAP_UNIX)
259                         rc = cifs_get_inode_info_unix(&newinode, full_path,
260                                                  inode->i_sb,xid);
261                 else {
262                         rc = cifs_get_inode_info(&newinode, full_path,
263                                                  buf, inode->i_sb,xid);
264                         if(newinode)
265                                 newinode->i_mode = mode;
266                 }
267
268                 if (rc != 0) {
269                         cFYI(1,("Create worked but get_inode_info failed with rc = %d",
270                               rc));
271                 } else {
272                         direntry->d_op = &cifs_dentry_ops;
273                         d_instantiate(direntry, newinode);
274                 }
275                 if((nd->flags & LOOKUP_OPEN) == FALSE) {
276                         /* mknod case - do not leave file open */
277                         CIFSSMBClose(xid, pTcon, fileHandle);
278                 } else if(newinode) {
279                         pCifsFile = (struct cifsFileInfo *)
280                            kmalloc(sizeof (struct cifsFileInfo), GFP_KERNEL);
281                 
282                         if (pCifsFile) {
283                                 memset((char *)pCifsFile, 0,
284                                        sizeof (struct cifsFileInfo));
285                                 pCifsFile->netfid = fileHandle;
286                                 pCifsFile->pid = current->tgid;
287                                 pCifsFile->pInode = newinode;
288                                 pCifsFile->invalidHandle = FALSE;
289                                 pCifsFile->closePend     = FALSE;
290                                 init_MUTEX(&pCifsFile->fh_sem);
291                                 /* put the following in at open now */
292                                 /* pCifsFile->pfile = file; */ 
293                                 write_lock(&GlobalSMBSeslock);
294                                 list_add(&pCifsFile->tlist,&pTcon->openFileList);
295                                 pCifsInode = CIFS_I(newinode);
296                                 if(pCifsInode) {
297                                 /* if readable file instance put first in list*/
298                                         if (write_only == TRUE) {
299                                                 list_add_tail(&pCifsFile->flist,
300                                                         &pCifsInode->openFileList);
301                                         } else {
302                                                 list_add(&pCifsFile->flist,
303                                                         &pCifsInode->openFileList);
304                                         }
305                                         if((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
306                                                 pCifsInode->clientCanCacheAll = TRUE;
307                                                 pCifsInode->clientCanCacheRead = TRUE;
308                                                 cFYI(1,("Exclusive Oplock granted on inode %p",
309                                                         newinode));
310                                         } else if((oplock & 0xF) == OPLOCK_READ)
311                                                 pCifsInode->clientCanCacheRead = TRUE;
312                                 }
313                                 write_unlock(&GlobalSMBSeslock);
314                         }
315                 }
316         } 
317
318         if (buf)
319             kfree(buf);
320         if (full_path)
321             kfree(full_path);
322         FreeXid(xid);
323
324         return rc;
325 }
326
327 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, dev_t device_number) 
328 {
329         int rc = -EPERM;
330         int xid;
331         struct cifs_sb_info *cifs_sb;
332         struct cifsTconInfo *pTcon;
333         char *full_path = NULL;
334         struct inode * newinode = NULL;
335
336         if (!old_valid_dev(device_number))
337                 return -EINVAL;
338
339         xid = GetXid();
340
341         cifs_sb = CIFS_SB(inode->i_sb);
342         pTcon = cifs_sb->tcon;
343
344         down(&direntry->d_sb->s_vfs_rename_sem);
345         full_path = build_path_from_dentry(direntry);
346         up(&direntry->d_sb->s_vfs_rename_sem);
347         if(full_path == NULL)
348                 rc = -ENOMEM;
349         
350         if (full_path && (pTcon->ses->capabilities & CAP_UNIX)) {
351                 rc = CIFSSMBUnixSetPerms(xid, pTcon,
352                         full_path, mode, current->euid, current->egid,
353                         device_number, cifs_sb->local_nls);
354                 if(!rc) {
355                         rc = cifs_get_inode_info_unix(&newinode, full_path,
356                                                 inode->i_sb,xid);
357                         direntry->d_op = &cifs_dentry_ops;
358                         if(rc == 0)
359                                 d_instantiate(direntry, newinode);
360                 }
361         }
362
363         if (full_path)
364                 kfree(full_path);
365         FreeXid(xid);
366
367         return rc;
368 }
369
370
371 struct dentry *
372 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
373 {
374         int xid;
375         int rc = 0; /* to get around spurious gcc warning, set to zero here */
376         struct cifs_sb_info *cifs_sb;
377         struct cifsTconInfo *pTcon;
378         struct inode *newInode = NULL;
379         char *full_path = NULL;
380
381         xid = GetXid();
382
383         cFYI(1,
384              (" parent inode = 0x%p name is: %s and dentry = 0x%p",
385               parent_dir_inode, direntry->d_name.name, direntry));
386
387         if(nd) {  /* BB removeme */
388                 cFYI(1,("In lookup nd flags 0x%x open intent flags 0x%x",nd->flags,nd->intent.open.flags));
389         } /* BB removeme BB */
390         /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
391
392         /* check whether path exists */
393
394         cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
395         pTcon = cifs_sb->tcon;
396
397         /* can not grab the rename sem here since it would
398         deadlock in the cases (beginning of sys_rename itself)
399         in which we already have the sb rename sem */
400         full_path = build_path_from_dentry(direntry);
401         if(full_path == NULL) {
402                 FreeXid(xid);
403                 return ERR_PTR(-ENOMEM);
404         }
405
406         if (direntry->d_inode != NULL) {
407                 cFYI(1, (" non-NULL inode in lookup"));
408         } else {
409                 cFYI(1, (" NULL inode in lookup"));
410         }
411         cFYI(1,
412              (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
413
414         if (pTcon->ses->capabilities & CAP_UNIX)
415                 rc = cifs_get_inode_info_unix(&newInode, full_path,
416                                               parent_dir_inode->i_sb,xid);
417         else
418                 rc = cifs_get_inode_info(&newInode, full_path, NULL,
419                                          parent_dir_inode->i_sb,xid);
420
421         if ((rc == 0) && (newInode != NULL)) {
422                 direntry->d_op = &cifs_dentry_ops;
423                 d_add(direntry, newInode);
424
425                 /* since paths are not looked up by component - the parent directories are presumed to be good here */
426                 renew_parental_timestamps(direntry);
427
428         } else if (rc == -ENOENT) {
429                 rc = 0;
430                 d_add(direntry, NULL);
431         } else {
432                 cERROR(1,("Error 0x%x or on cifs_get_inode_info in lookup",rc));
433                 /* BB special case check for Access Denied - watch security 
434                 exposure of returning dir info implicitly via different rc 
435                 if file exists or not but no access BB */
436         }
437
438         if (full_path)
439                 kfree(full_path);
440         FreeXid(xid);
441         return ERR_PTR(rc);
442 }
443
444 int
445 cifs_dir_open(struct inode *inode, struct file *file)
446 {                               /* NB: currently unused since searches are opened in readdir */
447         int rc = 0;
448         int xid;
449         struct cifs_sb_info *cifs_sb;
450         struct cifsTconInfo *pTcon;
451         char *full_path = NULL;
452
453         xid = GetXid();
454
455         cifs_sb = CIFS_SB(inode->i_sb);
456         pTcon = cifs_sb->tcon;
457
458         if(file->f_dentry) {
459                 down(&file->f_dentry->d_sb->s_vfs_rename_sem);
460                 full_path = build_wildcard_path_from_dentry(file->f_dentry);
461                 up(&file->f_dentry->d_sb->s_vfs_rename_sem);
462         } else {
463                 FreeXid(xid);
464                 return -EIO;
465         }
466
467         cFYI(1, ("inode = 0x%p and full path is %s", inode, full_path));
468
469         if (full_path)
470                 kfree(full_path);
471         FreeXid(xid);
472         return rc;
473 }
474
475 static int
476 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
477 {
478         int isValid = 1;
479
480 /*      lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
481
482         if (direntry->d_inode) {
483                 if (cifs_revalidate(direntry)) {
484                         /* unlock_kernel(); */
485                         return 0;
486                 }
487         } else {
488                 cFYI(1,
489                      ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
490                       direntry->d_name.name, direntry));
491         }
492
493 /*    unlock_kernel(); */
494
495         return isValid;
496 }
497
498 /* static int cifs_d_delete(struct dentry *direntry)
499 {
500         int rc = 0;
501
502         cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
503
504         return rc;
505 }     */
506
507 struct dentry_operations cifs_dentry_ops = {
508         .d_revalidate = cifs_d_revalidate,
509 /* d_delete:       cifs_d_delete,       *//* not needed except for debugging */
510         /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
511 };