dir.c 5.6 KB

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