1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1997, Stephen Tweedie
7 * Provide stub functions for unreadable inodes
9 * Fabian Frederick : August 2003 - All file operations assigned to EIO
13 #include <linux/export.h>
14 #include <linux/stat.h>
15 #include <linux/time.h>
16 #include <linux/namei.h>
17 #include <linux/poll.h>
18 #include <linux/fiemap.h>
20 static int bad_file_open(struct inode
*inode
, struct file
*filp
)
25 static const struct file_operations bad_file_ops
=
27 .open
= bad_file_open
,
30 static int bad_inode_create(struct mnt_idmap
*idmap
,
31 struct inode
*dir
, struct dentry
*dentry
,
32 umode_t mode
, bool excl
)
37 static struct dentry
*bad_inode_lookup(struct inode
*dir
,
38 struct dentry
*dentry
, unsigned int flags
)
43 static int bad_inode_link (struct dentry
*old_dentry
, struct inode
*dir
,
44 struct dentry
*dentry
)
49 static int bad_inode_unlink(struct inode
*dir
, struct dentry
*dentry
)
54 static int bad_inode_symlink(struct mnt_idmap
*idmap
,
55 struct inode
*dir
, struct dentry
*dentry
,
61 static int bad_inode_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
62 struct dentry
*dentry
, umode_t mode
)
67 static int bad_inode_rmdir (struct inode
*dir
, struct dentry
*dentry
)
72 static int bad_inode_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
73 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
78 static int bad_inode_rename2(struct mnt_idmap
*idmap
,
79 struct inode
*old_dir
, struct dentry
*old_dentry
,
80 struct inode
*new_dir
, struct dentry
*new_dentry
,
86 static int bad_inode_readlink(struct dentry
*dentry
, char __user
*buffer
,
92 static int bad_inode_permission(struct mnt_idmap
*idmap
,
93 struct inode
*inode
, int mask
)
98 static int bad_inode_getattr(struct mnt_idmap
*idmap
,
99 const struct path
*path
, struct kstat
*stat
,
100 u32 request_mask
, unsigned int query_flags
)
105 static int bad_inode_setattr(struct mnt_idmap
*idmap
,
106 struct dentry
*direntry
, struct iattr
*attrs
)
111 static ssize_t
bad_inode_listxattr(struct dentry
*dentry
, char *buffer
,
117 static const char *bad_inode_get_link(struct dentry
*dentry
,
119 struct delayed_call
*done
)
121 return ERR_PTR(-EIO
);
124 static struct posix_acl
*bad_inode_get_acl(struct inode
*inode
, int type
, bool rcu
)
126 return ERR_PTR(-EIO
);
129 static int bad_inode_fiemap(struct inode
*inode
,
130 struct fiemap_extent_info
*fieinfo
, u64 start
,
136 static int bad_inode_update_time(struct inode
*inode
, int flags
)
141 static int bad_inode_atomic_open(struct inode
*inode
, struct dentry
*dentry
,
142 struct file
*file
, unsigned int open_flag
,
148 static int bad_inode_tmpfile(struct mnt_idmap
*idmap
,
149 struct inode
*inode
, struct file
*file
,
155 static int bad_inode_set_acl(struct mnt_idmap
*idmap
,
156 struct dentry
*dentry
, struct posix_acl
*acl
,
162 static const struct inode_operations bad_inode_ops
=
164 .create
= bad_inode_create
,
165 .lookup
= bad_inode_lookup
,
166 .link
= bad_inode_link
,
167 .unlink
= bad_inode_unlink
,
168 .symlink
= bad_inode_symlink
,
169 .mkdir
= bad_inode_mkdir
,
170 .rmdir
= bad_inode_rmdir
,
171 .mknod
= bad_inode_mknod
,
172 .rename
= bad_inode_rename2
,
173 .readlink
= bad_inode_readlink
,
174 .permission
= bad_inode_permission
,
175 .getattr
= bad_inode_getattr
,
176 .setattr
= bad_inode_setattr
,
177 .listxattr
= bad_inode_listxattr
,
178 .get_link
= bad_inode_get_link
,
179 .get_inode_acl
= bad_inode_get_acl
,
180 .fiemap
= bad_inode_fiemap
,
181 .update_time
= bad_inode_update_time
,
182 .atomic_open
= bad_inode_atomic_open
,
183 .tmpfile
= bad_inode_tmpfile
,
184 .set_acl
= bad_inode_set_acl
,
189 * When a filesystem is unable to read an inode due to an I/O error in
190 * its read_inode() function, it can call make_bad_inode() to return a
191 * set of stubs which will return EIO errors as required.
193 * We only need to do limited initialisation: all other fields are
194 * preinitialised to zero automatically.
198 * make_bad_inode - mark an inode bad due to an I/O error
199 * @inode: Inode to mark bad
201 * When an inode cannot be read due to a media or remote network
202 * failure this function makes the inode "bad" and causes I/O operations
203 * on it to fail from this point on.
206 void make_bad_inode(struct inode
*inode
)
208 remove_inode_hash(inode
);
210 inode
->i_mode
= S_IFREG
;
211 simple_inode_init_ts(inode
);
212 inode
->i_op
= &bad_inode_ops
;
213 inode
->i_opflags
&= ~IOP_XATTR
;
214 inode
->i_fop
= &bad_file_ops
;
216 EXPORT_SYMBOL(make_bad_inode
);
219 * This tests whether an inode has been flagged as bad. The test uses
220 * &bad_inode_ops to cover the case of invalidated inodes as well as
221 * those created by make_bad_inode() above.
225 * is_bad_inode - is an inode errored
226 * @inode: inode to test
228 * Returns true if the inode in question has been marked as bad.
231 bool is_bad_inode(struct inode
*inode
)
233 return (inode
->i_op
== &bad_inode_ops
);
236 EXPORT_SYMBOL(is_bad_inode
);
239 * iget_failed - Mark an under-construction inode as dead and release it
240 * @inode: The inode to discard
242 * Mark an under-construction inode as dead and release it.
244 void iget_failed(struct inode
*inode
)
246 make_bad_inode(inode
);
247 unlock_new_inode(inode
);
250 EXPORT_SYMBOL(iget_failed
);