Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / usb / core / inode.c
1 /*****************************************************************************/
2
3 /*
4  *      inode.c  --  Inode/Dentry functions for the USB device file system.
5  *
6  *      Copyright (C) 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
7  *      Copyright (C) 2001,2002,2004 Greg Kroah-Hartman (greg@kroah.com)
8  *
9  *      This program is free software; you can redistribute it and/or modify
10  *      it under the terms of the GNU General Public License as published by
11  *      the Free Software Foundation; either version 2 of the License, or
12  *      (at your option) any later version.
13  *
14  *      This program is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *      GNU General Public License for more details.
18  *
19  *      You should have received a copy of the GNU General Public License
20  *      along with this program; if not, write to the Free Software
21  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  *  History:
24  *   0.1  04.01.2000  Created
25  *   0.2  10.12.2001  converted to use the vfs layer better
26  */
27
28 /*****************************************************************************/
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/fs.h>
33 #include <linux/mount.h>
34 #include <linux/pagemap.h>
35 #include <linux/init.h>
36 #include <linux/proc_fs.h>
37 #include <linux/usb.h>
38 #include <linux/namei.h>
39 #include <linux/usbdevice_fs.h>
40 #include <linux/smp_lock.h>
41 #include <linux/parser.h>
42 #include <asm/byteorder.h>
43 #include "usb.h"
44
45 static struct super_operations usbfs_ops;
46 static struct file_operations default_file_operations;
47 static struct inode_operations usbfs_dir_inode_operations;
48 static struct vfsmount *usbfs_mount;
49 static int usbfs_mount_count;   /* = 0 */
50 static int ignore_mount = 0;
51
52 static struct dentry *devices_usbfs_dentry;
53 static int num_buses;   /* = 0 */
54
55 static uid_t devuid;    /* = 0 */
56 static uid_t busuid;    /* = 0 */
57 static uid_t listuid;   /* = 0 */
58 static gid_t devgid;    /* = 0 */
59 static gid_t busgid;    /* = 0 */
60 static gid_t listgid;   /* = 0 */
61 static umode_t devmode = S_IWUSR | S_IRUGO;
62 static umode_t busmode = S_IXUGO | S_IRUGO;
63 static umode_t listmode = S_IRUGO;
64
65 enum {
66         Opt_devuid, Opt_devgid, Opt_devmode,
67         Opt_busuid, Opt_busgid, Opt_busmode,
68         Opt_listuid, Opt_listgid, Opt_listmode,
69         Opt_err,
70 };
71
72 static match_table_t tokens = {
73         {Opt_devuid, "devuid=%u"},
74         {Opt_devgid, "devgid=%u"},
75         {Opt_devmode, "devmode=%o"},
76         {Opt_busuid, "busuid=%u"},
77         {Opt_busgid, "busgid=%u"},
78         {Opt_busmode, "busmode=%o"},
79         {Opt_listuid, "listuid=%u"},
80         {Opt_listgid, "listgid=%u"},
81         {Opt_listmode, "listmode=%o"},
82         {Opt_err, NULL}
83 };
84
85 static int parse_options(struct super_block *s, char *data)
86 {
87         char *p;
88         int option;
89
90         /* (re)set to defaults. */
91         devuid = 0;
92         busuid = 0;
93         listuid = 0;
94         devgid = 0;
95         busgid = 0;
96         listgid = 0;
97         devmode = S_IWUSR | S_IRUGO;
98         busmode = S_IXUGO | S_IRUGO;
99         listmode = S_IRUGO;
100
101         while ((p = strsep(&data, ",")) != NULL) {
102                 substring_t args[MAX_OPT_ARGS];
103                 int token;
104                 if (!*p)
105                         continue;
106
107                 token = match_token(p, tokens, args);
108                 switch (token) {
109                 case Opt_devuid:
110                         if (match_int(&args[0], &option))
111                                return -EINVAL;
112                         devuid = option;
113                         break;
114                 case Opt_devgid:
115                         if (match_int(&args[0], &option))
116                                return -EINVAL;
117                         devgid = option;
118                         break;
119                 case Opt_devmode:
120                         if (match_octal(&args[0], &option))
121                                 return -EINVAL;
122                         devmode = option & S_IRWXUGO;
123                         break;
124                 case Opt_busuid:
125                         if (match_int(&args[0], &option))
126                                return -EINVAL;
127                         busuid = option;
128                         break;
129                 case Opt_busgid:
130                         if (match_int(&args[0], &option))
131                                return -EINVAL;
132                         busgid = option;
133                         break;
134                 case Opt_busmode:
135                         if (match_octal(&args[0], &option))
136                                 return -EINVAL;
137                         busmode = option & S_IRWXUGO;
138                         break;
139                 case Opt_listuid:
140                         if (match_int(&args[0], &option))
141                                return -EINVAL;
142                         listuid = option;
143                         break;
144                 case Opt_listgid:
145                         if (match_int(&args[0], &option))
146                                return -EINVAL;
147                         listgid = option;
148                         break;
149                 case Opt_listmode:
150                         if (match_octal(&args[0], &option))
151                                 return -EINVAL;
152                         listmode = option & S_IRWXUGO;
153                         break;
154                 default:
155                         err("usbfs: unrecognised mount option \"%s\" "
156                             "or missing value\n", p);
157                         return -EINVAL;
158                 }
159         }
160
161         return 0;
162 }
163
164 static void update_special(struct dentry *special)
165 {
166         special->d_inode->i_uid = listuid;
167         special->d_inode->i_gid = listgid;
168         special->d_inode->i_mode = S_IFREG | listmode;
169 }
170
171 static void update_dev(struct dentry *dev)
172 {
173         dev->d_inode->i_uid = devuid;
174         dev->d_inode->i_gid = devgid;
175         dev->d_inode->i_mode = S_IFREG | devmode;
176 }
177
178 static void update_bus(struct dentry *bus)
179 {
180         struct dentry *dev = NULL;
181
182         bus->d_inode->i_uid = busuid;
183         bus->d_inode->i_gid = busgid;
184         bus->d_inode->i_mode = S_IFDIR | busmode;
185
186         down(&bus->d_inode->i_sem);
187
188         list_for_each_entry(dev, &bus->d_subdirs, d_child)
189                 if (dev->d_inode)
190                         update_dev(dev);
191
192         up(&bus->d_inode->i_sem);
193 }
194
195 static void update_sb(struct super_block *sb)
196 {
197         struct dentry *root = sb->s_root;
198         struct dentry *bus = NULL;
199
200         if (!root)
201                 return;
202
203         down(&root->d_inode->i_sem);
204
205         list_for_each_entry(bus, &root->d_subdirs, d_child) {
206                 if (bus->d_inode) {
207                         switch (S_IFMT & bus->d_inode->i_mode) {
208                         case S_IFDIR:
209                                 update_bus(bus);
210                                 break;
211                         case S_IFREG:
212                                 update_special(bus);
213                                 break;
214                         default:
215                                 warn("Unknown node %s mode %x found on remount!\n",bus->d_name.name,bus->d_inode->i_mode);
216                                 break;
217                         }
218                 }
219         }
220
221         up(&root->d_inode->i_sem);
222 }
223
224 static int remount(struct super_block *sb, int *flags, char *data)
225 {
226         /* If this is not a real mount,
227          * i.e. it's a simple_pin_fs from create_special_files,
228          * then ignore it.
229          */
230         if (ignore_mount)
231                 return 0;
232
233         if (parse_options(sb, data)) {
234                 warn("usbfs: mount parameter error:");
235                 return -EINVAL;
236         }
237
238         if (usbfs_mount && usbfs_mount->mnt_sb)
239                 update_sb(usbfs_mount->mnt_sb);
240
241         return 0;
242 }
243
244 static struct inode *usbfs_get_inode (struct super_block *sb, int mode, dev_t dev)
245 {
246         struct inode *inode = new_inode(sb);
247
248         if (inode) {
249                 inode->i_mode = mode;
250                 inode->i_uid = current->fsuid;
251                 inode->i_gid = current->fsgid;
252                 inode->i_blksize = PAGE_CACHE_SIZE;
253                 inode->i_blocks = 0;
254                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
255                 switch (mode & S_IFMT) {
256                 default:
257                         init_special_inode(inode, mode, dev);
258                         break;
259                 case S_IFREG:
260                         inode->i_fop = &default_file_operations;
261                         break;
262                 case S_IFDIR:
263                         inode->i_op = &usbfs_dir_inode_operations;
264                         inode->i_fop = &simple_dir_operations;
265
266                         /* directory inodes start off with i_nlink == 2 (for "." entry) */
267                         inode->i_nlink++;
268                         break;
269                 }
270         }
271         return inode; 
272 }
273
274 /* SMP-safe */
275 static int usbfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
276                         dev_t dev)
277 {
278         struct inode *inode = usbfs_get_inode(dir->i_sb, mode, dev);
279         int error = -EPERM;
280
281         if (dentry->d_inode)
282                 return -EEXIST;
283
284         if (inode) {
285                 d_instantiate(dentry, inode);
286                 dget(dentry);
287                 error = 0;
288         }
289         return error;
290 }
291
292 static int usbfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
293 {
294         int res;
295
296         mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
297         res = usbfs_mknod (dir, dentry, mode, 0);
298         if (!res)
299                 dir->i_nlink++;
300         return res;
301 }
302
303 static int usbfs_create (struct inode *dir, struct dentry *dentry, int mode)
304 {
305         mode = (mode & S_IALLUGO) | S_IFREG;
306         return usbfs_mknod (dir, dentry, mode, 0);
307 }
308
309 static inline int usbfs_positive (struct dentry *dentry)
310 {
311         return dentry->d_inode && !d_unhashed(dentry);
312 }
313
314 static int usbfs_empty (struct dentry *dentry)
315 {
316         struct list_head *list;
317
318         spin_lock(&dcache_lock);
319
320         list_for_each(list, &dentry->d_subdirs) {
321                 struct dentry *de = list_entry(list, struct dentry, d_child);
322                 if (usbfs_positive(de)) {
323                         spin_unlock(&dcache_lock);
324                         return 0;
325                 }
326         }
327
328         spin_unlock(&dcache_lock);
329         return 1;
330 }
331
332 static int usbfs_unlink (struct inode *dir, struct dentry *dentry)
333 {
334         struct inode *inode = dentry->d_inode;
335         down(&inode->i_sem);
336         dentry->d_inode->i_nlink--;
337         dput(dentry);
338         up(&inode->i_sem);
339         d_delete(dentry);
340         return 0;
341 }
342
343 static int usbfs_rmdir(struct inode *dir, struct dentry *dentry)
344 {
345         int error = -ENOTEMPTY;
346         struct inode * inode = dentry->d_inode;
347
348         down(&inode->i_sem);
349         dentry_unhash(dentry);
350         if (usbfs_empty(dentry)) {
351                 dentry->d_inode->i_nlink -= 2;
352                 dput(dentry);
353                 inode->i_flags |= S_DEAD;
354                 dir->i_nlink--;
355                 error = 0;
356         }
357         up(&inode->i_sem);
358         if (!error)
359                 d_delete(dentry);
360         dput(dentry);
361         return error;
362 }
363
364
365 /* default file operations */
366 static ssize_t default_read_file (struct file *file, char __user *buf,
367                                   size_t count, loff_t *ppos)
368 {
369         return 0;
370 }
371
372 static ssize_t default_write_file (struct file *file, const char __user *buf,
373                                    size_t count, loff_t *ppos)
374 {
375         return count;
376 }
377
378 static loff_t default_file_lseek (struct file *file, loff_t offset, int orig)
379 {
380         loff_t retval = -EINVAL;
381
382         down(&file->f_dentry->d_inode->i_sem);
383         switch(orig) {
384         case 0:
385                 if (offset > 0) {
386                         file->f_pos = offset;
387                         retval = file->f_pos;
388                 } 
389                 break;
390         case 1:
391                 if ((offset + file->f_pos) > 0) {
392                         file->f_pos += offset;
393                         retval = file->f_pos;
394                 } 
395                 break;
396         default:
397                 break;
398         }
399         up(&file->f_dentry->d_inode->i_sem);
400         return retval;
401 }
402
403 static int default_open (struct inode *inode, struct file *file)
404 {
405         if (inode->u.generic_ip)
406                 file->private_data = inode->u.generic_ip;
407
408         return 0;
409 }
410
411 static struct file_operations default_file_operations = {
412         .read =         default_read_file,
413         .write =        default_write_file,
414         .open =         default_open,
415         .llseek =       default_file_lseek,
416 };
417
418 static struct inode_operations usbfs_dir_inode_operations = {
419         .lookup =       simple_lookup,
420 };
421
422 static struct super_operations usbfs_ops = {
423         .statfs =       simple_statfs,
424         .drop_inode =   generic_delete_inode,
425         .remount_fs =   remount,
426 };
427
428 static int usbfs_fill_super(struct super_block *sb, void *data, int silent)
429 {
430         struct inode *inode;
431         struct dentry *root;
432
433         sb->s_blocksize = PAGE_CACHE_SIZE;
434         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
435         sb->s_magic = USBDEVICE_SUPER_MAGIC;
436         sb->s_op = &usbfs_ops;
437         sb->s_time_gran = 1;
438         inode = usbfs_get_inode(sb, S_IFDIR | 0755, 0);
439
440         if (!inode) {
441                 dbg("%s: could not get inode!",__FUNCTION__);
442                 return -ENOMEM;
443         }
444
445         root = d_alloc_root(inode);
446         if (!root) {
447                 dbg("%s: could not get root dentry!",__FUNCTION__);
448                 iput(inode);
449                 return -ENOMEM;
450         }
451         sb->s_root = root;
452         return 0;
453 }
454
455 static struct dentry * get_dentry(struct dentry *parent, const char *name)
456 {               
457         struct qstr qstr;
458
459         qstr.name = name;
460         qstr.len = strlen(name);
461         qstr.hash = full_name_hash(name,qstr.len);
462         return lookup_hash(&qstr,parent);
463 }               
464
465
466 /*
467  * fs_create_by_name - create a file, given a name
468  * @name:       name of file
469  * @mode:       type of file
470  * @parent:     dentry of directory to create it in
471  * @dentry:     resulting dentry of file
472  *
473  * This function handles both regular files and directories.
474  */
475 static int fs_create_by_name (const char *name, mode_t mode,
476                               struct dentry *parent, struct dentry **dentry)
477 {
478         int error = 0;
479
480         /* If the parent is not specified, we create it in the root.
481          * We need the root dentry to do this, which is in the super 
482          * block. A pointer to that is in the struct vfsmount that we
483          * have around.
484          */
485         if (!parent ) {
486                 if (usbfs_mount && usbfs_mount->mnt_sb) {
487                         parent = usbfs_mount->mnt_sb->s_root;
488                 }
489         }
490
491         if (!parent) {
492                 dbg("Ah! can not find a parent!");
493                 return -EFAULT;
494         }
495
496         *dentry = NULL;
497         down(&parent->d_inode->i_sem);
498         *dentry = get_dentry (parent, name);
499         if (!IS_ERR(dentry)) {
500                 if ((mode & S_IFMT) == S_IFDIR)
501                         error = usbfs_mkdir (parent->d_inode, *dentry, mode);
502                 else 
503                         error = usbfs_create (parent->d_inode, *dentry, mode);
504         } else
505                 error = PTR_ERR(dentry);
506         up(&parent->d_inode->i_sem);
507
508         return error;
509 }
510
511 static struct dentry *fs_create_file (const char *name, mode_t mode,
512                                       struct dentry *parent, void *data,
513                                       struct file_operations *fops,
514                                       uid_t uid, gid_t gid)
515 {
516         struct dentry *dentry;
517         int error;
518
519         dbg("creating file '%s'",name);
520
521         error = fs_create_by_name (name, mode, parent, &dentry);
522         if (error) {
523                 dentry = NULL;
524         } else {
525                 if (dentry->d_inode) {
526                         if (data)
527                                 dentry->d_inode->u.generic_ip = data;
528                         if (fops)
529                                 dentry->d_inode->i_fop = fops;
530                         dentry->d_inode->i_uid = uid;
531                         dentry->d_inode->i_gid = gid;
532                 }
533         }
534
535         return dentry;
536 }
537
538 static void fs_remove_file (struct dentry *dentry)
539 {
540         struct dentry *parent = dentry->d_parent;
541         
542         if (!parent || !parent->d_inode)
543                 return;
544
545         down(&parent->d_inode->i_sem);
546         if (usbfs_positive(dentry)) {
547                 if (dentry->d_inode) {
548                         if (S_ISDIR(dentry->d_inode->i_mode))
549                                 usbfs_rmdir(parent->d_inode, dentry);
550                         else
551                                 usbfs_unlink(parent->d_inode, dentry);
552                 dput(dentry);
553                 }
554         }
555         up(&parent->d_inode->i_sem);
556 }
557
558 /* --------------------------------------------------------------------- */
559
560 static struct super_block *usb_get_sb(struct file_system_type *fs_type,
561         int flags, const char *dev_name, void *data)
562 {
563         return get_sb_single(fs_type, flags, data, usbfs_fill_super);
564 }
565
566 static struct file_system_type usb_fs_type = {
567         .owner =        THIS_MODULE,
568         .name =         "usbfs",
569         .get_sb =       usb_get_sb,
570         .kill_sb =      kill_litter_super,
571 };
572
573 /* --------------------------------------------------------------------- */
574
575 static int create_special_files (void)
576 {
577         struct dentry *parent;
578         int retval;
579
580         /* the simple_pin_fs calls will call remount with no options
581          * without this flag that would overwrite the real mount options (if any)
582          */
583         ignore_mount = 1;
584
585         /* create the devices special file */
586         retval = simple_pin_fs("usbfs", &usbfs_mount, &usbfs_mount_count);
587         if (retval) {
588                 err ("Unable to get usbfs mount");
589                 goto exit;
590         }
591
592         ignore_mount = 0;
593
594         parent = usbfs_mount->mnt_sb->s_root;
595         devices_usbfs_dentry = fs_create_file ("devices",
596                                                listmode | S_IFREG, parent,
597                                                NULL, &usbfs_devices_fops,
598                                                listuid, listgid);
599         if (devices_usbfs_dentry == NULL) {
600                 err ("Unable to create devices usbfs file");
601                 retval = -ENODEV;
602                 goto error_clean_mounts;
603         }
604
605         goto exit;
606         
607 error_clean_mounts:
608         simple_release_fs(&usbfs_mount, &usbfs_mount_count);
609 exit:
610         return retval;
611 }
612
613 static void remove_special_files (void)
614 {
615         if (devices_usbfs_dentry)
616                 fs_remove_file (devices_usbfs_dentry);
617         devices_usbfs_dentry = NULL;
618         simple_release_fs(&usbfs_mount, &usbfs_mount_count);
619 }
620
621 void usbfs_update_special (void)
622 {
623         struct inode *inode;
624
625         if (devices_usbfs_dentry) {
626                 inode = devices_usbfs_dentry->d_inode;
627                 if (inode)
628                         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
629         }
630 }
631
632 void usbfs_add_bus(struct usb_bus *bus)
633 {
634         struct dentry *parent;
635         char name[8];
636         int retval;
637
638         /* create the special files if this is the first bus added */
639         if (num_buses == 0) {
640                 retval = create_special_files();
641                 if (retval)
642                         return;
643         }
644         ++num_buses;
645
646         sprintf (name, "%03d", bus->busnum);
647
648         parent = usbfs_mount->mnt_sb->s_root;
649         bus->usbfs_dentry = fs_create_file (name, busmode | S_IFDIR, parent,
650                                             bus, NULL, busuid, busgid);
651         if (bus->usbfs_dentry == NULL) {
652                 err ("error creating usbfs bus entry");
653                 return;
654         }
655
656         usbfs_update_special();
657         usbfs_conn_disc_event();
658 }
659
660 void usbfs_remove_bus(struct usb_bus *bus)
661 {
662         if (bus->usbfs_dentry) {
663                 fs_remove_file (bus->usbfs_dentry);
664                 bus->usbfs_dentry = NULL;
665         }
666
667         --num_buses;
668         if (num_buses <= 0) {
669                 remove_special_files();
670                 num_buses = 0;
671         }
672
673         usbfs_update_special();
674         usbfs_conn_disc_event();
675 }
676
677 void usbfs_add_device(struct usb_device *dev)
678 {
679         char name[8];
680         int i;
681         int i_size;
682
683         sprintf (name, "%03d", dev->devnum);
684         dev->usbfs_dentry = fs_create_file (name, devmode | S_IFREG,
685                                             dev->bus->usbfs_dentry, dev,
686                                             &usbfs_device_file_operations,
687                                             devuid, devgid);
688         if (dev->usbfs_dentry == NULL) {
689                 err ("error creating usbfs device entry");
690                 return;
691         }
692
693         /* Set the size of the device's file to be
694          * equal to the size of the device descriptors. */
695         i_size = sizeof (struct usb_device_descriptor);
696         for (i = 0; i < dev->descriptor.bNumConfigurations; ++i) {
697                 struct usb_config_descriptor *config =
698                         (struct usb_config_descriptor *)dev->rawdescriptors[i];
699                 i_size += le16_to_cpu(config->wTotalLength);
700         }
701         if (dev->usbfs_dentry->d_inode)
702                 dev->usbfs_dentry->d_inode->i_size = i_size;
703
704         usbfs_update_special();
705         usbfs_conn_disc_event();
706 }
707
708 void usbfs_remove_device(struct usb_device *dev)
709 {
710         struct dev_state *ds;
711         struct siginfo sinfo;
712
713         if (dev->usbfs_dentry) {
714                 fs_remove_file (dev->usbfs_dentry);
715                 dev->usbfs_dentry = NULL;
716         }
717         while (!list_empty(&dev->filelist)) {
718                 ds = list_entry(dev->filelist.next, struct dev_state, list);
719                 wake_up_all(&ds->wait);
720                 list_del_init(&ds->list);
721                 if (ds->discsignr) {
722                         sinfo.si_signo = SIGPIPE;
723                         sinfo.si_errno = EPIPE;
724                         sinfo.si_code = SI_ASYNCIO;
725                         sinfo.si_addr = ds->disccontext;
726                         send_sig_info(ds->discsignr, &sinfo, ds->disctask);
727                 }
728         }
729         usbfs_update_special();
730         usbfs_conn_disc_event();
731 }
732
733 /* --------------------------------------------------------------------- */
734
735 static struct proc_dir_entry *usbdir = NULL;
736
737 int __init usbfs_init(void)
738 {
739         int retval;
740
741         retval = usb_register(&usbfs_driver);
742         if (retval)
743                 return retval;
744
745         retval = register_filesystem(&usb_fs_type);
746         if (retval) {
747                 usb_deregister(&usbfs_driver);
748                 return retval;
749         }
750
751         /* create mount point for usbfs */
752         usbdir = proc_mkdir("usb", proc_bus);
753
754         return 0;
755 }
756
757 void usbfs_cleanup(void)
758 {
759         usb_deregister(&usbfs_driver);
760         unregister_filesystem(&usb_fs_type);
761         if (usbdir)
762                 remove_proc_entry("usb", proc_bus);
763 }
764