Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *   Copyright (C) International Business Machines Corp., 2000-2004
0004  *   Portions Copyright (C) Christoph Hellwig, 2001-2002
0005  */
0006 
0007 #include <linux/fs.h>
0008 #include <linux/mpage.h>
0009 #include <linux/buffer_head.h>
0010 #include <linux/pagemap.h>
0011 #include <linux/quotaops.h>
0012 #include <linux/uio.h>
0013 #include <linux/writeback.h>
0014 #include "jfs_incore.h"
0015 #include "jfs_inode.h"
0016 #include "jfs_filsys.h"
0017 #include "jfs_imap.h"
0018 #include "jfs_extent.h"
0019 #include "jfs_unicode.h"
0020 #include "jfs_debug.h"
0021 #include "jfs_dmap.h"
0022 
0023 
0024 struct inode *jfs_iget(struct super_block *sb, unsigned long ino)
0025 {
0026     struct inode *inode;
0027     int ret;
0028 
0029     inode = iget_locked(sb, ino);
0030     if (!inode)
0031         return ERR_PTR(-ENOMEM);
0032     if (!(inode->i_state & I_NEW))
0033         return inode;
0034 
0035     ret = diRead(inode);
0036     if (ret < 0) {
0037         iget_failed(inode);
0038         return ERR_PTR(ret);
0039     }
0040 
0041     if (S_ISREG(inode->i_mode)) {
0042         inode->i_op = &jfs_file_inode_operations;
0043         inode->i_fop = &jfs_file_operations;
0044         inode->i_mapping->a_ops = &jfs_aops;
0045     } else if (S_ISDIR(inode->i_mode)) {
0046         inode->i_op = &jfs_dir_inode_operations;
0047         inode->i_fop = &jfs_dir_operations;
0048     } else if (S_ISLNK(inode->i_mode)) {
0049         if (inode->i_size >= IDATASIZE) {
0050             inode->i_op = &page_symlink_inode_operations;
0051             inode_nohighmem(inode);
0052             inode->i_mapping->a_ops = &jfs_aops;
0053         } else {
0054             inode->i_op = &jfs_fast_symlink_inode_operations;
0055             inode->i_link = JFS_IP(inode)->i_inline;
0056             /*
0057              * The inline data should be null-terminated, but
0058              * don't let on-disk corruption crash the kernel
0059              */
0060             inode->i_link[inode->i_size] = '\0';
0061         }
0062     } else {
0063         inode->i_op = &jfs_file_inode_operations;
0064         init_special_inode(inode, inode->i_mode, inode->i_rdev);
0065     }
0066     unlock_new_inode(inode);
0067     return inode;
0068 }
0069 
0070 /*
0071  * Workhorse of both fsync & write_inode
0072  */
0073 int jfs_commit_inode(struct inode *inode, int wait)
0074 {
0075     int rc = 0;
0076     tid_t tid;
0077     static int noisy = 5;
0078 
0079     jfs_info("In jfs_commit_inode, inode = 0x%p", inode);
0080 
0081     /*
0082      * Don't commit if inode has been committed since last being
0083      * marked dirty, or if it has been deleted.
0084      */
0085     if (inode->i_nlink == 0 || !test_cflag(COMMIT_Dirty, inode))
0086         return 0;
0087 
0088     if (isReadOnly(inode)) {
0089         /* kernel allows writes to devices on read-only
0090          * partitions and may think inode is dirty
0091          */
0092         if (!special_file(inode->i_mode) && noisy) {
0093             jfs_err("jfs_commit_inode(0x%p) called on read-only volume",
0094                 inode);
0095             jfs_err("Is remount racy?");
0096             noisy--;
0097         }
0098         return 0;
0099     }
0100 
0101     tid = txBegin(inode->i_sb, COMMIT_INODE);
0102     mutex_lock(&JFS_IP(inode)->commit_mutex);
0103 
0104     /*
0105      * Retest inode state after taking commit_mutex
0106      */
0107     if (inode->i_nlink && test_cflag(COMMIT_Dirty, inode))
0108         rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
0109 
0110     txEnd(tid);
0111     mutex_unlock(&JFS_IP(inode)->commit_mutex);
0112     return rc;
0113 }
0114 
0115 int jfs_write_inode(struct inode *inode, struct writeback_control *wbc)
0116 {
0117     int wait = wbc->sync_mode == WB_SYNC_ALL;
0118 
0119     if (inode->i_nlink == 0)
0120         return 0;
0121     /*
0122      * If COMMIT_DIRTY is not set, the inode isn't really dirty.
0123      * It has been committed since the last change, but was still
0124      * on the dirty inode list.
0125      */
0126     if (!test_cflag(COMMIT_Dirty, inode)) {
0127         /* Make sure committed changes hit the disk */
0128         jfs_flush_journal(JFS_SBI(inode->i_sb)->log, wait);
0129         return 0;
0130     }
0131 
0132     if (jfs_commit_inode(inode, wait)) {
0133         jfs_err("jfs_write_inode: jfs_commit_inode failed!");
0134         return -EIO;
0135     } else
0136         return 0;
0137 }
0138 
0139 void jfs_evict_inode(struct inode *inode)
0140 {
0141     struct jfs_inode_info *ji = JFS_IP(inode);
0142 
0143     jfs_info("In jfs_evict_inode, inode = 0x%p", inode);
0144 
0145     if (!inode->i_nlink && !is_bad_inode(inode)) {
0146         dquot_initialize(inode);
0147 
0148         if (JFS_IP(inode)->fileset == FILESYSTEM_I) {
0149             struct inode *ipimap = JFS_SBI(inode->i_sb)->ipimap;
0150             truncate_inode_pages_final(&inode->i_data);
0151 
0152             if (test_cflag(COMMIT_Freewmap, inode))
0153                 jfs_free_zero_link(inode);
0154 
0155             if (ipimap && JFS_IP(ipimap)->i_imap)
0156                 diFree(inode);
0157 
0158             /*
0159              * Free the inode from the quota allocation.
0160              */
0161             dquot_free_inode(inode);
0162         }
0163     } else {
0164         truncate_inode_pages_final(&inode->i_data);
0165     }
0166     clear_inode(inode);
0167     dquot_drop(inode);
0168 
0169     BUG_ON(!list_empty(&ji->anon_inode_list));
0170 
0171     spin_lock_irq(&ji->ag_lock);
0172     if (ji->active_ag != -1) {
0173         struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
0174         atomic_dec(&bmap->db_active[ji->active_ag]);
0175         ji->active_ag = -1;
0176     }
0177     spin_unlock_irq(&ji->ag_lock);
0178 }
0179 
0180 void jfs_dirty_inode(struct inode *inode, int flags)
0181 {
0182     static int noisy = 5;
0183 
0184     if (isReadOnly(inode)) {
0185         if (!special_file(inode->i_mode) && noisy) {
0186             /* kernel allows writes to devices on read-only
0187              * partitions and may try to mark inode dirty
0188              */
0189             jfs_err("jfs_dirty_inode called on read-only volume");
0190             jfs_err("Is remount racy?");
0191             noisy--;
0192         }
0193         return;
0194     }
0195 
0196     set_cflag(COMMIT_Dirty, inode);
0197 }
0198 
0199 int jfs_get_block(struct inode *ip, sector_t lblock,
0200           struct buffer_head *bh_result, int create)
0201 {
0202     s64 lblock64 = lblock;
0203     int rc = 0;
0204     xad_t xad;
0205     s64 xaddr;
0206     int xflag;
0207     s32 xlen = bh_result->b_size >> ip->i_blkbits;
0208 
0209     /*
0210      * Take appropriate lock on inode
0211      */
0212     if (create)
0213         IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
0214     else
0215         IREAD_LOCK(ip, RDWRLOCK_NORMAL);
0216 
0217     if (((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size) &&
0218         (!xtLookup(ip, lblock64, xlen, &xflag, &xaddr, &xlen, 0)) &&
0219         xaddr) {
0220         if (xflag & XAD_NOTRECORDED) {
0221             if (!create)
0222                 /*
0223                  * Allocated but not recorded, read treats
0224                  * this as a hole
0225                  */
0226                 goto unlock;
0227             XADoffset(&xad, lblock64);
0228             XADlength(&xad, xlen);
0229             XADaddress(&xad, xaddr);
0230             rc = extRecord(ip, &xad);
0231             if (rc)
0232                 goto unlock;
0233             set_buffer_new(bh_result);
0234         }
0235 
0236         map_bh(bh_result, ip->i_sb, xaddr);
0237         bh_result->b_size = xlen << ip->i_blkbits;
0238         goto unlock;
0239     }
0240     if (!create)
0241         goto unlock;
0242 
0243     /*
0244      * Allocate a new block
0245      */
0246     if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
0247         goto unlock;
0248     rc = extAlloc(ip, xlen, lblock64, &xad, false);
0249     if (rc)
0250         goto unlock;
0251 
0252     set_buffer_new(bh_result);
0253     map_bh(bh_result, ip->i_sb, addressXAD(&xad));
0254     bh_result->b_size = lengthXAD(&xad) << ip->i_blkbits;
0255 
0256       unlock:
0257     /*
0258      * Release lock on inode
0259      */
0260     if (create)
0261         IWRITE_UNLOCK(ip);
0262     else
0263         IREAD_UNLOCK(ip);
0264     return rc;
0265 }
0266 
0267 static int jfs_writepage(struct page *page, struct writeback_control *wbc)
0268 {
0269     return block_write_full_page(page, jfs_get_block, wbc);
0270 }
0271 
0272 static int jfs_writepages(struct address_space *mapping,
0273             struct writeback_control *wbc)
0274 {
0275     return mpage_writepages(mapping, wbc, jfs_get_block);
0276 }
0277 
0278 static int jfs_read_folio(struct file *file, struct folio *folio)
0279 {
0280     return mpage_read_folio(folio, jfs_get_block);
0281 }
0282 
0283 static void jfs_readahead(struct readahead_control *rac)
0284 {
0285     mpage_readahead(rac, jfs_get_block);
0286 }
0287 
0288 static void jfs_write_failed(struct address_space *mapping, loff_t to)
0289 {
0290     struct inode *inode = mapping->host;
0291 
0292     if (to > inode->i_size) {
0293         truncate_pagecache(inode, inode->i_size);
0294         jfs_truncate(inode);
0295     }
0296 }
0297 
0298 static int jfs_write_begin(struct file *file, struct address_space *mapping,
0299                 loff_t pos, unsigned len,
0300                 struct page **pagep, void **fsdata)
0301 {
0302     int ret;
0303 
0304     ret = block_write_begin(mapping, pos, len, pagep, jfs_get_block);
0305     if (unlikely(ret))
0306         jfs_write_failed(mapping, pos + len);
0307 
0308     return ret;
0309 }
0310 
0311 static int jfs_write_end(struct file *file, struct address_space *mapping,
0312         loff_t pos, unsigned len, unsigned copied, struct page *page,
0313         void *fsdata)
0314 {
0315     int ret;
0316 
0317     ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
0318     if (ret < len)
0319         jfs_write_failed(mapping, pos + len);
0320     return ret;
0321 }
0322 
0323 static sector_t jfs_bmap(struct address_space *mapping, sector_t block)
0324 {
0325     return generic_block_bmap(mapping, block, jfs_get_block);
0326 }
0327 
0328 static ssize_t jfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
0329 {
0330     struct file *file = iocb->ki_filp;
0331     struct address_space *mapping = file->f_mapping;
0332     struct inode *inode = file->f_mapping->host;
0333     size_t count = iov_iter_count(iter);
0334     ssize_t ret;
0335 
0336     ret = blockdev_direct_IO(iocb, inode, iter, jfs_get_block);
0337 
0338     /*
0339      * In case of error extending write may have instantiated a few
0340      * blocks outside i_size. Trim these off again.
0341      */
0342     if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
0343         loff_t isize = i_size_read(inode);
0344         loff_t end = iocb->ki_pos + count;
0345 
0346         if (end > isize)
0347             jfs_write_failed(mapping, end);
0348     }
0349 
0350     return ret;
0351 }
0352 
0353 const struct address_space_operations jfs_aops = {
0354     .dirty_folio    = block_dirty_folio,
0355     .invalidate_folio = block_invalidate_folio,
0356     .read_folio = jfs_read_folio,
0357     .readahead  = jfs_readahead,
0358     .writepage  = jfs_writepage,
0359     .writepages = jfs_writepages,
0360     .write_begin    = jfs_write_begin,
0361     .write_end  = jfs_write_end,
0362     .bmap       = jfs_bmap,
0363     .direct_IO  = jfs_direct_IO,
0364 };
0365 
0366 /*
0367  * Guts of jfs_truncate.  Called with locks already held.  Can be called
0368  * with directory for truncating directory index table.
0369  */
0370 void jfs_truncate_nolock(struct inode *ip, loff_t length)
0371 {
0372     loff_t newsize;
0373     tid_t tid;
0374 
0375     ASSERT(length >= 0);
0376 
0377     if (test_cflag(COMMIT_Nolink, ip)) {
0378         xtTruncate(0, ip, length, COMMIT_WMAP);
0379         return;
0380     }
0381 
0382     do {
0383         tid = txBegin(ip->i_sb, 0);
0384 
0385         /*
0386          * The commit_mutex cannot be taken before txBegin.
0387          * txBegin may block and there is a chance the inode
0388          * could be marked dirty and need to be committed
0389          * before txBegin unblocks
0390          */
0391         mutex_lock(&JFS_IP(ip)->commit_mutex);
0392 
0393         newsize = xtTruncate(tid, ip, length,
0394                      COMMIT_TRUNCATE | COMMIT_PWMAP);
0395         if (newsize < 0) {
0396             txEnd(tid);
0397             mutex_unlock(&JFS_IP(ip)->commit_mutex);
0398             break;
0399         }
0400 
0401         ip->i_mtime = ip->i_ctime = current_time(ip);
0402         mark_inode_dirty(ip);
0403 
0404         txCommit(tid, 1, &ip, 0);
0405         txEnd(tid);
0406         mutex_unlock(&JFS_IP(ip)->commit_mutex);
0407     } while (newsize > length); /* Truncate isn't always atomic */
0408 }
0409 
0410 void jfs_truncate(struct inode *ip)
0411 {
0412     jfs_info("jfs_truncate: size = 0x%lx", (ulong) ip->i_size);
0413 
0414     block_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
0415 
0416     IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
0417     jfs_truncate_nolock(ip, ip->i_size);
0418     IWRITE_UNLOCK(ip);
0419 }