1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
12 #include <linux/swap.h> /* for totalram_pages */
14 void reiser4_init_ra_info(ra_info_t
*rai
)
16 rai
->key_to_stop
= *reiser4_min_key();
19 /* global formatted node readahead parameter. It can be set by mount option
20 * -o readahead:NUM:1 */
21 static inline int ra_adjacent_only(int flags
)
23 return flags
& RA_ADJACENT_ONLY
;
26 /* this is used by formatted_readahead to decide whether read for right neighbor
27 * of node is to be issued. It returns 1 if right neighbor's first key is less
28 * or equal to readahead's stop key */
29 static int should_readahead_neighbor(znode
* node
, ra_info_t
*info
)
33 read_lock_dk(znode_get_tree(node
));
34 result
= keyle(znode_get_rd_key(node
), &info
->key_to_stop
);
35 read_unlock_dk(znode_get_tree(node
));
39 #define LOW_MEM_PERCENTAGE (5)
41 static int low_on_memory(void)
43 unsigned int freepages
;
45 freepages
= nr_free_pages();
46 return freepages
< (totalram_pages
* LOW_MEM_PERCENTAGE
/ 100);
49 /* start read for @node and for a few of its right neighbors */
50 void formatted_readahead(znode
* node
, ra_info_t
*info
)
52 struct formatted_ra_params
*ra_params
;
58 /* do nothing if node block number has not been assigned to node (which
59 * means it is still in cache). */
60 if (reiser4_blocknr_is_fake(znode_get_block(node
)))
63 ra_params
= get_current_super_ra_params();
65 if (znode_page(node
) == NULL
)
66 jstartio(ZJNODE(node
));
68 if (znode_get_level(node
) != LEAF_LEVEL
)
71 /* don't waste memory for read-ahead when low on memory */
75 /* We can have locked nodes on upper tree levels, in this situation lock
76 priorities do not help to resolve deadlocks, we have to use TRY_LOCK
78 grn_flags
= (GN_CAN_USE_UPPER_LEVELS
| GN_TRY_LOCK
);
83 while (i
< ra_params
->max
) {
84 const reiser4_block_nr
* nextblk
;
86 if (!should_readahead_neighbor(cur
, info
))
89 if (reiser4_get_right_neighbor
90 (&next_lh
, cur
, ZNODE_READ_LOCK
, grn_flags
))
93 nextblk
= znode_get_block(next_lh
.node
);
94 if (reiser4_blocknr_is_fake(nextblk
) ||
95 (ra_adjacent_only(ra_params
->flags
)
96 && *nextblk
!= *znode_get_block(cur
) + 1))
100 cur
= zref(next_lh
.node
);
102 if (znode_page(cur
) == NULL
)
103 jstartio(ZJNODE(cur
));
105 /* Do not scan read-ahead window if pages already
106 * allocated (and i/o already started). */
115 void reiser4_readdir_readahead_init(struct inode
*dir
, tap_t
*tap
)
117 reiser4_key
*stop_key
;
119 assert("nikita-3542", dir
!= NULL
);
120 assert("nikita-3543", tap
!= NULL
);
122 stop_key
= &tap
->ra_info
.key_to_stop
;
123 /* initialize readdir readahead information: include into readahead
124 * stat data of all files of the directory */
125 set_key_locality(stop_key
, get_inode_oid(dir
));
126 set_key_type(stop_key
, KEY_SD_MINOR
);
127 set_key_ordering(stop_key
, get_key_ordering(reiser4_max_key()));
128 set_key_objectid(stop_key
, get_key_objectid(reiser4_max_key()));
129 set_key_offset(stop_key
, get_key_offset(reiser4_max_key()));
134 c-indentation-style: "K&R"