bad_inode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/bad_inode.c
  4. *
  5. * Copyright (C) 1997, Stephen Tweedie
  6. *
  7. * Provide stub functions for unreadable inodes
  8. *
  9. * Fabian Frederick : August 2003 - All file operations assigned to EIO
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/export.h>
  13. #include <linux/stat.h>
  14. #include <linux/time.h>
  15. #include <linux/namei.h>
  16. #include <linux/poll.h>
  17. static int bad_file_open(struct inode *inode, struct file *filp)
  18. {
  19. return -EIO;
  20. }
  21. static const struct file_operations bad_file_ops =
  22. {
  23. .open = bad_file_open,
  24. };
  25. static int bad_inode_create (struct inode *dir, struct dentry *dentry,
  26. umode_t mode, bool excl)
  27. {
  28. return -EIO;
  29. }
  30. static struct dentry *bad_inode_lookup(struct inode *dir,
  31. struct dentry *dentry, unsigned int flags)
  32. {
  33. return ERR_PTR(-EIO);
  34. }
  35. static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
  36. struct dentry *dentry)
  37. {
  38. return -EIO;
  39. }
  40. static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
  41. {
  42. return -EIO;
  43. }
  44. static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
  45. const char *symname)
  46. {
  47. return -EIO;
  48. }
  49. static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
  50. umode_t mode)
  51. {
  52. return -EIO;
  53. }
  54. static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
  55. {
  56. return -EIO;
  57. }
  58. static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
  59. umode_t mode, dev_t rdev)
  60. {
  61. return -EIO;
  62. }
  63. static int bad_inode_rename2(struct inode *old_dir, struct dentry *old_dentry,
  64. struct inode *new_dir, struct dentry *new_dentry,
  65. unsigned int flags)
  66. {
  67. return -EIO;
  68. }
  69. static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
  70. int buflen)
  71. {
  72. return -EIO;
  73. }
  74. static int bad_inode_permission(struct inode *inode, int mask)
  75. {
  76. return -EIO;
  77. }
  78. static int bad_inode_getattr(const struct path *path, struct kstat *stat,
  79. u32 request_mask, unsigned int query_flags)
  80. {
  81. return -EIO;
  82. }
  83. static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
  84. {
  85. return -EIO;
  86. }
  87. static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
  88. size_t buffer_size)
  89. {
  90. return -EIO;
  91. }
  92. static const char *bad_inode_get_link(struct dentry *dentry,
  93. struct inode *inode,
  94. struct delayed_call *done)
  95. {
  96. return ERR_PTR(-EIO);
  97. }
  98. static struct posix_acl *bad_inode_get_acl(struct inode *inode, int type)
  99. {
  100. return ERR_PTR(-EIO);
  101. }
  102. static int bad_inode_fiemap(struct inode *inode,
  103. struct fiemap_extent_info *fieinfo, u64 start,
  104. u64 len)
  105. {
  106. return -EIO;
  107. }
  108. static int bad_inode_update_time(struct inode *inode, struct timespec64 *time,
  109. int flags)
  110. {
  111. return -EIO;
  112. }
  113. static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry,
  114. struct file *file, unsigned int open_flag,
  115. umode_t create_mode)
  116. {
  117. return -EIO;
  118. }
  119. static int bad_inode_tmpfile(struct inode *inode, struct dentry *dentry,
  120. umode_t mode)
  121. {
  122. return -EIO;
  123. }
  124. static int bad_inode_set_acl(struct inode *inode, struct posix_acl *acl,
  125. int type)
  126. {
  127. return -EIO;
  128. }
  129. static const struct inode_operations bad_inode_ops =
  130. {
  131. .create = bad_inode_create,
  132. .lookup = bad_inode_lookup,
  133. .link = bad_inode_link,
  134. .unlink = bad_inode_unlink,
  135. .symlink = bad_inode_symlink,
  136. .mkdir = bad_inode_mkdir,
  137. .rmdir = bad_inode_rmdir,
  138. .mknod = bad_inode_mknod,
  139. .rename = bad_inode_rename2,
  140. .readlink = bad_inode_readlink,
  141. .permission = bad_inode_permission,
  142. .getattr = bad_inode_getattr,
  143. .setattr = bad_inode_setattr,
  144. .listxattr = bad_inode_listxattr,
  145. .get_link = bad_inode_get_link,
  146. .get_acl = bad_inode_get_acl,
  147. .fiemap = bad_inode_fiemap,
  148. .update_time = bad_inode_update_time,
  149. .atomic_open = bad_inode_atomic_open,
  150. .tmpfile = bad_inode_tmpfile,
  151. .set_acl = bad_inode_set_acl,
  152. };
  153. /*
  154. * When a filesystem is unable to read an inode due to an I/O error in
  155. * its read_inode() function, it can call make_bad_inode() to return a
  156. * set of stubs which will return EIO errors as required.
  157. *
  158. * We only need to do limited initialisation: all other fields are
  159. * preinitialised to zero automatically.
  160. */
  161. /**
  162. * make_bad_inode - mark an inode bad due to an I/O error
  163. * @inode: Inode to mark bad
  164. *
  165. * When an inode cannot be read due to a media or remote network
  166. * failure this function makes the inode "bad" and causes I/O operations
  167. * on it to fail from this point on.
  168. */
  169. void make_bad_inode(struct inode *inode)
  170. {
  171. remove_inode_hash(inode);
  172. inode->i_mode = S_IFREG;
  173. inode->i_atime = inode->i_mtime = inode->i_ctime =
  174. current_time(inode);
  175. inode->i_op = &bad_inode_ops;
  176. inode->i_opflags &= ~IOP_XATTR;
  177. inode->i_fop = &bad_file_ops;
  178. }
  179. EXPORT_SYMBOL(make_bad_inode);
  180. /*
  181. * This tests whether an inode has been flagged as bad. The test uses
  182. * &bad_inode_ops to cover the case of invalidated inodes as well as
  183. * those created by make_bad_inode() above.
  184. */
  185. /**
  186. * is_bad_inode - is an inode errored
  187. * @inode: inode to test
  188. *
  189. * Returns true if the inode in question has been marked as bad.
  190. */
  191. bool is_bad_inode(struct inode *inode)
  192. {
  193. return (inode->i_op == &bad_inode_ops);
  194. }
  195. EXPORT_SYMBOL(is_bad_inode);
  196. /**
  197. * iget_failed - Mark an under-construction inode as dead and release it
  198. * @inode: The inode to discard
  199. *
  200. * Mark an under-construction inode as dead and release it.
  201. */
  202. void iget_failed(struct inode *inode)
  203. {
  204. make_bad_inode(inode);
  205. unlock_new_inode(inode);
  206. iput(inode);
  207. }
  208. EXPORT_SYMBOL(iget_failed);