ifile.c 5.4 KB

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