dir.c 5.7 KB

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