1 /* $NetBSD: advfsops.c,v 1.58 2009/06/29 05:08:17 dholland Exp $ */
4 * Copyright (c) 1994 Christian E. Hopps
5 * Copyright (c) 1996 Matthias Scheler
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Christian E. Hopps.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: advfsops.c,v 1.58 2009/06/29 05:08:17 dholland Exp $");
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <sys/vnode.h>
45 #include <sys/mount.h>
48 #include <sys/malloc.h>
50 #include <sys/disklabel.h>
51 #include <miscfs/genfs/genfs.h>
52 #include <miscfs/specfs/specdev.h> /* XXX */
53 #include <sys/fcntl.h>
54 #include <sys/namei.h>
55 #include <sys/ioctl.h>
56 #include <sys/queue.h>
59 #include <sys/kauth.h>
60 #include <sys/simplelock.h>
61 #include <sys/module.h>
62 #include <fs/adosfs/adosfs.h>
64 MODULE(MODULE_CLASS_VFS
, adosfs
, NULL
);
68 static struct sysctllog
*adosfs_sysctl_log
;
70 int adosfs_mountfs(struct vnode
*, struct mount
*, struct lwp
*);
71 int adosfs_loadbitmap(struct adosfsmount
*);
73 struct simplelock adosfs_hashlock
;
75 struct pool adosfs_node_pool
;
77 MALLOC_JUSTDEFINE(M_ADOSFSMNT
, "adosfs mount", "adosfs mount structures");
78 MALLOC_JUSTDEFINE(M_ANODE
, "adosfs anode","adosfs anode structures and tables");
79 MALLOC_JUSTDEFINE(M_ADOSFSBITMAP
, "adosfs bitmap", "adosfs bitmap");
81 static const struct genfs_ops adosfs_genfsops
= {
82 .gop_size
= genfs_size
,
85 int (**adosfs_vnodeop_p
)(void *);
88 adosfs_mount(struct mount
*mp
, const char *path
, void *data
, size_t *data_len
)
90 struct lwp
*l
= curlwp
;
92 struct adosfs_args
*args
= data
;
93 struct adosfsmount
*amp
;
97 if (*data_len
< sizeof *args
)
100 if (mp
->mnt_flag
& MNT_GETARGS
) {
101 amp
= VFSTOADOSFS(mp
);
104 args
->uid
= amp
->uid
;
105 args
->gid
= amp
->gid
;
106 args
->mask
= amp
->mask
;
108 *data_len
= sizeof *args
;
112 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
115 if ((mp
->mnt_flag
& MNT_UPDATE
) && args
->fspec
== NULL
)
119 * Not an update, or updating the name: look up the name
120 * and verify that it refers to a sensible block device.
122 error
= namei_simple_user(args
->fspec
,
123 NSM_FOLLOW_NOEMULROOT
, &devvp
);
127 if (devvp
->v_type
!= VBLK
) {
131 if (bdevsw_lookup(devvp
->v_rdev
) == NULL
) {
136 * If mount by non-root, then verify that user has necessary
137 * permissions on the device.
140 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
141 accessmode
|= VWRITE
;
142 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
143 error
= genfs_can_mount(devvp
, accessmode
, l
->l_cred
);
144 VOP_UNLOCK(devvp
, 0);
150 if ((error
= adosfs_mountfs(devvp
, mp
, l
)) != 0) {
154 amp
= VFSTOADOSFS(mp
);
155 amp
->uid
= args
->uid
;
156 amp
->gid
= args
->gid
;
157 amp
->mask
= args
->mask
;
158 return set_statvfs_info(path
, UIO_USERSPACE
, args
->fspec
, UIO_USERSPACE
,
159 mp
->mnt_op
->vfs_name
, mp
, l
);
163 adosfs_mountfs(struct vnode
*devvp
, struct mount
*mp
, struct lwp
*l
)
166 struct partition
*parp
;
167 struct adosfsmount
*amp
;
172 part
= DISKPART(devvp
->v_rdev
);
175 if ((error
= vinvalbuf(devvp
, V_SAVE
, l
->l_cred
, l
, 0, 0)) != 0)
179 * open blkdev and read root block
181 if ((error
= VOP_OPEN(devvp
, FREAD
, NOCRED
)) != 0)
183 error
= VOP_IOCTL(devvp
, DIOCGDINFO
, &dl
, FREAD
, NOCRED
);
187 parp
= &dl
.d_partitions
[part
];
188 amp
= malloc(sizeof(struct adosfsmount
), M_ADOSFSMNT
, M_WAITOK
);
189 memset((char *)amp
, 0, (u_long
)sizeof(struct adosfsmount
));
191 if (dl
.d_type
== DTYPE_FLOPPY
) {
192 amp
->bsize
= dl
.d_secsize
;
196 amp
->bsize
= parp
->p_fsize
* parp
->p_frag
;
197 amp
->secsperblk
= parp
->p_frag
;
201 if (amp
->secsperblk
== 0) {
207 if ((error
= bread(devvp
, (daddr_t
)BBOFF
,
208 amp
->bsize
, NOCRED
, 0, &bp
)) != 0) {
212 amp
->dostype
= adoswordn(bp
, 0);
215 /* basic sanity checks */
216 if (amp
->dostype
< 0x444f5300 || amp
->dostype
> 0x444f5305) {
221 amp
->rootb
= (parp
->p_size
/ amp
->secsperblk
- 1 + parp
->p_cpg
) >> 1;
222 amp
->numblks
= parp
->p_size
/ amp
->secsperblk
- parp
->p_cpg
;
224 amp
->nwords
= amp
->bsize
>> 2;
225 amp
->dbsize
= amp
->bsize
- (IS_FFS(amp
) ? 0 : OFS_DATA_OFFSET
);
229 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] = (long)devvp
->v_rdev
;
230 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1] = makefstype(MOUNT_ADOSFS
);
231 mp
->mnt_stat
.f_fsid
= mp
->mnt_stat
.f_fsidx
.__fsid_val
[0];
232 mp
->mnt_stat
.f_namemax
= ADMAXNAMELEN
;
233 mp
->mnt_fs_bshift
= ffs(amp
->bsize
) - 1;
234 mp
->mnt_dev_bshift
= DEV_BSHIFT
; /* XXX */
235 mp
->mnt_flag
|= MNT_LOCAL
;
240 for (i
= 0; i
< ANODEHASHSZ
; i
++)
241 LIST_INIT(&
->anodetab
[i
]);
244 * get the root anode, if not a valid fs this will fail.
246 if ((error
= VFS_ROOT(mp
, &rvp
)) != 0)
248 /* allocate and load bitmap, set free space */
249 amp
->bitmap
= malloc(((amp
->numblks
+ 31) / 32) * sizeof(*amp
->bitmap
),
250 M_ADOSFSBITMAP
, M_WAITOK
);
252 adosfs_loadbitmap(amp
);
253 if (mp
->mnt_flag
& MNT_RDONLY
&& amp
->bitmap
) {
255 * Don't need the bitmap any more if it's read-only.
257 free(amp
->bitmap
, M_ADOSFSBITMAP
);
265 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
266 (void) VOP_CLOSE(devvp
, FREAD
, NOCRED
);
267 VOP_UNLOCK(devvp
, 0);
268 if (amp
&& amp
->bitmap
)
269 free(amp
->bitmap
, M_ADOSFSBITMAP
);
271 free(amp
, M_ADOSFSMNT
);
276 adosfs_start(struct mount
*mp
, int flags
)
283 adosfs_unmount(struct mount
*mp
, int mntflags
)
285 struct adosfsmount
*amp
;
289 if (mntflags
& MNT_FORCE
)
291 if ((error
= vflush(mp
, NULLVP
, flags
)) != 0)
293 amp
= VFSTOADOSFS(mp
);
294 if (amp
->devvp
->v_type
!= VBAD
)
295 amp
->devvp
->v_specmountpoint
= NULL
;
296 vn_lock(amp
->devvp
, LK_EXCLUSIVE
| LK_RETRY
);
297 error
= VOP_CLOSE(amp
->devvp
, FREAD
, NOCRED
);
300 free(amp
->bitmap
, M_ADOSFSBITMAP
);
301 free(amp
, M_ADOSFSMNT
);
303 mp
->mnt_flag
&= ~MNT_LOCAL
;
308 adosfs_root(struct mount
*mp
, struct vnode
**vpp
)
313 if ((error
= VFS_VGET(mp
, (ino_t
)VFSTOADOSFS(mp
)->rootb
, &nvp
)) != 0)
315 /* XXX verify it's a root block? */
321 adosfs_statvfs(struct mount
*mp
, struct statvfs
*sbp
)
323 struct adosfsmount
*amp
;
325 amp
= VFSTOADOSFS(mp
);
326 sbp
->f_bsize
= amp
->bsize
;
327 sbp
->f_frsize
= amp
->bsize
;
328 sbp
->f_iosize
= amp
->dbsize
;
329 sbp
->f_blocks
= amp
->numblks
;
330 sbp
->f_bfree
= amp
->freeblks
;
331 sbp
->f_bavail
= amp
->freeblks
;
333 sbp
->f_files
= 0; /* who knows */
334 sbp
->f_ffree
= 0; /* " " */
335 sbp
->f_favail
= 0; /* " " */
337 copy_statvfs_info(sbp
, mp
);
342 * lookup an anode, check mount's hash table if not found, create
343 * return locked and referenced al la vget(vp, 1);
346 adosfs_vget(struct mount
*mp
, ino_t an
, struct vnode
**vpp
)
348 struct adosfsmount
*amp
;
356 amp
= VFSTOADOSFS(mp
);
360 * check hash table. we are done if found
362 if ((*vpp
= adosfs_ahashget(mp
, an
)) != NULL
)
365 error
= getnewvnode(VT_ADOSFS
, mp
, adosfs_vnodeop_p
, &vp
);
370 * setup, insert in hash, and lock before io.
372 vp
->v_data
= ap
= pool_get(&adosfs_node_pool
, PR_WAITOK
);
373 memset(ap
, 0, sizeof(struct anode
));
377 ap
->nwords
= amp
->nwords
;
378 genfs_node_init(vp
, &adosfs_genfsops
);
379 adosfs_ainshash(amp
, ap
);
381 if ((error
= bread(amp
->devvp
, an
* amp
->bsize
/ DEV_BSIZE
,
382 amp
->bsize
, NOCRED
, 0, &bp
)) != 0) {
389 * get type and fill rest in based on that.
391 switch (ap
->type
= adosfs_getblktype(amp
, bp
)) {
394 vp
->v_vflag
|= VV_ROOT
;
395 ap
->mtimev
.days
= adoswordn(bp
, ap
->nwords
- 10);
396 ap
->mtimev
.mins
= adoswordn(bp
, ap
->nwords
- 9);
397 ap
->mtimev
.ticks
= adoswordn(bp
, ap
->nwords
- 8);
398 ap
->created
.days
= adoswordn(bp
, ap
->nwords
- 7);
399 ap
->created
.mins
= adoswordn(bp
, ap
->nwords
- 6);
400 ap
->created
.ticks
= adoswordn(bp
, ap
->nwords
- 5);
409 ap
->fsize
= adoswordn(bp
, ap
->nwords
- 47);
411 case ASLINK
: /* XXX soft link */
414 * convert from BCPL string and
415 * from: "part:dir/file" to: "/part/dir/file"
417 nam
= (char *)bp
->b_data
+ (6 * sizeof(long));
418 namlen
= strlen(nam
);
420 while (*tmp
&& *tmp
!= ':')
423 ap
->slinkto
= malloc(namlen
+ 1, M_ANODE
, M_WAITOK
);
424 memcpy(ap
->slinkto
, nam
, namlen
);
425 } else if (*nam
== ':') {
426 ap
->slinkto
= malloc(namlen
+ 1, M_ANODE
, M_WAITOK
);
427 memcpy(ap
->slinkto
, nam
, namlen
);
428 ap
->slinkto
[0] = '/';
430 ap
->slinkto
= malloc(namlen
+ 2, M_ANODE
, M_WAITOK
);
431 ap
->slinkto
[0] = '/';
432 memcpy(&ap
->slinkto
[1], nam
, namlen
);
433 ap
->slinkto
[tmp
- nam
+ 1] = '/';
436 ap
->slinkto
[namlen
] = 0;
446 * Get appropriate data from this block; hard link needs
447 * to get other data from the "real" block.
451 * copy in name (from original block)
453 nam
= (char *)bp
->b_data
+ (ap
->nwords
- 20) * sizeof(u_int32_t
);
454 namlen
= *(u_char
*)nam
++;
457 printf("adosfs: aget: name length too long blk %llu\n",
458 (unsigned long long)an
);
464 memcpy(ap
->name
, nam
, namlen
);
465 ap
->name
[namlen
] = 0;
468 * if dir alloc hash table and copy it in
470 if (vp
->v_type
== VDIR
) {
473 ap
->tab
= malloc(ANODETABSZ(ap
) * 2, M_ANODE
, M_WAITOK
);
474 ap
->ntabent
= ANODETABENT(ap
);
475 ap
->tabi
= (int *)&ap
->tab
[ap
->ntabent
];
476 memset(ap
->tabi
, 0, ANODETABSZ(ap
));
477 for (i
= 0; i
< ap
->ntabent
; i
++)
478 ap
->tab
[i
] = adoswordn(bp
, i
+ 6);
484 ap
->pblock
= adoswordn(bp
, ap
->nwords
- 3);
485 ap
->hashf
= adoswordn(bp
, ap
->nwords
- 4);
486 ap
->linknext
= adoswordn(bp
, ap
->nwords
- 10);
487 ap
->linkto
= adoswordn(bp
, ap
->nwords
- 11);
490 * setup last indirect block cache.
493 if (ap
->type
== AFILE
) {
494 ap
->lastindblk
= ap
->block
;
495 if (adoswordn(bp
, ap
->nwords
- 10))
496 ap
->linkto
= ap
->block
;
497 } else if (ap
->type
== ALFILE
) {
498 ap
->lastindblk
= ap
->linkto
;
501 error
= bread(amp
->devvp
, ap
->linkto
* amp
->bsize
/ DEV_BSIZE
,
502 amp
->bsize
, NOCRED
, 0, &bp
);
508 ap
->fsize
= adoswordn(bp
, ap
->nwords
- 47);
510 * Should ap->block be set to the real file header block?
512 ap
->block
= ap
->linkto
;
515 if (ap
->type
== AROOT
) {
520 ap
->adprot
= adoswordn(bp
, ap
->nwords
- 48) ^ 15;
522 * ADOS directories do not have a `x' protection bit as
523 * it is known in VFS; this functionality is fulfilled
524 * by the ADOS `r' bit.
526 * To retain the ADOS behaviour, fake execute permissions
529 if ((ap
->type
== ADIR
|| ap
->type
== ALDIR
) &&
530 (ap
->adprot
& 0x00000008) == 0)
531 ap
->adprot
&= ~0x00000002;
534 * Get uid/gid from extensions in file header
535 * (really need to know if this is a muFS partition)
537 ap
->uid
= (adoswordn(bp
, ap
->nwords
- 49) >> 16) & 0xffff;
538 ap
->gid
= adoswordn(bp
, ap
->nwords
- 49) & 0xffff;
539 if (ap
->uid
|| ap
->gid
) {
540 if (ap
->uid
== 0xffff)
542 if (ap
->gid
== 0xffff)
544 ap
->adprot
|= 0x40000000; /* Kludge */
548 * uid & gid extension don't exist,
549 * so use the mount-point uid/gid
555 ap
->mtime
.days
= adoswordn(bp
, ap
->nwords
- 23);
556 ap
->mtime
.mins
= adoswordn(bp
, ap
->nwords
- 22);
557 ap
->mtime
.ticks
= adoswordn(bp
, ap
->nwords
- 21);
561 uvm_vnp_setsize(vp
, ap
->fsize
);
566 * Load the bitmap into memory, and count the number of available
568 * The bitmap will be released if the filesystem is read-only; it's
569 * only needed to find the free space.
572 adosfs_loadbitmap(struct adosfsmount
*amp
)
574 struct buf
*bp
, *mapbp
;
576 int blkix
, endix
, mapix
;
582 if ((error
= bread(amp
->devvp
, bn
* amp
->bsize
/ DEV_BSIZE
, amp
->bsize
,
583 NOCRED
, 0, &bp
)) != 0) {
587 blkix
= amp
->nwords
- 49;
588 endix
= amp
->nwords
- 24;
590 bmsize
= (amp
->numblks
+ 31) / 32;
591 while (mapix
< bmsize
) {
595 if (adoswordn(bp
, blkix
) == 0)
599 if ((error
= bread(amp
->devvp
,
600 adoswordn(bp
, blkix
) * amp
->bsize
/ DEV_BSIZE
, amp
->bsize
,
601 NOCRED
, 0, &mapbp
)) != 0)
603 if (adoscksum(mapbp
, amp
->nwords
)) {
605 printf("adosfs: loadbitmap - cksum of blk %d failed\n",
606 adoswordn(bp
, blkix
));
608 /* XXX Force read-only? Set free space 0? */
612 while (n
< amp
->nwords
&& mapix
< bmsize
) {
613 amp
->bitmap
[mapix
++] = bits
= adoswordn(mapbp
, n
);
615 if (mapix
== bmsize
&& amp
->numblks
& 31)
616 bits
&= ~(0xffffffff << (amp
->numblks
& 31));
624 if (mapix
< bmsize
&& blkix
== endix
) {
625 bn
= adoswordn(bp
, blkix
);
627 if ((error
= bread(amp
->devvp
, bn
* amp
->bsize
/ DEV_BSIZE
,
628 amp
->bsize
, NOCRED
, 0, &bp
)) != 0)
631 * Why is there no checksum on these blocks?
634 endix
= amp
->nwords
- 1;
646 * File handle to vnode
648 * Have to be really careful about stale file handles:
649 * - check that the inode number is in range
650 * - call iget() to get the locked inode
651 * - check for an unallocated inode (i_mode == 0)
652 * - check that the generation number matches
663 adosfs_fhtovp(struct mount
*mp
, struct fid
*fhp
, struct vnode
**vpp
)
672 if (fhp
->fid_len
!= sizeof(struct ifid
))
675 #ifdef ADOSFS_DIAGNOSTIC
676 printf("adfhtovp(%p, %p, %p)\n", mp
, fhp
, vpp
);
679 memcpy(&ifh
, fhp
, sizeof(ifh
));
681 if ((error
= VFS_VGET(mp
, ifh
.ifid_ino
, &nvp
)) != 0) {
687 if (ap
->inode
.iso_mode
== 0) {
698 adosfs_vptofh(struct vnode
*vp
, struct fid
*fhp
, size_t *fh_size
)
700 struct anode
*ap
= VTOA(vp
);
703 if (*fh_size
< sizeof(struct ifid
)) {
704 *fh_size
= sizeof(struct ifid
);
707 *fh_size
= sizeof(struct ifid
);
709 memset(&ifh
, 0, sizeof(ifh
));
710 ifh
.ifid_len
= sizeof(struct ifid
);
711 ifh
.ifid_ino
= ap
->block
;
712 ifh
.ifid_start
= ap
->block
;
713 memcpy(fhp
, &ifh
, sizeof(ifh
));
715 #ifdef ADOSFS_DIAGNOSTIC
716 printf("advptofh(%p, %p)\n", vp
, fhp
);
722 adosfs_sync(struct mount
*mp
, int waitfor
, kauth_cred_t uc
)
724 #ifdef ADOSFS_DIAGNOSTIC
725 printf("ad_sync(%p, %d)\n", mp
, waitfor
);
734 malloc_type_attach(M_ADOSFSMNT
);
735 malloc_type_attach(M_ANODE
);
736 malloc_type_attach(M_ADOSFSBITMAP
);
737 pool_init(&adosfs_node_pool
, sizeof(struct anode
), 0, 0, 0, "adosndpl",
738 &pool_allocator_nointr
, IPL_NONE
);
739 simple_lock_init(&adosfs_hashlock
);
746 pool_destroy(&adosfs_node_pool
);
747 malloc_type_detach(M_ADOSFSBITMAP
);
748 malloc_type_detach(M_ANODE
);
749 malloc_type_detach(M_ADOSFSMNT
);
753 * vfs generic function call table
756 extern const struct vnodeopv_desc adosfs_vnodeop_opv_desc
;
758 const struct vnodeopv_desc
*adosfs_vnodeopv_descs
[] = {
759 &adosfs_vnodeop_opv_desc
,
763 struct vfsops adosfs_vfsops
= {
765 sizeof (struct adosfs_args
),
770 (void *)eopnotsupp
, /* vfs_quotactl */
779 NULL
, /* vfs_mountroot */
780 (int (*)(struct mount
*, struct vnode
*, struct timespec
*)) eopnotsupp
,
782 (void *)eopnotsupp
, /* vfs_suspendctl */
783 genfs_renamelock_enter
,
784 genfs_renamelock_exit
,
786 adosfs_vnodeopv_descs
,
792 adosfs_modcmd(modcmd_t cmd
, void *arg
)
797 case MODULE_CMD_INIT
:
798 error
= vfs_attach(&adosfs_vfsops
);
801 sysctl_createv(&adosfs_sysctl_log
, 0, NULL
, NULL
,
803 CTLTYPE_NODE
, "vfs", NULL
,
806 sysctl_createv(&adosfs_sysctl_log
, 0, NULL
, NULL
,
808 CTLTYPE_NODE
, "adosfs",
809 SYSCTL_DESCR("AmigaDOS file system"),
811 CTL_VFS
, 16, CTL_EOL
);
813 * XXX the "16" above could be dynamic, thereby eliminating
814 * one more instance of the "number to vfs" mapping problem,
815 * but "16" is the order as taken from sys/mount.h
818 case MODULE_CMD_FINI
:
819 error
= vfs_detach(&adosfs_vfsops
);
822 sysctl_teardown(&adosfs_sysctl_log
);