xref: /csrg-svn/sys/miscfs/union/union_subr.c (revision 65997)
165934Spendry /*
265934Spendry  * Copyright (c) 1994 Jan-Simon Pendry
365934Spendry  * Copyright (c) 1994
465934Spendry  *	The Regents of the University of California.  All rights reserved.
565934Spendry  *
665934Spendry  * This code is derived from software contributed to Berkeley by
765934Spendry  * Jan-Simon Pendry.
865934Spendry  *
965934Spendry  * %sccs.include.redist.c%
1065934Spendry  *
11*65997Spendry  *	@(#)union_subr.c	1.6 (Berkeley) 02/04/94
1265934Spendry  */
1365934Spendry 
1465934Spendry #include <sys/param.h>
1565934Spendry #include <sys/systm.h>
1665934Spendry #include <sys/time.h>
1765934Spendry #include <sys/kernel.h>
1865934Spendry #include <sys/vnode.h>
1965934Spendry #include <sys/namei.h>
2065934Spendry #include <sys/malloc.h>
2165994Spendry #include <sys/file.h>
22*65997Spendry #include <sys/filedesc.h>
2365934Spendry #include "union.h" /*<miscfs/union/union.h>*/
2465934Spendry 
2565992Spendry #ifdef DIAGNOSTIC
2665992Spendry #include <sys/proc.h>
2765992Spendry #endif
2865992Spendry 
2965934Spendry static struct union_node *unhead;
3065934Spendry static int unvplock;
3165934Spendry 
3265934Spendry int
3365934Spendry union_init()
3465934Spendry {
3565934Spendry 
3665934Spendry 	unhead = 0;
3765934Spendry 	unvplock = 0;
3865934Spendry }
3965934Spendry 
4065934Spendry /*
4165934Spendry  * allocate a union_node/vnode pair.  the vnode is
4265965Spendry  * referenced and locked.  the new vnode is returned
4365965Spendry  * via (vpp).  (mp) is the mountpoint of the union filesystem,
4465965Spendry  * (dvp) is the parent directory where the upper layer object
4565965Spendry  * should exist (but doesn't) and (cnp) is the componentname
4665965Spendry  * information which is partially copied to allow the upper
4765965Spendry  * layer object to be created at a later time.  (uppervp)
4865965Spendry  * and (lowervp) reference the upper and lower layer objects
4965965Spendry  * being mapped.  either, but not both, can be nil.
50*65997Spendry  * the reference is either maintained in the new union_node
51*65997Spendry  * object which is allocated, or they are vrele'd.
5265934Spendry  *
5365934Spendry  * all union_nodes are maintained on a singly-linked
5465934Spendry  * list.  new nodes are only allocated when they cannot
5565934Spendry  * be found on this list.  entries on the list are
5665934Spendry  * removed when the vfs reclaim entry is called.
5765934Spendry  *
5865934Spendry  * a single lock is kept for the entire list.  this is
5965934Spendry  * needed because the getnewvnode() function can block
6065934Spendry  * waiting for a vnode to become free, in which case there
6165934Spendry  * may be more than one process trying to get the same
6265934Spendry  * vnode.  this lock is only taken if we are going to
6365934Spendry  * call getnewvnode, since the kernel itself is single-threaded.
6465934Spendry  *
6565934Spendry  * if an entry is found on the list, then call vget() to
6665934Spendry  * take a reference.  this is done because there may be
6765934Spendry  * zero references to it and so it needs to removed from
6865934Spendry  * the vnode free list.
6965934Spendry  */
7065934Spendry int
7165992Spendry union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
7265934Spendry 	struct vnode **vpp;
7365934Spendry 	struct mount *mp;
7465992Spendry 	struct vnode *undvp;
7565934Spendry 	struct vnode *dvp;		/* may be null */
7665934Spendry 	struct componentname *cnp;	/* may be null */
7765934Spendry 	struct vnode *uppervp;		/* may be null */
7865934Spendry 	struct vnode *lowervp;		/* may be null */
7965934Spendry {
8065934Spendry 	int error;
8165934Spendry 	struct union_node *un;
8265934Spendry 	struct union_node **pp;
8365965Spendry 	struct vnode *xlowervp = 0;
8465934Spendry 
8565965Spendry 	if (uppervp == 0 && lowervp == 0)
8665965Spendry 		panic("union: unidentifiable allocation");
8765965Spendry 
8865965Spendry 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
8965965Spendry 		xlowervp = lowervp;
9065965Spendry 		lowervp = 0;
9165965Spendry 	}
9265965Spendry 
9365934Spendry loop:
9465934Spendry 	for (un = unhead; un != 0; un = un->un_next) {
9565934Spendry 		if ((un->un_lowervp == lowervp ||
9665934Spendry 		     un->un_lowervp == 0) &&
9765934Spendry 		    (un->un_uppervp == uppervp ||
9865934Spendry 		     un->un_uppervp == 0) &&
9965934Spendry 		    (UNIONTOV(un)->v_mount == mp)) {
10065992Spendry 			if (vget(UNIONTOV(un), 0))
10165934Spendry 				goto loop;
10265992Spendry 			if (UNIONTOV(un) != undvp)
10365992Spendry 				VOP_LOCK(UNIONTOV(un));
10465992Spendry 			if (uppervp != un->un_uppervp) {
10565992Spendry 				if (un->un_uppervp)
10665992Spendry 					vrele(un->un_uppervp);
10765992Spendry 				un->un_uppervp = uppervp;
108*65997Spendry 			} else if (uppervp) {
109*65997Spendry 				vrele(uppervp);
11065992Spendry 			}
11165992Spendry 			if (lowervp != un->un_lowervp) {
11265992Spendry 				if (un->un_lowervp)
11365992Spendry 					vrele(un->un_lowervp);
11465992Spendry 				un->un_lowervp = lowervp;
115*65997Spendry 			} else if (lowervp) {
116*65997Spendry 				vrele(lowervp);
11765992Spendry 			}
11865992Spendry 			*vpp = UNIONTOV(un);
11965934Spendry 			return (0);
12065934Spendry 		}
12165934Spendry 	}
12265934Spendry 
12365934Spendry 	/*
12465934Spendry 	 * otherwise lock the vp list while we call getnewvnode
12565934Spendry 	 * since that can block.
12665934Spendry 	 */
12765934Spendry 	if (unvplock & UN_LOCKED) {
12865934Spendry 		unvplock |= UN_WANT;
12965934Spendry 		sleep((caddr_t) &unvplock, PINOD);
13065934Spendry 		goto loop;
13165934Spendry 	}
13265934Spendry 	unvplock |= UN_LOCKED;
13365934Spendry 
13465934Spendry 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
13565934Spendry 	if (error)
13665934Spendry 		goto out;
13765934Spendry 
13865934Spendry 	MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
13965934Spendry 		M_TEMP, M_WAITOK);
14065934Spendry 
14165965Spendry 	if (uppervp)
14265965Spendry 		(*vpp)->v_type = uppervp->v_type;
14365965Spendry 	else
14465965Spendry 		(*vpp)->v_type = lowervp->v_type;
14565934Spendry 	un = VTOUNION(*vpp);
14665992Spendry 	un->un_vnode = *vpp;
14765934Spendry 	un->un_next = 0;
14865934Spendry 	un->un_uppervp = uppervp;
14965934Spendry 	un->un_lowervp = lowervp;
150*65997Spendry 	un->un_open = 0;
15165934Spendry 	un->un_flags = 0;
15265965Spendry 	if (uppervp == 0 && cnp) {
15365934Spendry 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
15465934Spendry 		bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
15565934Spendry 		un->un_path[cnp->cn_namelen] = '\0';
15665965Spendry 		VREF(dvp);
15765965Spendry 		un->un_dirvp = dvp;
15865934Spendry 	} else {
15965934Spendry 		un->un_path = 0;
16065965Spendry 		un->un_dirvp = 0;
16165934Spendry 	}
16265934Spendry 
16365934Spendry 	/* add to union vnode list */
16465934Spendry 	for (pp = &unhead; *pp; pp = &(*pp)->un_next)
16565934Spendry 		continue;
16665934Spendry 	*pp = un;
16765934Spendry 
16865992Spendry 	un->un_flags |= UN_LOCKED;
16965965Spendry 
17065992Spendry #ifdef DIAGNOSTIC
17165992Spendry 	un->un_pid = curproc->p_pid;
17265992Spendry #endif
17365992Spendry 
17465965Spendry 	if (xlowervp)
17565965Spendry 		vrele(xlowervp);
17665965Spendry 
17765934Spendry out:
17865934Spendry 	unvplock &= ~UN_LOCKED;
17965934Spendry 
18065934Spendry 	if (unvplock & UN_WANT) {
18165934Spendry 		unvplock &= ~UN_WANT;
18265934Spendry 		wakeup((caddr_t) &unvplock);
18365934Spendry 	}
18465934Spendry 
18565934Spendry 	return (error);
18665934Spendry }
18765934Spendry 
18865934Spendry int
18965934Spendry union_freevp(vp)
19065934Spendry 	struct vnode *vp;
19165934Spendry {
19265934Spendry 	struct union_node **unpp;
19365934Spendry 	struct union_node *un = VTOUNION(vp);
19465934Spendry 
19565934Spendry 	for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
19665934Spendry 		if (*unpp == un) {
19765934Spendry 			*unpp = un->un_next;
19865934Spendry 			break;
19965934Spendry 		}
20065934Spendry 	}
20165934Spendry 
20265934Spendry 	FREE(vp->v_data, M_TEMP);
20365934Spendry 	vp->v_data = 0;
20465934Spendry 	return (0);
20565934Spendry }
20665994Spendry 
20765994Spendry /*
20865994Spendry  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
20965994Spendry  * using a sequence of reads and writes.  both (fvp)
21065994Spendry  * and (tvp) are locked on entry and exit.
21165994Spendry  */
21265994Spendry int
21365994Spendry union_copyfile(p, cred, fvp, tvp)
21465994Spendry 	struct proc *p;
21565994Spendry 	struct ucred *cred;
21665994Spendry 	struct vnode *fvp;
21765994Spendry 	struct vnode *tvp;
21865994Spendry {
21965994Spendry 	char *buf;
22065994Spendry 	struct uio uio;
22165994Spendry 	struct iovec iov;
22265994Spendry 	int error = 0;
22365994Spendry 
22465994Spendry 	/*
22565994Spendry 	 * strategy:
22665994Spendry 	 * allocate a buffer of size MAXBSIZE.
22765994Spendry 	 * loop doing reads and writes, keeping track
22865994Spendry 	 * of the current uio offset.
22965994Spendry 	 * give up at the first sign of trouble.
23065994Spendry 	 */
23165994Spendry 
23265994Spendry 	uio.uio_procp = p;
23365994Spendry 	uio.uio_segflg = UIO_SYSSPACE;
23465994Spendry 	uio.uio_offset = 0;
23565994Spendry 
23665994Spendry 	VOP_UNLOCK(fvp);				/* XXX */
23765994Spendry 	LEASE_CHECK(fvp, p, cred, LEASE_READ);
23865994Spendry 	VOP_LOCK(fvp);					/* XXX */
23965994Spendry 	VOP_UNLOCK(tvp);				/* XXX */
24065994Spendry 	LEASE_CHECK(tvp, p, cred, LEASE_WRITE);
24165994Spendry 	VOP_LOCK(tvp);					/* XXX */
24265994Spendry 
24365994Spendry 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
24465994Spendry 
24565994Spendry 	/* ugly loop follows... */
24665994Spendry 	do {
24765994Spendry 		off_t offset = uio.uio_offset;
24865994Spendry 
24965994Spendry 		uio.uio_iov = &iov;
25065994Spendry 		uio.uio_iovcnt = 1;
25165994Spendry 		iov.iov_base = buf;
25265994Spendry 		iov.iov_len = MAXBSIZE;
25365994Spendry 		uio.uio_resid = iov.iov_len;
25465994Spendry 		uio.uio_rw = UIO_READ;
25565994Spendry 		error = VOP_READ(fvp, &uio, 0, cred);
25665994Spendry 
25765994Spendry 		if (error == 0) {
25865994Spendry 			uio.uio_iov = &iov;
25965994Spendry 			uio.uio_iovcnt = 1;
26065994Spendry 			iov.iov_base = buf;
26165994Spendry 			iov.iov_len = MAXBSIZE - uio.uio_resid;
26265994Spendry 			uio.uio_offset = offset;
26365994Spendry 			uio.uio_rw = UIO_WRITE;
26465994Spendry 			uio.uio_resid = iov.iov_len;
26565994Spendry 
26665994Spendry 			if (uio.uio_resid == 0)
26765994Spendry 				break;
26865994Spendry 
26965994Spendry 			do {
27065994Spendry 				error = VOP_WRITE(tvp, &uio, 0, cred);
27165994Spendry 			} while ((uio.uio_resid > 0) && (error == 0));
27265994Spendry 		}
27365994Spendry 
27465994Spendry 	} while (error == 0);
27565994Spendry 
27665994Spendry 	free(buf, M_TEMP);
27765994Spendry 	return (error);
27865994Spendry }
27965994Spendry 
28065994Spendry /*
281*65997Spendry  * Create a shadow directory in the upper layer.
282*65997Spendry  * The new vnode is returned locked.
283*65997Spendry  *
284*65997Spendry  * (um) points to the union mount structure for access to the
285*65997Spendry  * the mounting process's credentials.
286*65997Spendry  * (dvp) is the directory in which to create the shadow directory.
287*65997Spendry  * it is unlocked on entry and exit.
288*65997Spendry  * (cnp) is the componentname to be created.
289*65997Spendry  * (vpp) is the returned newly created shadow directory, which
290*65997Spendry  * is returned locked.
291*65997Spendry  */
292*65997Spendry int
293*65997Spendry union_mkshadow(um, dvp, cnp, vpp)
294*65997Spendry 	struct union_mount *um;
295*65997Spendry 	struct vnode *dvp;
296*65997Spendry 	struct componentname *cnp;
297*65997Spendry 	struct vnode **vpp;
298*65997Spendry {
299*65997Spendry 	int error;
300*65997Spendry 	struct vattr va;
301*65997Spendry 	struct proc *p = cnp->cn_proc;
302*65997Spendry 	struct componentname cn;
303*65997Spendry 
304*65997Spendry 	/*
305*65997Spendry 	 * policy: when creating the shadow directory in the
306*65997Spendry 	 * upper layer, create it owned by the current user,
307*65997Spendry 	 * group from parent directory, and mode 777 modified
308*65997Spendry 	 * by umask (ie mostly identical to the mkdir syscall).
309*65997Spendry 	 * (jsp, kb)
310*65997Spendry 	 * TODO: create the directory owned by the user who
311*65997Spendry 	 * did the mount (um->um_cred).
312*65997Spendry 	 */
313*65997Spendry 
314*65997Spendry 	/*
315*65997Spendry 	 * A new componentname structure must be faked up because
316*65997Spendry 	 * there is no way to know where the upper level cnp came
317*65997Spendry 	 * from or what it is being used for.  This must duplicate
318*65997Spendry 	 * some of the work done by NDINIT, some of the work done
319*65997Spendry 	 * by namei, some of the work done by lookup and some of
320*65997Spendry 	 * the work done by VOP_LOOKUP when given a CREATE flag.
321*65997Spendry 	 * Conclusion: Horrible.
322*65997Spendry 	 *
323*65997Spendry 	 * The pathname buffer will be FREEed by VOP_MKDIR.
324*65997Spendry 	 */
325*65997Spendry 	cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK);
326*65997Spendry 	bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen+1);
327*65997Spendry 
328*65997Spendry 	cn.cn_nameiop = CREATE;
329*65997Spendry 	cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
330*65997Spendry 	cn.cn_proc = cnp->cn_proc;
331*65997Spendry 	cn.cn_cred = cnp->cn_cred;
332*65997Spendry 	cn.cn_nameptr = cn.cn_pnbuf;
333*65997Spendry 	cn.cn_namelen = cnp->cn_namelen;
334*65997Spendry 	cn.cn_hash = cnp->cn_hash;
335*65997Spendry 	cn.cn_consume = cnp->cn_consume;
336*65997Spendry 
337*65997Spendry 	if (error = relookup(dvp, vpp, &cn))
338*65997Spendry 		return (error);
339*65997Spendry 
340*65997Spendry 	if (*vpp) {
341*65997Spendry 		VOP_ABORTOP(dvp, &cn);
342*65997Spendry 		VOP_UNLOCK(dvp);
343*65997Spendry 		vrele(*vpp);
344*65997Spendry 		*vpp = NULLVP;
345*65997Spendry 		return (EEXIST);
346*65997Spendry 	}
347*65997Spendry 
348*65997Spendry 	VATTR_NULL(&va);
349*65997Spendry 	va.va_type = VDIR;
350*65997Spendry 	va.va_mode = UN_DIRMODE &~ p->p_fd->fd_cmask;
351*65997Spendry 
352*65997Spendry 	/* LEASE_CHECK: dvp is locked */
353*65997Spendry 	LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
354*65997Spendry 
355*65997Spendry 	VREF(dvp);
356*65997Spendry 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
357*65997Spendry 	return (error);
358*65997Spendry }
359*65997Spendry 
360*65997Spendry /*
36165994Spendry  * union_vn_create: creates and opens a new shadow file
36265994Spendry  * on the upper union layer.  this function is similar
36365994Spendry  * in spirit to calling vn_open but it avoids calling namei().
36465994Spendry  * the problem with calling namei is that a) it locks too many
36565994Spendry  * things, and b) it doesn't start at the "right" directory,
36665994Spendry  * whereas relookup is told where to start.
36765994Spendry  */
36865994Spendry int
369*65997Spendry union_vn_create(vpp, un, p)
37065994Spendry 	struct vnode **vpp;
37165994Spendry 	struct union_node *un;
37265994Spendry 	struct proc *p;
37365994Spendry {
37465994Spendry 	struct vnode *vp;
37565994Spendry 	struct ucred *cred = p->p_ucred;
37665994Spendry 	struct vattr vat;
37765994Spendry 	struct vattr *vap = &vat;
37865994Spendry 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
37965994Spendry 	int error;
38065994Spendry 	int hash;
381*65997Spendry 	int cmode = UN_FILEMODE &~ p->p_fd->fd_cmask;
38265994Spendry 	char *cp;
38365994Spendry 	struct componentname cn;
38465994Spendry 
38565994Spendry 	*vpp = NULLVP;
38665994Spendry 
38765994Spendry 	cn.cn_namelen = strlen(un->un_path);
38865994Spendry 	cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
38965994Spendry 	bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
39065994Spendry 	cn.cn_nameiop = CREATE;
39165994Spendry 	cn.cn_flags = (LOCKLEAF|LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
39265994Spendry 	cn.cn_proc = p;
39365994Spendry 	cn.cn_cred = p->p_ucred;
39465994Spendry 	cn.cn_nameptr = cn.cn_pnbuf;
39565994Spendry 	for (hash = 0, cp = cn.cn_nameptr; *cp != 0 && *cp != '/'; cp++)
39665994Spendry 		hash += (unsigned char)*cp;
39765994Spendry 	cn.cn_hash = hash;
39865994Spendry 	cn.cn_consume = 0;
39965994Spendry 
40065994Spendry 	if (error = relookup(un->un_dirvp, &vp, &cn))
40165994Spendry 		return (error);
40265994Spendry 	if (vp == NULLVP) {
40365994Spendry 		VATTR_NULL(vap);
40465994Spendry 		vap->va_type = VREG;
40565994Spendry 		vap->va_mode = cmode;
40665994Spendry 		LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
40765994Spendry 		if (error = VOP_CREATE(un->un_dirvp, &vp,
40865994Spendry 		    &cn, vap))
40965994Spendry 			return (error);
41065994Spendry 	} else {
41165994Spendry 		VOP_ABORTOP(un->un_dirvp, &cn);
41265994Spendry 		if (un->un_dirvp == vp)
41365994Spendry 			vrele(un->un_dirvp);
41465994Spendry 		else
41565994Spendry 			vput(vp);
41665994Spendry 		error = EEXIST;
41765994Spendry 		goto bad;
41865994Spendry 	}
41965994Spendry 
42065994Spendry 	if (vp->v_type != VREG) {
42165994Spendry 		error = EOPNOTSUPP;
42265994Spendry 		goto bad;
42365994Spendry 	}
42465994Spendry 
42565994Spendry 	VOP_UNLOCK(vp);				/* XXX */
42665994Spendry 	LEASE_CHECK(vp, p, cred, LEASE_WRITE);
42765994Spendry 	VOP_LOCK(vp);				/* XXX */
42865994Spendry 	VATTR_NULL(vap);
42965994Spendry 	vap->va_size = 0;
43065994Spendry 	if (error = VOP_SETATTR(vp, vap, cred, p))
43165994Spendry 		goto bad;
43265994Spendry 
43365994Spendry 	if (error = VOP_OPEN(vp, fmode, cred, p))
43465994Spendry 		goto bad;
43565994Spendry 
43665994Spendry 	vp->v_writecount++;
43765994Spendry 	*vpp = vp;
43865994Spendry 	return (0);
43965994Spendry bad:
44065994Spendry 	vput(vp);
44165994Spendry 	return (error);
44265994Spendry }
443