kern_descrip.c revision 65051
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
39 * $FreeBSD: head/sys/kern/kern_descrip.c 65051 2000-08-24 15:36:55Z phk $
40 */
41
42#include "opt_compat.h"
43#include "opt_devfs.h"
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/sysproto.h>
47#include <sys/conf.h>
48#include <sys/filedesc.h>
49#include <sys/kernel.h>
50#include <sys/sysctl.h>
51#include <sys/vnode.h>
52#include <sys/proc.h>
53#include <sys/file.h>
54#include <sys/stat.h>
55#include <sys/filio.h>
56#include <sys/fcntl.h>
57#include <sys/malloc.h>
58#include <sys/unistd.h>
59#include <sys/resourcevar.h>
60#include <sys/event.h>
61
62#include <vm/vm.h>
63#include <vm/vm_extern.h>
64
65#ifdef DEVFS
66#include <sys/ctype.h>
67#include <sys/eventhandler.h>
68#include <fs/devfs/devfs.h>
69#endif
70
71static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
72MALLOC_DEFINE(M_FILE, "file", "Open file structure");
73static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
74
75static	 d_open_t  fdopen;
76#define NUMFDESC 64
77
78#define CDEV_MAJOR 22
79static struct cdevsw fildesc_cdevsw = {
80	/* open */	fdopen,
81	/* close */	noclose,
82	/* read */	noread,
83	/* write */	nowrite,
84	/* ioctl */	noioctl,
85	/* poll */	nopoll,
86	/* mmap */	nommap,
87	/* strategy */	nostrategy,
88	/* name */	"FD",
89	/* maj */	CDEV_MAJOR,
90	/* dump */	nodump,
91	/* psize */	nopsize,
92	/* flags */	0,
93	/* bmaj */	-1
94};
95
96static int finishdup __P((struct filedesc *fdp, int old, int new, register_t *retval));
97static int badfo_readwrite __P((struct file *fp, struct uio *uio,
98    struct ucred *cred, int flags, struct proc *p));
99static int badfo_ioctl __P((struct file *fp, u_long com, caddr_t data,
100    struct proc *p));
101static int badfo_poll __P((struct file *fp, int events,
102    struct ucred *cred, struct proc *p));
103static int badfo_stat __P((struct file *fp, struct stat *sb, struct proc *p));
104static int badfo_close __P((struct file *fp, struct proc *p));
105
106/*
107 * Descriptor management.
108 */
109struct filelist filehead;	/* head of list of open files */
110int nfiles;			/* actual number of open files */
111extern int cmask;
112
113/*
114 * System calls on descriptors.
115 */
116#ifndef _SYS_SYSPROTO_H_
117struct getdtablesize_args {
118	int	dummy;
119};
120#endif
121/* ARGSUSED */
122int
123getdtablesize(p, uap)
124	struct proc *p;
125	struct getdtablesize_args *uap;
126{
127
128	p->p_retval[0] =
129	    min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
130	return (0);
131}
132
133/*
134 * Duplicate a file descriptor to a particular value.
135 */
136#ifndef _SYS_SYSPROTO_H_
137struct dup2_args {
138	u_int	from;
139	u_int	to;
140};
141#endif
142/* ARGSUSED */
143int
144dup2(p, uap)
145	struct proc *p;
146	struct dup2_args *uap;
147{
148	register struct filedesc *fdp = p->p_fd;
149	register u_int old = uap->from, new = uap->to;
150	int i, error;
151
152	if (old >= fdp->fd_nfiles ||
153	    fdp->fd_ofiles[old] == NULL ||
154	    new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
155	    new >= maxfilesperproc)
156		return (EBADF);
157	if (old == new) {
158		p->p_retval[0] = new;
159		return (0);
160	}
161	if (new >= fdp->fd_nfiles) {
162		if ((error = fdalloc(p, new, &i)))
163			return (error);
164		if (new != i)
165			panic("dup2: fdalloc");
166	} else if (fdp->fd_ofiles[new]) {
167		if (fdp->fd_ofileflags[new] & UF_MAPPED)
168			(void) munmapfd(p, new);
169		/*
170		 * dup2() must succeed even if the close has an error.
171		 */
172		(void) closef(fdp->fd_ofiles[new], p);
173	}
174	return (finishdup(fdp, (int)old, (int)new, p->p_retval));
175}
176
177/*
178 * Duplicate a file descriptor.
179 */
180#ifndef _SYS_SYSPROTO_H_
181struct dup_args {
182	u_int	fd;
183};
184#endif
185/* ARGSUSED */
186int
187dup(p, uap)
188	struct proc *p;
189	struct dup_args *uap;
190{
191	register struct filedesc *fdp;
192	u_int old;
193	int new, error;
194
195	old = uap->fd;
196	fdp = p->p_fd;
197	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
198		return (EBADF);
199	if ((error = fdalloc(p, 0, &new)))
200		return (error);
201	return (finishdup(fdp, (int)old, new, p->p_retval));
202}
203
204/*
205 * The file control system call.
206 */
207#ifndef _SYS_SYSPROTO_H_
208struct fcntl_args {
209	int	fd;
210	int	cmd;
211	long	arg;
212};
213#endif
214/* ARGSUSED */
215int
216fcntl(p, uap)
217	struct proc *p;
218	register struct fcntl_args *uap;
219{
220	register struct filedesc *fdp = p->p_fd;
221	register struct file *fp;
222	register char *pop;
223	struct vnode *vp;
224	int i, tmp, error, flg = F_POSIX;
225	struct flock fl;
226	u_int newmin;
227
228	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
229	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
230		return (EBADF);
231	pop = &fdp->fd_ofileflags[uap->fd];
232	switch (uap->cmd) {
233
234	case F_DUPFD:
235		newmin = uap->arg;
236		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
237		    newmin >= maxfilesperproc)
238			return (EINVAL);
239		if ((error = fdalloc(p, newmin, &i)))
240			return (error);
241		return (finishdup(fdp, uap->fd, i, p->p_retval));
242
243	case F_GETFD:
244		p->p_retval[0] = *pop & 1;
245		return (0);
246
247	case F_SETFD:
248		*pop = (*pop &~ 1) | (uap->arg & 1);
249		return (0);
250
251	case F_GETFL:
252		p->p_retval[0] = OFLAGS(fp->f_flag);
253		return (0);
254
255	case F_SETFL:
256		fp->f_flag &= ~FCNTLFLAGS;
257		fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
258		tmp = fp->f_flag & FNONBLOCK;
259		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
260		if (error)
261			return (error);
262		tmp = fp->f_flag & FASYNC;
263		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
264		if (!error)
265			return (0);
266		fp->f_flag &= ~FNONBLOCK;
267		tmp = 0;
268		(void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
269		return (error);
270
271	case F_GETOWN:
272		return (fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, p));
273
274	case F_SETOWN:
275		return (fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, p));
276
277	case F_SETLKW:
278		flg |= F_WAIT;
279		/* Fall into F_SETLK */
280
281	case F_SETLK:
282		if (fp->f_type != DTYPE_VNODE)
283			return (EBADF);
284		vp = (struct vnode *)fp->f_data;
285		/* Copy in the lock structure */
286		error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
287		    sizeof(fl));
288		if (error)
289			return (error);
290		if (fl.l_whence == SEEK_CUR)
291			fl.l_start += fp->f_offset;
292		switch (fl.l_type) {
293
294		case F_RDLCK:
295			if ((fp->f_flag & FREAD) == 0)
296				return (EBADF);
297			p->p_flag |= P_ADVLOCK;
298			return (VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, &fl, flg));
299
300		case F_WRLCK:
301			if ((fp->f_flag & FWRITE) == 0)
302				return (EBADF);
303			p->p_flag |= P_ADVLOCK;
304			return (VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, &fl, flg));
305
306		case F_UNLCK:
307			return (VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, &fl,
308				F_POSIX));
309
310		default:
311			return (EINVAL);
312		}
313
314	case F_GETLK:
315		if (fp->f_type != DTYPE_VNODE)
316			return (EBADF);
317		vp = (struct vnode *)fp->f_data;
318		/* Copy in the lock structure */
319		error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
320		    sizeof(fl));
321		if (error)
322			return (error);
323		if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
324		    fl.l_type != F_UNLCK)
325			return (EINVAL);
326		if (fl.l_whence == SEEK_CUR)
327			fl.l_start += fp->f_offset;
328		if ((error = VOP_ADVLOCK(vp,(caddr_t)p->p_leader,F_GETLK,&fl,F_POSIX)))
329			return (error);
330		return (copyout((caddr_t)&fl, (caddr_t)(intptr_t)uap->arg,
331		    sizeof(fl)));
332
333	default:
334		return (EINVAL);
335	}
336	/* NOTREACHED */
337}
338
339/*
340 * Common code for dup, dup2, and fcntl(F_DUPFD).
341 */
342static int
343finishdup(fdp, old, new, retval)
344	register struct filedesc *fdp;
345	register int old, new;
346	register_t *retval;
347{
348	register struct file *fp;
349
350	fp = fdp->fd_ofiles[old];
351	fdp->fd_ofiles[new] = fp;
352	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
353	fhold(fp);
354	if (new > fdp->fd_lastfile)
355		fdp->fd_lastfile = new;
356	*retval = new;
357	return (0);
358}
359
360/*
361 * If sigio is on the list associated with a process or process group,
362 * disable signalling from the device, remove sigio from the list and
363 * free sigio.
364 */
365void
366funsetown(sigio)
367	struct sigio *sigio;
368{
369	int s;
370
371	if (sigio == NULL)
372		return;
373	s = splhigh();
374	*(sigio->sio_myref) = NULL;
375	splx(s);
376	if (sigio->sio_pgid < 0) {
377		SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
378			     sigio, sio_pgsigio);
379	} else /* if ((*sigiop)->sio_pgid > 0) */ {
380		SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
381			     sigio, sio_pgsigio);
382	}
383	crfree(sigio->sio_ucred);
384	FREE(sigio, M_SIGIO);
385}
386
387/* Free a list of sigio structures. */
388void
389funsetownlst(sigiolst)
390	struct sigiolst *sigiolst;
391{
392	struct sigio *sigio;
393
394	while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
395		funsetown(sigio);
396}
397
398/*
399 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
400 *
401 * After permission checking, add a sigio structure to the sigio list for
402 * the process or process group.
403 */
404int
405fsetown(pgid, sigiop)
406	pid_t pgid;
407	struct sigio **sigiop;
408{
409	struct proc *proc;
410	struct pgrp *pgrp;
411	struct sigio *sigio;
412	int s;
413
414	if (pgid == 0) {
415		funsetown(*sigiop);
416		return (0);
417	}
418	if (pgid > 0) {
419		proc = pfind(pgid);
420		if (proc == NULL)
421			return (ESRCH);
422
423		/*
424		 * Policy - Don't allow a process to FSETOWN a process
425		 * in another session.
426		 *
427		 * Remove this test to allow maximum flexibility or
428		 * restrict FSETOWN to the current process or process
429		 * group for maximum safety.
430		 */
431		if (proc->p_session != curproc->p_session)
432			return (EPERM);
433
434		pgrp = NULL;
435	} else /* if (pgid < 0) */ {
436		pgrp = pgfind(-pgid);
437		if (pgrp == NULL)
438			return (ESRCH);
439
440		/*
441		 * Policy - Don't allow a process to FSETOWN a process
442		 * in another session.
443		 *
444		 * Remove this test to allow maximum flexibility or
445		 * restrict FSETOWN to the current process or process
446		 * group for maximum safety.
447		 */
448		if (pgrp->pg_session != curproc->p_session)
449			return (EPERM);
450
451		proc = NULL;
452	}
453	funsetown(*sigiop);
454	MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
455	if (pgid > 0) {
456		SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
457		sigio->sio_proc = proc;
458	} else {
459		SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
460		sigio->sio_pgrp = pgrp;
461	}
462	sigio->sio_pgid = pgid;
463	crhold(curproc->p_ucred);
464	sigio->sio_ucred = curproc->p_ucred;
465	/* It would be convenient if p_ruid was in ucred. */
466	sigio->sio_ruid = curproc->p_cred->p_ruid;
467	sigio->sio_myref = sigiop;
468	s = splhigh();
469	*sigiop = sigio;
470	splx(s);
471	return (0);
472}
473
474/*
475 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
476 */
477pid_t
478fgetown(sigio)
479	struct sigio *sigio;
480{
481	return (sigio != NULL ? sigio->sio_pgid : 0);
482}
483
484/*
485 * Close a file descriptor.
486 */
487#ifndef _SYS_SYSPROTO_H_
488struct close_args {
489        int     fd;
490};
491#endif
492/* ARGSUSED */
493int
494close(p, uap)
495	struct proc *p;
496	struct close_args *uap;
497{
498	register struct filedesc *fdp = p->p_fd;
499	register struct file *fp;
500	register int fd = uap->fd;
501	register u_char *pf;
502
503	if ((unsigned)fd >= fdp->fd_nfiles ||
504	    (fp = fdp->fd_ofiles[fd]) == NULL)
505		return (EBADF);
506	pf = (u_char *)&fdp->fd_ofileflags[fd];
507	if (*pf & UF_MAPPED)
508		(void) munmapfd(p, fd);
509	fdp->fd_ofiles[fd] = NULL;
510	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
511		fdp->fd_lastfile--;
512	if (fd < fdp->fd_freefile)
513		fdp->fd_freefile = fd;
514	*pf = 0;
515	if (fd < fdp->fd_knlistsize)
516		knote_fdclose(p, fd);
517	return (closef(fp, p));
518}
519
520#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
521/*
522 * Return status information about a file descriptor.
523 */
524#ifndef _SYS_SYSPROTO_H_
525struct ofstat_args {
526	int	fd;
527	struct	ostat *sb;
528};
529#endif
530/* ARGSUSED */
531int
532ofstat(p, uap)
533	struct proc *p;
534	register struct ofstat_args *uap;
535{
536	register struct filedesc *fdp = p->p_fd;
537	register struct file *fp;
538	struct stat ub;
539	struct ostat oub;
540	int error;
541
542	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
543	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
544		return (EBADF);
545	error = fo_stat(fp, &ub, p);
546	if (error == 0) {
547		cvtstat(&ub, &oub);
548		error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
549	}
550	return (error);
551}
552#endif /* COMPAT_43 || COMPAT_SUNOS */
553
554/*
555 * Return status information about a file descriptor.
556 */
557#ifndef _SYS_SYSPROTO_H_
558struct fstat_args {
559	int	fd;
560	struct	stat *sb;
561};
562#endif
563/* ARGSUSED */
564int
565fstat(p, uap)
566	struct proc *p;
567	register struct fstat_args *uap;
568{
569	register struct filedesc *fdp = p->p_fd;
570	register struct file *fp;
571	struct stat ub;
572	int error;
573
574	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
575	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
576		return (EBADF);
577	error = fo_stat(fp, &ub, p);
578	if (error == 0)
579		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
580	return (error);
581}
582
583/*
584 * Return status information about a file descriptor.
585 */
586#ifndef _SYS_SYSPROTO_H_
587struct nfstat_args {
588	int	fd;
589	struct	nstat *sb;
590};
591#endif
592/* ARGSUSED */
593int
594nfstat(p, uap)
595	struct proc *p;
596	register struct nfstat_args *uap;
597{
598	register struct filedesc *fdp = p->p_fd;
599	register struct file *fp;
600	struct stat ub;
601	struct nstat nub;
602	int error;
603
604	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
605	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
606		return (EBADF);
607	error = fo_stat(fp, &ub, p);
608	if (error == 0) {
609		cvtnstat(&ub, &nub);
610		error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
611	}
612	return (error);
613}
614
615/*
616 * Return pathconf information about a file descriptor.
617 */
618#ifndef _SYS_SYSPROTO_H_
619struct fpathconf_args {
620	int	fd;
621	int	name;
622};
623#endif
624/* ARGSUSED */
625int
626fpathconf(p, uap)
627	struct proc *p;
628	register struct fpathconf_args *uap;
629{
630	struct filedesc *fdp = p->p_fd;
631	struct file *fp;
632	struct vnode *vp;
633
634	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
635	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
636		return (EBADF);
637	switch (fp->f_type) {
638
639	case DTYPE_PIPE:
640	case DTYPE_SOCKET:
641		if (uap->name != _PC_PIPE_BUF)
642			return (EINVAL);
643		p->p_retval[0] = PIPE_BUF;
644		return (0);
645
646	case DTYPE_FIFO:
647	case DTYPE_VNODE:
648		vp = (struct vnode *)fp->f_data;
649		return (VOP_PATHCONF(vp, uap->name, p->p_retval));
650
651	default:
652		return (EOPNOTSUPP);
653	}
654	/*NOTREACHED*/
655}
656
657/*
658 * Allocate a file descriptor for the process.
659 */
660static int fdexpand;
661SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
662
663int
664fdalloc(p, want, result)
665	struct proc *p;
666	int want;
667	int *result;
668{
669	register struct filedesc *fdp = p->p_fd;
670	register int i;
671	int lim, last, nfiles;
672	struct file **newofile;
673	char *newofileflags;
674
675	/*
676	 * Search for a free descriptor starting at the higher
677	 * of want or fd_freefile.  If that fails, consider
678	 * expanding the ofile array.
679	 */
680	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
681	for (;;) {
682		last = min(fdp->fd_nfiles, lim);
683		if ((i = want) < fdp->fd_freefile)
684			i = fdp->fd_freefile;
685		for (; i < last; i++) {
686			if (fdp->fd_ofiles[i] == NULL) {
687				fdp->fd_ofileflags[i] = 0;
688				if (i > fdp->fd_lastfile)
689					fdp->fd_lastfile = i;
690				if (want <= fdp->fd_freefile)
691					fdp->fd_freefile = i;
692				*result = i;
693				return (0);
694			}
695		}
696
697		/*
698		 * No space in current array.  Expand?
699		 */
700		if (fdp->fd_nfiles >= lim)
701			return (EMFILE);
702		if (fdp->fd_nfiles < NDEXTENT)
703			nfiles = NDEXTENT;
704		else
705			nfiles = 2 * fdp->fd_nfiles;
706		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
707		    M_FILEDESC, M_WAITOK);
708		newofileflags = (char *) &newofile[nfiles];
709		/*
710		 * Copy the existing ofile and ofileflags arrays
711		 * and zero the new portion of each array.
712		 */
713		bcopy(fdp->fd_ofiles, newofile,
714			(i = sizeof(struct file *) * fdp->fd_nfiles));
715		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
716		bcopy(fdp->fd_ofileflags, newofileflags,
717			(i = sizeof(char) * fdp->fd_nfiles));
718		bzero(newofileflags + i, nfiles * sizeof(char) - i);
719		if (fdp->fd_nfiles > NDFILE)
720			FREE(fdp->fd_ofiles, M_FILEDESC);
721		fdp->fd_ofiles = newofile;
722		fdp->fd_ofileflags = newofileflags;
723		fdp->fd_nfiles = nfiles;
724		fdexpand++;
725	}
726	return (0);
727}
728
729/*
730 * Check to see whether n user file descriptors
731 * are available to the process p.
732 */
733int
734fdavail(p, n)
735	struct proc *p;
736	register int n;
737{
738	register struct filedesc *fdp = p->p_fd;
739	register struct file **fpp;
740	register int i, lim, last;
741
742	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
743	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
744		return (1);
745
746	last = min(fdp->fd_nfiles, lim);
747	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
748	for (i = last - fdp->fd_freefile; --i >= 0; fpp++)
749		if (*fpp == NULL && --n <= 0)
750			return (1);
751	return (0);
752}
753
754/*
755 * Create a new open file structure and allocate
756 * a file decriptor for the process that refers to it.
757 */
758int
759falloc(p, resultfp, resultfd)
760	register struct proc *p;
761	struct file **resultfp;
762	int *resultfd;
763{
764	register struct file *fp, *fq;
765	int error, i;
766
767	if ((error = fdalloc(p, 0, &i)))
768		return (error);
769	if (nfiles >= maxfiles) {
770		tablefull("file");
771		return (ENFILE);
772	}
773	/*
774	 * Allocate a new file descriptor.
775	 * If the process has file descriptor zero open, add to the list
776	 * of open files at that point, otherwise put it at the front of
777	 * the list of open files.
778	 */
779	nfiles++;
780	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
781	bzero(fp, sizeof(struct file));
782	fp->f_count = 1;
783	fp->f_cred = p->p_ucred;
784	fp->f_ops = &badfileops;
785	fp->f_seqcount = 1;
786	crhold(fp->f_cred);
787	if ((fq = p->p_fd->fd_ofiles[0])) {
788		LIST_INSERT_AFTER(fq, fp, f_list);
789	} else {
790		LIST_INSERT_HEAD(&filehead, fp, f_list);
791	}
792	p->p_fd->fd_ofiles[i] = fp;
793	if (resultfp)
794		*resultfp = fp;
795	if (resultfd)
796		*resultfd = i;
797	return (0);
798}
799
800/*
801 * Free a file descriptor.
802 */
803void
804ffree(fp)
805	register struct file *fp;
806{
807	LIST_REMOVE(fp, f_list);
808	crfree(fp->f_cred);
809#if defined(DIAGNOSTIC) || defined(INVARIANTS)
810	fp->f_count = 0;
811#endif
812	nfiles--;
813	FREE(fp, M_FILE);
814}
815
816/*
817 * Build a new filedesc structure.
818 */
819struct filedesc *
820fdinit(p)
821	struct proc *p;
822{
823	register struct filedesc0 *newfdp;
824	register struct filedesc *fdp = p->p_fd;
825
826	MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
827	    M_FILEDESC, M_WAITOK);
828	bzero(newfdp, sizeof(struct filedesc0));
829	newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
830	if (newfdp->fd_fd.fd_cdir)
831		VREF(newfdp->fd_fd.fd_cdir);
832	newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
833	if (newfdp->fd_fd.fd_rdir)
834		VREF(newfdp->fd_fd.fd_rdir);
835	newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
836	if (newfdp->fd_fd.fd_jdir)
837		VREF(newfdp->fd_fd.fd_jdir);
838
839	/* Create the file descriptor table. */
840	newfdp->fd_fd.fd_refcnt = 1;
841	newfdp->fd_fd.fd_cmask = cmask;
842	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
843	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
844	newfdp->fd_fd.fd_nfiles = NDFILE;
845	newfdp->fd_fd.fd_knlistsize = -1;
846
847	return (&newfdp->fd_fd);
848}
849
850/*
851 * Share a filedesc structure.
852 */
853struct filedesc *
854fdshare(p)
855	struct proc *p;
856{
857	p->p_fd->fd_refcnt++;
858	return (p->p_fd);
859}
860
861/*
862 * Copy a filedesc structure.
863 */
864struct filedesc *
865fdcopy(p)
866	struct proc *p;
867{
868	register struct filedesc *newfdp, *fdp = p->p_fd;
869	register struct file **fpp;
870	register int i;
871
872	/* Certain daemons might not have file descriptors. */
873	if (fdp == NULL)
874		return (NULL);
875
876	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
877	    M_FILEDESC, M_WAITOK);
878	bcopy(fdp, newfdp, sizeof(struct filedesc));
879	if (newfdp->fd_cdir)
880		VREF(newfdp->fd_cdir);
881	if (newfdp->fd_rdir)
882		VREF(newfdp->fd_rdir);
883	if (newfdp->fd_jdir)
884		VREF(newfdp->fd_jdir);
885	newfdp->fd_refcnt = 1;
886
887	/*
888	 * If the number of open files fits in the internal arrays
889	 * of the open file structure, use them, otherwise allocate
890	 * additional memory for the number of descriptors currently
891	 * in use.
892	 */
893	if (newfdp->fd_lastfile < NDFILE) {
894		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
895		newfdp->fd_ofileflags =
896		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
897		i = NDFILE;
898	} else {
899		/*
900		 * Compute the smallest multiple of NDEXTENT needed
901		 * for the file descriptors currently in use,
902		 * allowing the table to shrink.
903		 */
904		i = newfdp->fd_nfiles;
905		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
906			i /= 2;
907		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
908		    M_FILEDESC, M_WAITOK);
909		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
910	}
911	newfdp->fd_nfiles = i;
912	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
913	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
914
915	/*
916	 * kq descriptors cannot be copied.
917	 */
918	if (newfdp->fd_knlistsize != -1) {
919		fpp = newfdp->fd_ofiles;
920		for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
921			if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE)
922				*fpp = NULL;
923		newfdp->fd_knlist = NULL;
924		newfdp->fd_knlistsize = -1;
925		newfdp->fd_knhash = NULL;
926		newfdp->fd_knhashmask = 0;
927	}
928
929	fpp = newfdp->fd_ofiles;
930	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
931		if (*fpp != NULL)
932			fhold(*fpp);
933	return (newfdp);
934}
935
936/*
937 * Release a filedesc structure.
938 */
939void
940fdfree(p)
941	struct proc *p;
942{
943	register struct filedesc *fdp = p->p_fd;
944	struct file **fpp;
945	register int i;
946
947	/* Certain daemons might not have file descriptors. */
948	if (fdp == NULL)
949		return;
950
951	if (--fdp->fd_refcnt > 0)
952		return;
953	fpp = fdp->fd_ofiles;
954	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
955		if (*fpp)
956			(void) closef(*fpp, p);
957	if (fdp->fd_nfiles > NDFILE)
958		FREE(fdp->fd_ofiles, M_FILEDESC);
959	if (fdp->fd_cdir)
960		vrele(fdp->fd_cdir);
961	if (fdp->fd_rdir)
962		vrele(fdp->fd_rdir);
963	if (fdp->fd_jdir)
964		vrele(fdp->fd_jdir);
965	if (fdp->fd_knlist)
966		FREE(fdp->fd_knlist, M_TEMP);
967	if (fdp->fd_knhash)
968		FREE(fdp->fd_knhash, M_TEMP);
969	FREE(fdp, M_FILEDESC);
970}
971
972/*
973 * For setugid programs, we don't want to people to use that setugidness
974 * to generate error messages which write to a file which otherwise would
975 * otherwise be off-limits to the process.
976 *
977 * This is a gross hack to plug the hole.  A better solution would involve
978 * a special vop or other form of generalized access control mechanism.  We
979 * go ahead and just reject all procfs file systems accesses as dangerous.
980 *
981 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
982 * sufficient.  We also don't for check setugidness since we know we are.
983 */
984static int
985is_unsafe(struct file *fp)
986{
987	if (fp->f_type == DTYPE_VNODE &&
988	    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
989		return (1);
990	return (0);
991}
992
993/*
994 * Make this setguid thing safe, if at all possible.
995 */
996void
997setugidsafety(p)
998	struct proc *p;
999{
1000	struct filedesc *fdp = p->p_fd;
1001	struct file **fpp;
1002	char *fdfp;
1003	register int i;
1004
1005	/* Certain daemons might not have file descriptors. */
1006	if (fdp == NULL)
1007		return;
1008
1009	fpp = fdp->fd_ofiles;
1010	fdfp = fdp->fd_ofileflags;
1011	for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++) {
1012		if (i > 2)
1013			break;
1014		if (*fpp != NULL && is_unsafe(*fpp)) {
1015			if ((*fdfp & UF_MAPPED) != 0)
1016				(void) munmapfd(p, i);
1017			(void) closef(*fpp, p);
1018			*fpp = NULL;
1019			*fdfp = 0;
1020			if (i < fdp->fd_freefile)
1021				fdp->fd_freefile = i;
1022		}
1023	}
1024	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1025		fdp->fd_lastfile--;
1026}
1027
1028/*
1029 * Close any files on exec?
1030 */
1031void
1032fdcloseexec(p)
1033	struct proc *p;
1034{
1035	struct filedesc *fdp = p->p_fd;
1036	struct file **fpp;
1037	char *fdfp;
1038	register int i;
1039
1040	/* Certain daemons might not have file descriptors. */
1041	if (fdp == NULL)
1042		return;
1043
1044	fpp = fdp->fd_ofiles;
1045	fdfp = fdp->fd_ofileflags;
1046	for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++)
1047		if (*fpp != NULL && (*fdfp & UF_EXCLOSE)) {
1048			if (*fdfp & UF_MAPPED)
1049				(void) munmapfd(p, i);
1050			(void) closef(*fpp, p);
1051			*fpp = NULL;
1052			*fdfp = 0;
1053			if (i < fdp->fd_freefile)
1054				fdp->fd_freefile = i;
1055		}
1056	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1057		fdp->fd_lastfile--;
1058}
1059
1060/*
1061 * Internal form of close.
1062 * Decrement reference count on file structure.
1063 * Note: p may be NULL when closing a file
1064 * that was being passed in a message.
1065 */
1066int
1067closef(fp, p)
1068	register struct file *fp;
1069	register struct proc *p;
1070{
1071	struct vnode *vp;
1072	struct flock lf;
1073
1074	if (fp == NULL)
1075		return (0);
1076	/*
1077	 * POSIX record locking dictates that any close releases ALL
1078	 * locks owned by this process.  This is handled by setting
1079	 * a flag in the unlock to free ONLY locks obeying POSIX
1080	 * semantics, and not to free BSD-style file locks.
1081	 * If the descriptor was in a message, POSIX-style locks
1082	 * aren't passed with the descriptor.
1083	 */
1084	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1085		lf.l_whence = SEEK_SET;
1086		lf.l_start = 0;
1087		lf.l_len = 0;
1088		lf.l_type = F_UNLCK;
1089		vp = (struct vnode *)fp->f_data;
1090		(void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, &lf, F_POSIX);
1091	}
1092	return (fdrop(fp, p));
1093}
1094
1095int
1096fdrop(fp, p)
1097	struct file *fp;
1098	struct proc *p;
1099{
1100	struct flock lf;
1101	struct vnode *vp;
1102	int error;
1103
1104	if (--fp->f_count > 0)
1105		return (0);
1106	if (fp->f_count < 0)
1107		panic("fdrop: count < 0");
1108	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1109		lf.l_whence = SEEK_SET;
1110		lf.l_start = 0;
1111		lf.l_len = 0;
1112		lf.l_type = F_UNLCK;
1113		vp = (struct vnode *)fp->f_data;
1114		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1115	}
1116	if (fp->f_ops != &badfileops)
1117		error = fo_close(fp, p);
1118	else
1119		error = 0;
1120	ffree(fp);
1121	return (error);
1122}
1123
1124/*
1125 * Apply an advisory lock on a file descriptor.
1126 *
1127 * Just attempt to get a record lock of the requested type on
1128 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1129 */
1130#ifndef _SYS_SYSPROTO_H_
1131struct flock_args {
1132	int	fd;
1133	int	how;
1134};
1135#endif
1136/* ARGSUSED */
1137int
1138flock(p, uap)
1139	struct proc *p;
1140	register struct flock_args *uap;
1141{
1142	register struct filedesc *fdp = p->p_fd;
1143	register struct file *fp;
1144	struct vnode *vp;
1145	struct flock lf;
1146
1147	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1148	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1149		return (EBADF);
1150	if (fp->f_type != DTYPE_VNODE)
1151		return (EOPNOTSUPP);
1152	vp = (struct vnode *)fp->f_data;
1153	lf.l_whence = SEEK_SET;
1154	lf.l_start = 0;
1155	lf.l_len = 0;
1156	if (uap->how & LOCK_UN) {
1157		lf.l_type = F_UNLCK;
1158		fp->f_flag &= ~FHASLOCK;
1159		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1160	}
1161	if (uap->how & LOCK_EX)
1162		lf.l_type = F_WRLCK;
1163	else if (uap->how & LOCK_SH)
1164		lf.l_type = F_RDLCK;
1165	else
1166		return (EBADF);
1167	fp->f_flag |= FHASLOCK;
1168	if (uap->how & LOCK_NB)
1169		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1170	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1171}
1172
1173/*
1174 * File Descriptor pseudo-device driver (/dev/fd/).
1175 *
1176 * Opening minor device N dup()s the file (if any) connected to file
1177 * descriptor N belonging to the calling process.  Note that this driver
1178 * consists of only the ``open()'' routine, because all subsequent
1179 * references to this file will be direct to the other driver.
1180 */
1181/* ARGSUSED */
1182static int
1183fdopen(dev, mode, type, p)
1184	dev_t dev;
1185	int mode, type;
1186	struct proc *p;
1187{
1188
1189	/*
1190	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1191	 * the file descriptor being sought for duplication. The error
1192	 * return ensures that the vnode for this device will be released
1193	 * by vn_open. Open will detect this special error and take the
1194	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1195	 * will simply report the error.
1196	 */
1197	p->p_dupfd = minor(dev);
1198	return (ENODEV);
1199}
1200
1201/*
1202 * Duplicate the specified descriptor to a free descriptor.
1203 */
1204int
1205dupfdopen(fdp, indx, dfd, mode, error)
1206	register struct filedesc *fdp;
1207	register int indx, dfd;
1208	int mode;
1209	int error;
1210{
1211	register struct file *wfp;
1212	struct file *fp;
1213
1214	/*
1215	 * If the to-be-dup'd fd number is greater than the allowed number
1216	 * of file descriptors, or the fd to be dup'd has already been
1217	 * closed, reject.  Note, check for new == old is necessary as
1218	 * falloc could allocate an already closed to-be-dup'd descriptor
1219	 * as the new descriptor.
1220	 */
1221	fp = fdp->fd_ofiles[indx];
1222	if ((u_int)dfd >= fdp->fd_nfiles ||
1223	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
1224		return (EBADF);
1225
1226	/*
1227	 * There are two cases of interest here.
1228	 *
1229	 * For ENODEV simply dup (dfd) to file descriptor
1230	 * (indx) and return.
1231	 *
1232	 * For ENXIO steal away the file structure from (dfd) and
1233	 * store it in (indx).  (dfd) is effectively closed by
1234	 * this operation.
1235	 *
1236	 * Any other error code is just returned.
1237	 */
1238	switch (error) {
1239	case ENODEV:
1240		/*
1241		 * Check that the mode the file is being opened for is a
1242		 * subset of the mode of the existing descriptor.
1243		 */
1244		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1245			return (EACCES);
1246		fdp->fd_ofiles[indx] = wfp;
1247		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1248		fhold(wfp);
1249		if (indx > fdp->fd_lastfile)
1250			fdp->fd_lastfile = indx;
1251		return (0);
1252
1253	case ENXIO:
1254		/*
1255		 * Steal away the file pointer from dfd, and stuff it into indx.
1256		 */
1257		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1258		fdp->fd_ofiles[dfd] = NULL;
1259		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1260		fdp->fd_ofileflags[dfd] = 0;
1261		/*
1262		 * Complete the clean up of the filedesc structure by
1263		 * recomputing the various hints.
1264		 */
1265		if (indx > fdp->fd_lastfile)
1266			fdp->fd_lastfile = indx;
1267		else
1268			while (fdp->fd_lastfile > 0 &&
1269			       fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1270				fdp->fd_lastfile--;
1271			if (dfd < fdp->fd_freefile)
1272				fdp->fd_freefile = dfd;
1273		return (0);
1274
1275	default:
1276		return (error);
1277	}
1278	/* NOTREACHED */
1279}
1280
1281/*
1282 * Get file structures.
1283 */
1284static int
1285sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1286{
1287	int error;
1288	struct file *fp;
1289
1290	if (!req->oldptr) {
1291		/*
1292		 * overestimate by 10 files
1293		 */
1294		return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1295				(nfiles + 10) * sizeof(struct file)));
1296	}
1297
1298	error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1299	if (error)
1300		return (error);
1301
1302	/*
1303	 * followed by an array of file structures
1304	 */
1305	LIST_FOREACH(fp, &filehead, f_list) {
1306		error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1307		if (error)
1308			return (error);
1309	}
1310	return (0);
1311}
1312
1313SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1314    0, 0, sysctl_kern_file, "S,file", "Entire file table");
1315
1316SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1317    &maxfilesperproc, 0, "Maximum files allowed open per process");
1318
1319SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1320    &maxfiles, 0, "Maximum number of files");
1321
1322static void
1323fildesc_clone(void *arg, char *name, int namelen, dev_t *dev)
1324{
1325	int u;
1326
1327	if (*dev != NODEV)
1328		return;
1329	if (devfs_stdclone(name, NULL, "fd/", &u) != 1)
1330		return;
1331	if (u <= 2)
1332		return;
1333	*dev = make_dev(&fildesc_cdevsw, u, UID_BIN, GID_BIN, 0666, name);
1334	return;
1335}
1336
1337static void
1338fildesc_drvinit(void *unused)
1339{
1340	dev_t dev;
1341
1342	dev = make_dev(&fildesc_cdevsw, 0, UID_BIN, GID_BIN, 0666, "fd/0");
1343	make_dev_alias(dev, "stdin");
1344	dev = make_dev(&fildesc_cdevsw, 1, UID_BIN, GID_BIN, 0666, "fd/1");
1345	make_dev_alias(dev, "stdout");
1346	dev = make_dev(&fildesc_cdevsw, 2, UID_BIN, GID_BIN, 0666, "fd/2");
1347	make_dev_alias(dev, "stderr");
1348#ifdef DEVFS
1349	EVENTHANDLER_REGISTER(devfs_clone, fildesc_clone, 0, 1000);
1350#else
1351	{
1352	int fd;
1353
1354	for (fd = 3; fd < NUMFDESC; fd++)
1355		make_dev(&fildesc_cdevsw, fd, UID_BIN, GID_BIN, 0666, "fd/%d", fd);
1356	}
1357#endif
1358}
1359
1360struct fileops badfileops = {
1361	badfo_readwrite,
1362	badfo_readwrite,
1363	badfo_ioctl,
1364	badfo_poll,
1365	badfo_stat,
1366	badfo_close
1367};
1368
1369static int
1370badfo_readwrite(fp, uio, cred, flags, p)
1371	struct file *fp;
1372	struct uio *uio;
1373	struct ucred *cred;
1374	struct proc *p;
1375	int flags;
1376{
1377
1378	return (EBADF);
1379}
1380
1381static int
1382badfo_ioctl(fp, com, data, p)
1383	struct file *fp;
1384	u_long com;
1385	caddr_t data;
1386	struct proc *p;
1387{
1388
1389	return (EBADF);
1390}
1391
1392static int
1393badfo_poll(fp, events, cred, p)
1394	struct file *fp;
1395	int events;
1396	struct ucred *cred;
1397	struct proc *p;
1398{
1399
1400	return (0);
1401}
1402
1403static int
1404badfo_stat(fp, sb, p)
1405	struct file *fp;
1406	struct stat *sb;
1407	struct proc *p;
1408{
1409
1410	return (EBADF);
1411}
1412
1413static int
1414badfo_close(fp, p)
1415	struct file *fp;
1416	struct proc *p;
1417{
1418
1419	return (EBADF);
1420}
1421
1422SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1423					fildesc_drvinit,NULL)
1424