dir.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * OMFS (as used by RIO Karma) directory operations.
  3. * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
  4. * Released under GPL v2.
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/ctype.h>
  8. #include <linux/buffer_head.h>
  9. #include "omfs.h"
  10. static int omfs_hash(const char *name, int namelen, int mod)
  11. {
  12. int i, hash = 0;
  13. for (i = 0; i < namelen; i++)
  14. hash ^= tolower(name[i]) << (i % 24);
  15. return hash % mod;
  16. }
  17. /*
  18. * Finds the bucket for a given name and reads the containing block;
  19. * *ofs is set to the offset of the first list entry.
  20. */
  21. static struct buffer_head *omfs_get_bucket(struct inode *dir,
  22. const char *name, int namelen, int *ofs)
  23. {
  24. int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
  25. int bucket = omfs_hash(name, namelen, nbuckets);
  26. *ofs = OMFS_DIR_START + bucket * 8;
  27. return omfs_bread(dir->i_sb, dir->i_ino);
  28. }
  29. static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
  30. const char *name, int namelen,
  31. u64 *prev_block)
  32. {
  33. struct buffer_head *bh;
  34. struct omfs_inode *oi;
  35. int err = -ENOENT;
  36. *prev_block = ~0;
  37. while (block != ~0) {
  38. bh = omfs_bread(dir->i_sb, block);
  39. if (!bh) {
  40. err = -EIO;
  41. goto err;
  42. }
  43. oi = (struct omfs_inode *) bh->b_data;
  44. if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, block)) {
  45. brelse(bh);
  46. goto err;
  47. }
  48. if (strncmp(oi->i_name, name, namelen) == 0)
  49. return bh;
  50. *prev_block = block;
  51. block = be64_to_cpu(oi->i_sibling);
  52. brelse(bh);
  53. }
  54. err:
  55. return ERR_PTR(err);
  56. }
  57. static struct buffer_head *omfs_find_entry(struct inode *dir,
  58. const char *name, int namelen)
  59. {
  60. struct buffer_head *bh;
  61. int ofs;
  62. u64 block, dummy;
  63. bh = omfs_get_bucket(dir, name, namelen, &ofs);
  64. if (!bh)
  65. return ERR_PTR(-EIO);
  66. block = be64_to_cpu(*((__be64 *) &bh->b_data[ofs]));
  67. brelse(bh);
  68. return omfs_scan_list(dir, block, name, namelen, &dummy);
  69. }
  70. int omfs_make_empty(struct inode *inode, struct super_block *sb)
  71. {
  72. struct omfs_sb_info *sbi = OMFS_SB(sb);
  73. struct buffer_head *bh;
  74. struct omfs_inode *oi;
  75. bh = omfs_bread(sb, inode->i_ino);
  76. if (!bh)
  77. return -ENOMEM;
  78. memset(bh->b_data, 0, sizeof(struct omfs_inode));
  79. if (S_ISDIR(inode->i_mode)) {
  80. memset(&bh->b_data[OMFS_DIR_START], 0xff,
  81. sbi->s_sys_blocksize - OMFS_DIR_START);
  82. } else
  83. omfs_make_empty_table(bh, OMFS_EXTENT_START);
  84. oi = (struct omfs_inode *) bh->b_data;
  85. oi->i_head.h_self = cpu_to_be64(inode->i_ino);
  86. oi->i_sibling = ~cpu_to_be64(0ULL);
  87. mark_buffer_dirty(bh);
  88. brelse(bh);
  89. return 0;
  90. }
  91. static int omfs_add_link(struct dentry *dentry, struct inode *inode)
  92. {
  93. struct inode *dir = d_inode(dentry->d_parent);
  94. const char *name = dentry->d_name.name;
  95. int namelen = dentry->d_name.len;
  96. struct omfs_inode *oi;
  97. struct buffer_head *bh;
  98. u64 block;
  99. __be64 *entry;
  100. int ofs;
  101. /* just prepend to head of queue in proper bucket */
  102. bh = omfs_get_bucket(dir, name, namelen, &ofs);
  103. if (!bh)
  104. goto out;
  105. entry = (__be64 *) &bh->b_data[ofs];
  106. block = be64_to_cpu(*entry);
  107. *entry = cpu_to_be64(inode->i_ino);
  108. mark_buffer_dirty(bh);
  109. brelse(bh);
  110. /* now set the sibling and parent pointers on the new inode */
  111. bh = omfs_bread(dir->i_sb, inode->i_ino);
  112. if (!bh)
  113. goto out;
  114. oi = (struct omfs_inode *) bh->b_data;
  115. memcpy(oi->i_name, name, namelen);
  116. memset(oi->i_name + namelen, 0, OMFS_NAMELEN - namelen);
  117. oi->i_sibling = cpu_to_be64(block);
  118. oi->i_parent = cpu_to_be64(dir->i_ino);
  119. mark_buffer_dirty(bh);
  120. brelse(bh);
  121. dir->i_ctime = CURRENT_TIME_SEC;
  122. /* mark affected inodes dirty to rebuild checksums */
  123. mark_inode_dirty(dir);
  124. mark_inode_dirty(inode);
  125. return 0;
  126. out:
  127. return -ENOMEM;
  128. }
  129. static int omfs_delete_entry(struct dentry *dentry)
  130. {
  131. struct inode *dir = d_inode(dentry->d_parent);
  132. struct inode *dirty;
  133. const char *name = dentry->d_name.name;
  134. int namelen = dentry->d_name.len;
  135. struct omfs_inode *oi;
  136. struct buffer_head *bh, *bh2;
  137. __be64 *entry, next;
  138. u64 block, prev;
  139. int ofs;
  140. int err = -ENOMEM;
  141. /* delete the proper node in the bucket's linked list */
  142. bh = omfs_get_bucket(dir, name, namelen, &ofs);
  143. if (!bh)
  144. goto out;
  145. entry = (__be64 *) &bh->b_data[ofs];
  146. block = be64_to_cpu(*entry);
  147. bh2 = omfs_scan_list(dir, block, name, namelen, &prev);
  148. if (IS_ERR(bh2)) {
  149. err = PTR_ERR(bh2);
  150. goto out_free_bh;
  151. }
  152. oi = (struct omfs_inode *) bh2->b_data;
  153. next = oi->i_sibling;
  154. brelse(bh2);
  155. if (prev != ~0) {
  156. /* found in middle of list, get list ptr */
  157. brelse(bh);
  158. bh = omfs_bread(dir->i_sb, prev);
  159. if (!bh)
  160. goto out;
  161. oi = (struct omfs_inode *) bh->b_data;
  162. entry = &oi->i_sibling;
  163. }
  164. *entry = next;
  165. mark_buffer_dirty(bh);
  166. if (prev != ~0) {
  167. dirty = omfs_iget(dir->i_sb, prev);
  168. if (!IS_ERR(dirty)) {
  169. mark_inode_dirty(dirty);
  170. iput(dirty);
  171. }
  172. }
  173. err = 0;
  174. out_free_bh:
  175. brelse(bh);
  176. out:
  177. return err;
  178. }
  179. static int omfs_dir_is_empty(struct inode *inode)
  180. {
  181. int nbuckets = (inode->i_size - OMFS_DIR_START) / 8;
  182. struct buffer_head *bh;
  183. u64 *ptr;
  184. int i;
  185. bh = omfs_bread(inode->i_sb, inode->i_ino);
  186. if (!bh)
  187. return 0;
  188. ptr = (u64 *) &bh->b_data[OMFS_DIR_START];
  189. for (i = 0; i < nbuckets; i++, ptr++)
  190. if (*ptr != ~0)
  191. break;
  192. brelse(bh);
  193. return *ptr != ~0;
  194. }
  195. static int omfs_remove(struct inode *dir, struct dentry *dentry)
  196. {
  197. struct inode *inode = d_inode(dentry);
  198. int ret;
  199. if (S_ISDIR(inode->i_mode) &&
  200. !omfs_dir_is_empty(inode))
  201. return -ENOTEMPTY;
  202. ret = omfs_delete_entry(dentry);
  203. if (ret)
  204. return ret;
  205. clear_nlink(inode);
  206. mark_inode_dirty(inode);
  207. mark_inode_dirty(dir);
  208. return 0;
  209. }
  210. static int omfs_add_node(struct inode *dir, struct dentry *dentry, umode_t mode)
  211. {
  212. int err;
  213. struct inode *inode = omfs_new_inode(dir, mode);
  214. if (IS_ERR(inode))
  215. return PTR_ERR(inode);
  216. err = omfs_make_empty(inode, dir->i_sb);
  217. if (err)
  218. goto out_free_inode;
  219. err = omfs_add_link(dentry, inode);
  220. if (err)
  221. goto out_free_inode;
  222. d_instantiate(dentry, inode);
  223. return 0;
  224. out_free_inode:
  225. iput(inode);
  226. return err;
  227. }
  228. static int omfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  229. {
  230. return omfs_add_node(dir, dentry, mode | S_IFDIR);
  231. }
  232. static int omfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  233. bool excl)
  234. {
  235. return omfs_add_node(dir, dentry, mode | S_IFREG);
  236. }
  237. static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
  238. unsigned int flags)
  239. {
  240. struct buffer_head *bh;
  241. struct inode *inode = NULL;
  242. if (dentry->d_name.len > OMFS_NAMELEN)
  243. return ERR_PTR(-ENAMETOOLONG);
  244. bh = omfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
  245. if (!IS_ERR(bh)) {
  246. struct omfs_inode *oi = (struct omfs_inode *)bh->b_data;
  247. ino_t ino = be64_to_cpu(oi->i_head.h_self);
  248. brelse(bh);
  249. inode = omfs_iget(dir->i_sb, ino);
  250. if (IS_ERR(inode))
  251. return ERR_CAST(inode);
  252. }
  253. d_add(dentry, inode);
  254. return NULL;
  255. }
  256. /* sanity check block's self pointer */
  257. int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
  258. u64 fsblock)
  259. {
  260. int is_bad;
  261. u64 ino = be64_to_cpu(header->h_self);
  262. is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
  263. (ino > sbi->s_num_blocks));
  264. if (is_bad)
  265. printk(KERN_WARNING "omfs: bad hash chain detected\n");
  266. return is_bad;
  267. }
  268. static bool omfs_fill_chain(struct inode *dir, struct dir_context *ctx,
  269. u64 fsblock, int hindex)
  270. {
  271. /* follow chain in this bucket */
  272. while (fsblock != ~0) {
  273. struct buffer_head *bh = omfs_bread(dir->i_sb, fsblock);
  274. struct omfs_inode *oi;
  275. u64 self;
  276. unsigned char d_type;
  277. if (!bh)
  278. return true;
  279. oi = (struct omfs_inode *) bh->b_data;
  280. if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
  281. brelse(bh);
  282. return true;
  283. }
  284. self = fsblock;
  285. fsblock = be64_to_cpu(oi->i_sibling);
  286. /* skip visited nodes */
  287. if (hindex) {
  288. hindex--;
  289. brelse(bh);
  290. continue;
  291. }
  292. d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
  293. if (!dir_emit(ctx, oi->i_name,
  294. strnlen(oi->i_name, OMFS_NAMELEN),
  295. self, d_type)) {
  296. brelse(bh);
  297. return false;
  298. }
  299. brelse(bh);
  300. ctx->pos++;
  301. }
  302. return true;
  303. }
  304. static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  305. struct inode *new_dir, struct dentry *new_dentry)
  306. {
  307. struct inode *new_inode = d_inode(new_dentry);
  308. struct inode *old_inode = d_inode(old_dentry);
  309. int err;
  310. if (new_inode) {
  311. /* overwriting existing file/dir */
  312. err = omfs_remove(new_dir, new_dentry);
  313. if (err)
  314. goto out;
  315. }
  316. /* since omfs locates files by name, we need to unlink _before_
  317. * adding the new link or we won't find the old one */
  318. err = omfs_delete_entry(old_dentry);
  319. if (err)
  320. goto out;
  321. mark_inode_dirty(old_dir);
  322. err = omfs_add_link(new_dentry, old_inode);
  323. if (err)
  324. goto out;
  325. old_inode->i_ctime = CURRENT_TIME_SEC;
  326. mark_inode_dirty(old_inode);
  327. out:
  328. return err;
  329. }
  330. static int omfs_readdir(struct file *file, struct dir_context *ctx)
  331. {
  332. struct inode *dir = file_inode(file);
  333. struct buffer_head *bh;
  334. __be64 *p;
  335. unsigned int hchain, hindex;
  336. int nbuckets;
  337. if (ctx->pos >> 32)
  338. return -EINVAL;
  339. if (ctx->pos < 1 << 20) {
  340. if (!dir_emit_dots(file, ctx))
  341. return 0;
  342. ctx->pos = 1 << 20;
  343. }
  344. nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
  345. /* high 12 bits store bucket + 1 and low 20 bits store hash index */
  346. hchain = (ctx->pos >> 20) - 1;
  347. hindex = ctx->pos & 0xfffff;
  348. bh = omfs_bread(dir->i_sb, dir->i_ino);
  349. if (!bh)
  350. return -EINVAL;
  351. p = (__be64 *)(bh->b_data + OMFS_DIR_START) + hchain;
  352. for (; hchain < nbuckets; hchain++) {
  353. __u64 fsblock = be64_to_cpu(*p++);
  354. if (!omfs_fill_chain(dir, ctx, fsblock, hindex))
  355. break;
  356. hindex = 0;
  357. ctx->pos = (hchain+2) << 20;
  358. }
  359. brelse(bh);
  360. return 0;
  361. }
  362. const struct inode_operations omfs_dir_inops = {
  363. .lookup = omfs_lookup,
  364. .mkdir = omfs_mkdir,
  365. .rename = omfs_rename,
  366. .create = omfs_create,
  367. .unlink = omfs_remove,
  368. .rmdir = omfs_remove,
  369. };
  370. const struct file_operations omfs_dir_operations = {
  371. .read = generic_read_dir,
  372. .iterate = omfs_readdir,
  373. .llseek = generic_file_llseek,
  374. };