-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjnode.c
1906 lines (1647 loc) · 50.3 KB
/
jnode.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
/* Copyright 2001, 2002, 2003, 2004 by Hans Reiser, licensing governed by
* reiser4/README */
/* Jnode manipulation functions. */
/* Jnode is entity used to track blocks with data and meta-data in reiser4.
In particular, jnodes are used to track transactional information
associated with each block. Each znode contains jnode as ->zjnode field.
Jnode stands for either Josh or Journal node.
*/
/*
* Taxonomy.
*
* Jnode represents block containing data or meta-data. There are jnodes
* for:
*
* unformatted blocks (jnodes proper). There are plans, however to
* have a handle per extent unit rather than per each unformatted
* block, because there are so many of them.
*
* For bitmaps. Each bitmap is actually represented by two jnodes--one
* for working and another for "commit" data, together forming bnode.
*
* For io-heads. These are used by log writer.
*
* For formatted nodes (znode). See comment at the top of znode.c for
* details specific to the formatted nodes (znodes).
*
* Node data.
*
* Jnode provides access to the data of node it represents. Data are
* stored in a page. Page is kept in a page cache. This means, that jnodes
* are highly interconnected with page cache and VM internals.
*
* jnode has a pointer to page (->pg) containing its data. Pointer to data
* themselves is cached in ->data field to avoid frequent calls to
* page_address().
*
* jnode and page are attached to each other by jnode_attach_page(). This
* function places pointer to jnode in set_page_private(), sets PG_private
* flag and increments page counter.
*
* Opposite operation is performed by page_clear_jnode().
*
* jnode->pg is protected by jnode spin lock, and page->private is
* protected by page lock. See comment at the top of page_cache.c for
* more.
*
* page can be detached from jnode for two reasons:
*
* . jnode is removed from a tree (file is truncated, of formatted
* node is removed by balancing).
*
* . during memory pressure, VM calls ->releasepage() method
* (reiser4_releasepage()) to evict page from memory.
*
* (there, of course, is also umount, but this is special case we are not
* concerned with here).
*
* To protect jnode page from eviction, one calls jload() function that
* "pins" page in memory (loading it if necessary), increments
* jnode->d_count, and kmap()s page. Page is unpinned through call to
* jrelse().
*
* Jnode life cycle.
*
* jnode is created, placed in hash table, and, optionally, in per-inode
* radix tree. Page can be attached to jnode, pinned, released, etc.
*
* When jnode is captured into atom its reference counter is
* increased. While being part of an atom, jnode can be "early
* flushed". This means that as part of flush procedure, jnode is placed
* into "relocate set", and its page is submitted to the disk. After io
* completes, page can be detached, then loaded again, re-dirtied, etc.
*
* Thread acquired reference to jnode by calling jref() and releases it by
* jput(). When last reference is removed, jnode is still retained in
* memory (cached) if it has page attached, _unless_ it is scheduled for
* destruction (has JNODE_HEARD_BANSHEE bit set).
*
* Tree read-write lock was used as "existential" lock for jnodes. That is,
* jnode->x_count could be changed from 0 to 1 only under tree write lock,
* that is, tree lock protected unreferenced jnodes stored in the hash
* table, from recycling.
*
* This resulted in high contention on tree lock, because jref()/jput() is
* frequent operation. To ameliorate this problem, RCU is used: when jput()
* is just about to release last reference on jnode it sets JNODE_RIP bit
* on it, and then proceed with jnode destruction (removing jnode from hash
* table, cbk_cache, detaching page, etc.). All places that change jnode
* reference counter from 0 to 1 (jlookup(), zlook(), zget(), and
* cbk_cache_scan_slots()) check for JNODE_RIP bit (this is done by
* jnode_rip_check() function), and pretend that nothing was found in hash
* table if bit is set.
*
* jput defers actual return of jnode into slab cache to some later time
* (by call_rcu()), this guarantees that other threads can safely continue
* working with JNODE_RIP-ped jnode.
*
*/
#include "reiser4.h"
#include "debug.h"
#include "dformat.h"
#include "jnode.h"
#include "plugin/plugin_header.h"
#include "plugin/plugin.h"
#include "txnmgr.h"
/*#include "jnode.h"*/
#include "znode.h"
#include "tree.h"
#include "tree_walk.h"
#include "super.h"
#include "inode.h"
#include "page_cache.h"
#include <asm/uaccess.h> /* UML needs this for PAGE_OFFSET */
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/fs.h> /* for struct address_space */
#include <linux/writeback.h> /* for inode_wb_list_lock */
static struct kmem_cache *_jnode_slab = NULL;
static void jnode_set_type(jnode * node, jnode_type type);
static int jdelete(jnode * node);
static int jnode_try_drop(jnode * node);
#if REISER4_DEBUG
static int jnode_invariant(jnode * node, int tlocked, int jlocked);
#endif
/* true if valid page is attached to jnode */
static inline int jnode_is_parsed(jnode * node)
{
return JF_ISSET(node, JNODE_PARSED);
}
/* hash table support */
/* compare two jnode keys for equality. Used by hash-table macros */
static inline int jnode_key_eq(const struct jnode_key *k1,
const struct jnode_key *k2)
{
assert("nikita-2350", k1 != NULL);
assert("nikita-2351", k2 != NULL);
return (k1->index == k2->index && k1->objectid == k2->objectid);
}
/* Hash jnode by its key (inode plus offset). Used by hash-table macros */
static inline __u32 jnode_key_hashfn(j_hash_table * table,
const struct jnode_key *key)
{
assert("nikita-2352", key != NULL);
assert("nikita-3346", IS_POW(table->_buckets));
/* yes, this is remarkable simply (where not stupid) hash function. */
return (key->objectid + key->index) & (table->_buckets - 1);
}
/* The hash table definition */
#define KMALLOC(size) reiser4_vmalloc(size)
#define KFREE(ptr, size) vfree(ptr)
TYPE_SAFE_HASH_DEFINE(j, jnode, struct jnode_key, key.j, link.j,
jnode_key_hashfn, jnode_key_eq);
#undef KFREE
#undef KMALLOC
/* call this to initialise jnode hash table */
int jnodes_tree_init(reiser4_tree * tree/* tree to initialise jnodes for */)
{
assert("nikita-2359", tree != NULL);
return j_hash_init(&tree->jhash_table, 16384);
}
/* call this to destroy jnode hash table. This is called during umount. */
int jnodes_tree_done(reiser4_tree * tree/* tree to destroy jnodes for */)
{
j_hash_table *jtable;
jnode *node;
jnode *next;
assert("nikita-2360", tree != NULL);
/*
* Scan hash table and free all jnodes.
*/
jtable = &tree->jhash_table;
if (jtable->_table) {
for_all_in_htable(jtable, j, node, next) {
assert("nikita-2361", !atomic_read(&node->x_count));
jdrop(node);
}
j_hash_done(&tree->jhash_table);
}
return 0;
}
/**
* init_jnodes - create jnode cache
*
* Initializes slab cache jnodes. It is part of reiser4 module initialization.
*/
int init_jnodes(void)
{
assert("umka-168", _jnode_slab == NULL);
_jnode_slab = kmem_cache_create("jnode", sizeof(jnode), 0,
SLAB_HWCACHE_ALIGN |
SLAB_RECLAIM_ACCOUNT, NULL);
if (_jnode_slab == NULL)
return RETERR(-ENOMEM);
return 0;
}
/**
* done_znodes - delete znode cache
*
* This is called on reiser4 module unloading or system shutdown.
*/
void done_jnodes(void)
{
destroy_reiser4_cache(&_jnode_slab);
}
/* Initialize a jnode. */
void jnode_init(jnode * node, reiser4_tree * tree, jnode_type type)
{
memset(node, 0, sizeof(jnode));
ON_DEBUG(node->magic = JMAGIC);
jnode_set_type(node, type);
atomic_set(&node->d_count, 0);
atomic_set(&node->x_count, 0);
spin_lock_init(&node->guard);
spin_lock_init(&node->load);
node->atom = NULL;
node->tree = tree;
INIT_LIST_HEAD(&node->capture_link);
ASSIGN_NODE_LIST(node, NOT_CAPTURED);
#if REISER4_DEBUG
{
reiser4_super_info_data *sbinfo;
sbinfo = get_super_private(tree->super);
spin_lock_irq(&sbinfo->all_guard);
list_add(&node->jnodes, &sbinfo->all_jnodes);
spin_unlock_irq(&sbinfo->all_guard);
}
#endif
}
#if REISER4_DEBUG
/*
* Remove jnode from ->all_jnodes list.
*/
static void jnode_done(jnode * node, reiser4_tree * tree)
{
reiser4_super_info_data *sbinfo;
sbinfo = get_super_private(tree->super);
spin_lock_irq(&sbinfo->all_guard);
assert("nikita-2422", !list_empty(&node->jnodes));
list_del_init(&node->jnodes);
spin_unlock_irq(&sbinfo->all_guard);
}
#endif
/* return already existing jnode of page */
jnode *jnode_by_page(struct page *pg)
{
assert("nikita-2400", PageLocked(pg));
assert("nikita-2068", PagePrivate(pg));
assert("nikita-2067", jprivate(pg) != NULL);
return jprivate(pg);
}
/* exported functions to allocate/free jnode objects outside this file */
jnode *jalloc(void)
{
jnode *jal = kmem_cache_alloc(_jnode_slab, reiser4_ctx_gfp_mask_get());
return jal;
}
/* return jnode back to the slab allocator */
inline void jfree(jnode * node)
{
assert("nikita-2663", (list_empty_careful(&node->capture_link) &&
NODE_LIST(node) == NOT_CAPTURED));
assert("nikita-3222", list_empty(&node->jnodes));
assert("nikita-3221", jnode_page(node) == NULL);
/* not yet phash_jnode_destroy(node); */
kmem_cache_free(_jnode_slab, node);
}
/*
* This function is supplied as RCU callback. It actually frees jnode when
* last reference to it is gone.
*/
static void jnode_free_actor(struct rcu_head *head)
{
jnode *node;
jnode_type jtype;
node = container_of(head, jnode, rcu);
jtype = jnode_get_type(node);
ON_DEBUG(jnode_done(node, jnode_get_tree(node)));
switch (jtype) {
case JNODE_IO_HEAD:
case JNODE_BITMAP:
case JNODE_UNFORMATTED_BLOCK:
jfree(node);
break;
case JNODE_FORMATTED_BLOCK:
zfree(JZNODE(node));
break;
case JNODE_INODE:
default:
wrong_return_value("nikita-3197", "Wrong jnode type");
}
}
/*
* Free a jnode. Post a callback to be executed later through RCU when all
* references to @node are released.
*/
static inline void jnode_free(jnode * node, jnode_type jtype)
{
if (jtype != JNODE_INODE) {
/*assert("nikita-3219", list_empty(&node->rcu.list)); */
call_rcu(&node->rcu, jnode_free_actor);
} else
jnode_list_remove(node);
}
/* allocate new unformatted jnode */
static jnode *jnew_unformatted(void)
{
jnode *jal;
jal = jalloc();
if (jal == NULL)
return NULL;
jnode_init(jal, current_tree, JNODE_UNFORMATTED_BLOCK);
jal->key.j.mapping = NULL;
jal->key.j.index = (unsigned long)-1;
jal->key.j.objectid = 0;
return jal;
}
/* look for jnode with given mapping and offset within hash table */
jnode *jlookup(reiser4_tree * tree, oid_t objectid, unsigned long index)
{
struct jnode_key jkey;
jnode *node;
jkey.objectid = objectid;
jkey.index = index;
/*
* hash table is _not_ protected by any lock during lookups. All we
* have to do is to disable preemption to keep RCU happy.
*/
rcu_read_lock();
node = j_hash_find(&tree->jhash_table, &jkey);
if (node != NULL) {
/* protect @node from recycling */
jref(node);
assert("nikita-2955", jnode_invariant(node, 0, 0));
node = jnode_rip_check(tree, node);
}
rcu_read_unlock();
return node;
}
/* per inode radix tree of jnodes is protected by tree's read write spin lock */
static jnode *jfind_nolock(struct address_space *mapping, unsigned long index)
{
assert("vs-1694", mapping->host != NULL);
return radix_tree_lookup(jnode_tree_by_inode(mapping->host), index);
}
jnode *jfind(struct address_space *mapping, unsigned long index)
{
reiser4_tree *tree;
jnode *node;
assert("vs-1694", mapping->host != NULL);
tree = reiser4_tree_by_inode(mapping->host);
read_lock_tree(tree);
node = jfind_nolock(mapping, index);
if (node != NULL)
jref(node);
read_unlock_tree(tree);
return node;
}
static void inode_attach_jnode(jnode * node)
{
struct inode *inode;
reiser4_inode *info;
struct radix_tree_root *rtree;
assert_rw_write_locked(&(jnode_get_tree(node)->tree_lock));
assert("zam-1043", node->key.j.mapping != NULL);
inode = node->key.j.mapping->host;
info = reiser4_inode_data(inode);
rtree = jnode_tree_by_reiser4_inode(info);
if (radix_tree_empty(rtree)) {
/* prevent inode from being pruned when it has jnodes attached
to it */
xa_lock_irq(&inode->i_data.i_pages);
inode->i_data.nrpages++;
xa_unlock_irq(&inode->i_data.i_pages);
}
assert("zam-1049",
equi(!radix_tree_empty(rtree), info->nr_jnodes != 0));
check_me("zam-1045",
!radix_tree_insert(rtree, node->key.j.index, node));
ON_DEBUG(info->nr_jnodes++);
}
static void inode_detach_jnode(jnode * node)
{
struct inode *inode;
reiser4_inode *info;
struct radix_tree_root *rtree;
assert_rw_write_locked(&(jnode_get_tree(node)->tree_lock));
assert("zam-1044", node->key.j.mapping != NULL);
inode = node->key.j.mapping->host;
info = reiser4_inode_data(inode);
rtree = jnode_tree_by_reiser4_inode(info);
assert("zam-1051", info->nr_jnodes != 0);
assert("zam-1052", !radix_tree_empty(rtree));
ON_DEBUG(info->nr_jnodes--);
/* delete jnode from inode's radix tree of jnodes */
check_me("zam-1046", radix_tree_delete(rtree, node->key.j.index));
if (radix_tree_empty(rtree)) {
/* inode can be pruned now */
xa_lock_irq(&inode->i_data.i_pages);
inode->i_data.nrpages--;
xa_unlock_irq(&inode->i_data.i_pages);
}
}
/* put jnode into hash table (where they can be found by flush who does not know
mapping) and to inode's tree of jnodes (where they can be found (hopefully
faster) in places where mapping is known). Currently it is used by
fs/reiser4/plugin/item/extent_file_ops.c:index_extent_jnode when new jnode is
created */
static void
hash_unformatted_jnode(jnode * node, struct address_space *mapping,
unsigned long index)
{
j_hash_table *jtable;
assert("vs-1446", jnode_is_unformatted(node));
assert("vs-1442", node->key.j.mapping == 0);
assert("vs-1443", node->key.j.objectid == 0);
assert("vs-1444", node->key.j.index == (unsigned long)-1);
assert_rw_write_locked(&(jnode_get_tree(node)->tree_lock));
node->key.j.mapping = mapping;
node->key.j.objectid = get_inode_oid(mapping->host);
node->key.j.index = index;
jtable = &jnode_get_tree(node)->jhash_table;
/* race with some other thread inserting jnode into the hash table is
* impossible, because we keep the page lock. */
/*
* following assertion no longer holds because of RCU: it is possible
* jnode is in the hash table, but with JNODE_RIP bit set.
*/
/* assert("nikita-3211", j_hash_find(jtable, &node->key.j) == NULL); */
j_hash_insert_rcu(jtable, node);
inode_attach_jnode(node);
}
static void unhash_unformatted_node_nolock(jnode * node)
{
assert("vs-1683", node->key.j.mapping != NULL);
assert("vs-1684",
node->key.j.objectid ==
get_inode_oid(node->key.j.mapping->host));
/* remove jnode from hash-table */
j_hash_remove_rcu(&node->tree->jhash_table, node);
inode_detach_jnode(node);
node->key.j.mapping = NULL;
node->key.j.index = (unsigned long)-1;
node->key.j.objectid = 0;
}
/* remove jnode from hash table and from inode's tree of jnodes. This is used in
reiser4_invalidatepage and in kill_hook_extent -> truncate_inode_jnodes ->
reiser4_uncapture_jnode */
void unhash_unformatted_jnode(jnode * node)
{
assert("vs-1445", jnode_is_unformatted(node));
write_lock_tree(node->tree);
unhash_unformatted_node_nolock(node);
write_unlock_tree(node->tree);
}
/*
* search hash table for a jnode with given oid and index. If not found,
* allocate new jnode, insert it, and also insert into radix tree for the
* given inode/mapping.
*/
static jnode *find_get_jnode(reiser4_tree * tree,
struct address_space *mapping,
oid_t oid, unsigned long index)
{
jnode *result;
jnode *shadow;
int preload;
result = jnew_unformatted();
if (unlikely(result == NULL))
return ERR_PTR(RETERR(-ENOMEM));
preload = radix_tree_preload(reiser4_ctx_gfp_mask_get());
if (preload != 0)
return ERR_PTR(preload);
write_lock_tree(tree);
shadow = jfind_nolock(mapping, index);
if (likely(shadow == NULL)) {
/* add new jnode to hash table and inode's radix tree of
* jnodes */
jref(result);
hash_unformatted_jnode(result, mapping, index);
} else {
/* jnode is found in inode's radix tree of jnodes */
jref(shadow);
jnode_free(result, JNODE_UNFORMATTED_BLOCK);
assert("vs-1498", shadow->key.j.mapping == mapping);
result = shadow;
}
write_unlock_tree(tree);
assert("nikita-2955",
ergo(result != NULL, jnode_invariant(result, 0, 0)));
radix_tree_preload_end();
return result;
}
/* jget() (a la zget() but for unformatted nodes). Returns (and possibly
creates) jnode corresponding to page @pg. jnode is attached to page and
inserted into jnode hash-table. */
static jnode *do_jget(reiser4_tree * tree, struct page *pg)
{
/*
* There are two ways to create jnode: starting with pre-existing page
* and without page.
*
* When page already exists, jnode is created
* (jnode_of_page()->do_jget()) under page lock. This is done in
* ->writepage(), or when capturing anonymous page dirtied through
* mmap.
*
* Jnode without page is created by index_extent_jnode().
*
*/
jnode *result;
oid_t oid = get_inode_oid(pg->mapping->host);
assert("umka-176", pg != NULL);
assert("nikita-2394", PageLocked(pg));
result = jprivate(pg);
if (likely(result != NULL))
return jref(result);
tree = reiser4_tree_by_page(pg);
/* check hash-table first */
result = jfind(pg->mapping, pg->index);
if (unlikely(result != NULL)) {
spin_lock_jnode(result);
jnode_attach_page(result, pg);
spin_unlock_jnode(result);
result->key.j.mapping = pg->mapping;
return result;
}
/* since page is locked, jnode should be allocated with GFP_NOFS flag */
reiser4_ctx_gfp_mask_force(GFP_NOFS);
result = find_get_jnode(tree, pg->mapping, oid, pg->index);
if (unlikely(IS_ERR(result)))
return result;
/* attach jnode to page */
spin_lock_jnode(result);
jnode_attach_page(result, pg);
spin_unlock_jnode(result);
return result;
}
/*
* return jnode for @pg, creating it if necessary.
*/
jnode *jnode_of_page(struct page *pg)
{
jnode *result;
assert("nikita-2394", PageLocked(pg));
result = do_jget(reiser4_tree_by_page(pg), pg);
if (REISER4_DEBUG && !IS_ERR(result)) {
assert("nikita-3210", result == jprivate(pg));
assert("nikita-2046", jnode_page(jprivate(pg)) == pg);
if (jnode_is_unformatted(jprivate(pg))) {
assert("nikita-2364",
jprivate(pg)->key.j.index == pg->index);
assert("nikita-2367",
jprivate(pg)->key.j.mapping == pg->mapping);
assert("nikita-2365",
jprivate(pg)->key.j.objectid ==
get_inode_oid(pg->mapping->host));
assert("vs-1200",
jprivate(pg)->key.j.objectid ==
pg->mapping->host->i_ino);
assert("nikita-2356",
jnode_is_unformatted(jnode_by_page(pg)));
}
assert("nikita-2956", jnode_invariant(jprivate(pg), 0, 0));
}
return result;
}
/* attach page to jnode: set ->pg pointer in jnode, and ->private one in the
* page.*/
void jnode_attach_page(jnode * node, struct page *pg)
{
assert("nikita-2060", node != NULL);
assert("nikita-2061", pg != NULL);
assert("nikita-2050", jprivate(pg) == 0ul);
assert("nikita-2393", !PagePrivate(pg));
assert("vs-1741", node->pg == NULL);
assert("nikita-2396", PageLocked(pg));
assert_spin_locked(&(node->guard));
get_page(pg);
set_page_private(pg, (unsigned long)node);
node->pg = pg;
SetPagePrivate(pg);
}
/* Dual to jnode_attach_page: break a binding between page and jnode */
void page_clear_jnode(struct page *page, jnode * node)
{
assert("nikita-2425", PageLocked(page));
assert_spin_locked(&(node->guard));
assert("nikita-2428", PagePrivate(page));
assert("nikita-3551", !PageWriteback(page));
JF_CLR(node, JNODE_PARSED);
set_page_private(page, 0ul);
ClearPagePrivate(page);
node->pg = NULL;
put_page(page);
}
#if 0
/* it is only used in one place to handle error */
void
page_detach_jnode(struct page *page, struct address_space *mapping,
unsigned long index)
{
assert("nikita-2395", page != NULL);
lock_page(page);
if ((page->mapping == mapping) && (page->index == index)
&& PagePrivate(page)) {
jnode *node;
node = jprivate(page);
spin_lock_jnode(node);
page_clear_jnode(page, node);
spin_unlock_jnode(node);
}
unlock_page(page);
}
#endif /* 0 */
/* return @node page locked.
Locking ordering requires that one first takes page lock and afterwards
spin lock on node attached to this page. Sometimes it is necessary to go in
the opposite direction. This is done through standard trylock-and-release
loop.
*/
static struct page *jnode_lock_page(jnode * node)
{
struct page *page;
assert("nikita-2052", node != NULL);
assert("nikita-2401", LOCK_CNT_NIL(spin_locked_jnode));
while (1) {
spin_lock_jnode(node);
page = jnode_page(node);
if (page == NULL)
break;
/* no need to get_page( page ) here, because page cannot
be evicted from memory without detaching it from jnode and
this requires spin lock on jnode that we already hold.
*/
if (trylock_page(page)) {
/* We won a lock on jnode page, proceed. */
break;
}
/* Page is locked by someone else. */
get_page(page);
spin_unlock_jnode(node);
wait_on_page_locked(page);
/* it is possible that page was detached from jnode and
returned to the free pool, or re-assigned while we were
waiting on locked bit. This will be rechecked on the next
loop iteration.
*/
put_page(page);
/* try again */
}
return page;
}
/*
* is JNODE_PARSED bit is not set, call ->parse() method of jnode, to verify
* validness of jnode content.
*/
static inline int jparse(jnode * node)
{
int result;
assert("nikita-2466", node != NULL);
spin_lock_jnode(node);
if (likely(!jnode_is_parsed(node))) {
result = jnode_ops(node)->parse(node);
if (likely(result == 0))
JF_SET(node, JNODE_PARSED);
} else
result = 0;
spin_unlock_jnode(node);
return result;
}
/* Lock a page attached to jnode, create and attach page to jnode if it had no
* one. */
static struct page *jnode_get_page_locked(jnode * node, gfp_t gfp_flags)
{
struct page *page;
spin_lock_jnode(node);
page = jnode_page(node);
if (page == NULL) {
spin_unlock_jnode(node);
page = find_or_create_page(jnode_get_mapping(node),
jnode_get_index(node), gfp_flags);
if (page == NULL)
return ERR_PTR(RETERR(-ENOMEM));
} else {
if (trylock_page(page)) {
spin_unlock_jnode(node);
return page;
}
get_page(page);
spin_unlock_jnode(node);
lock_page(page);
assert("nikita-3134", page->mapping == jnode_get_mapping(node));
}
spin_lock_jnode(node);
if (!jnode_page(node))
jnode_attach_page(node, page);
spin_unlock_jnode(node);
put_page(page);
assert("zam-894", jnode_page(node) == page);
return page;
}
/* Start read operation for jnode's page if page is not up-to-date. */
static int jnode_start_read(jnode * node, struct page *page)
{
assert("zam-893", PageLocked(page));
if (PageUptodate(page)) {
unlock_page(page);
return 0;
}
return reiser4_page_io(page, node, READ, reiser4_ctx_gfp_mask_get());
}
#if REISER4_DEBUG
static void check_jload(jnode * node, struct page *page)
{
if (jnode_is_znode(node)) {
znode *z = JZNODE(node);
if (znode_is_any_locked(z)) {
assert("nikita-3253",
z->nr_items ==
node_plugin_by_node(z)->num_of_items(z));
kunmap(page);
}
assert("nikita-3565", znode_invariant(z));
}
}
#else
#define check_jload(node, page) noop
#endif
/* prefetch jnode to speed up next call to jload. Call this when you are going
* to call jload() shortly. This will bring appropriate portion of jnode into
* CPU cache. */
void jload_prefetch(jnode * node)
{
prefetchw(&node->x_count);
}
/* load jnode's data into memory */
int jload_gfp(jnode * node /* node to load */ ,
gfp_t gfp_flags /* allocation flags */ ,
int do_kmap/* true if page should be kmapped */)
{
struct page *page;
int result = 0;
int parsed;
assert("nikita-3010", reiser4_schedulable());
prefetchw(&node->pg);
/* taking d-reference implies taking x-reference. */
jref(node);
/*
* acquiring d-reference to @jnode and check for JNODE_PARSED bit
* should be atomic, otherwise there is a race against
* reiser4_releasepage().
*/
spin_lock(&(node->load));
add_d_ref(node);
parsed = jnode_is_parsed(node);
spin_unlock(&(node->load));
if (unlikely(!parsed)) {
page = jnode_get_page_locked(node, gfp_flags);
if (unlikely(IS_ERR(page))) {
result = PTR_ERR(page);
goto failed;
}
result = jnode_start_read(node, page);
if (unlikely(result != 0))
goto failed;
wait_on_page_locked(page);
if (unlikely(!PageUptodate(page))) {
result = RETERR(-EIO);
goto failed;
}
if (do_kmap)
node->data = kmap(page);
result = jparse(node);
if (unlikely(result != 0)) {
if (do_kmap)
kunmap(page);
goto failed;
}
check_jload(node, page);
} else {
page = jnode_page(node);
check_jload(node, page);
if (do_kmap)
node->data = kmap(page);
}
if (!is_writeout_mode())
/* We do not mark pages active if jload is called as a part of
* jnode_flush() or reiser4_write_logs(). Both jnode_flush()
* and write_logs() add no value to cached data, there is no
* sense to mark pages as active when they go to disk, it just
* confuses vm scanning routines because clean page could be
* moved out from inactive list as a result of this
* mark_page_accessed() call. */
mark_page_accessed(page);
return 0;
failed:
jrelse_tail(node);
return result;
}
/* start asynchronous reading for given jnode's page. */
int jstartio(jnode * node)
{
struct page *page;
page = jnode_get_page_locked(node, reiser4_ctx_gfp_mask_get());
if (IS_ERR(page))
return PTR_ERR(page);
return jnode_start_read(node, page);
}
/* Initialize a node by calling appropriate plugin instead of reading
* node from disk as in jload(). */
int jinit_new(jnode * node, gfp_t gfp_flags)
{
struct page *page;
int result;
jref(node);
add_d_ref(node);
page = jnode_get_page_locked(node, gfp_flags);
if (IS_ERR(page)) {
result = PTR_ERR(page);
goto failed;
}
SetPageUptodate(page);
unlock_page(page);
node->data = kmap(page);
if (!jnode_is_parsed(node)) {
jnode_plugin *jplug = jnode_ops(node);
spin_lock_jnode(node);
result = jplug->init(node);
spin_unlock_jnode(node);
if (result) {
kunmap(page);
goto failed;
}
JF_SET(node, JNODE_PARSED);
}
return 0;
failed:
jrelse(node);
return result;
}
/* release a reference to jnode acquired by jload(), decrement ->d_count */
void jrelse_tail(jnode * node/* jnode to release references to */)
{
assert("nikita-489", atomic_read(&node->d_count) > 0);
atomic_dec(&node->d_count);
/* release reference acquired in jload_gfp() or jinit_new() */
if (jnode_is_unformatted(node) || jnode_is_znode(node))
LOCK_CNT_DEC(d_refs);
jput(node);
}
/* drop reference to node data. When last reference is dropped, data are
unloaded. */
void jrelse(jnode * node/* jnode to release references to */)
{