Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/bad_inode.c
0004  *
0005  *  Copyright (C) 1997, Stephen Tweedie
0006  *
0007  *  Provide stub functions for unreadable inodes
0008  *
0009  *  Fabian Frederick : August 2003 - All file operations assigned to EIO
0010  */
0011 
0012 #include <linux/fs.h>
0013 #include <linux/export.h>
0014 #include <linux/stat.h>
0015 #include <linux/time.h>
0016 #include <linux/namei.h>
0017 #include <linux/poll.h>
0018 #include <linux/fiemap.h>
0019 
0020 static int bad_file_open(struct inode *inode, struct file *filp)
0021 {
0022     return -EIO;
0023 }
0024 
0025 static const struct file_operations bad_file_ops =
0026 {
0027     .open       = bad_file_open,
0028 };
0029 
0030 static int bad_inode_create(struct user_namespace *mnt_userns,
0031                 struct inode *dir, struct dentry *dentry,
0032                 umode_t mode, bool excl)
0033 {
0034     return -EIO;
0035 }
0036 
0037 static struct dentry *bad_inode_lookup(struct inode *dir,
0038             struct dentry *dentry, unsigned int flags)
0039 {
0040     return ERR_PTR(-EIO);
0041 }
0042 
0043 static int bad_inode_link (struct dentry *old_dentry, struct inode *dir,
0044         struct dentry *dentry)
0045 {
0046     return -EIO;
0047 }
0048 
0049 static int bad_inode_unlink(struct inode *dir, struct dentry *dentry)
0050 {
0051     return -EIO;
0052 }
0053 
0054 static int bad_inode_symlink(struct user_namespace *mnt_userns,
0055                  struct inode *dir, struct dentry *dentry,
0056                  const char *symname)
0057 {
0058     return -EIO;
0059 }
0060 
0061 static int bad_inode_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
0062                struct dentry *dentry, umode_t mode)
0063 {
0064     return -EIO;
0065 }
0066 
0067 static int bad_inode_rmdir (struct inode *dir, struct dentry *dentry)
0068 {
0069     return -EIO;
0070 }
0071 
0072 static int bad_inode_mknod(struct user_namespace *mnt_userns, struct inode *dir,
0073                struct dentry *dentry, umode_t mode, dev_t rdev)
0074 {
0075     return -EIO;
0076 }
0077 
0078 static int bad_inode_rename2(struct user_namespace *mnt_userns,
0079                  struct inode *old_dir, struct dentry *old_dentry,
0080                  struct inode *new_dir, struct dentry *new_dentry,
0081                  unsigned int flags)
0082 {
0083     return -EIO;
0084 }
0085 
0086 static int bad_inode_readlink(struct dentry *dentry, char __user *buffer,
0087         int buflen)
0088 {
0089     return -EIO;
0090 }
0091 
0092 static int bad_inode_permission(struct user_namespace *mnt_userns,
0093                 struct inode *inode, int mask)
0094 {
0095     return -EIO;
0096 }
0097 
0098 static int bad_inode_getattr(struct user_namespace *mnt_userns,
0099                  const struct path *path, struct kstat *stat,
0100                  u32 request_mask, unsigned int query_flags)
0101 {
0102     return -EIO;
0103 }
0104 
0105 static int bad_inode_setattr(struct user_namespace *mnt_userns,
0106                  struct dentry *direntry, struct iattr *attrs)
0107 {
0108     return -EIO;
0109 }
0110 
0111 static ssize_t bad_inode_listxattr(struct dentry *dentry, char *buffer,
0112             size_t buffer_size)
0113 {
0114     return -EIO;
0115 }
0116 
0117 static const char *bad_inode_get_link(struct dentry *dentry,
0118                       struct inode *inode,
0119                       struct delayed_call *done)
0120 {
0121     return ERR_PTR(-EIO);
0122 }
0123 
0124 static struct posix_acl *bad_inode_get_acl(struct inode *inode, int type, bool rcu)
0125 {
0126     return ERR_PTR(-EIO);
0127 }
0128 
0129 static int bad_inode_fiemap(struct inode *inode,
0130                 struct fiemap_extent_info *fieinfo, u64 start,
0131                 u64 len)
0132 {
0133     return -EIO;
0134 }
0135 
0136 static int bad_inode_update_time(struct inode *inode, struct timespec64 *time,
0137                  int flags)
0138 {
0139     return -EIO;
0140 }
0141 
0142 static int bad_inode_atomic_open(struct inode *inode, struct dentry *dentry,
0143                  struct file *file, unsigned int open_flag,
0144                  umode_t create_mode)
0145 {
0146     return -EIO;
0147 }
0148 
0149 static int bad_inode_tmpfile(struct user_namespace *mnt_userns,
0150                  struct inode *inode, struct dentry *dentry,
0151                  umode_t mode)
0152 {
0153     return -EIO;
0154 }
0155 
0156 static int bad_inode_set_acl(struct user_namespace *mnt_userns,
0157                  struct inode *inode, struct posix_acl *acl,
0158                  int type)
0159 {
0160     return -EIO;
0161 }
0162 
0163 static const struct inode_operations bad_inode_ops =
0164 {
0165     .create     = bad_inode_create,
0166     .lookup     = bad_inode_lookup,
0167     .link       = bad_inode_link,
0168     .unlink     = bad_inode_unlink,
0169     .symlink    = bad_inode_symlink,
0170     .mkdir      = bad_inode_mkdir,
0171     .rmdir      = bad_inode_rmdir,
0172     .mknod      = bad_inode_mknod,
0173     .rename     = bad_inode_rename2,
0174     .readlink   = bad_inode_readlink,
0175     .permission = bad_inode_permission,
0176     .getattr    = bad_inode_getattr,
0177     .setattr    = bad_inode_setattr,
0178     .listxattr  = bad_inode_listxattr,
0179     .get_link   = bad_inode_get_link,
0180     .get_acl    = bad_inode_get_acl,
0181     .fiemap     = bad_inode_fiemap,
0182     .update_time    = bad_inode_update_time,
0183     .atomic_open    = bad_inode_atomic_open,
0184     .tmpfile    = bad_inode_tmpfile,
0185     .set_acl    = bad_inode_set_acl,
0186 };
0187 
0188 
0189 /*
0190  * When a filesystem is unable to read an inode due to an I/O error in
0191  * its read_inode() function, it can call make_bad_inode() to return a
0192  * set of stubs which will return EIO errors as required. 
0193  *
0194  * We only need to do limited initialisation: all other fields are
0195  * preinitialised to zero automatically.
0196  */
0197  
0198 /**
0199  *  make_bad_inode - mark an inode bad due to an I/O error
0200  *  @inode: Inode to mark bad
0201  *
0202  *  When an inode cannot be read due to a media or remote network
0203  *  failure this function makes the inode "bad" and causes I/O operations
0204  *  on it to fail from this point on.
0205  */
0206  
0207 void make_bad_inode(struct inode *inode)
0208 {
0209     remove_inode_hash(inode);
0210 
0211     inode->i_mode = S_IFREG;
0212     inode->i_atime = inode->i_mtime = inode->i_ctime =
0213         current_time(inode);
0214     inode->i_op = &bad_inode_ops;   
0215     inode->i_opflags &= ~IOP_XATTR;
0216     inode->i_fop = &bad_file_ops;   
0217 }
0218 EXPORT_SYMBOL(make_bad_inode);
0219 
0220 /*
0221  * This tests whether an inode has been flagged as bad. The test uses
0222  * &bad_inode_ops to cover the case of invalidated inodes as well as
0223  * those created by make_bad_inode() above.
0224  */
0225  
0226 /**
0227  *  is_bad_inode - is an inode errored
0228  *  @inode: inode to test
0229  *
0230  *  Returns true if the inode in question has been marked as bad.
0231  */
0232  
0233 bool is_bad_inode(struct inode *inode)
0234 {
0235     return (inode->i_op == &bad_inode_ops); 
0236 }
0237 
0238 EXPORT_SYMBOL(is_bad_inode);
0239 
0240 /**
0241  * iget_failed - Mark an under-construction inode as dead and release it
0242  * @inode: The inode to discard
0243  *
0244  * Mark an under-construction inode as dead and release it.
0245  */
0246 void iget_failed(struct inode *inode)
0247 {
0248     make_bad_inode(inode);
0249     unlock_new_inode(inode);
0250     iput(inode);
0251 }
0252 EXPORT_SYMBOL(iget_failed);