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