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