blob: 125e8bbd22a250e3ea1c7710f29f942f7eaef150 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Eric Sandeenbe6aab02007-01-05 16:36:36 -080019static int bad_file_open(struct inode *inode, struct file *filp)
20{
21 return -EIO;
22}
23
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080024static const struct file_operations bad_file_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Eric Sandeenbe6aab02007-01-05 16:36:36 -080026 .open = bad_file_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
Eric Sandeenbe6aab02007-01-05 16:36:36 -080029static int bad_inode_create (struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -040030 umode_t mode, bool excl)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080031{
32 return -EIO;
33}
34
35static struct dentry *bad_inode_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -040036 struct dentry *dentry, unsigned int flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080037{
38 return ERR_PTR(-EIO);
39}
40
41static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
42 struct dentry *dentry)
43{
44 return -EIO;
45}
46
47static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
48{
49 return -EIO;
50}
51
52static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
53 const char *symname)
54{
55 return -EIO;
56}
57
58static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
Al Viro18bb1db2011-07-26 01:41:39 -040059 umode_t mode)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080060{
61 return -EIO;
62}
63
64static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
65{
66 return -EIO;
67}
68
69static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -040070 umode_t mode, dev_t rdev)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080071{
72 return -EIO;
73}
74
Miklos Szeredia0dbc562014-07-23 15:15:31 +020075static int bad_inode_rename2(struct inode *old_dir, struct dentry *old_dentry,
76 struct inode *new_dir, struct dentry *new_dentry,
77 unsigned int flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080078{
79 return -EIO;
80}
81
82static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
83 int buflen)
84{
85 return -EIO;
86}
87
Al Viro10556cb22011-06-20 19:28:19 -040088static int bad_inode_permission(struct inode *inode, int mask)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080089{
90 return -EIO;
91}
92
David Howellsa528d352017-01-31 16:46:22 +000093static int bad_inode_getattr(const struct path *path, struct kstat *stat,
94 u32 request_mask, unsigned int query_flags)
Eric Sandeenbe6aab02007-01-05 16:36:36 -080095{
96 return -EIO;
97}
98
99static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
100{
101 return -EIO;
102}
103
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800104static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
105 size_t buffer_size)
106{
107 return -EIO;
108}
109
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100110static const char *bad_inode_get_link(struct dentry *dentry,
111 struct inode *inode,
112 struct delayed_call *done)
113{
114 return ERR_PTR(-EIO);
115}
116
117static struct posix_acl *bad_inode_get_acl(struct inode *inode, int type)
118{
119 return ERR_PTR(-EIO);
120}
121
122static int bad_inode_fiemap(struct inode *inode,
123 struct fiemap_extent_info *fieinfo, u64 start,
124 u64 len)
125{
126 return -EIO;
127}
128
Deepa Dinamani95582b02018-05-08 19:36:02 -0700129static int bad_inode_update_time(struct inode *inode, struct timespec64 *time,
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100130 int flags)
131{
132 return -EIO;
133}
134
135static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry,
136 struct file *file, unsigned int open_flag,
137 umode_t create_mode, int *opened)
138{
139 return -EIO;
140}
141
142static int bad_inode_tmpfile(struct inode *inode, struct dentry *dentry,
143 umode_t mode)
144{
145 return -EIO;
146}
147
148static int bad_inode_set_acl(struct inode *inode, struct posix_acl *acl,
149 int type)
150{
151 return -EIO;
152}
153
Arjan van de Ven754661f2007-02-12 00:55:38 -0800154static const struct inode_operations bad_inode_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800156 .create = bad_inode_create,
157 .lookup = bad_inode_lookup,
158 .link = bad_inode_link,
159 .unlink = bad_inode_unlink,
160 .symlink = bad_inode_symlink,
161 .mkdir = bad_inode_mkdir,
162 .rmdir = bad_inode_rmdir,
163 .mknod = bad_inode_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200164 .rename = bad_inode_rename2,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800165 .readlink = bad_inode_readlink,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800166 .permission = bad_inode_permission,
167 .getattr = bad_inode_getattr,
168 .setattr = bad_inode_setattr,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800169 .listxattr = bad_inode_listxattr,
Miklos Szeredi3f9ca752016-12-09 11:57:43 +0100170 .get_link = bad_inode_get_link,
171 .get_acl = bad_inode_get_acl,
172 .fiemap = bad_inode_fiemap,
173 .update_time = bad_inode_update_time,
174 .atomic_open = bad_inode_atomic_open,
175 .tmpfile = bad_inode_tmpfile,
176 .set_acl = bad_inode_set_acl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179
180/*
181 * When a filesystem is unable to read an inode due to an I/O error in
182 * its read_inode() function, it can call make_bad_inode() to return a
183 * set of stubs which will return EIO errors as required.
184 *
185 * We only need to do limited initialisation: all other fields are
186 * preinitialised to zero automatically.
187 */
188
189/**
190 * make_bad_inode - mark an inode bad due to an I/O error
191 * @inode: Inode to mark bad
192 *
193 * When an inode cannot be read due to a media or remote network
194 * failure this function makes the inode "bad" and causes I/O operations
195 * on it to fail from this point on.
196 */
197
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800198void make_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 remove_inode_hash(inode);
201
202 inode->i_mode = S_IFREG;
203 inode->i_atime = inode->i_mtime = inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700204 current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 inode->i_op = &bad_inode_ops;
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +0200206 inode->i_opflags &= ~IOP_XATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 inode->i_fop = &bad_file_ops;
208}
209EXPORT_SYMBOL(make_bad_inode);
210
211/*
212 * This tests whether an inode has been flagged as bad. The test uses
213 * &bad_inode_ops to cover the case of invalidated inodes as well as
214 * those created by make_bad_inode() above.
215 */
216
217/**
218 * is_bad_inode - is an inode errored
219 * @inode: inode to test
220 *
221 * Returns true if the inode in question has been marked as bad.
222 */
223
Yaowei Bai0e3ef1f2015-11-19 21:00:11 +0800224bool is_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 return (inode->i_op == &bad_inode_ops);
227}
228
229EXPORT_SYMBOL(is_bad_inode);
David Howellsb46980f2008-02-07 00:15:27 -0800230
231/**
232 * iget_failed - Mark an under-construction inode as dead and release it
233 * @inode: The inode to discard
234 *
235 * Mark an under-construction inode as dead and release it.
236 */
237void iget_failed(struct inode *inode)
238{
239 make_bad_inode(inode);
240 unlock_new_inode(inode);
241 iput(inode);
242}
243EXPORT_SYMBOL(iget_failed);