From owner-svn-src-user@FreeBSD.ORG Mon Jan 11 00:11:17 2010 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 43FA61065672; Mon, 11 Jan 2010 00:11:17 +0000 (UTC) (envelope-from kmacy@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 333378FC08; Mon, 11 Jan 2010 00:11:17 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o0B0BHwY084399; Mon, 11 Jan 2010 00:11:17 GMT (envelope-from kmacy@svn.freebsd.org) Received: (from kmacy@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o0B0BH7U084397; Mon, 11 Jan 2010 00:11:17 GMT (envelope-from kmacy@svn.freebsd.org) Message-Id: <201001110011.o0B0BH7U084397@svn.freebsd.org> From: Kip Macy Date: Mon, 11 Jan 2010 00:11:17 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r202053 - user/kmacy/releng_8_rump/lib/libunet X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Jan 2010 00:11:17 -0000 Author: kmacy Date: Mon Jan 11 00:11:16 2010 New Revision: 202053 URL: http://svn.freebsd.org/changeset/base/202053 Log: add basic descriptor management Modified: user/kmacy/releng_8_rump/lib/libunet/unet_kern_descrip.c Modified: user/kmacy/releng_8_rump/lib/libunet/unet_kern_descrip.c ============================================================================== --- user/kmacy/releng_8_rump/lib/libunet/unet_kern_descrip.c Sun Jan 10 23:51:02 2010 (r202052) +++ user/kmacy/releng_8_rump/lib/libunet/unet_kern_descrip.c Mon Jan 11 00:11:16 2010 (r202053) @@ -76,6 +76,15 @@ __FBSDID("$FreeBSD$"); #include #include +static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table"); + +static uma_zone_t file_zone; + +volatile int openfiles; /* actual number of open files */ + +/* A mutex to protect the association between a proc and filedesc. */ +static struct mtx fdesc_mtx; + /* * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). @@ -87,16 +96,13 @@ int fsetown(pid_t pgid, struct sigio **sigiop) { - panic(""); - return (0); } pid_t fgetown(struct sigio **sigiop) { - - panic(""); + return (0); } @@ -104,8 +110,7 @@ fgetown(struct sigio **sigiop) void funsetown(struct sigio **sigiop) { - - panic(""); + /* nothing to do here */ } @@ -119,8 +124,52 @@ funsetown(struct sigio **sigiop) int falloc(struct thread *td, struct file **resultfp, int *resultfd) { - panic(""); + struct proc *p = td->td_proc; + struct file *fp; + int error, i; + int maxuserfiles = maxfiles - (maxfiles / 20); + static struct timeval lastfail; + static int curfail; + + fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO); + if ((openfiles >= maxuserfiles && + priv_check(td, PRIV_MAXFILES) != 0) || + openfiles >= maxfiles) { + if (ppsratecheck(&lastfail, &curfail, 1)) { + printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n", + td->td_ucred->cr_ruid); + } + uma_zfree(file_zone, fp); + return (ENFILE); + } + atomic_add_int(&openfiles, 1); + /* + * If the process has file descriptor zero open, add the new file + * descriptor to the list of open files at that point, otherwise + * put it at the front of the list of open files. + */ + refcount_init(&fp->f_count, 1); + if (resultfp) + fhold(fp); + fp->f_cred = crhold(td->td_ucred); + fp->f_ops = &badfileops; + fp->f_data = NULL; + fp->f_vnode = NULL; + FILEDESC_XLOCK(p->p_fd); + if ((error = fdalloc(td, 0, &i))) { + FILEDESC_XUNLOCK(p->p_fd); + fdrop(fp, td); + if (resultfp) + fdrop(fp, td); + return (error); + } + p->p_fd->fd_ofiles[i] = fp; + FILEDESC_XUNLOCK(p->p_fd); + if (resultfp) + *resultfp = fp; + if (resultfd) + *resultfd = i; return (0); } @@ -131,9 +180,24 @@ falloc(struct thread *td, struct file ** int _fdrop(struct file *fp, struct thread *td) { + int error; - panic(""); - return (0); + error = 0; + if (fp->f_count != 0) + panic("fdrop: count %d", fp->f_count); + if (fp->f_ops != &badfileops) + error = fo_close(fp, td); + /* + * The f_cdevpriv cannot be assigned non-NULL value while we + * are destroying the file. + */ + if (fp->f_cdevpriv != NULL) + devfs_fpdrop(fp); + atomic_subtract_int(&openfiles, 1); + crfree(fp->f_cred); + uma_zfree(file_zone, fp); + + return (error); } void @@ -245,6 +309,18 @@ fget_write(struct thread *td, int fd, st } +/* ARGSUSED*/ +static void +filelistinit(void *dummy) +{ + + file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, + NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); + mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); + mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF); +} +SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL); + /*-------------------------------------------------------------------*/ static int