162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README 362306a36Sopenharmony_ci */ 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci#include <linux/time.h> 662306a36Sopenharmony_ci#include "reiserfs.h" 762306a36Sopenharmony_ci#include "acl.h" 862306a36Sopenharmony_ci#include "xattr.h" 962306a36Sopenharmony_ci#include <linux/uaccess.h> 1062306a36Sopenharmony_ci#include <linux/pagemap.h> 1162306a36Sopenharmony_ci#include <linux/swap.h> 1262306a36Sopenharmony_ci#include <linux/writeback.h> 1362306a36Sopenharmony_ci#include <linux/blkdev.h> 1462306a36Sopenharmony_ci#include <linux/buffer_head.h> 1562306a36Sopenharmony_ci#include <linux/quotaops.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci/* 1862306a36Sopenharmony_ci * We pack the tails of files on file close, not at the time they are written. 1962306a36Sopenharmony_ci * This implies an unnecessary copy of the tail and an unnecessary indirect item 2062306a36Sopenharmony_ci * insertion/balancing, for files that are written in one write. 2162306a36Sopenharmony_ci * It avoids unnecessary tail packings (balances) for files that are written in 2262306a36Sopenharmony_ci * multiple writes and are small enough to have tails. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * file_release is called by the VFS layer when the file is closed. If 2562306a36Sopenharmony_ci * this is the last open file descriptor, and the file 2662306a36Sopenharmony_ci * small enough to have a tail, and the tail is currently in an 2762306a36Sopenharmony_ci * unformatted node, the tail is converted back into a direct item. 2862306a36Sopenharmony_ci * 2962306a36Sopenharmony_ci * We use reiserfs_truncate_file to pack the tail, since it already has 3062306a36Sopenharmony_ci * all the conditions coded. 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_cistatic int reiserfs_file_release(struct inode *inode, struct file *filp) 3362306a36Sopenharmony_ci{ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci struct reiserfs_transaction_handle th; 3662306a36Sopenharmony_ci int err; 3762306a36Sopenharmony_ci int jbegin_failure = 0; 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci BUG_ON(!S_ISREG(inode->i_mode)); 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers, 4262306a36Sopenharmony_ci &REISERFS_I(inode)->tailpack)) 4362306a36Sopenharmony_ci return 0; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci /* fast out for when nothing needs to be done */ 4662306a36Sopenharmony_ci if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) || 4762306a36Sopenharmony_ci !tail_has_to_be_packed(inode)) && 4862306a36Sopenharmony_ci REISERFS_I(inode)->i_prealloc_count <= 0) { 4962306a36Sopenharmony_ci mutex_unlock(&REISERFS_I(inode)->tailpack); 5062306a36Sopenharmony_ci return 0; 5162306a36Sopenharmony_ci } 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci reiserfs_write_lock(inode->i_sb); 5462306a36Sopenharmony_ci /* 5562306a36Sopenharmony_ci * freeing preallocation only involves relogging blocks that 5662306a36Sopenharmony_ci * are already in the current transaction. preallocation gets 5762306a36Sopenharmony_ci * freed at the end of each transaction, so it is impossible for 5862306a36Sopenharmony_ci * us to log any additional blocks (including quota blocks) 5962306a36Sopenharmony_ci */ 6062306a36Sopenharmony_ci err = journal_begin(&th, inode->i_sb, 1); 6162306a36Sopenharmony_ci if (err) { 6262306a36Sopenharmony_ci /* 6362306a36Sopenharmony_ci * uh oh, we can't allow the inode to go away while there 6462306a36Sopenharmony_ci * is still preallocation blocks pending. Try to join the 6562306a36Sopenharmony_ci * aborted transaction 6662306a36Sopenharmony_ci */ 6762306a36Sopenharmony_ci jbegin_failure = err; 6862306a36Sopenharmony_ci err = journal_join_abort(&th, inode->i_sb); 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci if (err) { 7162306a36Sopenharmony_ci /* 7262306a36Sopenharmony_ci * hmpf, our choices here aren't good. We can pin 7362306a36Sopenharmony_ci * the inode which will disallow unmount from ever 7462306a36Sopenharmony_ci * happening, we can do nothing, which will corrupt 7562306a36Sopenharmony_ci * random memory on unmount, or we can forcibly 7662306a36Sopenharmony_ci * remove the file from the preallocation list, which 7762306a36Sopenharmony_ci * will leak blocks on disk. Lets pin the inode 7862306a36Sopenharmony_ci * and let the admin know what is going on. 7962306a36Sopenharmony_ci */ 8062306a36Sopenharmony_ci igrab(inode); 8162306a36Sopenharmony_ci reiserfs_warning(inode->i_sb, "clm-9001", 8262306a36Sopenharmony_ci "pinning inode %lu because the " 8362306a36Sopenharmony_ci "preallocation can't be freed", 8462306a36Sopenharmony_ci inode->i_ino); 8562306a36Sopenharmony_ci goto out; 8662306a36Sopenharmony_ci } 8762306a36Sopenharmony_ci } 8862306a36Sopenharmony_ci reiserfs_update_inode_transaction(inode); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci#ifdef REISERFS_PREALLOCATE 9162306a36Sopenharmony_ci reiserfs_discard_prealloc(&th, inode); 9262306a36Sopenharmony_ci#endif 9362306a36Sopenharmony_ci err = journal_end(&th); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci /* copy back the error code from journal_begin */ 9662306a36Sopenharmony_ci if (!err) 9762306a36Sopenharmony_ci err = jbegin_failure; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci if (!err && 10062306a36Sopenharmony_ci (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) && 10162306a36Sopenharmony_ci tail_has_to_be_packed(inode)) { 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci /* 10462306a36Sopenharmony_ci * if regular file is released by last holder and it has been 10562306a36Sopenharmony_ci * appended (we append by unformatted node only) or its direct 10662306a36Sopenharmony_ci * item(s) had to be converted, then it may have to be 10762306a36Sopenharmony_ci * indirect2direct converted 10862306a36Sopenharmony_ci */ 10962306a36Sopenharmony_ci err = reiserfs_truncate_file(inode, 0); 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ciout: 11262306a36Sopenharmony_ci reiserfs_write_unlock(inode->i_sb); 11362306a36Sopenharmony_ci mutex_unlock(&REISERFS_I(inode)->tailpack); 11462306a36Sopenharmony_ci return err; 11562306a36Sopenharmony_ci} 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_cistatic int reiserfs_file_open(struct inode *inode, struct file *file) 11862306a36Sopenharmony_ci{ 11962306a36Sopenharmony_ci int err = dquot_file_open(inode, file); 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci /* somebody might be tailpacking on final close; wait for it */ 12262306a36Sopenharmony_ci if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) { 12362306a36Sopenharmony_ci mutex_lock(&REISERFS_I(inode)->tailpack); 12462306a36Sopenharmony_ci atomic_inc(&REISERFS_I(inode)->openers); 12562306a36Sopenharmony_ci mutex_unlock(&REISERFS_I(inode)->tailpack); 12662306a36Sopenharmony_ci } 12762306a36Sopenharmony_ci return err; 12862306a36Sopenharmony_ci} 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_civoid reiserfs_vfs_truncate_file(struct inode *inode) 13162306a36Sopenharmony_ci{ 13262306a36Sopenharmony_ci mutex_lock(&REISERFS_I(inode)->tailpack); 13362306a36Sopenharmony_ci reiserfs_truncate_file(inode, 1); 13462306a36Sopenharmony_ci mutex_unlock(&REISERFS_I(inode)->tailpack); 13562306a36Sopenharmony_ci} 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci/* Sync a reiserfs file. */ 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci/* 14062306a36Sopenharmony_ci * FIXME: sync_mapping_buffers() never has anything to sync. Can 14162306a36Sopenharmony_ci * be removed... 14262306a36Sopenharmony_ci */ 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_cistatic int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end, 14562306a36Sopenharmony_ci int datasync) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci struct inode *inode = filp->f_mapping->host; 14862306a36Sopenharmony_ci int err; 14962306a36Sopenharmony_ci int barrier_done; 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci err = file_write_and_wait_range(filp, start, end); 15262306a36Sopenharmony_ci if (err) 15362306a36Sopenharmony_ci return err; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci inode_lock(inode); 15662306a36Sopenharmony_ci BUG_ON(!S_ISREG(inode->i_mode)); 15762306a36Sopenharmony_ci err = sync_mapping_buffers(inode->i_mapping); 15862306a36Sopenharmony_ci reiserfs_write_lock(inode->i_sb); 15962306a36Sopenharmony_ci barrier_done = reiserfs_commit_for_inode(inode); 16062306a36Sopenharmony_ci reiserfs_write_unlock(inode->i_sb); 16162306a36Sopenharmony_ci if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb)) 16262306a36Sopenharmony_ci blkdev_issue_flush(inode->i_sb->s_bdev); 16362306a36Sopenharmony_ci inode_unlock(inode); 16462306a36Sopenharmony_ci if (barrier_done < 0) 16562306a36Sopenharmony_ci return barrier_done; 16662306a36Sopenharmony_ci return (err < 0) ? -EIO : 0; 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci/* taken fs/buffer.c:__block_commit_write */ 17062306a36Sopenharmony_ciint reiserfs_commit_page(struct inode *inode, struct page *page, 17162306a36Sopenharmony_ci unsigned from, unsigned to) 17262306a36Sopenharmony_ci{ 17362306a36Sopenharmony_ci unsigned block_start, block_end; 17462306a36Sopenharmony_ci int partial = 0; 17562306a36Sopenharmony_ci unsigned blocksize; 17662306a36Sopenharmony_ci struct buffer_head *bh, *head; 17762306a36Sopenharmony_ci unsigned long i_size_index = inode->i_size >> PAGE_SHIFT; 17862306a36Sopenharmony_ci int new; 17962306a36Sopenharmony_ci int logit = reiserfs_file_data_log(inode); 18062306a36Sopenharmony_ci struct super_block *s = inode->i_sb; 18162306a36Sopenharmony_ci int bh_per_page = PAGE_SIZE / s->s_blocksize; 18262306a36Sopenharmony_ci struct reiserfs_transaction_handle th; 18362306a36Sopenharmony_ci int ret = 0; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci th.t_trans_id = 0; 18662306a36Sopenharmony_ci blocksize = i_blocksize(inode); 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci if (logit) { 18962306a36Sopenharmony_ci reiserfs_write_lock(s); 19062306a36Sopenharmony_ci ret = journal_begin(&th, s, bh_per_page + 1); 19162306a36Sopenharmony_ci if (ret) 19262306a36Sopenharmony_ci goto drop_write_lock; 19362306a36Sopenharmony_ci reiserfs_update_inode_transaction(inode); 19462306a36Sopenharmony_ci } 19562306a36Sopenharmony_ci for (bh = head = page_buffers(page), block_start = 0; 19662306a36Sopenharmony_ci bh != head || !block_start; 19762306a36Sopenharmony_ci block_start = block_end, bh = bh->b_this_page) { 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci new = buffer_new(bh); 20062306a36Sopenharmony_ci clear_buffer_new(bh); 20162306a36Sopenharmony_ci block_end = block_start + blocksize; 20262306a36Sopenharmony_ci if (block_end <= from || block_start >= to) { 20362306a36Sopenharmony_ci if (!buffer_uptodate(bh)) 20462306a36Sopenharmony_ci partial = 1; 20562306a36Sopenharmony_ci } else { 20662306a36Sopenharmony_ci set_buffer_uptodate(bh); 20762306a36Sopenharmony_ci if (logit) { 20862306a36Sopenharmony_ci reiserfs_prepare_for_journal(s, bh, 1); 20962306a36Sopenharmony_ci journal_mark_dirty(&th, bh); 21062306a36Sopenharmony_ci } else if (!buffer_dirty(bh)) { 21162306a36Sopenharmony_ci mark_buffer_dirty(bh); 21262306a36Sopenharmony_ci /* 21362306a36Sopenharmony_ci * do data=ordered on any page past the end 21462306a36Sopenharmony_ci * of file and any buffer marked BH_New. 21562306a36Sopenharmony_ci */ 21662306a36Sopenharmony_ci if (reiserfs_data_ordered(inode->i_sb) && 21762306a36Sopenharmony_ci (new || page->index >= i_size_index)) { 21862306a36Sopenharmony_ci reiserfs_add_ordered_list(inode, bh); 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci } 22162306a36Sopenharmony_ci } 22262306a36Sopenharmony_ci } 22362306a36Sopenharmony_ci if (logit) { 22462306a36Sopenharmony_ci ret = journal_end(&th); 22562306a36Sopenharmony_cidrop_write_lock: 22662306a36Sopenharmony_ci reiserfs_write_unlock(s); 22762306a36Sopenharmony_ci } 22862306a36Sopenharmony_ci /* 22962306a36Sopenharmony_ci * If this is a partial write which happened to make all buffers 23062306a36Sopenharmony_ci * uptodate then we can optimize away a bogus read_folio() for 23162306a36Sopenharmony_ci * the next read(). Here we 'discover' whether the page went 23262306a36Sopenharmony_ci * uptodate as a result of this (potentially partial) write. 23362306a36Sopenharmony_ci */ 23462306a36Sopenharmony_ci if (!partial) 23562306a36Sopenharmony_ci SetPageUptodate(page); 23662306a36Sopenharmony_ci return ret; 23762306a36Sopenharmony_ci} 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ciconst struct file_operations reiserfs_file_operations = { 24062306a36Sopenharmony_ci .unlocked_ioctl = reiserfs_ioctl, 24162306a36Sopenharmony_ci#ifdef CONFIG_COMPAT 24262306a36Sopenharmony_ci .compat_ioctl = reiserfs_compat_ioctl, 24362306a36Sopenharmony_ci#endif 24462306a36Sopenharmony_ci .mmap = generic_file_mmap, 24562306a36Sopenharmony_ci .open = reiserfs_file_open, 24662306a36Sopenharmony_ci .release = reiserfs_file_release, 24762306a36Sopenharmony_ci .fsync = reiserfs_sync_file, 24862306a36Sopenharmony_ci .read_iter = generic_file_read_iter, 24962306a36Sopenharmony_ci .write_iter = generic_file_write_iter, 25062306a36Sopenharmony_ci .splice_read = filemap_splice_read, 25162306a36Sopenharmony_ci .splice_write = iter_file_splice_write, 25262306a36Sopenharmony_ci .llseek = generic_file_llseek, 25362306a36Sopenharmony_ci}; 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ciconst struct inode_operations reiserfs_file_inode_operations = { 25662306a36Sopenharmony_ci .setattr = reiserfs_setattr, 25762306a36Sopenharmony_ci .listxattr = reiserfs_listxattr, 25862306a36Sopenharmony_ci .permission = reiserfs_permission, 25962306a36Sopenharmony_ci .get_inode_acl = reiserfs_get_acl, 26062306a36Sopenharmony_ci .set_acl = reiserfs_set_acl, 26162306a36Sopenharmony_ci .fileattr_get = reiserfs_fileattr_get, 26262306a36Sopenharmony_ci .fileattr_set = reiserfs_fileattr_set, 26362306a36Sopenharmony_ci}; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ciconst struct inode_operations reiserfs_priv_file_inode_operations = { 26662306a36Sopenharmony_ci .setattr = reiserfs_setattr, 26762306a36Sopenharmony_ci .permission = reiserfs_permission, 26862306a36Sopenharmony_ci .fileattr_get = reiserfs_fileattr_get, 26962306a36Sopenharmony_ci .fileattr_set = reiserfs_fileattr_set, 27062306a36Sopenharmony_ci}; 271