xref: /dflybsd-src/sys/vfs/hpfs/hpfs_vfsops.c (revision b370aff7747b2e03ce9b829fbf2877dffdadfb64)
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 $
27  * $DragonFly: src/sys/vfs/hpfs/hpfs_vfsops.c,v 1.43 2008/09/17 21:44:22 dillon Exp $
28  */
29 
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/nlookup.h>
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 <machine/inttypes.h>
44 
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>
53 
54 #include <sys/buf2.h>
55 
56 #if defined(__NetBSD__)
57 #include <miscfs/specfs/specdev.h>
58 #endif
59 
60 #include "hpfs.h"
61 #include "hpfsmount.h"
62 #include "hpfs_subr.h"
63 
64 extern struct vop_ops hpfs_vnode_vops;
65 
66 MALLOC_DEFINE(M_HPFSMNT, "HPFS mount", "HPFS mount structure");
67 MALLOC_DEFINE(M_HPFSNO, "HPFS node", "HPFS node structure");
68 
69 static int	hpfs_root (struct mount *, struct vnode **);
70 static int	hpfs_statfs (struct mount *, struct statfs *, struct ucred *);
71 static int	hpfs_unmount (struct mount *, int);
72 static int	hpfs_vget (struct mount *mp, struct vnode *,
73 				ino_t ino, struct vnode **vpp);
74 static int	hpfs_mountfs (struct vnode *, struct mount *,
75 				struct hpfs_args *);
76 static int	hpfs_vptofh (struct vnode *, struct fid *);
77 static int	hpfs_fhtovp (struct mount *, struct vnode *,
78 				struct fid *, struct vnode **);
79 
80 
81 struct sockaddr;
82 static int	hpfs_mount (struct mount *, char *, caddr_t, struct ucred *);
83 static int	hpfs_init (struct vfsconf *);
84 static int	hpfs_checkexp (struct mount *, struct sockaddr *,
85 				   int *, struct ucred **);
86 
87 /*ARGSUSED*/
88 static int
89 hpfs_checkexp(struct mount *mp,
90 	      struct sockaddr *nam,
91 	      int *exflagsp, struct ucred **credanonp)
92 {
93 	struct netcred *np;
94 	struct hpfsmount *hpm = VFSTOHPFS(mp);
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 
108 static int
109 hpfs_init(struct vfsconf *vcp)
110 {
111 	dprintf(("hpfs_init():\n"));
112 
113 	hpfs_hphashinit();
114 	return 0;
115 }
116 
117 static int
118 hpfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
119 {
120 	size_t		size;
121 	int		error;
122 	struct vnode	*devvp;
123 	struct hpfs_args args;
124 	struct hpfsmount *hpmp = 0;
125 	struct nlookupdata nd;
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*/
135 	error = copyin(data, (caddr_t)&args, sizeof (struct hpfs_args));
136 	if (error)
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));
150 			error = vfs_export(mp, &hpmp->hpm_export, &args.export);
151 			if (error) {
152 				kprintf("hpfs_mount: vfs_export failed %d\n",
153 					error);
154 			}
155 			goto success;
156 		} else {
157 			dprintf(("name [FAILED]\n"));
158 			error = EINVAL;
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 	 */
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_nch, nd.nl_cred, &devvp);
174 	nlookup_done(&nd);
175 	if (error)
176 		goto error_1;
177 
178 	if (!vn_isdisk(devvp, &error))
179 		goto error_2;
180 
181 	/*
182 	 ********************
183 	 * NEW MOUNT
184 	 ********************
185 	 */
186 
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 
194 	error = hpfs_mountfs(devvp, mp, &args);
195 	if (error)
196 		goto error_2;
197 
198 	/*
199 	 * Initialize FS stat information in mount struct; uses
200 	 * mp->mnt_stat.f_mntfromname.
201 	 *
202 	 * This code is common to root and non-root mounts
203 	 */
204 	VFS_STATFS(mp, &mp->mnt_stat, cred);
205 	return (error);
206 
207 error_2:	/* error with devvp held*/
208 
209 	/* release devvp before failing*/
210 	vrele(devvp);
211 
212 error_1:	/* no state to back out*/
213 
214 success:
215 	return (error);
216 }
217 
218 /*
219  * Common code for mount and mountroot
220  */
221 int
222 hpfs_mountfs(struct vnode *devvp, struct mount *mp, struct hpfs_args *argsp)
223 {
224 	int error, ncount, ronly;
225 	struct sublock *sup;
226 	struct spblock *spp;
227 	struct hpfsmount *hpmp;
228 	struct buf *bp = NULL;
229 	struct vnode *vp;
230 	cdev_t dev;
231 
232 	dprintf(("hpfs_mountfs():\n"));
233 	/*
234 	 * Disallow multiple mounts of the same device.
235 	 * Disallow mounting of a device that is currently in use
236 	 * (except for root, which might share swap device for miniroot).
237 	 * Flush out any old buffers remaining from a previous use.
238 	 */
239 	error = vfs_mountedon(devvp);
240 	if (error)
241 		return (error);
242 	ncount = vcount(devvp);
243 	if (devvp->v_object)
244 		ncount -= 1;
245 	if (ncount > 0)
246 		return (EBUSY);
247 
248 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
249 	error = vinvalbuf(devvp, V_SAVE, 0, 0);
250 	VOP__UNLOCK(devvp, 0);
251 	if (error)
252 		return (error);
253 
254 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
255 	VN_LOCK(devvp, LK_EXCLUSIVE | LK_RETRY);
256 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
257 	VOP__UNLOCK(devvp, 0);
258 	if (error)
259 		return (error);
260 	dev = devvp->v_rdev;
261 
262 	/*
263 	 * Do actual mount
264 	 */
265 	hpmp = kmalloc(sizeof(struct hpfsmount), M_HPFSMNT, M_WAITOK | M_ZERO);
266 
267 	/* Read in SuperBlock */
268 	error = bread(devvp, dbtodoff(SUBLOCK), SUSIZE, &bp);
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 */
275 	error = bread(devvp, dbtodoff(SPBLOCK), SPSIZE, &bp);
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 		kprintf("hpfs_mountfs: SuperBlock MAGIC DOESN'T MATCH\n");
287 		error = EINVAL;
288 		goto failed;
289 	}
290 	if (spp->sp_magic != SP_MAGIC) {
291 		kprintf("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;
298 	hpmp->hpm_dev = dev;
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 	}
313 	vfs_add_vnodeops(mp, &hpfs_vnode_vops, &mp->mnt_vn_norm_ops);
314 
315 	error = hpfs_root(mp, &vp);
316 	if (error) {
317 		hpfs_cpdeinit(hpmp);
318 		hpfs_bmdeinit(hpmp);
319 		goto failed;
320 	}
321 
322 	vput(vp);
323 
324 	mp->mnt_stat.f_fsid.val[0] = (long)dev2udev(dev);
325 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
326 	mp->mnt_maxsymlinklen = 0;
327 	mp->mnt_flag |= MNT_LOCAL;
328 	dev->si_mountpoint = mp;
329 	return (0);
330 
331 failed:
332 	if (bp)
333 		brelse (bp);
334 	mp->mnt_data = (qaddr_t)NULL;
335 	dev->si_mountpoint = NULL;
336 	VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE);
337 	return (error);
338 }
339 
340 
341 static int
342 hpfs_unmount(struct mount *mp, int mntflags)
343 {
344 	int error, flags, ronly;
345 	struct hpfsmount *hpmp = VFSTOHPFS(mp);
346 
347 	dprintf(("hpfs_unmount():\n"));
348 
349 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
350 
351 	flags = 0;
352 	if(mntflags & MNT_FORCE)
353 		flags |= FORCECLOSE;
354 
355 	dprintf(("hpfs_unmount: vflushing...\n"));
356 
357 	error = vflush(mp, 0, flags);
358 	if (error) {
359 		kprintf("hpfs_unmount: vflush failed: %d\n",error);
360 		return (error);
361 	}
362 
363 	hpmp->hpm_devvp->v_rdev->si_mountpoint = NULL;
364 
365 	vinvalbuf(hpmp->hpm_devvp, V_SAVE, 0, 0);
366 	error = VOP_CLOSE(hpmp->hpm_devvp, ronly ? FREAD : FREAD|FWRITE);
367 
368 	vrele(hpmp->hpm_devvp);
369 
370 	dprintf(("hpfs_umount: freeing memory...\n"));
371 	hpfs_cpdeinit(hpmp);
372 	hpfs_bmdeinit(hpmp);
373 	mp->mnt_data = (qaddr_t)0;
374 	mp->mnt_flag &= ~MNT_LOCAL;
375 	FREE(hpmp, M_HPFSMNT);
376 
377 	return (0);
378 }
379 
380 static int
381 hpfs_root(struct mount *mp, struct vnode **vpp)
382 {
383 	int error = 0;
384 	struct hpfsmount *hpmp = VFSTOHPFS(mp);
385 
386 	dprintf(("hpfs_root():\n"));
387 	error = VFS_VGET(mp, NULL, (ino_t)hpmp->hpm_su.su_rootfno, vpp);
388 	if(error) {
389 		kprintf("hpfs_root: VFS_VGET failed: %d\n",error);
390 		return (error);
391 	}
392 
393 	return (error);
394 }
395 
396 static int
397 hpfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
398 {
399 	struct hpfsmount *hpmp = VFSTOHPFS(mp);
400 
401 	dprintf(("hpfs_statfs(): HPFS%d.%d\n",
402 		hpmp->hpm_su.su_hpfsver, hpmp->hpm_su.su_fnctver));
403 
404 	sbp->f_type = mp->mnt_vfc->vfc_typenum;
405 	sbp->f_bsize = DEV_BSIZE;
406 	sbp->f_iosize = DEV_BSIZE;
407 	sbp->f_blocks = hpmp->hpm_su.su_btotal;
408 	sbp->f_bfree = sbp->f_bavail = hpmp->hpm_bavail;
409 	sbp->f_ffree = 0;
410 	sbp->f_files = 0;
411 	if (sbp != &mp->mnt_stat) {
412 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
413 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
414 	}
415 	sbp->f_flags = mp->mnt_flag;
416 
417 	return (0);
418 }
419 
420 
421 /*ARGSUSED*/
422 static int
423 hpfs_fhtovp(struct mount *mp, struct vnode *rootvp,
424 	    struct fid *fhp, struct vnode **vpp)
425 {
426 	struct vnode *nvp;
427 	struct hpfid *hpfhp = (struct hpfid *)fhp;
428 	int error;
429 
430 	if ((error = VFS_VGET(mp, NULL, 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 
441 static int
442 hpfs_vptofh(struct vnode *vp, struct fid *fhp)
443 {
444 	struct hpfsnode *hpp;
445 	struct hpfid *hpfhp;
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 
455 static int
456 hpfs_vget(struct mount *mp, struct vnode *dvp, ino_t ino, struct vnode **vpp)
457 {
458 	struct hpfsmount *hpmp = VFSTOHPFS(mp);
459 	struct vnode *vp;
460 	struct hpfsnode *hp;
461 	struct buf *bp;
462 	int error;
463 
464 	dprintf(("hpfs_vget(0x%x): ",ino));
465 
466 	*vpp = NULL;
467 	hp = NULL;
468 	vp = NULL;
469 
470 	if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) {
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 
490 	error = getnewvnode(VT_HPFS, hpmp->hpm_mp, &vp, VLKTIMEOUT, 0);
491 	if (error) {
492 		kprintf("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 		vsetflags(vp, VROOT);
503 
504 	lwkt_token_init(&hp->h_interlock, "hpfsilock");
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;
515 	vref(hp->h_devvp);
516 
517 	do {
518 		if ((*vpp = hpfs_hphashvget(hpmp->hpm_dev, ino)) != NULL) {
519 			dprintf(("hashed2\n"));
520 			vx_put(vp);
521 			return (0);
522 		}
523 	} while (LOCKMGR(&hpfs_hphash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL));
524 
525 	hpfs_hphashins(hp);
526 
527 	LOCKMGR(&hpfs_hphash_lock, LK_RELEASE);
528 
529 	error = bread(hpmp->hpm_devvp, dbtodoff(ino), FNODESIZE, &bp);
530 	if (error) {
531 		kprintf("hpfs_vget: can't read ino %"PRId64"\n",ino);
532 		vx_put(vp);
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 		kprintf("hpfs_vget: MAGIC DOESN'T MATCH\n");
540 		vx_put(vp);
541 		return (EINVAL);
542 	}
543 
544 	vp->v_type = hp->h_fn.fn_flag ? VDIR:VREG;
545 	hp->h_flag &= ~H_INVAL;
546 
547 	/* Return the locked and refd vnode */
548 	*vpp = vp;
549 
550 	return (0);
551 }
552 
553 static struct vfsops hpfs_vfsops = {
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
565 };
566 
567 VFS_SET(hpfs_vfsops, hpfs, 0);
568 MODULE_VERSION(hpfs, 1);
569