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