File: /usr/src/linux/fs/devfs/base.c

1     /*  devfs (Device FileSystem) driver.
2     
3         Copyright (C) 1998-2001  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     */
549     #include <linux/types.h>
550     #include <linux/errno.h>
551     #include <linux/sched.h>
552     #include <linux/tty.h>
553     #include <linux/timer.h>
554     #include <linux/config.h>
555     #include <linux/kernel.h>
556     #include <linux/wait.h>
557     #include <linux/string.h>
558     #include <linux/slab.h>
559     #include <linux/ioport.h>
560     #include <linux/delay.h>
561     #include <linux/ctype.h>
562     #include <linux/mm.h>
563     #include <linux/module.h>
564     #include <linux/init.h>
565     #include <linux/devfs_fs.h>
566     #include <linux/devfs_fs_kernel.h>
567     #include <linux/smp_lock.h>
568     #include <linux/smp.h>
569     #include <linux/version.h>
570     #include <linux/rwsem.h>
571     
572     #include <asm/uaccess.h>
573     #include <asm/io.h>
574     #include <asm/processor.h>
575     #include <asm/system.h>
576     #include <asm/pgtable.h>
577     #include <asm/bitops.h>
578     #include <asm/atomic.h>
579     
580     #define DEVFS_VERSION            "0.116 (20010919)"
581     
582     #define DEVFS_NAME "devfs"
583     
584     #define FIRST_INODE 1
585     
586     #define STRING_LENGTH 256
587     
588     #ifndef TRUE
589     #  define TRUE 1
590     #  define FALSE 0
591     #endif
592     
593     #define IS_HIDDEN(de) (( ((de)->hide && !is_devfsd_or_child(fs_info)) || !(de)->registered))
594     
595     #define DEBUG_NONE         0x00000
596     #define DEBUG_MODULE_LOAD  0x00001
597     #define DEBUG_REGISTER     0x00002
598     #define DEBUG_UNREGISTER   0x00004
599     #define DEBUG_SET_FLAGS    0x00008
600     #define DEBUG_S_PUT        0x00010
601     #define DEBUG_I_LOOKUP     0x00020
602     #define DEBUG_I_CREATE     0x00040
603     #define DEBUG_I_GET        0x00080
604     #define DEBUG_I_CHANGE     0x00100
605     #define DEBUG_I_UNLINK     0x00200
606     #define DEBUG_I_RLINK      0x00400
607     #define DEBUG_I_FLINK      0x00800
608     #define DEBUG_I_MKNOD      0x01000
609     #define DEBUG_F_READDIR    0x02000
610     #define DEBUG_D_DELETE     0x04000
611     #define DEBUG_D_RELEASE    0x08000
612     #define DEBUG_D_IPUT       0x10000
613     #define DEBUG_ALL          0xfffff
614     #define DEBUG_DISABLED     DEBUG_NONE
615     
616     #define OPTION_NONE             0x00
617     #define OPTION_MOUNT            0x01
618     #define OPTION_ONLY             0x02
619     
620     #define OOPS(format, args...) {printk (format, ## args); \
621                                    printk ("Forcing Oops\n"); \
622                                    BUG();}
623     
624     struct directory_type
625     {
626         struct devfs_entry *first;
627         struct devfs_entry *last;
628         unsigned int num_removable;
629     };
630     
631     struct file_type
632     {
633         unsigned long size;
634     };
635     
636     struct device_type
637     {
638         unsigned short major;
639         unsigned short minor;
640     };
641     
642     struct fcb_type  /*  File, char, block type  */
643     {
644         uid_t default_uid;
645         gid_t default_gid;
646         void *ops;
647         union 
648         {
649     	struct file_type file;
650     	struct device_type device;
651         }
652         u;
653         unsigned char auto_owner:1;
654         unsigned char aopen_notify:1;
655         unsigned char removable:1;  /*  Belongs in device_type, but save space   */
656         unsigned char open:1;       /*  Not entirely correct                     */
657         unsigned char autogen:1;    /*  Belongs in device_type, but save space   */
658     };
659     
660     struct symlink_type
661     {
662         atomic_t refcount;           /*  When this drops to zero, it's unused    */
663         rwlock_t lock;               /*  Lock around the registered flag         */
664         unsigned int length;         /*  Not including the NULL-termimator       */
665         char *linkname;              /*  This is NULL-terminated                 */
666     };
667     
668     struct fifo_type
669     {
670         uid_t uid;
671         gid_t gid;
672     };
673     
674     struct devfs_inode  /*  This structure is for "persistent" inode storage  */
675     {
676         time_t atime;
677         time_t mtime;
678         time_t ctime;
679         unsigned int ino;          /*  Inode number as seen in the VFS  */
680         struct dentry *dentry;
681         umode_t mode;
682         uid_t uid;
683         gid_t gid;
684     };
685     
686     struct devfs_entry
687     {
688         void *info;
689         union 
690         {
691     	struct directory_type dir;
692     	struct fcb_type fcb;
693     	struct symlink_type symlink;
694     	struct fifo_type fifo;
695         }
696         u;
697         struct devfs_entry *prev;    /*  Previous entry in the parent directory  */
698         struct devfs_entry *next;    /*  Next entry in the parent directory      */
699         struct devfs_entry *parent;  /*  The parent directory                    */
700         struct devfs_entry *slave;   /*  Another entry to unregister             */
701         struct devfs_inode inode;
702         umode_t mode;
703         unsigned short namelen;  /*  I think 64k+ filenames are a way off...  */
704         unsigned char registered:1;
705         unsigned char hide:1;
706         unsigned char no_persistence:1;
707         char name[1];            /*  This is just a dummy: the allocated array is
708     				 bigger. This is NULL-terminated  */
709     };
710     
711     /*  The root of the device tree  */
712     static struct devfs_entry *root_entry;
713     
714     struct devfsd_buf_entry
715     {
716         void *data;
717         unsigned int type;
718         umode_t mode;
719         uid_t uid;
720         gid_t gid;
721     };
722     
723     struct fs_info      /*  This structure is for the mounted devfs  */
724     {
725         struct super_block *sb;
726         volatile struct devfsd_buf_entry *devfsd_buffer;
727         spinlock_t devfsd_buffer_lock;
728         volatile unsigned int devfsd_buf_in;
729         volatile unsigned int devfsd_buf_out;
730         volatile int devfsd_sleeping;
731         volatile struct task_struct *devfsd_task;
732         volatile struct file *devfsd_file;
733         struct devfsd_notify_struct *devfsd_info;
734         volatile unsigned long devfsd_event_mask;
735         atomic_t devfsd_overrun_count;
736         wait_queue_head_t devfsd_wait_queue;
737         wait_queue_head_t revalidate_wait_queue;
738     };
739     
740     static struct fs_info fs_info = {devfsd_buffer_lock: SPIN_LOCK_UNLOCKED};
741     static const int devfsd_buf_size = PAGE_SIZE / sizeof(struct devfsd_buf_entry);
742     #ifdef CONFIG_DEVFS_DEBUG
743     static unsigned int devfs_debug_init __initdata = DEBUG_NONE;
744     static unsigned int devfs_debug = DEBUG_NONE;
745     #endif
746     
747     #ifdef CONFIG_DEVFS_MOUNT
748     static unsigned int boot_options = OPTION_MOUNT;
749     #else
750     static unsigned int boot_options = OPTION_NONE;
751     #endif
752     
753     /*  Forward function declarations  */
754     static struct devfs_entry *search_for_entry (struct devfs_entry *dir,
755     					     const char *name,
756     					     unsigned int namelen, int mkdir,
757     					     int mkfile, int *is_new,
758     					     int traverse_symlink);
759     static ssize_t devfsd_read (struct file *file, char *buf, size_t len,
760     			    loff_t *ppos);
761     static int devfsd_ioctl (struct inode *inode, struct file *file,
762     			 unsigned int cmd, unsigned long arg);
763     static int devfsd_close (struct inode *inode, struct file *file);
764     
765     
766     /*  Devfs daemon file operations  */
767     static struct file_operations devfsd_fops =
768     {
769         read:    devfsd_read,
770         ioctl:   devfsd_ioctl,
771         release: devfsd_close,
772     };
773     
774     
775     /*  Support functions follow  */
776     
777     
778     /**
779      *	search_for_entry_in_dir - Search for a devfs entry inside another devfs entry.
780      *	@parent:  The parent devfs entry.
781      *	@name:  The name of the entry.
782      *	@namelen:  The number of characters in @name.
783      *	@traverse_symlink:  If %TRUE then the entry is traversed if it is a symlink.
784      *
785      *  Search for a devfs entry inside another devfs entry and returns a pointer
786      *   to the entry on success, else %NULL.
787      */
788     
789     static struct devfs_entry *search_for_entry_in_dir (struct devfs_entry *parent,
790     						    const char *name,
791     						    unsigned int namelen,
792     						    int traverse_symlink)
793     {
794         struct devfs_entry *curr, *retval;
795     
796         if ( !S_ISDIR (parent->mode) )
797         {
798     	printk ("%s: entry is not a directory\n", DEVFS_NAME);
799     	return NULL;
800         }
801         for (curr = parent->u.dir.first; curr != NULL; curr = curr->next)
802         {
803     	if (curr->namelen != namelen) continue;
804     	if (memcmp (curr->name, name, namelen) == 0) break;
805     	/*  Not found: try the next one  */
806         }
807         if (curr == NULL) return NULL;
808         if (!S_ISLNK (curr->mode) || !traverse_symlink) return curr;
809         /*  Need to follow the link: this is a stack chomper  */
810         read_lock (&curr->u.symlink.lock);
811         if (!curr->registered)
812         {
813     	read_unlock (&curr->u.symlink.lock);
814     	return NULL;
815         }
816         atomic_inc (&curr->u.symlink.refcount);
817         read_unlock (&curr->u.symlink.lock);
818         retval = search_for_entry (parent, curr->u.symlink.linkname,
819     			       curr->u.symlink.length, FALSE, FALSE, NULL,
820     			       TRUE);
821         if ( atomic_dec_and_test (&curr->u.symlink.refcount) )
822     	kfree (curr->u.symlink.linkname);
823         return retval;
824     }   /*  End Function search_for_entry_in_dir  */
825     
826     static struct devfs_entry *create_entry (struct devfs_entry *parent,
827     					 const char *name,unsigned int namelen)
828     {
829         struct devfs_entry *new;
830         static unsigned long inode_counter = FIRST_INODE;
831         static spinlock_t counter_lock = SPIN_LOCK_UNLOCKED;
832     
833         if ( name && (namelen < 1) ) namelen = strlen (name);
834         if ( ( new = kmalloc (sizeof *new + namelen, GFP_KERNEL) ) == NULL )
835     	return NULL;
836         /*  Magic: this will set the ctime to zero, thus subsequent lookups will
837     	trigger the call to <update_devfs_inode_from_entry>  */
838         memset (new, 0, sizeof *new + namelen);
839         spin_lock (&counter_lock);
840         new->inode.ino = inode_counter++;
841         spin_unlock (&counter_lock);
842         new->parent = parent;
843         if (name) memcpy (new->name, name, namelen);
844         new->namelen = namelen;
845         if (parent == NULL) return new;
846         new->prev = parent->u.dir.last;
847         /*  Insert into the parent directory's list of children  */
848         if (parent->u.dir.first == NULL) parent->u.dir.first = new;
849         else parent->u.dir.last->next = new;
850         parent->u.dir.last = new;
851         return new;
852     }   /*  End Function create_entry  */
853     
854     static void update_devfs_inode_from_entry (struct devfs_entry *de)
855     {
856         if (de == NULL) return;
857         if ( S_ISDIR (de->mode) )
858         {
859     	de->inode.mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
860     	de->inode.uid = 0;
861     	de->inode.gid = 0;
862         }
863         else if ( S_ISLNK (de->mode) )
864         {
865     	de->inode.mode = S_IFLNK | S_IRUGO | S_IXUGO;
866     	de->inode.uid = 0;
867     	de->inode.gid = 0;
868         }
869         else if ( S_ISFIFO (de->mode) )
870         {
871     	de->inode.mode = de->mode;
872     	de->inode.uid = de->u.fifo.uid;
873     	de->inode.gid = de->u.fifo.gid;
874         }
875         else
876         {
877     	if (de->u.fcb.auto_owner)
878     	    de->inode.mode = (de->mode & ~S_IALLUGO) | S_IRUGO | S_IWUGO;
879     	else de->inode.mode = de->mode;
880     	de->inode.uid = de->u.fcb.default_uid;
881     	de->inode.gid = de->u.fcb.default_gid;
882         }
883     }   /*  End Function update_devfs_inode_from_entry  */
884     
885     /**
886      *	get_root_entry - Get the root devfs entry.
887      *
888      *	Returns the root devfs entry on success, else %NULL.
889      */
890     
891     static struct devfs_entry *get_root_entry (void)
892     {
893         kdev_t devnum;
894         struct devfs_entry *new;
895     
896         /*  Always ensure the root is created  */
897         if (root_entry != NULL) return root_entry;
898         if ( ( root_entry = create_entry (NULL, NULL, 0) ) == NULL ) return NULL;
899         root_entry->mode = S_IFDIR;
900         /*  Force an inode update, because lookup() is never done for the root  */
901         update_devfs_inode_from_entry (root_entry);
902         root_entry->registered = TRUE;
903         /*  And create the entry for ".devfsd"  */
904         if ( ( new = create_entry (root_entry, ".devfsd", 0) ) == NULL )
905     	return NULL;
906         devnum = devfs_alloc_devnum (DEVFS_SPECIAL_CHR);
907         new->u.fcb.u.device.major = MAJOR (devnum);
908         new->u.fcb.u.device.minor = MINOR (devnum);
909         new->mode = S_IFCHR | S_IRUSR | S_IWUSR;
910         new->u.fcb.default_uid = 0;
911         new->u.fcb.default_gid = 0;
912         new->u.fcb.ops = &devfsd_fops;
913         new->registered = TRUE;
914         return root_entry;
915     }   /*  End Function get_root_entry  */
916     
917     
918     /**
919      *	search_for_entry - Search for an entry in the devfs tree.
920      *	@dir: The parent directory to search from. If this is %NULL the root is used
921      *	@name: The name of the entry.
922      *	@namelen: The number of characters in @name.
923      *	@mkdir: If %TRUE intermediate directories are created as needed.
924      *	@mkfile: If %TRUE the file entry is created if it doesn't exist.
925      *	@is_new: If the returned entry was newly made, %TRUE is written here. If
926      * 		this is %NULL nothing is written here.
927      *	@traverse_symlink: If %TRUE then symbolic links are traversed.
928      *
929      *	If the entry is created, then it will be in the unregistered state.
930      *	Returns a pointer to the entry on success, else %NULL.
931      */
932     
933     static struct devfs_entry *search_for_entry (struct devfs_entry *dir,
934     					     const char *name,
935     					     unsigned int namelen, int mkdir,
936     					     int mkfile, int *is_new,
937     					     int traverse_symlink)
938     {
939         int len;
940         const char *subname, *stop, *ptr;
941         struct devfs_entry *entry;
942     
943         if (is_new) *is_new = FALSE;
944         if (dir == NULL) dir = get_root_entry ();
945         if (dir == NULL) return NULL;
946         /*  Extract one filename component  */
947         subname = name;
948         stop = name + namelen;
949         while (subname < stop)
950         {
951     	/*  Search for a possible '/'  */
952     	for (ptr = subname; (ptr < stop) && (*ptr != '/'); ++ptr);
953     	if (ptr >= stop)
954     	{
955     	    /*  Look for trailing component  */
956     	    len = stop - subname;
957     	    entry = search_for_entry_in_dir (dir, subname, len,
958     					     traverse_symlink);
959     	    if (entry != NULL) return entry;
960     	    if (!mkfile) return NULL;
961     	    entry = create_entry (dir, subname, len);
962     	    if (entry && is_new) *is_new = TRUE;
963     	    return entry;
964     	}
965     	/*  Found '/': search for directory  */
966     	if (strncmp (subname, "../", 3) == 0)
967     	{
968     	    /*  Going up  */
969     	    dir = dir->parent;
970     	    if (dir == NULL) return NULL;  /*  Cannot escape from devfs  */
971     	    subname += 3;
972     	    continue;
973     	}
974     	len = ptr - subname;
975     	entry = search_for_entry_in_dir (dir, subname, len, traverse_symlink);
976     	if (!entry && !mkdir) return NULL;
977     	if (entry == NULL)
978     	{
979     	    /*  Make it  */
980     	    if ( ( entry = create_entry (dir, subname, len) ) == NULL )
981     		return NULL;
982     	    entry->mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
983     	    if (is_new) *is_new = TRUE;
984     	}
985     	if ( !S_ISDIR (entry->mode) )
986     	{
987     	    printk ("%s: existing non-directory entry\n", DEVFS_NAME);
988     	    return NULL;
989     	}
990     	/*  Ensure an unregistered entry is re-registered and visible  */
991     	entry->hide = FALSE;
992     	entry->registered = TRUE;
993     	subname = ptr + 1;
994     	dir = entry;
995         }
996         return NULL;
997     }   /*  End Function search_for_entry  */
998     
999     
1000     /**
1001      *	find_by_dev - Find a devfs entry in a directory.
1002      *	@dir: The directory where to search
1003      *	@major: The major number to search for.
1004      *	@minor: The minor number to search for.
1005      *	@type: The type of special file to search for. This may be either
1006      *		%DEVFS_SPECIAL_CHR or %DEVFS_SPECIAL_BLK.
1007      *
1008      *	Returns the devfs_entry pointer on success, else %NULL.
1009      */
1010     
1011     static struct devfs_entry *find_by_dev (struct devfs_entry *dir,
1012     					unsigned int major, unsigned int minor,
1013     					char type)
1014     {
1015         struct devfs_entry *entry, *de;
1016     
1017         if (dir == NULL) return NULL;
1018         if ( !S_ISDIR (dir->mode) )
1019         {
1020     	printk ("%s: find_by_dev(): not a directory\n", DEVFS_NAME);
1021     	return NULL;
1022         }
1023         /*  First search files in this directory  */
1024         for (entry = dir->u.dir.first; entry != NULL; entry = entry->next)
1025         {
1026     	if ( !S_ISCHR (entry->mode) && !S_ISBLK (entry->mode) ) continue;
1027     	if ( S_ISCHR (entry->mode) && (type != DEVFS_SPECIAL_CHR) ) continue;
1028     	if ( S_ISBLK (entry->mode) && (type != DEVFS_SPECIAL_BLK) ) continue;
1029     	if ( (entry->u.fcb.u.device.major == major) &&
1030     	     (entry->u.fcb.u.device.minor == minor) ) return entry;
1031     	/*  Not found: try the next one  */
1032         }
1033         /*  Now recursively search the subdirectories: this is a stack chomper  */
1034         for (entry = dir->u.dir.first; entry != NULL; entry = entry->next)
1035         {
1036     	if ( !S_ISDIR (entry->mode) ) continue;
1037     	de = find_by_dev (entry, major, minor, type);
1038     	if (de) return de;
1039         }
1040         return NULL;
1041     }   /*  End Function find_by_dev  */
1042     
1043     
1044     /**
1045      *	find_entry - Find a devfs entry.
1046      *	@dir: The handle to the parent devfs directory entry. If this is %NULL the
1047      *		name is relative to the root of the devfs.
1048      *	@name: The name of the entry. This is ignored if @handle is not %NULL.
1049      *	@namelen: The number of characters in @name, not including a %NULL
1050      *		terminator. If this is 0, then @name must be %NULL-terminated and the
1051      *		length is computed internally.
1052      *	@major: The major number. This is used if @handle and @name are %NULL.
1053      *	@minor: The minor number. This is used if @handle and @name are %NULL.
1054      *		NOTE: If @major and @minor are both 0, searching by major and minor
1055      *		numbers is disabled.
1056      *	@type: The type of special file to search for. This may be either
1057      *		%DEVFS_SPECIAL_CHR or %DEVFS_SPECIAL_BLK.
1058      *	@traverse_symlink: If %TRUE then symbolic links are traversed.
1059      *
1060      *	FIXME: What the hell is @handle? - ch
1061      *	Returns the devfs_entry pointer on success, else %NULL.
1062      */
1063     
1064     static struct devfs_entry *find_entry (devfs_handle_t dir,
1065     				       const char *name, unsigned int namelen,
1066     				       unsigned int major, unsigned int minor,
1067     				       char type, int traverse_symlink)
1068     {
1069         struct devfs_entry *entry;
1070     
1071         if (name != NULL)
1072         {
1073     	if (namelen < 1) namelen = strlen (name);
1074     	if (name[0] == '/')
1075     	{
1076     	    /*  Skip leading pathname component  */
1077     	    if (namelen < 2)
1078     	    {
1079     		printk ("%s: find_entry(%s): too short\n", DEVFS_NAME, name);
1080     		return NULL;
1081     	    }
1082     	    for (++name, --namelen; (*name != '/') && (namelen > 0);
1083     		 ++name, --namelen);
1084     	    if (namelen < 2)
1085     	    {
1086     		printk ("%s: find_entry(%s): too short\n", DEVFS_NAME, name);
1087     		return NULL;
1088     	    }
1089     	    ++name;
1090     	    --namelen;
1091     	}
1092     	entry = search_for_entry (dir, name, namelen, FALSE, FALSE, NULL,
1093     				  traverse_symlink);
1094     	if (entry != NULL) return entry;
1095         }
1096         /*  Have to search by major and minor: slow  */
1097         if ( (major == 0) && (minor == 0) ) return NULL;
1098         return find_by_dev (root_entry, major, minor, type);
1099     }   /*  End Function find_entry  */
1100     
1101     static struct devfs_entry *get_devfs_entry_from_vfs_inode (struct inode *inode,
1102     							   int do_check)
1103     {
1104         struct devfs_entry *de;
1105     
1106         if (inode == NULL) return NULL;
1107         de = inode->u.generic_ip;
1108         if (!de) printk (__FUNCTION__ "(): NULL de for inode %ld\n", inode->i_ino);
1109         if (do_check && de && !de->registered) de = NULL;
1110         return de;
1111     }   /*  End Function get_devfs_entry_from_vfs_inode  */
1112     
1113     
1114     /**
1115      *	free_dentries - Free the dentries for a device entry and invalidate inodes.
1116      *	@de: The entry.
1117      */
1118     
1119     static void free_dentries (struct devfs_entry *de)
1120     {
1121         struct dentry *dentry;
1122     
1123         spin_lock (&dcache_lock);
1124         dentry = de->inode.dentry;
1125         if (dentry != NULL)
1126         {
1127     	dget_locked (dentry);
1128     	de->inode.dentry = NULL;
1129     	spin_unlock (&dcache_lock);
1130     	/*  Forcefully remove the inode  */
1131     	if (dentry->d_inode != NULL) dentry->d_inode->i_nlink = 0;
1132     	d_drop (dentry);
1133     	dput (dentry);
1134         }
1135         else spin_unlock (&dcache_lock);
1136     }   /*  End Function free_dentries  */
1137     
1138     
1139     /**
1140      *	is_devfsd_or_child - Test if the current process is devfsd or one of its children.
1141      *	@fs_info: The filesystem information.
1142      *
1143      *	Returns %TRUE if devfsd or child, else %FALSE.
1144      */
1145     
1146     static int is_devfsd_or_child (struct fs_info *fs_info)
1147     {
1148         struct task_struct *p;
1149     
1150         for (p = current; p != &init_task; p = p->p_opptr)
1151         {
1152     	if (p == fs_info->devfsd_task) return (TRUE);
1153         }
1154         return (FALSE);
1155     }   /*  End Function is_devfsd_or_child  */
1156     
1157     
1158     /**
1159      *	devfsd_queue_empty - Test if devfsd has work pending in its event queue.
1160      *	@fs_info: The filesystem information.
1161      *
1162      *	Returns %TRUE if the queue is empty, else %FALSE.
1163      */
1164     
1165     static inline int devfsd_queue_empty (struct fs_info *fs_info)
1166     {
1167         return (fs_info->devfsd_buf_out == fs_info->devfsd_buf_in) ? TRUE : FALSE;
1168     }   /*  End Function devfsd_queue_empty  */
1169     
1170     
1171     /**
1172      *	wait_for_devfsd_finished - Wait for devfsd to finish processing its event queue.
1173      *	@fs_info: The filesystem information.
1174      *
1175      *	Returns %TRUE if no more waiting will be required, else %FALSE.
1176      */
1177     
1178     static int wait_for_devfsd_finished (struct fs_info *fs_info)
1179     {
1180         DECLARE_WAITQUEUE (wait, current);
1181     
1182         if (fs_info->devfsd_task == NULL) return (TRUE);
1183         if (devfsd_queue_empty (fs_info) && fs_info->devfsd_sleeping) return TRUE;
1184         if ( is_devfsd_or_child (fs_info) ) return (FALSE);
1185         add_wait_queue (&fs_info->revalidate_wait_queue, &wait);
1186         current->state = TASK_UNINTERRUPTIBLE;
1187         if (!devfsd_queue_empty (fs_info) || !fs_info->devfsd_sleeping)
1188     	if (fs_info->devfsd_task) schedule ();
1189         remove_wait_queue (&fs_info->revalidate_wait_queue, &wait);
1190         current->state = TASK_RUNNING;
1191         return (TRUE);
1192     }   /*  End Function wait_for_devfsd_finished  */
1193     
1194     
1195     /**
1196      *	devfsd_notify_one - Notify a single devfsd daemon of a change.
1197      *	@data: Data to be passed.
1198      *	@type: The type of change.
1199      *	@mode: The mode of the entry.
1200      *	@uid: The user ID.
1201      *	@gid: The group ID.
1202      *	@fs_info: The filesystem info.
1203      *
1204      *	Returns %TRUE if an event was queued and devfsd woken up, else %FALSE.
1205      */
1206     
1207     static int devfsd_notify_one (void *data, unsigned int type, umode_t mode,
1208     			      uid_t uid, gid_t gid, struct fs_info *fs_info)
1209     {
1210         unsigned int next_pos;
1211         unsigned long flags;
1212         struct devfsd_buf_entry *entry;
1213     
1214         if ( !( fs_info->devfsd_event_mask & (1 << type) ) ) return (FALSE);
1215         next_pos = fs_info->devfsd_buf_in + 1;
1216         if (next_pos >= devfsd_buf_size) next_pos = 0;
1217         if (next_pos == fs_info->devfsd_buf_out)
1218         {
1219     	/*  Running up the arse of the reader: drop it  */
1220     	atomic_inc (&fs_info->devfsd_overrun_count);
1221     	return (FALSE);
1222         }
1223         spin_lock_irqsave (&fs_info->devfsd_buffer_lock, flags);
1224         next_pos = fs_info->devfsd_buf_in + 1;
1225         if (next_pos >= devfsd_buf_size) next_pos = 0;
1226         entry = (struct devfsd_buf_entry *) fs_info->devfsd_buffer +
1227     	fs_info->devfsd_buf_in;
1228         entry->data = data;
1229         entry->type = type;
1230         entry->mode = mode;
1231         entry->uid = uid;
1232         entry->gid = gid;
1233         fs_info->devfsd_buf_in = next_pos;
1234         spin_unlock_irqrestore (&fs_info->devfsd_buffer_lock, flags);
1235         wake_up_interruptible (&fs_info->devfsd_wait_queue);
1236         return (TRUE);
1237     }   /*  End Function devfsd_notify_one  */
1238     
1239     
1240     /**
1241      *	devfsd_notify - Notify all devfsd daemons of a change.
1242      *	@de: The devfs entry that has changed.
1243      *	@type: The type of change event.
1244      *	@wait: If TRUE, the functions waits for all daemons to finish processing
1245      *		the event.
1246      */
1247     
1248     static void devfsd_notify (struct devfs_entry *de, unsigned int type, int wait)
1249     {
1250         if (devfsd_notify_one (de, type, de->mode, current->euid,
1251     			   current->egid, &fs_info) && wait)
1252     	wait_for_devfsd_finished (&fs_info);
1253     }   /*  End Function devfsd_notify  */
1254     
1255     
1256     /**
1257      *	devfs_register - Register a device entry.
1258      *	@dir: The handle to the parent devfs directory entry. If this is %NULL the
1259      *		new name is relative to the root of the devfs.
1260      *	@name: The name of the entry.
1261      *	@flags: A set of bitwise-ORed flags (DEVFS_FL_*).
1262      *	@major: The major number. Not needed for regular files.
1263      *	@minor: The minor number. Not needed for regular files.
1264      *	@mode: The default file mode.
1265      *	@ops: The &file_operations or &block_device_operations structure.
1266      *		This must not be externally deallocated.
1267      *	@info: An arbitrary pointer which will be written to the @private_data
1268      *		field of the &file structure passed to the device driver. You can set
1269      *		this to whatever you like, and change it once the file is opened (the next
1270      *		file opened will not see this change).
1271      *
1272      *	Returns a handle which may later be used in a call to devfs_unregister().
1273      *	On failure %NULL is returned.
1274      */
1275     
1276     devfs_handle_t devfs_register (devfs_handle_t dir, const char *name,
1277     			       unsigned int flags,
1278     			       unsigned int major, unsigned int minor,
1279     			       umode_t mode, void *ops, void *info)
1280     {
1281         char devtype = S_ISCHR (mode) ? DEVFS_SPECIAL_CHR : DEVFS_SPECIAL_BLK;
1282         int is_new;
1283         kdev_t devnum = NODEV;
1284         struct devfs_entry *de;
1285     
1286         if (name == NULL)
1287         {
1288     	printk ("%s: devfs_register(): NULL name pointer\n", DEVFS_NAME);
1289     	return NULL;
1290         }
1291         if (ops == NULL)
1292         {
1293     	if ( S_ISBLK (mode) ) ops = (void *) get_blkfops (major);
1294     	if (ops == NULL)
1295     	{
1296     	    printk ("%s: devfs_register(%s): NULL ops pointer\n",
1297     		    DEVFS_NAME, name);
1298     	    return NULL;
1299     	}
1300     	printk ("%s: devfs_register(%s): NULL ops, got %p from major table\n",
1301     		DEVFS_NAME, name, ops);
1302         }
1303         if ( S_ISDIR (mode) )
1304         {
1305     	printk("%s: devfs_register(%s): creating directories is not allowed\n",
1306     	       DEVFS_NAME, name);
1307     	return NULL;
1308         }
1309         if ( S_ISLNK (mode) )
1310         {
1311     	printk ("%s: devfs_register(%s): creating symlinks is not allowed\n",
1312     		DEVFS_NAME, name);
1313     	return NULL;
1314         }
1315         if ( ( S_ISCHR (mode) || S_ISBLK (mode) ) &&
1316     	 (flags & DEVFS_FL_AUTO_DEVNUM) )
1317         {
1318     	if ( ( devnum = devfs_alloc_devnum (devtype) ) == NODEV )
1319     	{
1320     	    printk ("%s: devfs_register(%s): exhausted %s device numbers\n",
1321     		    DEVFS_NAME, name, S_ISCHR (mode) ? "char" : "block");
1322     	    return NULL;
1323     	}
1324     	major = MAJOR (devnum);
1325     	minor = MINOR (devnum);
1326         }
1327         de = search_for_entry (dir, name, strlen (name), TRUE, TRUE, &is_new,
1328     			   FALSE);
1329         if (de == NULL)
1330         {
1331     	printk ("%s: devfs_register(): could not create entry: \"%s\"\n",
1332     		DEVFS_NAME, name);
1333     	if (devnum != NODEV) devfs_dealloc_devnum (devtype, devnum);
1334     	return NULL;
1335         }
1336     #ifdef CONFIG_DEVFS_DEBUG
1337         if (devfs_debug & DEBUG_REGISTER)
1338     	printk ("%s: devfs_register(%s): de: %p %s\n",
1339     		DEVFS_NAME, name, de, is_new ? "new" : "existing");
1340     #endif
1341         if (!is_new)
1342         {
1343     	/*  Existing entry  */
1344     	if ( !S_ISCHR (de->mode) && !S_ISBLK (de->mode) &&
1345     	     !S_ISREG (de->mode) )
1346     	{
1347     	    printk ("%s: devfs_register(): existing non-device/file entry: \"%s\"\n",
1348     		    DEVFS_NAME, name);
1349     	    if (devnum != NODEV) devfs_dealloc_devnum (devtype, devnum);
1350     	    return NULL;
1351     	}
1352     	if (de->registered)
1353     	{
1354     	    printk("%s: devfs_register(): device already registered: \"%s\"\n",
1355     		   DEVFS_NAME, name);
1356     	    if (devnum != NODEV) devfs_dealloc_devnum (devtype, devnum);
1357     	    return NULL;
1358     	}
1359         }
1360         de->u.fcb.autogen = FALSE;
1361         if ( S_ISCHR (mode) || S_ISBLK (mode) )
1362         {
1363     	de->u.fcb.u.device.major = major;
1364     	de->u.fcb.u.device.minor = minor;
1365     	de->u.fcb.autogen = (devnum == NODEV) ? FALSE : TRUE;
1366         }
1367         else if ( S_ISREG (mode) ) de->u.fcb.u.file.size = 0;
1368         else
1369         {
1370     	printk ("%s: devfs_register(): illegal mode: %x\n",
1371     		DEVFS_NAME, mode);
1372     	return (NULL);
1373         }
1374         de->info = info;
1375         de->mode = mode;
1376         if (flags & DEVFS_FL_CURRENT_OWNER)
1377         {
1378     	de->u.fcb.default_uid = current->uid;
1379     	de->u.fcb.default_gid = current->gid;
1380         }
1381         else
1382         {
1383     	de->u.fcb.default_uid = 0;
1384     	de->u.fcb.default_gid = 0;
1385         }
1386         de->u.fcb.ops = ops;
1387         de->u.fcb.auto_owner = (flags & DEVFS_FL_AUTO_OWNER) ? TRUE : FALSE;
1388         de->u.fcb.aopen_notify = (flags & DEVFS_FL_AOPEN_NOTIFY) ? TRUE : FALSE;
1389         if (flags & DEVFS_FL_REMOVABLE)
1390         {
1391     	de->u.fcb.removable = TRUE;
1392     	++de->parent->u.dir.num_removable;
1393         }
1394         de->u.fcb.open = FALSE;
1395         de->hide = (flags & DEVFS_FL_HIDE) ? TRUE : FALSE;
1396         de->no_persistence = (flags & DEVFS_FL_NO_PERSISTENCE) ? TRUE : FALSE;
1397         de->registered = TRUE;
1398         devfsd_notify (de, DEVFSD_NOTIFY_REGISTERED, flags & DEVFS_FL_WAIT);
1399         return de;
1400     }   /*  End Function devfs_register  */
1401     
1402     
1403     /**
1404      *	unregister - Unregister a device entry.
1405      *	@de: The entry to unregister.
1406      */
1407     
1408     static void unregister (struct devfs_entry *de)
1409     {
1410         struct devfs_entry *child;
1411     
1412         if ( (child = de->slave) != NULL )
1413         {
1414     	de->slave = NULL;  /* Unhook first in case slave is parent directory */
1415     	unregister (child);
1416         }
1417         if (de->registered)
1418         {
1419     	devfsd_notify (de, DEVFSD_NOTIFY_UNREGISTERED, 0);
1420     	free_dentries (de);
1421         }
1422         de->info = NULL;
1423         if ( S_ISCHR (de->mode) || S_ISBLK (de->mode) || S_ISREG (de->mode) )
1424         {
1425     	de->registered = FALSE;
1426     	de->u.fcb.ops = NULL;
1427     	if (!S_ISREG (de->mode) && de->u.fcb.autogen)
1428     	{
1429     	    devfs_dealloc_devnum ( S_ISCHR (de->mode) ? DEVFS_SPECIAL_CHR :
1430     				   DEVFS_SPECIAL_BLK,
1431     				   MKDEV (de->u.fcb.u.device.major,
1432     					  de->u.fcb.u.device.minor) );
1433     	}
1434     	de->u.fcb.autogen = FALSE;
1435     	return;
1436         }
1437         if (S_ISLNK (de->mode) && de->registered)
1438         {
1439     	write_lock (&de->u.symlink.lock);
1440     	de->registered = FALSE;
1441     	write_unlock (&de->u.symlink.lock);
1442     	if ( atomic_dec_and_test (&de->u.symlink.refcount) )
1443     	    kfree (de->u.symlink.linkname);
1444     	return;
1445         }
1446         if ( S_ISFIFO (de->mode) )
1447         {
1448     	de->registered = FALSE;
1449     	return;
1450         }
1451         if (!de->registered) return;
1452         if ( !S_ISDIR (de->mode) )
1453         {
1454     	printk ("%s: unregister(): unsupported type\n", DEVFS_NAME);
1455     	return;
1456         }
1457         de->registered = FALSE;
1458         /*  Now recursively search the subdirectories: this is a stack chomper  */
1459         for (child = de->u.dir.first; child != NULL; child = child->next)
1460         {
1461     #ifdef CONFIG_DEVFS_DEBUG
1462     	if (devfs_debug & DEBUG_UNREGISTER)
1463     	    printk ("%s: unregister(): child->name: \"%s\" child: %p\n",
1464     		    DEVFS_NAME, child->name, child);
1465     #endif
1466     	unregister (child);
1467         }
1468     }   /*  End Function unregister  */
1469     
1470     
1471     /**
1472      *	devfs_unregister - Unregister a device entry.
1473      *	@de: A handle previously created by devfs_register() or returned from
1474      *		devfs_find_handle(). If this is %NULL the routine does nothing.
1475      */
1476     
1477     void devfs_unregister (devfs_handle_t de)
1478     {
1479         if (de == NULL) return;
1480     #ifdef CONFIG_DEVFS_DEBUG
1481         if (devfs_debug & DEBUG_UNREGISTER)
1482     	printk ("%s: devfs_unregister(): de->name: \"%s\" de: %p\n",
1483     		DEVFS_NAME, de->name, de);
1484     #endif
1485         unregister (de);
1486     }   /*  End Function devfs_unregister  */
1487     
1488     static int devfs_do_symlink (devfs_handle_t dir, const char *name,
1489     			     unsigned int flags, const char *link,
1490     			     devfs_handle_t *handle, void *info)
1491     {
1492         int is_new;
1493         unsigned int linklength;
1494         char *newlink;
1495         struct devfs_entry *de;
1496     
1497         if (handle != NULL) *handle = NULL;
1498         if (name == NULL)
1499         {
1500     	printk ("%s: devfs_do_symlink(): NULL name pointer\n", DEVFS_NAME);
1501     	return -EINVAL;
1502         }
1503     #ifdef CONFIG_DEVFS_DEBUG
1504         if (devfs_debug & DEBUG_REGISTER)
1505     	printk ("%s: devfs_do_symlink(%s)\n", DEVFS_NAME, name);
1506     #endif
1507         if (link == NULL)
1508         {
1509     	printk ("%s: devfs_do_symlink(): NULL link pointer\n", DEVFS_NAME);
1510     	return -EINVAL;
1511         }
1512         linklength = strlen (link);
1513         if ( ( newlink = kmalloc (linklength + 1, GFP_KERNEL) ) == NULL )
1514     	return -ENOMEM;
1515         memcpy (newlink, link, linklength);
1516         newlink[linklength] = '\0';
1517         if ( ( de = search_for_entry (dir, name, strlen (name), TRUE, TRUE,
1518     				  &is_new, FALSE) ) == NULL )
1519         {
1520     	kfree (newlink);
1521     	return -ENOMEM;
1522         }
1523         if (de->registered)
1524         {
1525     	kfree (newlink);
1526     	printk ("%s: devfs_do_symlink(%s): entry already exists\n",
1527     		DEVFS_NAME, name);
1528     	return -EEXIST;
1529         }
1530         de->mode = S_IFLNK | S_IRUGO | S_IXUGO;
1531         de->info = info;
1532         de->hide = (flags & DEVFS_FL_HIDE) ? TRUE : FALSE;
1533         de->u.symlink.linkname = newlink;
1534         de->u.symlink.length = linklength;
1535         atomic_set (&de->u.symlink.refcount, 1);
1536         rwlock_init (&de->u.symlink.lock);
1537         de->registered = TRUE;
1538         if (handle != NULL) *handle = de;
1539         return 0;
1540     }   /*  End Function devfs_do_symlink  */
1541     
1542     
1543     /**
1544      *	devfs_mk_symlink Create a symbolic link in the devfs namespace.
1545      *	@dir: The handle to the parent devfs directory entry. If this is %NULL the
1546      *		new name is relative to the root of the devfs.
1547      *	@name: The name of the entry.
1548      *	@flags: A set of bitwise-ORed flags (DEVFS_FL_*).
1549      *	@link: The destination name.
1550      *	@handle: The handle to the symlink entry is written here. This may be %NULL.
1551      *	@info: An arbitrary pointer which will be associated with the entry.
1552      *
1553      *	Returns 0 on success, else a negative error code is returned.
1554      */
1555     
1556     int devfs_mk_symlink (devfs_handle_t dir, const char *name, unsigned int flags,
1557     		      const char *link, devfs_handle_t *handle, void *info)
1558     {
1559         int err;
1560         devfs_handle_t de;
1561     
1562         if (handle != NULL) *handle = NULL;
1563         err = devfs_do_symlink (dir, name, flags, link, &de, info);
1564         if (err) return err;
1565         if (handle != NULL) *handle = de;
1566         devfsd_notify (de, DEVFSD_NOTIFY_REGISTERED, flags & DEVFS_FL_WAIT);
1567         return 0;
1568     }   /*  End Function devfs_mk_symlink  */
1569     
1570     
1571     /**
1572      *	devfs_mk_dir - Create a directory in the devfs namespace.
1573      *	@dir: The handle to the parent devfs directory entry. If this is %NULL the
1574      *		new name is relative to the root of the devfs.
1575      *	@name: The name of the entry.
1576      *	@info: An arbitrary pointer which will be associated with the entry.
1577      *
1578      *	Use of this function is optional. The devfs_register() function
1579      *	will automatically create intermediate directories as needed. This function
1580      *	is provided for efficiency reasons, as it provides a handle to a directory.
1581      *	Returns a handle which may later be used in a call to devfs_unregister().
1582      *	On failure %NULL is returned.
1583      */
1584     
1585     devfs_handle_t devfs_mk_dir (devfs_handle_t dir, const char *name, void *info)
1586     {
1587         int is_new;
1588         struct devfs_entry *de;
1589     
1590         if (name == NULL)
1591         {
1592     	printk ("%s: devfs_mk_dir(): NULL name pointer\n", DEVFS_NAME);
1593     	return NULL;
1594         }
1595         de = search_for_entry (dir, name, strlen (name), TRUE, TRUE, &is_new,
1596     			   FALSE);
1597         if (de == NULL)
1598         {
1599     	printk ("%s: devfs_mk_dir(): could not create entry: \"%s\"\n",
1600     		DEVFS_NAME, name);
1601     	return NULL;
1602         }
1603         if (!S_ISDIR (de->mode) && de->registered)
1604         {
1605     	printk ("%s: devfs_mk_dir(): existing non-directory entry: \"%s\"\n",
1606     		DEVFS_NAME, name);
1607     	return NULL;
1608         }
1609     #ifdef CONFIG_DEVFS_DEBUG
1610         if (devfs_debug & DEBUG_REGISTER)
1611     	printk ("%s: devfs_mk_dir(%s): de: %p %s\n",
1612     		DEVFS_NAME, name, de, is_new ? "new" : "existing");
1613     #endif
1614         if (!S_ISDIR (de->mode) && !is_new)
1615         {
1616     	/*  Transmogrifying an old entry  */
1617     	de->u.dir.first = NULL;
1618     	de->u.dir.last = NULL;
1619         }
1620         de->mode = S_IFDIR | S_IRUGO | S_IXUGO;
1621         de->info = info;
1622         if (!de->registered) de->u.dir.num_removable = 0;
1623         de->hide = FALSE;
1624         de->registered = TRUE;
1625         return de;
1626     }   /*  End Function devfs_mk_dir  */
1627     
1628     
1629     /**
1630      *	devfs_find_handle - Find the handle of a devfs entry.
1631      *	@dir: The handle to the parent devfs directory entry. If this is %NULL the
1632      *		name is relative to the root of the devfs.
1633      *	@name: The name of the entry.
1634      *	@major: The major number. This is used if @name is %NULL.
1635      *	@minor: The minor number. This is used if @name is %NULL.
1636      *	@type: The type of special file to search for. This may be either
1637      *		%DEVFS_SPECIAL_CHR or %DEVFS_SPECIAL_BLK.
1638      *	@traverse_symlinks: If %TRUE then symlink entries in the devfs namespace are
1639      *		traversed. Symlinks pointing out of the devfs namespace will cause a
1640      *		failure. Symlink traversal consumes stack space.
1641      *
1642      *	Returns a handle which may later be used in a call to devfs_unregister(),
1643      *	devfs_get_flags(), or devfs_set_flags(). On failure %NULL is returned.
1644      */
1645     
1646     devfs_handle_t devfs_find_handle (devfs_handle_t dir, const char *name,
1647     				  unsigned int major, unsigned int minor,
1648     				  char type, int traverse_symlinks)
1649     {
1650         devfs_handle_t de;
1651     
1652         if ( (name != NULL) && (name[0] == '\0') ) name = NULL;
1653         de = find_entry (dir, name, 0, major, minor, type,
1654     		     traverse_symlinks);
1655         if (de == NULL) return NULL;
1656         if (!de->registered) return NULL;
1657         return de;
1658     }   /*  End Function devfs_find_handle  */
1659     
1660     
1661     /**
1662      *	devfs_get_flags - Get the flags for a devfs entry.
1663      *	@de: The handle to the device entry.
1664      *	@flags: The flags are written here.
1665      *
1666      *	Returns 0 on success, else a negative error code.
1667      */
1668     
1669     int devfs_get_flags (devfs_handle_t de, unsigned int *flags)
1670     {
1671         unsigned int fl = 0;
1672     
1673         if (de == NULL) return -EINVAL;
1674         if (!de->registered) return -ENODEV;
1675         if (de->hide) fl |= DEVFS_FL_HIDE;
1676         if ( S_ISCHR (de->mode) || S_ISBLK (de->mode) || S_ISREG (de->mode) )
1677         {
1678     	if (de->u.fcb.auto_owner) fl |= DEVFS_FL_AUTO_OWNER;
1679     	if (de->u.fcb.aopen_notify) fl |= DEVFS_FL_AOPEN_NOTIFY;
1680     	if (de->u.fcb.removable) fl |= DEVFS_FL_REMOVABLE;
1681         }
1682         *flags = fl;
1683         return 0;
1684     }   /*  End Function devfs_get_flags  */
1685     
1686     
1687     /*
1688      *	devfs_set_flags - Set the flags for a devfs entry.
1689      *	@de: The handle to the device entry.
1690      *	@flags: The flags to set. Unset flags are cleared.
1691      *
1692      *	Returns 0 on success, else a negative error code.
1693      */
1694     
1695     int devfs_set_flags (devfs_handle_t de, unsigned int flags)
1696     {
1697         if (de == NULL) return -EINVAL;
1698         if (!de->registered) return -ENODEV;
1699     #ifdef CONFIG_DEVFS_DEBUG
1700         if (devfs_debug & DEBUG_SET_FLAGS)
1701     	printk ("%s: devfs_set_flags(): de->name: \"%s\"\n",
1702     		DEVFS_NAME, de->name);
1703     #endif
1704         de->hide = (flags & DEVFS_FL_HIDE) ? TRUE : FALSE;
1705         if ( S_ISCHR (de->mode) || S_ISBLK (de->mode) || S_ISREG (de->mode) )
1706         {
1707     	de->u.fcb.auto_owner = (flags & DEVFS_FL_AUTO_OWNER) ? TRUE : FALSE;
1708     	de->u.fcb.aopen_notify = (flags & DEVFS_FL_AOPEN_NOTIFY) ? TRUE:FALSE;
1709     	if ( de->u.fcb.removable && !(flags & DEVFS_FL_REMOVABLE) )
1710     	{
1711     	    de->u.fcb.removable = FALSE;
1712     	    --de->parent->u.dir.num_removable;
1713     	}
1714     	else if ( !de->u.fcb.removable && (flags & DEVFS_FL_REMOVABLE) )
1715     	{
1716     	    de->u.fcb.removable = TRUE;
1717     	    ++de->parent->u.dir.num_removable;
1718     	}
1719         }
1720         return 0;
1721     }   /*  End Function devfs_set_flags  */
1722     
1723     
1724     /**
1725      *	devfs_get_maj_min - Get the major and minor numbers for a devfs entry.
1726      *	@de: The handle to the device entry.
1727      *	@major: The major number is written here. This may be %NULL.
1728      *	@minor: The minor number is written here. This may be %NULL.
1729      *
1730      *	Returns 0 on success, else a negative error code.
1731      */
1732     
1733     int devfs_get_maj_min (devfs_handle_t de, unsigned int *major,
1734     		       unsigned int *minor)
1735     {
1736         if (de == NULL) return -EINVAL;
1737         if (!de->registered) return -ENODEV;
1738         if ( S_ISDIR (de->mode) ) return -EISDIR;
1739         if ( !S_ISCHR (de->mode) && !S_ISBLK (de->mode) ) return -EINVAL;
1740         if (major != NULL) *major = de->u.fcb.u.device.major;
1741         if (minor != NULL) *minor = de->u.fcb.u.device.minor;
1742         return 0;
1743     }   /*  End Function devfs_get_maj_min  */
1744     
1745     
1746     /**
1747      *	devfs_get_handle_from_inode - Get the devfs handle for a VFS inode.
1748      *	@inode: The VFS inode.
1749      *
1750      *	Returns the devfs handle on success, else %NULL.
1751      */
1752     
1753     devfs_handle_t devfs_get_handle_from_inode (struct inode *inode)
1754     {
1755         if (!inode || !inode->i_sb) return NULL;
1756         if (inode->i_sb->s_magic != DEVFS_SUPER_MAGIC) return NULL;
1757         return get_devfs_entry_from_vfs_inode (inode, TRUE);
1758     }   /*  End Function devfs_get_handle_from_inode  */
1759     
1760     
1761     /**
1762      *	devfs_generate_path - Generate a pathname for an entry, relative to the devfs root.
1763      *	@de: The devfs entry.
1764      *	@path: The buffer to write the pathname to. The pathname and '\0'
1765      *		terminator will be written at the end of the buffer.
1766      *	@buflen: The length of the buffer.
1767      *
1768      *	Returns the offset in the buffer where the pathname starts on success,
1769      *	else a negative error code.
1770      */
1771     
1772     int devfs_generate_path (devfs_handle_t de, char *path, int buflen)
1773     {
1774         int pos;
1775     
1776         if (de == NULL) return -EINVAL;
1777         if (de->namelen >= buflen) return -ENAMETOOLONG; /*  Must be first       */
1778         path[buflen - 1] = '\0';
1779         if (de->parent == NULL) return buflen - 1;       /*  Don't prepend root  */
1780         pos = buflen - de->namelen - 1;
1781         memcpy (path + pos, de->name, de->namelen);
1782         for (de = de->parent; de->parent != NULL; de = de->parent)
1783         {
1784     	if (pos - de->namelen - 1 < 0) return -ENAMETOOLONG;
1785     	path[--pos] = '/';
1786     	pos -= de->namelen;
1787     	memcpy (path + pos, de->name, de->namelen);
1788         }
1789         return pos;
1790     }   /*  End Function devfs_generate_path  */
1791     
1792     
1793     /**
1794      *	devfs_get_ops - Get the device operations for a devfs entry.
1795      *	@de: The handle to the device entry.
1796      *
1797      *	Returns a pointer to the device operations on success, else NULL.
1798      */
1799     
1800     void *devfs_get_ops (devfs_handle_t de)
1801     {
1802         if (de == NULL) return NULL;
1803         if (!de->registered) return NULL;
1804         if ( S_ISCHR (de->mode) || S_ISBLK (de->mode) || S_ISREG (de->mode) )
1805     	return de->u.fcb.ops;
1806         return NULL;
1807     }   /*  End Function devfs_get_ops  */
1808     
1809     
1810     /**
1811      *	devfs_set_file_size - Set the file size for a devfs regular file.
1812      *	@de: The handle to the device entry.
1813      *	@size: The new file size.
1814      *
1815      *	Returns 0 on success, else a negative error code.
1816      */
1817     
1818     int devfs_set_file_size (devfs_handle_t de, unsigned long size)
1819     {
1820         if (de == NULL) return -EINVAL;
1821         if (!de->registered) return -EINVAL;
1822         if ( !S_ISREG (de->mode) ) return -EINVAL;
1823         if (de->u.fcb.u.file.size == size) return 0;
1824         de->u.fcb.u.file.size = size;
1825         if (de->inode.dentry == NULL) return 0;
1826         if (de->inode.dentry->d_inode == NULL) return 0;
1827         de->inode.dentry->d_inode->i_size = size;
1828         return 0;
1829     }   /*  End Function devfs_set_file_size  */
1830     
1831     
1832     /**
1833      *	devfs_get_info - Get the info pointer written to private_data of @de upon open.
1834      *	@de: The handle to the device entry.
1835      *
1836      *	Returns the info pointer.
1837      */
1838     void *devfs_get_info (devfs_handle_t de)
1839     {
1840         if (de == NULL) return NULL;
1841         if (!de->registered) return NULL;
1842         return de->info;
1843     }   /*  End Function devfs_get_info  */
1844     
1845     
1846     /**
1847      *	devfs_set_info - Set the info pointer written to private_data upon open.
1848      *	@de: The handle to the device entry.
1849      *	@info: pointer to the data
1850      *
1851      *	Returns 0 on success, else a negative error code.
1852      */
1853     int devfs_set_info (devfs_handle_t de, void *info)
1854     {
1855         if (de == NULL) return -EINVAL;
1856         if (!de->registered) return -EINVAL;
1857         de->info = info;
1858         return 0;
1859     }   /*  End Function devfs_set_info  */
1860     
1861     
1862     /**
1863      *	devfs_get_parent - Get the parent device entry.
1864      *	@de: The handle to the device entry.
1865      *
1866      *	Returns the parent device entry if it exists, else %NULL.
1867      */
1868     devfs_handle_t devfs_get_parent (devfs_handle_t de)
1869     {
1870         if (de == NULL) return NULL;
1871         if (!de->registered) return NULL;
1872         return de->parent;
1873     }   /*  End Function devfs_get_parent  */
1874     
1875     
1876     /**
1877      *	devfs_get_first_child - Get the first leaf node in a directory.
1878      *	@de: The handle to the device entry.
1879      *
1880      *	Returns the leaf node device entry if it exists, else %NULL.
1881      */
1882     
1883     devfs_handle_t devfs_get_first_child (devfs_handle_t de)
1884     {
1885         if (de == NULL) return NULL;
1886         if (!de->registered) return NULL;
1887         if ( !S_ISDIR (de->mode) ) return NULL;
1888         return de->u.dir.first;
1889     }   /*  End Function devfs_get_first_child  */
1890     
1891     
1892     /**
1893      *	devfs_get_next_sibling - Get the next sibling leaf node. for a device entry.
1894      *	@de: The handle to the device entry.
1895      *
1896      *	Returns the leaf node device entry if it exists, else %NULL.
1897      */
1898     
1899     devfs_handle_t devfs_get_next_sibling (devfs_handle_t de)
1900     {
1901         if (de == NULL) return NULL;
1902         if (!de->registered) return NULL;
1903         return de->next;
1904     }   /*  End Function devfs_get_next_sibling  */
1905     
1906     
1907     /**
1908      *	devfs_auto_unregister - Configure a devfs entry to be automatically unregistered.
1909      *	@master: The master devfs entry. Only one slave may be registered.
1910      *	@slave: The devfs entry which will be automatically unregistered when the
1911      *		master entry is unregistered. It is illegal to call devfs_unregister()
1912      *		on this entry.
1913      */
1914     
1915     void devfs_auto_unregister (devfs_handle_t master, devfs_handle_t slave)
1916     {
1917         if (master == NULL) return;
1918         if (master->slave != NULL)
1919         {
1920     	/*  Because of the dumbness of the layers above, ignore duplicates  */
1921     	if (master->slave == slave) return;
1922     	printk ("%s: devfs_auto_unregister(): only one slave allowed\n",
1923     		DEVFS_NAME);
1924     	OOPS ("  master: \"%s\"  old slave: \"%s\"  new slave: \"%s\"\n",
1925     	      master->name, master->slave->name, slave->name);
1926         }
1927         master->slave = slave;
1928     }   /*  End Function devfs_auto_unregister  */
1929     
1930     
1931     /**
1932      *	devfs_get_unregister_slave - Get the slave entry which will be automatically unregistered.
1933      *	@master: The master devfs entry.
1934      *
1935      *	Returns the slave which will be unregistered when @master is unregistered.
1936      */
1937     
1938     devfs_handle_t devfs_get_unregister_slave (devfs_handle_t master)
1939     {
1940         if (master == NULL) return NULL;
1941         return master->slave;
1942     }   /*  End Function devfs_get_unregister_slave  */
1943     
1944     
1945     /**
1946      *	devfs_get_name - Get the name for a device entry in its parent directory.
1947      *	@de: The handle to the device entry.
1948      *	@namelen: The length of the name is written here. This may be %NULL.
1949      *
1950      *	Returns the name on success, else %NULL.
1951      */
1952     
1953     const char *devfs_get_name (devfs_handle_t de, unsigned int *namelen)
1954     {
1955         if (de == NULL) return NULL;
1956         if (!de->registered) return NULL;
1957         if (namelen != NULL) *namelen = de->namelen;
1958         return de->name;
1959     }   /*  End Function devfs_get_name  */
1960     
1961     
1962     /**
1963      *	devfs_register_chrdev - Optionally register a conventional character driver.
1964      *	@major: The major number for the driver.
1965      *	@name: The name of the driver (as seen in /proc/devices).
1966      *	@fops: The &file_operations structure pointer.
1967      *
1968      *	This function will register a character driver provided the "devfs=only"
1969      *	option was not provided at boot time.
1970      *	Returns 0 on success, else a negative error code on failure.
1971      */
1972     
1973     int devfs_register_chrdev (unsigned int major, const char *name,
1974     			   struct file_operations *fops)
1975     {
1976         if (boot_options & OPTION_ONLY) return 0;
1977         return register_chrdev (major, name, fops);
1978     }   /*  End Function devfs_register_chrdev  */
1979     
1980     
1981     /**
1982      *	devfs_register_blkdev - Optionally register a conventional block driver.
1983      *	@major: The major number for the driver.
1984      *	@name: The name of the driver (as seen in /proc/devices).
1985      *	@bdops: The &block_device_operations structure pointer.
1986      *
1987      *	This function will register a block driver provided the "devfs=only"
1988      *	option was not provided at boot time.
1989      *	Returns 0 on success, else a negative error code on failure.
1990      */
1991     
1992     int devfs_register_blkdev (unsigned int major, const char *name,
1993     			   struct block_device_operations *bdops)
1994     {
1995         if (boot_options & OPTION_ONLY) return 0;
1996         return register_blkdev (major, name, bdops);
1997     }   /*  End Function devfs_register_blkdev  */
1998     
1999     
2000     /**
2001      *	devfs_unregister_chrdev - Optionally unregister a conventional character driver.
2002      *	@major: The major number for the driver.
2003      *	@name: The name of the driver (as seen in /proc/devices).
2004      *
2005      *	This function will unregister a character driver provided the "devfs=only"
2006      *	option was not provided at boot time.
2007      *	Returns 0 on success, else a negative error code on failure.
2008      */
2009     
2010     int devfs_unregister_chrdev (unsigned int major, const char *name)
2011     {
2012         if (boot_options & OPTION_ONLY) return 0;
2013         return unregister_chrdev (major, name);
2014     }   /*  End Function devfs_unregister_chrdev  */
2015     
2016     
2017     /**
2018      *	devfs_unregister_blkdev - Optionally unregister a conventional block driver.
2019      *	@major: The major number for the driver.
2020      *	@name: The name of the driver (as seen in /proc/devices).
2021      *
2022      *	This function will unregister a block driver provided the "devfs=only"
2023      *	option was not provided at boot time.
2024      *	Returns 0 on success, else a negative error code on failure.
2025      */
2026     
2027     int devfs_unregister_blkdev (unsigned int major, const char *name)
2028     {
2029         if (boot_options & OPTION_ONLY) return 0;
2030         return unregister_blkdev (major, name);
2031     }   /*  End Function devfs_unregister_blkdev  */
2032     
2033     /**
2034      *	devfs_setup - Process kernel boot options.
2035      *	@str: The boot options after the "devfs=".
2036      */
2037     
2038     static int __init devfs_setup (char *str)
2039     {
2040         static struct
2041         {
2042     	char *name;
2043     	unsigned int mask;
2044     	unsigned int *opt;
2045         } devfs_options_tab[] __initdata =
2046         {
2047     #ifdef CONFIG_DEVFS_DEBUG
2048     	{"dall",      DEBUG_ALL,          &devfs_debug_init},
2049     	{"dmod",      DEBUG_MODULE_LOAD,  &devfs_debug_init},
2050     	{"dreg",      DEBUG_REGISTER,     &devfs_debug_init},
2051     	{"dunreg",    DEBUG_UNREGISTER,   &devfs_debug_init},
2052     	{"diget",     DEBUG_I_GET,        &devfs_debug_init},
2053     	{"dchange",   DEBUG_SET_FLAGS,    &devfs_debug_init},
2054     	{"dichange",  DEBUG_I_CHANGE,     &devfs_debug_init},
2055     	{"dimknod",   DEBUG_I_MKNOD,      &devfs_debug_init},
2056     	{"dilookup",  DEBUG_I_LOOKUP,     &devfs_debug_init},
2057     	{"diunlink",  DEBUG_I_UNLINK,     &devfs_debug_init},
2058     #endif  /*  CONFIG_DEVFS_DEBUG  */
2059     	{"only",      OPTION_ONLY,        &boot_options},
2060     	{"mount",     OPTION_MOUNT,       &boot_options},
2061     	{NULL,        0,                  NULL}
2062         };
2063     
2064         while ( (*str != '\0') && !isspace (*str) )
2065         {
2066     	int i, found = 0, invert = 0;
2067     
2068     	if (strncmp (str, "no", 2) == 0)
2069     	{
2070     	    invert = 1;
2071     	    str += 2;
2072     	}
2073     	for (i = 0; devfs_options_tab[i].name != NULL; i++)
2074     	{
2075     	    int len = strlen (devfs_options_tab[i].name);
2076     
2077     	    if (strncmp (str, devfs_options_tab[i].name, len) == 0)
2078     	    {
2079     		if (invert)
2080     		    *devfs_options_tab[i].opt &= ~devfs_options_tab[i].mask;
2081     		else
2082     		    *devfs_options_tab[i].opt |= devfs_options_tab[i].mask;
2083     		str += len;
2084     		found = 1;
2085     		break;
2086     	    }
2087     	}
2088     	if (!found) return 0;       /*  No match         */
2089     	if (*str != ',') return 0;  /*  No more options  */
2090     	++str;
2091         }
2092         return 1;
2093     }   /*  End Function devfs_setup  */
2094     
2095     __setup("devfs=", devfs_setup);
2096     
2097     EXPORT_SYMBOL(devfs_register);
2098     EXPORT_SYMBOL(devfs_unregister);
2099     EXPORT_SYMBOL(devfs_mk_symlink);
2100     EXPORT_SYMBOL(devfs_mk_dir);
2101     EXPORT_SYMBOL(devfs_find_handle);
2102     EXPORT_SYMBOL(devfs_get_flags);
2103     EXPORT_SYMBOL(devfs_set_flags);
2104     EXPORT_SYMBOL(devfs_get_maj_min);
2105     EXPORT_SYMBOL(devfs_get_handle_from_inode);
2106     EXPORT_SYMBOL(devfs_generate_path);
2107     EXPORT_SYMBOL(devfs_get_ops);
2108     EXPORT_SYMBOL(devfs_set_file_size);
2109     EXPORT_SYMBOL(devfs_get_info);
2110     EXPORT_SYMBOL(devfs_set_info);
2111     EXPORT_SYMBOL(devfs_get_parent);
2112     EXPORT_SYMBOL(devfs_get_first_child);
2113     EXPORT_SYMBOL(devfs_get_next_sibling);
2114     EXPORT_SYMBOL(devfs_auto_unregister);
2115     EXPORT_SYMBOL(devfs_get_unregister_slave);
2116     EXPORT_SYMBOL(devfs_get_name);
2117     EXPORT_SYMBOL(devfs_register_chrdev);
2118     EXPORT_SYMBOL(devfs_register_blkdev);
2119     EXPORT_SYMBOL(devfs_unregister_chrdev);
2120     EXPORT_SYMBOL(devfs_unregister_blkdev);
2121     
2122     
2123     /**
2124      *	try_modload - Notify devfsd of an inode lookup.
2125      *	@parent: The parent devfs entry.
2126      *	@fs_info: The filesystem info.
2127      *	@name: The device name.
2128      *	@namelen: The number of characters in @name.
2129      *	@buf: A working area that will be used. This must not go out of scope until
2130      *		devfsd is idle again.
2131      *
2132      *	Returns 0 on success, else a negative error code.
2133      */
2134     
2135     static int try_modload (struct devfs_entry *parent, struct fs_info *fs_info,
2136     			const char *name, unsigned namelen,
2137     			char buf[STRING_LENGTH])
2138     {
2139         int pos = STRING_LENGTH - namelen - 1;
2140     
2141         if ( !( fs_info->devfsd_event_mask & (1 << DEVFSD_NOTIFY_LOOKUP) ) )
2142     	return -ENOENT;
2143         if ( is_devfsd_or_child (fs_info) ) return -ENOENT;
2144         if (namelen >= STRING_LENGTH) return -ENAMETOOLONG;
2145         memcpy (buf + pos, name, namelen);
2146         buf[STRING_LENGTH - 1] = '\0';
2147         if (parent->parent != NULL) pos = devfs_generate_path (parent, buf, pos);
2148         if (pos < 0) return pos;
2149         buf[STRING_LENGTH - namelen - 2] = '/';
2150         if ( !devfsd_notify_one (buf + pos, DEVFSD_NOTIFY_LOOKUP, 0,
2151     			     current->euid, current->egid, fs_info) )
2152     	return -ENOENT;
2153         /*  Possible success  */
2154         return 0;
2155     }   /*  End Function try_modload  */
2156     
2157     
2158     /**
2159      *	check_disc_changed - Check if a removable disc was changed.
2160      *	@de: The device.
2161      *
2162      *	Returns 1 if the media was changed, else 0.
2163      */
2164     
2165     static int check_disc_changed (struct devfs_entry *de)
2166     {
2167         int tmp;
2168         kdev_t dev = MKDEV (de->u.fcb.u.device.major, de->u.fcb.u.device.minor);
2169         struct block_device_operations *bdops = de->u.fcb.ops;
2170         extern int warn_no_part;
2171     
2172         if ( !S_ISBLK (de->mode) ) return 0;
2173         if (bdops == NULL) return 0;
2174         if (bdops->check_media_change == NULL) return 0;
2175         if ( !bdops->check_media_change (dev) ) return 0;
2176         printk ( KERN_DEBUG "VFS: Disk change detected on device %s\n",
2177     	     kdevname (dev) );
2178         if (invalidate_device(dev, 0))
2179     	printk("VFS: busy inodes on changed media..\n");
2180         /*  Ugly hack to disable messages about unable to read partition table  */
2181         tmp = warn_no_part;
2182         warn_no_part = 0;
2183         if (bdops->revalidate) bdops->revalidate (dev);
2184         warn_no_part = tmp;
2185         return 1;
2186     }   /*  End Function check_disc_changed  */
2187     
2188     
2189     /**
2190      *	scan_dir_for_removable - Scan a directory for removable media devices and check media.
2191      *	@dir: The directory.
2192      */
2193     
2194     static void scan_dir_for_removable (struct devfs_entry *dir)
2195     {
2196         struct devfs_entry *de;
2197     
2198         if (dir->u.dir.num_removable < 1) return;
2199         for (de = dir->u.dir.first; de != NULL; de = de->next)
2200         {
2201     	if (!de->registered) continue;
2202     	if ( !S_ISBLK (de->mode) ) continue;
2203     	if (!de->u.fcb.removable) continue;
2204     	check_disc_changed (de);
2205         }
2206     }   /*  End Function scan_dir_for_removable  */
2207     
2208     /**
2209      *	get_removable_partition - Get removable media partition.
2210      *	@dir: The parent directory.
2211      *	@name: The name of the entry.
2212      *	@namelen: The number of characters in <<name>>.
2213      *
2214      *	Returns 1 if the media was changed, else 0.
2215      */
2216     
2217     static int get_removable_partition (struct devfs_entry *dir, const char *name,
2218     				    unsigned int namelen)
2219     {
2220         struct devfs_entry *de;
2221     
2222         for (de = dir->u.dir.first; de != NULL; de = de->next)
2223         {
2224     	if (!de->registered) continue;
2225     	if ( !S_ISBLK (de->mode) ) continue;
2226     	if (!de->u.fcb.removable) continue;
2227     	if (strcmp (de->name, "disc") == 0) return check_disc_changed (de);
2228     	/*  Support for names where the partition is appended to the disc name
2229     	 */
2230     	if (de->namelen >= namelen) continue;
2231     	if (strncmp (de->name, name, de->namelen) != 0) continue;
2232     	return check_disc_changed (de);
2233         }
2234         return 0;
2235     }   /*  End Function get_removable_partition  */
2236     
2237     
2238     /*  Superblock operations follow  */
2239     
2240     static struct inode_operations devfs_iops;
2241     static struct inode_operations devfs_dir_iops;
2242     static struct file_operations devfs_fops;
2243     static struct file_operations devfs_dir_fops;
2244     static struct inode_operations devfs_symlink_iops;
2245     
2246     static int devfs_notify_change (struct dentry *dentry, struct iattr *iattr)
2247     {
2248         int retval;
2249         struct devfs_entry *de;
2250         struct inode *inode = dentry->d_inode;
2251         struct fs_info *fs_info = inode->i_sb->u.generic_sbp;
2252     
2253         de = get_devfs_entry_from_vfs_inode (inode, TRUE);
2254         if (de == NULL) return -ENODEV;
2255         retval = inode_change_ok (inode, iattr);
2256         if (retval != 0) return retval;
2257         retval = inode_setattr (inode, iattr);
2258         if (retval != 0) return retval;
2259     #ifdef CONFIG_DEVFS_DEBUG
2260         if (devfs_debug & DEBUG_I_CHANGE)
2261         {
2262     	printk ("%s: notify_change(%d): VFS inode: %p  devfs_entry: %p\n",
2263     		DEVFS_NAME, (int) inode->i_ino, inode, de);
2264     	printk ("%s:   mode: 0%o  uid: %d  gid: %d\n",
2265     		DEVFS_NAME, (int) inode->i_mode,
2266     		(int) inode->i_uid, (int) inode->i_gid);
2267         }
2268     #endif
2269         /*  Inode is not on hash chains, thus must save permissions here rather
2270     	than in a write_inode() method  */
2271         de->inode.mode = inode->i_mode;
2272         de->inode.uid = inode->i_uid;
2273         de->inode.gid = inode->i_gid;
2274         de->inode.atime = inode->i_atime;
2275         de->inode.mtime = inode->i_mtime;
2276         de->inode.ctime = inode->i_ctime;
2277         if ( iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID) )
2278     	devfsd_notify_one (de, DEVFSD_NOTIFY_CHANGE, inode->i_mode,
2279     			   inode->i_uid, inode->i_gid, fs_info);
2280         return 0;
2281     }   /*  End Function devfs_notify_change  */
2282     
2283     static int devfs_statfs (struct super_block *sb, struct statfs *buf)
2284     {
2285         buf->f_type = DEVFS_SUPER_MAGIC;
2286         buf->f_bsize = PAGE_SIZE / sizeof (long);
2287         buf->f_bfree = 0;
2288         buf->f_bavail = 0;
2289         buf->f_ffree = 0;
2290         buf->f_namelen = NAME_MAX;
2291         return 0;
2292     }   /*  End Function devfs_statfs  */
2293     
2294     static void devfs_clear_inode(struct inode *inode)
2295     {
2296     	if (S_ISBLK(inode->i_mode))
2297     		bdput(inode->i_bdev);
2298     }
2299     
2300     static struct super_operations devfs_sops =
2301     { 
2302         put_inode:     force_delete,
2303         clear_inode:   devfs_clear_inode,
2304         statfs:        devfs_statfs,
2305     };
2306     
2307     
2308     /**
2309      *	get_vfs_inode - Get a VFS inode.
2310      *	@sb: The super block.
2311      *	@de: The devfs inode.
2312      *	@dentry: The dentry to register with the devfs inode.
2313      *
2314      *	Returns the inode on success, else %NULL.
2315      */
2316     
2317     static struct inode *get_vfs_inode (struct super_block *sb,
2318     				    struct devfs_entry *de,
2319     				    struct dentry *dentry)
2320     {
2321         struct inode *inode;
2322     
2323         if (de->inode.dentry != NULL)
2324         {
2325     	printk ("%s: get_vfs_inode(%u): old de->inode.dentry: %p \"%s\"  new dentry: %p \"%s\"\n",
2326     		DEVFS_NAME, de->inode.ino,
2327     		de->inode.dentry, de->inode.dentry->d_name.name,
2328     		dentry, dentry->d_name.name);
2329     	printk ("  old inode: %p\n", de->inode.dentry->d_inode);
2330     	return NULL;
2331         }
2332         if ( ( inode = new_inode (sb) ) == NULL )
2333         {
2334     	printk ("%s: get_vfs_inode(%s): new_inode() failed, de: %p\n",
2335     		DEVFS_NAME, de->name, de);
2336     	return NULL;
2337         }
2338         de->inode.dentry = dentry;
2339         inode->u.generic_ip = de;
2340         inode->i_ino = de->inode.ino;
2341     #ifdef CONFIG_DEVFS_DEBUG
2342         if (devfs_debug & DEBUG_I_GET)
2343     	printk ("%s: get_vfs_inode(%d): VFS inode: %p  devfs_entry: %p\n",
2344     		DEVFS_NAME, (int) inode->i_ino, inode, de);
2345     #endif
2346         inode->i_blocks = 0;
2347         inode->i_blksize = 1024;
2348         inode->i_op = &devfs_iops;
2349         inode->i_fop = &devfs_fops;
2350         inode->i_rdev = NODEV;
2351         if ( S_ISCHR (de->inode.mode) )
2352         {
2353     	inode->i_rdev = MKDEV (de->u.fcb.u.device.major,
2354     			       de->u.fcb.u.device.minor);
2355     	inode->i_cdev = cdget (kdev_t_to_nr(inode->i_rdev));
2356         }
2357         else if ( S_ISBLK (de->inode.mode) )
2358         {
2359     	inode->i_rdev = MKDEV (de->u.fcb.u.device.major,
2360     			       de->u.fcb.u.device.minor);
2361     	if (bd_acquire(inode) == 0)
2362     	{
2363     	    if (!inode->i_bdev->bd_op && de->u.fcb.ops)
2364     		inode->i_bdev->bd_op = de->u.fcb.ops;
2365     	}
2366     	else printk ("%s: get_vfs_inode(%d): no block device from bdget()\n",
2367     		     DEVFS_NAME, (int) inode->i_ino);
2368         }
2369         else if ( S_ISFIFO (de->inode.mode) ) inode->i_fop = &def_fifo_fops;
2370         else if ( S_ISREG (de->inode.mode) ) inode->i_size = de->u.fcb.u.file.size;
2371         else if ( S_ISDIR (de->inode.mode) )
2372         {
2373     	inode->i_op = &devfs_dir_iops;
2374         	inode->i_fop = &devfs_dir_fops;
2375         }
2376         else if ( S_ISLNK (de->inode.mode) )
2377         {
2378     	inode->i_op = &devfs_symlink_iops;
2379     	inode->i_size = de->u.symlink.length;
2380         }
2381         inode->i_mode = de->inode.mode;
2382         inode->i_uid = de->inode.uid;
2383         inode->i_gid = de->inode.gid;
2384         inode->i_atime = de->inode.atime;
2385         inode->i_mtime = de->inode.mtime;
2386         inode->i_ctime = de->inode.ctime;
2387     #ifdef CONFIG_DEVFS_DEBUG
2388         if (devfs_debug & DEBUG_I_GET)
2389     	printk ("%s:   mode: 0%o  uid: %d  gid: %d\n",
2390     		DEVFS_NAME, (int) inode->i_mode,
2391     		(int) inode->i_uid, (int) inode->i_gid);
2392     #endif
2393         return inode;
2394     }   /*  End Function get_vfs_inode  */
2395     
2396     
2397     /*  File operations for device entries follow  */
2398     
2399     static int devfs_readdir (struct file *file, void *dirent, filldir_t filldir)
2400     {
2401         int err, count;
2402         int stored = 0;
2403         struct fs_info *fs_info;
2404         struct devfs_entry *parent, *de;
2405         struct inode *inode = file->f_dentry->d_inode;
2406     
2407         fs_info = inode->i_sb->u.generic_sbp;
2408         parent = get_devfs_entry_from_vfs_inode (file->f_dentry->d_inode, TRUE);
2409         if ( (long) file->f_pos < 0 ) return -EINVAL;
2410     #ifdef CONFIG_DEVFS_DEBUG
2411         if (devfs_debug & DEBUG_F_READDIR)
2412     	printk ("%s: readdir(): fs_info: %p  pos: %ld\n", DEVFS_NAME,
2413     		fs_info, (long) file->f_pos);
2414     #endif
2415         switch ( (long) file->f_pos )
2416         {
2417           case 0:
2418     	scan_dir_for_removable (parent);
2419     	err = (*filldir) (dirent, "..", 2, file->f_pos,
2420     			  file->f_dentry->d_parent->d_inode->i_ino, DT_DIR);
2421     	if (err == -EINVAL) break;
2422     	if (err < 0) return err;
2423     	file->f_pos++;
2424     	++stored;
2425     	/*  Fall through  */
2426           case 1:
2427     	err = (*filldir) (dirent, ".", 1, file->f_pos, inode->i_ino, DT_DIR);
2428     	if (err == -EINVAL) break;
2429     	if (err < 0) return err;
2430     	file->f_pos++;
2431     	++stored;
2432     	/*  Fall through  */
2433           default:
2434     	/*  Skip entries  */
2435     	count = file->f_pos - 2;
2436     	for (de = parent->u.dir.first; (de != NULL) && (count > 0);
2437     	     de = de->next)
2438     	    if ( !IS_HIDDEN (de) ) --count;
2439     	/*  Now add all remaining entries  */
2440     	for (; de != NULL; de = de->next)
2441     	{
2442     	    if ( IS_HIDDEN (de) ) continue;
2443     	    err = (*filldir) (dirent, de->name, de->namelen,
2444     			      file->f_pos, de->inode.ino, de->mode >> 12);
2445     	    if (err == -EINVAL) break;
2446     	    if (err < 0) return err;
2447     	    file->f_pos++;
2448     	    ++stored;
2449     	}
2450     	break;
2451         }
2452         return stored;
2453     }   /*  End Function devfs_readdir  */
2454     
2455     static int devfs_open (struct inode *inode, struct file *file)
2456     {
2457         int err;
2458         struct fcb_type *df;
2459         struct devfs_entry *de;
2460         struct fs_info *fs_info = inode->i_sb->u.generic_sbp;
2461     
2462         lock_kernel ();
2463         de = get_devfs_entry_from_vfs_inode (inode, TRUE);
2464         err = -ENODEV;
2465         if (de == NULL)
2466     	goto out;
2467         err = 0;
2468         if ( S_ISDIR (de->mode) )
2469     	goto out;
2470         df = &de->u.fcb;
2471         file->private_data = de->info;
2472         if ( S_ISBLK (inode->i_mode) )
2473         {
2474     	file->f_op = &def_blk_fops;
2475     	if (df->ops) inode->i_bdev->bd_op = df->ops;
2476         }
2477         else file->f_op = fops_get ( (struct file_operations*) df->ops );
2478         if (file->f_op)
2479     	err = file->f_op->open ? (*file->f_op->open) (inode, file) : 0;
2480         else
2481         {
2482     	/*  Fallback to legacy scheme  */
2483     	if ( S_ISCHR (inode->i_mode) ) err = chrdev_open (inode, file);
2484     	else err = -ENODEV;
2485         }
2486         if (err < 0) goto out;
2487         /*  Open was successful  */
2488         err = 0;
2489         if (df->open) goto out;
2490         df->open = TRUE;  /*  This is the first open  */
2491         if (df->auto_owner)
2492         {
2493     	/*  Change the ownership/protection  */
2494     	de->inode.mode = (de->inode.mode & ~S_IALLUGO) |(de->mode & S_IRWXUGO);
2495     	de->inode.uid = current->euid;
2496     	de->inode.gid = current->egid;
2497     	inode->i_mode = de->inode.mode;
2498     	inode->i_uid = de->inode.uid;
2499     	inode->i_gid = de->inode.gid;
2500         }
2501         if (df->aopen_notify)
2502     	devfsd_notify_one (de, DEVFSD_NOTIFY_ASYNC_OPEN, inode->i_mode,
2503     			   current->euid, current->egid, fs_info);
2504     out:
2505         unlock_kernel ();
2506         return err;
2507     }   /*  End Function devfs_open  */
2508     
2509     static struct file_operations devfs_fops =
2510     {
2511         open: devfs_open,
2512     };
2513     
2514     static struct file_operations devfs_dir_fops =
2515     {
2516         read: generic_read_dir,
2517         readdir: devfs_readdir,
2518         open: devfs_open,
2519     };
2520     
2521     
2522     /*  Dentry operations for device entries follow  */
2523     
2524     
2525     /**
2526      *	devfs_d_release - Callback for when a dentry is freed.
2527      *	@dentry: The dentry.
2528      */
2529     
2530     static void devfs_d_release (struct dentry *dentry)
2531     {
2532     #ifdef CONFIG_DEVFS_DEBUG
2533         struct inode *inode = dentry->d_inode;
2534     
2535         if (devfs_debug & DEBUG_D_RELEASE)
2536     	printk ("%s: d_release(): dentry: %p inode: %p\n",
2537     		DEVFS_NAME, dentry, inode);
2538     #endif
2539     }   /*  End Function devfs_d_release  */
2540     
2541     /**
2542      *	devfs_d_iput - Callback for when a dentry loses its inode.
2543      *	@dentry: The dentry.
2544      *	@inode:	The inode.
2545      */
2546     
2547     static void devfs_d_iput (struct dentry *dentry, struct inode *inode)
2548     {
2549         struct devfs_entry *de;
2550     
2551         lock_kernel ();
2552         de = get_devfs_entry_from_vfs_inode (inode, FALSE);
2553     #ifdef CONFIG_DEVFS_DEBUG
2554         if (devfs_debug & DEBUG_D_IPUT)
2555     	printk ("%s: d_iput(): dentry: %p inode: %p de: %p  de->dentry: %p\n",
2556     		DEVFS_NAME, dentry, inode, de, de->inode.dentry);
2557     #endif
2558         if (de->inode.dentry == dentry) de->inode.dentry = NULL;
2559         unlock_kernel ();
2560         iput (inode);
2561     }   /*  End Function devfs_d_iput  */
2562     
2563     static int devfs_d_delete (struct dentry *dentry);
2564     
2565     static struct dentry_operations devfs_dops =
2566     {
2567         d_delete:     devfs_d_delete,
2568         d_release:    devfs_d_release,
2569         d_iput:       devfs_d_iput,
2570     };
2571     
2572     static int devfs_d_revalidate_wait (struct dentry *dentry, int flags);
2573     
2574     static struct dentry_operations devfs_wait_dops =
2575     {
2576         d_delete:     devfs_d_delete,
2577         d_release:    devfs_d_release,
2578         d_iput:       devfs_d_iput,
2579         d_revalidate: devfs_d_revalidate_wait,
2580     };
2581     
2582     /**
2583      *	devfs_d_delete - Callback for when all files for a dentry are closed.
2584      *	@dentry: The dentry.
2585      */
2586     
2587     static int devfs_d_delete (struct dentry *dentry)
2588     {
2589         struct inode *inode = dentry->d_inode;
2590         struct devfs_entry *de;
2591         struct fs_info *fs_info;
2592     
2593         if (dentry->d_op == &devfs_wait_dops) dentry->d_op = &devfs_dops;
2594         /*  Unhash dentry if negative (has no inode)  */
2595         if (inode == NULL)
2596         {
2597     #ifdef CONFIG_DEVFS_DEBUG
2598     	if (devfs_debug & DEBUG_D_DELETE)
2599     	    printk ("%s: d_delete(): dropping negative dentry: %p\n",
2600     		    DEVFS_NAME, dentry);
2601     #endif
2602     	return 1;
2603         }
2604         fs_info = inode->i_sb->u.generic_sbp;
2605         de = get_devfs_entry_from_vfs_inode (inode, TRUE);
2606     #ifdef CONFIG_DEVFS_DEBUG
2607         if (devfs_debug & DEBUG_D_DELETE)
2608     	printk ("%s: d_delete(): dentry: %p  inode: %p  devfs_entry: %p\n",
2609     		DEVFS_NAME, dentry, inode, de);
2610     #endif
2611         if (de == NULL) return 0;
2612         if ( !S_ISCHR (de->mode) && !S_ISBLK (de->mode) && !S_ISREG (de->mode) )
2613     	return 0;
2614         if (!de->u.fcb.open) return 0;
2615         de->u.fcb.open = FALSE;
2616         if (de->u.fcb.aopen_notify)
2617     	devfsd_notify_one (de, DEVFSD_NOTIFY_CLOSE, inode->i_mode,
2618     			   current->euid, current->egid, fs_info);
2619         if (!de->u.fcb.auto_owner) return 0;
2620         /*  Change the ownership/protection back  */
2621         de->inode.mode = (de->inode.mode & ~S_IALLUGO) | S_IRUGO | S_IWUGO;
2622         de->inode.uid = de->u.fcb.default_uid;
2623         de->inode.gid = de->u.fcb.default_gid;
2624         inode->i_mode = de->inode.mode;
2625         inode->i_uid = de->inode.uid;
2626         inode->i_gid = de->inode.gid;
2627         return 0;
2628     }   /*  End Function devfs_d_delete  */
2629     
2630     static int devfs_d_revalidate_wait (struct dentry *dentry, int flags)
2631     {
2632         devfs_handle_t de = dentry->d_fsdata;
2633         struct inode *dir;
2634         struct fs_info *fs_info;
2635     
2636         lock_kernel ();
2637         dir = dentry->d_parent->d_inode;
2638         fs_info = dir->i_sb->u.generic_sbp;
2639         if (!de || de->registered)
2640         {
2641     	if ( !dentry->d_inode && is_devfsd_or_child (fs_info) )
2642     	{
2643     	    struct inode *inode;
2644     
2645     #ifdef CONFIG_DEVFS_DEBUG
2646     	    char txt[STRING_LENGTH];
2647     
2648     	    memset (txt, 0, STRING_LENGTH);
2649     	    memcpy (txt, dentry->d_name.name,
2650     		    (dentry->d_name.len >= STRING_LENGTH) ?
2651     		    (STRING_LENGTH - 1) : dentry->d_name.len);
2652     	    if (devfs_debug & DEBUG_I_LOOKUP)
2653     		printk ("%s: d_revalidate(): dentry: %p name: \"%s\" by: \"%s\"\n",
2654     			DEVFS_NAME, dentry, txt, current->comm);
2655     #endif
2656     	    if (de == NULL)
2657     	    {
2658     		devfs_handle_t parent;
2659     
2660     		parent = get_devfs_entry_from_vfs_inode (dir, TRUE);
2661     		de = search_for_entry_in_dir (parent, dentry->d_name.name,
2662     					      dentry->d_name.len, FALSE);
2663     	    }
2664     	    if (de == NULL) goto out;
2665     	    /*  Create an inode, now that the driver information is available
2666     	     */
2667     	    if (de->no_persistence) update_devfs_inode_from_entry (de);
2668     	    else if (de->inode.ctime == 0) update_devfs_inode_from_entry (de);
2669     	    else de->inode.mode =
2670     		     (de->mode & ~S_IALLUGO) | (de->inode.mode & S_IALLUGO);
2671     	    if ( ( inode = get_vfs_inode (dir->i_sb, de, dentry) ) == NULL )
2672     		goto out;
2673     #ifdef CONFIG_DEVFS_DEBUG
2674     	    if (devfs_debug & DEBUG_I_LOOKUP)
2675     		printk ("%s: d_revalidate(): new VFS inode(%u): %p  devfs_entry: %p\n",
2676     			DEVFS_NAME, de->inode.ino, inode, de);
2677     #endif
2678     	    d_instantiate (dentry, inode);
2679     	    goto out;
2680     	}
2681         }
2682         if ( wait_for_devfsd_finished (fs_info) ) dentry->d_op = &devfs_dops;
2683     out:
2684         unlock_kernel ();
2685         return 1;
2686     }   /*  End Function devfs_d_revalidate_wait  */
2687     
2688     
2689     /*  Inode operations for device entries follow  */
2690     
2691     static struct dentry *devfs_lookup (struct inode *dir, struct dentry *dentry)
2692     {
2693         struct fs_info *fs_info;
2694         struct devfs_entry *parent, *de;
2695         struct inode *inode;
2696         char txt[STRING_LENGTH];
2697     
2698         /*  Set up the dentry operations before anything else, to ensure cleaning
2699     	up on any error  */
2700         dentry->d_op = &devfs_dops;
2701         memset (txt, 0, STRING_LENGTH);
2702         memcpy (txt, dentry->d_name.name,
2703     	    (dentry->d_name.len >= STRING_LENGTH) ?
2704     	    (STRING_LENGTH - 1) : dentry->d_name.len);
2705         fs_info = dir->i_sb->u.generic_sbp;
2706         /*  First try to get the devfs entry for this directory  */
2707         parent = get_devfs_entry_from_vfs_inode (dir, TRUE);
2708     #ifdef CONFIG_DEVFS_DEBUG
2709         if (devfs_debug & DEBUG_I_LOOKUP)
2710     	printk ("%s: lookup(%s): dentry: %p parent: %p by: \"%s\"\n",
2711     		DEVFS_NAME, txt, dentry, parent, current->comm);
2712     #endif
2713         if (parent == NULL) return ERR_PTR (-ENOENT);
2714         /*  Try to reclaim an existing devfs entry  */
2715         de = search_for_entry_in_dir (parent,
2716     				  dentry->d_name.name, dentry->d_name.len,
2717     				  FALSE);
2718         if ( ( (de == NULL) || !de->registered ) &&
2719     	 (parent->u.dir.num_removable > 0) &&
2720     	 get_removable_partition (parent, dentry->d_name.name,
2721     				  dentry->d_name.len) )
2722         {
2723     	if (de == NULL)
2724     	    de = search_for_entry_in_dir (parent, dentry->d_name.name,
2725     					  dentry->d_name.len, FALSE);
2726         }
2727         if ( (de == NULL) || !de->registered )
2728         {
2729     	/*  Try with devfsd. For any kind of failure, leave a negative dentry
2730     	    so someone else can deal with it (in the case where the sysadmin
2731     	    does a mknod()). It's important to do this before hashing the
2732     	    dentry, so that the devfsd queue is filled before revalidates
2733     	    can start  */
2734     	if (try_modload (parent, fs_info,
2735     			 dentry->d_name.name, dentry->d_name.len, txt) < 0)
2736     	{
2737     	    d_add (dentry, NULL);
2738     	    return NULL;
2739     	}
2740     	/*  devfsd claimed success  */
2741     	dentry->d_op = &devfs_wait_dops;
2742     	dentry->d_fsdata = de;
2743     	d_add (dentry, NULL);  /*  Open the floodgates  */
2744     	/*  Unlock directory semaphore, which will release any waiters. They
2745     	    will get the hashed dentry, and may be forced to wait for
2746     	    revalidation  */
2747     	up (&dir->i_sem);
2748     	devfs_d_revalidate_wait (dentry, 0);  /*  I might have to wait too  */
2749     	down (&dir->i_sem);      /*  Grab it again because them's the rules  */
2750     	/*  If someone else has been so kind as to make the inode, we go home
2751     	    early  */
2752     	if (dentry->d_inode) return NULL;
2753     	if (de && !de->registered) return NULL;
2754     	if (de == NULL)
2755     	    de = search_for_entry_in_dir (parent, dentry->d_name.name,
2756     					  dentry->d_name.len, FALSE);
2757     	if (de == NULL) return NULL;
2758     	/*  OK, there's an entry now, but no VFS inode yet  */
2759         }
2760         else
2761         {
2762     	dentry->d_op = &devfs_wait_dops;
2763     	d_add (dentry, NULL);  /*  Open the floodgates  */
2764         }
2765         /*  Create an inode, now that the driver information is available  */
2766         if (de->no_persistence) update_devfs_inode_from_entry (de);
2767         else if (de->inode.ctime == 0) update_devfs_inode_from_entry (de);
2768         else de->inode.mode =
2769     	     (de->mode & ~S_IALLUGO) | (de->inode.mode & S_IALLUGO);
2770         if ( ( inode = get_vfs_inode (dir->i_sb, de, dentry) ) == NULL )
2771     	return ERR_PTR (-ENOMEM);
2772     #ifdef CONFIG_DEVFS_DEBUG
2773         if (devfs_debug & DEBUG_I_LOOKUP)
2774     	printk ("%s: lookup(): new VFS inode(%u): %p  devfs_entry: %p\n",
2775     		DEVFS_NAME, de->inode.ino, inode, de);
2776     #endif
2777         d_instantiate (dentry, inode);
2778         /*  Unlock directory semaphore, which will release any waiters. They will
2779     	get the hashed dentry, and may be forced to wait for revalidation  */
2780         up (&dir->i_sem);
2781         if (dentry->d_op == &devfs_wait_dops)
2782     	devfs_d_revalidate_wait (dentry, 0);  /*  I might have to wait too  */
2783         down (&dir->i_sem);          /*  Grab it again because them's the rules  */
2784         return NULL;
2785     }   /*  End Function devfs_lookup  */
2786     
2787     static int devfs_link (struct dentry *old_dentry, struct inode *dir,
2788     		       struct dentry *dentry)
2789     {
2790         /*struct inode *inode = old_dentry->d_inode;*/
2791         char txt[STRING_LENGTH];
2792     
2793         memset (txt, 0, STRING_LENGTH);
2794         memcpy (txt, old_dentry->d_name.name, old_dentry->d_name.len);
2795         txt[STRING_LENGTH - 1] = '\0';
2796         printk ("%s: link of \"%s\"\n", DEVFS_NAME, txt);
2797         return -EPERM;
2798     }   /*  End Function devfs_link  */
2799     
2800     static int devfs_unlink (struct inode *dir, struct dentry *dentry)
2801     {
2802         struct devfs_entry *de;
2803         struct inode *inode = dentry->d_inode;
2804     
2805     #ifdef CONFIG_DEVFS_DEBUG
2806         char txt[STRING_LENGTH];
2807     
2808         if (devfs_debug & DEBUG_I_UNLINK)
2809         {
2810     	memset (txt, 0, STRING_LENGTH);
2811     	memcpy (txt, dentry->d_name.name, dentry->d_name.len);
2812     	txt[STRING_LENGTH - 1] = '\0';
2813     	printk ("%s: unlink(%s)\n", DEVFS_NAME, txt);
2814         }
2815     #endif
2816     
2817         de = get_devfs_entry_from_vfs_inode (dentry->d_inode, TRUE);
2818         if (de == NULL) return -ENOENT;
2819         devfsd_notify_one (de, DEVFSD_NOTIFY_DELETE, inode->i_mode,
2820     		       inode->i_uid, inode->i_gid, dir->i_sb->u.generic_sbp);
2821         if ( S_ISLNK (de->mode) )
2822         {
2823     	write_lock (&de->u.symlink.lock);
2824     	de->registered = FALSE;
2825     	write_unlock (&de->u.symlink.lock);
2826     	if ( atomic_dec_and_test (&de->u.symlink.refcount) )
2827     	    kfree (de->u.symlink.linkname);
2828         }
2829         else de->registered = FALSE;
2830         de->hide = TRUE;
2831         free_dentries (de);
2832         return 0;
2833     }   /*  End Function devfs_unlink  */
2834     
2835     static int devfs_symlink (struct inode *dir, struct dentry *dentry,
2836     			  const char *symname)
2837     {
2838         int err;
2839         struct fs_info *fs_info;
2840         struct devfs_entry *parent, *de;
2841         struct inode *inode;
2842     
2843         fs_info = dir->i_sb->u.generic_sbp;
2844         /*  First try to get the devfs entry for this directory  */
2845         parent = get_devfs_entry_from_vfs_inode (dir, TRUE);
2846         if (parent == NULL) return -ENOENT;
2847         err = devfs_do_symlink (parent, dentry->d_name.name, DEVFS_FL_NONE,
2848     			    symname, &de, NULL);
2849     #ifdef CONFIG_DEVFS_DEBUG
2850         if (devfs_debug & DEBUG_DISABLED)
2851     	printk ("%s: symlink(): errcode from <devfs_do_symlink>: %d\n",
2852     		DEVFS_NAME, err);
2853     #endif
2854         if (err < 0) return err;
2855         de->inode.mode = de->mode;
2856         de->inode.atime = CURRENT_TIME;
2857         de->inode.mtime = CURRENT_TIME;
2858         de->inode.ctime = CURRENT_TIME;
2859         if ( ( inode = get_vfs_inode (dir->i_sb, de, dentry) ) == NULL )
2860     	return -ENOMEM;
2861     #ifdef CONFIG_DEVFS_DEBUG
2862         if (devfs_debug & DEBUG_DISABLED)
2863     	printk ("%s: symlink(): new VFS inode(%u): %p  dentry: %p\n",
2864     		DEVFS_NAME, de->inode.ino, inode, dentry);
2865     #endif
2866         de->hide = FALSE;
2867         d_instantiate (dentry, inode);
2868         devfsd_notify_one (de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
2869     		       inode->i_uid, inode->i_gid, fs_info);
2870         return 0;
2871     }   /*  End Function devfs_symlink  */
2872     
2873     static int devfs_mkdir (struct inode *dir, struct dentry *dentry, int mode)
2874     {
2875         int is_new;
2876         struct fs_info *fs_info;
2877         struct devfs_entry *parent, *de;
2878         struct inode *inode;
2879     
2880         mode = (mode & ~S_IFMT) | S_IFDIR;
2881         fs_info = dir->i_sb->u.generic_sbp;
2882         /*  First try to get the devfs entry for this directory  */
2883         parent = get_devfs_entry_from_vfs_inode (dir, TRUE);
2884         if (parent == NULL) return -ENOENT;
2885         /*  Try to reclaim an existing devfs entry, create if there isn't one  */
2886         de = search_for_entry (parent, dentry->d_name.name, dentry->d_name.len,
2887     			   FALSE, TRUE, &is_new, FALSE);
2888         if (de == NULL) return -ENOMEM;
2889         if (de->registered)
2890         {
2891     	printk ("%s: mkdir(): existing entry\n", DEVFS_NAME);
2892     	return -EEXIST;
2893         }
2894         de->hide = FALSE;
2895         if (!S_ISDIR (de->mode) && !is_new)
2896         {
2897     	/*  Transmogrifying an old entry  */
2898     	de->u.dir.first = NULL;
2899     	de->u.dir.last = NULL;
2900         }
2901         de->mode = mode;
2902         de->u.dir.num_removable = 0;
2903         de->inode.mode = mode;
2904         de->inode.uid = current->euid;
2905         de->inode.gid = current->egid;
2906         de->inode.atime = CURRENT_TIME;
2907         de->inode.mtime = CURRENT_TIME;
2908         de->inode.ctime = CURRENT_TIME;
2909         de->registered = TRUE;
2910         if ( ( inode = get_vfs_inode (dir->i_sb, de, dentry) ) == NULL )
2911     	return -ENOMEM;
2912     #ifdef CONFIG_DEVFS_DEBUG
2913         if (devfs_debug & DEBUG_DISABLED)
2914     	printk ("%s: mkdir(): new VFS inode(%u): %p  dentry: %p\n",
2915     		DEVFS_NAME, de->inode.ino, inode, dentry);
2916     #endif
2917         d_instantiate (dentry, inode);
2918         devfsd_notify_one (de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
2919     		       inode->i_uid, inode->i_gid, fs_info);
2920         return 0;
2921     }   /*  End Function devfs_mkdir  */
2922     
2923     static int devfs_rmdir (struct inode *dir, struct dentry *dentry)
2924     {
2925         int has_children = FALSE;
2926         struct fs_info *fs_info;
2927         struct devfs_entry *de, *child;
2928         struct inode *inode = dentry->d_inode;
2929     
2930         if (dir->i_sb->u.generic_sbp != inode->i_sb->u.generic_sbp) return -EINVAL;
2931         fs_info = dir->i_sb->u.generic_sbp;
2932         de = get_devfs_entry_from_vfs_inode (inode, TRUE);
2933         if (de == NULL) return -ENOENT;
2934         if ( !S_ISDIR (de->mode) ) return -ENOTDIR;
2935         for (child = de->u.dir.first; child != NULL; child = child->next)
2936         {
2937     	if (child->registered)
2938     	{
2939     	    has_children = TRUE;
2940     	    break;
2941     	}
2942         }
2943         if (has_children) return -ENOTEMPTY;
2944         devfsd_notify_one (de, DEVFSD_NOTIFY_DELETE, inode->i_mode,
2945     		       inode->i_uid, inode->i_gid, fs_info);
2946         de->hide = TRUE;
2947         de->registered = FALSE;
2948         free_dentries (de);
2949         return 0;
2950     }   /*  End Function devfs_rmdir  */
2951     
2952     static int devfs_mknod (struct inode *dir, struct dentry *dentry, int mode,
2953     			int rdev)
2954     {
2955         int is_new;
2956         struct fs_info *fs_info;
2957         struct devfs_entry *parent, *de;
2958         struct inode *inode;
2959     
2960     #ifdef CONFIG_DEVFS_DEBUG
2961         char txt[STRING_LENGTH];
2962     
2963         if (devfs_debug & DEBUG_I_MKNOD)
2964         {
2965     	memset (txt, 0, STRING_LENGTH);
2966     	memcpy (txt, dentry->d_name.name, dentry->d_name.len);
2967     	txt[STRING_LENGTH - 1] = '\0';
2968     	printk ("%s: mknod(%s): mode: 0%o  dev: %d\n",
2969     		DEVFS_NAME, txt, mode, rdev);
2970         }
2971     #endif
2972     
2973         fs_info = dir->i_sb->u.generic_sbp;
2974         /*  First try to get the devfs entry for this directory  */
2975         parent = get_devfs_entry_from_vfs_inode (dir, TRUE);
2976         if (parent == NULL) return -ENOENT;
2977         /*  Try to reclaim an existing devfs entry, create if there isn't one  */
2978         de = search_for_entry (parent, dentry->d_name.name, dentry->d_name.len,
2979     			   FALSE, TRUE, &is_new, FALSE);
2980         if (de == NULL) return -ENOMEM;
2981         if (de->registered)
2982         {
2983     	printk ("%s: mknod(): existing entry\n", DEVFS_NAME);
2984     	return -EEXIST;
2985         }
2986         de->info = NULL;
2987         de->mode = mode;
2988         if ( S_ISBLK (mode) || S_ISCHR (mode) )
2989         {
2990     	de->u.fcb.u.device.major = MAJOR (rdev);
2991     	de->u.fcb.u.device.minor = MINOR (rdev);
2992     	de->u.fcb.default_uid = current->euid;
2993     	de->u.fcb.default_gid = current->egid;
2994     	de->u.fcb.ops = NULL;
2995     	de->u.fcb.auto_owner = FALSE;
2996     	de->u.fcb.aopen_notify = FALSE;
2997     	de->u.fcb.open = FALSE;
2998         }
2999         else if ( S_ISFIFO (mode) )
3000         {
3001     	de->u.fifo.uid = current->euid;
3002     	de->u.fifo.gid = current->egid;
3003         }
3004         de->hide = FALSE;
3005         de->inode.mode = mode;
3006         de->inode.uid = current->euid;
3007         de->inode.gid = current->egid;
3008         de->inode.atime = CURRENT_TIME;
3009         de->inode.mtime = CURRENT_TIME;
3010         de->inode.ctime = CURRENT_TIME;
3011         de->registered = TRUE;
3012         if ( ( inode = get_vfs_inode (dir->i_sb, de, dentry) ) == NULL )
3013     	return -ENOMEM;
3014     #ifdef CONFIG_DEVFS_DEBUG
3015         if (devfs_debug & DEBUG_I_MKNOD)
3016     	printk ("%s:   new VFS inode(%u): %p  dentry: %p\n",
3017     		DEVFS_NAME, de->inode.ino, inode, dentry);
3018     #endif
3019         d_instantiate (dentry, inode);
3020         devfsd_notify_one (de, DEVFSD_NOTIFY_CREATE, inode->i_mode,
3021     		       inode->i_uid, inode->i_gid, fs_info);
3022         return 0;
3023     }   /*  End Function devfs_mknod  */
3024     
3025     static int devfs_readlink (struct dentry *dentry, char *buffer, int buflen)
3026     {
3027         int err;
3028         struct devfs_entry *de;
3029     
3030         de = get_devfs_entry_from_vfs_inode (dentry->d_inode, TRUE);
3031         if (!de) return -ENODEV;
3032         read_lock (&de->u.symlink.lock);
3033         if (!de->registered)
3034         {
3035     	read_unlock (&de->u.symlink.lock);
3036     	return -ENODEV;
3037         }
3038         atomic_inc (&de->u.symlink.refcount);
3039         read_unlock (&de->u.symlink.lock);
3040         err = vfs_readlink (dentry, buffer, buflen, de->u.symlink.linkname);
3041         if ( atomic_dec_and_test (&de->u.symlink.refcount) )
3042     	kfree (de->u.symlink.linkname);
3043         return err;
3044     }   /*  End Function devfs_readlink  */
3045     
3046     static int devfs_follow_link (struct dentry *dentry, struct nameidata *nd)
3047     {
3048         int err;
3049         struct devfs_entry *de;
3050     
3051         de = get_devfs_entry_from_vfs_inode (dentry->d_inode, TRUE);
3052         if (!de) return -ENODEV;
3053         read_lock (&de->u.symlink.lock);
3054         if (!de->registered)
3055         {
3056     	read_unlock (&de->u.symlink.lock);
3057     	return -ENODEV;
3058         }
3059         atomic_inc (&de->u.symlink.refcount);
3060         read_unlock (&de->u.symlink.lock);
3061         err = vfs_follow_link (nd, de->u.symlink.linkname);
3062         if ( atomic_dec_and_test (&de->u.symlink.refcount) )
3063     	kfree (de->u.symlink.linkname);
3064         return err;
3065     }   /*  End Function devfs_follow_link  */
3066     
3067     static struct inode_operations devfs_iops =
3068     {
3069         setattr:        devfs_notify_change,
3070     };
3071     
3072     static struct inode_operations devfs_dir_iops =
3073     {
3074         lookup:         devfs_lookup,
3075         link:           devfs_link,
3076         unlink:         devfs_unlink,
3077         symlink:        devfs_symlink,
3078         mkdir:          devfs_mkdir,
3079         rmdir:          devfs_rmdir,
3080         mknod:          devfs_mknod,
3081         setattr:        devfs_notify_change,
3082     };
3083     
3084     static struct inode_operations devfs_symlink_iops =
3085     {
3086         readlink:       devfs_readlink,
3087         follow_link:    devfs_follow_link,
3088         setattr:        devfs_notify_change,
3089     };
3090     
3091     static struct super_block *devfs_read_super (struct super_block *sb,
3092     					     void *data, int silent)
3093     {
3094         struct inode *root_inode = NULL;
3095     
3096         if (get_root_entry () == NULL) goto out_no_root;
3097         atomic_set (&fs_info.devfsd_overrun_count, 0);
3098         init_waitqueue_head (&fs_info.devfsd_wait_queue);
3099         init_waitqueue_head (&fs_info.revalidate_wait_queue);
3100         fs_info.sb = sb;
3101         sb->u.generic_sbp = &fs_info;
3102         sb->s_blocksize = 1024;
3103         sb->s_blocksize_bits = 10;
3104         sb->s_magic = DEVFS_SUPER_MAGIC;
3105         sb->s_op = &devfs_sops;
3106         if ( ( root_inode = get_vfs_inode (sb, root_entry, NULL) ) == NULL )
3107     	goto out_no_root;
3108         sb->s_root = d_alloc_root (root_inode);
3109         if (!sb->s_root) goto out_no_root;
3110     #ifdef CONFIG_DEVFS_DEBUG
3111         if (devfs_debug & DEBUG_DISABLED)
3112     	printk ("%s: read super, made devfs ptr: %p\n",
3113     		DEVFS_NAME, sb->u.generic_sbp);
3114     #endif
3115         return sb;
3116     
3117     out_no_root:
3118         printk ("devfs_read_super: get root inode failed\n");
3119         if (root_inode) iput (root_inode);
3120         return NULL;
3121     }   /*  End Function devfs_read_super  */
3122     
3123     
3124     static DECLARE_FSTYPE (devfs_fs_type, DEVFS_NAME, devfs_read_super, FS_SINGLE);
3125     
3126     
3127     /*  File operations for devfsd follow  */
3128     
3129     static ssize_t devfsd_read (struct file *file, char *buf, size_t len,
3130     			    loff_t *ppos)
3131     {
3132         int done = FALSE;
3133         int ival;
3134         loff_t pos, devname_offset, tlen, rpos;
3135         struct devfsd_buf_entry *entry;
3136         struct fs_info *fs_info = file->f_dentry->d_inode->i_sb->u.generic_sbp;
3137         struct devfsd_notify_struct *info = fs_info->devfsd_info;
3138         DECLARE_WAITQUEUE (wait, current);
3139     
3140         /*  Can't seek (pread) on this device  */
3141         if (ppos != &file->f_pos) return -ESPIPE;
3142         /*  Verify the task has grabbed the queue  */
3143         if (fs_info->devfsd_task != current) return -EPERM;
3144         info->major = 0;
3145         info->minor = 0;
3146         /*  Block for a new entry  */
3147         add_wait_queue (&fs_info->devfsd_wait_queue, &wait);
3148         current->state = TASK_INTERRUPTIBLE;
3149         while ( devfsd_queue_empty (fs_info) )
3150         {
3151     	fs_info->devfsd_sleeping = TRUE;
3152     	wake_up (&fs_info->revalidate_wait_queue);
3153     	schedule ();
3154     	fs_info->devfsd_sleeping = FALSE;
3155     	if ( signal_pending (current) )
3156     	{
3157     	    remove_wait_queue (&fs_info->devfsd_wait_queue, &wait);
3158     	    current->state = TASK_RUNNING;
3159     	    return -EINTR;
3160     	}
3161     	set_current_state(TASK_INTERRUPTIBLE);
3162         }
3163         remove_wait_queue (&fs_info->devfsd_wait_queue, &wait);
3164         current->state = TASK_RUNNING;
3165         /*  Now play with the data  */
3166         ival = atomic_read (&fs_info->devfsd_overrun_count);
3167         if (ival > 0) atomic_sub (ival, &fs_info->devfsd_overrun_count);
3168         info->overrun_count = ival;
3169         entry = (struct devfsd_buf_entry *) fs_info->devfsd_buffer +
3170     	fs_info->devfsd_buf_out;
3171         info->type = entry->type;
3172         info->mode = entry->mode;
3173         info->uid = entry->uid;
3174         info->gid = entry->gid;
3175         if (entry->type == DEVFSD_NOTIFY_LOOKUP)
3176         {
3177     	info->namelen = strlen (entry->data);
3178     	pos = 0;
3179     	memcpy (info->devname, entry->data, info->namelen + 1);
3180         }
3181         else
3182         {
3183     	devfs_handle_t de = entry->data;
3184     
3185     	if ( S_ISCHR (de->mode) || S_ISBLK (de->mode) || S_ISREG (de->mode) )
3186     	{
3187     	    info->major = de->u.fcb.u.device.major;
3188     	    info->minor = de->u.fcb.u.device.minor;
3189     	}
3190     	pos = devfs_generate_path (de, info->devname, DEVFS_PATHLEN);
3191     	if (pos < 0) return pos;
3192     	info->namelen = DEVFS_PATHLEN - pos - 1;
3193     	if (info->mode == 0) info->mode = de->mode;
3194         }
3195         devname_offset = info->devname - (char *) info;
3196         rpos = *ppos;
3197         if (rpos < devname_offset)
3198         {
3199     	/*  Copy parts of the header  */
3200     	tlen = devname_offset - rpos;
3201     	if (tlen > len) tlen = len;
3202     	if ( copy_to_user (buf, (char *) info + rpos, tlen) )
3203     	{
3204     	    return -EFAULT;
3205     	}
3206     	rpos += tlen;
3207     	buf += tlen;
3208     	len -= tlen;
3209         }
3210         if ( (rpos >= devname_offset) && (len > 0) )
3211         {
3212     	/*  Copy the name  */
3213     	tlen = info->namelen + 1;
3214     	if (tlen > len) tlen = len;
3215     	else done = TRUE;
3216     	if ( copy_to_user (buf, info->devname + pos + rpos - devname_offset,
3217     			   tlen) )
3218     	{
3219     	    return -EFAULT;
3220     	}
3221     	rpos += tlen;
3222         }
3223         tlen = rpos - *ppos;
3224         if (done)
3225         {
3226     	unsigned int next_pos = fs_info->devfsd_buf_out + 1;
3227     
3228     	if (next_pos >= devfsd_buf_size) next_pos = 0;
3229     	fs_info->devfsd_buf_out = next_pos;
3230     	*ppos = 0;
3231         }
3232         else *ppos = rpos;
3233         return tlen;
3234     }   /*  End Function devfsd_read  */
3235     
3236     static int devfsd_ioctl (struct inode *inode, struct file *file,
3237     			 unsigned int cmd, unsigned long arg)
3238     {
3239         int ival;
3240         struct fs_info *fs_info = inode->i_sb->u.generic_sbp;
3241         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
3242     
3243         switch (cmd)
3244         {
3245           case DEVFSDIOC_GET_PROTO_REV:
3246     	ival = DEVFSD_PROTOCOL_REVISION_KERNEL;
3247     	if ( copy_to_user ( (void *)arg, &ival, sizeof ival ) ) return -EFAULT;
3248     	break;
3249           case DEVFSDIOC_SET_EVENT_MASK:
3250     	/*  Ensure only one reader has access to the queue. This scheme will
3251     	    work even if the global kernel lock were to be removed, because it
3252     	    doesn't matter who gets in first, as long as only one gets it  */
3253     	if (fs_info->devfsd_task == NULL)
3254     	{
3255     	    if ( !spin_trylock (&lock) ) return -EBUSY;
3256     	    fs_info->devfsd_task = current;
3257     	    spin_unlock (&lock);
3258     	    fs_info->devfsd_file = file;
3259     	    fs_info->devfsd_buffer = (void *) __get_free_page (GFP_KERNEL);
3260     	    fs_info->devfsd_info = kmalloc (sizeof *fs_info->devfsd_info,
3261     					    GFP_KERNEL);
3262     	    if (!fs_info->devfsd_buffer || !fs_info->devfsd_info)
3263     	    {
3264     		devfsd_close (inode, file);
3265     		return -ENOMEM;
3266     	    }
3267     	    fs_info->devfsd_buf_out = fs_info->devfsd_buf_in;
3268     	}
3269     	else if (fs_info->devfsd_task != current) return -EBUSY;
3270     	fs_info->devfsd_event_mask = arg;  /*  Let the masses come forth  */
3271     	break;
3272           case DEVFSDIOC_RELEASE_EVENT_QUEUE:
3273     	if (fs_info->devfsd_file != file) return -EPERM;
3274     	return devfsd_close (inode, file);
3275     	/*break;*/
3276     #ifdef CONFIG_DEVFS_DEBUG
3277           case DEVFSDIOC_SET_DEBUG_MASK:
3278     	if ( copy_from_user (&ival, (void *) arg, sizeof ival) )return -EFAULT;
3279     	devfs_debug = ival;
3280     	break;
3281     #endif
3282           default:
3283     	return -ENOIOCTLCMD;
3284         }
3285         return 0;
3286     }   /*  End Function devfsd_ioctl  */
3287     
3288     static int devfsd_close (struct inode *inode, struct file *file)
3289     {
3290         unsigned long flags;
3291         struct fs_info *fs_info = inode->i_sb->u.generic_sbp;
3292     
3293         if (fs_info->devfsd_file != file) return 0;
3294         fs_info->devfsd_event_mask = 0;
3295         fs_info->devfsd_file = NULL;
3296         spin_lock_irqsave (&fs_info->devfsd_buffer_lock, flags);
3297         if (fs_info->devfsd_buffer)
3298         {
3299     	free_page ( (unsigned long) fs_info->devfsd_buffer );
3300     	fs_info->devfsd_buffer = NULL;
3301         }
3302         if (fs_info->devfsd_info)
3303         {
3304     	kfree (fs_info->devfsd_info);
3305     	fs_info->devfsd_info = NULL;
3306         }
3307         spin_unlock_irqrestore (&fs_info->devfsd_buffer_lock, flags);
3308         fs_info->devfsd_task = NULL;
3309         wake_up (&fs_info->revalidate_wait_queue);
3310         return 0;
3311     }   /*  End Function devfsd_close  */
3312     
3313     
3314     static int __init init_devfs_fs (void)
3315     {
3316         int err;
3317     
3318         printk ("%s: v%s Richard Gooch (rgooch@atnf.csiro.au)\n",
3319     	    DEVFS_NAME, DEVFS_VERSION);
3320     #ifdef CONFIG_DEVFS_DEBUG
3321         devfs_debug = devfs_debug_init;
3322         printk ("%s: devfs_debug: 0x%0x\n", DEVFS_NAME, devfs_debug);
3323     #endif
3324         printk ("%s: boot_options: 0x%0x\n", DEVFS_NAME, boot_options);
3325         err = register_filesystem (&devfs_fs_type);
3326         if (!err)
3327         {
3328     	struct vfsmount *devfs_mnt = kern_mount (&devfs_fs_type);
3329     	err = PTR_ERR (devfs_mnt);
3330     	if ( !IS_ERR (devfs_mnt) ) err = 0;
3331         }
3332         return err;
3333     }   /*  End Function init_devfs_fs  */
3334     
3335     void __init mount_devfs_fs (void)
3336     {
3337         int err;
3338     
3339         if ( !(boot_options & OPTION_MOUNT) ) return;
3340         err = do_mount ("none", "/dev", "devfs", 0, "");
3341         if (err == 0) printk ("Mounted devfs on /dev\n");
3342         else printk ("Warning: unable to mount devfs, err: %d\n", err);
3343     }   /*  End Function mount_devfs_fs  */
3344     
3345     module_init(init_devfs_fs)
3346