xref: /minix3/sys/fs/v7fs/v7fs_file.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: v7fs_file.c,v 1.6 2014/12/29 15:28:58 hannken Exp $	*/
29f988b79SJean-Baptiste Boric 
39f988b79SJean-Baptiste Boric /*-
49f988b79SJean-Baptiste Boric  * Copyright (c) 2011 The NetBSD Foundation, Inc.
59f988b79SJean-Baptiste Boric  * All rights reserved.
69f988b79SJean-Baptiste Boric  *
79f988b79SJean-Baptiste Boric  * This code is derived from software contributed to The NetBSD Foundation
89f988b79SJean-Baptiste Boric  * by UCHIYAMA Yasushi.
99f988b79SJean-Baptiste Boric  *
109f988b79SJean-Baptiste Boric  * Redistribution and use in source and binary forms, with or without
119f988b79SJean-Baptiste Boric  * modification, are permitted provided that the following conditions
129f988b79SJean-Baptiste Boric  * are met:
139f988b79SJean-Baptiste Boric  * 1. Redistributions of source code must retain the above copyright
149f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer.
159f988b79SJean-Baptiste Boric  * 2. Redistributions in binary form must reproduce the above copyright
169f988b79SJean-Baptiste Boric  *    notice, this list of conditions and the following disclaimer in the
179f988b79SJean-Baptiste Boric  *    documentation and/or other materials provided with the distribution.
189f988b79SJean-Baptiste Boric  *
199f988b79SJean-Baptiste Boric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209f988b79SJean-Baptiste Boric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219f988b79SJean-Baptiste Boric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229f988b79SJean-Baptiste Boric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239f988b79SJean-Baptiste Boric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249f988b79SJean-Baptiste Boric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259f988b79SJean-Baptiste Boric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269f988b79SJean-Baptiste Boric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279f988b79SJean-Baptiste Boric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289f988b79SJean-Baptiste Boric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299f988b79SJean-Baptiste Boric  * POSSIBILITY OF SUCH DAMAGE.
309f988b79SJean-Baptiste Boric  */
319f988b79SJean-Baptiste Boric 
329f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
339f988b79SJean-Baptiste Boric #include "nbtool_config.h"
349f988b79SJean-Baptiste Boric #endif
359f988b79SJean-Baptiste Boric 
369f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
37*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: v7fs_file.c,v 1.6 2014/12/29 15:28:58 hannken Exp $");
389f988b79SJean-Baptiste Boric #if defined _KERNEL_OPT
399f988b79SJean-Baptiste Boric #include "opt_v7fs.h"
409f988b79SJean-Baptiste Boric #endif
419f988b79SJean-Baptiste Boric 
429f988b79SJean-Baptiste Boric #include <sys/param.h>
439f988b79SJean-Baptiste Boric #ifdef _KERNEL
449f988b79SJean-Baptiste Boric #include <sys/systm.h>
459f988b79SJean-Baptiste Boric #else
469f988b79SJean-Baptiste Boric #include <stdio.h>
479f988b79SJean-Baptiste Boric #include <string.h>
489f988b79SJean-Baptiste Boric #include <errno.h>
499f988b79SJean-Baptiste Boric #endif
509f988b79SJean-Baptiste Boric 
519f988b79SJean-Baptiste Boric #include "v7fs.h"
529f988b79SJean-Baptiste Boric #include "v7fs_impl.h"
539f988b79SJean-Baptiste Boric #include "v7fs_endian.h"
549f988b79SJean-Baptiste Boric #include "v7fs_inode.h"
559f988b79SJean-Baptiste Boric #include "v7fs_dirent.h"
569f988b79SJean-Baptiste Boric #include "v7fs_file.h"
579f988b79SJean-Baptiste Boric #include "v7fs_datablock.h"
589f988b79SJean-Baptiste Boric 
599f988b79SJean-Baptiste Boric #ifdef V7FS_FILE_DEBUG
609f988b79SJean-Baptiste Boric #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
619f988b79SJean-Baptiste Boric #else
629f988b79SJean-Baptiste Boric #define	DPRINTF(fmt, args...)	((void)0)
639f988b79SJean-Baptiste Boric #endif
649f988b79SJean-Baptiste Boric 
659f988b79SJean-Baptiste Boric static int lookup_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
669f988b79SJean-Baptiste Boric static int remove_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
679f988b79SJean-Baptiste Boric 
689f988b79SJean-Baptiste Boric int
v7fs_file_lookup_by_name(struct v7fs_self * fs,struct v7fs_inode * parent_dir,const char * name,v7fs_ino_t * ino)699f988b79SJean-Baptiste Boric v7fs_file_lookup_by_name(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
709f988b79SJean-Baptiste Boric     const char *name, v7fs_ino_t *ino)
719f988b79SJean-Baptiste Boric {
729f988b79SJean-Baptiste Boric 	char filename[V7FS_NAME_MAX + 1];
739f988b79SJean-Baptiste Boric 	char *q;
749f988b79SJean-Baptiste Boric 	int error;
759f988b79SJean-Baptiste Boric 	size_t len;
769f988b79SJean-Baptiste Boric 
779f988b79SJean-Baptiste Boric 	if ((q = strchr(name, '/'))) {
789f988b79SJean-Baptiste Boric 		/* Zap following path. */
799f988b79SJean-Baptiste Boric 		len = MIN(V7FS_NAME_MAX, q - name);
809f988b79SJean-Baptiste Boric 		memcpy(filename, name, len);
819f988b79SJean-Baptiste Boric 		filename[len] = '\0';	/* '/' -> '\0' */
829f988b79SJean-Baptiste Boric 	} else {
839f988b79SJean-Baptiste Boric 		v7fs_dirent_filename(filename, name);
849f988b79SJean-Baptiste Boric 	}
859f988b79SJean-Baptiste Boric 	DPRINTF("%s(%s) dir=%d\n", filename, name, parent_dir->inode_number);
869f988b79SJean-Baptiste Boric 
879f988b79SJean-Baptiste Boric 	struct v7fs_lookup_arg lookup_arg = { .name = filename,
889f988b79SJean-Baptiste Boric 					      .inode_number = 0 };
899f988b79SJean-Baptiste Boric 	if ((error = v7fs_datablock_foreach(fs, parent_dir, lookup_subr,
909f988b79SJean-Baptiste Boric 		    &lookup_arg)) != V7FS_ITERATOR_BREAK) {
919f988b79SJean-Baptiste Boric 		DPRINTF("not found.\n");
929f988b79SJean-Baptiste Boric 		return ENOENT;
939f988b79SJean-Baptiste Boric 	}
949f988b79SJean-Baptiste Boric 
959f988b79SJean-Baptiste Boric 	*ino = lookup_arg.inode_number;
969f988b79SJean-Baptiste Boric 	DPRINTF("done. ino=%d\n", *ino);
979f988b79SJean-Baptiste Boric 
989f988b79SJean-Baptiste Boric 	return 0;
999f988b79SJean-Baptiste Boric }
1009f988b79SJean-Baptiste Boric 
1019f988b79SJean-Baptiste Boric static int
lookup_subr(struct v7fs_self * fs,void * ctx,v7fs_daddr_t blk,size_t sz)1029f988b79SJean-Baptiste Boric lookup_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
1039f988b79SJean-Baptiste Boric {
1049f988b79SJean-Baptiste Boric 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
1059f988b79SJean-Baptiste Boric 	struct v7fs_dirent *dir;
1069f988b79SJean-Baptiste Boric 	const char *name = p->name;
1079f988b79SJean-Baptiste Boric 	void *buf;
1089f988b79SJean-Baptiste Boric 	size_t i, n;
1099f988b79SJean-Baptiste Boric 	int ret = 0;
1109f988b79SJean-Baptiste Boric 
1119f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, blk)))
1129f988b79SJean-Baptiste Boric 		return EIO;
1139f988b79SJean-Baptiste Boric 
1149f988b79SJean-Baptiste Boric 	dir = (struct v7fs_dirent *)buf;
1159f988b79SJean-Baptiste Boric 	n = sz / sizeof(*dir);
1169f988b79SJean-Baptiste Boric 	v7fs_dirent_endian_convert(fs, dir, n);
1179f988b79SJean-Baptiste Boric 
1189f988b79SJean-Baptiste Boric 	for (i = 0; i < n; i++, dir++) {
1199f988b79SJean-Baptiste Boric 		if (dir->inode_number < 1) {
1209f988b79SJean-Baptiste Boric 			DPRINTF("*** bad inode #%d ***\n", dir->inode_number);
1219f988b79SJean-Baptiste Boric 			continue;
1229f988b79SJean-Baptiste Boric 		}
1239f988b79SJean-Baptiste Boric 
1249f988b79SJean-Baptiste Boric 		if (strncmp((const char *)dir->name, name, V7FS_NAME_MAX) == 0)
1259f988b79SJean-Baptiste Boric 		{
1269f988b79SJean-Baptiste Boric 			p->inode_number = dir->inode_number;
1279f988b79SJean-Baptiste Boric 			ret =  V7FS_ITERATOR_BREAK; /* found */
1289f988b79SJean-Baptiste Boric 			break;
1299f988b79SJean-Baptiste Boric 		}
1309f988b79SJean-Baptiste Boric 	}
1319f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
1329f988b79SJean-Baptiste Boric 
1339f988b79SJean-Baptiste Boric 	return ret;
1349f988b79SJean-Baptiste Boric }
1359f988b79SJean-Baptiste Boric 
1369f988b79SJean-Baptiste Boric int
v7fs_file_allocate(struct v7fs_self * fs,struct v7fs_inode * parent_dir,const char * srcname,struct v7fs_fileattr * attr,v7fs_ino_t * ino)1379f988b79SJean-Baptiste Boric v7fs_file_allocate(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
1389f988b79SJean-Baptiste Boric     const char *srcname, struct v7fs_fileattr *attr, v7fs_ino_t *ino)
1399f988b79SJean-Baptiste Boric {
1409f988b79SJean-Baptiste Boric 	struct v7fs_inode inode;
1419f988b79SJean-Baptiste Boric 	char filename[V7FS_NAME_MAX + 1];
1429f988b79SJean-Baptiste Boric 	struct v7fs_dirent *dir;
1439f988b79SJean-Baptiste Boric 	int error;
1449f988b79SJean-Baptiste Boric 
1459f988b79SJean-Baptiste Boric 	/* Truncate filename. */
1469f988b79SJean-Baptiste Boric 	v7fs_dirent_filename(filename, srcname);
1479f988b79SJean-Baptiste Boric 	DPRINTF("%s(%s)\n", filename, srcname);
1489f988b79SJean-Baptiste Boric 
1499f988b79SJean-Baptiste Boric 	/* Check filename. */
1509f988b79SJean-Baptiste Boric 	if (v7fs_file_lookup_by_name(fs, parent_dir, filename, ino) == 0) {
1519f988b79SJean-Baptiste Boric 		DPRINTF("%s exists\n", filename);
1529f988b79SJean-Baptiste Boric 		return EEXIST;
1539f988b79SJean-Baptiste Boric 	}
1549f988b79SJean-Baptiste Boric 
1559f988b79SJean-Baptiste Boric 	/* Get new inode. */
1569f988b79SJean-Baptiste Boric 	if ((error = v7fs_inode_allocate(fs, ino)))
1579f988b79SJean-Baptiste Boric 		return error;
1589f988b79SJean-Baptiste Boric 
1599f988b79SJean-Baptiste Boric 	/* Set initial attribute. */
1609f988b79SJean-Baptiste Boric 	memset(&inode, 0, sizeof(inode));
1619f988b79SJean-Baptiste Boric 	inode.inode_number = *ino;
1629f988b79SJean-Baptiste Boric 	inode.mode = attr->mode;
1639f988b79SJean-Baptiste Boric 	inode.uid = attr->uid;
1649f988b79SJean-Baptiste Boric 	inode.gid = attr->gid;
1659f988b79SJean-Baptiste Boric 	if (attr->ctime)
1669f988b79SJean-Baptiste Boric 		inode.ctime = attr->ctime;
1679f988b79SJean-Baptiste Boric 	if (attr->mtime)
1689f988b79SJean-Baptiste Boric 		inode.mtime = attr->mtime;
1699f988b79SJean-Baptiste Boric 	if (attr->atime)
1709f988b79SJean-Baptiste Boric 		inode.atime = attr->atime;
1719f988b79SJean-Baptiste Boric 
1729f988b79SJean-Baptiste Boric 	switch (inode.mode & V7FS_IFMT)	{
1739f988b79SJean-Baptiste Boric 	default:
1749f988b79SJean-Baptiste Boric 		DPRINTF("Can't allocate %o type.\n", inode.mode);
1759f988b79SJean-Baptiste Boric 		v7fs_inode_deallocate(fs, *ino);
1769f988b79SJean-Baptiste Boric 		return EINVAL;
1779f988b79SJean-Baptiste Boric 	case V7FS_IFCHR:
1789f988b79SJean-Baptiste Boric 		/* FALLTHROUGH */
1799f988b79SJean-Baptiste Boric 	case V7FS_IFBLK:
1809f988b79SJean-Baptiste Boric 		inode.nlink = 1;
1819f988b79SJean-Baptiste Boric 		inode.device = attr->device;
1829f988b79SJean-Baptiste Boric 		inode.addr[0] = inode.device;
1839f988b79SJean-Baptiste Boric 		break;
1849f988b79SJean-Baptiste Boric 	case V7FSBSD_IFFIFO:
1859f988b79SJean-Baptiste Boric 		/* FALLTHROUGH */
1869f988b79SJean-Baptiste Boric 	case V7FSBSD_IFSOCK:
1879f988b79SJean-Baptiste Boric 		/* FALLTHROUGH */
1889f988b79SJean-Baptiste Boric 	case V7FSBSD_IFLNK:
1899f988b79SJean-Baptiste Boric 		/* FALLTHROUGH */
1909f988b79SJean-Baptiste Boric 	case V7FS_IFREG:
1919f988b79SJean-Baptiste Boric 		inode.nlink = 1;
1929f988b79SJean-Baptiste Boric 		break;
1939f988b79SJean-Baptiste Boric 	case V7FS_IFDIR:
1949f988b79SJean-Baptiste Boric 		inode.nlink = 2;	/* . + .. */
1959f988b79SJean-Baptiste Boric 		if ((error = v7fs_datablock_expand(fs, &inode, sizeof(*dir) * 2
1969f988b79SJean-Baptiste Boric 		    ))) {
1979f988b79SJean-Baptiste Boric 			v7fs_inode_deallocate(fs, *ino);
1989f988b79SJean-Baptiste Boric 			return error;
1999f988b79SJean-Baptiste Boric 		}
2009f988b79SJean-Baptiste Boric 		v7fs_daddr_t blk = inode.addr[0];
2019f988b79SJean-Baptiste Boric 		void *buf;
2029f988b79SJean-Baptiste Boric 		if (!(buf = scratch_read(fs, blk))) {
2039f988b79SJean-Baptiste Boric 			v7fs_inode_deallocate(fs, *ino);
2049f988b79SJean-Baptiste Boric 			return EIO;
2059f988b79SJean-Baptiste Boric 		}
2069f988b79SJean-Baptiste Boric 		dir = (struct v7fs_dirent *)buf;
2079f988b79SJean-Baptiste Boric 		strcpy(dir[0].name, ".");
2089f988b79SJean-Baptiste Boric 		dir[0].inode_number = V7FS_VAL16(fs, *ino);
2099f988b79SJean-Baptiste Boric 		strcpy(dir[1].name, "..");
2109f988b79SJean-Baptiste Boric 		dir[1].inode_number = V7FS_VAL16(fs, parent_dir->inode_number);
2119f988b79SJean-Baptiste Boric 		if (!fs->io.write(fs->io.cookie, buf, blk)) {
2129f988b79SJean-Baptiste Boric 			scratch_free(fs, buf);
2139f988b79SJean-Baptiste Boric 			return EIO;
2149f988b79SJean-Baptiste Boric 		}
2159f988b79SJean-Baptiste Boric 		scratch_free(fs, buf);
2169f988b79SJean-Baptiste Boric 		break;
2179f988b79SJean-Baptiste Boric 	}
2189f988b79SJean-Baptiste Boric 
2199f988b79SJean-Baptiste Boric 	v7fs_inode_writeback(fs, &inode);
2209f988b79SJean-Baptiste Boric 
2219f988b79SJean-Baptiste Boric 	/* Link this inode to parent directory. */
2229f988b79SJean-Baptiste Boric 	if ((error = v7fs_directory_add_entry(fs, parent_dir, *ino, filename)))
2239f988b79SJean-Baptiste Boric 	{
2249f988b79SJean-Baptiste Boric 		DPRINTF("can't add dirent.\n");
2259f988b79SJean-Baptiste Boric 		return error;
2269f988b79SJean-Baptiste Boric 	}
2279f988b79SJean-Baptiste Boric 
2289f988b79SJean-Baptiste Boric 	return 0;
2299f988b79SJean-Baptiste Boric }
2309f988b79SJean-Baptiste Boric 
2319f988b79SJean-Baptiste Boric int
v7fs_file_deallocate(struct v7fs_self * fs,struct v7fs_inode * parent_dir,const char * name)2329f988b79SJean-Baptiste Boric v7fs_file_deallocate(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
2339f988b79SJean-Baptiste Boric     const char *name)
2349f988b79SJean-Baptiste Boric {
2359f988b79SJean-Baptiste Boric 	v7fs_ino_t ino;
2369f988b79SJean-Baptiste Boric 	struct v7fs_inode inode;
2379f988b79SJean-Baptiste Boric 	int error;
2389f988b79SJean-Baptiste Boric 
2399f988b79SJean-Baptiste Boric 	DPRINTF("%s\n", name);
2409f988b79SJean-Baptiste Boric 	if ((error = v7fs_file_lookup_by_name(fs, parent_dir, name, &ino))) {
2419f988b79SJean-Baptiste Boric 		DPRINTF("no such a file: %s\n", name);
2429f988b79SJean-Baptiste Boric 		return error;
2439f988b79SJean-Baptiste Boric 	}
2449f988b79SJean-Baptiste Boric 	DPRINTF("%s->#%d\n", name, ino);
2459f988b79SJean-Baptiste Boric 	if ((error = v7fs_inode_load(fs, &inode, ino)))
2469f988b79SJean-Baptiste Boric 		return error;
2479f988b79SJean-Baptiste Boric 
2489f988b79SJean-Baptiste Boric 	if (v7fs_inode_isdir(&inode)) {
2499f988b79SJean-Baptiste Boric 		char filename[V7FS_NAME_MAX + 1];
2509f988b79SJean-Baptiste Boric 		v7fs_dirent_filename(filename, name);
2519f988b79SJean-Baptiste Boric 		/* Check parent */
2529f988b79SJean-Baptiste Boric 		if (strncmp(filename, "..", V7FS_NAME_MAX) == 0) {
2539f988b79SJean-Baptiste Boric 			DPRINTF("can not remove '..'\n");
2549f988b79SJean-Baptiste Boric 			return EINVAL; /* t_vnops rename_dotdot */
2559f988b79SJean-Baptiste Boric 		}
2569f988b79SJean-Baptiste Boric 		/* Check empty */
2579f988b79SJean-Baptiste Boric 		if (v7fs_inode_filesize(&inode) !=
2589f988b79SJean-Baptiste Boric 		    sizeof(struct v7fs_dirent) * 2 /*"." + ".."*/) {
2599f988b79SJean-Baptiste Boric 			DPRINTF("directory not empty.\n");
2609f988b79SJean-Baptiste Boric 			return ENOTEMPTY;/* t_vnops dir_noempty, rename_dir(6)*/
2619f988b79SJean-Baptiste Boric 		}
262*0a6a1f1dSLionel Sambuc 		error = v7fs_datablock_size_change(fs, 0, &inode);
263*0a6a1f1dSLionel Sambuc 		if (error)
264*0a6a1f1dSLionel Sambuc 			return error;
2659f988b79SJean-Baptiste Boric 		inode.nlink = 0;	/* remove this. */
2669f988b79SJean-Baptiste Boric 	} else {
2679f988b79SJean-Baptiste Boric 		/* Decrement reference count. */
2689f988b79SJean-Baptiste Boric 		--inode.nlink;	/* regular file. */
2699f988b79SJean-Baptiste Boric 		DPRINTF("%s nlink=%d\n", name, inode.nlink);
2709f988b79SJean-Baptiste Boric 	}
2719f988b79SJean-Baptiste Boric 
2729f988b79SJean-Baptiste Boric 
2739f988b79SJean-Baptiste Boric 	if ((error = v7fs_directory_remove_entry(fs, parent_dir, name)))
2749f988b79SJean-Baptiste Boric 		return error;
2759f988b79SJean-Baptiste Boric 	DPRINTF("remove dirent\n");
2769f988b79SJean-Baptiste Boric 
2779f988b79SJean-Baptiste Boric 	v7fs_inode_writeback(fs, &inode);
2789f988b79SJean-Baptiste Boric 
2799f988b79SJean-Baptiste Boric 	return 0;
2809f988b79SJean-Baptiste Boric }
2819f988b79SJean-Baptiste Boric 
2829f988b79SJean-Baptiste Boric int
v7fs_directory_add_entry(struct v7fs_self * fs,struct v7fs_inode * parent_dir,v7fs_ino_t ino,const char * srcname)2839f988b79SJean-Baptiste Boric v7fs_directory_add_entry(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
2849f988b79SJean-Baptiste Boric     v7fs_ino_t ino, const char *srcname)
2859f988b79SJean-Baptiste Boric {
2869f988b79SJean-Baptiste Boric 	struct v7fs_inode inode;
2879f988b79SJean-Baptiste Boric 	struct v7fs_dirent *dir;
2889f988b79SJean-Baptiste Boric 	int error = 0;
2899f988b79SJean-Baptiste Boric 	v7fs_daddr_t blk;
2909f988b79SJean-Baptiste Boric 	void *buf;
2919f988b79SJean-Baptiste Boric 	char filename[V7FS_NAME_MAX + 1];
2929f988b79SJean-Baptiste Boric 
2939f988b79SJean-Baptiste Boric 	/* Truncate filename. */
2949f988b79SJean-Baptiste Boric 	v7fs_dirent_filename(filename, srcname);
2959f988b79SJean-Baptiste Boric 	DPRINTF("%s(%s) %d\n", filename, srcname, ino);
2969f988b79SJean-Baptiste Boric 
2979f988b79SJean-Baptiste Boric 	/* Target inode */
2989f988b79SJean-Baptiste Boric 	if ((error = v7fs_inode_load(fs, &inode, ino)))
2999f988b79SJean-Baptiste Boric 		return error;
3009f988b79SJean-Baptiste Boric 
3019f988b79SJean-Baptiste Boric 	/* Expand datablock. */
3029f988b79SJean-Baptiste Boric 	if ((error = v7fs_datablock_expand(fs, parent_dir, sizeof(*dir))))
3039f988b79SJean-Baptiste Boric 		return error;
3049f988b79SJean-Baptiste Boric 
3059f988b79SJean-Baptiste Boric 	/* Read last entry. */
3069f988b79SJean-Baptiste Boric 	if (!(blk = v7fs_datablock_last(fs, parent_dir,
3079f988b79SJean-Baptiste Boric 	    v7fs_inode_filesize(parent_dir))))
3089f988b79SJean-Baptiste Boric 		return EIO;
3099f988b79SJean-Baptiste Boric 
3109f988b79SJean-Baptiste Boric 	/* Load dirent block. This vnode(parent dir) is locked by VFS layer. */
3119f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, blk)))
3129f988b79SJean-Baptiste Boric 		return EIO;
3139f988b79SJean-Baptiste Boric 
3149f988b79SJean-Baptiste Boric 	size_t sz = v7fs_inode_filesize(parent_dir);
3159f988b79SJean-Baptiste Boric 	sz = V7FS_RESIDUE_BSIZE(sz);	/* last block payload. */
3169f988b79SJean-Baptiste Boric 	int n = sz / sizeof(*dir) - 1;
3179f988b79SJean-Baptiste Boric 	/* Add dirent. */
3189f988b79SJean-Baptiste Boric 	dir = (struct v7fs_dirent *)buf;
3199f988b79SJean-Baptiste Boric 	dir[n].inode_number = V7FS_VAL16(fs, ino);
3209f988b79SJean-Baptiste Boric 	memcpy((char *)dir[n].name, filename, V7FS_NAME_MAX);
3219f988b79SJean-Baptiste Boric 	/* Write back datablock */
3229f988b79SJean-Baptiste Boric 	if (!fs->io.write(fs->io.cookie, buf, blk))
3239f988b79SJean-Baptiste Boric 		error = EIO;
3249f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
3259f988b79SJean-Baptiste Boric 
3269f988b79SJean-Baptiste Boric 	if (v7fs_inode_isdir(&inode)) {
3279f988b79SJean-Baptiste Boric 		parent_dir->nlink++;
3289f988b79SJean-Baptiste Boric 		v7fs_inode_writeback(fs, parent_dir);
3299f988b79SJean-Baptiste Boric 	}
3309f988b79SJean-Baptiste Boric 
3319f988b79SJean-Baptiste Boric 	DPRINTF("done. (dirent size=%dbyte)\n", parent_dir->filesize);
3329f988b79SJean-Baptiste Boric 
3339f988b79SJean-Baptiste Boric 	return error;
3349f988b79SJean-Baptiste Boric }
3359f988b79SJean-Baptiste Boric 
3369f988b79SJean-Baptiste Boric int
v7fs_directory_remove_entry(struct v7fs_self * fs,struct v7fs_inode * parent_dir,const char * name)3379f988b79SJean-Baptiste Boric v7fs_directory_remove_entry(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
3389f988b79SJean-Baptiste Boric     const char *name)
3399f988b79SJean-Baptiste Boric {
3409f988b79SJean-Baptiste Boric 	struct v7fs_inode inode;
3419f988b79SJean-Baptiste Boric 	int error;
3429f988b79SJean-Baptiste Boric 	struct v7fs_dirent lastdirent;
3439f988b79SJean-Baptiste Boric 	v7fs_daddr_t lastblk;
3449f988b79SJean-Baptiste Boric 	size_t sz, lastsz;
3459f988b79SJean-Baptiste Boric 	v7fs_off_t pos;
3469f988b79SJean-Baptiste Boric 	void *buf;
3479f988b79SJean-Baptiste Boric 
3489f988b79SJean-Baptiste Boric 	/* Setup replaced entry. */
3499f988b79SJean-Baptiste Boric 	sz = parent_dir->filesize;
3509f988b79SJean-Baptiste Boric 	lastblk = v7fs_datablock_last(fs, parent_dir,
3519f988b79SJean-Baptiste Boric 	    v7fs_inode_filesize(parent_dir));
3529f988b79SJean-Baptiste Boric 	lastsz = V7FS_RESIDUE_BSIZE(sz);
3539f988b79SJean-Baptiste Boric 	pos = lastsz - sizeof(lastdirent);
3549f988b79SJean-Baptiste Boric 
3559f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, lastblk)))
3569f988b79SJean-Baptiste Boric 		return EIO;
3579f988b79SJean-Baptiste Boric 	lastdirent = *((struct v7fs_dirent *)((uint8_t *)buf + pos));
3589f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
3599f988b79SJean-Baptiste Boric 	DPRINTF("last dirent=%d %s pos=%d\n",
3609f988b79SJean-Baptiste Boric 	    V7FS_VAL16(fs, lastdirent.inode_number), lastdirent.name, pos);
3619f988b79SJean-Baptiste Boric 
3629f988b79SJean-Baptiste Boric 	struct v7fs_lookup_arg lookup_arg =
3639f988b79SJean-Baptiste Boric 	    { .name = name, .replace = &lastdirent/*disk endian */ };
3649f988b79SJean-Baptiste Boric 	/* Search entry that removed. replace it to last dirent. */
3659f988b79SJean-Baptiste Boric 	if ((error = v7fs_datablock_foreach(fs, parent_dir, remove_subr,
3669f988b79SJean-Baptiste Boric 	    &lookup_arg)) != V7FS_ITERATOR_BREAK)
3679f988b79SJean-Baptiste Boric 		return ENOENT;
3689f988b79SJean-Baptiste Boric 
3699f988b79SJean-Baptiste Boric 	/* Contract dirent entries. */
3709f988b79SJean-Baptiste Boric 	v7fs_datablock_contract(fs, parent_dir, sizeof(lastdirent));
3719f988b79SJean-Baptiste Boric 	DPRINTF("done. (dirent size=%dbyte)\n", parent_dir->filesize);
3729f988b79SJean-Baptiste Boric 
3739f988b79SJean-Baptiste Boric 	/* Target inode */
3749f988b79SJean-Baptiste Boric 	if ((error = v7fs_inode_load(fs, &inode, lookup_arg.inode_number)))
3759f988b79SJean-Baptiste Boric 		return error;
3769f988b79SJean-Baptiste Boric 
3779f988b79SJean-Baptiste Boric 	if (v7fs_inode_isdir(&inode)) {
3789f988b79SJean-Baptiste Boric 		parent_dir->nlink--;
3799f988b79SJean-Baptiste Boric 		v7fs_inode_writeback(fs, parent_dir);
3809f988b79SJean-Baptiste Boric 	}
3819f988b79SJean-Baptiste Boric 
3829f988b79SJean-Baptiste Boric 	return 0;
3839f988b79SJean-Baptiste Boric }
3849f988b79SJean-Baptiste Boric 
3859f988b79SJean-Baptiste Boric static int
remove_subr(struct v7fs_self * fs,void * ctx,v7fs_daddr_t blk,size_t sz)3869f988b79SJean-Baptiste Boric remove_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
3879f988b79SJean-Baptiste Boric {
3889f988b79SJean-Baptiste Boric 	struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
3899f988b79SJean-Baptiste Boric 	struct v7fs_dirent *dir;
3909f988b79SJean-Baptiste Boric 	void *buf;
3919f988b79SJean-Baptiste Boric 	size_t i;
3929f988b79SJean-Baptiste Boric 	int ret = 0;
3939f988b79SJean-Baptiste Boric 
3949f988b79SJean-Baptiste Boric 	DPRINTF("match start blk=%x\n", blk);
3959f988b79SJean-Baptiste Boric 	if (!(buf = scratch_read(fs, blk)))
3969f988b79SJean-Baptiste Boric 		return EIO;
3979f988b79SJean-Baptiste Boric 
3989f988b79SJean-Baptiste Boric 	dir = (struct v7fs_dirent *)buf;
3999f988b79SJean-Baptiste Boric 
4009f988b79SJean-Baptiste Boric 	for (i = 0; i < sz / sizeof(*dir); i++, dir++) {
4019f988b79SJean-Baptiste Boric 		DPRINTF("%d\n", V7FS_VAL16(fs, dir->inode_number));
4029f988b79SJean-Baptiste Boric 		if (strncmp(p->name,
4039f988b79SJean-Baptiste Boric 			(const char *)dir->name, V7FS_NAME_MAX) == 0) {
4049f988b79SJean-Baptiste Boric 			p->inode_number = V7FS_VAL16(fs, dir->inode_number);
4059f988b79SJean-Baptiste Boric 			/* Replace to last dirent. */
4069f988b79SJean-Baptiste Boric 			*dir = *(p->replace); /* disk endian */
4079f988b79SJean-Baptiste Boric 			/* Write back. */
4089f988b79SJean-Baptiste Boric 			if (!fs->io.write(fs->io.cookie, buf, blk))
4099f988b79SJean-Baptiste Boric 				ret = EIO;
4109f988b79SJean-Baptiste Boric 			else
4119f988b79SJean-Baptiste Boric 				ret = V7FS_ITERATOR_BREAK;
4129f988b79SJean-Baptiste Boric 			break;
4139f988b79SJean-Baptiste Boric 		}
4149f988b79SJean-Baptiste Boric 	}
4159f988b79SJean-Baptiste Boric 	scratch_free(fs, buf);
4169f988b79SJean-Baptiste Boric 
4179f988b79SJean-Baptiste Boric 	return ret;
4189f988b79SJean-Baptiste Boric }
419