1 /*
2  *  linux/fs/bad_inode.c
3  *
4  *  Copyright (C) 1997, Stephen Tweedie
5  *
6  *  Provide stub functions for unreadable inodes
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/stat.h>
11 #include <linux/sched.h>
12 #include <linux/poll.h>
13 
bad_file_llseek(struct file * file,loff_t offset,int origin)14 static loff_t bad_file_llseek(struct file *file, loff_t offset, int origin)
15 {
16 	return -EIO;
17 }
18 
bad_file_read(struct file * filp,char __user * buf,size_t size,loff_t * ppos)19 static ssize_t bad_file_read(struct file *filp, char __user *buf,
20 			size_t size, loff_t *ppos)
21 {
22         return -EIO;
23 }
24 
bad_file_write(struct file * filp,const char __user * buf,size_t siz,loff_t * ppos)25 static ssize_t bad_file_write(struct file *filp, const char __user *buf,
26 			size_t siz, loff_t *ppos)
27 {
28         return -EIO;
29 }
30 
bad_file_readdir(struct file * filp,void * dirent,filldir_t filldir)31 static int bad_file_readdir(struct file *filp, void *dirent, filldir_t filldir)
32 {
33 	return -EIO;
34 }
35 
bad_file_poll(struct file * filp,poll_table * wait)36 static unsigned int bad_file_poll(struct file *filp, poll_table *wait)
37 {
38 	return POLLERR;
39 }
40 
bad_file_ioctl(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)41 static int bad_file_ioctl (struct inode *inode, struct file *filp,
42 			unsigned int cmd, unsigned long arg)
43 {
44 	return -EIO;
45 }
46 
bad_file_mmap(struct file * file,struct vm_area_struct * vma)47 static int bad_file_mmap(struct file *file, struct vm_area_struct *vma)
48 {
49 	return -EIO;
50 }
51 
bad_file_open(struct inode * inode,struct file * filp)52 static int bad_file_open(struct inode *inode, struct file *filp)
53 {
54 	return -EIO;
55 }
56 
bad_file_flush(struct file * file)57 static int bad_file_flush(struct file *file)
58 {
59 	return -EIO;
60 }
61 
bad_file_release(struct inode * inode,struct file * filp)62 static int bad_file_release(struct inode *inode, struct file *filp)
63 {
64 	return -EIO;
65 }
66 
bad_file_fsync(struct file * file,struct dentry * dentry,int datasync)67 static int bad_file_fsync(struct file *file, struct dentry *dentry,
68 			int datasync)
69 {
70 	return -EIO;
71 }
72 
bad_file_fasync(int fd,struct file * filp,int on)73 static int bad_file_fasync(int fd, struct file *filp, int on)
74 {
75 	return -EIO;
76 }
77 
bad_file_lock(struct file * file,int cmd,struct file_lock * fl)78 static int bad_file_lock(struct file *file, int cmd, struct file_lock *fl)
79 {
80 	return -EIO;
81 }
82 
83 /*
84  * The follow_link operation is special: it must behave as a no-op
85  * so that a bad root inode can at least be unmounted. To do this
86  * we must dput() the base and return the dentry with a dget().
87  */
bad_follow_link(struct dentry * dent,struct nameidata * nd)88 static int bad_follow_link(struct dentry *dent, struct nameidata *nd)
89 {
90 	return vfs_follow_link(nd, ERR_PTR(-EIO));
91 }
92 
93 static struct file_operations bad_file_ops =
94 {
95 	llseek:		bad_file_llseek,
96 	read:		bad_file_read,
97 	write:		bad_file_write,
98 	readdir:	bad_file_readdir,
99 	poll:		bad_file_poll,
100 	ioctl:		bad_file_ioctl,
101 	mmap:		bad_file_mmap,
102 	open:		bad_file_open,
103 	flush:		bad_file_flush,
104 	release:	bad_file_release,
105 	fsync:		bad_file_fsync,
106 	fasync:		bad_file_fasync,
107 	lock:		bad_file_lock,
108 };
109 
bad_inode_create(struct inode * dir,struct dentry * dentry,int mode)110 static int bad_inode_create (struct inode *dir, struct dentry *dentry,
111 		int mode)
112 {
113 	return -EIO;
114 }
115 
bad_inode_lookup(struct inode * dir,struct dentry * dentry)116 static struct dentry *bad_inode_lookup(struct inode *dir,
117 			struct dentry *dentry)
118 {
119 	return ERR_PTR(-EIO);
120 }
121 
bad_inode_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)122 static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
123 		struct dentry *dentry)
124 {
125 	return -EIO;
126 }
127 
bad_inode_unlink(struct inode * dir,struct dentry * dentry)128 static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
129 {
130 	return -EIO;
131 }
132 
bad_inode_symlink(struct inode * dir,struct dentry * dentry,const char * symname)133 static int bad_inode_symlink (struct inode *dir, struct dentry *dentry,
134 		const char *symname)
135 {
136 	return -EIO;
137 }
138 
bad_inode_mkdir(struct inode * dir,struct dentry * dentry,int mode)139 static int bad_inode_mkdir(struct inode *dir, struct dentry *dentry,
140 			int mode)
141 {
142 	return -EIO;
143 }
144 
bad_inode_rmdir(struct inode * dir,struct dentry * dentry)145 static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
146 {
147 	return -EIO;
148 }
149 
bad_inode_mknod(struct inode * dir,struct dentry * dentry,int mode,int rdev)150 static int bad_inode_mknod (struct inode *dir, struct dentry *dentry,
151 			int mode, int rdev)
152 {
153 	return -EIO;
154 }
155 
bad_inode_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)156 static int bad_inode_rename (struct inode *old_dir, struct dentry *old_dentry,
157 		struct inode *new_dir, struct dentry *new_dentry)
158 {
159 	return -EIO;
160 }
161 
bad_inode_readlink(struct dentry * dentry,char __user * buffer,int buflen)162 static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
163 		int buflen)
164 {
165 	return -EIO;
166 }
167 
bad_inode_permission(struct inode * inode,int mask)168 static int bad_inode_permission(struct inode *inode, int mask)
169 {
170 	return -EIO;
171 }
172 
bad_inode_revalidate(struct dentry * dentry)173 static int bad_inode_revalidate(struct dentry *dentry)
174 {
175 	return -EIO;
176 }
177 
178 struct inode_operations bad_inode_ops =
179 {
180 	create:		bad_inode_create,
181 	lookup:		bad_inode_lookup,
182 	link:		bad_inode_link,
183 	unlink:		bad_inode_unlink,
184 	symlink:	bad_inode_symlink,
185 	mkdir:		bad_inode_mkdir,
186 	rmdir:		bad_inode_rmdir,
187 	mknod:		bad_inode_mknod,
188 	rename:		bad_inode_rename,
189 	readlink:	bad_inode_readlink,
190 	follow_link:	bad_follow_link,
191 	/* truncate returns void */
192 	permission:	bad_inode_permission,
193 	revalidate:	bad_inode_revalidate,
194 };
195 
196 
197 /*
198  * When a filesystem is unable to read an inode due to an I/O error in
199  * its read_inode() function, it can call make_bad_inode() to return a
200  * set of stubs which will return EIO errors as required.
201  *
202  * We only need to do limited initialisation: all other fields are
203  * preinitialised to zero automatically.
204  */
205 
206 /**
207  *	make_bad_inode - mark an inode bad due to an I/O error
208  *	@inode: Inode to mark bad
209  *
210  *	When an inode cannot be read due to a media or remote network
211  *	failure this function makes the inode "bad" and causes I/O operations
212  *	on it to fail from this point on.
213  */
214 
make_bad_inode(struct inode * inode)215 void make_bad_inode(struct inode * inode)
216 {
217 	inode->i_mode = S_IFREG;
218 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
219 	inode->i_op = &bad_inode_ops;
220 	inode->i_fop = &bad_file_ops;
221 }
222 
223 /*
224  * This tests whether an inode has been flagged as bad. The test uses
225  * &bad_inode_ops to cover the case of invalidated inodes as well as
226  * those created by make_bad_inode() above.
227  */
228 
229 /**
230  *	is_bad_inode - is an inode errored
231  *	@inode: inode to test
232  *
233  *	Returns true if the inode in question has been marked as bad.
234  */
235 
is_bad_inode(struct inode * inode)236 int is_bad_inode(struct inode * inode)
237 {
238 	return (inode->i_op == &bad_inode_ops);
239 }
240