bad_inode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/module.h>
  12. #include <linux/stat.h>
  13. #include <linux/time.h>
  14. #include <linux/namei.h>
  15. #include <linux/poll.h>
  16. static loff_t bad_file_llseek(struct file *file, loff_t offset, int origin)
  17. {
  18. return -EIO;
  19. }
  20. static ssize_t bad_file_read(struct file *filp, char __user *buf,
  21. size_t size, loff_t *ppos)
  22. {
  23. return -EIO;
  24. }
  25. static ssize_t bad_file_write(struct file *filp, const char __user *buf,
  26. size_t siz, loff_t *ppos)
  27. {
  28. return -EIO;
  29. }
  30. static ssize_t bad_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  31. unsigned long nr_segs, loff_t pos)
  32. {
  33. return -EIO;
  34. }
  35. static ssize_t bad_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  36. unsigned long nr_segs, loff_t pos)
  37. {
  38. return -EIO;
  39. }
  40. static int bad_file_readdir(struct file *filp, void *dirent, filldir_t filldir)
  41. {
  42. return -EIO;
  43. }
  44. static unsigned int bad_file_poll(struct file *filp, poll_table *wait)
  45. {
  46. return POLLERR;
  47. }
  48. static int bad_file_ioctl (struct inode *inode, struct file *filp,
  49. unsigned int cmd, unsigned long arg)
  50. {
  51. return -EIO;
  52. }
  53. static long bad_file_unlocked_ioctl(struct file *file, unsigned cmd,
  54. unsigned long arg)
  55. {
  56. return -EIO;
  57. }
  58. static long bad_file_compat_ioctl(struct file *file, unsigned int cmd,
  59. unsigned long arg)
  60. {
  61. return -EIO;
  62. }
  63. static int bad_file_mmap(struct file *file, struct vm_area_struct *vma)
  64. {
  65. return -EIO;
  66. }
  67. static int bad_file_open(struct inode *inode, struct file *filp)
  68. {
  69. return -EIO;
  70. }
  71. static int bad_file_flush(struct file *file, fl_owner_t id)
  72. {
  73. return -EIO;
  74. }
  75. static int bad_file_release(struct inode *inode, struct file *filp)
  76. {
  77. return -EIO;
  78. }
  79. static int bad_file_fsync(struct file *file, struct dentry *dentry,
  80. int datasync)
  81. {
  82. return -EIO;
  83. }
  84. static int bad_file_aio_fsync(struct kiocb *iocb, int datasync)
  85. {
  86. return -EIO;
  87. }
  88. static int bad_file_fasync(int fd, struct file *filp, int on)
  89. {
  90. return -EIO;
  91. }
  92. static int bad_file_lock(struct file *file, int cmd, struct file_lock *fl)
  93. {
  94. return -EIO;
  95. }
  96. static ssize_t bad_file_sendpage(struct file *file, struct page *page,
  97. int off, size_t len, loff_t *pos, int more)
  98. {
  99. return -EIO;
  100. }
  101. static unsigned long bad_file_get_unmapped_area(struct file *file,
  102. unsigned long addr, unsigned long len,
  103. unsigned long pgoff, unsigned long flags)
  104. {
  105. return -EIO;
  106. }
  107. static int bad_file_check_flags(int flags)
  108. {
  109. return -EIO;
  110. }
  111. static int bad_file_dir_notify(struct file *file, unsigned long arg)
  112. {
  113. return -EIO;
  114. }
  115. static int bad_file_flock(struct file *filp, int cmd, struct file_lock *fl)
  116. {
  117. return -EIO;
  118. }
  119. static ssize_t bad_file_splice_write(struct pipe_inode_info *pipe,
  120. struct file *out, loff_t *ppos, size_t len,
  121. unsigned int flags)
  122. {
  123. return -EIO;
  124. }
  125. static ssize_t bad_file_splice_read(struct file *in, loff_t *ppos,
  126. struct pipe_inode_info *pipe, size_t len,
  127. unsigned int flags)
  128. {
  129. return -EIO;
  130. }
  131. static const struct file_operations bad_file_ops =
  132. {
  133. .llseek = bad_file_llseek,
  134. .read = bad_file_read,
  135. .write = bad_file_write,
  136. .aio_read = bad_file_aio_read,
  137. .aio_write = bad_file_aio_write,
  138. .readdir = bad_file_readdir,
  139. .poll = bad_file_poll,
  140. .ioctl = bad_file_ioctl,
  141. .unlocked_ioctl = bad_file_unlocked_ioctl,
  142. .compat_ioctl = bad_file_compat_ioctl,
  143. .mmap = bad_file_mmap,
  144. .open = bad_file_open,
  145. .flush = bad_file_flush,
  146. .release = bad_file_release,
  147. .fsync = bad_file_fsync,
  148. .aio_fsync = bad_file_aio_fsync,
  149. .fasync = bad_file_fasync,
  150. .lock = bad_file_lock,
  151. .sendpage = bad_file_sendpage,
  152. .get_unmapped_area = bad_file_get_unmapped_area,
  153. .check_flags = bad_file_check_flags,
  154. .dir_notify = bad_file_dir_notify,
  155. .flock = bad_file_flock,
  156. .splice_write = bad_file_splice_write,
  157. .splice_read = bad_file_splice_read,
  158. };
  159. static int bad_inode_create (struct inode *dir, struct dentry *dentry,
  160. int mode, struct nameidata *nd)
  161. {
  162. return -EIO;
  163. }
  164. static struct dentry *bad_inode_lookup(struct inode *dir,
  165. struct dentry *dentry, struct nameidata *nd)
  166. {
  167. return ERR_PTR(-EIO);
  168. }
  169. static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
  170. struct dentry *dentry)
  171. {
  172. return -EIO;
  173. }
  174. static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
  175. {
  176. return -EIO;
  177. }
  178. static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
  179. const char *symname)
  180. {
  181. return -EIO;
  182. }
  183. static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
  184. int mode)
  185. {
  186. return -EIO;
  187. }
  188. static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
  189. {
  190. return -EIO;
  191. }
  192. static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
  193. int mode, dev_t rdev)
  194. {
  195. return -EIO;
  196. }
  197. static int bad_inode_rename (struct inode *old_dir, struct dentry *old_dentry,
  198. struct inode *new_dir, struct dentry *new_dentry)
  199. {
  200. return -EIO;
  201. }
  202. static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
  203. int buflen)
  204. {
  205. return -EIO;
  206. }
  207. static int bad_inode_permission(struct inode *inode, int mask)
  208. {
  209. return -EIO;
  210. }
  211. static int bad_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  212. struct kstat *stat)
  213. {
  214. return -EIO;
  215. }
  216. static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
  217. {
  218. return -EIO;
  219. }
  220. static int bad_inode_setxattr(struct dentry *dentry, const char *name,
  221. const void *value, size_t size, int flags)
  222. {
  223. return -EIO;
  224. }
  225. static ssize_t bad_inode_getxattr(struct dentry *dentry, const char *name,
  226. void *buffer, size_t size)
  227. {
  228. return -EIO;
  229. }
  230. static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
  231. size_t buffer_size)
  232. {
  233. return -EIO;
  234. }
  235. static int bad_inode_removexattr(struct dentry *dentry, const char *name)
  236. {
  237. return -EIO;
  238. }
  239. static const struct inode_operations bad_inode_ops =
  240. {
  241. .create = bad_inode_create,
  242. .lookup = bad_inode_lookup,
  243. .link = bad_inode_link,
  244. .unlink = bad_inode_unlink,
  245. .symlink = bad_inode_symlink,
  246. .mkdir = bad_inode_mkdir,
  247. .rmdir = bad_inode_rmdir,
  248. .mknod = bad_inode_mknod,
  249. .rename = bad_inode_rename,
  250. .readlink = bad_inode_readlink,
  251. /* follow_link must be no-op, otherwise unmounting this inode
  252. won't work */
  253. /* put_link returns void */
  254. /* truncate returns void */
  255. .permission = bad_inode_permission,
  256. .getattr = bad_inode_getattr,
  257. .setattr = bad_inode_setattr,
  258. .setxattr = bad_inode_setxattr,
  259. .getxattr = bad_inode_getxattr,
  260. .listxattr = bad_inode_listxattr,
  261. .removexattr = bad_inode_removexattr,
  262. /* truncate_range returns void */
  263. };
  264. /*
  265. * When a filesystem is unable to read an inode due to an I/O error in
  266. * its read_inode() function, it can call make_bad_inode() to return a
  267. * set of stubs which will return EIO errors as required.
  268. *
  269. * We only need to do limited initialisation: all other fields are
  270. * preinitialised to zero automatically.
  271. */
  272. /**
  273. * make_bad_inode - mark an inode bad due to an I/O error
  274. * @inode: Inode to mark bad
  275. *
  276. * When an inode cannot be read due to a media or remote network
  277. * failure this function makes the inode "bad" and causes I/O operations
  278. * on it to fail from this point on.
  279. */
  280. void make_bad_inode(struct inode *inode)
  281. {
  282. remove_inode_hash(inode);
  283. inode->i_mode = S_IFREG;
  284. inode->i_atime = inode->i_mtime = inode->i_ctime =
  285. current_fs_time(inode->i_sb);
  286. inode->i_op = &bad_inode_ops;
  287. inode->i_fop = &bad_file_ops;
  288. }
  289. EXPORT_SYMBOL(make_bad_inode);
  290. /*
  291. * This tests whether an inode has been flagged as bad. The test uses
  292. * &bad_inode_ops to cover the case of invalidated inodes as well as
  293. * those created by make_bad_inode() above.
  294. */
  295. /**
  296. * is_bad_inode - is an inode errored
  297. * @inode: inode to test
  298. *
  299. * Returns true if the inode in question has been marked as bad.
  300. */
  301. int is_bad_inode(struct inode *inode)
  302. {
  303. return (inode->i_op == &bad_inode_ops);
  304. }
  305. EXPORT_SYMBOL(is_bad_inode);
  306. /**
  307. * iget_failed - Mark an under-construction inode as dead and release it
  308. * @inode: The inode to discard
  309. *
  310. * Mark an under-construction inode as dead and release it.
  311. */
  312. void iget_failed(struct inode *inode)
  313. {
  314. make_bad_inode(inode);
  315. unlock_new_inode(inode);
  316. iput(inode);
  317. }
  318. EXPORT_SYMBOL(iget_failed);