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