Merge branch 'vendor/TCPDUMP'
[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  */
40
41 /*
42  * Union Layer
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/nlookup.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 extern struct vop_ops union_vnode_vops;
59
60 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
61
62 extern int      union_init (struct vfsconf *);
63 static int      union_mount (struct mount *mp, char *path, caddr_t data,
64                                  struct ucred *cred);
65 static int      union_root (struct mount *mp, struct vnode **vpp);
66 static int      union_statfs (struct mount *mp, struct statfs *sbp,
67                                 struct ucred *cred);
68 static int      union_unmount (struct mount *mp, int mntflags);
69
70 /*
71  * Mount union filesystem
72  */
73 static int
74 union_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
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 = NULL;
81         struct ucred *cred = NULL;
82         struct nlookupdata nd;
83         char *cp = NULL;
84         int len;
85         u_int size;
86
87         UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
88
89         /*
90          * Disable clustered write, otherwise system becomes unstable.
91          */
92         mp->mnt_flag |= MNT_NOCLUSTERW;
93
94         /*
95          * Update is a no-op
96          */
97         if (mp->mnt_flag & MNT_UPDATE) {
98                 /*
99                  * Need to provide.
100                  * 1. a way to convert between rdonly and rdwr mounts.
101                  * 2. support for nfs exports.
102                  */
103                 error = EOPNOTSUPP;
104                 goto bad;
105         }
106
107         /*
108          * Get argument
109          */
110         error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
111         if (error)
112                 goto bad;
113
114         /*
115          * Obtain lower vnode. 
116          */
117
118         lowerrootvp = mp->mnt_vnodecovered;
119         vref(lowerrootvp);
120
121 #if 0
122         /*
123          * Unlock lower node to avoid deadlock.
124          */
125         if (lowerrootvp->v_tag == VT_UNION)
126                 vn_unlock(lowerrootvp);
127 #endif
128
129         /*
130          * Obtain upper vnode by calling nlookup() on the path.  The
131          * upperrootvp will be turned referenced but not locked.
132          */
133         error = nlookup_init(&nd, args.target, UIO_USERSPACE, NLC_FOLLOW);
134         if (error == 0)
135                 error = nlookup(&nd);
136         if (error == 0)
137                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &upperrootvp);
138         nlookup_done(&nd);
139         if (error)
140                 goto bad;
141
142         UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp,
143             vn_islocked(upperrootvp)));
144
145         /*
146          * Check multi union mount to avoid `lock myself again' panic.
147          * Also require that it be a directory.
148          */
149         if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
150 #ifdef DIAGNOSTIC
151                 kprintf("union_mount: multi union mount?\n");
152 #endif
153                 error = EDEADLK;
154                 goto bad;
155         }
156
157         if (upperrootvp->v_type != VDIR) {
158                 error = EINVAL;
159                 goto bad;
160         }
161
162         /*
163          * Allocate our union_mount structure and populate the fields.
164          * The vnode references are stored in the union_mount as held,
165          * unlocked references.  Depending on the _BELOW flag, the
166          * filesystems are viewed in a different order.  In effect this
167          * is the same as providing a mount-under option to the mount
168          * syscall.
169          */
170
171         um = (struct union_mount *) malloc(sizeof(struct union_mount),
172                                 M_UNIONFSMNT, M_WAITOK);
173
174         bzero(um, sizeof(struct union_mount));
175
176         um->um_op = args.mntflags & UNMNT_OPMASK;
177
178         switch (um->um_op) {
179         case UNMNT_ABOVE:
180                 um->um_lowervp = lowerrootvp;
181                 um->um_uppervp = upperrootvp;
182                 upperrootvp = NULL;
183                 lowerrootvp = NULL;
184                 break;
185
186         case UNMNT_BELOW:
187                 um->um_lowervp = upperrootvp;
188                 um->um_uppervp = lowerrootvp;
189                 upperrootvp = NULL;
190                 lowerrootvp = NULL;
191                 break;
192
193         case UNMNT_REPLACE:
194                 vrele(lowerrootvp);
195                 lowerrootvp = NULL;
196                 um->um_uppervp = upperrootvp;
197                 um->um_lowervp = lowerrootvp;
198                 upperrootvp = NULL;
199                 break;
200
201         default:
202                 error = EINVAL;
203                 goto bad;
204         }
205
206         /*
207          * Unless the mount is readonly, ensure that the top layer
208          * supports whiteout operations
209          */
210         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
211                 error = VOP_WHITEOUT(um->um_uppervp, NULL, NAMEI_LOOKUP);
212                 if (error)
213                         goto bad;
214         }
215
216         /*
217          * File creds and modes for shadowed files are based on the user
218          * that did the mount.
219          */
220         um->um_cred = crhold(cred);
221         um->um_cmode = UN_DIRMODE;
222         if (curproc)
223                 um->um_cmode &= ~curproc->p_fd->fd_cmask;
224
225         /*
226          * Depending on what you think the MNT_LOCAL flag might mean,
227          * you may want the && to be || on the conditional below.
228          * At the moment it has been defined that the filesystem is
229          * only local if it is all local, ie the MNT_LOCAL flag implies
230          * that the entire namespace is local.  If you think the MNT_LOCAL
231          * flag implies that some of the files might be stored locally
232          * then you will want to change the conditional.
233          */
234         if (um->um_op == UNMNT_ABOVE) {
235                 if (((um->um_lowervp == NULLVP) ||
236                      (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
237                     (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
238                         mp->mnt_flag |= MNT_LOCAL;
239         }
240
241         /*
242          * Copy in the upper layer's RDONLY flag.  This is for the benefit
243          * of lookup() which explicitly checks the flag, rather than asking
244          * the filesystem for its own opinion.  This means, that an update
245          * mount of the underlying filesystem to go from rdonly to rdwr
246          * will leave the unioned view as read-only.
247          */
248         mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
249
250         mp->mnt_data = (qaddr_t) um;
251         vfs_getnewfsid(mp);
252
253         switch (um->um_op) {
254         case UNMNT_ABOVE:
255                 cp = "<above>:";
256                 break;
257         case UNMNT_BELOW:
258                 cp = "<below>:";
259                 break;
260         case UNMNT_REPLACE:
261                 cp = "";
262                 break;
263         }
264         len = strlen(cp);
265         bcopy(cp, mp->mnt_stat.f_mntfromname, len);
266
267         cp = mp->mnt_stat.f_mntfromname + len;
268         len = MNAMELEN - len;
269
270         (void) copyinstr(args.target, cp, len - 1, &size);
271         bzero(cp + size, len - size);
272
273         vfs_add_vnodeops(mp, &union_vnode_vops, &mp->mnt_vn_norm_ops);
274
275         (void)union_statfs(mp, &mp->mnt_stat, cred);
276
277         return (0);
278
279 bad:
280         if (um) {
281                 if (um->um_uppervp)
282                         vrele(um->um_uppervp);
283                 if (um->um_lowervp)
284                         vrele(um->um_lowervp);
285                 /* XXX other fields */
286                 kfree(um, M_UNIONFSMNT);
287         }
288         if (cred)
289                 crfree(cred);
290         if (upperrootvp)
291                 vrele(upperrootvp);
292         if (lowerrootvp)
293                 vrele(lowerrootvp);
294         return (error);
295 }
296
297 /*
298  * Free reference to union layer
299  */
300 static int
301 union_unmount(struct mount *mp, int mntflags)
302 {
303         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
304         int error;
305         int freeing;
306         int flags = 0;
307
308         UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
309
310         if (mntflags & MNT_FORCE)
311                 flags |= FORCECLOSE;
312
313         /*
314          * Keep flushing vnodes from the mount list.
315          * This is needed because of the un_pvp held
316          * reference to the parent vnode.
317          * If more vnodes have been freed on a given pass,
318          * the try again.  The loop will iterate at most
319          * (d) times, where (d) is the maximum tree depth
320          * in the filesystem.
321          */
322         for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) {
323                 int n = mp->mnt_nvnodelistsize;
324
325                 /* if this is unchanged then stop */
326                 if (n == freeing)
327                         break;
328
329                 /* otherwise try once more time */
330                 freeing = n;
331         }
332
333         /* If the most recent vflush failed, the filesystem is still busy. */
334         if (error)
335                 return (error);
336
337         /*
338          * Discard references to upper and lower target vnodes.
339          */
340         if (um->um_lowervp)
341                 vrele(um->um_lowervp);
342         vrele(um->um_uppervp);
343         crfree(um->um_cred);
344         /*
345          * Finally, throw away the union_mount structure
346          */
347         kfree(mp->mnt_data, M_UNIONFSMNT);      /* XXX */
348         mp->mnt_data = 0;
349         return (0);
350 }
351
352 static int
353 union_root(struct mount *mp, struct vnode **vpp)
354 {
355         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
356         int error;
357
358         /*
359          * Supply an unlocked reference to um_uppervp and to um_lowervp.  It
360          * is possible for um_uppervp to be locked without the associated
361          * root union_node being locked.  We let union_allocvp() deal with
362          * it.
363          */
364         UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp,
365             vn_islocked(um->um_uppervp)));
366
367         vref(um->um_uppervp);
368         if (um->um_lowervp)
369                 vref(um->um_lowervp);
370
371         error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, 
372                     um->um_uppervp, um->um_lowervp, 1);
373         UDEBUG(("error %d\n", error));
374         UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp,
375             vn_islocked(um->um_uppervp)));
376
377         return (error);
378 }
379
380 static int
381 union_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
382 {
383         int error;
384         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
385         struct statfs mstat;
386         int lbsize;
387
388         UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n",
389             (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp));
390
391         bzero(&mstat, sizeof(mstat));
392
393         if (um->um_lowervp) {
394                 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, cred);
395                 if (error)
396                         return (error);
397         }
398
399         /* now copy across the "interesting" information and fake the rest */
400 #if 0
401         sbp->f_type = mstat.f_type;
402         sbp->f_flags = mstat.f_flags;
403         sbp->f_bsize = mstat.f_bsize;
404         sbp->f_iosize = mstat.f_iosize;
405 #endif
406         lbsize = mstat.f_bsize;
407         sbp->f_blocks = mstat.f_blocks;
408         sbp->f_bfree = mstat.f_bfree;
409         sbp->f_bavail = mstat.f_bavail;
410         sbp->f_files = mstat.f_files;
411         sbp->f_ffree = mstat.f_ffree;
412
413         error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, cred);
414         if (error)
415                 return (error);
416
417         sbp->f_flags = mstat.f_flags;
418         sbp->f_bsize = mstat.f_bsize;
419         sbp->f_iosize = mstat.f_iosize;
420
421         /*
422          * if the lower and upper blocksizes differ, then frig the
423          * block counts so that the sizes reported by df make some
424          * kind of sense.  none of this makes sense though.
425          */
426
427         if (mstat.f_bsize != lbsize)
428                 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
429
430         /*
431          * The "total" fields count total resources in all layers,
432          * the "free" fields count only those resources which are
433          * free in the upper layer (since only the upper layer
434          * is writeable).
435          */
436         sbp->f_blocks += mstat.f_blocks;
437         sbp->f_bfree = mstat.f_bfree;
438         sbp->f_bavail = mstat.f_bavail;
439         sbp->f_files += mstat.f_files;
440         sbp->f_ffree = mstat.f_ffree;
441
442         if (sbp != &mp->mnt_stat) {
443                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
444                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
445                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
446         }
447         return (0);
448 }
449
450 static struct vfsops union_vfsops = {
451         .vfs_mount =            union_mount,
452         .vfs_unmount =          union_unmount,
453         .vfs_root =             union_root,
454         .vfs_statfs =           union_statfs,
455         .vfs_sync =             vfs_stdsync,
456         .vfs_init =             union_init
457 };
458
459 VFS_SET(union_vfsops, union, VFCF_LOOPBACK);