etc/protocols - sync with NetBSD-8
[minix.git] / sys / fs / v7fs / v7fs_file_util.c
bloba6cfb32e80f4a864bcdeb043d4515e6103059dea
1 /* $NetBSD: v7fs_file_util.c,v 1.4 2011/07/30 03:52:04 uch Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: v7fs_file_util.c,v 1.4 2011/07/30 03:52:04 uch Exp $");
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <sys/param.h>
41 #else
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #endif
47 #include "v7fs.h"
48 #include "v7fs_impl.h"
49 #include "v7fs_endian.h"
50 #include "v7fs_inode.h"
51 #include "v7fs_dirent.h"
52 #include "v7fs_file.h"
53 #include "v7fs_datablock.h"
55 #ifdef V7FS_FILE_DEBUG
56 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
57 #else
58 #define DPRINTF(fmt, args...) ((void)0)
59 #endif
61 static int replace_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
62 static int lookup_by_number_subr(struct v7fs_self *, void *, v7fs_daddr_t,
63 size_t);
64 static int can_dirmove(struct v7fs_self *, v7fs_ino_t, v7fs_ino_t);
65 static int lookup_parent_from_dir_subr(struct v7fs_self *, void *,
66 v7fs_daddr_t, size_t);
68 int
69 v7fs_file_link(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
70 struct v7fs_inode *p, const char *name)
72 int error = 0;
74 DPRINTF("%d %d %s\n", parent_dir->inode_number, p->inode_number, name);
75 if ((error = v7fs_directory_add_entry(fs, parent_dir, p->inode_number,
76 name))) {
77 DPRINTF("can't add entry");
78 return error;
80 p->nlink++;
81 v7fs_inode_writeback(fs, p);
83 return 0;
86 int
87 v7fs_file_symlink(struct v7fs_self *fs, struct v7fs_inode *p,
88 const char *target)
90 int error;
91 size_t len = strlen(target) + 1;
93 if (len > V7FSBSD_MAXSYMLINKLEN) {/* limited target 512byte pathname */
94 DPRINTF("too long pathname.");
95 return ENAMETOOLONG;
98 if ((error = v7fs_datablock_expand(fs, p, len))) {
99 return error;
102 v7fs_daddr_t blk = p->addr[0]; /* 1block only. */
103 void *buf;
104 if (!(buf = scratch_read(fs, blk))) {
105 return EIO;
108 strncpy(buf, target, V7FS_BSIZE);
109 if (!fs->io.write(fs->io.cookie, buf, blk)) {
110 scratch_free(fs, buf);
111 return EIO;
113 scratch_free(fs, buf);
114 v7fs_inode_writeback(fs, p);
116 return 0;
120 v7fs_file_rename(struct v7fs_self *fs, struct v7fs_inode *parent_from,
121 const char *from, struct v7fs_inode *parent_to, const char *to)
123 v7fs_ino_t from_ino, to_ino;
124 struct v7fs_inode inode;
125 int error;
126 bool dir_move;
128 /* Check source file */
129 if ((error = v7fs_file_lookup_by_name(fs, parent_from, from,
130 &from_ino))) {
131 DPRINTF("%s don't exists\n", from);
132 return error;
134 v7fs_inode_load(fs, &inode, from_ino);
135 dir_move = v7fs_inode_isdir(&inode);
137 /* Check target file */
138 error = v7fs_file_lookup_by_name(fs, parent_to, to, &to_ino);
139 if (error == 0) { /* found */
140 DPRINTF("%s already exists\n", to);
141 if ((error = v7fs_file_deallocate(fs, parent_to, to))) {
142 DPRINTF("%s can't remove %d\n", to, error);
143 return error;
145 } else if (error != ENOENT) {
146 DPRINTF("error=%d\n", error);
147 return error;
149 /* Check directory hierarchy. t_vnops rename_dir(5) */
150 if (dir_move && (error = can_dirmove(fs, from_ino,
151 parent_to->inode_number))) {
152 DPRINTF("dst '%s' is child dir of '%s'. error=%d\n", to, from,
153 error);
154 return error;
157 if ((error = v7fs_directory_add_entry(fs, parent_to, from_ino, to))) {
158 DPRINTF("can't add entry");
159 return error;
162 if ((error = v7fs_directory_remove_entry(fs, parent_from, from))) {
163 DPRINTF("can't remove entry");
164 return error;
167 if (dir_move && (parent_from != parent_to)) {
168 /* If directory move, update ".." */
169 if ((error = v7fs_directory_replace_entry(fs, &inode, "..",
170 parent_to->inode_number))) {
171 DPRINTF("can't replace parent dir");
172 return error;
174 v7fs_inode_writeback(fs, &inode);
177 return 0;
182 v7fs_directory_replace_entry(struct v7fs_self *fs, struct v7fs_inode *self_dir,
183 const char *name, v7fs_ino_t ino)
185 int error;
187 /* Search entry that replaced. replace it to new inode number. */
188 struct v7fs_lookup_arg lookup_arg = { .name = name,
189 .inode_number = ino };
190 if ((error = v7fs_datablock_foreach(fs, self_dir, replace_subr,
191 &lookup_arg)) != V7FS_ITERATOR_BREAK)
192 return ENOENT;
194 return 0;
197 static int
198 replace_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
200 struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
201 struct v7fs_dirent *dir;
202 void *buf;
203 size_t i, n;
204 int ret = 0;
206 DPRINTF("match start blk=%x\n", blk);
207 if (!(buf = scratch_read(fs, blk)))
208 return EIO;
210 dir = (struct v7fs_dirent *)buf;
211 n = sz / sizeof(*dir);
213 for (i = 0; i < n; i++, dir++) { /*disk endian */
214 if (strncmp(p->name, (const char *)dir->name, V7FS_NAME_MAX)
215 == 0) {
216 /* Replace inode# */
217 dir->inode_number = V7FS_VAL16(fs, p->inode_number);
218 /* Write back. */
219 if (!fs->io.write(fs->io.cookie, buf, blk))
220 ret = EIO;
221 else
222 ret = V7FS_ITERATOR_BREAK;
223 break;
226 scratch_free(fs, buf);
228 return ret;
231 bool
232 v7fs_file_lookup_by_number(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
233 v7fs_ino_t ino, char *buf)
235 int ret;
237 ret = v7fs_datablock_foreach(fs, parent_dir, lookup_by_number_subr,
238 &(struct v7fs_lookup_arg){ .inode_number = ino, .buf = buf });
240 return ret == V7FS_ITERATOR_BREAK;
243 static int
244 lookup_by_number_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
245 size_t sz)
247 struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
248 struct v7fs_dirent *dir;
249 void *buf;
250 size_t i, n;
251 int ret = 0;
253 if (!(buf = scratch_read(fs, blk)))
254 return EIO;
256 dir = (struct v7fs_dirent *)buf;
257 n = sz / sizeof(*dir);
258 v7fs_dirent_endian_convert(fs, dir, n);
260 for (i = 0; i < n; i++, dir++) {
261 if (dir->inode_number == p->inode_number) {
262 if (p->buf)
263 v7fs_dirent_filename(p->buf, dir->name);
264 ret = V7FS_ITERATOR_BREAK;
265 break;
268 scratch_free(fs, buf);
270 return ret;
273 struct lookup_parent_arg {
274 v7fs_ino_t parent_ino;
277 static int
278 can_dirmove(struct v7fs_self *fs, v7fs_ino_t from_ino, v7fs_ino_t to_ino)
280 struct v7fs_inode inode;
281 v7fs_ino_t parent;
282 int error;
284 /* Start dir. */
285 if ((error = v7fs_inode_load(fs, &inode, to_ino)))
286 return error;
288 if (!v7fs_inode_isdir(&inode))
289 return ENOTDIR;
291 /* Lookup the parent. */
292 do {
293 struct lookup_parent_arg arg;
294 /* Search parent dir */
295 arg.parent_ino = 0;
296 v7fs_datablock_foreach(fs, &inode, lookup_parent_from_dir_subr,
297 &arg);
298 if ((parent = arg.parent_ino) == 0) {
299 DPRINTF("***parent missing\n");
300 return ENOENT;
302 /* Load parent dir */
303 if ((error = v7fs_inode_load(fs, &inode, parent)))
304 return error;
305 if (parent == from_ino) {
306 DPRINTF("#%d is child dir of #%d\n", to_ino, from_ino);
307 return EINVAL;
309 } while (parent != V7FS_ROOT_INODE);
311 return 0;
314 static int
315 lookup_parent_from_dir_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk,
316 size_t sz)
318 struct lookup_parent_arg *arg = (struct lookup_parent_arg *)ctx;
319 char name[V7FS_NAME_MAX + 1];
320 void *buf;
321 int ret = 0;
323 if (!(buf = scratch_read(fs, blk)))
324 return 0;
325 struct v7fs_dirent *dir = (struct v7fs_dirent *)buf;
326 size_t i, n = sz / sizeof(*dir);
327 if (!v7fs_dirent_endian_convert(fs, dir, n)) {
328 scratch_free(fs, buf);
329 return V7FS_ITERATOR_ERROR;
332 for (i = 0; i < n; i++, dir++) {
333 v7fs_dirent_filename(name, dir->name);
334 if (strncmp(dir->name, "..", V7FS_NAME_MAX) != 0)
335 continue;
337 arg->parent_ino = dir->inode_number;
338 ret = V7FS_ITERATOR_BREAK;
339 break;
342 scratch_free(fs, buf);
343 return ret;