1/*	$NetBSD: v7fs_inode_util.c,v 1.1 2011/06/27 11:52:25 uch Exp $	*/
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_NBTOOL_CONFIG_H
33#include "nbtool_config.h"
34#endif
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: v7fs_inode_util.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
38#if defined _KERNEL_OPT
39#include "opt_v7fs.h"
40#endif
41
42#ifdef _KERNEL
43#include <sys/systm.h>
44#include <sys/param.h>
45#else
46#include <stdio.h>
47#include <time.h>
48#endif
49
50#include "v7fs.h"
51#include "v7fs_impl.h"
52#include "v7fs_inode.h"
53
54#ifdef V7FS_INODE_DEBUG
55#define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
56#else
57#define	DPRINTF(fmt, args...)	((void)0)
58#endif
59
60void
61v7fs_inode_chmod(struct v7fs_inode *inode, v7fs_mode_t mode)
62{
63#define	V7FS_MODE_MASK	0xfff
64	DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
65
66	inode->mode &= ~V7FS_MODE_MASK;
67	inode->mode |= (mode & V7FS_MODE_MASK);
68	DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
69}
70
71void
72v7fs_inode_dump(const struct v7fs_inode *p)
73{
74	printf("nlink %d mode %06o  %d/%d %d bytes\n",
75	    p->nlink, p->mode,
76	    p->uid, p->gid, p->filesize);
77
78	printf("atime %d mtime %d ctime %d\n",
79	    p->atime, p->mtime, p->ctime);
80#ifndef _KERNEL
81	time_t at = p->atime;
82	time_t mt = p->mtime;
83	time_t ct = p->ctime;
84	printf(" atime %s mtime %s ctime %s", ctime(&at), ctime(&mt),
85	    ctime(&ct));
86#endif
87	if (v7fs_inode_iscdev(p) || v7fs_inode_isbdev(p)) {
88		printf("device:%d/%d\n", (p->device >> 8), p->device & 0xff);
89	}
90	printf("\n");
91}
92
93
94int
95v7fs_ilist_foreach
96(struct v7fs_self *fs,
97    int (*func)(struct v7fs_self *, void *, struct v7fs_inode *, v7fs_ino_t),
98    void *ctx)
99{
100	struct v7fs_superblock *sb = &fs->superblock;
101	size_t i, j, k;
102	int ret;
103
104	/* Loop over ilist. */
105	for (k = 1, i = V7FS_ILIST_SECTOR; i < sb->datablock_start_sector;
106	    i++) {
107		struct v7fs_inode_diskimage *di;
108		struct v7fs_inode inode;
109		void *buf;
110
111		if (!(buf = scratch_read(fs, i))) {
112			DPRINTF("block %zu I/O error.\n", i);
113			k += V7FS_INODE_PER_BLOCK;
114			continue;
115		}
116		di = (struct v7fs_inode_diskimage *)buf;
117		for (j = 0; j < V7FS_INODE_PER_BLOCK; j++, k++) {
118			v7fs_inode_setup_memory_image(fs, &inode, di + j);
119			inode.inode_number = k;
120			if ((ret = func(fs, ctx, &inode, k))) {
121				scratch_free(fs, buf);
122				return ret;
123			}
124		}
125		scratch_free(fs, buf);
126	}
127	return 0;
128}
129