chfs_scan.c revision 1.1 1 1.1 ahoka /* $NetBSD: chfs_scan.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 * Copyright (c) 2010 David Tengeri <dtengeri (at) inf.u-szeged.hu>
7 1.1 ahoka * All rights reserved.
8 1.1 ahoka *
9 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
10 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
11 1.1 ahoka *
12 1.1 ahoka * Redistribution and use in source and binary forms, with or without
13 1.1 ahoka * modification, are permitted provided that the following conditions
14 1.1 ahoka * are met:
15 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
16 1.1 ahoka * notice, this list of conditions and the following disclaimer.
17 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
19 1.1 ahoka * documentation and/or other materials provided with the distribution.
20 1.1 ahoka *
21 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 ahoka * SUCH DAMAGE.
32 1.1 ahoka */
33 1.1 ahoka
34 1.1 ahoka /*
35 1.1 ahoka * chfs_scan.c
36 1.1 ahoka *
37 1.1 ahoka * Created on: 2009.11.05.
38 1.1 ahoka * Author: dtengeri
39 1.1 ahoka */
40 1.1 ahoka
41 1.1 ahoka #include "chfs.h"
42 1.1 ahoka
43 1.1 ahoka /**
44 1.1 ahoka * chfs_scan_make_vnode_cache - makes a new vnode cache during scan
45 1.1 ahoka * @chmp: CHFS main descriptor structure
46 1.1 ahoka * @vno: vnode identifier
47 1.1 ahoka * This function returns a vnode cache belonging to @vno.
48 1.1 ahoka */
49 1.1 ahoka struct chfs_vnode_cache *
50 1.1 ahoka chfs_scan_make_vnode_cache(struct chfs_mount *chmp, ino_t vno)
51 1.1 ahoka {
52 1.1 ahoka struct chfs_vnode_cache *vc;
53 1.1 ahoka
54 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_vnocache));
55 1.1 ahoka
56 1.1 ahoka vc = chfs_vnode_cache_get(chmp, vno);
57 1.1 ahoka if (vc) {
58 1.1 ahoka return vc;
59 1.1 ahoka }
60 1.1 ahoka
61 1.1 ahoka if (vno > chmp->chm_max_vno) {
62 1.1 ahoka chmp->chm_max_vno = vno;
63 1.1 ahoka }
64 1.1 ahoka
65 1.1 ahoka vc = chfs_vnode_cache_alloc(vno);
66 1.1 ahoka
67 1.1 ahoka //mutex_enter(&chmp->chm_lock_vnocache);
68 1.1 ahoka
69 1.1 ahoka chfs_vnode_cache_add(chmp, vc);
70 1.1 ahoka
71 1.1 ahoka //mutex_exit(&chmp->chm_lock_vnocache);
72 1.1 ahoka
73 1.1 ahoka if (vno == CHFS_ROOTINO) {
74 1.1 ahoka vc->nlink = 2;
75 1.1 ahoka vc->pvno = CHFS_ROOTINO;
76 1.1 ahoka chfs_vnode_cache_set_state(chmp,
77 1.1 ahoka vc, VNO_STATE_CHECKEDABSENT);
78 1.1 ahoka }
79 1.1 ahoka
80 1.1 ahoka return vc;
81 1.1 ahoka }
82 1.1 ahoka
83 1.1 ahoka /**
84 1.1 ahoka * chfs_scan_check_node_hdr - checks node magic and crc
85 1.1 ahoka * @nhdr: node header to check
86 1.1 ahoka * Returns 0 if everything is OK, error code otherwise.
87 1.1 ahoka */
88 1.1 ahoka int
89 1.1 ahoka chfs_scan_check_node_hdr(struct chfs_flash_node_hdr *nhdr)
90 1.1 ahoka {
91 1.1 ahoka uint16_t magic;
92 1.1 ahoka uint32_t crc, hdr_crc;
93 1.1 ahoka
94 1.1 ahoka magic = le16toh(nhdr->magic);
95 1.1 ahoka
96 1.1 ahoka if (magic != CHFS_FS_MAGIC_BITMASK) {
97 1.1 ahoka dbg("bad magic\n");
98 1.1 ahoka return CHFS_NODE_BADMAGIC;
99 1.1 ahoka }
100 1.1 ahoka
101 1.1 ahoka hdr_crc = le32toh(nhdr->hdr_crc);
102 1.1 ahoka crc = crc32(0, (uint8_t *)nhdr, CHFS_NODE_HDR_SIZE - 4);
103 1.1 ahoka
104 1.1 ahoka if (crc != hdr_crc) {
105 1.1 ahoka dbg("bad crc\n");
106 1.1 ahoka return CHFS_NODE_BADCRC;
107 1.1 ahoka }
108 1.1 ahoka
109 1.1 ahoka return CHFS_NODE_OK;
110 1.1 ahoka }
111 1.1 ahoka
112 1.1 ahoka /**
113 1.1 ahoka * chfs_scan_check_vnode - check vnode crc and add to vnode cache
114 1.1 ahoka * @chmp: CHFS main descriptor structure
115 1.1 ahoka * @cheb: eraseblock informations
116 1.1 ahoka * @buf: vnode to check
117 1.1 ahoka * @ofs: offset in eraseblock where vnode starts
118 1.1 ahoka */
119 1.1 ahoka int
120 1.1 ahoka chfs_scan_check_vnode(struct chfs_mount *chmp,
121 1.1 ahoka struct chfs_eraseblock *cheb, void *buf, off_t ofs)
122 1.1 ahoka {
123 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
124 1.1 ahoka struct chfs_vnode_cache *vc;
125 1.1 ahoka struct chfs_flash_vnode *vnode = buf;
126 1.1 ahoka struct chfs_node_ref *nref;
127 1.1 ahoka int err;
128 1.1 ahoka uint32_t crc;
129 1.1 ahoka ino_t vno;
130 1.1 ahoka
131 1.1 ahoka crc = crc32(0, (uint8_t *)vnode,
132 1.1 ahoka sizeof(struct chfs_flash_vnode) - 4);
133 1.1 ahoka
134 1.1 ahoka if (crc != le32toh(vnode->node_crc)) {
135 1.1 ahoka err = chfs_update_eb_dirty(chmp,
136 1.1 ahoka cheb, le32toh(vnode->length));
137 1.1 ahoka if (err) {
138 1.1 ahoka return err;
139 1.1 ahoka }
140 1.1 ahoka
141 1.1 ahoka return CHFS_NODE_BADCRC;
142 1.1 ahoka }
143 1.1 ahoka
144 1.1 ahoka vno = le64toh(vnode->vno);
145 1.1 ahoka
146 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
147 1.1 ahoka vc = chfs_vnode_cache_get(chmp, vno);
148 1.1 ahoka if (!vc) {
149 1.1 ahoka vc = chfs_scan_make_vnode_cache(chmp, vno);
150 1.1 ahoka if (!vc) {
151 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
152 1.1 ahoka return ENOMEM;
153 1.1 ahoka }
154 1.1 ahoka }
155 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
156 1.1 ahoka
157 1.1 ahoka nref = chfs_alloc_node_ref(cheb);
158 1.1 ahoka
159 1.1 ahoka nref->nref_offset = ofs;
160 1.1 ahoka
161 1.1 ahoka KASSERT(nref->nref_lnr == cheb->lnr);
162 1.1 ahoka
163 1.1 ahoka /* Check version of vnode. */
164 1.1 ahoka if ((struct chfs_vnode_cache *)vc->v != vc) {
165 1.1 ahoka if (le64toh(vnode->version) > *vc->vno_version) {
166 1.1 ahoka //err = chfs_update_eb_dirty(chmp, &chmp->chm_blocks[vc->v->lnr],
167 1.1 ahoka // sizeof(struct chfs_flash_vnode));
168 1.1 ahoka *vc->vno_version = le64toh(vnode->version);
169 1.1 ahoka chfs_add_vnode_ref_to_vc(chmp, vc, nref);
170 1.1 ahoka } else {
171 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb,
172 1.1 ahoka sizeof(struct chfs_flash_vnode));
173 1.1 ahoka return CHFS_NODE_OK;
174 1.1 ahoka }
175 1.1 ahoka } else {
176 1.1 ahoka vc->vno_version = kmem_alloc(sizeof(uint64_t), KM_SLEEP);
177 1.1 ahoka if (!vc->vno_version)
178 1.1 ahoka return ENOMEM;
179 1.1 ahoka *vc->vno_version = le64toh(vnode->version);
180 1.1 ahoka chfs_add_vnode_ref_to_vc(chmp, vc, nref);
181 1.1 ahoka }
182 1.1 ahoka
183 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
184 1.1 ahoka //dbg("B:lnr: %d |free_size: %d node's size: %d\n", cheb->lnr, cheb->free_size, le32toh(vnode->length));
185 1.1 ahoka chfs_change_size_free(chmp, cheb, -le32toh(vnode->length));
186 1.1 ahoka chfs_change_size_used(chmp, cheb, le32toh(vnode->length));
187 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
188 1.1 ahoka
189 1.1 ahoka KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size);
190 1.1 ahoka
191 1.1 ahoka KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size);
192 1.1 ahoka
193 1.1 ahoka //dbg(" A: free_size: %d\n", cheb->free_size);
194 1.1 ahoka
195 1.1 ahoka /*dbg("vnode dump:\n");
196 1.1 ahoka dbg(" ->magic: 0x%x\n", le16toh(vnode->magic));
197 1.1 ahoka dbg(" ->type: %d\n", le16toh(vnode->type));
198 1.1 ahoka dbg(" ->length: %d\n", le32toh(vnode->length));
199 1.1 ahoka dbg(" ->hdr_crc: 0x%x\n", le32toh(vnode->hdr_crc));
200 1.1 ahoka dbg(" ->vno: %d\n", le64toh(vnode->vno));
201 1.1 ahoka dbg(" ->version: %ld\n", le64toh(vnode->version));
202 1.1 ahoka dbg(" ->uid: %d\n", le16toh(vnode->uid));
203 1.1 ahoka dbg(" ->gid: %d\n", le16toh(vnode->gid));
204 1.1 ahoka dbg(" ->mode: %d\n", le32toh(vnode->mode));
205 1.1 ahoka dbg(" ->dn_size: %d\n", le32toh(vnode->dn_size));
206 1.1 ahoka dbg(" ->atime: %d\n", le32toh(vnode->atime));
207 1.1 ahoka dbg(" ->mtime: %d\n", le32toh(vnode->mtime));
208 1.1 ahoka dbg(" ->ctime: %d\n", le32toh(vnode->ctime));
209 1.1 ahoka dbg(" ->dsize: %d\n", le32toh(vnode->dsize));
210 1.1 ahoka dbg(" ->node_crc: 0x%x\n", le32toh(vnode->node_crc));*/
211 1.1 ahoka
212 1.1 ahoka return CHFS_NODE_OK;
213 1.1 ahoka }
214 1.1 ahoka
215 1.1 ahoka int
216 1.1 ahoka chfs_scan_mark_dirent_obsolete(struct chfs_mount *chmp,
217 1.1 ahoka struct chfs_vnode_cache *vc, struct chfs_dirent *fd)
218 1.1 ahoka {
219 1.1 ahoka //int size;
220 1.1 ahoka struct chfs_eraseblock *cheb;
221 1.1 ahoka struct chfs_node_ref *prev, *nref;
222 1.1 ahoka
223 1.1 ahoka nref = fd->nref;
224 1.1 ahoka cheb = &chmp->chm_blocks[fd->nref->nref_lnr];
225 1.1 ahoka
226 1.1 ahoka /* Remove dirent's node ref from vnode cache */
227 1.1 ahoka prev = vc->dirents;
228 1.1 ahoka if (prev && prev == nref) {
229 1.1 ahoka vc->dirents = prev->nref_next;
230 1.1 ahoka } else if (prev && prev != (void *)vc) {
231 1.1 ahoka while (prev->nref_next && prev->nref_next !=
232 1.1 ahoka (void *)vc && prev->nref_next != nref) {
233 1.1 ahoka prev = prev->nref_next;
234 1.1 ahoka }
235 1.1 ahoka
236 1.1 ahoka if (prev->nref_next == nref) {
237 1.1 ahoka prev->nref_next = nref->nref_next;
238 1.1 ahoka }
239 1.1 ahoka }
240 1.1 ahoka /*dbg("XXX - start\n");
241 1.1 ahoka //nref = vc->dirents;
242 1.1 ahoka struct chfs_dirent *tmp;
243 1.1 ahoka tmp = vc->scan_dirents;
244 1.1 ahoka while (tmp) {
245 1.1 ahoka dbg(" ->tmp->name: %s\n", tmp->name);
246 1.1 ahoka dbg(" ->tmp->version: %ld\n", tmp->version);
247 1.1 ahoka dbg(" ->tmp->vno: %d\n", tmp->vno);
248 1.1 ahoka tmp = tmp->next;
249 1.1 ahoka }
250 1.1 ahoka dbg("XXX - end\n");*/
251 1.1 ahoka //size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + fd->nsize);
252 1.1 ahoka
253 1.1 ahoka KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size +
254 1.1 ahoka cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size);
255 1.1 ahoka
256 1.1 ahoka return 0;
257 1.1 ahoka }
258 1.1 ahoka
259 1.1 ahoka void
260 1.1 ahoka chfs_add_fd_to_list(struct chfs_mount *chmp,
261 1.1 ahoka struct chfs_dirent *new, struct chfs_vnode_cache *pvc)
262 1.1 ahoka {
263 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
264 1.1 ahoka int size;
265 1.1 ahoka struct chfs_eraseblock *cheb, *oldcheb;
266 1.1 ahoka // struct chfs_dirent **prev;
267 1.1 ahoka struct chfs_dirent *fd, *tmpfd;
268 1.1 ahoka
269 1.1 ahoka dbg("adding fd to list: %s\n", new->name);
270 1.1 ahoka
271 1.1 ahoka if ((new->version > pvc->highest_version))
272 1.1 ahoka pvc->highest_version = new->version;
273 1.1 ahoka
274 1.1 ahoka size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) +
275 1.1 ahoka new->nsize);
276 1.1 ahoka cheb = &chmp->chm_blocks[new->nref->nref_lnr];
277 1.1 ahoka
278 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
279 1.1 ahoka TAILQ_FOREACH_SAFE(fd, &pvc->scan_dirents, fds, tmpfd) {
280 1.1 ahoka if (fd->nhash > new->nhash) {
281 1.1 ahoka /* insert new before fd */
282 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
283 1.1 ahoka goto out;
284 1.1 ahoka } else if (fd->nhash == new->nhash &&
285 1.1 ahoka !strcmp(fd->name, new->name)) {
286 1.1 ahoka if (new->version > fd->version) {
287 1.1 ahoka // new->next = fd->next;
288 1.1 ahoka /* replace fd with new */
289 1.1 ahoka TAILQ_INSERT_BEFORE(fd, new, fds);
290 1.1 ahoka chfs_change_size_free(chmp, cheb, -size);
291 1.1 ahoka chfs_change_size_used(chmp, cheb, size);
292 1.1 ahoka
293 1.1 ahoka TAILQ_REMOVE(&pvc->scan_dirents, fd, fds);
294 1.1 ahoka if (fd->nref) {
295 1.1 ahoka size = CHFS_PAD(sizeof(struct chfs_flash_dirent_node) + fd->nsize);
296 1.1 ahoka chfs_scan_mark_dirent_obsolete(chmp, pvc, fd);
297 1.1 ahoka oldcheb = &chmp->chm_blocks[fd->nref->nref_lnr];
298 1.1 ahoka chfs_change_size_used(chmp, oldcheb, -size);
299 1.1 ahoka chfs_change_size_dirty(chmp, oldcheb, size);
300 1.1 ahoka }
301 1.1 ahoka chfs_free_dirent(fd);
302 1.1 ahoka // *prev = new;//XXX
303 1.1 ahoka } else {
304 1.1 ahoka chfs_scan_mark_dirent_obsolete(chmp, pvc, new);
305 1.1 ahoka chfs_change_size_free(chmp, cheb, -size);
306 1.1 ahoka chfs_change_size_dirty(chmp, cheb, size);
307 1.1 ahoka chfs_free_dirent(new);
308 1.1 ahoka }
309 1.1 ahoka /*dbg("START\n");
310 1.1 ahoka fd = pvc->scan_dirents;
311 1.1 ahoka while (fd) {
312 1.1 ahoka dbg("dirent dump:\n");
313 1.1 ahoka dbg(" ->vno: %d\n", fd->vno);
314 1.1 ahoka dbg(" ->version: %ld\n", fd->version);
315 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
316 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
317 1.1 ahoka dbg(" ->name: %s\n", fd->name);
318 1.1 ahoka dbg(" ->type: %d\n", fd->type);
319 1.1 ahoka fd = fd->next;
320 1.1 ahoka }
321 1.1 ahoka dbg("END\n");*/
322 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
323 1.1 ahoka return;
324 1.1 ahoka }
325 1.1 ahoka }
326 1.1 ahoka /* if we couldnt fit it elsewhere, lets add to the end */
327 1.1 ahoka TAILQ_INSERT_TAIL(&pvc->scan_dirents, new, fds);
328 1.1 ahoka
329 1.1 ahoka out:
330 1.1 ahoka //dbg("B:lnr: %d |free_size: %d size: %d\n", cheb->lnr, cheb->free_size, size);
331 1.1 ahoka chfs_change_size_free(chmp, cheb, -size);
332 1.1 ahoka chfs_change_size_used(chmp, cheb, size);
333 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
334 1.1 ahoka
335 1.1 ahoka KASSERT(cheb->used_size <= chmp->chm_ebh->eb_size);
336 1.1 ahoka //dbg(" A: free_size: %d\n", cheb->free_size);
337 1.1 ahoka
338 1.1 ahoka KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size + cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size);
339 1.1 ahoka
340 1.1 ahoka
341 1.1 ahoka // fd = pvc->scan_dirents;
342 1.1 ahoka /*dbg("START\n");
343 1.1 ahoka while (fd) {
344 1.1 ahoka dbg("dirent dump:\n");
345 1.1 ahoka dbg(" ->vno: %d\n", fd->vno);
346 1.1 ahoka dbg(" ->version: %ld\n", fd->version);
347 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
348 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
349 1.1 ahoka dbg(" ->name: %s\n", fd->name);
350 1.1 ahoka dbg(" ->type: %d\n", fd->type);
351 1.1 ahoka fd = fd->next;
352 1.1 ahoka }
353 1.1 ahoka dbg("END\n");*/
354 1.1 ahoka }
355 1.1 ahoka /**
356 1.1 ahoka * chfs_scan_check_dirent_node - check vnode crc and add to vnode cache
357 1.1 ahoka * @chmp: CHFS main descriptor structure
358 1.1 ahoka * @cheb: eraseblock informations
359 1.1 ahoka * @buf: directory entry to check
360 1.1 ahoka * @ofs: offset in eraseblock where dirent starts
361 1.1 ahoka */
362 1.1 ahoka int
363 1.1 ahoka chfs_scan_check_dirent_node(struct chfs_mount *chmp,
364 1.1 ahoka struct chfs_eraseblock *cheb, void *buf, off_t ofs)
365 1.1 ahoka {
366 1.1 ahoka int err, namelen;
367 1.1 ahoka uint32_t crc;
368 1.1 ahoka struct chfs_dirent *fd;
369 1.1 ahoka struct chfs_vnode_cache *vc;
370 1.1 ahoka struct chfs_flash_dirent_node *dirent = buf;
371 1.1 ahoka
372 1.1 ahoka //struct chfs_node_ref *tmp;
373 1.1 ahoka
374 1.1 ahoka crc = crc32(0, (uint8_t *)dirent, sizeof(*dirent) - 4);
375 1.1 ahoka if (crc != le32toh(dirent->node_crc)) {
376 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length));
377 1.1 ahoka if (err)
378 1.1 ahoka return err;
379 1.1 ahoka return CHFS_NODE_BADCRC;
380 1.1 ahoka }
381 1.1 ahoka namelen = dirent->nsize;
382 1.1 ahoka
383 1.1 ahoka fd = chfs_alloc_dirent(namelen + 1);
384 1.1 ahoka if (!fd)
385 1.1 ahoka return ENOMEM;
386 1.1 ahoka
387 1.1 ahoka fd->nref = chfs_alloc_node_ref(cheb);
388 1.1 ahoka if (!fd->nref)
389 1.1 ahoka return ENOMEM;
390 1.1 ahoka
391 1.1 ahoka KASSERT(fd->nref->nref_lnr == cheb->lnr);
392 1.1 ahoka
393 1.1 ahoka memcpy(&fd->name, dirent->name, namelen);
394 1.1 ahoka fd->nsize = namelen;
395 1.1 ahoka fd->name[namelen] = 0;
396 1.1 ahoka crc = crc32(0, fd->name, dirent->nsize);
397 1.1 ahoka if (crc != le32toh(dirent->name_crc)) {
398 1.1 ahoka chfs_err("Directory entry's name has bad crc: read: 0x%x, "
399 1.1 ahoka "calculated: 0x%x\n", le32toh(dirent->name_crc), crc);
400 1.1 ahoka chfs_free_dirent(fd);
401 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb, le32toh(dirent->length));
402 1.1 ahoka if (err)
403 1.1 ahoka return err;
404 1.1 ahoka return CHFS_NODE_BADNAMECRC;
405 1.1 ahoka }
406 1.1 ahoka
407 1.1 ahoka /* Check vnode_cache of parent node */
408 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
409 1.1 ahoka vc = chfs_scan_make_vnode_cache(chmp, le64toh(dirent->pvno));
410 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
411 1.1 ahoka if (!vc) {
412 1.1 ahoka chfs_free_dirent(fd);
413 1.1 ahoka return ENOMEM;
414 1.1 ahoka }
415 1.1 ahoka
416 1.1 ahoka fd->nref->nref_offset = ofs;
417 1.1 ahoka
418 1.1 ahoka dbg("add dirent to #%llu\n", vc->vno);
419 1.1 ahoka chfs_add_node_to_list(chmp, vc, fd->nref, &vc->dirents);
420 1.1 ahoka /*tmp = vc->dirents;
421 1.1 ahoka dbg("START|vno: %d dirents dump\n", vc->vno);
422 1.1 ahoka while (tmp) {
423 1.1 ahoka dbg(" ->nref->nref_lnr: %d\n", tmp->lnr);
424 1.1 ahoka dbg(" ->nref->nref_offset: %d\n", tmp->offset);
425 1.1 ahoka tmp = tmp->next;
426 1.1 ahoka }
427 1.1 ahoka dbg(" END|vno: %d dirents dump\n", vc->vno);*/
428 1.1 ahoka
429 1.1 ahoka // fd->next = NULL;
430 1.1 ahoka fd->vno = le64toh(dirent->vno);
431 1.1 ahoka fd->version = le64toh(dirent->version);
432 1.1 ahoka fd->nhash = hash32_buf(fd->name, namelen, HASH32_BUF_INIT);
433 1.1 ahoka fd->type = dirent->dtype;
434 1.1 ahoka
435 1.1 ahoka /*dbg("dirent dump:\n");
436 1.1 ahoka dbg(" ->vno: %d\n", fd->vno);
437 1.1 ahoka dbg(" ->version: %ld\n", fd->version);
438 1.1 ahoka dbg(" ->nhash: 0x%x\n", fd->nhash);
439 1.1 ahoka dbg(" ->nsize: %d\n", fd->nsize);
440 1.1 ahoka dbg(" ->name: %s\n", fd->name);
441 1.1 ahoka dbg(" ->type: %d\n", fd->type);*/
442 1.1 ahoka
443 1.1 ahoka chfs_add_fd_to_list(chmp, fd, vc);
444 1.1 ahoka
445 1.1 ahoka /*struct chfs_node_ref *tmp;
446 1.1 ahoka tmp = vc->dirents;
447 1.1 ahoka dbg("START|vno: %d dirents dump\n", vc->vno);
448 1.1 ahoka while (tmp) {
449 1.1 ahoka dbg(" ->nref->nref_lnr: %d\n", tmp->lnr);
450 1.1 ahoka dbg(" ->nref->nref_offset: %d\n", tmp->offset);
451 1.1 ahoka tmp = tmp->next;
452 1.1 ahoka }
453 1.1 ahoka dbg(" END|vno: %d dirents dump\n", vc->vno);*/
454 1.1 ahoka
455 1.1 ahoka /*dbg("dirent dump:\n");
456 1.1 ahoka dbg(" ->magic: 0x%x\n", le16toh(dirent->magic));
457 1.1 ahoka dbg(" ->type: %d\n", le16toh(dirent->type));
458 1.1 ahoka dbg(" ->length: %d\n", le32toh(dirent->length));
459 1.1 ahoka dbg(" ->hdr_crc: 0x%x\n", le32toh(dirent->hdr_crc));
460 1.1 ahoka dbg(" ->vno: %d\n", le64toh(dirent->vno));
461 1.1 ahoka dbg(" ->pvno: %d\n", le64toh(dirent->pvno));
462 1.1 ahoka dbg(" ->version: %ld\n", le64toh(dirent->version));
463 1.1 ahoka dbg(" ->mctime: %d\n", le32toh(dirent->mctime));
464 1.1 ahoka dbg(" ->nsize: %d\n", dirent->nsize);
465 1.1 ahoka dbg(" ->dtype: %d\n", dirent->dtype);
466 1.1 ahoka dbg(" ->name_crc: 0x%x\n", le32toh(dirent->name_crc));
467 1.1 ahoka dbg(" ->node_crc: 0x%x\n", le32toh(dirent->node_crc));
468 1.1 ahoka dbg(" ->name: %s\n", dirent->name);*/
469 1.1 ahoka
470 1.1 ahoka return CHFS_NODE_OK;
471 1.1 ahoka }
472 1.1 ahoka
473 1.1 ahoka /**
474 1.1 ahoka * chfs_scan_check_data_node - check vnode crc and add to vnode cache
475 1.1 ahoka * @chmp: CHFS main descriptor structure
476 1.1 ahoka * @cheb: eraseblock informations
477 1.1 ahoka * @buf: data node to check
478 1.1 ahoka * @ofs: offset in eraseblock where data node starts
479 1.1 ahoka */
480 1.1 ahoka int
481 1.1 ahoka chfs_scan_check_data_node(struct chfs_mount *chmp,
482 1.1 ahoka struct chfs_eraseblock *cheb, void *buf, off_t ofs)
483 1.1 ahoka {
484 1.1 ahoka KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
485 1.1 ahoka int err;
486 1.1 ahoka uint32_t crc, vno;
487 1.1 ahoka struct chfs_node_ref *nref;
488 1.1 ahoka struct chfs_vnode_cache *vc;
489 1.1 ahoka struct chfs_flash_data_node *dnode = buf;
490 1.1 ahoka
491 1.1 ahoka crc = crc32(0, (uint8_t *)dnode, sizeof(struct chfs_flash_data_node) - 4);
492 1.1 ahoka if (crc != le32toh(dnode->node_crc)) {
493 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb, le32toh(dnode->length));
494 1.1 ahoka if (err)
495 1.1 ahoka return err;
496 1.1 ahoka return CHFS_NODE_BADCRC;
497 1.1 ahoka }
498 1.1 ahoka /**
499 1.1 ahoka * Don't check data nodes crc and version here, it will be done in
500 1.1 ahoka * the background GC thread.
501 1.1 ahoka */
502 1.1 ahoka nref = chfs_alloc_node_ref(cheb);
503 1.1 ahoka if (!nref)
504 1.1 ahoka return ENOMEM;
505 1.1 ahoka
506 1.1 ahoka nref->nref_offset = ofs | CHFS_UNCHECKED_NODE_MASK;
507 1.1 ahoka
508 1.1 ahoka KASSERT(nref->nref_lnr == cheb->lnr);
509 1.1 ahoka
510 1.1 ahoka vno = le64toh(dnode->vno);
511 1.1 ahoka mutex_enter(&chmp->chm_lock_vnocache);
512 1.1 ahoka vc = chfs_vnode_cache_get(chmp, vno);
513 1.1 ahoka if (!vc) {
514 1.1 ahoka vc = chfs_scan_make_vnode_cache(chmp, vno);
515 1.1 ahoka if (!vc)
516 1.1 ahoka return ENOMEM;
517 1.1 ahoka }
518 1.1 ahoka mutex_exit(&chmp->chm_lock_vnocache);
519 1.1 ahoka chfs_add_node_to_list(chmp, vc, nref, &vc->dnode);
520 1.1 ahoka
521 1.1 ahoka dbg("chmpfree: %u, chebfree: %u, dnode: %u\n", chmp->chm_free_size, cheb->free_size, dnode->length);
522 1.1 ahoka
523 1.1 ahoka mutex_enter(&chmp->chm_lock_sizes);
524 1.1 ahoka chfs_change_size_free(chmp, cheb, -dnode->length);
525 1.1 ahoka chfs_change_size_unchecked(chmp, cheb, dnode->length);
526 1.1 ahoka mutex_exit(&chmp->chm_lock_sizes);
527 1.1 ahoka return CHFS_NODE_OK;
528 1.1 ahoka }
529 1.1 ahoka
530 1.1 ahoka /**
531 1.1 ahoka * chfs_scan_classify_cheb - determine eraseblock's state
532 1.1 ahoka * @chmp: CHFS main descriptor structure
533 1.1 ahoka * @cheb: eraseblock to classify
534 1.1 ahoka */
535 1.1 ahoka int
536 1.1 ahoka chfs_scan_classify_cheb(struct chfs_mount *chmp,
537 1.1 ahoka struct chfs_eraseblock *cheb)
538 1.1 ahoka {
539 1.1 ahoka if (cheb->free_size == chmp->chm_ebh->eb_size)
540 1.1 ahoka return CHFS_BLK_STATE_FREE;
541 1.1 ahoka else if (cheb->dirty_size < MAX_DIRTY_TO_CLEAN)
542 1.1 ahoka return CHFS_BLK_STATE_CLEAN;
543 1.1 ahoka else if (cheb->used_size || cheb->unchecked_size)
544 1.1 ahoka return CHFS_BLK_STATE_PARTDIRTY;
545 1.1 ahoka else
546 1.1 ahoka return CHFS_BLK_STATE_ALLDIRTY;
547 1.1 ahoka }
548 1.1 ahoka
549 1.1 ahoka
550 1.1 ahoka /**
551 1.1 ahoka * chfs_scan_eraseblock - scans an eraseblock and looking for nodes
552 1.1 ahoka * @chmp: CHFS main descriptor structure
553 1.1 ahoka * @cheb: eraseblock to scan
554 1.1 ahoka *
555 1.1 ahoka * This function scans a whole eraseblock, checks the nodes on it and add them
556 1.1 ahoka * to the vnode cache.
557 1.1 ahoka * Returns eraseblock state on success, error code if fails.
558 1.1 ahoka */
559 1.1 ahoka int
560 1.1 ahoka chfs_scan_eraseblock(struct chfs_mount *chmp,
561 1.1 ahoka struct chfs_eraseblock *cheb) {
562 1.1 ahoka
563 1.1 ahoka int err;
564 1.1 ahoka size_t len, retlen;
565 1.1 ahoka off_t ofs = 0;
566 1.1 ahoka int lnr = cheb->lnr;
567 1.1 ahoka u_char *buf;
568 1.1 ahoka struct chfs_flash_node_hdr *nhdr;
569 1.1 ahoka int read_free = 0;
570 1.1 ahoka struct chfs_node_ref *nref;
571 1.1 ahoka
572 1.1 ahoka
573 1.1 ahoka dbg("scanning eraseblock content: %d free_size: %d\n", cheb->lnr, cheb->free_size);
574 1.1 ahoka dbg("scanned physical block: %d\n", chmp->chm_ebh->lmap[lnr]);
575 1.1 ahoka buf = kmem_alloc(CHFS_MAX_NODE_SIZE, KM_SLEEP);
576 1.1 ahoka
577 1.1 ahoka while((ofs + CHFS_NODE_HDR_SIZE) < chmp->chm_ebh->eb_size) {
578 1.1 ahoka memset(buf, 0 , CHFS_MAX_NODE_SIZE);
579 1.1 ahoka err = chfs_read_leb(chmp,
580 1.1 ahoka lnr, buf, ofs, CHFS_NODE_HDR_SIZE, &retlen);
581 1.1 ahoka if (err) {
582 1.1 ahoka return err;
583 1.1 ahoka }
584 1.1 ahoka
585 1.1 ahoka if (retlen != CHFS_NODE_HDR_SIZE) {
586 1.1 ahoka chfs_err("Error reading node header: "
587 1.1 ahoka "read: %zu instead of: %zu\n",
588 1.1 ahoka CHFS_NODE_HDR_SIZE, retlen);
589 1.1 ahoka return EIO;
590 1.1 ahoka }
591 1.1 ahoka
592 1.1 ahoka /* first we check if the buffer we read is full with 0xff, if yes maybe
593 1.1 ahoka * the blocks remaining area is free. We increase read_free and if it
594 1.1 ahoka * reaches MAX_READ_FREE we stop reading the block*/
595 1.1 ahoka if (check_pattern(buf, 0xff, 0, CHFS_NODE_HDR_SIZE)) {
596 1.1 ahoka read_free += CHFS_NODE_HDR_SIZE;
597 1.1 ahoka if (read_free >= MAX_READ_FREE(chmp)) {
598 1.1 ahoka dbg("rest of the block is free. Size: %d\n", cheb->free_size);
599 1.1 ahoka return chfs_scan_classify_cheb(chmp, cheb);
600 1.1 ahoka }
601 1.1 ahoka ofs += CHFS_NODE_HDR_SIZE;
602 1.1 ahoka continue;
603 1.1 ahoka } else {
604 1.1 ahoka chfs_update_eb_dirty(chmp, cheb, read_free);
605 1.1 ahoka read_free = 0;
606 1.1 ahoka }
607 1.1 ahoka
608 1.1 ahoka nhdr = (struct chfs_flash_node_hdr *)buf;
609 1.1 ahoka
610 1.1 ahoka err = chfs_scan_check_node_hdr(nhdr);
611 1.1 ahoka if (err) {
612 1.1 ahoka dbg("node hdr error\n");
613 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb, 4);
614 1.1 ahoka if (err) {
615 1.1 ahoka return err;
616 1.1 ahoka }
617 1.1 ahoka
618 1.1 ahoka ofs += 4;
619 1.1 ahoka continue;
620 1.1 ahoka }
621 1.1 ahoka ofs += CHFS_NODE_HDR_SIZE;
622 1.1 ahoka if (ofs > chmp->chm_ebh->eb_size) {
623 1.1 ahoka chfs_err("Second part of node is on the next eraseblock.\n");
624 1.1 ahoka return EIO;
625 1.1 ahoka }
626 1.1 ahoka switch (le16toh(nhdr->type)) {
627 1.1 ahoka case CHFS_NODETYPE_VNODE:
628 1.1 ahoka /* Read up the node */
629 1.1 ahoka //dbg("nodetype vnode\n");
630 1.1 ahoka len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE;
631 1.1 ahoka err = chfs_read_leb(chmp,
632 1.1 ahoka lnr, buf + CHFS_NODE_HDR_SIZE,
633 1.1 ahoka ofs, len, &retlen);
634 1.1 ahoka if (err) {
635 1.1 ahoka return err;
636 1.1 ahoka }
637 1.1 ahoka
638 1.1 ahoka if (retlen != len) {
639 1.1 ahoka chfs_err("Error reading vnode: read: %zu instead of: %zu\n",
640 1.1 ahoka len, retlen);
641 1.1 ahoka return EIO;
642 1.1 ahoka }
643 1.1 ahoka KASSERT(lnr == cheb->lnr);
644 1.1 ahoka err = chfs_scan_check_vnode(chmp,
645 1.1 ahoka cheb, buf, ofs - CHFS_NODE_HDR_SIZE);
646 1.1 ahoka if (err) {
647 1.1 ahoka return err;
648 1.1 ahoka }
649 1.1 ahoka
650 1.1 ahoka //dbg("XXX5end\n");
651 1.1 ahoka break;
652 1.1 ahoka case CHFS_NODETYPE_DIRENT:
653 1.1 ahoka /* Read up the node */
654 1.1 ahoka //dbg("nodetype dirent\n");
655 1.1 ahoka len = le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE;
656 1.1 ahoka
657 1.1 ahoka err = chfs_read_leb(chmp,
658 1.1 ahoka lnr, buf + CHFS_NODE_HDR_SIZE,
659 1.1 ahoka ofs, len, &retlen);
660 1.1 ahoka if (err) {
661 1.1 ahoka return err;
662 1.1 ahoka }
663 1.1 ahoka
664 1.1 ahoka if (retlen != len) {
665 1.1 ahoka chfs_err("Error reading dirent node: read: %zu "
666 1.1 ahoka "instead of: %zu\n", len, retlen);
667 1.1 ahoka return EIO;
668 1.1 ahoka }
669 1.1 ahoka
670 1.1 ahoka KASSERT(lnr == cheb->lnr);
671 1.1 ahoka
672 1.1 ahoka err = chfs_scan_check_dirent_node(chmp,
673 1.1 ahoka cheb, buf, ofs - CHFS_NODE_HDR_SIZE);
674 1.1 ahoka if (err) {
675 1.1 ahoka return err;
676 1.1 ahoka }
677 1.1 ahoka
678 1.1 ahoka //dbg("XXX6end\n");
679 1.1 ahoka break;
680 1.1 ahoka case CHFS_NODETYPE_DATA:
681 1.1 ahoka //dbg("nodetype data\n");
682 1.1 ahoka len = sizeof(struct chfs_flash_data_node) -
683 1.1 ahoka CHFS_NODE_HDR_SIZE;
684 1.1 ahoka err = chfs_read_leb(chmp,
685 1.1 ahoka lnr, buf + CHFS_NODE_HDR_SIZE,
686 1.1 ahoka ofs, len, &retlen);
687 1.1 ahoka if (err) {
688 1.1 ahoka return err;
689 1.1 ahoka }
690 1.1 ahoka
691 1.1 ahoka if (retlen != len) {
692 1.1 ahoka chfs_err("Error reading data node: read: %zu "
693 1.1 ahoka "instead of: %zu\n", len, retlen);
694 1.1 ahoka return EIO;
695 1.1 ahoka }
696 1.1 ahoka KASSERT(lnr == cheb->lnr);
697 1.1 ahoka err = chfs_scan_check_data_node(chmp,
698 1.1 ahoka cheb, buf, ofs - CHFS_NODE_HDR_SIZE);
699 1.1 ahoka if (err)
700 1.1 ahoka return err;
701 1.1 ahoka
702 1.1 ahoka //dbg("XXX7end\n");
703 1.1 ahoka break;
704 1.1 ahoka case CHFS_NODETYPE_PADDING:
705 1.1 ahoka //dbg("nodetype padding\n");
706 1.1 ahoka //dbg("padding len: %d\n", le32toh(nhdr->length));
707 1.1 ahoka //dbg("BEF: cheb->free_size: %d\n", cheb->free_size);
708 1.1 ahoka nref = chfs_alloc_node_ref(cheb);
709 1.1 ahoka nref->nref_offset = ofs - CHFS_NODE_HDR_SIZE;
710 1.1 ahoka nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) |
711 1.1 ahoka CHFS_OBSOLETE_NODE_MASK;
712 1.1 ahoka
713 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb,
714 1.1 ahoka le32toh(nhdr->length));
715 1.1 ahoka //dbg("AFT: cheb->free_size: %d\n", cheb->free_size);
716 1.1 ahoka if (err)
717 1.1 ahoka return err;
718 1.1 ahoka
719 1.1 ahoka //dbg("XXX8end\n");
720 1.1 ahoka break;
721 1.1 ahoka default:
722 1.1 ahoka //dbg("nodetype ? (default)\n");
723 1.1 ahoka /* Unknown node type, update dirty and skip */
724 1.1 ahoka err = chfs_update_eb_dirty(chmp, cheb,
725 1.1 ahoka le32toh(nhdr->length));
726 1.1 ahoka if (err)
727 1.1 ahoka return err;
728 1.1 ahoka
729 1.1 ahoka //dbg("XXX9end\n");
730 1.1 ahoka break;
731 1.1 ahoka }
732 1.1 ahoka ofs += le32toh(nhdr->length) - CHFS_NODE_HDR_SIZE;
733 1.1 ahoka }
734 1.1 ahoka
735 1.1 ahoka KASSERT(cheb->used_size + cheb->free_size + cheb->dirty_size +
736 1.1 ahoka cheb->unchecked_size + cheb->wasted_size == chmp->chm_ebh->eb_size);
737 1.1 ahoka
738 1.1 ahoka //dbg("XXX10\n");
739 1.1 ahoka return chfs_scan_classify_cheb(chmp, cheb);
740 1.1 ahoka }
741