ifile.c 5.9 KB

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