Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / vfs / union / union_vfsops.c
1 /*
2  * Copyright (c) 1994, 1995 The Regents of the University of California.
3  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)union_vfsops.c      8.20 (Berkeley) 5/20/95
38  * $FreeBSD: src/sys/miscfs/union/union_vfsops.c,v 1.39.2.2 2001/10/25 19:18:53 dillon Exp $
39  * $DragonFly: src/sys/vfs/union/union_vfsops.c,v 1.12 2004/05/20 05:09:18 cpressey Exp $
40  */
41
42 /*
43  * Union Layer
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/proc.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
54 #include <sys/filedesc.h>
55 #include "union.h"
56 #include <vm/vm_zone.h>
57
58 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
59
60 extern int      union_init (struct vfsconf *);
61 static int      union_mount (struct mount *mp, char *path, caddr_t data,
62                                  struct nameidata *ndp, struct thread *td);
63 static int      union_root (struct mount *mp, struct vnode **vpp);
64 static int      union_statfs (struct mount *mp, struct statfs *sbp,
65                                   struct thread *td);
66 static int      union_unmount (struct mount *mp, int mntflags,
67                                    struct thread *td);
68
69 /*
70  * Mount union filesystem
71  */
72 static int
73 union_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
74             struct thread *td)
75 {
76         int error = 0;
77         struct union_args args;
78         struct vnode *lowerrootvp = NULLVP;
79         struct vnode *upperrootvp = NULLVP;
80         struct union_mount *um = 0;
81         struct ucred *cred = 0;
82         char *cp = 0;
83         int len;
84         u_int size;
85
86         UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
87
88         KKASSERT(td->td_proc);
89
90         /*
91          * Disable clustered write, otherwise system becomes unstable.
92          */
93         mp->mnt_flag |= MNT_NOCLUSTERW;
94
95         /*
96          * Update is a no-op
97          */
98         if (mp->mnt_flag & MNT_UPDATE) {
99                 /*
100                  * Need to provide.
101                  * 1. a way to convert between rdonly and rdwr mounts.
102                  * 2. support for nfs exports.
103                  */
104                 error = EOPNOTSUPP;
105                 goto bad;
106         }
107
108         /*
109          * Get argument
110          */
111         error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
112         if (error)
113                 goto bad;
114
115         /*
116          * Obtain lower vnode.  Vnode is stored in mp->mnt_vnodecovered.
117          * We need to reference it but not lock it.
118          */
119
120         lowerrootvp = mp->mnt_vnodecovered;
121         vref(lowerrootvp);
122
123 #if 0
124         /*
125          * Unlock lower node to avoid deadlock.
126          */
127         if (lowerrootvp->v_op == union_vnodeop_p)
128                 VOP_UNLOCK(lowerrootvp, NULL, 0, td);
129 #endif
130
131         /*
132          * Obtain upper vnode by calling namei() on the path.  The
133          * upperrootvp will be turned referenced but not locked.
134          */
135         NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW | CNP_WANTPARENT,
136                 UIO_USERSPACE, args.target, td);
137
138         error = namei(ndp);
139
140 #if 0
141         if (lowerrootvp->v_op == union_vnodeop_p)
142                 vn_lock(lowerrootvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
143 #endif
144         if (error)
145                 goto bad;
146
147         NDFREE(ndp, NDF_ONLY_PNBUF);
148         upperrootvp = ndp->ni_vp;
149         vrele(ndp->ni_dvp);
150         ndp->ni_dvp = NULL;
151
152         UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp,
153             VOP_ISLOCKED(upperrootvp, NULL)));
154
155         /*
156          * Check multi union mount to avoid `lock myself again' panic.
157          * Also require that it be a directory.
158          */
159         if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
160 #ifdef DIAGNOSTIC
161                 printf("union_mount: multi union mount?\n");
162 #endif
163                 error = EDEADLK;
164                 goto bad;
165         }
166
167         if (upperrootvp->v_type != VDIR) {
168                 error = EINVAL;
169                 goto bad;
170         }
171
172         /*
173          * Allocate our union_mount structure and populate the fields.
174          * The vnode references are stored in the union_mount as held,
175          * unlocked references.  Depending on the _BELOW flag, the
176          * filesystems are viewed in a different order.  In effect this
177          * is the same as providing a mount-under option to the mount
178          * syscall.
179          */
180
181         um = (struct union_mount *) malloc(sizeof(struct union_mount),
182                                 M_UNIONFSMNT, M_WAITOK);
183
184         bzero(um, sizeof(struct union_mount));
185
186         um->um_op = args.mntflags & UNMNT_OPMASK;
187
188         switch (um->um_op) {
189         case UNMNT_ABOVE:
190                 um->um_lowervp = lowerrootvp;
191                 um->um_uppervp = upperrootvp;
192                 upperrootvp = NULL;
193                 lowerrootvp = NULL;
194                 break;
195
196         case UNMNT_BELOW:
197                 um->um_lowervp = upperrootvp;
198                 um->um_uppervp = lowerrootvp;
199                 upperrootvp = NULL;
200                 lowerrootvp = NULL;
201                 break;
202
203         case UNMNT_REPLACE:
204                 vrele(lowerrootvp);
205                 lowerrootvp = NULL;
206                 um->um_uppervp = upperrootvp;
207                 um->um_lowervp = lowerrootvp;
208                 upperrootvp = NULL;
209                 break;
210
211         default:
212                 error = EINVAL;
213                 goto bad;
214         }
215
216         /*
217          * Unless the mount is readonly, ensure that the top layer
218          * supports whiteout operations
219          */
220         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
221                 error = VOP_WHITEOUT(um->um_uppervp, NCPNULL, NULL, NAMEI_LOOKUP);
222                 if (error)
223                         goto bad;
224         }
225
226         um->um_cred = crhold(td->td_proc->p_ucred);
227         um->um_cmode = UN_DIRMODE & ~td->td_proc->p_fd->fd_cmask;
228
229         /*
230          * Depending on what you think the MNT_LOCAL flag might mean,
231          * you may want the && to be || on the conditional below.
232          * At the moment it has been defined that the filesystem is
233          * only local if it is all local, ie the MNT_LOCAL flag implies
234          * that the entire namespace is local.  If you think the MNT_LOCAL
235          * flag implies that some of the files might be stored locally
236          * then you will want to change the conditional.
237          */
238         if (um->um_op == UNMNT_ABOVE) {
239                 if (((um->um_lowervp == NULLVP) ||
240                      (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
241                     (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
242                         mp->mnt_flag |= MNT_LOCAL;
243         }
244
245         /*
246          * Copy in the upper layer's RDONLY flag.  This is for the benefit
247          * of lookup() which explicitly checks the flag, rather than asking
248          * the filesystem for its own opinion.  This means, that an update
249          * mount of the underlying filesystem to go from rdonly to rdwr
250          * will leave the unioned view as read-only.
251          */
252         mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
253
254         mp->mnt_data = (qaddr_t) um;
255         vfs_getnewfsid(mp);
256
257         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
258         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
259
260         switch (um->um_op) {
261         case UNMNT_ABOVE:
262                 cp = "<above>:";
263                 break;
264         case UNMNT_BELOW:
265                 cp = "<below>:";
266                 break;
267         case UNMNT_REPLACE:
268                 cp = "";
269                 break;
270         }
271         len = strlen(cp);
272         bcopy(cp, mp->mnt_stat.f_mntfromname, len);
273
274         cp = mp->mnt_stat.f_mntfromname + len;
275         len = MNAMELEN - len;
276
277         (void) copyinstr(args.target, cp, len - 1, &size);
278         bzero(cp + size, len - size);
279
280         (void)union_statfs(mp, &mp->mnt_stat, td);
281
282         UDEBUG(("union_mount: from %s, on %s\n",
283                 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname));
284         return (0);
285
286 bad:
287         if (um) {
288                 if (um->um_uppervp)
289                         vrele(um->um_uppervp);
290                 if (um->um_lowervp)
291                         vrele(um->um_lowervp);
292                 /* XXX other fields */
293                 free(um, M_UNIONFSMNT);
294         }
295         if (cred)
296                 crfree(cred);
297         if (upperrootvp)
298                 vrele(upperrootvp);
299         if (lowerrootvp)
300                 vrele(lowerrootvp);
301         return (error);
302 }
303
304 /*
305  * Free reference to union layer
306  */
307 static int
308 union_unmount(struct mount *mp, int mntflags, struct thread *td)
309 {
310         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
311         int error;
312         int freeing;
313         int flags = 0;
314
315         UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
316
317         if (mntflags & MNT_FORCE)
318                 flags |= FORCECLOSE;
319
320         /*
321          * Keep flushing vnodes from the mount list.
322          * This is needed because of the un_pvp held
323          * reference to the parent vnode.
324          * If more vnodes have been freed on a given pass,
325          * the try again.  The loop will iterate at most
326          * (d) times, where (d) is the maximum tree depth
327          * in the filesystem.
328          */
329         for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) {
330                 int n = mp->mnt_nvnodelistsize;
331
332                 /* if this is unchanged then stop */
333                 if (n == freeing)
334                         break;
335
336                 /* otherwise try once more time */
337                 freeing = n;
338         }
339
340         /* If the most recent vflush failed, the filesystem is still busy. */
341         if (error)
342                 return (error);
343
344         /*
345          * Discard references to upper and lower target vnodes.
346          */
347         if (um->um_lowervp)
348                 vrele(um->um_lowervp);
349         vrele(um->um_uppervp);
350         crfree(um->um_cred);
351         /*
352          * Finally, throw away the union_mount structure
353          */
354         free(mp->mnt_data, M_UNIONFSMNT);       /* XXX */
355         mp->mnt_data = 0;
356         return (0);
357 }
358
359 static int
360 union_root(struct mount *mp, struct vnode **vpp)
361 {
362         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
363         int error;
364
365         /*
366          * Supply an unlocked reference to um_uppervp and to um_lowervp.  It
367          * is possible for um_uppervp to be locked without the associated
368          * root union_node being locked.  We let union_allocvp() deal with
369          * it.
370          */
371         UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp,
372             VOP_ISLOCKED(um->um_uppervp, NULL)));
373
374         vref(um->um_uppervp);
375         if (um->um_lowervp)
376                 vref(um->um_lowervp);
377
378         error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, 
379                     um->um_uppervp, um->um_lowervp, 1);
380         UDEBUG(("error %d\n", error));
381         UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp,
382             VOP_ISLOCKED(um->um_uppervp, NULL)));
383
384         return (error);
385 }
386
387 static int
388 union_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
389 {
390         int error;
391         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
392         struct statfs mstat;
393         int lbsize;
394
395         UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n",
396             (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp));
397
398         bzero(&mstat, sizeof(mstat));
399
400         if (um->um_lowervp) {
401                 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td);
402                 if (error)
403                         return (error);
404         }
405
406         /* now copy across the "interesting" information and fake the rest */
407 #if 0
408         sbp->f_type = mstat.f_type;
409         sbp->f_flags = mstat.f_flags;
410         sbp->f_bsize = mstat.f_bsize;
411         sbp->f_iosize = mstat.f_iosize;
412 #endif
413         lbsize = mstat.f_bsize;
414         sbp->f_blocks = mstat.f_blocks;
415         sbp->f_bfree = mstat.f_bfree;
416         sbp->f_bavail = mstat.f_bavail;
417         sbp->f_files = mstat.f_files;
418         sbp->f_ffree = mstat.f_ffree;
419
420         error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td);
421         if (error)
422                 return (error);
423
424         sbp->f_flags = mstat.f_flags;
425         sbp->f_bsize = mstat.f_bsize;
426         sbp->f_iosize = mstat.f_iosize;
427
428         /*
429          * if the lower and upper blocksizes differ, then frig the
430          * block counts so that the sizes reported by df make some
431          * kind of sense.  none of this makes sense though.
432          */
433
434         if (mstat.f_bsize != lbsize)
435                 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
436
437         /*
438          * The "total" fields count total resources in all layers,
439          * the "free" fields count only those resources which are
440          * free in the upper layer (since only the upper layer
441          * is writeable).
442          */
443         sbp->f_blocks += mstat.f_blocks;
444         sbp->f_bfree = mstat.f_bfree;
445         sbp->f_bavail = mstat.f_bavail;
446         sbp->f_files += mstat.f_files;
447         sbp->f_ffree = mstat.f_ffree;
448
449         if (sbp != &mp->mnt_stat) {
450                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
451                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
452                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
453                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
454         }
455         return (0);
456 }
457
458 static struct vfsops union_vfsops = {
459         union_mount,
460         vfs_stdstart,   /* underlying start already done */
461         union_unmount,
462         union_root,
463         vfs_stdquotactl,
464         union_statfs,
465         vfs_stdsync,    /* XXX assumes no cached data on union level */
466         vfs_stdvget,
467         vfs_stdfhtovp,
468         vfs_stdcheckexp,
469         vfs_stdvptofh,
470         union_init,
471         vfs_stduninit,
472         vfs_stdextattrctl,
473 };
474
475 VFS_SET(union_vfsops, union, VFCF_LOOPBACK);