chfs_build.c revision 1.1 1 1.1 ahoka /* $NetBSD: chfs_build.c,v 1.1 2011/11/24 15:51:31 ahoka Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2010 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * All rights reserved.
7 1.1 ahoka *
8 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
9 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
10 1.1 ahoka *
11 1.1 ahoka * Redistribution and use in source and binary forms, with or without
12 1.1 ahoka * modification, are permitted provided that the following conditions
13 1.1 ahoka * are met:
14 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
15 1.1 ahoka * notice, this list of conditions and the following disclaimer.
16 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
18 1.1 ahoka * documentation and/or other materials provided with the distribution.
19 1.1 ahoka *
20 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 ahoka * SUCH DAMAGE.
31 1.1 ahoka */
32 1.1 ahoka
33 1.1 ahoka #include "chfs.h"
34 1.1 ahoka //#include </root/xipffs/netbsd.chfs/chfs.h>
35 1.1 ahoka
36 1.1 ahoka
37 1.1 ahoka void
38 1.1 ahoka chfs_calc_trigger_levels(struct chfs_mount *chmp)
39 1.1 ahoka {
40 1.1 ahoka uint32_t size;
41 1.1 ahoka
42 1.1 ahoka chmp->chm_resv_blocks_deletion = 2;
43 1.1 ahoka
44 1.1 ahoka size = chmp->chm_ebh->flash_size / 50; //2% of flash size
45 1.1 ahoka size += chmp->chm_ebh->peb_nr * 100;
46 1.1 ahoka size += chmp->chm_ebh->eb_size - 1;
47 1.1 ahoka
48 1.1 ahoka chmp->chm_resv_blocks_write =
49 1.1 ahoka chmp->chm_resv_blocks_deletion + (size / chmp->chm_ebh->eb_size);
50 1.1 ahoka chmp->chm_resv_blocks_gctrigger = chmp->chm_resv_blocks_write + 1;
51 1.1 ahoka chmp->chm_resv_blocks_gcmerge = chmp->chm_resv_blocks_deletion + 1;
52 1.1 ahoka chmp->chm_vdirty_blocks_gctrigger = chmp->chm_resv_blocks_gctrigger * 10;
53 1.1 ahoka
54 1.1 ahoka chmp->chm_nospc_dirty =
55 1.1 ahoka chmp->chm_ebh->eb_size + (chmp->chm_ebh->flash_size / 100);
56 1.1 ahoka }
57 1.1 ahoka
58 1.1 ahoka
59 1.1 ahoka /**
60 1.1 ahoka * chfs_build_set_vnodecache_nlink - set pvno and nlink in vnodecaches
61 1.1 ahoka * @chmp: CHFS main descriptor structure
62 1.1 ahoka * @vc: vnode cache
63 1.1 ahoka * This function travels @vc's directory entries and sets the pvno and nlink
64 1.1 ahoka * attribute of the vnode where the dirent's vno points.
65 1.1 ahoka */
66 1.1 ahoka void
67 1.1 ahoka chfs_build_set_vnodecache_nlink(struct chfs_mount *chmp,
68 1.1 ahoka struct chfs_vnode_cache *vc)
69 1.1 ahoka {
70 1.1 ahoka struct chfs_dirent *fd;
71 1.1 ahoka //dbg("set nlink\n");
72 1.1 ahoka
73 1.1 ahoka // for (fd = vc->scan_dirents; fd; fd = fd->next) {
74 1.1 ahoka TAILQ_FOREACH(fd, &vc->scan_dirents, fds) {
75 1.1 ahoka struct chfs_vnode_cache *child_vc;
76 1.1 ahoka
77 1.1 ahoka if (!fd->vno)
78 1.1 ahoka continue;
79 1.1 ahoka
80 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
81 1.1 ahoka child_vc = chfs_vnode_cache_get(chmp, fd->vno);
82 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
83 1.1 ahoka if (!child_vc) {
84 1.1 ahoka chfs_mark_node_obsolete(chmp, fd->nref);
85 1.1 ahoka continue;
86 1.1 ahoka }
87 1.1 ahoka if (fd->type == VDIR) {
88 1.1 ahoka if (child_vc->nlink < 1)
89 1.1 ahoka child_vc->nlink = 1;
90 1.1 ahoka
91 1.1 ahoka if (child_vc->pvno) {
92 1.1 ahoka chfs_err("found a hard link: child dir: %s"
93 1.1 ahoka ", (vno: %llu) of dir vno: %llu\n",
94 1.1 ahoka fd->name, fd->vno, vc->vno);
95 1.1 ahoka } else {
96 1.1 ahoka //dbg("child_vc->pvno =
97 1.1 ahoka // vc->vno; pvno = %d\n", child_vc->pvno);
98 1.1 ahoka child_vc->pvno = vc->vno;
99 1.1 ahoka }
100 1.1 ahoka }
101 1.1 ahoka child_vc->nlink++;
102 1.1 ahoka //dbg("child_vc->nlink++;\n");
103 1.1 ahoka //child_vc->nlink++;
104 1.1 ahoka vc->nlink++;
105 1.1 ahoka }
106 1.1 ahoka }
107 1.1 ahoka
108 1.1 ahoka /**
109 1.1 ahoka * chfs_build_remove_unlinked vnode
110 1.1 ahoka */
111 1.1 ahoka /* static */
112 1.1 ahoka void
113 1.1 ahoka chfs_build_remove_unlinked_vnode(struct chfs_mount *chmp,
114 1.1 ahoka struct chfs_vnode_cache *vc,
115 1.1 ahoka // struct chfs_dirent **unlinked)
116 1.1 ahoka struct chfs_dirent_list *unlinked)
117 1.1 ahoka {
118 1.1 ahoka struct chfs_node_ref *nref;
119 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
120 1.1 ahoka
121 1.1 ahoka dbg("START\n");
122 1.1 ahoka dbg("vno: %llu\n", vc->vno);
123 1.1 ahoka
124 1.1 ahoka nref = vc->dnode;
125 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
126 1.1 ahoka // The vnode cache is at the end of the data node's chain
127 1.1 ahoka while (nref != (struct chfs_node_ref *)vc) {
128 1.1 ahoka struct chfs_node_ref *next = nref->nref_next;
129 1.1 ahoka dbg("mark dnode\n");
130 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
131 1.1 ahoka nref = next;
132 1.1 ahoka }
133 1.1 ahoka nref = vc->dirents;
134 1.1 ahoka // The vnode cache is at the end of the dirent node's chain
135 1.1 ahoka while (nref != (struct chfs_node_ref *)vc) {
136 1.1 ahoka struct chfs_node_ref *next = nref->nref_next;
137 1.1 ahoka dbg("mark dirent\n");
138 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
139 1.1 ahoka nref = next;
140 1.1 ahoka }
141 1.1 ahoka if (!TAILQ_EMPTY(&vc->scan_dirents)) {
142 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &vc->scan_dirents, fds, tmpfd) {
143 1.1 ahoka // while (vc->scan_dirents) {
144 1.1 ahoka struct chfs_vnode_cache *child_vc;
145 1.1 ahoka // fd = vc->scan_dirents;
146 1.1 ahoka dbg("dirent dump:\n");
147 1.1 ahoka dbg(" ->vno: %llu\n", fd->vno);
148 1.1 ahoka dbg(" ->version: %llu\n", fd->version);
149 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
150 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
151 1.1 ahoka dbg(" ->name: %s\n", fd->name);
152 1.1 ahoka dbg(" ->type: %d\n", fd->type);
153 1.1 ahoka // vc->scan_dirents = fd->next;
154 1.1 ahoka TAILQ_REMOVE(&vc->scan_dirents, fd, fds);
155 1.1 ahoka
156 1.1 ahoka if (!fd->vno) {
157 1.1 ahoka chfs_free_dirent(fd);
158 1.1 ahoka continue;
159 1.1 ahoka }
160 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
161 1.1 ahoka child_vc = chfs_vnode_cache_get(chmp, fd->vno);
162 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
163 1.1 ahoka if (!child_vc) {
164 1.1 ahoka chfs_free_dirent(fd);
165 1.1 ahoka continue;
166 1.1 ahoka }
167 1.1 ahoka /**
168 1.1 ahoka * Decrease nlink in child. If it is 0, add to unlinked
169 1.1 ahoka * dirents or just free it otherwise.
170 1.1 ahoka */
171 1.1 ahoka child_vc->nlink--;
172 1.1 ahoka
173 1.1 ahoka if (!child_vc->nlink) {
174 1.1 ahoka //dbg("nlink is 0\n");
175 1.1 ahoka // fd->next = *unlinked;
176 1.1 ahoka // *unlinked = fd;
177 1.1 ahoka // XXX HEAD or TAIL?
178 1.1 ahoka // original code did HEAD, but we could add
179 1.1 ahoka // it to the TAIL easily with TAILQ.
180 1.1 ahoka TAILQ_INSERT_TAIL(unlinked, fd, fds);
181 1.1 ahoka } else {
182 1.1 ahoka chfs_free_dirent(fd);
183 1.1 ahoka }
184 1.1 ahoka }
185 1.1 ahoka } else {
186 1.1 ahoka dbg("there are no scan dirents\n");
187 1.1 ahoka }
188 1.1 ahoka
189 1.1 ahoka nref = vc->v;
190 1.1 ahoka while ((struct chfs_vnode_cache *)nref != vc) {
191 1.1 ahoka if (!CHFS_REF_OBSOLETE(nref))
192 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
193 1.1 ahoka nref = nref->nref_next;
194 1.1 ahoka }
195 1.1 ahoka
196 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
197 1.1 ahoka if (vc->vno != CHFS_ROOTINO)
198 1.1 ahoka chfs_vnode_cache_set_state(chmp, vc, VNO_STATE_UNCHECKED);
199 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
200 1.1 ahoka dbg("END\n");
201 1.1 ahoka }
202 1.1 ahoka
203 1.1 ahoka /**
204 1.1 ahoka * chfs_build_filesystem - build in-memory representation of filesystem
205 1.1 ahoka * @chmp: super block information
206 1.1 ahoka *
207 1.1 ahoka * Step 1:
208 1.1 ahoka * This function scans through the eraseblocks mapped in EBH.
209 1.1 ahoka * During scan builds up the map of vnodes and directory entries and puts them
210 1.1 ahoka * into the vnode_cache.
211 1.1 ahoka * Step 2:
212 1.1 ahoka * Scans the directory tree and set the nlink in the vnode caches.
213 1.1 ahoka * Step 3:
214 1.1 ahoka * Scans vnode caches with nlink = 0
215 1.1 ahoka */
216 1.1 ahoka int
217 1.1 ahoka chfs_build_filesystem(struct chfs_mount *chmp)
218 1.1 ahoka {
219 1.1 ahoka int i,err = 0;
220 1.1 ahoka struct chfs_vnode_cache *vc;
221 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
222 1.1 ahoka // struct chfs_dirent *unlinked = NULL;
223 1.1 ahoka struct chfs_node_ref **nref;
224 1.1 ahoka struct chfs_dirent_list unlinked;
225 1.1 ahoka struct chfs_vnode_cache *notregvc;
226 1.1 ahoka
227 1.1 ahoka TAILQ_INIT(&unlinked);
228 1.1 ahoka
229 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
230 1.1 ahoka
231 1.1 ahoka /**
232 1.1 ahoka * Step 1
233 1.1 ahoka */
234 1.1 ahoka chmp->chm_flags |= CHFS_MP_FLAG_SCANNING;
235 1.1 ahoka for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
236 1.1 ahoka //dbg("processing block: %d\n", i);
237 1.1 ahoka chmp->chm_blocks[i].lnr = i;
238 1.1 ahoka chmp->chm_blocks[i].free_size = chmp->chm_ebh->eb_size;
239 1.1 ahoka //If the LEB is add to free list skip it.
240 1.1 ahoka if (chmp->chm_ebh->lmap[i] < 0) {
241 1.1 ahoka //dbg("block %d is unmapped\n", i);
242 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_free_queue,
243 1.1 ahoka &chmp->chm_blocks[i], queue);
244 1.1 ahoka chmp->chm_nr_free_blocks++;
245 1.1 ahoka continue;
246 1.1 ahoka }
247 1.1 ahoka
248 1.1 ahoka err = chfs_scan_eraseblock(chmp, &chmp->chm_blocks[i]);
249 1.1 ahoka switch (err) {
250 1.1 ahoka case CHFS_BLK_STATE_FREE:
251 1.1 ahoka chmp->chm_nr_free_blocks++;
252 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_free_queue,
253 1.1 ahoka &chmp->chm_blocks[i], queue);
254 1.1 ahoka break;
255 1.1 ahoka case CHFS_BLK_STATE_CLEAN:
256 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_clean_queue,
257 1.1 ahoka &chmp->chm_blocks[i], queue);
258 1.1 ahoka break;
259 1.1 ahoka case CHFS_BLK_STATE_PARTDIRTY:
260 1.1 ahoka //dbg("free size: %d\n", chmp->chm_blocks[i].free_size);
261 1.1 ahoka if (chmp->chm_blocks[i].free_size > chmp->chm_wbuf_pagesize &&
262 1.1 ahoka (!chmp->chm_nextblock ||
263 1.1 ahoka chmp->chm_blocks[i].free_size >
264 1.1 ahoka chmp->chm_nextblock->free_size)) {
265 1.1 ahoka /* convert the old nextblock's free size to
266 1.1 ahoka * dirty and put it on a list */
267 1.1 ahoka if (chmp->chm_nextblock) {
268 1.1 ahoka err = chfs_close_eraseblock(chmp,
269 1.1 ahoka chmp->chm_nextblock);
270 1.1 ahoka if (err)
271 1.1 ahoka return err;
272 1.1 ahoka }
273 1.1 ahoka chmp->chm_nextblock = &chmp->chm_blocks[i];
274 1.1 ahoka } else {
275 1.1 ahoka /* convert the scanned block's free size to
276 1.1 ahoka * dirty and put it on a list */
277 1.1 ahoka err = chfs_close_eraseblock(chmp,
278 1.1 ahoka &chmp->chm_blocks[i]);
279 1.1 ahoka if (err)
280 1.1 ahoka return err;
281 1.1 ahoka }
282 1.1 ahoka break;
283 1.1 ahoka case CHFS_BLK_STATE_ALLDIRTY:
284 1.1 ahoka /*
285 1.1 ahoka * The block has a valid EBH header, but it doesn't
286 1.1 ahoka * contain any valid data.
287 1.1 ahoka */
288 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erase_pending_queue,
289 1.1 ahoka &chmp->chm_blocks[i], queue);
290 1.1 ahoka chmp->chm_nr_erasable_blocks++;
291 1.1 ahoka break;
292 1.1 ahoka default:
293 1.1 ahoka /* It was an error, unknown state */
294 1.1 ahoka break;
295 1.1 ahoka }
296 1.1 ahoka
297 1.1 ahoka }
298 1.1 ahoka chmp->chm_flags &= ~CHFS_MP_FLAG_SCANNING;
299 1.1 ahoka
300 1.1 ahoka
301 1.1 ahoka //TODO need bad block check (and bad block handling in EBH too!!)
302 1.1 ahoka /* Now EBH only checks block is bad during its scan operation.
303 1.1 ahoka * Need check at erase + write + read...
304 1.1 ahoka */
305 1.1 ahoka
306 1.1 ahoka /**
307 1.1 ahoka * Step 2
308 1.1 ahoka */
309 1.1 ahoka chmp->chm_flags |= CHFS_MP_FLAG_BUILDING;
310 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
311 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
312 1.1 ahoka while (vc) {
313 1.1 ahoka dbg("vc->vno: %llu\n", vc->vno);
314 1.1 ahoka if (!TAILQ_EMPTY(&vc->scan_dirents))
315 1.1 ahoka chfs_build_set_vnodecache_nlink(chmp, vc);
316 1.1 ahoka vc = vc->next;
317 1.1 ahoka }
318 1.1 ahoka }
319 1.1 ahoka
320 1.1 ahoka /**
321 1.1 ahoka * Step 3
322 1.1 ahoka * Scan for vnodes with 0 nlink.
323 1.1 ahoka */
324 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
325 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
326 1.1 ahoka while (vc) {
327 1.1 ahoka if (vc->nlink) {
328 1.1 ahoka vc = vc->next;
329 1.1 ahoka continue;
330 1.1 ahoka }
331 1.1 ahoka
332 1.1 ahoka //dbg("remove unlinked start i: %d\n", i);
333 1.1 ahoka chfs_build_remove_unlinked_vnode(chmp,
334 1.1 ahoka vc, &unlinked);
335 1.1 ahoka //dbg("remove unlinked end\n");
336 1.1 ahoka vc = vc->next;
337 1.1 ahoka }
338 1.1 ahoka }
339 1.1 ahoka /* Remove the newly unlinked vnodes. They are on the unlinked list */
340 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &unlinked, fds, tmpfd) {
341 1.1 ahoka // while (unlinked) {
342 1.1 ahoka // fd = unlinked;
343 1.1 ahoka // unlinked = fd->next;
344 1.1 ahoka TAILQ_REMOVE(&unlinked, fd, fds);
345 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
346 1.1 ahoka vc = chfs_vnode_cache_get(chmp, fd->vno);
347 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
348 1.1 ahoka if (vc) {
349 1.1 ahoka chfs_build_remove_unlinked_vnode(chmp,
350 1.1 ahoka vc, &unlinked);
351 1.1 ahoka }
352 1.1 ahoka chfs_free_dirent(fd);
353 1.1 ahoka }
354 1.1 ahoka
355 1.1 ahoka chmp->chm_flags &= ~CHFS_MP_FLAG_BUILDING;
356 1.1 ahoka
357 1.1 ahoka /* Free all dirents */
358 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
359 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
360 1.1 ahoka while (vc) {
361 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &vc->scan_dirents, fds, tmpfd) {
362 1.1 ahoka // while (vc->scan_dirents) {
363 1.1 ahoka // fd = vc->scan_dirents;
364 1.1 ahoka // vc->scan_dirents = fd->next;
365 1.1 ahoka TAILQ_REMOVE(&vc->scan_dirents, fd, fds);
366 1.1 ahoka if (fd->vno == 0) {
367 1.1 ahoka //for (nref = &vc->dirents;
368 1.1 ahoka // *nref != fd->nref;
369 1.1 ahoka // nref = &((*nref)->next));
370 1.1 ahoka
371 1.1 ahoka nref = &fd->nref;
372 1.1 ahoka *nref = fd->nref->nref_next;
373 1.1 ahoka //fd->nref->nref_next = NULL;
374 1.1 ahoka } else if (fd->type == VDIR) {
375 1.1 ahoka //set state every non-VREG file's vc
376 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
377 1.1 ahoka notregvc =
378 1.1 ahoka chfs_vnode_cache_get(chmp,
379 1.1 ahoka fd->vno);
380 1.1 ahoka chfs_vnode_cache_set_state(chmp,
381 1.1 ahoka notregvc, VNO_STATE_PRESENT);
382 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
383 1.1 ahoka }
384 1.1 ahoka chfs_free_dirent(fd);
385 1.1 ahoka }
386 1.1 ahoka // vc->scan_dirents = NULL;
387 1.1 ahoka KASSERT(TAILQ_EMPTY(&vc->scan_dirents));
388 1.1 ahoka vc = vc->next;
389 1.1 ahoka }
390 1.1 ahoka }
391 1.1 ahoka
392 1.1 ahoka //Set up chmp->chm_wbuf_ofs for the first write
393 1.1 ahoka if (chmp->chm_nextblock) {
394 1.1 ahoka dbg("free_size: %d\n", chmp->chm_nextblock->free_size);
395 1.1 ahoka chmp->chm_wbuf_ofs = chmp->chm_ebh->eb_size -
396 1.1 ahoka chmp->chm_nextblock->free_size;
397 1.1 ahoka } else {
398 1.1 ahoka chmp->chm_wbuf_ofs = 0xffffffff;
399 1.1 ahoka }
400 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
401 1.1 ahoka
402 1.1 ahoka return 0;
403 1.1 ahoka }
404 1.1 ahoka
405