Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  fs/libfs.c
0004  *  Library for filesystems writers.
0005  */
0006 
0007 #include <linux/blkdev.h>
0008 #include <linux/export.h>
0009 #include <linux/pagemap.h>
0010 #include <linux/slab.h>
0011 #include <linux/cred.h>
0012 #include <linux/mount.h>
0013 #include <linux/vfs.h>
0014 #include <linux/quotaops.h>
0015 #include <linux/mutex.h>
0016 #include <linux/namei.h>
0017 #include <linux/exportfs.h>
0018 #include <linux/writeback.h>
0019 #include <linux/buffer_head.h> /* sync_mapping_buffers */
0020 #include <linux/fs_context.h>
0021 #include <linux/pseudo_fs.h>
0022 #include <linux/fsnotify.h>
0023 #include <linux/unicode.h>
0024 #include <linux/fscrypt.h>
0025 
0026 #include <linux/uaccess.h>
0027 
0028 #include "internal.h"
0029 
0030 int simple_getattr(struct user_namespace *mnt_userns, const struct path *path,
0031            struct kstat *stat, u32 request_mask,
0032            unsigned int query_flags)
0033 {
0034     struct inode *inode = d_inode(path->dentry);
0035     generic_fillattr(&init_user_ns, inode, stat);
0036     stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
0037     return 0;
0038 }
0039 EXPORT_SYMBOL(simple_getattr);
0040 
0041 int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
0042 {
0043     buf->f_type = dentry->d_sb->s_magic;
0044     buf->f_bsize = PAGE_SIZE;
0045     buf->f_namelen = NAME_MAX;
0046     return 0;
0047 }
0048 EXPORT_SYMBOL(simple_statfs);
0049 
0050 /*
0051  * Retaining negative dentries for an in-memory filesystem just wastes
0052  * memory and lookup time: arrange for them to be deleted immediately.
0053  */
0054 int always_delete_dentry(const struct dentry *dentry)
0055 {
0056     return 1;
0057 }
0058 EXPORT_SYMBOL(always_delete_dentry);
0059 
0060 const struct dentry_operations simple_dentry_operations = {
0061     .d_delete = always_delete_dentry,
0062 };
0063 EXPORT_SYMBOL(simple_dentry_operations);
0064 
0065 /*
0066  * Lookup the data. This is trivial - if the dentry didn't already
0067  * exist, we know it is negative.  Set d_op to delete negative dentries.
0068  */
0069 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
0070 {
0071     if (dentry->d_name.len > NAME_MAX)
0072         return ERR_PTR(-ENAMETOOLONG);
0073     if (!dentry->d_sb->s_d_op)
0074         d_set_d_op(dentry, &simple_dentry_operations);
0075     d_add(dentry, NULL);
0076     return NULL;
0077 }
0078 EXPORT_SYMBOL(simple_lookup);
0079 
0080 int dcache_dir_open(struct inode *inode, struct file *file)
0081 {
0082     file->private_data = d_alloc_cursor(file->f_path.dentry);
0083 
0084     return file->private_data ? 0 : -ENOMEM;
0085 }
0086 EXPORT_SYMBOL(dcache_dir_open);
0087 
0088 int dcache_dir_close(struct inode *inode, struct file *file)
0089 {
0090     dput(file->private_data);
0091     return 0;
0092 }
0093 EXPORT_SYMBOL(dcache_dir_close);
0094 
0095 /* parent is locked at least shared */
0096 /*
0097  * Returns an element of siblings' list.
0098  * We are looking for <count>th positive after <p>; if
0099  * found, dentry is grabbed and returned to caller.
0100  * If no such element exists, NULL is returned.
0101  */
0102 static struct dentry *scan_positives(struct dentry *cursor,
0103                     struct list_head *p,
0104                     loff_t count,
0105                     struct dentry *last)
0106 {
0107     struct dentry *dentry = cursor->d_parent, *found = NULL;
0108 
0109     spin_lock(&dentry->d_lock);
0110     while ((p = p->next) != &dentry->d_subdirs) {
0111         struct dentry *d = list_entry(p, struct dentry, d_child);
0112         // we must at least skip cursors, to avoid livelocks
0113         if (d->d_flags & DCACHE_DENTRY_CURSOR)
0114             continue;
0115         if (simple_positive(d) && !--count) {
0116             spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
0117             if (simple_positive(d))
0118                 found = dget_dlock(d);
0119             spin_unlock(&d->d_lock);
0120             if (likely(found))
0121                 break;
0122             count = 1;
0123         }
0124         if (need_resched()) {
0125             list_move(&cursor->d_child, p);
0126             p = &cursor->d_child;
0127             spin_unlock(&dentry->d_lock);
0128             cond_resched();
0129             spin_lock(&dentry->d_lock);
0130         }
0131     }
0132     spin_unlock(&dentry->d_lock);
0133     dput(last);
0134     return found;
0135 }
0136 
0137 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
0138 {
0139     struct dentry *dentry = file->f_path.dentry;
0140     switch (whence) {
0141         case 1:
0142             offset += file->f_pos;
0143             fallthrough;
0144         case 0:
0145             if (offset >= 0)
0146                 break;
0147             fallthrough;
0148         default:
0149             return -EINVAL;
0150     }
0151     if (offset != file->f_pos) {
0152         struct dentry *cursor = file->private_data;
0153         struct dentry *to = NULL;
0154 
0155         inode_lock_shared(dentry->d_inode);
0156 
0157         if (offset > 2)
0158             to = scan_positives(cursor, &dentry->d_subdirs,
0159                         offset - 2, NULL);
0160         spin_lock(&dentry->d_lock);
0161         if (to)
0162             list_move(&cursor->d_child, &to->d_child);
0163         else
0164             list_del_init(&cursor->d_child);
0165         spin_unlock(&dentry->d_lock);
0166         dput(to);
0167 
0168         file->f_pos = offset;
0169 
0170         inode_unlock_shared(dentry->d_inode);
0171     }
0172     return offset;
0173 }
0174 EXPORT_SYMBOL(dcache_dir_lseek);
0175 
0176 /* Relationship between i_mode and the DT_xxx types */
0177 static inline unsigned char dt_type(struct inode *inode)
0178 {
0179     return (inode->i_mode >> 12) & 15;
0180 }
0181 
0182 /*
0183  * Directory is locked and all positive dentries in it are safe, since
0184  * for ramfs-type trees they can't go away without unlink() or rmdir(),
0185  * both impossible due to the lock on directory.
0186  */
0187 
0188 int dcache_readdir(struct file *file, struct dir_context *ctx)
0189 {
0190     struct dentry *dentry = file->f_path.dentry;
0191     struct dentry *cursor = file->private_data;
0192     struct list_head *anchor = &dentry->d_subdirs;
0193     struct dentry *next = NULL;
0194     struct list_head *p;
0195 
0196     if (!dir_emit_dots(file, ctx))
0197         return 0;
0198 
0199     if (ctx->pos == 2)
0200         p = anchor;
0201     else if (!list_empty(&cursor->d_child))
0202         p = &cursor->d_child;
0203     else
0204         return 0;
0205 
0206     while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
0207         if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
0208                   d_inode(next)->i_ino, dt_type(d_inode(next))))
0209             break;
0210         ctx->pos++;
0211         p = &next->d_child;
0212     }
0213     spin_lock(&dentry->d_lock);
0214     if (next)
0215         list_move_tail(&cursor->d_child, &next->d_child);
0216     else
0217         list_del_init(&cursor->d_child);
0218     spin_unlock(&dentry->d_lock);
0219     dput(next);
0220 
0221     return 0;
0222 }
0223 EXPORT_SYMBOL(dcache_readdir);
0224 
0225 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
0226 {
0227     return -EISDIR;
0228 }
0229 EXPORT_SYMBOL(generic_read_dir);
0230 
0231 const struct file_operations simple_dir_operations = {
0232     .open       = dcache_dir_open,
0233     .release    = dcache_dir_close,
0234     .llseek     = dcache_dir_lseek,
0235     .read       = generic_read_dir,
0236     .iterate_shared = dcache_readdir,
0237     .fsync      = noop_fsync,
0238 };
0239 EXPORT_SYMBOL(simple_dir_operations);
0240 
0241 const struct inode_operations simple_dir_inode_operations = {
0242     .lookup     = simple_lookup,
0243 };
0244 EXPORT_SYMBOL(simple_dir_inode_operations);
0245 
0246 static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
0247 {
0248     struct dentry *child = NULL;
0249     struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
0250 
0251     spin_lock(&parent->d_lock);
0252     while ((p = p->next) != &parent->d_subdirs) {
0253         struct dentry *d = container_of(p, struct dentry, d_child);
0254         if (simple_positive(d)) {
0255             spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
0256             if (simple_positive(d))
0257                 child = dget_dlock(d);
0258             spin_unlock(&d->d_lock);
0259             if (likely(child))
0260                 break;
0261         }
0262     }
0263     spin_unlock(&parent->d_lock);
0264     dput(prev);
0265     return child;
0266 }
0267 
0268 void simple_recursive_removal(struct dentry *dentry,
0269                               void (*callback)(struct dentry *))
0270 {
0271     struct dentry *this = dget(dentry);
0272     while (true) {
0273         struct dentry *victim = NULL, *child;
0274         struct inode *inode = this->d_inode;
0275 
0276         inode_lock(inode);
0277         if (d_is_dir(this))
0278             inode->i_flags |= S_DEAD;
0279         while ((child = find_next_child(this, victim)) == NULL) {
0280             // kill and ascend
0281             // update metadata while it's still locked
0282             inode->i_ctime = current_time(inode);
0283             clear_nlink(inode);
0284             inode_unlock(inode);
0285             victim = this;
0286             this = this->d_parent;
0287             inode = this->d_inode;
0288             inode_lock(inode);
0289             if (simple_positive(victim)) {
0290                 d_invalidate(victim);   // avoid lost mounts
0291                 if (d_is_dir(victim))
0292                     fsnotify_rmdir(inode, victim);
0293                 else
0294                     fsnotify_unlink(inode, victim);
0295                 if (callback)
0296                     callback(victim);
0297                 dput(victim);       // unpin it
0298             }
0299             if (victim == dentry) {
0300                 inode->i_ctime = inode->i_mtime =
0301                     current_time(inode);
0302                 if (d_is_dir(dentry))
0303                     drop_nlink(inode);
0304                 inode_unlock(inode);
0305                 dput(dentry);
0306                 return;
0307             }
0308         }
0309         inode_unlock(inode);
0310         this = child;
0311     }
0312 }
0313 EXPORT_SYMBOL(simple_recursive_removal);
0314 
0315 static const struct super_operations simple_super_operations = {
0316     .statfs     = simple_statfs,
0317 };
0318 
0319 static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
0320 {
0321     struct pseudo_fs_context *ctx = fc->fs_private;
0322     struct inode *root;
0323 
0324     s->s_maxbytes = MAX_LFS_FILESIZE;
0325     s->s_blocksize = PAGE_SIZE;
0326     s->s_blocksize_bits = PAGE_SHIFT;
0327     s->s_magic = ctx->magic;
0328     s->s_op = ctx->ops ?: &simple_super_operations;
0329     s->s_xattr = ctx->xattr;
0330     s->s_time_gran = 1;
0331     root = new_inode(s);
0332     if (!root)
0333         return -ENOMEM;
0334 
0335     /*
0336      * since this is the first inode, make it number 1. New inodes created
0337      * after this must take care not to collide with it (by passing
0338      * max_reserved of 1 to iunique).
0339      */
0340     root->i_ino = 1;
0341     root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
0342     root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
0343     s->s_root = d_make_root(root);
0344     if (!s->s_root)
0345         return -ENOMEM;
0346     s->s_d_op = ctx->dops;
0347     return 0;
0348 }
0349 
0350 static int pseudo_fs_get_tree(struct fs_context *fc)
0351 {
0352     return get_tree_nodev(fc, pseudo_fs_fill_super);
0353 }
0354 
0355 static void pseudo_fs_free(struct fs_context *fc)
0356 {
0357     kfree(fc->fs_private);
0358 }
0359 
0360 static const struct fs_context_operations pseudo_fs_context_ops = {
0361     .free       = pseudo_fs_free,
0362     .get_tree   = pseudo_fs_get_tree,
0363 };
0364 
0365 /*
0366  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
0367  * will never be mountable)
0368  */
0369 struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
0370                     unsigned long magic)
0371 {
0372     struct pseudo_fs_context *ctx;
0373 
0374     ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
0375     if (likely(ctx)) {
0376         ctx->magic = magic;
0377         fc->fs_private = ctx;
0378         fc->ops = &pseudo_fs_context_ops;
0379         fc->sb_flags |= SB_NOUSER;
0380         fc->global = true;
0381     }
0382     return ctx;
0383 }
0384 EXPORT_SYMBOL(init_pseudo);
0385 
0386 int simple_open(struct inode *inode, struct file *file)
0387 {
0388     if (inode->i_private)
0389         file->private_data = inode->i_private;
0390     return 0;
0391 }
0392 EXPORT_SYMBOL(simple_open);
0393 
0394 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
0395 {
0396     struct inode *inode = d_inode(old_dentry);
0397 
0398     inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
0399     inc_nlink(inode);
0400     ihold(inode);
0401     dget(dentry);
0402     d_instantiate(dentry, inode);
0403     return 0;
0404 }
0405 EXPORT_SYMBOL(simple_link);
0406 
0407 int simple_empty(struct dentry *dentry)
0408 {
0409     struct dentry *child;
0410     int ret = 0;
0411 
0412     spin_lock(&dentry->d_lock);
0413     list_for_each_entry(child, &dentry->d_subdirs, d_child) {
0414         spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
0415         if (simple_positive(child)) {
0416             spin_unlock(&child->d_lock);
0417             goto out;
0418         }
0419         spin_unlock(&child->d_lock);
0420     }
0421     ret = 1;
0422 out:
0423     spin_unlock(&dentry->d_lock);
0424     return ret;
0425 }
0426 EXPORT_SYMBOL(simple_empty);
0427 
0428 int simple_unlink(struct inode *dir, struct dentry *dentry)
0429 {
0430     struct inode *inode = d_inode(dentry);
0431 
0432     inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
0433     drop_nlink(inode);
0434     dput(dentry);
0435     return 0;
0436 }
0437 EXPORT_SYMBOL(simple_unlink);
0438 
0439 int simple_rmdir(struct inode *dir, struct dentry *dentry)
0440 {
0441     if (!simple_empty(dentry))
0442         return -ENOTEMPTY;
0443 
0444     drop_nlink(d_inode(dentry));
0445     simple_unlink(dir, dentry);
0446     drop_nlink(dir);
0447     return 0;
0448 }
0449 EXPORT_SYMBOL(simple_rmdir);
0450 
0451 int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
0452                struct inode *new_dir, struct dentry *new_dentry)
0453 {
0454     bool old_is_dir = d_is_dir(old_dentry);
0455     bool new_is_dir = d_is_dir(new_dentry);
0456 
0457     if (old_dir != new_dir && old_is_dir != new_is_dir) {
0458         if (old_is_dir) {
0459             drop_nlink(old_dir);
0460             inc_nlink(new_dir);
0461         } else {
0462             drop_nlink(new_dir);
0463             inc_nlink(old_dir);
0464         }
0465     }
0466     old_dir->i_ctime = old_dir->i_mtime =
0467     new_dir->i_ctime = new_dir->i_mtime =
0468     d_inode(old_dentry)->i_ctime =
0469     d_inode(new_dentry)->i_ctime = current_time(old_dir);
0470 
0471     return 0;
0472 }
0473 EXPORT_SYMBOL_GPL(simple_rename_exchange);
0474 
0475 int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
0476           struct dentry *old_dentry, struct inode *new_dir,
0477           struct dentry *new_dentry, unsigned int flags)
0478 {
0479     struct inode *inode = d_inode(old_dentry);
0480     int they_are_dirs = d_is_dir(old_dentry);
0481 
0482     if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
0483         return -EINVAL;
0484 
0485     if (flags & RENAME_EXCHANGE)
0486         return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
0487 
0488     if (!simple_empty(new_dentry))
0489         return -ENOTEMPTY;
0490 
0491     if (d_really_is_positive(new_dentry)) {
0492         simple_unlink(new_dir, new_dentry);
0493         if (they_are_dirs) {
0494             drop_nlink(d_inode(new_dentry));
0495             drop_nlink(old_dir);
0496         }
0497     } else if (they_are_dirs) {
0498         drop_nlink(old_dir);
0499         inc_nlink(new_dir);
0500     }
0501 
0502     old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
0503         new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
0504 
0505     return 0;
0506 }
0507 EXPORT_SYMBOL(simple_rename);
0508 
0509 /**
0510  * simple_setattr - setattr for simple filesystem
0511  * @mnt_userns: user namespace of the target mount
0512  * @dentry: dentry
0513  * @iattr: iattr structure
0514  *
0515  * Returns 0 on success, -error on failure.
0516  *
0517  * simple_setattr is a simple ->setattr implementation without a proper
0518  * implementation of size changes.
0519  *
0520  * It can either be used for in-memory filesystems or special files
0521  * on simple regular filesystems.  Anything that needs to change on-disk
0522  * or wire state on size changes needs its own setattr method.
0523  */
0524 int simple_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0525            struct iattr *iattr)
0526 {
0527     struct inode *inode = d_inode(dentry);
0528     int error;
0529 
0530     error = setattr_prepare(mnt_userns, dentry, iattr);
0531     if (error)
0532         return error;
0533 
0534     if (iattr->ia_valid & ATTR_SIZE)
0535         truncate_setsize(inode, iattr->ia_size);
0536     setattr_copy(mnt_userns, inode, iattr);
0537     mark_inode_dirty(inode);
0538     return 0;
0539 }
0540 EXPORT_SYMBOL(simple_setattr);
0541 
0542 static int simple_read_folio(struct file *file, struct folio *folio)
0543 {
0544     folio_zero_range(folio, 0, folio_size(folio));
0545     flush_dcache_folio(folio);
0546     folio_mark_uptodate(folio);
0547     folio_unlock(folio);
0548     return 0;
0549 }
0550 
0551 int simple_write_begin(struct file *file, struct address_space *mapping,
0552             loff_t pos, unsigned len,
0553             struct page **pagep, void **fsdata)
0554 {
0555     struct page *page;
0556     pgoff_t index;
0557 
0558     index = pos >> PAGE_SHIFT;
0559 
0560     page = grab_cache_page_write_begin(mapping, index);
0561     if (!page)
0562         return -ENOMEM;
0563 
0564     *pagep = page;
0565 
0566     if (!PageUptodate(page) && (len != PAGE_SIZE)) {
0567         unsigned from = pos & (PAGE_SIZE - 1);
0568 
0569         zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
0570     }
0571     return 0;
0572 }
0573 EXPORT_SYMBOL(simple_write_begin);
0574 
0575 /**
0576  * simple_write_end - .write_end helper for non-block-device FSes
0577  * @file: See .write_end of address_space_operations
0578  * @mapping:        "
0579  * @pos:        "
0580  * @len:        "
0581  * @copied:         "
0582  * @page:       "
0583  * @fsdata:         "
0584  *
0585  * simple_write_end does the minimum needed for updating a page after writing is
0586  * done. It has the same API signature as the .write_end of
0587  * address_space_operations vector. So it can just be set onto .write_end for
0588  * FSes that don't need any other processing. i_mutex is assumed to be held.
0589  * Block based filesystems should use generic_write_end().
0590  * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
0591  * is not called, so a filesystem that actually does store data in .write_inode
0592  * should extend on what's done here with a call to mark_inode_dirty() in the
0593  * case that i_size has changed.
0594  *
0595  * Use *ONLY* with simple_read_folio()
0596  */
0597 static int simple_write_end(struct file *file, struct address_space *mapping,
0598             loff_t pos, unsigned len, unsigned copied,
0599             struct page *page, void *fsdata)
0600 {
0601     struct inode *inode = page->mapping->host;
0602     loff_t last_pos = pos + copied;
0603 
0604     /* zero the stale part of the page if we did a short copy */
0605     if (!PageUptodate(page)) {
0606         if (copied < len) {
0607             unsigned from = pos & (PAGE_SIZE - 1);
0608 
0609             zero_user(page, from + copied, len - copied);
0610         }
0611         SetPageUptodate(page);
0612     }
0613     /*
0614      * No need to use i_size_read() here, the i_size
0615      * cannot change under us because we hold the i_mutex.
0616      */
0617     if (last_pos > inode->i_size)
0618         i_size_write(inode, last_pos);
0619 
0620     set_page_dirty(page);
0621     unlock_page(page);
0622     put_page(page);
0623 
0624     return copied;
0625 }
0626 
0627 /*
0628  * Provides ramfs-style behavior: data in the pagecache, but no writeback.
0629  */
0630 const struct address_space_operations ram_aops = {
0631     .read_folio = simple_read_folio,
0632     .write_begin    = simple_write_begin,
0633     .write_end  = simple_write_end,
0634     .dirty_folio    = noop_dirty_folio,
0635 };
0636 EXPORT_SYMBOL(ram_aops);
0637 
0638 /*
0639  * the inodes created here are not hashed. If you use iunique to generate
0640  * unique inode values later for this filesystem, then you must take care
0641  * to pass it an appropriate max_reserved value to avoid collisions.
0642  */
0643 int simple_fill_super(struct super_block *s, unsigned long magic,
0644               const struct tree_descr *files)
0645 {
0646     struct inode *inode;
0647     struct dentry *root;
0648     struct dentry *dentry;
0649     int i;
0650 
0651     s->s_blocksize = PAGE_SIZE;
0652     s->s_blocksize_bits = PAGE_SHIFT;
0653     s->s_magic = magic;
0654     s->s_op = &simple_super_operations;
0655     s->s_time_gran = 1;
0656 
0657     inode = new_inode(s);
0658     if (!inode)
0659         return -ENOMEM;
0660     /*
0661      * because the root inode is 1, the files array must not contain an
0662      * entry at index 1
0663      */
0664     inode->i_ino = 1;
0665     inode->i_mode = S_IFDIR | 0755;
0666     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0667     inode->i_op = &simple_dir_inode_operations;
0668     inode->i_fop = &simple_dir_operations;
0669     set_nlink(inode, 2);
0670     root = d_make_root(inode);
0671     if (!root)
0672         return -ENOMEM;
0673     for (i = 0; !files->name || files->name[0]; i++, files++) {
0674         if (!files->name)
0675             continue;
0676 
0677         /* warn if it tries to conflict with the root inode */
0678         if (unlikely(i == 1))
0679             printk(KERN_WARNING "%s: %s passed in a files array"
0680                 "with an index of 1!\n", __func__,
0681                 s->s_type->name);
0682 
0683         dentry = d_alloc_name(root, files->name);
0684         if (!dentry)
0685             goto out;
0686         inode = new_inode(s);
0687         if (!inode) {
0688             dput(dentry);
0689             goto out;
0690         }
0691         inode->i_mode = S_IFREG | files->mode;
0692         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0693         inode->i_fop = files->ops;
0694         inode->i_ino = i;
0695         d_add(dentry, inode);
0696     }
0697     s->s_root = root;
0698     return 0;
0699 out:
0700     d_genocide(root);
0701     shrink_dcache_parent(root);
0702     dput(root);
0703     return -ENOMEM;
0704 }
0705 EXPORT_SYMBOL(simple_fill_super);
0706 
0707 static DEFINE_SPINLOCK(pin_fs_lock);
0708 
0709 int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
0710 {
0711     struct vfsmount *mnt = NULL;
0712     spin_lock(&pin_fs_lock);
0713     if (unlikely(!*mount)) {
0714         spin_unlock(&pin_fs_lock);
0715         mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
0716         if (IS_ERR(mnt))
0717             return PTR_ERR(mnt);
0718         spin_lock(&pin_fs_lock);
0719         if (!*mount)
0720             *mount = mnt;
0721     }
0722     mntget(*mount);
0723     ++*count;
0724     spin_unlock(&pin_fs_lock);
0725     mntput(mnt);
0726     return 0;
0727 }
0728 EXPORT_SYMBOL(simple_pin_fs);
0729 
0730 void simple_release_fs(struct vfsmount **mount, int *count)
0731 {
0732     struct vfsmount *mnt;
0733     spin_lock(&pin_fs_lock);
0734     mnt = *mount;
0735     if (!--*count)
0736         *mount = NULL;
0737     spin_unlock(&pin_fs_lock);
0738     mntput(mnt);
0739 }
0740 EXPORT_SYMBOL(simple_release_fs);
0741 
0742 /**
0743  * simple_read_from_buffer - copy data from the buffer to user space
0744  * @to: the user space buffer to read to
0745  * @count: the maximum number of bytes to read
0746  * @ppos: the current position in the buffer
0747  * @from: the buffer to read from
0748  * @available: the size of the buffer
0749  *
0750  * The simple_read_from_buffer() function reads up to @count bytes from the
0751  * buffer @from at offset @ppos into the user space address starting at @to.
0752  *
0753  * On success, the number of bytes read is returned and the offset @ppos is
0754  * advanced by this number, or negative value is returned on error.
0755  **/
0756 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
0757                 const void *from, size_t available)
0758 {
0759     loff_t pos = *ppos;
0760     size_t ret;
0761 
0762     if (pos < 0)
0763         return -EINVAL;
0764     if (pos >= available || !count)
0765         return 0;
0766     if (count > available - pos)
0767         count = available - pos;
0768     ret = copy_to_user(to, from + pos, count);
0769     if (ret == count)
0770         return -EFAULT;
0771     count -= ret;
0772     *ppos = pos + count;
0773     return count;
0774 }
0775 EXPORT_SYMBOL(simple_read_from_buffer);
0776 
0777 /**
0778  * simple_write_to_buffer - copy data from user space to the buffer
0779  * @to: the buffer to write to
0780  * @available: the size of the buffer
0781  * @ppos: the current position in the buffer
0782  * @from: the user space buffer to read from
0783  * @count: the maximum number of bytes to read
0784  *
0785  * The simple_write_to_buffer() function reads up to @count bytes from the user
0786  * space address starting at @from into the buffer @to at offset @ppos.
0787  *
0788  * On success, the number of bytes written is returned and the offset @ppos is
0789  * advanced by this number, or negative value is returned on error.
0790  **/
0791 ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
0792         const void __user *from, size_t count)
0793 {
0794     loff_t pos = *ppos;
0795     size_t res;
0796 
0797     if (pos < 0)
0798         return -EINVAL;
0799     if (pos >= available || !count)
0800         return 0;
0801     if (count > available - pos)
0802         count = available - pos;
0803     res = copy_from_user(to + pos, from, count);
0804     if (res == count)
0805         return -EFAULT;
0806     count -= res;
0807     *ppos = pos + count;
0808     return count;
0809 }
0810 EXPORT_SYMBOL(simple_write_to_buffer);
0811 
0812 /**
0813  * memory_read_from_buffer - copy data from the buffer
0814  * @to: the kernel space buffer to read to
0815  * @count: the maximum number of bytes to read
0816  * @ppos: the current position in the buffer
0817  * @from: the buffer to read from
0818  * @available: the size of the buffer
0819  *
0820  * The memory_read_from_buffer() function reads up to @count bytes from the
0821  * buffer @from at offset @ppos into the kernel space address starting at @to.
0822  *
0823  * On success, the number of bytes read is returned and the offset @ppos is
0824  * advanced by this number, or negative value is returned on error.
0825  **/
0826 ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
0827                 const void *from, size_t available)
0828 {
0829     loff_t pos = *ppos;
0830 
0831     if (pos < 0)
0832         return -EINVAL;
0833     if (pos >= available)
0834         return 0;
0835     if (count > available - pos)
0836         count = available - pos;
0837     memcpy(to, from + pos, count);
0838     *ppos = pos + count;
0839 
0840     return count;
0841 }
0842 EXPORT_SYMBOL(memory_read_from_buffer);
0843 
0844 /*
0845  * Transaction based IO.
0846  * The file expects a single write which triggers the transaction, and then
0847  * possibly a read which collects the result - which is stored in a
0848  * file-local buffer.
0849  */
0850 
0851 void simple_transaction_set(struct file *file, size_t n)
0852 {
0853     struct simple_transaction_argresp *ar = file->private_data;
0854 
0855     BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
0856 
0857     /*
0858      * The barrier ensures that ar->size will really remain zero until
0859      * ar->data is ready for reading.
0860      */
0861     smp_mb();
0862     ar->size = n;
0863 }
0864 EXPORT_SYMBOL(simple_transaction_set);
0865 
0866 char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
0867 {
0868     struct simple_transaction_argresp *ar;
0869     static DEFINE_SPINLOCK(simple_transaction_lock);
0870 
0871     if (size > SIMPLE_TRANSACTION_LIMIT - 1)
0872         return ERR_PTR(-EFBIG);
0873 
0874     ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
0875     if (!ar)
0876         return ERR_PTR(-ENOMEM);
0877 
0878     spin_lock(&simple_transaction_lock);
0879 
0880     /* only one write allowed per open */
0881     if (file->private_data) {
0882         spin_unlock(&simple_transaction_lock);
0883         free_page((unsigned long)ar);
0884         return ERR_PTR(-EBUSY);
0885     }
0886 
0887     file->private_data = ar;
0888 
0889     spin_unlock(&simple_transaction_lock);
0890 
0891     if (copy_from_user(ar->data, buf, size))
0892         return ERR_PTR(-EFAULT);
0893 
0894     return ar->data;
0895 }
0896 EXPORT_SYMBOL(simple_transaction_get);
0897 
0898 ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
0899 {
0900     struct simple_transaction_argresp *ar = file->private_data;
0901 
0902     if (!ar)
0903         return 0;
0904     return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
0905 }
0906 EXPORT_SYMBOL(simple_transaction_read);
0907 
0908 int simple_transaction_release(struct inode *inode, struct file *file)
0909 {
0910     free_page((unsigned long)file->private_data);
0911     return 0;
0912 }
0913 EXPORT_SYMBOL(simple_transaction_release);
0914 
0915 /* Simple attribute files */
0916 
0917 struct simple_attr {
0918     int (*get)(void *, u64 *);
0919     int (*set)(void *, u64);
0920     char get_buf[24];   /* enough to store a u64 and "\n\0" */
0921     char set_buf[24];
0922     void *data;
0923     const char *fmt;    /* format for read operation */
0924     struct mutex mutex; /* protects access to these buffers */
0925 };
0926 
0927 /* simple_attr_open is called by an actual attribute open file operation
0928  * to set the attribute specific access operations. */
0929 int simple_attr_open(struct inode *inode, struct file *file,
0930              int (*get)(void *, u64 *), int (*set)(void *, u64),
0931              const char *fmt)
0932 {
0933     struct simple_attr *attr;
0934 
0935     attr = kzalloc(sizeof(*attr), GFP_KERNEL);
0936     if (!attr)
0937         return -ENOMEM;
0938 
0939     attr->get = get;
0940     attr->set = set;
0941     attr->data = inode->i_private;
0942     attr->fmt = fmt;
0943     mutex_init(&attr->mutex);
0944 
0945     file->private_data = attr;
0946 
0947     return nonseekable_open(inode, file);
0948 }
0949 EXPORT_SYMBOL_GPL(simple_attr_open);
0950 
0951 int simple_attr_release(struct inode *inode, struct file *file)
0952 {
0953     kfree(file->private_data);
0954     return 0;
0955 }
0956 EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only?  This?  Really? */
0957 
0958 /* read from the buffer that is filled with the get function */
0959 ssize_t simple_attr_read(struct file *file, char __user *buf,
0960              size_t len, loff_t *ppos)
0961 {
0962     struct simple_attr *attr;
0963     size_t size;
0964     ssize_t ret;
0965 
0966     attr = file->private_data;
0967 
0968     if (!attr->get)
0969         return -EACCES;
0970 
0971     ret = mutex_lock_interruptible(&attr->mutex);
0972     if (ret)
0973         return ret;
0974 
0975     if (*ppos && attr->get_buf[0]) {
0976         /* continued read */
0977         size = strlen(attr->get_buf);
0978     } else {
0979         /* first read */
0980         u64 val;
0981         ret = attr->get(attr->data, &val);
0982         if (ret)
0983             goto out;
0984 
0985         size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
0986                  attr->fmt, (unsigned long long)val);
0987     }
0988 
0989     ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
0990 out:
0991     mutex_unlock(&attr->mutex);
0992     return ret;
0993 }
0994 EXPORT_SYMBOL_GPL(simple_attr_read);
0995 
0996 /* interpret the buffer as a number to call the set function with */
0997 ssize_t simple_attr_write(struct file *file, const char __user *buf,
0998               size_t len, loff_t *ppos)
0999 {
1000     struct simple_attr *attr;
1001     unsigned long long val;
1002     size_t size;
1003     ssize_t ret;
1004 
1005     attr = file->private_data;
1006     if (!attr->set)
1007         return -EACCES;
1008 
1009     ret = mutex_lock_interruptible(&attr->mutex);
1010     if (ret)
1011         return ret;
1012 
1013     ret = -EFAULT;
1014     size = min(sizeof(attr->set_buf) - 1, len);
1015     if (copy_from_user(attr->set_buf, buf, size))
1016         goto out;
1017 
1018     attr->set_buf[size] = '\0';
1019     ret = kstrtoull(attr->set_buf, 0, &val);
1020     if (ret)
1021         goto out;
1022     ret = attr->set(attr->data, val);
1023     if (ret == 0)
1024         ret = len; /* on success, claim we got the whole input */
1025 out:
1026     mutex_unlock(&attr->mutex);
1027     return ret;
1028 }
1029 EXPORT_SYMBOL_GPL(simple_attr_write);
1030 
1031 /**
1032  * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
1033  * @sb:     filesystem to do the file handle conversion on
1034  * @fid:    file handle to convert
1035  * @fh_len: length of the file handle in bytes
1036  * @fh_type:    type of file handle
1037  * @get_inode:  filesystem callback to retrieve inode
1038  *
1039  * This function decodes @fid as long as it has one of the well-known
1040  * Linux filehandle types and calls @get_inode on it to retrieve the
1041  * inode for the object specified in the file handle.
1042  */
1043 struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
1044         int fh_len, int fh_type, struct inode *(*get_inode)
1045             (struct super_block *sb, u64 ino, u32 gen))
1046 {
1047     struct inode *inode = NULL;
1048 
1049     if (fh_len < 2)
1050         return NULL;
1051 
1052     switch (fh_type) {
1053     case FILEID_INO32_GEN:
1054     case FILEID_INO32_GEN_PARENT:
1055         inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
1056         break;
1057     }
1058 
1059     return d_obtain_alias(inode);
1060 }
1061 EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
1062 
1063 /**
1064  * generic_fh_to_parent - generic helper for the fh_to_parent export operation
1065  * @sb:     filesystem to do the file handle conversion on
1066  * @fid:    file handle to convert
1067  * @fh_len: length of the file handle in bytes
1068  * @fh_type:    type of file handle
1069  * @get_inode:  filesystem callback to retrieve inode
1070  *
1071  * This function decodes @fid as long as it has one of the well-known
1072  * Linux filehandle types and calls @get_inode on it to retrieve the
1073  * inode for the _parent_ object specified in the file handle if it
1074  * is specified in the file handle, or NULL otherwise.
1075  */
1076 struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
1077         int fh_len, int fh_type, struct inode *(*get_inode)
1078             (struct super_block *sb, u64 ino, u32 gen))
1079 {
1080     struct inode *inode = NULL;
1081 
1082     if (fh_len <= 2)
1083         return NULL;
1084 
1085     switch (fh_type) {
1086     case FILEID_INO32_GEN_PARENT:
1087         inode = get_inode(sb, fid->i32.parent_ino,
1088                   (fh_len > 3 ? fid->i32.parent_gen : 0));
1089         break;
1090     }
1091 
1092     return d_obtain_alias(inode);
1093 }
1094 EXPORT_SYMBOL_GPL(generic_fh_to_parent);
1095 
1096 /**
1097  * __generic_file_fsync - generic fsync implementation for simple filesystems
1098  *
1099  * @file:   file to synchronize
1100  * @start:  start offset in bytes
1101  * @end:    end offset in bytes (inclusive)
1102  * @datasync:   only synchronize essential metadata if true
1103  *
1104  * This is a generic implementation of the fsync method for simple
1105  * filesystems which track all non-inode metadata in the buffers list
1106  * hanging off the address_space structure.
1107  */
1108 int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
1109                  int datasync)
1110 {
1111     struct inode *inode = file->f_mapping->host;
1112     int err;
1113     int ret;
1114 
1115     err = file_write_and_wait_range(file, start, end);
1116     if (err)
1117         return err;
1118 
1119     inode_lock(inode);
1120     ret = sync_mapping_buffers(inode->i_mapping);
1121     if (!(inode->i_state & I_DIRTY_ALL))
1122         goto out;
1123     if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
1124         goto out;
1125 
1126     err = sync_inode_metadata(inode, 1);
1127     if (ret == 0)
1128         ret = err;
1129 
1130 out:
1131     inode_unlock(inode);
1132     /* check and advance again to catch errors after syncing out buffers */
1133     err = file_check_and_advance_wb_err(file);
1134     if (ret == 0)
1135         ret = err;
1136     return ret;
1137 }
1138 EXPORT_SYMBOL(__generic_file_fsync);
1139 
1140 /**
1141  * generic_file_fsync - generic fsync implementation for simple filesystems
1142  *          with flush
1143  * @file:   file to synchronize
1144  * @start:  start offset in bytes
1145  * @end:    end offset in bytes (inclusive)
1146  * @datasync:   only synchronize essential metadata if true
1147  *
1148  */
1149 
1150 int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1151                int datasync)
1152 {
1153     struct inode *inode = file->f_mapping->host;
1154     int err;
1155 
1156     err = __generic_file_fsync(file, start, end, datasync);
1157     if (err)
1158         return err;
1159     return blkdev_issue_flush(inode->i_sb->s_bdev);
1160 }
1161 EXPORT_SYMBOL(generic_file_fsync);
1162 
1163 /**
1164  * generic_check_addressable - Check addressability of file system
1165  * @blocksize_bits: log of file system block size
1166  * @num_blocks:     number of blocks in file system
1167  *
1168  * Determine whether a file system with @num_blocks blocks (and a
1169  * block size of 2**@blocksize_bits) is addressable by the sector_t
1170  * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
1171  */
1172 int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1173 {
1174     u64 last_fs_block = num_blocks - 1;
1175     u64 last_fs_page =
1176         last_fs_block >> (PAGE_SHIFT - blocksize_bits);
1177 
1178     if (unlikely(num_blocks == 0))
1179         return 0;
1180 
1181     if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
1182         return -EINVAL;
1183 
1184     if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1185         (last_fs_page > (pgoff_t)(~0ULL))) {
1186         return -EFBIG;
1187     }
1188     return 0;
1189 }
1190 EXPORT_SYMBOL(generic_check_addressable);
1191 
1192 /*
1193  * No-op implementation of ->fsync for in-memory filesystems.
1194  */
1195 int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1196 {
1197     return 0;
1198 }
1199 EXPORT_SYMBOL(noop_fsync);
1200 
1201 ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1202 {
1203     /*
1204      * iomap based filesystems support direct I/O without need for
1205      * this callback. However, it still needs to be set in
1206      * inode->a_ops so that open/fcntl know that direct I/O is
1207      * generally supported.
1208      */
1209     return -EINVAL;
1210 }
1211 EXPORT_SYMBOL_GPL(noop_direct_IO);
1212 
1213 /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1214 void kfree_link(void *p)
1215 {
1216     kfree(p);
1217 }
1218 EXPORT_SYMBOL(kfree_link);
1219 
1220 struct inode *alloc_anon_inode(struct super_block *s)
1221 {
1222     static const struct address_space_operations anon_aops = {
1223         .dirty_folio    = noop_dirty_folio,
1224     };
1225     struct inode *inode = new_inode_pseudo(s);
1226 
1227     if (!inode)
1228         return ERR_PTR(-ENOMEM);
1229 
1230     inode->i_ino = get_next_ino();
1231     inode->i_mapping->a_ops = &anon_aops;
1232 
1233     /*
1234      * Mark the inode dirty from the very beginning,
1235      * that way it will never be moved to the dirty
1236      * list because mark_inode_dirty() will think
1237      * that it already _is_ on the dirty list.
1238      */
1239     inode->i_state = I_DIRTY;
1240     inode->i_mode = S_IRUSR | S_IWUSR;
1241     inode->i_uid = current_fsuid();
1242     inode->i_gid = current_fsgid();
1243     inode->i_flags |= S_PRIVATE;
1244     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1245     return inode;
1246 }
1247 EXPORT_SYMBOL(alloc_anon_inode);
1248 
1249 /**
1250  * simple_nosetlease - generic helper for prohibiting leases
1251  * @filp: file pointer
1252  * @arg: type of lease to obtain
1253  * @flp: new lease supplied for insertion
1254  * @priv: private data for lm_setup operation
1255  *
1256  * Generic helper for filesystems that do not wish to allow leases to be set.
1257  * All arguments are ignored and it just returns -EINVAL.
1258  */
1259 int
1260 simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1261           void **priv)
1262 {
1263     return -EINVAL;
1264 }
1265 EXPORT_SYMBOL(simple_nosetlease);
1266 
1267 /**
1268  * simple_get_link - generic helper to get the target of "fast" symlinks
1269  * @dentry: not used here
1270  * @inode: the symlink inode
1271  * @done: not used here
1272  *
1273  * Generic helper for filesystems to use for symlink inodes where a pointer to
1274  * the symlink target is stored in ->i_link.  NOTE: this isn't normally called,
1275  * since as an optimization the path lookup code uses any non-NULL ->i_link
1276  * directly, without calling ->get_link().  But ->get_link() still must be set,
1277  * to mark the inode_operations as being for a symlink.
1278  *
1279  * Return: the symlink target
1280  */
1281 const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1282                 struct delayed_call *done)
1283 {
1284     return inode->i_link;
1285 }
1286 EXPORT_SYMBOL(simple_get_link);
1287 
1288 const struct inode_operations simple_symlink_inode_operations = {
1289     .get_link = simple_get_link,
1290 };
1291 EXPORT_SYMBOL(simple_symlink_inode_operations);
1292 
1293 /*
1294  * Operations for a permanently empty directory.
1295  */
1296 static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1297 {
1298     return ERR_PTR(-ENOENT);
1299 }
1300 
1301 static int empty_dir_getattr(struct user_namespace *mnt_userns,
1302                  const struct path *path, struct kstat *stat,
1303                  u32 request_mask, unsigned int query_flags)
1304 {
1305     struct inode *inode = d_inode(path->dentry);
1306     generic_fillattr(&init_user_ns, inode, stat);
1307     return 0;
1308 }
1309 
1310 static int empty_dir_setattr(struct user_namespace *mnt_userns,
1311                  struct dentry *dentry, struct iattr *attr)
1312 {
1313     return -EPERM;
1314 }
1315 
1316 static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1317 {
1318     return -EOPNOTSUPP;
1319 }
1320 
1321 static const struct inode_operations empty_dir_inode_operations = {
1322     .lookup     = empty_dir_lookup,
1323     .permission = generic_permission,
1324     .setattr    = empty_dir_setattr,
1325     .getattr    = empty_dir_getattr,
1326     .listxattr  = empty_dir_listxattr,
1327 };
1328 
1329 static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1330 {
1331     /* An empty directory has two entries . and .. at offsets 0 and 1 */
1332     return generic_file_llseek_size(file, offset, whence, 2, 2);
1333 }
1334 
1335 static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1336 {
1337     dir_emit_dots(file, ctx);
1338     return 0;
1339 }
1340 
1341 static const struct file_operations empty_dir_operations = {
1342     .llseek     = empty_dir_llseek,
1343     .read       = generic_read_dir,
1344     .iterate_shared = empty_dir_readdir,
1345     .fsync      = noop_fsync,
1346 };
1347 
1348 
1349 void make_empty_dir_inode(struct inode *inode)
1350 {
1351     set_nlink(inode, 2);
1352     inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1353     inode->i_uid = GLOBAL_ROOT_UID;
1354     inode->i_gid = GLOBAL_ROOT_GID;
1355     inode->i_rdev = 0;
1356     inode->i_size = 0;
1357     inode->i_blkbits = PAGE_SHIFT;
1358     inode->i_blocks = 0;
1359 
1360     inode->i_op = &empty_dir_inode_operations;
1361     inode->i_opflags &= ~IOP_XATTR;
1362     inode->i_fop = &empty_dir_operations;
1363 }
1364 
1365 bool is_empty_dir_inode(struct inode *inode)
1366 {
1367     return (inode->i_fop == &empty_dir_operations) &&
1368         (inode->i_op == &empty_dir_inode_operations);
1369 }
1370 
1371 #if IS_ENABLED(CONFIG_UNICODE)
1372 /*
1373  * Determine if the name of a dentry should be casefolded.
1374  *
1375  * Return: if names will need casefolding
1376  */
1377 static bool needs_casefold(const struct inode *dir)
1378 {
1379     return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding;
1380 }
1381 
1382 /**
1383  * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1384  * @dentry: dentry whose name we are checking against
1385  * @len:    len of name of dentry
1386  * @str:    str pointer to name of dentry
1387  * @name:   Name to compare against
1388  *
1389  * Return: 0 if names match, 1 if mismatch, or -ERRNO
1390  */
1391 static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1392                 const char *str, const struct qstr *name)
1393 {
1394     const struct dentry *parent = READ_ONCE(dentry->d_parent);
1395     const struct inode *dir = READ_ONCE(parent->d_inode);
1396     const struct super_block *sb = dentry->d_sb;
1397     const struct unicode_map *um = sb->s_encoding;
1398     struct qstr qstr = QSTR_INIT(str, len);
1399     char strbuf[DNAME_INLINE_LEN];
1400     int ret;
1401 
1402     if (!dir || !needs_casefold(dir))
1403         goto fallback;
1404     /*
1405      * If the dentry name is stored in-line, then it may be concurrently
1406      * modified by a rename.  If this happens, the VFS will eventually retry
1407      * the lookup, so it doesn't matter what ->d_compare() returns.
1408      * However, it's unsafe to call utf8_strncasecmp() with an unstable
1409      * string.  Therefore, we have to copy the name into a temporary buffer.
1410      */
1411     if (len <= DNAME_INLINE_LEN - 1) {
1412         memcpy(strbuf, str, len);
1413         strbuf[len] = 0;
1414         qstr.name = strbuf;
1415         /* prevent compiler from optimizing out the temporary buffer */
1416         barrier();
1417     }
1418     ret = utf8_strncasecmp(um, name, &qstr);
1419     if (ret >= 0)
1420         return ret;
1421 
1422     if (sb_has_strict_encoding(sb))
1423         return -EINVAL;
1424 fallback:
1425     if (len != name->len)
1426         return 1;
1427     return !!memcmp(str, name->name, len);
1428 }
1429 
1430 /**
1431  * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1432  * @dentry: dentry of the parent directory
1433  * @str:    qstr of name whose hash we should fill in
1434  *
1435  * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1436  */
1437 static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1438 {
1439     const struct inode *dir = READ_ONCE(dentry->d_inode);
1440     struct super_block *sb = dentry->d_sb;
1441     const struct unicode_map *um = sb->s_encoding;
1442     int ret = 0;
1443 
1444     if (!dir || !needs_casefold(dir))
1445         return 0;
1446 
1447     ret = utf8_casefold_hash(um, dentry, str);
1448     if (ret < 0 && sb_has_strict_encoding(sb))
1449         return -EINVAL;
1450     return 0;
1451 }
1452 
1453 static const struct dentry_operations generic_ci_dentry_ops = {
1454     .d_hash = generic_ci_d_hash,
1455     .d_compare = generic_ci_d_compare,
1456 };
1457 #endif
1458 
1459 #ifdef CONFIG_FS_ENCRYPTION
1460 static const struct dentry_operations generic_encrypted_dentry_ops = {
1461     .d_revalidate = fscrypt_d_revalidate,
1462 };
1463 #endif
1464 
1465 #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1466 static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1467     .d_hash = generic_ci_d_hash,
1468     .d_compare = generic_ci_d_compare,
1469     .d_revalidate = fscrypt_d_revalidate,
1470 };
1471 #endif
1472 
1473 /**
1474  * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1475  * @dentry: dentry to set ops on
1476  *
1477  * Casefolded directories need d_hash and d_compare set, so that the dentries
1478  * contained in them are handled case-insensitively.  Note that these operations
1479  * are needed on the parent directory rather than on the dentries in it, and
1480  * while the casefolding flag can be toggled on and off on an empty directory,
1481  * dentry_operations can't be changed later.  As a result, if the filesystem has
1482  * casefolding support enabled at all, we have to give all dentries the
1483  * casefolding operations even if their inode doesn't have the casefolding flag
1484  * currently (and thus the casefolding ops would be no-ops for now).
1485  *
1486  * Encryption works differently in that the only dentry operation it needs is
1487  * d_revalidate, which it only needs on dentries that have the no-key name flag.
1488  * The no-key flag can't be set "later", so we don't have to worry about that.
1489  *
1490  * Finally, to maximize compatibility with overlayfs (which isn't compatible
1491  * with certain dentry operations) and to avoid taking an unnecessary
1492  * performance hit, we use custom dentry_operations for each possible
1493  * combination rather than always installing all operations.
1494  */
1495 void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
1496 {
1497 #ifdef CONFIG_FS_ENCRYPTION
1498     bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
1499 #endif
1500 #if IS_ENABLED(CONFIG_UNICODE)
1501     bool needs_ci_ops = dentry->d_sb->s_encoding;
1502 #endif
1503 #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1504     if (needs_encrypt_ops && needs_ci_ops) {
1505         d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1506         return;
1507     }
1508 #endif
1509 #ifdef CONFIG_FS_ENCRYPTION
1510     if (needs_encrypt_ops) {
1511         d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1512         return;
1513     }
1514 #endif
1515 #if IS_ENABLED(CONFIG_UNICODE)
1516     if (needs_ci_ops) {
1517         d_set_d_op(dentry, &generic_ci_dentry_ops);
1518         return;
1519     }
1520 #endif
1521 }
1522 EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);