mm-only debug patch...
[mmotm.git] / fs / reiser4 / plugin / cluster.h
blobe3c8294d9457a2d5c973eeb72d33b23bb909d3d7
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__
9 #include "../inode.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
122 * by @count elements
124 static inline unsigned long size_in_blocks(loff_t count, unsigned blkbits)
126 return (count + (1UL << blkbits) - 1) >> blkbits;
129 /* size in pages */
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),
155 and square @size.
157 ******
158 *******
159 *******
160 *******
161 ----------> pos
163 static inline unsigned __mbb(loff_t size, unsigned long pos, int blkbits)
165 unsigned end = size >> blkbits;
166 if (pos < end)
167 return 1U << blkbits;
168 if (unlikely(pos > end))
169 return 0;
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);
192 * number of pages occuped by @win->count bytes starting from
193 * @win->off at logical cluster defined by @win. This is exactly
194 * a number of pages to be modified and dirtied in any cluster operation.
196 static inline pgoff_t win_count_to_nrpages(struct reiser4_slide * win)
198 return ((win->off + win->count +
199 (1UL << PAGE_CACHE_SHIFT) - 1) >> PAGE_CACHE_SHIFT) -
200 off_to_pg(win->off);
203 /* return true, if logical cluster is not occupied by the file */
204 static inline int new_logical_cluster(struct cluster_handle *clust,
205 struct inode *inode)
207 return clust_to_off(clust->index, inode) >= i_size_read(inode);
210 /* return true, if pages @p1 and @p2 are of the same page cluster */
211 static inline int same_page_cluster(struct page *p1, struct page *p2)
213 assert("edward-1490", p1 != NULL);
214 assert("edward-1491", p2 != NULL);
215 assert("edward-1492", p1->mapping != NULL);
216 assert("edward-1493", p2->mapping != NULL);
218 return (pg_to_clust(page_index(p1), p1->mapping->host) ==
219 pg_to_clust(page_index(p2), p2->mapping->host));
222 static inline int cluster_is_complete(struct cluster_handle *clust,
223 struct inode *inode)
225 return clust->tc.lsize == inode_cluster_size(inode);
228 static inline void reiser4_slide_init(struct reiser4_slide *win)
230 assert("edward-1084", win != NULL);
231 memset(win, 0, sizeof *win);
234 static inline tfm_action
235 cluster_get_tfm_act(struct tfm_cluster *tc)
237 assert("edward-1356", tc != NULL);
238 return tc->act;
241 static inline void
242 cluster_set_tfm_act(struct tfm_cluster *tc, tfm_action act)
244 assert("edward-1356", tc != NULL);
245 tc->act = act;
248 static inline void cluster_init_act(struct cluster_handle *clust,
249 tfm_action act,
250 struct reiser4_slide *window)
252 assert("edward-84", clust != NULL);
253 memset(clust, 0, sizeof *clust);
254 cluster_set_tfm_act(&clust->tc, act);
255 clust->dstat = INVAL_DISK_CLUSTER;
256 clust->win = window;
259 static inline void cluster_init_read(struct cluster_handle *clust,
260 struct reiser4_slide *window)
262 cluster_init_act(clust, TFMA_READ, window);
265 static inline void cluster_init_write(struct cluster_handle *clust,
266 struct reiser4_slide *window)
268 cluster_init_act(clust, TFMA_WRITE, window);
271 /* true if @p1 and @p2 are items of the same disk cluster */
272 static inline int same_disk_cluster(const coord_t *p1, const coord_t *p2)
274 /* drop this if you have other items to aggregate */
275 assert("edward-1494", item_id_by_coord(p1) == CTAIL_ID);
277 return item_plugin_by_coord(p1)->b.mergeable(p1, p2);
280 static inline int dclust_get_extension_dsize(hint_t *hint)
282 return hint->ext_coord.extension.ctail.dsize;
285 static inline void dclust_set_extension_dsize(hint_t *hint, int dsize)
287 hint->ext_coord.extension.ctail.dsize = dsize;
290 static inline int dclust_get_extension_shift(hint_t *hint)
292 return hint->ext_coord.extension.ctail.shift;
295 static inline int dclust_get_extension_ncount(hint_t *hint)
297 return hint->ext_coord.extension.ctail.ncount;
300 static inline void dclust_inc_extension_ncount(hint_t *hint)
302 hint->ext_coord.extension.ctail.ncount++;
305 static inline void dclust_init_extension(hint_t *hint)
307 memset(&hint->ext_coord.extension.ctail, 0,
308 sizeof(hint->ext_coord.extension.ctail));
311 static inline int hint_is_unprepped_dclust(hint_t *hint)
313 assert("edward-1451", hint_is_valid(hint));
314 return dclust_get_extension_shift(hint) == (int)UCTAIL_SHIFT;
317 static inline void coord_set_between_clusters(coord_t *coord)
319 #if REISER4_DEBUG
320 int result;
321 result = zload(coord->node);
322 assert("edward-1296", !result);
323 #endif
324 if (!coord_is_between_items(coord)) {
325 coord->between = AFTER_ITEM;
326 coord->unit_pos = 0;
328 #if REISER4_DEBUG
329 zrelse(coord->node);
330 #endif
333 int reiser4_inflate_cluster(struct cluster_handle *, struct inode *);
334 int find_disk_cluster(struct cluster_handle *, struct inode *, int read,
335 znode_lock_mode mode);
336 int checkout_logical_cluster(struct cluster_handle *, jnode * , struct inode *);
337 int reiser4_deflate_cluster(struct cluster_handle *, struct inode *);
338 void truncate_complete_page_cluster(struct inode *inode, cloff_t start,
339 int even_cows);
340 void invalidate_hint_cluster(struct cluster_handle *clust);
341 int get_disk_cluster_locked(struct cluster_handle *clust, struct inode *inode,
342 znode_lock_mode lock_mode);
343 void reset_cluster_params(struct cluster_handle *clust);
344 int set_cluster_by_page(struct cluster_handle *clust, struct page *page,
345 int count);
346 int prepare_page_cluster(struct inode *inode, struct cluster_handle *clust,
347 rw_op rw);
348 void __put_page_cluster(int from, int count, struct page **pages,
349 struct inode *inode);
350 void put_page_cluster(struct cluster_handle *clust,
351 struct inode *inode, rw_op rw);
352 void put_cluster_handle(struct cluster_handle *clust);
353 int grab_tfm_stream(struct inode *inode, struct tfm_cluster *tc,
354 tfm_stream_id id);
355 int tfm_cluster_is_uptodate(struct tfm_cluster *tc);
356 void tfm_cluster_set_uptodate(struct tfm_cluster *tc);
357 void tfm_cluster_clr_uptodate(struct tfm_cluster *tc);
359 /* move cluster handle to the target position
360 specified by the page of index @pgidx */
361 static inline void move_cluster_forward(struct cluster_handle *clust,
362 struct inode *inode,
363 pgoff_t pgidx)
365 assert("edward-1297", clust != NULL);
366 assert("edward-1298", inode != NULL);
368 reset_cluster_params(clust);
369 if (clust->index_valid &&
370 /* Hole in the indices. Hint became invalid and can not be
371 used by find_cluster_item() even if seal/node versions
372 will coincide */
373 pg_to_clust(pgidx, inode) != clust->index + 1) {
374 reiser4_unset_hint(clust->hint);
375 invalidate_hint_cluster(clust);
377 clust->index = pg_to_clust(pgidx, inode);
378 clust->index_valid = 1;
381 static inline int alloc_clust_pages(struct cluster_handle *clust,
382 struct inode *inode)
384 assert("edward-791", clust != NULL);
385 assert("edward-792", inode != NULL);
386 clust->pages =
387 kmalloc(sizeof(*clust->pages) << inode_cluster_shift(inode),
388 reiser4_ctx_gfp_mask_get());
389 if (!clust->pages)
390 return -ENOMEM;
391 return 0;
394 static inline void free_clust_pages(struct cluster_handle *clust)
396 kfree(clust->pages);
399 #endif /* __FS_REISER4_CLUSTER_H__ */
401 /* Make Linus happy.
402 Local variables:
403 c-indentation-style: "K&R"
404 mode-name: "LC"
405 c-basic-offset: 8
406 tab-width: 8
407 fill-column: 120
408 scroll-step: 1
409 End: