Home | History | Annotate | Line # | Download | only in v7fs
v7fs_file_util.c revision 1.4
      1  1.4  uch /*	$NetBSD: v7fs_file_util.c,v 1.4 2011/07/30 03:52:04 uch 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.3  apb #if HAVE_NBTOOL_CONFIG_H
     33  1.3  apb #include "nbtool_config.h"
     34  1.3  apb #endif
     35  1.3  apb 
     36  1.1  uch #include <sys/cdefs.h>
     37  1.4  uch __KERNEL_RCSID(0, "$NetBSD: v7fs_file_util.c,v 1.4 2011/07/30 03:52:04 uch Exp $");
     38  1.1  uch #ifdef _KERNEL
     39  1.1  uch #include <sys/systm.h>
     40  1.1  uch #include <sys/param.h>
     41  1.1  uch #else
     42  1.1  uch #include <stdio.h>
     43  1.1  uch #include <string.h>
     44  1.1  uch #include <errno.h>
     45  1.1  uch #endif
     46  1.1  uch 
     47  1.1  uch #include "v7fs.h"
     48  1.1  uch #include "v7fs_impl.h"
     49  1.1  uch #include "v7fs_endian.h"
     50  1.1  uch #include "v7fs_inode.h"
     51  1.1  uch #include "v7fs_dirent.h"
     52  1.1  uch #include "v7fs_file.h"
     53  1.1  uch #include "v7fs_datablock.h"
     54  1.1  uch 
     55  1.1  uch #ifdef V7FS_FILE_DEBUG
     56  1.1  uch #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
     57  1.1  uch #else
     58  1.1  uch #define	DPRINTF(fmt, args...)	((void)0)
     59  1.1  uch #endif
     60  1.1  uch 
     61  1.1  uch static int replace_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
     62  1.1  uch static int lookup_by_number_subr(struct v7fs_self *, void *, v7fs_daddr_t,
     63  1.1  uch     size_t);
     64  1.4  uch static int can_dirmove(struct v7fs_self *, v7fs_ino_t, v7fs_ino_t);
     65  1.4  uch static int lookup_parent_from_dir_subr(struct v7fs_self *, void *,
     66  1.4  uch     v7fs_daddr_t, size_t);
     67  1.1  uch 
     68  1.1  uch int
     69  1.1  uch v7fs_file_link(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
     70  1.1  uch     struct v7fs_inode *p, const char *name)
     71  1.1  uch {
     72  1.1  uch 	int error = 0;
     73  1.1  uch 
     74  1.1  uch 	DPRINTF("%d %d %s\n", parent_dir->inode_number, p->inode_number, name);
     75  1.1  uch 	if ((error = v7fs_directory_add_entry(fs, parent_dir, p->inode_number,
     76  1.1  uch 	    name))) {
     77  1.1  uch 		DPRINTF("can't add entry");
     78  1.1  uch 		return error;
     79  1.1  uch 	}
     80  1.1  uch 	p->nlink++;
     81  1.1  uch 	v7fs_inode_writeback(fs, p);
     82  1.1  uch 
     83  1.1  uch 	return 0;
     84  1.1  uch }
     85  1.1  uch 
     86  1.1  uch int
     87  1.2  uch v7fs_file_symlink(struct v7fs_self *fs, struct v7fs_inode *p,
     88  1.2  uch     const char *target)
     89  1.2  uch {
     90  1.2  uch 	int error;
     91  1.2  uch 	size_t len = strlen(target) + 1;
     92  1.2  uch 
     93  1.2  uch 	if (len > V7FSBSD_MAXSYMLINKLEN) {/* limited target 512byte pathname */
     94  1.2  uch 		DPRINTF("too long pathname.");
     95  1.2  uch 		return ENAMETOOLONG;
     96  1.2  uch 	}
     97  1.2  uch 
     98  1.2  uch 	if ((error = v7fs_datablock_expand(fs, p, len))) {
     99  1.2  uch 		return error;
    100  1.2  uch 	}
    101  1.2  uch 
    102  1.2  uch 	v7fs_daddr_t blk = p->addr[0];	/* 1block only.  */
    103  1.2  uch 	void *buf;
    104  1.2  uch 	if (!(buf = scratch_read(fs, blk))) {
    105  1.2  uch 		return EIO;
    106  1.2  uch 	}
    107  1.2  uch 
    108  1.2  uch 	strncpy(buf, target, V7FS_BSIZE);
    109  1.2  uch 	if (!fs->io.write(fs->io.cookie, buf, blk)) {
    110  1.2  uch 		scratch_free(fs, buf);
    111  1.2  uch 		return EIO;
    112  1.2  uch 	}
    113  1.2  uch 	scratch_free(fs, buf);
    114  1.2  uch 	v7fs_inode_writeback(fs, p);
    115  1.2  uch 
    116  1.2  uch 	return 0;
    117  1.2  uch }
    118  1.2  uch 
    119  1.2  uch int
    120  1.1  uch v7fs_file_rename(struct v7fs_self *fs, struct v7fs_inode *parent_from,
    121  1.1  uch     const char *from, struct v7fs_inode *parent_to, const char *to)
    122  1.1  uch {
    123  1.1  uch 	v7fs_ino_t from_ino, to_ino;
    124  1.4  uch 	struct v7fs_inode inode;
    125  1.1  uch 	int error;
    126  1.4  uch 	bool dir_move;
    127  1.1  uch 
    128  1.4  uch 	/* Check source file */
    129  1.1  uch 	if ((error = v7fs_file_lookup_by_name(fs, parent_from, from,
    130  1.1  uch 	    &from_ino))) {
    131  1.1  uch 		DPRINTF("%s don't exists\n", from);
    132  1.1  uch 		return error;
    133  1.1  uch 	}
    134  1.4  uch 	v7fs_inode_load(fs, &inode, from_ino);
    135  1.4  uch 	dir_move = v7fs_inode_isdir(&inode);
    136  1.1  uch 
    137  1.4  uch 	/* Check target file */
    138  1.1  uch 	error = v7fs_file_lookup_by_name(fs, parent_to, to, &to_ino);
    139  1.4  uch 	if (error == 0) {	/* found */
    140  1.1  uch 		DPRINTF("%s already exists\n", to);
    141  1.1  uch 		if ((error = v7fs_file_deallocate(fs, parent_to, to))) {
    142  1.4  uch 			DPRINTF("%s can't remove %d\n", to, error);
    143  1.1  uch 			return error;
    144  1.1  uch 		}
    145  1.1  uch 	} else if (error != ENOENT) {
    146  1.1  uch 		DPRINTF("error=%d\n", error);
    147  1.1  uch 		return error;
    148  1.1  uch 	}
    149  1.4  uch 	/* Check directory hierarchy. t_vnops rename_dir(5) */
    150  1.4  uch 	if (dir_move && (error = can_dirmove(fs, from_ino,
    151  1.4  uch 	    parent_to->inode_number))) {
    152  1.4  uch 		DPRINTF("dst '%s' is child dir of '%s'. error=%d\n", to, from,
    153  1.4  uch 		    error);
    154  1.4  uch 		return error;
    155  1.4  uch 	}
    156  1.1  uch 
    157  1.1  uch 	if ((error = v7fs_directory_add_entry(fs, parent_to, from_ino, to))) {
    158  1.1  uch 		DPRINTF("can't add entry");
    159  1.1  uch 		return error;
    160  1.1  uch 	}
    161  1.1  uch 
    162  1.1  uch 	if ((error = v7fs_directory_remove_entry(fs, parent_from, from))) {
    163  1.1  uch 		DPRINTF("can't remove entry");
    164  1.1  uch 		return error;
    165  1.1  uch 	}
    166  1.1  uch 
    167  1.4  uch 	if (dir_move && (parent_from != parent_to)) {
    168  1.1  uch 		/* If directory move, update ".." */
    169  1.4  uch 		if ((error = v7fs_directory_replace_entry(fs, &inode, "..",
    170  1.4  uch 			    parent_to->inode_number))) {
    171  1.4  uch 			DPRINTF("can't replace parent dir");
    172  1.4  uch 			return error;
    173  1.1  uch 		}
    174  1.4  uch 		v7fs_inode_writeback(fs, &inode);
    175  1.1  uch 	}
    176  1.1  uch 
    177  1.1  uch 	return 0;
    178  1.1  uch }
    179  1.1  uch 
    180  1.1  uch 
    181  1.1  uch int
    182  1.1  uch v7fs_directory_replace_entry(struct v7fs_self *fs,  struct v7fs_inode *self_dir,
    183  1.1  uch     const char *name, v7fs_ino_t ino)
    184  1.1  uch {
    185  1.1  uch 	int error;
    186  1.1  uch 
    187  1.1  uch 	/* Search entry that replaced. replace it to new inode number. */
    188  1.1  uch 	struct v7fs_lookup_arg lookup_arg = { .name = name,
    189  1.1  uch 					      .inode_number = ino };
    190  1.1  uch 	if ((error = v7fs_datablock_foreach(fs, self_dir, replace_subr,
    191  1.1  uch 	    &lookup_arg)) != V7FS_ITERATOR_BREAK)
    192  1.1  uch 		return ENOENT;
    193  1.1  uch 
    194  1.1  uch 	return 0;
    195  1.1  uch }
    196  1.1  uch 
    197  1.1  uch static int
    198  1.1  uch replace_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
    199  1.1  uch {
    200  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    201  1.1  uch 	struct v7fs_dirent *dir;
    202  1.1  uch 	void *buf;
    203  1.1  uch 	size_t i, n;
    204  1.1  uch 	int ret = 0;
    205  1.1  uch 
    206  1.1  uch 	DPRINTF("match start blk=%x\n", blk);
    207  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    208  1.1  uch 		return EIO;
    209  1.1  uch 
    210  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    211  1.1  uch 	n = sz / sizeof(*dir);
    212  1.1  uch 
    213  1.1  uch 	for (i = 0; i < n; i++, dir++) { /*disk endian */
    214  1.1  uch 		if (strncmp(p->name, (const char *)dir->name, V7FS_NAME_MAX)
    215  1.1  uch 		    == 0) {
    216  1.1  uch 			/* Replace inode# */
    217  1.1  uch 			dir->inode_number = V7FS_VAL16(fs, p->inode_number);
    218  1.1  uch 			/* Write back. */
    219  1.1  uch 			if (!fs->io.write(fs->io.cookie, buf, blk))
    220  1.1  uch 				ret = EIO;
    221  1.1  uch 			else
    222  1.1  uch 				ret = V7FS_ITERATOR_BREAK;
    223  1.1  uch 			break;
    224  1.1  uch 		}
    225  1.1  uch 	}
    226  1.1  uch 	scratch_free(fs, buf);
    227  1.1  uch 
    228  1.1  uch 	return ret;
    229  1.1  uch }
    230  1.1  uch 
    231  1.1  uch bool
    232  1.1  uch v7fs_file_lookup_by_number(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
    233  1.1  uch     v7fs_ino_t ino, char *buf)
    234  1.1  uch {
    235  1.1  uch 	int ret;
    236  1.1  uch 
    237  1.1  uch 	ret = v7fs_datablock_foreach(fs, parent_dir, lookup_by_number_subr,
    238  1.1  uch 	    &(struct v7fs_lookup_arg){ .inode_number = ino, .buf = buf });
    239  1.1  uch 
    240  1.1  uch 	return ret == V7FS_ITERATOR_BREAK;
    241  1.1  uch }
    242  1.1  uch 
    243  1.1  uch static int
    244  1.1  uch lookup_by_number_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
    245  1.1  uch     size_t sz)
    246  1.1  uch {
    247  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    248  1.1  uch 	struct v7fs_dirent *dir;
    249  1.1  uch 	void *buf;
    250  1.1  uch 	size_t i, n;
    251  1.1  uch 	int ret = 0;
    252  1.1  uch 
    253  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    254  1.1  uch 		return EIO;
    255  1.1  uch 
    256  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    257  1.1  uch 	n = sz / sizeof(*dir);
    258  1.1  uch 	v7fs_dirent_endian_convert(fs, dir, n);
    259  1.1  uch 
    260  1.1  uch 	for (i = 0; i < n; i++, dir++) {
    261  1.1  uch 		if (dir->inode_number == p->inode_number) {
    262  1.1  uch 			if (p->buf)
    263  1.1  uch 				v7fs_dirent_filename(p->buf, dir->name);
    264  1.1  uch 			ret = V7FS_ITERATOR_BREAK;
    265  1.1  uch 			break;
    266  1.1  uch 		}
    267  1.1  uch 	}
    268  1.1  uch 	scratch_free(fs, buf);
    269  1.1  uch 
    270  1.1  uch 	return ret;
    271  1.1  uch }
    272  1.4  uch 
    273  1.4  uch struct lookup_parent_arg {
    274  1.4  uch 	v7fs_ino_t parent_ino;
    275  1.4  uch };
    276  1.4  uch 
    277  1.4  uch static int
    278  1.4  uch can_dirmove(struct v7fs_self *fs, v7fs_ino_t from_ino, v7fs_ino_t to_ino)
    279  1.4  uch {
    280  1.4  uch 	struct v7fs_inode inode;
    281  1.4  uch 	v7fs_ino_t parent;
    282  1.4  uch 	int error;
    283  1.4  uch 
    284  1.4  uch 	/* Start dir. */
    285  1.4  uch 	if ((error = v7fs_inode_load(fs, &inode, to_ino)))
    286  1.4  uch 		return error;
    287  1.4  uch 
    288  1.4  uch 	if (!v7fs_inode_isdir(&inode))
    289  1.4  uch 		return ENOTDIR;
    290  1.4  uch 
    291  1.4  uch 	/* Lookup the parent. */
    292  1.4  uch 	do {
    293  1.4  uch 		struct lookup_parent_arg arg;
    294  1.4  uch 		/* Search parent dir */
    295  1.4  uch 		arg.parent_ino = 0;
    296  1.4  uch 		v7fs_datablock_foreach(fs, &inode, lookup_parent_from_dir_subr,
    297  1.4  uch 		    &arg);
    298  1.4  uch 		if ((parent = arg.parent_ino) == 0) {
    299  1.4  uch 			DPRINTF("***parent missing\n");
    300  1.4  uch 			return ENOENT;
    301  1.4  uch 		}
    302  1.4  uch 		/* Load parent dir */
    303  1.4  uch 		if ((error = v7fs_inode_load(fs, &inode, parent)))
    304  1.4  uch 			return error;
    305  1.4  uch 		if (parent == from_ino) {
    306  1.4  uch 			DPRINTF("#%d is child dir of #%d\n", to_ino, from_ino);
    307  1.4  uch 			return EINVAL;
    308  1.4  uch 		}
    309  1.4  uch 	} while (parent != V7FS_ROOT_INODE);
    310  1.4  uch 
    311  1.4  uch 	return 0;
    312  1.4  uch }
    313  1.4  uch 
    314  1.4  uch static int
    315  1.4  uch lookup_parent_from_dir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
    316  1.4  uch     size_t sz)
    317  1.4  uch {
    318  1.4  uch 	struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
    319  1.4  uch 	char name[V7FS_NAME_MAX + 1];
    320  1.4  uch 	void *buf;
    321  1.4  uch 	int ret = 0;
    322  1.4  uch 
    323  1.4  uch 	if (!(buf = scratch_read(fs, blk)))
    324  1.4  uch 		return 0;
    325  1.4  uch 	struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
    326  1.4  uch 	size_t i, n = sz / sizeof(*dir);
    327  1.4  uch 	if (!v7fs_dirent_endian_convert(fs, dir, n)) {
    328  1.4  uch 		scratch_free(fs, buf);
    329  1.4  uch 		return V7FS_ITERATOR_ERROR;
    330  1.4  uch 	}
    331  1.4  uch 
    332  1.4  uch 	for (i = 0; i < n; i++, dir++) {
    333  1.4  uch 		v7fs_dirent_filename(name, dir->name);
    334  1.4  uch 		if (strncmp(dir->name, "..", V7FS_NAME_MAX) != 0)
    335  1.4  uch 			continue;
    336  1.4  uch 
    337  1.4  uch 		arg->parent_ino = dir->inode_number;
    338  1.4  uch 		ret = V7FS_ITERATOR_BREAK;
    339  1.4  uch 		break;
    340  1.4  uch 	}
    341  1.4  uch 
    342  1.4  uch 	scratch_free(fs, buf);
    343  1.4  uch 	return ret;
    344  1.4  uch }
    345