bad_inode.c 5.6 KB

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