blob: 54f0ce44427200ba32ba0db078faf120a9b5f172 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
12#include <linux/fs.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/stat.h>
15#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
Eric Sandeenbe6aab02007-01-05 16:36:36 -080017#include <linux/poll.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020018#include <linux/fiemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Eric Sandeenbe6aab02007-01-05 16:36:36 -080020static int bad_file_open(struct inode *inode, struct file *filp)
21{
22 return -EIO;
23}
24
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080025static const struct file_operations bad_file_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Eric Sandeenbe6aab02007-01-05 16:36:36 -080027 .open = bad_file_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028};
29
Eric Sandeenbe6aab02007-01-05 16:36:36 -080030static int bad_inode_create (struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -040031 umode_t mode, bool excl)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080032{
33 return -EIO;
34}
35
36static struct dentry *bad_inode_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -040037 struct dentry *dentry, unsigned int flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080038{
39 return ERR_PTR(-EIO);
40}
41
42static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
43 struct dentry *dentry)
44{
45 return -EIO;
46}
47
48static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
49{
50 return -EIO;
51}
52
53static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
54 const char *symname)
55{
56 return -EIO;
57}
58
59static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
Al Viro18bb1db2011-07-26 01:41:39 -040060 umode_t mode)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080061{
62 return -EIO;
63}
64
65static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
66{
67 return -EIO;
68}
69
70static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -040071 umode_t mode, dev_t rdev)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080072{
73 return -EIO;
74}
75
Miklos Szeredia0dbc562014-07-23 15:15:31 +020076static int bad_inode_rename2(struct inode *old_dir, struct dentry *old_dentry,
77 struct inode *new_dir, struct dentry *new_dentry,
78 unsigned int flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080079{
80 return -EIO;
81}
82
83static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
84 int buflen)
85{
86 return -EIO;
87}
88
Al Viro10556cb2011-06-20 19:28:19 -040089static int bad_inode_permission(struct inode *inode, int mask)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080090{
91 return -EIO;
92}
93
David Howellsa528d352017-01-31 16:46:22 +000094static int bad_inode_getattr(const struct path *path, struct kstat *stat,
95 u32 request_mask, unsigned int query_flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080096{
97 return -EIO;
98}
99
100static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
101{
102 return -EIO;
103}
104
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800105static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
106 size_t buffer_size)
107{
108 return -EIO;
109}
110
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100111static const char *bad_inode_get_link(struct dentry *dentry,
112 struct inode *inode,
113 struct delayed_call *done)
114{
115 return ERR_PTR(-EIO);
116}
117
118static struct posix_acl *bad_inode_get_acl(struct inode *inode, int type)
119{
120 return ERR_PTR(-EIO);
121}
122
123static int bad_inode_fiemap(struct inode *inode,
124 struct fiemap_extent_info *fieinfo, u64 start,
125 u64 len)
126{
127 return -EIO;
128}
129
Deepa Dinamani95582b02018-05-08 19:36:02 -0700130static int bad_inode_update_time(struct inode *inode, struct timespec64 *time,
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100131 int flags)
132{
133 return -EIO;
134}
135
136static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry,
137 struct file *file, unsigned int open_flag,
Al Viro44907d72018-06-08 13:32:02 -0400138 umode_t create_mode)
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100139{
140 return -EIO;
141}
142
143static int bad_inode_tmpfile(struct inode *inode, struct dentry *dentry,
144 umode_t mode)
145{
146 return -EIO;
147}
148
149static int bad_inode_set_acl(struct inode *inode, struct posix_acl *acl,
150 int type)
151{
152 return -EIO;
153}
154
Arjan van de Ven754661f2007-02-12 00:55:38 -0800155static const struct inode_operations bad_inode_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800157 .create = bad_inode_create,
158 .lookup = bad_inode_lookup,
159 .link = bad_inode_link,
160 .unlink = bad_inode_unlink,
161 .symlink = bad_inode_symlink,
162 .mkdir = bad_inode_mkdir,
163 .rmdir = bad_inode_rmdir,
164 .mknod = bad_inode_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200165 .rename = bad_inode_rename2,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800166 .readlink = bad_inode_readlink,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800167 .permission = bad_inode_permission,
168 .getattr = bad_inode_getattr,
169 .setattr = bad_inode_setattr,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800170 .listxattr = bad_inode_listxattr,
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100171 .get_link = bad_inode_get_link,
172 .get_acl = bad_inode_get_acl,
173 .fiemap = bad_inode_fiemap,
174 .update_time = bad_inode_update_time,
175 .atomic_open = bad_inode_atomic_open,
176 .tmpfile = bad_inode_tmpfile,
177 .set_acl = bad_inode_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180
181/*
182 * When a filesystem is unable to read an inode due to an I/O error in
183 * its read_inode() function, it can call make_bad_inode() to return a
184 * set of stubs which will return EIO errors as required.
185 *
186 * We only need to do limited initialisation: all other fields are
187 * preinitialised to zero automatically.
188 */
189
190/**
191 * make_bad_inode - mark an inode bad due to an I/O error
192 * @inode: Inode to mark bad
193 *
194 * When an inode cannot be read due to a media or remote network
195 * failure this function makes the inode "bad" and causes I/O operations
196 * on it to fail from this point on.
197 */
198
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800199void make_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 remove_inode_hash(inode);
202
203 inode->i_mode = S_IFREG;
204 inode->i_atime = inode->i_mtime = inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700205 current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 inode->i_op = &bad_inode_ops;
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +0200207 inode->i_opflags &= ~IOP_XATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 inode->i_fop = &bad_file_ops;
209}
210EXPORT_SYMBOL(make_bad_inode);
211
212/*
213 * This tests whether an inode has been flagged as bad. The test uses
214 * &bad_inode_ops to cover the case of invalidated inodes as well as
215 * those created by make_bad_inode() above.
216 */
217
218/**
219 * is_bad_inode - is an inode errored
220 * @inode: inode to test
221 *
222 * Returns true if the inode in question has been marked as bad.
223 */
224
Yaowei Bai0e3ef1f2015-11-19 21:00:11 +0800225bool is_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 return (inode->i_op == &bad_inode_ops);
228}
229
230EXPORT_SYMBOL(is_bad_inode);
David Howellsb46980f2008-02-07 00:15:27 -0800231
232/**
233 * iget_failed - Mark an under-construction inode as dead and release it
234 * @inode: The inode to discard
235 *
236 * Mark an under-construction inode as dead and release it.
237 */
238void iget_failed(struct inode *inode)
239{
240 make_bad_inode(inode);
241 unlock_new_inode(inode);
242 iput(inode);
243}
244EXPORT_SYMBOL(iget_failed);