ifile.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * nilfs_ifile_create_inode() allocates a new inode in the ifile metadata
  38. * file and stores the inode number in the variable pointed to by @out_ino,
  39. * as well as storing the ifile's buffer with the disk inode in the location
  40. * pointed to by @out_bh.
  41. *
  42. * Return: 0 on success, or one of the following negative error codes on
  43. * failure:
  44. * * %-EIO - I/O error (including metadata corruption).
  45. * * %-ENOMEM - Insufficient memory available.
  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 = NILFS_FIRST_INO(ifile->i_sb);
  54. req.pr_entry_bh = NULL;
  55. ret = nilfs_palloc_prepare_alloc_entry(ifile, &req, false);
  56. if (!ret) {
  57. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
  58. &req.pr_entry_bh);
  59. if (ret < 0)
  60. nilfs_palloc_abort_alloc_entry(ifile, &req);
  61. }
  62. if (ret < 0) {
  63. brelse(req.pr_entry_bh);
  64. return ret;
  65. }
  66. nilfs_palloc_commit_alloc_entry(ifile, &req);
  67. mark_buffer_dirty(req.pr_entry_bh);
  68. nilfs_mdt_mark_dirty(ifile);
  69. *out_ino = (ino_t)req.pr_entry_nr;
  70. *out_bh = req.pr_entry_bh;
  71. return 0;
  72. }
  73. /**
  74. * nilfs_ifile_delete_inode - delete a disk inode
  75. * @ifile: ifile inode
  76. * @ino: inode number
  77. *
  78. * Return: 0 on success, or one of the following negative error codes on
  79. * failure:
  80. * * %-EIO - I/O error (including metadata corruption).
  81. * * %-ENOENT - Inode number unallocated.
  82. * * %-ENOMEM - Insufficient memory available.
  83. */
  84. int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
  85. {
  86. struct nilfs_palloc_req req = {
  87. .pr_entry_nr = ino, .pr_entry_bh = NULL
  88. };
  89. struct nilfs_inode *raw_inode;
  90. size_t offset;
  91. int ret;
  92. ret = nilfs_palloc_prepare_free_entry(ifile, &req);
  93. if (!ret) {
  94. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
  95. &req.pr_entry_bh);
  96. if (ret < 0)
  97. nilfs_palloc_abort_free_entry(ifile, &req);
  98. }
  99. if (ret < 0) {
  100. brelse(req.pr_entry_bh);
  101. return ret;
  102. }
  103. offset = nilfs_palloc_entry_offset(ifile, req.pr_entry_nr,
  104. req.pr_entry_bh);
  105. raw_inode = kmap_local_folio(req.pr_entry_bh->b_folio, offset);
  106. raw_inode->i_flags = 0;
  107. kunmap_local(raw_inode);
  108. mark_buffer_dirty(req.pr_entry_bh);
  109. brelse(req.pr_entry_bh);
  110. nilfs_palloc_commit_free_entry(ifile, &req);
  111. return 0;
  112. }
  113. int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
  114. struct buffer_head **out_bh)
  115. {
  116. struct super_block *sb = ifile->i_sb;
  117. int err;
  118. if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
  119. nilfs_error(sb, "bad inode number: %lu", (unsigned long)ino);
  120. return -EINVAL;
  121. }
  122. err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
  123. if (unlikely(err))
  124. nilfs_warn(sb, "error %d reading inode: ino=%lu",
  125. err, (unsigned long)ino);
  126. return err;
  127. }
  128. /**
  129. * nilfs_ifile_count_free_inodes - calculate free inodes count
  130. * @ifile: ifile inode
  131. * @nmaxinodes: current maximum of available inodes count [out]
  132. * @nfreeinodes: free inodes count [out]
  133. *
  134. * Return: 0 on success, or a negative error code on failure.
  135. */
  136. int nilfs_ifile_count_free_inodes(struct inode *ifile,
  137. u64 *nmaxinodes, u64 *nfreeinodes)
  138. {
  139. u64 nused;
  140. int err;
  141. *nmaxinodes = 0;
  142. *nfreeinodes = 0;
  143. nused = atomic64_read(&NILFS_I(ifile)->i_root->inodes_count);
  144. err = nilfs_palloc_count_max_entries(ifile, nused, nmaxinodes);
  145. if (likely(!err))
  146. *nfreeinodes = *nmaxinodes - nused;
  147. return err;
  148. }
  149. /**
  150. * nilfs_ifile_read - read or get ifile inode
  151. * @sb: super block instance
  152. * @root: root object
  153. * @cno: number of checkpoint entry to read
  154. * @inode_size: size of an inode
  155. *
  156. * Return: 0 on success, or one of the following negative error codes on
  157. * failure:
  158. * * %-EINVAL - Invalid checkpoint.
  159. * * %-ENOMEM - Insufficient memory available.
  160. * * %-EIO - I/O error (including metadata corruption).
  161. */
  162. int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
  163. __u64 cno, size_t inode_size)
  164. {
  165. struct the_nilfs *nilfs;
  166. struct inode *ifile;
  167. int err;
  168. ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
  169. if (unlikely(!ifile))
  170. return -ENOMEM;
  171. if (!(inode_state_read_once(ifile) & I_NEW))
  172. goto out;
  173. err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
  174. sizeof(struct nilfs_ifile_info));
  175. if (err)
  176. goto failed;
  177. err = nilfs_palloc_init_blockgroup(ifile, inode_size);
  178. if (err)
  179. goto failed;
  180. nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
  181. nilfs = sb->s_fs_info;
  182. err = nilfs_cpfile_read_checkpoint(nilfs->ns_cpfile, cno, root, ifile);
  183. if (err)
  184. goto failed;
  185. unlock_new_inode(ifile);
  186. out:
  187. return 0;
  188. failed:
  189. iget_failed(ifile);
  190. return err;
  191. }