dir.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * linux/fs/adfs/dir.c
  3. *
  4. * Copyright (C) 1999-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Common directory handling for ADFS
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/adfs_fs.h>
  15. #include <linux/time.h>
  16. #include <linux/stat.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/smp_lock.h>
  19. #include <linux/buffer_head.h> /* for file_fsync() */
  20. #include "adfs.h"
  21. /*
  22. * For future. This should probably be per-directory.
  23. */
  24. static DEFINE_RWLOCK(adfs_dir_lock);
  25. static int
  26. adfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  27. {
  28. struct inode *inode = filp->f_path.dentry->d_inode;
  29. struct super_block *sb = inode->i_sb;
  30. struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
  31. struct object_info obj;
  32. struct adfs_dir dir;
  33. int ret = 0;
  34. lock_kernel();
  35. if (filp->f_pos >> 32)
  36. goto out;
  37. ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
  38. if (ret)
  39. goto out;
  40. switch ((unsigned long)filp->f_pos) {
  41. case 0:
  42. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  43. goto free_out;
  44. filp->f_pos += 1;
  45. case 1:
  46. if (filldir(dirent, "..", 2, 1, dir.parent_id, DT_DIR) < 0)
  47. goto free_out;
  48. filp->f_pos += 1;
  49. default:
  50. break;
  51. }
  52. read_lock(&adfs_dir_lock);
  53. ret = ops->setpos(&dir, filp->f_pos - 2);
  54. if (ret)
  55. goto unlock_out;
  56. while (ops->getnext(&dir, &obj) == 0) {
  57. if (filldir(dirent, obj.name, obj.name_len,
  58. filp->f_pos, obj.file_id, DT_UNKNOWN) < 0)
  59. goto unlock_out;
  60. filp->f_pos += 1;
  61. }
  62. unlock_out:
  63. read_unlock(&adfs_dir_lock);
  64. free_out:
  65. ops->free(&dir);
  66. out:
  67. unlock_kernel();
  68. return ret;
  69. }
  70. int
  71. adfs_dir_update(struct super_block *sb, struct object_info *obj)
  72. {
  73. int ret = -EINVAL;
  74. #ifdef CONFIG_ADFS_FS_RW
  75. struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
  76. struct adfs_dir dir;
  77. printk(KERN_INFO "adfs_dir_update: object %06X in dir %06X\n",
  78. obj->file_id, obj->parent_id);
  79. if (!ops->update) {
  80. ret = -EINVAL;
  81. goto out;
  82. }
  83. ret = ops->read(sb, obj->parent_id, 0, &dir);
  84. if (ret)
  85. goto out;
  86. write_lock(&adfs_dir_lock);
  87. ret = ops->update(&dir, obj);
  88. write_unlock(&adfs_dir_lock);
  89. ops->free(&dir);
  90. out:
  91. #endif
  92. return ret;
  93. }
  94. static int
  95. adfs_match(struct qstr *name, struct object_info *obj)
  96. {
  97. int i;
  98. if (name->len != obj->name_len)
  99. return 0;
  100. for (i = 0; i < name->len; i++) {
  101. char c1, c2;
  102. c1 = name->name[i];
  103. c2 = obj->name[i];
  104. if (c1 >= 'A' && c1 <= 'Z')
  105. c1 += 'a' - 'A';
  106. if (c2 >= 'A' && c2 <= 'Z')
  107. c2 += 'a' - 'A';
  108. if (c1 != c2)
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. static int
  114. adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
  115. {
  116. struct super_block *sb = inode->i_sb;
  117. struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
  118. struct adfs_dir dir;
  119. int ret;
  120. ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
  121. if (ret)
  122. goto out;
  123. if (ADFS_I(inode)->parent_id != dir.parent_id) {
  124. adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
  125. ADFS_I(inode)->parent_id, dir.parent_id);
  126. ret = -EIO;
  127. goto free_out;
  128. }
  129. obj->parent_id = inode->i_ino;
  130. /*
  131. * '.' is handled by reserved_lookup() in fs/namei.c
  132. */
  133. if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
  134. /*
  135. * Currently unable to fill in the rest of 'obj',
  136. * but this is better than nothing. We need to
  137. * ascend one level to find it's parent.
  138. */
  139. obj->name_len = 0;
  140. obj->file_id = obj->parent_id;
  141. goto free_out;
  142. }
  143. read_lock(&adfs_dir_lock);
  144. ret = ops->setpos(&dir, 0);
  145. if (ret)
  146. goto unlock_out;
  147. ret = -ENOENT;
  148. while (ops->getnext(&dir, obj) == 0) {
  149. if (adfs_match(name, obj)) {
  150. ret = 0;
  151. break;
  152. }
  153. }
  154. unlock_out:
  155. read_unlock(&adfs_dir_lock);
  156. free_out:
  157. ops->free(&dir);
  158. out:
  159. return ret;
  160. }
  161. const struct file_operations adfs_dir_operations = {
  162. .read = generic_read_dir,
  163. .readdir = adfs_readdir,
  164. .fsync = file_fsync,
  165. };
  166. static int
  167. adfs_hash(struct dentry *parent, struct qstr *qstr)
  168. {
  169. const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
  170. const unsigned char *name;
  171. unsigned long hash;
  172. int i;
  173. if (qstr->len < name_len)
  174. return 0;
  175. /*
  176. * Truncate the name in place, avoids
  177. * having to define a compare function.
  178. */
  179. qstr->len = i = name_len;
  180. name = qstr->name;
  181. hash = init_name_hash();
  182. while (i--) {
  183. char c;
  184. c = *name++;
  185. if (c >= 'A' && c <= 'Z')
  186. c += 'a' - 'A';
  187. hash = partial_name_hash(c, hash);
  188. }
  189. qstr->hash = end_name_hash(hash);
  190. return 0;
  191. }
  192. /*
  193. * Compare two names, taking note of the name length
  194. * requirements of the underlying filesystem.
  195. */
  196. static int
  197. adfs_compare(struct dentry *parent, struct qstr *entry, struct qstr *name)
  198. {
  199. int i;
  200. if (entry->len != name->len)
  201. return 1;
  202. for (i = 0; i < name->len; i++) {
  203. char a, b;
  204. a = entry->name[i];
  205. b = name->name[i];
  206. if (a >= 'A' && a <= 'Z')
  207. a += 'a' - 'A';
  208. if (b >= 'A' && b <= 'Z')
  209. b += 'a' - 'A';
  210. if (a != b)
  211. return 1;
  212. }
  213. return 0;
  214. }
  215. struct dentry_operations adfs_dentry_operations = {
  216. .d_hash = adfs_hash,
  217. .d_compare = adfs_compare,
  218. };
  219. static struct dentry *
  220. adfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  221. {
  222. struct inode *inode = NULL;
  223. struct object_info obj;
  224. int error;
  225. dentry->d_op = &adfs_dentry_operations;
  226. lock_kernel();
  227. error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
  228. if (error == 0) {
  229. error = -EACCES;
  230. /*
  231. * This only returns NULL if get_empty_inode
  232. * fails.
  233. */
  234. inode = adfs_iget(dir->i_sb, &obj);
  235. if (inode)
  236. error = 0;
  237. }
  238. unlock_kernel();
  239. d_add(dentry, inode);
  240. return ERR_PTR(error);
  241. }
  242. /*
  243. * directories can handle most operations...
  244. */
  245. const struct inode_operations adfs_dir_inode_operations = {
  246. .lookup = adfs_lookup,
  247. .setattr = adfs_notify_change,
  248. };