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