2008年05月05日

acl.h:

C:
  1. /*
  2.   File: fs//acl.h
  3.  
  4.   (C) 2001 Andreas Gruenbacher, <a .gruenbacher@computer.org>
  5. */
  6.  
  7. #include <linux /posix_acl_xattr.h>
  8.  
  9. //简单说一下acl(访问控制表),比传统的Linux具有更灵活的文件权限设定,文件系统的扩展属性支持acl,这一点在编译的时候可以指定。
  10. #define EXT2_ACL_VERSION        0x0001
  11.  
  12. //定义acl的版本;
  13. typedef struct {
  14.         __le16          e_tag;
  15.         __le16          e_perm;
  16.         __le32          e_id;
  17. } ext2_acl_entry;
  18. //__le16类型的定义:
  19. //typedef __u16 __bitwise __le16;
  20. //看起来比较复杂,其实(在x86平台下)只是一个unsigned short,__bitwise这个只是通过gcc的扩展使用sparse这个工具来进行代码检查。
  21.  
  22. //ext2_acl_entry定义访问控制项的结构体
  23. //__le16          e_tag;   自身的使用者或組
  24. //__le16          e_perm;  允许访问的
  25. //__le32          e_id;
  26.  
  27. typedef struct {
  28.         __le16          e_tag;
  29.         __le16          e_perm;
  30. } ext2_acl_entry_short;
  31. //定义访问控制项的简单结构;
  32.  
  33. typedef struct {
  34.         __le32          a_version;
  35. } ext2_acl_header;
  36.  
  37. //接下来两个函数分别用到了size_t与ssize_t对应在x86平台下分别是unsigned int与int
  38. static inline size_t ext2_acl_size(int count)
  39. {
  40.         if (count <= 4) {
  41.                 return sizeof(ext2_acl_header) +
  42.                        count * sizeof(ext2_acl_entry_short);
  43.         } else {
  44.                 return sizeof(ext2_acl_header) +
  45.                        4 * sizeof(ext2_acl_entry_short) +
  46.                        (count - 4) * sizeof(ext2_acl_entry);
  47.         }
  48. }
  49.  
  50. //该函数用来计算acl的占用空间大小,当使用的acl多于4后剩余的acl使用完整的ext2_acl_entry来记录。
  51. static inline int ext2_acl_count(size_t size)
  52. {
  53.         ssize_t s;
  54.         size -= sizeof(ext2_acl_header);
  55.         s = size - 4 * sizeof(ext2_acl_entry_short);
  56.         if (s <0) {
  57.                 if (size % sizeof(ext2_acl_entry_short))
  58.                         return -1;
  59.                 return size / sizeof(ext2_acl_entry_short);
  60.         } else {
  61.                 if (s % sizeof(ext2_acl_entry))
  62.                         return -1;
  63.                 return s / sizeof(ext2_acl_entry) + 4;
  64.         }
  65. }
  66. //该函数与上面的函数刚好相反
  67. #ifdef CONFIG_EXT2_FS_POSIX_ACL
  68.  
  69. //当内核编译时指定使用的acl属性时这里ext2_permission等函数被申明,否则这些函数被定义为空。
  70. /* Value for inode->u.ext2_i.i_acl and inode->u.ext2_i.i_default_acl
  71.    if the ACL has not been cached */
  72. #define EXT2_ACL_NOT_CACHED ((void *)-1)
  73.  
  74. /* acl.c */
  75. extern int ext2_permission (struct inode *, int, struct nameidata *);
  76. extern int ext2_acl_chmod (struct inode *);
  77. extern int ext2_init_acl (struct inode *, struct inode *);
  78.  
  79. #else
  80. #include </linux><linux /sched.h>
  81. #define ext2_permission NULL
  82. #define ext2_get_acl    NULL
  83. #define ext2_set_acl    NULL
  84.  
  85. static inline int
  86. ext2_acl_chmod (struct inode *inode)
  87. {
  88.         return 0;
  89. }
  90.  
  91. static inline int ext2_init_acl (struct inode *inode, struct inode *dir)
  92. {
  93.         return 0;
  94. }
  95. #endif

file.c:

C:
  1. /*
  2.  *  linux/fs//file.c
  3.  *
  4.  * Copyright (C) 1992, 1993, 1994, 1995
  5.  * Remy Card (card@masi.ibp.fr)
  6.  * Laboratoire MASI - Institut Blaise Pascal
  7.  * Universite Pierre et Marie Curie (Paris VI)
  8.  *
  9.  *  from
  10.  *
  11.  *  linux/fs/minix/file.c
  12.  *
  13.  *  Copyright (C) 1991, 1992  Linus Torvalds
  14.  *
  15.  *   fs regular file handling primitives
  16.  *
  17.  *  64-bit file support on 64-bit platforms by Jakub Jelinek
  18.  *      (jj@sunsite.ms.mff.cuni.cz)
  19.  */
  20.  
  21. #include </linux><linux /time.h>
  22. #include ".h"
  23. #include "xattr.h"
  24. #include "acl.h"
  25.  
  26. /*
  27.  * Called when filp is released. This happens when all file descriptors
  28.  * for a single struct file are closed. Note that different open() calls
  29.  * for the same file yield different struct file structures.
  30.  */
  31. static int ext2_release_file (struct inode * inode, struct file * filp)
  32. {
  33.         if (filp->f_mode & FMODE_WRITE) {
  34.                 mutex_lock(&EXT2_I(inode)->truncate_mutex);
  35.                 ext2_discard_reservation(inode);
  36.                 mutex_unlock(&EXT2_I(inode)->truncate_mutex);
  37.         }
  38.         return 0;
  39. }
  40.  
  41. //该函数释放文件所使用的block,前提是该文件以"写"的模式被打开。
  42. /*
  43.  * We have mostly NULL's here: the current defaults are ok for
  44.  * the filesystem.
  45.  */
  46. const struct file_operations ext2_file_operations = {
  47.         .llseek         = generic_file_llseek,
  48.         .read           = do_sync_read,
  49.         .write          = do_sync_write,
  50.         .aio_read       = generic_file_aio_read,
  51.         .aio_write      = generic_file_aio_write,
  52.         .ioctl          = ext2_ioctl,
  53. #ifdef CONFIG_COMPAT
  54.         .compat_ioctl   = ext2_compat_ioctl,
  55. #endif
  56.         .mmap           = generic_file_mmap,
  57.         .open           = generic_file_open,
  58.         .release        = ext2_release_file,
  59.         .fsync          = ext2_sync_file,
  60.         .splice_read    = generic_file_splice_read,
  61.         .splice_write   = generic_file_splice_write,
  62. };
  63.  
  64. //ext2_file_operations这个结构体定义了上层(VFS)文件操作函数在文件系统这一层中的具体实现函数。
  65. //这里又用到了gcc的扩展使用.[index]这样的方式来初始化结构体,这样结构体中的成员就不会受到顺序的限制。
  66. #ifdef CONFIG_EXT2_FS_XIP
  67. const struct file_operations ext2_xip_file_operations = {
  68.         .llseek         = generic_file_llseek,
  69.         .read           = xip_file_read,
  70.         .write          = xip_file_write,
  71.         .ioctl          = ext2_ioctl,
  72. #ifdef CONFIG_COMPAT
  73.         .compat_ioctl   = ext2_compat_ioctl,
  74. #endif
  75.         .mmap           = xip_file_mmap,
  76.         .open           = generic_file_open,
  77.         .release        = ext2_release_file,
  78.         .fsync          = ext2_sync_file,
  79. };
  80. #endif
  81.  
  82. //ext2_xip_file_operations定义了使用xip(eXecute In Place)的文件操作函数
  83. const struct inode_operations ext2_file_inode_operations = {
  84.         .truncate       = ext2_truncate,
  85. #ifdef CONFIG_EXT2_FS_XATTR
  86.         .setxattr       = generic_setxattr,
  87.         .getxattr       = generic_getxattr,
  88.         .listxattr      = ext2_listxattr,
  89.         .removexattr    = generic_removexattr,
  90. #endif
  91.         .setattr        = ext2_setattr,
  92.         .permission     = ext2_permission,
  93. };
  94. //ext2_file_inode_operations定义了中inode的文件操作函数。

标签 :

发表评论

在下面加入你的评论,或者 trackback 从你的博客站点。 订阅本文的评论。

:

:

:

« Ext2文件系统源代码分析(0)
» Ext2文件系统源代码分析(2)