summaryrefslogtreecommitdiffstats
path: root/fs/logfs/logfs.h
blob: 27d040e35faa9d01b57649aa498869960e270d02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
 * fs/logfs/logfs.h
 *
 * As should be obvious for Linux kernel code, license is GPLv2
 *
 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
 *
 * Private header for logfs.
 */
#ifndef FS_LOGFS_LOGFS_H
#define FS_LOGFS_LOGFS_H

#undef __CHECK_ENDIAN__
#define __CHECK_ENDIAN__

#include <linux/btree.h>
#include <linux/crc32.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/mempool.h>
#include <linux/pagemap.h>
#include <linux/mtd/mtd.h>
#include "logfs_abi.h"

#define LOGFS_DEBUG_SUPER	(0x0001)
#define LOGFS_DEBUG_SEGMENT	(0x0002)
#define LOGFS_DEBUG_JOURNAL	(0x0004)
#define LOGFS_DEBUG_DIR		(0x0008)
#define LOGFS_DEBUG_FILE	(0x0010)
#define LOGFS_DEBUG_INODE	(0x0020)
#define LOGFS_DEBUG_READWRITE	(0x0040)
#define LOGFS_DEBUG_GC		(0x0080)
#define LOGFS_DEBUG_GC_NOISY	(0x0100)
#define LOGFS_DEBUG_ALIASES	(0x0200)
#define LOGFS_DEBUG_BLOCKMOVE	(0x0400)
#define LOGFS_DEBUG_ALL		(0xffffffff)

#define LOGFS_DEBUG		(0x01)
/*
 * To enable specific log messages, simply define LOGFS_DEBUG to match any
 * or all of the above.
 */
#ifndef LOGFS_DEBUG
#define LOGFS_DEBUG		(0)
#endif

#define log_cond(cond, fmt, arg...) do {	\
	if (cond)				\
		printk(KERN_DEBUG fmt, ##arg);	\
} while (0)

#define log_super(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SUPER, fmt, ##arg)
#define log_segment(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_SEGMENT, fmt, ##arg)
#define log_journal(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_JOURNAL, fmt, ##arg)
#define log_dir(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_DIR, fmt, ##arg)
#define log_file(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_FILE, fmt, ##arg)
#define log_inode(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_INODE, fmt, ##arg)
#define log_readwrite(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_READWRITE, fmt, ##arg)
#define log_gc(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC, fmt, ##arg)
#define log_gc_noisy(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_GC_NOISY, fmt, ##arg)
#define log_aliases(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_ALIASES, fmt, ##arg)
#define log_blockmove(fmt, arg...) \
	log_cond(LOGFS_DEBUG & LOGFS_DEBUG_BLOCKMOVE, fmt, ##arg)

#define PG_pre_locked		PG_owner_priv_1
#define PagePreLocked(page)	test_bit(PG_pre_locked, &(page)->flags)
#define SetPagePreLocked(page)	set_bit(PG_pre_locked, &(page)->flags)
#define ClearPagePreLocked(page) clear_bit(PG_pre_locked, &(page)->flags)

/* FIXME: This should really be somewhere in the 64bit area. */
#define LOGFS_LINK_MAX		(1<<30)

/* Read-only filesystem */
#define LOGFS_SB_FLAG_RO	0x0001
#define LOGFS_SB_FLAG_DIRTY	0x0002
#define LOGFS_SB_FLAG_OBJ_ALIAS	0x0004
#define LOGFS_SB_FLAG_SHUTDOWN	0x0008

/* Write Control Flags */
#define WF_LOCK			0x01 /* take write lock */
#define WF_WRITE		0x02 /* write block */
#define WF_DELETE		0x04 /* delete old block */

typedef u8 __bitwise level_t;
typedef u8 __bitwise gc_level_t;

#define LEVEL(level) ((__force level_t)(level))
#define GC_LEVEL(gc_level) ((__force gc_level_t)(gc_level))

#define SUBLEVEL(level) ( (void)((level) == LEVEL(1)),	\
		(__force level_t)((__force u8)(level) - 1) )

/**
 * struct logfs_area - area management information
 *
 * @a_sb:			the superblock this area belongs to
 * @a_is_open:			1 if the area is currently open, else 0
 * @a_segno:			segment number of area
 * @a_written_bytes:		number of bytes already written back
 * @a_used_bytes:		number of used bytes
 * @a_ops:			area operations (either journal or ostore)
 * @a_erase_count:		erase count
 * @a_level:			GC level
 */
struct logfs_area { /* a segment open for writing */
	struct super_block *a_sb;
	int	a_is_open;
	u32	a_segno;
	u32	a_written_bytes;
	u32	a_used_bytes;
	const struct logfs_area_ops *a_ops;
	u32	a_erase_count;
	gc_level_t a_level;
};

/**
 * struct logfs_area_ops - area operations
 *
 * @get_free_segment:		fill area->ofs with the offset of a free segment
 * @get_erase_count:		fill area->erase_count (needs area->ofs)
 * @erase_segment:		erase and setup segment
 */
struct logfs_area_ops {
	void	(*get_free_segment)(struct logfs_area *area);
	void	(*get_erase_count)(struct logfs_area *area);
	int	(*erase_segment)(struct logfs_area *area);
};

struct logfs_super;	/* forward */
/**
 * struct logfs_device_ops - device access operations
 *
 * @readpage:			read one page (mm page)
 * @writeseg:			write one segment.  may be a partial segment
 * @erase:			erase one segment
 * @read:			read from the device
 * @erase:			erase part of the device
 * @can_write_buf:		decide whether wbuf can be written to ofs
 */
struct logfs_device_ops {
	struct page *(*find_first_sb)(struct super_block *sb, u64 *ofs);
	struct page *(*find_last_sb)(struct super_block *sb, u64 *ofs);
	int (*write_sb)(struct super_block *sb, struct page *page);
	int (*readpage)(void *_sb, struct page *page);
	void (*writeseg)(struct super_block *sb, u64 ofs, size_t len);
	int (*erase)(struct super_block *sb, loff_t ofs, size_t len,
			int ensure_write);
	int (*can_write_buf)(struct super_block *sb, u64 ofs);
	void (*sync)(struct super_block *sb);
	void (*put_device)(struct logfs_super *s);
};

/**
 * struct candidate_list - list of similar candidates
 */
struct candidate_list {
	struct rb_root rb_tree;
	int count;
	int maxcount;
	int sort_by_ec;
};

/**
 * struct gc_candidate - "candidate" segment to be garbage collected next
 *
 * @list:			list (either free of low)
 * @segno:			segment number
 * @valid:			number of valid bytes
 * @erase_count:		erase count of segment
 * @dist:			distance from tree root
 *
 * Candidates can be on two lists.  The free list contains electees rather
 * than candidates - segments that no longer contain any valid data.  The
 * low list contains candidates to be picked for GC.  It should be kept
 * short.  It is not required to always pick a perfect candidate.  In the
 * worst case GC will have to move more data than absolutely necessary.
 */
struct gc_candidate {
	struct rb_node rb_node;
	struct candidate_list *list;
	u32	segno;
	u32	valid;
	u32	erase_count;
	u8	dist;
};

/**
 * struct logfs_journal_entry - temporary structure used during journal scan
 *
 * @used:
 * @version:			normalized version
 * @len:			length
 * @offset:			offset
 */
struct logfs_journal_entry {
	int used;
	s16 version;
	u16 len;
	u16 datalen;
	u64 offset;
};

enum transaction_state {
	CREATE_1 = 1,
	CREATE_2,
	UNLINK_1,
	UNLINK_2,
	CROSS_RENAME_1,
	CROSS_RENAME_2,
	TARGET_RENAME_1,
	TARGET_RENAME_2,
	TARGET_RENAME_3
};

/**
 * struct logfs_transaction - essential fields to support atomic dirops
 *
 * @ino:			target inode
 * @dir:			inode of directory containing dentry
 * @pos:			pos of dentry in directory
 */
struct logfs_transaction {
	enum transaction_state state;
	u64	 ino;
	u64	 dir;
	u64	 pos;
};

/**
 * struct logfs_shadow - old block in the shadow of a not-yet-committed new one
 * @old_ofs:			offset of old block on medium
 * @new_ofs:			offset of new block on medium
 * @ino:			inode number
 * @bix:			block index
 * @old_len:			size of old block, including header
 * @new_len:			size of new block, including header
 * @level:			block level
 */
struct logfs_shadow {
	u64 old_ofs;
	u64 new_ofs;
	u64 ino;
	u64 bix;
	int old_len;
	int new_len;
	gc_level_t gc_level;
};

/**
 * struct shadow_tree
 * @new:			shadows where old_ofs==0, indexed by new_ofs
 * @old:			shadows where old_ofs!=0, indexed by old_ofs
 * @segment_map:		bitfield of segments containing shadows
 * @no_shadowed_segment:	number of segments containing shadows
 */
struct shadow_tree {
	struct btree_head64 new;
	struct btree_head64 old;
	struct btree_head32 segment_map;
	int no_shadowed_segments;
};

struct object_alias_item {
	struct list_head list;
	__be64 val;
	int child_no;
};

/**
 * struct logfs_block - contains any block state
 * @type:			indirect block or inode
 * @full:			number of fully populated children
 * @partial:			number of partially populated children
 *
 * Most blocks are directly represented by page cache pages.  But when a block
 * becomes dirty, is part of a transaction, contains aliases or is otherwise
 * special, a struct logfs_block is allocated to track the additional state.
 * Inodes are very similar to indirect blocks, so they can also get one of
 * these structures added when appropriate.
 */
#define BLOCK_INDIRECT	1	/* Indirect block */
#define BLOCK_INODE	2	/* Inode */
struct logfs_block_ops;
struct logfs_block {
	struct list_head alias_list;
	struct list_head item_list;
	struct super_block *sb;
	u64 ino;
	u64 bix;
	level_t level;
	struct page *page;
	struct inode *inode;
	struct logfs_transaction *ta;
	unsigned long alias_map[LOGFS_BLOCK_FACTOR / BITS_PER_LONG];
	const struct logfs_block_ops *ops;
	int full;
	int partial;
	int reserved_bytes;
};

typedef int write_alias_t(struct super_block *sb, u64 ino, u64 bix,
		level_t level, int child_no, __be64 val);
struct logfs_block_ops {
	void	(*write_block)(struct logfs_block *block);
	void	(*free_block)(struct super_block *sb, struct logfs_block*block);
	int	(*write_alias)(struct super_block *sb,
			struct logfs_bloc