]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/unionfs/union_vfsops.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[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  * 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/fcntl.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/stat.h>
51
52 #include <fs/unionfs/union.h>
53
54 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
55
56 static vfs_fhtovp_t     unionfs_fhtovp;
57 static vfs_checkexp_t   unionfs_checkexp;
58 static vfs_mount_t      unionfs_domount;
59 static vfs_quotactl_t   unionfs_quotactl;
60 static vfs_root_t       unionfs_root;
61 static vfs_sync_t       unionfs_sync;
62 static vfs_statfs_t     unionfs_statfs;
63 static vfs_unmount_t    unionfs_unmount;
64 static vfs_vget_t       unionfs_vget;
65 static vfs_extattrctl_t unionfs_extattrctl;
66
67 static struct vfsops unionfs_vfsops;
68
69 /*
70  * Exchange from userland file mode to vmode.
71  */
72 static u_short 
73 mode2vmode(mode_t mode)
74 {
75         u_short         ret;
76
77         ret = 0;
78
79         /* other */
80         if (mode & S_IXOTH)
81                 ret |= VEXEC >> 6;
82         if (mode & S_IWOTH)
83                 ret |= VWRITE >> 6;
84         if (mode & S_IROTH)
85                 ret |= VREAD >> 6;
86
87         /* group */
88         if (mode & S_IXGRP)
89                 ret |= VEXEC >> 3;
90         if (mode & S_IWGRP)
91                 ret |= VWRITE >> 3;
92         if (mode & S_IRGRP)
93                 ret |= VREAD >> 3;
94
95         /* owner */
96         if (mode & S_IXUSR)
97                 ret |= VEXEC;
98         if (mode & S_IWUSR)
99                 ret |= VWRITE;
100         if (mode & S_IRUSR)
101                 ret |= VREAD;
102
103         return (ret);
104 }
105
106 /*
107  * Mount unionfs layer.
108  */
109 static int
110 unionfs_domount(struct mount *mp, struct thread *td)
111 {
112         int             error;
113         struct vnode   *lowerrootvp;
114         struct vnode   *upperrootvp;
115         struct unionfs_mount *ump;
116         char           *target;
117         char           *tmp;
118         char           *ep;
119         int             len;
120         size_t          done;
121         int             below;
122         uid_t           uid;
123         gid_t           gid;
124         u_short         udir;
125         u_short         ufile;
126         unionfs_copymode copymode;
127         unionfs_whitemode whitemode;
128         struct componentname fakecn;
129         struct nameidata nd, *ndp;
130         struct vattr    va;
131
132         UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
133
134         error = 0;
135         below = 0;
136         uid = 0;
137         gid = 0;
138         udir = 0;
139         ufile = 0;
140         copymode = UNIONFS_TRANSPARENT; /* default */
141         whitemode = UNIONFS_WHITE_ALWAYS;
142         ndp = &nd;
143
144         if (mp->mnt_flag & MNT_ROOTFS) {
145                 vfs_mount_error(mp, "Cannot union mount root filesystem");
146                 return (EOPNOTSUPP);
147         }
148
149         /*
150          * Update is a no operation.
151          */
152         if (mp->mnt_flag & MNT_UPDATE) {
153                 vfs_mount_error(mp, "unionfs does not support mount update");
154                 return (EOPNOTSUPP);
155         }
156
157         /*
158          * Get argument
159          */
160         error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
161         if (error)
162                 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
163                     &len);
164         if (error || target[len - 1] != '\0') {
165                 vfs_mount_error(mp, "Invalid target");
166                 return (EINVAL);
167         }
168         if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
169                 below = 1;
170         if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
171                 if (tmp != NULL)
172                         udir = (mode_t)strtol(tmp, &ep, 8);
173                 if (tmp == NULL || *ep) {
174                         vfs_mount_error(mp, "Invalid udir");
175                         return (EINVAL);
176                 }
177                 udir = mode2vmode(udir);
178         }
179         if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
180                 if (tmp != NULL)
181                         ufile = (mode_t)strtol(tmp, &ep, 8);
182                 if (tmp == NULL || *ep) {
183                         vfs_mount_error(mp, "Invalid ufile");
184                         return (EINVAL);
185                 }
186                 ufile = mode2vmode(ufile);
187         }
188         /* check umask, uid and gid */
189         if (udir == 0 && ufile != 0)
190                 udir = ufile;
191         if (ufile == 0 && udir != 0)
192                 ufile = udir;
193
194         vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
195         error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
196         if (!error) {
197                 if (udir == 0)
198                         udir = va.va_mode;
199                 if (ufile == 0)
200                         ufile = va.va_mode;
201                 uid = va.va_uid;
202                 gid = va.va_gid;
203         }
204         VOP_UNLOCK(mp->mnt_vnodecovered, 0);
205         if (error)
206                 return (error);
207
208         if (mp->mnt_cred->cr_ruid == 0) {       /* root only */
209                 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
210                     NULL) == 0) {
211                         if (tmp != NULL)
212                                 uid = (uid_t)strtol(tmp, &ep, 10);
213                         if (tmp == NULL || *ep) {
214                                 vfs_mount_error(mp, "Invalid uid");
215                                 return (EINVAL);
216                         }
217                 }
218                 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
219                     NULL) == 0) {
220                         if (tmp != NULL)
221                                 gid = (gid_t)strtol(tmp, &ep, 10);
222                         if (tmp == NULL || *ep) {
223                                 vfs_mount_error(mp, "Invalid gid");
224                                 return (EINVAL);
225                         }
226                 }
227                 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
228                     NULL) == 0) {
229                         if (tmp == NULL) {
230                                 vfs_mount_error(mp, "Invalid copymode");
231                                 return (EINVAL);
232                         } else if (strcasecmp(tmp, "traditional") == 0)
233                                 copymode = UNIONFS_TRADITIONAL;
234                         else if (strcasecmp(tmp, "transparent") == 0)
235                                 copymode = UNIONFS_TRANSPARENT;
236                         else if (strcasecmp(tmp, "masquerade") == 0)
237                                 copymode = UNIONFS_MASQUERADE;
238                         else {
239                                 vfs_mount_error(mp, "Invalid copymode");
240                                 return (EINVAL);
241                         }
242                 }
243                 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
244                     NULL) == 0) {
245                         if (tmp == NULL) {
246                                 vfs_mount_error(mp, "Invalid whiteout mode");
247                                 return (EINVAL);
248                         } else if (strcasecmp(tmp, "always") == 0)
249                                 whitemode = UNIONFS_WHITE_ALWAYS;
250                         else if (strcasecmp(tmp, "whenneeded") == 0)
251                                 whitemode = UNIONFS_WHITE_WHENNEEDED;
252                         else {
253                                 vfs_mount_error(mp, "Invalid whiteout mode");
254                                 return (EINVAL);
255                         }
256                 }
257         }
258         /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
259         if (copymode == UNIONFS_TRADITIONAL) {
260                 uid = mp->mnt_cred->cr_ruid;
261                 gid = mp->mnt_cred->cr_rgid;
262         }
263
264         UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
265         UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
266         UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
267
268         /*
269          * Find upper node
270          */
271         NDINIT(ndp, LOOKUP, FOLLOW | WANTPARENT | LOCKLEAF, UIO_SYSSPACE, target, td);
272         if ((error = namei(ndp)))
273                 return (error);
274
275         NDFREE(ndp, NDF_ONLY_PNBUF);
276
277         /* get root vnodes */
278         lowerrootvp = mp->mnt_vnodecovered;
279         upperrootvp = ndp->ni_vp;
280
281         vrele(ndp->ni_dvp);
282         ndp->ni_dvp = NULLVP;
283
284         /* create unionfs_mount */
285         ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
286             M_UNIONFSMNT, M_WAITOK | M_ZERO);
287
288         /*
289          * Save reference
290          */
291         if (below) {
292                 VOP_UNLOCK(upperrootvp, 0);
293                 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
294                 ump->um_lowervp = upperrootvp;
295                 ump->um_uppervp = lowerrootvp;
296         } else {
297                 ump->um_lowervp = lowerrootvp;
298                 ump->um_uppervp = upperrootvp;
299         }
300         ump->um_rootvp = NULLVP;
301         ump->um_uid = uid;
302         ump->um_gid = gid;
303         ump->um_udir = udir;
304         ump->um_ufile = ufile;
305         ump->um_copymode = copymode;
306         ump->um_whitemode = whitemode;
307
308         MNT_ILOCK(mp);
309         if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) &&
310             (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE))
311                 mp->mnt_kern_flag |= MNTK_MPSAFE;
312         MNT_IUNLOCK(mp);
313         mp->mnt_data = ump;
314
315         /*
316          * Copy upper layer's RDONLY flag.
317          */
318         mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
319
320         /*
321          * Check whiteout
322          */
323         if ((mp->mnt_flag & MNT_RDONLY) == 0) {
324                 memset(&fakecn, 0, sizeof(fakecn));
325                 fakecn.cn_nameiop = LOOKUP;
326                 fakecn.cn_thread = td;
327                 error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
328                 if (error) {
329                         if (below) {
330                                 VOP_UNLOCK(ump->um_uppervp, 0);
331                                 vrele(upperrootvp);
332                         } else
333                                 vput(ump->um_uppervp);
334                         free(ump, M_UNIONFSMNT);
335                         mp->mnt_data = NULL;
336                         return (error);
337                 }
338         }
339
340         /*
341          * Unlock the node
342          */
343         VOP_UNLOCK(ump->um_uppervp, 0);
344
345         /*
346          * Get the unionfs root vnode.
347          */
348         error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
349             NULLVP, &(ump->um_rootvp), NULL, td);
350         vrele(upperrootvp);
351         if (error) {
352                 free(ump, M_UNIONFSMNT);
353                 mp->mnt_data = NULL;
354                 return (error);
355         }
356
357         /*
358          * Check mnt_flag
359          */
360         if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
361             (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
362                 mp->mnt_flag |= MNT_LOCAL;
363
364         /*
365          * Get new fsid
366          */
367         vfs_getnewfsid(mp);
368
369         len = MNAMELEN - 1;
370         tmp = mp->mnt_stat.f_mntfromname;
371         copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
372         len -= done - 1;
373         tmp += done - 1;
374         copystr(target, tmp, len, NULL);
375
376         UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
377             mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
378
379         return (0);
380 }
381
382 /*
383  * Free reference to unionfs layer
384  */
385 static int
386 unionfs_unmount(struct mount *mp, int mntflags, struct thread *td)
387 {
388         struct unionfs_mount *ump;
389         int             error;
390         int             num;
391         int             freeing;
392         int             flags;
393
394         UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
395
396         ump = MOUNTTOUNIONFSMOUNT(mp);
397         flags = 0;
398
399         if (mntflags & MNT_FORCE)
400                 flags |= FORCECLOSE;
401
402         /* vflush (no need to call vrele) */
403         for (freeing = 0; (error = vflush(mp, 1, flags, td)) != 0;) {
404                 num = mp->mnt_nvnodelistsize;
405                 if (num == freeing)
406                         break;
407                 freeing = num;
408         }
409
410         if (error)
411                 return (error);
412
413         free(ump, M_UNIONFSMNT);
414         mp->mnt_data = 0;
415
416         return (0);
417 }
418
419 static int
420 unionfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
421 {
422         struct unionfs_mount *ump;
423         struct vnode   *vp;
424
425         ump = MOUNTTOUNIONFSMOUNT(mp);
426         vp = ump->um_rootvp;
427
428         UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
429             vp, VOP_ISLOCKED(vp));
430
431         vref(vp);
432         if (flags & LK_TYPE_MASK)
433                 vn_lock(vp, flags);
434
435         *vpp = vp;
436
437         return (0);
438 }
439
440 static int
441 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
442     struct thread *td)
443 {
444         struct unionfs_mount *ump;
445
446         ump = MOUNTTOUNIONFSMOUNT(mp);
447
448         /*
449          * Writing is always performed to upper vnode.
450          */
451         return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg, td));
452 }
453
454 static int
455 unionfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
456 {
457         struct unionfs_mount *ump;
458         int             error;
459         struct statfs   mstat;
460         uint64_t        lbsize;
461
462         ump = MOUNTTOUNIONFSMOUNT(mp);
463
464         UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
465             (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
466
467         bzero(&mstat, sizeof(mstat));
468
469         error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat, td);
470         if (error)
471                 return (error);
472
473         /* now copy across the "interesting" information and fake the rest */
474         sbp->f_blocks = mstat.f_blocks;
475         sbp->f_files = mstat.f_files;
476
477         lbsize = mstat.f_bsize;
478
479         error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat, td);
480         if (error)
481                 return (error);
482
483         /*
484          * The FS type etc is copy from upper vfs.
485          * (write able vfs have priority)
486          */
487         sbp->f_type = mstat.f_type;
488         sbp->f_flags = mstat.f_flags;
489         sbp->f_bsize = mstat.f_bsize;
490         sbp->f_iosize = mstat.f_iosize;
491
492         if (mstat.f_bsize != lbsize)
493                 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
494
495         sbp->f_blocks += mstat.f_blocks;
496         sbp->f_bfree = mstat.f_bfree;
497         sbp->f_bavail = mstat.f_bavail;
498         sbp->f_files += mstat.f_files;
499         sbp->f_ffree = mstat.f_ffree;
500         return (0);
501 }
502
503 static int
504 unionfs_sync(struct mount *mp, int waitfor, struct thread *td)
505 {
506         /* nothing to do */
507         return (0);
508 }
509
510 static int
511 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
512 {
513         return (EOPNOTSUPP);
514 }
515
516 static int
517 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
518 {
519         return (EOPNOTSUPP);
520 }
521
522 static int
523 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
524                  struct ucred **credanonp)
525 {
526         return (EOPNOTSUPP);
527 }
528
529 static int
530 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
531     int namespace, const char *attrname, struct thread *td)
532 {
533         struct unionfs_mount *ump;
534         struct unionfs_node *unp;
535
536         ump = MOUNTTOUNIONFSMOUNT(mp);
537         unp = VTOUNIONFS(filename_vp);
538
539         if (unp->un_uppervp != NULLVP) {
540                 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
541                     unp->un_uppervp, namespace, attrname, td));
542         } else {
543                 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
544                     unp->un_lowervp, namespace, attrname, td));
545         }
546 }
547
548 static struct vfsops unionfs_vfsops = {
549         .vfs_checkexp =         unionfs_checkexp,
550         .vfs_extattrctl =       unionfs_extattrctl,
551         .vfs_fhtovp =           unionfs_fhtovp,
552         .vfs_init =             unionfs_init,
553         .vfs_mount =            unionfs_domount,
554         .vfs_quotactl =         unionfs_quotactl,
555         .vfs_root =             unionfs_root,
556         .vfs_statfs =           unionfs_statfs,
557         .vfs_sync =             unionfs_sync,
558         .vfs_uninit =           unionfs_uninit,
559         .vfs_unmount =          unionfs_unmount,
560         .vfs_vget =             unionfs_vget,
561 };
562
563 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);