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