18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright 1999 Hans Reiser, see reiserfs/README for licensing and copyright 48c2ecf20Sopenharmony_ci * details 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/time.h> 88c2ecf20Sopenharmony_ci#include <linux/pagemap.h> 98c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> 108c2ecf20Sopenharmony_ci#include "reiserfs.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci/* 138c2ecf20Sopenharmony_ci * access to tail : when one is going to read tail it must make sure, that is 148c2ecf20Sopenharmony_ci * not running. direct2indirect and indirect2direct can not run concurrently 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci * Converts direct items to an unformatted node. Panics if file has no 198c2ecf20Sopenharmony_ci * tail. -ENOSPC if no disk space for conversion 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * path points to first direct item of the file regardless of how many of 238c2ecf20Sopenharmony_ci * them are there 248c2ecf20Sopenharmony_ci */ 258c2ecf20Sopenharmony_ciint direct2indirect(struct reiserfs_transaction_handle *th, struct inode *inode, 268c2ecf20Sopenharmony_ci struct treepath *path, struct buffer_head *unbh, 278c2ecf20Sopenharmony_ci loff_t tail_offset) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci struct super_block *sb = inode->i_sb; 308c2ecf20Sopenharmony_ci struct buffer_head *up_to_date_bh; 318c2ecf20Sopenharmony_ci struct item_head *p_le_ih = tp_item_head(path); 328c2ecf20Sopenharmony_ci unsigned long total_tail = 0; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci /* Key to search for the last byte of the converted item. */ 358c2ecf20Sopenharmony_ci struct cpu_key end_key; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* 388c2ecf20Sopenharmony_ci * new indirect item to be inserted or key 398c2ecf20Sopenharmony_ci * of unfm pointer to be pasted 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_ci struct item_head ind_ih; 428c2ecf20Sopenharmony_ci int blk_size; 438c2ecf20Sopenharmony_ci /* returned value for reiserfs_insert_item and clones */ 448c2ecf20Sopenharmony_ci int retval; 458c2ecf20Sopenharmony_ci /* Handle on an unformatted node that will be inserted in the tree. */ 468c2ecf20Sopenharmony_ci unp_t unfm_ptr; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci BUG_ON(!th->t_trans_id); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci REISERFS_SB(sb)->s_direct2indirect++; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci blk_size = sb->s_blocksize; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* 558c2ecf20Sopenharmony_ci * and key to search for append or insert pointer to the new 568c2ecf20Sopenharmony_ci * unformatted node. 578c2ecf20Sopenharmony_ci */ 588c2ecf20Sopenharmony_ci copy_item_head(&ind_ih, p_le_ih); 598c2ecf20Sopenharmony_ci set_le_ih_k_offset(&ind_ih, tail_offset); 608c2ecf20Sopenharmony_ci set_le_ih_k_type(&ind_ih, TYPE_INDIRECT); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci /* Set the key to search for the place for new unfm pointer */ 638c2ecf20Sopenharmony_ci make_cpu_key(&end_key, inode, tail_offset, TYPE_INDIRECT, 4); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* FIXME: we could avoid this */ 668c2ecf20Sopenharmony_ci if (search_for_position_by_key(sb, &end_key, path) == POSITION_FOUND) { 678c2ecf20Sopenharmony_ci reiserfs_error(sb, "PAP-14030", 688c2ecf20Sopenharmony_ci "pasted or inserted byte exists in " 698c2ecf20Sopenharmony_ci "the tree %K. Use fsck to repair.", &end_key); 708c2ecf20Sopenharmony_ci pathrelse(path); 718c2ecf20Sopenharmony_ci return -EIO; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci p_le_ih = tp_item_head(path); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci unfm_ptr = cpu_to_le32(unbh->b_blocknr); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci if (is_statdata_le_ih(p_le_ih)) { 798c2ecf20Sopenharmony_ci /* Insert new indirect item. */ 808c2ecf20Sopenharmony_ci set_ih_free_space(&ind_ih, 0); /* delete at nearest future */ 818c2ecf20Sopenharmony_ci put_ih_item_len(&ind_ih, UNFM_P_SIZE); 828c2ecf20Sopenharmony_ci PATH_LAST_POSITION(path)++; 838c2ecf20Sopenharmony_ci retval = 848c2ecf20Sopenharmony_ci reiserfs_insert_item(th, path, &end_key, &ind_ih, inode, 858c2ecf20Sopenharmony_ci (char *)&unfm_ptr); 868c2ecf20Sopenharmony_ci } else { 878c2ecf20Sopenharmony_ci /* Paste into last indirect item of an object. */ 888c2ecf20Sopenharmony_ci retval = reiserfs_paste_into_item(th, path, &end_key, inode, 898c2ecf20Sopenharmony_ci (char *)&unfm_ptr, 908c2ecf20Sopenharmony_ci UNFM_P_SIZE); 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci if (retval) { 938c2ecf20Sopenharmony_ci return retval; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci /* 968c2ecf20Sopenharmony_ci * note: from here there are two keys which have matching first 978c2ecf20Sopenharmony_ci * three key components. They only differ by the fourth one. 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* Set the key to search for the direct items of the file */ 1018c2ecf20Sopenharmony_ci make_cpu_key(&end_key, inode, max_reiserfs_offset(inode), TYPE_DIRECT, 1028c2ecf20Sopenharmony_ci 4); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* 1058c2ecf20Sopenharmony_ci * Move bytes from the direct items to the new unformatted node 1068c2ecf20Sopenharmony_ci * and delete them. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_ci while (1) { 1098c2ecf20Sopenharmony_ci int tail_size; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* 1128c2ecf20Sopenharmony_ci * end_key.k_offset is set so, that we will always have found 1138c2ecf20Sopenharmony_ci * last item of the file 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_ci if (search_for_position_by_key(sb, &end_key, path) == 1168c2ecf20Sopenharmony_ci POSITION_FOUND) 1178c2ecf20Sopenharmony_ci reiserfs_panic(sb, "PAP-14050", 1188c2ecf20Sopenharmony_ci "direct item (%K) not found", &end_key); 1198c2ecf20Sopenharmony_ci p_le_ih = tp_item_head(path); 1208c2ecf20Sopenharmony_ci RFALSE(!is_direct_le_ih(p_le_ih), 1218c2ecf20Sopenharmony_ci "vs-14055: direct item expected(%K), found %h", 1228c2ecf20Sopenharmony_ci &end_key, p_le_ih); 1238c2ecf20Sopenharmony_ci tail_size = (le_ih_k_offset(p_le_ih) & (blk_size - 1)) 1248c2ecf20Sopenharmony_ci + ih_item_len(p_le_ih) - 1; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* 1278c2ecf20Sopenharmony_ci * we only send the unbh pointer if the buffer is not 1288c2ecf20Sopenharmony_ci * up to date. this avoids overwriting good data from 1298c2ecf20Sopenharmony_ci * writepage() with old data from the disk or buffer cache 1308c2ecf20Sopenharmony_ci * Special case: unbh->b_page will be NULL if we are coming 1318c2ecf20Sopenharmony_ci * through DIRECT_IO handler here. 1328c2ecf20Sopenharmony_ci */ 1338c2ecf20Sopenharmony_ci if (!unbh->b_page || buffer_uptodate(unbh) 1348c2ecf20Sopenharmony_ci || PageUptodate(unbh->b_page)) { 1358c2ecf20Sopenharmony_ci up_to_date_bh = NULL; 1368c2ecf20Sopenharmony_ci } else { 1378c2ecf20Sopenharmony_ci up_to_date_bh = unbh; 1388c2ecf20Sopenharmony_ci } 1398c2ecf20Sopenharmony_ci retval = reiserfs_delete_item(th, path, &end_key, inode, 1408c2ecf20Sopenharmony_ci up_to_date_bh); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci total_tail += retval; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* done: file does not have direct items anymore */ 1458c2ecf20Sopenharmony_ci if (tail_size == retval) 1468c2ecf20Sopenharmony_ci break; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci /* 1508c2ecf20Sopenharmony_ci * if we've copied bytes from disk into the page, we need to zero 1518c2ecf20Sopenharmony_ci * out the unused part of the block (it was not up to date before) 1528c2ecf20Sopenharmony_ci */ 1538c2ecf20Sopenharmony_ci if (up_to_date_bh) { 1548c2ecf20Sopenharmony_ci unsigned pgoff = 1558c2ecf20Sopenharmony_ci (tail_offset + total_tail - 1) & (PAGE_SIZE - 1); 1568c2ecf20Sopenharmony_ci char *kaddr = kmap_atomic(up_to_date_bh->b_page); 1578c2ecf20Sopenharmony_ci memset(kaddr + pgoff, 0, blk_size - total_tail); 1588c2ecf20Sopenharmony_ci kunmap_atomic(kaddr); 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci REISERFS_I(inode)->i_first_direct_byte = U32_MAX; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci return 0; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci/* stolen from fs/buffer.c */ 1678c2ecf20Sopenharmony_civoid reiserfs_unmap_buffer(struct buffer_head *bh) 1688c2ecf20Sopenharmony_ci{ 1698c2ecf20Sopenharmony_ci lock_buffer(bh); 1708c2ecf20Sopenharmony_ci if (buffer_journaled(bh) || buffer_journal_dirty(bh)) { 1718c2ecf20Sopenharmony_ci BUG(); 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci clear_buffer_dirty(bh); 1748c2ecf20Sopenharmony_ci /* 1758c2ecf20Sopenharmony_ci * Remove the buffer from whatever list it belongs to. We are mostly 1768c2ecf20Sopenharmony_ci * interested in removing it from per-sb j_dirty_buffers list, to avoid 1778c2ecf20Sopenharmony_ci * BUG() on attempt to write not mapped buffer 1788c2ecf20Sopenharmony_ci */ 1798c2ecf20Sopenharmony_ci if ((!list_empty(&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) { 1808c2ecf20Sopenharmony_ci struct inode *inode = bh->b_page->mapping->host; 1818c2ecf20Sopenharmony_ci struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb); 1828c2ecf20Sopenharmony_ci spin_lock(&j->j_dirty_buffers_lock); 1838c2ecf20Sopenharmony_ci list_del_init(&bh->b_assoc_buffers); 1848c2ecf20Sopenharmony_ci reiserfs_free_jh(bh); 1858c2ecf20Sopenharmony_ci spin_unlock(&j->j_dirty_buffers_lock); 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci clear_buffer_mapped(bh); 1888c2ecf20Sopenharmony_ci clear_buffer_req(bh); 1898c2ecf20Sopenharmony_ci clear_buffer_new(bh); 1908c2ecf20Sopenharmony_ci bh->b_bdev = NULL; 1918c2ecf20Sopenharmony_ci unlock_buffer(bh); 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci/* 1958c2ecf20Sopenharmony_ci * this first locks inode (neither reads nor sync are permitted), 1968c2ecf20Sopenharmony_ci * reads tail through page cache, insert direct item. When direct item 1978c2ecf20Sopenharmony_ci * inserted successfully inode is left locked. Return value is always 1988c2ecf20Sopenharmony_ci * what we expect from it (number of cut bytes). But when tail remains 1998c2ecf20Sopenharmony_ci * in the unformatted node, we set mode to SKIP_BALANCING and unlock 2008c2ecf20Sopenharmony_ci * inode 2018c2ecf20Sopenharmony_ci */ 2028c2ecf20Sopenharmony_ciint indirect2direct(struct reiserfs_transaction_handle *th, 2038c2ecf20Sopenharmony_ci struct inode *inode, struct page *page, 2048c2ecf20Sopenharmony_ci struct treepath *path, /* path to the indirect item. */ 2058c2ecf20Sopenharmony_ci const struct cpu_key *item_key, /* Key to look for 2068c2ecf20Sopenharmony_ci * unformatted node 2078c2ecf20Sopenharmony_ci * pointer to be cut. */ 2088c2ecf20Sopenharmony_ci loff_t n_new_file_size, /* New file size. */ 2098c2ecf20Sopenharmony_ci char *mode) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci struct super_block *sb = inode->i_sb; 2128c2ecf20Sopenharmony_ci struct item_head s_ih; 2138c2ecf20Sopenharmony_ci unsigned long block_size = sb->s_blocksize; 2148c2ecf20Sopenharmony_ci char *tail; 2158c2ecf20Sopenharmony_ci int tail_len, round_tail_len; 2168c2ecf20Sopenharmony_ci loff_t pos, pos1; /* position of first byte of the tail */ 2178c2ecf20Sopenharmony_ci struct cpu_key key; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci BUG_ON(!th->t_trans_id); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci REISERFS_SB(sb)->s_indirect2direct++; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci *mode = M_SKIP_BALANCING; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci /* store item head path points to. */ 2268c2ecf20Sopenharmony_ci copy_item_head(&s_ih, tp_item_head(path)); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci tail_len = (n_new_file_size & (block_size - 1)); 2298c2ecf20Sopenharmony_ci if (get_inode_sd_version(inode) == STAT_DATA_V2) 2308c2ecf20Sopenharmony_ci round_tail_len = ROUND_UP(tail_len); 2318c2ecf20Sopenharmony_ci else 2328c2ecf20Sopenharmony_ci round_tail_len = tail_len; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci pos = 2358c2ecf20Sopenharmony_ci le_ih_k_offset(&s_ih) - 1 + (ih_item_len(&s_ih) / UNFM_P_SIZE - 2368c2ecf20Sopenharmony_ci 1) * sb->s_blocksize; 2378c2ecf20Sopenharmony_ci pos1 = pos; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci /* 2408c2ecf20Sopenharmony_ci * we are protected by i_mutex. The tail can not disapper, not 2418c2ecf20Sopenharmony_ci * append can be done either 2428c2ecf20Sopenharmony_ci * we are in truncate or packing tail in file_release 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci tail = (char *)kmap(page); /* this can schedule */ 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci if (path_changed(&s_ih, path)) { 2488c2ecf20Sopenharmony_ci /* re-search indirect item */ 2498c2ecf20Sopenharmony_ci if (search_for_position_by_key(sb, item_key, path) 2508c2ecf20Sopenharmony_ci == POSITION_NOT_FOUND) 2518c2ecf20Sopenharmony_ci reiserfs_panic(sb, "PAP-5520", 2528c2ecf20Sopenharmony_ci "item to be converted %K does not exist", 2538c2ecf20Sopenharmony_ci item_key); 2548c2ecf20Sopenharmony_ci copy_item_head(&s_ih, tp_item_head(path)); 2558c2ecf20Sopenharmony_ci#ifdef CONFIG_REISERFS_CHECK 2568c2ecf20Sopenharmony_ci pos = le_ih_k_offset(&s_ih) - 1 + 2578c2ecf20Sopenharmony_ci (ih_item_len(&s_ih) / UNFM_P_SIZE - 2588c2ecf20Sopenharmony_ci 1) * sb->s_blocksize; 2598c2ecf20Sopenharmony_ci if (pos != pos1) 2608c2ecf20Sopenharmony_ci reiserfs_panic(sb, "vs-5530", "tail position " 2618c2ecf20Sopenharmony_ci "changed while we were reading it"); 2628c2ecf20Sopenharmony_ci#endif 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* Set direct item header to insert. */ 2668c2ecf20Sopenharmony_ci make_le_item_head(&s_ih, NULL, get_inode_item_key_version(inode), 2678c2ecf20Sopenharmony_ci pos1 + 1, TYPE_DIRECT, round_tail_len, 2688c2ecf20Sopenharmony_ci 0xffff /*ih_free_space */ ); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* 2718c2ecf20Sopenharmony_ci * we want a pointer to the first byte of the tail in the page. 2728c2ecf20Sopenharmony_ci * the page was locked and this part of the page was up to date when 2738c2ecf20Sopenharmony_ci * indirect2direct was called, so we know the bytes are still valid 2748c2ecf20Sopenharmony_ci */ 2758c2ecf20Sopenharmony_ci tail = tail + (pos & (PAGE_SIZE - 1)); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci PATH_LAST_POSITION(path)++; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci key = *item_key; 2808c2ecf20Sopenharmony_ci set_cpu_key_k_type(&key, TYPE_DIRECT); 2818c2ecf20Sopenharmony_ci key.key_length = 4; 2828c2ecf20Sopenharmony_ci /* Insert tail as new direct item in the tree */ 2838c2ecf20Sopenharmony_ci if (reiserfs_insert_item(th, path, &key, &s_ih, inode, 2848c2ecf20Sopenharmony_ci tail ? tail : NULL) < 0) { 2858c2ecf20Sopenharmony_ci /* 2868c2ecf20Sopenharmony_ci * No disk memory. So we can not convert last unformatted node 2878c2ecf20Sopenharmony_ci * to the direct item. In this case we used to adjust 2888c2ecf20Sopenharmony_ci * indirect items's ih_free_space. Now ih_free_space is not 2898c2ecf20Sopenharmony_ci * used, it would be ideal to write zeros to corresponding 2908c2ecf20Sopenharmony_ci * unformatted node. For now i_size is considered as guard for 2918c2ecf20Sopenharmony_ci * going out of file size 2928c2ecf20Sopenharmony_ci */ 2938c2ecf20Sopenharmony_ci kunmap(page); 2948c2ecf20Sopenharmony_ci return block_size - round_tail_len; 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci kunmap(page); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci /* make sure to get the i_blocks changes from reiserfs_insert_item */ 2998c2ecf20Sopenharmony_ci reiserfs_update_sd(th, inode); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci /* 3028c2ecf20Sopenharmony_ci * note: we have now the same as in above direct2indirect 3038c2ecf20Sopenharmony_ci * conversion: there are two keys which have matching first three 3048c2ecf20Sopenharmony_ci * key components. They only differ by the fourth one. 3058c2ecf20Sopenharmony_ci */ 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci /* 3088c2ecf20Sopenharmony_ci * We have inserted new direct item and must remove last 3098c2ecf20Sopenharmony_ci * unformatted node. 3108c2ecf20Sopenharmony_ci */ 3118c2ecf20Sopenharmony_ci *mode = M_CUT; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci /* we store position of first direct item in the in-core inode */ 3148c2ecf20Sopenharmony_ci /* mark_file_with_tail (inode, pos1 + 1); */ 3158c2ecf20Sopenharmony_ci REISERFS_I(inode)->i_first_direct_byte = pos1 + 1; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci return block_size - round_tail_len; 3188c2ecf20Sopenharmony_ci} 319