forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 8
/
readwrite.c
2298 lines (1953 loc) · 55.7 KB
/
readwrite.c
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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* fs/logfs/readwrite.c
*
* As should be obvious for Linux kernel code, license is GPLv2
*
* Copyright (c) 2005-2008 Joern Engel <[email protected]>
*
*
* Actually contains five sets of very similar functions:
* read read blocks from a file
* seek_hole find next hole
* seek_data find next data block
* valid check whether a block still belongs to a file
* write write blocks to a file
* delete delete a block (for directories and ifile)
* rewrite move existing blocks of a file to a new location (gc helper)
* truncate truncate a file
*/
#include "logfs.h"
#include <linux/sched.h>
#include <linux/slab.h>
static u64 adjust_bix(u64 bix, level_t level)
{
switch (level) {
case 0:
return bix;
case LEVEL(1):
return max_t(u64, bix, I0_BLOCKS);
case LEVEL(2):
return max_t(u64, bix, I1_BLOCKS);
case LEVEL(3):
return max_t(u64, bix, I2_BLOCKS);
case LEVEL(4):
return max_t(u64, bix, I3_BLOCKS);
case LEVEL(5):
return max_t(u64, bix, I4_BLOCKS);
default:
WARN_ON(1);
return bix;
}
}
static inline u64 maxbix(u8 height)
{
return 1ULL << (LOGFS_BLOCK_BITS * height);
}
/**
* The inode address space is cut in two halves. Lower half belongs to data
* pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is
* set, the actual block index (bix) and level can be derived from the page
* index.
*
* The lowest three bits of the block index are set to 0 after packing and
* unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored
* anyway this is harmless.
*/
#define ARCH_SHIFT (BITS_PER_LONG - 32)
#define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT)
#define LEVEL_SHIFT (28 + ARCH_SHIFT)
static inline pgoff_t first_indirect_block(void)
{
return INDIRECT_BIT | (1ULL << LEVEL_SHIFT);
}
pgoff_t logfs_pack_index(u64 bix, level_t level)
{
pgoff_t index;
BUG_ON(bix >= INDIRECT_BIT);
if (level == 0)
return bix;
index = INDIRECT_BIT;
index |= (__force long)level << LEVEL_SHIFT;
index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS);
return index;
}
void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level)
{
u8 __level;
if (!(index & INDIRECT_BIT)) {
*bix = index;
*level = 0;
return;
}
__level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT;
*level = LEVEL(__level);
*bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT;
*bix = adjust_bix(*bix, *level);
return;
}
#undef ARCH_SHIFT
#undef INDIRECT_BIT
#undef LEVEL_SHIFT
/*
* Time is stored as nanoseconds since the epoch.
*/
static struct timespec be64_to_timespec(__be64 betime)
{
return ns_to_timespec(be64_to_cpu(betime));
}
static __be64 timespec_to_be64(struct timespec tsp)
{
return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec);
}
static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode)
{
struct logfs_inode *li = logfs_inode(inode);
int i;
inode->i_mode = be16_to_cpu(di->di_mode);
li->li_height = di->di_height;
li->li_flags = be32_to_cpu(di->di_flags);
i_uid_write(inode, be32_to_cpu(di->di_uid));
i_gid_write(inode, be32_to_cpu(di->di_gid));
inode->i_size = be64_to_cpu(di->di_size);
logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes));
inode->i_atime = be64_to_timespec(di->di_atime);
inode->i_ctime = be64_to_timespec(di->di_ctime);
inode->i_mtime = be64_to_timespec(di->di_mtime);
set_nlink(inode, be32_to_cpu(di->di_refcount));
inode->i_generation = be32_to_cpu(di->di_generation);
switch (inode->i_mode & S_IFMT) {
case S_IFSOCK: /* fall through */
case S_IFBLK: /* fall through */
case S_IFCHR: /* fall through */
case S_IFIFO:
inode->i_rdev = be64_to_cpu(di->di_data[0]);
break;
case S_IFDIR: /* fall through */
case S_IFREG: /* fall through */
case S_IFLNK:
for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
li->li_data[i] = be64_to_cpu(di->di_data[i]);
break;
default:
BUG();
}
}
static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di)
{
struct logfs_inode *li = logfs_inode(inode);
int i;
di->di_mode = cpu_to_be16(inode->i_mode);
di->di_height = li->li_height;
di->di_pad = 0;
di->di_flags = cpu_to_be32(li->li_flags);
di->di_uid = cpu_to_be32(i_uid_read(inode));
di->di_gid = cpu_to_be32(i_gid_read(inode));
di->di_size = cpu_to_be64(i_size_read(inode));
di->di_used_bytes = cpu_to_be64(li->li_used_bytes);
di->di_atime = timespec_to_be64(inode->i_atime);
di->di_ctime = timespec_to_be64(inode->i_ctime);
di->di_mtime = timespec_to_be64(inode->i_mtime);
di->di_refcount = cpu_to_be32(inode->i_nlink);
di->di_generation = cpu_to_be32(inode->i_generation);
switch (inode->i_mode & S_IFMT) {
case S_IFSOCK: /* fall through */
case S_IFBLK: /* fall through */
case S_IFCHR: /* fall through */
case S_IFIFO:
di->di_data[0] = cpu_to_be64(inode->i_rdev);
break;
case S_IFDIR: /* fall through */
case S_IFREG: /* fall through */
case S_IFLNK:
for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
di->di_data[i] = cpu_to_be64(li->li_data[i]);
break;
default:
BUG();
}
}
static void __logfs_set_blocks(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct logfs_inode *li = logfs_inode(inode);
inode->i_blocks = ULONG_MAX;
if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX)
inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9;
}
void logfs_set_blocks(struct inode *inode, u64 bytes)
{
struct logfs_inode *li = logfs_inode(inode);
li->li_used_bytes = bytes;
__logfs_set_blocks(inode);
}
static void prelock_page(struct super_block *sb, struct page *page, int lock)
{
struct logfs_super *super = logfs_super(sb);
BUG_ON(!PageLocked(page));
if (lock) {
BUG_ON(PagePreLocked(page));
SetPagePreLocked(page);
} else {
/* We are in GC path. */
if (PagePreLocked(page))
super->s_lock_count++;
else
SetPagePreLocked(page);
}
}
static void preunlock_page(struct super_block *sb, struct page *page, int lock)
{
struct logfs_super *super = logfs_super(sb);
BUG_ON(!PageLocked(page));
if (lock)
ClearPagePreLocked(page);
else {
/* We are in GC path. */
BUG_ON(!PagePreLocked(page));
if (super->s_lock_count)
super->s_lock_count--;
else
ClearPagePreLocked(page);
}
}
/*
* Logfs is prone to an AB-BA deadlock where one task tries to acquire
* s_write_mutex with a locked page and GC tries to get that page while holding
* s_write_mutex.
* To solve this issue logfs will ignore the page lock iff the page in question
* is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked
* in addition to PG_locked.
*/
void logfs_get_wblocks(struct super_block *sb, struct page *page, int lock)
{
struct logfs_super *super = logfs_super(sb);
if (page)
prelock_page(sb, page, lock);
if (lock) {
mutex_lock(&super->s_write_mutex);
logfs_gc_pass(sb);
/* FIXME: We also have to check for shadowed space
* and mempool fill grade */
}
}
void logfs_put_wblocks(struct super_block *sb, struct page *page, int lock)
{
struct logfs_super *super = logfs_super(sb);
if (page)
preunlock_page(sb, page, lock);
/* Order matters - we must clear PG_pre_locked before releasing
* s_write_mutex or we could race against another task. */
if (lock)
mutex_unlock(&super->s_write_mutex);
}
static struct page *logfs_get_read_page(struct inode *inode, u64 bix,
level_t level)
{
return find_or_create_page(inode->i_mapping,
logfs_pack_index(bix, level), GFP_NOFS);
}
static void logfs_put_read_page(struct page *page)
{
unlock_page(page);
put_page(page);
}
static void logfs_lock_write_page(struct page *page)
{
int loop = 0;
while (unlikely(!trylock_page(page))) {
if (loop++ > 0x1000) {
/* Has been observed once so far... */
printk(KERN_ERR "stack at %p\n", &loop);
BUG();
}
if (PagePreLocked(page)) {
/* Holder of page lock is waiting for us, it
* is safe to use this page. */
break;
}
/* Some other process has this page locked and has
* nothing to do with us. Wait for it to finish.
*/
schedule();
}
BUG_ON(!PageLocked(page));
}
static struct page *logfs_get_write_page(struct inode *inode, u64 bix,
level_t level)
{
struct address_space *mapping = inode->i_mapping;
pgoff_t index = logfs_pack_index(bix, level);
struct page *page;
int err;
repeat:
page = find_get_page(mapping, index);
if (!page) {
page = __page_cache_alloc(GFP_NOFS);
if (!page)
return NULL;
err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS);
if (unlikely(err)) {
put_page(page);
if (err == -EEXIST)
goto repeat;
return NULL;
}
} else logfs_lock_write_page(page);
BUG_ON(!PageLocked(page));
return page;
}
static void logfs_unlock_write_page(struct page *page)
{
if (!PagePreLocked(page))
unlock_page(page);
}
static void logfs_put_write_page(struct page *page)
{
logfs_unlock_write_page(page);
put_page(page);
}
static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level,
int rw)
{
if (rw == READ)
return logfs_get_read_page(inode, bix, level);
else
return logfs_get_write_page(inode, bix, level);
}
static void logfs_put_page(struct page *page, int rw)
{
if (rw == READ)
logfs_put_read_page(page);
else
logfs_put_write_page(page);
}
static unsigned long __get_bits(u64 val, int skip, int no)
{
u64 ret = val;
ret >>= skip * no;
ret <<= 64 - no;
ret >>= 64 - no;
return ret;
}
static unsigned long get_bits(u64 val, level_t skip)
{
return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS);
}
static inline void init_shadow_tree(struct super_block *sb,
struct shadow_tree *tree)
{
struct logfs_super *super = logfs_super(sb);
btree_init_mempool64(&tree->new, super->s_btree_pool);
btree_init_mempool64(&tree->old, super->s_btree_pool);
}
static void indirect_write_block(struct logfs_block *block)
{
struct page *page;
struct inode *inode;
int ret;
page = block->page;
inode = page->mapping->host;
logfs_lock_write_page(page);
ret = logfs_write_buf(inode, page, 0);
logfs_unlock_write_page(page);
/*
* This needs some rework. Unless you want your filesystem to run
* completely synchronously (you don't), the filesystem will always
* report writes as 'successful' before the actual work has been
* done. The actual work gets done here and this is where any errors
* will show up. And there isn't much we can do about it, really.
*
* Some attempts to fix the errors (move from bad blocks, retry io,...)
* have already been done, so anything left should be either a broken
* device or a bug somewhere in logfs itself. Being relatively new,
* the odds currently favor a bug, so for now the line below isn't
* entirely tasteles.
*/
BUG_ON(ret);
}
static void inode_write_block(struct logfs_block *block)
{
struct inode *inode;
int ret;
inode = block->inode;
if (inode->i_ino == LOGFS_INO_MASTER)
logfs_write_anchor(inode->i_sb);
else {
ret = __logfs_write_inode(inode, NULL, 0);
/* see indirect_write_block comment */
BUG_ON(ret);
}
}
/*
* This silences a false, yet annoying gcc warning. I hate it when my editor
* jumps into bitops.h each time I recompile this file.
* TODO: Complain to gcc folks about this and upgrade compiler.
*/
static unsigned long fnb(const unsigned long *addr,
unsigned long size, unsigned long offset)
{
return find_next_bit(addr, size, offset);
}
static __be64 inode_val0(struct inode *inode)
{
struct logfs_inode *li = logfs_inode(inode);
u64 val;
/*
* Explicit shifting generates good code, but must match the format
* of the structure. Add some paranoia just in case.
*/
BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0);
BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2);
BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4);
val = (u64)inode->i_mode << 48 |
(u64)li->li_height << 40 |
(u64)li->li_flags;
return cpu_to_be64(val);
}
static int inode_write_alias(struct super_block *sb,
struct logfs_block *block, write_alias_t *write_one_alias)
{
struct inode *inode = block->inode;
struct logfs_inode *li = logfs_inode(inode);
unsigned long pos;
u64 ino , bix;
__be64 val;
level_t level;
int err;
for (pos = 0; ; pos++) {
pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS)
return 0;
switch (pos) {
case INODE_HEIGHT_OFS:
val = inode_val0(inode);
break;
case INODE_USED_OFS:
val = cpu_to_be64(li->li_used_bytes);
break;
case INODE_SIZE_OFS:
val = cpu_to_be64(i_size_read(inode));
break;
case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1:
val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]);
break;
default:
BUG();
}
ino = LOGFS_INO_MASTER;
bix = inode->i_ino;
level = LEVEL(0);
err = write_one_alias(sb, ino, bix, level, pos, val);
if (err)
return err;
}
}
static int indirect_write_alias(struct super_block *sb,
struct logfs_block *block, write_alias_t *write_one_alias)
{
unsigned long pos;
struct page *page = block->page;
u64 ino , bix;
__be64 *child, val;
level_t level;
int err;
for (pos = 0; ; pos++) {
pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
if (pos >= LOGFS_BLOCK_FACTOR)
return 0;
ino = page->mapping->host->i_ino;
logfs_unpack_index(page->index, &bix, &level);
child = kmap_atomic(page);
val = child[pos];
kunmap_atomic(child);
err = write_one_alias(sb, ino, bix, level, pos, val);
if (err)
return err;
}
}
int logfs_write_obj_aliases_pagecache(struct super_block *sb)
{
struct logfs_super *super = logfs_super(sb);
struct logfs_block *block;
int err;
list_for_each_entry(block, &super->s_object_alias, alias_list) {
err = block->ops->write_alias(sb, block, write_alias_journal);
if (err)
return err;
}
return 0;
}
void __free_block(struct super_block *sb, struct logfs_block *block)
{
BUG_ON(!list_empty(&block->item_list));
list_del(&block->alias_list);
mempool_free(block, logfs_super(sb)->s_block_pool);
}
static void inode_free_block(struct super_block *sb, struct logfs_block *block)
{
struct inode *inode = block->inode;
logfs_inode(inode)->li_block = NULL;
__free_block(sb, block);
}
static void indirect_free_block(struct super_block *sb,
struct logfs_block *block)
{
struct page *page = block->page;
if (PagePrivate(page)) {
ClearPagePrivate(page);
put_page(page);
set_page_private(page, 0);
}
__free_block(sb, block);
}
static const struct logfs_block_ops inode_block_ops = {
.write_block = inode_write_block,
.free_block = inode_free_block,
.write_alias = inode_write_alias,
};
const struct logfs_block_ops indirect_block_ops = {
.write_block = indirect_write_block,
.free_block = indirect_free_block,
.write_alias = indirect_write_alias,
};
struct logfs_block *__alloc_block(struct super_block *sb,
u64 ino, u64 bix, level_t level)
{
struct logfs_super *super = logfs_super(sb);
struct logfs_block *block;
block = mempool_alloc(super->s_block_pool, GFP_NOFS);
memset(block, 0, sizeof(*block));
INIT_LIST_HEAD(&block->alias_list);
INIT_LIST_HEAD(&block->item_list);
block->sb = sb;
block->ino = ino;
block->bix = bix;
block->level = level;
return block;
}
static void alloc_inode_block(struct inode *inode)
{
struct logfs_inode *li = logfs_inode(inode);
struct logfs_block *block;
if (li->li_block)
return;
block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0);
block->inode = inode;
li->li_block = block;
block->ops = &inode_block_ops;
}
void initialize_block_counters(struct page *page, struct logfs_block *block,
__be64 *array, int page_is_empty)
{
u64 ptr;
int i, start;
block->partial = 0;
block->full = 0;
start = 0;
if (page->index < first_indirect_block()) {
/* Counters are pointless on level 0 */
return;
}
if (page->index == first_indirect_block()) {
/* Skip unused pointers */
start = I0_BLOCKS;
block->full = I0_BLOCKS;
}
if (!page_is_empty) {
for (i = start; i < LOGFS_BLOCK_FACTOR; i++) {
ptr = be64_to_cpu(array[i]);
if (ptr)
block->partial++;
if (ptr & LOGFS_FULLY_POPULATED)
block->full++;
}
}
}
static void alloc_data_block(struct inode *inode, struct page *page)
{
struct logfs_block *block;
u64 bix;
level_t level;
if (PagePrivate(page))
return;
logfs_unpack_index(page->index, &bix, &level);
block = __alloc_block(inode->i_sb, inode->i_ino, bix, level);
block->page = page;
SetPagePrivate(page);
get_page(page);
set_page_private(page, (unsigned long) block);
block->ops = &indirect_block_ops;
}
static void alloc_indirect_block(struct inode *inode, struct page *page,
int page_is_empty)
{
struct logfs_block *block;
__be64 *array;
if (PagePrivate(page))
return;
alloc_data_block(inode, page);
block = logfs_block(page);
array = kmap_atomic(page);
initialize_block_counters(page, block, array, page_is_empty);
kunmap_atomic(array);
}
static void block_set_pointer(struct page *page, int index, u64 ptr)
{
struct logfs_block *block = logfs_block(page);
__be64 *array;
u64 oldptr;
BUG_ON(!block);
array = kmap_atomic(page);
oldptr = be64_to_cpu(array[index]);
array[index] = cpu_to_be64(ptr);
kunmap_atomic(array);
SetPageUptodate(page);
block->full += !!(ptr & LOGFS_FULLY_POPULATED)
- !!(oldptr & LOGFS_FULLY_POPULATED);
block->partial += !!ptr - !!oldptr;
}
static u64 block_get_pointer(struct page *page, int index)
{
__be64 *block;
u64 ptr;
block = kmap_atomic(page);
ptr = be64_to_cpu(block[index]);
kunmap_atomic(block);
return ptr;
}
static int logfs_read_empty(struct page *page)
{
zero_user_segment(page, 0, PAGE_SIZE);
return 0;
}
static int logfs_read_direct(struct inode *inode, struct page *page)
{
struct logfs_inode *li = logfs_inode(inode);
pgoff_t index = page->index;
u64 block;
block = li->li_data[index];
if (!block)
return logfs_read_empty(page);
return logfs_segment_read(inode, page, block, index, 0);
}
static int logfs_read_loop(struct inode *inode, struct page *page,
int rw_context)
{
struct logfs_inode *li = logfs_inode(inode);
u64 bix, bofs = li->li_data[INDIRECT_INDEX];
level_t level, target_level;
int ret;
struct page *ipage;
logfs_unpack_index(page->index, &bix, &target_level);
if (!bofs)
return logfs_read_empty(page);
if (bix >= maxbix(li->li_height))
return logfs_read_empty(page);
for (level = LEVEL(li->li_height);
(__force u8)level > (__force u8)target_level;
level = SUBLEVEL(level)){
ipage = logfs_get_page(inode, bix, level, rw_context);
if (!ipage)
return -ENOMEM;
ret = logfs_segment_read(inode, ipage, bofs, bix, level);
if (ret) {
logfs_put_read_page(ipage);
return ret;
}
bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
logfs_put_page(ipage, rw_context);
if (!bofs)
return logfs_read_empty(page);
}
return logfs_segment_read(inode, page, bofs, bix, 0);
}
static int logfs_read_block(struct inode *inode, struct page *page,
int rw_context)
{
pgoff_t index = page->index;
if (index < I0_BLOCKS)
return logfs_read_direct(inode, page);
return logfs_read_loop(inode, page, rw_context);
}
static int logfs_exist_loop(struct inode *inode, u64 bix)
{
struct logfs_inode *li = logfs_inode(inode);
u64 bofs = li->li_data[INDIRECT_INDEX];
level_t level;
int ret;
struct page *ipage;
if (!bofs)
return 0;
if (bix >= maxbix(li->li_height))
return 0;
for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
ipage = logfs_get_read_page(inode, bix, level);
if (!ipage)
return -ENOMEM;
ret = logfs_segment_read(inode, ipage, bofs, bix, level);
if (ret) {
logfs_put_read_page(ipage);
return ret;
}
bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
logfs_put_read_page(ipage);
if (!bofs)
return 0;
}
return 1;
}
int logfs_exist_block(struct inode *inode, u64 bix)
{
struct logfs_inode *li = logfs_inode(inode);
if (bix < I0_BLOCKS)
return !!li->li_data[bix];
return logfs_exist_loop(inode, bix);
}
static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data)
{
struct logfs_inode *li = logfs_inode(inode);
for (; bix < I0_BLOCKS; bix++)
if (data ^ (li->li_data[bix] == 0))
return bix;
return I0_BLOCKS;
}
static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data)
{
struct logfs_inode *li = logfs_inode(inode);
__be64 *rblock;
u64 increment, bofs = li->li_data[INDIRECT_INDEX];
level_t level;
int ret, slot;
struct page *page;
BUG_ON(!bofs);
for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1));
page = logfs_get_read_page(inode, bix, level);
if (!page)
return bix;
ret = logfs_segment_read(inode, page, bofs, bix, level);
if (ret) {
logfs_put_read_page(page);
return bix;
}
slot = get_bits(bix, SUBLEVEL(level));
rblock = kmap_atomic(page);
while (slot < LOGFS_BLOCK_FACTOR) {
if (data && (rblock[slot] != 0))
break;
if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED))
break;
slot++;
bix += increment;
bix &= ~(increment - 1);
}
if (slot >= LOGFS_BLOCK_FACTOR) {
kunmap_atomic(rblock);
logfs_put_read_page(page);
return bix;
}
bofs = be64_to_cpu(rblock[slot]);
kunmap_atomic(rblock);
logfs_put_read_page(page);
if (!bofs) {
BUG_ON(data);
return bix;
}
}
return bix;
}
/**
* logfs_seek_hole - find next hole starting at a given block index
* @inode: inode to search in
* @bix: block index to start searching
*
* Returns next hole. If the file doesn't contain any further holes, the
* block address next to eof is returned instead.
*/
u64 logfs_seek_hole(struct inode *inode, u64 bix)
{
struct logfs_inode *li = logfs_inode(inode);
if (bix < I0_BLOCKS) {
bix = seek_holedata_direct(inode, bix, 0);
if (bix < I0_BLOCKS)
return bix;
}
if (!li->li_data[INDIRECT_INDEX])
return bix;
else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED)
bix = maxbix(li->li_height);
else if (bix >= maxbix(li->li_height))
return bix;
else {
bix = seek_holedata_loop(inode, bix, 0);
if (bix < maxbix(li->li_height))
return bix;
/* Should not happen anymore. But if some port writes semi-
* corrupt images (as this one used to) we might run into it.
*/
WARN_ON_ONCE(bix == maxbix(li->li_height));
}
return bix;
}
static u64 __logfs_seek_data(struct inode *inode, u64 bix)
{
struct logfs_inode *li = logfs_inode(inode);
if (bix < I0_BLOCKS) {
bix = seek_holedata_direct(inode, bix, 1);
if (bix < I0_BLOCKS)
return bix;
}
if (bix < maxbix(li->li_height)) {
if (!li->li_data[INDIRECT_INDEX])
bix = maxbix(li->li_height);
else
return seek_holedata_loop(inode, bix, 1);
}
return bix;
}
/**
* logfs_seek_data - find next data block after a given block index
* @inode: inode to search in
* @bix: block index to start searching
*
* Returns next data block. If the file doesn't contain any further data
* blocks, the last block in the file is returned instead.
*/
u64 logfs_seek_data(struct inode *inode, u64 bix)
{
struct super_block *sb = inode->i_sb;
u64 ret, end;
ret = __logfs_seek_data(inode, bix);
end = i_size_read(inode) >> sb->s_blocksize_bits;
if (ret >= end)
ret = max(bix, end);
return ret;
}
static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs)
{
return pure_ofs(li->li_data[bix]) == ofs;
}
static int __logfs_is_valid_loop(struct inode *inode, u64 bix,
u64 ofs, u64 bofs)
{
struct logfs_inode *li = logfs_inode(inode);
level_t level;
int ret;
struct page *page;
for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){
page = logfs_get_write_page(inode, bix, level);
BUG_ON(!page);
ret = logfs_segment_read(inode, page, bofs, bix, level);
if (ret) {
logfs_put_write_page(page);
return 0;
}
bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level)));
logfs_put_write_page(page);
if (!bofs)
return 0;
if (pure_ofs(bofs) == ofs)
return 1;
}
return 0;
}
static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs)
{
struct logfs_inode *li = logfs_inode(inode);
u64 bofs = li->li_data[INDIRECT_INDEX];
if (!bofs)
return 0;
if (bix >= maxbix(li->li_height))
return 0;