]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/unionfs/union_vfsops.c
The intent is to get rid of WILLRELE in vnode_if.src by making
[FreeBSD/FreeBSD.git] / sys / fs / unionfs / 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  * $Id: union_vfsops.c,v 1.26 1998/02/10 08:04:31 kato Exp $
39  */
40
41 /*
42  * Union Layer
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/filedesc.h>
54 #include <miscfs/union/union.h>
55
56 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
57
58 extern int      union_init __P((struct vfsconf *));
59
60 extern int      union_fhtovp __P((struct mount *mp, struct fid *fidp,
61                                   struct mbuf *nam, struct vnode **vpp,
62                                   int *exflagsp, struct ucred **credanonp));
63 static int      union_mount __P((struct mount *mp, char *path, caddr_t data,
64                                  struct nameidata *ndp, struct proc *p));
65 extern int      union_quotactl __P((struct mount *mp, int cmd, uid_t uid,
66                                     caddr_t arg, struct proc *p));
67 static int      union_root __P((struct mount *mp, struct vnode **vpp));
68 static int      union_start __P((struct mount *mp, int flags, struct proc *p));
69 static int      union_statfs __P((struct mount *mp, struct statfs *sbp,
70                                   struct proc *p));
71 extern int      union_sync __P((struct mount *mp, int waitfor,
72                                 struct ucred *cred, struct proc *p));
73 static int      union_unmount __P((struct mount *mp, int mntflags,
74                                    struct proc *p));
75 extern int      union_vget __P((struct mount *mp, ino_t ino,
76                                 struct vnode **vpp));
77 static int      union_vrele __P((struct mount *mp, struct vnode *vp));
78 extern int      union_vptofh __P((struct vnode *vp, struct fid *fhp));
79
80 /*
81  * Mount union filesystem
82  */
83 static int
84 union_mount(mp, path, data, ndp, p)
85         struct mount *mp;
86         char *path;
87         caddr_t data;
88         struct nameidata *ndp;
89         struct proc *p;
90 {
91         int error = 0;
92         struct union_args args;
93         struct vnode *lowerrootvp = NULLVP;
94         struct vnode *upperrootvp = NULLVP;
95         struct union_mount *um = 0;
96         struct ucred *cred = 0;
97         char *cp = 0;
98         int len;
99         u_int size;
100
101 #ifdef UNION_DIAGNOSTIC
102         printf("union_mount(mp = %x)\n", mp);
103 #endif
104
105         /*
106          * Disable clustered write, otherwise system becomes unstable.
107          */
108         mp->mnt_flag |= MNT_NOCLUSTERW;
109
110         /*
111          * Update is a no-op
112          */
113         if (mp->mnt_flag & MNT_UPDATE) {
114                 /*
115                  * Need to provide.
116                  * 1. a way to convert between rdonly and rdwr mounts.
117                  * 2. support for nfs exports.
118                  */
119                 error = EOPNOTSUPP;
120                 goto bad;
121         }
122
123         /*
124          * Get argument
125          */
126         error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
127         if (error)
128                 goto bad;
129
130         lowerrootvp = mp->mnt_vnodecovered;
131         VREF(lowerrootvp);
132
133         /*
134          * Unlock lower node to avoid deadlock.
135          */
136         if (lowerrootvp->v_op == union_vnodeop_p)
137                 VOP_UNLOCK(lowerrootvp, 0, p);
138
139         /*
140          * Find upper node.
141          */
142         NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
143                UIO_USERSPACE, args.target, p);
144
145         error = namei(ndp);
146         if (lowerrootvp->v_op == union_vnodeop_p)
147                 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, p);
148         if (error)
149                 goto bad;
150
151         upperrootvp = ndp->ni_vp;
152         vrele(ndp->ni_dvp);
153         ndp->ni_dvp = NULL;
154
155         /*
156          * Check multi union mount to avoid `lock myself again' panic.
157          */
158         if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
159 #ifdef DIAGNOSTIC
160                 printf("union_mount: multi union mount?\n");
161 #endif
162                 error = EDEADLK;
163                 goto bad;
164         }
165
166         if (upperrootvp->v_type != VDIR) {
167                 error = EINVAL;
168                 goto bad;
169         }
170
171         um = (struct union_mount *) malloc(sizeof(struct union_mount),
172                                 M_UNIONFSMNT, M_WAITOK);        /* XXX */
173
174         /*
175          * Keep a held reference to the target vnodes.
176          * They are vrele'd in union_unmount.
177          *
178          * Depending on the _BELOW flag, the filesystems are
179          * viewed in a different order.  In effect, this is the
180          * same as providing a mount under option to the mount syscall.
181          */
182
183         um->um_op = args.mntflags & UNMNT_OPMASK;
184         switch (um->um_op) {
185         case UNMNT_ABOVE:
186                 um->um_lowervp = lowerrootvp;
187                 um->um_uppervp = upperrootvp;
188                 break;
189
190         case UNMNT_BELOW:
191                 um->um_lowervp = upperrootvp;
192                 um->um_uppervp = lowerrootvp;
193                 break;
194
195         case UNMNT_REPLACE:
196                 vrele(lowerrootvp);
197                 lowerrootvp = NULLVP;
198                 um->um_uppervp = upperrootvp;
199                 um->um_lowervp = lowerrootvp;
200                 break;
201
202         default:
203                 error = EINVAL;
204                 goto bad;
205         }
206
207         /*
208          * Unless the mount is readonly, ensure that the top layer
209          * supports whiteout operations
210          */
211         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
212                 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
213                 if (error)
214                         goto bad;
215         }
216
217         um->um_cred = p->p_ucred;
218         crhold(um->um_cred);
219         um->um_cmode = UN_DIRMODE &~ p->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 it's 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         (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
250         bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
251
252         switch (um->um_op) {
253         case UNMNT_ABOVE:
254                 cp = "<above>:";
255                 break;
256         case UNMNT_BELOW:
257                 cp = "<below>:";
258                 break;
259         case UNMNT_REPLACE:
260                 cp = "";
261                 break;
262         }
263         len = strlen(cp);
264         bcopy(cp, mp->mnt_stat.f_mntfromname, len);
265
266         cp = mp->mnt_stat.f_mntfromname + len;
267         len = MNAMELEN - len;
268
269         (void) copyinstr(args.target, cp, len - 1, &size);
270         bzero(cp + size, len - size);
271
272         (void)union_statfs(mp, &mp->mnt_stat, p);
273
274 #ifdef UNION_DIAGNOSTIC
275         printf("union_mount: from %s, on %s\n",
276                 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
277 #endif
278         return (0);
279
280 bad:
281         if (um)
282                 free(um, M_UNIONFSMNT);
283         if (cred)
284                 crfree(cred);
285         if (upperrootvp)
286                 vrele(upperrootvp);
287         if (lowerrootvp)
288                 vrele(lowerrootvp);
289         return (error);
290 }
291
292 /*
293  * VFS start.  Nothing needed here - the start routine
294  * on the underlying filesystem(s) will have been called
295  * when that filesystem was mounted.
296  */
297 static int
298 union_start(mp, flags, p)
299         struct mount *mp;
300         int flags;
301         struct proc *p;
302 {
303
304         return (0);
305 }
306
307 /*
308  * Free reference to union layer
309  */
310 static int
311 union_unmount(mp, mntflags, p)
312         struct mount *mp;
313         int mntflags;
314         struct proc *p;
315 {
316         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
317         struct vnode *um_rootvp;
318         int error;
319         int freeing;
320         int flags = 0;
321
322 #ifdef UNION_DIAGNOSTIC
323         printf("union_unmount(mp = %x)\n", mp);
324 #endif
325
326         if (mntflags & MNT_FORCE)
327                 flags |= FORCECLOSE;
328
329         if (error = union_root(mp, &um_rootvp))
330                 return (error);
331
332         /*
333          * Keep flushing vnodes from the mount list.
334          * This is needed because of the un_pvp held
335          * reference to the parent vnode.
336          * If more vnodes have been freed on a given pass,
337          * the try again.  The loop will iterate at most
338          * (d) times, where (d) is the maximum tree depth
339          * in the filesystem.
340          */
341         for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) {
342                 struct vnode *vp;
343                 int n;
344
345                 /* count #vnodes held on mount list */
346                 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
347                                 vp != NULLVP;
348                                 vp = vp->v_mntvnodes.le_next)
349                         n++;
350
351                 /* if this is unchanged then stop */
352                 if (n == freeing)
353                         break;
354
355                 /* otherwise try once more time */
356                 freeing = n;
357         }
358
359         /* At this point the root vnode should have a single reference */
360         if (um_rootvp->v_usecount > 1) {
361                 vput(um_rootvp);
362                 return (EBUSY);
363         }
364
365 #ifdef UNION_DIAGNOSTIC
366         vprint("union root", um_rootvp);
367 #endif   
368         /*
369          * Discard references to upper and lower target vnodes.
370          */
371         if (um->um_lowervp)
372                 vrele(um->um_lowervp);
373         vrele(um->um_uppervp);
374         crfree(um->um_cred);
375         /*
376          * Release reference on underlying root vnode
377          */
378         vput(um_rootvp);
379         /*
380          * And blow it away for future re-use
381          */
382         vgone(um_rootvp);
383         /*
384          * Finally, throw away the union_mount structure
385          */
386         free(mp->mnt_data, M_UNIONFSMNT);       /* XXX */
387         mp->mnt_data = 0;
388         return (0);
389 }
390
391 static int
392 union_root(mp, vpp)
393         struct mount *mp;
394         struct vnode **vpp;
395 {
396         struct proc *p = curproc;       /* XXX */
397         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
398         int error;
399         int loselock;
400         int lockadj = 0;
401
402         if (um->um_lowervp && um->um_op != UNMNT_BELOW &&
403                 VOP_ISLOCKED(um->um_lowervp)) {
404                 VREF(um->um_lowervp);
405                 VOP_UNLOCK(um->um_lowervp, 0, p);
406                 lockadj = 1;
407         }
408
409         /*
410          * Return locked reference to root.
411          */
412         VREF(um->um_uppervp);
413         if ((um->um_op == UNMNT_BELOW) &&
414             VOP_ISLOCKED(um->um_uppervp)) {
415                 loselock = 1;
416         } else {
417                 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
418                 loselock = 0;
419         }
420         if (um->um_lowervp)
421                 VREF(um->um_lowervp);
422         error = union_allocvp(vpp, mp,
423                               (struct vnode *) 0,
424                               (struct vnode *) 0,
425                               (struct componentname *) 0,
426                               um->um_uppervp,
427                               um->um_lowervp,
428                               1);
429
430         if (error) {
431                 if (loselock)
432                         vrele(um->um_uppervp);
433                 else
434                         vput(um->um_uppervp);
435                 if (um->um_lowervp)
436                         vrele(um->um_lowervp);
437         } else {
438                 if (loselock)
439                         VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
440         }
441         if (lockadj) {
442                 vn_lock(um->um_lowervp, LK_EXCLUSIVE | LK_RETRY, p);
443                 vrele(um->um_lowervp);
444         }
445
446         return (error);
447 }
448
449 static int
450 union_statfs(mp, sbp, p)
451         struct mount *mp;
452         struct statfs *sbp;
453         struct proc *p;
454 {
455         int error;
456         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
457         struct statfs mstat;
458         int lbsize;
459
460 #ifdef UNION_DIAGNOSTIC
461         printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp,
462                         um->um_lowervp,
463                         um->um_uppervp);
464 #endif
465
466         bzero(&mstat, sizeof(mstat));
467
468         if (um->um_lowervp) {
469                 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
470                 if (error)
471                         return (error);
472         }
473
474         /* now copy across the "interesting" information and fake the rest */
475 #if 0
476         sbp->f_type = mstat.f_type;
477         sbp->f_flags = mstat.f_flags;
478         sbp->f_bsize = mstat.f_bsize;
479         sbp->f_iosize = mstat.f_iosize;
480 #endif
481         lbsize = mstat.f_bsize;
482         sbp->f_blocks = mstat.f_blocks;
483         sbp->f_bfree = mstat.f_bfree;
484         sbp->f_bavail = mstat.f_bavail;
485         sbp->f_files = mstat.f_files;
486         sbp->f_ffree = mstat.f_ffree;
487
488         error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
489         if (error)
490                 return (error);
491
492         sbp->f_flags = mstat.f_flags;
493         sbp->f_bsize = mstat.f_bsize;
494         sbp->f_iosize = mstat.f_iosize;
495
496         /*
497          * if the lower and upper blocksizes differ, then frig the
498          * block counts so that the sizes reported by df make some
499          * kind of sense.  none of this makes sense though.
500          */
501
502         if (mstat.f_bsize != lbsize)
503                 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
504
505         /*
506          * The "total" fields count total resources in all layers,
507          * the "free" fields count only those resources which are
508          * free in the upper layer (since only the upper layer
509          * is writeable).
510          */
511         sbp->f_blocks += mstat.f_blocks;
512         sbp->f_bfree = mstat.f_bfree;
513         sbp->f_bavail = mstat.f_bavail;
514         sbp->f_files += mstat.f_files;
515         sbp->f_ffree = mstat.f_ffree;
516
517         if (sbp != &mp->mnt_stat) {
518                 sbp->f_type = mp->mnt_vfc->vfc_typenum;
519                 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
520                 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
521                 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
522         }
523         return (0);
524 }
525
526 /*
527  * Complement to all vpp returning ops.
528  * XXX - initially only to get rid of WILLRELE.
529  * XXX - may change when modification of vops start.
530  */
531 /* ARGSUSED */
532 static int
533 union_vrele(mp, vp)
534         struct mount *mp;
535         struct vnode *vp;
536 {
537         vrele(vp);
538         return (0);
539 }
540
541 /*
542  * XXX - Assumes no data cached at union layer.
543  */
544 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
545             struct proc *)))nullop)
546
547 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
548             struct sockaddr *, struct vnode **, int *, struct ucred **)))eopnotsupp)
549 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
550             struct proc *)))eopnotsupp)
551 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
552             size_t, struct proc *)))eopnotsupp)
553 #define union_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
554             eopnotsupp)
555 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
556
557 static struct vfsops union_vfsops = {
558         union_mount,
559         union_start,
560         union_unmount,
561         union_root,
562         union_quotactl,
563         union_statfs,
564         union_sync,
565         union_vget,
566         union_vrele,
567         union_fhtovp,
568         union_vptofh,
569         union_init,
570 };
571
572 VFS_SET(union_vfsops, union, MOUNT_UNION, VFCF_LOOPBACK);