bad_inode.c 5.4 KB

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