]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/hpfs/hpfs_vfsops.c
VFS_STATFS(mp, ...) is mostly called with &mp->mnt_stat, but a few cases
[FreeBSD/FreeBSD.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 hpfs_args *, struct thread *);
63
64 static vfs_init_t       hpfs_init;
65 static vfs_uninit_t     hpfs_uninit;
66 static vfs_fhtovp_t     hpfs_fhtovp;
67 static vfs_vget_t       hpfs_vget;
68 static vfs_omount_t     hpfs_omount;
69 static vfs_root_t       hpfs_root;
70 static vfs_statfs_t     hpfs_statfs;
71 static vfs_unmount_t    hpfs_unmount;
72 static vfs_vptofh_t     hpfs_vptofh;
73
74 static int
75 hpfs_init (
76         struct vfsconf *vcp )
77 {
78         dprintf(("hpfs_init():\n"));
79         
80         hpfs_hphashinit();
81         return 0;
82 }
83
84 static int
85 hpfs_uninit (vfsp)
86         struct vfsconf *vfsp;
87 {
88         hpfs_hphashdestroy();
89         return 0;;
90 }
91
92 static int
93 hpfs_omount ( 
94         struct mount *mp,
95         char *path,
96         caddr_t data,
97         struct thread *td )
98 {
99         size_t          size;
100         int             err = 0;
101         struct vnode    *devvp;
102         struct hpfs_args args;
103         struct hpfsmount *hpmp = 0;
104         struct nameidata ndp;
105
106         dprintf(("hpfs_omount():\n"));
107         /*
108          ***
109          * Mounting non-root filesystem or updating a filesystem
110          ***
111          */
112
113         /* copy in user arguments*/
114         err = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
115         if (err)
116                 goto error_1;           /* can't get arguments*/
117
118         /*
119          * If updating, check whether changing from read-only to
120          * read/write; if there is no device name, that's all we do.
121          */
122         if (mp->mnt_flag & MNT_UPDATE) {
123                 dprintf(("hpfs_omount: MNT_UPDATE: "));
124
125                 hpmp = VFSTOHPFS(mp);
126
127                 if (args.fspec == 0) {
128                         dprintf(("export 0x%x\n",args.export.ex_flags));
129                         err = vfs_export(mp, &args.export);
130                         if (err) {
131                                 printf("hpfs_omount: vfs_export failed %d\n",
132                                         err);
133                         }
134                         goto success;
135                 } else {
136                         dprintf(("name [FAILED]\n"));
137                         err = EINVAL;
138                         goto success;
139                 }
140                 dprintf(("\n"));
141         }
142
143         /*
144          * Not an update, or updating the name: look up the name
145          * and verify that it refers to a sensible block device.
146          */
147         NDINIT(&ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
148         err = namei(&ndp);
149         if (err) {
150                 /* can't get devvp!*/
151                 goto error_1;
152         }
153
154         devvp = ndp.ni_vp;
155
156         if (!vn_isdisk(devvp, &err)) 
157                 goto error_2;
158
159         /*
160          ********************
161          * NEW MOUNT
162          ********************
163          */
164
165         /*
166          * Since this is a new mount, we want the names for
167          * the device and the mount point copied in.  If an
168          * error occurs, the mountpoint is discarded by the
169          * upper level code.  Note that vfs_omount() handles
170          * copying the mountpoint f_mntonname for us, so we
171          * don't have to do it here unless we want to set it
172          * to something other than "path" for some rason.
173          */
174         /* Save "mounted from" info for mount point (NULL pad)*/
175         copyinstr(      args.fspec,                     /* device name*/
176                         mp->mnt_stat.f_mntfromname,     /* save area*/
177                         MNAMELEN - 1,                   /* max size*/
178                         &size);                         /* real size*/
179         bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
180
181         err = hpfs_mountfs(devvp, mp, &args, td);
182         if (err)
183                 goto error_2;
184
185         /*
186          * Initialize FS stat information in mount struct; uses both
187          * mp->mnt_stat.f_mntonname and mp->mnt_stat.f_mntfromname
188          *
189          * This code is common to root and non-root mounts
190          */
191         (void)VFS_STATFS(mp, &mp->mnt_stat, td);
192
193         goto success;
194
195
196 error_2:        /* error with devvp held*/
197
198         /* release devvp before failing*/
199         vrele(devvp);
200
201 error_1:        /* no state to back out*/
202         /* XXX: Missing NDFREE(&ndp, ...) */
203
204 success:
205         return( err);
206 }
207
208 /*
209  * Common code for mount and mountroot
210  */
211 int
212 hpfs_mountfs(devvp, mp, argsp, td)
213         register struct vnode *devvp;
214         struct mount *mp;
215         struct hpfs_args *argsp;
216         struct thread *td;
217 {
218         int error, ronly;
219         struct sublock *sup;
220         struct spblock *spp;
221         struct hpfsmount *hpmp;
222         struct buf *bp = NULL;
223         struct vnode *vp;
224         struct cdev *dev = devvp->v_rdev;
225         struct g_consumer *cp;
226         struct bufobj *bo;
227
228         if (mp->mnt_flag & MNT_ROOTFS)
229                 return (EOPNOTSUPP);
230         dprintf(("hpfs_mountfs():\n"));
231         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
232         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
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         hpmp->hpm_uid = argsp->uid;
289         hpmp->hpm_gid = argsp->gid;
290         hpmp->hpm_mode = argsp->mode;
291
292         error = hpfs_bminit(hpmp);
293         if (error)
294                 goto failed;
295
296         error = hpfs_cpinit(hpmp, argsp);
297         if (error) {
298                 hpfs_bmdeinit(hpmp);
299                 goto failed;
300         }
301
302         error = hpfs_root(mp, &vp, td);
303         if (error) {
304                 hpfs_cpdeinit(hpmp);
305                 hpfs_bmdeinit(hpmp);
306                 goto failed;
307         }
308
309         vput(vp);
310
311         mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
312         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
313         mp->mnt_maxsymlinklen = 0;
314         mp->mnt_flag |= MNT_LOCAL;
315         return (0);
316
317 failed:
318         if (bp)
319                 brelse (bp);
320         mp->mnt_data = (qaddr_t)NULL;
321         g_wither_geom_close(cp->geom, ENXIO);
322         return (error);
323 }
324
325 static int
326 hpfs_unmount( 
327         struct mount *mp,
328         int mntflags,
329         struct thread *td)
330 {
331         int error, flags, ronly;
332         register struct hpfsmount *hpmp = VFSTOHPFS(mp);
333
334         dprintf(("hpfs_unmount():\n"));
335
336         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
337
338         flags = 0;
339         if(mntflags & MNT_FORCE)
340                 flags |= FORCECLOSE;
341
342         dprintf(("hpfs_unmount: vflushing...\n"));
343         
344         error = vflush(mp, 0, flags, td);
345         if (error) {
346                 printf("hpfs_unmount: vflush failed: %d\n",error);
347                 return (error);
348         }
349
350         vinvalbuf(hpmp->hpm_devvp, V_SAVE, NOCRED, td, 0, 0);
351         g_wither_geom_close(hpmp->hpm_cp->geom, ENXIO);
352         vrele(hpmp->hpm_devvp);
353
354         dprintf(("hpfs_umount: freeing memory...\n"));
355         hpfs_cpdeinit(hpmp);
356         hpfs_bmdeinit(hpmp);
357         mp->mnt_data = (qaddr_t)0;
358         mp->mnt_flag &= ~MNT_LOCAL;
359         FREE(hpmp, M_HPFSMNT);
360
361         return (0);
362 }
363
364 static int
365 hpfs_root(
366         struct mount *mp,
367         struct vnode **vpp,
368         struct thread *td )
369 {
370         int error = 0;
371         struct hpfsmount *hpmp = VFSTOHPFS(mp);
372
373         dprintf(("hpfs_root():\n"));
374         error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, LK_EXCLUSIVE, vpp);
375         if(error) {
376                 printf("hpfs_root: VFS_VGET failed: %d\n",error);
377                 return (error);
378         }
379
380         return (error);
381 }
382
383 static int
384 hpfs_statfs(
385         struct mount *mp,
386         struct statfs *sbp,
387         struct thread *td)
388 {
389         struct hpfsmount *hpmp = VFSTOHPFS(mp);
390
391         dprintf(("hpfs_statfs(): HPFS%d.%d\n",
392                 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
393
394         sbp->f_type = mp->mnt_vfc->vfc_typenum;
395         sbp->f_bsize = DEV_BSIZE;
396         sbp->f_iosize = DEV_BSIZE;
397         sbp->f_blocks = hpmp->hpm_su.su_btotal;
398         sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
399         sbp->f_ffree = 0;
400         sbp->f_files = 0;
401         sbp->f_flags = mp->mnt_flag;
402         
403         return (0);
404 }
405
406 /*ARGSUSED*/
407 static int
408 hpfs_fhtovp(
409         struct mount *mp,
410         struct fid *fhp,
411         struct vnode **vpp)
412 {
413         struct vnode *nvp;
414         struct hpfid *hpfhp = (struct hpfid *)fhp;
415         int error;
416
417         if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
418                 *vpp = NULLVP;
419                 return (error);
420         }
421         /* XXX as unlink/rmdir/mkdir/creat are not currently possible
422          * with HPFS, we don't need to check anything else for now */
423         *vpp = nvp;
424
425         return (0);
426 }
427
428 static int
429 hpfs_vptofh(
430         struct vnode *vp,
431         struct fid *fhp)
432 {
433         register struct hpfsnode *hpp;
434         register struct hpfid *hpfhp;
435
436         hpp = VTOHP(vp);
437         hpfhp = (struct hpfid *)fhp;
438         hpfhp->hpfid_len = sizeof(struct hpfid);
439         hpfhp->hpfid_ino = hpp->h_no;
440         /* hpfhp->hpfid_gen = hpp->h_gen; */
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         struct thread *td = curthread;  /* XXX */
456         int error;
457
458         dprintf(("hpfs_vget(0x%x): ",ino));
459
460         *vpp = NULL;
461         hp = NULL;
462         vp = NULL;
463
464         if ((error = hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td)) != 0)
465                 return (error);
466         if (*vpp != NULL) {
467                 dprintf(("hashed\n"));
468                 return (0);
469         }
470
471         /*
472          * We have to lock node creation for a while,
473          * but then we have to call getnewvnode(), 
474          * this may cause hpfs_reclaim() to be called,
475          * this may need to VOP_VGET() parent dir for
476          * update reasons, and if parent is not in
477          * hash, we have to lock node creation...
478          * To solve this, we MALLOC, getnewvnode and init while
479          * not locked (probability of node appearence
480          * at that time is little, and anyway - we'll
481          * check for it).
482          */
483         MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode), 
484                 M_HPFSNO, M_WAITOK);
485
486         error = getnewvnode("hpfs", hpmp->hpm_mp, &hpfs_vnodeops, &vp);
487         if (error) {
488                 printf("hpfs_vget: can't get new vnode\n");
489                 FREE(hp, M_HPFSNO);
490                 return (error);
491         }
492
493         dprintf(("prenew "));
494
495         vp->v_data = hp;
496
497         if (ino == (ino_t)hpmp->hpm_su.su_rootfno) 
498                 vp->v_vflag |= VV_ROOT;
499
500
501         mtx_init(&hp->h_interlock, "hpfsnode interlock", NULL, MTX_DEF);
502
503         hp->h_flag = H_INVAL;
504         hp->h_vp = vp;
505         hp->h_hpmp = hpmp;
506         hp->h_no = ino;
507         hp->h_dev = hpmp->hpm_dev;
508         hp->h_uid = hpmp->hpm_uid;
509         hp->h_gid = hpmp->hpm_uid;
510         hp->h_mode = hpmp->hpm_mode;
511         hp->h_devvp = hpmp->hpm_devvp;
512         VREF(hp->h_devvp);
513
514         error = vn_lock(vp, LK_EXCLUSIVE, td);
515         if (error) {
516                 vput(vp);
517                 return (error);
518         }
519
520         do {
521                 if ((error =
522                      hpfs_hphashvget(hpmp->hpm_dev, ino, flags, vpp, td))) {
523                         vput(vp);
524                         return (error);
525                 }
526                 if (*vpp != NULL) {
527                         dprintf(("hashed2\n"));
528                         vput(vp);
529                         return (0);
530                 }
531         } while(lockmgr(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL,NULL));
532
533         hpfs_hphashins(hp);
534
535         lockmgr(&hpfs_hphash_lock, LK_RELEASE, NULL, NULL);
536
537         error = bread(hpmp->hpm_devvp, ino, FNODESIZE, NOCRED, &bp);
538         if (error) {
539                 printf("hpfs_vget: can't read ino %d\n",ino);
540                 vput(vp);
541                 return (error);
542         }
543         bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
544         brelse(bp);
545
546         if (hp->h_fn.fn_magic != FN_MAGIC) {
547                 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
548                 vput(vp);
549                 return (EINVAL);
550         }
551
552         vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
553         hp->h_flag &= ~H_INVAL;
554
555         *vpp = vp;
556
557         return (0);
558 }
559
560 static struct vfsops hpfs_vfsops = {
561         .vfs_fhtovp =           hpfs_fhtovp,
562         .vfs_init =             hpfs_init,
563         .vfs_omount =           hpfs_omount,
564         .vfs_root =             hpfs_root,
565         .vfs_statfs =           hpfs_statfs,
566         .vfs_uninit =           hpfs_uninit,
567         .vfs_unmount =          hpfs_unmount,
568         .vfs_vget =             hpfs_vget,
569         .vfs_vptofh =           hpfs_vptofh,
570 };
571 VFS_SET(hpfs_vfsops, hpfs, 0);