Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / fs / smbfs / dir.c
1 /*
2  *  dir.c
3  *
4  *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5  *  Copyright (C) 1997 by Volker Lendecke
6  *
7  *  Please add a note about your changes to smbfs in the ChangeLog file.
8  */
9
10 #include <linux/time.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/smp_lock.h>
14 #include <linux/ctype.h>
15 #include <linux/net.h>
16
17 #include <linux/smb_fs.h>
18 #include <linux/smb_mount.h>
19 #include <linux/smbno.h>
20
21 #include "smb_debug.h"
22 #include "proto.h"
23
24 static int smb_readdir(struct file *, void *, filldir_t);
25 static int smb_dir_open(struct inode *, struct file *);
26
27 static struct dentry *smb_lookup(struct inode *, struct dentry *, struct nameidata *);
28 static int smb_create(struct inode *, struct dentry *, int, struct nameidata *);
29 static int smb_mkdir(struct inode *, struct dentry *, int);
30 static int smb_rmdir(struct inode *, struct dentry *);
31 static int smb_unlink(struct inode *, struct dentry *);
32 static int smb_rename(struct inode *, struct dentry *,
33                       struct inode *, struct dentry *);
34 static int smb_make_node(struct inode *,struct dentry *,int,dev_t);
35 static int smb_link(struct dentry *, struct inode *, struct dentry *);
36
37 struct file_operations smb_dir_operations =
38 {
39         .read           = generic_read_dir,
40         .readdir        = smb_readdir,
41         .ioctl          = smb_ioctl,
42         .open           = smb_dir_open,
43 };
44
45 struct inode_operations smb_dir_inode_operations =
46 {
47         .create         = smb_create,
48         .lookup         = smb_lookup,
49         .unlink         = smb_unlink,
50         .mkdir          = smb_mkdir,
51         .rmdir          = smb_rmdir,
52         .rename         = smb_rename,
53         .getattr        = smb_getattr,
54         .setattr        = smb_notify_change,
55 };
56
57 struct inode_operations smb_dir_inode_operations_unix =
58 {
59         .create         = smb_create,
60         .lookup         = smb_lookup,
61         .unlink         = smb_unlink,
62         .mkdir          = smb_mkdir,
63         .rmdir          = smb_rmdir,
64         .rename         = smb_rename,
65         .getattr        = smb_getattr,
66         .setattr        = smb_notify_change,
67         .symlink        = smb_symlink,
68         .mknod          = smb_make_node,
69         .link           = smb_link,
70 };
71
72 /*
73  * Read a directory, using filldir to fill the dirent memory.
74  * smb_proc_readdir does the actual reading from the smb server.
75  *
76  * The cache code is almost directly taken from ncpfs
77  */
78 static int 
79 smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
80 {
81         struct dentry *dentry = filp->f_dentry;
82         struct inode *dir = dentry->d_inode;
83         struct smb_sb_info *server = server_from_dentry(dentry);
84         union  smb_dir_cache *cache = NULL;
85         struct smb_cache_control ctl;
86         struct page *page = NULL;
87         int result;
88
89         ctl.page  = NULL;
90         ctl.cache = NULL;
91
92         VERBOSE("reading %s/%s, f_pos=%d\n",
93                 DENTRY_PATH(dentry),  (int) filp->f_pos);
94
95         result = 0;
96
97         lock_kernel();
98
99         switch ((unsigned int) filp->f_pos) {
100         case 0:
101                 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
102                         goto out;
103                 filp->f_pos = 1;
104                 /* fallthrough */
105         case 1:
106                 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR) < 0)
107                         goto out;
108                 filp->f_pos = 2;
109         }
110
111         /*
112          * Make sure our inode is up-to-date.
113          */
114         result = smb_revalidate_inode(dentry);
115         if (result)
116                 goto out;
117
118
119         page = grab_cache_page(&dir->i_data, 0);
120         if (!page)
121                 goto read_really;
122
123         ctl.cache = cache = kmap(page);
124         ctl.head  = cache->head;
125
126         if (!PageUptodate(page) || !ctl.head.eof) {
127                 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
128                          DENTRY_PATH(dentry), PageUptodate(page),ctl.head.eof);
129                 goto init_cache;
130         }
131
132         if (filp->f_pos == 2) {
133                 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
134                         goto init_cache;
135
136                 /*
137                  * N.B. ncpfs checks mtime of dentry too here, we don't.
138                  *   1. common smb servers do not update mtime on dir changes
139                  *   2. it requires an extra smb request
140                  *      (revalidate has the same timeout as ctl.head.time)
141                  *
142                  * Instead smbfs invalidates its own cache on local changes
143                  * and remote changes are not seen until timeout.
144                  */
145         }
146
147         if (filp->f_pos > ctl.head.end)
148                 goto finished;
149
150         ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
151         ctl.ofs  = ctl.fpos / SMB_DIRCACHE_SIZE;
152         ctl.idx  = ctl.fpos % SMB_DIRCACHE_SIZE;
153
154         for (;;) {
155                 if (ctl.ofs != 0) {
156                         ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
157                         if (!ctl.page)
158                                 goto invalid_cache;
159                         ctl.cache = kmap(ctl.page);
160                         if (!PageUptodate(ctl.page))
161                                 goto invalid_cache;
162                 }
163                 while (ctl.idx < SMB_DIRCACHE_SIZE) {
164                         struct dentry *dent;
165                         int res;
166
167                         dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
168                                              dentry, filp->f_pos);
169                         if (!dent)
170                                 goto invalid_cache;
171
172                         res = filldir(dirent, dent->d_name.name,
173                                       dent->d_name.len, filp->f_pos,
174                                       dent->d_inode->i_ino, DT_UNKNOWN);
175                         dput(dent);
176                         if (res)
177                                 goto finished;
178                         filp->f_pos += 1;
179                         ctl.idx += 1;
180                         if (filp->f_pos > ctl.head.end)
181                                 goto finished;
182                 }
183                 if (ctl.page) {
184                         kunmap(ctl.page);
185                         SetPageUptodate(ctl.page);
186                         unlock_page(ctl.page);
187                         page_cache_release(ctl.page);
188                         ctl.page = NULL;
189                 }
190                 ctl.idx  = 0;
191                 ctl.ofs += 1;
192         }
193 invalid_cache:
194         if (ctl.page) {
195                 kunmap(ctl.page);
196                 unlock_page(ctl.page);
197                 page_cache_release(ctl.page);
198                 ctl.page = NULL;
199         }
200         ctl.cache = cache;
201 init_cache:
202         smb_invalidate_dircache_entries(dentry);
203         ctl.head.time = jiffies;
204         ctl.head.eof = 0;
205         ctl.fpos = 2;
206         ctl.ofs = 0;
207         ctl.idx = SMB_DIRCACHE_START;
208         ctl.filled = 0;
209         ctl.valid  = 1;
210 read_really:
211         result = server->ops->readdir(filp, dirent, filldir, &ctl);
212         if (ctl.idx == -1)
213                 goto invalid_cache;     /* retry */
214         ctl.head.end = ctl.fpos - 1;
215         ctl.head.eof = ctl.valid;
216 finished:
217         if (page) {
218                 cache->head = ctl.head;
219                 kunmap(page);
220                 SetPageUptodate(page);
221                 unlock_page(page);
222                 page_cache_release(page);
223         }
224         if (ctl.page) {
225                 kunmap(ctl.page);
226                 SetPageUptodate(ctl.page);
227                 unlock_page(ctl.page);
228                 page_cache_release(ctl.page);
229         }
230 out:
231         unlock_kernel();
232         return result;
233 }
234
235 static int
236 smb_dir_open(struct inode *dir, struct file *file)
237 {
238         struct dentry *dentry = file->f_dentry;
239         struct smb_sb_info *server;
240         int error = 0;
241
242         VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
243                 file->f_dentry->d_name.name);
244
245         /*
246          * Directory timestamps in the core protocol aren't updated
247          * when a file is added, so we give them a very short TTL.
248          */
249         lock_kernel();
250         server = server_from_dentry(dentry);
251         if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
252                 unsigned long age = jiffies - SMB_I(dir)->oldmtime;
253                 if (age > 2*HZ)
254                         smb_invalid_dir_cache(dir);
255         }
256
257         /*
258          * Note: in order to allow the smbmount process to open the
259          * mount point, we only revalidate if the connection is valid or
260          * if the process is trying to access something other than the root.
261          */
262         if (server->state == CONN_VALID || !IS_ROOT(dentry))
263                 error = smb_revalidate_inode(dentry);
264         unlock_kernel();
265         return error;
266 }
267
268 /*
269  * Dentry operations routines
270  */
271 static int smb_lookup_validate(struct dentry *, struct nameidata *);
272 static int smb_hash_dentry(struct dentry *, struct qstr *);
273 static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
274 static int smb_delete_dentry(struct dentry *);
275
276 static struct dentry_operations smbfs_dentry_operations =
277 {
278         .d_revalidate   = smb_lookup_validate,
279         .d_hash         = smb_hash_dentry,
280         .d_compare      = smb_compare_dentry,
281         .d_delete       = smb_delete_dentry,
282 };
283
284 static struct dentry_operations smbfs_dentry_operations_case =
285 {
286         .d_revalidate   = smb_lookup_validate,
287         .d_delete       = smb_delete_dentry,
288 };
289
290
291 /*
292  * This is the callback when the dcache has a lookup hit.
293  */
294 static int
295 smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
296 {
297         struct smb_sb_info *server = server_from_dentry(dentry);
298         struct inode * inode = dentry->d_inode;
299         unsigned long age = jiffies - dentry->d_time;
300         int valid;
301
302         /*
303          * The default validation is based on dentry age:
304          * we believe in dentries for a few seconds.  (But each
305          * successful server lookup renews the timestamp.)
306          */
307         valid = (age <= SMB_MAX_AGE(server));
308 #ifdef SMBFS_DEBUG_VERBOSE
309         if (!valid)
310                 VERBOSE("%s/%s not valid, age=%lu\n", 
311                         DENTRY_PATH(dentry), age);
312 #endif
313
314         if (inode) {
315                 lock_kernel();
316                 if (is_bad_inode(inode)) {
317                         PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
318                         valid = 0;
319                 } else if (!valid)
320                         valid = (smb_revalidate_inode(dentry) == 0);
321                 unlock_kernel();
322         } else {
323                 /*
324                  * What should we do for negative dentries?
325                  */
326         }
327         return valid;
328 }
329
330 static int 
331 smb_hash_dentry(struct dentry *dir, struct qstr *this)
332 {
333         unsigned long hash;
334         int i;
335
336         hash = init_name_hash();
337         for (i=0; i < this->len ; i++)
338                 hash = partial_name_hash(tolower(this->name[i]), hash);
339         this->hash = end_name_hash(hash);
340   
341         return 0;
342 }
343
344 static int
345 smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
346 {
347         int i, result = 1;
348
349         if (a->len != b->len)
350                 goto out;
351         for (i=0; i < a->len; i++) {
352                 if (tolower(a->name[i]) != tolower(b->name[i]))
353                         goto out;
354         }
355         result = 0;
356 out:
357         return result;
358 }
359
360 /*
361  * This is the callback from dput() when d_count is going to 0.
362  * We use this to unhash dentries with bad inodes.
363  */
364 static int
365 smb_delete_dentry(struct dentry * dentry)
366 {
367         if (dentry->d_inode) {
368                 if (is_bad_inode(dentry->d_inode)) {
369                         PARANOIA("bad inode, unhashing %s/%s\n",
370                                  DENTRY_PATH(dentry));
371                         return 1;
372                 }
373         } else {
374                 /* N.B. Unhash negative dentries? */
375         }
376         return 0;
377 }
378
379 /*
380  * Initialize a new dentry
381  */
382 void
383 smb_new_dentry(struct dentry *dentry)
384 {
385         struct smb_sb_info *server = server_from_dentry(dentry);
386
387         if (server->mnt->flags & SMB_MOUNT_CASE)
388                 dentry->d_op = &smbfs_dentry_operations_case;
389         else
390                 dentry->d_op = &smbfs_dentry_operations;
391         dentry->d_time = jiffies;
392 }
393
394
395 /*
396  * Whenever a lookup succeeds, we know the parent directories
397  * are all valid, so we want to update the dentry timestamps.
398  * N.B. Move this to dcache?
399  */
400 void
401 smb_renew_times(struct dentry * dentry)
402 {
403         dget(dentry);
404         spin_lock(&dentry->d_lock);
405         for (;;) {
406                 struct dentry *parent;
407
408                 dentry->d_time = jiffies;
409                 if (IS_ROOT(dentry))
410                         break;
411                 parent = dentry->d_parent;
412                 dget(parent);
413                 spin_unlock(&dentry->d_lock);
414                 dput(dentry);
415                 dentry = parent;
416                 spin_lock(&dentry->d_lock);
417         }
418         spin_unlock(&dentry->d_lock);
419         dput(dentry);
420 }
421
422 static struct dentry *
423 smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
424 {
425         struct smb_fattr finfo;
426         struct inode *inode;
427         int error;
428         struct smb_sb_info *server;
429
430         error = -ENAMETOOLONG;
431         if (dentry->d_name.len > SMB_MAXNAMELEN)
432                 goto out;
433
434         lock_kernel();
435         error = smb_proc_getattr(dentry, &finfo);
436 #ifdef SMBFS_PARANOIA
437         if (error && error != -ENOENT)
438                 PARANOIA("find %s/%s failed, error=%d\n",
439                          DENTRY_PATH(dentry), error);
440 #endif
441
442         inode = NULL;
443         if (error == -ENOENT)
444                 goto add_entry;
445         if (!error) {
446                 error = -EACCES;
447                 finfo.f_ino = iunique(dentry->d_sb, 2);
448                 inode = smb_iget(dir->i_sb, &finfo);
449                 if (inode) {
450         add_entry:
451                         server = server_from_dentry(dentry);
452                         if (server->mnt->flags & SMB_MOUNT_CASE)
453                                 dentry->d_op = &smbfs_dentry_operations_case;
454                         else
455                                 dentry->d_op = &smbfs_dentry_operations;
456
457                         d_add(dentry, inode);
458                         smb_renew_times(dentry);
459                         error = 0;
460                 }
461         }
462         unlock_kernel();
463 out:
464         return ERR_PTR(error);
465 }
466
467 /*
468  * This code is common to all routines creating a new inode.
469  */
470 static int
471 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
472 {
473         struct smb_sb_info *server = server_from_dentry(dentry);
474         struct inode *inode;
475         int error;
476         struct smb_fattr fattr;
477
478         VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
479
480         error = smb_proc_getattr(dentry, &fattr);
481         if (error)
482                 goto out_close;
483
484         smb_renew_times(dentry);
485         fattr.f_ino = iunique(dentry->d_sb, 2);
486         inode = smb_iget(dentry->d_sb, &fattr);
487         if (!inode)
488                 goto out_no_inode;
489
490         if (have_id) {
491                 struct smb_inode_info *ei = SMB_I(inode);
492                 ei->fileid = fileid;
493                 ei->access = SMB_O_RDWR;
494                 ei->open = server->generation;
495         }
496         d_instantiate(dentry, inode);
497 out:
498         return error;
499
500 out_no_inode:
501         error = -EACCES;
502 out_close:
503         if (have_id) {
504                 PARANOIA("%s/%s failed, error=%d, closing %u\n",
505                          DENTRY_PATH(dentry), error, fileid);
506                 smb_close_fileid(dentry, fileid);
507         }
508         goto out;
509 }
510
511 /* N.B. How should the mode argument be used? */
512 static int
513 smb_create(struct inode *dir, struct dentry *dentry, int mode,
514                 struct nameidata *nd)
515 {
516         struct smb_sb_info *server = server_from_dentry(dentry);
517         __u16 fileid;
518         int error;
519         struct iattr attr;
520
521         VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
522
523         lock_kernel();
524         smb_invalid_dir_cache(dir);
525         error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
526         if (!error) {
527                 if (server->opt.capabilities & SMB_CAP_UNIX) {
528                         /* Set attributes for new file */
529                         attr.ia_valid = ATTR_MODE;
530                         attr.ia_mode = mode;
531                         error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
532                 }
533                 error = smb_instantiate(dentry, fileid, 1);
534         } else {
535                 PARANOIA("%s/%s failed, error=%d\n",
536                          DENTRY_PATH(dentry), error);
537         }
538         unlock_kernel();
539         return error;
540 }
541
542 /* N.B. How should the mode argument be used? */
543 static int
544 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
545 {
546         struct smb_sb_info *server = server_from_dentry(dentry);
547         int error;
548         struct iattr attr;
549
550         lock_kernel();
551         smb_invalid_dir_cache(dir);
552         error = smb_proc_mkdir(dentry);
553         if (!error) {
554                 if (server->opt.capabilities & SMB_CAP_UNIX) {
555                         /* Set attributes for new directory */
556                         attr.ia_valid = ATTR_MODE;
557                         attr.ia_mode = mode;
558                         error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
559                 }
560                 error = smb_instantiate(dentry, 0, 0);
561         }
562         unlock_kernel();
563         return error;
564 }
565
566 static int
567 smb_rmdir(struct inode *dir, struct dentry *dentry)
568 {
569         struct inode *inode = dentry->d_inode;
570         int error;
571
572         /*
573          * Close the directory if it's open.
574          */
575         lock_kernel();
576         smb_close(inode);
577
578         /*
579          * Check that nobody else is using the directory..
580          */
581         error = -EBUSY;
582         if (!d_unhashed(dentry))
583                 goto out;
584
585         smb_invalid_dir_cache(dir);
586         error = smb_proc_rmdir(dentry);
587
588 out:
589         unlock_kernel();
590         return error;
591 }
592
593 static int
594 smb_unlink(struct inode *dir, struct dentry *dentry)
595 {
596         int error;
597
598         /*
599          * Close the file if it's open.
600          */
601         lock_kernel();
602         smb_close(dentry->d_inode);
603
604         smb_invalid_dir_cache(dir);
605         error = smb_proc_unlink(dentry);
606         if (!error)
607                 smb_renew_times(dentry);
608         unlock_kernel();
609         return error;
610 }
611
612 static int
613 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
614            struct inode *new_dir, struct dentry *new_dentry)
615 {
616         int error;
617
618         /*
619          * Close any open files, and check whether to delete the
620          * target before attempting the rename.
621          */
622         lock_kernel();
623         if (old_dentry->d_inode)
624                 smb_close(old_dentry->d_inode);
625         if (new_dentry->d_inode) {
626                 smb_close(new_dentry->d_inode);
627                 error = smb_proc_unlink(new_dentry);
628                 if (error) {
629                         VERBOSE("unlink %s/%s, error=%d\n",
630                                 DENTRY_PATH(new_dentry), error);
631                         goto out;
632                 }
633                 /* FIXME */
634                 d_delete(new_dentry);
635         }
636
637         smb_invalid_dir_cache(old_dir);
638         smb_invalid_dir_cache(new_dir);
639         error = smb_proc_mv(old_dentry, new_dentry);
640         if (!error) {
641                 smb_renew_times(old_dentry);
642                 smb_renew_times(new_dentry);
643         }
644 out:
645         unlock_kernel();
646         return error;
647 }
648
649 /*
650  * FIXME: samba servers won't let you create device nodes unless uid/gid
651  * matches the connection credentials (and we don't know which those are ...)
652  */
653 static int
654 smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
655 {
656         int error;
657         struct iattr attr;
658
659         attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
660         attr.ia_mode = mode;
661         attr.ia_uid = current->euid;
662         attr.ia_gid = current->egid;
663
664         if (!new_valid_dev(dev))
665                 return -EINVAL;
666
667         smb_invalid_dir_cache(dir);
668         error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
669         if (!error) {
670                 error = smb_instantiate(dentry, 0, 0);
671         }
672         return error;
673 }
674
675 /*
676  * dentry = existing file
677  * new_dentry = new file
678  */
679 static int
680 smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
681 {
682         int error;
683
684         DEBUG1("smb_link old=%s/%s new=%s/%s\n",
685                DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
686         smb_invalid_dir_cache(dir);
687         error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
688         if (!error) {
689                 smb_renew_times(dentry);
690                 error = smb_instantiate(new_dentry, 0, 0);
691         }
692         return error;
693 }