nfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* fs/fat/nfs.c
  2. *
  3. * This software is licensed under the terms of the GNU General Public
  4. * License version 2, as published by the Free Software Foundation, and
  5. * may be copied, distributed, and modified under those terms.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/exportfs.h>
  14. #include "fat.h"
  15. /*
  16. * a FAT file handle with fhtype 3 is
  17. * 0/ i_ino - for fast, reliable lookup if still in the cache
  18. * 1/ i_generation - to see if i_ino is still valid
  19. * bit 0 == 0 iff directory
  20. * 2/ i_pos(8-39) - if ino has changed, but still in cache
  21. * 3/ i_pos(4-7)|i_logstart - to semi-verify inode found at i_pos
  22. * 4/ i_pos(0-3)|parent->i_logstart - maybe used to hunt for the file on disc
  23. *
  24. * Hack for NFSv2: Maximum FAT entry number is 28bits and maximum
  25. * i_pos is 40bits (blocknr(32) + dir offset(8)), so two 4bits
  26. * of i_logstart is used to store the directory entry offset.
  27. */
  28. int
  29. fat_encode_fh(struct inode *inode, __u32 *fh, int *lenp, struct inode *parent)
  30. {
  31. int len = *lenp;
  32. struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
  33. loff_t i_pos;
  34. if (len < 5) {
  35. *lenp = 5;
  36. return 255; /* no room */
  37. }
  38. i_pos = fat_i_pos_read(sbi, inode);
  39. *lenp = 5;
  40. fh[0] = inode->i_ino;
  41. fh[1] = inode->i_generation;
  42. fh[2] = i_pos >> 8;
  43. fh[3] = ((i_pos & 0xf0) << 24) | MSDOS_I(inode)->i_logstart;
  44. fh[4] = (i_pos & 0x0f) << 28;
  45. if (parent)
  46. fh[4] |= MSDOS_I(parent)->i_logstart;
  47. return 3;
  48. }
  49. static int fat_is_valid_fh(int fh_len, int fh_type)
  50. {
  51. return ((fh_len >= 5) && (fh_type == 3));
  52. }
  53. /**
  54. * Map a NFS file handle to a corresponding dentry.
  55. * The dentry may or may not be connected to the filesystem root.
  56. */
  57. struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
  58. int fh_len, int fh_type)
  59. {
  60. struct inode *inode = NULL;
  61. u32 *fh = fid->raw;
  62. loff_t i_pos;
  63. unsigned long i_ino;
  64. __u32 i_generation;
  65. int i_logstart;
  66. if (!fat_is_valid_fh(fh_len, fh_type))
  67. return NULL;
  68. i_ino = fh[0];
  69. i_generation = fh[1];
  70. i_logstart = fh[3] & 0x0fffffff;
  71. /* Try i_ino lookup first - fastest and most reliable */
  72. inode = ilookup(sb, i_ino);
  73. if (inode && (inode->i_generation != i_generation)) {
  74. iput(inode);
  75. inode = NULL;
  76. }
  77. if (!inode) {
  78. i_pos = (loff_t)fh[2] << 8;
  79. i_pos |= ((fh[3] >> 24) & 0xf0) | (fh[4] >> 28);
  80. /* try 2 - see if i_pos is in F-d-c
  81. * require i_logstart to be the same
  82. * Will fail if you truncate and then re-write
  83. */
  84. inode = fat_iget(sb, i_pos);
  85. if (inode && MSDOS_I(inode)->i_logstart != i_logstart) {
  86. iput(inode);
  87. inode = NULL;
  88. }
  89. }
  90. /*
  91. * For now, do nothing if the inode is not found.
  92. *
  93. * What we could do is:
  94. *
  95. * - follow the file starting at fh[4], and record the ".." entry,
  96. * and the name of the fh[2] entry.
  97. * - then follow the ".." file finding the next step up.
  98. *
  99. * This way we build a path to the root of the tree. If this works, we
  100. * lookup the path and so get this inode into the cache. Finally try
  101. * the fat_iget lookup again. If that fails, then we are totally out
  102. * of luck. But all that is for another day
  103. */
  104. return d_obtain_alias(inode);
  105. }
  106. /*
  107. * Find the parent for a directory that is not currently connected to
  108. * the filesystem root.
  109. *
  110. * On entry, the caller holds child_dir->d_inode->i_mutex.
  111. */
  112. struct dentry *fat_get_parent(struct dentry *child_dir)
  113. {
  114. struct super_block *sb = child_dir->d_sb;
  115. struct buffer_head *bh = NULL;
  116. struct msdos_dir_entry *de;
  117. loff_t i_pos;
  118. struct dentry *parent;
  119. struct inode *inode;
  120. int err;
  121. lock_super(sb);
  122. err = fat_get_dotdot_entry(child_dir->d_inode, &bh, &de, &i_pos);
  123. if (err) {
  124. parent = ERR_PTR(err);
  125. goto out;
  126. }
  127. inode = fat_build_inode(sb, de, i_pos);
  128. parent = d_obtain_alias(inode);
  129. out:
  130. brelse(bh);
  131. unlock_super(sb);
  132. return parent;
  133. }