blob: 9028d7f83bbd8c99b95f4381d622940b54f375aa [file] [log] [blame]
Joern Engel5db53f32009-11-20 20:13:39 +01001/*
2 * fs/logfs/logfs.h
3 *
4 * As should be obvious for Linux kernel code, license is GPLv2
5 *
6 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
7 *
8 * Private header for logfs.
9 */
10#ifndef FS_LOGFS_LOGFS_H
11#define FS_LOGFS_LOGFS_H
12
13#undef __CHECK_ENDIAN__
14#define __CHECK_ENDIAN__
15
16#include <linux/btree.h>
17#include <linux/crc32.h>
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/mempool.h>
21#include <linux/pagemap.h>
22#include <linux/mtd/mtd.h>
23#include "logfs_abi.h"
24
25#define LOGFS_DEBUG_SUPER (0x0001)
26#define LOGFS_DEBUG_SEGMENT (0x0002)
27#define LOGFS_DEBUG_JOURNAL (0x0004)
28#define LOGFS_DEBUG_DIR (0x0008)
29#define LOGFS_DEBUG_FILE (0x0010)
30#define LOGFS_DEBUG_INODE (0x0020)
31#define LOGFS_DEBUG_READWRITE (0x0040)
32#define LOGFS_DEBUG_GC (0x0080)
33#define LOGFS_DEBUG_GC_NOISY (0x0100)
34#define LOGFS_DEBUG_ALIASES (0x0200)
35#define LOGFS_DEBUG_BLOCKMOVE (0x0400)
36#define LOGFS_DEBUG_ALL (0xffffffff)
37
38#define LOGFS_DEBUG (0x01)
39/*
40 * To enable specific log messages, simply define LOGFS_DEBUG to match any
41 * or all of the above.
42 */
43#ifndef LOGFS_DEBUG
44#define LOGFS_DEBUG (0)
45#endif
46
47#define log_cond(cond, fmt, arg...) do { \
48 if (cond) \
49 printk(KERN_DEBUG fmt, ##arg); \
50} while (0)
51
52#define log_super(fmt, arg...) \
53 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SUPER, fmt, ##arg)
54#define log_segment(fmt, arg...) \
55 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SEGMENT, fmt, ##arg)
56#define log_journal(fmt, arg...) \
57 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_JOURNAL, fmt, ##arg)
58#define log_dir(fmt, arg...) \
59 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_DIR, fmt, ##arg)
60#define log_file(fmt, arg...) \
61 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_FILE, fmt, ##arg)
62#define log_inode(fmt, arg...) \
63 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_INODE, fmt, ##arg)
64#define log_readwrite(fmt, arg...) \
65 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_READWRITE, fmt, ##arg)
66#define log_gc(fmt, arg...) \
67 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC, fmt, ##arg)
68#define log_gc_noisy(fmt, arg...) \
69 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC_NOISY, fmt, ##arg)
70#define log_aliases(fmt, arg...) \
71 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_ALIASES, fmt, ##arg)
72#define log_blockmove(fmt, arg...) \
73 log_cond(LOGFS_DEBUG & LOGFS_DEBUG_BLOCKMOVE, fmt, ##arg)
74
75#define PG_pre_locked PG_owner_priv_1
76#define PagePreLocked(page) test_bit(PG_pre_locked, &(page)->flags)
77#define SetPagePreLocked(page) set_bit(PG_pre_locked, &(page)->flags)
78#define ClearPagePreLocked(page) clear_bit(PG_pre_locked, &(page)->flags)
79
80/* FIXME: This should really be somewhere in the 64bit area. */
81#define LOGFS_LINK_MAX (1<<30)
82
83/* Read-only filesystem */
84#define LOGFS_SB_FLAG_RO 0x0001
Joern Engelc6d38302010-03-04 21:36:19 +010085#define LOGFS_SB_FLAG_DIRTY 0x0002
Joern Engel5db53f32009-11-20 20:13:39 +010086#define LOGFS_SB_FLAG_OBJ_ALIAS 0x0004
87#define LOGFS_SB_FLAG_SHUTDOWN 0x0008
88
89/* Write Control Flags */
90#define WF_LOCK 0x01 /* take write lock */
91#define WF_WRITE 0x02 /* write block */
92#define WF_DELETE 0x04 /* delete old block */
93
94typedef u8 __bitwise level_t;
95typedef u8 __bitwise gc_level_t;
96
97#define LEVEL(level) ((__force level_t)(level))
98#define GC_LEVEL(gc_level) ((__force gc_level_t)(gc_level))
99
100#define SUBLEVEL(level) ( (void)((level) == LEVEL(1)), \
101 (__force level_t)((__force u8)(level) - 1) )
102
103/**
104 * struct logfs_area - area management information
105 *
106 * @a_sb: the superblock this area belongs to
107 * @a_is_open: 1 if the area is currently open, else 0
108 * @a_segno: segment number of area
109 * @a_written_bytes: number of bytes already written back
110 * @a_used_bytes: number of used bytes
111 * @a_ops: area operations (either journal or ostore)
112 * @a_erase_count: erase count
113 * @a_level: GC level
114 */
115struct logfs_area { /* a segment open for writing */
116 struct super_block *a_sb;
117 int a_is_open;
118 u32 a_segno;
119 u32 a_written_bytes;
120 u32 a_used_bytes;
121 const struct logfs_area_ops *a_ops;
122 u32 a_erase_count;
123 gc_level_t a_level;
124};
125
126/**
127 * struct logfs_area_ops - area operations
128 *
129 * @get_free_segment: fill area->ofs with the offset of a free segment
130 * @get_erase_count: fill area->erase_count (needs area->ofs)
131 * @erase_segment: erase and setup segment
132 */
133struct logfs_area_ops {
134 void (*get_free_segment)(struct logfs_area *area);
135 void (*get_erase_count)(struct logfs_area *area);
136 int (*erase_segment)(struct logfs_area *area);
137};
138
139/**
140 * struct logfs_device_ops - device access operations
141 *
142 * @readpage: read one page (mm page)
143 * @writeseg: write one segment. may be a partial segment
144 * @erase: erase one segment
145 * @read: read from the device
146 * @erase: erase part of the device
147 */
148struct logfs_device_ops {
149 struct page *(*find_first_sb)(struct super_block *sb, u64 *ofs);
150 struct page *(*find_last_sb)(struct super_block *sb, u64 *ofs);
151 int (*write_sb)(struct super_block *sb, struct page *page);
152 int (*readpage)(void *_sb, struct page *page);
153 void (*writeseg)(struct super_block *sb, u64 ofs, size_t len);
Joern Engel94215022010-03-04 21:30:58 +0100154 int (*erase)(struct super_block *sb, loff_t ofs, size_t len,
155 int ensure_write);
Joern Engel5db53f32009-11-20 20:13:39 +0100156 void (*sync)(struct super_block *sb);
157 void (*put_device)(struct super_block *sb);
158};
159
160/**
161 * struct candidate_list - list of similar candidates
162 */
163struct candidate_list {
164 struct rb_root rb_tree;
165 int count;
166 int maxcount;
167 int sort_by_ec;
168};
169
170/**
171 * struct gc_candidate - "candidate" segment to be garbage collected next
172 *
173 * @list: list (either free of low)
174 * @segno: segment number
175 * @valid: number of valid bytes
176 * @erase_count: erase count of segment
177 * @dist: distance from tree root
178 *
179 * Candidates can be on two lists. The free list contains electees rather
180 * than candidates - segments that no longer contain any valid data. The
181 * low list contains candidates to be picked for GC. It should be kept
182 * short. It is not required to always pick a perfect candidate. In the
183 * worst case GC will have to move more data than absolutely necessary.
184 */
185struct gc_candidate {
186 struct rb_node rb_node;
187 struct candidate_list *list;
188 u32 segno;
189 u32 valid;
190 u32 erase_count;
191 u8 dist;
192};
193
194/**
195 * struct logfs_journal_entry - temporary structure used during journal scan
196 *
197 * @used:
198 * @version: normalized version
199 * @len: length
200 * @offset: offset
201 */
202struct logfs_journal_entry {
203 int used;
204 s16 version;
205 u16 len;
206 u16 datalen;
207 u64 offset;
208};
209
210enum transaction_state {
211 CREATE_1 = 1,
212 CREATE_2,
213 UNLINK_1,
214 UNLINK_2,
215 CROSS_RENAME_1,
216 CROSS_RENAME_2,
217 TARGET_RENAME_1,
218 TARGET_RENAME_2,
219 TARGET_RENAME_3
220};
221
222/**
223 * struct logfs_transaction - essential fields to support atomic dirops
224 *
225 * @ino: target inode
226 * @dir: inode of directory containing dentry
227 * @pos: pos of dentry in directory
228 */
229struct logfs_transaction {
230 enum transaction_state state;
231 u64 ino;
232 u64 dir;
233 u64 pos;
234};
235
236/**
237 * struct logfs_shadow - old block in the shadow of a not-yet-committed new one
238 * @old_ofs: offset of old block on medium
239 * @new_ofs: offset of new block on medium
240 * @ino: inode number
241 * @bix: block index
242 * @old_len: size of old block, including header
243 * @new_len: size of new block, including header
244 * @level: block level
245 */
246struct logfs_shadow {
247 u64 old_ofs;
248 u64 new_ofs;
249 u64 ino;
250 u64 bix;
251 int old_len;
252 int new_len;
253 gc_level_t gc_level;
254};
255
256/**
257 * struct shadow_tree
258 * @new: shadows where old_ofs==0, indexed by new_ofs
259 * @old: shadows where old_ofs!=0, indexed by old_ofs
Joern Engel032d8f72010-04-13 17:46:37 +0200260 * @segment_map: bitfield of segments containing shadows
261 * @no_shadowed_segment: number of segments containing shadows
Joern Engel5db53f32009-11-20 20:13:39 +0100262 */
263struct shadow_tree {
264 struct btree_head64 new;
265 struct btree_head64 old;
Joern Engel032d8f72010-04-13 17:46:37 +0200266 struct btree_head32 segment_map;
267 int no_shadowed_segments;
Joern Engel5db53f32009-11-20 20:13:39 +0100268};
269
270struct object_alias_item {
271 struct list_head list;
272 __be64 val;
273 int child_no;
274};
275
276/**
277 * struct logfs_block - contains any block state
278 * @type: indirect block or inode
279 * @full: number of fully populated children
280 * @partial: number of partially populated children
281 *
282 * Most blocks are directly represented by page cache pages. But when a block
283 * becomes dirty, is part of a transaction, contains aliases or is otherwise
284 * special, a struct logfs_block is allocated to track the additional state.
285 * Inodes are very similar to indirect blocks, so they can also get one of
286 * these structures added when appropriate.
287 */
288#define BLOCK_INDIRECT 1 /* Indirect block */
289#define BLOCK_INODE 2 /* Inode */
290struct logfs_block_ops;
291struct logfs_block {
292 struct list_head alias_list;
293 struct list_head item_list;
294 struct super_block *sb;
295 u64 ino;
296 u64 bix;
297 level_t level;
298 struct page *page;
299 struct inode *inode;
300 struct logfs_transaction *ta;
301 unsigned long alias_map[LOGFS_BLOCK_FACTOR / BITS_PER_LONG];
302 struct logfs_block_ops *ops;
303 int full;
304 int partial;
305 int reserved_bytes;
306};
307
308typedef int write_alias_t(struct super_block *sb, u64 ino, u64 bix,
309 level_t level, int child_no, __be64 val);
310struct logfs_block_ops {
311 void (*write_block)(struct logfs_block *block);
Joern Engel5db53f32009-11-20 20:13:39 +0100312 void (*free_block)(struct super_block *sb, struct logfs_block*block);
313 int (*write_alias)(struct super_block *sb,
314 struct logfs_block *block,
315 write_alias_t *write_one_alias);
316};
317
Joern Engel032d8f72010-04-13 17:46:37 +0200318#define MAX_JOURNAL_ENTRIES 256
319
Joern Engel5db53f32009-11-20 20:13:39 +0100320struct logfs_super {
321 struct mtd_info *s_mtd; /* underlying device */
322 struct block_device *s_bdev; /* underlying device */
323 const struct logfs_device_ops *s_devops;/* device access */
324 struct inode *s_master_inode; /* inode file */
325 struct inode *s_segfile_inode; /* segment file */
326 struct inode *s_mapping_inode; /* device mapping */
327 atomic_t s_pending_writes; /* outstanting bios */
328 long s_flags;
329 mempool_t *s_btree_pool; /* for btree nodes */
330 mempool_t *s_alias_pool; /* aliases in segment.c */
331 u64 s_feature_incompat;
332 u64 s_feature_ro_compat;
333 u64 s_feature_compat;
334 u64 s_feature_flags;
335 u64 s_sb_ofs[2];
Joern Engel94215022010-03-04 21:30:58 +0100336 struct page *s_erase_page; /* for dev_bdev.c */
Joern Engel5db53f32009-11-20 20:13:39 +0100337 /* alias.c fields */
338 struct btree_head32 s_segment_alias; /* remapped segments */
339 int s_no_object_aliases;
340 struct list_head s_object_alias; /* remapped objects */
341 struct btree_head128 s_object_alias_tree; /* remapped objects */
342 struct mutex s_object_alias_mutex;
343 /* dir.c fields */
344 struct mutex s_dirop_mutex; /* for creat/unlink/rename */
345 u64 s_victim_ino; /* used for atomic dir-ops */
346 u64 s_rename_dir; /* source directory ino */
347 u64 s_rename_pos; /* position of source dd */
348 /* gc.c fields */
349 long s_segsize; /* size of a segment */
350 int s_segshift; /* log2 of segment size */
351 long s_segmask; /* 1 << s_segshift - 1 */
352 long s_no_segs; /* segments on device */
353 long s_no_journal_segs; /* segments used for journal */
354 long s_no_blocks; /* blocks per segment */
355 long s_writesize; /* minimum write size */
356 int s_writeshift; /* log2 of write size */
357 u64 s_size; /* filesystem size */
358 struct logfs_area *s_area[LOGFS_NO_AREAS]; /* open segment array */
359 u64 s_gec; /* global erase count */
360 u64 s_wl_gec_ostore; /* time of last wl event */
361 u64 s_wl_gec_journal; /* time of last wl event */
362 u64 s_sweeper; /* current sweeper pos */
363 u8 s_ifile_levels; /* max level of ifile */
364 u8 s_iblock_levels; /* max level of regular files */
365 u8 s_data_levels; /* # of segments to leaf block*/
366 u8 s_total_levels; /* sum of above three */
367 struct btree_head32 s_cand_tree; /* all candidates */
368 struct candidate_list s_free_list; /* 100% free segments */
369 struct candidate_list s_reserve_list; /* Bad segment reserve */
370 struct candidate_list s_low_list[LOGFS_NO_AREAS];/* good candidates */
371 struct candidate_list s_ec_list; /* wear level candidates */
372 struct btree_head32 s_reserved_segments;/* sb, journal, bad, etc. */
373 /* inode.c fields */
374 u64 s_last_ino; /* highest ino used */
375 long s_inos_till_wrap;
376 u32 s_generation; /* i_generation for new files */
377 struct list_head s_freeing_list; /* inodes being freed */
378 /* journal.c fields */
379 struct mutex s_journal_mutex;
380 void *s_je; /* journal entry to compress */
381 void *s_compressed_je; /* block to write to journal */
382 u32 s_journal_seg[LOGFS_JOURNAL_SEGS]; /* journal segments */
383 u32 s_journal_ec[LOGFS_JOURNAL_SEGS]; /* journal erasecounts */
384 u64 s_last_version;
385 struct logfs_area *s_journal_area; /* open journal segment */
Joern Engel032d8f72010-04-13 17:46:37 +0200386 __be64 s_je_array[MAX_JOURNAL_ENTRIES];
Joern Engel5db53f32009-11-20 20:13:39 +0100387 int s_no_je;
388
389 int s_sum_index; /* for the 12 summaries */
390 struct shadow_tree s_shadow_tree;
391 int s_je_fill; /* index of current je */
392 /* readwrite.c fields */
393 struct mutex s_write_mutex;
394 int s_lock_count;
395 mempool_t *s_block_pool; /* struct logfs_block pool */
396 mempool_t *s_shadow_pool; /* struct logfs_shadow pool */
397 /*
398 * Space accounting:
399 * - s_used_bytes specifies space used to store valid data objects.
400 * - s_dirty_used_bytes is space used to store non-committed data
401 * objects. Those objects have already been written themselves,
402 * but they don't become valid until all indirect blocks up to the
403 * journal have been written as well.
404 * - s_dirty_free_bytes is space used to store the old copy of a
405 * replaced object, as long as the replacement is non-committed.
406 * In other words, it is the amount of space freed when all dirty
407 * blocks are written back.
408 * - s_free_bytes is the amount of free space available for any
409 * purpose.
410 * - s_root_reserve is the amount of free space available only to
411 * the root user. Non-privileged users can no longer write once
412 * this watermark has been reached.
413 * - s_speed_reserve is space which remains unused to speed up
414 * garbage collection performance.
415 * - s_dirty_pages is the space reserved for currently dirty pages.
416 * It is a pessimistic estimate, so some/most will get freed on
417 * page writeback.
418 *
419 * s_used_bytes + s_free_bytes + s_speed_reserve = total usable size
420 */
421 u64 s_free_bytes;
422 u64 s_used_bytes;
423 u64 s_dirty_free_bytes;
424 u64 s_dirty_used_bytes;
425 u64 s_root_reserve;
426 u64 s_speed_reserve;
427 u64 s_dirty_pages;
428 /* Bad block handling:
429 * - s_bad_seg_reserve is a number of segments usually kept
430 * free. When encountering bad blocks, the affected segment's data
431 * is _temporarily_ moved to a reserved segment.
432 * - s_bad_segments is the number of known bad segments.
433 */
434 u32 s_bad_seg_reserve;
435 u32 s_bad_segments;
436};
437
438/**
439 * struct logfs_inode - in-memory inode
440 *
441 * @vfs_inode: struct inode
442 * @li_data: data pointers
443 * @li_used_bytes: number of used bytes
444 * @li_freeing_list: used to track inodes currently being freed
445 * @li_flags: inode flags
446 * @li_refcount: number of internal (GC-induced) references
447 */
448struct logfs_inode {
449 struct inode vfs_inode;
450 u64 li_data[LOGFS_EMBEDDED_FIELDS];
451 u64 li_used_bytes;
452 struct list_head li_freeing_list;
453 struct logfs_block *li_block;
454 u32 li_flags;
455 u8 li_height;
456 int li_refcount;
457};
458
459#define journal_for_each(__i) for (__i = 0; __i < LOGFS_JOURNAL_SEGS; __i++)
460#define for_each_area(__i) for (__i = 0; __i < LOGFS_NO_AREAS; __i++)
461#define for_each_area_down(__i) for (__i = LOGFS_NO_AREAS - 1; __i >= 0; __i--)
462
463/* compr.c */
464int logfs_compress(void *in, void *out, size_t inlen, size_t outlen);
465int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen);
466int __init logfs_compr_init(void);
467void logfs_compr_exit(void);
468
469/* dev_bdev.c */
470#ifdef CONFIG_BLOCK
471int logfs_get_sb_bdev(struct file_system_type *type, int flags,
472 const char *devname, struct vfsmount *mnt);
473#else
474static inline int logfs_get_sb_bdev(struct file_system_type *type, int flags,
475 const char *devname, struct vfsmount *mnt)
476{
477 return -ENODEV;
478}
479#endif
480
481/* dev_mtd.c */
482#ifdef CONFIG_MTD
483int logfs_get_sb_mtd(struct file_system_type *type, int flags,
484 int mtdnr, struct vfsmount *mnt);
485#else
486static inline int logfs_get_sb_mtd(struct file_system_type *type, int flags,
487 int mtdnr, struct vfsmount *mnt)
488{
489 return -ENODEV;
490}
491#endif
492
493/* dir.c */
494extern const struct inode_operations logfs_symlink_iops;
495extern const struct inode_operations logfs_dir_iops;
496extern const struct file_operations logfs_dir_fops;
497int logfs_replay_journal(struct super_block *sb);
498
499/* file.c */
500extern const struct inode_operations logfs_reg_iops;
501extern const struct file_operations logfs_reg_fops;
502extern const struct address_space_operations logfs_reg_aops;
503int logfs_readpage(struct file *file, struct page *page);
504int logfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
505 unsigned long arg);
506int logfs_fsync(struct file *file, struct dentry *dentry, int datasync);
507
508/* gc.c */
509u32 get_best_cand(struct super_block *sb, struct candidate_list *list, u32 *ec);
510void logfs_gc_pass(struct super_block *sb);
511int logfs_check_areas(struct super_block *sb);
512int logfs_init_gc(struct super_block *sb);
513void logfs_cleanup_gc(struct super_block *sb);
514
515/* inode.c */
516extern const struct super_operations logfs_super_operations;
517struct inode *logfs_iget(struct super_block *sb, ino_t ino);
518struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *cookie);
519void logfs_safe_iput(struct inode *inode, int cookie);
520struct inode *logfs_new_inode(struct inode *dir, int mode);
521struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino);
522struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino);
523int logfs_init_inode_cache(void);
524void logfs_destroy_inode_cache(void);
525void destroy_meta_inode(struct inode *inode);
526void logfs_set_blocks(struct inode *inode, u64 no);
527/* these logically belong into inode.c but actually reside in readwrite.c */
528int logfs_read_inode(struct inode *inode);
529int __logfs_write_inode(struct inode *inode, long flags);
530void logfs_delete_inode(struct inode *inode);
531void logfs_clear_inode(struct inode *inode);
532
533/* journal.c */
Joern Engelc6d38302010-03-04 21:36:19 +0100534void logfs_write_anchor(struct super_block *sb);
Joern Engel5db53f32009-11-20 20:13:39 +0100535int logfs_init_journal(struct super_block *sb);
536void logfs_cleanup_journal(struct super_block *sb);
537int write_alias_journal(struct super_block *sb, u64 ino, u64 bix,
538 level_t level, int child_no, __be64 val);
539void do_logfs_journal_wl_pass(struct super_block *sb);
540
541/* readwrite.c */
542pgoff_t logfs_pack_index(u64 bix, level_t level);
543void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level);
544int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
545 loff_t bix, long flags, struct shadow_tree *shadow_tree);
546int logfs_readpage_nolock(struct page *page);
547int logfs_write_buf(struct inode *inode, struct page *page, long flags);
548int logfs_delete(struct inode *inode, pgoff_t index,
549 struct shadow_tree *shadow_tree);
550int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
551 gc_level_t gc_level, long flags);
552int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
553 gc_level_t gc_level);
554int logfs_truncate(struct inode *inode, u64 size);
555u64 logfs_seek_hole(struct inode *inode, u64 bix);
556u64 logfs_seek_data(struct inode *inode, u64 bix);
557int logfs_open_segfile(struct super_block *sb);
558int logfs_init_rw(struct super_block *sb);
559void logfs_cleanup_rw(struct super_block *sb);
560void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta);
561void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta);
562void logfs_write_block(struct logfs_block *block, long flags);
563int logfs_write_obj_aliases_pagecache(struct super_block *sb);
564void logfs_get_segment_entry(struct super_block *sb, u32 segno,
565 struct logfs_segment_entry *se);
566void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment);
567void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
568 gc_level_t gc_level);
569void logfs_set_segment_reserved(struct super_block *sb, u32 segno);
570void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec);
571struct logfs_block *__alloc_block(struct super_block *sb,
572 u64 ino, u64 bix, level_t level);
573void __free_block(struct super_block *sb, struct logfs_block *block);
574void btree_write_block(struct logfs_block *block);
575void initialize_block_counters(struct page *page, struct logfs_block *block,
576 __be64 *array, int page_is_empty);
577int logfs_exist_block(struct inode *inode, u64 bix);
578int get_page_reserve(struct inode *inode, struct page *page);
579extern struct logfs_block_ops indirect_block_ops;
580
581/* segment.c */
Joern Engel94215022010-03-04 21:30:58 +0100582int logfs_erase_segment(struct super_block *sb, u32 ofs, int ensure_erase);
Joern Engel5db53f32009-11-20 20:13:39 +0100583int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf);
584int logfs_segment_read(struct inode *inode, struct page *page, u64 ofs, u64 bix,
585 level_t level);
586int logfs_segment_write(struct inode *inode, struct page *page,
587 struct logfs_shadow *shadow);
588int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow);
589int logfs_load_object_aliases(struct super_block *sb,
590 struct logfs_obj_alias *oa, int count);
591void move_page_to_btree(struct page *page);
592int logfs_init_mapping(struct super_block *sb);
593void logfs_sync_area(struct logfs_area *area);
594void logfs_sync_segments(struct super_block *sb);
Joern Engel723b2ff2010-03-28 18:10:07 +0200595void freeseg(struct super_block *sb, u32 segno);
Joern Engel5db53f32009-11-20 20:13:39 +0100596
597/* area handling */
598int logfs_init_areas(struct super_block *sb);
599void logfs_cleanup_areas(struct super_block *sb);
600int logfs_open_area(struct logfs_area *area, size_t bytes);
601void __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len,
602 int use_filler);
603
604static inline void logfs_buf_write(struct logfs_area *area, u64 ofs,
605 void *buf, size_t len)
606{
607 __logfs_buf_write(area, ofs, buf, len, 0);
608}
609
610static inline void logfs_buf_recover(struct logfs_area *area, u64 ofs,
611 void *buf, size_t len)
612{
613 __logfs_buf_write(area, ofs, buf, len, 1);
614}
615
616/* super.c */
617struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index);
618void emergency_read_end(struct page *page);
619void logfs_crash_dump(struct super_block *sb);
620void *memchr_inv(const void *s, int c, size_t n);
621int logfs_statfs(struct dentry *dentry, struct kstatfs *stats);
622int logfs_get_sb_device(struct file_system_type *type, int flags,
623 struct mtd_info *mtd, struct block_device *bdev,
624 const struct logfs_device_ops *devops, struct vfsmount *mnt);
625int logfs_check_ds(struct logfs_disk_super *ds);
626int logfs_write_sb(struct super_block *sb);
627
628static inline struct logfs_super *logfs_super(struct super_block *sb)
629{
630 return sb->s_fs_info;
631}
632
633static inline struct logfs_inode *logfs_inode(struct inode *inode)
634{
635 return container_of(inode, struct logfs_inode, vfs_inode);
636}
637
638static inline void logfs_set_ro(struct super_block *sb)
639{
640 logfs_super(sb)->s_flags |= LOGFS_SB_FLAG_RO;
641}
642
643#define LOGFS_BUG(sb) do { \
644 struct super_block *__sb = sb; \
645 logfs_crash_dump(__sb); \
646 logfs_super(__sb)->s_flags |= LOGFS_SB_FLAG_RO; \
647 BUG(); \
648} while (0)
649
650#define LOGFS_BUG_ON(condition, sb) \
651 do { if (unlikely(condition)) LOGFS_BUG((sb)); } while (0)
652
653static inline __be32 logfs_crc32(void *data, size_t len, size_t skip)
654{
655 return cpu_to_be32(crc32(~0, data+skip, len-skip));
656}
657
658static inline u8 logfs_type(struct inode *inode)
659{
660 return (inode->i_mode >> 12) & 15;
661}
662
663static inline pgoff_t logfs_index(struct super_block *sb, u64 pos)
664{
665 return pos >> sb->s_blocksize_bits;
666}
667
668static inline u64 dev_ofs(struct super_block *sb, u32 segno, u32 ofs)
669{
670 return ((u64)segno << logfs_super(sb)->s_segshift) + ofs;
671}
672
673static inline u32 seg_no(struct super_block *sb, u64 ofs)
674{
675 return ofs >> logfs_super(sb)->s_segshift;
676}
677
678static inline u32 seg_ofs(struct super_block *sb, u64 ofs)
679{
680 return ofs & logfs_super(sb)->s_segmask;
681}
682
683static inline u64 seg_align(struct super_block *sb, u64 ofs)
684{
685 return ofs & ~logfs_super(sb)->s_segmask;
686}
687
688static inline struct logfs_block *logfs_block(struct page *page)
689{
690 return (void *)page->private;
691}
692
693static inline level_t shrink_level(gc_level_t __level)
694{
695 u8 level = (__force u8)__level;
696
697 if (level >= LOGFS_MAX_LEVELS)
698 level -= LOGFS_MAX_LEVELS;
699 return (__force level_t)level;
700}
701
702static inline gc_level_t expand_level(u64 ino, level_t __level)
703{
704 u8 level = (__force u8)__level;
705
706 if (ino == LOGFS_INO_MASTER) {
Anand Gadiyara8cd4562010-05-10 14:51:19 +0530707 /* ifile has separate areas */
Joern Engel5db53f32009-11-20 20:13:39 +0100708 level += LOGFS_MAX_LEVELS;
709 }
710 return (__force gc_level_t)level;
711}
712
713static inline int logfs_block_shift(struct super_block *sb, level_t level)
714{
715 level = shrink_level((__force gc_level_t)level);
716 return (__force int)level * (sb->s_blocksize_bits - 3);
717}
718
719static inline u64 logfs_block_mask(struct super_block *sb, level_t level)
720{
721 return ~0ull << logfs_block_shift(sb, level);
722}
723
724static inline struct logfs_area *get_area(struct super_block *sb,
725 gc_level_t gc_level)
726{
727 return logfs_super(sb)->s_area[(__force u8)gc_level];
728}
729
Joern Engel1f1b0002010-04-15 08:03:57 +0200730static inline void logfs_mempool_destroy(mempool_t *pool)
731{
732 if (pool)
733 mempool_destroy(pool);
734}
735
Joern Engel5db53f32009-11-20 20:13:39 +0100736#endif