ifile.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ifile.c - NILFS inode file
  3. *
  4. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Amagai Yoshiji.
  17. * Revised by Ryusuke Konishi.
  18. *
  19. */
  20. #include <linux/types.h>
  21. #include <linux/buffer_head.h>
  22. #include "nilfs.h"
  23. #include "mdt.h"
  24. #include "alloc.h"
  25. #include "ifile.h"
  26. /**
  27. * struct nilfs_ifile_info - on-memory private data of ifile
  28. * @mi: on-memory private data of metadata file
  29. * @palloc_cache: persistent object allocator cache of ifile
  30. */
  31. struct nilfs_ifile_info {
  32. struct nilfs_mdt_info mi;
  33. struct nilfs_palloc_cache palloc_cache;
  34. };
  35. static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
  36. {
  37. return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
  38. }
  39. /**
  40. * nilfs_ifile_create_inode - create a new disk inode
  41. * @ifile: ifile inode
  42. * @out_ino: pointer to a variable to store inode number
  43. * @out_bh: buffer_head contains newly allocated disk inode
  44. *
  45. * Return Value: On success, 0 is returned and the newly allocated inode
  46. * number is stored in the place pointed by @ino, and buffer_head pointer
  47. * that contains newly allocated disk inode structure is stored in the
  48. * place pointed by @out_bh
  49. * On error, one of the following negative error codes is returned.
  50. *
  51. * %-EIO - I/O error.
  52. *
  53. * %-ENOMEM - Insufficient amount of memory available.
  54. *
  55. * %-ENOSPC - No inode left.
  56. */
  57. int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
  58. struct buffer_head **out_bh)
  59. {
  60. struct nilfs_palloc_req req;
  61. int ret;
  62. req.pr_entry_nr = 0; /*
  63. * 0 says find free inode from beginning
  64. * of a group. dull code!!
  65. */
  66. req.pr_entry_bh = NULL;
  67. ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
  68. if (!ret) {
  69. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
  70. &req.pr_entry_bh);
  71. if (ret < 0)
  72. nilfs_palloc_abort_alloc_entry(ifile, &req);
  73. }
  74. if (ret < 0) {
  75. brelse(req.pr_entry_bh);
  76. return ret;
  77. }
  78. nilfs_palloc_commit_alloc_entry(ifile, &req);
  79. mark_buffer_dirty(req.pr_entry_bh);
  80. nilfs_mdt_mark_dirty(ifile);
  81. *out_ino = (ino_t)req.pr_entry_nr;
  82. *out_bh = req.pr_entry_bh;
  83. return 0;
  84. }
  85. /**
  86. * nilfs_ifile_delete_inode - delete a disk inode
  87. * @ifile: ifile inode
  88. * @ino: inode number
  89. *
  90. * Return Value: On success, 0 is returned. On error, one of the following
  91. * negative error codes is returned.
  92. *
  93. * %-EIO - I/O error.
  94. *
  95. * %-ENOMEM - Insufficient amount of memory available.
  96. *
  97. * %-ENOENT - The inode number @ino have not been allocated.
  98. */
  99. int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
  100. {
  101. struct nilfs_palloc_req req = {
  102. .pr_entry_nr = ino, .pr_entry_bh = NULL
  103. };
  104. struct nilfs_inode *raw_inode;
  105. void *kaddr;
  106. int ret;
  107. ret = nilfs_palloc_prepare_free_entry(ifile, &req);
  108. if (!ret) {
  109. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
  110. &req.pr_entry_bh);
  111. if (ret < 0)
  112. nilfs_palloc_abort_free_entry(ifile, &req);
  113. }
  114. if (ret < 0) {
  115. brelse(req.pr_entry_bh);
  116. return ret;
  117. }
  118. kaddr = kmap_atomic(req.pr_entry_bh->b_page);
  119. raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
  120. req.pr_entry_bh, kaddr);
  121. raw_inode->i_flags = 0;
  122. kunmap_atomic(kaddr);
  123. mark_buffer_dirty(req.pr_entry_bh);
  124. brelse(req.pr_entry_bh);
  125. nilfs_palloc_commit_free_entry(ifile, &req);
  126. return 0;
  127. }
  128. int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
  129. struct buffer_head **out_bh)
  130. {
  131. struct super_block *sb = ifile->i_sb;
  132. int err;
  133. if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
  134. nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
  135. return -EINVAL;
  136. }
  137. err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
  138. if (unlikely(err))
  139. nilfs_msg(sb, KERN_WARNING, "error %d reading inode: ino=%lu",
  140. err, (unsigned long)ino);
  141. return err;
  142. }
  143. /**
  144. * nilfs_ifile_count_free_inodes - calculate free inodes count
  145. * @ifile: ifile inode
  146. * @nmaxinodes: current maximum of available inodes count [out]
  147. * @nfreeinodes: free inodes count [out]
  148. */
  149. int nilfs_ifile_count_free_inodes(struct inode *ifile,
  150. u64 *nmaxinodes, u64 *nfreeinodes)
  151. {
  152. u64 nused;
  153. int err;
  154. *nmaxinodes = 0;
  155. *nfreeinodes = 0;
  156. nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
  157. err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
  158. if (likely(!err))
  159. *nfreeinodes = *nmaxinodes - nused;
  160. return err;
  161. }
  162. /**
  163. * nilfs_ifile_read - read or get ifile inode
  164. * @sb: super block instance
  165. * @root: root object
  166. * @inode_size: size of an inode
  167. * @raw_inode: on-disk ifile inode
  168. * @inodep: buffer to store the inode
  169. */
  170. int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
  171. size_t inode_size, struct nilfs_inode *raw_inode,
  172. struct inode **inodep)
  173. {
  174. struct inode *ifile;
  175. int err;
  176. ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
  177. if (unlikely(!ifile))
  178. return -ENOMEM;
  179. if (!(ifile->i_state & I_NEW))
  180. goto out;
  181. err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
  182. sizeof(struct nilfs_ifile_info));
  183. if (err)
  184. goto failed;
  185. err = nilfs_palloc_init_blockgroup(ifile, inode_size);
  186. if (err)
  187. goto failed;
  188. nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
  189. err = nilfs_read_inode_common(ifile, raw_inode);
  190. if (err)
  191. goto failed;
  192. unlock_new_inode(ifile);
  193. out:
  194. *inodep = ifile;
  195. return 0;
  196. failed:
  197. iget_failed(ifile);
  198. return err;
  199. }