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