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