revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / plugin / cluster.h
blobaf7a3054eb067f6b693b18aff2a061f9316b15af
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);
191 /* return true, if logical cluster is not occupied by the file */
192 static inline int new_logical_cluster(struct cluster_handle * clust,
193 struct inode *inode)
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);
226 return tc->act;
229 static inline void
230 cluster_set_tfm_act(struct tfm_cluster * tc, tfm_action act)
232 assert("edward-1356", tc != NULL);
233 tc->act = act;
236 static inline void cluster_init_act(struct cluster_handle * clust,
237 tfm_action act,
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;
244 clust->win = window;
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)
307 #if REISER4_DEBUG
308 int result;
309 result = zload(coord->node);
310 assert("edward-1296", !result);
311 #endif
312 if (!coord_is_between_items(coord)) {
313 coord->between = AFTER_ITEM;
314 coord->unit_pos = 0;
316 #if REISER4_DEBUG
317 zrelse(coord->node);
318 #endif
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,
327 int even_cows);
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,
333 int count);
334 int prepare_page_cluster(struct inode *inode, struct cluster_handle * clust,
335 rw_op rw);
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,
347 struct inode *inode,
348 pgoff_t pgidx)
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
357 will coincide */
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,
367 struct inode *inode)
369 assert("edward-791", clust != NULL);
370 assert("edward-792", inode != NULL);
371 clust->pages =
372 kmalloc(sizeof(*clust->pages) << inode_cluster_shift(inode),
373 reiser4_ctx_gfp_mask_get());
374 if (!clust->pages)
375 return -ENOMEM;
376 return 0;
379 static inline void free_clust_pages(struct cluster_handle * clust)
381 kfree(clust->pages);
384 #endif /* __FS_REISER4_CLUSTER_H__ */
386 /* Make Linus happy.
387 Local variables:
388 c-indentation-style: "K&R"
389 mode-name: "LC"
390 c-basic-offset: 8
391 tab-width: 8
392 fill-column: 120
393 scroll-step: 1
394 End: