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