v7fs_file.c revision 1.2 1 1.2 apb /* $NetBSD: v7fs_file.c,v 1.2 2011/07/18 21:51:49 apb 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.2 apb #if HAVE_NBTOOL_CONFIG_H
33 1.2 apb #include "nbtool_config.h"
34 1.2 apb #endif
35 1.2 apb
36 1.1 uch #include <sys/cdefs.h>
37 1.2 apb __KERNEL_RCSID(0, "$NetBSD: v7fs_file.c,v 1.2 2011/07/18 21:51:49 apb Exp $");
38 1.1 uch #if defined _KERNEL_OPT
39 1.1 uch #include "opt_v7fs.h"
40 1.1 uch #endif
41 1.1 uch
42 1.1 uch #ifdef _KERNEL
43 1.1 uch #include <sys/systm.h>
44 1.1 uch #include <sys/param.h>
45 1.1 uch #else
46 1.1 uch #include <stdio.h>
47 1.1 uch #include <string.h>
48 1.1 uch #include <errno.h>
49 1.1 uch #define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b))
50 1.1 uch #endif
51 1.1 uch
52 1.1 uch #include "v7fs.h"
53 1.1 uch #include "v7fs_impl.h"
54 1.1 uch #include "v7fs_endian.h"
55 1.1 uch #include "v7fs_inode.h"
56 1.1 uch #include "v7fs_dirent.h"
57 1.1 uch #include "v7fs_file.h"
58 1.1 uch #include "v7fs_datablock.h"
59 1.1 uch
60 1.1 uch #ifdef V7FS_FILE_DEBUG
61 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
62 1.1 uch #else
63 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
64 1.1 uch #endif
65 1.1 uch
66 1.1 uch static int lookup_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
67 1.1 uch static int remove_subr(struct v7fs_self *, void *, v7fs_daddr_t, size_t);
68 1.1 uch
69 1.1 uch int
70 1.1 uch v7fs_file_lookup_by_name(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
71 1.1 uch const char *name, v7fs_ino_t *ino)
72 1.1 uch {
73 1.1 uch char filename[V7FS_NAME_MAX + 1];
74 1.1 uch char *q;
75 1.1 uch int error;
76 1.1 uch size_t len;
77 1.1 uch
78 1.1 uch if ((q = strchr(name, '/'))) {
79 1.1 uch /* Zap following path. */
80 1.1 uch len = MIN(V7FS_NAME_MAX + 1, q - name);
81 1.1 uch memcpy(filename, name, len);
82 1.1 uch filename[len] = '\0'; /* '/' -> '\0' */
83 1.1 uch } else {
84 1.1 uch v7fs_dirent_filename(filename, name);
85 1.1 uch }
86 1.1 uch DPRINTF("%s(%s) dir=%d\n", filename, name, parent_dir->inode_number);
87 1.1 uch
88 1.1 uch struct v7fs_lookup_arg lookup_arg = { .name = filename,
89 1.1 uch .inode_number = 0 };
90 1.1 uch if ((error = v7fs_datablock_foreach(fs, parent_dir, lookup_subr,
91 1.1 uch &lookup_arg)) != V7FS_ITERATOR_BREAK) {
92 1.1 uch DPRINTF("not found.\n");
93 1.1 uch return ENOENT;
94 1.1 uch }
95 1.1 uch
96 1.1 uch *ino = lookup_arg.inode_number;
97 1.1 uch DPRINTF("done. ino=%d\n", *ino);
98 1.1 uch
99 1.1 uch return 0;
100 1.1 uch }
101 1.1 uch
102 1.1 uch static int
103 1.1 uch lookup_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
104 1.1 uch {
105 1.1 uch struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
106 1.1 uch struct v7fs_dirent *dir;
107 1.1 uch const char *name = p->name;
108 1.1 uch void *buf;
109 1.1 uch size_t i, n;
110 1.1 uch int ret = 0;
111 1.1 uch
112 1.1 uch if (!(buf = scratch_read(fs, blk)))
113 1.1 uch return EIO;
114 1.1 uch
115 1.1 uch dir = (struct v7fs_dirent *)buf;
116 1.1 uch n = sz / sizeof(*dir);
117 1.1 uch v7fs_dirent_endian_convert(fs, dir, n);
118 1.1 uch
119 1.1 uch for (i = 0; i < n; i++, dir++) {
120 1.1 uch if (dir->inode_number < 1) {
121 1.1 uch DPRINTF("*** bad inode #%d ***\n", dir->inode_number);
122 1.1 uch continue;
123 1.1 uch }
124 1.1 uch
125 1.1 uch if (strncmp((const char *)dir->name, name, V7FS_NAME_MAX) == 0)
126 1.1 uch {
127 1.1 uch p->inode_number = dir->inode_number;
128 1.1 uch ret = V7FS_ITERATOR_BREAK; /* found */
129 1.1 uch break;
130 1.1 uch }
131 1.1 uch }
132 1.1 uch scratch_free(fs, buf);
133 1.1 uch
134 1.1 uch return ret;
135 1.1 uch }
136 1.1 uch
137 1.1 uch int
138 1.1 uch v7fs_file_allocate(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
139 1.1 uch const char *srcname, struct v7fs_fileattr *attr, v7fs_ino_t *ino)
140 1.1 uch {
141 1.1 uch struct v7fs_inode inode;
142 1.1 uch char filename[V7FS_NAME_MAX + 1];
143 1.1 uch struct v7fs_dirent *dir;
144 1.1 uch int error;
145 1.1 uch
146 1.1 uch /* Truncate filename. */
147 1.1 uch v7fs_dirent_filename(filename, srcname);
148 1.1 uch DPRINTF("%s(%s)\n", filename, srcname);
149 1.1 uch
150 1.1 uch /* Check filename. */
151 1.1 uch if (v7fs_file_lookup_by_name(fs, parent_dir, filename, ino) == 0) {
152 1.1 uch DPRINTF("%s exists\n", filename);
153 1.1 uch return EEXIST;
154 1.1 uch }
155 1.1 uch
156 1.1 uch /* Get new inode. */
157 1.1 uch if ((error = v7fs_inode_allocate(fs, ino)))
158 1.1 uch return error;
159 1.1 uch
160 1.1 uch /* Set initial attribute. */
161 1.1 uch memset(&inode, 0, sizeof(inode));
162 1.1 uch inode.inode_number = *ino;
163 1.1 uch inode.mode = attr->mode;
164 1.1 uch inode.uid = attr->uid;
165 1.1 uch inode.gid = attr->gid;
166 1.1 uch if (attr->ctime)
167 1.1 uch inode.ctime = attr->ctime;
168 1.1 uch if (attr->mtime)
169 1.1 uch inode.mtime = attr->mtime;
170 1.1 uch if (attr->atime)
171 1.1 uch inode.atime = attr->atime;
172 1.1 uch
173 1.1 uch switch (inode.mode & V7FS_IFMT) {
174 1.1 uch default:
175 1.1 uch DPRINTF("Can't allocate %o type.\n", inode.mode);
176 1.1 uch v7fs_inode_deallocate(fs, *ino);
177 1.1 uch return EINVAL;
178 1.1 uch case V7FS_IFCHR:
179 1.1 uch /* FALLTHROUGH */
180 1.1 uch case V7FS_IFBLK:
181 1.1 uch inode.nlink = 1;
182 1.1 uch inode.device = attr->device;
183 1.1 uch inode.addr[0] = inode.device;
184 1.1 uch break;
185 1.1 uch case V7FSBSD_IFFIFO:
186 1.1 uch /* FALLTHROUGH */
187 1.1 uch case V7FSBSD_IFSOCK:
188 1.1 uch /* FALLTHROUGH */
189 1.1 uch case V7FSBSD_IFLNK:
190 1.1 uch /* FALLTHROUGH */
191 1.1 uch case V7FS_IFREG:
192 1.1 uch inode.nlink = 1;
193 1.1 uch break;
194 1.1 uch case V7FS_IFDIR:
195 1.1 uch inode.nlink = 2; /* . + .. */
196 1.1 uch if ((error = v7fs_datablock_expand(fs, &inode, sizeof(*dir) * 2
197 1.1 uch ))) {
198 1.1 uch v7fs_inode_deallocate(fs, *ino);
199 1.1 uch return error;
200 1.1 uch }
201 1.1 uch v7fs_daddr_t blk = inode.addr[0];
202 1.1 uch void *buf;
203 1.1 uch if (!(buf = scratch_read(fs, blk))) {
204 1.1 uch v7fs_inode_deallocate(fs, *ino);
205 1.1 uch return EIO;
206 1.1 uch }
207 1.1 uch dir = (struct v7fs_dirent *)buf;
208 1.1 uch strcpy(dir[0].name, ".");
209 1.1 uch dir[0].inode_number = V7FS_VAL16(fs, *ino);
210 1.1 uch strcpy(dir[1].name, "..");
211 1.1 uch dir[1].inode_number = V7FS_VAL16(fs, parent_dir->inode_number);
212 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk)) {
213 1.1 uch scratch_free(fs, buf);
214 1.1 uch return EIO;
215 1.1 uch }
216 1.1 uch scratch_free(fs, buf);
217 1.1 uch break;
218 1.1 uch }
219 1.1 uch
220 1.1 uch v7fs_inode_writeback(fs, &inode);
221 1.1 uch
222 1.1 uch /* Link this inode to parent directory. */
223 1.1 uch if ((error = v7fs_directory_add_entry(fs, parent_dir, *ino, filename)))
224 1.1 uch {
225 1.1 uch DPRINTF("can't add dirent.\n");
226 1.1 uch return error;
227 1.1 uch }
228 1.1 uch
229 1.1 uch return 0;
230 1.1 uch }
231 1.1 uch
232 1.1 uch int
233 1.1 uch v7fs_file_deallocate(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
234 1.1 uch const char *name)
235 1.1 uch {
236 1.1 uch v7fs_ino_t ino;
237 1.1 uch struct v7fs_inode inode;
238 1.1 uch int error;
239 1.1 uch
240 1.1 uch DPRINTF("%s\n", name);
241 1.1 uch if ((error = v7fs_file_lookup_by_name(fs, parent_dir, name, &ino))) {
242 1.1 uch DPRINTF("no such a file: %s\n", name);
243 1.1 uch return error;
244 1.1 uch }
245 1.1 uch DPRINTF("%s->#%d\n", name, ino);
246 1.1 uch if ((error = v7fs_inode_load(fs, &inode, ino)))
247 1.1 uch return error;
248 1.1 uch
249 1.1 uch if (v7fs_inode_isdir(&inode)) {
250 1.1 uch /* Check child directory exists. */
251 1.1 uch if (v7fs_inode_filesize(&inode) !=
252 1.1 uch sizeof(struct v7fs_dirent) * 2 /*"." + ".."*/) {
253 1.1 uch DPRINTF("file exists.\n");
254 1.1 uch return EEXIST;
255 1.1 uch }
256 1.1 uch inode.nlink = 0; /* remove this. */
257 1.1 uch } else {
258 1.1 uch /* Decrement reference count. */
259 1.1 uch --inode.nlink; /* regular file. */
260 1.1 uch }
261 1.1 uch
262 1.1 uch
263 1.1 uch if ((error = v7fs_directory_remove_entry(fs, parent_dir, name)))
264 1.1 uch return error;
265 1.1 uch DPRINTF("remove dirent\n");
266 1.1 uch
267 1.1 uch if (inode.nlink == 0) {
268 1.1 uch v7fs_datablock_contract(fs, &inode, inode.filesize);
269 1.1 uch DPRINTF("remove datablock\n");
270 1.1 uch v7fs_inode_deallocate(fs, ino);
271 1.1 uch DPRINTF("remove inode\n");
272 1.1 uch } else {
273 1.1 uch v7fs_inode_writeback(fs, &inode);
274 1.1 uch }
275 1.1 uch
276 1.1 uch return 0;
277 1.1 uch }
278 1.1 uch
279 1.1 uch int
280 1.1 uch v7fs_directory_add_entry(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
281 1.1 uch v7fs_ino_t ino, const char *srcname)
282 1.1 uch {
283 1.1 uch struct v7fs_inode inode;
284 1.1 uch struct v7fs_dirent *dir;
285 1.1 uch int error = 0;
286 1.1 uch v7fs_daddr_t blk;
287 1.1 uch void *buf;
288 1.1 uch char filename[V7FS_NAME_MAX + 1];
289 1.1 uch
290 1.1 uch /* Truncate filename. */
291 1.1 uch v7fs_dirent_filename(filename, srcname);
292 1.1 uch DPRINTF("%s(%s) %d\n", filename, srcname, ino);
293 1.1 uch
294 1.1 uch /* Target inode */
295 1.1 uch if ((error = v7fs_inode_load(fs, &inode, ino)))
296 1.1 uch return error;
297 1.1 uch
298 1.1 uch /* Expand datablock. */
299 1.1 uch if ((error = v7fs_datablock_expand(fs, parent_dir, sizeof(*dir))))
300 1.1 uch return error;
301 1.1 uch
302 1.1 uch /* Read last entry. */
303 1.1 uch if (!(blk = v7fs_datablock_last(fs, parent_dir,
304 1.1 uch v7fs_inode_filesize(parent_dir))))
305 1.1 uch return EIO;
306 1.1 uch
307 1.1 uch /* Load dirent block. This vnode(parent dir) is locked by VFS layer. */
308 1.1 uch if (!(buf = scratch_read(fs, blk)))
309 1.1 uch return EIO;
310 1.1 uch
311 1.1 uch size_t sz = v7fs_inode_filesize(parent_dir);
312 1.1 uch sz = V7FS_RESIDUE_BSIZE(sz); /* last block payload. */
313 1.1 uch int n = sz / sizeof(*dir) - 1;
314 1.1 uch /* Add dirent. */
315 1.1 uch dir = (struct v7fs_dirent *)buf;
316 1.1 uch dir[n].inode_number = V7FS_VAL16(fs, ino);
317 1.1 uch memcpy((char *)dir[n].name, filename, V7FS_NAME_MAX);
318 1.1 uch /* Write back datablock */
319 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk))
320 1.1 uch error = EIO;
321 1.1 uch scratch_free(fs, buf);
322 1.1 uch
323 1.1 uch if (v7fs_inode_isdir(&inode)) {
324 1.1 uch parent_dir->nlink++;
325 1.1 uch v7fs_inode_writeback(fs, parent_dir);
326 1.1 uch }
327 1.1 uch
328 1.1 uch DPRINTF("done. (dirent size=%dbyte)\n", parent_dir->filesize);
329 1.1 uch
330 1.1 uch return error;
331 1.1 uch }
332 1.1 uch
333 1.1 uch int
334 1.1 uch v7fs_directory_remove_entry(struct v7fs_self *fs, struct v7fs_inode *parent_dir,
335 1.1 uch const char *name)
336 1.1 uch {
337 1.1 uch struct v7fs_inode inode;
338 1.1 uch int error;
339 1.1 uch struct v7fs_dirent lastdirent;
340 1.1 uch v7fs_daddr_t lastblk;
341 1.1 uch size_t sz, lastsz;
342 1.1 uch v7fs_off_t pos;
343 1.1 uch void *buf;
344 1.1 uch
345 1.1 uch /* Setup replaced entry. */
346 1.1 uch sz = parent_dir->filesize;
347 1.1 uch lastblk = v7fs_datablock_last(fs, parent_dir,
348 1.1 uch v7fs_inode_filesize(parent_dir));
349 1.1 uch lastsz = V7FS_RESIDUE_BSIZE(sz);
350 1.1 uch pos = lastsz - sizeof(lastdirent);
351 1.1 uch
352 1.1 uch if (!(buf = scratch_read(fs, lastblk)))
353 1.1 uch return EIO;
354 1.1 uch lastdirent = *((struct v7fs_dirent *)((uint8_t *)buf + pos));
355 1.1 uch scratch_free(fs, buf);
356 1.1 uch DPRINTF("last dirent=%d %s pos=%d\n",
357 1.1 uch V7FS_VAL16(fs, lastdirent.inode_number), lastdirent.name, pos);
358 1.1 uch
359 1.1 uch struct v7fs_lookup_arg lookup_arg =
360 1.1 uch { .name = name, .replace = &lastdirent/*disk endian */ };
361 1.1 uch /* Search entry that removed. replace it to last dirent. */
362 1.1 uch if ((error = v7fs_datablock_foreach(fs, parent_dir, remove_subr,
363 1.1 uch &lookup_arg)) != V7FS_ITERATOR_BREAK)
364 1.1 uch return ENOENT;
365 1.1 uch
366 1.1 uch /* Contract dirent entries. */
367 1.1 uch v7fs_datablock_contract(fs, parent_dir, sizeof(lastdirent));
368 1.1 uch DPRINTF("done. (dirent size=%dbyte)\n", parent_dir->filesize);
369 1.1 uch
370 1.1 uch /* Target inode */
371 1.1 uch if ((error = v7fs_inode_load(fs, &inode, lookup_arg.inode_number)))
372 1.1 uch return error;
373 1.1 uch
374 1.1 uch if (v7fs_inode_isdir(&inode)) {
375 1.1 uch parent_dir->nlink--;
376 1.1 uch v7fs_inode_writeback(fs, parent_dir);
377 1.1 uch }
378 1.1 uch
379 1.1 uch return 0;
380 1.1 uch }
381 1.1 uch
382 1.1 uch static int
383 1.1 uch remove_subr(struct v7fs_self *fs, void *ctx, v7fs_daddr_t blk, size_t sz)
384 1.1 uch {
385 1.1 uch struct v7fs_lookup_arg *p = (struct v7fs_lookup_arg *)ctx;
386 1.1 uch struct v7fs_dirent *dir;
387 1.1 uch void *buf;
388 1.1 uch size_t i;
389 1.1 uch int ret = 0;
390 1.1 uch
391 1.1 uch DPRINTF("match start blk=%x\n", blk);
392 1.1 uch if (!(buf = scratch_read(fs, blk)))
393 1.1 uch return EIO;
394 1.1 uch
395 1.1 uch dir = (struct v7fs_dirent *)buf;
396 1.1 uch
397 1.1 uch for (i = 0; i < sz / sizeof(*dir); i++, dir++) {
398 1.1 uch DPRINTF("%d\n", V7FS_VAL16(fs, dir->inode_number));
399 1.1 uch if (strncmp(p->name,
400 1.1 uch (const char *)dir->name, V7FS_NAME_MAX) == 0) {
401 1.1 uch p->inode_number = V7FS_VAL16(fs, dir->inode_number);
402 1.1 uch /* Replace to last dirent. */
403 1.1 uch *dir = *(p->replace); /* disk endian */
404 1.1 uch /* Write back. */
405 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk))
406 1.1 uch ret = EIO;
407 1.1 uch else
408 1.1 uch ret = V7FS_ITERATOR_BREAK;
409 1.1 uch break;
410 1.1 uch }
411 1.1 uch }
412 1.1 uch scratch_free(fs, buf);
413 1.1 uch
414 1.1 uch return ret;
415 1.1 uch }
416