Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 12 | #include <linux/fs.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 13 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/stat.h> |
| 15 | #include <linux/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/namei.h> |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 17 | #include <linux/poll.h> |
Christoph Hellwig | 10c5db2 | 2020-05-23 09:30:11 +0200 | [diff] [blame] | 18 | #include <linux/fiemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 20 | static int bad_file_open(struct inode *inode, struct file *filp) |
| 21 | { |
| 22 | return -EIO; |
| 23 | } |
| 24 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 25 | static const struct file_operations bad_file_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 27 | .open = bad_file_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 30 | static int bad_inode_create(struct user_namespace *mnt_userns, |
| 31 | struct inode *dir, struct dentry *dentry, |
| 32 | umode_t mode, bool excl) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 33 | { |
| 34 | return -EIO; |
| 35 | } |
| 36 | |
| 37 | static struct dentry *bad_inode_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 38 | struct dentry *dentry, unsigned int flags) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 39 | { |
| 40 | return ERR_PTR(-EIO); |
| 41 | } |
| 42 | |
| 43 | static int bad_inode_link (struct dentry *old_dentry, struct inode *dir, |
| 44 | struct dentry *dentry) |
| 45 | { |
| 46 | return -EIO; |
| 47 | } |
| 48 | |
| 49 | static int bad_inode_unlink(struct inode *dir, struct dentry *dentry) |
| 50 | { |
| 51 | return -EIO; |
| 52 | } |
| 53 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 54 | static int bad_inode_symlink(struct user_namespace *mnt_userns, |
| 55 | struct inode *dir, struct dentry *dentry, |
| 56 | const char *symname) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 57 | { |
| 58 | return -EIO; |
| 59 | } |
| 60 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 61 | static int bad_inode_mkdir(struct user_namespace *mnt_userns, struct inode *dir, |
| 62 | struct dentry *dentry, umode_t mode) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 63 | { |
| 64 | return -EIO; |
| 65 | } |
| 66 | |
| 67 | static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry) |
| 68 | { |
| 69 | return -EIO; |
| 70 | } |
| 71 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 72 | static int bad_inode_mknod(struct user_namespace *mnt_userns, struct inode *dir, |
| 73 | struct dentry *dentry, umode_t mode, dev_t rdev) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 74 | { |
| 75 | return -EIO; |
| 76 | } |
| 77 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 78 | static int bad_inode_rename2(struct user_namespace *mnt_userns, |
| 79 | struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | a0dbc56 | 2014-07-23 15:15:31 +0200 | [diff] [blame] | 80 | struct inode *new_dir, struct dentry *new_dentry, |
| 81 | unsigned int flags) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 82 | { |
| 83 | return -EIO; |
| 84 | } |
| 85 | |
| 86 | static int bad_inode_readlink(struct dentry *dentry, char __user *buffer, |
| 87 | int buflen) |
| 88 | { |
| 89 | return -EIO; |
| 90 | } |
| 91 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 92 | static int bad_inode_permission(struct user_namespace *mnt_userns, |
| 93 | struct inode *inode, int mask) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 94 | { |
| 95 | return -EIO; |
| 96 | } |
| 97 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 98 | static int bad_inode_getattr(struct user_namespace *mnt_userns, |
| 99 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 100 | u32 request_mask, unsigned int query_flags) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 101 | { |
| 102 | return -EIO; |
| 103 | } |
| 104 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 105 | static int bad_inode_setattr(struct user_namespace *mnt_userns, |
| 106 | struct dentry *direntry, struct iattr *attrs) |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 107 | { |
| 108 | return -EIO; |
| 109 | } |
| 110 | |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 111 | static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer, |
| 112 | size_t buffer_size) |
| 113 | { |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 117 | static const char *bad_inode_get_link(struct dentry *dentry, |
| 118 | struct inode *inode, |
| 119 | struct delayed_call *done) |
| 120 | { |
| 121 | return ERR_PTR(-EIO); |
| 122 | } |
| 123 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 124 | static struct posix_acl *bad_inode_get_acl(struct inode *inode, int type, bool rcu) |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 125 | { |
| 126 | return ERR_PTR(-EIO); |
| 127 | } |
| 128 | |
| 129 | static int bad_inode_fiemap(struct inode *inode, |
| 130 | struct fiemap_extent_info *fieinfo, u64 start, |
| 131 | u64 len) |
| 132 | { |
| 133 | return -EIO; |
| 134 | } |
| 135 | |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 136 | static int bad_inode_update_time(struct inode *inode, struct timespec64 *time, |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 137 | int flags) |
| 138 | { |
| 139 | return -EIO; |
| 140 | } |
| 141 | |
| 142 | static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry, |
| 143 | struct file *file, unsigned int open_flag, |
Al Viro | 44907d7 | 2018-06-08 13:32:02 -0400 | [diff] [blame] | 144 | umode_t create_mode) |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 145 | { |
| 146 | return -EIO; |
| 147 | } |
| 148 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 149 | static int bad_inode_tmpfile(struct user_namespace *mnt_userns, |
| 150 | struct inode *inode, struct dentry *dentry, |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 151 | umode_t mode) |
| 152 | { |
| 153 | return -EIO; |
| 154 | } |
| 155 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 156 | static int bad_inode_set_acl(struct user_namespace *mnt_userns, |
| 157 | struct inode *inode, struct posix_acl *acl, |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 158 | int type) |
| 159 | { |
| 160 | return -EIO; |
| 161 | } |
| 162 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 163 | static const struct inode_operations bad_inode_ops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 165 | .create = bad_inode_create, |
| 166 | .lookup = bad_inode_lookup, |
| 167 | .link = bad_inode_link, |
| 168 | .unlink = bad_inode_unlink, |
| 169 | .symlink = bad_inode_symlink, |
| 170 | .mkdir = bad_inode_mkdir, |
| 171 | .rmdir = bad_inode_rmdir, |
| 172 | .mknod = bad_inode_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 173 | .rename = bad_inode_rename2, |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 174 | .readlink = bad_inode_readlink, |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 175 | .permission = bad_inode_permission, |
| 176 | .getattr = bad_inode_getattr, |
| 177 | .setattr = bad_inode_setattr, |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 178 | .listxattr = bad_inode_listxattr, |
Miklos Szeredi | 3f9ca75 | 2016-12-09 11:57:43 +0100 | [diff] [blame] | 179 | .get_link = bad_inode_get_link, |
| 180 | .get_acl = bad_inode_get_acl, |
| 181 | .fiemap = bad_inode_fiemap, |
| 182 | .update_time = bad_inode_update_time, |
| 183 | .atomic_open = bad_inode_atomic_open, |
| 184 | .tmpfile = bad_inode_tmpfile, |
| 185 | .set_acl = bad_inode_set_acl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | |
| 189 | /* |
| 190 | * When a filesystem is unable to read an inode due to an I/O error in |
| 191 | * its read_inode() function, it can call make_bad_inode() to return a |
| 192 | * set of stubs which will return EIO errors as required. |
| 193 | * |
| 194 | * We only need to do limited initialisation: all other fields are |
| 195 | * preinitialised to zero automatically. |
| 196 | */ |
| 197 | |
| 198 | /** |
| 199 | * make_bad_inode - mark an inode bad due to an I/O error |
| 200 | * @inode: Inode to mark bad |
| 201 | * |
| 202 | * When an inode cannot be read due to a media or remote network |
| 203 | * failure this function makes the inode "bad" and causes I/O operations |
| 204 | * on it to fail from this point on. |
| 205 | */ |
| 206 | |
Eric Sandeen | be6aab0 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 207 | void make_bad_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | remove_inode_hash(inode); |
| 210 | |
| 211 | inode->i_mode = S_IFREG; |
| 212 | inode->i_atime = inode->i_mtime = inode->i_ctime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 213 | current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | inode->i_op = &bad_inode_ops; |
Andreas Gruenbacher | 5f6e59a | 2016-09-29 17:48:40 +0200 | [diff] [blame] | 215 | inode->i_opflags &= ~IOP_XATTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | inode->i_fop = &bad_file_ops; |
| 217 | } |
| 218 | EXPORT_SYMBOL(make_bad_inode); |
| 219 | |
| 220 | /* |
| 221 | * This tests whether an inode has been flagged as bad. The test uses |
| 222 | * &bad_inode_ops to cover the case of invalidated inodes as well as |
| 223 | * those created by make_bad_inode() above. |
| 224 | */ |
| 225 | |
| 226 | /** |
| 227 | * is_bad_inode - is an inode errored |
| 228 | * @inode: inode to test |
| 229 | * |
| 230 | * Returns true if the inode in question has been marked as bad. |
| 231 | */ |
| 232 | |
Yaowei Bai | 0e3ef1f | 2015-11-19 21:00:11 +0800 | [diff] [blame] | 233 | bool is_bad_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | return (inode->i_op == &bad_inode_ops); |
| 236 | } |
| 237 | |
| 238 | EXPORT_SYMBOL(is_bad_inode); |
David Howells | b46980f | 2008-02-07 00:15:27 -0800 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * iget_failed - Mark an under-construction inode as dead and release it |
| 242 | * @inode: The inode to discard |
| 243 | * |
| 244 | * Mark an under-construction inode as dead and release it. |
| 245 | */ |
| 246 | void iget_failed(struct inode *inode) |
| 247 | { |
| 248 | make_bad_inode(inode); |
| 249 | unlock_new_inode(inode); |
| 250 | iput(inode); |
| 251 | } |
| 252 | EXPORT_SYMBOL(iget_failed); |