bad_inode.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * linux/fs/bad_inode.c
  3. *
  4. * Copyright (C) 1997, Stephen Tweedie
  5. *
  6. * Provide stub functions for unreadable inodes
  7. *
  8. * Fabian Frederick : August 2003 - All file operations assigned to EIO
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/export.h>
  12. #include <linux/stat.h>
  13. #include <linux/time.h>
  14. #include <linux/namei.h>
  15. #include <linux/poll.h>
  16. static int bad_file_open(struct inode *inode, struct file *filp)
  17. {
  18. return -EIO;
  19. }
  20. static const struct file_operations bad_file_ops =
  21. {
  22. .open = bad_file_open,
  23. };
  24. static int bad_inode_create (struct inode *dir, struct dentry *dentry,
  25. umode_t mode, bool excl)
  26. {
  27. return -EIO;
  28. }
  29. static struct dentry *bad_inode_lookup(struct inode *dir,
  30. struct dentry *dentry, unsigned int flags)
  31. {
  32. return ERR_PTR(-EIO);
  33. }
  34. static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
  35. struct dentry *dentry)
  36. {
  37. return -EIO;
  38. }
  39. static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
  40. {
  41. return -EIO;
  42. }
  43. static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
  44. const char *symname)
  45. {
  46. return -EIO;
  47. }
  48. static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
  49. umode_t mode)
  50. {
  51. return -EIO;
  52. }
  53. static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
  54. {
  55. return -EIO;
  56. }
  57. static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
  58. umode_t mode, dev_t rdev)
  59. {
  60. return -EIO;
  61. }
  62. static int bad_inode_rename2(struct inode *old_dir, struct dentry *old_dentry,
  63. struct inode *new_dir, struct dentry *new_dentry,
  64. unsigned int flags)
  65. {
  66. return -EIO;
  67. }
  68. static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
  69. int buflen)
  70. {
  71. return -EIO;
  72. }
  73. static int bad_inode_permission(struct inode *inode, int mask)
  74. {
  75. return -EIO;
  76. }
  77. static int bad_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  78. struct kstat *stat)
  79. {
  80. return -EIO;
  81. }
  82. static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
  83. {
  84. return -EIO;
  85. }
  86. static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
  87. size_t buffer_size)
  88. {
  89. return -EIO;
  90. }
  91. static const struct inode_operations bad_inode_ops =
  92. {
  93. .create = bad_inode_create,
  94. .lookup = bad_inode_lookup,
  95. .link = bad_inode_link,
  96. .unlink = bad_inode_unlink,
  97. .symlink = bad_inode_symlink,
  98. .mkdir = bad_inode_mkdir,
  99. .rmdir = bad_inode_rmdir,
  100. .mknod = bad_inode_mknod,
  101. .rename2 = bad_inode_rename2,
  102. .readlink = bad_inode_readlink,
  103. /* follow_link must be no-op, otherwise unmounting this inode
  104. won't work */
  105. /* put_link returns void */
  106. /* truncate returns void */
  107. .permission = bad_inode_permission,
  108. .getattr = bad_inode_getattr,
  109. .setattr = bad_inode_setattr,
  110. .listxattr = bad_inode_listxattr,
  111. };
  112. /*
  113. * When a filesystem is unable to read an inode due to an I/O error in
  114. * its read_inode() function, it can call make_bad_inode() to return a
  115. * set of stubs which will return EIO errors as required.
  116. *
  117. * We only need to do limited initialisation: all other fields are
  118. * preinitialised to zero automatically.
  119. */
  120. /**
  121. * make_bad_inode - mark an inode bad due to an I/O error
  122. * @inode: Inode to mark bad
  123. *
  124. * When an inode cannot be read due to a media or remote network
  125. * failure this function makes the inode "bad" and causes I/O operations
  126. * on it to fail from this point on.
  127. */
  128. void make_bad_inode(struct inode *inode)
  129. {
  130. remove_inode_hash(inode);
  131. inode->i_mode = S_IFREG;
  132. inode->i_atime = inode->i_mtime = inode->i_ctime =
  133. current_fs_time(inode->i_sb);
  134. inode->i_op = &bad_inode_ops;
  135. inode->i_opflags &= ~IOP_XATTR;
  136. inode->i_fop = &bad_file_ops;
  137. }
  138. EXPORT_SYMBOL(make_bad_inode);
  139. /*
  140. * This tests whether an inode has been flagged as bad. The test uses
  141. * &bad_inode_ops to cover the case of invalidated inodes as well as
  142. * those created by make_bad_inode() above.
  143. */
  144. /**
  145. * is_bad_inode - is an inode errored
  146. * @inode: inode to test
  147. *
  148. * Returns true if the inode in question has been marked as bad.
  149. */
  150. bool is_bad_inode(struct inode *inode)
  151. {
  152. return (inode->i_op == &bad_inode_ops);
  153. }
  154. EXPORT_SYMBOL(is_bad_inode);
  155. /**
  156. * iget_failed - Mark an under-construction inode as dead and release it
  157. * @inode: The inode to discard
  158. *
  159. * Mark an under-construction inode as dead and release it.
  160. */
  161. void iget_failed(struct inode *inode)
  162. {
  163. make_bad_inode(inode);
  164. unlock_new_inode(inode);
  165. iput(inode);
  166. }
  167. EXPORT_SYMBOL(iget_failed);