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