xref: /netbsd-src/sys/fs/v7fs/v7fs_inode_util.c (revision b49cc1491953ef2348eff9c84520ffd0678a5c8d)
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_inode_util.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37 
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <sys/param.h>
41 #else
42 #include <stdio.h>
43 #include <time.h>
44 #endif
45 
46 #include "v7fs.h"
47 #include "v7fs_impl.h"
48 #include "v7fs_inode.h"
49 
50 #ifdef V7FS_INODE_DEBUG
51 #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
52 #else
53 #define	DPRINTF(fmt, args...)	((void)0)
54 #endif
55 
56 void
57 v7fs_inode_chmod(struct v7fs_inode *inode, v7fs_mode_t mode)
58 {
59 #define	V7FS_MODE_MASK	0xfff
60 	DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
61 
62 	inode->mode &= ~V7FS_MODE_MASK;
63 	inode->mode |= (mode & V7FS_MODE_MASK);
64 	DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
65 }
66 
67 void
68 v7fs_inode_dump(const struct v7fs_inode *p)
69 {
70 	printf("nlink %d mode %06o  %d/%d %d bytes\n",
71 	    p->nlink, p->mode,
72 	    p->uid, p->gid, p->filesize);
73 
74 	printf("atime %d mtime %d ctime %d\n",
75 	    p->atime, p->mtime, p->ctime);
76 #ifndef _KERNEL
77 	time_t at = p->atime;
78 	time_t mt = p->mtime;
79 	time_t ct = p->ctime;
80 	printf(" atime %s mtime %s ctime %s", ctime(&at), ctime(&mt),
81 	    ctime(&ct));
82 #endif
83 	if (v7fs_inode_iscdev(p) || v7fs_inode_isbdev(p)) {
84 		printf("device:%d/%d\n", (p->device >> 8), p->device & 0xff);
85 	}
86 	printf("\n");
87 }
88 
89 
90 int
91 v7fs_ilist_foreach
92 (struct v7fs_self *fs,
93     int (*func)(struct v7fs_self *, void *, struct v7fs_inode *, v7fs_ino_t),
94     void *ctx)
95 {
96 	struct v7fs_superblock *sb = &fs->superblock;
97 	size_t i, j, k;
98 	int ret;
99 
100 	/* Loop over ilist. */
101 	for (k = 1, i = V7FS_ILIST_SECTOR; i < sb->datablock_start_sector;
102 	    i++) {
103 		struct v7fs_inode_diskimage *di;
104 		struct v7fs_inode inode;
105 		void *buf;
106 
107 		if (!(buf = scratch_read(fs, i))) {
108 			DPRINTF("block %zu I/O error.\n", i);
109 			k += V7FS_INODE_PER_BLOCK;
110 			continue;
111 		}
112 		di = (struct v7fs_inode_diskimage *)buf;
113 		for (j = 0; j < V7FS_INODE_PER_BLOCK; j++, k++) {
114 			v7fs_inode_setup_memory_image(fs, &inode, di + j);
115 			inode.inode_number = k;
116 			if ((ret = func(fs, ctx, &inode, k))) {
117 				scratch_free(fs, buf);
118 				return ret;
119 			}
120 		}
121 		scratch_free(fs, buf);
122 	}
123 	return 0;
124 }
125