advfsops.c revision 1.75 1 1.75 phx /* $NetBSD: advfsops.c,v 1.75 2015/11/18 22:06:25 phx Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 1994 Christian E. Hopps
5 1.1 jdolecek * Copyright (c) 1996 Matthias Scheler
6 1.1 jdolecek * All rights reserved.
7 1.1 jdolecek *
8 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
9 1.1 jdolecek * modification, are permitted provided that the following conditions
10 1.1 jdolecek * are met:
11 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
12 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
13 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
15 1.1 jdolecek * documentation and/or other materials provided with the distribution.
16 1.1 jdolecek * 3. All advertising materials mentioning features or use of this software
17 1.1 jdolecek * must display the following acknowledgement:
18 1.1 jdolecek * This product includes software developed by Christian E. Hopps.
19 1.1 jdolecek * 4. The name of the author may not be used to endorse or promote products
20 1.1 jdolecek * derived from this software without specific prior written permission
21 1.1 jdolecek *
22 1.1 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 jdolecek * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 jdolecek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 jdolecek * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 jdolecek * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 jdolecek * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 jdolecek * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 jdolecek * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 jdolecek * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 jdolecek * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 jdolecek */
33 1.1 jdolecek
34 1.1 jdolecek #include <sys/cdefs.h>
35 1.75 phx __KERNEL_RCSID(0, "$NetBSD: advfsops.c,v 1.75 2015/11/18 22:06:25 phx Exp $");
36 1.1 jdolecek
37 1.1 jdolecek #if defined(_KERNEL_OPT)
38 1.1 jdolecek #include "opt_compat_netbsd.h"
39 1.1 jdolecek #endif
40 1.1 jdolecek
41 1.1 jdolecek #include <sys/param.h>
42 1.1 jdolecek #include <sys/systm.h>
43 1.9 atatat #include <sys/sysctl.h>
44 1.1 jdolecek #include <sys/vnode.h>
45 1.1 jdolecek #include <sys/mount.h>
46 1.1 jdolecek #include <sys/proc.h>
47 1.1 jdolecek #include <sys/time.h>
48 1.1 jdolecek #include <sys/malloc.h>
49 1.1 jdolecek #include <sys/pool.h>
50 1.1 jdolecek #include <sys/disklabel.h>
51 1.65 mlelstv #include <sys/disk.h>
52 1.48 dholland #include <miscfs/genfs/genfs.h>
53 1.1 jdolecek #include <miscfs/specfs/specdev.h> /* XXX */
54 1.1 jdolecek #include <sys/fcntl.h>
55 1.1 jdolecek #include <sys/namei.h>
56 1.1 jdolecek #include <sys/ioctl.h>
57 1.1 jdolecek #include <sys/queue.h>
58 1.1 jdolecek #include <sys/buf.h>
59 1.1 jdolecek #include <sys/conf.h>
60 1.28 christos #include <sys/kauth.h>
61 1.51 rumble #include <sys/module.h>
62 1.1 jdolecek #include <fs/adosfs/adosfs.h>
63 1.1 jdolecek
64 1.51 rumble MODULE(MODULE_CLASS_VFS, adosfs, NULL);
65 1.51 rumble
66 1.41 pooka VFS_PROTOS(adosfs);
67 1.1 jdolecek
68 1.53 rumble static struct sysctllog *adosfs_sysctl_log;
69 1.53 rumble
70 1.54 dsl int adosfs_mountfs(struct vnode *, struct mount *, struct lwp *);
71 1.54 dsl int adosfs_loadbitmap(struct adosfsmount *);
72 1.1 jdolecek
73 1.36 pooka struct pool adosfs_node_pool;
74 1.2 thorpej
75 1.36 pooka MALLOC_JUSTDEFINE(M_ANODE, "adosfs anode","adosfs anode structures and tables");
76 1.1 jdolecek
77 1.23 yamt static const struct genfs_ops adosfs_genfsops = {
78 1.23 yamt .gop_size = genfs_size,
79 1.1 jdolecek };
80 1.1 jdolecek
81 1.54 dsl int (**adosfs_vnodeop_p)(void *);
82 1.1 jdolecek
83 1.1 jdolecek int
84 1.55 dsl adosfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
85 1.1 jdolecek {
86 1.44 pooka struct lwp *l = curlwp;
87 1.1 jdolecek struct vnode *devvp;
88 1.37 dsl struct adosfs_args *args = data;
89 1.1 jdolecek struct adosfsmount *amp;
90 1.1 jdolecek int error;
91 1.1 jdolecek mode_t accessmode;
92 1.1 jdolecek
93 1.70 maxv if (args == NULL)
94 1.70 maxv return EINVAL;
95 1.37 dsl if (*data_len < sizeof *args)
96 1.37 dsl return EINVAL;
97 1.37 dsl
98 1.1 jdolecek if (mp->mnt_flag & MNT_GETARGS) {
99 1.1 jdolecek amp = VFSTOADOSFS(mp);
100 1.1 jdolecek if (amp == NULL)
101 1.1 jdolecek return EIO;
102 1.37 dsl args->uid = amp->uid;
103 1.37 dsl args->gid = amp->gid;
104 1.37 dsl args->mask = amp->mask;
105 1.37 dsl args->fspec = NULL;
106 1.37 dsl *data_len = sizeof *args;
107 1.37 dsl return 0;
108 1.1 jdolecek }
109 1.21 perry
110 1.1 jdolecek if ((mp->mnt_flag & MNT_RDONLY) == 0)
111 1.1 jdolecek return (EROFS);
112 1.25 jmmv
113 1.37 dsl if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
114 1.25 jmmv return EOPNOTSUPP;
115 1.25 jmmv
116 1.1 jdolecek /*
117 1.1 jdolecek * Not an update, or updating the name: look up the name
118 1.1 jdolecek * and verify that it refers to a sensible block device.
119 1.1 jdolecek */
120 1.58 dholland error = namei_simple_user(args->fspec,
121 1.58 dholland NSM_FOLLOW_NOEMULROOT, &devvp);
122 1.58 dholland if (error != 0)
123 1.1 jdolecek return (error);
124 1.1 jdolecek
125 1.1 jdolecek if (devvp->v_type != VBLK) {
126 1.1 jdolecek vrele(devvp);
127 1.1 jdolecek return (ENOTBLK);
128 1.1 jdolecek }
129 1.1 jdolecek if (bdevsw_lookup(devvp->v_rdev) == NULL) {
130 1.1 jdolecek vrele(devvp);
131 1.1 jdolecek return (ENXIO);
132 1.1 jdolecek }
133 1.1 jdolecek /*
134 1.1 jdolecek * If mount by non-root, then verify that user has necessary
135 1.1 jdolecek * permissions on the device.
136 1.1 jdolecek */
137 1.57 elad accessmode = VREAD;
138 1.57 elad if ((mp->mnt_flag & MNT_RDONLY) == 0)
139 1.57 elad accessmode |= VWRITE;
140 1.57 elad vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
141 1.64 elad error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
142 1.64 elad KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
143 1.60 hannken VOP_UNLOCK(devvp);
144 1.57 elad if (error) {
145 1.57 elad vrele(devvp);
146 1.57 elad return (error);
147 1.1 jdolecek }
148 1.1 jdolecek /* MNT_UPDATE? */
149 1.26 christos if ((error = adosfs_mountfs(devvp, mp, l)) != 0) {
150 1.1 jdolecek vrele(devvp);
151 1.1 jdolecek return (error);
152 1.1 jdolecek }
153 1.1 jdolecek amp = VFSTOADOSFS(mp);
154 1.37 dsl amp->uid = args->uid;
155 1.37 dsl amp->gid = args->gid;
156 1.37 dsl amp->mask = args->mask;
157 1.37 dsl return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
158 1.38 pooka mp->mnt_op->vfs_name, mp, l);
159 1.1 jdolecek }
160 1.1 jdolecek
161 1.1 jdolecek int
162 1.55 dsl adosfs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l)
163 1.1 jdolecek {
164 1.1 jdolecek struct disklabel dl;
165 1.1 jdolecek struct partition *parp;
166 1.1 jdolecek struct adosfsmount *amp;
167 1.1 jdolecek struct buf *bp;
168 1.1 jdolecek struct vnode *rvp;
169 1.61 rmind size_t bitmap_sz = 0;
170 1.71 hannken int error;
171 1.65 mlelstv uint64_t numsecs;
172 1.65 mlelstv unsigned secsize;
173 1.65 mlelstv unsigned long secsperblk, blksperdisk, resvblks;
174 1.1 jdolecek
175 1.1 jdolecek amp = NULL;
176 1.1 jdolecek
177 1.47 ad if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
178 1.1 jdolecek return (error);
179 1.1 jdolecek
180 1.21 perry /*
181 1.65 mlelstv * open blkdev and read boot and root block
182 1.1 jdolecek */
183 1.63 hannken vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
184 1.63 hannken if ((error = VOP_OPEN(devvp, FREAD, NOCRED)) != 0) {
185 1.63 hannken VOP_UNLOCK(devvp);
186 1.1 jdolecek return (error);
187 1.63 hannken }
188 1.65 mlelstv
189 1.65 mlelstv error = getdisksize(devvp, &numsecs, &secsize);
190 1.65 mlelstv if (error)
191 1.65 mlelstv goto fail;
192 1.65 mlelstv
193 1.65 mlelstv amp = kmem_zalloc(sizeof(struct adosfsmount), KM_SLEEP);
194 1.65 mlelstv
195 1.65 mlelstv /*
196 1.65 mlelstv * compute filesystem parameters from disklabel
197 1.65 mlelstv * on arch/amiga the disklabel is computed from the native
198 1.65 mlelstv * partition tables
199 1.65 mlelstv * - p_fsize is the filesystem block size
200 1.65 mlelstv * - p_frag is the number of sectors per filesystem block
201 1.65 mlelstv * - p_cpg is the number of reserved blocks (boot blocks)
202 1.65 mlelstv * - p_psize is reduced by the number of preallocated blocks
203 1.65 mlelstv * at the end of a partition
204 1.65 mlelstv *
205 1.65 mlelstv * XXX
206 1.65 mlelstv * - bsize and secsperblk could be computed from the first sector
207 1.65 mlelstv * of the root block
208 1.65 mlelstv * - resvblks (the number of boot blocks) can only be guessed
209 1.65 mlelstv * by scanning for the root block as its position moves
210 1.65 mlelstv * with resvblks
211 1.65 mlelstv */
212 1.44 pooka error = VOP_IOCTL(devvp, DIOCGDINFO, &dl, FREAD, NOCRED);
213 1.63 hannken VOP_UNLOCK(devvp);
214 1.1 jdolecek if (error)
215 1.1 jdolecek goto fail;
216 1.65 mlelstv parp = &dl.d_partitions[DISKPART(devvp->v_rdev)];
217 1.72 christos if (dl.d_type == DKTYPE_FLOPPY) {
218 1.65 mlelstv amp->bsize = secsize;
219 1.65 mlelstv secsperblk = 1;
220 1.65 mlelstv resvblks = 2;
221 1.65 mlelstv } else if (parp->p_fsize > 0 && parp->p_frag > 0) {
222 1.1 jdolecek amp->bsize = parp->p_fsize * parp->p_frag;
223 1.65 mlelstv secsperblk = parp->p_frag;
224 1.65 mlelstv resvblks = parp->p_cpg;
225 1.65 mlelstv } else {
226 1.1 jdolecek error = EINVAL;
227 1.1 jdolecek goto fail;
228 1.1 jdolecek }
229 1.65 mlelstv blksperdisk = numsecs / secsperblk;
230 1.1 jdolecek
231 1.65 mlelstv
232 1.65 mlelstv /* The filesytem variant ('dostype') is stored in the boot block */
233 1.1 jdolecek bp = NULL;
234 1.1 jdolecek if ((error = bread(devvp, (daddr_t)BBOFF,
235 1.73 maxv amp->bsize, 0, &bp)) != 0) {
236 1.1 jdolecek goto fail;
237 1.1 jdolecek }
238 1.1 jdolecek amp->dostype = adoswordn(bp, 0);
239 1.42 ad brelse(bp, 0);
240 1.1 jdolecek
241 1.1 jdolecek /* basic sanity checks */
242 1.1 jdolecek if (amp->dostype < 0x444f5300 || amp->dostype > 0x444f5305) {
243 1.1 jdolecek error = EINVAL;
244 1.1 jdolecek goto fail;
245 1.1 jdolecek }
246 1.1 jdolecek
247 1.65 mlelstv amp->rootb = (blksperdisk - 1 + resvblks) / 2;
248 1.65 mlelstv amp->numblks = blksperdisk - resvblks;
249 1.1 jdolecek
250 1.1 jdolecek amp->nwords = amp->bsize >> 2;
251 1.1 jdolecek amp->dbsize = amp->bsize - (IS_FFS(amp) ? 0 : OFS_DATA_OFFSET);
252 1.1 jdolecek amp->devvp = devvp;
253 1.21 perry
254 1.65 mlelstv amp->mp = mp;
255 1.1 jdolecek mp->mnt_data = amp;
256 1.18 jdolecek mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)devvp->v_rdev;
257 1.18 jdolecek mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_ADOSFS);
258 1.18 jdolecek mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
259 1.19 skrll mp->mnt_stat.f_namemax = ADMAXNAMELEN;
260 1.1 jdolecek mp->mnt_fs_bshift = ffs(amp->bsize) - 1;
261 1.65 mlelstv mp->mnt_dev_bshift = DEV_BSHIFT;
262 1.1 jdolecek mp->mnt_flag |= MNT_LOCAL;
263 1.1 jdolecek
264 1.1 jdolecek /*
265 1.1 jdolecek * get the root anode, if not a valid fs this will fail.
266 1.1 jdolecek */
267 1.7 thorpej if ((error = VFS_ROOT(mp, &rvp)) != 0)
268 1.1 jdolecek goto fail;
269 1.1 jdolecek /* allocate and load bitmap, set free space */
270 1.61 rmind bitmap_sz = ((amp->numblks + 31) / 32) * sizeof(*amp->bitmap);
271 1.61 rmind amp->bitmap = kmem_alloc(bitmap_sz, KM_SLEEP);
272 1.1 jdolecek if (amp->bitmap)
273 1.1 jdolecek adosfs_loadbitmap(amp);
274 1.1 jdolecek if (mp->mnt_flag & MNT_RDONLY && amp->bitmap) {
275 1.1 jdolecek /*
276 1.1 jdolecek * Don't need the bitmap any more if it's read-only.
277 1.1 jdolecek */
278 1.61 rmind kmem_free(amp->bitmap, bitmap_sz);
279 1.1 jdolecek amp->bitmap = NULL;
280 1.1 jdolecek }
281 1.1 jdolecek vput(rvp);
282 1.1 jdolecek
283 1.1 jdolecek return(0);
284 1.1 jdolecek
285 1.1 jdolecek fail:
286 1.1 jdolecek vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
287 1.44 pooka (void) VOP_CLOSE(devvp, FREAD, NOCRED);
288 1.60 hannken VOP_UNLOCK(devvp);
289 1.1 jdolecek if (amp && amp->bitmap)
290 1.61 rmind kmem_free(amp->bitmap, bitmap_sz);
291 1.1 jdolecek if (amp)
292 1.61 rmind kmem_free(amp, sizeof(*amp));
293 1.1 jdolecek return (error);
294 1.1 jdolecek }
295 1.1 jdolecek
296 1.1 jdolecek int
297 1.55 dsl adosfs_start(struct mount *mp, int flags)
298 1.1 jdolecek {
299 1.1 jdolecek
300 1.1 jdolecek return (0);
301 1.1 jdolecek }
302 1.1 jdolecek
303 1.1 jdolecek int
304 1.55 dsl adosfs_unmount(struct mount *mp, int mntflags)
305 1.1 jdolecek {
306 1.1 jdolecek struct adosfsmount *amp;
307 1.1 jdolecek int error, flags;
308 1.1 jdolecek
309 1.1 jdolecek flags = 0;
310 1.1 jdolecek if (mntflags & MNT_FORCE)
311 1.1 jdolecek flags |= FORCECLOSE;
312 1.1 jdolecek if ((error = vflush(mp, NULLVP, flags)) != 0)
313 1.1 jdolecek return (error);
314 1.1 jdolecek amp = VFSTOADOSFS(mp);
315 1.1 jdolecek if (amp->devvp->v_type != VBAD)
316 1.67 hannken spec_node_setmountedfs(amp->devvp, NULL);
317 1.1 jdolecek vn_lock(amp->devvp, LK_EXCLUSIVE | LK_RETRY);
318 1.44 pooka error = VOP_CLOSE(amp->devvp, FREAD, NOCRED);
319 1.1 jdolecek vput(amp->devvp);
320 1.61 rmind if (amp->bitmap) {
321 1.61 rmind size_t bitmap_sz = ((amp->numblks + 31) / 32) *
322 1.61 rmind sizeof(*amp->bitmap);
323 1.61 rmind kmem_free(amp->bitmap, bitmap_sz);
324 1.61 rmind }
325 1.61 rmind kmem_free(amp, sizeof(*amp));
326 1.1 jdolecek mp->mnt_data = NULL;
327 1.1 jdolecek mp->mnt_flag &= ~MNT_LOCAL;
328 1.1 jdolecek return (error);
329 1.1 jdolecek }
330 1.1 jdolecek
331 1.1 jdolecek int
332 1.55 dsl adosfs_root(struct mount *mp, struct vnode **vpp)
333 1.1 jdolecek {
334 1.1 jdolecek struct vnode *nvp;
335 1.1 jdolecek int error;
336 1.1 jdolecek
337 1.1 jdolecek if ((error = VFS_VGET(mp, (ino_t)VFSTOADOSFS(mp)->rootb, &nvp)) != 0)
338 1.1 jdolecek return (error);
339 1.1 jdolecek /* XXX verify it's a root block? */
340 1.1 jdolecek *vpp = nvp;
341 1.1 jdolecek return (0);
342 1.1 jdolecek }
343 1.1 jdolecek
344 1.1 jdolecek int
345 1.55 dsl adosfs_statvfs(struct mount *mp, struct statvfs *sbp)
346 1.1 jdolecek {
347 1.1 jdolecek struct adosfsmount *amp;
348 1.1 jdolecek
349 1.1 jdolecek amp = VFSTOADOSFS(mp);
350 1.1 jdolecek sbp->f_bsize = amp->bsize;
351 1.12 christos sbp->f_frsize = amp->bsize;
352 1.1 jdolecek sbp->f_iosize = amp->dbsize;
353 1.1 jdolecek sbp->f_blocks = amp->numblks;
354 1.1 jdolecek sbp->f_bfree = amp->freeblks;
355 1.1 jdolecek sbp->f_bavail = amp->freeblks;
356 1.12 christos sbp->f_bresvd = 0;
357 1.1 jdolecek sbp->f_files = 0; /* who knows */
358 1.1 jdolecek sbp->f_ffree = 0; /* " " */
359 1.12 christos sbp->f_favail = 0; /* " " */
360 1.12 christos sbp->f_fresvd = 0;
361 1.12 christos copy_statvfs_info(sbp, mp);
362 1.1 jdolecek return (0);
363 1.1 jdolecek }
364 1.1 jdolecek
365 1.21 perry /*
366 1.71 hannken * lookup an anode, if not found, create
367 1.74 riastrad * return locked and referenced
368 1.1 jdolecek */
369 1.1 jdolecek int
370 1.55 dsl adosfs_vget(struct mount *mp, ino_t an, struct vnode **vpp)
371 1.1 jdolecek {
372 1.75 phx u_long block;
373 1.71 hannken int error;
374 1.71 hannken
375 1.75 phx block = an;
376 1.75 phx KASSERT(block == an);
377 1.75 phx error = vcache_get(mp, &block, sizeof(block), vpp);
378 1.71 hannken if (error)
379 1.71 hannken return error;
380 1.71 hannken error = vn_lock(*vpp, LK_EXCLUSIVE);
381 1.71 hannken if (error) {
382 1.71 hannken vrele(*vpp);
383 1.71 hannken *vpp = NULL;
384 1.71 hannken return error;
385 1.71 hannken }
386 1.71 hannken return 0;
387 1.71 hannken }
388 1.71 hannken
389 1.71 hannken /*
390 1.71 hannken * Initialize this vnode / anode pair.
391 1.71 hannken * Caller assures no other thread will try to load this inode.
392 1.71 hannken */
393 1.71 hannken int
394 1.71 hannken adosfs_loadvnode(struct mount *mp, struct vnode *vp,
395 1.71 hannken const void *key, size_t key_len, const void **new_key)
396 1.71 hannken {
397 1.1 jdolecek struct adosfsmount *amp;
398 1.1 jdolecek struct anode *ap;
399 1.1 jdolecek struct buf *bp;
400 1.75 phx u_long an;
401 1.1 jdolecek char *nam, *tmp;
402 1.1 jdolecek int namlen, error;
403 1.1 jdolecek
404 1.71 hannken KASSERT(key_len == sizeof(an));
405 1.71 hannken memcpy(&an, key, key_len);
406 1.1 jdolecek amp = VFSTOADOSFS(mp);
407 1.1 jdolecek
408 1.71 hannken if ((error = bread(amp->devvp, an * amp->bsize / DEV_BSIZE,
409 1.73 maxv amp->bsize, 0, &bp)) != 0)
410 1.71 hannken return error;
411 1.1 jdolecek
412 1.71 hannken ap = pool_get(&adosfs_node_pool, PR_WAITOK);
413 1.1 jdolecek memset(ap, 0, sizeof(struct anode));
414 1.1 jdolecek ap->vp = vp;
415 1.1 jdolecek ap->amp = amp;
416 1.1 jdolecek ap->block = an;
417 1.1 jdolecek ap->nwords = amp->nwords;
418 1.1 jdolecek
419 1.1 jdolecek /*
420 1.1 jdolecek * get type and fill rest in based on that.
421 1.1 jdolecek */
422 1.1 jdolecek switch (ap->type = adosfs_getblktype(amp, bp)) {
423 1.1 jdolecek case AROOT:
424 1.1 jdolecek vp->v_type = VDIR;
425 1.43 ad vp->v_vflag |= VV_ROOT;
426 1.1 jdolecek ap->mtimev.days = adoswordn(bp, ap->nwords - 10);
427 1.1 jdolecek ap->mtimev.mins = adoswordn(bp, ap->nwords - 9);
428 1.1 jdolecek ap->mtimev.ticks = adoswordn(bp, ap->nwords - 8);
429 1.1 jdolecek ap->created.days = adoswordn(bp, ap->nwords - 7);
430 1.1 jdolecek ap->created.mins = adoswordn(bp, ap->nwords - 6);
431 1.1 jdolecek ap->created.ticks = adoswordn(bp, ap->nwords - 5);
432 1.1 jdolecek break;
433 1.1 jdolecek case ALDIR:
434 1.1 jdolecek case ADIR:
435 1.1 jdolecek vp->v_type = VDIR;
436 1.1 jdolecek break;
437 1.1 jdolecek case ALFILE:
438 1.1 jdolecek case AFILE:
439 1.1 jdolecek vp->v_type = VREG;
440 1.1 jdolecek ap->fsize = adoswordn(bp, ap->nwords - 47);
441 1.1 jdolecek break;
442 1.1 jdolecek case ASLINK: /* XXX soft link */
443 1.1 jdolecek vp->v_type = VLNK;
444 1.1 jdolecek /*
445 1.1 jdolecek * convert from BCPL string and
446 1.1 jdolecek * from: "part:dir/file" to: "/part/dir/file"
447 1.1 jdolecek */
448 1.34 yamt nam = (char *)bp->b_data + (6 * sizeof(long));
449 1.1 jdolecek namlen = strlen(nam);
450 1.1 jdolecek tmp = nam;
451 1.1 jdolecek while (*tmp && *tmp != ':')
452 1.1 jdolecek tmp++;
453 1.1 jdolecek if (*tmp == 0) {
454 1.1 jdolecek ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
455 1.1 jdolecek memcpy(ap->slinkto, nam, namlen);
456 1.1 jdolecek } else if (*nam == ':') {
457 1.1 jdolecek ap->slinkto = malloc(namlen + 1, M_ANODE, M_WAITOK);
458 1.1 jdolecek memcpy(ap->slinkto, nam, namlen);
459 1.1 jdolecek ap->slinkto[0] = '/';
460 1.1 jdolecek } else {
461 1.1 jdolecek ap->slinkto = malloc(namlen + 2, M_ANODE, M_WAITOK);
462 1.1 jdolecek ap->slinkto[0] = '/';
463 1.1 jdolecek memcpy(&ap->slinkto[1], nam, namlen);
464 1.1 jdolecek ap->slinkto[tmp - nam + 1] = '/';
465 1.1 jdolecek namlen++;
466 1.1 jdolecek }
467 1.1 jdolecek ap->slinkto[namlen] = 0;
468 1.1 jdolecek ap->fsize = namlen;
469 1.1 jdolecek break;
470 1.1 jdolecek default:
471 1.71 hannken error = EINVAL;
472 1.71 hannken goto bad;
473 1.1 jdolecek }
474 1.1 jdolecek
475 1.1 jdolecek /*
476 1.1 jdolecek * Get appropriate data from this block; hard link needs
477 1.1 jdolecek * to get other data from the "real" block.
478 1.1 jdolecek */
479 1.1 jdolecek
480 1.1 jdolecek /*
481 1.1 jdolecek * copy in name (from original block)
482 1.1 jdolecek */
483 1.34 yamt nam = (char *)bp->b_data + (ap->nwords - 20) * sizeof(u_int32_t);
484 1.1 jdolecek namlen = *(u_char *)nam++;
485 1.1 jdolecek if (namlen > 30) {
486 1.1 jdolecek #ifdef DIAGNOSTIC
487 1.24 christos printf("adosfs: aget: name length too long blk %llu\n",
488 1.24 christos (unsigned long long)an);
489 1.1 jdolecek #endif
490 1.71 hannken error = EINVAL;
491 1.71 hannken goto bad;
492 1.1 jdolecek }
493 1.1 jdolecek memcpy(ap->name, nam, namlen);
494 1.1 jdolecek ap->name[namlen] = 0;
495 1.1 jdolecek
496 1.21 perry /*
497 1.21 perry * if dir alloc hash table and copy it in
498 1.1 jdolecek */
499 1.1 jdolecek if (vp->v_type == VDIR) {
500 1.1 jdolecek int i;
501 1.1 jdolecek
502 1.1 jdolecek ap->tab = malloc(ANODETABSZ(ap) * 2, M_ANODE, M_WAITOK);
503 1.1 jdolecek ap->ntabent = ANODETABENT(ap);
504 1.1 jdolecek ap->tabi = (int *)&ap->tab[ap->ntabent];
505 1.1 jdolecek memset(ap->tabi, 0, ANODETABSZ(ap));
506 1.1 jdolecek for (i = 0; i < ap->ntabent; i++)
507 1.1 jdolecek ap->tab[i] = adoswordn(bp, i + 6);
508 1.1 jdolecek }
509 1.1 jdolecek
510 1.1 jdolecek /*
511 1.1 jdolecek * misc.
512 1.1 jdolecek */
513 1.1 jdolecek ap->pblock = adoswordn(bp, ap->nwords - 3);
514 1.1 jdolecek ap->hashf = adoswordn(bp, ap->nwords - 4);
515 1.1 jdolecek ap->linknext = adoswordn(bp, ap->nwords - 10);
516 1.1 jdolecek ap->linkto = adoswordn(bp, ap->nwords - 11);
517 1.1 jdolecek
518 1.1 jdolecek /*
519 1.1 jdolecek * setup last indirect block cache.
520 1.1 jdolecek */
521 1.1 jdolecek ap->lastlindblk = 0;
522 1.1 jdolecek if (ap->type == AFILE) {
523 1.1 jdolecek ap->lastindblk = ap->block;
524 1.1 jdolecek if (adoswordn(bp, ap->nwords - 10))
525 1.1 jdolecek ap->linkto = ap->block;
526 1.1 jdolecek } else if (ap->type == ALFILE) {
527 1.1 jdolecek ap->lastindblk = ap->linkto;
528 1.42 ad brelse(bp, 0);
529 1.1 jdolecek bp = NULL;
530 1.1 jdolecek error = bread(amp->devvp, ap->linkto * amp->bsize / DEV_BSIZE,
531 1.73 maxv amp->bsize, 0, &bp);
532 1.71 hannken if (error)
533 1.71 hannken goto bad;
534 1.1 jdolecek ap->fsize = adoswordn(bp, ap->nwords - 47);
535 1.1 jdolecek }
536 1.1 jdolecek
537 1.1 jdolecek if (ap->type == AROOT) {
538 1.1 jdolecek ap->adprot = 15;
539 1.1 jdolecek ap->uid = amp->uid;
540 1.1 jdolecek ap->gid = amp->gid;
541 1.1 jdolecek } else {
542 1.1 jdolecek ap->adprot = adoswordn(bp, ap->nwords - 48) ^ 15;
543 1.1 jdolecek /*
544 1.1 jdolecek * ADOS directories do not have a `x' protection bit as
545 1.1 jdolecek * it is known in VFS; this functionality is fulfilled
546 1.1 jdolecek * by the ADOS `r' bit.
547 1.1 jdolecek *
548 1.1 jdolecek * To retain the ADOS behaviour, fake execute permissions
549 1.1 jdolecek * in that case.
550 1.1 jdolecek */
551 1.1 jdolecek if ((ap->type == ADIR || ap->type == ALDIR) &&
552 1.1 jdolecek (ap->adprot & 0x00000008) == 0)
553 1.1 jdolecek ap->adprot &= ~0x00000002;
554 1.1 jdolecek
555 1.1 jdolecek /*
556 1.1 jdolecek * Get uid/gid from extensions in file header
557 1.1 jdolecek * (really need to know if this is a muFS partition)
558 1.1 jdolecek */
559 1.1 jdolecek ap->uid = (adoswordn(bp, ap->nwords - 49) >> 16) & 0xffff;
560 1.1 jdolecek ap->gid = adoswordn(bp, ap->nwords - 49) & 0xffff;
561 1.1 jdolecek if (ap->uid || ap->gid) {
562 1.1 jdolecek if (ap->uid == 0xffff)
563 1.1 jdolecek ap->uid = 0;
564 1.1 jdolecek if (ap->gid == 0xffff)
565 1.1 jdolecek ap->gid = 0;
566 1.1 jdolecek ap->adprot |= 0x40000000; /* Kludge */
567 1.1 jdolecek }
568 1.1 jdolecek else {
569 1.1 jdolecek /*
570 1.1 jdolecek * uid & gid extension don't exist,
571 1.1 jdolecek * so use the mount-point uid/gid
572 1.1 jdolecek */
573 1.1 jdolecek ap->uid = amp->uid;
574 1.1 jdolecek ap->gid = amp->gid;
575 1.1 jdolecek }
576 1.1 jdolecek }
577 1.1 jdolecek ap->mtime.days = adoswordn(bp, ap->nwords - 23);
578 1.1 jdolecek ap->mtime.mins = adoswordn(bp, ap->nwords - 22);
579 1.1 jdolecek ap->mtime.ticks = adoswordn(bp, ap->nwords - 21);
580 1.1 jdolecek
581 1.42 ad brelse(bp, 0);
582 1.71 hannken vp->v_tag = VT_ADOSFS;
583 1.71 hannken vp->v_op = adosfs_vnodeop_p;
584 1.71 hannken vp->v_data = ap;
585 1.71 hannken genfs_node_init(vp, &adosfs_genfsops);
586 1.39 pooka uvm_vnp_setsize(vp, ap->fsize);
587 1.71 hannken *new_key = &ap->block;
588 1.71 hannken return 0;
589 1.71 hannken
590 1.71 hannken bad:
591 1.71 hannken if (bp)
592 1.71 hannken brelse(bp, 0);
593 1.71 hannken pool_put(&adosfs_node_pool, ap);
594 1.71 hannken return error;
595 1.1 jdolecek }
596 1.1 jdolecek
597 1.1 jdolecek /*
598 1.1 jdolecek * Load the bitmap into memory, and count the number of available
599 1.1 jdolecek * blocks.
600 1.1 jdolecek * The bitmap will be released if the filesystem is read-only; it's
601 1.1 jdolecek * only needed to find the free space.
602 1.1 jdolecek */
603 1.1 jdolecek int
604 1.55 dsl adosfs_loadbitmap(struct adosfsmount *amp)
605 1.1 jdolecek {
606 1.1 jdolecek struct buf *bp, *mapbp;
607 1.1 jdolecek u_long bn;
608 1.1 jdolecek int blkix, endix, mapix;
609 1.1 jdolecek int bmsize;
610 1.1 jdolecek int error;
611 1.1 jdolecek
612 1.1 jdolecek bp = mapbp = NULL;
613 1.1 jdolecek bn = amp->rootb;
614 1.1 jdolecek if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE, amp->bsize,
615 1.73 maxv 0, &bp)) != 0) {
616 1.1 jdolecek return (error);
617 1.1 jdolecek }
618 1.1 jdolecek blkix = amp->nwords - 49;
619 1.1 jdolecek endix = amp->nwords - 24;
620 1.1 jdolecek mapix = 0;
621 1.1 jdolecek bmsize = (amp->numblks + 31) / 32;
622 1.1 jdolecek while (mapix < bmsize) {
623 1.1 jdolecek int n;
624 1.1 jdolecek u_long bits;
625 1.1 jdolecek
626 1.1 jdolecek if (adoswordn(bp, blkix) == 0)
627 1.1 jdolecek break;
628 1.1 jdolecek if (mapbp != NULL)
629 1.42 ad brelse(mapbp, 0);
630 1.1 jdolecek if ((error = bread(amp->devvp,
631 1.1 jdolecek adoswordn(bp, blkix) * amp->bsize / DEV_BSIZE, amp->bsize,
632 1.73 maxv 0, &mapbp)) != 0)
633 1.1 jdolecek break;
634 1.1 jdolecek if (adoscksum(mapbp, amp->nwords)) {
635 1.1 jdolecek #ifdef DIAGNOSTIC
636 1.1 jdolecek printf("adosfs: loadbitmap - cksum of blk %d failed\n",
637 1.1 jdolecek adoswordn(bp, blkix));
638 1.1 jdolecek #endif
639 1.1 jdolecek /* XXX Force read-only? Set free space 0? */
640 1.1 jdolecek break;
641 1.1 jdolecek }
642 1.1 jdolecek n = 1;
643 1.1 jdolecek while (n < amp->nwords && mapix < bmsize) {
644 1.1 jdolecek amp->bitmap[mapix++] = bits = adoswordn(mapbp, n);
645 1.1 jdolecek ++n;
646 1.1 jdolecek if (mapix == bmsize && amp->numblks & 31)
647 1.1 jdolecek bits &= ~(0xffffffff << (amp->numblks & 31));
648 1.1 jdolecek while (bits) {
649 1.1 jdolecek if (bits & 1)
650 1.1 jdolecek ++amp->freeblks;
651 1.1 jdolecek bits >>= 1;
652 1.1 jdolecek }
653 1.1 jdolecek }
654 1.1 jdolecek ++blkix;
655 1.1 jdolecek if (mapix < bmsize && blkix == endix) {
656 1.1 jdolecek bn = adoswordn(bp, blkix);
657 1.42 ad brelse(bp, 0);
658 1.1 jdolecek if ((error = bread(amp->devvp, bn * amp->bsize / DEV_BSIZE,
659 1.73 maxv amp->bsize, 0, &bp)) != 0)
660 1.1 jdolecek break;
661 1.1 jdolecek /*
662 1.1 jdolecek * Why is there no checksum on these blocks?
663 1.1 jdolecek */
664 1.1 jdolecek blkix = 0;
665 1.1 jdolecek endix = amp->nwords - 1;
666 1.1 jdolecek }
667 1.1 jdolecek }
668 1.1 jdolecek if (bp)
669 1.42 ad brelse(bp, 0);
670 1.1 jdolecek if (mapbp)
671 1.42 ad brelse(mapbp, 0);
672 1.1 jdolecek return (error);
673 1.1 jdolecek }
674 1.1 jdolecek
675 1.1 jdolecek
676 1.1 jdolecek /*
677 1.1 jdolecek * File handle to vnode
678 1.1 jdolecek *
679 1.1 jdolecek * Have to be really careful about stale file handles:
680 1.1 jdolecek * - check that the inode number is in range
681 1.1 jdolecek * - call iget() to get the locked inode
682 1.1 jdolecek * - check for an unallocated inode (i_mode == 0)
683 1.1 jdolecek * - check that the generation number matches
684 1.1 jdolecek */
685 1.1 jdolecek
686 1.1 jdolecek struct ifid {
687 1.1 jdolecek ushort ifid_len;
688 1.1 jdolecek ushort ifid_pad;
689 1.1 jdolecek int ifid_ino;
690 1.1 jdolecek long ifid_start;
691 1.1 jdolecek };
692 1.1 jdolecek
693 1.1 jdolecek int
694 1.55 dsl adosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
695 1.1 jdolecek {
696 1.29 martin struct ifid ifh;
697 1.1 jdolecek #if 0
698 1.1 jdolecek struct anode *ap;
699 1.1 jdolecek #endif
700 1.1 jdolecek struct vnode *nvp;
701 1.1 jdolecek int error;
702 1.1 jdolecek
703 1.29 martin if (fhp->fid_len != sizeof(struct ifid))
704 1.29 martin return EINVAL;
705 1.29 martin
706 1.1 jdolecek #ifdef ADOSFS_DIAGNOSTIC
707 1.59 phx printf("adfhtovp(%p, %p, %p)\n", mp, fhp, vpp);
708 1.1 jdolecek #endif
709 1.21 perry
710 1.29 martin memcpy(&ifh, fhp, sizeof(ifh));
711 1.29 martin
712 1.29 martin if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
713 1.1 jdolecek *vpp = NULLVP;
714 1.1 jdolecek return (error);
715 1.1 jdolecek }
716 1.1 jdolecek #if 0
717 1.1 jdolecek ap = VTOA(nvp);
718 1.1 jdolecek if (ap->inode.iso_mode == 0) {
719 1.1 jdolecek vput(nvp);
720 1.1 jdolecek *vpp = NULLVP;
721 1.1 jdolecek return (ESTALE);
722 1.1 jdolecek }
723 1.1 jdolecek #endif
724 1.1 jdolecek *vpp = nvp;
725 1.1 jdolecek return(0);
726 1.1 jdolecek }
727 1.1 jdolecek
728 1.1 jdolecek int
729 1.55 dsl adosfs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
730 1.1 jdolecek {
731 1.1 jdolecek struct anode *ap = VTOA(vp);
732 1.29 martin struct ifid ifh;
733 1.21 perry
734 1.29 martin if (*fh_size < sizeof(struct ifid)) {
735 1.29 martin *fh_size = sizeof(struct ifid);
736 1.29 martin return E2BIG;
737 1.29 martin }
738 1.29 martin *fh_size = sizeof(struct ifid);
739 1.29 martin
740 1.29 martin memset(&ifh, 0, sizeof(ifh));
741 1.29 martin ifh.ifid_len = sizeof(struct ifid);
742 1.29 martin ifh.ifid_ino = ap->block;
743 1.29 martin ifh.ifid_start = ap->block;
744 1.29 martin memcpy(fhp, &ifh, sizeof(ifh));
745 1.21 perry
746 1.1 jdolecek #ifdef ADOSFS_DIAGNOSTIC
747 1.59 phx printf("advptofh(%p, %p)\n", vp, fhp);
748 1.1 jdolecek #endif
749 1.1 jdolecek return(0);
750 1.1 jdolecek }
751 1.1 jdolecek
752 1.1 jdolecek int
753 1.55 dsl adosfs_sync(struct mount *mp, int waitfor, kauth_cred_t uc)
754 1.1 jdolecek {
755 1.1 jdolecek #ifdef ADOSFS_DIAGNOSTIC
756 1.59 phx printf("ad_sync(%p, %d)\n", mp, waitfor);
757 1.1 jdolecek #endif
758 1.1 jdolecek return(0);
759 1.1 jdolecek }
760 1.1 jdolecek
761 1.1 jdolecek void
762 1.56 cegger adosfs_init(void)
763 1.1 jdolecek {
764 1.36 pooka
765 1.11 atatat malloc_type_attach(M_ANODE);
766 1.15 atatat pool_init(&adosfs_node_pool, sizeof(struct anode), 0, 0, 0, "adosndpl",
767 1.35 ad &pool_allocator_nointr, IPL_NONE);
768 1.1 jdolecek }
769 1.1 jdolecek
770 1.1 jdolecek void
771 1.56 cegger adosfs_done(void)
772 1.1 jdolecek {
773 1.36 pooka
774 1.1 jdolecek pool_destroy(&adosfs_node_pool);
775 1.11 atatat malloc_type_detach(M_ANODE);
776 1.1 jdolecek }
777 1.1 jdolecek
778 1.1 jdolecek /*
779 1.1 jdolecek * vfs generic function call table
780 1.1 jdolecek */
781 1.1 jdolecek
782 1.21 perry extern const struct vnodeopv_desc adosfs_vnodeop_opv_desc;
783 1.1 jdolecek
784 1.1 jdolecek const struct vnodeopv_desc *adosfs_vnodeopv_descs[] = {
785 1.1 jdolecek &adosfs_vnodeop_opv_desc,
786 1.1 jdolecek NULL,
787 1.1 jdolecek };
788 1.1 jdolecek
789 1.1 jdolecek struct vfsops adosfs_vfsops = {
790 1.69 hannken .vfs_name = MOUNT_ADOSFS,
791 1.69 hannken .vfs_min_mount_data = sizeof (struct adosfs_args),
792 1.69 hannken .vfs_mount = adosfs_mount,
793 1.69 hannken .vfs_start = adosfs_start,
794 1.69 hannken .vfs_unmount = adosfs_unmount,
795 1.69 hannken .vfs_root = adosfs_root,
796 1.69 hannken .vfs_quotactl = (void *)eopnotsupp,
797 1.69 hannken .vfs_statvfs = adosfs_statvfs,
798 1.69 hannken .vfs_sync = adosfs_sync,
799 1.69 hannken .vfs_vget = adosfs_vget,
800 1.71 hannken .vfs_loadvnode = adosfs_loadvnode,
801 1.69 hannken .vfs_fhtovp = adosfs_fhtovp,
802 1.69 hannken .vfs_vptofh = adosfs_vptofh,
803 1.69 hannken .vfs_init = adosfs_init,
804 1.69 hannken .vfs_done = adosfs_done,
805 1.69 hannken .vfs_snapshot = (void *)eopnotsupp,
806 1.69 hannken .vfs_extattrctl = vfs_stdextattrctl,
807 1.69 hannken .vfs_suspendctl = (void *)eopnotsupp,
808 1.69 hannken .vfs_renamelock_enter = genfs_renamelock_enter,
809 1.69 hannken .vfs_renamelock_exit = genfs_renamelock_exit,
810 1.69 hannken .vfs_fsync = (void *)eopnotsupp,
811 1.69 hannken .vfs_opv_descs = adosfs_vnodeopv_descs
812 1.1 jdolecek };
813 1.51 rumble
814 1.51 rumble static int
815 1.51 rumble adosfs_modcmd(modcmd_t cmd, void *arg)
816 1.51 rumble {
817 1.53 rumble int error;
818 1.51 rumble
819 1.51 rumble switch (cmd) {
820 1.51 rumble case MODULE_CMD_INIT:
821 1.53 rumble error = vfs_attach(&adosfs_vfsops);
822 1.53 rumble if (error != 0)
823 1.53 rumble break;
824 1.53 rumble sysctl_createv(&adosfs_sysctl_log, 0, NULL, NULL,
825 1.53 rumble CTLFLAG_PERMANENT,
826 1.53 rumble CTLTYPE_NODE, "adosfs",
827 1.53 rumble SYSCTL_DESCR("AmigaDOS file system"),
828 1.53 rumble NULL, 0, NULL, 0,
829 1.53 rumble CTL_VFS, 16, CTL_EOL);
830 1.53 rumble /*
831 1.53 rumble * XXX the "16" above could be dynamic, thereby eliminating
832 1.53 rumble * one more instance of the "number to vfs" mapping problem,
833 1.53 rumble * but "16" is the order as taken from sys/mount.h
834 1.53 rumble */
835 1.53 rumble break;
836 1.51 rumble case MODULE_CMD_FINI:
837 1.53 rumble error = vfs_detach(&adosfs_vfsops);
838 1.53 rumble if (error != 0)
839 1.53 rumble break;
840 1.53 rumble sysctl_teardown(&adosfs_sysctl_log);
841 1.53 rumble break;
842 1.51 rumble default:
843 1.53 rumble error = ENOTTY;
844 1.53 rumble break;
845 1.51 rumble }
846 1.53 rumble
847 1.53 rumble return (error);
848 1.51 rumble }
849