Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
0003  */
0004 
0005 #include <linux/time.h>
0006 #include "reiserfs.h"
0007 #include "acl.h"
0008 #include "xattr.h"
0009 #include <linux/uaccess.h>
0010 #include <linux/pagemap.h>
0011 #include <linux/swap.h>
0012 #include <linux/writeback.h>
0013 #include <linux/blkdev.h>
0014 #include <linux/buffer_head.h>
0015 #include <linux/quotaops.h>
0016 
0017 /*
0018  * We pack the tails of files on file close, not at the time they are written.
0019  * This implies an unnecessary copy of the tail and an unnecessary indirect item
0020  * insertion/balancing, for files that are written in one write.
0021  * It avoids unnecessary tail packings (balances) for files that are written in
0022  * multiple writes and are small enough to have tails.
0023  *
0024  * file_release is called by the VFS layer when the file is closed.  If
0025  * this is the last open file descriptor, and the file
0026  * small enough to have a tail, and the tail is currently in an
0027  * unformatted node, the tail is converted back into a direct item.
0028  *
0029  * We use reiserfs_truncate_file to pack the tail, since it already has
0030  * all the conditions coded.
0031  */
0032 static int reiserfs_file_release(struct inode *inode, struct file *filp)
0033 {
0034 
0035     struct reiserfs_transaction_handle th;
0036     int err;
0037     int jbegin_failure = 0;
0038 
0039     BUG_ON(!S_ISREG(inode->i_mode));
0040 
0041     if (!atomic_dec_and_mutex_lock(&REISERFS_I(inode)->openers,
0042                        &REISERFS_I(inode)->tailpack))
0043         return 0;
0044 
0045     /* fast out for when nothing needs to be done */
0046     if ((!(REISERFS_I(inode)->i_flags & i_pack_on_close_mask) ||
0047          !tail_has_to_be_packed(inode)) &&
0048         REISERFS_I(inode)->i_prealloc_count <= 0) {
0049         mutex_unlock(&REISERFS_I(inode)->tailpack);
0050         return 0;
0051     }
0052 
0053     reiserfs_write_lock(inode->i_sb);
0054     /*
0055      * freeing preallocation only involves relogging blocks that
0056      * are already in the current transaction.  preallocation gets
0057      * freed at the end of each transaction, so it is impossible for
0058      * us to log any additional blocks (including quota blocks)
0059      */
0060     err = journal_begin(&th, inode->i_sb, 1);
0061     if (err) {
0062         /*
0063          * uh oh, we can't allow the inode to go away while there
0064          * is still preallocation blocks pending.  Try to join the
0065          * aborted transaction
0066          */
0067         jbegin_failure = err;
0068         err = journal_join_abort(&th, inode->i_sb);
0069 
0070         if (err) {
0071             /*
0072              * hmpf, our choices here aren't good.  We can pin
0073              * the inode which will disallow unmount from ever
0074              * happening, we can do nothing, which will corrupt
0075              * random memory on unmount, or we can forcibly
0076              * remove the file from the preallocation list, which
0077              * will leak blocks on disk.  Lets pin the inode
0078              * and let the admin know what is going on.
0079              */
0080             igrab(inode);
0081             reiserfs_warning(inode->i_sb, "clm-9001",
0082                      "pinning inode %lu because the "
0083                      "preallocation can't be freed",
0084                      inode->i_ino);
0085             goto out;
0086         }
0087     }
0088     reiserfs_update_inode_transaction(inode);
0089 
0090 #ifdef REISERFS_PREALLOCATE
0091     reiserfs_discard_prealloc(&th, inode);
0092 #endif
0093     err = journal_end(&th);
0094 
0095     /* copy back the error code from journal_begin */
0096     if (!err)
0097         err = jbegin_failure;
0098 
0099     if (!err &&
0100         (REISERFS_I(inode)->i_flags & i_pack_on_close_mask) &&
0101         tail_has_to_be_packed(inode)) {
0102 
0103         /*
0104          * if regular file is released by last holder and it has been
0105          * appended (we append by unformatted node only) or its direct
0106          * item(s) had to be converted, then it may have to be
0107          * indirect2direct converted
0108          */
0109         err = reiserfs_truncate_file(inode, 0);
0110     }
0111 out:
0112     reiserfs_write_unlock(inode->i_sb);
0113     mutex_unlock(&REISERFS_I(inode)->tailpack);
0114     return err;
0115 }
0116 
0117 static int reiserfs_file_open(struct inode *inode, struct file *file)
0118 {
0119     int err = dquot_file_open(inode, file);
0120 
0121     /* somebody might be tailpacking on final close; wait for it */
0122         if (!atomic_inc_not_zero(&REISERFS_I(inode)->openers)) {
0123         mutex_lock(&REISERFS_I(inode)->tailpack);
0124         atomic_inc(&REISERFS_I(inode)->openers);
0125         mutex_unlock(&REISERFS_I(inode)->tailpack);
0126     }
0127     return err;
0128 }
0129 
0130 void reiserfs_vfs_truncate_file(struct inode *inode)
0131 {
0132     mutex_lock(&REISERFS_I(inode)->tailpack);
0133     reiserfs_truncate_file(inode, 1);
0134     mutex_unlock(&REISERFS_I(inode)->tailpack);
0135 }
0136 
0137 /* Sync a reiserfs file. */
0138 
0139 /*
0140  * FIXME: sync_mapping_buffers() never has anything to sync.  Can
0141  * be removed...
0142  */
0143 
0144 static int reiserfs_sync_file(struct file *filp, loff_t start, loff_t end,
0145                   int datasync)
0146 {
0147     struct inode *inode = filp->f_mapping->host;
0148     int err;
0149     int barrier_done;
0150 
0151     err = file_write_and_wait_range(filp, start, end);
0152     if (err)
0153         return err;
0154 
0155     inode_lock(inode);
0156     BUG_ON(!S_ISREG(inode->i_mode));
0157     err = sync_mapping_buffers(inode->i_mapping);
0158     reiserfs_write_lock(inode->i_sb);
0159     barrier_done = reiserfs_commit_for_inode(inode);
0160     reiserfs_write_unlock(inode->i_sb);
0161     if (barrier_done != 1 && reiserfs_barrier_flush(inode->i_sb))
0162         blkdev_issue_flush(inode->i_sb->s_bdev);
0163     inode_unlock(inode);
0164     if (barrier_done < 0)
0165         return barrier_done;
0166     return (err < 0) ? -EIO : 0;
0167 }
0168 
0169 /* taken fs/buffer.c:__block_commit_write */
0170 int reiserfs_commit_page(struct inode *inode, struct page *page,
0171              unsigned from, unsigned to)
0172 {
0173     unsigned block_start, block_end;
0174     int partial = 0;
0175     unsigned blocksize;
0176     struct buffer_head *bh, *head;
0177     unsigned long i_size_index = inode->i_size >> PAGE_SHIFT;
0178     int new;
0179     int logit = reiserfs_file_data_log(inode);
0180     struct super_block *s = inode->i_sb;
0181     int bh_per_page = PAGE_SIZE / s->s_blocksize;
0182     struct reiserfs_transaction_handle th;
0183     int ret = 0;
0184 
0185     th.t_trans_id = 0;
0186     blocksize = i_blocksize(inode);
0187 
0188     if (logit) {
0189         reiserfs_write_lock(s);
0190         ret = journal_begin(&th, s, bh_per_page + 1);
0191         if (ret)
0192             goto drop_write_lock;
0193         reiserfs_update_inode_transaction(inode);
0194     }
0195     for (bh = head = page_buffers(page), block_start = 0;
0196          bh != head || !block_start;
0197          block_start = block_end, bh = bh->b_this_page) {
0198 
0199         new = buffer_new(bh);
0200         clear_buffer_new(bh);
0201         block_end = block_start + blocksize;
0202         if (block_end <= from || block_start >= to) {
0203             if (!buffer_uptodate(bh))
0204                 partial = 1;
0205         } else {
0206             set_buffer_uptodate(bh);
0207             if (logit) {
0208                 reiserfs_prepare_for_journal(s, bh, 1);
0209                 journal_mark_dirty(&th, bh);
0210             } else if (!buffer_dirty(bh)) {
0211                 mark_buffer_dirty(bh);
0212                 /*
0213                  * do data=ordered on any page past the end
0214                  * of file and any buffer marked BH_New.
0215                  */
0216                 if (reiserfs_data_ordered(inode->i_sb) &&
0217                     (new || page->index >= i_size_index)) {
0218                     reiserfs_add_ordered_list(inode, bh);
0219                 }
0220             }
0221         }
0222     }
0223     if (logit) {
0224         ret = journal_end(&th);
0225 drop_write_lock:
0226         reiserfs_write_unlock(s);
0227     }
0228     /*
0229      * If this is a partial write which happened to make all buffers
0230      * uptodate then we can optimize away a bogus read_folio() for
0231      * the next read(). Here we 'discover' whether the page went
0232      * uptodate as a result of this (potentially partial) write.
0233      */
0234     if (!partial)
0235         SetPageUptodate(page);
0236     return ret;
0237 }
0238 
0239 const struct file_operations reiserfs_file_operations = {
0240     .unlocked_ioctl = reiserfs_ioctl,
0241 #ifdef CONFIG_COMPAT
0242     .compat_ioctl = reiserfs_compat_ioctl,
0243 #endif
0244     .mmap = generic_file_mmap,
0245     .open = reiserfs_file_open,
0246     .release = reiserfs_file_release,
0247     .fsync = reiserfs_sync_file,
0248     .read_iter = generic_file_read_iter,
0249     .write_iter = generic_file_write_iter,
0250     .splice_read = generic_file_splice_read,
0251     .splice_write = iter_file_splice_write,
0252     .llseek = generic_file_llseek,
0253 };
0254 
0255 const struct inode_operations reiserfs_file_inode_operations = {
0256     .setattr = reiserfs_setattr,
0257     .listxattr = reiserfs_listxattr,
0258     .permission = reiserfs_permission,
0259     .get_acl = reiserfs_get_acl,
0260     .set_acl = reiserfs_set_acl,
0261     .fileattr_get = reiserfs_fileattr_get,
0262     .fileattr_set = reiserfs_fileattr_set,
0263 };