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