ifile.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * 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; /* 0 says find free inode from beginning of
  54. a group. dull code!! */
  55. req.pr_entry_bh = NULL;
  56. ret = nilfs_palloc_prepare_alloc_entry(ifile, &req);
  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. nilfs_mdt_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_atomic(req.pr_entry_bh->b_page, KM_USER0);
  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_atomic(kaddr, KM_USER0);
  112. nilfs_mdt_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, __func__, "bad inode number: %lu",
  124. (unsigned long) ino);
  125. return -EINVAL;
  126. }
  127. err = nilfs_palloc_get_entry_block(ifile, ino, 0, out_bh);
  128. if (unlikely(err)) {
  129. if (err == -EINVAL)
  130. nilfs_error(sb, __func__, "ifile is broken");
  131. else
  132. nilfs_warning(sb, __func__,
  133. "unable to read inode: %lu",
  134. (unsigned long) ino);
  135. }
  136. return err;
  137. }