GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / reiserfs / tail_conversion.c
blobe21403d853ac8fef012e0043501d774b1257bd0f
1 /*
2 * Copyright 1999 Hans Reiser, see reiserfs/README for licensing and copyright details
3 */
5 #include <linux/time.h>
6 #include <linux/pagemap.h>
7 #include <linux/buffer_head.h>
8 #include <linux/reiserfs_fs.h>
10 /* access to tail : when one is going to read tail it must make sure, that is not running.
11 direct2indirect and indirect2direct can not run concurrently */
13 /* Converts direct items to an unformatted node. Panics if file has no
14 tail. -ENOSPC if no disk space for conversion */
15 /* path points to first direct item of the file regarless of how many of
16 them are there */
17 int direct2indirect(struct reiserfs_transaction_handle *th, struct inode *inode,
18 struct treepath *path, struct buffer_head *unbh,
19 loff_t tail_offset)
21 struct super_block *sb = inode->i_sb;
22 struct buffer_head *up_to_date_bh;
23 struct item_head *p_le_ih = PATH_PITEM_HEAD(path);
24 unsigned long total_tail = 0;
25 struct cpu_key end_key; /* Key to search for the last byte of the
26 converted item. */
27 struct item_head ind_ih; /* new indirect item to be inserted or
28 key of unfm pointer to be pasted */
29 int blk_size, retval; /* returned value for reiserfs_insert_item and clones */
30 unp_t unfm_ptr; /* Handle on an unformatted node
31 that will be inserted in the
32 tree. */
34 BUG_ON(!th->t_trans_id);
36 REISERFS_SB(sb)->s_direct2indirect++;
38 blk_size = sb->s_blocksize;
40 /* and key to search for append or insert pointer to the new
41 unformatted node. */
42 copy_item_head(&ind_ih, p_le_ih);
43 set_le_ih_k_offset(&ind_ih, tail_offset);
44 set_le_ih_k_type(&ind_ih, TYPE_INDIRECT);
46 /* Set the key to search for the place for new unfm pointer */
47 make_cpu_key(&end_key, inode, tail_offset, TYPE_INDIRECT, 4);
49 if (search_for_position_by_key(sb, &end_key, path) == POSITION_FOUND) {
50 reiserfs_error(sb, "PAP-14030",
51 "pasted or inserted byte exists in "
52 "the tree %K. Use fsck to repair.", &end_key);
53 pathrelse(path);
54 return -EIO;
57 p_le_ih = PATH_PITEM_HEAD(path);
59 unfm_ptr = cpu_to_le32(unbh->b_blocknr);
61 if (is_statdata_le_ih(p_le_ih)) {
62 /* Insert new indirect item. */
63 set_ih_free_space(&ind_ih, 0); /* delete at nearest future */
64 put_ih_item_len(&ind_ih, UNFM_P_SIZE);
65 PATH_LAST_POSITION(path)++;
66 retval =
67 reiserfs_insert_item(th, path, &end_key, &ind_ih, inode,
68 (char *)&unfm_ptr);
69 } else {
70 /* Paste into last indirect item of an object. */
71 retval = reiserfs_paste_into_item(th, path, &end_key, inode,
72 (char *)&unfm_ptr,
73 UNFM_P_SIZE);
75 if (retval) {
76 return retval;
78 // note: from here there are two keys which have matching first
79 // three key components. They only differ by the fourth one.
81 /* Set the key to search for the direct items of the file */
82 make_cpu_key(&end_key, inode, max_reiserfs_offset(inode), TYPE_DIRECT,
83 4);
85 /* Move bytes from the direct items to the new unformatted node
86 and delete them. */
87 while (1) {
88 int tail_size;
90 /* end_key.k_offset is set so, that we will always have found
91 last item of the file */
92 if (search_for_position_by_key(sb, &end_key, path) ==
93 POSITION_FOUND)
94 reiserfs_panic(sb, "PAP-14050",
95 "direct item (%K) not found", &end_key);
96 p_le_ih = PATH_PITEM_HEAD(path);
97 RFALSE(!is_direct_le_ih(p_le_ih),
98 "vs-14055: direct item expected(%K), found %h",
99 &end_key, p_le_ih);
100 tail_size = (le_ih_k_offset(p_le_ih) & (blk_size - 1))
101 + ih_item_len(p_le_ih) - 1;
103 /* we only send the unbh pointer if the buffer is not up to date.
104 ** this avoids overwriting good data from writepage() with old data
105 ** from the disk or buffer cache
106 ** Special case: unbh->b_page will be NULL if we are coming through
107 ** DIRECT_IO handler here.
109 if (!unbh->b_page || buffer_uptodate(unbh)
110 || PageUptodate(unbh->b_page)) {
111 up_to_date_bh = NULL;
112 } else {
113 up_to_date_bh = unbh;
115 retval = reiserfs_delete_item(th, path, &end_key, inode,
116 up_to_date_bh);
118 total_tail += retval;
119 if (tail_size == retval)
120 // done: file does not have direct items anymore
121 break;
124 /* if we've copied bytes from disk into the page, we need to zero
125 ** out the unused part of the block (it was not up to date before)
127 if (up_to_date_bh) {
128 unsigned pgoff =
129 (tail_offset + total_tail - 1) & (PAGE_CACHE_SIZE - 1);
130 char *kaddr = kmap_atomic(up_to_date_bh->b_page, KM_USER0);
131 memset(kaddr + pgoff, 0, blk_size - total_tail);
132 kunmap_atomic(kaddr, KM_USER0);
135 REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
137 return 0;
140 /* stolen from fs/buffer.c */
141 void reiserfs_unmap_buffer(struct buffer_head *bh)
143 lock_buffer(bh);
144 if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
145 BUG();
147 clear_buffer_dirty(bh);
148 /* Remove the buffer from whatever list it belongs to. We are mostly
149 interested in removing it from per-sb j_dirty_buffers list, to avoid
150 BUG() on attempt to write not mapped buffer */
151 if ((!list_empty(&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) {
152 struct inode *inode = bh->b_page->mapping->host;
153 struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
154 spin_lock(&j->j_dirty_buffers_lock);
155 list_del_init(&bh->b_assoc_buffers);
156 reiserfs_free_jh(bh);
157 spin_unlock(&j->j_dirty_buffers_lock);
159 clear_buffer_mapped(bh);
160 clear_buffer_req(bh);
161 clear_buffer_new(bh);
162 bh->b_bdev = NULL;
163 unlock_buffer(bh);
166 /* this first locks inode (neither reads nor sync are permitted),
167 reads tail through page cache, insert direct item. When direct item
168 inserted successfully inode is left locked. Return value is always
169 what we expect from it (number of cut bytes). But when tail remains
170 in the unformatted node, we set mode to SKIP_BALANCING and unlock
171 inode */
172 int indirect2direct(struct reiserfs_transaction_handle *th,
173 struct inode *inode, struct page *page,
174 struct treepath *path, /* path to the indirect item. */
175 const struct cpu_key *item_key, /* Key to look for
176 * unformatted node
177 * pointer to be cut. */
178 loff_t n_new_file_size, /* New file size. */
179 char *mode)
181 struct super_block *sb = inode->i_sb;
182 struct item_head s_ih;
183 unsigned long block_size = sb->s_blocksize;
184 char *tail;
185 int tail_len, round_tail_len;
186 loff_t pos, pos1; /* position of first byte of the tail */
187 struct cpu_key key;
189 BUG_ON(!th->t_trans_id);
191 REISERFS_SB(sb)->s_indirect2direct++;
193 *mode = M_SKIP_BALANCING;
195 /* store item head path points to. */
196 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
198 tail_len = (n_new_file_size & (block_size - 1));
199 if (get_inode_sd_version(inode) == STAT_DATA_V2)
200 round_tail_len = ROUND_UP(tail_len);
201 else
202 round_tail_len = tail_len;
204 pos =
205 le_ih_k_offset(&s_ih) - 1 + (ih_item_len(&s_ih) / UNFM_P_SIZE -
206 1) * sb->s_blocksize;
207 pos1 = pos;
209 // we are protected by i_mutex. The tail can not disapper, not
210 // append can be done either
211 // we are in truncate or packing tail in file_release
213 tail = (char *)kmap(page); /* this can schedule */
215 if (path_changed(&s_ih, path)) {
216 /* re-search indirect item */
217 if (search_for_position_by_key(sb, item_key, path)
218 == POSITION_NOT_FOUND)
219 reiserfs_panic(sb, "PAP-5520",
220 "item to be converted %K does not exist",
221 item_key);
222 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
223 #ifdef CONFIG_REISERFS_CHECK
224 pos = le_ih_k_offset(&s_ih) - 1 +
225 (ih_item_len(&s_ih) / UNFM_P_SIZE -
226 1) * sb->s_blocksize;
227 if (pos != pos1)
228 reiserfs_panic(sb, "vs-5530", "tail position "
229 "changed while we were reading it");
230 #endif
233 /* Set direct item header to insert. */
234 make_le_item_head(&s_ih, NULL, get_inode_item_key_version(inode),
235 pos1 + 1, TYPE_DIRECT, round_tail_len,
236 0xffff /*ih_free_space */ );
238 /* we want a pointer to the first byte of the tail in the page.
239 ** the page was locked and this part of the page was up to date when
240 ** indirect2direct was called, so we know the bytes are still valid
242 tail = tail + (pos & (PAGE_CACHE_SIZE - 1));
244 PATH_LAST_POSITION(path)++;
246 key = *item_key;
247 set_cpu_key_k_type(&key, TYPE_DIRECT);
248 key.key_length = 4;
249 /* Insert tail as new direct item in the tree */
250 if (reiserfs_insert_item(th, path, &key, &s_ih, inode,
251 tail ? tail : NULL) < 0) {
252 /* No disk memory. So we can not convert last unformatted node
253 to the direct item. In this case we used to adjust
254 indirect items's ih_free_space. Now ih_free_space is not
255 used, it would be ideal to write zeros to corresponding
256 unformatted node. For now i_size is considered as guard for
257 going out of file size */
258 kunmap(page);
259 return block_size - round_tail_len;
261 kunmap(page);
263 /* make sure to get the i_blocks changes from reiserfs_insert_item */
264 reiserfs_update_sd(th, inode);
266 // note: we have now the same as in above direct2indirect
267 // conversion: there are two keys which have matching first three
268 // key components. They only differ by the fouhth one.
270 /* We have inserted new direct item and must remove last
271 unformatted node. */
272 *mode = M_CUT;
274 /* we store position of first direct item in the in-core inode */
275 /* mark_file_with_tail (inode, pos1 + 1); */
276 REISERFS_I(inode)->i_first_direct_byte = pos1 + 1;
278 return block_size - round_tail_len;