+void
+cluster_init(void) {
+ /*
+ * allocate lock group attribute and group
+ */
+ cl_mtx_grp_attr = lck_grp_attr_alloc_init();
+ cl_mtx_grp = lck_grp_alloc_init("cluster I/O", cl_mtx_grp_attr);
+
+ /*
+ * allocate the lock attribute
+ */
+ cl_mtx_attr = lck_attr_alloc_init();
+
+ /*
+ * allocate and initialize mutex's used to protect updates and waits
+ * on the cluster_io context
+ */
+ cl_mtxp = lck_mtx_alloc_init(cl_mtx_grp, cl_mtx_attr);
+
+ if (cl_mtxp == NULL)
+ panic("cluster_init: failed to allocate cl_mtxp");
+}
+
+
+
+#define CLW_ALLOCATE 0x01
+#define CLW_RETURNLOCKED 0x02
+#define CLW_IONOCACHE 0x04
+#define CLW_IOPASSIVE 0x08
+
+/*
+ * if the read ahead context doesn't yet exist,
+ * allocate and initialize it...
+ * the vnode lock serializes multiple callers
+ * during the actual assignment... first one
+ * to grab the lock wins... the other callers
+ * will release the now unnecessary storage
+ *
+ * once the context is present, try to grab (but don't block on)
+ * the lock associated with it... if someone
+ * else currently owns it, than the read
+ * will run without read-ahead. this allows
+ * multiple readers to run in parallel and
+ * since there's only 1 read ahead context,
+ * there's no real loss in only allowing 1
+ * reader to have read-ahead enabled.
+ */
+static struct cl_readahead *
+cluster_get_rap(vnode_t vp)
+{
+ struct ubc_info *ubc;
+ struct cl_readahead *rap;
+
+ ubc = vp->v_ubcinfo;
+
+ if ((rap = ubc->cl_rahead) == NULL) {
+ MALLOC_ZONE(rap, struct cl_readahead *, sizeof *rap, M_CLRDAHEAD, M_WAITOK);
+
+ bzero(rap, sizeof *rap);
+ rap->cl_lastr = -1;
+ lck_mtx_init(&rap->cl_lockr, cl_mtx_grp, cl_mtx_attr);
+
+ vnode_lock(vp);
+
+ if (ubc->cl_rahead == NULL)
+ ubc->cl_rahead = rap;
+ else {
+ lck_mtx_destroy(&rap->cl_lockr, cl_mtx_grp);
+ FREE_ZONE((void *)rap, sizeof *rap, M_CLRDAHEAD);
+ rap = ubc->cl_rahead;
+ }
+ vnode_unlock(vp);
+ }
+ if (lck_mtx_try_lock(&rap->cl_lockr) == TRUE)
+ return(rap);
+
+ return ((struct cl_readahead *)NULL);
+}
+
+
+/*
+ * if the write behind context doesn't yet exist,
+ * and CLW_ALLOCATE is specified, allocate and initialize it...
+ * the vnode lock serializes multiple callers
+ * during the actual assignment... first one
+ * to grab the lock wins... the other callers
+ * will release the now unnecessary storage
+ *
+ * if CLW_RETURNLOCKED is set, grab (blocking if necessary)
+ * the lock associated with the write behind context before
+ * returning
+ */
+
+static struct cl_writebehind *
+cluster_get_wbp(vnode_t vp, int flags)
+{
+ struct ubc_info *ubc;
+ struct cl_writebehind *wbp;
+
+ ubc = vp->v_ubcinfo;
+
+ if ((wbp = ubc->cl_wbehind) == NULL) {
+
+ if ( !(flags & CLW_ALLOCATE))
+ return ((struct cl_writebehind *)NULL);
+
+ MALLOC_ZONE(wbp, struct cl_writebehind *, sizeof *wbp, M_CLWRBEHIND, M_WAITOK);
+
+ bzero(wbp, sizeof *wbp);
+ lck_mtx_init(&wbp->cl_lockw, cl_mtx_grp, cl_mtx_attr);
+
+ vnode_lock(vp);
+
+ if (ubc->cl_wbehind == NULL)
+ ubc->cl_wbehind = wbp;
+ else {
+ lck_mtx_destroy(&wbp->cl_lockw, cl_mtx_grp);
+ FREE_ZONE((void *)wbp, sizeof *wbp, M_CLWRBEHIND);
+ wbp = ubc->cl_wbehind;
+ }
+ vnode_unlock(vp);
+ }
+ if (flags & CLW_RETURNLOCKED)
+ lck_mtx_lock(&wbp->cl_lockw);
+
+ return (wbp);
+}
+
+
+static void
+cluster_syncup(vnode_t vp, off_t newEOF, int (*callback)(buf_t, void *), void *callback_arg)
+{
+ struct cl_writebehind *wbp;
+
+ if ((wbp = cluster_get_wbp(vp, 0)) != NULL) {
+
+ if (wbp->cl_number) {
+ lck_mtx_lock(&wbp->cl_lockw);
+
+ cluster_try_push(wbp, vp, newEOF, PUSH_ALL | PUSH_SYNC, callback, callback_arg);
+
+ lck_mtx_unlock(&wbp->cl_lockw);
+ }
+ }
+}
+
+