11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
31da177e4SLinus Torvalds */
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds #include <linux/time.h>
6f466c6fdSAl Viro #include "reiserfs.h"
7a3063ab8SAl Viro #include "acl.h"
8c45ac888SAl Viro #include "xattr.h"
917093991SFabian Frederick #include <linux/uaccess.h>
101da177e4SLinus Torvalds #include <linux/pagemap.h>
111da177e4SLinus Torvalds #include <linux/swap.h>
121da177e4SLinus Torvalds #include <linux/writeback.h>
131da177e4SLinus Torvalds #include <linux/blkdev.h>
141da177e4SLinus Torvalds #include <linux/buffer_head.h>
151da177e4SLinus Torvalds #include <linux/quotaops.h>
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds /*
18098297b2SJeff Mahoney * We pack the tails of files on file close, not at the time they are written.
19098297b2SJeff Mahoney * This implies an unnecessary copy of the tail and an unnecessary indirect item
20098297b2SJeff Mahoney * insertion/balancing, for files that are written in one write.
21098297b2SJeff Mahoney * It avoids unnecessary tail packings (balances) for files that are written in
22098297b2SJeff Mahoney * multiple writes and are small enough to have tails.
23098297b2SJeff Mahoney *
24098297b2SJeff Mahoney * file_release is called by the VFS layer when the file is closed. If
25098297b2SJeff Mahoney * this is the last open file descriptor, and the file
26098297b2SJeff Mahoney * small enough to have a tail, and the tail is currently in an
27098297b2SJeff Mahoney * unformatted node, the tail is converted back into a direct item.
28098297b2SJeff Mahoney *
29098297b2SJeff Mahoney * We use reiserfs_truncate_file to pack the tail, since it already has
30098297b2SJeff Mahoney * all the conditions coded.
311da177e4SLinus Torvalds */
reiserfs_file_release(struct inode * inode,struct file * filp)321da177e4SLinus Torvalds static int reiserfs_file_release(struct inode *inode, struct file *filp)
331da177e4SLinus Torvalds {
341da177e4SLinus Torvalds
351da177e4SLinus Torvalds struct reiserfs_transaction_handle th;
361da177e4SLinus Torvalds int err;
371da177e4SLinus Torvalds int jbegin_failure = 0;
381da177e4SLinus Torvalds
3914a61442SEric Sesterhenn BUG_ON(!S_ISREG(inode->i_mode));
401da177e4SLinus Torvalds
41a9913d7eSNikitas Angelinas if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers,
42a9913d7eSNikitas Angelinas &REISERFS_I(inode)->tailpack))
430e4f6a79SAl Viro return 0;
440e4f6a79SAl Viro
450e4f6a79SAl Viro /* fast out for when nothing needs to be done */
460e4f6a79SAl Viro if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
470e4f6a79SAl Viro !tail_has_to_be_packed(inode)) &&
480e4f6a79SAl Viro REISERFS_I(inode)->i_prealloc_count <= 0) {
49a228bf8fSJeff Mahoney mutex_unlock(&REISERFS_I(inode)->tailpack);
500e4f6a79SAl Viro return 0;
510e4f6a79SAl Viro }
52de14569fSVladimir Saveliev
53b5f3953cSChris Mason reiserfs_write_lock(inode->i_sb);
54098297b2SJeff Mahoney /*
55098297b2SJeff Mahoney * freeing preallocation only involves relogging blocks that
561da177e4SLinus Torvalds * are already in the current transaction. preallocation gets
571da177e4SLinus Torvalds * freed at the end of each transaction, so it is impossible for
581da177e4SLinus Torvalds * us to log any additional blocks (including quota blocks)
591da177e4SLinus Torvalds */
601da177e4SLinus Torvalds err = journal_begin(&th, inode->i_sb, 1);
611da177e4SLinus Torvalds if (err) {
62098297b2SJeff Mahoney /*
63098297b2SJeff Mahoney * uh oh, we can't allow the inode to go away while there
641da177e4SLinus Torvalds * is still preallocation blocks pending. Try to join the
651da177e4SLinus Torvalds * aborted transaction
661da177e4SLinus Torvalds */
671da177e4SLinus Torvalds jbegin_failure = err;
68b491dd17SJeff Mahoney err = journal_join_abort(&th, inode->i_sb);
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds if (err) {
71098297b2SJeff Mahoney /*
72098297b2SJeff Mahoney * hmpf, our choices here aren't good. We can pin
73098297b2SJeff Mahoney * the inode which will disallow unmount from ever
74098297b2SJeff Mahoney * happening, we can do nothing, which will corrupt
75098297b2SJeff Mahoney * random memory on unmount, or we can forcibly
76098297b2SJeff Mahoney * remove the file from the preallocation list, which
77098297b2SJeff Mahoney * will leak blocks on disk. Lets pin the inode
781da177e4SLinus Torvalds * and let the admin know what is going on.
791da177e4SLinus Torvalds */
801da177e4SLinus Torvalds igrab(inode);
8145b03d5eSJeff Mahoney reiserfs_warning(inode->i_sb, "clm-9001",
82bd4c625cSLinus Torvalds "pinning inode %lu because the "
83533221fbSAlexey Dobriyan "preallocation can't be freed",
84533221fbSAlexey Dobriyan inode->i_ino);
851da177e4SLinus Torvalds goto out;
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds reiserfs_update_inode_transaction(inode);
891da177e4SLinus Torvalds
901da177e4SLinus Torvalds #ifdef REISERFS_PREALLOCATE
911da177e4SLinus Torvalds reiserfs_discard_prealloc(&th, inode);
921da177e4SLinus Torvalds #endif
9358d85426SJeff Mahoney err = journal_end(&th);
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds /* copy back the error code from journal_begin */
961da177e4SLinus Torvalds if (!err)
971da177e4SLinus Torvalds err = jbegin_failure;
981da177e4SLinus Torvalds
990e4f6a79SAl Viro if (!err &&
1001da177e4SLinus Torvalds (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
1011da177e4SLinus Torvalds tail_has_to_be_packed(inode)) {
1020e4f6a79SAl Viro
103098297b2SJeff Mahoney /*
104098297b2SJeff Mahoney * if regular file is released by last holder and it has been
105098297b2SJeff Mahoney * appended (we append by unformatted node only) or its direct
106098297b2SJeff Mahoney * item(s) had to be converted, then it may have to be
107098297b2SJeff Mahoney * indirect2direct converted
108098297b2SJeff Mahoney */
1091da177e4SLinus Torvalds err = reiserfs_truncate_file(inode, 0);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds out:
1121da177e4SLinus Torvalds reiserfs_write_unlock(inode->i_sb);
113a228bf8fSJeff Mahoney mutex_unlock(&REISERFS_I(inode)->tailpack);
1141da177e4SLinus Torvalds return err;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds
reiserfs_file_open(struct inode * inode,struct file * file)1170e4f6a79SAl Viro static int reiserfs_file_open(struct inode *inode, struct file *file)
118de14569fSVladimir Saveliev {
1190e4f6a79SAl Viro int err = dquot_file_open(inode, file);
120098297b2SJeff Mahoney
1210e4f6a79SAl Viro /* somebody might be tailpacking on final close; wait for it */
122098297b2SJeff Mahoney if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
123a228bf8fSJeff Mahoney mutex_lock(&REISERFS_I(inode)->tailpack);
1240e4f6a79SAl Viro atomic_inc(&REISERFS_I(inode)->openers);
125a228bf8fSJeff Mahoney mutex_unlock(&REISERFS_I(inode)->tailpack);
1260e4f6a79SAl Viro }
1270e4f6a79SAl Viro return err;
128de14569fSVladimir Saveliev }
129de14569fSVladimir Saveliev
reiserfs_vfs_truncate_file(struct inode * inode)130cfac4b47SMarco Stornelli void reiserfs_vfs_truncate_file(struct inode *inode)
131bd4c625cSLinus Torvalds {
132a228bf8fSJeff Mahoney mutex_lock(&REISERFS_I(inode)->tailpack);
1331da177e4SLinus Torvalds reiserfs_truncate_file(inode, 1);
134a228bf8fSJeff Mahoney mutex_unlock(&REISERFS_I(inode)->tailpack);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds
1371da177e4SLinus Torvalds /* Sync a reiserfs file. */
1381da177e4SLinus Torvalds
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds * FIXME: sync_mapping_buffers() never has anything to sync. Can
1411da177e4SLinus Torvalds * be removed...
1421da177e4SLinus Torvalds */
1431da177e4SLinus Torvalds
reiserfs_sync_file(struct file * filp,loff_t start,loff_t end,int datasync)14402c24a82SJosef Bacik static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
14502c24a82SJosef Bacik int datasync)
146bd4c625cSLinus Torvalds {
1477ea80859SChristoph Hellwig struct inode *inode = filp->f_mapping->host;
148ee93961bSJeff Mahoney int err;
1491da177e4SLinus Torvalds int barrier_done;
1501da177e4SLinus Torvalds
1513b49c9a1SJeff Layton err = file_write_and_wait_range(filp, start, end);
15202c24a82SJosef Bacik if (err)
15302c24a82SJosef Bacik return err;
15402c24a82SJosef Bacik
1555955102cSAl Viro inode_lock(inode);
156995c762eSJeff Mahoney BUG_ON(!S_ISREG(inode->i_mode));
157ee93961bSJeff Mahoney err = sync_mapping_buffers(inode->i_mapping);
158995c762eSJeff Mahoney reiserfs_write_lock(inode->i_sb);
159995c762eSJeff Mahoney barrier_done = reiserfs_commit_for_inode(inode);
160995c762eSJeff Mahoney reiserfs_write_unlock(inode->i_sb);
161995c762eSJeff Mahoney if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
162c6bf3f0eSChristoph Hellwig blkdev_issue_flush(inode->i_sb->s_bdev);
1635955102cSAl Viro inode_unlock(inode);
1641da177e4SLinus Torvalds if (barrier_done < 0)
1651da177e4SLinus Torvalds return barrier_done;
166ee93961bSJeff Mahoney return (err < 0) ? -EIO : 0;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds /* taken fs/buffer.c:__block_commit_write */
reiserfs_commit_page(struct inode * inode,struct page * page,unsigned from,unsigned to)1701da177e4SLinus Torvalds int reiserfs_commit_page(struct inode *inode, struct page *page,
1711da177e4SLinus Torvalds unsigned from, unsigned to)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds unsigned block_start, block_end;
1741da177e4SLinus Torvalds int partial = 0;
1751da177e4SLinus Torvalds unsigned blocksize;
1761da177e4SLinus Torvalds struct buffer_head *bh, *head;
17709cbfeafSKirill A. Shutemov unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
1781da177e4SLinus Torvalds int new;
1791da177e4SLinus Torvalds int logit = reiserfs_file_data_log(inode);
1801da177e4SLinus Torvalds struct super_block *s = inode->i_sb;
18109cbfeafSKirill A. Shutemov int bh_per_page = PAGE_SIZE / s->s_blocksize;
1821da177e4SLinus Torvalds struct reiserfs_transaction_handle th;
1831da177e4SLinus Torvalds int ret = 0;
1841da177e4SLinus Torvalds
1851da177e4SLinus Torvalds th.t_trans_id = 0;
18693407472SFabian Frederick blocksize = i_blocksize(inode);
1871da177e4SLinus Torvalds
1881da177e4SLinus Torvalds if (logit) {
1891da177e4SLinus Torvalds reiserfs_write_lock(s);
1901da177e4SLinus Torvalds ret = journal_begin(&th, s, bh_per_page + 1);
1911da177e4SLinus Torvalds if (ret)
1921da177e4SLinus Torvalds goto drop_write_lock;
1931da177e4SLinus Torvalds reiserfs_update_inode_transaction(inode);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds for (bh = head = page_buffers(page), block_start = 0;
1961da177e4SLinus Torvalds bh != head || !block_start;
197bd4c625cSLinus Torvalds block_start = block_end, bh = bh->b_this_page) {
1981da177e4SLinus Torvalds
1991da177e4SLinus Torvalds new = buffer_new(bh);
2001da177e4SLinus Torvalds clear_buffer_new(bh);
2011da177e4SLinus Torvalds block_end = block_start + blocksize;
2021da177e4SLinus Torvalds if (block_end <= from || block_start >= to) {
2031da177e4SLinus Torvalds if (!buffer_uptodate(bh))
2041da177e4SLinus Torvalds partial = 1;
2051da177e4SLinus Torvalds } else {
2061da177e4SLinus Torvalds set_buffer_uptodate(bh);
2071da177e4SLinus Torvalds if (logit) {
2081da177e4SLinus Torvalds reiserfs_prepare_for_journal(s, bh, 1);
20909f1b80bSJeff Mahoney journal_mark_dirty(&th, bh);
2101da177e4SLinus Torvalds } else if (!buffer_dirty(bh)) {
2111da177e4SLinus Torvalds mark_buffer_dirty(bh);
212098297b2SJeff Mahoney /*
213098297b2SJeff Mahoney * do data=ordered on any page past the end
2141da177e4SLinus Torvalds * of file and any buffer marked BH_New.
2151da177e4SLinus Torvalds */
2161da177e4SLinus Torvalds if (reiserfs_data_ordered(inode->i_sb) &&
2171da177e4SLinus Torvalds (new || page->index >= i_size_index)) {
2181da177e4SLinus Torvalds reiserfs_add_ordered_list(inode, bh);
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds if (logit) {
22458d85426SJeff Mahoney ret = journal_end(&th);
2251da177e4SLinus Torvalds drop_write_lock:
2261da177e4SLinus Torvalds reiserfs_write_unlock(s);
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds /*
2291da177e4SLinus Torvalds * If this is a partial write which happened to make all buffers
2302c69e205SMatthew Wilcox (Oracle) * uptodate then we can optimize away a bogus read_folio() for
2311da177e4SLinus Torvalds * the next read(). Here we 'discover' whether the page went
2321da177e4SLinus Torvalds * uptodate as a result of this (potentially partial) write.
2331da177e4SLinus Torvalds */
2341da177e4SLinus Torvalds if (!partial)
2351da177e4SLinus Torvalds SetPageUptodate(page);
2361da177e4SLinus Torvalds return ret;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds
2394b6f5d20SArjan van de Ven const struct file_operations reiserfs_file_operations = {
240205cb37bSFrederic Weisbecker .unlocked_ioctl = reiserfs_ioctl,
24152b499c4SDavid Howells #ifdef CONFIG_COMPAT
24252b499c4SDavid Howells .compat_ioctl = reiserfs_compat_ioctl,
24352b499c4SDavid Howells #endif
2440e4f6a79SAl Viro .mmap = generic_file_mmap,
2450e4f6a79SAl Viro .open = reiserfs_file_open,
2461da177e4SLinus Torvalds .release = reiserfs_file_release,
2471da177e4SLinus Torvalds .fsync = reiserfs_sync_file,
248aad4f8bbSAl Viro .read_iter = generic_file_read_iter,
2498174202bSAl Viro .write_iter = generic_file_write_iter,
2505274f052SJens Axboe .splice_read = filemap_splice_read,
2518d020765SAl Viro .splice_write = iter_file_splice_write,
25291efc167SChristoph Hellwig .llseek = generic_file_llseek,
2531da177e4SLinus Torvalds };
2541da177e4SLinus Torvalds
255c5ef1c42SArjan van de Ven const struct inode_operations reiserfs_file_inode_operations = {
2561da177e4SLinus Torvalds .setattr = reiserfs_setattr,
2571da177e4SLinus Torvalds .listxattr = reiserfs_listxattr,
2581da177e4SLinus Torvalds .permission = reiserfs_permission,
259cac2f8b8SChristian Brauner .get_inode_acl = reiserfs_get_acl,
26047f70d08SChristoph Hellwig .set_acl = reiserfs_set_acl,
26103eb6066SMiklos Szeredi .fileattr_get = reiserfs_fileattr_get,
26203eb6066SMiklos Szeredi .fileattr_set = reiserfs_fileattr_set,
2631da177e4SLinus Torvalds };
264*d9f892b9SChristian Brauner
265*d9f892b9SChristian Brauner const struct inode_operations reiserfs_priv_file_inode_operations = {
266*d9f892b9SChristian Brauner .setattr = reiserfs_setattr,
267*d9f892b9SChristian Brauner .permission = reiserfs_permission,
268*d9f892b9SChristian Brauner .fileattr_get = reiserfs_fileattr_get,
269*d9f892b9SChristian Brauner .fileattr_set = reiserfs_fileattr_set,
270*d9f892b9SChristian Brauner };
271