blob: 5e3b720779516d8ec6d8c74794c57c861a5436d5 [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
Joern Engel6f485b42010-05-07 19:38:40 +0200147 * @can_write_buf: decide whether wbuf can be written to ofs
Joern Engel5db53f32009-11-20 20:13:39 +0100148 */
149struct logfs_device_ops {
150 struct page *(*find_first_sb)(struct super_block *sb, u64 *ofs);
151 struct page *(*find_last_sb)(struct super_block *sb, u64 *ofs);
152 int (*write_sb)(struct super_block *sb, struct page *page);
153 int (*readpage)(void *_sb, struct page *page);
154 void (*writeseg)(struct super_block *sb, u64 ofs, size_t len);
Joern Engel94215022010-03-04 21:30:58 +0100155 int (*erase)(struct super_block *sb, loff_t ofs, size_t len,
156 int ensure_write);
Joern Engel6f485b42010-05-07 19:38:40 +0200157 int (*can_write_buf)(struct super_block *sb, u64 ofs);
Joern Engel5db53f32009-11-20 20:13:39 +0100158 void (*sync)(struct super_block *sb);
159 void (*put_device)(struct super_block *sb);
160};
161
162/**
163 * struct candidate_list - list of similar candidates
164 */
165struct candidate_list {
166 struct rb_root rb_tree;
167 int count;
168 int maxcount;
169 int sort_by_ec;
170};
171
172/**
173 * struct gc_candidate - "candidate" segment to be garbage collected next
174 *
175 * @list: list (either free of low)
176 * @segno: segment number
177 * @valid: number of valid bytes
178 * @erase_count: erase count of segment
179 * @dist: distance from tree root
180 *
181 * Candidates can be on two lists. The free list contains electees rather
182 * than candidates - segments that no longer contain any valid data. The
183 * low list contains candidates to be picked for GC. It should be kept
184 * short. It is not required to always pick a perfect candidate. In the
185 * worst case GC will have to move more data than absolutely necessary.
186 */
187struct gc_candidate {
188 struct rb_node rb_node;
189 struct candidate_list *list;
190 u32 segno;
191 u32 valid;
192 u32 erase_count;
193 u8 dist;
194};
195
196/**
197 * struct logfs_journal_entry - temporary structure used during journal scan
198 *
199 * @used:
200 * @version: normalized version
201 * @len: length
202 * @offset: offset
203 */
204struct logfs_journal_entry {
205 int used;
206 s16 version;
207 u16 len;
208 u16 datalen;
209 u64 offset;
210};
211
212enum transaction_state {
213 CREATE_1 = 1,
214 CREATE_2,
215 UNLINK_1,
216 UNLINK_2,
217 CROSS_RENAME_1,
218 CROSS_RENAME_2,
219 TARGET_RENAME_1,
220 TARGET_RENAME_2,
221 TARGET_RENAME_3
222};
223
224/**
225 * struct logfs_transaction - essential fields to support atomic dirops
226 *
227 * @ino: target inode
228 * @dir: inode of directory containing dentry
229 * @pos: pos of dentry in directory
230 */
231struct logfs_transaction {
232 enum transaction_state state;
233 u64 ino;
234 u64 dir;
235 u64 pos;
236};
237
238/**
239 * struct logfs_shadow - old block in the shadow of a not-yet-committed new one
240 * @old_ofs: offset of old block on medium
241 * @new_ofs: offset of new block on medium
242 * @ino: inode number
243 * @bix: block index
244 * @old_len: size of old block, including header
245 * @new_len: size of new block, including header
246 * @level: block level
247 */
248struct logfs_shadow {
249 u64 old_ofs;
250 u64 new_ofs;
251 u64 ino;
252 u64 bix;
253 int old_len;
254 int new_len;
255 gc_level_t gc_level;
256};
257
258/**
259 * struct shadow_tree
260 * @new: shadows where old_ofs==0, indexed by new_ofs
261 * @old: shadows where old_ofs!=0, indexed by old_ofs
Joern Engel032d8f72010-04-13 17:46:37 +0200262 * @segment_map: bitfield of segments containing shadows
263 * @no_shadowed_segment: number of segments containing shadows
Joern Engel5db53f32009-11-20 20:13:39 +0100264 */
265struct shadow_tree {
266 struct btree_head64 new;
267 struct btree_head64 old;
Joern Engel032d8f72010-04-13 17:46:37 +0200268 struct btree_head32 segment_map;
269 int no_shadowed_segments;
Joern Engel5db53f32009-11-20 20:13:39 +0100270};
271
272struct object_alias_item {
273 struct list_head list;
274 __be64 val;
275 int child_no;
276};
277
278/**
279 * struct logfs_block - contains any block state
280 * @type: indirect block or inode
281 * @full: number of fully populated children
282 * @partial: number of partially populated children
283 *
284 * Most blocks are directly represented by page cache pages. But when a block
285 * becomes dirty, is part of a transaction, contains aliases or is otherwise
286 * special, a struct logfs_block is allocated to track the additional state.
287 * Inodes are very similar to indirect blocks, so they can also get one of
288 * these structures added when appropriate.
289 */
290#define BLOCK_INDIRECT 1 /* Indirect block */
291#define BLOCK_INODE 2 /* Inode */
292struct logfs_block_ops;
293struct logfs_block {
294 struct list_head alias_list;
295 struct list_head item_list;
296 struct super_block *sb;
297 u64 ino;
298 u64 bix;
299 level_t level;
300 struct page *page;
301 struct inode *inode;
302 struct logfs_transaction *ta;
303 unsigned long alias_map[LOGFS_BLOCK_FACTOR / BITS_PER_LONG];
304 struct logfs_block_ops *ops;
305 int full;
306 int partial;
307 int reserved_bytes;
308};
309
310typedef int write_alias_t(struct super_block *sb, u64 ino, u64 bix,
311 level_t level, int child_no, __be64 val);
312struct logfs_block_ops {
313 void (*write_block)(struct logfs_block *block);
Joern Engel5db53f32009-11-20 20:13:39 +0100314 void (*free_block)(struct super_block *sb, struct logfs_block*block);
315 int (*write_alias)(struct super_block *sb,
316 struct logfs_block *block,
317 write_alias_t *write_one_alias);
318};
319
Joern Engel032d8f72010-04-13 17:46:37 +0200320#define MAX_JOURNAL_ENTRIES 256
321
Joern Engel5db53f32009-11-20 20:13:39 +0100322struct logfs_super {
323 struct mtd_info *s_mtd; /* underlying device */
324 struct block_device *s_bdev; /* underlying device */
325 const struct logfs_device_ops *s_devops;/* device access */
326 struct inode *s_master_inode; /* inode file */
327 struct inode *s_segfile_inode; /* segment file */
328 struct inode *s_mapping_inode; /* device mapping */
329 atomic_t s_pending_writes; /* outstanting bios */
330 long s_flags;
331 mempool_t *s_btree_pool; /* for btree nodes */
332 mempool_t *s_alias_pool; /* aliases in segment.c */
333 u64 s_feature_incompat;
334 u64 s_feature_ro_compat;
335 u64 s_feature_compat;
336 u64 s_feature_flags;
337 u64 s_sb_ofs[2];
Joern Engel94215022010-03-04 21:30:58 +0100338 struct page *s_erase_page; /* for dev_bdev.c */
Joern Engel5db53f32009-11-20 20:13:39 +0100339 /* alias.c fields */
340 struct btree_head32 s_segment_alias; /* remapped segments */
341 int s_no_object_aliases;
342 struct list_head s_object_alias; /* remapped objects */
343 struct btree_head128 s_object_alias_tree; /* remapped objects */
344 struct mutex s_object_alias_mutex;
345 /* dir.c fields */
346 struct mutex s_dirop_mutex; /* for creat/unlink/rename */
347 u64 s_victim_ino; /* used for atomic dir-ops */
348 u64 s_rename_dir; /* source directory ino */
349 u64 s_rename_pos; /* position of source dd */
350 /* gc.c fields */
351 long s_segsize; /* size of a segment */
352 int s_segshift; /* log2 of segment size */
353 long s_segmask; /* 1 << s_segshift - 1 */
354 long s_no_segs; /* segments on device */
355 long s_no_journal_segs; /* segments used for journal */
356 long s_no_blocks; /* blocks per segment */
357 long s_writesize; /* minimum write size */
358 int s_writeshift; /* log2 of write size */
359 u64 s_size; /* filesystem size */
360 struct logfs_area *s_area[LOGFS_NO_AREAS]; /* open segment array */
361 u64 s_gec; /* global erase count */
362 u64 s_wl_gec_ostore; /* time of last wl event */
363 u64 s_wl_gec_journal; /* time of last wl event */
364 u64 s_sweeper; /* current sweeper pos */
365 u8 s_ifile_levels; /* max level of ifile */
366 u8 s_iblock_levels; /* max level of regular files */
367 u8 s_data_levels; /* # of segments to leaf block*/
368 u8 s_total_levels; /* sum of above three */
369 struct btree_head32 s_cand_tree; /* all candidates */
370 struct candidate_list s_free_list; /* 100% free segments */
371 struct candidate_list s_reserve_list; /* Bad segment reserve */
372 struct candidate_list s_low_list[LOGFS_NO_AREAS];/* good candidates */
373 struct candidate_list s_ec_list; /* wear level candidates */
374 struct btree_head32 s_reserved_segments;/* sb, journal, bad, etc. */
375 /* inode.c fields */
376 u64 s_last_ino; /* highest ino used */
377 long s_inos_till_wrap;
378 u32 s_generation; /* i_generation for new files */
379 struct list_head s_freeing_list; /* inodes being freed */
380 /* journal.c fields */
381 struct mutex s_journal_mutex;
382 void *s_je; /* journal entry to compress */
383 void *s_compressed_je; /* block to write to journal */
384 u32 s_journal_seg[LOGFS_JOURNAL_SEGS]; /* journal segments */
385 u32 s_journal_ec[LOGFS_JOURNAL_SEGS]; /* journal erasecounts */
386 u64 s_last_version;
387 struct logfs_area *s_journal_area; /* open journal segment */
Joern Engel032d8f72010-04-13 17:46:37 +0200388 __be64 s_je_array[MAX_JOURNAL_ENTRIES];
Joern Engel5db53f32009-11-20 20:13:39 +0100389 int s_no_je;
390
391 int s_sum_index; /* for the 12 summaries */
392 struct shadow_tree s_shadow_tree;
393 int s_je_fill; /* index of current je */
394 /* readwrite.c fields */
395 struct mutex s_write_mutex;
396 int s_lock_count;
397 mempool_t *s_block_pool; /* struct logfs_block pool */
398 mempool_t *s_shadow_pool; /* struct logfs_shadow pool */
Joern Engel05ebad82010-05-04 19:41:09 +0200399 struct list_head s_writeback_list; /* writeback pages */
Joern Engel5db53f32009-11-20 20:13:39 +0100400 /*
401 * Space accounting:
402 * - s_used_bytes specifies space used to store valid data objects.
403 * - s_dirty_used_bytes is space used to store non-committed data
404 * objects. Those objects have already been written themselves,
405 * but they don't become valid until all indirect blocks up to the
406 * journal have been written as well.
407 * - s_dirty_free_bytes is space used to store the old copy of a
408 * replaced object, as long as the replacement is non-committed.
409 * In other words, it is the amount of space freed when all dirty
410 * blocks are written back.
411 * - s_free_bytes is the amount of free space available for any
412 * purpose.
413 * - s_root_reserve is the amount of free space available only to
414 * the root user. Non-privileged users can no longer write once
415 * this watermark has been reached.
416 * - s_speed_reserve is space which remains unused to speed up
417 * garbage collection performance.
418 * - s_dirty_pages is the space reserved for currently dirty pages.
419 * It is a pessimistic estimate, so some/most will get freed on
420 * page writeback.
421 *
422 * s_used_bytes + s_free_bytes + s_speed_reserve = total usable size
423 */
424 u64 s_free_bytes;
425 u64 s_used_bytes;
426 u64 s_dirty_free_bytes;
427 u64 s_dirty_used_bytes;
428 u64 s_root_reserve;
429 u64 s_speed_reserve;
430 u64 s_dirty_pages;
431 /* Bad block handling:
432 * - s_bad_seg_reserve is a number of segments usually kept
433 * free. When encountering bad blocks, the affected segment's data
434 * is _temporarily_ moved to a reserved segment.
435 * - s_bad_segments is the number of known bad segments.
436 */
437 u32 s_bad_seg_reserve;
438 u32 s_bad_segments;
439};
440
441/**
442 * struct logfs_inode - in-memory inode
443 *
444 * @vfs_inode: struct inode
445 * @li_data: data pointers
446 * @li_used_bytes: number of used bytes
447 * @li_freeing_list: used to track inodes currently being freed
448 * @li_flags: inode flags
449 * @li_refcount: number of internal (GC-induced) references
450 */
451struct logfs_inode {
452 struct inode vfs_inode;
453 u64 li_data[LOGFS_EMBEDDED_FIELDS];
454 u64 li_used_bytes;
455 struct list_head li_freeing_list;
456 struct logfs_block *li_block;
457 u32 li_flags;
458 u8 li_height;
459 int li_refcount;
460};
461
462#define journal_for_each(__i) for (__i = 0; __i < LOGFS_JOURNAL_SEGS; __i++)
463#define for_each_area(__i) for (__i = 0; __i < LOGFS_NO_AREAS; __i++)
464#define for_each_area_down(__i) for (__i = LOGFS_NO_AREAS - 1; __i >= 0; __i--)
465
466/* compr.c */
467int logfs_compress(void *in, void *out, size_t inlen, size_t outlen);
468int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen);
469int __init logfs_compr_init(void);
470void logfs_compr_exit(void);
471
472/* dev_bdev.c */
473#ifdef CONFIG_BLOCK
474int logfs_get_sb_bdev(struct file_system_type *type, int flags,
475 const char *devname, struct vfsmount *mnt);
476#else
477static inline int logfs_get_sb_bdev(struct file_system_type *type, int flags,
478 const char *devname, struct vfsmount *mnt)
479{
480 return -ENODEV;
481}
482#endif
483
484/* dev_mtd.c */
485#ifdef CONFIG_MTD
486int logfs_get_sb_mtd(struct file_system_type *type, int flags,
487 int mtdnr, struct vfsmount *mnt);
488#else
489static inline int logfs_get_sb_mtd(struct file_system_type *type, int flags,
490 int mtdnr, struct vfsmount *mnt)
491{
492 return -ENODEV;
493}
494#endif
495
496/* dir.c */
497extern const struct inode_operations logfs_symlink_iops;
498extern const struct inode_operations logfs_dir_iops;
499extern const struct file_operations logfs_dir_fops;
500int logfs_replay_journal(struct super_block *sb);
501
502/* file.c */
503extern const struct inode_operations logfs_reg_iops;
504extern const struct file_operations logfs_reg_fops;
505extern const struct address_space_operations logfs_reg_aops;
506int logfs_readpage(struct file *file, struct page *page);
507int logfs_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
508 unsigned long arg);
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200509int logfs_fsync(struct file *file, int datasync);
Joern Engel5db53f32009-11-20 20:13:39 +0100510
511/* gc.c */
512u32 get_best_cand(struct super_block *sb, struct candidate_list *list, u32 *ec);
513void logfs_gc_pass(struct super_block *sb);
514int logfs_check_areas(struct super_block *sb);
515int logfs_init_gc(struct super_block *sb);
516void logfs_cleanup_gc(struct super_block *sb);
517
518/* inode.c */
519extern const struct super_operations logfs_super_operations;
520struct inode *logfs_iget(struct super_block *sb, ino_t ino);
521struct inode *logfs_safe_iget(struct super_block *sb, ino_t ino, int *cookie);
522void logfs_safe_iput(struct inode *inode, int cookie);
523struct inode *logfs_new_inode(struct inode *dir, int mode);
524struct inode *logfs_new_meta_inode(struct super_block *sb, u64 ino);
525struct inode *logfs_read_meta_inode(struct super_block *sb, u64 ino);
526int logfs_init_inode_cache(void);
527void logfs_destroy_inode_cache(void);
Joern Engel5db53f32009-11-20 20:13:39 +0100528void logfs_set_blocks(struct inode *inode, u64 no);
529/* these logically belong into inode.c but actually reside in readwrite.c */
530int logfs_read_inode(struct inode *inode);
531int __logfs_write_inode(struct inode *inode, long flags);
Al Viro7da08fd2010-06-07 13:11:34 -0400532void logfs_evict_inode(struct inode *inode);
Joern Engel5db53f32009-11-20 20:13:39 +0100533
534/* journal.c */
Joern Engelc6d38302010-03-04 21:36:19 +0100535void logfs_write_anchor(struct super_block *sb);
Joern Engel5db53f32009-11-20 20:13:39 +0100536int logfs_init_journal(struct super_block *sb);
537void logfs_cleanup_journal(struct super_block *sb);
538int write_alias_journal(struct super_block *sb, u64 ino, u64 bix,
539 level_t level, int child_no, __be64 val);
540void do_logfs_journal_wl_pass(struct super_block *sb);
541
542/* readwrite.c */
543pgoff_t logfs_pack_index(u64 bix, level_t level);
544void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level);
545int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
546 loff_t bix, long flags, struct shadow_tree *shadow_tree);
547int logfs_readpage_nolock(struct page *page);
548int logfs_write_buf(struct inode *inode, struct page *page, long flags);
549int logfs_delete(struct inode *inode, pgoff_t index,
550 struct shadow_tree *shadow_tree);
551int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
552 gc_level_t gc_level, long flags);
553int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
554 gc_level_t gc_level);
555int logfs_truncate(struct inode *inode, u64 size);
556u64 logfs_seek_hole(struct inode *inode, u64 bix);
557u64 logfs_seek_data(struct inode *inode, u64 bix);
558int logfs_open_segfile(struct super_block *sb);
559int logfs_init_rw(struct super_block *sb);
560void logfs_cleanup_rw(struct super_block *sb);
561void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta);
562void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta);
563void logfs_write_block(struct logfs_block *block, long flags);
564int logfs_write_obj_aliases_pagecache(struct super_block *sb);
565void logfs_get_segment_entry(struct super_block *sb, u32 segno,
566 struct logfs_segment_entry *se);
567void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment);
568void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
569 gc_level_t gc_level);
570void logfs_set_segment_reserved(struct super_block *sb, u32 segno);
571void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec);
572struct logfs_block *__alloc_block(struct super_block *sb,
573 u64 ino, u64 bix, level_t level);
574void __free_block(struct super_block *sb, struct logfs_block *block);
575void btree_write_block(struct logfs_block *block);
576void initialize_block_counters(struct page *page, struct logfs_block *block,
577 __be64 *array, int page_is_empty);
578int logfs_exist_block(struct inode *inode, u64 bix);
579int get_page_reserve(struct inode *inode, struct page *page);
580extern struct logfs_block_ops indirect_block_ops;
581
582/* segment.c */
Joern Engel94215022010-03-04 21:30:58 +0100583int logfs_erase_segment(struct super_block *sb, u32 ofs, int ensure_erase);
Joern Engel5db53f32009-11-20 20:13:39 +0100584int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf);
585int logfs_segment_read(struct inode *inode, struct page *page, u64 ofs, u64 bix,
586 level_t level);
587int logfs_segment_write(struct inode *inode, struct page *page,
588 struct logfs_shadow *shadow);
589int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow);
590int logfs_load_object_aliases(struct super_block *sb,
591 struct logfs_obj_alias *oa, int count);
592void move_page_to_btree(struct page *page);
593int logfs_init_mapping(struct super_block *sb);
594void logfs_sync_area(struct logfs_area *area);
595void logfs_sync_segments(struct super_block *sb);
Joern Engel723b2ff2010-03-28 18:10:07 +0200596void freeseg(struct super_block *sb, u32 segno);
Joern Engel5db53f32009-11-20 20:13:39 +0100597
598/* area handling */
599int logfs_init_areas(struct super_block *sb);
600void logfs_cleanup_areas(struct super_block *sb);
601int logfs_open_area(struct logfs_area *area, size_t bytes);
Joern Engel20503662010-05-03 20:54:34 +0200602int __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len,
Joern Engel5db53f32009-11-20 20:13:39 +0100603 int use_filler);
604
Joern Engel20503662010-05-03 20:54:34 +0200605static inline int logfs_buf_write(struct logfs_area *area, u64 ofs,
Joern Engel5db53f32009-11-20 20:13:39 +0100606 void *buf, size_t len)
607{
Joern Engel20503662010-05-03 20:54:34 +0200608 return __logfs_buf_write(area, ofs, buf, len, 0);
Joern Engel5db53f32009-11-20 20:13:39 +0100609}
610
Joern Engel20503662010-05-03 20:54:34 +0200611static inline int logfs_buf_recover(struct logfs_area *area, u64 ofs,
Joern Engel5db53f32009-11-20 20:13:39 +0100612 void *buf, size_t len)
613{
Joern Engel20503662010-05-03 20:54:34 +0200614 return __logfs_buf_write(area, ofs, buf, len, 1);
Joern Engel5db53f32009-11-20 20:13:39 +0100615}
616
617/* super.c */
618struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index);
619void emergency_read_end(struct page *page);
620void logfs_crash_dump(struct super_block *sb);
621void *memchr_inv(const void *s, int c, size_t n);
622int logfs_statfs(struct dentry *dentry, struct kstatfs *stats);
623int logfs_get_sb_device(struct file_system_type *type, int flags,
624 struct mtd_info *mtd, struct block_device *bdev,
625 const struct logfs_device_ops *devops, struct vfsmount *mnt);
626int logfs_check_ds(struct logfs_disk_super *ds);
627int logfs_write_sb(struct super_block *sb);
628
629static inline struct logfs_super *logfs_super(struct super_block *sb)
630{
631 return sb->s_fs_info;
632}
633
634static inline struct logfs_inode *logfs_inode(struct inode *inode)
635{
636 return container_of(inode, struct logfs_inode, vfs_inode);
637}
638
639static inline void logfs_set_ro(struct super_block *sb)
640{
641 logfs_super(sb)->s_flags |= LOGFS_SB_FLAG_RO;
642}
643
644#define LOGFS_BUG(sb) do { \
645 struct super_block *__sb = sb; \
646 logfs_crash_dump(__sb); \
647 logfs_super(__sb)->s_flags |= LOGFS_SB_FLAG_RO; \
648 BUG(); \
649} while (0)
650
651#define LOGFS_BUG_ON(condition, sb) \
652 do { if (unlikely(condition)) LOGFS_BUG((sb)); } while (0)
653
654static inline __be32 logfs_crc32(void *data, size_t len, size_t skip)
655{
656 return cpu_to_be32(crc32(~0, data+skip, len-skip));
657}
658
659static inline u8 logfs_type(struct inode *inode)
660{
661 return (inode->i_mode >> 12) & 15;
662}
663
664static inline pgoff_t logfs_index(struct super_block *sb, u64 pos)
665{
666 return pos >> sb->s_blocksize_bits;
667}
668
669static inline u64 dev_ofs(struct super_block *sb, u32 segno, u32 ofs)
670{
671 return ((u64)segno << logfs_super(sb)->s_segshift) + ofs;
672}
673
674static inline u32 seg_no(struct super_block *sb, u64 ofs)
675{
676 return ofs >> logfs_super(sb)->s_segshift;
677}
678
679static inline u32 seg_ofs(struct super_block *sb, u64 ofs)
680{
681 return ofs & logfs_super(sb)->s_segmask;
682}
683
684static inline u64 seg_align(struct super_block *sb, u64 ofs)
685{
686 return ofs & ~logfs_super(sb)->s_segmask;
687}
688
689static inline struct logfs_block *logfs_block(struct page *page)
690{
691 return (void *)page->private;
692}
693
694static inline level_t shrink_level(gc_level_t __level)
695{
696 u8 level = (__force u8)__level;
697
698 if (level >= LOGFS_MAX_LEVELS)
699 level -= LOGFS_MAX_LEVELS;
700 return (__force level_t)level;
701}
702
703static inline gc_level_t expand_level(u64 ino, level_t __level)
704{
705 u8 level = (__force u8)__level;
706
707 if (ino == LOGFS_INO_MASTER) {
Anand Gadiyara8cd4562010-05-10 14:51:19 +0530708 /* ifile has separate areas */
Joern Engel5db53f32009-11-20 20:13:39 +0100709 level += LOGFS_MAX_LEVELS;
710 }
711 return (__force gc_level_t)level;
712}
713
714static inline int logfs_block_shift(struct super_block *sb, level_t level)
715{
716 level = shrink_level((__force gc_level_t)level);
717 return (__force int)level * (sb->s_blocksize_bits - 3);
718}
719
720static inline u64 logfs_block_mask(struct super_block *sb, level_t level)
721{
722 return ~0ull << logfs_block_shift(sb, level);
723}
724
725static inline struct logfs_area *get_area(struct super_block *sb,
726 gc_level_t gc_level)
727{
728 return logfs_super(sb)->s_area[(__force u8)gc_level];
729}
730
Joern Engel1f1b0002010-04-15 08:03:57 +0200731static inline void logfs_mempool_destroy(mempool_t *pool)
732{
733 if (pool)
734 mempool_destroy(pool);
735}
736
Joern Engel5db53f32009-11-20 20:13:39 +0100737#endif