patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / devfs / base.c
1 /*  devfs (Device FileSystem) driver.
2
3     Copyright (C) 1998-2002  Richard Gooch
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public
16     License along with this library; if not, write to the Free
17     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
20     The postal address is:
21       Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
22
23     ChangeLog
24
25     19980110   Richard Gooch <rgooch@atnf.csiro.au>
26                Original version.
27   v0.1
28     19980111   Richard Gooch <rgooch@atnf.csiro.au>
29                Created per-fs inode table rather than using inode->u.generic_ip
30   v0.2
31     19980111   Richard Gooch <rgooch@atnf.csiro.au>
32                Created .epoch inode which has a ctime of 0.
33                Fixed loss of named pipes when dentries lost.
34                Fixed loss of inode data when devfs_register() follows mknod().
35   v0.3
36     19980111   Richard Gooch <rgooch@atnf.csiro.au>
37                Fix for when compiling with CONFIG_KERNELD.
38     19980112   Richard Gooch <rgooch@atnf.csiro.au>
39                Fix for readdir() which sometimes didn't show entries.
40                Added <<tolerant>> option to <devfs_register>.
41   v0.4
42     19980113   Richard Gooch <rgooch@atnf.csiro.au>
43                Created <devfs_fill_file> function.
44   v0.5
45     19980115   Richard Gooch <rgooch@atnf.csiro.au>
46                Added subdirectory support. Major restructuring.
47     19980116   Richard Gooch <rgooch@atnf.csiro.au>
48                Fixed <find_by_dev> to not search major=0,minor=0.
49                Added symlink support.
50   v0.6
51     19980120   Richard Gooch <rgooch@atnf.csiro.au>
52                Created <devfs_mk_dir> function and support directory unregister
53     19980120   Richard Gooch <rgooch@atnf.csiro.au>
54                Auto-ownership uses real uid/gid rather than effective uid/gid.
55   v0.7
56     19980121   Richard Gooch <rgooch@atnf.csiro.au>
57                Supported creation of sockets.
58   v0.8
59     19980122   Richard Gooch <rgooch@atnf.csiro.au>
60                Added DEVFS_FL_HIDE_UNREG flag.
61                Interface change to <devfs_mk_symlink>.
62                Created <devfs_symlink> to support symlink(2).
63   v0.9
64     19980123   Richard Gooch <rgooch@atnf.csiro.au>
65                Added check to <devfs_fill_file> to check inode is in devfs.
66                Added optional traversal of symlinks.
67   v0.10
68     19980124   Richard Gooch <rgooch@atnf.csiro.au>
69                Created <devfs_get_flags> and <devfs_set_flags>.
70   v0.11
71     19980125   C. Scott Ananian <cananian@alumni.princeton.edu>
72                Created <devfs_find_handle>.
73     19980125   Richard Gooch <rgooch@atnf.csiro.au>
74                Allow removal of symlinks.
75   v0.12
76     19980125   Richard Gooch <rgooch@atnf.csiro.au>
77                Created <devfs_set_symlink_destination>.
78     19980126   Richard Gooch <rgooch@atnf.csiro.au>
79                Moved DEVFS_SUPER_MAGIC into header file.
80                Added DEVFS_FL_HIDE flag.
81                Created <devfs_get_maj_min>.
82                Created <devfs_get_handle_from_inode>.
83                Fixed minor bug in <find_by_dev>.
84     19980127   Richard Gooch <rgooch@atnf.csiro.au>
85                Changed interface to <find_by_dev>, <find_entry>,
86                <devfs_unregister>, <devfs_fill_file> and <devfs_find_handle>.
87                Fixed inode times when symlink created with symlink(2).
88   v0.13
89     19980129   C. Scott Ananian <cananian@alumni.princeton.edu>
90                Exported <devfs_set_symlink_destination>, <devfs_get_maj_min>
91                and <devfs_get_handle_from_inode>.
92     19980129   Richard Gooch <rgooch@atnf.csiro.au>
93                Created <devfs_unlink> to support unlink(2).
94   v0.14
95     19980129   Richard Gooch <rgooch@atnf.csiro.au>
96                Fixed kerneld support for entries in devfs subdirectories.
97     19980130   Richard Gooch <rgooch@atnf.csiro.au>
98                Bugfixes in <call_kerneld>.
99   v0.15
100     19980207   Richard Gooch <rgooch@atnf.csiro.au>
101                Call kerneld when looking up unregistered entries.
102   v0.16
103     19980326   Richard Gooch <rgooch@atnf.csiro.au>
104                Modified interface to <devfs_find_handle> for symlink traversal.
105   v0.17
106     19980331   Richard Gooch <rgooch@atnf.csiro.au>
107                Fixed persistence bug with device numbers for manually created
108                device files.
109                Fixed problem with recreating symlinks with different content.
110   v0.18
111     19980401   Richard Gooch <rgooch@atnf.csiro.au>
112                Changed to CONFIG_KMOD.
113                Hide entries which are manually unlinked.
114                Always invalidate devfs dentry cache when registering entries.
115                Created <devfs_rmdir> to support rmdir(2).
116                Ensure directories created by <devfs_mk_dir> are visible.
117   v0.19
118     19980402   Richard Gooch <rgooch@atnf.csiro.au>
119                Invalidate devfs dentry cache when making directories.
120                Invalidate devfs dentry cache when removing entries.
121                Fixed persistence bug with fifos.
122   v0.20
123     19980421   Richard Gooch <rgooch@atnf.csiro.au>
124                Print process command when debugging kerneld/kmod.
125                Added debugging for register/unregister/change operations.
126     19980422   Richard Gooch <rgooch@atnf.csiro.au>
127                Added "devfs=" boot options.
128   v0.21
129     19980426   Richard Gooch <rgooch@atnf.csiro.au>
130                No longer lock/unlock superblock in <devfs_put_super>.
131                Drop negative dentries when they are released.
132                Manage dcache more efficiently.
133   v0.22
134     19980427   Richard Gooch <rgooch@atnf.csiro.au>
135                Added DEVFS_FL_AUTO_DEVNUM flag.
136   v0.23
137     19980430   Richard Gooch <rgooch@atnf.csiro.au>
138                No longer set unnecessary methods.
139   v0.24
140     19980504   Richard Gooch <rgooch@atnf.csiro.au>
141                Added PID display to <call_kerneld> debugging message.
142                Added "after" debugging message to <call_kerneld>.
143     19980519   Richard Gooch <rgooch@atnf.csiro.au>
144                Added "diread" and "diwrite" boot options.
145     19980520   Richard Gooch <rgooch@atnf.csiro.au>
146                Fixed persistence problem with permissions.
147   v0.25
148     19980602   Richard Gooch <rgooch@atnf.csiro.au>
149                Support legacy device nodes.
150                Fixed bug where recreated inodes were hidden.
151   v0.26
152     19980602   Richard Gooch <rgooch@atnf.csiro.au>
153                Improved debugging in <get_vfs_inode>.
154     19980607   Richard Gooch <rgooch@atnf.csiro.au>
155                No longer free old dentries in <devfs_mk_dir>.
156                Free all dentries for a given entry when deleting inodes.
157   v0.27
158     19980627   Richard Gooch <rgooch@atnf.csiro.au>
159                Limit auto-device numbering to majors 128 to 239.
160   v0.28
161     19980629   Richard Gooch <rgooch@atnf.csiro.au>
162                Fixed inode times persistence problem.
163   v0.29
164     19980704   Richard Gooch <rgooch@atnf.csiro.au>
165                Fixed spelling in <devfs_readlink> debug.
166                Fixed bug in <devfs_setup> parsing "dilookup".
167   v0.30
168     19980705   Richard Gooch <rgooch@atnf.csiro.au>
169                Fixed devfs inode leak when manually recreating inodes.
170                Fixed permission persistence problem when recreating inodes.
171   v0.31
172     19980727   Richard Gooch <rgooch@atnf.csiro.au>
173                Removed harmless "unused variable" compiler warning.
174                Fixed modes for manually recreated device nodes.
175   v0.32
176     19980728   Richard Gooch <rgooch@atnf.csiro.au>
177                Added NULL devfs inode warning in <devfs_read_inode>.
178                Force all inode nlink values to 1.
179   v0.33
180     19980730   Richard Gooch <rgooch@atnf.csiro.au>
181                Added "dimknod" boot option.
182                Set inode nlink to 0 when freeing dentries.
183                Fixed modes for manually recreated symlinks.
184   v0.34
185     19980802   Richard Gooch <rgooch@atnf.csiro.au>
186                Fixed bugs in recreated directories and symlinks.
187   v0.35
188     19980806   Richard Gooch <rgooch@atnf.csiro.au>
189                Fixed bugs in recreated device nodes.
190     19980807   Richard Gooch <rgooch@atnf.csiro.au>
191                Fixed bug in currently unused <devfs_get_handle_from_inode>.
192                Defined new <devfs_handle_t> type.
193                Improved debugging when getting entries.
194                Fixed bug where directories could be emptied.
195   v0.36
196     19980809   Richard Gooch <rgooch@atnf.csiro.au>
197                Replaced dummy .epoch inode with .devfsd character device.
198     19980810   Richard Gooch <rgooch@atnf.csiro.au>
199                Implemented devfsd protocol revision 0.
200   v0.37
201     19980819   Richard Gooch <rgooch@atnf.csiro.au>
202                Added soothing message to warning in <devfs_d_iput>.
203   v0.38
204     19980829   Richard Gooch <rgooch@atnf.csiro.au>
205                Use GCC extensions for structure initialisations.
206                Implemented async open notification.
207                Incremented devfsd protocol revision to 1.
208   v0.39
209     19980908   Richard Gooch <rgooch@atnf.csiro.au>
210                Moved async open notification to end of <devfs_open>.
211   v0.40
212     19980910   Richard Gooch <rgooch@atnf.csiro.au>
213                Prepended "/dev/" to module load request.
214                Renamed <call_kerneld> to <call_kmod>.
215   v0.41
216     19980910   Richard Gooch <rgooch@atnf.csiro.au>
217                Fixed typo "AYSNC" -> "ASYNC".
218   v0.42
219     19980910   Richard Gooch <rgooch@atnf.csiro.au>
220                Added open flag for files.
221   v0.43
222     19980927   Richard Gooch <rgooch@atnf.csiro.au>
223                Set i_blocks=0 and i_blksize=1024 in <devfs_read_inode>.
224   v0.44
225     19981005   Richard Gooch <rgooch@atnf.csiro.au>
226                Added test for empty <<name>> in <devfs_find_handle>.
227                Renamed <generate_path> to <devfs_generate_path> and published.
228   v0.45
229     19981006   Richard Gooch <rgooch@atnf.csiro.au>
230                Created <devfs_get_fops>.
231   v0.46
232     19981007   Richard Gooch <rgooch@atnf.csiro.au>
233                Limit auto-device numbering to majors 144 to 239.
234   v0.47
235     19981010   Richard Gooch <rgooch@atnf.csiro.au>
236                Updated <devfs_follow_link> for VFS change in 2.1.125.
237   v0.48
238     19981022   Richard Gooch <rgooch@atnf.csiro.au>
239                Created DEVFS_ FL_COMPAT flag.
240   v0.49
241     19981023   Richard Gooch <rgooch@atnf.csiro.au>
242                Created "nocompat" boot option.
243   v0.50
244     19981025   Richard Gooch <rgooch@atnf.csiro.au>
245                Replaced "mount" boot option with "nomount".
246   v0.51
247     19981110   Richard Gooch <rgooch@atnf.csiro.au>
248                Created "only" boot option.
249   v0.52
250     19981112   Richard Gooch <rgooch@atnf.csiro.au>
251                Added DEVFS_FL_REMOVABLE flag.
252   v0.53
253     19981114   Richard Gooch <rgooch@atnf.csiro.au>
254                Only call <scan_dir_for_removable> on first call to
255                <devfs_readdir>.
256   v0.54
257     19981205   Richard Gooch <rgooch@atnf.csiro.au>
258                Updated <devfs_rmdir> for VFS change in 2.1.131.
259   v0.55
260     19981218   Richard Gooch <rgooch@atnf.csiro.au>
261                Created <devfs_mk_compat>.
262     19981220   Richard Gooch <rgooch@atnf.csiro.au>
263                Check for partitions on removable media in <devfs_lookup>.
264   v0.56
265     19990118   Richard Gooch <rgooch@atnf.csiro.au>
266                Added support for registering regular files.
267                Created <devfs_set_file_size>.
268                Update devfs inodes from entries if not changed through FS.
269   v0.57
270     19990124   Richard Gooch <rgooch@atnf.csiro.au>
271                Fixed <devfs_fill_file> to only initialise temporary inodes.
272                Trap for NULL fops in <devfs_register>.
273                Return -ENODEV in <devfs_fill_file> for non-driver inodes.
274   v0.58
275     19990126   Richard Gooch <rgooch@atnf.csiro.au>
276                Switched from PATH_MAX to DEVFS_PATHLEN.
277   v0.59
278     19990127   Richard Gooch <rgooch@atnf.csiro.au>
279                Created "nottycompat" boot option.
280   v0.60
281     19990318   Richard Gooch <rgooch@atnf.csiro.au>
282                Fixed <devfsd_read> to not overrun event buffer.
283   v0.61
284     19990329   Richard Gooch <rgooch@atnf.csiro.au>
285                Created <devfs_auto_unregister>.
286   v0.62
287     19990330   Richard Gooch <rgooch@atnf.csiro.au>
288                Don't return unregistred entries in <devfs_find_handle>.
289                Panic in <devfs_unregister> if entry unregistered.
290     19990401   Richard Gooch <rgooch@atnf.csiro.au>
291                Don't panic in <devfs_auto_unregister> for duplicates.
292   v0.63
293     19990402   Richard Gooch <rgooch@atnf.csiro.au>
294                Don't unregister already unregistered entries in <unregister>.
295   v0.64
296     19990510   Richard Gooch <rgooch@atnf.csiro.au>
297                Disable warning messages when unable to read partition table for
298                removable media.
299   v0.65
300     19990512   Richard Gooch <rgooch@atnf.csiro.au>
301                Updated <devfs_lookup> for VFS change in 2.3.1-pre1.
302                Created "oops-on-panic" boot option.
303                Improved debugging in <devfs_register> and <devfs_unregister>.
304   v0.66
305     19990519   Richard Gooch <rgooch@atnf.csiro.au>
306                Added documentation for some functions.
307     19990525   Richard Gooch <rgooch@atnf.csiro.au>
308                Removed "oops-on-panic" boot option: now always Oops.
309   v0.67
310     19990531   Richard Gooch <rgooch@atnf.csiro.au>
311                Improved debugging in <devfs_register>.
312   v0.68
313     19990604   Richard Gooch <rgooch@atnf.csiro.au>
314                Added "diunlink" and "nokmod" boot options.
315                Removed superfluous warning message in <devfs_d_iput>.
316   v0.69
317     19990611   Richard Gooch <rgooch@atnf.csiro.au>
318                Took account of change to <d_alloc_root>.
319   v0.70
320     19990614   Richard Gooch <rgooch@atnf.csiro.au>
321                Created separate event queue for each mounted devfs.
322                Removed <devfs_invalidate_dcache>.
323                Created new ioctl()s.
324                Incremented devfsd protocol revision to 3.
325                Fixed bug when re-creating directories: contents were lost.
326                Block access to inodes until devfsd updates permissions.
327     19990615   Richard Gooch <rgooch@atnf.csiro.au>
328                Support 2.2.x kernels.
329   v0.71
330     19990623   Richard Gooch <rgooch@atnf.csiro.au>
331                Switched to sending process uid/gid to devfsd.
332                Renamed <call_kmod> to <try_modload>.
333                Added DEVFSD_NOTIFY_LOOKUP event.
334     19990624   Richard Gooch <rgooch@atnf.csiro.au>
335                Added DEVFSD_NOTIFY_CHANGE event.
336                Incremented devfsd protocol revision to 4.
337   v0.72
338     19990713   Richard Gooch <rgooch@atnf.csiro.au>
339                Return EISDIR rather than EINVAL for read(2) on directories.
340   v0.73
341     19990809   Richard Gooch <rgooch@atnf.csiro.au>
342                Changed <devfs_setup> to new __init scheme.
343   v0.74
344     19990901   Richard Gooch <rgooch@atnf.csiro.au>
345                Changed remaining function declarations to new __init scheme.
346   v0.75
347     19991013   Richard Gooch <rgooch@atnf.csiro.au>
348                Created <devfs_get_info>, <devfs_set_info>,
349                <devfs_get_first_child> and <devfs_get_next_sibling>.
350                Added <<dir>> parameter to <devfs_register>, <devfs_mk_compat>,
351                <devfs_mk_dir> and <devfs_find_handle>.
352                Work sponsored by SGI.
353   v0.76
354     19991017   Richard Gooch <rgooch@atnf.csiro.au>
355                Allow multiple unregistrations.
356                Work sponsored by SGI.
357   v0.77
358     19991026   Richard Gooch <rgooch@atnf.csiro.au>
359                Added major and minor number to devfsd protocol.
360                Incremented devfsd protocol revision to 5.
361                Work sponsored by SGI.
362   v0.78
363     19991030   Richard Gooch <rgooch@atnf.csiro.au>
364                Support info pointer for all devfs entry types.
365                Added <<info>> parameter to <devfs_mk_dir> and
366                <devfs_mk_symlink>.
367                Work sponsored by SGI.
368   v0.79
369     19991031   Richard Gooch <rgooch@atnf.csiro.au>
370                Support "../" when searching devfs namespace.
371                Work sponsored by SGI.
372   v0.80
373     19991101   Richard Gooch <rgooch@atnf.csiro.au>
374                Created <devfs_get_unregister_slave>.
375                Work sponsored by SGI.
376   v0.81
377     19991103   Richard Gooch <rgooch@atnf.csiro.au>
378                Exported <devfs_get_parent>.
379                Work sponsored by SGI.
380   v0.82
381     19991104   Richard Gooch <rgooch@atnf.csiro.au>
382                Removed unused <devfs_set_symlink_destination>.
383     19991105   Richard Gooch <rgooch@atnf.csiro.au>
384                Do not hide entries from devfsd or children.
385                Removed DEVFS_ FL_TTY_COMPAT flag.
386                Removed "nottycompat" boot option.
387                Removed <devfs_mk_compat>.
388                Work sponsored by SGI.
389   v0.83
390     19991107   Richard Gooch <rgooch@atnf.csiro.au>
391                Added DEVFS_FL_WAIT flag.
392                Work sponsored by SGI.
393   v0.84
394     19991107   Richard Gooch <rgooch@atnf.csiro.au>
395                Support new "disc" naming scheme in <get_removable_partition>.
396                Allow NULL fops in <devfs_register>.
397                Work sponsored by SGI.
398   v0.85
399     19991110   Richard Gooch <rgooch@atnf.csiro.au>
400                Fall back to major table if NULL fops given to <devfs_register>.
401                Work sponsored by SGI.
402   v0.86
403     19991204   Richard Gooch <rgooch@atnf.csiro.au>
404                Support fifos when unregistering.
405                Work sponsored by SGI.
406   v0.87
407     19991209   Richard Gooch <rgooch@atnf.csiro.au>
408                Removed obsolete DEVFS_ FL_COMPAT and DEVFS_ FL_TOLERANT flags.
409                Work sponsored by SGI.
410   v0.88
411     19991214   Richard Gooch <rgooch@atnf.csiro.au>
412                Removed kmod support.
413                Work sponsored by SGI.
414   v0.89
415     19991216   Richard Gooch <rgooch@atnf.csiro.au>
416                Improved debugging in <get_vfs_inode>.
417                Ensure dentries created by devfsd will be cleaned up.
418                Work sponsored by SGI.
419   v0.90
420     19991223   Richard Gooch <rgooch@atnf.csiro.au>
421                Created <devfs_get_name>.
422                Work sponsored by SGI.
423   v0.91
424     20000203   Richard Gooch <rgooch@atnf.csiro.au>
425                Ported to kernel 2.3.42.
426                Removed <devfs_fill_file>.
427                Work sponsored by SGI.
428   v0.92
429     20000306   Richard Gooch <rgooch@atnf.csiro.au>
430                Added DEVFS_ FL_NO_PERSISTENCE flag.
431                Removed unnecessary call to <update_devfs_inode_from_entry> in
432                <devfs_readdir>.
433                Work sponsored by SGI.
434   v0.93
435     20000413   Richard Gooch <rgooch@atnf.csiro.au>
436                Set inode->i_size to correct size for symlinks.
437     20000414   Richard Gooch <rgooch@atnf.csiro.au>
438                Only give lookup() method to directories to comply with new VFS
439                assumptions.
440                Work sponsored by SGI.
441     20000415   Richard Gooch <rgooch@atnf.csiro.au>
442                Remove unnecessary tests in symlink methods.
443                Don't kill existing block ops in <devfs_read_inode>.
444                Work sponsored by SGI.
445   v0.94
446     20000424   Richard Gooch <rgooch@atnf.csiro.au>
447                Don't create missing directories in <devfs_find_handle>.
448                Work sponsored by SGI.
449   v0.95
450     20000430   Richard Gooch <rgooch@atnf.csiro.au>
451                Added CONFIG_DEVFS_MOUNT.
452                Work sponsored by SGI.
453   v0.96
454     20000608   Richard Gooch <rgooch@atnf.csiro.au>
455                Disabled multi-mount capability (use VFS bindings instead).
456                Work sponsored by SGI.
457   v0.97
458     20000610   Richard Gooch <rgooch@atnf.csiro.au>
459                Switched to FS_SINGLE to disable multi-mounts.
460     20000612   Richard Gooch <rgooch@atnf.csiro.au>
461                Removed module support.
462                Removed multi-mount code.
463                Removed compatibility macros: VFS has changed too much.
464                Work sponsored by SGI.
465   v0.98
466     20000614   Richard Gooch <rgooch@atnf.csiro.au>
467                Merged devfs inode into devfs entry.
468                Work sponsored by SGI.
469   v0.99
470     20000619   Richard Gooch <rgooch@atnf.csiro.au>
471                Removed dead code in <devfs_register> which used to call
472                <free_dentries>.
473                Work sponsored by SGI.
474   v0.100
475     20000621   Richard Gooch <rgooch@atnf.csiro.au>
476                Changed interface to <devfs_register>.
477                Work sponsored by SGI.
478   v0.101
479     20000622   Richard Gooch <rgooch@atnf.csiro.au>
480                Simplified interface to <devfs_mk_symlink> and <devfs_mk_dir>.
481                Simplified interface to <devfs_find_handle>.
482                Work sponsored by SGI.
483   v0.102
484     20010519   Richard Gooch <rgooch@atnf.csiro.au>
485                Ensure <devfs_generate_path> terminates string for root entry.
486                Exported <devfs_get_name> to modules.
487     20010520   Richard Gooch <rgooch@atnf.csiro.au>
488                Make <devfs_mk_symlink> send events to devfsd.
489                Cleaned up option processing in <devfs_setup>.
490     20010521   Richard Gooch <rgooch@atnf.csiro.au>
491                Fixed bugs in handling symlinks: could leak or cause Oops.
492     20010522   Richard Gooch <rgooch@atnf.csiro.au>
493                Cleaned up directory handling by separating fops.
494   v0.103
495     20010601   Richard Gooch <rgooch@atnf.csiro.au>
496                Fixed handling of inverted options in <devfs_setup>.
497   v0.104
498     20010604   Richard Gooch <rgooch@atnf.csiro.au>
499                Adjusted <try_modload> to account for <devfs_generate_path> fix.
500   v0.105
501     20010617   Richard Gooch <rgooch@atnf.csiro.au>
502                Answered question posed by Al Viro and removed his comments.
503                Moved setting of registered flag after other fields are changed.
504                Fixed race between <devfsd_close> and <devfsd_notify_one>.
505                Global VFS changes added bogus BKL to <devfsd_close>: removed.
506                Widened locking in <devfs_readlink> and <devfs_follow_link>.
507                Replaced <devfsd_read> stack usage with <devfsd_ioctl> kmalloc.
508                Simplified locking in <devfsd_ioctl> and fixed memory leak.
509   v0.106
510     20010709   Richard Gooch <rgooch@atnf.csiro.au>
511                Removed broken devnum allocation and use <devfs_alloc_devnum>.
512                Fixed old devnum leak by calling new <devfs_dealloc_devnum>.
513   v0.107
514     20010712   Richard Gooch <rgooch@atnf.csiro.au>
515                Fixed bug in <devfs_setup> which could hang boot process.
516   v0.108
517     20010730   Richard Gooch <rgooch@atnf.csiro.au>
518                Added DEVFSD_NOTIFY_DELETE event.
519     20010801   Richard Gooch <rgooch@atnf.csiro.au>
520                Removed #include <asm/segment.h>.
521   v0.109
522     20010807   Richard Gooch <rgooch@atnf.csiro.au>
523                Fixed inode table races by removing it and using
524                inode->u.generic_ip instead.
525                Moved <devfs_read_inode> into <get_vfs_inode>.
526                Moved <devfs_write_inode> into <devfs_notify_change>.
527   v0.110
528     20010808   Richard Gooch <rgooch@atnf.csiro.au>
529                Fixed race in <devfs_do_symlink> for uni-processor.
530   v0.111
531     20010818   Richard Gooch <rgooch@atnf.csiro.au>
532                Removed remnant of multi-mount support in <devfs_mknod>.
533                Removed unused DEVFS_FL_SHOW_UNREG flag.
534   v0.112
535     20010820   Richard Gooch <rgooch@atnf.csiro.au>
536                Removed nlink field from struct devfs_inode.
537   v0.113
538     20010823   Richard Gooch <rgooch@atnf.csiro.au>
539                Replaced BKL with global rwsem to protect symlink data (quick
540                and dirty hack).
541   v0.114
542     20010827   Richard Gooch <rgooch@atnf.csiro.au>
543                Replaced global rwsem for symlink with per-link refcount.
544   v0.115
545     20010919   Richard Gooch <rgooch@atnf.csiro.au>
546                Set inode->i_mapping->a_ops for block nodes in <get_vfs_inode>.
547   v0.116
548     20011008   Richard Gooch <rgooch@atnf.csiro.au>
549                Fixed overrun in <devfs_link> by removing function (not needed).
550     20011009   Richard Gooch <rgooch@atnf.csiro.au>
551                Fixed buffer underrun in <try_modload>.
552     20011029   Richard Gooch <rgooch@atnf.csiro.au>
553                Fixed race in <devfsd_ioctl> when setting event mask.
554     20011114   Richard Gooch <rgooch@atnf.csiro.au>
555                First release of new locking code.
556   v1.0
557     20011117   Richard Gooch <rgooch@atnf.csiro.au>
558                Discard temporary buffer, now use "%s" for dentry names.
559     20011118   Richard Gooch <rgooch@atnf.csiro.au>
560                Don't generate path in <try_modload>: use fake entry instead.
561                Use "existing" directory in <_devfs_make_parent_for_leaf>.
562     20011122   Richard Gooch <rgooch@atnf.csiro.au>
563                Use slab cache rather than fixed buffer for devfsd events.
564   v1.1
565     20011125   Richard Gooch <rgooch@atnf.csiro.au>
566                Send DEVFSD_NOTIFY_REGISTERED events in <devfs_mk_dir>.
567     20011127   Richard Gooch <rgooch@atnf.csiro.au>
568                Fixed locking bug in <devfs_d_revalidate_wait> due to typo.
569                Do not send CREATE, CHANGE, ASYNC_OPEN or DELETE events from
570                devfsd or children.
571   v1.2
572     20011202   Richard Gooch <rgooch@atnf.csiro.au>
573                Fixed bug in <devfsd_read>: was dereferencing freed pointer.
574   v1.3
575     20011203   Richard Gooch <rgooch@atnf.csiro.au>
576                Fixed bug in <devfsd_close>: was dereferencing freed pointer.
577                Added process group check for devfsd privileges.
578   v1.4
579     20011204   Richard Gooch <rgooch@atnf.csiro.au>
580                Use SLAB_ATOMIC in <devfsd_notify_de> from <devfs_d_delete>.
581   v1.5
582     20011211   Richard Gooch <rgooch@atnf.csiro.au>
583                Return old entry in <devfs_mk_dir> for 2.4.x kernels.
584     20011212   Richard Gooch <rgooch@atnf.csiro.au>
585                Increment refcount on module in <check_disc_changed>.
586     20011215   Richard Gooch <rgooch@atnf.csiro.au>
587                Created <devfs_get_handle> and exported <devfs_put>.
588                Increment refcount on module in <devfs_get_ops>.
589                Created <devfs_put_ops>.
590   v1.6
591     20011216   Richard Gooch <rgooch@atnf.csiro.au>
592                Added poisoning to <devfs_put>.
593                Improved debugging messages.
594   v1.7
595     20011221   Richard Gooch <rgooch@atnf.csiro.au>
596                Corrected (made useful) debugging message in <unregister>.
597                Moved <kmem_cache_create> in <mount_devfs_fs> to <init_devfs_fs>
598     20011224   Richard Gooch <rgooch@atnf.csiro.au>
599                Added magic number to guard against scribbling drivers.
600     20011226   Richard Gooch <rgooch@atnf.csiro.au>
601                Only return old entry in <devfs_mk_dir> if a directory.
602                Defined macros for error and debug messages.
603   v1.8
604     20020113   Richard Gooch <rgooch@atnf.csiro.au>
605                Fixed (rare, old) race in <devfs_lookup>.
606   v1.9
607     20020120   Richard Gooch <rgooch@atnf.csiro.au>
608                Fixed deadlock bug in <devfs_d_revalidate_wait>.
609                Tag VFS deletable in <devfs_mk_symlink> if handle ignored.
610   v1.10
611     20020129   Richard Gooch <rgooch@atnf.csiro.au>
612                Added KERN_* to remaining messages.
613                Cleaned up declaration of <stat_read>.
614   v1.11
615     20020219   Richard Gooch <rgooch@atnf.csiro.au>
616                Changed <devfs_rmdir> to allow later additions if not yet empty.
617   v1.12
618     20020406   Richard Gooch <rgooch@atnf.csiro.au>
619                Removed silently introduced calls to lock_kernel() and
620                unlock_kernel() due to recent VFS locking changes. BKL isn't
621                required in devfs.
622   v1.13
623     20020428   Richard Gooch <rgooch@atnf.csiro.au>
624                Removed 2.4.x compatibility code.
625   v1.14
626     20020510   Richard Gooch <rgooch@atnf.csiro.au>
627                Added BKL to <devfs_open> because drivers still need it.
628   v1.15
629     20020512   Richard Gooch <rgooch@atnf.csiro.au>
630                Protected <scan_dir_for_removable> and <get_removable_partition>
631                from changing directory contents.
632   v1.16
633     20020514   Richard Gooch <rgooch@atnf.csiro.au>
634                Minor cleanup of <scan_dir_for_removable>.
635   v1.17
636     20020721   Richard Gooch <rgooch@atnf.csiro.au>
637                Switched to ISO C structure field initialisers.
638                Switch to set_current_state() and move before add_wait_queue().
639     20020722   Richard Gooch <rgooch@atnf.csiro.au>
640                Fixed devfs entry leak in <devfs_readdir> when *readdir fails.
641   v1.18
642     20020725   Richard Gooch <rgooch@atnf.csiro.au>
643                Created <devfs_find_and_unregister>.
644   v1.19
645     20020728   Richard Gooch <rgooch@atnf.csiro.au>
646                Removed deprecated <devfs_find_handle>.
647   v1.20
648     20020820   Richard Gooch <rgooch@atnf.csiro.au>
649                Fixed module unload race in <devfs_open>.
650   v1.21
651     20021013   Richard Gooch <rgooch@atnf.csiro.au>
652                Removed DEVFS_ FL_AUTO_OWNER.
653                Switched lingering structure field initialiser to ISO C.
654                Added locking when updating FCB flags.
655   v1.22
656 */
657 #include <linux/types.h>
658 #include <linux/errno.h>
659 #include <linux/time.h>
660 #include <linux/tty.h>
661 #include <linux/timer.h>
662 #include <linux/config.h>
663 #include <linux/kernel.h>
664 #include <linux/wait.h>
665 #include <linux/string.h>
666 #include <linux/slab.h>
667 #include <linux/ioport.h>
668 #include <linux/delay.h>
669 #include <linux/ctype.h>
670 #include <linux/mm.h>
671 #include <linux/module.h>
672 #include <linux/init.h>
673 #include <linux/devfs_fs.h>
674 #include <linux/devfs_fs_kernel.h>
675 #include <linux/smp_lock.h>
676 #include <linux/smp.h>
677 #include <linux/rwsem.h>
678 #include <linux/sched.h>
679 #include <linux/namei.h>
680
681 #include <asm/uaccess.h>
682 #include <asm/io.h>
683 #include <asm/processor.h>
684 #include <asm/system.h>
685 #include <asm/pgtable.h>
686 #include <asm/bitops.h>
687 #include <asm/atomic.h>
688
689 #define DEVFS_VERSION            "2004-01-31"
690
691 #define DEVFS_NAME "devfs"
692
693 #define FIRST_INODE 1
694
695 #define STRING_LENGTH 256
696 #define FAKE_BLOCK_SIZE 1024
697 #define POISON_PTR ( *(void **) poison_array )
698 #define MAGIC_VALUE 0x327db823
699
700 #ifndef TRUE
701 #  define TRUE 1
702 #  define FALSE 0
703 #endif
704
705 #define MODE_DIR (S_IFDIR | S_IWUSR | S_IRUGO | S_IXUGO)
706
707 #define DEBUG_NONE         0x0000000
708 #define DEBUG_MODULE_LOAD  0x0000001
709 #define DEBUG_REGISTER     0x0000002
710 #define DEBUG_UNREGISTER   0x0000004
711 #define DEBUG_FREE         0x0000008
712 #define DEBUG_SET_FLAGS    0x0000010
713 #define DEBUG_S_READ       0x0000100    /*  Break  */
714 #define DEBUG_I_LOOKUP     0x0001000    /*  Break  */
715 #define DEBUG_I_CREATE     0x0002000
716 #define DEBUG_I_GET        0x0004000
717 #define DEBUG_I_CHANGE     0x0008000
718 #define DEBUG_I_UNLINK     0x0010000
719 #define DEBUG_I_RLINK      0x0020000
720 #define DEBUG_I_FLINK      0x0040000
721 #define DEBUG_I_MKNOD      0x0080000
722 #define DEBUG_F_READDIR    0x0100000    /*  Break  */
723 #define DEBUG_D_DELETE     0x1000000    /*  Break  */
724 #define DEBUG_D_RELEASE    0x2000000
725 #define DEBUG_D_IPUT       0x4000000
726 #define DEBUG_ALL          0xfffffff
727 #define DEBUG_DISABLED     DEBUG_NONE
728
729 #define OPTION_NONE             0x00
730 #define OPTION_MOUNT            0x01
731
732 #define PRINTK(format, args...) \
733    {printk (KERN_ERR "%s" format, __FUNCTION__ , ## args);}
734
735 #define OOPS(format, args...) \
736    {printk (KERN_CRIT "%s" format, __FUNCTION__ , ## args); \
737     printk ("Forcing Oops\n"); \
738     BUG();}
739
740 #ifdef CONFIG_DEVFS_DEBUG
741 #  define VERIFY_ENTRY(de) \
742    {if ((de) && (de)->magic_number != MAGIC_VALUE) \
743         OOPS ("(%p): bad magic value: %x\n", (de), (de)->magic_number);}
744 #  define WRITE_ENTRY_MAGIC(de,magic) (de)->magic_number = (magic)
745 #  define DPRINTK(flag, format, args...) \
746    {if (devfs_debug & flag) \
747         printk (KERN_INFO "%s" format, __FUNCTION__ , ## args);}
748 #else
749 #  define VERIFY_ENTRY(de)
750 #  define WRITE_ENTRY_MAGIC(de,magic)
751 #  define DPRINTK(flag, format, args...)
752 #endif
753
754 typedef struct devfs_entry *devfs_handle_t;
755
756 struct directory_type {
757         rwlock_t lock;          /*  Lock for searching(R)/updating(W)   */
758         struct devfs_entry *first;
759         struct devfs_entry *last;
760         unsigned char no_more_additions:1;
761 };
762
763 struct symlink_type {
764         unsigned int length;    /*  Not including the NULL-termimator       */
765         char *linkname;         /*  This is NULL-terminated                 */
766 };
767
768 struct devfs_inode {            /*  This structure is for "persistent" inode storage  */
769         struct dentry *dentry;
770         struct timespec atime;
771         struct timespec mtime;
772         struct timespec ctime;
773         unsigned int ino;       /*  Inode number as seen in the VFS         */
774         uid_t uid;
775         gid_t gid;
776 };
777
778 struct devfs_entry {
779 #ifdef CONFIG_DEVFS_DEBUG
780         unsigned int magic_number;
781 #endif
782         void *info;
783         atomic_t refcount;      /*  When this drops to zero, it's unused    */
784         union {
785                 struct directory_type dir;
786                 dev_t dev;
787                 struct symlink_type symlink;
788                 const char *name;       /*  Only used for (mode == 0)               */
789         } u;
790         struct devfs_entry *prev;       /*  Previous entry in the parent directory  */
791         struct devfs_entry *next;       /*  Next entry in the parent directory      */
792         struct devfs_entry *parent;     /*  The parent directory                    */
793         struct devfs_inode inode;
794         umode_t mode;
795         unsigned short namelen; /*  I think 64k+ filenames are a way off... */
796         unsigned char vfs:1;    /*  Whether the VFS may delete the entry   */
797         char name[1];           /*  This is just a dummy: the allocated array
798                                    is bigger. This is NULL-terminated      */
799 };
800
801 /*  The root of the device tree  */
802 static struct devfs_entry *root_entry;
803
804 struct devfsd_buf_entry {
805         struct devfs_entry *de; /*  The name is generated with this         */
806         unsigned short type;    /*  The type of event                       */
807         umode_t mode;
808         uid_t uid;
809         gid_t gid;
810         struct devfsd_buf_entry *next;
811 };
812
813 struct fs_info {                /*  This structure is for the mounted devfs  */
814         struct super_block *sb;
815         spinlock_t devfsd_buffer_lock;  /*  Lock when inserting/deleting events  */
816         struct devfsd_buf_entry *devfsd_first_event;
817         struct devfsd_buf_entry *devfsd_last_event;
818         volatile int devfsd_sleeping;
819         volatile struct task_struct *devfsd_task;
820         volatile pid_t devfsd_pgrp;
821         volatile struct file *devfsd_file;
822         struct devfsd_notify_struct *devfsd_info;
823         volatile unsigned long devfsd_event_mask;
824         atomic_t devfsd_overrun_count;
825         wait_queue_head_t devfsd_wait_queue;    /*  Wake devfsd on input       */
826         wait_queue_head_t revalidate_wait_queue;        /*  Wake when devfsd sleeps    */
827 };
828
829 static struct fs_info fs_info = {.devfsd_buffer_lock = SPIN_LOCK_UNLOCKED };
830 static kmem_cache_t *devfsd_buf_cache;
831 #ifdef CONFIG_DEVFS_DEBUG
832 static unsigned int devfs_debug_init __initdata = DEBUG_NONE;
833 static unsigned int devfs_debug = DEBUG_NONE;
834 static spinlock_t stat_lock = SPIN_LOCK_UNLOCKED;
835 static unsigned int stat_num_entries;
836 static unsigned int stat_num_bytes;
837 #endif
838 static unsigned char poison_array[8] =
839     { 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a };
840
841 #ifdef CONFIG_DEVFS_MOUNT
842 static unsigned int boot_options = OPTION_MOUNT;
843 #else
844 static unsigned int boot_options = OPTION_NONE;
845 #endif
846
847 /*  Forward function declarations  */
848 static devfs_handle_t _devfs_walk_path(struct devfs_entry *dir,
849                                        const char *name, int namelen,
850                                        int traverse_symlink);
851 static ssize_t devfsd_read(struct file *file, char __user *buf, size_t len,
852                            loff_t * ppos);
853 static int devfsd_ioctl(struct inode *inode, struct file *file,
854                         unsigned int cmd, unsigned long arg);
855 static int devfsd_close(struct inode *inode, struct file *file);
856 #ifdef CONFIG_DEVFS_DEBUG
857 static ssize_t stat_read(struct file *file, char __user *buf, size_t len,
858                          loff_t * ppos);
859 static struct file_operations stat_fops = {
860         .read = stat_read,
861 };
862 #endif
863
864 /*  Devfs daemon file operations  */
865 static struct file_operations devfsd_fops = {
866         .read = devfsd_read,
867         .ioctl = devfsd_ioctl,
868         .release = devfsd_close,
869 };
870
871 /*  Support functions follow  */
872
873 /**
874  *      devfs_get - Get a reference to a devfs entry.
875  *      @de:  The devfs entry.
876  */
877
878 static struct devfs_entry *devfs_get(struct devfs_entry *de)
879 {
880         VERIFY_ENTRY(de);
881         if (de)
882                 atomic_inc(&de->refcount);
883         return de;
884 }                               /*  End Function devfs_get  */
885
886 /**
887  *      devfs_put - Put (release) a reference to a devfs entry.
888  *      @de:  The handle to the devfs entry.
889  */
890
891 static void devfs_put(devfs_handle_t de)
892 {
893         if (!de)
894                 return;
895         VERIFY_ENTRY(de);
896         if (de->info == POISON_PTR)
897                 OOPS("(%p): poisoned pointer\n", de);
898         if (!atomic_dec_and_test(&de->refcount))
899                 return;
900         if (de == root_entry)
901                 OOPS("(%p): root entry being freed\n", de);
902         DPRINTK(DEBUG_FREE, "(%s): de: %p, parent: %p \"%s\"\n",
903                 de->name, de, de->parent,
904                 de->parent ? de->parent->name : "no parent");
905         if (S_ISLNK(de->mode))
906                 kfree(de->u.symlink.linkname);
907         WRITE_ENTRY_MAGIC(de, 0);
908 #ifdef CONFIG_DEVFS_DEBUG
909         spin_lock(&stat_lock);
910         --stat_num_entries;
911         stat_num_bytes -= sizeof *de + de->namelen;
912         if (S_ISLNK(de->mode))
913                 stat_num_bytes -= de->u.symlink.length + 1;
914         spin_unlock(&stat_lock);
915 #endif
916         de->info = POISON_PTR;
917         kfree(de);
918 }                               /*  End Function devfs_put  */
919
920 /**
921  *      _devfs_search_dir - Search for a devfs entry in a directory.
922  *      @dir:  The directory to search.
923  *      @name:  The name of the entry to search for.
924  *      @namelen:  The number of characters in @name.
925  *
926  *  Search for a devfs entry in a directory and returns a pointer to the entry
927  *   on success, else %NULL. The directory must be locked already.
928  *   An implicit devfs_get() is performed on the returned entry.
929  */
930
931 static struct devfs_entry *_devfs_search_dir(struct devfs_entry *dir,
932                                              const char *name,
933                                              unsigned int namelen)
934 {
935         struct devfs_entry *curr;
936
937         if (!S_ISDIR(dir->mode)) {
938                 PRINTK("(%s): not a directory\n", dir->name);
939                 return NULL;
940         }
941         for (curr = dir->u.dir.first; curr != NULL; curr = curr->next) {
942                 if (curr->namelen != namelen)
943                         continue;
944                 if (memcmp(curr->name, name, namelen) == 0)
945                         break;
946                 /*  Not found: try the next one  */
947         }
948         return devfs_get(curr);
949 }                               /*  End Function _devfs_search_dir  */
950
951 /**
952  *      _devfs_alloc_entry - Allocate a devfs entry.
953  *      @name:     the name of the entry
954  *      @namelen:  the number of characters in @name
955  *      @mode:     the mode for the entry
956  *
957  *  Allocate a devfs entry and returns a pointer to the entry on success, else
958  *   %NULL.
959  */
960
961 static struct devfs_entry *_devfs_alloc_entry(const char *name,
962                                               unsigned int namelen,
963                                               umode_t mode)
964 {
965         struct devfs_entry *new;
966         static unsigned long inode_counter = FIRST_INODE;
967         static spinlock_t counter_lock = SPIN_LOCK_UNLOCKED;
968
969         if (name && (namelen < 1))
970                 namelen = strlen(name);
971         if ((new = kmalloc(sizeof *new + namelen, GFP_KERNEL)) == NULL)
972                 return NULL;
973         memset(new, 0, sizeof *new + namelen);  /*  Will set '\0' on name  */
974         new->mode = mode;
975         if (S_ISDIR(mode))
976                 rwlock_init(&new->u.dir.lock);
977         atomic_set(&new->refcount, 1);
978         spin_lock(&counter_lock);
979         new->inode.ino = inode_counter++;
980         spin_unlock(&counter_lock);
981         if (name)
982                 memcpy(new->name, name, namelen);
983         new->namelen = namelen;
984         WRITE_ENTRY_MAGIC(new, MAGIC_VALUE);
985 #ifdef CONFIG_DEVFS_DEBUG
986         spin_lock(&stat_lock);
987         ++stat_num_entries;
988         stat_num_bytes += sizeof *new + namelen;
989         spin_unlock(&stat_lock);
990 #endif
991         return new;
992 }                               /*  End Function _devfs_alloc_entry  */
993
994 /**
995  *      _devfs_append_entry - Append a devfs entry to a directory's child list.
996  *      @dir:  The directory to add to.
997  *      @de:  The devfs entry to append.
998  *      @old_de: If an existing entry exists, it will be written here. This may
999  *               be %NULL. An implicit devfs_get() is performed on this entry.
1000  *
1001  *  Append a devfs entry to a directory's list of children, checking first to
1002  *   see if an entry of the same name exists. The directory will be locked.
1003  *   The value 0 is returned on success, else a negative error code.
1004  *   On failure, an implicit devfs_put() is performed on %de.
1005  */
1006
1007 static int _devfs_append_entry(devfs_handle_t dir, devfs_handle_t de,
1008                                devfs_handle_t * old_de)
1009 {
1010         int retval;
1011
1012         if (old_de)
1013                 *old_de = NULL;
1014         if (!S_ISDIR(dir->mode)) {
1015                 PRINTK("(%s): dir: \"%s\" is not a directory\n", de->name,
1016                        dir->name);
1017                 devfs_put(de);
1018                 return -ENOTDIR;
1019         }
1020         write_lock(&dir->u.dir.lock);
1021         if (dir->u.dir.no_more_additions)
1022                 retval = -ENOENT;
1023         else {
1024                 struct devfs_entry *old;
1025
1026                 old = _devfs_search_dir(dir, de->name, de->namelen);
1027                 if (old_de)
1028                         *old_de = old;
1029                 else
1030                         devfs_put(old);
1031                 if (old == NULL) {
1032                         de->parent = dir;
1033                         de->prev = dir->u.dir.last;
1034                         /*  Append to the directory's list of children  */
1035                         if (dir->u.dir.first == NULL)
1036                                 dir->u.dir.first = de;
1037                         else
1038                                 dir->u.dir.last->next = de;
1039                         dir->u.dir.last = de;
1040                         retval = 0;
1041                 } else
1042                         retval = -EEXIST;
1043         }
1044         write_unlock(&dir->u.dir.lock);
1045         if (retval)
1046                 devfs_put(de);
1047         return retval;
1048 }                               /*  End Function _devfs_append_entry  */
1049
1050 /**
1051  *      _devfs_get_root_entry - Get the root devfs entry.
1052  *
1053  *      Returns the root devfs entry on success, else %NULL.
1054  *
1055  *      TODO it must be called asynchronously due to the fact
1056  *      that devfs is initialized relatively late. Proper way
1057  *      is to remove module_init from init_devfs_fs and manually
1058  *      call it early enough during system init
1059  */
1060
1061 static struct devfs_entry *_devfs_get_root_entry(void)
1062 {
1063         struct devfs_entry *new;
1064         static spinlock_t root_lock = SPIN_LOCK_UNLOCKED;
1065
1066         if (root_entry)
1067                 return root_entry;
1068
1069         new = _devfs_alloc_entry(NULL, 0, MODE_DIR);
1070         if (new == NULL)
1071                 return NULL;
1072
1073         spin_lock(&root_lock);
1074         if (root_entry) {
1075                 spin_unlock(&root_lock);
1076                 devfs_put(new);
1077                 return root_entry;
1078         }
1079         root_entry = new;
1080         spin_unlock(&root_lock);
1081
1082         return root_entry;
1083 }                               /*  End Function _devfs_get_root_entry  */
1084
1085 /**
1086  *      _devfs_descend - Descend down a tree using the next component name.
1087  *      @dir:  The directory to search.
1088  *      @name:  The component name to search for.
1089  *      @namelen:  The length of %name.
1090  *      @next_pos:  The position of the next '/' or '\0' is written here.
1091  *
1092  *  Descend into a directory, searching for a component. This function forms
1093  *   the core of a tree-walking algorithm. The directory will be locked.
1094  *   The devfs entry corresponding to the component is returned. If there is
1095  *   no matching entry, %NULL is returned.
1096  *   An implicit devfs_get() is performed on the returned entry.
1097  */
1098
1099 static struct devfs_entry *_devfs_descend(struct devfs_entry *dir,
1100                                           const char *name, int namelen,
1101                                           int *next_pos)
1102 {
1103         const char *stop, *ptr;
1104         struct devfs_entry *entry;
1105
1106         if ((namelen >= 3) && (strncmp(name, "../", 3) == 0)) { /*  Special-case going to parent directory  */
1107                 *next_pos = 3;
1108                 return devfs_get(dir->parent);
1109         }
1110         stop = name + namelen;
1111         /*  Search for a possible '/'  */
1112         for (ptr = name; (ptr < stop) && (*ptr != '/'); ++ptr) ;
1113         *next_pos = ptr - name;
1114         read_lock(&dir->u.dir.lock);
1115         entry = _devfs_search_dir(dir, name, *next_pos);
1116         read_unlock(&dir->u.dir.lock);
1117         return entry;
1118 }                               /*  End Function _devfs_descend  */
1119
1120 static devfs_handle_t _devfs_make_parent_for_leaf(struct devfs_entry *dir,
1121                                                   const char *name,
1122                                                   int namelen, int *leaf_pos)
1123 {
1124         int next_pos = 0;
1125
1126         if (dir == NULL)
1127                 dir = _devfs_get_root_entry();
1128         if (dir == NULL)
1129                 return NULL;
1130         devfs_get(dir);
1131         /*  Search for possible trailing component and ignore it  */
1132         for (--namelen; (namelen > 0) && (name[namelen] != '/'); --namelen) ;
1133         *leaf_pos = (name[namelen] == '/') ? (namelen + 1) : 0;
1134         for (; namelen > 0; name += next_pos, namelen -= next_pos) {
1135                 struct devfs_entry *de, *old = NULL;
1136
1137                 if ((de =
1138                      _devfs_descend(dir, name, namelen, &next_pos)) == NULL) {
1139                         de = _devfs_alloc_entry(name, next_pos, MODE_DIR);
1140                         devfs_get(de);
1141                         if (!de || _devfs_append_entry(dir, de, &old)) {
1142                                 devfs_put(de);
1143                                 if (!old || !S_ISDIR(old->mode)) {
1144                                         devfs_put(old);
1145                                         devfs_put(dir);
1146                                         return NULL;
1147                                 }
1148                                 de = old;       /*  Use the existing directory  */
1149                         }
1150                 }
1151                 if (de == dir->parent) {
1152                         devfs_put(dir);
1153                         devfs_put(de);
1154                         return NULL;
1155                 }
1156                 devfs_put(dir);
1157                 dir = de;
1158                 if (name[next_pos] == '/')
1159                         ++next_pos;
1160         }
1161         return dir;
1162 }                               /*  End Function _devfs_make_parent_for_leaf  */
1163
1164 static devfs_handle_t _devfs_prepare_leaf(devfs_handle_t * dir,
1165                                           const char *name, umode_t mode)
1166 {
1167         int namelen, leaf_pos;
1168         struct devfs_entry *de;
1169
1170         namelen = strlen(name);
1171         if ((*dir = _devfs_make_parent_for_leaf(*dir, name, namelen,
1172                                                 &leaf_pos)) == NULL) {
1173                 PRINTK("(%s): could not create parent path\n", name);
1174                 return NULL;
1175         }
1176         if ((de = _devfs_alloc_entry(name + leaf_pos, namelen - leaf_pos, mode))
1177             == NULL) {
1178                 PRINTK("(%s): could not allocate entry\n", name);
1179                 devfs_put(*dir);
1180                 return NULL;
1181         }
1182         return de;
1183 }                               /*  End Function _devfs_prepare_leaf  */
1184
1185 static devfs_handle_t _devfs_walk_path(struct devfs_entry *dir,
1186                                        const char *name, int namelen,
1187                                        int traverse_symlink)
1188 {
1189         int next_pos = 0;
1190
1191         if (dir == NULL)
1192                 dir = _devfs_get_root_entry();
1193         if (dir == NULL)
1194                 return NULL;
1195         devfs_get(dir);
1196         for (; namelen > 0; name += next_pos, namelen -= next_pos) {
1197                 struct devfs_entry *de, *link;
1198
1199                 if (!S_ISDIR(dir->mode)) {
1200                         devfs_put(dir);
1201                         return NULL;
1202                 }
1203
1204                 if ((de =
1205                      _devfs_descend(dir, name, namelen, &next_pos)) == NULL) {
1206                         devfs_put(dir);
1207                         return NULL;
1208                 }
1209                 if (S_ISLNK(de->mode) && traverse_symlink) {    /*  Need to follow the link: this is a stack chomper  */
1210                         /* FIXME what if it puts outside of mounted tree? */
1211                         link = _devfs_walk_path(dir, de->u.symlink.linkname,
1212                                                 de->u.symlink.length, TRUE);
1213                         devfs_put(de);
1214                         if (!link) {
1215                                 devfs_put(dir);
1216                                 return NULL;
1217                         }
1218                         de = link;
1219                 }
1220                 devfs_put(dir);
1221                 dir = de;
1222                 if (name[next_pos] == '/')
1223                         ++next_pos;
1224         }
1225         return dir;
1226 }                               /*  End Function _devfs_walk_path  */
1227
1228 /**
1229  *      _devfs_find_entry - Find a devfs entry.
1230  *      @dir: The handle to the parent devfs directory entry. If this is %NULL the
1231  *              name is relative to the root of the devfs.
1232  *      @name: The name of the entry. This may be %NULL.
1233  *      @traverse_symlink: If %TRUE then symbolic links are traversed.
1234  *
1235  *      Returns the devfs_entry pointer on success, else %NULL. An implicit
1236  *      devfs_get() is performed.
1237  */
1238
1239 static struct devfs_entry *_devfs_find_entry(devfs_handle_t dir,
1240                                              const char *name,
1241                                              int traverse_symlink)
1242 {
1243         unsigned int namelen = strlen(name);
1244
1245         if (name[0] == '/') {
1246                 /*  Skip leading pathname component  */
1247                 if (namelen < 2) {
1248                         PRINTK("(%s): too short\n", name);
1249                         return NULL;
1250                 }
1251                 for (++name, --namelen; (*name != '/') && (namelen > 0);
1252                      ++name, --namelen) ;
1253                 if (namelen < 2) {
1254                         PRINTK("(%s): too short\n", name);
1255                         return NULL;
1256                 }
1257                 ++name;
1258                 --namelen;
1259         }
1260         return _devfs_walk_path(dir, name, namelen, traverse_symlink);
1261 }                               /*  End Function _devfs_find_entry  */
1262
1263 static struct devfs_entry *get_devfs_entry_from_vfs_inode(struct inode *inode)
1264 {
1265         if (inode == NULL)
1266                 return NULL;
1267         VERIFY_ENTRY((struct devfs_entry *)inode->u.generic_ip);
1268         return inode->u.generic_ip;
1269 }                               /*  End Function get_devfs_entry_from_vfs_inode  */
1270
1271 /**
1272  *      free_dentry - Free the dentry for a device entry and invalidate inode.
1273  *      @de: The entry.
1274  *
1275  *      This must only be called after the entry has been unhooked from its
1276  *       parent directory.
1277  */
1278
1279 static void free_dentry(struct devfs_entry *de)
1280 {
1281         struct dentry *dentry = de->inode.dentry;
1282
1283         if (!dentry)
1284                 return;
1285         spin_lock(&dcache_lock);
1286         dget_locked(dentry);
1287         spin_unlock(&dcache_lock);
1288         /*  Forcefully remove the inode  */
1289         if (dentry->d_inode != NULL)
1290                 dentry->d_inode->i_nlink = 0;
1291         d_drop(dentry);
1292         dput(dentry);
1293 }                               /*  End Function free_dentry  */
1294
1295 /**
1296  *      is_devfsd_or_child - Test if the current process is devfsd or one of its children.
1297  *      @fs_info: The filesystem information.
1298  *
1299  *      Returns %TRUE if devfsd or child, else %FALSE.
1300  */
1301
1302 static int is_devfsd_or_child(struct fs_info *fs_info)
1303 {
1304         struct task_struct *p = current;
1305
1306         if (p == fs_info->devfsd_task)
1307                 return (TRUE);
1308         if (process_group(p) == fs_info->devfsd_pgrp)
1309                 return (TRUE);
1310         read_lock(&tasklist_lock);
1311         for (; p != &init_task; p = p->real_parent) {
1312                 if (p == fs_info->devfsd_task) {
1313                         read_unlock(&tasklist_lock);
1314                         return (TRUE);
1315                 }
1316         }
1317         read_unlock(&tasklist_lock);
1318         return (FALSE);
1319 }                               /*  End Function is_devfsd_or_child  */
1320
1321 /**
1322  *      devfsd_queue_empty - Test if devfsd has work pending in its event queue.
1323  *      @fs_info: The filesystem information.
1324  *
1325  *      Returns %TRUE if the queue is empty, else %FALSE.
1326  */
1327
1328 static inline int devfsd_queue_empty(struct fs_info *fs_info)
1329 {
1330         return (fs_info->devfsd_last_event) ? FALSE : TRUE;
1331 }                               /*  End Function devfsd_queue_empty  */
1332
1333 /**
1334  *      wait_for_devfsd_finished - Wait for devfsd to finish processing its event queue.
1335  *      @fs_info: The filesystem information.
1336  *
1337  *      Returns %TRUE if no more waiting will be required, else %FALSE.
1338  */
1339
1340 static int wait_for_devfsd_finished(struct fs_info *fs_info)
1341 {
1342         DECLARE_WAITQUEUE(wait, current);
1343
1344         if (fs_info->devfsd_task == NULL)
1345                 return (TRUE);
1346         if (devfsd_queue_empty(fs_info) && fs_info->devfsd_sleeping)
1347                 return TRUE;
1348         if (is_devfsd_or_child(fs_info))
1349                 return (FALSE);
1350         set_current_state(TASK_UNINTERRUPTIBLE);
1351         add_wait_queue(&fs_info->revalidate_wait_queue, &wait);
1352         if (!devfsd_queue_empty(fs_info) || !fs_info->devfsd_sleeping)
1353                 if (fs_info->devfsd_task)
1354                         schedule();
1355         remove_wait_queue(&fs_info->revalidate_wait_queue, &wait);
1356         __set_current_state(TASK_RUNNING);
1357         return (TRUE);
1358 }                               /*  End Function wait_for_devfsd_finished  */
1359
1360 /**
1361  *      devfsd_notify_de - Notify the devfsd daemon of a change.
1362  *      @de: The devfs entry that has changed. This and all parent entries will
1363  *            have their reference counts incremented if the event was queued.
1364  *      @type: The type of change.
1365  *      @mode: The mode of the entry.
1366  *      @uid: The user ID.
1367  *      @gid: The group ID.
1368  *      @fs_info: The filesystem info.
1369  *
1370  *      Returns %TRUE if an event was queued and devfsd woken up, else %FALSE.
1371  */
1372
1373 static int devfsd_notify_de(struct devfs_entry *de,
1374                             unsigned short type, umode_t mode,
1375                             uid_t uid, gid_t gid, struct fs_info *fs_info)
1376 {
1377         struct devfsd_buf_entry *entry;
1378         struct devfs_entry *curr;
1379
1380         if (!(fs_info->devfsd_event_mask & (1 << type)))
1381                 return (FALSE);
1382         if ((entry = kmem_cache_alloc(devfsd_buf_cache, SLAB_KERNEL)) == NULL) {
1383                 atomic_inc(&fs_info->devfsd_overrun_count);
1384                 return (FALSE);
1385         }
1386         for (curr = de; curr != NULL; curr = curr->parent)
1387                 devfs_get(curr);
1388         entry->de = de;
1389         entry->type = type;
1390         entry->mode = mode;
1391         entry->uid = uid;
1392         entry->gid = gid;
1393         entry->next = NULL;
1394         spin_lock(&fs_info->devfsd_buffer_lock);
1395         if (!fs_info->devfsd_first_event)
1396                 fs_info->devfsd_first_event = entry;
1397         if (fs_info->devfsd_last_event)
1398                 fs_info->devfsd_last_event->next = entry;
1399         fs_info->devfsd_last_event = entry;
1400         spin_unlock(&fs_info->devfsd_buffer_lock);
1401         wake_up_interruptible(&fs_info->devfsd_wait_queue);
1402         return (TRUE);
1403 }                               /*  End Function devfsd_notify_de  */
1404
1405 /**
1406  *      devfsd_notify - Notify the devfsd daemon of a change.
1407  *      @de: The devfs entry that has changed.
1408  *      @type: The type of change event.
1409  *      @wait: If TRUE, the function waits for the daemon to finish processing
1410  *              the event.
1411  */
1412
1413 static void devfsd_notify(struct devfs_entry *de, unsigned short type)
1414 {
1415         devfsd_notify_de(de, type, de->mode, current->euid,
1416                          current->egid, &fs_info);
1417 }
1418
1419 static int devfs_mk_dev(dev_t dev, umode_t mode, const char *fmt, va_list args)
1420 {
1421         struct devfs_entry *dir = NULL, *de;
1422         char buf[64];
1423         int error, n;
1424
1425         n = vsnprintf(buf, sizeof(buf), fmt, args);
1426         if (n >= sizeof(buf) || !buf[0]) {
1427                 printk(KERN_WARNING "%s: invalid format string %s\n",
1428                        __FUNCTION__, fmt);
1429                 return -EINVAL;
1430         }
1431
1432         de = _devfs_prepare_leaf(&dir, buf, mode);
1433         if (!de) {
1434                 printk(KERN_WARNING "%s: could not prepare leaf for %s\n",
1435                        __FUNCTION__, buf);
1436                 return -ENOMEM; /* could be more accurate... */
1437         }
1438
1439         de->u.dev = dev;
1440
1441         error = _devfs_append_entry(dir, de, NULL);
1442         if (error) {
1443                 printk(KERN_WARNING "%s: could not append to parent for %s\n",
1444                        __FUNCTION__, buf);
1445                 goto out;
1446         }
1447
1448         devfsd_notify(de, DEVFSD_NOTIFY_REGISTERED);
1449       out:
1450         devfs_put(dir);
1451         return error;
1452 }
1453
1454 int devfs_mk_bdev(dev_t dev, umode_t mode, const char *fmt, ...)
1455 {
1456         va_list args;
1457
1458         if (!S_ISBLK(mode)) {
1459                 printk(KERN_WARNING "%s: invalide mode (%u) for %s\n",
1460                        __FUNCTION__, mode, fmt);
1461                 return -EINVAL;
1462         }
1463
1464         va_start(args, fmt);
1465         return devfs_mk_dev(dev, mode, fmt, args);
1466 }
1467
1468 EXPORT_SYMBOL(devfs_mk_bdev);
1469
1470 int devfs_mk_cdev(dev_t dev, umode_t mode, const char *fmt, ...)
1471 {
1472         va_list args;
1473
1474         if (!S_ISCHR(mode)) {
1475                 printk(KERN_WARNING "%s: invalide mode (%u) for %s\n",
1476                        __FUNCTION__, mode, fmt);
1477                 return -EINVAL;
1478         }
1479
1480         va_start(args, fmt);
1481         return devfs_mk_dev(dev, mode, fmt, args);
1482 }
1483
1484 EXPORT_SYMBOL(devfs_mk_cdev);
1485
1486 /**
1487  *      _devfs_unhook - Unhook a device entry from its parents list
1488  *      @de: The entry to unhook.
1489  *
1490  *      Returns %TRUE if the entry was unhooked, else %FALSE if it was
1491  *              previously unhooked.
1492  *      The caller must have a write lock on the parent directory.
1493  */
1494
1495 static int _devfs_unhook(struct devfs_entry *de)
1496 {
1497         struct devfs_entry *parent;
1498
1499         if (!de || (de->prev == de))
1500                 return FALSE;
1501         parent = de->parent;
1502         if (de->prev == NULL)
1503                 parent->u.dir.first = de->next;
1504         else
1505                 de->prev->next = de->next;
1506         if (de->next == NULL)
1507                 parent->u.dir.last = de->prev;
1508         else
1509                 de->next->prev = de->prev;
1510         de->prev = de;          /*  Indicate we're unhooked                      */
1511         de->next = NULL;        /*  Force early termination for <devfs_readdir>  */
1512         return TRUE;
1513 }                               /*  End Function _devfs_unhook  */
1514
1515 /**
1516  *      _devfs_unregister - Unregister a device entry from its parent.
1517  *      @dir: The parent directory.
1518  *      @de: The entry to unregister.
1519  *
1520  *      The caller must have a write lock on the parent directory, which is
1521  *      unlocked by this function.
1522  */
1523
1524 static void _devfs_unregister(struct devfs_entry *dir, struct devfs_entry *de)
1525 {
1526         int unhooked = _devfs_unhook(de);
1527
1528         write_unlock(&dir->u.dir.lock);
1529         if (!unhooked)
1530                 return;
1531         devfs_get(dir);
1532         devfsd_notify(de, DEVFSD_NOTIFY_UNREGISTERED);
1533         free_dentry(de);
1534         devfs_put(dir);
1535         if (!S_ISDIR(de->mode))
1536                 return;
1537         while (TRUE) {          /*  Recursively unregister: this is a stack chomper  */
1538                 struct devfs_entry *child;
1539
1540                 write_lock(&de->u.dir.lock);
1541                 de->u.dir.no_more_additions = TRUE;
1542                 child = de->u.dir.first;
1543                 VERIFY_ENTRY(child);
1544                 _devfs_unregister(de, child);
1545                 if (!child)
1546                         break;
1547                 DPRINTK(DEBUG_UNREGISTER, "(%s): child: %p  refcount: %d\n",
1548                         child->name, child, atomic_read(&child->refcount));
1549                 devfs_put(child);
1550         }
1551 }                               /*  End Function _devfs_unregister  */
1552
1553 static int devfs_do_symlink(devfs_handle_t dir, const char *name,
1554                             const char *link, devfs_handle_t * handle)
1555 {
1556         int err;
1557         unsigned int linklength;
1558         char *newlink;
1559         struct devfs_entry *de;
1560
1561         if (handle != NULL)
1562                 *handle = NULL;
1563         if (name == NULL) {
1564                 PRINTK("(): NULL name pointer\n");
1565                 return -EINVAL;
1566         }
1567         if (link == NULL) {
1568                 PRINTK("(%s): NULL link pointer\n", name);
1569                 return -EINVAL;
1570         }
1571         linklength = strlen(link);
1572         if ((newlink = kmalloc(linklength + 1, GFP_KERNEL)) == NULL)
1573                 return -ENOMEM;
1574         memcpy(newlink, link, linklength);
1575         newlink[linklength] = '\0';
1576         if ((de = _devfs_prepare_leaf(&dir, name, S_IFLNK | S_IRUGO | S_IXUGO))
1577             == NULL) {
1578                 PRINTK("(%s): could not prepare leaf\n", name);
1579                 kfree(newlink);
1580                 return -ENOTDIR;
1581         }
1582         de->info = NULL;
1583         de->u.symlink.linkname = newlink;
1584         de->u.symlink.length = linklength;
1585         if ((err = _devfs_append_entry(dir, de, NULL)) != 0) {
1586                 PRINTK("(%s): could not append to parent, err: %d\n", name,
1587                        err);
1588                 devfs_put(dir);
1589                 return err;
1590         }
1591         devfs_put(dir);
1592 #ifdef CONFIG_DEVFS_DEBUG
1593         spin_lock(&stat_lock);
1594         stat_num_bytes += linklength + 1;
1595         spin_unlock(&stat_lock);
1596 #endif
1597         if (handle != NULL)
1598                 *handle = de;
1599         return 0;
1600 }                               /*  End Function devfs_do_symlink  */
1601
1602 /**
1603  *      devfs_mk_symlink Create a symbolic link in the devfs namespace.
1604  *      @from: The name of the entry.
1605  *      @to: Name of the destination
1606  *
1607  *      Returns 0 on success, else a negative error code is returned.
1608  */
1609
1610 int devfs_mk_symlink(const char *from, const char *to)
1611 {
1612         devfs_handle_t de;
1613         int err;
1614
1615         err = devfs_do_symlink(NULL, from, to, &de);
1616         if (!err) {
1617                 de->vfs = TRUE;
1618                 devfsd_notify(de, DEVFSD_NOTIFY_REGISTERED);
1619         }
1620
1621         return err;
1622 }
1623
1624 /**
1625  *      devfs_mk_dir - Create a directory in the devfs namespace.
1626  *              new name is relative to the root of the devfs.
1627  *      @fmt: The name of the entry.
1628  *
1629  *      Use of this function is optional. The devfs_register() function
1630  *      will automatically create intermediate directories as needed. This function
1631  *      is provided for efficiency reasons, as it provides a handle to a directory.
1632  *      On failure %NULL is returned.
1633  */
1634
1635 int devfs_mk_dir(const char *fmt, ...)
1636 {
1637         struct devfs_entry *dir = NULL, *de = NULL, *old;
1638         char buf[64];
1639         va_list args;
1640         int error, n;
1641
1642         va_start(args, fmt);
1643         n = vsnprintf(buf, 64, fmt, args);
1644         if (n >= 64 || !buf[0]) {
1645                 printk(KERN_WARNING "%s: invalid argument.", __FUNCTION__);
1646                 return -EINVAL;
1647         }
1648
1649         de = _devfs_prepare_leaf(&dir, buf, MODE_DIR);
1650         if (!de) {
1651                 PRINTK("(%s): could not prepare leaf\n", buf);
1652                 return -EINVAL;
1653         }
1654
1655         error = _devfs_append_entry(dir, de, &old);
1656         if (error == -EEXIST && S_ISDIR(old->mode)) {
1657                 /*
1658                  * devfs_mk_dir() of an already-existing directory will
1659                  * return success.
1660                  */
1661                 error = 0;
1662                 goto out_put;
1663         } else if (error) {
1664                 PRINTK("(%s): could not append to dir: %p \"%s\"\n",
1665                        buf, dir, dir->name);
1666                 devfs_put(old);
1667                 goto out_put;
1668         }
1669
1670         devfsd_notify(de, DEVFSD_NOTIFY_REGISTERED);
1671
1672       out_put:
1673         devfs_put(dir);
1674         return error;
1675 }
1676
1677 void devfs_remove(const char *fmt, ...)
1678 {
1679         char buf[64];
1680         va_list args;
1681         int n;
1682
1683         va_start(args, fmt);
1684         n = vsnprintf(buf, sizeof(buf), fmt, args);
1685         if (n < sizeof(buf) && buf[0]) {
1686                 devfs_handle_t de = _devfs_find_entry(NULL, buf, 0);
1687
1688                 if (!de) {
1689                         printk(KERN_ERR "%s: %s not found, cannot remove\n",
1690                                __FUNCTION__, buf);
1691                         dump_stack();
1692                         return;
1693                 }
1694
1695                 write_lock(&de->parent->u.dir.lock);
1696                 _devfs_unregister(de->parent, de);
1697                 devfs_put(de);
1698                 devfs_put(de);
1699         }
1700 }
1701
1702 /**
1703  *      devfs_generate_path - Generate a pathname for an entry, relative to the devfs root.
1704  *      @de: The devfs entry.
1705  *      @path: The buffer to write the pathname to. The pathname and '\0'
1706  *              terminator will be written at the end of the buffer.
1707  *      @buflen: The length of the buffer.
1708  *
1709  *      Returns the offset in the buffer where the pathname starts on success,
1710  *      else a negative error code.
1711  */
1712
1713 static int devfs_generate_path(devfs_handle_t de, char *path, int buflen)
1714 {
1715         int pos;
1716 #define NAMEOF(de) ( (de)->mode ? (de)->name : (de)->u.name )
1717
1718         if (de == NULL)
1719                 return -EINVAL;
1720         VERIFY_ENTRY(de);
1721         if (de->namelen >= buflen)
1722                 return -ENAMETOOLONG;   /*  Must be first       */
1723         path[buflen - 1] = '\0';
1724         if (de->parent == NULL)
1725                 return buflen - 1;      /*  Don't prepend root  */
1726         pos = buflen - de->namelen - 1;
1727         memcpy(path + pos, NAMEOF(de), de->namelen);
1728         for (de = de->parent; de->parent != NULL; de = de->parent) {
1729                 if (pos - de->namelen - 1 < 0)
1730                         return -ENAMETOOLONG;
1731                 path[--pos] = '/';
1732                 pos -= de->namelen;
1733                 memcpy(path + pos, NAMEOF(de), de->namelen);
1734         }
1735         return pos;
1736 }                               /*  End Function devfs_generate_path  */
1737
1738 /**
1739  *      devfs_setup - Process kernel boot options.
1740  *      @str: The boot options after the "devfs=".
1741  */
1742
1743 static int __init devfs_setup(char *str)
1744 {
1745         static struct {
1746                 char *name;
1747                 unsigned int mask;
1748                 unsigned int *opt;
1749         } devfs_options_tab[] __initdata = {
1750 #ifdef CONFIG_DEVFS_DEBUG
1751                 {
1752                 "dall", DEBUG_ALL, &devfs_debug_init}, {
1753                 "dmod", DEBUG_MODULE_LOAD, &devfs_debug_init}, {
1754                 "dreg", DEBUG_REGISTER, &devfs_debug_init}, {
1755                 "dunreg", DEBUG_UNREGISTER, &devfs_debug_init}, {
1756                 "dfree", DEBUG_FREE, &devfs_debug_init}, {
1757                 "diget", DEBUG_I_GET, &devfs_debug_init}, {
1758                 "dchange", DEBUG_SET_FLAGS, &devfs_debug_init}, {
1759                 "dsread", DEBUG_S_READ, &devfs_debug_init}, {
1760                 "dichange", DEBUG_I_CHANGE, &devfs_debug_init}, {
1761                 "dimknod", DEBUG_I_MKNOD, &devfs_debug_init}, {
1762                 "dilookup", DEBUG_I_LOOKUP, &devfs_debug_init}, {
1763                 "diunlink", DEBUG_I_UNLINK, &devfs_debug_init},
1764 #endif                          /*  CONFIG_DEVFS_DEBUG  */
1765                 {
1766                 "mount", OPTION_MOUNT, &boot_options}, {
1767                 NULL, 0, NULL}
1768         };
1769
1770         while ((*str != '\0') && !isspace(*str)) {
1771                 int i, found = 0, invert = 0;
1772
1773                 if (strncmp(str, "no", 2) == 0) {
1774                         invert = 1;
1775                         str += 2;
1776                 }
1777                 for (i = 0; devfs_options_tab[i].name != NULL; i++) {
1778                         int len = strlen(devfs_options_tab[i].name);
1779
1780                         if (strncmp(str, devfs_options_tab[i].name, len) == 0) {
1781                                 if (invert)
1782                                         *devfs_options_tab[i].opt &=
1783                                             ~devfs_options_tab[i].mask;
1784                                 else
1785                                         *devfs_options_tab[i].opt |=
1786                                             devfs_options_tab[i].mask;
1787                                 str += len;
1788                                 found = 1;
1789                                 break;
1790                         }
1791                 }
1792                 if (!found)
1793                         return 0;       /*  No match         */
1794                 if (*str != ',')
1795                         return 0;       /*  No more options  */
1796                 ++str;
1797         }
1798         return 1;
1799 }                               /*  End Function devfs_setup  */
1800
1801 __setup("devfs=", devfs_setup);
1802
1803 EXPORT_SYMBOL(devfs_mk_symlink);
1804 EXPORT_SYMBOL(devfs_mk_dir);
1805 EXPORT_SYMBOL(devfs_remove);
1806
1807 /**
1808  *      try_modload - Notify devfsd of an inode lookup by a non-devfsd process.
1809  *      @parent: The parent devfs entry.
1810  *      @fs_info: The filesystem info.
1811  *      @name: The device name.
1812  *      @namelen: The number of characters in @name.
1813  *      @buf: A working area that will be used. This must not go out of scope
1814  *            until devfsd is idle again.
1815  *
1816  *      Returns 0 on success (event was queued), else a negative error code.
1817  */
1818
1819 static int try_modload(struct devfs_entry *parent, struct fs_info *fs_info,
1820                        const char *name, unsigned namelen,
1821                        struct devfs_entry *buf)
1822 {
1823         if (!(fs_info->devfsd_event_mask & (1 << DEVFSD_NOTIFY_LOOKUP)))
1824                 return -ENOENT;
1825         if (is_devfsd_or_child(fs_info))
1826                 return -ENOENT;
1827         memset(buf, 0, sizeof *buf);
1828         atomic_set(&buf->refcount, 1);
1829         buf->parent = parent;
1830         buf->namelen = namelen;
1831         buf->u.name = name;
1832         WRITE_ENTRY_MAGIC(buf, MAGIC_VALUE);
1833         if (!devfsd_notify_de(buf, DEVFSD_NOTIFY_LOOKUP, 0,
1834                               current->euid, current->egid, fs_info))
1835                 return -ENOENT;
1836         /*  Possible success: event has been queued  */
1837         return 0;
1838 }                               /*  End Function try_modload  */
1839
1840 /*  Superblock operations follow  */
1841
1842 static struct inode_operations devfs_iops;
1843 static struct inode_operations devfs_dir_iops;
1844 static struct file_operations devfs_fops;
1845 static struct file_operations devfs_dir_fops;
1846 static struct inode_operations devfs_symlink_iops;
1847
1848 static int devfs_notify_change(struct dentry *dentry, struct iattr *iattr)
1849 {
1850         int retval;
1851         struct devfs_entry *de;
1852         struct inode *inode = dentry->d_inode;
1853         struct fs_info *fs_info = inode->i_sb->s_fs_info;
1854
1855         de = get_devfs_entry_from_vfs_inode(inode);
1856         if (de == NULL)
1857                 return -ENODEV;
1858         retval = inode_change_ok(inode, iattr);
1859         if (retval != 0)
1860                 return retval;
1861         retval = inode_setattr(inode, iattr);
1862         if (retval != 0)
1863                 return retval;
1864         DPRINTK(DEBUG_I_CHANGE, "(%d): VFS inode: %p  devfs_entry: %p\n",
1865                 (int)inode->i_ino, inode, de);
1866         DPRINTK(DEBUG_I_CHANGE, "():   mode: 0%o  uid: %d  gid: %d\n",
1867                 (int)inode->i_mode, (int)inode->i_uid, (int)inode->i_gid);
1868         /*  Inode is not on hash chains, thus must save permissions here rather
1869            than in a write_inode() method  */
1870         de->mode = inode->i_mode;
1871         de->inode.uid = inode->i_uid;
1872         de->inode.gid = inode->i_gid;
1873         de->inode.atime = inode->i_atime;
1874         de->inode.mtime = inode->i_mtime;
1875         de->inode.ctime = inode->i_ctime;
1876         if ((iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) &&
1877             !is_devfsd_or_child(fs_info))
1878                 devfsd_notify_de(de, DEVFSD_NOTIFY_CHANGE, inode->i_mode,
1879                                  inode->i_uid, inode->i_gid, fs_info);
1880         return 0;
1881 }                               /*  End Function devfs_notify_change  */
1882
1883 static struct super_operations devfs_sops = {
1884         .drop_inode = generic_delete_inode,
1885         .statfs = simple_statfs,
1886 };
1887
1888 /**
1889  *      _devfs_get_vfs_inode - Get a VFS inode.
1890  *      @sb: The super block.
1891  *      @de: The devfs inode.
1892  *      @dentry: The dentry to register with the devfs inode.
1893  *
1894  *      Returns the inode on success, else %NULL. An implicit devfs_get() is
1895  *       performed if the inode is created.
1896  */
1897
1898 static struct inode *_devfs_get_vfs_inode(struct super_block *sb,
1899                                           struct devfs_entry *de,
1900                                           struct dentry *dentry)
1901 {
1902         struct inode *inode;
1903
1904         if (de->prev == de)
1905                 return NULL;    /*  Quick check to see if unhooked  */
1906         if ((inode = new_inode(sb)) == NULL) {
1907                 PRINTK("(%s): new_inode() failed, de: %p\n", de->name, de);
1908                 return NULL;
1909         }
1910         if (de->parent) {
1911                 read_lock(&de->parent->u.dir.lock);
1912                 if (de->prev != de)
1913                         de->inode.dentry = dentry;      /*      Not unhooked  */
1914                 read_unlock(&de->parent->u.dir.lock);
1915         } else
1916                 de->inode.dentry = dentry;      /*  Root: no locking needed  */
1917         if (de->inode.dentry != dentry) {       /*  Must have been unhooked  */
1918                 iput(inode);
1919                 return NULL;
1920         }
1921         /* FIXME where is devfs_put? */
1922         inode->u.generic_ip = devfs_get(de);
1923         inode->i_ino = de->inode.ino;
1924         DPRINTK(DEBUG_I_GET, "(%d): VFS inode: %p  devfs_entry: %p\n",
1925                 (int)inode->i_ino, inode, de);
1926         inode->i_blocks = 0;
1927         inode->i_blksize = FAKE_BLOCK_SIZE;
1928         inode->i_op = &devfs_iops;
1929         inode->i_mode = de->mode;
1930         if (S_ISDIR(de->mode)) {
1931                 inode->i_op = &devfs_dir_iops;
1932                 inode->i_fop = &devfs_dir_fops;
1933         } else if (S_ISLNK(de->mode)) {
1934                 inode->i_op = &devfs_symlink_iops;
1935                 inode->i_size = de->u.symlink.length;
1936         } else if (S_ISCHR(de->mode) || S_ISBLK(de->mode)) {
1937                 init_special_inode(inode, de->mode, de->u.dev);
1938         } else if (S_ISFIFO(de->mode) || S_ISSOCK(de->mode)) {
1939                 init_special_inode(inode, de->mode, 0);
1940         } else {
1941                 PRINTK("(%s): unknown mode %o de: %p\n",
1942                        de->name, de->mode, de);
1943                 iput(inode);
1944                 devfs_put(de);
1945                 return NULL;
1946         }
1947
1948         inode->i_uid = de->inode.uid;
1949         inode->i_gid = de->inode.gid;
1950         inode->i_atime = de->inode.atime;
1951         inode->i_mtime = de->inode.mtime;
1952         inode->i_ctime = de->inode.ctime;
1953         DPRINTK(DEBUG_I_GET, "():   mode: 0%o  uid: %d  gid: %d\n",
1954                 (int)inode->i_mode, (int)inode->i_uid, (int)inode->i_gid);
1955         return inode;
1956 }                               /*  End Function _devfs_get_vfs_inode  */
1957
1958 /*  File operations for device entries follow  */
1959
1960 static int devfs_readdir(struct file *file, void *dirent, filldir_t filldir)
1961 {
1962         int err, count;
1963         int stored = 0;
1964         struct fs_info *fs_info;
1965         struct devfs_entry *parent, *de, *next = NULL;
1966         struct inode *inode = file->f_dentry->d_inode;
1967
1968         fs_info = inode->i_sb->s_fs_info;
1969         parent = get_devfs_entry_from_vfs_inode(file->f_dentry->d_inode);
1970         if ((long)file->f_pos < 0)
1971                 return -EINVAL;
1972         DPRINTK(DEBUG_F_READDIR, "(%s): fs_info: %p  pos: %ld\n",
1973                 parent->name, fs_info, (long)file->f_pos);
1974         switch ((long)file->f_pos) {
1975         case 0:
1976                 err = (*filldir) (dirent, "..", 2, file->f_pos,
1977                                   parent_ino(file->f_dentry), DT_DIR);
1978                 if (err == -EINVAL)
1979                         break;
1980                 if (err < 0)
1981                         return err;
1982                 file->f_pos++;
1983                 ++stored;
1984                 /*  Fall through  */
1985         case 1:
1986                 err =
1987                     (*filldir) (dirent, ".", 1, file->f_pos, inode->i_ino,
1988                                 DT_DIR);
1989                 if (err == -EINVAL)
1990                         break;
1991                 if (err < 0)
1992                         return err;
1993                 file->f_pos++;
1994                 ++stored;
1995                 /*  Fall through  */
1996         default:
1997                 /*  Skip entries  */
1998                 count = file->f_pos - 2;
1999                 read_lock(&parent->u.dir.lock);
2000                 for (de = parent->u.dir.first; de && (count > 0); de = de->next)
2001                         --count;
2002                 devfs_get(de);
2003                 read_unlock(&parent->u.dir.lock);
2004                 /*  Now add all remaining entries  */
2005                 while (de) {
2006                         err = (*filldir) (dirent, de->name, de->namelen,
2007                                           file->f_pos, de->inode.ino,
2008                                           de->mode >> 12);
2009                         if (err < 0)
2010                                 devfs_put(de);
2011                         else {
2012                                 file->f_pos++;
2013                                 ++stored;
2014                         }
2015                         if (err == -EINVAL)
2016                                 break;
2017                         if (err < 0)
2018                                 return err;
2019                         read_lock(&parent->u.dir.lock);
2020                         next = devfs_get(de->next);
2021                         read_unlock(&parent->u.dir.lock);
2022                         devfs_put(de);
2023                         de = next;
2024                 }
2025                 break;
2026         }
2027         return stored;
2028 }                               /*  End Function devfs_readdir  */
2029
2030 /* Open devfs specific special files */
2031 static int devfs_open(struct inode *inode, struct file *file)
2032 {
2033         int err;
2034         int minor = MINOR(inode->i_rdev);
2035         struct file_operations *old_fops, *new_fops;
2036
2037         switch (minor) {
2038         case 0:         /* /dev/.devfsd */
2039                 new_fops = fops_get(&devfsd_fops);
2040                 break;
2041 #ifdef CONFIG_DEVFS_DEBUG
2042         case 1:         /* /dev/.stat */
2043                 new_fops = fops_get(&stat_fops);
2044                 break;
2045 #endif
2046         default:
2047                 return -ENODEV;
2048         }
2049
2050         if (new_fops == NULL)
2051                 return -ENODEV;
2052         old_fops = file->f_op;
2053         file->f_op = new_fops;
2054         err = new_fops->open ? new_fops->open(inode, file) : 0;
2055         if (err) {
2056                 file->f_op = old_fops;
2057                 fops_put(new_fops);
2058         } else
2059                 fops_put(old_fops);
2060         return err;
2061 }                               /*  End Function devfs_open  */
2062
2063 static struct file_operations devfs_fops = {
2064         .open = devfs_open,
2065 };
2066
2067 static struct file_operations devfs_dir_fops = {
2068         .read = generic_read_dir,
2069         .readdir = devfs_readdir,
2070 };
2071
2072 /*  Dentry operations for device entries follow  */
2073
2074 /**
2075  *      devfs_d_release - Callback for when a dentry is freed.
2076  *      @dentry: The dentry.
2077  */
2078
2079 static void devfs_d_release(struct dentry *dentry)
2080 {
2081         DPRINTK(DEBUG_D_RELEASE, "(%p): inode: %p\n", dentry, dentry->d_inode);
2082 }                               /*  End Function devfs_d_release  */
2083
2084 /**
2085  *      devfs_d_iput - Callback for when a dentry loses its inode.
2086  *      @dentry: The dentry.
2087  *      @inode: The inode.
2088  */
2089
2090 static void devfs_d_iput(struct dentry *dentry, struct inode *inode)
2091 {
2092         struct devfs_entry *de;
2093
2094         de = get_devfs_entry_from_vfs_inode(inode);
2095         DPRINTK(DEBUG_D_IPUT,
2096                 "(%s): dentry: %p inode: %p de: %p de->dentry: %p\n", de->name,
2097                 dentry, inode, de, de->inode.dentry);
2098         if (de->inode.dentry && (de->inode.dentry != dentry))
2099                 OOPS("(%s): de: %p dentry: %p de->dentry: %p\n",
2100                      de->name, de, dentry, de->inode.dentry);
2101         de->inode.dentry = NULL;
2102         iput(inode);
2103         devfs_put(de);
2104 }                               /*  End Function devfs_d_iput  */
2105
2106 static int devfs_d_delete(struct dentry *dentry);
2107
2108 static struct dentry_operations devfs_dops = {
2109         .d_delete = devfs_d_delete,
2110         .d_release = devfs_d_release,
2111         .d_iput = devfs_d_iput,
2112 };
2113
2114 static int devfs_d_revalidate_wait(struct dentry *dentry, struct nameidata *);
2115
2116 static struct dentry_operations devfs_wait_dops = {
2117         .d_delete = devfs_d_delete,
2118         .d_release = devfs_d_release,
2119         .d_iput = devfs_d_iput,
2120         .d_revalidate = devfs_d_revalidate_wait,
2121 };
2122
2123 /**
2124  *      devfs_d_delete - Callback for when all files for a dentry are closed.
2125  *      @dentry: The dentry.
2126  */
2127
2128 static int devfs_d_delete(struct dentry *dentry)
2129 {
2130         struct inode *inode = dentry->d_inode;
2131
2132         if (dentry->d_op == &devfs_wait_dops)
2133                 dentry->d_op = &devfs_dops;
2134         /*  Unhash dentry if negative (has no inode)  */
2135         if (inode == NULL) {
2136                 DPRINTK(DEBUG_D_DELETE, "(%p): dropping negative dentry\n",
2137                         dentry);
2138                 return 1;
2139         }
2140         return 0;
2141 }                               /*  End Function devfs_d_delete  */
2142
2143 struct devfs_lookup_struct {
2144         devfs_handle_t de;
2145         wait_queue_head_t wait_queue;
2146 };
2147
2148 /* XXX: this doesn't handle the case where we got a negative dentry
2149         but a devfs entry has been registered in the meanwhile */
2150 static int devfs_d_revalidate_wait(struct dentry *dentry, struct nameidata *nd)
2151 {
2152         struct inode *dir = dentry->d_parent->d_inode;
2153         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2154         devfs_handle_t parent = get_devfs_entry_from_vfs_inode(dir);
2155         struct devfs_lookup_struct *lookup_info = dentry->d_fsdata;
2156         DECLARE_WAITQUEUE(wait, current);
2157         int need_lock;
2158
2159         /*
2160          * FIXME HACK
2161          *
2162          * make sure that
2163          *   d_instantiate always runs under lock
2164          *   we release i_sem lock before going to sleep
2165          *
2166          * unfortunately sometimes d_revalidate is called with
2167          * and sometimes without i_sem lock held. The following checks
2168          * attempt to deduce when we need to add (and drop resp.) lock
2169          * here. This relies on current (2.6.2) calling coventions:
2170          *
2171          *   lookup_hash is always run under i_sem and is passing NULL
2172          *   as nd
2173          *
2174          *   open(...,O_CREATE,...) calls _lookup_hash under i_sem
2175          *   and sets flags to LOOKUP_OPEN|LOOKUP_CREATE
2176          *
2177          *   all other invocations of ->d_revalidate seem to happen
2178          *   outside of i_sem
2179          */
2180         need_lock = nd &&
2181             (!(nd->flags & LOOKUP_CREATE) || (nd->flags & LOOKUP_PARENT));
2182
2183         if (need_lock)
2184                 down(&dir->i_sem);
2185
2186         if (is_devfsd_or_child(fs_info)) {
2187                 devfs_handle_t de = lookup_info->de;
2188                 struct inode *inode;
2189
2190                 DPRINTK(DEBUG_I_LOOKUP,
2191                         "(%s): dentry: %p inode: %p de: %p by: \"%s\"\n",
2192                         dentry->d_name.name, dentry, dentry->d_inode, de,
2193                         current->comm);
2194                 if (dentry->d_inode)
2195                         goto out;
2196                 if (de == NULL) {
2197                         read_lock(&parent->u.dir.lock);
2198                         de = _devfs_search_dir(parent, dentry->d_name.name,
2199                                                dentry->d_name.len);
2200                         read_unlock(&parent->u.dir.lock);
2201                         if (de == NULL)
2202                                 goto out;
2203                         lookup_info->de = de;
2204                 }
2205                 /*  Create an inode, now that the driver information is available  */
2206                 inode = _devfs_get_vfs_inode(dir->i_sb, de, dentry);
2207                 if (!inode)
2208                         goto out;
2209                 DPRINTK(DEBUG_I_LOOKUP,
2210                         "(%s): new VFS inode(%u): %p de: %p by: \"%s\"\n",
2211                         de->name, de->inode.ino, inode, de, current->comm);
2212                 d_instantiate(dentry, inode);
2213                 goto out;
2214         }
2215         if (lookup_info == NULL)
2216                 goto out;       /*  Early termination  */
2217         read_lock(&parent->u.dir.lock);
2218         if (dentry->d_fsdata) {
2219                 set_current_state(TASK_UNINTERRUPTIBLE);
2220                 add_wait_queue(&lookup_info->wait_queue, &wait);
2221                 read_unlock(&parent->u.dir.lock);
2222                 /* at this point it is always (hopefully) locked */
2223                 up(&dir->i_sem);
2224                 schedule();
2225                 down(&dir->i_sem);
2226                 /*
2227                  * This does not need nor should remove wait from wait_queue.
2228                  * Wait queue head is never reused - nothing is ever added to it
2229                  * after all waiters have been waked up and head itself disappears
2230                  * very soon after it. Moreover it is local variable on stack that
2231                  * is likely to have already disappeared so any reference to it
2232                  * at this point is buggy.
2233                  */
2234
2235         } else
2236                 read_unlock(&parent->u.dir.lock);
2237
2238       out:
2239         if (need_lock)
2240                 up(&dir->i_sem);
2241         return 1;
2242 }                               /*  End Function devfs_d_revalidate_wait  */
2243
2244 /*  Inode operations for device entries follow  */
2245
2246 static struct dentry *devfs_lookup(struct inode *dir, struct dentry *dentry,
2247                                    struct nameidata *nd)
2248 {
2249         struct devfs_entry tmp; /*  Must stay in scope until devfsd idle again  */
2250         struct devfs_lookup_struct lookup_info;
2251         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2252         struct devfs_entry *parent, *de;
2253         struct inode *inode;
2254         struct dentry *retval = NULL;
2255
2256         /*  Set up the dentry operations before anything else, to ensure cleaning
2257            up on any error  */
2258         dentry->d_op = &devfs_dops;
2259         /*  First try to get the devfs entry for this directory  */
2260         parent = get_devfs_entry_from_vfs_inode(dir);
2261         DPRINTK(DEBUG_I_LOOKUP, "(%s): dentry: %p parent: %p by: \"%s\"\n",
2262                 dentry->d_name.name, dentry, parent, current->comm);
2263         if (parent == NULL)
2264                 return ERR_PTR(-ENOENT);
2265         read_lock(&parent->u.dir.lock);
2266         de = _devfs_search_dir(parent, dentry->d_name.name, dentry->d_name.len);
2267         read_unlock(&parent->u.dir.lock);
2268         lookup_info.de = de;
2269         init_waitqueue_head(&lookup_info.wait_queue);
2270         dentry->d_fsdata = &lookup_info;
2271         if (de == NULL) {       /*  Try with devfsd. For any kind of failure, leave a negative dentry
2272                                    so someone else can deal with it (in the case where the sysadmin
2273                                    does a mknod()). It's important to do this before hashing the
2274                                    dentry, so that the devfsd queue is filled before revalidates
2275                                    can start  */
2276                 if (try_modload(parent, fs_info, dentry->d_name.name, dentry->d_name.len, &tmp) < 0) {  /*  Lookup event was not queued to devfsd  */
2277                         d_add(dentry, NULL);
2278                         return NULL;
2279                 }
2280         }
2281         dentry->d_op = &devfs_wait_dops;
2282         d_add(dentry, NULL);    /*  Open the floodgates  */
2283         /*  Unlock directory semaphore, which will release any waiters. They
2284            will get the hashed dentry, and may be forced to wait for
2285            revalidation  */
2286         up(&dir->i_sem);
2287         wait_for_devfsd_finished(fs_info);      /*  If I'm not devfsd, must wait  */
2288         down(&dir->i_sem);      /*  Grab it again because them's the rules  */
2289         de = lookup_info.de;
2290         /*  If someone else has been so kind as to make the inode, we go home
2291            early  */
2292         if (dentry->d_inode)
2293                 goto out;
2294         if (de == NULL) {
2295                 read_lock(&parent->u.dir.lock);
2296                 de = _devfs_search_dir(parent, dentry->d_name.name,
2297                                        dentry->d_name.len);
2298                 read_unlock(&parent->u.dir.lock);
2299                 if (de == NULL)
2300                         goto out;
2301                 /*  OK, there's an entry now, but no VFS inode yet  */
2302         }
2303         /*  Create an inode, now that the driver information is available  */
2304         inode = _devfs_get_vfs_inode(dir->i_sb, de, dentry);
2305         if (!inode) {
2306                 retval = ERR_PTR(-ENOMEM);
2307                 goto out;
2308         }
2309         DPRINTK(DEBUG_I_LOOKUP,
2310                 "(%s): new VFS inode(%u): %p de: %p by: \"%s\"\n", de->name,
2311                 de->inode.ino, inode, de, current->comm);
2312         d_instantiate(dentry, inode);
2313       out:
2314         write_lock(&parent->u.dir.lock);
2315         dentry->d_op = &devfs_dops;
2316         dentry->d_fsdata = NULL;
2317         wake_up(&lookup_info.wait_queue);
2318         write_unlock(&parent->u.dir.lock);
2319         devfs_put(de);
2320         return retval;
2321 }                               /*  End Function devfs_lookup  */
2322
2323 static int devfs_unlink(struct inode *dir, struct dentry *dentry)
2324 {
2325         int unhooked;
2326         struct devfs_entry *de;
2327         struct inode *inode = dentry->d_inode;
2328         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2329
2330         de = get_devfs_entry_from_vfs_inode(inode);
2331         DPRINTK(DEBUG_I_UNLINK, "(%s): de: %p\n", dentry->d_name.name, de);
2332         if (de == NULL)
2333                 return -ENOENT;
2334         if (!de->vfs)
2335                 return -EPERM;
2336         write_lock(&de->parent->u.dir.lock);
2337         unhooked = _devfs_unhook(de);
2338         write_unlock(&de->parent->u.dir.lock);
2339         if (!unhooked)
2340                 return -ENOENT;
2341         if (!is_devfsd_or_child(fs_info))
2342                 devfsd_notify_de(de, DEVFSD_NOTIFY_DELETE, inode->i_mode,
2343                                  inode->i_uid, inode->i_gid, fs_info);
2344         free_dentry(de);
2345         devfs_put(de);
2346         return 0;
2347 }                               /*  End Function devfs_unlink  */
2348
2349 static int devfs_symlink(struct inode *dir, struct dentry *dentry,
2350                          const char *symname)
2351 {
2352         int err;
2353         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2354         struct devfs_entry *parent, *de;
2355         struct inode *inode;
2356
2357         /*  First try to get the devfs entry for this directory  */
2358         parent = get_devfs_entry_from_vfs_inode(dir);
2359         if (parent == NULL)
2360                 return -ENOENT;
2361         err = devfs_do_symlink(parent, dentry->d_name.name, symname, &de);
2362         DPRINTK(DEBUG_DISABLED, "(%s): errcode from <devfs_do_symlink>: %d\n",
2363                 dentry->d_name.name, err);
2364         if (err < 0)
2365                 return err;
2366         de->vfs = TRUE;
2367         de->inode.uid = current->euid;
2368         de->inode.gid = current->egid;
2369         de->inode.atime = CURRENT_TIME;
2370         de->inode.mtime = CURRENT_TIME;
2371         de->inode.ctime = CURRENT_TIME;
2372         if ((inode = _devfs_get_vfs_inode(dir->i_sb, de, dentry)) == NULL)
2373                 return -ENOMEM;
2374         DPRINTK(DEBUG_DISABLED, "(%s): new VFS inode(%u): %p  dentry: %p\n",
2375                 dentry->d_name.name, de->inode.ino, inode, dentry);
2376         d_instantiate(dentry, inode);
2377         if (!is_devfsd_or_child(fs_info))
2378                 devfsd_notify_de(de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
2379                                  inode->i_uid, inode->i_gid, fs_info);
2380         return 0;
2381 }                               /*  End Function devfs_symlink  */
2382
2383 static int devfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2384 {
2385         int err;
2386         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2387         struct devfs_entry *parent, *de;
2388         struct inode *inode;
2389
2390         mode = (mode & ~S_IFMT) | S_IFDIR;      /*  VFS doesn't pass S_IFMT part  */
2391         parent = get_devfs_entry_from_vfs_inode(dir);
2392         if (parent == NULL)
2393                 return -ENOENT;
2394         de = _devfs_alloc_entry(dentry->d_name.name, dentry->d_name.len, mode);
2395         if (!de)
2396                 return -ENOMEM;
2397         de->vfs = TRUE;
2398         if ((err = _devfs_append_entry(parent, de, NULL)) != 0)
2399                 return err;
2400         de->inode.uid = current->euid;
2401         de->inode.gid = current->egid;
2402         de->inode.atime = CURRENT_TIME;
2403         de->inode.mtime = CURRENT_TIME;
2404         de->inode.ctime = CURRENT_TIME;
2405         if ((inode = _devfs_get_vfs_inode(dir->i_sb, de, dentry)) == NULL)
2406                 return -ENOMEM;
2407         DPRINTK(DEBUG_DISABLED, "(%s): new VFS inode(%u): %p  dentry: %p\n",
2408                 dentry->d_name.name, de->inode.ino, inode, dentry);
2409         d_instantiate(dentry, inode);
2410         if (!is_devfsd_or_child(fs_info))
2411                 devfsd_notify_de(de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
2412                                  inode->i_uid, inode->i_gid, fs_info);
2413         return 0;
2414 }                               /*  End Function devfs_mkdir  */
2415
2416 static int devfs_rmdir(struct inode *dir, struct dentry *dentry)
2417 {
2418         int err = 0;
2419         struct devfs_entry *de;
2420         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2421         struct inode *inode = dentry->d_inode;
2422
2423         if (dir->i_sb->s_fs_info != inode->i_sb->s_fs_info)
2424                 return -EINVAL;
2425         de = get_devfs_entry_from_vfs_inode(inode);
2426         if (de == NULL)
2427                 return -ENOENT;
2428         if (!S_ISDIR(de->mode))
2429                 return -ENOTDIR;
2430         if (!de->vfs)
2431                 return -EPERM;
2432         /*  First ensure the directory is empty and will stay that way  */
2433         write_lock(&de->u.dir.lock);
2434         if (de->u.dir.first)
2435                 err = -ENOTEMPTY;
2436         else
2437                 de->u.dir.no_more_additions = TRUE;
2438         write_unlock(&de->u.dir.lock);
2439         if (err)
2440                 return err;
2441         /*  Now unhook the directory from its parent  */
2442         write_lock(&de->parent->u.dir.lock);
2443         if (!_devfs_unhook(de))
2444                 err = -ENOENT;
2445         write_unlock(&de->parent->u.dir.lock);
2446         if (err)
2447                 return err;
2448         if (!is_devfsd_or_child(fs_info))
2449                 devfsd_notify_de(de, DEVFSD_NOTIFY_DELETE, inode->i_mode,
2450                                  inode->i_uid, inode->i_gid, fs_info);
2451         free_dentry(de);
2452         devfs_put(de);
2453         return 0;
2454 }                               /*  End Function devfs_rmdir  */
2455
2456 static int devfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2457                        dev_t rdev)
2458 {
2459         int err;
2460         struct fs_info *fs_info = dir->i_sb->s_fs_info;
2461         struct devfs_entry *parent, *de;
2462         struct inode *inode;
2463
2464         DPRINTK(DEBUG_I_MKNOD, "(%s): mode: 0%o  dev: %u:%u\n",
2465                 dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
2466         parent = get_devfs_entry_from_vfs_inode(dir);
2467         if (parent == NULL)
2468                 return -ENOENT;
2469         de = _devfs_alloc_entry(dentry->d_name.name, dentry->d_name.len, mode);
2470         if (!de)
2471                 return -ENOMEM;
2472         de->vfs = TRUE;
2473         if (S_ISCHR(mode) || S_ISBLK(mode))
2474                 de->u.dev = rdev;
2475         if ((err = _devfs_append_entry(parent, de, NULL)) != 0)
2476                 return err;
2477         de->inode.uid = current->euid;
2478         de->inode.gid = current->egid;
2479         de->inode.atime = CURRENT_TIME;
2480         de->inode.mtime = CURRENT_TIME;
2481         de->inode.ctime = CURRENT_TIME;
2482         if ((inode = _devfs_get_vfs_inode(dir->i_sb, de, dentry)) == NULL)
2483                 return -ENOMEM;
2484         DPRINTK(DEBUG_I_MKNOD, ":   new VFS inode(%u): %p  dentry: %p\n",
2485                 de->inode.ino, inode, dentry);
2486         d_instantiate(dentry, inode);
2487         if (!is_devfsd_or_child(fs_info))
2488                 devfsd_notify_de(de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
2489                                  inode->i_uid, inode->i_gid, fs_info);
2490         return 0;
2491 }                               /*  End Function devfs_mknod  */
2492
2493 static int devfs_readlink(struct dentry *dentry, char *buffer, int buflen)
2494 {
2495         int err;
2496         struct devfs_entry *de;
2497
2498         de = get_devfs_entry_from_vfs_inode(dentry->d_inode);
2499         if (!de)
2500                 return -ENODEV;
2501         err = vfs_readlink(dentry, buffer, buflen, de->u.symlink.linkname);
2502         return err;
2503 }                               /*  End Function devfs_readlink  */
2504
2505 static int devfs_follow_link(struct dentry *dentry, struct nameidata *nd)
2506 {
2507         int err;
2508         struct devfs_entry *de;
2509
2510         de = get_devfs_entry_from_vfs_inode(dentry->d_inode);
2511         if (!de)
2512                 return -ENODEV;
2513         err = vfs_follow_link(nd, de->u.symlink.linkname);
2514         return err;
2515 }                               /*  End Function devfs_follow_link  */
2516
2517 static struct inode_operations devfs_iops = {
2518         .setattr = devfs_notify_change,
2519 };
2520
2521 static struct inode_operations devfs_dir_iops = {
2522         .lookup = devfs_lookup,
2523         .unlink = devfs_unlink,
2524         .symlink = devfs_symlink,
2525         .mkdir = devfs_mkdir,
2526         .rmdir = devfs_rmdir,
2527         .mknod = devfs_mknod,
2528         .setattr = devfs_notify_change,
2529 };
2530
2531 static struct inode_operations devfs_symlink_iops = {
2532         .readlink = devfs_readlink,
2533         .follow_link = devfs_follow_link,
2534         .setattr = devfs_notify_change,
2535 };
2536
2537 static int devfs_fill_super(struct super_block *sb, void *data, int silent)
2538 {
2539         struct inode *root_inode = NULL;
2540
2541         if (_devfs_get_root_entry() == NULL)
2542                 goto out_no_root;
2543         atomic_set(&fs_info.devfsd_overrun_count, 0);
2544         init_waitqueue_head(&fs_info.devfsd_wait_queue);
2545         init_waitqueue_head(&fs_info.revalidate_wait_queue);
2546         fs_info.sb = sb;
2547         sb->s_fs_info = &fs_info;
2548         sb->s_blocksize = 1024;
2549         sb->s_blocksize_bits = 10;
2550         sb->s_magic = DEVFS_SUPER_MAGIC;
2551         sb->s_op = &devfs_sops;
2552         if ((root_inode = _devfs_get_vfs_inode(sb, root_entry, NULL)) == NULL)
2553                 goto out_no_root;
2554         sb->s_root = d_alloc_root(root_inode);
2555         if (!sb->s_root)
2556                 goto out_no_root;
2557         DPRINTK(DEBUG_S_READ, "(): made devfs ptr: %p\n", sb->s_fs_info);
2558         return 0;
2559
2560       out_no_root:
2561         PRINTK("(): get root inode failed\n");
2562         if (root_inode)
2563                 iput(root_inode);
2564         return -EINVAL;
2565 }                               /*  End Function devfs_fill_super  */
2566
2567 static struct super_block *devfs_get_sb(struct file_system_type *fs_type,
2568                                         int flags, const char *dev_name,
2569                                         void *data)
2570 {
2571         return get_sb_single(fs_type, flags, data, devfs_fill_super);
2572 }
2573
2574 static struct file_system_type devfs_fs_type = {
2575         .name = DEVFS_NAME,
2576         .get_sb = devfs_get_sb,
2577         .kill_sb = kill_anon_super,
2578 };
2579
2580 /*  File operations for devfsd follow  */
2581
2582 static ssize_t devfsd_read(struct file *file, char __user *buf, size_t len,
2583                            loff_t * ppos)
2584 {
2585         int done = FALSE;
2586         int ival;
2587         loff_t pos, devname_offset, tlen, rpos;
2588         devfs_handle_t de;
2589         struct devfsd_buf_entry *entry;
2590         struct fs_info *fs_info = file->f_dentry->d_inode->i_sb->s_fs_info;
2591         struct devfsd_notify_struct *info = fs_info->devfsd_info;
2592         DECLARE_WAITQUEUE(wait, current);
2593
2594         /*  Can't seek (pread) on this device  */
2595         if (ppos != &file->f_pos)
2596                 return -ESPIPE;
2597         /*  Verify the task has grabbed the queue  */
2598         if (fs_info->devfsd_task != current)
2599                 return -EPERM;
2600         info->major = 0;
2601         info->minor = 0;
2602         /*  Block for a new entry  */
2603         set_current_state(TASK_INTERRUPTIBLE);
2604         add_wait_queue(&fs_info->devfsd_wait_queue, &wait);
2605         while (devfsd_queue_empty(fs_info)) {
2606                 fs_info->devfsd_sleeping = TRUE;
2607                 wake_up(&fs_info->revalidate_wait_queue);
2608                 schedule();
2609                 fs_info->devfsd_sleeping = FALSE;
2610                 if (signal_pending(current)) {
2611                         remove_wait_queue(&fs_info->devfsd_wait_queue, &wait);
2612                         __set_current_state(TASK_RUNNING);
2613                         return -EINTR;
2614                 }
2615                 set_current_state(TASK_INTERRUPTIBLE);
2616         }
2617         remove_wait_queue(&fs_info->devfsd_wait_queue, &wait);
2618         __set_current_state(TASK_RUNNING);
2619         /*  Now play with the data  */
2620         ival = atomic_read(&fs_info->devfsd_overrun_count);
2621         info->overrun_count = ival;
2622         entry = fs_info->devfsd_first_event;
2623         info->type = entry->type;
2624         info->mode = entry->mode;
2625         info->uid = entry->uid;
2626         info->gid = entry->gid;
2627         de = entry->de;
2628         if (S_ISCHR(de->mode) || S_ISBLK(de->mode)) {
2629                 info->major = MAJOR(de->u.dev);
2630                 info->minor = MINOR(de->u.dev);
2631         }
2632         pos = devfs_generate_path(de, info->devname, DEVFS_PATHLEN);
2633         if (pos < 0)
2634                 return pos;
2635         info->namelen = DEVFS_PATHLEN - pos - 1;
2636         if (info->mode == 0)
2637                 info->mode = de->mode;
2638         devname_offset = info->devname - (char *)info;
2639         rpos = *ppos;
2640         if (rpos < devname_offset) {
2641                 /*  Copy parts of the header  */
2642                 tlen = devname_offset - rpos;
2643                 if (tlen > len)
2644                         tlen = len;
2645                 if (copy_to_user(buf, (char *)info + rpos, tlen)) {
2646                         return -EFAULT;
2647                 }
2648                 rpos += tlen;
2649                 buf += tlen;
2650                 len -= tlen;
2651         }
2652         if ((rpos >= devname_offset) && (len > 0)) {
2653                 /*  Copy the name  */
2654                 tlen = info->namelen + 1;
2655                 if (tlen > len)
2656                         tlen = len;
2657                 else
2658                         done = TRUE;
2659                 if (copy_to_user
2660                     (buf, info->devname + pos + rpos - devname_offset, tlen)) {
2661                         return -EFAULT;
2662                 }
2663                 rpos += tlen;
2664         }
2665         tlen = rpos - *ppos;
2666         if (done) {
2667                 devfs_handle_t parent;
2668
2669                 spin_lock(&fs_info->devfsd_buffer_lock);
2670                 fs_info->devfsd_first_event = entry->next;
2671                 if (entry->next == NULL)
2672                         fs_info->devfsd_last_event = NULL;
2673                 spin_unlock(&fs_info->devfsd_buffer_lock);
2674                 for (; de != NULL; de = parent) {
2675                         parent = de->parent;
2676                         devfs_put(de);
2677                 }
2678                 kmem_cache_free(devfsd_buf_cache, entry);
2679                 if (ival > 0)
2680                         atomic_sub(ival, &fs_info->devfsd_overrun_count);
2681                 *ppos = 0;
2682         } else
2683                 *ppos = rpos;
2684         return tlen;
2685 }                               /*  End Function devfsd_read  */
2686
2687 static int devfsd_ioctl(struct inode *inode, struct file *file,
2688                         unsigned int cmd, unsigned long arg)
2689 {
2690         int ival;
2691         struct fs_info *fs_info = inode->i_sb->s_fs_info;
2692
2693         switch (cmd) {
2694         case DEVFSDIOC_GET_PROTO_REV:
2695                 ival = DEVFSD_PROTOCOL_REVISION_KERNEL;
2696                 if (copy_to_user((void __user *)arg, &ival, sizeof ival))
2697                         return -EFAULT;
2698                 break;
2699         case DEVFSDIOC_SET_EVENT_MASK:
2700                 /*  Ensure only one reader has access to the queue. This scheme will
2701                    work even if the global kernel lock were to be removed, because it
2702                    doesn't matter who gets in first, as long as only one gets it  */
2703                 if (fs_info->devfsd_task == NULL) {
2704                         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
2705
2706                         if (!spin_trylock(&lock))
2707                                 return -EBUSY;
2708                         if (fs_info->devfsd_task != NULL) {     /*  We lost the race...  */
2709                                 spin_unlock(&lock);
2710                                 return -EBUSY;
2711                         }
2712                         fs_info->devfsd_task = current;
2713                         spin_unlock(&lock);
2714                         fs_info->devfsd_pgrp =
2715                             (process_group(current) ==
2716                              current->pid) ? process_group(current) : 0;
2717                         fs_info->devfsd_file = file;
2718                         fs_info->devfsd_info =
2719                             kmalloc(sizeof *fs_info->devfsd_info, GFP_KERNEL);
2720                         if (!fs_info->devfsd_info) {
2721                                 devfsd_close(inode, file);
2722                                 return -ENOMEM;
2723                         }
2724                 } else if (fs_info->devfsd_task != current)
2725                         return -EBUSY;
2726                 fs_info->devfsd_event_mask = arg;       /*  Let the masses come forth  */
2727                 break;
2728         case DEVFSDIOC_RELEASE_EVENT_QUEUE:
2729                 if (fs_info->devfsd_file != file)
2730                         return -EPERM;
2731                 return devfsd_close(inode, file);
2732                 /*break; */
2733 #ifdef CONFIG_DEVFS_DEBUG
2734         case DEVFSDIOC_SET_DEBUG_MASK:
2735                 if (copy_from_user(&ival, (void __user *)arg, sizeof ival))
2736                         return -EFAULT;
2737                 devfs_debug = ival;
2738                 break;
2739 #endif
2740         default:
2741                 return -ENOIOCTLCMD;
2742         }
2743         return 0;
2744 }                               /*  End Function devfsd_ioctl  */
2745
2746 static int devfsd_close(struct inode *inode, struct file *file)
2747 {
2748         struct devfsd_buf_entry *entry, *next;
2749         struct fs_info *fs_info = inode->i_sb->s_fs_info;
2750
2751         if (fs_info->devfsd_file != file)
2752                 return 0;
2753         fs_info->devfsd_event_mask = 0;
2754         fs_info->devfsd_file = NULL;
2755         spin_lock(&fs_info->devfsd_buffer_lock);
2756         entry = fs_info->devfsd_first_event;
2757         fs_info->devfsd_first_event = NULL;
2758         fs_info->devfsd_last_event = NULL;
2759         if (fs_info->devfsd_info) {
2760                 kfree(fs_info->devfsd_info);
2761                 fs_info->devfsd_info = NULL;
2762         }
2763         spin_unlock(&fs_info->devfsd_buffer_lock);
2764         fs_info->devfsd_pgrp = 0;
2765         fs_info->devfsd_task = NULL;
2766         wake_up(&fs_info->revalidate_wait_queue);
2767         for (; entry; entry = next) {
2768                 next = entry->next;
2769                 kmem_cache_free(devfsd_buf_cache, entry);
2770         }
2771         return 0;
2772 }                               /*  End Function devfsd_close  */
2773
2774 #ifdef CONFIG_DEVFS_DEBUG
2775 static ssize_t stat_read(struct file *file, char __user *buf, size_t len,
2776                          loff_t * ppos)
2777 {
2778         ssize_t num;
2779         char txt[80];
2780
2781         num = sprintf(txt, "Number of entries: %u  number of bytes: %u\n",
2782                       stat_num_entries, stat_num_bytes) + 1;
2783         /*  Can't seek (pread) on this device  */
2784         if (ppos != &file->f_pos)
2785                 return -ESPIPE;
2786         if (*ppos >= num)
2787                 return 0;
2788         if (*ppos + len > num)
2789                 len = num - *ppos;
2790         if (copy_to_user(buf, txt + *ppos, len))
2791                 return -EFAULT;
2792         *ppos += len;
2793         return len;
2794 }                               /*  End Function stat_read  */
2795 #endif
2796
2797 static int __init init_devfs_fs(void)
2798 {
2799         int err;
2800         int major;
2801         struct devfs_entry *devfsd;
2802 #ifdef CONFIG_DEVFS_DEBUG
2803         struct devfs_entry *stat;
2804 #endif
2805
2806         if (_devfs_get_root_entry() == NULL)
2807                 return -ENOMEM;
2808
2809         printk(KERN_INFO "%s: %s Richard Gooch (rgooch@atnf.csiro.au)\n",
2810                DEVFS_NAME, DEVFS_VERSION);
2811         devfsd_buf_cache = kmem_cache_create("devfsd_event",
2812                                              sizeof(struct devfsd_buf_entry),
2813                                              0, 0, NULL, NULL);
2814         if (!devfsd_buf_cache)
2815                 OOPS("(): unable to allocate event slab\n");
2816 #ifdef CONFIG_DEVFS_DEBUG
2817         devfs_debug = devfs_debug_init;
2818         printk(KERN_INFO "%s: devfs_debug: 0x%0x\n", DEVFS_NAME, devfs_debug);
2819 #endif
2820         printk(KERN_INFO "%s: boot_options: 0x%0x\n", DEVFS_NAME, boot_options);
2821
2822         /* register special device for devfsd communication */
2823         major = register_chrdev(0, "devfs", &devfs_fops);
2824         if (major < 0)
2825                 return major;
2826
2827         /*  And create the entry for ".devfsd"  */
2828         devfsd = _devfs_alloc_entry(".devfsd", 0, S_IFCHR | S_IRUSR | S_IWUSR);
2829         if (devfsd == NULL)
2830                 return -ENOMEM;
2831         devfsd->u.dev = MKDEV(major, 0);
2832         _devfs_append_entry(root_entry, devfsd, NULL);
2833
2834 #ifdef CONFIG_DEVFS_DEBUG
2835         stat = _devfs_alloc_entry(".stat", 0, S_IFCHR | S_IRUGO);
2836         if (stat == NULL)
2837                 return -ENOMEM;
2838         stat->u.dev = MKDEV(major, 1);
2839         _devfs_append_entry(root_entry, stat, NULL);
2840 #endif
2841
2842         err = register_filesystem(&devfs_fs_type);
2843         return err;
2844 }                               /*  End Function init_devfs_fs  */
2845
2846 void __init mount_devfs_fs(void)
2847 {
2848         int err;
2849
2850         if (!(boot_options & OPTION_MOUNT))
2851                 return;
2852         err = do_mount("none", "/dev", "devfs", 0, NULL);
2853         if (err == 0)
2854                 printk(KERN_INFO "Mounted devfs on /dev\n");
2855         else
2856                 PRINTK("(): unable to mount devfs, err: %d\n", err);
2857 }                               /*  End Function mount_devfs_fs  */
2858
2859 module_init(init_devfs_fs)