chfs_build.c revision 1.3 1 1.3 ttoth /* $NetBSD: chfs_build.c,v 1.3 2012/04/12 15:31:01 ttoth 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.3 ttoth if (fd->type == CHT_DIR) {
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.2 agc fd->name, (unsigned long long)fd->vno,
95 1.2 agc (unsigned long long)vc->vno);
96 1.1 ahoka } else {
97 1.1 ahoka //dbg("child_vc->pvno =
98 1.1 ahoka // vc->vno; pvno = %d\n", child_vc->pvno);
99 1.1 ahoka child_vc->pvno = vc->vno;
100 1.1 ahoka }
101 1.1 ahoka }
102 1.1 ahoka child_vc->nlink++;
103 1.1 ahoka //dbg("child_vc->nlink++;\n");
104 1.1 ahoka //child_vc->nlink++;
105 1.1 ahoka vc->nlink++;
106 1.1 ahoka }
107 1.1 ahoka }
108 1.1 ahoka
109 1.1 ahoka /**
110 1.1 ahoka * chfs_build_remove_unlinked vnode
111 1.1 ahoka */
112 1.1 ahoka /* static */
113 1.1 ahoka void
114 1.1 ahoka chfs_build_remove_unlinked_vnode(struct chfs_mount *chmp,
115 1.1 ahoka struct chfs_vnode_cache *vc,
116 1.1 ahoka // struct chfs_dirent **unlinked)
117 1.1 ahoka struct chfs_dirent_list *unlinked)
118 1.1 ahoka {
119 1.1 ahoka struct chfs_node_ref *nref;
120 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
121 1.1 ahoka
122 1.1 ahoka dbg("START\n");
123 1.2 agc dbg("vno: %llu\n", (unsigned long long)vc->vno);
124 1.1 ahoka
125 1.1 ahoka nref = vc->dnode;
126 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
127 1.1 ahoka // The vnode cache is at the end of the data node's chain
128 1.1 ahoka while (nref != (struct chfs_node_ref *)vc) {
129 1.1 ahoka struct chfs_node_ref *next = nref->nref_next;
130 1.1 ahoka dbg("mark dnode\n");
131 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
132 1.1 ahoka nref = next;
133 1.1 ahoka }
134 1.1 ahoka nref = vc->dirents;
135 1.1 ahoka // The vnode cache is at the end of the dirent node's chain
136 1.1 ahoka while (nref != (struct chfs_node_ref *)vc) {
137 1.1 ahoka struct chfs_node_ref *next = nref->nref_next;
138 1.1 ahoka dbg("mark dirent\n");
139 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
140 1.1 ahoka nref = next;
141 1.1 ahoka }
142 1.1 ahoka if (!TAILQ_EMPTY(&vc->scan_dirents)) {
143 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &vc->scan_dirents, fds, tmpfd) {
144 1.1 ahoka // while (vc->scan_dirents) {
145 1.1 ahoka struct chfs_vnode_cache *child_vc;
146 1.1 ahoka // fd = vc->scan_dirents;
147 1.1 ahoka dbg("dirent dump:\n");
148 1.2 agc dbg(" ->vno: %llu\n", (unsigned long long)fd->vno);
149 1.2 agc dbg(" ->version: %llu\n", (unsigned long long)fd->version);
150 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
151 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
152 1.1 ahoka dbg(" ->name: %s\n", fd->name);
153 1.1 ahoka dbg(" ->type: %d\n", fd->type);
154 1.1 ahoka // vc->scan_dirents = fd->next;
155 1.1 ahoka TAILQ_REMOVE(&vc->scan_dirents, fd, fds);
156 1.1 ahoka
157 1.1 ahoka if (!fd->vno) {
158 1.1 ahoka chfs_free_dirent(fd);
159 1.1 ahoka continue;
160 1.1 ahoka }
161 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
162 1.1 ahoka child_vc = chfs_vnode_cache_get(chmp, fd->vno);
163 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
164 1.1 ahoka if (!child_vc) {
165 1.1 ahoka chfs_free_dirent(fd);
166 1.1 ahoka continue;
167 1.1 ahoka }
168 1.1 ahoka /**
169 1.1 ahoka * Decrease nlink in child. If it is 0, add to unlinked
170 1.1 ahoka * dirents or just free it otherwise.
171 1.1 ahoka */
172 1.1 ahoka child_vc->nlink--;
173 1.1 ahoka
174 1.1 ahoka if (!child_vc->nlink) {
175 1.1 ahoka //dbg("nlink is 0\n");
176 1.1 ahoka // fd->next = *unlinked;
177 1.1 ahoka // *unlinked = fd;
178 1.1 ahoka // XXX HEAD or TAIL?
179 1.1 ahoka // original code did HEAD, but we could add
180 1.1 ahoka // it to the TAIL easily with TAILQ.
181 1.1 ahoka TAILQ_INSERT_TAIL(unlinked, fd, fds);
182 1.1 ahoka } else {
183 1.1 ahoka chfs_free_dirent(fd);
184 1.1 ahoka }
185 1.1 ahoka }
186 1.1 ahoka } else {
187 1.1 ahoka dbg("there are no scan dirents\n");
188 1.1 ahoka }
189 1.1 ahoka
190 1.1 ahoka nref = vc->v;
191 1.1 ahoka while ((struct chfs_vnode_cache *)nref != vc) {
192 1.1 ahoka if (!CHFS_REF_OBSOLETE(nref))
193 1.1 ahoka chfs_mark_node_obsolete(chmp, nref);
194 1.1 ahoka nref = nref->nref_next;
195 1.1 ahoka }
196 1.1 ahoka
197 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
198 1.1 ahoka if (vc->vno != CHFS_ROOTINO)
199 1.1 ahoka chfs_vnode_cache_set_state(chmp, vc, VNO_STATE_UNCHECKED);
200 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
201 1.1 ahoka dbg("END\n");
202 1.1 ahoka }
203 1.1 ahoka
204 1.1 ahoka /**
205 1.1 ahoka * chfs_build_filesystem - build in-memory representation of filesystem
206 1.1 ahoka * @chmp: super block information
207 1.1 ahoka *
208 1.1 ahoka * Step 1:
209 1.1 ahoka * This function scans through the eraseblocks mapped in EBH.
210 1.1 ahoka * During scan builds up the map of vnodes and directory entries and puts them
211 1.1 ahoka * into the vnode_cache.
212 1.1 ahoka * Step 2:
213 1.1 ahoka * Scans the directory tree and set the nlink in the vnode caches.
214 1.1 ahoka * Step 3:
215 1.1 ahoka * Scans vnode caches with nlink = 0
216 1.1 ahoka */
217 1.1 ahoka int
218 1.1 ahoka chfs_build_filesystem(struct chfs_mount *chmp)
219 1.1 ahoka {
220 1.1 ahoka int i,err = 0;
221 1.1 ahoka struct chfs_vnode_cache *vc;
222 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
223 1.1 ahoka // struct chfs_dirent *unlinked = NULL;
224 1.1 ahoka struct chfs_node_ref **nref;
225 1.1 ahoka struct chfs_dirent_list unlinked;
226 1.1 ahoka struct chfs_vnode_cache *notregvc;
227 1.1 ahoka
228 1.1 ahoka TAILQ_INIT(&unlinked);
229 1.1 ahoka
230 1.1 ahoka mutex_enter(&chmp->chm_lock_mountfields);
231 1.1 ahoka
232 1.1 ahoka /**
233 1.1 ahoka * Step 1
234 1.1 ahoka */
235 1.1 ahoka chmp->chm_flags |= CHFS_MP_FLAG_SCANNING;
236 1.1 ahoka for (i = 0; i < chmp->chm_ebh->peb_nr; i++) {
237 1.1 ahoka //dbg("processing block: %d\n", i);
238 1.1 ahoka chmp->chm_blocks[i].lnr = i;
239 1.1 ahoka chmp->chm_blocks[i].free_size = chmp->chm_ebh->eb_size;
240 1.1 ahoka //If the LEB is add to free list skip it.
241 1.1 ahoka if (chmp->chm_ebh->lmap[i] < 0) {
242 1.1 ahoka //dbg("block %d is unmapped\n", i);
243 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_free_queue,
244 1.1 ahoka &chmp->chm_blocks[i], queue);
245 1.1 ahoka chmp->chm_nr_free_blocks++;
246 1.1 ahoka continue;
247 1.1 ahoka }
248 1.1 ahoka
249 1.1 ahoka err = chfs_scan_eraseblock(chmp, &chmp->chm_blocks[i]);
250 1.1 ahoka switch (err) {
251 1.1 ahoka case CHFS_BLK_STATE_FREE:
252 1.1 ahoka chmp->chm_nr_free_blocks++;
253 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_free_queue,
254 1.1 ahoka &chmp->chm_blocks[i], queue);
255 1.1 ahoka break;
256 1.1 ahoka case CHFS_BLK_STATE_CLEAN:
257 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_clean_queue,
258 1.1 ahoka &chmp->chm_blocks[i], queue);
259 1.1 ahoka break;
260 1.1 ahoka case CHFS_BLK_STATE_PARTDIRTY:
261 1.1 ahoka //dbg("free size: %d\n", chmp->chm_blocks[i].free_size);
262 1.1 ahoka if (chmp->chm_blocks[i].free_size > chmp->chm_wbuf_pagesize &&
263 1.1 ahoka (!chmp->chm_nextblock ||
264 1.1 ahoka chmp->chm_blocks[i].free_size >
265 1.1 ahoka chmp->chm_nextblock->free_size)) {
266 1.1 ahoka /* convert the old nextblock's free size to
267 1.1 ahoka * dirty and put it on a list */
268 1.1 ahoka if (chmp->chm_nextblock) {
269 1.1 ahoka err = chfs_close_eraseblock(chmp,
270 1.1 ahoka chmp->chm_nextblock);
271 1.1 ahoka if (err)
272 1.1 ahoka return err;
273 1.1 ahoka }
274 1.1 ahoka chmp->chm_nextblock = &chmp->chm_blocks[i];
275 1.1 ahoka } else {
276 1.1 ahoka /* convert the scanned block's free size to
277 1.1 ahoka * dirty and put it on a list */
278 1.1 ahoka err = chfs_close_eraseblock(chmp,
279 1.1 ahoka &chmp->chm_blocks[i]);
280 1.1 ahoka if (err)
281 1.1 ahoka return err;
282 1.1 ahoka }
283 1.1 ahoka break;
284 1.1 ahoka case CHFS_BLK_STATE_ALLDIRTY:
285 1.1 ahoka /*
286 1.1 ahoka * The block has a valid EBH header, but it doesn't
287 1.1 ahoka * contain any valid data.
288 1.1 ahoka */
289 1.1 ahoka TAILQ_INSERT_TAIL(&chmp->chm_erase_pending_queue,
290 1.1 ahoka &chmp->chm_blocks[i], queue);
291 1.1 ahoka chmp->chm_nr_erasable_blocks++;
292 1.1 ahoka break;
293 1.1 ahoka default:
294 1.1 ahoka /* It was an error, unknown state */
295 1.1 ahoka break;
296 1.1 ahoka }
297 1.1 ahoka
298 1.1 ahoka }
299 1.1 ahoka chmp->chm_flags &= ~CHFS_MP_FLAG_SCANNING;
300 1.1 ahoka
301 1.1 ahoka
302 1.1 ahoka //TODO need bad block check (and bad block handling in EBH too!!)
303 1.1 ahoka /* Now EBH only checks block is bad during its scan operation.
304 1.1 ahoka * Need check at erase + write + read...
305 1.1 ahoka */
306 1.1 ahoka
307 1.1 ahoka /**
308 1.1 ahoka * Step 2
309 1.1 ahoka */
310 1.1 ahoka chmp->chm_flags |= CHFS_MP_FLAG_BUILDING;
311 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
312 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
313 1.1 ahoka while (vc) {
314 1.2 agc dbg("vc->vno: %llu\n", (unsigned long long)vc->vno);
315 1.1 ahoka if (!TAILQ_EMPTY(&vc->scan_dirents))
316 1.1 ahoka chfs_build_set_vnodecache_nlink(chmp, vc);
317 1.1 ahoka vc = vc->next;
318 1.1 ahoka }
319 1.1 ahoka }
320 1.1 ahoka
321 1.1 ahoka /**
322 1.1 ahoka * Step 3
323 1.1 ahoka * Scan for vnodes with 0 nlink.
324 1.1 ahoka */
325 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
326 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
327 1.1 ahoka while (vc) {
328 1.1 ahoka if (vc->nlink) {
329 1.1 ahoka vc = vc->next;
330 1.1 ahoka continue;
331 1.1 ahoka }
332 1.1 ahoka
333 1.1 ahoka //dbg("remove unlinked start i: %d\n", i);
334 1.1 ahoka chfs_build_remove_unlinked_vnode(chmp,
335 1.1 ahoka vc, &unlinked);
336 1.1 ahoka //dbg("remove unlinked end\n");
337 1.1 ahoka vc = vc->next;
338 1.1 ahoka }
339 1.1 ahoka }
340 1.1 ahoka /* Remove the newly unlinked vnodes. They are on the unlinked list */
341 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &unlinked, fds, tmpfd) {
342 1.1 ahoka // while (unlinked) {
343 1.1 ahoka // fd = unlinked;
344 1.1 ahoka // unlinked = fd->next;
345 1.1 ahoka TAILQ_REMOVE(&unlinked, fd, fds);
346 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
347 1.1 ahoka vc = chfs_vnode_cache_get(chmp, fd->vno);
348 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
349 1.1 ahoka if (vc) {
350 1.1 ahoka chfs_build_remove_unlinked_vnode(chmp,
351 1.1 ahoka vc, &unlinked);
352 1.1 ahoka }
353 1.1 ahoka chfs_free_dirent(fd);
354 1.1 ahoka }
355 1.1 ahoka
356 1.1 ahoka chmp->chm_flags &= ~CHFS_MP_FLAG_BUILDING;
357 1.1 ahoka
358 1.1 ahoka /* Free all dirents */
359 1.1 ahoka for (i = 0; i < VNODECACHE_SIZE; i++) {
360 1.1 ahoka vc = chmp->chm_vnocache_hash[i];
361 1.1 ahoka while (vc) {
362 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &vc->scan_dirents, fds, tmpfd) {
363 1.1 ahoka // while (vc->scan_dirents) {
364 1.1 ahoka // fd = vc->scan_dirents;
365 1.1 ahoka // vc->scan_dirents = fd->next;
366 1.1 ahoka TAILQ_REMOVE(&vc->scan_dirents, fd, fds);
367 1.1 ahoka if (fd->vno == 0) {
368 1.1 ahoka //for (nref = &vc->dirents;
369 1.1 ahoka // *nref != fd->nref;
370 1.1 ahoka // nref = &((*nref)->next));
371 1.1 ahoka
372 1.1 ahoka nref = &fd->nref;
373 1.1 ahoka *nref = fd->nref->nref_next;
374 1.1 ahoka //fd->nref->nref_next = NULL;
375 1.3 ttoth } else if (fd->type == CHT_DIR) {
376 1.1 ahoka //set state every non-VREG file's vc
377 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
378 1.1 ahoka notregvc =
379 1.1 ahoka chfs_vnode_cache_get(chmp,
380 1.1 ahoka fd->vno);
381 1.1 ahoka chfs_vnode_cache_set_state(chmp,
382 1.1 ahoka notregvc, VNO_STATE_PRESENT);
383 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
384 1.1 ahoka }
385 1.1 ahoka chfs_free_dirent(fd);
386 1.1 ahoka }
387 1.1 ahoka // vc->scan_dirents = NULL;
388 1.1 ahoka KASSERT(TAILQ_EMPTY(&vc->scan_dirents));
389 1.1 ahoka vc = vc->next;
390 1.1 ahoka }
391 1.1 ahoka }
392 1.1 ahoka
393 1.1 ahoka //Set up chmp->chm_wbuf_ofs for the first write
394 1.1 ahoka if (chmp->chm_nextblock) {
395 1.1 ahoka dbg("free_size: %d\n", chmp->chm_nextblock->free_size);
396 1.1 ahoka chmp->chm_wbuf_ofs = chmp->chm_ebh->eb_size -
397 1.1 ahoka chmp->chm_nextblock->free_size;
398 1.1 ahoka } else {
399 1.1 ahoka chmp->chm_wbuf_ofs = 0xffffffff;
400 1.1 ahoka }
401 1.1 ahoka mutex_exit(&chmp->chm_lock_mountfields);
402 1.1 ahoka
403 1.1 ahoka return 0;
404 1.1 ahoka }
405 1.1 ahoka
406