1 /* Copyright 2002, 2003 by Hans Reiser, licensing governed by reiser4/README */
3 /* This file contains size/offset translators, modulators
4 and other helper functions. */
6 #if !defined( __FS_REISER4_CLUSTER_H__ )
7 #define __FS_REISER4_CLUSTER_H__
11 static inline int inode_cluster_shift(struct inode
*inode
)
13 assert("edward-92", inode
!= NULL
);
14 assert("edward-93", reiser4_inode_data(inode
) != NULL
);
16 return inode_cluster_plugin(inode
)->shift
;
19 static inline unsigned cluster_nrpages_shift(struct inode
*inode
)
21 return inode_cluster_shift(inode
) - PAGE_CACHE_SHIFT
;
24 /* cluster size in page units */
25 static inline unsigned cluster_nrpages(struct inode
*inode
)
27 return 1U << cluster_nrpages_shift(inode
);
30 static inline size_t inode_cluster_size(struct inode
*inode
)
32 assert("edward-96", inode
!= NULL
);
34 return 1U << inode_cluster_shift(inode
);
37 static inline cloff_t
pg_to_clust(pgoff_t idx
, struct inode
*inode
)
39 return idx
>> cluster_nrpages_shift(inode
);
42 static inline pgoff_t
clust_to_pg(cloff_t idx
, struct inode
*inode
)
44 return idx
<< cluster_nrpages_shift(inode
);
47 static inline pgoff_t
pg_to_clust_to_pg(pgoff_t idx
, struct inode
*inode
)
49 return clust_to_pg(pg_to_clust(idx
, inode
), inode
);
52 static inline pgoff_t
off_to_pg(loff_t off
)
54 return (off
>> PAGE_CACHE_SHIFT
);
57 static inline loff_t
pg_to_off(pgoff_t idx
)
59 return ((loff_t
) (idx
) << PAGE_CACHE_SHIFT
);
62 static inline cloff_t
off_to_clust(loff_t off
, struct inode
*inode
)
64 return off
>> inode_cluster_shift(inode
);
67 static inline loff_t
clust_to_off(cloff_t idx
, struct inode
*inode
)
69 return (loff_t
) idx
<< inode_cluster_shift(inode
);
72 static inline loff_t
off_to_clust_to_off(loff_t off
, struct inode
*inode
)
74 return clust_to_off(off_to_clust(off
, inode
), inode
);
77 static inline pgoff_t
off_to_clust_to_pg(loff_t off
, struct inode
*inode
)
79 return clust_to_pg(off_to_clust(off
, inode
), inode
);
82 static inline unsigned off_to_pgoff(loff_t off
)
84 return off
& (PAGE_CACHE_SIZE
- 1);
87 static inline unsigned off_to_cloff(loff_t off
, struct inode
*inode
)
89 return off
& ((loff_t
) (inode_cluster_size(inode
)) - 1);
92 static inline pgoff_t
offset_in_clust(struct page
* page
)
94 assert("edward-1488", page
!= NULL
);
95 assert("edward-1489", page
->mapping
!= NULL
);
97 return page_index(page
) & ((cluster_nrpages(page
->mapping
->host
)) - 1);
100 static inline int first_page_in_cluster(struct page
* page
)
102 return offset_in_clust(page
) == 0;
105 static inline int last_page_in_cluster(struct page
* page
)
107 return offset_in_clust(page
) ==
108 cluster_nrpages(page
->mapping
->host
) - 1;
111 static inline unsigned
112 pg_to_off_to_cloff(unsigned long idx
, struct inode
*inode
)
114 return off_to_cloff(pg_to_off(idx
), inode
);
117 /*********************** Size translators **************************/
119 /* Translate linear size.
120 * New units are (1 << @blk_shift) times larger, then old ones.
121 * In other words, calculate number of logical blocks, occupied
124 static inline unsigned long size_in_blocks(loff_t count
, unsigned blkbits
)
126 return (count
+ (1UL << blkbits
) - 1) >> blkbits
;
130 static inline pgoff_t
size_in_pages(loff_t size
)
132 return size_in_blocks(size
, PAGE_CACHE_SHIFT
);
135 /* size in logical clusters */
136 static inline cloff_t
size_in_lc(loff_t size
, struct inode
*inode
)
138 return size_in_blocks(size
, inode_cluster_shift(inode
));
141 /* size in pages to the size in page clusters */
142 static inline cloff_t
sp_to_spcl(pgoff_t size
, struct inode
*inode
)
144 return size_in_blocks(size
, cluster_nrpages_shift(inode
));
147 /*********************** Size modulators ***************************/
150 Modulate linear size by nominated block size and offset.
152 The "finite" function (which is zero almost everywhere).
153 How much is a height of the figure at a position @pos,
154 when trying to construct rectangle of height (1 << @blkbits),
163 static inline unsigned __mbb(loff_t size
, unsigned long pos
, int blkbits
)
165 unsigned end
= size
>> blkbits
;
167 return 1U << blkbits
;
168 if (unlikely(pos
> end
))
170 return size
& ~(~0ull << blkbits
);
173 /* the same as above, but block size is page size */
174 static inline unsigned __mbp(loff_t size
, pgoff_t pos
)
176 return __mbb(size
, pos
, PAGE_CACHE_SHIFT
);
179 /* number of file's bytes in the nominated logical cluster */
180 static inline unsigned lbytes(cloff_t index
, struct inode
* inode
)
182 return __mbb(i_size_read(inode
), index
, inode_cluster_shift(inode
));
185 /* number of file's bytes in the nominated page */
186 static inline unsigned pbytes(pgoff_t index
, struct inode
* inode
)
188 return __mbp(i_size_read(inode
), index
);
191 /* return true, if logical cluster is not occupied by the file */
192 static inline int new_logical_cluster(struct cluster_handle
* clust
,
195 return clust_to_off(clust
->index
, inode
) >= i_size_read(inode
);
198 /* return true, if pages @p1 and @p2 are of the same page cluster */
199 static inline int same_page_cluster(struct page
* p1
, struct page
* p2
)
201 assert("edward-1490", p1
!= NULL
);
202 assert("edward-1491", p2
!= NULL
);
203 assert("edward-1492", p1
->mapping
!= NULL
);
204 assert("edward-1493", p2
->mapping
!= NULL
);
206 return (pg_to_clust(page_index(p1
), p1
->mapping
->host
) ==
207 pg_to_clust(page_index(p2
), p2
->mapping
->host
));
210 static inline int cluster_is_complete(struct cluster_handle
* clust
,
211 struct inode
* inode
)
213 return clust
->tc
.lsize
== inode_cluster_size(inode
);
216 static inline void reiser4_slide_init(struct reiser4_slide
* win
)
218 assert("edward-1084", win
!= NULL
);
219 memset(win
, 0, sizeof *win
);
222 static inline tfm_action
223 cluster_get_tfm_act(struct tfm_cluster
* tc
)
225 assert("edward-1356", tc
!= NULL
);
230 cluster_set_tfm_act(struct tfm_cluster
* tc
, tfm_action act
)
232 assert("edward-1356", tc
!= NULL
);
236 static inline void cluster_init_act(struct cluster_handle
* clust
,
238 struct reiser4_slide
* window
)
240 assert("edward-84", clust
!= NULL
);
241 memset(clust
, 0, sizeof *clust
);
242 cluster_set_tfm_act(&clust
->tc
, act
);
243 clust
->dstat
= INVAL_DISK_CLUSTER
;
247 static inline void cluster_init_read(struct cluster_handle
* clust
,
248 struct reiser4_slide
* window
)
250 cluster_init_act (clust
, TFMA_READ
, window
);
253 static inline void cluster_init_write(struct cluster_handle
* clust
,
254 struct reiser4_slide
* window
)
256 cluster_init_act (clust
, TFMA_WRITE
, window
);
259 /* true if @p1 and @p2 are items of the same disk cluster */
260 static inline int same_disk_cluster(const coord_t
* p1
, const coord_t
* p2
)
262 /* drop this if you have other items to aggregate */
263 assert("edward-1494", item_id_by_coord(p1
) == CTAIL_ID
);
265 return item_plugin_by_coord(p1
)->b
.mergeable(p1
, p2
);
268 static inline int dclust_get_extension_dsize(hint_t
* hint
)
270 return hint
->ext_coord
.extension
.ctail
.dsize
;
273 static inline void dclust_set_extension_dsize(hint_t
* hint
, int dsize
)
275 hint
->ext_coord
.extension
.ctail
.dsize
= dsize
;
278 static inline int dclust_get_extension_shift(hint_t
* hint
)
280 return hint
->ext_coord
.extension
.ctail
.shift
;
283 static inline int dclust_get_extension_ncount(hint_t
* hint
)
285 return hint
->ext_coord
.extension
.ctail
.ncount
;
288 static inline void dclust_inc_extension_ncount(hint_t
* hint
)
290 hint
->ext_coord
.extension
.ctail
.ncount
++;
293 static inline void dclust_init_extension(hint_t
* hint
)
295 memset(&hint
->ext_coord
.extension
.ctail
, 0,
296 sizeof(hint
->ext_coord
.extension
.ctail
));
299 static inline int hint_is_unprepped_dclust(hint_t
* hint
)
301 assert("edward-1451", hint_is_valid(hint
));
302 return dclust_get_extension_shift(hint
) == (int)UCTAIL_SHIFT
;
305 static inline void coord_set_between_clusters(coord_t
* coord
)
309 result
= zload(coord
->node
);
310 assert("edward-1296", !result
);
312 if (!coord_is_between_items(coord
)) {
313 coord
->between
= AFTER_ITEM
;
321 int reiser4_inflate_cluster(struct cluster_handle
*, struct inode
*);
322 int find_disk_cluster(struct cluster_handle
*, struct inode
*, int read
,
323 znode_lock_mode mode
);
324 int checkout_logical_cluster(struct cluster_handle
*, jnode
*, struct inode
*);
325 int reiser4_deflate_cluster(struct cluster_handle
*, struct inode
*);
326 void truncate_complete_page_cluster(struct inode
*inode
, cloff_t start
,
328 void invalidate_hint_cluster(struct cluster_handle
* clust
);
329 int get_disk_cluster_locked(struct cluster_handle
* clust
, struct inode
* inode
,
330 znode_lock_mode lock_mode
);
331 void reset_cluster_params(struct cluster_handle
* clust
);
332 int set_cluster_by_page(struct cluster_handle
* clust
, struct page
* page
,
334 int prepare_page_cluster(struct inode
*inode
, struct cluster_handle
* clust
,
336 void put_page_cluster(struct cluster_handle
* clust
,
337 struct inode
* inode
, rw_op rw
);
338 void put_cluster_handle(struct cluster_handle
* clust
);
339 int grab_tfm_stream(struct inode
*inode
, struct tfm_cluster
* tc
, tfm_stream_id id
);
340 int tfm_cluster_is_uptodate(struct tfm_cluster
* tc
);
341 void tfm_cluster_set_uptodate(struct tfm_cluster
* tc
);
342 void tfm_cluster_clr_uptodate(struct tfm_cluster
* tc
);
344 /* move cluster handle to the target position
345 specified by the page of index @pgidx */
346 static inline void move_cluster_forward(struct cluster_handle
* clust
,
350 assert("edward-1297", clust
!= NULL
);
351 assert("edward-1298", inode
!= NULL
);
353 reset_cluster_params(clust
);
354 if (clust
->index_valid
&&
355 /* Hole in the indices. Hint became invalid and can not be
356 used by find_cluster_item() even if seal/node versions
358 pg_to_clust(pgidx
, inode
) != clust
->index
+ 1) {
359 reiser4_unset_hint(clust
->hint
);
360 invalidate_hint_cluster(clust
);
362 clust
->index
= pg_to_clust(pgidx
, inode
);
363 clust
->index_valid
= 1;
366 static inline int alloc_clust_pages(struct cluster_handle
* clust
,
369 assert("edward-791", clust
!= NULL
);
370 assert("edward-792", inode
!= NULL
);
372 kmalloc(sizeof(*clust
->pages
) << inode_cluster_shift(inode
),
373 reiser4_ctx_gfp_mask_get());
379 static inline void free_clust_pages(struct cluster_handle
* clust
)
384 #endif /* __FS_REISER4_CLUSTER_H__ */
388 c-indentation-style: "K&R"