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