blob: 5f1538c03b1bbeb068c089146cc28fc502bb0d83 [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
96static int bad_file_fsync(struct file *file, struct dentry *dentry,
97 int datasync)
98{
99 return -EIO;
100}
101
102static int bad_file_aio_fsync(struct kiocb *iocb, int datasync)
103{
104 return -EIO;
105}
106
107static int bad_file_fasync(int fd, struct file *filp, int on)
108{
109 return -EIO;
110}
111
112static int bad_file_lock(struct file *file, int cmd, struct file_lock *fl)
113{
114 return -EIO;
115}
116
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800117static ssize_t bad_file_sendpage(struct file *file, struct page *page,
118 int off, size_t len, loff_t *pos, int more)
119{
120 return -EIO;
121}
122
123static unsigned long bad_file_get_unmapped_area(struct file *file,
124 unsigned long addr, unsigned long len,
125 unsigned long pgoff, unsigned long flags)
126{
127 return -EIO;
128}
129
130static int bad_file_check_flags(int flags)
131{
132 return -EIO;
133}
134
135static int bad_file_dir_notify(struct file *file, unsigned long arg)
136{
137 return -EIO;
138}
139
140static int bad_file_flock(struct file *filp, int cmd, struct file_lock *fl)
141{
142 return -EIO;
143}
144
145static ssize_t bad_file_splice_write(struct pipe_inode_info *pipe,
146 struct file *out, loff_t *ppos, size_t len,
147 unsigned int flags)
148{
149 return -EIO;
150}
151
152static ssize_t bad_file_splice_read(struct file *in, loff_t *ppos,
153 struct pipe_inode_info *pipe, size_t len,
154 unsigned int flags)
155{
156 return -EIO;
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800159static const struct file_operations bad_file_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800161 .llseek = bad_file_llseek,
162 .read = bad_file_read,
163 .write = bad_file_write,
164 .aio_read = bad_file_aio_read,
165 .aio_write = bad_file_aio_write,
166 .readdir = bad_file_readdir,
167 .poll = bad_file_poll,
168 .ioctl = bad_file_ioctl,
169 .unlocked_ioctl = bad_file_unlocked_ioctl,
170 .compat_ioctl = bad_file_compat_ioctl,
171 .mmap = bad_file_mmap,
172 .open = bad_file_open,
173 .flush = bad_file_flush,
174 .release = bad_file_release,
175 .fsync = bad_file_fsync,
176 .aio_fsync = bad_file_aio_fsync,
177 .fasync = bad_file_fasync,
178 .lock = bad_file_lock,
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800179 .sendpage = bad_file_sendpage,
180 .get_unmapped_area = bad_file_get_unmapped_area,
181 .check_flags = bad_file_check_flags,
182 .dir_notify = bad_file_dir_notify,
183 .flock = bad_file_flock,
184 .splice_write = bad_file_splice_write,
185 .splice_read = bad_file_splice_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800188static int bad_inode_create (struct inode *dir, struct dentry *dentry,
189 int mode, struct nameidata *nd)
190{
191 return -EIO;
192}
193
194static struct dentry *bad_inode_lookup(struct inode *dir,
195 struct dentry *dentry, struct nameidata *nd)
196{
197 return ERR_PTR(-EIO);
198}
199
200static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
201 struct dentry *dentry)
202{
203 return -EIO;
204}
205
206static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
207{
208 return -EIO;
209}
210
211static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
212 const char *symname)
213{
214 return -EIO;
215}
216
217static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
218 int mode)
219{
220 return -EIO;
221}
222
223static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
224{
225 return -EIO;
226}
227
228static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
229 int mode, dev_t rdev)
230{
231 return -EIO;
232}
233
234static int bad_inode_rename (struct inode *old_dir, struct dentry *old_dentry,
235 struct inode *new_dir, struct dentry *new_dentry)
236{
237 return -EIO;
238}
239
240static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
241 int buflen)
242{
243 return -EIO;
244}
245
Al Viroe6305c42008-07-15 21:03:57 -0400246static int bad_inode_permission(struct inode *inode, int mask)
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800247{
248 return -EIO;
249}
250
251static int bad_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
252 struct kstat *stat)
253{
254 return -EIO;
255}
256
257static int bad_inode_setattr(struct dentry *direntry, struct iattr *attrs)
258{
259 return -EIO;
260}
261
262static int bad_inode_setxattr(struct dentry *dentry, const char *name,
263 const void *value, size_t size, int flags)
264{
265 return -EIO;
266}
267
268static ssize_t bad_inode_getxattr(struct dentry *dentry, const char *name,
269 void *buffer, size_t size)
270{
271 return -EIO;
272}
273
274static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
275 size_t buffer_size)
276{
277 return -EIO;
278}
279
280static int bad_inode_removexattr(struct dentry *dentry, const char *name)
281{
282 return -EIO;
283}
284
Arjan van de Ven754661f2007-02-12 00:55:38 -0800285static const struct inode_operations bad_inode_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800287 .create = bad_inode_create,
288 .lookup = bad_inode_lookup,
289 .link = bad_inode_link,
290 .unlink = bad_inode_unlink,
291 .symlink = bad_inode_symlink,
292 .mkdir = bad_inode_mkdir,
293 .rmdir = bad_inode_rmdir,
294 .mknod = bad_inode_mknod,
295 .rename = bad_inode_rename,
296 .readlink = bad_inode_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* follow_link must be no-op, otherwise unmounting this inode
298 won't work */
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800299 /* put_link returns void */
300 /* truncate returns void */
301 .permission = bad_inode_permission,
302 .getattr = bad_inode_getattr,
303 .setattr = bad_inode_setattr,
304 .setxattr = bad_inode_setxattr,
305 .getxattr = bad_inode_getxattr,
306 .listxattr = bad_inode_listxattr,
307 .removexattr = bad_inode_removexattr,
308 /* truncate_range returns void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311
312/*
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.
316 *
317 * We only need to do limited initialisation: all other fields are
318 * preinitialised to zero automatically.
319 */
320
321/**
322 * make_bad_inode - mark an inode bad due to an I/O error
323 * @inode: Inode to mark bad
324 *
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.
328 */
329
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800330void make_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 remove_inode_hash(inode);
333
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;
339}
340EXPORT_SYMBOL(make_bad_inode);
341
342/*
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.
346 */
347
348/**
349 * is_bad_inode - is an inode errored
350 * @inode: inode to test
351 *
352 * Returns true if the inode in question has been marked as bad.
353 */
354
Eric Sandeenbe6aab02007-01-05 16:36:36 -0800355int is_bad_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 return (inode->i_op == &bad_inode_ops);
358}
359
360EXPORT_SYMBOL(is_bad_inode);
David Howellsb46980f2008-02-07 00:15:27 -0800361
362/**
363 * iget_failed - Mark an under-construction inode as dead and release it
364 * @inode: The inode to discard
365 *
366 * Mark an under-construction inode as dead and release it.
367 */
368void iget_failed(struct inode *inode)
369{
370 make_bad_inode(inode);
371 unlock_new_inode(inode);
372 iput(inode);
373}
374EXPORT_SYMBOL(iget_failed);