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