generalize the buffer pool so that NFS can become a client
[unix-history] / usr / src / sys / kern / uipc_mbuf.c
index 691e346..d301a64 100644 (file)
@@ -1,9 +1,19 @@
 /*
 /*
- * Copyright (c) 1982 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
  *
  *
- *     @(#)uipc_mbuf.c 6.6 (Berkeley) %G%
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)uipc_mbuf.c 7.4.1.3 (Berkeley) %G%
  */
 
 #include "../machine/pte.h"
  */
 
 #include "../machine/pte.h"
 #include "mbuf.h"
 #include "vm.h"
 #include "kernel.h"
 #include "mbuf.h"
 #include "vm.h"
 #include "kernel.h"
+#include "syslog.h"
+#include "domain.h"
+#include "protosw.h"
 
 mbinit()
 {
        int s;
 
 
 mbinit()
 {
        int s;
 
+#if CLBYTES < 4096
+#define NCL_INIT       (4096/CLBYTES)
+#else
+#define NCL_INIT       1
+#endif
        s = splimp();
        s = splimp();
-       if (m_clalloc(4096/CLBYTES, MPG_MBUFS, M_DONTWAIT) == 0)
+       if (m_clalloc(NCL_INIT, MPG_MBUFS, M_DONTWAIT) == 0)
                goto bad;
                goto bad;
-       if (m_clalloc(8*4096/CLBYTES, MPG_CLUSTERS, M_DONTWAIT) == 0)
+       if (m_clalloc(NCL_INIT, MPG_CLUSTERS, M_DONTWAIT) == 0)
                goto bad;
        splx(s);
        return;
                goto bad;
        splx(s);
        return;
@@ -36,6 +54,7 @@ bad:
 /*
  * Must be called at splimp.
  */
 /*
  * Must be called at splimp.
  */
+/* ARGSUSED */
 caddr_t
 m_clalloc(ncl, how, canwait)
        register int ncl;
 caddr_t
 m_clalloc(ncl, how, canwait)
        register int ncl;
@@ -44,15 +63,18 @@ m_clalloc(ncl, how, canwait)
        int npg, mbx;
        register struct mbuf *m;
        register int i;
        int npg, mbx;
        register struct mbuf *m;
        register int i;
+       static int logged;
 
        npg = ncl * CLSIZE;
        mbx = rmalloc(mbmap, (long)npg);
        if (mbx == 0) {
 
        npg = ncl * CLSIZE;
        mbx = rmalloc(mbmap, (long)npg);
        if (mbx == 0) {
-               if (canwait == M_WAIT)
-                       panic("out of mbuf map");
+               if (logged == 0) {
+                       logged++;
+                       log(LOG_ERR, "mbuf map full\n");
+               }
                return (0);
        }
                return (0);
        }
-       m = cltom(mbx / CLSIZE);
+       m = cltom(mbx * NBPG / MCLBYTES);
        if (memall(&Mbmap[mbx], npg, proc, CSYS) == 0) {
                rmfree(mbmap, (long)npg, (long)mbx);
                return (0);
        if (memall(&Mbmap[mbx], npg, proc, CSYS) == 0) {
                rmfree(mbmap, (long)npg, (long)mbx);
                return (0);
@@ -61,11 +83,12 @@ m_clalloc(ncl, how, canwait)
        switch (how) {
 
        case MPG_CLUSTERS:
        switch (how) {
 
        case MPG_CLUSTERS:
+               ncl = ncl * CLBYTES / MCLBYTES;
                for (i = 0; i < ncl; i++) {
                        m->m_off = 0;
                        m->m_next = mclfree;
                        mclfree = m;
                for (i = 0; i < ncl; i++) {
                        m->m_off = 0;
                        m->m_next = mclfree;
                        mclfree = m;
-                       m += CLBYTES / sizeof (*m);
+                       m += MCLBYTES / sizeof (*m);
                        mbstat.m_clfree++;
                }
                mbstat.m_clusters += ncl;
                        mbstat.m_clfree++;
                }
                mbstat.m_clusters += ncl;
@@ -85,29 +108,30 @@ m_clalloc(ncl, how, canwait)
        return ((caddr_t)m);
 }
 
        return ((caddr_t)m);
 }
 
-m_pgfree(addr, n)
-       caddr_t addr;
-       int n;
-{
-
-#ifdef lint
-       addr = addr; n = n;
-#endif
-}
-
 /*
  * Must be called at splimp.
  */
 m_expand(canwait)
        int canwait;
 {
 /*
  * Must be called at splimp.
  */
 m_expand(canwait)
        int canwait;
 {
+       register struct domain *dp;
+       register struct protosw *pr;
+       int tries;
 
 
-       if (m_clalloc(1, MPG_MBUFS, canwait) == 0)
-               goto steal;
-       return (1);
-steal:
-       /* should ask protocols to free code */
-       return (0);
+       for (tries = 0;; ) {
+               if (m_clalloc(1, MPG_MBUFS, canwait))
+                       return (1);
+               if (canwait == 0 || tries++)
+                       return (0);
+
+               /* ask protocols to free space */
+               for (dp = domains; dp; dp = dp->dom_next)
+                       for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW;
+                           pr++)
+                               if (pr->pr_drain)
+                                       (*pr->pr_drain)();
+               mbstat.m_drain++;
+       }
 }
 
 /* NEED SOME WAY TO RELEASE SPACE */
 }
 
 /* NEED SOME WAY TO RELEASE SPACE */
@@ -163,8 +187,11 @@ m_more(canwait, type)
 
        while (m_expand(canwait) == 0) {
                if (canwait == M_WAIT) {
 
        while (m_expand(canwait) == 0) {
                if (canwait == M_WAIT) {
+                       mbstat.m_wait++;
                        m_want++;
                        m_want++;
-                       sleep((caddr_t)mfree, PZERO - 1);
+                       sleep((caddr_t)&mfree, PZERO - 1);
+                       if (mfree)
+                               break;
                } else {
                        mbstat.m_drops++;
                        return (NULL);
                } else {
                        mbstat.m_drops++;
                        return (NULL);
@@ -195,6 +222,7 @@ m_freem(m)
  * Mbuffer utility routines.
  */
 
  * Mbuffer utility routines.
  */
 
+/*
 /*
  * Make a copy of an mbuf chain starting "off" bytes from the beginning,
  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
 /*
  * Make a copy of an mbuf chain starting "off" bytes from the beginning,
  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
@@ -253,6 +281,40 @@ nospace:
        return (0);
 }
 
        return (0);
 }
 
+/*
+ * Copy data from an mbuf chain starting "off" bytes from the beginning,
+ * continuing for "len" bytes, into the indicated buffer.
+ */
+m_copydata(m, off, len, cp)
+       register struct mbuf *m;
+       register int off;
+       register int len;
+       caddr_t cp;
+{
+       register unsigned count;
+
+       if (off < 0 || len < 0)
+               panic("m_copydata");
+       while (off > 0) {
+               if (m == 0)
+                       panic("m_copydata");
+               if (off < m->m_len)
+                       break;
+               off -= m->m_len;
+               m = m->m_next;
+       }
+       while (len > 0) {
+               if (m == 0)
+                       panic("m_copydata");
+               count = MIN(m->m_len - off, len);
+               bcopy(mtod(m, caddr_t) + off, cp, count);
+               len -= count;
+               cp += count;
+               off = 0;
+               m = m->m_next;
+       }
+}
+
 m_cat(m, n)
        register struct mbuf *m, *n;
 {
 m_cat(m, n)
        register struct mbuf *m, *n;
 {
@@ -277,7 +339,8 @@ m_adj(mp, len)
        struct mbuf *mp;
        register int len;
 {
        struct mbuf *mp;
        register int len;
 {
-       register struct mbuf *m, *n;
+       register struct mbuf *m;
+       register count;
 
        if ((m = mp) == NULL)
                return;
 
        if ((m = mp) == NULL)
                return;
@@ -294,49 +357,75 @@ m_adj(mp, len)
                        }
                }
        } else {
                        }
                }
        } else {
-               /* a 2 pass algorithm might be better */
+               /*
+                * Trim from tail.  Scan the mbuf chain,
+                * calculating its length and finding the last mbuf.
+                * If the adjustment only affects this mbuf, then just
+                * adjust and return.  Otherwise, rescan and truncate
+                * after the remaining size.
+                */
                len = -len;
                len = -len;
-               while (len > 0 && m->m_len != 0) {
-                       while (m != NULL && m->m_len != 0) {
-                               n = m;
-                               m = m->m_next;
-                       }
-                       if (n->m_len <= len) {
-                               len -= n->m_len;
-                               n->m_len = 0;
-                               m = mp;
-                       } else {
-                               n->m_len -= len;
+               count = 0;
+               for (;;) {
+                       count += m->m_len;
+                       if (m->m_next == (struct mbuf *)0)
+                               break;
+                       m = m->m_next;
+               }
+               if (m->m_len >= len) {
+                       m->m_len -= len;
+                       return;
+               }
+               count -= len;
+               /*
+                * Correct length for chain is "count".
+                * Find the mbuf with last data, adjust its length,
+                * and toss data from remaining mbufs on chain.
+                */
+               for (m = mp; m; m = m->m_next) {
+                       if (m->m_len >= count) {
+                               m->m_len = count;
                                break;
                        }
                                break;
                        }
+                       count -= m->m_len;
                }
                }
+               while (m = m->m_next)
+                       m->m_len = 0;
        }
 }
 
 /*
  * Rearange an mbuf chain so that len bytes are contiguous
  * and in the data area of an mbuf (so that mtod and dtom
        }
 }
 
 /*
  * Rearange an mbuf chain so that len bytes are contiguous
  * and in the data area of an mbuf (so that mtod and dtom
- * will work for a structure of size len).
- * Returns the resulting mbuf chain on success,
- * frees it and returns null on failure.
+ * will work for a structure of size len).  Returns the resulting
+ * mbuf chain on success, frees it and returns null on failure.
+ * If there is room, it will add up to MPULL_EXTRA bytes to the
+ * contiguous region in an attempt to avoid being called next time.
  */
 struct mbuf *
  */
 struct mbuf *
-m_pullup(m0, len)
-       struct mbuf *m0;
+m_pullup(n, len)
+       register struct mbuf *n;
        int len;
 {
        int len;
 {
-       register struct mbuf *m, *n;
-       int count;
+       register struct mbuf *m;
+       register int count;
+       int space;
 
 
-       n = m0;
-       if (len > MLEN)
-               goto bad;
-       MGET(m, M_DONTWAIT, n->m_type);
-       if (m == 0)
-               goto bad;
-       m->m_len = 0;
+       if (n->m_off + len <= MMAXOFF && n->m_next) {
+               m = n;
+               n = n->m_next;
+               len -= m->m_len;
+       } else {
+               if (len > MLEN)
+                       goto bad;
+               MGET(m, M_DONTWAIT, n->m_type);
+               if (m == 0)
+                       goto bad;
+               m->m_len = 0;
+       }
+       space = MMAXOFF - m->m_off;
        do {
        do {
-               count = MIN(n->m_len, len);
+               count = MIN(MIN(space - m->m_len, len + MPULL_EXTRA), n->m_len);
                bcopy(mtod(n, caddr_t), mtod(m, caddr_t)+m->m_len,
                  (unsigned)count);
                len -= count;
                bcopy(mtod(n, caddr_t), mtod(m, caddr_t)+m->m_len,
                  (unsigned)count);
                len -= count;
@@ -346,8 +435,8 @@ m_pullup(m0, len)
                        n->m_off += count;
                else
                        n = m_free(n);
                        n->m_off += count;
                else
                        n = m_free(n);
-       } while (len && n);
-       if (len) {
+       } while (len > 0 && n);
+       if (len > 0) {
                (void) m_free(m);
                goto bad;
        }
                (void) m_free(m);
                goto bad;
        }