xref: /netbsd-src/sys/fs/nilfs/nilfs_vfsops.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /* $NetBSD: nilfs_vfsops.c,v 1.10 2012/12/20 08:03:43 hannken Exp $ */
2 
3 /*
4  * Copyright (c) 2008, 2009 Reinoud Zandijk
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __KERNEL_RCSID(0, "$NetBSD: nilfs_vfsops.c,v 1.10 2012/12/20 08:03:43 hannken Exp $");
32 #endif /* not lint */
33 
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <miscfs/genfs/genfs.h>
47 #include <miscfs/specfs/specdev.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/file.h>
51 #include <sys/device.h>
52 #include <sys/disklabel.h>
53 #include <sys/ioctl.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/stat.h>
57 #include <sys/conf.h>
58 #include <sys/kauth.h>
59 #include <sys/module.h>
60 
61 #include <fs/nilfs/nilfs_mount.h>
62 #include <sys/dirhash.h>
63 
64 
65 #include "nilfs.h"
66 #include "nilfs_subr.h"
67 #include "nilfs_bswap.h"
68 
69 MODULE(MODULE_CLASS_VFS, nilfs, NULL);
70 
71 #define VTOI(vnode) ((struct nilfs_node *) vnode->v_data)
72 
73 #define NILFS_SET_SYSTEMFILE(vp) { \
74 	/* XXXAD Is the vnode locked? */	\
75 	(vp)->v_vflag |= VV_SYSTEM;		\
76 	vref(vp);				\
77 	vput(vp); }
78 
79 #define NILFS_UNSET_SYSTEMFILE(vp) { \
80 	/* XXXAD Is the vnode locked? */	\
81 	(vp)->v_vflag &= ~VV_SYSTEM;		\
82 	vrele(vp); }
83 
84 
85 /* verbose levels of the nilfs filingsystem */
86 int nilfs_verbose = NILFS_DEBUGGING;
87 
88 /* malloc regions */
89 MALLOC_JUSTDEFINE(M_NILFSMNT,   "NILFS mount",	"NILFS mount structures");
90 MALLOC_JUSTDEFINE(M_NILFSTEMP,  "NILFS temp",	"NILFS scrap space");
91 struct pool nilfs_node_pool;
92 
93 /* globals */
94 struct _nilfs_devices nilfs_devices;
95 static struct sysctllog *nilfs_sysctl_log;
96 
97 /* supported functions predefined */
98 VFS_PROTOS(nilfs);
99 
100 
101 /* --------------------------------------------------------------------- */
102 
103 /* predefine vnode-op list descriptor */
104 extern const struct vnodeopv_desc nilfs_vnodeop_opv_desc;
105 
106 const struct vnodeopv_desc * const nilfs_vnodeopv_descs[] = {
107 	&nilfs_vnodeop_opv_desc,
108 	NULL,
109 };
110 
111 
112 /* vfsops descriptor linked in as anchor point for the filingsystem */
113 struct vfsops nilfs_vfsops = {
114 	MOUNT_NILFS,			/* vfs_name */
115 	sizeof (struct nilfs_args),
116 	nilfs_mount,
117 	nilfs_start,
118 	nilfs_unmount,
119 	nilfs_root,
120 	(void *)eopnotsupp,		/* vfs_quotactl */
121 	nilfs_statvfs,
122 	nilfs_sync,
123 	nilfs_vget,
124 	nilfs_fhtovp,
125 	nilfs_vptofh,
126 	nilfs_init,
127 	nilfs_reinit,
128 	nilfs_done,
129 	nilfs_mountroot,
130 	nilfs_snapshot,
131 	vfs_stdextattrctl,
132 	(void *)eopnotsupp,		/* vfs_suspendctl */
133 	genfs_renamelock_enter,
134 	genfs_renamelock_exit,
135 	(void *)eopnotsupp,		/* vfs_full_fsync */
136 	nilfs_vnodeopv_descs,
137 	0, /* int vfs_refcount   */
138 	{ NULL, NULL, }, /* LIST_ENTRY(vfsops) */
139 };
140 
141 /* --------------------------------------------------------------------- */
142 
143 /* file system starts here */
144 void
145 nilfs_init(void)
146 {
147 	size_t size;
148 
149 	/* setup memory types */
150 	malloc_type_attach(M_NILFSMNT);
151 	malloc_type_attach(M_NILFSTEMP);
152 
153 	/* init device lists */
154 	SLIST_INIT(&nilfs_devices);
155 
156 	/* init node pools */
157 	size = sizeof(struct nilfs_node);
158 	pool_init(&nilfs_node_pool, size, 0, 0, 0,
159 		"nilfs_node_pool", NULL, IPL_NONE);
160 }
161 
162 
163 void
164 nilfs_reinit(void)
165 {
166 	/* nothing to do */
167 }
168 
169 
170 void
171 nilfs_done(void)
172 {
173 	/* remove pools */
174 	pool_destroy(&nilfs_node_pool);
175 
176 	malloc_type_detach(M_NILFSMNT);
177 	malloc_type_detach(M_NILFSTEMP);
178 }
179 
180 /*
181  * If running a DEBUG kernel, provide an easy way to set the debug flags when
182  * running into a problem.
183  */
184 #define NILFS_VERBOSE_SYSCTLOPT        1
185 
186 static int
187 nilfs_modcmd(modcmd_t cmd, void *arg)
188 {
189 	const struct sysctlnode *node;
190 	int error;
191 
192 	switch (cmd) {
193 	case MODULE_CMD_INIT:
194 		error = vfs_attach(&nilfs_vfsops);
195 		if (error != 0)
196 			break;
197 		/*
198 		 * XXX the "30" below could be dynamic, thereby eliminating one
199 		 * more instance of the "number to vfs" mapping problem, but
200 		 * "30" is the order as taken from sys/mount.h
201 		 */
202 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, NULL,
203 			       CTLFLAG_PERMANENT,
204 			       CTLTYPE_NODE, "vfs", NULL,
205 			       NULL, 0, NULL, 0,
206 			       CTL_VFS, CTL_EOL);
207 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node,
208 			       CTLFLAG_PERMANENT,
209 			       CTLTYPE_NODE, "nilfs",
210 			       SYSCTL_DESCR("NTT's NILFSv2"),
211 			       NULL, 0, NULL, 0,
212 			       CTL_VFS, 30, CTL_EOL);
213 #ifdef DEBUG
214 		sysctl_createv(&nilfs_sysctl_log, 0, NULL, &node,
215 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
216 			       CTLTYPE_INT, "verbose",
217 			       SYSCTL_DESCR("Bitmask for filesystem debugging"),
218 			       NULL, 0, &nilfs_verbose, 0,
219 			       CTL_VFS, 30, NILFS_VERBOSE_SYSCTLOPT, CTL_EOL);
220 #endif
221 		break;
222 	case MODULE_CMD_FINI:
223 		error = vfs_detach(&nilfs_vfsops);
224 		if (error != 0)
225 			break;
226 		sysctl_teardown(&nilfs_sysctl_log);
227 		break;
228 	default:
229 		error = ENOTTY;
230 		break;
231 	}
232 
233 	return (error);
234 }
235 
236 /* --------------------------------------------------------------------- */
237 
238 int
239 nilfs_mountroot(void)
240 {
241 	return EOPNOTSUPP;
242 }
243 
244 /* --------------------------------------------------------------------- */
245 
246 /* system nodes */
247 static int
248 nilfs_create_system_nodes(struct nilfs_device *nilfsdev)
249 {
250 	int error;
251 
252 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_DAT_INO,
253 		&nilfsdev->super_root.sr_dat, &nilfsdev->dat_node);
254 	if (error)
255 		goto errorout;
256 
257 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_CPFILE_INO,
258 		&nilfsdev->super_root.sr_cpfile, &nilfsdev->cp_node);
259 	if (error)
260 		goto errorout;
261 
262 	error = nilfs_get_node_raw(nilfsdev, NULL, NILFS_SUFILE_INO,
263 		&nilfsdev->super_root.sr_sufile, &nilfsdev->su_node);
264 	if (error)
265 		goto errorout;
266 
267 	NILFS_SET_SYSTEMFILE(nilfsdev->dat_node->vnode);
268 	NILFS_SET_SYSTEMFILE(nilfsdev->cp_node->vnode);
269 	NILFS_SET_SYSTEMFILE(nilfsdev->su_node->vnode);
270 
271 	return 0;
272 errorout:
273 	nilfs_dispose_node(&nilfsdev->dat_node);
274 	nilfs_dispose_node(&nilfsdev->cp_node);
275 	nilfs_dispose_node(&nilfsdev->su_node);
276 
277 	return error;
278 }
279 
280 
281 static void
282 nilfs_release_system_nodes(struct nilfs_device *nilfsdev)
283 {
284 	if (!nilfsdev)
285 		return;
286 	if (nilfsdev->refcnt > 0)
287 		return;
288 
289 	if (nilfsdev->dat_node)
290 		NILFS_UNSET_SYSTEMFILE(nilfsdev->dat_node->vnode);
291 	if (nilfsdev->cp_node)
292 		NILFS_UNSET_SYSTEMFILE(nilfsdev->cp_node->vnode);
293 	if (nilfsdev->su_node)
294 		NILFS_UNSET_SYSTEMFILE(nilfsdev->su_node->vnode);
295 
296 	nilfs_dispose_node(&nilfsdev->dat_node);
297 	nilfs_dispose_node(&nilfsdev->cp_node);
298 	nilfs_dispose_node(&nilfsdev->su_node);
299 }
300 
301 
302 /* --------------------------------------------------------------------- */
303 
304 static int
305 nilfs_check_superblock_crc(struct nilfs_super_block *super)
306 {
307 	uint32_t super_crc, comp_crc;
308 
309 	/* check super block magic */
310 	if (nilfs_rw16(super->s_magic) != NILFS_SUPER_MAGIC)
311 		return 0;
312 
313 	/* preserve crc */
314 	super_crc  = nilfs_rw32(super->s_sum);
315 
316 	/* calculate */
317 	super->s_sum = 0;
318 	comp_crc = crc32_le(nilfs_rw32(super->s_crc_seed),
319 		(uint8_t *) super, nilfs_rw16(super->s_bytes));
320 
321 	/* restore */
322 	super->s_sum = nilfs_rw32(super_crc);
323 
324 	/* check CRC */
325 	return (super_crc == comp_crc);
326 }
327 
328 
329 
330 static int
331 nilfs_read_superblock(struct nilfs_device *nilfsdev)
332 {
333 	struct nilfs_super_block *super, tmp_super;
334 	struct buf *bp;
335 	uint64_t sb1off, sb2off;
336 	uint64_t last_cno1, last_cno2;
337 	uint64_t dev_blk;
338 	int dev_bsize, dev_blks;
339 	int sb1ok, sb2ok, swp;
340 	int error;
341 
342 	sb1off = NILFS_SB_OFFSET_BYTES;
343 	sb2off = NILFS_SB2_OFFSET_BYTES(nilfsdev->devsize);
344 
345 	dev_bsize = 1 << nilfsdev->devvp->v_mount->mnt_fs_bshift;
346 
347 	/* read our superblock regardless of backing device blocksize */
348 	dev_blk   = 0;
349 	dev_blks  = (sb1off + dev_bsize -1)/dev_bsize;
350 	error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp);
351 	if (error) {
352 		return error;
353 	}
354 
355 	/* copy read-in super block at the offset */
356 	super = &nilfsdev->super;
357 	memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES,
358 		sizeof(struct nilfs_super_block));
359 	brelse(bp, BC_AGE);
360 
361 	/* read our 2nd superblock regardless of backing device blocksize */
362 	dev_blk   = sb2off / dev_bsize;
363 	dev_blks  = 2;		/* assumption max one dev_bsize */
364 	error = bread(nilfsdev->devvp, dev_blk, dev_blks * dev_bsize, NOCRED, 0, &bp);
365 	if (error) {
366 		return error;
367 	}
368 
369 	/* copy read-in superblock2 at the offset */
370 	super = &nilfsdev->super2;
371 	memcpy(super, (uint8_t *) bp->b_data + NILFS_SB_OFFSET_BYTES,
372 		sizeof(struct nilfs_super_block));
373 	brelse(bp, BC_AGE);
374 
375 	sb1ok = nilfs_check_superblock_crc(&nilfsdev->super);
376 	sb2ok = nilfs_check_superblock_crc(&nilfsdev->super2);
377 
378 	last_cno1 = nilfs_rw64(nilfsdev->super.s_last_cno);
379 	last_cno2 = nilfs_rw64(nilfsdev->super2.s_last_cno);
380 	swp = sb2ok && (last_cno2 > last_cno1);
381 
382 	if (swp) {
383 		printf("nilfs warning: broken superblock, using spare\n");
384 		tmp_super = nilfsdev->super2;
385 		nilfsdev->super2 = nilfsdev->super;	/* why preserve? */
386 		nilfsdev->super  = tmp_super;
387 	}
388 
389 	if (!sb1ok && !sb2ok) {
390 		printf("nilfs: no valid superblocks found\n");
391 		return EINVAL;
392 	}
393 
394 	return 0;
395 }
396 
397 
398 /* XXX NOTHING from the system nodes should need to be written here */
399 static void
400 nilfs_unmount_base(struct nilfs_device *nilfsdev)
401 {
402 	int error;
403 
404 	if (!nilfsdev)
405 		return;
406 
407 	/* remove all our information */
408 	error = vinvalbuf(nilfsdev->devvp, 0, FSCRED, curlwp, 0, 0);
409 	KASSERT(error == 0);
410 
411 	/* release the device's system nodes */
412 	nilfs_release_system_nodes(nilfsdev);
413 
414 	/* TODO writeout super_block? */
415 }
416 
417 
418 static int
419 nilfs_mount_base(struct nilfs_device *nilfsdev,
420 		struct mount *mp, struct nilfs_args *args)
421 {
422 	struct lwp *l = curlwp;
423 	uint64_t last_pseg, last_cno, last_seq;
424 	uint32_t log_blocksize;
425 	int error;
426 
427 	/* flush out any old buffers remaining from a previous use. */
428 	if ((error = vinvalbuf(nilfsdev->devvp, V_SAVE, l->l_cred, l, 0, 0)))
429 		return error;
430 
431 	/* read in our superblock */
432 	error = nilfs_read_superblock(nilfsdev);
433 	if (error) {
434 		printf("nilfs_mount: can't read in super block : %d\n", error);
435 		return error;
436 	}
437 
438 	/* get our blocksize */
439 	log_blocksize = nilfs_rw32(nilfsdev->super.s_log_block_size);
440 	nilfsdev->blocksize   = (uint64_t) 1 << (log_blocksize + 10);
441 	/* TODO check superblock's blocksize limits */
442 
443 	/* calculate dat structure parameters */
444 	nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->dat_mdt,
445 			nilfs_rw16(nilfsdev->super.s_dat_entry_size));
446 	nilfs_calc_mdt_consts(nilfsdev, &nilfsdev->ifile_mdt,
447 			nilfs_rw16(nilfsdev->super.s_inode_size));
448 
449 	DPRINTF(VOLUMES, ("nilfs_mount: accepted super block\n"));
450 
451 	/* search for the super root and roll forward when needed */
452 	nilfs_search_super_root(nilfsdev);
453 
454 	nilfsdev->mount_state = nilfs_rw16(nilfsdev->super.s_state);
455 	if (nilfsdev->mount_state != NILFS_VALID_FS) {
456 		printf("FS is seriously damaged, needs repairing\n");
457 		printf("aborting mount\n");
458 		return EINVAL;
459 	}
460 
461 	/*
462 	 * FS should be ok now. The superblock and the last segsum could be
463 	 * updated from the repair so extract running values again.
464 	 */
465 	last_pseg = nilfs_rw64(nilfsdev->super.s_last_pseg); /*blknr */
466 	last_cno  = nilfs_rw64(nilfsdev->super.s_last_cno);
467 	last_seq  = nilfs_rw64(nilfsdev->super.s_last_seq);
468 
469 	nilfsdev->last_seg_seq = last_seq;
470 	nilfsdev->last_seg_num = nilfs_get_segnum_of_block(nilfsdev, last_pseg);
471 	nilfsdev->next_seg_num = nilfs_get_segnum_of_block(nilfsdev,
472 		nilfs_rw64(nilfsdev->last_segsum.ss_next));
473 	nilfsdev->last_cno     = last_cno;
474 
475 	DPRINTF(VOLUMES, ("nilfs_mount: accepted super root\n"));
476 
477 	/* create system vnodes for DAT, CP and SEGSUM */
478 	error = nilfs_create_system_nodes(nilfsdev);
479 	if (error)
480 		nilfs_unmount_base(nilfsdev);
481 	return error;
482 }
483 
484 
485 static void
486 nilfs_unmount_device(struct nilfs_device *nilfsdev)
487 {
488 	int error;
489 
490 	/* is there anything? */
491 	if (nilfsdev == NULL)
492 		return;
493 
494 	/* remove the device only if we're the last reference */
495 	nilfsdev->refcnt--;
496 	if (nilfsdev->refcnt >= 1)
497 		return;
498 
499 	/* unmount our base */
500 	nilfs_unmount_base(nilfsdev);
501 
502 	/* remove from our device list */
503 	SLIST_REMOVE(&nilfs_devices, nilfsdev, nilfs_device, next_device);
504 
505 	/* close device */
506 	DPRINTF(VOLUMES, ("closing device\n"));
507 
508 	/* remove our mount reference before closing device */
509 	nilfsdev->devvp->v_specmountpoint = NULL;
510 
511 	/* devvp is still locked by us */
512 	vn_lock(nilfsdev->devvp, LK_EXCLUSIVE | LK_RETRY);
513 	error = VOP_CLOSE(nilfsdev->devvp, FREAD | FWRITE, NOCRED);
514 	if (error)
515 		printf("Error during closure of device! error %d, "
516 		       "device might stay locked\n", error);
517 	DPRINTF(VOLUMES, ("device close ok\n"));
518 
519 	/* clear our mount reference and release device node */
520 	vput(nilfsdev->devvp);
521 
522 	/* free our device info */
523 	free(nilfsdev, M_NILFSMNT);
524 }
525 
526 
527 static int
528 nilfs_check_mounts(struct nilfs_device *nilfsdev, struct mount *mp,
529 	struct nilfs_args *args)
530 {
531 	struct nilfs_mount  *ump;
532 	uint64_t last_cno;
533 
534 	/* no double-mounting of the same checkpoint */
535 	STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) {
536 		if (ump->mount_args.cpno == args->cpno)
537 			return EBUSY;
538 	}
539 
540 	/* allow readonly mounts without questioning here */
541 	if (mp->mnt_flag & MNT_RDONLY)
542 		return 0;
543 
544 	/* readwrite mount you want */
545 	STAILQ_FOREACH(ump, &nilfsdev->mounts, next_mount) {
546 		/* only one RW mount on this device! */
547 		if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY)==0)
548 			return EROFS;
549 		/* RDONLY on last mountpoint is device busy */
550 		last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno);
551 		if (ump->mount_args.cpno == last_cno)
552 			return EBUSY;
553 	}
554 
555 	/* OK for now */
556 	return 0;
557 }
558 
559 
560 static int
561 nilfs_mount_device(struct vnode *devvp, struct mount *mp, struct nilfs_args *args,
562 	struct nilfs_device **nilfsdev_p)
563 {
564 	uint64_t psize;
565 	unsigned secsize;
566 	struct nilfs_device *nilfsdev;
567 	struct lwp *l = curlwp;
568 	int openflags, accessmode, error;
569 
570 	DPRINTF(VOLUMES, ("Mounting NILFS device\n"));
571 
572 	/* lookup device in our nilfs_mountpoints */
573 	*nilfsdev_p = NULL;
574 	SLIST_FOREACH(nilfsdev, &nilfs_devices, next_device)
575 		if (nilfsdev->devvp == devvp)
576 			break;
577 
578 	if (nilfsdev) {
579 		DPRINTF(VOLUMES, ("device already mounted\n"));
580 		error = nilfs_check_mounts(nilfsdev, mp, args);
581 		if (error)
582 			return error;
583 		nilfsdev->refcnt++;
584 		*nilfsdev_p = nilfsdev;
585 		return 0;
586 	}
587 
588 	DPRINTF(VOLUMES, ("no previous mounts on this device, mounting device\n"));
589 
590 	/* check if its a block device specified */
591 	if (devvp->v_type != VBLK) {
592 		vrele(devvp);
593 		return ENOTBLK;
594 	}
595 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
596 		vrele(devvp);
597 		return ENXIO;
598 	}
599 
600 	/*
601 	 * If mount by non-root, then verify that user has necessary
602 	 * permissions on the device.
603 	 */
604 	accessmode = VREAD;
605 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
606 		accessmode |= VWRITE;
607 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
608 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
609 	    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode));
610 	VOP_UNLOCK(devvp);
611 	if (error) {
612 		vrele(devvp);
613 		return error;
614 	}
615 
616 	/*
617 	 * Open device read-write; TODO how about upgrading later when needed?
618 	 */
619 	openflags = FREAD | FWRITE;
620 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
621 	error = VOP_OPEN(devvp, openflags, FSCRED);
622 	VOP_UNLOCK(devvp);
623 	if (error) {
624 		vrele(devvp);
625 		return error;
626 	}
627 
628 	/* opened ok, try mounting */
629 	nilfsdev = malloc(sizeof(*nilfsdev), M_NILFSMNT, M_WAITOK | M_ZERO);
630 
631 	/* initialise */
632 	nilfsdev->refcnt        = 1;
633 	nilfsdev->devvp         = devvp;
634 	nilfsdev->uncomitted_bl = 0;
635 	cv_init(&nilfsdev->sync_cv, "nilfssyn");
636 	STAILQ_INIT(&nilfsdev->mounts);
637 
638 	/* register nilfs_device in list */
639 	SLIST_INSERT_HEAD(&nilfs_devices, nilfsdev, next_device);
640 
641 	/* get our device's size */
642 	error = getdisksize(devvp, &psize, &secsize);
643 	if (error) {
644 		/* remove all our information */
645 		nilfs_unmount_device(nilfsdev);
646 		return EINVAL;
647 	}
648 
649 	nilfsdev->devsize = psize * secsize;
650 
651 	/* connect to the head for most recent files XXX really pass mp and args? */
652 	error = nilfs_mount_base(nilfsdev, mp, args);
653 	if (error) {
654 		/* remove all our information */
655 		nilfs_unmount_device(nilfsdev);
656 		return EINVAL;
657 	}
658 
659 	*nilfsdev_p = nilfsdev;
660 	DPRINTF(VOLUMES, ("NILFS device mounted ok\n"));
661 
662 	return 0;
663 }
664 
665 
666 static int
667 nilfs_mount_checkpoint(struct nilfs_mount *ump)
668 {
669 	struct nilfs_cpfile_header *cphdr;
670 	struct nilfs_checkpoint *cp;
671 	struct nilfs_inode  ifile_inode;
672 	struct nilfs_node  *cp_node;
673 	struct buf *bp;
674 	uint64_t ncp, nsn, fcpno, blocknr, last_cno;
675 	uint32_t off, dlen;
676 	int cp_per_block, error;
677 
678 	DPRINTF(VOLUMES, ("mount_nilfs: trying to mount checkpoint number "
679 		"%"PRIu64"\n", ump->mount_args.cpno));
680 
681 	cp_node = ump->nilfsdev->cp_node;
682 
683 	/* get cpfile header from 1st block of cp file */
684 	error = nilfs_bread(cp_node, 0, NOCRED, 0, &bp);
685 	if (error)
686 		return error;
687 	cphdr = (struct nilfs_cpfile_header *) bp->b_data;
688 	ncp = nilfs_rw64(cphdr->ch_ncheckpoints);
689 	nsn = nilfs_rw64(cphdr->ch_nsnapshots);
690 
691 	brelse(bp, BC_AGE);
692 
693 	DPRINTF(VOLUMES, ("mount_nilfs: checkpoint header read in\n"));
694 	DPRINTF(VOLUMES, ("\tNumber of checkpoints %"PRIu64"\n", ncp));
695 	DPRINTF(VOLUMES, ("\tNumber of snapshots   %"PRIu64"\n", nsn));
696 
697 	/* read in our specified checkpoint */
698 	dlen = nilfs_rw16(ump->nilfsdev->super.s_checkpoint_size);
699 	cp_per_block = ump->nilfsdev->blocksize / dlen;
700 
701 	fcpno = ump->mount_args.cpno + NILFS_CPFILE_FIRST_CHECKPOINT_OFFSET -1;
702 	blocknr =  fcpno / cp_per_block;
703 	off     = (fcpno % cp_per_block) * dlen;
704 
705 	error = nilfs_bread(cp_node, blocknr, NOCRED, 0, &bp);
706 	if (error) {
707 		printf("mount_nilfs: couldn't read cp block %"PRIu64"\n",
708 			fcpno);
709 		return EINVAL;
710 	}
711 
712 	/* needs to be a valid checkpoint */
713 	cp = (struct nilfs_checkpoint *) ((uint8_t *) bp->b_data + off);
714 	if (cp->cp_flags & NILFS_CHECKPOINT_INVALID) {
715 		printf("mount_nilfs: checkpoint marked invalid\n");
716 		brelse(bp, BC_AGE);
717 		return EINVAL;
718 	}
719 
720 	/* is this really the checkpoint we want? */
721 	if (nilfs_rw64(cp->cp_cno) != ump->mount_args.cpno) {
722 		printf("mount_nilfs: checkpoint file corrupt? "
723 			"expected cpno %"PRIu64", found cpno %"PRIu64"\n",
724 			ump->mount_args.cpno, nilfs_rw64(cp->cp_cno));
725 		brelse(bp, BC_AGE);
726 		return EINVAL;
727 	}
728 
729 	/* check if its a snapshot ! */
730 	last_cno = nilfs_rw64(ump->nilfsdev->super.s_last_cno);
731 	if (ump->mount_args.cpno != last_cno) {
732 		/* only allow snapshots if not mounting on the last cp */
733 		if ((cp->cp_flags & NILFS_CHECKPOINT_SNAPSHOT) == 0) {
734 			printf( "mount_nilfs: checkpoint %"PRIu64" is not a "
735 				"snapshot\n", ump->mount_args.cpno);
736 			brelse(bp, BC_AGE);
737 			return EINVAL;
738 		}
739 	}
740 
741 	ifile_inode = cp->cp_ifile_inode;
742 	brelse(bp, BC_AGE);
743 
744 	/* get ifile inode */
745 	error = nilfs_get_node_raw(ump->nilfsdev, NULL, NILFS_IFILE_INO,
746 		&ifile_inode, &ump->ifile_node);
747 	if (error) {
748 		printf("mount_nilfs: can't read ifile node\n");
749 		return EINVAL;
750 	}
751 	NILFS_SET_SYSTEMFILE(ump->ifile_node->vnode);
752 
753 	/* get root node? */
754 
755 	return 0;
756 }
757 
758 
759 static int
760 nilfs_stop_writing(struct nilfs_mount *ump)
761 {
762 	/* readonly mounts won't write */
763 	if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
764 		return 0;
765 
766 	DPRINTF(CALL, ("nilfs_stop_writing called for RW mount\n"));
767 
768 	/* TODO writeout super_block? */
769 	/* XXX no support for writing yet anyway */
770 	return 0;
771 }
772 
773 
774 /* --------------------------------------------------------------------- */
775 
776 
777 
778 #define MPFREE(a, lst) \
779 	if ((a)) free((a), lst);
780 static void
781 free_nilfs_mountinfo(struct mount *mp)
782 {
783 	struct nilfs_mount *ump = VFSTONILFS(mp);
784 
785 	if (ump == NULL)
786 		return;
787 
788 	mutex_destroy(&ump->ihash_lock);
789 	mutex_destroy(&ump->get_node_lock);
790 	MPFREE(ump, M_NILFSMNT);
791 }
792 #undef MPFREE
793 
794 int
795 nilfs_mount(struct mount *mp, const char *path,
796 	  void *data, size_t *data_len)
797 {
798 	struct nilfs_args   *args = data;
799 	struct nilfs_device *nilfsdev;
800 	struct nilfs_mount  *ump;
801 	struct vnode *devvp;
802 	int lst, error;
803 
804 	DPRINTF(VFSCALL, ("nilfs_mount called\n"));
805 
806 	if (*data_len < sizeof *args)
807 		return EINVAL;
808 
809 	if (mp->mnt_flag & MNT_GETARGS) {
810 		/* request for the mount arguments */
811 		ump = VFSTONILFS(mp);
812 		if (ump == NULL)
813 			return EINVAL;
814 		*args = ump->mount_args;
815 		*data_len = sizeof *args;
816 		return 0;
817 	}
818 
819 	/* check/translate struct version */
820 	if (args->version != 1) {
821 		printf("mount_nilfs: unrecognized argument structure version\n");
822 		return EINVAL;
823 	}
824 	/* TODO sanity checking other mount arguments */
825 
826 	/* handle request for updating mount parameters */
827 	if (mp->mnt_flag & MNT_UPDATE) {
828 		/* TODO can't update my mountpoint yet */
829 		return EOPNOTSUPP;
830 	}
831 
832 	/* lookup name to get its vnode */
833 	error = namei_simple_user(args->fspec, NSM_FOLLOW_NOEMULROOT, &devvp);
834 	if (error)
835 		return error;
836 
837 #ifdef DEBUG
838 	if (nilfs_verbose & NILFS_DEBUG_VOLUMES)
839 		vprint("NILFS mount, trying to mount \n", devvp);
840 #endif
841 
842 	error = nilfs_mount_device(devvp, mp, args, &nilfsdev);
843 	if (error)
844 		return error;
845 
846 	/*
847 	 * Create a nilfs_mount on the specified checkpoint. Note that only
848 	 * ONE RW mount point can exist and it needs to have the highest
849 	 * checkpoint nr. If mounting RW and its not on the last checkpoint we
850 	 * need to invalidate all checkpoints that follow!!! This is an
851 	 * advanced option.
852 	 */
853 
854 	/* setup basic mountpoint structure */
855 	mp->mnt_data = NULL;
856 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (uint32_t) devvp->v_rdev;
857 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_NILFS);
858 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
859 	mp->mnt_stat.f_namemax = NILFS_NAME_LEN;
860 	mp->mnt_flag  |= MNT_LOCAL;
861 
862 	/* XXX can't enable MPSAFE yet since genfs barfs on bad CV */
863 	// mp->mnt_iflag |= IMNT_MPSAFE;
864 
865 	/* set our dev and fs units */
866 	mp->mnt_dev_bshift = nilfs_rw32(nilfsdev->super.s_log_block_size) + 10;
867 	mp->mnt_fs_bshift  = mp->mnt_dev_bshift;
868 
869 	/* allocate nilfs part of mount structure; malloc always succeeds */
870 	ump = malloc(sizeof(struct nilfs_mount), M_NILFSMNT, M_WAITOK | M_ZERO);
871 
872 	/* init locks */
873 	mutex_init(&ump->ihash_lock, MUTEX_DEFAULT, IPL_NONE);
874 	mutex_init(&ump->get_node_lock, MUTEX_DEFAULT, IPL_NONE);
875 
876 	/* init our hash table for inode lookup */
877 	for (lst = 0; lst < NILFS_INODE_HASHSIZE; lst++) {
878 		LIST_INIT(&ump->nilfs_nodes[lst]);
879 	}
880 
881 	/* set up linkage */
882 	mp->mnt_data    =  ump;
883 	ump->vfs_mountp =  mp;
884 	ump->nilfsdev   =  nilfsdev;
885 
886 #if 0
887 #ifndef NILFS_READWRITE
888 	/* force read-only for now */
889 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
890 		printf( "Enable kernel/module option NILFS_READWRITE for "
891 			"writing, downgrading access to read-only\n");
892 		mp->mnt_flag |= MNT_RDONLY;
893 	}
894 #endif
895 #endif
896 
897 	/* DONT register our nilfs mountpoint on our vfs mountpoint */
898 	devvp->v_specmountpoint = NULL;
899 #if 0
900 	if (devvp->v_specmountpoint == NULL)
901 		devvp->v_specmountpoint = mp;
902 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
903 		devvp->v_specmountpoint = mp;
904 #endif
905 
906 	/* add our mountpoint */
907 	STAILQ_INSERT_TAIL(&nilfsdev->mounts, ump, next_mount);
908 
909 	/* get our selected checkpoint */
910 	if (args->cpno == 0)
911 		args->cpno = nilfsdev->last_cno;
912 	args->cpno = MIN(args->cpno, nilfsdev->last_cno);
913 
914 	/* setting up other parameters */
915 	ump->mount_args = *args;
916 	error = nilfs_mount_checkpoint(ump);
917 	if (error) {
918 		nilfs_unmount(mp, MNT_FORCE);
919 		return error;
920 	}
921 
922 	/* set VFS info */
923 	error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
924 			mp->mnt_op->vfs_name, mp, curlwp);
925 	if (error) {
926 		nilfs_unmount(mp, MNT_FORCE);
927 		return error;
928 	}
929 
930 	/* successfully mounted */
931 	DPRINTF(VOLUMES, ("nilfs_mount() successfull\n"));
932 
933 	return 0;
934 }
935 
936 /* --------------------------------------------------------------------- */
937 
938 
939 /* remove our mountpoint and if its the last reference, remove our device */
940 int
941 nilfs_unmount(struct mount *mp, int mntflags)
942 {
943 	struct nilfs_device *nilfsdev;
944 	struct nilfs_mount  *ump;
945 	int error, flags;
946 
947 	DPRINTF(VFSCALL, ("nilfs_umount called\n"));
948 
949 	ump = VFSTONILFS(mp);
950 	if (!ump)
951 		panic("NILFS unmount: empty ump\n");
952 	nilfsdev = ump->nilfsdev;
953 
954 	/*
955 	 * Flush all nodes associated to this mountpoint. By specifying
956 	 * SKIPSYSTEM we can skip vnodes marked with VV_SYSTEM. This hardly
957 	 * documented feature allows us to exempt certain files from being
958 	 * flushed.
959 	 */
960 	flags = (mntflags & MNT_FORCE) ? FORCECLOSE : 0;
961 	if ((error = vflush(mp, NULLVP, flags | SKIPSYSTEM)) != 0)
962 		return error;
963 
964 	/* if we're the write mount, we ought to close the writing session */
965 	error = nilfs_stop_writing(ump);
966 	if (error)
967 		return error;
968 
969 	if (ump->ifile_node)
970 		NILFS_UNSET_SYSTEMFILE(ump->ifile_node->vnode);
971 	nilfs_dispose_node(&ump->ifile_node);
972 
973 	/* remove our mount point */
974 	STAILQ_REMOVE(&nilfsdev->mounts, ump, nilfs_mount, next_mount);
975 	free_nilfs_mountinfo(mp);
976 
977 	/* free ump struct references */
978 	mp->mnt_data = NULL;
979 	mp->mnt_flag &= ~MNT_LOCAL;
980 
981 	/* unmount the device itself when we're the last one */
982 	nilfs_unmount_device(nilfsdev);
983 
984 	DPRINTF(VOLUMES, ("Fin unmount\n"));
985 	return error;
986 }
987 
988 /* --------------------------------------------------------------------- */
989 
990 int
991 nilfs_start(struct mount *mp, int flags)
992 {
993 	/* do we have to do something here? */
994 	return 0;
995 }
996 
997 /* --------------------------------------------------------------------- */
998 
999 int
1000 nilfs_root(struct mount *mp, struct vnode **vpp)
1001 {
1002 	struct nilfs_mount *ump = VFSTONILFS(mp);
1003 	struct nilfs_node  *node;
1004 	int error;
1005 
1006 	DPRINTF(NODE, ("nilfs_root called\n"));
1007 
1008 	error = nilfs_get_node(ump, NILFS_ROOT_INO, &node);
1009 	if (node)  {
1010 		*vpp = node->vnode;
1011 		KASSERT(node->vnode->v_vflag & VV_ROOT);
1012 	}
1013 
1014 	DPRINTF(NODE, ("nilfs_root finished\n"));
1015 	return error;
1016 }
1017 
1018 /* --------------------------------------------------------------------- */
1019 
1020 int
1021 nilfs_statvfs(struct mount *mp, struct statvfs *sbp)
1022 {
1023 	struct nilfs_mount *ump = VFSTONILFS(mp);
1024 	uint32_t blocksize;
1025 
1026 	DPRINTF(VFSCALL, ("nilfs_statvfs called\n"));
1027 
1028 	blocksize = ump->nilfsdev->blocksize;
1029 	sbp->f_flag   = mp->mnt_flag;
1030 	sbp->f_bsize  = blocksize;
1031 	sbp->f_frsize = blocksize;
1032 	sbp->f_iosize = blocksize;
1033 
1034 	copy_statvfs_info(sbp, mp);
1035 	return 0;
1036 }
1037 
1038 /* --------------------------------------------------------------------- */
1039 
1040 int
1041 nilfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1042 {
1043 //	struct nilfs_mount *ump = VFSTONILFS(mp);
1044 
1045 	DPRINTF(VFSCALL, ("nilfs_sync called\n"));
1046 	/* if called when mounted readonly, just ignore */
1047 	if (mp->mnt_flag & MNT_RDONLY)
1048 		return 0;
1049 
1050 	DPRINTF(VFSCALL, ("end of nilfs_sync()\n"));
1051 
1052 	return 0;
1053 }
1054 
1055 /* --------------------------------------------------------------------- */
1056 
1057 /*
1058  * Get vnode for the file system type specific file id ino for the fs. Its
1059  * used for reference to files by unique ID and for NFSv3.
1060  * (optional) TODO lookup why some sources state NFSv3
1061  */
1062 int
1063 nilfs_vget(struct mount *mp, ino_t ino,
1064     struct vnode **vpp)
1065 {
1066 	DPRINTF(NOTIMPL, ("nilfs_vget called\n"));
1067 	return EOPNOTSUPP;
1068 }
1069 
1070 /* --------------------------------------------------------------------- */
1071 
1072 /*
1073  * Lookup vnode for file handle specified
1074  */
1075 int
1076 nilfs_fhtovp(struct mount *mp, struct fid *fhp,
1077     struct vnode **vpp)
1078 {
1079 	DPRINTF(NOTIMPL, ("nilfs_fhtovp called\n"));
1080 	return EOPNOTSUPP;
1081 }
1082 
1083 /* --------------------------------------------------------------------- */
1084 
1085 /*
1086  * Create an unique file handle. Its structure is opaque and won't be used by
1087  * other subsystems. It should uniquely identify the file in the filingsystem
1088  * and enough information to know if a file has been removed and/or resources
1089  * have been recycled.
1090  */
1091 int
1092 nilfs_vptofh(struct vnode *vp, struct fid *fid,
1093     size_t *fh_size)
1094 {
1095 	DPRINTF(NOTIMPL, ("nilfs_vptofh called\n"));
1096 	return EOPNOTSUPP;
1097 }
1098 
1099 /* --------------------------------------------------------------------- */
1100 
1101 /*
1102  * Create a file system snapshot at the specified timestamp.
1103  */
1104 int
1105 nilfs_snapshot(struct mount *mp, struct vnode *vp,
1106     struct timespec *tm)
1107 {
1108 	DPRINTF(NOTIMPL, ("nilfs_snapshot called\n"));
1109 	return EOPNOTSUPP;
1110 }
1111 
1112 /* --------------------------------------------------------------------- */
1113