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