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