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