journal_ext3.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:7k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * Intermezzo. (C) 1998 Peter J. Braam
  3.  * Intermezzo. (C) 2000 Red Hat, Inc.
  4.  * Intermezzo. (C) 2000 Los Alamos National Laboratory
  5.  * Intermezzo. (C) 2000 TurboLinux, Inc.
  6.  * Intermezzo. (C) 2001 Mountain View Data, Inc.
  7.  */
  8. #include <linux/types.h>
  9. #include <linux/param.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/locks.h>
  18. #include <asm/segment.h>
  19. #include <asm/uaccess.h>
  20. #include <linux/string.h>
  21. #include <linux/smp_lock.h>
  22. #if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
  23. #include <linux/jbd.h>
  24. #include <linux/ext3_fs.h>
  25. #include <linux/ext3_jbd.h>
  26. #endif
  27. #include <linux/intermezzo_fs.h>
  28. #include <linux/intermezzo_upcall.h>
  29. #include <linux/intermezzo_psdev.h>
  30. #include <linux/intermezzo_kml.h>
  31. #if defined(CONFIG_EXT3_FS) || defined (CONFIG_EXT3_FS_MODULE)
  32. #define MAX_PATH_BLOCKS(inode) (PATH_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
  33. #define MAX_NAME_BLOCKS(inode) (NAME_MAX >> EXT3_BLOCK_SIZE_BITS((inode)->i_sb))
  34. /* space requirements: 
  35.    presto_do_truncate: 
  36.         used to truncate the KML forward to next fset->chunksize boundary
  37.           - zero partial block
  38.           - update inode
  39.    presto_write_record: 
  40.         write header (< one block) 
  41.         write one path (< MAX_PATHLEN) 
  42.         possibly write another path (< MAX_PATHLEN)
  43.         write suffix (< one block) 
  44.    presto_update_last_rcvd
  45.         write one block
  46. */
  47. static loff_t presto_e3_freespace(struct presto_cache *cache,
  48.                                          struct super_block *sb)
  49. {
  50.         loff_t freebl = le32_to_cpu(sb->u.ext3_sb.s_es->s_free_blocks_count);
  51.         loff_t avail =   freebl - 
  52.                 le32_to_cpu(sb->u.ext3_sb.s_es->s_r_blocks_count);
  53.         return (avail <<  EXT3_BLOCK_SIZE_BITS(sb));
  54. }
  55. /* start the filesystem journal operations */
  56. static void *presto_e3_trans_start(struct presto_file_set *fset, 
  57.                                    struct inode *inode, 
  58.                                    int op)
  59. {
  60.         int jblocks;
  61.         int trunc_blks, one_path_blks, extra_path_blks, 
  62.                 extra_name_blks, lml_blks; 
  63.         __u32 avail_kmlblocks;
  64.         handle_t *handle;
  65.         if ( presto_no_journal(fset) ||
  66.              strcmp(fset->fset_cache->cache_type, "ext3"))
  67.           {
  68.             CDEBUG(D_JOURNAL, "got cache_type "%s"n",
  69.                    fset->fset_cache->cache_type);
  70.             return NULL;
  71.           }
  72.         avail_kmlblocks = inode->i_sb->u.ext3_sb.s_es->s_free_blocks_count;
  73.         
  74.         if ( avail_kmlblocks < 3 ) {
  75.                 return ERR_PTR(-ENOSPC);
  76.         }
  77.         
  78.         if (  (op != PRESTO_OP_UNLINK && op != PRESTO_OP_RMDIR)
  79.               && avail_kmlblocks < 6 ) {
  80.                 return ERR_PTR(-ENOSPC);
  81.         }            
  82.         /* Need journal space for:
  83.              at least three writes to KML (two one block writes, one a path) 
  84.              possibly a second name (unlink, rmdir)
  85.              possibly a second path (symlink, rename)
  86.              a one block write to the last rcvd file 
  87.         */
  88.         trunc_blks = EXT3_DATA_TRANS_BLOCKS + 1; 
  89.         one_path_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 3;
  90.         lml_blks = 4*EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode) + 2;
  91.         extra_path_blks = EXT3_DATA_TRANS_BLOCKS + MAX_PATH_BLOCKS(inode); 
  92.         extra_name_blks = EXT3_DATA_TRANS_BLOCKS + MAX_NAME_BLOCKS(inode); 
  93.         /* additional blocks appear for "two pathname" operations
  94.            and operations involving the LML records 
  95.         */
  96.         switch (op) {
  97.         case PRESTO_OP_TRUNC:
  98.                 jblocks = one_path_blks + extra_name_blks + trunc_blks
  99.                         + EXT3_DELETE_TRANS_BLOCKS; 
  100.                 break;
  101.         case PRESTO_OP_RELEASE:
  102.                 /* 
  103.                 jblocks = one_path_blks + lml_blks + 2*trunc_blks; 
  104.                 */
  105.                 jblocks = one_path_blks; 
  106.                 break;
  107.         case PRESTO_OP_SETATTR:
  108.                 jblocks = one_path_blks + trunc_blks + 1 ; 
  109.                 break;
  110.         case PRESTO_OP_CREATE:
  111.                 jblocks = one_path_blks + trunc_blks 
  112.                         + EXT3_DATA_TRANS_BLOCKS + 3 + 2; 
  113.                 break;
  114.         case PRESTO_OP_LINK:
  115.                 jblocks = one_path_blks + trunc_blks 
  116.                         + EXT3_DATA_TRANS_BLOCKS + 2; 
  117.                 break;
  118.         case PRESTO_OP_UNLINK:
  119.                 jblocks = one_path_blks + extra_name_blks + trunc_blks
  120.                         + EXT3_DELETE_TRANS_BLOCKS + 2; 
  121.                 break;
  122.         case PRESTO_OP_SYMLINK:
  123.                 jblocks = one_path_blks + extra_path_blks + trunc_blks
  124.                         + EXT3_DATA_TRANS_BLOCKS + 5; 
  125.                 break;
  126.         case PRESTO_OP_MKDIR:
  127.                 jblocks = one_path_blks + trunc_blks
  128.                         + EXT3_DATA_TRANS_BLOCKS + 4 + 2;
  129.                 break;
  130.         case PRESTO_OP_RMDIR:
  131.                 jblocks = one_path_blks + extra_name_blks + trunc_blks
  132.                         + EXT3_DELETE_TRANS_BLOCKS + 1; 
  133.                 break;
  134.         case PRESTO_OP_MKNOD:
  135.                 jblocks = one_path_blks + trunc_blks + 
  136.                         EXT3_DATA_TRANS_BLOCKS + 3 + 2;
  137.                 break;
  138.         case PRESTO_OP_RENAME:
  139.                 jblocks = one_path_blks + extra_path_blks + trunc_blks + 
  140.                         2 * EXT3_DATA_TRANS_BLOCKS + 2 + 3;
  141.                 break;
  142.         case PRESTO_OP_WRITE:
  143.                 jblocks = one_path_blks; 
  144.                 /*  add this when we can wrap our transaction with 
  145.                     that of ext3_file_write (ordered writes)
  146.                     +  EXT3_DATA_TRANS_BLOCKS;
  147.                 */
  148.                 break;
  149.         default:
  150.                 CDEBUG(D_JOURNAL, "invalid operation %d for journaln", op);
  151.                 return NULL;
  152.         }
  153.         CDEBUG(D_JOURNAL, "creating journal handle (%d blocks)n", jblocks);
  154.         /* journal_start/stop does not do its own locking while updating
  155.          * the handle/transaction information. Hence we create our own
  156.          * critical section to protect these calls. -SHP
  157.          */
  158.         lock_kernel();
  159.         handle = journal_start(EXT3_JOURNAL(inode), jblocks);
  160.         unlock_kernel();
  161.         return handle;
  162. }
  163. void presto_e3_trans_commit(struct presto_file_set *fset, void *handle)
  164. {
  165.         if ( presto_no_journal(fset) || !handle)
  166.                 return;
  167.         /* See comments before journal_start above. -SHP */
  168.         lock_kernel();
  169.         journal_stop(handle);
  170.         unlock_kernel();
  171. }
  172. void presto_e3_journal_file_data(struct inode *inode)
  173. {
  174. #ifdef EXT3_JOURNAL_DATA_FL
  175.         inode->u.ext3_i.i_flags |= EXT3_JOURNAL_DATA_FL;
  176. #else
  177. #warning You must have a facility to enable journaled writes for recovery!
  178. #endif
  179. }
  180. struct journal_ops presto_ext3_journal_ops = {
  181.         tr_avail: presto_e3_freespace,
  182.         tr_start:  presto_e3_trans_start,
  183.         tr_commit: presto_e3_trans_commit,
  184.         tr_journal_data: presto_e3_journal_file_data
  185. };
  186. #endif /* CONFIG_EXT3_FS */