umap_vfsops.c revision 1.102 1 1.102 pgoyette /* $NetBSD: umap_vfsops.c,v 1.102 2020/03/16 21:20:11 pgoyette Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1992, 1993
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * This code is derived from software donated to Berkeley by
8 1.1 mycroft * the UCLA Ficus project.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.40 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 mycroft * may be used to endorse or promote products derived from this software
20 1.1 mycroft * without specific prior written permission.
21 1.1 mycroft *
22 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 mycroft * SUCH DAMAGE.
33 1.1 mycroft *
34 1.1 mycroft * from: @(#)null_vfsops.c 1.5 (Berkeley) 7/10/92
35 1.18 fvdl * @(#)umap_vfsops.c 8.8 (Berkeley) 5/14/95
36 1.1 mycroft */
37 1.1 mycroft
38 1.1 mycroft /*
39 1.1 mycroft * Umap Layer
40 1.1 mycroft * (See mount_umap(8) for a description of this layer.)
41 1.1 mycroft */
42 1.32 lukem
43 1.32 lukem #include <sys/cdefs.h>
44 1.102 pgoyette __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.102 2020/03/16 21:20:11 pgoyette Exp $");
45 1.1 mycroft
46 1.1 mycroft #include <sys/param.h>
47 1.1 mycroft #include <sys/systm.h>
48 1.41 atatat #include <sys/sysctl.h>
49 1.18 fvdl #include <sys/proc.h>
50 1.1 mycroft #include <sys/time.h>
51 1.1 mycroft #include <sys/vnode.h>
52 1.1 mycroft #include <sys/mount.h>
53 1.1 mycroft #include <sys/namei.h>
54 1.101 perseant #include <sys/syslog.h>
55 1.57 elad #include <sys/kauth.h>
56 1.78 rumble #include <sys/module.h>
57 1.57 elad
58 1.1 mycroft #include <miscfs/umapfs/umap.h>
59 1.23 wrstuden #include <miscfs/genfs/layer_extern.h>
60 1.1 mycroft
61 1.83 pooka MODULE(MODULE_CLASS_VFS, umap, "layerfs");
62 1.78 rumble
63 1.69 pooka VFS_PROTOS(umapfs);
64 1.9 christos
65 1.1 mycroft /*
66 1.1 mycroft * Mount umap layer
67 1.1 mycroft */
68 1.1 mycroft int
69 1.82 dsl umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
70 1.1 mycroft {
71 1.72 pooka struct lwp *l = curlwp;
72 1.86 dholland struct pathbuf *pb;
73 1.69 pooka struct nameidata nd;
74 1.66 dsl struct umap_args *args = data;
75 1.1 mycroft struct vnode *lowerrootvp, *vp;
76 1.1 mycroft struct umap_mount *amp;
77 1.1 mycroft int error;
78 1.22 perseant #ifdef UMAPFS_DIAGNOSTIC
79 1.22 perseant int i;
80 1.22 perseant #endif
81 1.101 perseant fsid_t tfsid;
82 1.56 christos
83 1.92 maxv if (args == NULL)
84 1.92 maxv return EINVAL;
85 1.101 perseant if (*data_len < sizeof *args) {
86 1.101 perseant #ifdef UMAPFS_DIAGNOSTIC
87 1.101 perseant printf("mount_umap: data len %d < args %d\n",
88 1.101 perseant (int)*data_len, (int)(sizeof *args));
89 1.101 perseant #endif
90 1.66 dsl return EINVAL;
91 1.101 perseant }
92 1.66 dsl
93 1.35 christos if (mp->mnt_flag & MNT_GETARGS) {
94 1.35 christos amp = MOUNTTOUMAPMOUNT(mp);
95 1.35 christos if (amp == NULL)
96 1.35 christos return EIO;
97 1.66 dsl args->la.target = NULL;
98 1.66 dsl args->nentries = amp->info_nentries;
99 1.66 dsl args->gnentries = amp->info_gnentries;
100 1.66 dsl *data_len = sizeof *args;
101 1.66 dsl return 0;
102 1.35 christos }
103 1.21 bouyer
104 1.21 bouyer /* only for root */
105 1.87 elad error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
106 1.87 elad KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL);
107 1.87 elad if (error)
108 1.21 bouyer return error;
109 1.1 mycroft
110 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
111 1.15 christos printf("umapfs_mount(mp = %p)\n", mp);
112 1.1 mycroft #endif
113 1.1 mycroft
114 1.1 mycroft /*
115 1.55 jmmv * Update is not supported
116 1.23 wrstuden */
117 1.55 jmmv if (mp->mnt_flag & MNT_UPDATE)
118 1.55 jmmv return EOPNOTSUPP;
119 1.23 wrstuden
120 1.23 wrstuden /*
121 1.1 mycroft * Find lower node
122 1.1 mycroft */
123 1.86 dholland error = pathbuf_copyin(args->umap_target, &pb);
124 1.86 dholland if (error) {
125 1.86 dholland return error;
126 1.86 dholland }
127 1.86 dholland NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
128 1.86 dholland if ((error = namei(&nd)) != 0) {
129 1.86 dholland pathbuf_destroy(pb);
130 1.86 dholland return error;
131 1.86 dholland }
132 1.1 mycroft
133 1.1 mycroft /*
134 1.1 mycroft * Sanity check on lower vnode
135 1.1 mycroft */
136 1.69 pooka lowerrootvp = nd.ni_vp;
137 1.86 dholland pathbuf_destroy(pb);
138 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
139 1.15 christos printf("vp = %p, check for VDIR...\n", lowerrootvp);
140 1.1 mycroft #endif
141 1.1 mycroft
142 1.1 mycroft if (lowerrootvp->v_type != VDIR) {
143 1.1 mycroft vput(lowerrootvp);
144 1.1 mycroft return (EINVAL);
145 1.1 mycroft }
146 1.1 mycroft
147 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
148 1.15 christos printf("mp = %p\n", mp);
149 1.1 mycroft #endif
150 1.1 mycroft
151 1.88 rmind amp = kmem_zalloc(sizeof(struct umap_mount), KM_SLEEP);
152 1.34 soren mp->mnt_data = amp;
153 1.1 mycroft
154 1.51 perry /*
155 1.1 mycroft * Now copy in the number of entries and maps for umap mapping.
156 1.1 mycroft */
157 1.94 maxv if (args->nentries < 0 || args->nentries > MAPFILEENTRIES ||
158 1.94 maxv args->gnentries < 0 || args->gnentries > GMAPFILEENTRIES) {
159 1.29 jdolecek vput(lowerrootvp);
160 1.94 maxv return (EINVAL);
161 1.29 jdolecek }
162 1.29 jdolecek
163 1.66 dsl amp->info_nentries = args->nentries;
164 1.66 dsl amp->info_gnentries = args->gnentries;
165 1.66 dsl error = copyin(args->mapdata, amp->info_mapdata,
166 1.66 dsl 2*sizeof(u_long)*args->nentries);
167 1.23 wrstuden if (error) {
168 1.23 wrstuden vput(lowerrootvp);
169 1.1 mycroft return (error);
170 1.23 wrstuden }
171 1.1 mycroft
172 1.22 perseant #ifdef UMAPFS_DIAGNOSTIC
173 1.66 dsl printf("umap_mount:nentries %d\n",args->nentries);
174 1.66 dsl for (i = 0; i < args->nentries; i++)
175 1.22 perseant printf(" %ld maps to %ld\n", amp->info_mapdata[i][0],
176 1.1 mycroft amp->info_mapdata[i][1]);
177 1.1 mycroft #endif
178 1.1 mycroft
179 1.66 dsl error = copyin(args->gmapdata, amp->info_gmapdata,
180 1.66 dsl 2*sizeof(u_long)*args->gnentries);
181 1.23 wrstuden if (error) {
182 1.23 wrstuden vput(lowerrootvp);
183 1.1 mycroft return (error);
184 1.23 wrstuden }
185 1.1 mycroft
186 1.22 perseant #ifdef UMAPFS_DIAGNOSTIC
187 1.66 dsl printf("umap_mount:gnentries %d\n",args->gnentries);
188 1.66 dsl for (i = 0; i < args->gnentries; i++)
189 1.51 perry printf("\tgroup %ld maps to %ld\n",
190 1.1 mycroft amp->info_gmapdata[i][0],
191 1.1 mycroft amp->info_gmapdata[i][1]);
192 1.1 mycroft #endif
193 1.1 mycroft
194 1.1 mycroft /*
195 1.23 wrstuden * Make sure the mount point's sufficiently initialized
196 1.23 wrstuden * that the node create call will work.
197 1.1 mycroft */
198 1.101 perseant tfsid.__fsid_val[0] = (int32_t)args->fsid;
199 1.101 perseant tfsid.__fsid_val[1] = makefstype(MOUNT_UMAP);
200 1.101 perseant if (tfsid.__fsid_val[0] == 0) {
201 1.101 perseant log(LOG_WARNING, "umapfs: fsid given as 0, ignoring\n");
202 1.101 perseant vfs_getnewfsid(mp);
203 1.101 perseant } else if (vfs_getvfs(&tfsid)) {
204 1.101 perseant log(LOG_WARNING, "umapfs: fsid %x already mounted\n",
205 1.101 perseant tfsid.__fsid_val[0]);
206 1.101 perseant vfs_getnewfsid(mp);
207 1.101 perseant } else {
208 1.101 perseant mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
209 1.101 perseant mp->mnt_stat.f_fsidx.__fsid_val[1] = tfsid.__fsid_val[1];
210 1.101 perseant mp->mnt_stat.f_fsid = tfsid.__fsid_val[0];
211 1.101 perseant }
212 1.101 perseant log(LOG_DEBUG, "umapfs: using fsid %x/%x\n",
213 1.101 perseant mp->mnt_stat.f_fsidx.__fsid_val[0],
214 1.101 perseant mp->mnt_stat.f_fsidx.__fsid_val[1]);
215 1.100 hannken mp->mnt_lower = lowerrootvp->v_mount;
216 1.100 hannken
217 1.23 wrstuden amp->umapm_size = sizeof(struct umap_node);
218 1.23 wrstuden amp->umapm_tag = VT_UMAP;
219 1.23 wrstuden amp->umapm_bypass = umap_bypass;
220 1.23 wrstuden amp->umapm_vnodeop_p = umap_vnodeop_p;
221 1.23 wrstuden
222 1.1 mycroft /*
223 1.23 wrstuden * fix up umap node for root vnode.
224 1.1 mycroft */
225 1.89 hannken VOP_UNLOCK(lowerrootvp);
226 1.23 wrstuden error = layer_node_create(mp, lowerrootvp, &vp);
227 1.1 mycroft /*
228 1.1 mycroft * Make sure the node alias worked
229 1.1 mycroft */
230 1.1 mycroft if (error) {
231 1.89 hannken vrele(lowerrootvp);
232 1.88 rmind kmem_free(amp, sizeof(struct umap_mount));
233 1.88 rmind return error;
234 1.1 mycroft }
235 1.1 mycroft
236 1.1 mycroft /*
237 1.1 mycroft * Keep a held reference to the root vnode.
238 1.1 mycroft * It is vrele'd in umapfs_unmount.
239 1.1 mycroft */
240 1.89 hannken vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
241 1.89 hannken vp->v_vflag |= VV_ROOT;
242 1.23 wrstuden amp->umapm_rootvp = vp;
243 1.89 hannken VOP_UNLOCK(vp);
244 1.1 mycroft
245 1.66 dsl error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
246 1.67 pooka UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
247 1.98 hannken if (error)
248 1.98 hannken return error;
249 1.99 hannken
250 1.99 hannken if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
251 1.99 hannken mp->mnt_flag |= MNT_LOCAL;
252 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
253 1.11 christos printf("umapfs_mount: lower %s, alias at %s\n",
254 1.1 mycroft mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
255 1.1 mycroft #endif
256 1.98 hannken return 0;
257 1.1 mycroft }
258 1.1 mycroft
259 1.1 mycroft /*
260 1.1 mycroft * Free reference to umap layer
261 1.1 mycroft */
262 1.1 mycroft int
263 1.72 pooka umapfs_unmount(struct mount *mp, int mntflags)
264 1.1 mycroft {
265 1.71 ad struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp);
266 1.71 ad struct vnode *rtvp = amp->umapm_rootvp;
267 1.1 mycroft int error;
268 1.1 mycroft int flags = 0;
269 1.1 mycroft
270 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
271 1.15 christos printf("umapfs_unmount(mp = %p)\n", mp);
272 1.1 mycroft #endif
273 1.1 mycroft
274 1.18 fvdl if (mntflags & MNT_FORCE)
275 1.1 mycroft flags |= FORCECLOSE;
276 1.1 mycroft
277 1.65 pooka if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
278 1.1 mycroft return (EBUSY);
279 1.53 christos if ((error = vflush(mp, rtvp, flags)) != 0)
280 1.1 mycroft return (error);
281 1.1 mycroft
282 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
283 1.53 christos vprint("alias root of lower", rtvp);
284 1.51 perry #endif
285 1.1 mycroft /*
286 1.74 ad * Blow it away for future re-use
287 1.1 mycroft */
288 1.53 christos vgone(rtvp);
289 1.1 mycroft /*
290 1.1 mycroft * Finally, throw away the umap_mount structure
291 1.1 mycroft */
292 1.88 rmind kmem_free(amp, sizeof(struct umap_mount));
293 1.79 simonb mp->mnt_data = NULL;
294 1.88 rmind return 0;
295 1.1 mycroft }
296 1.1 mycroft
297 1.27 jdolecek extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
298 1.17 thorpej
299 1.27 jdolecek const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
300 1.17 thorpej &umapfs_vnodeop_opv_desc,
301 1.17 thorpej NULL,
302 1.17 thorpej };
303 1.17 thorpej
304 1.16 thorpej struct vfsops umapfs_vfsops = {
305 1.91 hannken .vfs_name = MOUNT_UMAP,
306 1.91 hannken .vfs_min_mount_data = sizeof (struct umap_args),
307 1.91 hannken .vfs_mount = umapfs_mount,
308 1.91 hannken .vfs_start = layerfs_start,
309 1.91 hannken .vfs_unmount = umapfs_unmount,
310 1.91 hannken .vfs_root = layerfs_root,
311 1.91 hannken .vfs_quotactl = layerfs_quotactl,
312 1.91 hannken .vfs_statvfs = layerfs_statvfs,
313 1.91 hannken .vfs_sync = layerfs_sync,
314 1.93 hannken .vfs_loadvnode = layerfs_loadvnode,
315 1.91 hannken .vfs_vget = layerfs_vget,
316 1.91 hannken .vfs_fhtovp = layerfs_fhtovp,
317 1.91 hannken .vfs_vptofh = layerfs_vptofh,
318 1.91 hannken .vfs_init = layerfs_init,
319 1.91 hannken .vfs_done = layerfs_done,
320 1.91 hannken .vfs_snapshot = layerfs_snapshot,
321 1.91 hannken .vfs_extattrctl = vfs_stdextattrctl,
322 1.96 hannken .vfs_suspendctl = layerfs_suspendctl,
323 1.91 hannken .vfs_renamelock_enter = layerfs_renamelock_enter,
324 1.91 hannken .vfs_renamelock_exit = layerfs_renamelock_exit,
325 1.91 hannken .vfs_fsync = (void *)eopnotsupp,
326 1.91 hannken .vfs_opv_descs = umapfs_vnodeopv_descs
327 1.1 mycroft };
328 1.78 rumble
329 1.102 pgoyette SYSCTL_SETUP(umapfs_sysctl_setup, "umapfs sysctl")
330 1.102 pgoyette {
331 1.102 pgoyette
332 1.102 pgoyette sysctl_createv(clog, 0, NULL, NULL,
333 1.102 pgoyette CTLFLAG_PERMANENT,
334 1.102 pgoyette CTLTYPE_NODE, "umap",
335 1.102 pgoyette SYSCTL_DESCR("UID/GID remapping file system"),
336 1.102 pgoyette NULL, 0, NULL, 0,
337 1.102 pgoyette CTL_VFS, 10, CTL_EOL);
338 1.102 pgoyette /*
339 1.102 pgoyette * XXX the "10" above could be dynamic, thereby eliminating
340 1.102 pgoyette * one more instance of the "number to vfs" mapping problem,
341 1.102 pgoyette * but "10" is the order as taken from sys/mount.h
342 1.102 pgoyette */
343 1.102 pgoyette }
344 1.102 pgoyette
345 1.78 rumble static int
346 1.84 mlelstv umap_modcmd(modcmd_t cmd, void *arg)
347 1.78 rumble {
348 1.80 rumble int error;
349 1.78 rumble
350 1.78 rumble switch (cmd) {
351 1.78 rumble case MODULE_CMD_INIT:
352 1.80 rumble error = vfs_attach(&umapfs_vfsops);
353 1.80 rumble if (error != 0)
354 1.80 rumble break;
355 1.80 rumble break;
356 1.78 rumble case MODULE_CMD_FINI:
357 1.80 rumble error = vfs_detach(&umapfs_vfsops);
358 1.80 rumble if (error != 0)
359 1.80 rumble break;
360 1.80 rumble break;
361 1.78 rumble default:
362 1.80 rumble error = ENOTTY;
363 1.80 rumble break;
364 1.78 rumble }
365 1.80 rumble
366 1.80 rumble return (error);
367 1.78 rumble }
368