Fix .Os and a .Xr. Expand HISTORY a bit.
[dragonfly.git] / sys / vfs / hpfs / hpfs_vfsops.c
CommitLineData
984263bc
MD
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: src/sys/fs/hpfs/hpfs_vfsops.c,v 1.3.2.2 2001/12/25 01:44:45 dillon Exp $
87de5057 27 * $DragonFly: src/sys/vfs/hpfs/hpfs_vfsops.c,v 1.34 2006/05/06 02:43:13 dillon Exp $
984263bc
MD
28 */
29
30
31#include <sys/param.h>
32#include <sys/systm.h>
fad57d0e 33#include <sys/nlookup.h>
984263bc
MD
34#include <sys/conf.h>
35#include <sys/proc.h>
36#include <sys/kernel.h>
37#include <sys/vnode.h>
38#include <sys/mount.h>
39#include <sys/buf.h>
40#include <sys/fcntl.h>
41#include <sys/malloc.h>
42
f91a71dd
JS
43#include <machine/inttypes.h>
44
984263bc
MD
45#include <vm/vm.h>
46#include <vm/vm_param.h>
47#if defined(__NetBSD__)
48#include <vm/vm_prot.h>
49#endif
50#include <vm/vm_page.h>
51#include <vm/vm_object.h>
52#include <vm/vm_extern.h>
7d1b9a3b 53#include <sys/buf2.h>
984263bc
MD
54
55#if defined(__NetBSD__)
56#include <miscfs/specfs/specdev.h>
57#endif
58
1f2de5d4
MD
59#include "hpfs.h"
60#include "hpfsmount.h"
61#include "hpfs_subr.h"
984263bc 62
0961aa92
MD
63extern struct vnodeopv_entry_desc hpfs_vnodeop_entries[];
64
984263bc
MD
65MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
66MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
984263bc 67
a6ee311a
RG
68static int hpfs_root (struct mount *, struct vnode **);
69static int hpfs_statfs (struct mount *, struct statfs *,
70 struct thread *);
71static int hpfs_unmount (struct mount *, int, struct thread *);
72static int hpfs_vget (struct mount *mp, ino_t ino,
73 struct vnode **vpp);
74static int hpfs_mountfs (struct vnode *, struct mount *,
75 struct hpfs_args *, struct thread *);
76static int hpfs_vptofh (struct vnode *, struct fid *);
77static int hpfs_fhtovp (struct mount *, struct fid *,
78 struct vnode **);
984263bc 79
984263bc 80
984263bc 81struct sockaddr;
21739618 82static int hpfs_mount (struct mount *, char *, caddr_t, struct thread *);
a6ee311a
RG
83static int hpfs_init (struct vfsconf *);
84static int hpfs_checkexp (struct mount *, struct sockaddr *,
85 int *, struct ucred **);
984263bc
MD
86
87/*ARGSUSED*/
88static int
dd3af26c 89hpfs_checkexp(struct mount *mp,
dd3af26c 90 struct sockaddr *nam,
dd3af26c 91 int *exflagsp, struct ucred **credanonp)
984263bc 92{
f7aae92f
RG
93 struct netcred *np;
94 struct hpfsmount *hpm = VFSTOHPFS(mp);
984263bc
MD
95
96 /*
97 * Get the export permission structure for this <mp, client> tuple.
98 */
99 np = vfs_export_lookup(mp, &hpm->hpm_export, nam);
100 if (np == NULL)
101 return (EACCES);
102
103 *exflagsp = np->netc_exflags;
104 *credanonp = &np->netc_anon;
105 return (0);
106}
107
984263bc 108static int
dd3af26c 109hpfs_init(struct vfsconf *vcp)
984263bc
MD
110{
111 dprintf(("hpfs_init():\n"));
112
113 hpfs_hphashinit();
984263bc 114 return 0;
984263bc
MD
115}
116
117static int
dd3af26c 118hpfs_mount(struct mount *mp,
dd3af26c 119 char *path, caddr_t data,
21739618 120 struct thread *td)
984263bc
MD
121{
122 u_int size;
fad57d0e 123 int error;
984263bc
MD
124 struct vnode *devvp;
125 struct hpfs_args args;
126 struct hpfsmount *hpmp = 0;
fad57d0e 127 struct nlookupdata nd;
984263bc
MD
128
129 dprintf(("hpfs_mount():\n"));
130 /*
131 ***
132 * Mounting non-root file system or updating a file system
133 ***
134 */
135
136 /* copy in user arguments*/
fad57d0e
MD
137 error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
138 if (error)
984263bc
MD
139 goto error_1; /* can't get arguments*/
140
141 /*
142 * If updating, check whether changing from read-only to
143 * read/write; if there is no device name, that's all we do.
144 */
145 if (mp->mnt_flag & MNT_UPDATE) {
146 dprintf(("hpfs_mount: MNT_UPDATE: "));
147
148 hpmp = VFSTOHPFS(mp);
149
150 if (args.fspec == 0) {
151 dprintf(("export 0x%x\n",args.export.ex_flags));
fad57d0e
MD
152 error = vfs_export(mp, &hpmp->hpm_export, &args.export);
153 if (error) {
984263bc 154 printf("hpfs_mount: vfs_export failed %d\n",
fad57d0e 155 error);
984263bc
MD
156 }
157 goto success;
158 } else {
159 dprintf(("name [FAILED]\n"));
fad57d0e 160 error = EINVAL;
984263bc
MD
161 goto success;
162 }
163 dprintf(("\n"));
164 }
165
166 /*
167 * Not an update, or updating the name: look up the name
168 * and verify that it refers to a sensible block device.
169 */
fad57d0e
MD
170 devvp = NULL;
171 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
172 if (error == 0)
173 error = nlookup(&nd);
174 if (error == 0)
175 error = cache_vref(nd.nl_ncp, nd.nl_cred, &devvp);
176 nlookup_done(&nd);
177 if (error)
984263bc 178 goto error_1;
984263bc 179
fad57d0e 180 if (!vn_isdisk(devvp, &error))
984263bc 181 goto error_2;
984263bc
MD
182
183 /*
184 ********************
185 * NEW MOUNT
186 ********************
187 */
188
984263bc
MD
189 /* Save "mounted from" info for mount point (NULL pad)*/
190 copyinstr( args.fspec, /* device name*/
191 mp->mnt_stat.f_mntfromname, /* save area*/
192 MNAMELEN - 1, /* max size*/
193 &size); /* real size*/
194 bzero( mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
195
fad57d0e
MD
196 error = hpfs_mountfs(devvp, mp, &args, td);
197 if (error)
984263bc
MD
198 goto error_2;
199
200 /*
75ffff0d
JS
201 * Initialize FS stat information in mount struct; uses
202 * mp->mnt_stat.f_mntfromname.
984263bc
MD
203 *
204 * This code is common to root and non-root mounts
205 */
fad57d0e
MD
206 VFS_STATFS(mp, &mp->mnt_stat, td);
207 return (error);
984263bc
MD
208
209error_2: /* error with devvp held*/
210
211 /* release devvp before failing*/
212 vrele(devvp);
213
214error_1: /* no state to back out*/
215
216success:
fad57d0e 217 return (error);
984263bc
MD
218}
219
220/*
221 * Common code for mount and mountroot
222 */
223int
dd3af26c
CP
224hpfs_mountfs(struct vnode *devvp, struct mount *mp, struct hpfs_args *argsp,
225 struct thread *td)
984263bc
MD
226{
227 int error, ncount, ronly;
228 struct sublock *sup;
229 struct spblock *spp;
230 struct hpfsmount *hpmp;
231 struct buf *bp = NULL;
232 struct vnode *vp;
e4c9c0c8 233 dev_t dev;
984263bc
MD
234
235 dprintf(("hpfs_mountfs():\n"));
236 /*
237 * Disallow multiple mounts of the same device.
238 * Disallow mounting of a device that is currently in use
239 * (except for root, which might share swap device for miniroot).
240 * Flush out any old buffers remaining from a previous use.
241 */
242 error = vfs_mountedon(devvp);
243 if (error)
244 return (error);
715f92b6 245 ncount = count_udev(devvp->v_udev);
984263bc
MD
246 if (devvp->v_object)
247 ncount -= 1;
21739618 248 if (ncount > 0)
984263bc
MD
249 return (EBUSY);
250
7d1b9a3b 251 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
87de5057 252 error = vinvalbuf(devvp, V_SAVE, 0, 0);
7d1b9a3b 253 VOP__UNLOCK(devvp, 0, td);
984263bc
MD
254 if (error)
255 return (error);
256
257 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
7d1b9a3b 258 VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY, td);
87de5057 259 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
7d1b9a3b 260 VOP__UNLOCK(devvp, 0, td);
984263bc
MD
261 if (error)
262 return (error);
e4c9c0c8 263 dev = devvp->v_rdev;
984263bc
MD
264
265 /*
266 * Do actual mount
267 */
268 hpmp = malloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK);
269 bzero(hpmp, sizeof(struct hpfsmount));
270
271 /* Read in SuperBlock */
54078292 272 error = bread(devvp, dbtodoff(SUBLOCK), SUSIZE, &bp);
984263bc
MD
273 if (error)
274 goto failed;
275 bcopy(bp->b_data, &hpmp->hpm_su, sizeof(struct sublock));
276 brelse(bp); bp = NULL;
277
278 /* Read in SpareBlock */
54078292 279 error = bread(devvp, dbtodoff(SPBLOCK), SPSIZE, &bp);
984263bc
MD
280 if (error)
281 goto failed;
282 bcopy(bp->b_data, &hpmp->hpm_sp, sizeof(struct spblock));
283 brelse(bp); bp = NULL;
284
285 sup = &hpmp->hpm_su;
286 spp = &hpmp->hpm_sp;
287
288 /* Check magic */
289 if (sup->su_magic != SU_MAGIC) {
290 printf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
291 error = EINVAL;
292 goto failed;
293 }
294 if (spp->sp_magic != SP_MAGIC) {
295 printf("hpfs_mountfs: SpareBlock MAGIC DOESN'T MATCH\n");
296 error = EINVAL;
297 goto failed;
298 }
299
300 mp->mnt_data = (qaddr_t)hpmp;
301 hpmp->hpm_devvp = devvp;
e4c9c0c8 302 hpmp->hpm_dev = dev;
984263bc
MD
303 hpmp->hpm_mp = mp;
304 hpmp->hpm_uid = argsp->uid;
305 hpmp->hpm_gid = argsp->gid;
306 hpmp->hpm_mode = argsp->mode;
307
308 error = hpfs_bminit(hpmp);
309 if (error)
310 goto failed;
311
312 error = hpfs_cpinit(hpmp, argsp);
313 if (error) {
314 hpfs_bmdeinit(hpmp);
315 goto failed;
316 }
dc1be39c
MD
317 vfs_add_vnodeops(mp, &mp->mnt_vn_norm_ops,
318 hpfs_vnodeop_entries, 0);
984263bc
MD
319
320 error = hpfs_root(mp, &vp);
321 if (error) {
322 hpfs_cpdeinit(hpmp);
323 hpfs_bmdeinit(hpmp);
324 goto failed;
325 }
326
327 vput(vp);
328
984263bc
MD
329 mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
330 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
984263bc
MD
331 mp->mnt_maxsymlinklen = 0;
332 mp->mnt_flag |= MNT_LOCAL;
e4c9c0c8 333 dev->si_mountpoint = mp;
984263bc
MD
334 return (0);
335
336failed:
337 if (bp)
338 brelse (bp);
339 mp->mnt_data = (qaddr_t)NULL;
e4c9c0c8 340 dev->si_mountpoint = NULL;
87de5057 341 VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
984263bc
MD
342 return (error);
343}
344
984263bc
MD
345
346static int
dd3af26c 347hpfs_unmount(struct mount *mp, int mntflags, struct thread *td)
984263bc
MD
348{
349 int error, flags, ronly;
f7aae92f 350 struct hpfsmount *hpmp = VFSTOHPFS(mp);
984263bc
MD
351
352 dprintf(("hpfs_unmount():\n"));
353
354 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
355
356 flags = 0;
357 if(mntflags & MNT_FORCE)
358 flags |= FORCECLOSE;
359
360 dprintf(("hpfs_unmount: vflushing...\n"));
361
362 error = vflush(mp, 0, flags);
363 if (error) {
364 printf("hpfs_unmount: vflush failed: %d\n",error);
365 return (error);
366 }
367
e4c9c0c8 368 hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL;
984263bc 369
87de5057
MD
370 vinvalbuf(hpmp->hpm_devvp, V_SAVE, 0, 0);
371 error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE);
984263bc
MD
372
373 vrele(hpmp->hpm_devvp);
374
375 dprintf(("hpfs_umount: freeing memory...\n"));
376 hpfs_cpdeinit(hpmp);
377 hpfs_bmdeinit(hpmp);
378 mp->mnt_data = (qaddr_t)0;
379 mp->mnt_flag &= ~MNT_LOCAL;
380 FREE(hpmp, M_HPFSMNT);
381
382 return (0);
383}
384
385static int
dd3af26c 386hpfs_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
387{
388 int error = 0;
389 struct hpfsmount *hpmp = VFSTOHPFS(mp);
390
391 dprintf(("hpfs_root():\n"));
392 error = VFS_VGET(mp, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
393 if(error) {
394 printf("hpfs_root: VFS_VGET failed: %d\n",error);
395 return (error);
396 }
397
398 return (error);
399}
400
401static int
dd3af26c 402hpfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
984263bc
MD
403{
404 struct hpfsmount *hpmp = VFSTOHPFS(mp);
405
406 dprintf(("hpfs_statfs(): HPFS%d.%d\n",
407 hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
408
984263bc 409 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
410 sbp->f_bsize = DEV_BSIZE;
411 sbp->f_iosize = DEV_BSIZE;
412 sbp->f_blocks = hpmp->hpm_su.su_btotal;
413 sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
414 sbp->f_ffree = 0;
415 sbp->f_files = 0;
416 if (sbp != &mp->mnt_stat) {
984263bc
MD
417 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
418 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
419 }
420 sbp->f_flags = mp->mnt_flag;
421
422 return (0);
423}
424
984263bc
MD
425
426/*ARGSUSED*/
427static int
dd3af26c 428hpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
984263bc
MD
429{
430 struct vnode *nvp;
431 struct hpfid *hpfhp = (struct hpfid *)fhp;
432 int error;
433
434 if ((error = VFS_VGET(mp, hpfhp->hpfid_ino, &nvp)) != 0) {
435 *vpp = NULLVP;
436 return (error);
437 }
438 /* XXX as unlink/rmdir/mkdir/creat are not currently possible
439 * with HPFS, we don't need to check anything else for now */
440 *vpp = nvp;
441
442 return (0);
443}
444
445static int
dd3af26c 446hpfs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc 447{
f7aae92f
RG
448 struct hpfsnode *hpp;
449 struct hpfid *hpfhp;
984263bc
MD
450
451 hpp = VTOHP(vp);
452 hpfhp = (struct hpfid *)fhp;
453 hpfhp->hpfid_len = sizeof(struct hpfid);
454 hpfhp->hpfid_ino = hpp->h_no;
455 /* hpfhp->hpfid_gen = hpp->h_gen; */
456 return (0);
457}
458
459static int
dd3af26c 460hpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
984263bc
MD
461{
462 struct hpfsmount *hpmp = VFSTOHPFS(mp);
463 struct vnode *vp;
464 struct hpfsnode *hp;
465 struct buf *bp;
984263bc
MD
466 int error;
467
468 dprintf(("hpfs_vget(0x%x): ",ino));
469
470 *vpp = NULL;
471 hp = NULL;
472 vp = NULL;
473
87de5057 474 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) {
984263bc
MD
475 dprintf(("hashed\n"));
476 return (0);
477 }
478
479 /*
480 * We have to lock node creation for a while,
481 * but then we have to call getnewvnode(),
482 * this may cause hpfs_reclaim() to be called,
483 * this may need to VOP_VGET() parent dir for
484 * update reasons, and if parent is not in
485 * hash, we have to lock node creation...
486 * To solve this, we MALLOC, getnewvnode and init while
487 * not locked (probability of node appearence
488 * at that time is little, and anyway - we'll
489 * check for it).
490 */
491 MALLOC(hp, struct hpfsnode *, sizeof(struct hpfsnode),
492 M_HPFSNO, M_WAITOK);
493
6ddb7618 494 error = getnewvnode(VT_HPFS, hpmp->hpm_mp, &vp, VLKTIMEOUT, 0);
984263bc
MD
495 if (error) {
496 printf("hpfs_vget: can't get new vnode\n");
497 FREE(hp, M_HPFSNO);
498 return (error);
499 }
500
501 dprintf(("prenew "));
502
503 vp->v_data = hp;
504
505 if (ino == (ino_t)hpmp->hpm_su.su_rootfno)
506 vp->v_flag |= VROOT;
507
41a01a4d 508 lwkt_token_init(&hp->h_interlock);
984263bc
MD
509
510 hp->h_flag = H_INVAL;
511 hp->h_vp = vp;
512 hp->h_hpmp = hpmp;
513 hp->h_no = ino;
514 hp->h_dev = hpmp->hpm_dev;
515 hp->h_uid = hpmp->hpm_uid;
516 hp->h_gid = hpmp->hpm_uid;
517 hp->h_mode = hpmp->hpm_mode;
518 hp->h_devvp = hpmp->hpm_devvp;
597aea93 519 vref(hp->h_devvp);
984263bc 520
984263bc 521 do {
87de5057 522 if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) {
984263bc 523 dprintf(("hashed2\n"));
5fd012e0 524 vx_put(vp);
984263bc
MD
525 return (0);
526 }
056f4388 527 } while(LOCKMGR(&hpfs_hphash_lock,LK_EXCLUSIVE|LK_SLEEPFAIL,NULL));
984263bc
MD
528
529 hpfs_hphashins(hp);
530
056f4388 531 LOCKMGR(&hpfs_hphash_lock, LK_RELEASE, NULL);
984263bc 532
54078292 533 error = bread(hpmp->hpm_devvp, dbtodoff(ino), FNODESIZE, &bp);
984263bc 534 if (error) {
f91a71dd 535 printf("hpfs_vget: can't read ino %"PRId64"\n",ino);
5fd012e0 536 vx_put(vp);
984263bc
MD
537 return (error);
538 }
539 bcopy(bp->b_data, &hp->h_fn, sizeof(struct fnode));
540 brelse(bp);
541
542 if (hp->h_fn.fn_magic != FN_MAGIC) {
543 printf("hpfs_vget: MAGIC DOESN'T MATCH\n");
5fd012e0 544 vx_put(vp);
984263bc
MD
545 return (EINVAL);
546 }
547
548 vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
549 hp->h_flag &= ~H_INVAL;
550
5fd012e0 551 /* Return the locked and refd vnode */
984263bc
MD
552 *vpp = vp;
553
554 return (0);
555}
556
984263bc 557static struct vfsops hpfs_vfsops = {
43c45e8f
HP
558 .vfs_mount = hpfs_mount,
559 .vfs_unmount = hpfs_unmount,
560 .vfs_root = hpfs_root,
561 .vfs_statfs = hpfs_statfs,
562 .vfs_sync = vfs_stdsync,
563 .vfs_vget = hpfs_vget,
564 .vfs_fhtovp = hpfs_fhtovp,
565 .vfs_checkexp = hpfs_checkexp,
566 .vfs_vptofh = hpfs_vptofh,
567 .vfs_init = hpfs_init,
568 .vfs_uninit = hpfs_hphash_uninit
984263bc 569};
984263bc 570
64b4604e 571VFS_SET(hpfs_vfsops, hpfs, 0);
984263bc 572