Home | History | Annotate | Line # | Download | only in fsck_v7fs
pathname.c revision 1.2
      1  1.2  hannken /*	$NetBSD: pathname.c,v 1.2 2022/02/11 10:55:15 hannken 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.1      uch #include <sys/cdefs.h>
     33  1.1      uch #ifndef lint
     34  1.2  hannken __RCSID("$NetBSD: pathname.c,v 1.2 2022/02/11 10:55:15 hannken Exp $");
     35  1.1      uch #endif /* not lint */
     36  1.1      uch 
     37  1.1      uch #include <sys/types.h>
     38  1.1      uch #include <sys/syslimits.h>	/*PATH_MAX */
     39  1.1      uch #include <stdio.h>
     40  1.1      uch #include <string.h>
     41  1.1      uch #include <time.h>
     42  1.1      uch 
     43  1.1      uch #include "v7fs.h"
     44  1.1      uch #include "v7fs_impl.h"
     45  1.1      uch #include "v7fs_inode.h"
     46  1.1      uch #include "v7fs_datablock.h"
     47  1.1      uch #include "v7fs_superblock.h"
     48  1.1      uch #include "v7fs_dirent.h"
     49  1.1      uch #include "v7fs_file.h"
     50  1.1      uch #include "fsck_v7fs.h"
     51  1.1      uch 
     52  1.1      uch /*
     53  1.1      uch  * Check pathname. for each inode, check parent, if it is directory,
     54  1.1      uch  * check child.
     55  1.1      uch  */
     56  1.1      uch 
     57  1.1      uch static int
     58  1.1      uch connect_lost_and_found(struct v7fs_self *fs, v7fs_ino_t ino)
     59  1.1      uch {
     60  1.1      uch 	char name[V7FS_NAME_MAX];
     61  1.1      uch 	v7fs_time_t t;
     62  1.1      uch 
     63  1.1      uch 	if (!lost_and_found.mode || !reply("CONNECT?"))
     64  1.1      uch 		return FSCK_EXIT_CHECK_FAILED;
     65  1.1      uch 
     66  1.1      uch 	snprintf(name, sizeof(name), "%d", ino);
     67  1.2  hannken 	v7fs_directory_add_entry(fs, &lost_and_found, ino, name, strlen(name));
     68  1.1      uch 	t = (v7fs_time_t)time(NULL);
     69  1.1      uch 	lost_and_found.mtime = lost_and_found.atime = t;
     70  1.1      uch 	v7fs_inode_writeback(fs, &lost_and_found);
     71  1.1      uch 	v7fs_superblock_writeback(fs); /* # of freeblocks may change. */
     72  1.1      uch 
     73  1.1      uch 	return 0;
     74  1.1      uch }
     75  1.1      uch 
     76  1.1      uch /* Check child (dir) */
     77  1.1      uch struct lookup_child_arg {
     78  1.1      uch 	int dir_cnt;
     79  1.1      uch 	bool print;
     80  1.1      uch 	struct v7fs_inode *parent;
     81  1.1      uch };
     82  1.1      uch 
     83  1.1      uch static int
     84  1.1      uch lookup_child_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
     85  1.1      uch {
     86  1.1      uch 	struct lookup_child_arg *arg = (struct lookup_child_arg *)ctx;
     87  1.1      uch 	void *buf;
     88  1.1      uch 	int error = 0;
     89  1.1      uch 
     90  1.1      uch 	if (!(buf = scratch_read(fs, blk)))
     91  1.1      uch 		return 0;
     92  1.1      uch 	struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
     93  1.1      uch 	size_t i, n = sz / sizeof(*dir);
     94  1.1      uch 	if (!v7fs_dirent_endian_convert(fs, dir, n)) {
     95  1.1      uch 		pwarn("*** bogus entry found *** dir#%d entry=%zu\n",
     96  1.1      uch 		    arg->parent->inode_number, n);
     97  1.1      uch 		arg->print = true;
     98  1.1      uch 	}
     99  1.1      uch 
    100  1.1      uch 	for (i = 0; i < n; i++, dir++) {
    101  1.1      uch 		struct v7fs_inode inode;
    102  1.1      uch 		if (arg->print)
    103  1.1      uch 			pwarn("%s %d\n", dir->name, dir->inode_number);
    104  1.1      uch 		/* Bogus enties are removed here. */
    105  1.1      uch 		if ((error = v7fs_inode_load(fs, &inode, dir->inode_number)))
    106  1.1      uch 		{
    107  1.1      uch 			pwarn("entry #%d not found.", dir->inode_number);
    108  1.1      uch 			if (reply("REMOVE?"))
    109  1.1      uch 				v7fs_directory_remove_entry(fs, arg->parent,
    110  1.2  hannken 				    dir->name, strlen(dir->name));
    111  1.1      uch 		} else {
    112  1.1      uch 			/* Count child dir. */
    113  1.1      uch 			if (v7fs_inode_isdir(&inode))
    114  1.1      uch 				arg->dir_cnt++;
    115  1.1      uch 		}
    116  1.1      uch 	}
    117  1.1      uch 	scratch_free(fs, buf);
    118  1.1      uch 
    119  1.1      uch 	return error;
    120  1.1      uch }
    121  1.1      uch 
    122  1.1      uch static int
    123  1.1      uch lookup_child_from_dir(struct v7fs_self *fs, struct v7fs_inode *p, bool print)
    124  1.1      uch {
    125  1.1      uch 	struct lookup_child_arg arg = { .dir_cnt = 0, .print = print,
    126  1.1      uch 	    .parent = p };
    127  1.1      uch 
    128  1.1      uch 	v7fs_datablock_foreach(fs, p, lookup_child_subr, &arg);
    129  1.1      uch 
    130  1.1      uch 	return arg.dir_cnt;
    131  1.1      uch }
    132  1.1      uch 
    133  1.1      uch /* Find parent directory (file) */
    134  1.1      uch struct lookup_parent_arg {
    135  1.1      uch 	v7fs_ino_t target_ino;
    136  1.1      uch 	v7fs_ino_t parent_ino;
    137  1.1      uch };
    138  1.1      uch 
    139  1.1      uch static int
    140  1.1      uch lookup_parent_from_file_subr(struct v7fs_self *fs, void *ctx,
    141  1.1      uch     struct v7fs_inode *p, v7fs_ino_t ino)
    142  1.1      uch {
    143  1.1      uch 	struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
    144  1.1      uch 
    145  1.1      uch 	if (!v7fs_inode_isdir(p))
    146  1.1      uch 		return 0;
    147  1.1      uch 
    148  1.1      uch 	if (v7fs_file_lookup_by_number(fs, p, arg->target_ino, NULL)) {
    149  1.1      uch 		arg->parent_ino = ino; /* My inode found. */
    150  1.1      uch 		return V7FS_ITERATOR_BREAK;
    151  1.1      uch 	}
    152  1.1      uch 
    153  1.1      uch 	return 0; /* not found. */
    154  1.1      uch }
    155  1.1      uch 
    156  1.1      uch 
    157  1.1      uch static v7fs_ino_t
    158  1.1      uch lookup_parent_from_file(struct v7fs_self *fs, v7fs_ino_t ino)
    159  1.1      uch {
    160  1.1      uch 	struct lookup_parent_arg arg = { .target_ino = ino, .parent_ino = 0 };
    161  1.1      uch 
    162  1.1      uch 	v7fs_ilist_foreach(fs, lookup_parent_from_file_subr, &arg);
    163  1.1      uch 
    164  1.1      uch 	return arg.parent_ino;
    165  1.1      uch }
    166  1.1      uch 
    167  1.1      uch /* Find parent directory (dir) */
    168  1.1      uch static int
    169  1.1      uch lookup_parent_from_dir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
    170  1.1      uch     size_t sz)
    171  1.1      uch {
    172  1.1      uch 	struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
    173  1.1      uch 	void *buf;
    174  1.1      uch 	int ret = 0;
    175  1.1      uch 
    176  1.1      uch 	if (!(buf = scratch_read(fs, blk)))
    177  1.1      uch 		return 0;
    178  1.1      uch 	struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
    179  1.1      uch 	size_t i, n = sz / sizeof(*dir);
    180  1.1      uch 	if (!v7fs_dirent_endian_convert(fs, dir, n)) {
    181  1.1      uch 		scratch_free(fs, buf);
    182  1.1      uch 		return V7FS_ITERATOR_ERROR;
    183  1.1      uch 	}
    184  1.1      uch 
    185  1.1      uch 	for (i = 0; i < n; i++, dir++) {
    186  1.1      uch 		if (strncmp(dir->name, "..", 2) != 0)
    187  1.1      uch 			continue;
    188  1.1      uch 
    189  1.1      uch 		arg->parent_ino = dir->inode_number;
    190  1.1      uch 		ret = V7FS_ITERATOR_BREAK;
    191  1.1      uch 		break;
    192  1.1      uch 	}
    193  1.1      uch 
    194  1.1      uch 	scratch_free(fs, buf);
    195  1.1      uch 	return ret;
    196  1.1      uch }
    197  1.1      uch 
    198  1.1      uch static v7fs_ino_t
    199  1.1      uch lookup_parent_from_dir(struct v7fs_self *fs, struct v7fs_inode *p)
    200  1.1      uch {
    201  1.1      uch 	struct lookup_parent_arg arg = { .target_ino = 0, .parent_ino = 0 };
    202  1.1      uch 
    203  1.1      uch 	/* Search parent("..") from my dirent. */
    204  1.1      uch 	v7fs_datablock_foreach(fs, p, lookup_parent_from_dir_subr, &arg);
    205  1.1      uch 
    206  1.1      uch 	return arg.parent_ino;
    207  1.1      uch }
    208  1.1      uch 
    209  1.1      uch static int
    210  1.1      uch pathname_check_file(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t ino)
    211  1.1      uch {
    212  1.1      uch 	v7fs_ino_t parent_ino;
    213  1.1      uch 	struct v7fs_inode parent_inode;
    214  1.1      uch 	int error = 0;
    215  1.1      uch 
    216  1.1      uch 	if (ino == 1)	/* reserved. */
    217  1.1      uch 		return 0;
    218  1.1      uch 
    219  1.1      uch 	/* Check parent. */
    220  1.1      uch 	if (!(parent_ino = lookup_parent_from_file(fs, ino)) ||
    221  1.1      uch 	    (error = v7fs_inode_load(fs, &parent_inode, parent_ino)) ||
    222  1.1      uch 	    !v7fs_inode_isdir(&parent_inode)) {
    223  1.1      uch 		pwarn("*** inode#%d don't have parent.", ino);
    224  1.1      uch 		v7fs_inode_dump(p);
    225  1.1      uch 		error = connect_lost_and_found(fs, ino);
    226  1.1      uch 	}
    227  1.1      uch 
    228  1.1      uch 	return error;
    229  1.1      uch }
    230  1.1      uch 
    231  1.1      uch static int
    232  1.1      uch pathname_check_dir(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t ino)
    233  1.1      uch {
    234  1.1      uch 	v7fs_ino_t parent_ino;
    235  1.1      uch 	struct v7fs_inode parent_inode;
    236  1.1      uch 	int error = 0;
    237  1.1      uch 
    238  1.1      uch 	/* Check parent */
    239  1.1      uch 	if (!(parent_ino = lookup_parent_from_dir(fs, p)) ||
    240  1.1      uch 	    (error = v7fs_inode_load(fs, &parent_inode, parent_ino)) ||
    241  1.1      uch 	    !v7fs_inode_isdir(&parent_inode)) {
    242  1.1      uch 		pwarn("*** ino#%d parent dir missing parent=%d", ino,
    243  1.1      uch 		    parent_ino);
    244  1.1      uch 		/* link to lost+found */
    245  1.1      uch 		v7fs_inode_dump(p);
    246  1.1      uch 		if ((error = connect_lost_and_found(fs, ino)))
    247  1.1      uch 			return error;
    248  1.1      uch 	}
    249  1.1      uch 
    250  1.1      uch 	/* Check child */
    251  1.1      uch 	int cnt = lookup_child_from_dir(fs, p, false);
    252  1.1      uch 	if ((error = (cnt != p->nlink))) {
    253  1.1      uch 		pwarn("*** ino#%d corrupt link # of child"
    254  1.1      uch 		    " dir:%d(inode) != %d(cnt)", ino, p->nlink, cnt);
    255  1.1      uch 		v7fs_inode_dump(p);
    256  1.1      uch 		lookup_child_from_dir(fs, p, true);
    257  1.1      uch 
    258  1.1      uch 		if (reply("CORRECT?")) {
    259  1.1      uch 			p->nlink = cnt;
    260  1.1      uch 			v7fs_inode_writeback(fs, p);
    261  1.1      uch 			error = 0;
    262  1.1      uch 		} else {
    263  1.1      uch 			error = FSCK_EXIT_CHECK_FAILED;
    264  1.1      uch 		}
    265  1.1      uch 	}
    266  1.1      uch 
    267  1.1      uch 	return error;
    268  1.1      uch }
    269  1.1      uch 
    270  1.1      uch static int
    271  1.1      uch pathname_subr(struct v7fs_self *fs, void *ctx __unused, struct v7fs_inode *p,
    272  1.1      uch     v7fs_ino_t ino)
    273  1.1      uch {
    274  1.1      uch 	int error = 0;
    275  1.1      uch 
    276  1.1      uch 	if (!v7fs_inode_allocated(p))
    277  1.1      uch 		return 0;
    278  1.1      uch 
    279  1.1      uch 	progress(0);
    280  1.1      uch 
    281  1.1      uch 	if (v7fs_inode_isdir(p)) {
    282  1.1      uch 		error = pathname_check_dir(fs, p, ino);
    283  1.1      uch 	} else if (v7fs_inode_isfile(p)) {
    284  1.1      uch 		error = pathname_check_file(fs, p, ino);
    285  1.1      uch 	}
    286  1.1      uch 
    287  1.1      uch 	return error;
    288  1.1      uch }
    289  1.1      uch 
    290  1.1      uch int
    291  1.1      uch pathname_check(struct v7fs_self *fs)
    292  1.1      uch {
    293  1.1      uch 	struct v7fs_superblock *sb = &fs->superblock;
    294  1.1      uch 	int inodes = V7FS_MAX_INODE(sb) - sb->total_freeinode;
    295  1.1      uch 
    296  1.1      uch 	progress(&(struct progress_arg){ .label = "pathname", .tick = inodes /
    297  1.1      uch 	    PROGRESS_BAR_GRANULE });
    298  1.1      uch 
    299  1.1      uch 	return v7fs_ilist_foreach(fs, pathname_subr, 0);
    300  1.1      uch }
    301  1.1      uch 
    302  1.1      uch 
    303  1.1      uch char *
    304  1.1      uch filename(struct v7fs_self *fs, v7fs_ino_t ino)
    305  1.1      uch {
    306  1.1      uch 	static char path[V7FS_PATH_MAX];
    307  1.1      uch 	struct v7fs_inode inode;
    308  1.1      uch 	v7fs_ino_t parent;
    309  1.1      uch 	int error;
    310  1.1      uch 	char name[V7FS_NAME_MAX + 1];
    311  1.1      uch 	char *p = path + V7FS_PATH_MAX;
    312  1.1      uch 	size_t n;
    313  1.1      uch 
    314  1.1      uch 	if ((error = v7fs_inode_load(fs, &inode, ino)))
    315  1.1      uch 		return 0;
    316  1.1      uch 
    317  1.1      uch 	/* Lookup the 1st parent. */
    318  1.1      uch 	if (v7fs_inode_isdir(&inode))
    319  1.1      uch 		parent = lookup_parent_from_dir(fs, &inode);
    320  1.1      uch 	else
    321  1.1      uch 		parent = lookup_parent_from_file(fs, ino);
    322  1.1      uch 
    323  1.1      uch 	if ((error = v7fs_inode_load(fs, &inode, parent)))
    324  1.1      uch 		return 0;
    325  1.1      uch 
    326  1.1      uch 	if (!v7fs_file_lookup_by_number(fs, &inode, ino, name))
    327  1.1      uch 		return 0;
    328  1.1      uch 
    329  1.1      uch 	n = strlen(name) + 1;
    330  1.1      uch 	strcpy(p -= n, name);
    331  1.1      uch 
    332  1.1      uch 	/* Lookup until / */
    333  1.1      uch 	ino = parent;
    334  1.1      uch 	while (parent != V7FS_ROOT_INODE) {
    335  1.1      uch 		parent = lookup_parent_from_dir(fs, &inode);
    336  1.1      uch 		if ((error = v7fs_inode_load(fs, &inode, parent)))
    337  1.1      uch 			return 0;
    338  1.1      uch 		if (!v7fs_file_lookup_by_number(fs, &inode, ino, name))
    339  1.1      uch 			return 0;
    340  1.1      uch 		ino = parent;
    341  1.1      uch 		n = strlen(name) + 1;
    342  1.1      uch 		strcpy(p - n, name);
    343  1.1      uch 		p[-1] = '/';
    344  1.1      uch 		p -= n;
    345  1.1      uch 	}
    346  1.1      uch 	*--p = '/';
    347  1.1      uch 
    348  1.1      uch 	return p;
    349  1.1      uch }
    350