dir.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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, int wait)
  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. if (wait) {
  90. int err = ops->sync(&dir);
  91. if (!ret)
  92. ret = err;
  93. }
  94. ops->free(&dir);
  95. out:
  96. #endif
  97. return ret;
  98. }
  99. static int
  100. adfs_match(struct qstr *name, struct object_info *obj)
  101. {
  102. int i;
  103. if (name->len != obj->name_len)
  104. return 0;
  105. for (i = 0; i < name->len; i++) {
  106. char c1, c2;
  107. c1 = name->name[i];
  108. c2 = obj->name[i];
  109. if (c1 >= 'A' && c1 <= 'Z')
  110. c1 += 'a' - 'A';
  111. if (c2 >= 'A' && c2 <= 'Z')
  112. c2 += 'a' - 'A';
  113. if (c1 != c2)
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. static int
  119. adfs_dir_lookup_byname(struct inode *inode, struct qstr *name, struct object_info *obj)
  120. {
  121. struct super_block *sb = inode->i_sb;
  122. struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
  123. struct adfs_dir dir;
  124. int ret;
  125. ret = ops->read(sb, inode->i_ino, inode->i_size, &dir);
  126. if (ret)
  127. goto out;
  128. if (ADFS_I(inode)->parent_id != dir.parent_id) {
  129. adfs_error(sb, "parent directory changed under me! (%lx but got %lx)\n",
  130. ADFS_I(inode)->parent_id, dir.parent_id);
  131. ret = -EIO;
  132. goto free_out;
  133. }
  134. obj->parent_id = inode->i_ino;
  135. /*
  136. * '.' is handled by reserved_lookup() in fs/namei.c
  137. */
  138. if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.') {
  139. /*
  140. * Currently unable to fill in the rest of 'obj',
  141. * but this is better than nothing. We need to
  142. * ascend one level to find it's parent.
  143. */
  144. obj->name_len = 0;
  145. obj->file_id = obj->parent_id;
  146. goto free_out;
  147. }
  148. read_lock(&adfs_dir_lock);
  149. ret = ops->setpos(&dir, 0);
  150. if (ret)
  151. goto unlock_out;
  152. ret = -ENOENT;
  153. while (ops->getnext(&dir, obj) == 0) {
  154. if (adfs_match(name, obj)) {
  155. ret = 0;
  156. break;
  157. }
  158. }
  159. unlock_out:
  160. read_unlock(&adfs_dir_lock);
  161. free_out:
  162. ops->free(&dir);
  163. out:
  164. return ret;
  165. }
  166. const struct file_operations adfs_dir_operations = {
  167. .read = generic_read_dir,
  168. .llseek = generic_file_llseek,
  169. .readdir = adfs_readdir,
  170. .fsync = simple_fsync,
  171. };
  172. static int
  173. adfs_hash(struct dentry *parent, struct qstr *qstr)
  174. {
  175. const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
  176. const unsigned char *name;
  177. unsigned long hash;
  178. int i;
  179. if (qstr->len < name_len)
  180. return 0;
  181. /*
  182. * Truncate the name in place, avoids
  183. * having to define a compare function.
  184. */
  185. qstr->len = i = name_len;
  186. name = qstr->name;
  187. hash = init_name_hash();
  188. while (i--) {
  189. char c;
  190. c = *name++;
  191. if (c >= 'A' && c <= 'Z')
  192. c += 'a' - 'A';
  193. hash = partial_name_hash(c, hash);
  194. }
  195. qstr->hash = end_name_hash(hash);
  196. return 0;
  197. }
  198. /*
  199. * Compare two names, taking note of the name length
  200. * requirements of the underlying filesystem.
  201. */
  202. static int
  203. adfs_compare(struct dentry *parent, struct qstr *entry, struct qstr *name)
  204. {
  205. int i;
  206. if (entry->len != name->len)
  207. return 1;
  208. for (i = 0; i < name->len; i++) {
  209. char a, b;
  210. a = entry->name[i];
  211. b = name->name[i];
  212. if (a >= 'A' && a <= 'Z')
  213. a += 'a' - 'A';
  214. if (b >= 'A' && b <= 'Z')
  215. b += 'a' - 'A';
  216. if (a != b)
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. const struct dentry_operations adfs_dentry_operations = {
  222. .d_hash = adfs_hash,
  223. .d_compare = adfs_compare,
  224. };
  225. static struct dentry *
  226. adfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  227. {
  228. struct inode *inode = NULL;
  229. struct object_info obj;
  230. int error;
  231. dentry->d_op = &adfs_dentry_operations;
  232. lock_kernel();
  233. error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
  234. if (error == 0) {
  235. error = -EACCES;
  236. /*
  237. * This only returns NULL if get_empty_inode
  238. * fails.
  239. */
  240. inode = adfs_iget(dir->i_sb, &obj);
  241. if (inode)
  242. error = 0;
  243. }
  244. unlock_kernel();
  245. d_add(dentry, inode);
  246. return ERR_PTR(error);
  247. }
  248. /*
  249. * directories can handle most operations...
  250. */
  251. const struct inode_operations adfs_dir_inode_operations = {
  252. .lookup = adfs_lookup,
  253. .setattr = adfs_notify_change,
  254. };