dir.c 5.4 KB

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