1*f1ca1ce2Sapb /* $NetBSD: v7fs_inode_util.c,v 1.2 2011/07/18 21:51:49 apb Exp $ */
29255b46fSuch
39255b46fSuch /*-
49255b46fSuch * Copyright (c) 2011 The NetBSD Foundation, Inc.
59255b46fSuch * All rights reserved.
69255b46fSuch *
79255b46fSuch * This code is derived from software contributed to The NetBSD Foundation
89255b46fSuch * by UCHIYAMA Yasushi.
99255b46fSuch *
109255b46fSuch * Redistribution and use in source and binary forms, with or without
119255b46fSuch * modification, are permitted provided that the following conditions
129255b46fSuch * are met:
139255b46fSuch * 1. Redistributions of source code must retain the above copyright
149255b46fSuch * notice, this list of conditions and the following disclaimer.
159255b46fSuch * 2. Redistributions in binary form must reproduce the above copyright
169255b46fSuch * notice, this list of conditions and the following disclaimer in the
179255b46fSuch * documentation and/or other materials provided with the distribution.
189255b46fSuch *
199255b46fSuch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209255b46fSuch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219255b46fSuch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229255b46fSuch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239255b46fSuch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249255b46fSuch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259255b46fSuch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269255b46fSuch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279255b46fSuch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289255b46fSuch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299255b46fSuch * POSSIBILITY OF SUCH DAMAGE.
309255b46fSuch */
319255b46fSuch
32*f1ca1ce2Sapb #if HAVE_NBTOOL_CONFIG_H
33*f1ca1ce2Sapb #include "nbtool_config.h"
34*f1ca1ce2Sapb #endif
35*f1ca1ce2Sapb
369255b46fSuch #include <sys/cdefs.h>
37*f1ca1ce2Sapb __KERNEL_RCSID(0, "$NetBSD: v7fs_inode_util.c,v 1.2 2011/07/18 21:51:49 apb Exp $");
389255b46fSuch #if defined _KERNEL_OPT
399255b46fSuch #include "opt_v7fs.h"
409255b46fSuch #endif
419255b46fSuch
429255b46fSuch #ifdef _KERNEL
439255b46fSuch #include <sys/systm.h>
449255b46fSuch #include <sys/param.h>
459255b46fSuch #else
469255b46fSuch #include <stdio.h>
479255b46fSuch #include <time.h>
489255b46fSuch #endif
499255b46fSuch
509255b46fSuch #include "v7fs.h"
519255b46fSuch #include "v7fs_impl.h"
529255b46fSuch #include "v7fs_inode.h"
539255b46fSuch
549255b46fSuch #ifdef V7FS_INODE_DEBUG
559255b46fSuch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
569255b46fSuch #else
579255b46fSuch #define DPRINTF(fmt, args...) ((void)0)
589255b46fSuch #endif
599255b46fSuch
609255b46fSuch void
v7fs_inode_chmod(struct v7fs_inode * inode,v7fs_mode_t mode)619255b46fSuch v7fs_inode_chmod(struct v7fs_inode *inode, v7fs_mode_t mode)
629255b46fSuch {
639255b46fSuch #define V7FS_MODE_MASK 0xfff
649255b46fSuch DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
659255b46fSuch
669255b46fSuch inode->mode &= ~V7FS_MODE_MASK;
679255b46fSuch inode->mode |= (mode & V7FS_MODE_MASK);
689255b46fSuch DPRINTF("setattr %08o -> %08o\n", inode->mode, mode);
699255b46fSuch }
709255b46fSuch
719255b46fSuch void
v7fs_inode_dump(const struct v7fs_inode * p)729255b46fSuch v7fs_inode_dump(const struct v7fs_inode *p)
739255b46fSuch {
749255b46fSuch printf("nlink %d mode %06o %d/%d %d bytes\n",
759255b46fSuch p->nlink, p->mode,
769255b46fSuch p->uid, p->gid, p->filesize);
779255b46fSuch
789255b46fSuch printf("atime %d mtime %d ctime %d\n",
799255b46fSuch p->atime, p->mtime, p->ctime);
809255b46fSuch #ifndef _KERNEL
819255b46fSuch time_t at = p->atime;
829255b46fSuch time_t mt = p->mtime;
839255b46fSuch time_t ct = p->ctime;
849255b46fSuch printf(" atime %s mtime %s ctime %s", ctime(&at), ctime(&mt),
859255b46fSuch ctime(&ct));
869255b46fSuch #endif
879255b46fSuch if (v7fs_inode_iscdev(p) || v7fs_inode_isbdev(p)) {
889255b46fSuch printf("device:%d/%d\n", (p->device >> 8), p->device & 0xff);
899255b46fSuch }
909255b46fSuch printf("\n");
919255b46fSuch }
929255b46fSuch
939255b46fSuch
949255b46fSuch int
v7fs_ilist_foreach(struct v7fs_self * fs,int (* func)(struct v7fs_self *,void *,struct v7fs_inode *,v7fs_ino_t),void * ctx)959255b46fSuch v7fs_ilist_foreach
969255b46fSuch (struct v7fs_self *fs,
979255b46fSuch int (*func)(struct v7fs_self *, void *, struct v7fs_inode *, v7fs_ino_t),
989255b46fSuch void *ctx)
999255b46fSuch {
1009255b46fSuch struct v7fs_superblock *sb = &fs->superblock;
1019255b46fSuch size_t i, j, k;
1029255b46fSuch int ret;
1039255b46fSuch
1049255b46fSuch /* Loop over ilist. */
1059255b46fSuch for (k = 1, i = V7FS_ILIST_SECTOR; i < sb->datablock_start_sector;
1069255b46fSuch i++) {
1079255b46fSuch struct v7fs_inode_diskimage *di;
1089255b46fSuch struct v7fs_inode inode;
1099255b46fSuch void *buf;
1109255b46fSuch
1119255b46fSuch if (!(buf = scratch_read(fs, i))) {
1129255b46fSuch DPRINTF("block %zu I/O error.\n", i);
1139255b46fSuch k += V7FS_INODE_PER_BLOCK;
1149255b46fSuch continue;
1159255b46fSuch }
1169255b46fSuch di = (struct v7fs_inode_diskimage *)buf;
1179255b46fSuch for (j = 0; j < V7FS_INODE_PER_BLOCK; j++, k++) {
1189255b46fSuch v7fs_inode_setup_memory_image(fs, &inode, di + j);
1199255b46fSuch inode.inode_number = k;
1209255b46fSuch if ((ret = func(fs, ctx, &inode, k))) {
1219255b46fSuch scratch_free(fs, buf);
1229255b46fSuch return ret;
1239255b46fSuch }
1249255b46fSuch }
1259255b46fSuch scratch_free(fs, buf);
1269255b46fSuch }
1279255b46fSuch return 0;
1289255b46fSuch }
129