4 * Copyright (C) 1997, Stephen Tweedie
6 * Provide stub functions for unreadable inodes
8 * Fabian Frederick : August 2003 - All file operations assigned to EIO
12 #include <linux/export.h>
13 #include <linux/stat.h>
14 #include <linux/time.h>
15 #include <linux/namei.h>
16 #include <linux/poll.h>
19 static loff_t bad_file_llseek(struct file *file, loff_t offset, int whence)
24 static ssize_t bad_file_read(struct file *filp, char __user *buf,
25 size_t size, loff_t *ppos)
30 static ssize_t bad_file_write(struct file *filp, const char __user *buf,
31 size_t siz, loff_t *ppos)
36 static ssize_t bad_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
37 unsigned long nr_segs, loff_t pos)
42 static ssize_t bad_file_read_iter(struct kiocb *iocb, struct iov_iter *iter,
48 static ssize_t bad_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
49 unsigned long nr_segs, loff_t pos)
54 static ssize_t bad_file_write_iter(struct kiocb *iocb, struct iov_iter *iter,
60 static int bad_file_readdir(struct file *file, struct dir_context *ctx)
65 static unsigned int bad_file_poll(struct file *filp, poll_table *wait)
70 static long bad_file_unlocked_ioctl(struct file *file, unsigned cmd,
76 static long bad_file_compat_ioctl(struct file *file, unsigned int cmd,
82 static int bad_file_mmap(struct file *file, struct vm_area_struct *vma)
87 static int bad_file_open(struct inode *inode, struct file *filp)
92 static int bad_file_flush(struct file *file, fl_owner_t id)
97 static int bad_file_release(struct inode *inode, struct file *filp)
102 static int bad_file_fsync(struct file *file, loff_t start, loff_t end,
108 static int bad_file_aio_fsync(struct kiocb *iocb, int datasync)
113 static int bad_file_fasync(int fd, struct file *filp, int on)
118 static int bad_file_lock(struct file *file, int cmd, struct file_lock *fl)
123 static ssize_t bad_file_sendpage(struct file *file, struct page *page,
124 int off, size_t len, loff_t *pos, int more)
129 static unsigned long bad_file_get_unmapped_area(struct file *file,
130 unsigned long addr, unsigned long len,
131 unsigned long pgoff, unsigned long flags)
136 static int bad_file_check_flags(int flags)
141 static int bad_file_flock(struct file *filp, int cmd, struct file_lock *fl)
146 static ssize_t bad_file_splice_write(struct pipe_inode_info *pipe,
147 struct file *out, loff_t *ppos, size_t len,
153 static ssize_t bad_file_splice_read(struct file *in, loff_t *ppos,
154 struct pipe_inode_info *pipe, size_t len,
160 static const struct file_operations bad_file_ops =
162 .llseek = bad_file_llseek,
163 .read = bad_file_read,
164 .write = bad_file_write,
165 .aio_read = bad_file_aio_read,
166 .read_iter = bad_file_read_iter,
167 .aio_write = bad_file_aio_write,
168 .write_iter = bad_file_write_iter,
169 .iterate = bad_file_readdir,
170 .poll = bad_file_poll,
171 .unlocked_ioctl = bad_file_unlocked_ioctl,
172 .compat_ioctl = bad_file_compat_ioctl,
173 .mmap = bad_file_mmap,
174 .open = bad_file_open,
175 .flush = bad_file_flush,
176 .release = bad_file_release,
177 .fsync = bad_file_fsync,
178 .aio_fsync = bad_file_aio_fsync,
179 .fasync = bad_file_fasync,
180 .lock = bad_file_lock,
181 .sendpage = bad_file_sendpage,
182 .get_unmapped_area = bad_file_get_unmapped_area,
183 .check_flags = bad_file_check_flags,
184 .flock = bad_file_flock,
185 .splice_write = bad_file_splice_write,
186 .splice_read = bad_file_splice_read,
189 static int bad_inode_create (struct inode *dir, struct dentry *dentry,
190 umode_t mode, bool excl)
195 static struct dentry *bad_inode_lookup(struct inode *dir,
196 struct dentry *dentry, unsigned int flags)
198 return ERR_PTR(-EIO);
201 static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
202 struct dentry *dentry)
207 static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
212 static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
218 static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
224 static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
229 static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
230 umode_t mode, dev_t rdev)
235 static int bad_inode_rename (struct inode *old_dir, struct dentry *old_dentry,
236 struct inode *new_dir, struct dentry *new_dentry)
241 static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
247 static int bad_inode_permission(struct inode *inode, int mask)
252 static int bad_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
258 static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
263 static int bad_inode_setxattr(struct dentry *dentry, const char *name,
264 const void *value, size_t size, int flags)
269 static ssize_t bad_inode_getxattr(struct dentry *dentry, const char *name,
270 void *buffer, size_t size)
275 static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
281 static int bad_inode_removexattr(struct dentry *dentry, const char *name)
286 static const struct inode_operations bad_inode_ops =
288 .create = bad_inode_create,
289 .lookup = bad_inode_lookup,
290 .link = bad_inode_link,
291 .unlink = bad_inode_unlink,
292 .symlink = bad_inode_symlink,
293 .mkdir = bad_inode_mkdir,
294 .rmdir = bad_inode_rmdir,
295 .mknod = bad_inode_mknod,
296 .rename = bad_inode_rename,
297 .readlink = bad_inode_readlink,
298 /* follow_link must be no-op, otherwise unmounting this inode
300 /* put_link returns void */
301 /* truncate returns void */
302 .permission = bad_inode_permission,
303 .getattr = bad_inode_getattr,
304 .setattr = bad_inode_setattr,
305 .setxattr = bad_inode_setxattr,
306 .getxattr = bad_inode_getxattr,
307 .listxattr = bad_inode_listxattr,
308 .removexattr = bad_inode_removexattr,
313 * When a filesystem is unable to read an inode due to an I/O error in
314 * its read_inode() function, it can call make_bad_inode() to return a
315 * set of stubs which will return EIO errors as required.
317 * We only need to do limited initialisation: all other fields are
318 * preinitialised to zero automatically.
322 * make_bad_inode - mark an inode bad due to an I/O error
323 * @inode: Inode to mark bad
325 * When an inode cannot be read due to a media or remote network
326 * failure this function makes the inode "bad" and causes I/O operations
327 * on it to fail from this point on.
330 void make_bad_inode(struct inode *inode)
332 remove_inode_hash(inode);
334 inode->i_mode = S_IFREG;
335 inode->i_atime = inode->i_mtime = inode->i_ctime =
336 current_fs_time(inode->i_sb);
337 inode->i_op = &bad_inode_ops;
338 inode->i_fop = &bad_file_ops;
340 EXPORT_SYMBOL(make_bad_inode);
343 * This tests whether an inode has been flagged as bad. The test uses
344 * &bad_inode_ops to cover the case of invalidated inodes as well as
345 * those created by make_bad_inode() above.
349 * is_bad_inode - is an inode errored
350 * @inode: inode to test
352 * Returns true if the inode in question has been marked as bad.
355 int is_bad_inode(struct inode *inode)
357 return (inode->i_op == &bad_inode_ops);
360 EXPORT_SYMBOL(is_bad_inode);
363 * iget_failed - Mark an under-construction inode as dead and release it
364 * @inode: The inode to discard
366 * Mark an under-construction inode as dead and release it.
368 void iget_failed(struct inode *inode)
370 make_bad_inode(inode);
371 unlock_new_inode(inode);
374 EXPORT_SYMBOL(iget_failed);