dir.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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(dir);
  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. } else if (bh != ERR_PTR(-ENOENT)) {
  251. inode = ERR_CAST(bh);
  252. }
  253. return d_splice_alias(inode, dentry);
  254. }
  255. /* sanity check block's self pointer */
  256. int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
  257. u64 fsblock)
  258. {
  259. int is_bad;
  260. u64 ino = be64_to_cpu(header->h_self);
  261. is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
  262. (ino > sbi->s_num_blocks));
  263. if (is_bad)
  264. printk(KERN_WARNING "omfs: bad hash chain detected\n");
  265. return is_bad;
  266. }
  267. static bool omfs_fill_chain(struct inode *dir, struct dir_context *ctx,
  268. u64 fsblock, int hindex)
  269. {
  270. /* follow chain in this bucket */
  271. while (fsblock != ~0) {
  272. struct buffer_head *bh = omfs_bread(dir->i_sb, fsblock);
  273. struct omfs_inode *oi;
  274. u64 self;
  275. unsigned char d_type;
  276. if (!bh)
  277. return true;
  278. oi = (struct omfs_inode *) bh->b_data;
  279. if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
  280. brelse(bh);
  281. return true;
  282. }
  283. self = fsblock;
  284. fsblock = be64_to_cpu(oi->i_sibling);
  285. /* skip visited nodes */
  286. if (hindex) {
  287. hindex--;
  288. brelse(bh);
  289. continue;
  290. }
  291. d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
  292. if (!dir_emit(ctx, oi->i_name,
  293. strnlen(oi->i_name, OMFS_NAMELEN),
  294. self, d_type)) {
  295. brelse(bh);
  296. return false;
  297. }
  298. brelse(bh);
  299. ctx->pos++;
  300. }
  301. return true;
  302. }
  303. static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  304. struct inode *new_dir, struct dentry *new_dentry,
  305. unsigned int flags)
  306. {
  307. struct inode *new_inode = d_inode(new_dentry);
  308. struct inode *old_inode = d_inode(old_dentry);
  309. int err;
  310. if (flags & ~RENAME_NOREPLACE)
  311. return -EINVAL;
  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(old_inode);
  328. mark_inode_dirty(old_inode);
  329. out:
  330. return err;
  331. }
  332. static int omfs_readdir(struct file *file, struct dir_context *ctx)
  333. {
  334. struct inode *dir = file_inode(file);
  335. struct buffer_head *bh;
  336. __be64 *p;
  337. unsigned int hchain, hindex;
  338. int nbuckets;
  339. if (ctx->pos >> 32)
  340. return -EINVAL;
  341. if (ctx->pos < 1 << 20) {
  342. if (!dir_emit_dots(file, ctx))
  343. return 0;
  344. ctx->pos = 1 << 20;
  345. }
  346. nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
  347. /* high 12 bits store bucket + 1 and low 20 bits store hash index */
  348. hchain = (ctx->pos >> 20) - 1;
  349. hindex = ctx->pos & 0xfffff;
  350. bh = omfs_bread(dir->i_sb, dir->i_ino);
  351. if (!bh)
  352. return -EINVAL;
  353. p = (__be64 *)(bh->b_data + OMFS_DIR_START) + hchain;
  354. for (; hchain < nbuckets; hchain++) {
  355. __u64 fsblock = be64_to_cpu(*p++);
  356. if (!omfs_fill_chain(dir, ctx, fsblock, hindex))
  357. break;
  358. hindex = 0;
  359. ctx->pos = (hchain+2) << 20;
  360. }
  361. brelse(bh);
  362. return 0;
  363. }
  364. const struct inode_operations omfs_dir_inops = {
  365. .lookup = omfs_lookup,
  366. .mkdir = omfs_mkdir,
  367. .rename = omfs_rename,
  368. .create = omfs_create,
  369. .unlink = omfs_remove,
  370. .rmdir = omfs_remove,
  371. };
  372. const struct file_operations omfs_dir_operations = {
  373. .read = generic_read_dir,
  374. .iterate_shared = omfs_readdir,
  375. .llseek = generic_file_llseek,
  376. };