brintos

brintos / linux-shallow public Read only

0
0
Text · 7.7 KiB · 8eb3ad3 Raw
271 lines · c
1/*2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README3 */4 5#include <linux/time.h>6#include "reiserfs.h"7#include "acl.h"8#include "xattr.h"9#include <linux/uaccess.h>10#include <linux/pagemap.h>11#include <linux/swap.h>12#include <linux/writeback.h>13#include <linux/blkdev.h>14#include <linux/buffer_head.h>15#include <linux/quotaops.h>16 17/*18 * We pack the tails of files on file close, not at the time they are written.19 * This implies an unnecessary copy of the tail and an unnecessary indirect item20 * insertion/balancing, for files that are written in one write.21 * It avoids unnecessary tail packings (balances) for files that are written in22 * multiple writes and are small enough to have tails.23 *24 * file_release is called by the VFS layer when the file is closed.  If25 * this is the last open file descriptor, and the file26 * small enough to have a tail, and the tail is currently in an27 * unformatted node, the tail is converted back into a direct item.28 *29 * We use reiserfs_truncate_file to pack the tail, since it already has30 * all the conditions coded.31 */32static int reiserfs_file_release(struct inode *inode, struct file *filp)33{34 35	struct reiserfs_transaction_handle th;36	int err;37	int jbegin_failure = 0;38 39	BUG_ON(!S_ISREG(inode->i_mode));40 41	if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers,42				       &REISERFS_I(inode)->tailpack))43		return 0;44 45	/* fast out for when nothing needs to be done */46	if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||47	     !tail_has_to_be_packed(inode)) &&48	    REISERFS_I(inode)->i_prealloc_count <= 0) {49		mutex_unlock(&REISERFS_I(inode)->tailpack);50		return 0;51	}52 53	reiserfs_write_lock(inode->i_sb);54	/*55	 * freeing preallocation only involves relogging blocks that56	 * are already in the current transaction.  preallocation gets57	 * freed at the end of each transaction, so it is impossible for58	 * us to log any additional blocks (including quota blocks)59	 */60	err = journal_begin(&th, inode->i_sb, 1);61	if (err) {62		/*63		 * uh oh, we can't allow the inode to go away while there64		 * is still preallocation blocks pending.  Try to join the65		 * aborted transaction66		 */67		jbegin_failure = err;68		err = journal_join_abort(&th, inode->i_sb);69 70		if (err) {71			/*72			 * hmpf, our choices here aren't good.  We can pin73			 * the inode which will disallow unmount from ever74			 * happening, we can do nothing, which will corrupt75			 * random memory on unmount, or we can forcibly76			 * remove the file from the preallocation list, which77			 * will leak blocks on disk.  Lets pin the inode78			 * and let the admin know what is going on.79			 */80			igrab(inode);81			reiserfs_warning(inode->i_sb, "clm-9001",82					 "pinning inode %lu because the "83					 "preallocation can't be freed",84					 inode->i_ino);85			goto out;86		}87	}88	reiserfs_update_inode_transaction(inode);89 90#ifdef REISERFS_PREALLOCATE91	reiserfs_discard_prealloc(&th, inode);92#endif93	err = journal_end(&th);94 95	/* copy back the error code from journal_begin */96	if (!err)97		err = jbegin_failure;98 99	if (!err &&100	    (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&101	    tail_has_to_be_packed(inode)) {102 103		/*104		 * if regular file is released by last holder and it has been105		 * appended (we append by unformatted node only) or its direct106		 * item(s) had to be converted, then it may have to be107		 * indirect2direct converted108		 */109		err = reiserfs_truncate_file(inode, 0);110	}111out:112	reiserfs_write_unlock(inode->i_sb);113	mutex_unlock(&REISERFS_I(inode)->tailpack);114	return err;115}116 117static int reiserfs_file_open(struct inode *inode, struct file *file)118{119	int err = dquot_file_open(inode, file);120 121	/* somebody might be tailpacking on final close; wait for it */122        if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {123		mutex_lock(&REISERFS_I(inode)->tailpack);124		atomic_inc(&REISERFS_I(inode)->openers);125		mutex_unlock(&REISERFS_I(inode)->tailpack);126	}127	return err;128}129 130void reiserfs_vfs_truncate_file(struct inode *inode)131{132	mutex_lock(&REISERFS_I(inode)->tailpack);133	reiserfs_truncate_file(inode, 1);134	mutex_unlock(&REISERFS_I(inode)->tailpack);135}136 137/* Sync a reiserfs file. */138 139/*140 * FIXME: sync_mapping_buffers() never has anything to sync.  Can141 * be removed...142 */143 144static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,145			      int datasync)146{147	struct inode *inode = filp->f_mapping->host;148	int err;149	int barrier_done;150 151	err = file_write_and_wait_range(filp, start, end);152	if (err)153		return err;154 155	inode_lock(inode);156	BUG_ON(!S_ISREG(inode->i_mode));157	err = sync_mapping_buffers(inode->i_mapping);158	reiserfs_write_lock(inode->i_sb);159	barrier_done = reiserfs_commit_for_inode(inode);160	reiserfs_write_unlock(inode->i_sb);161	if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))162		blkdev_issue_flush(inode->i_sb->s_bdev);163	inode_unlock(inode);164	if (barrier_done < 0)165		return barrier_done;166	return (err < 0) ? -EIO : 0;167}168 169/* taken fs/buffer.c:__block_commit_write */170int reiserfs_commit_page(struct inode *inode, struct page *page,171			 unsigned from, unsigned to)172{173	unsigned block_start, block_end;174	int partial = 0;175	unsigned blocksize;176	struct buffer_head *bh, *head;177	unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;178	int new;179	int logit = reiserfs_file_data_log(inode);180	struct super_block *s = inode->i_sb;181	int bh_per_page = PAGE_SIZE / s->s_blocksize;182	struct reiserfs_transaction_handle th;183	int ret = 0;184 185	th.t_trans_id = 0;186	blocksize = i_blocksize(inode);187 188	if (logit) {189		reiserfs_write_lock(s);190		ret = journal_begin(&th, s, bh_per_page + 1);191		if (ret)192			goto drop_write_lock;193		reiserfs_update_inode_transaction(inode);194	}195	for (bh = head = page_buffers(page), block_start = 0;196	     bh != head || !block_start;197	     block_start = block_end, bh = bh->b_this_page) {198 199		new = buffer_new(bh);200		clear_buffer_new(bh);201		block_end = block_start + blocksize;202		if (block_end <= from || block_start >= to) {203			if (!buffer_uptodate(bh))204				partial = 1;205		} else {206			set_buffer_uptodate(bh);207			if (logit) {208				reiserfs_prepare_for_journal(s, bh, 1);209				journal_mark_dirty(&th, bh);210			} else if (!buffer_dirty(bh)) {211				mark_buffer_dirty(bh);212				/*213				 * do data=ordered on any page past the end214				 * of file and any buffer marked BH_New.215				 */216				if (reiserfs_data_ordered(inode->i_sb) &&217				    (new || page->index >= i_size_index)) {218					reiserfs_add_ordered_list(inode, bh);219				}220			}221		}222	}223	if (logit) {224		ret = journal_end(&th);225drop_write_lock:226		reiserfs_write_unlock(s);227	}228	/*229	 * If this is a partial write which happened to make all buffers230	 * uptodate then we can optimize away a bogus read_folio() for231	 * the next read(). Here we 'discover' whether the page went232	 * uptodate as a result of this (potentially partial) write.233	 */234	if (!partial)235		SetPageUptodate(page);236	return ret;237}238 239const struct file_operations reiserfs_file_operations = {240	.unlocked_ioctl = reiserfs_ioctl,241#ifdef CONFIG_COMPAT242	.compat_ioctl = reiserfs_compat_ioctl,243#endif244	.mmap = generic_file_mmap,245	.open = reiserfs_file_open,246	.release = reiserfs_file_release,247	.fsync = reiserfs_sync_file,248	.read_iter = generic_file_read_iter,249	.write_iter = generic_file_write_iter,250	.splice_read = filemap_splice_read,251	.splice_write = iter_file_splice_write,252	.llseek = generic_file_llseek,253};254 255const struct inode_operations reiserfs_file_inode_operations = {256	.setattr = reiserfs_setattr,257	.listxattr = reiserfs_listxattr,258	.permission = reiserfs_permission,259	.get_inode_acl = reiserfs_get_acl,260	.set_acl = reiserfs_set_acl,261	.fileattr_get = reiserfs_fileattr_get,262	.fileattr_set = reiserfs_fileattr_set,263};264 265const struct inode_operations reiserfs_priv_file_inode_operations = {266	.setattr = reiserfs_setattr,267	.permission = reiserfs_permission,268	.fileattr_get = reiserfs_fileattr_get,269	.fileattr_set = reiserfs_fileattr_set,270};271