dir_dbl.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * linux/fs/hfs/dir_dbl.c
  3.  *
  4.  * Copyright (C) 1995-1997  Paul H. Hargrove
  5.  * This file may be distributed under the terms of the GNU General Public License.
  6.  *
  7.  * This file contains the inode_operations and file_operations
  8.  * structures for HFS directories.
  9.  *
  10.  * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
  11.  *
  12.  * "XXX" in a comment is a note to myself to consider changing something.
  13.  *
  14.  * In function preconditions the term "valid" applied to a pointer to
  15.  * a structure means that the pointer is non-NULL and the structure it
  16.  * points to has all fields initialized to consistent values.
  17.  */
  18. #include "hfs.h"
  19. #include <linux/hfs_fs_sb.h>
  20. #include <linux/hfs_fs_i.h>
  21. #include <linux/hfs_fs.h>
  22. /*================ Forward declarations ================*/
  23. static struct dentry *dbl_lookup(struct inode *, struct dentry *);
  24. static int dbl_readdir(struct file *, void *, filldir_t);
  25. static int dbl_create(struct inode *, struct dentry *, int);
  26. static int dbl_mkdir(struct inode *, struct dentry *, int);
  27. static int dbl_unlink(struct inode *, struct dentry *);
  28. static int dbl_rmdir(struct inode *, struct dentry *);
  29. static int dbl_rename(struct inode *, struct dentry *,
  30.       struct inode *, struct dentry *);
  31. /*================ Global variables ================*/
  32. #define DOT_LEN 1
  33. #define DOT_DOT_LEN 2
  34. #define ROOTINFO_LEN 8
  35. #define PCNT_ROOTINFO_LEN 9
  36. const struct hfs_name hfs_dbl_reserved1[] = {
  37. {DOT_LEN, "."},
  38. {DOT_DOT_LEN, ".."},
  39. {0, ""},
  40. };
  41. const struct hfs_name hfs_dbl_reserved2[] = {
  42. {ROOTINFO_LEN, "RootInfo"},
  43. {PCNT_ROOTINFO_LEN, "%RootInfo"},
  44. {0, ""},
  45. };
  46. #define DOT (&hfs_dbl_reserved1[0])
  47. #define DOT_DOT (&hfs_dbl_reserved1[1])
  48. #define ROOTINFO (&hfs_dbl_reserved2[0])
  49. #define PCNT_ROOTINFO (&hfs_dbl_reserved2[1])
  50. struct file_operations hfs_dbl_dir_operations = {
  51. read: generic_read_dir,
  52. readdir: dbl_readdir,
  53. fsync: file_fsync,
  54. };
  55. struct inode_operations hfs_dbl_dir_inode_operations = {
  56. create: dbl_create,
  57. lookup: dbl_lookup,
  58. unlink: dbl_unlink,
  59. mkdir: dbl_mkdir,
  60. rmdir: dbl_rmdir,
  61. rename: dbl_rename,
  62. setattr: hfs_notify_change,
  63. };
  64. /*================ File-local functions ================*/
  65. /*
  66.  * is_hdr()
  67.  */
  68. static int is_hdr(struct inode *dir, const char *name, int len)
  69. {
  70. int retval = 0;
  71. if (name[0] == '%') {
  72. struct hfs_cat_entry *entry = HFS_I(dir)->entry;
  73. struct hfs_cat_entry *victim;
  74. struct hfs_name cname;
  75. struct hfs_cat_key key;
  76. hfs_nameout(dir, &cname, name+1, len-1);
  77. hfs_cat_build_key(entry->cnid, &cname, &key);
  78. if ((victim = hfs_cat_get(entry->mdb, &key))) {
  79. hfs_cat_put(victim);
  80. retval = 1;
  81. }
  82. }
  83. return retval;
  84. }
  85. /*
  86.  * dbl_lookup()
  87.  *
  88.  * This is the lookup() entry in the inode_operations structure for
  89.  * HFS directories in the AppleDouble scheme.  The purpose is to
  90.  * generate the inode corresponding to an entry in a directory, given
  91.  * the inode for the directory and the name (and its length) of the
  92.  * entry.
  93.  */
  94. static struct dentry *dbl_lookup(struct inode * dir, struct dentry *dentry)
  95. {
  96. struct hfs_name cname;
  97. struct hfs_cat_entry *entry;
  98. struct hfs_cat_key key;
  99. struct inode *inode = NULL;
  100. dentry->d_op = &hfs_dentry_operations;
  101. entry = HFS_I(dir)->entry;
  102. /* Perform name-mangling */
  103. hfs_nameout(dir, &cname, dentry->d_name.name, dentry->d_name.len);
  104.  
  105. /* no need to check for "."  or ".." */
  106. /* Check for "%RootInfo" if in the root directory. */
  107. if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
  108.     hfs_streq(cname.Name, cname.Len, 
  109.       PCNT_ROOTINFO->Name, PCNT_ROOTINFO_LEN)) {
  110. ++entry->count; /* __hfs_iget() eats one */
  111. inode = hfs_iget(entry, HFS_DBL_HDR, dentry);
  112. goto done;
  113. }
  114. /* Do an hfs_iget() on the mangled name. */
  115. hfs_cat_build_key(entry->cnid, &cname, &key);
  116. inode = hfs_iget(hfs_cat_get(entry->mdb, &key), HFS_DBL_NORM, dentry);
  117. /* Try as a header if not found and first character is '%' */
  118. if (!inode && (dentry->d_name.name[0] == '%')) {
  119. hfs_nameout(dir, &cname, dentry->d_name.name+1,
  120.     dentry->d_name.len-1);
  121. hfs_cat_build_key(entry->cnid, &cname, &key);
  122. inode = hfs_iget(hfs_cat_get(entry->mdb, &key),
  123.  HFS_DBL_HDR, dentry);
  124. }
  125. done:
  126. d_add(dentry, inode);
  127. return NULL;
  128. }
  129. /*
  130.  * dbl_readdir()
  131.  *
  132.  * This is the readdir() entry in the file_operations structure for
  133.  * HFS directories in the AppleDouble scheme.  The purpose is to
  134.  * enumerate the entries in a directory, given the inode of the
  135.  * directory and a (struct file *), the 'f_pos' field of which
  136.  * indicates the location in the directory.  The (struct file *) is
  137.  * updated so that the next call with the same 'dir' and 'filp'
  138.  * arguments will produce the next directory entry.  The entries are
  139.  * returned in 'dirent', which is "filled-in" by calling filldir().
  140.  * This allows the same readdir() function be used for different
  141.  * formats.  We try to read in as many entries as we can before
  142.  * filldir() refuses to take any more.
  143.  *
  144.  * XXX: In the future it may be a good idea to consider not generating
  145.  * metadata files for covered directories since the data doesn't
  146.  * correspond to the mounted directory.  However this requires an
  147.  * iget() for every directory which could be considered an excessive
  148.  * amount of overhead. Since the inode for a mount point is always
  149.  * in-core this is another argument for a call to get an inode if it
  150.  * is in-core or NULL if it is not.
  151.  */
  152. static int dbl_readdir(struct file * filp,
  153.        void * dirent, filldir_t filldir)
  154. {
  155. struct hfs_brec brec;
  156.         struct hfs_cat_entry *entry;
  157. struct inode *dir = filp->f_dentry->d_inode;
  158. entry = HFS_I(dir)->entry;
  159. if (filp->f_pos == 0) {
  160. /* Entry 0 is for "." */
  161. if (filldir(dirent, DOT->Name, DOT_LEN, 0, dir->i_ino,
  162.     DT_DIR)) {
  163. return 0;
  164. }
  165. filp->f_pos = 1;
  166. }
  167. if (filp->f_pos == 1) {
  168. /* Entry 1 is for ".." */
  169. if (filldir(dirent, DOT_DOT->Name, DOT_DOT_LEN, 1,
  170.     hfs_get_hl(entry->key.ParID), DT_DIR)) {
  171. return 0;
  172. }
  173. filp->f_pos = 2;
  174. }
  175. if (filp->f_pos < (dir->i_size - 1)) {
  176.                 hfs_u32 cnid;
  177.                 hfs_u8 type;
  178. if (hfs_cat_open(entry, &brec) ||
  179.     hfs_cat_next(entry, &brec, (filp->f_pos - 1) >> 1,
  180.  &cnid, &type)) {
  181. return 0;
  182. }
  183. while (filp->f_pos < (dir->i_size - 1)) {
  184. unsigned char tmp_name[HFS_NAMEMAX + 1];
  185. ino_t ino;
  186. int is_hdr = (filp->f_pos & 1);
  187. unsigned int len;
  188. if (is_hdr) {
  189. ino = ntohl(cnid) | HFS_DBL_HDR;
  190. tmp_name[0] = '%';
  191. len = 1 + hfs_namein(dir, tmp_name + 1,
  192.     &((struct hfs_cat_key *)brec.key)->CName);
  193. } else {
  194. if (hfs_cat_next(entry, &brec, 1,
  195. &cnid, &type)) {
  196. return 0;
  197. }
  198. ino = ntohl(cnid);
  199. len = hfs_namein(dir, tmp_name,
  200.     &((struct hfs_cat_key *)brec.key)->CName);
  201. }
  202. if (filldir(dirent, tmp_name, len, filp->f_pos, ino,
  203.     DT_UNKNOWN)) {
  204. hfs_cat_close(entry, &brec);
  205. return 0;
  206. }
  207. ++filp->f_pos;
  208. }
  209. hfs_cat_close(entry, &brec);
  210. }
  211. if (filp->f_pos == (dir->i_size - 1)) {
  212. if (entry->cnid == htonl(HFS_ROOT_CNID)) {
  213. /* In root dir last entry is for "%RootInfo" */
  214. if (filldir(dirent, PCNT_ROOTINFO->Name,
  215.     PCNT_ROOTINFO_LEN, filp->f_pos,
  216.     ntohl(entry->cnid) | HFS_DBL_HDR,
  217.     DT_UNKNOWN)) {
  218. return 0;
  219. }
  220. }
  221. ++filp->f_pos;
  222. }
  223. return 0;
  224. }
  225. /*
  226.  * dbl_create()
  227.  *
  228.  * This is the create() entry in the inode_operations structure for
  229.  * AppleDouble directories.  The purpose is to create a new file in
  230.  * a directory and return a corresponding inode, given the inode for
  231.  * the directory and the name (and its length) of the new file.
  232.  */
  233. static int dbl_create(struct inode * dir, struct dentry *dentry,
  234.       int mode)
  235. {
  236. int error;
  237. if (is_hdr(dir, dentry->d_name.name, dentry->d_name.len)) {
  238. error = -EEXIST;
  239. } else {
  240. error = hfs_create(dir, dentry, mode);
  241. }
  242. return error;
  243. }
  244. /*
  245.  * dbl_mkdir()
  246.  *
  247.  * This is the mkdir() entry in the inode_operations structure for
  248.  * AppleDouble directories.  The purpose is to create a new directory
  249.  * in a directory, given the inode for the parent directory and the
  250.  * name (and its length) of the new directory.
  251.  */
  252. static int dbl_mkdir(struct inode * parent, struct dentry *dentry,
  253.      int mode)
  254. {
  255. int error;
  256. if (is_hdr(parent, dentry->d_name.name, dentry->d_name.len)) {
  257. error = -EEXIST;
  258. } else {
  259. error = hfs_mkdir(parent, dentry, mode);
  260. }
  261. return error;
  262. }
  263. /*
  264.  * dbl_unlink()
  265.  *
  266.  * This is the unlink() entry in the inode_operations structure for
  267.  * AppleDouble directories.  The purpose is to delete an existing
  268.  * file, given the inode for the parent directory and the name
  269.  * (and its length) of the existing file.
  270.  */
  271. static int dbl_unlink(struct inode * dir, struct dentry *dentry)
  272. {
  273. int error;
  274. error = hfs_unlink(dir, dentry);
  275. if ((error == -ENOENT) && is_hdr(dir, dentry->d_name.name,
  276.  dentry->d_name.len)) {
  277. error = -EPERM;
  278. }
  279. return error;
  280. }
  281. /*
  282.  * dbl_rmdir()
  283.  *
  284.  * This is the rmdir() entry in the inode_operations structure for
  285.  * AppleDouble directories.  The purpose is to delete an existing
  286.  * directory, given the inode for the parent directory and the name
  287.  * (and its length) of the existing directory.
  288.  */
  289. static int dbl_rmdir(struct inode * parent, struct dentry *dentry)
  290. {
  291. int error;
  292. error = hfs_rmdir(parent, dentry);
  293. if ((error == -ENOENT) && is_hdr(parent, dentry->d_name.name,
  294.  dentry->d_name.len)) {
  295. error = -ENOTDIR;
  296. }
  297. return error;
  298. }
  299. /*
  300.  * dbl_rename()
  301.  *
  302.  * This is the rename() entry in the inode_operations structure for
  303.  * AppleDouble directories.  The purpose is to rename an existing
  304.  * file or directory, given the inode for the current directory and
  305.  * the name (and its length) of the existing file/directory and the
  306.  * inode for the new directory and the name (and its length) of the
  307.  * new file/directory.
  308.  * 
  309.  * XXX: how do we handle must_be_dir?
  310.  */
  311. static int dbl_rename(struct inode *old_dir, struct dentry *old_dentry,
  312.       struct inode *new_dir, struct dentry *new_dentry)
  313. {
  314. int error;
  315. if (is_hdr(new_dir, new_dentry->d_name.name,
  316.    new_dentry->d_name.len)) {
  317. error = -EPERM;
  318. } else {
  319. error = hfs_rename(old_dir, old_dentry,
  320.    new_dir, new_dentry);
  321. if ((error == -ENOENT) /*&& !must_be_dir*/ &&
  322.     is_hdr(old_dir, old_dentry->d_name.name,
  323.    old_dentry->d_name.len)) {
  324. error = -EPERM;
  325. }
  326. }
  327. return error;
  328. }
  329. /* due to the dcache caching negative dentries for non-existent files,
  330.  * we need to drop those entries when a file silently gets created.
  331.  * as far as i can tell, the calls that need to do this are the file
  332.  * related calls (create, rename, and mknod). the directory calls
  333.  * should be immune. the relevant calls in dir.c call drop_dentry 
  334.  * upon successful completion. */
  335. void hfs_dbl_drop_dentry(struct dentry *dentry, const ino_t type)
  336. {
  337.   unsigned char tmp_name[HFS_NAMEMAX + 1];
  338.   struct dentry *de = NULL;
  339.   switch (type) {
  340.   case HFS_DBL_HDR:
  341.    /* given %name, look for name. i don't think this happens. */
  342.    de = hfs_lookup_dentry(dentry->d_parent,
  343.   dentry->d_name.name + 1, dentry->d_name.len - 1);
  344.     break;
  345.   case HFS_DBL_DATA:
  346.     /* given name, look for %name */
  347.     tmp_name[0] = '%';
  348.     strncpy(tmp_name + 1, dentry->d_name.name, HFS_NAMELEN - 1);
  349.     de = hfs_lookup_dentry(dentry->d_parent, 
  350.    tmp_name, dentry->d_name.len + 1);
  351.   }
  352.   if (de) {
  353.     if (!de->d_inode)
  354.       d_drop(de);
  355.     dput(de);
  356.   }
  357. }