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