]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/unionfs/union_vfsops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
5  * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
6  * All rights reserved.
7  *
8  * This code is derived from software donated to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)union_vfsops.c      8.20 (Berkeley) 5/20/95
36  * $FreeBSD$
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kdb.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/stat.h>
50
51 #include <fs/unionfs/union.h>
52
53 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
54
55 static vfs_fhtovp_t     unionfs_fhtovp;
56 static vfs_checkexp_t   unionfs_checkexp;
57 static vfs_mount_t      unionfs_domount;
58 static vfs_quotactl_t   unionfs_quotactl;
59 static vfs_root_t       unionfs_root;
60 static vfs_sync_t       unionfs_sync;
61 static vfs_statfs_t     unionfs_statfs;
62 static vfs_unmount_t    unionfs_unmount;
63 static vfs_vget_t       unionfs_vget;
64 static vfs_extattrctl_t unionfs_extattrctl;
65
66 static struct vfsops unionfs_vfsops;
67
68 /*
69  * Mount unionfs layer.
70  */
71 static int
72 unionfs_domount(struct mount *mp, struct thread *td)
73 {
74         int             error;
75         struct vnode   *lowerrootvp;
76         struct vnode   *upperrootvp;
77         struct unionfs_mount *ump;
78         char           *target;
79         char           *tmp;
80         char           *ep;
81         int             len;
82         size_t          done;
83         int             below;
84         uid_t           uid;
85         gid_t           gid;
86         u_short         udir;
87         u_short         ufile;
88         unionfs_copymode copymode;
89         unionfs_whitemode whitemode;
90         struct componentname fakecn;
91         struct nameidata nd, *ndp;
92         struct vattr    va;
93
94         UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
95
96         error = 0;
97         below = 0;
98         uid = 0;
99         gid = 0;
100         udir = 0;
101         ufile = 0;
102         copymode = UNIONFS_TRANSPARENT; /* default */
103         whitemode = UNIONFS_WHITE_ALWAYS;
104         ndp = &nd;
105
106         if (mp->mnt_flag & MNT_ROOTFS) {
107                 vfs_mount_error(mp, "Cannot union mount root filesystem");
108                 return (EOPNOTSUPP);
109         }
110
111         /*
112          * Update is a no operation.
113          */
114         if (mp->mnt_flag & MNT_UPDATE) {
115                 vfs_mount_error(mp, "unionfs does not support mount update");
116                 return (EOPNOTSUPP);
117         }
118
119         /*
120          * Get argument
121          */
122         error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
123         if (error)
124                 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
125                     &len);
126         if (error || target[len - 1] != '\0') {
127                 vfs_mount_error(mp, "Invalid target");
128                 return (EINVAL);
129         }
130         if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
131                 below = 1;
132         if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
133                 if (tmp != NULL)
134                         udir = (mode_t)strtol(tmp, &ep, 8);
135                 if (tmp == NULL || *ep) {
136                         vfs_mount_error(mp, "Invalid udir");
137                         return (EINVAL);
138                 }
139                 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
140         }
141         if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
142                 if (tmp != NULL)
143                         ufile = (mode_t)strtol(tmp, &ep, 8);
144                 if (tmp == NULL || *ep) {
145                         vfs_mount_error(mp, "Invalid ufile");
146                         return (EINVAL);
147                 }
148                 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
149         }
150         /* check umask, uid and gid */
151         if (udir == 0 && ufile != 0)
152                 udir = ufile;
153         if (ufile == 0 && udir != 0)
154                 ufile = udir;
155
156         vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY, td);
157         error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred, td);
158         if (!error) {
159                 if (udir == 0)
160                         udir = va.va_mode;
161                 if (ufile == 0)
162                         ufile = va.va_mode;
163                 uid = va.va_uid;
164                 gid = va.va_gid;
165         }
166         VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
167         if (error)
168                 return (error);
169
170         if (mp->mnt_cred->cr_ruid == 0) {       /* root only */
171                 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
172                     NULL) == 0) {
173                         if (tmp != NULL)
174                                 uid = (uid_t)strtol(tmp, &ep, 10);
175                         if (tmp == NULL || *ep) {
176                                 vfs_mount_error(mp, "Invalid uid");
177                                 return (EINVAL);
178                         }
179                 }
180                 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
181                     NULL) == 0) {
182                         if (tmp != NULL)
183                                 gid = (gid_t)strtol(tmp, &ep, 10);
184                         if (tmp == NULL || *ep) {
185                                 vfs_mount_error(mp, "Invalid gid");
186                                 return (EINVAL);
187                         }
188                 }
189                 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
190                     NULL) == 0) {
191                         if (tmp == NULL) {
192                                 vfs_mount_error(mp, "Invalid copymode");
193                                 return (EINVAL);
194                         } else if (strcasecmp(tmp, "traditional") == 0)
195                                 copymode = UNIONFS_TRADITIONAL;
196                         else if (strcasecmp(tmp, "transparent") == 0)
197                                 copymode = UNIONFS_TRANSPARENT;
198                         else if (strcasecmp(tmp, "masquerade") == 0)
199                                 copymode = UNIONFS_MASQUERADE;
200                         else {
201                                 vfs_mount_error(mp, "Invalid copymode");
202                                 return (EINVAL);
203                         }
204                 }
205                 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
206                     NULL) == 0) {
207                         if (tmp == NULL) {
208                                 vfs_mount_error(mp, "Invalid whiteout mode");
209                                 return (EINVAL);
210                         } else if (strcasecmp(tmp, "always") == 0)
211                                 whitemode = UNIONFS_WHITE_ALWAYS;
212                         else if (strcasecmp(tmp, "whenneeded") == 0)
213                                 whitemode = UNIONFS_WHITE_WHENNEEDED;
214                         else {
215                                 vfs_mount_error(mp, "Invalid whiteout mode");
216                                 return (EINVAL);
217                         }
218                 }
219         }
220         /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
221         if (copymode == UNIONFS_TRADITIONAL) {
222                 uid = mp->mnt_cred->cr_ruid;
223                 gid = mp->mnt_cred->cr_rgid;
224         }
225
226         UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
227         UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
228         UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
229
230         /*
231          * Find upper node
232          */
233         NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
234         if ((error = namei(ndp)))
235                 return (error);
236
237         NDFREE(ndp, NDF_ONLY_PNBUF);
238
239         /* get root vnodes */
240         lowerrootvp = mp->mnt_vnodecovered;
241         upperrootvp = ndp->ni_vp;
242
243         /* create unionfs_mount */
244         ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
245             M_UNIONFSMNT, M_WAITOK | M_ZERO);
246
247         /*
248          * Save reference
249          */
250         if (below) {
251                 VOP_UNLOCK(upperrootvp, 0, td);
252                 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td);
253                 ump->um_lowervp = upperrootvp;
254                 ump->um_uppervp = lowerrootvp;
255         } else {
256                 ump->um_lowervp = lowerrootvp;
257                 ump->um_uppervp = upperrootvp;
258         }
259         ump->um_rootvp = NULLVP;
260         ump->um_uid = uid;
261         ump->um_gid = gid;
262         ump->um_udir = udir;
263         ump->um_ufile = ufile;
264         ump->um_copymode = copymode;
265         ump->um_whitemode = whitemode;
266
267         MNT_ILOCK(mp);
268         if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) &&
269             (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE))
270                 mp->mnt_kern_flag |= MNTK_MPSAFE;
271         MNT_IUNLOCK(mp);
272         mp->mnt_data = ump;
273
274         /*
275          * Copy upper layer's RDONLY flag.
276          */
277         mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
278
279         /*
280          * Check whiteout
281          */
282         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
283                 memset(&fakecn, 0, sizeof(fakecn));
284                 fakecn.cn_nameiop = LOOKUP;
285                 fakecn.cn_thread = td;
286                 error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
287                 if (error) {
288                         if (below) {
289                                 VOP_UNLOCK(ump->um_uppervp, 0, td);
290                                 vrele(upperrootvp);
291                         } else
292                                 vput(ump->um_uppervp);
293                         free(ump, M_UNIONFSMNT);
294                         mp->mnt_data = NULL;
295                         return (error);
296                 }
297         }
298
299         /*
300          * Unlock the node
301          */
302         VOP_UNLOCK(ump->um_uppervp, 0, td);
303
304         /*
305          * Get the unionfs root vnode.
306          */
307         error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
308             NULLVP, &(ump->um_rootvp), NULL, td);
309         vrele(upperrootvp);
310         if (error) {
311                 free(ump, M_UNIONFSMNT);
312                 mp->mnt_data = NULL;
313                 return (error);
314         }
315
316         /*
317          * Check mnt_flag
318          */
319         if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
320             (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
321                 mp->mnt_flag |= MNT_LOCAL;
322
323         /*
324          * Get new fsid
325          */
326         vfs_getnewfsid(mp);
327
328         len = MNAMELEN - 1;
329         tmp = mp->mnt_stat.f_mntfromname;
330         copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
331         len -= done - 1;
332         tmp += done - 1;
333         copystr(target, tmp, len, NULL);
334
335         UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
336             mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
337
338         return (0);
339 }
340
341 /*
342  * Free reference to unionfs layer
343  */
344 static int
345 unionfs_unmount(struct mount *mp, int mntflags, struct thread *td)
346 {
347         struct unionfs_mount *ump;
348         int             error;
349         int             num;
350         int             freeing;
351         int             flags;
352
353         UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
354
355         ump = MOUNTTOUNIONFSMOUNT(mp);
356         flags = 0;
357
358         if (mntflags & MNT_FORCE)
359                 flags |= FORCECLOSE;
360
361         /* vflush (no need to call vrele) */
362         for (freeing = 0; (error = vflush(mp, 1, flags, td)) != 0;) {
363                 num = mp->mnt_nvnodelistsize;
364                 if (num == freeing)
365                         break;
366                 freeing = num;
367         }
368
369         if (error)
370                 return (error);
371
372         free(ump, M_UNIONFSMNT);
373         mp->mnt_data = 0;
374
375         return (0);
376 }
377
378 static int
379 unionfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
380 {
381         struct unionfs_mount *ump;
382         struct vnode   *vp;
383
384         ump = MOUNTTOUNIONFSMOUNT(mp);
385         vp = ump->um_rootvp;
386
387         UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
388             vp, VOP_ISLOCKED(vp, td));
389
390         vref(vp);
391         if (flags & LK_TYPE_MASK)
392                 vn_lock(vp, flags, td);
393
394         *vpp = vp;
395
396         return (0);
397 }
398
399 static int
400 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
401     struct thread *td)
402 {
403         struct unionfs_mount *ump;
404
405         ump = MOUNTTOUNIONFSMOUNT(mp);
406
407         /*
408          * Writing is always performed to upper vnode.
409          */
410         return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg, td));
411 }
412
413 static int
414 unionfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
415 {
416         struct unionfs_mount *ump;
417         int             error;
418         struct statfs   mstat;
419         uint64_t        lbsize;
420
421         ump = MOUNTTOUNIONFSMOUNT(mp);
422
423         UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
424             (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
425
426         bzero(&mstat, sizeof(mstat));
427
428         error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat, td);
429         if (error)
430                 return (error);
431
432         /* now copy across the "interesting" information and fake the rest */
433         sbp->f_blocks = mstat.f_blocks;
434         sbp->f_files = mstat.f_files;
435
436         lbsize = mstat.f_bsize;
437
438         error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat, td);
439         if (error)
440                 return (error);
441
442         /*
443          * The FS type etc is copy from upper vfs.
444          * (write able vfs have priority)
445          */
446         sbp->f_type = mstat.f_type;
447         sbp->f_flags = mstat.f_flags;
448         sbp->f_bsize = mstat.f_bsize;
449         sbp->f_iosize = mstat.f_iosize;
450
451         if (mstat.f_bsize != lbsize)
452                 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
453
454         sbp->f_blocks += mstat.f_blocks;
455         sbp->f_bfree = mstat.f_bfree;
456         sbp->f_bavail = mstat.f_bavail;
457         sbp->f_files += mstat.f_files;
458         sbp->f_ffree = mstat.f_ffree;
459         return (0);
460 }
461
462 static int
463 unionfs_sync(struct mount *mp, int waitfor, struct thread *td)
464 {
465         /* nothing to do */
466         return (0);
467 }
468
469 static int
470 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
471 {
472         return (EOPNOTSUPP);
473 }
474
475 static int
476 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
477 {
478         return (EOPNOTSUPP);
479 }
480
481 static int
482 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
483                  struct ucred **credanonp)
484 {
485         return (EOPNOTSUPP);
486 }
487
488 static int
489 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
490     int namespace, const char *attrname, struct thread *td)
491 {
492         struct unionfs_mount *ump;
493         struct unionfs_node *unp;
494
495         ump = MOUNTTOUNIONFSMOUNT(mp);
496         unp = VTOUNIONFS(filename_vp);
497
498         if (unp->un_uppervp != NULLVP) {
499                 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
500                     unp->un_uppervp, namespace, attrname, td));
501         } else {
502                 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
503                     unp->un_lowervp, namespace, attrname, td));
504         }
505 }
506
507 static struct vfsops unionfs_vfsops = {
508         .vfs_checkexp =         unionfs_checkexp,
509         .vfs_extattrctl =       unionfs_extattrctl,
510         .vfs_fhtovp =           unionfs_fhtovp,
511         .vfs_init =             unionfs_init,
512         .vfs_mount =            unionfs_domount,
513         .vfs_quotactl =         unionfs_quotactl,
514         .vfs_root =             unionfs_root,
515         .vfs_statfs =           unionfs_statfs,
516         .vfs_sync =             unionfs_sync,
517         .vfs_uninit =           unionfs_uninit,
518         .vfs_unmount =          unionfs_unmount,
519         .vfs_vget =             unionfs_vget,
520 };
521
522 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);