Home | History | Annotate | Line # | Download | only in v7fs
v7fs_file_util.c revision 1.1
      1  1.1  uch /*	$NetBSD: v7fs_file_util.c,v 1.1 2011/06/27 11:52:24 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.1  uch __KERNEL_RCSID(0, "$NetBSD: v7fs_file_util.c,v 1.1 2011/06/27 11:52:24 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.1  uch v7fs_file_rename(struct v7fs_self *fs, struct v7fs_inode *parent_from,
     81  1.1  uch     const char *from, struct v7fs_inode *parent_to, const char *to)
     82  1.1  uch {
     83  1.1  uch 	v7fs_ino_t from_ino, to_ino;
     84  1.1  uch 	int error;
     85  1.1  uch 
     86  1.1  uch 	if ((error = v7fs_file_lookup_by_name(fs, parent_from, from,
     87  1.1  uch 	    &from_ino))) {
     88  1.1  uch 		DPRINTF("%s don't exists\n", from);
     89  1.1  uch 		return error;
     90  1.1  uch 	}
     91  1.1  uch 
     92  1.1  uch 	/* If target file exists, remove. */
     93  1.1  uch 	error = v7fs_file_lookup_by_name(fs, parent_to, to, &to_ino);
     94  1.1  uch 	if (error == 0) {
     95  1.1  uch 		DPRINTF("%s already exists\n", to);
     96  1.1  uch 		if ((error = v7fs_file_deallocate(fs, parent_to, to))) {
     97  1.1  uch 			DPRINTF("%s can't remove\n", to);
     98  1.1  uch 			return error;
     99  1.1  uch 		}
    100  1.1  uch 	} else if (error != ENOENT) {
    101  1.1  uch 		DPRINTF("error=%d\n", error);
    102  1.1  uch 		return error;
    103  1.1  uch 	}
    104  1.1  uch 
    105  1.1  uch 	if ((error = v7fs_directory_add_entry(fs, parent_to, from_ino, to))) {
    106  1.1  uch 		DPRINTF("can't add entry");
    107  1.1  uch 		return error;
    108  1.1  uch 	}
    109  1.1  uch 
    110  1.1  uch 	if ((error = v7fs_directory_remove_entry(fs, parent_from, from))) {
    111  1.1  uch 		DPRINTF("can't remove entry");
    112  1.1  uch 		return error;
    113  1.1  uch 	}
    114  1.1  uch 
    115  1.1  uch 	if (parent_from != parent_to) {
    116  1.1  uch 		/* If directory move, update ".." */
    117  1.1  uch 		struct v7fs_inode inode;
    118  1.1  uch 		v7fs_inode_load(fs, &inode, from_ino);
    119  1.1  uch 		if (v7fs_inode_isdir(&inode)) {
    120  1.1  uch 			if ((error = v7fs_directory_replace_entry(fs, &inode,
    121  1.1  uch 			    "..", parent_to->inode_number))) {
    122  1.1  uch 				DPRINTF("can't replace parent dir");
    123  1.1  uch 				return error;
    124  1.1  uch 			}
    125  1.1  uch 			v7fs_inode_writeback(fs, &inode);
    126  1.1  uch 		}
    127  1.1  uch 	}
    128  1.1  uch 
    129  1.1  uch 	return 0;
    130  1.1  uch }
    131  1.1  uch 
    132  1.1  uch 
    133  1.1  uch int
    134  1.1  uch v7fs_directory_replace_entry(struct v7fs_self *fs,  struct v7fs_inode *self_dir,
    135  1.1  uch     const char *name, v7fs_ino_t ino)
    136  1.1  uch {
    137  1.1  uch 	int error;
    138  1.1  uch 
    139  1.1  uch 	/* Search entry that replaced. replace it to new inode number. */
    140  1.1  uch 	struct v7fs_lookup_arg lookup_arg = { .name = name,
    141  1.1  uch 					      .inode_number = ino };
    142  1.1  uch 	if ((error = v7fs_datablock_foreach(fs, self_dir, replace_subr,
    143  1.1  uch 	    &lookup_arg)) != V7FS_ITERATOR_BREAK)
    144  1.1  uch 		return ENOENT;
    145  1.1  uch 
    146  1.1  uch 	return 0;
    147  1.1  uch }
    148  1.1  uch 
    149  1.1  uch static int
    150  1.1  uch replace_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
    151  1.1  uch {
    152  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    153  1.1  uch 	struct v7fs_dirent *dir;
    154  1.1  uch 	void *buf;
    155  1.1  uch 	size_t i, n;
    156  1.1  uch 	int ret = 0;
    157  1.1  uch 
    158  1.1  uch 	DPRINTF("match start blk=%x\n", blk);
    159  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    160  1.1  uch 		return EIO;
    161  1.1  uch 
    162  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    163  1.1  uch 	n = sz / sizeof(*dir);
    164  1.1  uch 
    165  1.1  uch 	for (i = 0; i < n; i++, dir++) { /*disk endian */
    166  1.1  uch 		if (strncmp(p->name, (const char *)dir->name, V7FS_NAME_MAX)
    167  1.1  uch 		    == 0) {
    168  1.1  uch 			/* Replace inode# */
    169  1.1  uch 			dir->inode_number = V7FS_VAL16(fs, p->inode_number);
    170  1.1  uch 			/* Write back. */
    171  1.1  uch 			if (!fs->io.write(fs->io.cookie, buf, blk))
    172  1.1  uch 				ret = EIO;
    173  1.1  uch 			else
    174  1.1  uch 				ret = V7FS_ITERATOR_BREAK;
    175  1.1  uch 			break;
    176  1.1  uch 		}
    177  1.1  uch 	}
    178  1.1  uch 	scratch_free(fs, buf);
    179  1.1  uch 
    180  1.1  uch 	return ret;
    181  1.1  uch }
    182  1.1  uch 
    183  1.1  uch bool
    184  1.1  uch v7fs_file_lookup_by_number(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
    185  1.1  uch     v7fs_ino_t ino, char *buf)
    186  1.1  uch {
    187  1.1  uch 	int ret;
    188  1.1  uch 
    189  1.1  uch 	ret = v7fs_datablock_foreach(fs, parent_dir, lookup_by_number_subr,
    190  1.1  uch 	    &(struct v7fs_lookup_arg){ .inode_number = ino, .buf = buf });
    191  1.1  uch 
    192  1.1  uch 	return ret == V7FS_ITERATOR_BREAK;
    193  1.1  uch }
    194  1.1  uch 
    195  1.1  uch static int
    196  1.1  uch lookup_by_number_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
    197  1.1  uch     size_t sz)
    198  1.1  uch {
    199  1.1  uch 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
    200  1.1  uch 	struct v7fs_dirent *dir;
    201  1.1  uch 	void *buf;
    202  1.1  uch 	size_t i, n;
    203  1.1  uch 	int ret = 0;
    204  1.1  uch 
    205  1.1  uch 	if (!(buf = scratch_read(fs, blk)))
    206  1.1  uch 		return EIO;
    207  1.1  uch 
    208  1.1  uch 	dir = (struct v7fs_dirent *)buf;
    209  1.1  uch 	n = sz / sizeof(*dir);
    210  1.1  uch 	v7fs_dirent_endian_convert(fs, dir, n);
    211  1.1  uch 
    212  1.1  uch 	for (i = 0; i < n; i++, dir++) {
    213  1.1  uch 		if (dir->inode_number == p->inode_number) {
    214  1.1  uch 			if (p->buf)
    215  1.1  uch 				v7fs_dirent_filename(p->buf, dir->name);
    216  1.1  uch 			ret = V7FS_ITERATOR_BREAK;
    217  1.1  uch 			break;
    218  1.1  uch 		}
    219  1.1  uch 	}
    220  1.1  uch 	scratch_free(fs, buf);
    221  1.1  uch 
    222  1.1  uch 	return ret;
    223  1.1  uch }
    224