chfs.h revision 1.6 1 1.6 ttoth /* $NetBSD: chfs.h,v 1.6 2012/04/13 14:50:35 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 * Copyright (C) 2009 Ferenc Havasi <havasi (at) inf.u-szeged.hu>
7 1.1 ahoka * Copyright (C) 2009 Zoltan Sogor <weth (at) inf.u-szeged.hu>
8 1.1 ahoka * Copyright (C) 2009 David Tengeri <dtengeri (at) inf.u-szeged.hu>
9 1.1 ahoka * Copyright (C) 2009 Tamas Toth <ttoth (at) inf.u-szeged.hu>
10 1.1 ahoka * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
11 1.1 ahoka * All rights reserved.
12 1.1 ahoka *
13 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
14 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
15 1.1 ahoka *
16 1.1 ahoka * Redistribution and use in source and binary forms, with or without
17 1.1 ahoka * modification, are permitted provided that the following conditions
18 1.1 ahoka * are met:
19 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
20 1.1 ahoka * notice, this list of conditions and the following disclaimer.
21 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
23 1.1 ahoka * documentation and/or other materials provided with the distribution.
24 1.1 ahoka *
25 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 ahoka * SUCH DAMAGE.
36 1.1 ahoka */
37 1.1 ahoka
38 1.1 ahoka #ifndef __CHFS_H__
39 1.1 ahoka #define __CHFS_H__
40 1.1 ahoka
41 1.6 ttoth
42 1.6 ttoth #ifdef _KERNEL
43 1.6 ttoth
44 1.3 ahoka #if 0
45 1.1 ahoka #define DBG_MSG
46 1.1 ahoka #define DBG_MSG_GC
47 1.3 ahoka #endif
48 1.1 ahoka
49 1.1 ahoka #include <sys/param.h>
50 1.1 ahoka #include <sys/kernel.h>
51 1.1 ahoka #include <sys/cdefs.h>
52 1.1 ahoka #include <sys/stdint.h>
53 1.1 ahoka #include <sys/types.h>
54 1.1 ahoka #include <sys/tree.h>
55 1.1 ahoka #include <sys/queue.h>
56 1.1 ahoka #include <sys/kmem.h>
57 1.1 ahoka #include <sys/endian.h>
58 1.1 ahoka #include <sys/rwlock.h>
59 1.1 ahoka #include <sys/condvar.h>
60 1.1 ahoka #include <sys/mutex.h>
61 1.1 ahoka #include <sys/kthread.h>
62 1.1 ahoka #include <sys/rbtree.h>
63 1.1 ahoka #include <sys/vnode.h>
64 1.1 ahoka #include <sys/mount.h>
65 1.1 ahoka #include <sys/hash.h>
66 1.1 ahoka #include <sys/module.h>
67 1.1 ahoka #include <sys/dirent.h>
68 1.1 ahoka
69 1.1 ahoka #include <ufs/ufs/quota.h>
70 1.1 ahoka #include <ufs/ufs/ufsmount.h>
71 1.1 ahoka #include <ufs/ufs/dir.h>
72 1.1 ahoka
73 1.1 ahoka /* XXX shouldnt be defined here, but needed by chfs_inode.h */
74 1.1 ahoka TAILQ_HEAD(chfs_dirent_list, chfs_dirent);
75 1.1 ahoka
76 1.1 ahoka #include "chfs_pool.h"
77 1.6 ttoth #endif /* _KERNEL */
78 1.6 ttoth
79 1.1 ahoka #include "ebh.h"
80 1.1 ahoka #include "media.h"
81 1.1 ahoka #include "chfs_inode.h"
82 1.1 ahoka
83 1.6 ttoth #define CHFS_PAD(x) (((x)+3)&~3)
84 1.6 ttoth
85 1.6 ttoth #ifdef _KERNEL
86 1.6 ttoth
87 1.1 ahoka #ifndef MOUNT_CHFS
88 1.1 ahoka #define MOUNT_CHFS "chfs"
89 1.6 ttoth #endif /* MOUNT_CHFS */
90 1.1 ahoka
91 1.1 ahoka enum {
92 1.1 ahoka VNO_STATE_UNCHECKED, /* CRC checks not yet done */
93 1.1 ahoka VNO_STATE_CHECKING, /* CRC checks in progress */
94 1.1 ahoka VNO_STATE_PRESENT, /* In core */
95 1.1 ahoka VNO_STATE_CHECKEDABSENT,/* Checked, cleared again */
96 1.1 ahoka VNO_STATE_GC, /* GCing a 'pristine' node */
97 1.1 ahoka VNO_STATE_READING, /* In read_inode() */
98 1.1 ahoka VNO_STATE_CLEARING /* In clear_inode() */
99 1.1 ahoka };
100 1.1 ahoka
101 1.5 ttoth
102 1.1 ahoka #define VNODECACHE_SIZE 128
103 1.1 ahoka
104 1.1 ahoka #define MAX_READ_FREE(chmp) (((chmp)->chm_ebh)->eb_size / 8)
105 1.1 ahoka /* an eraseblock will be clean if its dirty size is smaller than this */
106 1.1 ahoka #define MAX_DIRTY_TO_CLEAN 255
107 1.1 ahoka #define VERY_DIRTY(chmp, size) ((size) >= (((chmp)->chm_ebh)->eb_size / 2))
108 1.1 ahoka
109 1.1 ahoka enum {
110 1.1 ahoka CHFS_NODE_OK = 0,
111 1.1 ahoka CHFS_NODE_BADMAGIC,
112 1.1 ahoka CHFS_NODE_BADCRC,
113 1.1 ahoka CHFS_NODE_BADNAMECRC
114 1.1 ahoka };
115 1.1 ahoka
116 1.1 ahoka enum {
117 1.1 ahoka CHFS_BLK_STATE_FREE = 100,
118 1.1 ahoka CHFS_BLK_STATE_CLEAN,
119 1.1 ahoka CHFS_BLK_STATE_PARTDIRTY,
120 1.1 ahoka CHFS_BLK_STATE_ALLDIRTY
121 1.1 ahoka };
122 1.1 ahoka
123 1.1 ahoka extern struct pool chfs_inode_pool;
124 1.1 ahoka extern const struct genfs_ops chfs_genfsops;
125 1.1 ahoka
126 1.1 ahoka /**
127 1.1 ahoka * struct chfs_node_ref - a reference to a node
128 1.1 ahoka * @lnr: logical identifier of the eraseblock where the node is
129 1.1 ahoka * @offset: offset int hte eraseblock where the node starts
130 1.1 ahoka * @next: used at data and dirent nodes, it points to the next data node which
131 1.1 ahoka * belongs to the same vnode
132 1.1 ahoka */
133 1.1 ahoka struct chfs_node_ref
134 1.1 ahoka {
135 1.1 ahoka struct chfs_node_ref *nref_next;
136 1.1 ahoka uint32_t nref_lnr;
137 1.1 ahoka uint32_t nref_offset;
138 1.1 ahoka };
139 1.1 ahoka
140 1.1 ahoka /* Constants for allocating node refs */
141 1.1 ahoka #define REFS_BLOCK_LEN (255/sizeof(struct chfs_node_ref))
142 1.1 ahoka #define REF_EMPTY_NODE (UINT_MAX)
143 1.1 ahoka #define REF_LINK_TO_NEXT (UINT_MAX - 1)
144 1.1 ahoka
145 1.1 ahoka enum {
146 1.1 ahoka CHFS_NORMAL_NODE_MASK,
147 1.1 ahoka CHFS_UNCHECKED_NODE_MASK,
148 1.1 ahoka CHFS_OBSOLETE_NODE_MASK,
149 1.1 ahoka CHFS_PRISTINE_NODE_MASK
150 1.1 ahoka };
151 1.1 ahoka
152 1.1 ahoka #define CHFS_REF_FLAGS(ref) ((ref)->nref_offset & 3)
153 1.1 ahoka #define CHFS_REF_OBSOLETE(ref) (((ref)->nref_offset & 3) == CHFS_OBSOLETE_NODE_MASK)
154 1.1 ahoka #define CHFS_MARK_REF_NORMAL(ref) \
155 1.1 ahoka do { \
156 1.1 ahoka (ref)->nref_offset = CHFS_GET_OFS((ref)->nref_offset) | CHFS_NORMAL_NODE_MASK; \
157 1.1 ahoka } while(0)
158 1.1 ahoka
159 1.1 ahoka #define CHFS_GET_OFS(ofs) (ofs & ~ 3)
160 1.1 ahoka
161 1.1 ahoka static inline struct chfs_node_ref *
162 1.1 ahoka node_next(struct chfs_node_ref *nref)
163 1.1 ahoka {
164 1.1 ahoka //dbg("node next: %u : %u\n", nref->nref_lnr, nref->nref_offset);
165 1.1 ahoka nref++;
166 1.1 ahoka //dbg("nref++: %u : %u\n", nref->nref_lnr, nref->nref_offset);
167 1.1 ahoka
168 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
169 1.1 ahoka //dbg("link to next\n");
170 1.1 ahoka nref = nref->nref_next;
171 1.1 ahoka if (!nref)
172 1.1 ahoka return nref;
173 1.1 ahoka }
174 1.1 ahoka
175 1.1 ahoka if (nref->nref_lnr == REF_EMPTY_NODE) {
176 1.1 ahoka //dbg("empty\n");
177 1.1 ahoka return NULL;
178 1.1 ahoka }
179 1.1 ahoka
180 1.1 ahoka return nref;
181 1.1 ahoka }
182 1.1 ahoka
183 1.1 ahoka /**
184 1.1 ahoka * struct chfs_dirent - full representation of a directory entry
185 1.1 ahoka */
186 1.1 ahoka struct chfs_dirent
187 1.1 ahoka {
188 1.1 ahoka struct chfs_node_ref *nref;
189 1.1 ahoka // struct chfs_dirent *next;
190 1.1 ahoka TAILQ_ENTRY(chfs_dirent) fds;
191 1.1 ahoka uint64_t version;
192 1.1 ahoka ino_t vno;
193 1.1 ahoka uint32_t nhash;
194 1.5 ttoth enum chtype type;
195 1.1 ahoka uint8_t nsize;
196 1.1 ahoka uint8_t name[0];
197 1.1 ahoka
198 1.1 ahoka /* used by chfs_alloc_dirent and free counterpart */
199 1.1 ahoka // size_t alloc_size;
200 1.1 ahoka };
201 1.1 ahoka
202 1.1 ahoka struct chfs_tmp_dnode {
203 1.1 ahoka struct chfs_full_dnode *node;
204 1.1 ahoka uint64_t version;
205 1.1 ahoka uint32_t data_crc;
206 1.1 ahoka //uint32_t partial_crc;
207 1.1 ahoka //uint16_t csize;
208 1.1 ahoka uint16_t overlapped;
209 1.1 ahoka struct chfs_tmp_dnode *next;
210 1.1 ahoka };
211 1.1 ahoka
212 1.1 ahoka struct chfs_tmp_dnode_info {
213 1.1 ahoka struct rb_node rb_node;
214 1.1 ahoka struct chfs_tmp_dnode *tmpnode;
215 1.1 ahoka };
216 1.1 ahoka
217 1.1 ahoka struct chfs_readinode_info {
218 1.1 ahoka struct rb_tree tdi_root;
219 1.1 ahoka struct chfs_tmp_dnode_info *mdata_tn;
220 1.1 ahoka uint64_t highest_version;
221 1.1 ahoka struct chfs_node_ref *latest_ref;
222 1.1 ahoka };
223 1.1 ahoka
224 1.1 ahoka struct chfs_full_dnode {
225 1.1 ahoka struct chfs_node_ref *nref;
226 1.1 ahoka uint64_t ofs;
227 1.1 ahoka uint32_t size;
228 1.1 ahoka uint32_t frags;
229 1.1 ahoka };
230 1.1 ahoka
231 1.1 ahoka struct chfs_node_frag {
232 1.1 ahoka struct rb_node rb_node;
233 1.1 ahoka struct chfs_full_dnode *node;
234 1.1 ahoka uint32_t size;
235 1.1 ahoka uint64_t ofs;
236 1.1 ahoka };
237 1.1 ahoka
238 1.1 ahoka static inline struct chfs_node_frag *
239 1.1 ahoka frag_first(struct rb_tree *tree)
240 1.1 ahoka {
241 1.1 ahoka struct chfs_node_frag *frag;
242 1.1 ahoka
243 1.1 ahoka frag = (struct chfs_node_frag *)RB_TREE_MIN(tree);
244 1.1 ahoka
245 1.1 ahoka return frag;
246 1.1 ahoka }
247 1.1 ahoka
248 1.1 ahoka static inline struct chfs_node_frag *
249 1.1 ahoka frag_last(struct rb_tree *tree)
250 1.1 ahoka {
251 1.1 ahoka struct chfs_node_frag *frag;
252 1.1 ahoka
253 1.1 ahoka frag = (struct chfs_node_frag *)RB_TREE_MAX(tree);
254 1.1 ahoka
255 1.1 ahoka return frag;
256 1.1 ahoka }
257 1.1 ahoka
258 1.1 ahoka #define frag_next(tree, frag) (struct chfs_node_frag *)rb_tree_iterate(tree, frag, RB_DIR_RIGHT)
259 1.1 ahoka #define frag_prev(tree, frag) (struct chfs_node_frag *)rb_tree_iterate(tree, frag, RB_DIR_LEFT)
260 1.1 ahoka
261 1.1 ahoka
262 1.1 ahoka /* XXX hack
263 1.1 ahoka #ifndef CHFS_FRAG_TREE
264 1.1 ahoka #define CHFS_FRAG_TREE
265 1.1 ahoka RB_HEAD(chfs_frag_tree, chfs_node_frag);
266 1.1 ahoka #endif
267 1.1 ahoka */
268 1.1 ahoka
269 1.1 ahoka /* for prototypes, properly defined in chfs_inode.h */
270 1.1 ahoka //struct chfs_inode_ext;
271 1.1 ahoka
272 1.1 ahoka /**
273 1.1 ahoka * struct chfs_vnode_cache - in memory representation of a vnode
274 1.1 ahoka * @v: pointer to the vnode info node
275 1.1 ahoka * @dnode: pointer to the list of data nodes
276 1.1 ahoka * @dirents: pointer to the list of directory entries
277 1.1 ahoka * @vno_version: used only during scan, holds the current version number of
278 1.1 ahoka * chfs_flash_vnode
279 1.1 ahoka * @scan_dirents: used only during scan, holds the full representation of
280 1.1 ahoka * directory entries of this vnode
281 1.1 ahoka * @pvno: parent vnode number
282 1.1 ahoka * @nlink: number of links to this vnode
283 1.1 ahoka */
284 1.1 ahoka struct chfs_vnode_cache {
285 1.1 ahoka // struct chfs_dirent *scan_dirents;
286 1.1 ahoka void *p;
287 1.1 ahoka struct chfs_dirent_list scan_dirents;
288 1.1 ahoka
289 1.1 ahoka struct chfs_node_ref *v;
290 1.1 ahoka struct chfs_node_ref *dnode;
291 1.1 ahoka struct chfs_node_ref *dirents;
292 1.1 ahoka
293 1.1 ahoka uint64_t *vno_version;
294 1.1 ahoka uint64_t highest_version;
295 1.1 ahoka
296 1.1 ahoka uint8_t flags;
297 1.1 ahoka uint16_t state;
298 1.1 ahoka ino_t vno;
299 1.1 ahoka ino_t pvno;
300 1.1 ahoka struct chfs_vnode_cache* next;
301 1.1 ahoka uint32_t nlink;
302 1.1 ahoka };
303 1.1 ahoka
304 1.1 ahoka struct chfs_eraseblock
305 1.1 ahoka {
306 1.1 ahoka uint32_t lnr;
307 1.1 ahoka
308 1.1 ahoka TAILQ_ENTRY(chfs_eraseblock) queue;
309 1.1 ahoka //uint32_t bad_count;
310 1.1 ahoka uint32_t unchecked_size;
311 1.1 ahoka uint32_t used_size;
312 1.1 ahoka uint32_t dirty_size;
313 1.1 ahoka uint32_t free_size;
314 1.1 ahoka uint32_t wasted_size;
315 1.1 ahoka
316 1.1 ahoka struct chfs_node_ref *first_node;
317 1.1 ahoka struct chfs_node_ref *last_node;
318 1.1 ahoka
319 1.4 ahoka /* Next block to be garbage collected */
320 1.4 ahoka struct chfs_node_ref *gc_node;
321 1.1 ahoka };
322 1.1 ahoka
323 1.1 ahoka TAILQ_HEAD(chfs_eraseblock_queue, chfs_eraseblock);
324 1.1 ahoka
325 1.1 ahoka #define ALLOC_NORMAL 0
326 1.1 ahoka #define ALLOC_DELETION 1
327 1.1 ahoka #define ALLOC_GC 2
328 1.1 ahoka
329 1.1 ahoka struct garbage_collector_thread {
330 1.1 ahoka lwp_t *gcth_thread;
331 1.1 ahoka kcondvar_t gcth_wakeup;
332 1.1 ahoka bool gcth_running;
333 1.1 ahoka };
334 1.1 ahoka
335 1.1 ahoka #define CHFS_MP_FLAG_SCANNING 2
336 1.1 ahoka #define CHFS_MP_FLAG_BUILDING 4
337 1.1 ahoka
338 1.1 ahoka /**
339 1.1 ahoka * struct chfs_mount - CHFS main descriptor structure
340 1.1 ahoka * @ebh: eraseblock handler
341 1.1 ahoka * @fl_index: index of flash device in the flash layer
342 1.1 ahoka * @fs_version: filesystem version descriptor
343 1.1 ahoka * @gbl_version: global version number
344 1.1 ahoka * @max_vno: max vnode id
345 1.1 ahoka * @chm_lock_mountfields:
346 1.1 ahoka * @vnocache_hash: hash table of vnode caches
347 1.1 ahoka * @vnocache_lock:
348 1.1 ahoka * @blocks: array of eraseblocks on flash
349 1.1 ahoka * @chm_root: used to protect all fields
350 1.1 ahoka * @free_size: free size on the flash
351 1.1 ahoka * @dirty_size: dirtied size on flash
352 1.1 ahoka * @unchecked_size: size of unchecked data on flash
353 1.1 ahoka * @free_queue: queue of free eraseblocks
354 1.1 ahoka * @clean_queue: queue of clean eraseblocks
355 1.1 ahoka * @dirty_queue: queue of dirty eraseblocks
356 1.1 ahoka * @very_dirty_queue: queue of very dirty eraseblocks
357 1.1 ahoka * @erase_pending_queue: queue of eraseblocks waiting for erasing
358 1.1 ahoka * @erasable_pending_wbuf_queue: queue of eraseblocks waiting for erasing and
359 1.4 ahoka * have data to write to them
360 1.1 ahoka * @nextblock: next eraseblock to write to
361 1.1 ahoka * @nr_free_blocks: number of free blocks on the free_queue
362 1.1 ahoka * @nr_erasable_blocks: number of blocks that can be erased and are on the
363 1.4 ahoka * erasable_queue
364 1.1 ahoka */
365 1.1 ahoka struct chfs_mount {
366 1.1 ahoka struct mount *chm_fsmp;
367 1.1 ahoka struct chfs_ebh *chm_ebh;
368 1.1 ahoka int chm_fs_version;
369 1.1 ahoka uint64_t chm_gbl_version;
370 1.1 ahoka ino_t chm_max_vno;
371 1.1 ahoka ino_t chm_checked_vno;
372 1.1 ahoka unsigned int chm_flags;
373 1.1 ahoka
374 1.1 ahoka /* chm_lock_mountfields:
375 1.1 ahoka * Used to protect all the following fields. */
376 1.1 ahoka kmutex_t chm_lock_mountfields;
377 1.1 ahoka
378 1.1 ahoka struct chfs_vnode_cache **chm_vnocache_hash;
379 1.1 ahoka /* chm_lock_vnocache:
380 1.1 ahoka * Used to protect the vnode cache.
381 1.1 ahoka * If you have to lock chm_lock_mountfields and also chm_lock_vnocache,
382 1.1 ahoka * you must lock chm_lock_mountfields first. */
383 1.1 ahoka kmutex_t chm_lock_vnocache;
384 1.1 ahoka
385 1.1 ahoka struct chfs_eraseblock *chm_blocks;
386 1.1 ahoka
387 1.1 ahoka struct chfs_node *chm_root;
388 1.1 ahoka
389 1.1 ahoka uint32_t chm_free_size;
390 1.1 ahoka uint32_t chm_dirty_size;
391 1.1 ahoka uint32_t chm_unchecked_size;
392 1.1 ahoka uint32_t chm_used_size;
393 1.1 ahoka uint32_t chm_wasted_size;
394 1.1 ahoka /* chm_lock_sizes:
395 1.1 ahoka * Used to protect the (free, used, etc.) sizes of the FS
396 1.1 ahoka * (and also the sizes of each eraseblock).
397 1.1 ahoka * If you have to lock chm_lock_mountfields and also chm_lock_sizes,
398 1.1 ahoka * you must lock chm_lock_mountfields first. */
399 1.1 ahoka kmutex_t chm_lock_sizes;
400 1.1 ahoka
401 1.1 ahoka struct chfs_eraseblock_queue chm_free_queue;
402 1.1 ahoka struct chfs_eraseblock_queue chm_clean_queue;
403 1.1 ahoka struct chfs_eraseblock_queue chm_dirty_queue;
404 1.1 ahoka struct chfs_eraseblock_queue chm_very_dirty_queue;
405 1.1 ahoka struct chfs_eraseblock_queue chm_erasable_pending_wbuf_queue;
406 1.1 ahoka struct chfs_eraseblock_queue chm_erase_pending_queue;
407 1.1 ahoka
408 1.1 ahoka uint8_t chm_resv_blocks_deletion;
409 1.1 ahoka uint8_t chm_resv_blocks_write;
410 1.1 ahoka uint8_t chm_resv_blocks_gctrigger;
411 1.1 ahoka uint8_t chm_resv_blocks_gcmerge;
412 1.1 ahoka uint8_t chm_nospc_dirty;
413 1.1 ahoka
414 1.1 ahoka uint8_t chm_vdirty_blocks_gctrigger;
415 1.1 ahoka
416 1.1 ahoka struct chfs_eraseblock *chm_nextblock;
417 1.1 ahoka
418 1.1 ahoka struct garbage_collector_thread chm_gc_thread;
419 1.1 ahoka struct chfs_eraseblock *chm_gcblock;
420 1.1 ahoka
421 1.1 ahoka int chm_nr_free_blocks;
422 1.1 ahoka int chm_nr_erasable_blocks;
423 1.1 ahoka
424 1.1 ahoka int32_t chm_fs_bmask;
425 1.1 ahoka int32_t chm_fs_bsize;
426 1.1 ahoka int32_t chm_fs_qbmask;
427 1.1 ahoka int32_t chm_fs_bshift;
428 1.1 ahoka int32_t chm_fs_fmask;
429 1.1 ahoka int64_t chm_fs_qfmask;
430 1.1 ahoka
431 1.1 ahoka /* TODO will we use these? */
432 1.1 ahoka unsigned int chm_pages_max;
433 1.1 ahoka unsigned int chm_pages_used;
434 1.1 ahoka unsigned int chm_nodes_max;
435 1.1 ahoka unsigned int chm_nodes_cnt;
436 1.1 ahoka struct chfs_pool chm_dirent_pool;
437 1.1 ahoka struct chfs_pool chm_node_pool;
438 1.1 ahoka struct chfs_str_pool chm_str_pool;
439 1.1 ahoka /**/
440 1.1 ahoka
441 1.1 ahoka size_t chm_wbuf_pagesize;
442 1.1 ahoka unsigned char* chm_wbuf;
443 1.1 ahoka size_t chm_wbuf_ofs;
444 1.1 ahoka size_t chm_wbuf_len;
445 1.1 ahoka /* chm_lock_wbuf:
446 1.1 ahoka * Used to protect the write buffer.
447 1.1 ahoka * If you have to lock chm_lock_mountfields and also chm_lock_wbuf,
448 1.1 ahoka * you must lock chm_lock_mountfields first. */
449 1.1 ahoka krwlock_t chm_lock_wbuf;
450 1.1 ahoka };
451 1.1 ahoka
452 1.1 ahoka /*
453 1.1 ahoka * TODO we should move here all of these from the bottom of the file
454 1.1 ahoka * Macros/functions to convert from generic data structures to chfs
455 1.1 ahoka * specific ones.
456 1.1 ahoka */
457 1.1 ahoka
458 1.4 ahoka #define CHFS_OFFSET_DOT 0
459 1.1 ahoka #define CHFS_OFFSET_DOTDOT 1
460 1.4 ahoka #define CHFS_OFFSET_EOF 2
461 1.1 ahoka #define CHFS_OFFSET_FIRST 3
462 1.1 ahoka
463 1.1 ahoka
464 1.1 ahoka /*---------------------------------------------------------------------------*/
465 1.1 ahoka
466 1.1 ahoka /* chfs_build.c */
467 1.1 ahoka void chfs_calc_trigger_levels(struct chfs_mount *);
468 1.1 ahoka int chfs_build_filesystem(struct chfs_mount *);
469 1.4 ahoka void chfs_build_set_vnodecache_nlink(struct chfs_mount *,
470 1.4 ahoka struct chfs_vnode_cache *);
471 1.4 ahoka void chfs_build_remove_unlinked_vnode(struct chfs_mount *,
472 1.4 ahoka struct chfs_vnode_cache *, struct chfs_dirent_list *);
473 1.1 ahoka
474 1.1 ahoka /* chfs_scan.c */
475 1.1 ahoka int chfs_scan_eraseblock(struct chfs_mount *, struct chfs_eraseblock *);
476 1.4 ahoka struct chfs_vnode_cache *chfs_scan_make_vnode_cache(struct chfs_mount *,
477 1.4 ahoka ino_t);
478 1.4 ahoka int chfs_scan_check_node_hdr(struct chfs_flash_node_hdr *);
479 1.4 ahoka int chfs_scan_check_vnode(struct chfs_mount *,
480 1.4 ahoka struct chfs_eraseblock *, void *, off_t);
481 1.4 ahoka int chfs_scan_mark_dirent_obsolete(struct chfs_mount *,
482 1.4 ahoka struct chfs_vnode_cache *, struct chfs_dirent *);
483 1.4 ahoka void chfs_add_fd_to_list(struct chfs_mount *,
484 1.4 ahoka struct chfs_dirent *, struct chfs_vnode_cache *);
485 1.4 ahoka int chfs_scan_check_dirent_node(struct chfs_mount *,
486 1.4 ahoka struct chfs_eraseblock *, void *, off_t);
487 1.4 ahoka int chfs_scan_check_data_node(struct chfs_mount *,
488 1.4 ahoka struct chfs_eraseblock *, void *, off_t);
489 1.4 ahoka int chfs_scan_classify_cheb(struct chfs_mount *,
490 1.4 ahoka struct chfs_eraseblock *);
491 1.4 ahoka
492 1.1 ahoka /* chfs_nodeops.c */
493 1.4 ahoka int chfs_update_eb_dirty(struct chfs_mount *,
494 1.4 ahoka struct chfs_eraseblock *, uint32_t);
495 1.1 ahoka void chfs_add_node_to_list(struct chfs_mount *, struct chfs_vnode_cache *,
496 1.1 ahoka struct chfs_node_ref *, struct chfs_node_ref **);
497 1.1 ahoka void chfs_add_fd_to_inode(struct chfs_mount *,
498 1.1 ahoka struct chfs_inode *, struct chfs_dirent *);
499 1.1 ahoka void chfs_add_vnode_ref_to_vc(struct chfs_mount *, struct chfs_vnode_cache *,
500 1.1 ahoka struct chfs_node_ref *);
501 1.1 ahoka struct chfs_node_ref* chfs_nref_next(struct chfs_node_ref *);
502 1.1 ahoka int chfs_nref_len(struct chfs_mount *,
503 1.1 ahoka struct chfs_eraseblock *, struct chfs_node_ref *);
504 1.1 ahoka int chfs_close_eraseblock(struct chfs_mount *,
505 1.1 ahoka struct chfs_eraseblock *);
506 1.1 ahoka int chfs_reserve_space_normal(struct chfs_mount *, uint32_t, int);
507 1.1 ahoka int chfs_reserve_space_gc(struct chfs_mount *, uint32_t);
508 1.1 ahoka int chfs_reserve_space(struct chfs_mount *, uint32_t);
509 1.1 ahoka void chfs_mark_node_obsolete(struct chfs_mount *, struct chfs_node_ref *);
510 1.1 ahoka
511 1.1 ahoka static inline struct chfs_vnode_cache *
512 1.1 ahoka chfs_nref_to_vc(struct chfs_node_ref *nref)
513 1.1 ahoka {
514 1.1 ahoka while (nref->nref_next) {
515 1.1 ahoka nref = nref->nref_next;
516 1.1 ahoka //dbg("lnr: %u, ofs: %u\n", nref->nref_lnr, nref->nref_offset);
517 1.1 ahoka //dbg("vno: %llu\n", ((struct chfs_vnode_cache *)(nref))->vno);
518 1.1 ahoka //dbg("scan_dirents: %p\n", ((struct chfs_vnode_cache *)(nref))->scan_dirents);
519 1.1 ahoka if (nref->nref_lnr == REF_LINK_TO_NEXT) {
520 1.1 ahoka dbg("Link to next!\n");
521 1.1 ahoka } else if (nref->nref_lnr == REF_EMPTY_NODE) {
522 1.1 ahoka dbg("Empty!\n");
523 1.1 ahoka }
524 1.1 ahoka }
525 1.1 ahoka //dbg("vno: %llu\n", ((struct chfs_vnode_cache *)(nref))->vno);
526 1.1 ahoka
527 1.1 ahoka //dbg("NREF_TO_VC: GET IT\n");
528 1.1 ahoka //dbg("nref_next: %p, lnr: %u, ofs: %u\n", nref->nref_next, nref->nref_lnr, nref->nref_offset);
529 1.1 ahoka struct chfs_vnode_cache *vc = (struct chfs_vnode_cache *) nref;
530 1.2 ahoka dbg("vno: %ju, pvno: %ju, hv: %ju, nlink: %u\n", (intmax_t )vc->vno,
531 1.2 ahoka (intmax_t )vc->pvno, (intmax_t )vc->highest_version, vc->nlink);
532 1.1 ahoka //return ((struct chfs_vnode_cache *)nref);
533 1.1 ahoka return vc;
534 1.1 ahoka }
535 1.1 ahoka
536 1.1 ahoka
537 1.1 ahoka /* chfs_malloc.c */
538 1.1 ahoka int chfs_alloc_pool_caches(void);
539 1.1 ahoka void chfs_destroy_pool_caches(void);
540 1.1 ahoka struct chfs_vnode_cache* chfs_vnode_cache_alloc(ino_t);
541 1.1 ahoka void chfs_vnode_cache_free(struct chfs_vnode_cache *);
542 1.1 ahoka struct chfs_node_ref* chfs_alloc_node_ref(
543 1.1 ahoka struct chfs_eraseblock *);
544 1.4 ahoka void chfs_free_node_refs(struct chfs_eraseblock *);
545 1.1 ahoka struct chfs_dirent* chfs_alloc_dirent(int);
546 1.1 ahoka void chfs_free_dirent(struct chfs_dirent *);
547 1.1 ahoka struct chfs_flash_vnode* chfs_alloc_flash_vnode(void);
548 1.1 ahoka void chfs_free_flash_vnode(struct chfs_flash_vnode *);
549 1.1 ahoka struct chfs_flash_dirent_node* chfs_alloc_flash_dirent(void);
550 1.1 ahoka void chfs_free_flash_dirent(struct chfs_flash_dirent_node *);
551 1.1 ahoka struct chfs_flash_data_node* chfs_alloc_flash_dnode(void);
552 1.1 ahoka void chfs_free_flash_dnode(struct chfs_flash_data_node *);
553 1.1 ahoka struct chfs_node_frag* chfs_alloc_node_frag(void);
554 1.1 ahoka void chfs_free_node_frag(struct chfs_node_frag *);
555 1.1 ahoka struct chfs_node_ref* chfs_alloc_refblock(void);
556 1.4 ahoka void chfs_free_refblock(struct chfs_node_ref *);
557 1.1 ahoka struct chfs_full_dnode* chfs_alloc_full_dnode(void);
558 1.4 ahoka void chfs_free_full_dnode(struct chfs_full_dnode *);
559 1.1 ahoka struct chfs_tmp_dnode * chfs_alloc_tmp_dnode(void);
560 1.1 ahoka void chfs_free_tmp_dnode(struct chfs_tmp_dnode *);
561 1.1 ahoka struct chfs_tmp_dnode_info * chfs_alloc_tmp_dnode_info(void);
562 1.1 ahoka void chfs_free_tmp_dnode_info(struct chfs_tmp_dnode_info *);
563 1.1 ahoka
564 1.1 ahoka /* chfs_readinode.c */
565 1.1 ahoka int chfs_read_inode(struct chfs_mount *, struct chfs_inode *);
566 1.1 ahoka int chfs_read_inode_internal(struct chfs_mount *, struct chfs_inode *);
567 1.1 ahoka void chfs_kill_fragtree(struct rb_tree *);
568 1.1 ahoka uint32_t chfs_truncate_fragtree(struct chfs_mount *,
569 1.1 ahoka struct rb_tree *, uint32_t);
570 1.1 ahoka int chfs_add_full_dnode_to_inode(struct chfs_mount *,
571 1.1 ahoka struct chfs_inode *,
572 1.1 ahoka struct chfs_full_dnode *);
573 1.1 ahoka int chfs_read_data(struct chfs_mount*, struct vnode *,
574 1.1 ahoka struct buf *);
575 1.1 ahoka
576 1.1 ahoka /* chfs_erase.c */
577 1.4 ahoka int chfs_remap_leb(struct chfs_mount *);
578 1.1 ahoka
579 1.1 ahoka /* chfs_ihash.c */
580 1.1 ahoka void chfs_ihashinit(void);
581 1.1 ahoka void chfs_ihashreinit(void);
582 1.1 ahoka void chfs_ihashdone(void);
583 1.1 ahoka struct vnode *chfs_ihashlookup(dev_t, ino_t);
584 1.1 ahoka struct vnode *chfs_ihashget(dev_t, ino_t, int);
585 1.1 ahoka void chfs_ihashins(struct chfs_inode *);
586 1.1 ahoka void chfs_ihashrem(struct chfs_inode *);
587 1.1 ahoka
588 1.1 ahoka extern kmutex_t chfs_ihash_lock;
589 1.1 ahoka extern kmutex_t chfs_hashlock;
590 1.1 ahoka
591 1.1 ahoka /* chfs_gc.c */
592 1.1 ahoka void chfs_gc_trigger(struct chfs_mount *);
593 1.1 ahoka int chfs_gc_thread_should_wake(struct chfs_mount *);
594 1.1 ahoka void chfs_gc_thread(void *);
595 1.1 ahoka void chfs_gc_thread_start(struct chfs_mount *);
596 1.1 ahoka void chfs_gc_thread_stop(struct chfs_mount *);
597 1.1 ahoka int chfs_gcollect_pass(struct chfs_mount *);
598 1.1 ahoka
599 1.1 ahoka /* chfs_vfsops.c*/
600 1.4 ahoka int chfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
601 1.4 ahoka int chfs_mountfs(struct vnode *, struct mount *);
602 1.1 ahoka
603 1.1 ahoka /* chfs_vnops.c */
604 1.1 ahoka extern int (**chfs_vnodeop_p)(void *);
605 1.1 ahoka extern int (**chfs_specop_p)(void *);
606 1.1 ahoka extern int (**chfs_fifoop_p)(void *);
607 1.1 ahoka int chfs_lookup(void *);
608 1.1 ahoka int chfs_create(void *);
609 1.1 ahoka int chfs_mknod(void *);
610 1.1 ahoka int chfs_open(void *);
611 1.1 ahoka int chfs_close(void *);
612 1.1 ahoka int chfs_access(void *);
613 1.1 ahoka int chfs_getattr(void *);
614 1.1 ahoka int chfs_setattr(void *);
615 1.1 ahoka int chfs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t);
616 1.1 ahoka int chfs_chmod(struct vnode *, int, kauth_cred_t);
617 1.1 ahoka int chfs_read(void *);
618 1.1 ahoka int chfs_write(void *);
619 1.1 ahoka int chfs_fsync(void *);
620 1.1 ahoka int chfs_remove(void *);
621 1.1 ahoka int chfs_link(void *);
622 1.1 ahoka int chfs_rename(void *);
623 1.1 ahoka int chfs_mkdir(void *);
624 1.1 ahoka int chfs_rmdir(void *);
625 1.1 ahoka int chfs_symlink(void *);
626 1.1 ahoka int chfs_readdir(void *);
627 1.1 ahoka int chfs_readlink(void *);
628 1.1 ahoka int chfs_inactive(void *);
629 1.1 ahoka int chfs_reclaim(void *);
630 1.1 ahoka int chfs_advlock(void *);
631 1.1 ahoka int chfs_strategy(void *);
632 1.1 ahoka int chfs_bmap(void *);
633 1.1 ahoka
634 1.1 ahoka /* chfs_vnode.c */
635 1.1 ahoka struct vnode *chfs_vnode_lookup(struct chfs_mount *, ino_t);
636 1.1 ahoka int chfs_readvnode(struct mount *, ino_t, struct vnode **);
637 1.1 ahoka int chfs_readdirent(struct mount *, struct chfs_node_ref *,
638 1.1 ahoka struct chfs_inode *);
639 1.1 ahoka int chfs_makeinode(int, struct vnode *, struct vnode **,
640 1.5 ttoth struct componentname *, enum vtype );
641 1.1 ahoka void chfs_set_vnode_size(struct vnode *, size_t);
642 1.1 ahoka void chfs_change_size_free(struct chfs_mount *,
643 1.1 ahoka struct chfs_eraseblock *, int);
644 1.1 ahoka void chfs_change_size_dirty(struct chfs_mount *,
645 1.1 ahoka struct chfs_eraseblock *, int);
646 1.1 ahoka void chfs_change_size_unchecked(struct chfs_mount *,
647 1.1 ahoka struct chfs_eraseblock *, int);
648 1.1 ahoka void chfs_change_size_used(struct chfs_mount *,
649 1.1 ahoka struct chfs_eraseblock *, int);
650 1.1 ahoka void chfs_change_size_wasted(struct chfs_mount *,
651 1.1 ahoka struct chfs_eraseblock *, int);
652 1.1 ahoka
653 1.1 ahoka /* chfs_vnode_cache.c */
654 1.1 ahoka struct chfs_vnode_cache **chfs_vnocache_hash_init(void);
655 1.1 ahoka void chfs_vnocache_hash_destroy(struct chfs_vnode_cache **);
656 1.1 ahoka void chfs_vnode_cache_set_state(struct chfs_mount *,
657 1.1 ahoka struct chfs_vnode_cache *, int);
658 1.4 ahoka struct chfs_vnode_cache* chfs_vnode_cache_get(struct chfs_mount *, ino_t);
659 1.4 ahoka void chfs_vnode_cache_add(struct chfs_mount *, struct chfs_vnode_cache *);
660 1.4 ahoka void chfs_vnode_cache_remove(struct chfs_mount *, struct chfs_vnode_cache *);
661 1.1 ahoka
662 1.1 ahoka /* chfs_wbuf.c */
663 1.4 ahoka int chfs_write_wbuf(struct chfs_mount*,
664 1.4 ahoka const struct iovec *, long, off_t, size_t *);
665 1.1 ahoka int chfs_flush_pending_wbuf(struct chfs_mount *);
666 1.1 ahoka
667 1.1 ahoka /* chfs_write.c */
668 1.1 ahoka int chfs_write_flash_vnode(struct chfs_mount *, struct chfs_inode *, int);
669 1.1 ahoka int chfs_write_flash_dirent(struct chfs_mount *, struct chfs_inode *,
670 1.1 ahoka struct chfs_inode *, struct chfs_dirent *, ino_t, int);
671 1.1 ahoka int chfs_write_flash_dnode(struct chfs_mount *, struct vnode *,
672 1.1 ahoka struct buf *, struct chfs_full_dnode *);
673 1.4 ahoka int chfs_do_link(struct chfs_inode *,
674 1.5 ttoth struct chfs_inode *, const char *, int, enum chtype);
675 1.4 ahoka int chfs_do_unlink(struct chfs_inode *,
676 1.4 ahoka struct chfs_inode *, const char *, int);
677 1.1 ahoka
678 1.1 ahoka /* chfs_subr.c */
679 1.1 ahoka size_t chfs_mem_info(bool);
680 1.1 ahoka struct chfs_dirent * chfs_dir_lookup(struct chfs_inode *,
681 1.1 ahoka struct componentname *);
682 1.5 ttoth int chfs_filldir (struct uio *, ino_t, const char *, int, enum chtype);
683 1.1 ahoka int chfs_chsize(struct vnode *, u_quad_t, kauth_cred_t);
684 1.1 ahoka int chfs_chflags(struct vnode *, int, kauth_cred_t);
685 1.1 ahoka void chfs_itimes(struct chfs_inode *, const struct timespec *,
686 1.1 ahoka const struct timespec *, const struct timespec *);
687 1.1 ahoka int chfs_update(struct vnode *, const struct timespec *,
688 1.1 ahoka const struct timespec *, int);
689 1.1 ahoka //int chfs_truncate(struct vnode *, off_t);
690 1.1 ahoka
691 1.1 ahoka /*---------------------------------------------------------------------------*/
692 1.1 ahoka
693 1.1 ahoka /* Some inline functions temporarily placed here */
694 1.1 ahoka static inline int
695 1.1 ahoka chfs_map_leb(struct chfs_mount *chmp, int lnr)
696 1.1 ahoka {
697 1.1 ahoka int err;
698 1.1 ahoka
699 1.1 ahoka err = ebh_map_leb(chmp->chm_ebh, lnr);
700 1.1 ahoka if (err)
701 1.1 ahoka chfs_err("unmap leb %d failed, error: %d\n",lnr, err);
702 1.1 ahoka
703 1.1 ahoka return err;
704 1.1 ahoka
705 1.1 ahoka }
706 1.1 ahoka
707 1.1 ahoka static inline int
708 1.1 ahoka chfs_unmap_leb(struct chfs_mount *chmp, int lnr)
709 1.1 ahoka {
710 1.1 ahoka int err;
711 1.1 ahoka
712 1.1 ahoka err = ebh_unmap_leb(chmp->chm_ebh, lnr);
713 1.1 ahoka if (err)
714 1.1 ahoka chfs_err("unmap leb %d failed, error: %d\n",lnr, err);
715 1.1 ahoka
716 1.1 ahoka return err;
717 1.1 ahoka }
718 1.1 ahoka
719 1.1 ahoka static inline int
720 1.1 ahoka chfs_read_leb(struct chfs_mount *chmp, int lnr, char *buf,
721 1.1 ahoka int offset, int len, size_t *retlen)
722 1.1 ahoka {
723 1.1 ahoka int err;
724 1.1 ahoka
725 1.1 ahoka err = ebh_read_leb(chmp->chm_ebh, lnr, buf, offset, len, retlen);
726 1.1 ahoka if (err)
727 1.4 ahoka chfs_err("read leb %d:%d failed, error: %d\n",
728 1.4 ahoka lnr, offset, err);
729 1.1 ahoka
730 1.1 ahoka return err;
731 1.1 ahoka }
732 1.1 ahoka
733 1.1 ahoka static inline int chfs_write_leb(struct chfs_mount *chmp, int lnr, char *buf,
734 1.1 ahoka int offset, int len, size_t *retlen)
735 1.1 ahoka {
736 1.1 ahoka int err;
737 1.1 ahoka err = ebh_write_leb(chmp->chm_ebh, lnr, buf, offset, len, retlen);
738 1.1 ahoka if (err)
739 1.4 ahoka chfs_err("write leb %d:%d failed, error: %d\n",
740 1.4 ahoka lnr, offset, err);
741 1.1 ahoka
742 1.1 ahoka return err;
743 1.1 ahoka }
744 1.1 ahoka
745 1.1 ahoka /******************************************************************************/
746 1.1 ahoka /* Code from dummyfs.h */
747 1.1 ahoka /******************************************************************************/
748 1.1 ahoka /* --------------------------------------------------------------------- */
749 1.1 ahoka
750 1.1 ahoka #define CHFS_PAGES_RESERVED (4 * 1024 * 1024 / PAGE_SIZE)
751 1.1 ahoka
752 1.1 ahoka static __inline size_t
753 1.1 ahoka CHFS_PAGES_MAX(struct chfs_mount *chmp)
754 1.1 ahoka {
755 1.1 ahoka size_t freepages;
756 1.1 ahoka
757 1.1 ahoka freepages = chfs_mem_info(false);
758 1.1 ahoka if (freepages < CHFS_PAGES_RESERVED)
759 1.1 ahoka freepages = 0;
760 1.1 ahoka else
761 1.1 ahoka freepages -= CHFS_PAGES_RESERVED;
762 1.1 ahoka
763 1.1 ahoka return MIN(chmp->chm_pages_max, freepages + chmp->chm_pages_used);
764 1.1 ahoka }
765 1.1 ahoka
766 1.1 ahoka #define CHFS_ITIMES(ip, acc, mod, cre) \
767 1.1 ahoka while ((ip)->iflag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY)) \
768 1.1 ahoka chfs_itimes(ip, acc, mod, cre)
769 1.4 ahoka
770 1.1 ahoka /* used for KASSERTs */
771 1.1 ahoka #define IMPLIES(a, b) (!(a) || (b))
772 1.1 ahoka #define IFF(a, b) (IMPLIES(a, b) && IMPLIES(b, a))
773 1.1 ahoka
774 1.6 ttoth #endif /* _KERNEL */
775 1.1 ahoka #endif /* __CHFS_H__ */
776