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