The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/jfs/inode.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  *   Copyright (c) International Business Machines Corp., 2000-2002
    3  *   Portions Copyright (c) Christoph Hellwig, 2001-2002
    4  *
    5  *   This program is free software;  you can redistribute it and/or modify
    6  *   it under the terms of the GNU General Public License as published by
    7  *   the Free Software Foundation; either version 2 of the License, or 
    8  *   (at your option) any later version.
    9  * 
   10  *   This program is distributed in the hope that it will be useful,
   11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
   12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   13  *   the GNU General Public License for more details.
   14  *
   15  *   You should have received a copy of the GNU General Public License
   16  *   along with this program;  if not, write to the Free Software 
   17  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   18  */
   19 
   20 #include <linux/fs.h>
   21 //#include <linux/locks.h>
   22 #include "jfs_incore.h"
   23 #include "jfs_filsys.h"
   24 #include "jfs_dmap.h"
   25 #include "jfs_imap.h"
   26 #include "jfs_extent.h"
   27 #include "jfs_unicode.h"
   28 #include "jfs_debug.h"
   29 
   30 
   31 extern struct inode_operations jfs_dir_inode_operations;
   32 extern struct inode_operations jfs_file_inode_operations;
   33 extern struct inode_operations jfs_symlink_inode_operations;
   34 extern struct file_operations jfs_dir_operations;
   35 extern struct file_operations jfs_file_operations;
   36 struct address_space_operations jfs_aops;
   37 extern int freeZeroLink(struct inode *);
   38 
   39 void jfs_clear_inode(struct inode *inode)
   40 {
   41         struct jfs_inode_info *ji = JFS_IP(inode);
   42 
   43         if (is_bad_inode(inode))
   44                 /*
   45                  * We free the fs-dependent structure before making the
   46                  * inode bad
   47                  */
   48                 return;
   49 
   50         jfs_info("jfs_clear_inode called ip = 0x%p", inode);
   51 
   52         if (ji->active_ag != -1) {
   53                 struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
   54                 atomic_dec(&bmap->db_active[ji->active_ag]);
   55         }
   56 
   57         ASSERT(list_empty(&ji->anon_inode_list));
   58 
   59         if (ji->atlhead) {
   60                 jfs_err("jfs_clear_inode: inode %p has anonymous tlocks",
   61                         inode);
   62                 jfs_err("i_state = 0x%lx, cflag = 0x%lx", inode->i_state,
   63                         ji->cflag);
   64         }
   65 
   66         free_jfs_inode(inode);
   67 }
   68 
   69 void jfs_read_inode(struct inode *inode)
   70 {
   71         int rc;
   72 
   73         rc = alloc_jfs_inode(inode);
   74         if (rc) {
   75                 jfs_warn("In jfs_read_inode, alloc_jfs_inode failed");
   76                 goto bad_inode;
   77         }
   78         jfs_info("In jfs_read_inode, inode = 0x%p", inode);
   79 
   80         if (diRead(inode))
   81                 goto bad_inode_free;
   82 
   83         if (S_ISREG(inode->i_mode)) {
   84                 inode->i_op = &jfs_file_inode_operations;
   85                 inode->i_fop = &jfs_file_operations;
   86                 inode->i_mapping->a_ops = &jfs_aops;
   87         } else if (S_ISDIR(inode->i_mode)) {
   88                 inode->i_op = &jfs_dir_inode_operations;
   89                 inode->i_fop = &jfs_dir_operations;
   90                 inode->i_mapping->a_ops = &jfs_aops;
   91                 inode->i_mapping->gfp_mask = GFP_NOFS;
   92         } else if (S_ISLNK(inode->i_mode)) {
   93                 if (inode->i_size >= IDATASIZE) {
   94                         inode->i_op = &page_symlink_inode_operations;
   95                         inode->i_mapping->a_ops = &jfs_aops;
   96                 } else
   97                         inode->i_op = &jfs_symlink_inode_operations;
   98         } else {
   99                 inode->i_op = &jfs_file_inode_operations;
  100                 init_special_inode(inode, inode->i_mode,
  101                                    kdev_t_to_nr(inode->i_rdev));
  102         }
  103 
  104         return;
  105 
  106       bad_inode_free:
  107         free_jfs_inode(inode);
  108       bad_inode:
  109         make_bad_inode(inode);
  110 }
  111 
  112 /* This define is from fs/open.c */
  113 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
  114 
  115 /*
  116  * Workhorse of both fsync & write_inode
  117  */
  118 int jfs_commit_inode(struct inode *inode, int wait)
  119 {
  120         int rc = 0;
  121         tid_t tid;
  122         static int noisy = 5;
  123 
  124         jfs_info("In jfs_commit_inode, inode = 0x%p", inode);
  125 
  126         /*
  127          * Don't commit if inode has been committed since last being
  128          * marked dirty, or if it has been deleted.
  129          */
  130         if (test_cflag(COMMIT_Nolink, inode) ||
  131             !test_cflag(COMMIT_Dirty, inode))
  132                 return 0;
  133 
  134         if (isReadOnly(inode)) {
  135                 /* kernel allows writes to devices on read-only
  136                  * partitions and may think inode is dirty
  137                  */
  138                 if (!special_file(inode->i_mode) && noisy) {
  139                         jfs_err("jfs_commit_inode(0x%p) called on "
  140                                    "read-only volume", inode);
  141                         jfs_err("Is remount racy?");
  142                         noisy--;
  143                 }
  144                 return 0;
  145         }
  146 
  147         tid = txBegin(inode->i_sb, COMMIT_INODE);
  148         down(&JFS_IP(inode)->commit_sem);
  149         rc = txCommit(tid, 1, &inode, wait ? COMMIT_SYNC : 0);
  150         txEnd(tid);
  151         up(&JFS_IP(inode)->commit_sem);
  152         return -rc;
  153 }
  154 
  155 void jfs_write_inode(struct inode *inode, int wait)
  156 {
  157         /*
  158          * If COMMIT_DIRTY is not set, the inode isn't really dirty.
  159          * It has been committed since the last change, but was still
  160          * on the dirty inode list
  161          */
  162         if (test_cflag(COMMIT_Nolink, inode) ||
  163             !test_cflag(COMMIT_Dirty, inode))
  164                 return;
  165 
  166         if (jfs_commit_inode(inode, wait)) {
  167                 jfs_err("jfs_write_inode: jfs_commit_inode failed!");
  168         }
  169 }
  170 
  171 void jfs_delete_inode(struct inode *inode)
  172 {
  173         jfs_info("In jfs_delete_inode, inode = 0x%p", inode);
  174 
  175         if (test_cflag(COMMIT_Freewmap, inode))
  176                 freeZeroLink(inode);
  177 
  178         diFree(inode);
  179 
  180         clear_inode(inode);
  181 }
  182 
  183 void jfs_dirty_inode(struct inode *inode)
  184 {
  185         static int noisy = 5;
  186 
  187         if (isReadOnly(inode)) {
  188                 if (!special_file(inode->i_mode) && noisy) {
  189                         /* kernel allows writes to devices on read-only
  190                          * partitions and may try to mark inode dirty
  191                          */
  192                         jfs_err("jfs_dirty_inode called on read-only volume");
  193                         jfs_err("Is remount racy?");
  194                         noisy--;
  195                 }
  196                 return;
  197         }
  198 
  199         set_cflag(COMMIT_Dirty, inode);
  200 }
  201 
  202 static int jfs_get_block(struct inode *ip, long lblock,
  203                          struct buffer_head *bh_result, int create)
  204 {
  205         s64 lblock64 = lblock;
  206         int no_size_check = 0;
  207         int rc = 0;
  208         int take_locks;
  209         xad_t xad;
  210         s64 xaddr;
  211         int xflag;
  212         s32 xlen;
  213 
  214         /*
  215          * If this is a special inode (imap, dmap) or directory,
  216          * the lock should already be taken
  217          */
  218         take_locks = ((JFS_IP(ip)->fileset != AGGREGATE_I) &&
  219                       !S_ISDIR(ip->i_mode));
  220         /*
  221          * Take appropriate lock on inode
  222          */
  223         if (take_locks) {
  224                 if (create)
  225                         IWRITE_LOCK(ip);
  226                 else
  227                         IREAD_LOCK(ip);
  228         }
  229 
  230         /*
  231          * A directory's "data" is the inode index table, but i_size is the
  232          * size of the d-tree, so don't check the offset against i_size
  233          */
  234         if (S_ISDIR(ip->i_mode))
  235                 no_size_check = 1;
  236 
  237         if ((no_size_check ||
  238              ((lblock64 << ip->i_sb->s_blocksize_bits) < ip->i_size)) &&
  239             (xtLookup(ip, lblock64, 1, &xflag, &xaddr, &xlen, no_size_check)
  240              == 0) && xlen) {
  241                 if (xflag & XAD_NOTRECORDED) {
  242                         if (!create)
  243                                 /*
  244                                  * Allocated but not recorded, read treats
  245                                  * this as a hole
  246                                  */
  247                                 goto unlock;
  248 #ifdef _JFS_4K
  249                         XADoffset(&xad, lblock64);
  250                         XADlength(&xad, xlen);
  251                         XADaddress(&xad, xaddr);
  252 #else                           /* _JFS_4K */
  253                         /*
  254                          * As long as block size = 4K, this isn't a problem.
  255                          * We should mark the whole page not ABNR, but how
  256                          * will we know to mark the other blocks BH_New?
  257                          */
  258                         BUG();
  259 #endif                          /* _JFS_4K */
  260                         rc = extRecord(ip, &xad);
  261                         if (rc)
  262                                 goto unlock;
  263                         bh_result->b_state |= (1UL << BH_New);
  264                 }
  265 
  266                 bh_result->b_dev = ip->i_dev;
  267                 bh_result->b_blocknr = xaddr;
  268                 bh_result->b_state |= (1UL << BH_Mapped);
  269                 goto unlock;
  270         }
  271         if (!create)
  272                 goto unlock;
  273 
  274         /*
  275          * Allocate a new block
  276          */
  277 #ifdef _JFS_4K
  278         if ((rc = extHint(ip, lblock64 << ip->i_sb->s_blocksize_bits, &xad)))
  279                 goto unlock;
  280         rc = extAlloc(ip, 1, lblock64, &xad, FALSE);
  281         if (rc)
  282                 goto unlock;
  283 
  284         bh_result->b_dev = ip->i_dev;
  285         bh_result->b_blocknr = addressXAD(&xad);
  286         bh_result->b_state |= ((1UL << BH_Mapped) | (1UL << BH_New));
  287 
  288 #else                           /* _JFS_4K */
  289         /*
  290          * We need to do whatever it takes to keep all but the last buffers
  291          * in 4K pages - see jfs_write.c
  292          */
  293         BUG();
  294 #endif                          /* _JFS_4K */
  295 
  296       unlock:
  297         /*
  298          * Release lock on inode
  299          */
  300         if (take_locks) {
  301                 if (create)
  302                         IWRITE_UNLOCK(ip);
  303                 else
  304                         IREAD_UNLOCK(ip);
  305         }
  306         return -rc;
  307 }
  308 
  309 static int jfs_writepage(struct page *page)
  310 {
  311         return block_write_full_page(page, jfs_get_block);
  312 }
  313 
  314 static int jfs_readpage(struct file *file, struct page *page)
  315 {
  316         return block_read_full_page(page, jfs_get_block);
  317 }
  318 
  319 static int jfs_prepare_write(struct file *file,
  320                              struct page *page, unsigned from, unsigned to)
  321 {
  322         return block_prepare_write(page, from, to, jfs_get_block);
  323 }
  324 
  325 static int jfs_bmap(struct address_space *mapping, long block)
  326 {
  327         return generic_block_bmap(mapping, block, jfs_get_block);
  328 }
  329 
  330 static int jfs_direct_IO(int rw, struct inode *inode, struct kiobuf *iobuf,
  331                          unsigned long blocknr, int blocksize)
  332 {
  333         return generic_direct_IO(rw, inode, iobuf, blocknr,
  334                                  blocksize, jfs_get_block);
  335 }
  336 
  337 struct address_space_operations jfs_aops = {
  338         .readpage       = jfs_readpage,
  339         .writepage      = jfs_writepage,
  340         .sync_page      = block_sync_page,
  341         .prepare_write  = jfs_prepare_write,
  342         .commit_write   = generic_commit_write,
  343         .bmap           = jfs_bmap,
  344         .direct_IO      = jfs_direct_IO,
  345 };
  346 
  347 /*
  348  * Guts of jfs_truncate.  Called with locks already held.  Can be called
  349  * with directory for truncating directory index table.
  350  */
  351 void jfs_truncate_nolock(struct inode *ip, loff_t length)
  352 {
  353         loff_t newsize;
  354         tid_t tid;
  355 
  356         ASSERT(length >= 0);
  357 
  358         if (test_cflag(COMMIT_Nolink, ip)) {
  359                 xtTruncate(0, ip, length, COMMIT_WMAP);
  360                 return;
  361         }
  362 
  363         do {
  364                 tid = txBegin(ip->i_sb, 0);
  365 
  366                 /*
  367                  * The commit_sem cannot be taken before txBegin.
  368                  * txBegin may block and there is a chance the inode
  369                  * could be marked dirty and need to be committed
  370                  * before txBegin unblocks
  371                  */
  372                 down(&JFS_IP(ip)->commit_sem);
  373 
  374                 newsize = xtTruncate(tid, ip, length,
  375                                      COMMIT_TRUNCATE | COMMIT_PWMAP);
  376                 if (newsize < 0) {
  377                         txEnd(tid);
  378                         up(&JFS_IP(ip)->commit_sem);
  379                         break;
  380                 }
  381 
  382                 ip->i_mtime = ip->i_ctime = CURRENT_TIME;
  383                 mark_inode_dirty(ip);
  384 
  385                 txCommit(tid, 1, &ip, 0);
  386                 txEnd(tid);
  387                 up(&JFS_IP(ip)->commit_sem);
  388         } while (newsize > length);     /* Truncate isn't always atomic */
  389 }
  390 
  391 void jfs_truncate(struct inode *ip)
  392 {
  393         jfs_info("jfs_truncate: size = 0x%lx", (ulong) ip->i_size);
  394 
  395         block_truncate_page(ip->i_mapping, ip->i_size, jfs_get_block);
  396 
  397         IWRITE_LOCK(ip);
  398         jfs_truncate_nolock(ip, ip->i_size);
  399         IWRITE_UNLOCK(ip);
  400 }

Cache object: 1ab27e9e9f165c2a5ac6e41b84c9a464


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.