dir.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 (inode->i_mode & S_IFDIR) {
  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 = dentry->d_parent->d_inode;
  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 = dentry->d_parent->d_inode;
  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 = dentry->d_inode;
  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, int 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, int mode)
  229. {
  230. return omfs_add_node(dir, dentry, mode | S_IFDIR);
  231. }
  232. static int omfs_create(struct inode *dir, struct dentry *dentry, int mode,
  233. struct nameidata *nd)
  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. struct nameidata *nd)
  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 int omfs_fill_chain(struct file *filp, void *dirent, filldir_t filldir,
  269. u64 fsblock, int hindex)
  270. {
  271. struct inode *dir = filp->f_dentry->d_inode;
  272. struct buffer_head *bh;
  273. struct omfs_inode *oi;
  274. u64 self;
  275. int res = 0;
  276. unsigned char d_type;
  277. /* follow chain in this bucket */
  278. while (fsblock != ~0) {
  279. bh = omfs_bread(dir->i_sb, fsblock);
  280. if (!bh)
  281. goto out;
  282. oi = (struct omfs_inode *) bh->b_data;
  283. if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
  284. brelse(bh);
  285. goto out;
  286. }
  287. self = fsblock;
  288. fsblock = be64_to_cpu(oi->i_sibling);
  289. /* skip visited nodes */
  290. if (hindex) {
  291. hindex--;
  292. brelse(bh);
  293. continue;
  294. }
  295. d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
  296. res = filldir(dirent, oi->i_name, strnlen(oi->i_name,
  297. OMFS_NAMELEN), filp->f_pos, self, d_type);
  298. brelse(bh);
  299. if (res < 0)
  300. break;
  301. filp->f_pos++;
  302. }
  303. out:
  304. return res;
  305. }
  306. static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  307. struct inode *new_dir, struct dentry *new_dentry)
  308. {
  309. struct inode *new_inode = new_dentry->d_inode;
  310. struct inode *old_inode = old_dentry->d_inode;
  311. int err;
  312. if (new_inode) {
  313. /* overwriting existing file/dir */
  314. err = omfs_remove(new_dir, new_dentry);
  315. if (err)
  316. goto out;
  317. }
  318. /* since omfs locates files by name, we need to unlink _before_
  319. * adding the new link or we won't find the old one */
  320. err = omfs_delete_entry(old_dentry);
  321. if (err)
  322. goto out;
  323. mark_inode_dirty(old_dir);
  324. err = omfs_add_link(new_dentry, old_inode);
  325. if (err)
  326. goto out;
  327. old_inode->i_ctime = CURRENT_TIME_SEC;
  328. mark_inode_dirty(old_inode);
  329. out:
  330. return err;
  331. }
  332. static int omfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  333. {
  334. struct inode *dir = filp->f_dentry->d_inode;
  335. struct buffer_head *bh;
  336. loff_t offset, res;
  337. unsigned int hchain, hindex;
  338. int nbuckets;
  339. u64 fsblock;
  340. int ret = -EINVAL;
  341. if (filp->f_pos >> 32)
  342. goto success;
  343. switch ((unsigned long) filp->f_pos) {
  344. case 0:
  345. if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
  346. goto success;
  347. filp->f_pos++;
  348. /* fall through */
  349. case 1:
  350. if (filldir(dirent, "..", 2, 1,
  351. parent_ino(filp->f_dentry), DT_DIR) < 0)
  352. goto success;
  353. filp->f_pos = 1 << 20;
  354. /* fall through */
  355. }
  356. nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
  357. /* high 12 bits store bucket + 1 and low 20 bits store hash index */
  358. hchain = (filp->f_pos >> 20) - 1;
  359. hindex = filp->f_pos & 0xfffff;
  360. bh = omfs_bread(dir->i_sb, dir->i_ino);
  361. if (!bh)
  362. goto out;
  363. offset = OMFS_DIR_START + hchain * 8;
  364. for (; hchain < nbuckets; hchain++, offset += 8) {
  365. fsblock = be64_to_cpu(*((__be64 *) &bh->b_data[offset]));
  366. res = omfs_fill_chain(filp, dirent, filldir, fsblock, hindex);
  367. hindex = 0;
  368. if (res < 0)
  369. break;
  370. filp->f_pos = (hchain+2) << 20;
  371. }
  372. brelse(bh);
  373. success:
  374. ret = 0;
  375. out:
  376. return ret;
  377. }
  378. const struct inode_operations omfs_dir_inops = {
  379. .lookup = omfs_lookup,
  380. .mkdir = omfs_mkdir,
  381. .rename = omfs_rename,
  382. .create = omfs_create,
  383. .unlink = omfs_remove,
  384. .rmdir = omfs_remove,
  385. };
  386. const struct file_operations omfs_dir_operations = {
  387. .read = generic_read_dir,
  388. .readdir = omfs_readdir,
  389. .llseek = generic_file_llseek,
  390. };