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