ifile.c 5.2 KB

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