]> Pileus Git - ~andy/linux/blob - drivers/staging/smbfs/dir.c
Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[~andy/linux] / 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         struct smb_sb_info *server = server_from_dentry(dentry);
407
408         if (server->mnt->flags & SMB_MOUNT_CASE)
409                 d_set_d_op(dentry, &smbfs_dentry_operations_case);
410         else
411                 d_set_d_op(dentry, &smbfs_dentry_operations);
412         dentry->d_time = jiffies;
413 }
414
415
416 /*
417  * Whenever a lookup succeeds, we know the parent directories
418  * are all valid, so we want to update the dentry timestamps.
419  * N.B. Move this to dcache?
420  */
421 void
422 smb_renew_times(struct dentry * dentry)
423 {
424         dget(dentry);
425         dentry->d_time = jiffies;
426
427         while (!IS_ROOT(dentry)) {
428                 struct dentry *parent = dget_parent(dentry);
429                 dput(dentry);
430                 dentry = parent;
431
432                 dentry->d_time = jiffies;
433         }
434         dput(dentry);
435 }
436
437 static struct dentry *
438 smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
439 {
440         struct smb_fattr finfo;
441         struct inode *inode;
442         int error;
443         struct smb_sb_info *server;
444
445         error = -ENAMETOOLONG;
446         if (dentry->d_name.len > SMB_MAXNAMELEN)
447                 goto out;
448
449         /* Do not allow lookup of names with backslashes in */
450         error = -EINVAL;
451         if (memchr(dentry->d_name.name, '\\', dentry->d_name.len))
452                 goto out;
453
454         lock_kernel();
455         error = smb_proc_getattr(dentry, &finfo);
456 #ifdef SMBFS_PARANOIA
457         if (error && error != -ENOENT)
458                 PARANOIA("find %s/%s failed, error=%d\n",
459                          DENTRY_PATH(dentry), error);
460 #endif
461
462         inode = NULL;
463         if (error == -ENOENT)
464                 goto add_entry;
465         if (!error) {
466                 error = -EACCES;
467                 finfo.f_ino = iunique(dentry->d_sb, 2);
468                 inode = smb_iget(dir->i_sb, &finfo);
469                 if (inode) {
470         add_entry:
471                         server = server_from_dentry(dentry);
472                         if (server->mnt->flags & SMB_MOUNT_CASE)
473                                 d_set_d_op(dentry, &smbfs_dentry_operations_case);
474                         else
475                                 d_set_d_op(dentry, &smbfs_dentry_operations);
476
477                         d_add(dentry, inode);
478                         smb_renew_times(dentry);
479                         error = 0;
480                 }
481         }
482         unlock_kernel();
483 out:
484         return ERR_PTR(error);
485 }
486
487 /*
488  * This code is common to all routines creating a new inode.
489  */
490 static int
491 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
492 {
493         struct smb_sb_info *server = server_from_dentry(dentry);
494         struct inode *inode;
495         int error;
496         struct smb_fattr fattr;
497
498         VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
499
500         error = smb_proc_getattr(dentry, &fattr);
501         if (error)
502                 goto out_close;
503
504         smb_renew_times(dentry);
505         fattr.f_ino = iunique(dentry->d_sb, 2);
506         inode = smb_iget(dentry->d_sb, &fattr);
507         if (!inode)
508                 goto out_no_inode;
509
510         if (have_id) {
511                 struct smb_inode_info *ei = SMB_I(inode);
512                 ei->fileid = fileid;
513                 ei->access = SMB_O_RDWR;
514                 ei->open = server->generation;
515         }
516         d_instantiate(dentry, inode);
517 out:
518         return error;
519
520 out_no_inode:
521         error = -EACCES;
522 out_close:
523         if (have_id) {
524                 PARANOIA("%s/%s failed, error=%d, closing %u\n",
525                          DENTRY_PATH(dentry), error, fileid);
526                 smb_close_fileid(dentry, fileid);
527         }
528         goto out;
529 }
530
531 /* N.B. How should the mode argument be used? */
532 static int
533 smb_create(struct inode *dir, struct dentry *dentry, int mode,
534                 struct nameidata *nd)
535 {
536         struct smb_sb_info *server = server_from_dentry(dentry);
537         __u16 fileid;
538         int error;
539         struct iattr attr;
540
541         VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
542
543         lock_kernel();
544         smb_invalid_dir_cache(dir);
545         error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
546         if (!error) {
547                 if (server->opt.capabilities & SMB_CAP_UNIX) {
548                         /* Set attributes for new file */
549                         attr.ia_valid = ATTR_MODE;
550                         attr.ia_mode = mode;
551                         error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
552                 }
553                 error = smb_instantiate(dentry, fileid, 1);
554         } else {
555                 PARANOIA("%s/%s failed, error=%d\n",
556                          DENTRY_PATH(dentry), error);
557         }
558         unlock_kernel();
559         return error;
560 }
561
562 /* N.B. How should the mode argument be used? */
563 static int
564 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
565 {
566         struct smb_sb_info *server = server_from_dentry(dentry);
567         int error;
568         struct iattr attr;
569
570         lock_kernel();
571         smb_invalid_dir_cache(dir);
572         error = smb_proc_mkdir(dentry);
573         if (!error) {
574                 if (server->opt.capabilities & SMB_CAP_UNIX) {
575                         /* Set attributes for new directory */
576                         attr.ia_valid = ATTR_MODE;
577                         attr.ia_mode = mode;
578                         error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
579                 }
580                 error = smb_instantiate(dentry, 0, 0);
581         }
582         unlock_kernel();
583         return error;
584 }
585
586 static int
587 smb_rmdir(struct inode *dir, struct dentry *dentry)
588 {
589         struct inode *inode = dentry->d_inode;
590         int error;
591
592         /*
593          * Close the directory if it's open.
594          */
595         lock_kernel();
596         smb_close(inode);
597
598         /*
599          * Check that nobody else is using the directory..
600          */
601         error = -EBUSY;
602         if (!d_unhashed(dentry))
603                 goto out;
604
605         smb_invalid_dir_cache(dir);
606         error = smb_proc_rmdir(dentry);
607
608 out:
609         unlock_kernel();
610         return error;
611 }
612
613 static int
614 smb_unlink(struct inode *dir, struct dentry *dentry)
615 {
616         int error;
617
618         /*
619          * Close the file if it's open.
620          */
621         lock_kernel();
622         smb_close(dentry->d_inode);
623
624         smb_invalid_dir_cache(dir);
625         error = smb_proc_unlink(dentry);
626         if (!error)
627                 smb_renew_times(dentry);
628         unlock_kernel();
629         return error;
630 }
631
632 static int
633 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
634            struct inode *new_dir, struct dentry *new_dentry)
635 {
636         int error;
637
638         /*
639          * Close any open files, and check whether to delete the
640          * target before attempting the rename.
641          */
642         lock_kernel();
643         if (old_dentry->d_inode)
644                 smb_close(old_dentry->d_inode);
645         if (new_dentry->d_inode) {
646                 smb_close(new_dentry->d_inode);
647                 error = smb_proc_unlink(new_dentry);
648                 if (error) {
649                         VERBOSE("unlink %s/%s, error=%d\n",
650                                 DENTRY_PATH(new_dentry), error);
651                         goto out;
652                 }
653                 /* FIXME */
654                 d_delete(new_dentry);
655         }
656
657         smb_invalid_dir_cache(old_dir);
658         smb_invalid_dir_cache(new_dir);
659         error = smb_proc_mv(old_dentry, new_dentry);
660         if (!error) {
661                 smb_renew_times(old_dentry);
662                 smb_renew_times(new_dentry);
663         }
664 out:
665         unlock_kernel();
666         return error;
667 }
668
669 /*
670  * FIXME: samba servers won't let you create device nodes unless uid/gid
671  * matches the connection credentials (and we don't know which those are ...)
672  */
673 static int
674 smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
675 {
676         int error;
677         struct iattr attr;
678
679         attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
680         attr.ia_mode = mode;
681         current_euid_egid(&attr.ia_uid, &attr.ia_gid);
682
683         if (!new_valid_dev(dev))
684                 return -EINVAL;
685
686         smb_invalid_dir_cache(dir);
687         error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
688         if (!error) {
689                 error = smb_instantiate(dentry, 0, 0);
690         }
691         return error;
692 }
693
694 /*
695  * dentry = existing file
696  * new_dentry = new file
697  */
698 static int
699 smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
700 {
701         int error;
702
703         DEBUG1("smb_link old=%s/%s new=%s/%s\n",
704                DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
705         smb_invalid_dir_cache(dir);
706         error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
707         if (!error) {
708                 smb_renew_times(dentry);
709                 error = smb_instantiate(new_dentry, 0, 0);
710         }
711         return error;
712 }