dir.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const 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. const 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(const 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, const struct qstr *name, struct object_info *obj)
  107. {
  108. struct super_block *sb = inode->i_sb;
  109. const 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. read_lock(&adfs_dir_lock);
  123. ret = ops->setpos(&dir, 0);
  124. if (ret)
  125. goto unlock_out;
  126. ret = -ENOENT;
  127. while (ops->getnext(&dir, obj) == 0) {
  128. if (adfs_match(name, obj)) {
  129. ret = 0;
  130. break;
  131. }
  132. }
  133. unlock_out:
  134. read_unlock(&adfs_dir_lock);
  135. free_out:
  136. ops->free(&dir);
  137. out:
  138. return ret;
  139. }
  140. const struct file_operations adfs_dir_operations = {
  141. .read = generic_read_dir,
  142. .llseek = generic_file_llseek,
  143. .iterate = adfs_readdir,
  144. .fsync = generic_file_fsync,
  145. };
  146. static int
  147. adfs_hash(const struct dentry *parent, struct qstr *qstr)
  148. {
  149. const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
  150. const unsigned char *name;
  151. unsigned long hash;
  152. int i;
  153. if (qstr->len < name_len)
  154. return 0;
  155. /*
  156. * Truncate the name in place, avoids
  157. * having to define a compare function.
  158. */
  159. qstr->len = i = name_len;
  160. name = qstr->name;
  161. hash = init_name_hash(parent);
  162. while (i--) {
  163. char c;
  164. c = *name++;
  165. if (c >= 'A' && c <= 'Z')
  166. c += 'a' - 'A';
  167. hash = partial_name_hash(c, hash);
  168. }
  169. qstr->hash = end_name_hash(hash);
  170. return 0;
  171. }
  172. /*
  173. * Compare two names, taking note of the name length
  174. * requirements of the underlying filesystem.
  175. */
  176. static int
  177. adfs_compare(const struct dentry *dentry,
  178. unsigned int len, const char *str, const struct qstr *name)
  179. {
  180. int i;
  181. if (len != name->len)
  182. return 1;
  183. for (i = 0; i < name->len; i++) {
  184. char a, b;
  185. a = str[i];
  186. b = name->name[i];
  187. if (a >= 'A' && a <= 'Z')
  188. a += 'a' - 'A';
  189. if (b >= 'A' && b <= 'Z')
  190. b += 'a' - 'A';
  191. if (a != b)
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. const struct dentry_operations adfs_dentry_operations = {
  197. .d_hash = adfs_hash,
  198. .d_compare = adfs_compare,
  199. };
  200. static struct dentry *
  201. adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  202. {
  203. struct inode *inode = NULL;
  204. struct object_info obj;
  205. int error;
  206. error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
  207. if (error == 0) {
  208. /*
  209. * This only returns NULL if get_empty_inode
  210. * fails.
  211. */
  212. inode = adfs_iget(dir->i_sb, &obj);
  213. if (!inode)
  214. inode = ERR_PTR(-EACCES);
  215. } else if (error != -ENOENT) {
  216. inode = ERR_PTR(error);
  217. }
  218. return d_splice_alias(inode, dentry);
  219. }
  220. /*
  221. * directories can handle most operations...
  222. */
  223. const struct inode_operations adfs_dir_inode_operations = {
  224. .lookup = adfs_lookup,
  225. .setattr = adfs_notify_change,
  226. };