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