inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * fs/bfs/inode.c
  3. * BFS superblock and inode operations.
  4. * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com>
  5. * From fs/minix, Copyright (C) 1991, 1992 Linus Torvalds.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/fs.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/vfs.h>
  15. #include <asm/uaccess.h>
  16. #include "bfs.h"
  17. MODULE_AUTHOR("Tigran A. Aivazian <tigran@veritas.com>");
  18. MODULE_DESCRIPTION("SCO UnixWare BFS filesystem for Linux");
  19. MODULE_LICENSE("GPL");
  20. #undef DEBUG
  21. #ifdef DEBUG
  22. #define dprintf(x...) printf(x)
  23. #else
  24. #define dprintf(x...)
  25. #endif
  26. void dump_imap(const char *prefix, struct super_block * s);
  27. static void bfs_read_inode(struct inode * inode)
  28. {
  29. unsigned long ino = inode->i_ino;
  30. struct bfs_inode * di;
  31. struct buffer_head * bh;
  32. int block, off;
  33. if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
  34. printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
  35. make_bad_inode(inode);
  36. return;
  37. }
  38. block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
  39. bh = sb_bread(inode->i_sb, block);
  40. if (!bh) {
  41. printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
  42. make_bad_inode(inode);
  43. return;
  44. }
  45. off = (ino - BFS_ROOT_INO) % BFS_INODES_PER_BLOCK;
  46. di = (struct bfs_inode *)bh->b_data + off;
  47. inode->i_mode = 0x0000FFFF & di->i_mode;
  48. if (di->i_vtype == BFS_VDIR) {
  49. inode->i_mode |= S_IFDIR;
  50. inode->i_op = &bfs_dir_inops;
  51. inode->i_fop = &bfs_dir_operations;
  52. } else if (di->i_vtype == BFS_VREG) {
  53. inode->i_mode |= S_IFREG;
  54. inode->i_op = &bfs_file_inops;
  55. inode->i_fop = &bfs_file_operations;
  56. inode->i_mapping->a_ops = &bfs_aops;
  57. }
  58. inode->i_uid = di->i_uid;
  59. inode->i_gid = di->i_gid;
  60. inode->i_nlink = di->i_nlink;
  61. inode->i_size = BFS_FILESIZE(di);
  62. inode->i_blocks = BFS_FILEBLOCKS(di);
  63. inode->i_blksize = PAGE_SIZE;
  64. inode->i_atime.tv_sec = di->i_atime;
  65. inode->i_mtime.tv_sec = di->i_mtime;
  66. inode->i_ctime.tv_sec = di->i_ctime;
  67. inode->i_atime.tv_nsec = 0;
  68. inode->i_mtime.tv_nsec = 0;
  69. inode->i_ctime.tv_nsec = 0;
  70. BFS_I(inode)->i_dsk_ino = di->i_ino; /* can be 0 so we store a copy */
  71. BFS_I(inode)->i_sblock = di->i_sblock;
  72. BFS_I(inode)->i_eblock = di->i_eblock;
  73. brelse(bh);
  74. }
  75. static int bfs_write_inode(struct inode * inode, int unused)
  76. {
  77. unsigned long ino = inode->i_ino;
  78. struct bfs_inode * di;
  79. struct buffer_head * bh;
  80. int block, off;
  81. if (ino < BFS_ROOT_INO || ino > BFS_SB(inode->i_sb)->si_lasti) {
  82. printf("Bad inode number %s:%08lx\n", inode->i_sb->s_id, ino);
  83. return -EIO;
  84. }
  85. lock_kernel();
  86. block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
  87. bh = sb_bread(inode->i_sb, block);
  88. if (!bh) {
  89. printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
  90. unlock_kernel();
  91. return -EIO;
  92. }
  93. off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
  94. di = (struct bfs_inode *)bh->b_data + off;
  95. if (inode->i_ino == BFS_ROOT_INO)
  96. di->i_vtype = BFS_VDIR;
  97. else
  98. di->i_vtype = BFS_VREG;
  99. di->i_ino = inode->i_ino;
  100. di->i_mode = inode->i_mode;
  101. di->i_uid = inode->i_uid;
  102. di->i_gid = inode->i_gid;
  103. di->i_nlink = inode->i_nlink;
  104. di->i_atime = inode->i_atime.tv_sec;
  105. di->i_mtime = inode->i_mtime.tv_sec;
  106. di->i_ctime = inode->i_ctime.tv_sec;
  107. di->i_sblock = BFS_I(inode)->i_sblock;
  108. di->i_eblock = BFS_I(inode)->i_eblock;
  109. di->i_eoffset = di->i_sblock * BFS_BSIZE + inode->i_size - 1;
  110. mark_buffer_dirty(bh);
  111. brelse(bh);
  112. unlock_kernel();
  113. return 0;
  114. }
  115. static void bfs_delete_inode(struct inode * inode)
  116. {
  117. unsigned long ino = inode->i_ino;
  118. struct bfs_inode * di;
  119. struct buffer_head * bh;
  120. int block, off;
  121. struct super_block * s = inode->i_sb;
  122. struct bfs_sb_info * info = BFS_SB(s);
  123. dprintf("ino=%08lx\n", inode->i_ino);
  124. truncate_inode_pages(&inode->i_data, 0);
  125. if (inode->i_ino < BFS_ROOT_INO || inode->i_ino > info->si_lasti) {
  126. printf("invalid ino=%08lx\n", inode->i_ino);
  127. return;
  128. }
  129. inode->i_size = 0;
  130. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
  131. lock_kernel();
  132. mark_inode_dirty(inode);
  133. block = (ino - BFS_ROOT_INO)/BFS_INODES_PER_BLOCK + 1;
  134. bh = sb_bread(s, block);
  135. if (!bh) {
  136. printf("Unable to read inode %s:%08lx\n", inode->i_sb->s_id, ino);
  137. unlock_kernel();
  138. return;
  139. }
  140. off = (ino - BFS_ROOT_INO)%BFS_INODES_PER_BLOCK;
  141. di = (struct bfs_inode *)bh->b_data + off;
  142. if (di->i_ino) {
  143. info->si_freeb += BFS_FILEBLOCKS(di);
  144. info->si_freei++;
  145. clear_bit(di->i_ino, info->si_imap);
  146. dump_imap("delete_inode", s);
  147. }
  148. di->i_ino = 0;
  149. di->i_sblock = 0;
  150. mark_buffer_dirty(bh);
  151. brelse(bh);
  152. /* if this was the last file, make the previous
  153. block "last files last block" even if there is no real file there,
  154. saves us 1 gap */
  155. if (info->si_lf_eblk == BFS_I(inode)->i_eblock) {
  156. info->si_lf_eblk = BFS_I(inode)->i_sblock - 1;
  157. mark_buffer_dirty(info->si_sbh);
  158. }
  159. unlock_kernel();
  160. clear_inode(inode);
  161. }
  162. static void bfs_put_super(struct super_block *s)
  163. {
  164. struct bfs_sb_info *info = BFS_SB(s);
  165. brelse(info->si_sbh);
  166. kfree(info->si_imap);
  167. kfree(info);
  168. s->s_fs_info = NULL;
  169. }
  170. static int bfs_statfs(struct super_block *s, struct kstatfs *buf)
  171. {
  172. struct bfs_sb_info *info = BFS_SB(s);
  173. u64 id = huge_encode_dev(s->s_bdev->bd_dev);
  174. buf->f_type = BFS_MAGIC;
  175. buf->f_bsize = s->s_blocksize;
  176. buf->f_blocks = info->si_blocks;
  177. buf->f_bfree = buf->f_bavail = info->si_freeb;
  178. buf->f_files = info->si_lasti + 1 - BFS_ROOT_INO;
  179. buf->f_ffree = info->si_freei;
  180. buf->f_fsid.val[0] = (u32)id;
  181. buf->f_fsid.val[1] = (u32)(id >> 32);
  182. buf->f_namelen = BFS_NAMELEN;
  183. return 0;
  184. }
  185. static void bfs_write_super(struct super_block *s)
  186. {
  187. lock_kernel();
  188. if (!(s->s_flags & MS_RDONLY))
  189. mark_buffer_dirty(BFS_SB(s)->si_sbh);
  190. s->s_dirt = 0;
  191. unlock_kernel();
  192. }
  193. static kmem_cache_t * bfs_inode_cachep;
  194. static struct inode *bfs_alloc_inode(struct super_block *sb)
  195. {
  196. struct bfs_inode_info *bi;
  197. bi = kmem_cache_alloc(bfs_inode_cachep, SLAB_KERNEL);
  198. if (!bi)
  199. return NULL;
  200. return &bi->vfs_inode;
  201. }
  202. static void bfs_destroy_inode(struct inode *inode)
  203. {
  204. kmem_cache_free(bfs_inode_cachep, BFS_I(inode));
  205. }
  206. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  207. {
  208. struct bfs_inode_info *bi = foo;
  209. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  210. SLAB_CTOR_CONSTRUCTOR)
  211. inode_init_once(&bi->vfs_inode);
  212. }
  213. static int init_inodecache(void)
  214. {
  215. bfs_inode_cachep = kmem_cache_create("bfs_inode_cache",
  216. sizeof(struct bfs_inode_info),
  217. 0, SLAB_RECLAIM_ACCOUNT,
  218. init_once, NULL);
  219. if (bfs_inode_cachep == NULL)
  220. return -ENOMEM;
  221. return 0;
  222. }
  223. static void destroy_inodecache(void)
  224. {
  225. if (kmem_cache_destroy(bfs_inode_cachep))
  226. printk(KERN_INFO "bfs_inode_cache: not all structures were freed\n");
  227. }
  228. static struct super_operations bfs_sops = {
  229. .alloc_inode = bfs_alloc_inode,
  230. .destroy_inode = bfs_destroy_inode,
  231. .read_inode = bfs_read_inode,
  232. .write_inode = bfs_write_inode,
  233. .delete_inode = bfs_delete_inode,
  234. .put_super = bfs_put_super,
  235. .write_super = bfs_write_super,
  236. .statfs = bfs_statfs,
  237. };
  238. void dump_imap(const char *prefix, struct super_block * s)
  239. {
  240. #if 0
  241. int i;
  242. char *tmpbuf = (char *)get_zeroed_page(GFP_KERNEL);
  243. if (!tmpbuf)
  244. return;
  245. for (i=BFS_SB(s)->si_lasti; i>=0; i--) {
  246. if (i>PAGE_SIZE-100) break;
  247. if (test_bit(i, BFS_SB(s)->si_imap))
  248. strcat(tmpbuf, "1");
  249. else
  250. strcat(tmpbuf, "0");
  251. }
  252. printk(KERN_ERR "BFS-fs: %s: lasti=%08lx <%s>\n", prefix, BFS_SB(s)->si_lasti, tmpbuf);
  253. free_page((unsigned long)tmpbuf);
  254. #endif
  255. }
  256. static int bfs_fill_super(struct super_block *s, void *data, int silent)
  257. {
  258. struct buffer_head * bh;
  259. struct bfs_super_block * bfs_sb;
  260. struct inode * inode;
  261. int i, imap_len;
  262. struct bfs_sb_info * info;
  263. info = kmalloc(sizeof(*info), GFP_KERNEL);
  264. if (!info)
  265. return -ENOMEM;
  266. s->s_fs_info = info;
  267. memset(info, 0, sizeof(*info));
  268. sb_set_blocksize(s, BFS_BSIZE);
  269. bh = sb_bread(s, 0);
  270. if(!bh)
  271. goto out;
  272. bfs_sb = (struct bfs_super_block *)bh->b_data;
  273. if (bfs_sb->s_magic != BFS_MAGIC) {
  274. if (!silent)
  275. printf("No BFS filesystem on %s (magic=%08x)\n",
  276. s->s_id, bfs_sb->s_magic);
  277. goto out;
  278. }
  279. if (BFS_UNCLEAN(bfs_sb, s) && !silent)
  280. printf("%s is unclean, continuing\n", s->s_id);
  281. s->s_magic = BFS_MAGIC;
  282. info->si_bfs_sb = bfs_sb;
  283. info->si_sbh = bh;
  284. info->si_lasti = (bfs_sb->s_start - BFS_BSIZE)/sizeof(struct bfs_inode)
  285. + BFS_ROOT_INO - 1;
  286. imap_len = info->si_lasti/8 + 1;
  287. info->si_imap = kmalloc(imap_len, GFP_KERNEL);
  288. if (!info->si_imap)
  289. goto out;
  290. memset(info->si_imap, 0, imap_len);
  291. for (i=0; i<BFS_ROOT_INO; i++)
  292. set_bit(i, info->si_imap);
  293. s->s_op = &bfs_sops;
  294. inode = iget(s, BFS_ROOT_INO);
  295. if (!inode) {
  296. kfree(info->si_imap);
  297. goto out;
  298. }
  299. s->s_root = d_alloc_root(inode);
  300. if (!s->s_root) {
  301. iput(inode);
  302. kfree(info->si_imap);
  303. goto out;
  304. }
  305. info->si_blocks = (bfs_sb->s_end + 1)>>BFS_BSIZE_BITS; /* for statfs(2) */
  306. info->si_freeb = (bfs_sb->s_end + 1 - bfs_sb->s_start)>>BFS_BSIZE_BITS;
  307. info->si_freei = 0;
  308. info->si_lf_eblk = 0;
  309. info->si_lf_sblk = 0;
  310. info->si_lf_ioff = 0;
  311. for (i=BFS_ROOT_INO; i<=info->si_lasti; i++) {
  312. inode = iget(s,i);
  313. if (BFS_I(inode)->i_dsk_ino == 0)
  314. info->si_freei++;
  315. else {
  316. set_bit(i, info->si_imap);
  317. info->si_freeb -= inode->i_blocks;
  318. if (BFS_I(inode)->i_eblock > info->si_lf_eblk) {
  319. info->si_lf_eblk = BFS_I(inode)->i_eblock;
  320. info->si_lf_sblk = BFS_I(inode)->i_sblock;
  321. info->si_lf_ioff = BFS_INO2OFF(i);
  322. }
  323. }
  324. iput(inode);
  325. }
  326. if (!(s->s_flags & MS_RDONLY)) {
  327. mark_buffer_dirty(bh);
  328. s->s_dirt = 1;
  329. }
  330. dump_imap("read_super", s);
  331. return 0;
  332. out:
  333. brelse(bh);
  334. kfree(info);
  335. s->s_fs_info = NULL;
  336. return -EINVAL;
  337. }
  338. static struct super_block *bfs_get_sb(struct file_system_type *fs_type,
  339. int flags, const char *dev_name, void *data)
  340. {
  341. return get_sb_bdev(fs_type, flags, dev_name, data, bfs_fill_super);
  342. }
  343. static struct file_system_type bfs_fs_type = {
  344. .owner = THIS_MODULE,
  345. .name = "bfs",
  346. .get_sb = bfs_get_sb,
  347. .kill_sb = kill_block_super,
  348. .fs_flags = FS_REQUIRES_DEV,
  349. };
  350. static int __init init_bfs_fs(void)
  351. {
  352. int err = init_inodecache();
  353. if (err)
  354. goto out1;
  355. err = register_filesystem(&bfs_fs_type);
  356. if (err)
  357. goto out;
  358. return 0;
  359. out:
  360. destroy_inodecache();
  361. out1:
  362. return err;
  363. }
  364. static void __exit exit_bfs_fs(void)
  365. {
  366. unregister_filesystem(&bfs_fs_type);
  367. destroy_inodecache();
  368. }
  369. module_init(init_bfs_fs)
  370. module_exit(exit_bfs_fs)