1
2
3
4
5
6
7
8
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
41
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
81#define LOGFS_LINK_MAX (1<<30)
82
83
84#define LOGFS_SB_FLAG_RO 0x0001
85#define LOGFS_SB_FLAG_DIRTY 0x0002
86#define LOGFS_SB_FLAG_OBJ_ALIAS 0x0004
87#define LOGFS_SB_FLAG_SHUTDOWN 0x0008
88
89
90#define WF_LOCK 0x01
91#define WF_WRITE 0x02
92#define WF_DELETE 0x04
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
105
106
107
108
109
110
111
112
113
114
115struct logfs_area {
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
128
129
130
131
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
139struct logfs_super;
140
141
142
143
144
145
146
147
148
149
150struct logfs_device_ops {
151 struct page *(*find_first_sb)(struct super_block *sb, u64 *ofs);
152 struct page *(*find_last_sb)(struct super_block *sb, u64 *ofs);
153 int (*write_sb)(struct super_block *sb, struct page *page);
154 int (*readpage)(void *_sb, struct page *page);
155 void (*writeseg)(struct super_block *sb, u64 ofs, size_t len);
156 int (*erase)(struct super_block *sb, loff_t ofs, size_t len,
157 int ensure_write);
158 int (*can_write_buf)(struct super_block *sb, u64 ofs);
159 void (*sync)(struct super_block *sb);
160 void (*put_device)(struct logfs_super *s);
161};
162
163
164
165
166struct candidate_list {
167 struct rb_root rb_tree;
168 int count;
169 int maxcount;
170 int sort_by_ec;
171};
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188struct gc_candidate {
189 struct rb_node rb_node;
190 struct candidate_list *list;
191 u32 segno;
192 u32 valid;
193 u32 erase_count;
194 u8 dist;
195};
196
197
198
199
200
201
202
203
204
205struct logfs_journal_entry {
206 int used;
207 s16 version;
208 u16 len;
209 u16 datalen;
210 u64 offset;
211};
212
213enum transaction_state {
214 CREATE_1 = 1,
215 CREATE_2,
216 UNLINK_1,
217 UNLINK_2,
218 CROSS_RENAME_1,
219 CROSS_RENAME_2,
220 TARGET_RENAME_1,
221 TARGET_RENAME_2,
222 TARGET_RENAME_3
223};
224
225
226
227
228
229
230
231
232struct logfs_transaction {
233 enum transaction_state state;
234 u64 ino;
235 u64 dir;
236 u64 pos;
237};
238
239
240
241
242
243
244
245
246
247
248
249struct logfs_shadow {
250 u64 old_ofs;
251 u64 new_ofs;
252 u64 ino;
253 u64 bix;
254 int old_len;
255 int new_len;
256 gc_level_t gc_level;
257};
258
259
260
261
262
263
264
265
266struct shadow_tree {
267 struct btree_head64 new;
268 struct btree_head64 old;
269 struct btree_head32 segment_map;
270 int no_shadowed_segments;
271};
272
273struct object_alias_item {
274 struct list_head list;
275 __be64 val;
276 int child_no;
277};
278
279
280
281
282
283
284
285
286
287
288
289
290
291#define BLOCK_INDIRECT 1
292#define BLOCK_INODE 2
293struct logfs_block_ops;
294struct logfs_block {
295 struct list_head alias_list;
296 struct list_head item_list;
297 struct super_block *sb;
298 u64 ino;
299 u64 bix;
300 level_t level;
301 struct page *page;
302 struct inode *inode;
303 struct logfs_transaction *ta;
304 unsigned long alias_map[LOGFS_BLOCK_FACTOR / BITS_PER_LONG];
305 struct logfs_block_ops *ops;
306 int full;
307 int partial;
308 int reserved_bytes;
309};
310
311typedef int write_alias_t(struct super_block *sb, u64 ino, u64 bix,
312 level_t level, int child_no, __be64 val);
313struct logfs_block_ops {
314 void (*write_block)(struct logfs_block *block);
315 void (*free_block)(struct super_block *sb, struct logfs_block*block);
316 int (*write_alias)(struct super_block *sb,
317 struct logfs_block *block,
318 write_alias_t *write_one_alias);
319};
320
321#define MAX_JOURNAL_ENTRIES 256
322
323struct logfs_super {
324 struct mtd_info *s_mtd;
325 struct block_device *s_bdev;
326 const struct logfs_device_ops *s_devops;
327 struct inode *s_master_inode;
328 struct inode *s_segfile_inode;
329 struct inode *s_mapping_inode;
330 atomic_t s_pending_writes;
331 long s_flags;
332 mempool_t *s_btree_pool;
333 mempool_t *s_alias_pool;
334 u64 s_feature_incompat;
335 u64 s_feature_ro_compat;
336 u64 s_feature_compat;
337 u64 s_feature_flags;
338 u64 s_sb_ofs[2];
339 struct page *s_erase_page;
340
341 struct btree_head32 s_segment_alias;
342 int s_no_object_aliases;
343 struct list_head s_object_alias;
344 struct btree_head128 s_object_alias_tree;
345 struct mutex s_object_alias_mutex;
346
347 struct mutex s_dirop_mutex;
348 u64 s_victim_ino;
349 u64 s_rename_dir;
350 u64 s_rename_pos;
351
352 long s_segsize;
353 int s_segshift;
354 long s_segmask;
355 long s_no_segs;
356 long s_no_journal_segs;
357 long s_no_blocks;
358 long s_writesize;
359 int s_writeshift;
360 u64 s_size;
361 struct logfs_area *s_area[LOGFS_NO_AREAS];
362 u64 s_gec;
363 u64 s_wl_gec_ostore;
364 u64 s_wl_gec_journal;
365 u64 s_sweeper;
366 u8 s_ifile_levels;
367 u8 s_iblock_levels;
368 u8 s_data_levels;
369 u8 s_total_levels;
370 struct btree_head32 s_cand_tree;
371 struct candidate_list s_free_list;
372 struct candidate_list s_reserve_list;
373 struct candidate_list s_low_list[LOGFS_NO_AREAS];
374 struct candidate_list s_ec_list;
375 struct btree_head32 s_reserved_segments;
376
377 u64 s_last_ino;
378 long s_inos_till_wrap;
379 u32 s_generation;
380 struct list_head s_freeing_list;
381
382 struct mutex s_journal_mutex;
383 void *s_je;
384 void *s_compressed_je;
385 u32 s_journal_seg[LOGFS_JOURNAL_SEGS];
386 u32 s_journal_ec[LOGFS_JOURNAL_SEGS];
387 u64 s_last_version;
388 struct logfs_area *s_journal_area;
389 __be64 s_je_array[MAX_JOURNAL_ENTRIES];
390 int s_no_je;
391
392 int s_sum_index;
393 struct shadow_tree s_shadow_tree;
394 int s_je_fill;
395
396 struct mutex s_write_mutex;
397 int s_lock_count;
398 mempool_t *s_block_pool;
399 mempool_t *s_shadow_pool;
400 struct list_head s_writeback_list;
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 u64 s_free_bytes;
426 u64 s_used_bytes;
427 u64 s_dirty_free_bytes;
428 u64 s_dirty_used_bytes;
429 u64 s_root_reserve;
430 u64 s_speed_reserve;
431 u64 s_dirty_pages;
432
433
434
435
436
437
438 u32 s_bad_seg_reserve;
439 u32 s_bad_segments;
440};
441
442
443
444
445
446
447
448
449
450
451
452struct logfs_inode {
453 struct inode vfs_inode;
454 u64 li_data[LOGFS_EMBEDDED_FIELDS];
455 u64 li_used_bytes;
456 struct list_head li_freeing_list;
457 struct logfs_block *li_block;
458 u32 li_flags;
459 u8 li_height;
460 int li_refcount;
461};
462
463#define journal_for_each(__i) for (__i = 0; __i < LOGFS_JOURNAL_SEGS; __i++)
464#define for_each_area(__i) for (__i = 0; __i < LOGFS_NO_AREAS; __i++)
465#define for_each_area_down(__i) for (__i = LOGFS_NO_AREAS - 1; __i >= 0; __i--)
466
467
468int logfs_compress(void *in, void *out, size_t inlen, size_t outlen);
469int logfs_uncompress(void *in, void *out, size_t inlen, size_t outlen);
470int __init logfs_compr_init(void);
471void logfs_compr_exit(void);
472
473
474#ifdef CONFIG_BLOCK
475int logfs_get_sb_bdev(struct logfs_super *s,
476 struct file_system_type *type,
477 const char *devname);
478#else
479static inline int logfs_get_sb_bdev(struct logfs_super *s,
480 struct file_system_type *type,
481 const char *devname)
482{
483 return -ENODEV;
484}
485#endif
486
487
488#if IS_ENABLED(CONFIG_MTD)
489int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr);
490#else
491static inline int logfs_get_sb_mtd(struct logfs_super *s, int mtdnr)
492{
493 return -ENODEV;
494}
495#endif
496
497
498extern const struct inode_operations logfs_symlink_iops;
499extern const struct inode_operations logfs_dir_iops;
500extern const struct file_operations logfs_dir_fops;
501int logfs_replay_journal(struct super_block *sb);
502
503
504extern const struct inode_operations logfs_reg_iops;
505extern const struct file_operations logfs_reg_fops;
506extern const struct address_space_operations logfs_reg_aops;
507int logfs_readpage(struct file *file, struct page *page);
508long logfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
509int logfs_fsync(struct file *file, loff_t start, loff_t end, int datasync);
510
511
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
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, umode_t 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);
528void logfs_set_blocks(struct inode *inode, u64 no);
529
530int logfs_read_inode(struct inode *inode);
531int __logfs_write_inode(struct inode *inode, struct page *, long flags);
532void logfs_evict_inode(struct inode *inode);
533
534
535void logfs_write_anchor(struct super_block *sb);
536int 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
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);
580void logfs_get_wblocks(struct super_block *sb, struct page *page, int lock);
581void logfs_put_wblocks(struct super_block *sb, struct page *page, int lock);
582extern struct logfs_block_ops indirect_block_ops;
583
584
585int logfs_erase_segment(struct super_block *sb, u32 ofs, int ensure_erase);
586int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf);
587int logfs_segment_read(struct inode *inode, struct page *page, u64 ofs, u64 bix,
588 level_t level);
589int logfs_segment_write(struct inode *inode, struct page *page,
590 struct logfs_shadow *shadow);
591int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow);
592int logfs_load_object_aliases(struct super_block *sb,
593 struct logfs_obj_alias *oa, int count);
594void move_page_to_btree(struct page *page);
595int logfs_init_mapping(struct super_block *sb);
596void logfs_sync_area(struct logfs_area *area);
597void logfs_sync_segments(struct super_block *sb);
598void freeseg(struct super_block *sb, u32 segno);
599void free_areas(struct super_block *sb);
600
601
602int logfs_init_areas(struct super_block *sb);
603void logfs_cleanup_areas(struct super_block *sb);
604int logfs_open_area(struct logfs_area *area, size_t bytes);
605int __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len,
606 int use_filler);
607
608static inline int logfs_buf_write(struct logfs_area *area, u64 ofs,
609 void *buf, size_t len)
610{
611 return __logfs_buf_write(area, ofs, buf, len, 0);
612}
613
614static inline int logfs_buf_recover(struct logfs_area *area, u64 ofs,
615 void *buf, size_t len)
616{
617 return __logfs_buf_write(area, ofs, buf, len, 1);
618}
619
620
621struct page *emergency_read_begin(struct address_space *mapping, pgoff_t index);
622void emergency_read_end(struct page *page);
623void logfs_crash_dump(struct super_block *sb);
624int logfs_statfs(struct dentry *dentry, struct kstatfs *stats);
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) {
707
708 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
730static inline void logfs_mempool_destroy(mempool_t *pool)
731{
732 if (pool)
733 mempool_destroy(pool);
734}
735
736#endif
737