1 /* $NetBSD: v7fs_file_util.c,v 1.4 2011/07/30 03:52:04 uch Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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"
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 $");
39 #include <sys/systm.h>
40 #include <sys/param.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)
58 #define DPRINTF(fmt, args...) ((void)0)
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
,
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);
69 v7fs_file_link(struct v7fs_self
*fs
, struct v7fs_inode
*parent_dir
,
70 struct v7fs_inode
*p
, const char *name
)
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
,
77 DPRINTF("can't add entry");
81 v7fs_inode_writeback(fs
, p
);
87 v7fs_file_symlink(struct v7fs_self
*fs
, struct v7fs_inode
*p
,
91 size_t len
= strlen(target
) + 1;
93 if (len
> V7FSBSD_MAXSYMLINKLEN
) {/* limited target 512byte pathname */
94 DPRINTF("too long pathname.");
98 if ((error
= v7fs_datablock_expand(fs
, p
, len
))) {
102 v7fs_daddr_t blk
= p
->addr
[0]; /* 1block only. */
104 if (!(buf
= scratch_read(fs
, blk
))) {
108 strncpy(buf
, target
, V7FS_BSIZE
);
109 if (!fs
->io
.write(fs
->io
.cookie
, buf
, blk
)) {
110 scratch_free(fs
, buf
);
113 scratch_free(fs
, buf
);
114 v7fs_inode_writeback(fs
, p
);
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
;
128 /* Check source file */
129 if ((error
= v7fs_file_lookup_by_name(fs
, parent_from
, from
,
131 DPRINTF("%s don't exists\n", from
);
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
);
145 } else if (error
!= ENOENT
) {
146 DPRINTF("error=%d\n", 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
,
157 if ((error
= v7fs_directory_add_entry(fs
, parent_to
, from_ino
, to
))) {
158 DPRINTF("can't add entry");
162 if ((error
= v7fs_directory_remove_entry(fs
, parent_from
, from
))) {
163 DPRINTF("can't remove entry");
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");
174 v7fs_inode_writeback(fs
, &inode
);
182 v7fs_directory_replace_entry(struct v7fs_self
*fs
, struct v7fs_inode
*self_dir
,
183 const char *name
, v7fs_ino_t ino
)
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
)
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
;
206 DPRINTF("match start blk=%x\n", blk
);
207 if (!(buf
= scratch_read(fs
, blk
)))
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
)
217 dir
->inode_number
= V7FS_VAL16(fs
, p
->inode_number
);
219 if (!fs
->io
.write(fs
->io
.cookie
, buf
, blk
))
222 ret
= V7FS_ITERATOR_BREAK
;
226 scratch_free(fs
, buf
);
232 v7fs_file_lookup_by_number(struct v7fs_self
*fs
, struct v7fs_inode
*parent_dir
,
233 v7fs_ino_t ino
, char *buf
)
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
;
244 lookup_by_number_subr(struct v7fs_self
*fs
, void *ctx
, v7fs_daddr_t blk
,
247 struct v7fs_lookup_arg
*p
= (struct v7fs_lookup_arg
*)ctx
;
248 struct v7fs_dirent
*dir
;
253 if (!(buf
= scratch_read(fs
, blk
)))
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
) {
263 v7fs_dirent_filename(p
->buf
, dir
->name
);
264 ret
= V7FS_ITERATOR_BREAK
;
268 scratch_free(fs
, buf
);
273 struct lookup_parent_arg
{
274 v7fs_ino_t parent_ino
;
278 can_dirmove(struct v7fs_self
*fs
, v7fs_ino_t from_ino
, v7fs_ino_t to_ino
)
280 struct v7fs_inode inode
;
285 if ((error
= v7fs_inode_load(fs
, &inode
, to_ino
)))
288 if (!v7fs_inode_isdir(&inode
))
291 /* Lookup the parent. */
293 struct lookup_parent_arg arg
;
294 /* Search parent dir */
296 v7fs_datablock_foreach(fs
, &inode
, lookup_parent_from_dir_subr
,
298 if ((parent
= arg
.parent_ino
) == 0) {
299 DPRINTF("***parent missing\n");
302 /* Load parent dir */
303 if ((error
= v7fs_inode_load(fs
, &inode
, parent
)))
305 if (parent
== from_ino
) {
306 DPRINTF("#%d is child dir of #%d\n", to_ino
, from_ino
);
309 } while (parent
!= V7FS_ROOT_INODE
);
315 lookup_parent_from_dir_subr(struct v7fs_self
*fs
, void *ctx
, v7fs_daddr_t blk
,
318 struct lookup_parent_arg
*arg
= (struct lookup_parent_arg
*)ctx
;
319 char name
[V7FS_NAME_MAX
+ 1];
323 if (!(buf
= scratch_read(fs
, blk
)))
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)
337 arg
->parent_ino
= dir
->inode_number
;
338 ret
= V7FS_ITERATOR_BREAK
;
342 scratch_free(fs
, buf
);