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