Home | History | Annotate | Line # | Download | only in v7fs
v7fs_file_util.c revision 1.2
      1  1.2  uch /*	$NetBSD: v7fs_file_util.c,v 1.2 2011/07/16 12:35:40 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.1  uch #include <sys/cdefs.h>
     33  1.2  uch __KERNEL_RCSID(0, "$NetBSD: v7fs_file_util.c,v 1.2 2011/07/16 12:35:40 uch Exp $");
     34  1.1  uch #ifdef _KERNEL
     35  1.1  uch #include <sys/systm.h>
     36  1.1  uch #include <sys/param.h>
     37  1.1  uch #else
     38  1.1  uch #include <stdio.h>
     39  1.1  uch #include <string.h>
     40  1.1  uch #include <errno.h>
     41  1.1  uch #endif
     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_endian.h"
     46  1.1  uch #include "v7fs_inode.h"
     47  1.1  uch #include "v7fs_dirent.h"
     48  1.1  uch #include "v7fs_file.h"
     49  1.1  uch #include "v7fs_datablock.h"
     50  1.1  uch 
     51  1.1  uch #ifdef V7FS_FILE_DEBUG
     52  1.1  uch #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
     53  1.1  uch #else
     54  1.1  uch #define	DPRINTF(fmt, args...)	((void)0)
     55  1.1  uch #endif
     56  1.1  uch 
     57  1.1  uch static int replace_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
     58  1.1  uch static int lookup_by_number_subr(struct v7fs_self *, void *, v7fs_daddr_t,
     59  1.1  uch     size_t);
     60  1.1  uch 
     61  1.1  uch int
     62  1.1  uch v7fs_file_link(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
     63  1.1  uch     struct v7fs_inode *p, const char *name)
     64  1.1  uch {
     65  1.1  uch 	int error = 0;
     66  1.1  uch 
     67  1.1  uch 	DPRINTF("%d %d %s\n", parent_dir->inode_number, p->inode_number, name);
     68  1.1  uch 	if ((error = v7fs_directory_add_entry(fs, parent_dir, p->inode_number,
     69  1.1  uch 	    name))) {
     70  1.1  uch 		DPRINTF("can't add entry");
     71  1.1  uch 		return error;
     72  1.1  uch 	}
     73  1.1  uch 	p->nlink++;
     74  1.1  uch 	v7fs_inode_writeback(fs, p);
     75  1.1  uch 
     76  1.1  uch 	return 0;
     77  1.1  uch }
     78  1.1  uch 
     79  1.1  uch int
     80  1.2  uch v7fs_file_symlink(struct v7fs_self *fs, struct v7fs_inode *p,
     81  1.2  uch     const char *target)
     82  1.2  uch {
     83  1.2  uch 	int error;
     84  1.2  uch 	size_t len = strlen(target) + 1;
     85  1.2  uch 
     86  1.2  uch 	if (len > V7FSBSD_MAXSYMLINKLEN) {/* limited target 512byte pathname */
     87  1.2  uch 		DPRINTF("too long pathname.");
     88  1.2  uch 		return ENAMETOOLONG;
     89  1.2  uch 	}
     90  1.2  uch 
     91  1.2  uch 	if ((error = v7fs_datablock_expand(fs, p, len))) {
     92  1.2  uch 		return error;
     93  1.2  uch 	}
     94  1.2  uch 
     95  1.2  uch 	v7fs_daddr_t blk = p->addr[0];	/* 1block only.  */
     96  1.2  uch 	void *buf;
     97  1.2  uch 	if (!(buf = scratch_read(fs, blk))) {
     98  1.2  uch 		return EIO;
     99  1.2  uch 	}
    100  1.2  uch 
    101  1.2  uch 	strncpy(buf, target, V7FS_BSIZE);
    102  1.2  uch 	if (!fs->io.write(fs->io.cookie, buf, blk)) {
    103  1.2  uch 		scratch_free(fs, buf);
    104  1.2  uch 		return EIO;
    105  1.2  uch 	}
    106  1.2  uch 	scratch_free(fs, buf);
    107  1.2  uch 	v7fs_inode_writeback(fs, p);
    108  1.2  uch 
    109  1.2  uch 	return 0;
    110  1.2  uch }
    111  1.2  uch 
    112  1.2  uch int
    113  1.1  uch v7fs_file_rename(struct v7fs_self *fs, struct v7fs_inode *parent_from,
    114  1.1  uch     const char *from, struct v7fs_inode *parent_to, const char *to)
    115  1.1  uch {
    116  1.1  uch 	v7fs_ino_t from_ino, to_ino;
    117  1.1  uch 	int error;
    118  1.1  uch 
    119  1.1  uch 	if ((error = v7fs_file_lookup_by_name(fs, parent_from, from,
    120  1.1  uch 	    &from_ino))) {
    121  1.1  uch 		DPRINTF("%s don't exists\n", from);
    122  1.1  uch 		return error;
    123  1.1  uch 	}
    124  1.1  uch 
    125  1.1  uch 	/* If target file exists, remove. */
    126  1.1  uch 	error = v7fs_file_lookup_by_name(fs, parent_to, to, &to_ino);
    127  1.1  uch 	if (error == 0) {
    128  1.1  uch 		DPRINTF("%s already exists\n", to);
    129  1.1  uch 		if ((error = v7fs_file_deallocate(fs, parent_to, to))) {
    130  1.1  uch 			DPRINTF("%s can't remove\n", to);
    131  1.1  uch 			return error;
    132  1.1  uch 		}
    133  1.1  uch 	} else if (error != ENOENT) {
    134  1.1  uch 		DPRINTF("error=%d\n", error);
    135  1.1  uch 		return error;
    136  1.1  uch 	}
    137  1.1  uch 
    138  1.1  uch 	if ((error = v7fs_directory_add_entry(fs, parent_to, from_ino, to))) {
    139  1.1  uch 		DPRINTF("can't add entry");
    140  1.1  uch 		return error;
    141  1.1  uch 	}
    142  1.1  uch 
    143  1.1  uch 	if ((error = v7fs_directory_remove_entry(fs, parent_from, from))) {
    144  1.1  uch 		DPRINTF("can't remove entry");
    145  1.1  uch 		return error;
    146  1.1  uch 	}
    147  1.1  uch 
    148  1.1  uch 	if (parent_from != parent_to) {
    149  1.1  uch 		/* If directory move, update ".." */
    150  1.1  uch 		struct v7fs_inode inode;
    151  1.1  uch 		v7fs_inode_load(fs, &inode, from_ino);
    152  1.1  uch 		if (v7fs_inode_isdir(&inode)) {
    153  1.1  uch 			if ((error = v7fs_directory_replace_entry(fs, &inode,
    154  1.1  uch 			    "..", parent_to->inode_number))) {
    155  1.1  uch 				DPRINTF("can't replace parent dir");
    156  1.1  uch 				return error;
    157  1.1  uch 			}
    158  1.1  uch 			v7fs_inode_writeback(fs, &inode);
    159  1.1  uch 		}
    160  1.1  uch 	}
    161  1.1  uch 
    162  1.1  uch 	return 0;
    163  1.1  uch }
    164  1.1  uch 
    165  1.1  uch 
    166  1.1  uch int
    167  1.1  uch v7fs_directory_replace_entry(struct v7fs_self *fs,  struct v7fs_inode *self_dir,
    168  1.1  uch     const char *name, v7fs_ino_t ino)
    169  1.1  uch {
    170  1.1  uch 	int error;
    171  1.1  uch 
    172  1.1  uch 	/* Search entry that replaced. replace it to new inode number. */
    173  1.1  uch 	struct v7fs_lookup_arg lookup_arg = { .name = name,
    174  1.1  uch 					      .inode_number = ino };
    175  1.1  uch 	if ((error = v7fs_datablock_foreach(fs, self_dir, replace_subr,
    176  1.1  uch 	    &lookup_arg)) != V7FS_ITERATOR_BREAK)
    177  1.1  uch 		return ENOENT;
    178  1.1  uch 
    179  1.1  uch 	return 0;
    180  1.1  uch }
    181  1.1  uch 
    182  1.1  uch static int
    183  1.1  uch replace_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
    184  1.1  uch {
    185  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    186  1.1  uch 	struct v7fs_dirent *dir;
    187  1.1  uch 	void *buf;
    188  1.1  uch 	size_t i, n;
    189  1.1  uch 	int ret = 0;
    190  1.1  uch 
    191  1.1  uch 	DPRINTF("match start blk=%x\n", blk);
    192  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    193  1.1  uch 		return EIO;
    194  1.1  uch 
    195  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    196  1.1  uch 	n = sz / sizeof(*dir);
    197  1.1  uch 
    198  1.1  uch 	for (i = 0; i < n; i++, dir++) { /*disk endian */
    199  1.1  uch 		if (strncmp(p->name, (const char *)dir->name, V7FS_NAME_MAX)
    200  1.1  uch 		    == 0) {
    201  1.1  uch 			/* Replace inode# */
    202  1.1  uch 			dir->inode_number = V7FS_VAL16(fs, p->inode_number);
    203  1.1  uch 			/* Write back. */
    204  1.1  uch 			if (!fs->io.write(fs->io.cookie, buf, blk))
    205  1.1  uch 				ret = EIO;
    206  1.1  uch 			else
    207  1.1  uch 				ret = V7FS_ITERATOR_BREAK;
    208  1.1  uch 			break;
    209  1.1  uch 		}
    210  1.1  uch 	}
    211  1.1  uch 	scratch_free(fs, buf);
    212  1.1  uch 
    213  1.1  uch 	return ret;
    214  1.1  uch }
    215  1.1  uch 
    216  1.1  uch bool
    217  1.1  uch v7fs_file_lookup_by_number(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
    218  1.1  uch     v7fs_ino_t ino, char *buf)
    219  1.1  uch {
    220  1.1  uch 	int ret;
    221  1.1  uch 
    222  1.1  uch 	ret = v7fs_datablock_foreach(fs, parent_dir, lookup_by_number_subr,
    223  1.1  uch 	    &(struct v7fs_lookup_arg){ .inode_number = ino, .buf = buf });
    224  1.1  uch 
    225  1.1  uch 	return ret == V7FS_ITERATOR_BREAK;
    226  1.1  uch }
    227  1.1  uch 
    228  1.1  uch static int
    229  1.1  uch lookup_by_number_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
    230  1.1  uch     size_t sz)
    231  1.1  uch {
    232  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    233  1.1  uch 	struct v7fs_dirent *dir;
    234  1.1  uch 	void *buf;
    235  1.1  uch 	size_t i, n;
    236  1.1  uch 	int ret = 0;
    237  1.1  uch 
    238  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    239  1.1  uch 		return EIO;
    240  1.1  uch 
    241  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    242  1.1  uch 	n = sz / sizeof(*dir);
    243  1.1  uch 	v7fs_dirent_endian_convert(fs, dir, n);
    244  1.1  uch 
    245  1.1  uch 	for (i = 0; i < n; i++, dir++) {
    246  1.1  uch 		if (dir->inode_number == p->inode_number) {
    247  1.1  uch 			if (p->buf)
    248  1.1  uch 				v7fs_dirent_filename(p->buf, dir->name);
    249  1.1  uch 			ret = V7FS_ITERATOR_BREAK;
    250  1.1  uch 			break;
    251  1.1  uch 		}
    252  1.1  uch 	}
    253  1.1  uch 	scratch_free(fs, buf);
    254  1.1  uch 
    255  1.1  uch 	return ret;
    256  1.1  uch }
    257