ifile.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. struct nilfs_ifile_info {
  31. struct nilfs_mdt_info mi;
  32. struct nilfs_palloc_cache palloc_cache;
  33. };
  34. static inline struct nilfs_ifile_info *NILFS_IFILE_I(struct inode *ifile)
  35. {
  36. return (struct nilfs_ifile_info *)NILFS_MDT(ifile);
  37. }
  38. /**
  39. * nilfs_ifile_create_inode - create a new disk inode
  40. * @ifile: ifile inode
  41. * @out_ino: pointer to a variable to store inode number
  42. * @out_bh: buffer_head contains newly allocated disk inode
  43. *
  44. * Return Value: On success, 0 is returned and the newly allocated inode
  45. * number is stored in the place pointed by @ino, and buffer_head pointer
  46. * that contains newly allocated disk inode structure is stored in the
  47. * place pointed by @out_bh
  48. * On error, one of the following negative error codes is returned.
  49. *
  50. * %-EIO - I/O error.
  51. *
  52. * %-ENOMEM - Insufficient amount of memory available.
  53. *
  54. * %-ENOSPC - No inode left.
  55. */
  56. int nilfs_ifile_create_inode(struct inode *ifile, ino_t *out_ino,
  57. struct buffer_head **out_bh)
  58. {
  59. struct nilfs_palloc_req req;
  60. int ret;
  61. req.pr_entry_nr = 0; /* 0 says find free inode from beginning of
  62. a group. dull code!! */
  63. req.pr_entry_bh = NULL;
  64. ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
  65. if (!ret) {
  66. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 1,
  67. &req.pr_entry_bh);
  68. if (ret < 0)
  69. nilfs_palloc_abort_alloc_entry(ifile, &req);
  70. }
  71. if (ret < 0) {
  72. brelse(req.pr_entry_bh);
  73. return ret;
  74. }
  75. nilfs_palloc_commit_alloc_entry(ifile, &req);
  76. mark_buffer_dirty(req.pr_entry_bh);
  77. nilfs_mdt_mark_dirty(ifile);
  78. *out_ino = (ino_t)req.pr_entry_nr;
  79. *out_bh = req.pr_entry_bh;
  80. return 0;
  81. }
  82. /**
  83. * nilfs_ifile_delete_inode - delete a disk inode
  84. * @ifile: ifile inode
  85. * @ino: inode number
  86. *
  87. * Return Value: On success, 0 is returned. On error, one of the following
  88. * negative error codes is returned.
  89. *
  90. * %-EIO - I/O error.
  91. *
  92. * %-ENOMEM - Insufficient amount of memory available.
  93. *
  94. * %-ENOENT - The inode number @ino have not been allocated.
  95. */
  96. int nilfs_ifile_delete_inode(struct inode *ifile, ino_t ino)
  97. {
  98. struct nilfs_palloc_req req = {
  99. .pr_entry_nr = ino, .pr_entry_bh = NULL
  100. };
  101. struct nilfs_inode *raw_inode;
  102. void *kaddr;
  103. int ret;
  104. ret = nilfs_palloc_prepare_free_entry(ifile, &req);
  105. if (!ret) {
  106. ret = nilfs_palloc_get_entry_block(ifile, req.pr_entry_nr, 0,
  107. &req.pr_entry_bh);
  108. if (ret < 0)
  109. nilfs_palloc_abort_free_entry(ifile, &req);
  110. }
  111. if (ret < 0) {
  112. brelse(req.pr_entry_bh);
  113. return ret;
  114. }
  115. kaddr = kmap_atomic(req.pr_entry_bh->b_page);
  116. raw_inode = nilfs_palloc_block_get_entry(ifile, req.pr_entry_nr,
  117. req.pr_entry_bh, kaddr);
  118. raw_inode->i_flags = 0;
  119. kunmap_atomic(kaddr);
  120. mark_buffer_dirty(req.pr_entry_bh);
  121. brelse(req.pr_entry_bh);
  122. nilfs_palloc_commit_free_entry(ifile, &req);
  123. return 0;
  124. }
  125. int nilfs_ifile_get_inode_block(struct inode *ifile, ino_t ino,
  126. struct buffer_head **out_bh)
  127. {
  128. struct super_block *sb = ifile->i_sb;
  129. int err;
  130. if (unlikely(!NILFS_VALID_INODE(sb, ino))) {
  131. nilfs_error(sb, __func__, "bad inode number: %lu",
  132. (unsigned long) ino);
  133. return -EINVAL;
  134. }
  135. err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
  136. if (unlikely(err))
  137. nilfs_warning(sb, __func__, "unable to read inode: %lu",
  138. (unsigned long) ino);
  139. return err;
  140. }
  141. /**
  142. * nilfs_ifile_read - read or get ifile inode
  143. * @sb: super block instance
  144. * @root: root object
  145. * @inode_size: size of an inode
  146. * @raw_inode: on-disk ifile inode
  147. * @inodep: buffer to store the inode
  148. */
  149. int nilfs_ifile_read(struct super_block *sb, struct nilfs_root *root,
  150. size_t inode_size, struct nilfs_inode *raw_inode,
  151. struct inode **inodep)
  152. {
  153. struct inode *ifile;
  154. int err;
  155. ifile = nilfs_iget_locked(sb, root, NILFS_IFILE_INO);
  156. if (unlikely(!ifile))
  157. return -ENOMEM;
  158. if (!(ifile->i_state & I_NEW))
  159. goto out;
  160. err = nilfs_mdt_init(ifile, NILFS_MDT_GFP,
  161. sizeof(struct nilfs_ifile_info));
  162. if (err)
  163. goto failed;
  164. err = nilfs_palloc_init_blockgroup(ifile, inode_size);
  165. if (err)
  166. goto failed;
  167. nilfs_palloc_setup_cache(ifile, &NILFS_IFILE_I(ifile)->palloc_cache);
  168. err = nilfs_read_inode_common(ifile, raw_inode);
  169. if (err)
  170. goto failed;
  171. unlock_new_inode(ifile);
  172. out:
  173. *inodep = ifile;
  174. return 0;
  175. failed:
  176. iget_failed(ifile);
  177. return err;
  178. }