]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/hpfs/hpfs_vfsops.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / fs / hpfs / hpfs_vfsops.c
1 /*-
2  * Copyright (c) 1998, 1999 Semen Ustimenko (semenu@FreeBSD.org)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/namei.h>
33 #include <sys/conf.h>
34 #include <sys/proc.h>
35 #include <sys/kernel.h>
36 #include <sys/vnode.h>
37 #include <sys/mount.h>
38 #include <sys/bio.h>
39 #include <sys/buf.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
42
43 #include <geom/geom.h>
44 #include <geom/geom_vfs.h>
45
46 #include <vm/vm.h>
47 #include <vm/vm_param.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_extern.h>
51
52 #include <fs/hpfs/hpfs.h>
53 #include <fs/hpfs/hpfsmount.h>
54 #include <fs/hpfs/hpfs_subr.h>
55
56 MALLOC_DEFINE(M_HPFSMNT, "hpfs_mount", "HPFS mount structure");
57 MALLOC_DEFINE(M_HPFSNO, "hpfs_node", "HPFS node structure");
58
59 struct sockaddr;
60
61 static int      hpfs_mountfs(register struct vnode *, struct mount *, 
62                                   struct thread *);
63
64 static vfs_fhtovp_t     hpfs_fhtovp;
65 static vfs_vget_t       hpfs_vget;
66 static vfs_cmount_t     hpfs_cmount;
67 static vfs_mount_t      hpfs_mount;
68 static vfs_root_t       hpfs_root;
69 static vfs_statfs_t     hpfs_statfs;
70 static vfs_unmount_t    hpfs_unmount;
71
72 static int
73 hpfs_cmount ( 
74         struct mntarg *ma,
75         void *data,
76         int flags,
77         struct thread *td )
78 {
79         struct hpfs_args args;
80         int error;
81
82         error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
83         if (error)
84                 return (error);
85
86         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
87         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
88         ma = mount_argf(ma, "uid", "%d", args.uid);
89         ma = mount_argf(ma, "gid", "%d", args.gid);
90         ma = mount_argf(ma, "mode", "%d", args.mode);
91         if (args.flags & HPFSMNT_TABLES) {
92                 ma = mount_arg(ma, "d2u", args.d2u, sizeof args.d2u);
93                 ma = mount_arg(ma, "u2d", args.u2d, sizeof args.u2d);
94         }
95
96         error = kernel_mount(ma, flags);
97
98         return (error);
99 }
100
101 static const char *hpfs_opts[] = {
102         "from", "export", "uid", "gid", "mode", "d2u", "u2d", NULL
103 };
104
105 static int
106 hpfs_mount ( 
107         struct mount *mp,
108         struct thread *td )
109 {
110         int             err = 0, error;
111         struct vnode    *devvp;
112         struct hpfsmount *hpmp = 0;
113         struct nameidata ndp;
114         struct export_args export;
115         char *from;
116
117         dprintf(("hpfs_omount():\n"));
118         /*
119          ***
120          * Mounting non-root filesystem or updating a filesystem
121          ***
122          */
123         if (vfs_filteropt(mp->mnt_optnew, hpfs_opts))
124                 return (EINVAL);
125
126         from = vfs_getopts(mp->mnt_optnew, "from", &error);
127         if (error)
128                 return (error);
129
130         /*
131          * If updating, check whether changing from read-only to
132          * read/write; if there is no device name, that's all we do.
133          */
134         if (mp->mnt_flag & MNT_UPDATE) {
135                 dprintf(("hpfs_omount: MNT_UPDATE: "));
136
137                 hpmp = VFSTOHPFS(mp);
138
139                 if (from == NULL) {
140                         error = vfs_copyopt(mp->mnt_optnew, "export",
141                             &export, sizeof export);
142                         if (error)
143                                 return (error);
144                         dprintf(("export 0x%x\n",args.export.ex_flags));
145                         err = vfs_export(mp, &export);
146                         if (err) {
147                                 printf("hpfs_omount: vfs_export failed %d\n",
148                                         err);
149                         }
150                         goto success;
151                 } else {
152                         dprintf(("name [FAILED]\n"));
153                         err = EINVAL;
154                         goto success;
155                 }
156                 dprintf(("\n"));
157         }
158
159         /*
160          * Not an update, or updating the name: look up the name
161          * and verify that it refers to a sensible block device.
162          */
163         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
164         err = namei(&ndp);
165         if (err) {
166                 /* can't get devvp!*/
167                 goto error_1;
168         }
169
170         devvp = ndp.ni_vp;
171
172         if (!vn_isdisk(devvp, &err)) {
173                 vput(devvp);
174                 return (err);
175         }
176
177         /*
178          ********************
179          * NEW MOUNT
180          ********************
181          */
182
183         /*
184          * Since this is a new mount, we want the names for
185          * the device and the mount point copied in.  If an
186          * error occurs, the mountpoint is discarded by the
187          * upper level code.  Note that vfs_omount() handles
188          * copying the mountpoint f_mntonname for us, so we
189          * don't have to do it here unless we want to set it
190          * to something other than "path" for some rason.
191          */
192         /* Save "mounted from" info for mount point (NULL pad)*/
193         vfs_mountedfrom(mp, from);
194
195         err = hpfs_mountfs(devvp, mp, td);
196         if (err) {
197                 vrele(devvp);
198                 goto error_1;
199         }
200
201         goto success;
202
203 error_1:        /* no state to back out*/
204         /* XXX: Missing NDFREE(&ndp, ...) */
205
206 success:
207         return( err);
208 }
209
210 /*
211  * Common code for mount and mountroot
212  */
213 int
214 hpfs_mountfs(devvp, mp, td)
215         register struct vnode *devvp;
216         struct mount *mp;
217         struct thread *td;
218 {
219         int error, ronly, v;
220         struct sublock *sup;
221         struct spblock *spp;
222         struct hpfsmount *hpmp;
223         struct buf *bp = NULL;
224         struct vnode *vp;
225         struct cdev *dev = devvp->v_rdev;
226         struct g_consumer *cp;
227         struct bufobj *bo;
228
229         if (mp->mnt_flag & MNT_ROOTFS)
230                 return (EOPNOTSUPP);
231         dprintf(("hpfs_mountfs():\n"));
232         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
233         /* XXX: use VOP_ACCESS to check FS perms */
234         DROP_GIANT();
235         g_topology_lock();
236         error = g_vfs_open(devvp, &cp, "hpfs", ronly ? 0 : 1);
237         g_topology_unlock();
238         PICKUP_GIANT();
239         VOP_UNLOCK(devvp, 0, td);
240         if (error)
241                 return (error);
242
243         bo = &devvp->v_bufobj;
244         bo->bo_private = cp;
245         bo->bo_ops = g_vfs_bufops;
246
247         /*
248          * Do actual mount
249          */
250         hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO);
251
252         hpmp->hpm_cp = cp;
253         hpmp->hpm_bo = bo;
254
255         /* Read in SuperBlock */
256         error = bread(devvp, SUBLOCK, SUSIZE, NOCRED, &bp);
257         if (error)
258                 goto failed;
259         bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
260         brelse(bp); bp = NULL;
261
262         /* Read in SpareBlock */
263         error = bread(devvp, SPBLOCK, SPSIZE, NOCRED, &bp);
264         if (error)
265                 goto failed;
266         bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
267         brelse(bp); bp = NULL;
268
269         sup = &hpmp->hpm_su;
270         spp = &hpmp->hpm_sp;
271
272         /* Check magic */
273         if (sup->su_magic != SU_MAGIC) {
274                 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
275                 error = EINVAL;
276                 goto failed;
277         }
278         if (spp->sp_magic != SP_MAGIC) {
279                 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
280                 error = EINVAL;
281                 goto failed;
282         }
283
284         mp->mnt_data = (qaddr_t)hpmp;
285         hpmp->hpm_devvp = devvp;
286         hpmp->hpm_dev = devvp->v_rdev;
287         hpmp->hpm_mp = mp;
288         if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
289                 hpmp->hpm_uid = v;
290         if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
291                 hpmp->hpm_gid = v;
292         if (1 == vfs_scanopt(mp->mnt_optnew, "mode", "%d", &v))
293                 hpmp->hpm_mode = v;
294
295         error = hpfs_bminit(hpmp);
296         if (error)
297                 goto failed;
298
299         error = hpfs_cpinit(mp, hpmp);
300         if (error) {
301                 hpfs_bmdeinit(hpmp);
302                 goto failed;
303         }
304
305         error = hpfs_root(mp, LK_EXCLUSIVE, &vp, td);
306         if (error) {
307                 hpfs_cpdeinit(hpmp);
308                 hpfs_bmdeinit(hpmp);
309                 goto failed;
310         }
311
312         vput(vp);
313
314         mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
315         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
316         mp->mnt_maxsymlinklen = 0;
317         MNT_ILOCK(mp);
318         mp->mnt_flag |= MNT_LOCAL;
319         MNT_IUNLOCK(mp);
320         return (0);
321
322 failed:
323         if (bp)
324                 brelse (bp);
325         mp->mnt_data = (qaddr_t)NULL;
326         DROP_GIANT();
327         g_topology_lock();
328         g_vfs_close(cp, td);
329         g_topology_unlock();
330         PICKUP_GIANT();
331         return (error);
332 }
333
334 static int
335 hpfs_unmount( 
336         struct mount *mp,
337         int mntflags,
338         struct thread *td)
339 {
340         int error, flags, ronly;
341         register struct hpfsmount *hpmp = VFSTOHPFS(mp);
342
343         dprintf(("hpfs_unmount():\n"));
344
345         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
346
347         flags = 0;
348         if(mntflags & MNT_FORCE)
349                 flags |= FORCECLOSE;
350
351         dprintf(("hpfs_unmount: vflushing...\n"));
352         
353         error = vflush(mp, 0, flags, td);
354         if (error) {
355                 printf("hpfs_unmount: vflush failed: %d\n",error);
356                 return (error);
357         }
358
359         vinvalbuf(hpmp->hpm_devvp, V_SAVE, td, 0, 0);
360         DROP_GIANT();
361         g_topology_lock();
362         g_vfs_close(hpmp->hpm_cp, td);
363         g_topology_unlock();
364         PICKUP_GIANT();
365         vrele(hpmp->hpm_devvp);
366
367         dprintf(("hpfs_umount: freeing memory...\n"));
368         hpfs_cpdeinit(hpmp);
369         hpfs_bmdeinit(hpmp);
370         mp->mnt_data = (qaddr_t)0;
371         MNT_ILOCK(mp);
372         mp->mnt_flag &= ~MNT_LOCAL;
373         MNT_IUNLOCK(mp);
374         FREE(hpmp, M_HPFSMNT);
375
376         return (0);
377 }
378
379 static int
380 hpfs_root(
381         struct mount *mp,
382         int flags,
383         struct vnode **vpp,
384         struct thread *td )
385 {
386         int error = 0;
387         struct hpfsmount *hpmp = VFSTOHPFS(mp);
388
389         dprintf(("hpfs_root():\n"));
390         error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, LK_EXCLUSIVE, vpp);
391         if(error) {
392                 printf("hpfs_root: VFS_VGET failed: %d\n",error);
393                 return (error);
394         }
395
396         return (error);
397 }
398
399 static int
400 hpfs_statfs(
401         struct mount *mp,
402         struct statfs *sbp,
403         struct thread *td)
404 {
405         struct hpfsmount *hpmp = VFSTOHPFS(mp);
406
407         dprintf(("hpfs_statfs(): HPFS%d.%d\n",
408                 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
409
410         sbp->f_type = mp->mnt_vfc->vfc_typenum;
411         sbp->f_bsize = DEV_BSIZE;
412         sbp->f_iosize = DEV_BSIZE;
413         sbp->f_blocks = hpmp->hpm_su.su_btotal;
414         sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
415         sbp->f_ffree = 0;
416         sbp->f_files = 0;
417         sbp->f_flags = mp->mnt_flag;
418         
419         return (0);
420 }
421
422 /*ARGSUSED*/
423 static int
424 hpfs_fhtovp(
425         struct mount *mp,
426         struct fid *fhp,
427         struct vnode **vpp)
428 {
429         struct vnode *nvp;
430         struct hpfid *hpfhp = (struct hpfid *)fhp;
431         int error;
432
433         if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
434                 *vpp = NULLVP;
435                 return (error);
436         }
437         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
438          * with HPFS, we don't need to check anything else for now */
439         *vpp = nvp;
440
441         return (0);
442 }
443
444 static int
445 hpfs_vget(
446         struct mount *mp,
447         ino_t ino,
448         int flags,
449         struct vnode **vpp) 
450 {
451         struct hpfsmount *hpmp = VFSTOHPFS(mp);
452         struct vnode *vp;
453         struct hpfsnode *hp;
454         struct buf *bp;
455         int error;
456         struct thread *td;
457
458         dprintf(("hpfs_vget(0x%x): ",ino));
459
460         error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
461         if (error || *vpp != NULL)
462                 return (error);
463
464         *vpp = NULL;
465         hp = NULL;
466         vp = NULL;
467
468         /*
469          * We have to lock node creation for a while,
470          * but then we have to call getnewvnode(), 
471          * this may cause hpfs_reclaim() to be called,
472          * this may need to VOP_VGET() parent dir for
473          * update reasons, and if parent is not in
474          * hash, we have to lock node creation...
475          * To solve this, we MALLOC, getnewvnode and init while
476          * not locked (probability of node appearence
477          * at that time is little, and anyway - we'll
478          * check for it).
479          */
480         MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode), 
481                 M_HPFSNO, M_WAITOK);
482
483         error = getnewvnode("hpfs", mp, &hpfs_vnodeops, &vp);
484         if (error) {
485                 printf("hpfs_vget: can't get new vnode\n");
486                 FREE(hp, M_HPFSNO);
487                 return (error);
488         }
489
490         dprintf(("prenew "));
491
492         vp->v_data = hp;
493
494         if (ino == (ino_t)hpmp->hpm_su.su_rootfno) 
495                 vp->v_vflag |= VV_ROOT;
496
497
498         mtx_init(&hp->h_interlock, "hpfsnode interlock", NULL, MTX_DEF);
499
500         hp->h_flag = H_INVAL;
501         hp->h_vp = vp;
502         hp->h_hpmp = hpmp;
503         hp->h_no = ino;
504         hp->h_dev = hpmp->hpm_dev;
505         hp->h_uid = hpmp->hpm_uid;
506         hp->h_gid = hpmp->hpm_uid;
507         hp->h_mode = hpmp->hpm_mode;
508         hp->h_devvp = hpmp->hpm_devvp;
509
510         td = curthread;
511         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
512         error = insmntque(vp, mp);
513         if (error != 0) {
514                 free(hp, M_HPFSNO);
515                 return (error);
516         }
517         error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
518         if (error || *vpp != NULL)
519                 return (error);
520
521         error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
522         if (error) {
523                 printf("hpfs_vget: can't read ino %d\n",ino);
524                 vput(vp);
525                 return (error);
526         }
527         bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
528         brelse(bp);
529
530         if (hp->h_fn.fn_magic != FN_MAGIC) {
531                 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
532                 vput(vp);
533                 return (EINVAL);
534         }
535
536         vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
537         hp->h_flag &= ~H_INVAL;
538
539         *vpp = vp;
540
541         return (0);
542 }
543
544 static struct vfsops hpfs_vfsops = {
545         .vfs_fhtovp =           hpfs_fhtovp,
546         .vfs_cmount =           hpfs_cmount,
547         .vfs_mount =            hpfs_mount,
548         .vfs_root =             hpfs_root,
549         .vfs_statfs =           hpfs_statfs,
550         .vfs_unmount =          hpfs_unmount,
551         .vfs_vget =             hpfs_vget,
552 };
553 VFS_SET(hpfs_vfsops, hpfs, 0);