blob: 78476f10075588abd441b9ad6a2464db81aca035 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * fs/libfs.c
4 * Library for filesystems writers.
5 */
6
7#include <linux/blkdev.h>
8#include <linux/export.h>
9#include <linux/pagemap.h>
10#include <linux/slab.h>
11#include <linux/cred.h>
12#include <linux/mount.h>
13#include <linux/vfs.h>
14#include <linux/quotaops.h>
15#include <linux/mutex.h>
16#include <linux/namei.h>
17#include <linux/exportfs.h>
18#include <linux/writeback.h>
19#include <linux/buffer_head.h> /* sync_mapping_buffers */
20#include <linux/fs_context.h>
21#include <linux/pseudo_fs.h>
22#include <linux/unicode.h>
23#include <linux/fscrypt.h>
24
25#include <linux/uaccess.h>
26
27#include "internal.h"
28
29int simple_getattr(const struct path *path, struct kstat *stat,
30 u32 request_mask, unsigned int query_flags)
31{
32 struct inode *inode = d_inode(path->dentry);
33 generic_fillattr(inode, stat);
34 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
35 return 0;
36}
37EXPORT_SYMBOL(simple_getattr);
38
39int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
40{
41 buf->f_type = dentry->d_sb->s_magic;
42 buf->f_bsize = PAGE_SIZE;
43 buf->f_namelen = NAME_MAX;
44 return 0;
45}
46EXPORT_SYMBOL(simple_statfs);
47
48/*
49 * Retaining negative dentries for an in-memory filesystem just wastes
50 * memory and lookup time: arrange for them to be deleted immediately.
51 */
52int always_delete_dentry(const struct dentry *dentry)
53{
54 return 1;
55}
56EXPORT_SYMBOL(always_delete_dentry);
57
58const struct dentry_operations simple_dentry_operations = {
59 .d_delete = always_delete_dentry,
60};
61EXPORT_SYMBOL(simple_dentry_operations);
62
63/*
64 * Lookup the data. This is trivial - if the dentry didn't already
65 * exist, we know it is negative. Set d_op to delete negative dentries.
66 */
67struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
68{
69 if (dentry->d_name.len > NAME_MAX)
70 return ERR_PTR(-ENAMETOOLONG);
71 if (!dentry->d_sb->s_d_op)
72 d_set_d_op(dentry, &simple_dentry_operations);
73 d_add(dentry, NULL);
74 return NULL;
75}
76EXPORT_SYMBOL(simple_lookup);
77
78int dcache_dir_open(struct inode *inode, struct file *file)
79{
80 file->private_data = d_alloc_cursor(file->f_path.dentry);
81
82 return file->private_data ? 0 : -ENOMEM;
83}
84EXPORT_SYMBOL(dcache_dir_open);
85
86int dcache_dir_close(struct inode *inode, struct file *file)
87{
88 dput(file->private_data);
89 return 0;
90}
91EXPORT_SYMBOL(dcache_dir_close);
92
93/* parent is locked at least shared */
94/*
95 * Returns an element of siblings' list.
96 * We are looking for <count>th positive after <p>; if
97 * found, dentry is grabbed and returned to caller.
98 * If no such element exists, NULL is returned.
99 */
100static struct dentry *scan_positives(struct dentry *cursor,
101 struct list_head *p,
102 loff_t count,
103 struct dentry *last)
104{
105 struct dentry *dentry = cursor->d_parent, *found = NULL;
106
107 spin_lock(&dentry->d_lock);
108 while ((p = p->next) != &dentry->d_subdirs) {
109 struct dentry *d = list_entry(p, struct dentry, d_child);
110 // we must at least skip cursors, to avoid livelocks
111 if (d->d_flags & DCACHE_DENTRY_CURSOR)
112 continue;
113 if (simple_positive(d) && !--count) {
114 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
115 if (simple_positive(d))
116 found = dget_dlock(d);
117 spin_unlock(&d->d_lock);
118 if (likely(found))
119 break;
120 count = 1;
121 }
122 if (need_resched()) {
123 list_move(&cursor->d_child, p);
124 p = &cursor->d_child;
125 spin_unlock(&dentry->d_lock);
126 cond_resched();
127 spin_lock(&dentry->d_lock);
128 }
129 }
130 spin_unlock(&dentry->d_lock);
131 dput(last);
132 return found;
133}
134
135loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
136{
137 struct dentry *dentry = file->f_path.dentry;
138 switch (whence) {
139 case 1:
140 offset += file->f_pos;
141 /* fall through */
142 case 0:
143 if (offset >= 0)
144 break;
145 /* fall through */
146 default:
147 return -EINVAL;
148 }
149 if (offset != file->f_pos) {
150 struct dentry *cursor = file->private_data;
151 struct dentry *to = NULL;
152
153 inode_lock_shared(dentry->d_inode);
154
155 if (offset > 2)
156 to = scan_positives(cursor, &dentry->d_subdirs,
157 offset - 2, NULL);
158 spin_lock(&dentry->d_lock);
159 if (to)
160 list_move(&cursor->d_child, &to->d_child);
161 else
162 list_del_init(&cursor->d_child);
163 spin_unlock(&dentry->d_lock);
164 dput(to);
165
166 file->f_pos = offset;
167
168 inode_unlock_shared(dentry->d_inode);
169 }
170 return offset;
171}
172EXPORT_SYMBOL(dcache_dir_lseek);
173
174/* Relationship between i_mode and the DT_xxx types */
175static inline unsigned char dt_type(struct inode *inode)
176{
177 return (inode->i_mode >> 12) & 15;
178}
179
180/*
181 * Directory is locked and all positive dentries in it are safe, since
182 * for ramfs-type trees they can't go away without unlink() or rmdir(),
183 * both impossible due to the lock on directory.
184 */
185
186int dcache_readdir(struct file *file, struct dir_context *ctx)
187{
188 struct dentry *dentry = file->f_path.dentry;
189 struct dentry *cursor = file->private_data;
190 struct list_head *anchor = &dentry->d_subdirs;
191 struct dentry *next = NULL;
192 struct list_head *p;
193
194 if (!dir_emit_dots(file, ctx))
195 return 0;
196
197 if (ctx->pos == 2)
198 p = anchor;
199 else if (!list_empty(&cursor->d_child))
200 p = &cursor->d_child;
201 else
202 return 0;
203
204 while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
205 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
206 d_inode(next)->i_ino, dt_type(d_inode(next))))
207 break;
208 ctx->pos++;
209 p = &next->d_child;
210 }
211 spin_lock(&dentry->d_lock);
212 if (next)
213 list_move_tail(&cursor->d_child, &next->d_child);
214 else
215 list_del_init(&cursor->d_child);
216 spin_unlock(&dentry->d_lock);
217 dput(next);
218
219 return 0;
220}
221EXPORT_SYMBOL(dcache_readdir);
222
223ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
224{
225 return -EISDIR;
226}
227EXPORT_SYMBOL(generic_read_dir);
228
229const struct file_operations simple_dir_operations = {
230 .open = dcache_dir_open,
231 .release = dcache_dir_close,
232 .llseek = dcache_dir_lseek,
233 .read = generic_read_dir,
234 .iterate_shared = dcache_readdir,
235 .fsync = noop_fsync,
236};
237EXPORT_SYMBOL(simple_dir_operations);
238
239const struct inode_operations simple_dir_inode_operations = {
240 .lookup = simple_lookup,
241};
242EXPORT_SYMBOL(simple_dir_inode_operations);
243
244static const struct super_operations simple_super_operations = {
245 .statfs = simple_statfs,
246};
247
248static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
249{
250 struct pseudo_fs_context *ctx = fc->fs_private;
251 struct inode *root;
252
253 s->s_maxbytes = MAX_LFS_FILESIZE;
254 s->s_blocksize = PAGE_SIZE;
255 s->s_blocksize_bits = PAGE_SHIFT;
256 s->s_magic = ctx->magic;
257 s->s_op = ctx->ops ?: &simple_super_operations;
258 s->s_xattr = ctx->xattr;
259 s->s_time_gran = 1;
260 root = new_inode(s);
261 if (!root)
262 return -ENOMEM;
263
264 /*
265 * since this is the first inode, make it number 1. New inodes created
266 * after this must take care not to collide with it (by passing
267 * max_reserved of 1 to iunique).
268 */
269 root->i_ino = 1;
270 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
271 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
272 s->s_root = d_make_root(root);
273 if (!s->s_root)
274 return -ENOMEM;
275 s->s_d_op = ctx->dops;
276 return 0;
277}
278
279static int pseudo_fs_get_tree(struct fs_context *fc)
280{
281 return get_tree_nodev(fc, pseudo_fs_fill_super);
282}
283
284static void pseudo_fs_free(struct fs_context *fc)
285{
286 kfree(fc->fs_private);
287}
288
289static const struct fs_context_operations pseudo_fs_context_ops = {
290 .free = pseudo_fs_free,
291 .get_tree = pseudo_fs_get_tree,
292};
293
294/*
295 * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
296 * will never be mountable)
297 */
298struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
299 unsigned long magic)
300{
301 struct pseudo_fs_context *ctx;
302
303 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
304 if (likely(ctx)) {
305 ctx->magic = magic;
306 fc->fs_private = ctx;
307 fc->ops = &pseudo_fs_context_ops;
308 fc->sb_flags |= SB_NOUSER;
309 fc->global = true;
310 }
311 return ctx;
312}
313EXPORT_SYMBOL(init_pseudo);
314
315int simple_open(struct inode *inode, struct file *file)
316{
317 if (inode->i_private)
318 file->private_data = inode->i_private;
319 return 0;
320}
321EXPORT_SYMBOL(simple_open);
322
323int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
324{
325 struct inode *inode = d_inode(old_dentry);
326
327 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
328 inc_nlink(inode);
329 ihold(inode);
330 dget(dentry);
331 d_instantiate(dentry, inode);
332 return 0;
333}
334EXPORT_SYMBOL(simple_link);
335
336int simple_empty(struct dentry *dentry)
337{
338 struct dentry *child;
339 int ret = 0;
340
341 spin_lock(&dentry->d_lock);
342 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
343 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
344 if (simple_positive(child)) {
345 spin_unlock(&child->d_lock);
346 goto out;
347 }
348 spin_unlock(&child->d_lock);
349 }
350 ret = 1;
351out:
352 spin_unlock(&dentry->d_lock);
353 return ret;
354}
355EXPORT_SYMBOL(simple_empty);
356
357int simple_unlink(struct inode *dir, struct dentry *dentry)
358{
359 struct inode *inode = d_inode(dentry);
360
361 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
362 drop_nlink(inode);
363 dput(dentry);
364 return 0;
365}
366EXPORT_SYMBOL(simple_unlink);
367
368int simple_rmdir(struct inode *dir, struct dentry *dentry)
369{
370 if (!simple_empty(dentry))
371 return -ENOTEMPTY;
372
373 drop_nlink(d_inode(dentry));
374 simple_unlink(dir, dentry);
375 drop_nlink(dir);
376 return 0;
377}
378EXPORT_SYMBOL(simple_rmdir);
379
380int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
381 struct inode *new_dir, struct dentry *new_dentry,
382 unsigned int flags)
383{
384 struct inode *inode = d_inode(old_dentry);
385 int they_are_dirs = d_is_dir(old_dentry);
386
387 if (flags & ~RENAME_NOREPLACE)
388 return -EINVAL;
389
390 if (!simple_empty(new_dentry))
391 return -ENOTEMPTY;
392
393 if (d_really_is_positive(new_dentry)) {
394 simple_unlink(new_dir, new_dentry);
395 if (they_are_dirs) {
396 drop_nlink(d_inode(new_dentry));
397 drop_nlink(old_dir);
398 }
399 } else if (they_are_dirs) {
400 drop_nlink(old_dir);
401 inc_nlink(new_dir);
402 }
403
404 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
405 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
406
407 return 0;
408}
409EXPORT_SYMBOL(simple_rename);
410
411/**
412 * simple_setattr - setattr for simple filesystem
413 * @dentry: dentry
414 * @iattr: iattr structure
415 *
416 * Returns 0 on success, -error on failure.
417 *
418 * simple_setattr is a simple ->setattr implementation without a proper
419 * implementation of size changes.
420 *
421 * It can either be used for in-memory filesystems or special files
422 * on simple regular filesystems. Anything that needs to change on-disk
423 * or wire state on size changes needs its own setattr method.
424 */
425int simple_setattr(struct dentry *dentry, struct iattr *iattr)
426{
427 struct inode *inode = d_inode(dentry);
428 int error;
429
430 error = setattr_prepare(dentry, iattr);
431 if (error)
432 return error;
433
434 if (iattr->ia_valid & ATTR_SIZE)
435 truncate_setsize(inode, iattr->ia_size);
436 setattr_copy(inode, iattr);
437 mark_inode_dirty(inode);
438 return 0;
439}
440EXPORT_SYMBOL(simple_setattr);
441
442int simple_readpage(struct file *file, struct page *page)
443{
444 clear_highpage(page);
445 flush_dcache_page(page);
446 SetPageUptodate(page);
447 unlock_page(page);
448 return 0;
449}
450EXPORT_SYMBOL(simple_readpage);
451
452int simple_write_begin(struct file *file, struct address_space *mapping,
453 loff_t pos, unsigned len, unsigned flags,
454 struct page **pagep, void **fsdata)
455{
456 struct page *page;
457 pgoff_t index;
458
459 index = pos >> PAGE_SHIFT;
460
461 page = grab_cache_page_write_begin(mapping, index, flags);
462 if (!page)
463 return -ENOMEM;
464
465 *pagep = page;
466
467 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
468 unsigned from = pos & (PAGE_SIZE - 1);
469
470 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
471 }
472 return 0;
473}
474EXPORT_SYMBOL(simple_write_begin);
475
476/**
477 * simple_write_end - .write_end helper for non-block-device FSes
478 * @file: See .write_end of address_space_operations
479 * @mapping: "
480 * @pos: "
481 * @len: "
482 * @copied: "
483 * @page: "
484 * @fsdata: "
485 *
486 * simple_write_end does the minimum needed for updating a page after writing is
487 * done. It has the same API signature as the .write_end of
488 * address_space_operations vector. So it can just be set onto .write_end for
489 * FSes that don't need any other processing. i_mutex is assumed to be held.
490 * Block based filesystems should use generic_write_end().
491 * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
492 * is not called, so a filesystem that actually does store data in .write_inode
493 * should extend on what's done here with a call to mark_inode_dirty() in the
494 * case that i_size has changed.
495 *
496 * Use *ONLY* with simple_readpage()
497 */
498int simple_write_end(struct file *file, struct address_space *mapping,
499 loff_t pos, unsigned len, unsigned copied,
500 struct page *page, void *fsdata)
501{
502 struct inode *inode = page->mapping->host;
503 loff_t last_pos = pos + copied;
504
505 /* zero the stale part of the page if we did a short copy */
506 if (!PageUptodate(page)) {
507 if (copied < len) {
508 unsigned from = pos & (PAGE_SIZE - 1);
509
510 zero_user(page, from + copied, len - copied);
511 }
512 SetPageUptodate(page);
513 }
514 /*
515 * No need to use i_size_read() here, the i_size
516 * cannot change under us because we hold the i_mutex.
517 */
518 if (last_pos > inode->i_size)
519 i_size_write(inode, last_pos);
520
521 set_page_dirty(page);
522 unlock_page(page);
523 put_page(page);
524
525 return copied;
526}
527EXPORT_SYMBOL(simple_write_end);
528
529/*
530 * the inodes created here are not hashed. If you use iunique to generate
531 * unique inode values later for this filesystem, then you must take care
532 * to pass it an appropriate max_reserved value to avoid collisions.
533 */
534int simple_fill_super(struct super_block *s, unsigned long magic,
535 const struct tree_descr *files)
536{
537 struct inode *inode;
538 struct dentry *root;
539 struct dentry *dentry;
540 int i;
541
542 s->s_blocksize = PAGE_SIZE;
543 s->s_blocksize_bits = PAGE_SHIFT;
544 s->s_magic = magic;
545 s->s_op = &simple_super_operations;
546 s->s_time_gran = 1;
547
548 inode = new_inode(s);
549 if (!inode)
550 return -ENOMEM;
551 /*
552 * because the root inode is 1, the files array must not contain an
553 * entry at index 1
554 */
555 inode->i_ino = 1;
556 inode->i_mode = S_IFDIR | 0755;
557 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
558 inode->i_op = &simple_dir_inode_operations;
559 inode->i_fop = &simple_dir_operations;
560 set_nlink(inode, 2);
561 root = d_make_root(inode);
562 if (!root)
563 return -ENOMEM;
564 for (i = 0; !files->name || files->name[0]; i++, files++) {
565 if (!files->name)
566 continue;
567
568 /* warn if it tries to conflict with the root inode */
569 if (unlikely(i == 1))
570 printk(KERN_WARNING "%s: %s passed in a files array"
571 "with an index of 1!\n", __func__,
572 s->s_type->name);
573
574 dentry = d_alloc_name(root, files->name);
575 if (!dentry)
576 goto out;
577 inode = new_inode(s);
578 if (!inode) {
579 dput(dentry);
580 goto out;
581 }
582 inode->i_mode = S_IFREG | files->mode;
583 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
584 inode->i_fop = files->ops;
585 inode->i_ino = i;
586 d_add(dentry, inode);
587 }
588 s->s_root = root;
589 return 0;
590out:
591 d_genocide(root);
592 shrink_dcache_parent(root);
593 dput(root);
594 return -ENOMEM;
595}
596EXPORT_SYMBOL(simple_fill_super);
597
598static DEFINE_SPINLOCK(pin_fs_lock);
599
600int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
601{
602 struct vfsmount *mnt = NULL;
603 spin_lock(&pin_fs_lock);
604 if (unlikely(!*mount)) {
605 spin_unlock(&pin_fs_lock);
606 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
607 if (IS_ERR(mnt))
608 return PTR_ERR(mnt);
609 spin_lock(&pin_fs_lock);
610 if (!*mount)
611 *mount = mnt;
612 }
613 mntget(*mount);
614 ++*count;
615 spin_unlock(&pin_fs_lock);
616 mntput(mnt);
617 return 0;
618}
619EXPORT_SYMBOL(simple_pin_fs);
620
621void simple_release_fs(struct vfsmount **mount, int *count)
622{
623 struct vfsmount *mnt;
624 spin_lock(&pin_fs_lock);
625 mnt = *mount;
626 if (!--*count)
627 *mount = NULL;
628 spin_unlock(&pin_fs_lock);
629 mntput(mnt);
630}
631EXPORT_SYMBOL(simple_release_fs);
632
633/**
634 * simple_read_from_buffer - copy data from the buffer to user space
635 * @to: the user space buffer to read to
636 * @count: the maximum number of bytes to read
637 * @ppos: the current position in the buffer
638 * @from: the buffer to read from
639 * @available: the size of the buffer
640 *
641 * The simple_read_from_buffer() function reads up to @count bytes from the
642 * buffer @from at offset @ppos into the user space address starting at @to.
643 *
644 * On success, the number of bytes read is returned and the offset @ppos is
645 * advanced by this number, or negative value is returned on error.
646 **/
647ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
648 const void *from, size_t available)
649{
650 loff_t pos = *ppos;
651 size_t ret;
652
653 if (pos < 0)
654 return -EINVAL;
655 if (pos >= available || !count)
656 return 0;
657 if (count > available - pos)
658 count = available - pos;
659 ret = copy_to_user(to, from + pos, count);
660 if (ret == count)
661 return -EFAULT;
662 count -= ret;
663 *ppos = pos + count;
664 return count;
665}
666EXPORT_SYMBOL(simple_read_from_buffer);
667
668/**
669 * simple_write_to_buffer - copy data from user space to the buffer
670 * @to: the buffer to write to
671 * @available: the size of the buffer
672 * @ppos: the current position in the buffer
673 * @from: the user space buffer to read from
674 * @count: the maximum number of bytes to read
675 *
676 * The simple_write_to_buffer() function reads up to @count bytes from the user
677 * space address starting at @from into the buffer @to at offset @ppos.
678 *
679 * On success, the number of bytes written is returned and the offset @ppos is
680 * advanced by this number, or negative value is returned on error.
681 **/
682ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
683 const void __user *from, size_t count)
684{
685 loff_t pos = *ppos;
686 size_t res;
687
688 if (pos < 0)
689 return -EINVAL;
690 if (pos >= available || !count)
691 return 0;
692 if (count > available - pos)
693 count = available - pos;
694 res = copy_from_user(to + pos, from, count);
695 if (res == count)
696 return -EFAULT;
697 count -= res;
698 *ppos = pos + count;
699 return count;
700}
701EXPORT_SYMBOL(simple_write_to_buffer);
702
703/**
704 * memory_read_from_buffer - copy data from the buffer
705 * @to: the kernel space buffer to read to
706 * @count: the maximum number of bytes to read
707 * @ppos: the current position in the buffer
708 * @from: the buffer to read from
709 * @available: the size of the buffer
710 *
711 * The memory_read_from_buffer() function reads up to @count bytes from the
712 * buffer @from at offset @ppos into the kernel space address starting at @to.
713 *
714 * On success, the number of bytes read is returned and the offset @ppos is
715 * advanced by this number, or negative value is returned on error.
716 **/
717ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
718 const void *from, size_t available)
719{
720 loff_t pos = *ppos;
721
722 if (pos < 0)
723 return -EINVAL;
724 if (pos >= available)
725 return 0;
726 if (count > available - pos)
727 count = available - pos;
728 memcpy(to, from + pos, count);
729 *ppos = pos + count;
730
731 return count;
732}
733EXPORT_SYMBOL(memory_read_from_buffer);
734
735/*
736 * Transaction based IO.
737 * The file expects a single write which triggers the transaction, and then
738 * possibly a read which collects the result - which is stored in a
739 * file-local buffer.
740 */
741
742void simple_transaction_set(struct file *file, size_t n)
743{
744 struct simple_transaction_argresp *ar = file->private_data;
745
746 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
747
748 /*
749 * The barrier ensures that ar->size will really remain zero until
750 * ar->data is ready for reading.
751 */
752 smp_mb();
753 ar->size = n;
754}
755EXPORT_SYMBOL(simple_transaction_set);
756
757char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
758{
759 struct simple_transaction_argresp *ar;
760 static DEFINE_SPINLOCK(simple_transaction_lock);
761
762 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
763 return ERR_PTR(-EFBIG);
764
765 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
766 if (!ar)
767 return ERR_PTR(-ENOMEM);
768
769 spin_lock(&simple_transaction_lock);
770
771 /* only one write allowed per open */
772 if (file->private_data) {
773 spin_unlock(&simple_transaction_lock);
774 free_page((unsigned long)ar);
775 return ERR_PTR(-EBUSY);
776 }
777
778 file->private_data = ar;
779
780 spin_unlock(&simple_transaction_lock);
781
782 if (copy_from_user(ar->data, buf, size))
783 return ERR_PTR(-EFAULT);
784
785 return ar->data;
786}
787EXPORT_SYMBOL(simple_transaction_get);
788
789ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
790{
791 struct simple_transaction_argresp *ar = file->private_data;
792
793 if (!ar)
794 return 0;
795 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
796}
797EXPORT_SYMBOL(simple_transaction_read);
798
799int simple_transaction_release(struct inode *inode, struct file *file)
800{
801 free_page((unsigned long)file->private_data);
802 return 0;
803}
804EXPORT_SYMBOL(simple_transaction_release);
805
806/* Simple attribute files */
807
808struct simple_attr {
809 int (*get)(void *, u64 *);
810 int (*set)(void *, u64);
811 char get_buf[24]; /* enough to store a u64 and "\n\0" */
812 char set_buf[24];
813 void *data;
814 const char *fmt; /* format for read operation */
815 struct mutex mutex; /* protects access to these buffers */
816};
817
818/* simple_attr_open is called by an actual attribute open file operation
819 * to set the attribute specific access operations. */
820int simple_attr_open(struct inode *inode, struct file *file,
821 int (*get)(void *, u64 *), int (*set)(void *, u64),
822 const char *fmt)
823{
824 struct simple_attr *attr;
825
826 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
827 if (!attr)
828 return -ENOMEM;
829
830 attr->get = get;
831 attr->set = set;
832 attr->data = inode->i_private;
833 attr->fmt = fmt;
834 mutex_init(&attr->mutex);
835
836 file->private_data = attr;
837
838 return nonseekable_open(inode, file);
839}
840EXPORT_SYMBOL_GPL(simple_attr_open);
841
842int simple_attr_release(struct inode *inode, struct file *file)
843{
844 kfree(file->private_data);
845 return 0;
846}
847EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
848
849/* read from the buffer that is filled with the get function */
850ssize_t simple_attr_read(struct file *file, char __user *buf,
851 size_t len, loff_t *ppos)
852{
853 struct simple_attr *attr;
854 size_t size;
855 ssize_t ret;
856
857 attr = file->private_data;
858
859 if (!attr->get)
860 return -EACCES;
861
862 ret = mutex_lock_interruptible(&attr->mutex);
863 if (ret)
864 return ret;
865
866 if (*ppos && attr->get_buf[0]) {
867 /* continued read */
868 size = strlen(attr->get_buf);
869 } else {
870 /* first read */
871 u64 val;
872 ret = attr->get(attr->data, &val);
873 if (ret)
874 goto out;
875
876 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
877 attr->fmt, (unsigned long long)val);
878 }
879
880 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
881out:
882 mutex_unlock(&attr->mutex);
883 return ret;
884}
885EXPORT_SYMBOL_GPL(simple_attr_read);
886
887/* interpret the buffer as a number to call the set function with */
888static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
889 size_t len, loff_t *ppos, bool is_signed)
890{
891 struct simple_attr *attr;
892 unsigned long long val;
893 size_t size;
894 ssize_t ret;
895
896 attr = file->private_data;
897 if (!attr->set)
898 return -EACCES;
899
900 ret = mutex_lock_interruptible(&attr->mutex);
901 if (ret)
902 return ret;
903
904 ret = -EFAULT;
905 size = min(sizeof(attr->set_buf) - 1, len);
906 if (copy_from_user(attr->set_buf, buf, size))
907 goto out;
908
909 attr->set_buf[size] = '\0';
910 if (is_signed)
911 ret = kstrtoll(attr->set_buf, 0, &val);
912 else
913 ret = kstrtoull(attr->set_buf, 0, &val);
914 if (ret)
915 goto out;
916 ret = attr->set(attr->data, val);
917 if (ret == 0)
918 ret = len; /* on success, claim we got the whole input */
919out:
920 mutex_unlock(&attr->mutex);
921 return ret;
922}
923
924ssize_t simple_attr_write(struct file *file, const char __user *buf,
925 size_t len, loff_t *ppos)
926{
927 return simple_attr_write_xsigned(file, buf, len, ppos, false);
928}
929EXPORT_SYMBOL_GPL(simple_attr_write);
930
931ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
932 size_t len, loff_t *ppos)
933{
934 return simple_attr_write_xsigned(file, buf, len, ppos, true);
935}
936EXPORT_SYMBOL_GPL(simple_attr_write_signed);
937
938/**
939 * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
940 * @sb: filesystem to do the file handle conversion on
941 * @fid: file handle to convert
942 * @fh_len: length of the file handle in bytes
943 * @fh_type: type of file handle
944 * @get_inode: filesystem callback to retrieve inode
945 *
946 * This function decodes @fid as long as it has one of the well-known
947 * Linux filehandle types and calls @get_inode on it to retrieve the
948 * inode for the object specified in the file handle.
949 */
950struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
951 int fh_len, int fh_type, struct inode *(*get_inode)
952 (struct super_block *sb, u64 ino, u32 gen))
953{
954 struct inode *inode = NULL;
955
956 if (fh_len < 2)
957 return NULL;
958
959 switch (fh_type) {
960 case FILEID_INO32_GEN:
961 case FILEID_INO32_GEN_PARENT:
962 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
963 break;
964 }
965
966 return d_obtain_alias(inode);
967}
968EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
969
970/**
971 * generic_fh_to_parent - generic helper for the fh_to_parent export operation
972 * @sb: filesystem to do the file handle conversion on
973 * @fid: file handle to convert
974 * @fh_len: length of the file handle in bytes
975 * @fh_type: type of file handle
976 * @get_inode: filesystem callback to retrieve inode
977 *
978 * This function decodes @fid as long as it has one of the well-known
979 * Linux filehandle types and calls @get_inode on it to retrieve the
980 * inode for the _parent_ object specified in the file handle if it
981 * is specified in the file handle, or NULL otherwise.
982 */
983struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
984 int fh_len, int fh_type, struct inode *(*get_inode)
985 (struct super_block *sb, u64 ino, u32 gen))
986{
987 struct inode *inode = NULL;
988
989 if (fh_len <= 2)
990 return NULL;
991
992 switch (fh_type) {
993 case FILEID_INO32_GEN_PARENT:
994 inode = get_inode(sb, fid->i32.parent_ino,
995 (fh_len > 3 ? fid->i32.parent_gen : 0));
996 break;
997 }
998
999 return d_obtain_alias(inode);
1000}
1001EXPORT_SYMBOL_GPL(generic_fh_to_parent);
1002
1003/**
1004 * __generic_file_fsync - generic fsync implementation for simple filesystems
1005 *
1006 * @file: file to synchronize
1007 * @start: start offset in bytes
1008 * @end: end offset in bytes (inclusive)
1009 * @datasync: only synchronize essential metadata if true
1010 *
1011 * This is a generic implementation of the fsync method for simple
1012 * filesystems which track all non-inode metadata in the buffers list
1013 * hanging off the address_space structure.
1014 */
1015int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
1016 int datasync)
1017{
1018 struct inode *inode = file->f_mapping->host;
1019 int err;
1020 int ret;
1021
1022 err = file_write_and_wait_range(file, start, end);
1023 if (err)
1024 return err;
1025
1026 inode_lock(inode);
1027 ret = sync_mapping_buffers(inode->i_mapping);
1028 if (!(inode->i_state & I_DIRTY_ALL))
1029 goto out;
1030 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
1031 goto out;
1032
1033 err = sync_inode_metadata(inode, 1);
1034 if (ret == 0)
1035 ret = err;
1036
1037out:
1038 inode_unlock(inode);
1039 /* check and advance again to catch errors after syncing out buffers */
1040 err = file_check_and_advance_wb_err(file);
1041 if (ret == 0)
1042 ret = err;
1043 return ret;
1044}
1045EXPORT_SYMBOL(__generic_file_fsync);
1046
1047/**
1048 * generic_file_fsync - generic fsync implementation for simple filesystems
1049 * with flush
1050 * @file: file to synchronize
1051 * @start: start offset in bytes
1052 * @end: end offset in bytes (inclusive)
1053 * @datasync: only synchronize essential metadata if true
1054 *
1055 */
1056
1057int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1058 int datasync)
1059{
1060 struct inode *inode = file->f_mapping->host;
1061 int err;
1062
1063 err = __generic_file_fsync(file, start, end, datasync);
1064 if (err)
1065 return err;
1066 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1067}
1068EXPORT_SYMBOL(generic_file_fsync);
1069
1070/**
1071 * generic_check_addressable - Check addressability of file system
1072 * @blocksize_bits: log of file system block size
1073 * @num_blocks: number of blocks in file system
1074 *
1075 * Determine whether a file system with @num_blocks blocks (and a
1076 * block size of 2**@blocksize_bits) is addressable by the sector_t
1077 * and page cache of the system. Return 0 if so and -EFBIG otherwise.
1078 */
1079int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1080{
1081 u64 last_fs_block = num_blocks - 1;
1082 u64 last_fs_page =
1083 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
1084
1085 if (unlikely(num_blocks == 0))
1086 return 0;
1087
1088 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
1089 return -EINVAL;
1090
1091 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1092 (last_fs_page > (pgoff_t)(~0ULL))) {
1093 return -EFBIG;
1094 }
1095 return 0;
1096}
1097EXPORT_SYMBOL(generic_check_addressable);
1098
1099/*
1100 * No-op implementation of ->fsync for in-memory filesystems.
1101 */
1102int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1103{
1104 return 0;
1105}
1106EXPORT_SYMBOL(noop_fsync);
1107
1108int noop_set_page_dirty(struct page *page)
1109{
1110 /*
1111 * Unlike __set_page_dirty_no_writeback that handles dirty page
1112 * tracking in the page object, dax does all dirty tracking in
1113 * the inode address_space in response to mkwrite faults. In the
1114 * dax case we only need to worry about potentially dirty CPU
1115 * caches, not dirty page cache pages to write back.
1116 *
1117 * This callback is defined to prevent fallback to
1118 * __set_page_dirty_buffers() in set_page_dirty().
1119 */
1120 return 0;
1121}
1122EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1123
1124void noop_invalidatepage(struct page *page, unsigned int offset,
1125 unsigned int length)
1126{
1127 /*
1128 * There is no page cache to invalidate in the dax case, however
1129 * we need this callback defined to prevent falling back to
1130 * block_invalidatepage() in do_invalidatepage().
1131 */
1132}
1133EXPORT_SYMBOL_GPL(noop_invalidatepage);
1134
1135ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1136{
1137 /*
1138 * iomap based filesystems support direct I/O without need for
1139 * this callback. However, it still needs to be set in
1140 * inode->a_ops so that open/fcntl know that direct I/O is
1141 * generally supported.
1142 */
1143 return -EINVAL;
1144}
1145EXPORT_SYMBOL_GPL(noop_direct_IO);
1146
1147/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1148void kfree_link(void *p)
1149{
1150 kfree(p);
1151}
1152EXPORT_SYMBOL(kfree_link);
1153
1154/*
1155 * nop .set_page_dirty method so that people can use .page_mkwrite on
1156 * anon inodes.
1157 */
1158static int anon_set_page_dirty(struct page *page)
1159{
1160 return 0;
1161};
1162
1163struct inode *alloc_anon_inode(struct super_block *s)
1164{
1165 static const struct address_space_operations anon_aops = {
1166 .set_page_dirty = anon_set_page_dirty,
1167 };
1168 struct inode *inode = new_inode_pseudo(s);
1169
1170 if (!inode)
1171 return ERR_PTR(-ENOMEM);
1172
1173 inode->i_ino = get_next_ino();
1174 inode->i_mapping->a_ops = &anon_aops;
1175
1176 /*
1177 * Mark the inode dirty from the very beginning,
1178 * that way it will never be moved to the dirty
1179 * list because mark_inode_dirty() will think
1180 * that it already _is_ on the dirty list.
1181 */
1182 inode->i_state = I_DIRTY;
1183 inode->i_mode = S_IRUSR | S_IWUSR;
1184 inode->i_uid = current_fsuid();
1185 inode->i_gid = current_fsgid();
1186 inode->i_flags |= S_PRIVATE;
1187 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1188 return inode;
1189}
1190EXPORT_SYMBOL(alloc_anon_inode);
1191
1192/**
1193 * simple_nosetlease - generic helper for prohibiting leases
1194 * @filp: file pointer
1195 * @arg: type of lease to obtain
1196 * @flp: new lease supplied for insertion
1197 * @priv: private data for lm_setup operation
1198 *
1199 * Generic helper for filesystems that do not wish to allow leases to be set.
1200 * All arguments are ignored and it just returns -EINVAL.
1201 */
1202int
1203simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1204 void **priv)
1205{
1206 return -EINVAL;
1207}
1208EXPORT_SYMBOL(simple_nosetlease);
1209
1210/**
1211 * simple_get_link - generic helper to get the target of "fast" symlinks
1212 * @dentry: not used here
1213 * @inode: the symlink inode
1214 * @done: not used here
1215 *
1216 * Generic helper for filesystems to use for symlink inodes where a pointer to
1217 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1218 * since as an optimization the path lookup code uses any non-NULL ->i_link
1219 * directly, without calling ->get_link(). But ->get_link() still must be set,
1220 * to mark the inode_operations as being for a symlink.
1221 *
1222 * Return: the symlink target
1223 */
1224const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1225 struct delayed_call *done)
1226{
1227 return inode->i_link;
1228}
1229EXPORT_SYMBOL(simple_get_link);
1230
1231const struct inode_operations simple_symlink_inode_operations = {
1232 .get_link = simple_get_link,
1233};
1234EXPORT_SYMBOL(simple_symlink_inode_operations);
1235
1236/*
1237 * Operations for a permanently empty directory.
1238 */
1239static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1240{
1241 return ERR_PTR(-ENOENT);
1242}
1243
1244static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1245 u32 request_mask, unsigned int query_flags)
1246{
1247 struct inode *inode = d_inode(path->dentry);
1248 generic_fillattr(inode, stat);
1249 return 0;
1250}
1251
1252static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1253{
1254 return -EPERM;
1255}
1256
1257static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1258{
1259 return -EOPNOTSUPP;
1260}
1261
1262static const struct inode_operations empty_dir_inode_operations = {
1263 .lookup = empty_dir_lookup,
1264 .permission = generic_permission,
1265 .setattr = empty_dir_setattr,
1266 .getattr = empty_dir_getattr,
1267 .listxattr = empty_dir_listxattr,
1268};
1269
1270static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1271{
1272 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1273 return generic_file_llseek_size(file, offset, whence, 2, 2);
1274}
1275
1276static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1277{
1278 dir_emit_dots(file, ctx);
1279 return 0;
1280}
1281
1282static const struct file_operations empty_dir_operations = {
1283 .llseek = empty_dir_llseek,
1284 .read = generic_read_dir,
1285 .iterate_shared = empty_dir_readdir,
1286 .fsync = noop_fsync,
1287};
1288
1289
1290void make_empty_dir_inode(struct inode *inode)
1291{
1292 set_nlink(inode, 2);
1293 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1294 inode->i_uid = GLOBAL_ROOT_UID;
1295 inode->i_gid = GLOBAL_ROOT_GID;
1296 inode->i_rdev = 0;
1297 inode->i_size = 0;
1298 inode->i_blkbits = PAGE_SHIFT;
1299 inode->i_blocks = 0;
1300
1301 inode->i_op = &empty_dir_inode_operations;
1302 inode->i_opflags &= ~IOP_XATTR;
1303 inode->i_fop = &empty_dir_operations;
1304}
1305
1306bool is_empty_dir_inode(struct inode *inode)
1307{
1308 return (inode->i_fop == &empty_dir_operations) &&
1309 (inode->i_op == &empty_dir_inode_operations);
1310}
1311
1312#ifdef CONFIG_UNICODE
1313/*
1314 * Determine if the name of a dentry should be casefolded.
1315 *
1316 * Return: if names will need casefolding
1317 */
1318static bool needs_casefold(const struct inode *dir)
1319{
1320 return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding;
1321}
1322
1323/**
1324 * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1325 * @dentry: dentry whose name we are checking against
1326 * @len: len of name of dentry
1327 * @str: str pointer to name of dentry
1328 * @name: Name to compare against
1329 *
1330 * Return: 0 if names match, 1 if mismatch, or -ERRNO
1331 */
1332static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1333 const char *str, const struct qstr *name)
1334{
1335 const struct dentry *parent = READ_ONCE(dentry->d_parent);
1336 const struct inode *dir = READ_ONCE(parent->d_inode);
1337 const struct super_block *sb = dentry->d_sb;
1338 const struct unicode_map *um = sb->s_encoding;
1339 struct qstr qstr = QSTR_INIT(str, len);
1340 char strbuf[DNAME_INLINE_LEN];
1341 int ret;
1342
1343 if (!dir || !needs_casefold(dir))
1344 goto fallback;
1345 /*
1346 * If the dentry name is stored in-line, then it may be concurrently
1347 * modified by a rename. If this happens, the VFS will eventually retry
1348 * the lookup, so it doesn't matter what ->d_compare() returns.
1349 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1350 * string. Therefore, we have to copy the name into a temporary buffer.
1351 */
1352 if (len <= DNAME_INLINE_LEN - 1) {
1353 memcpy(strbuf, str, len);
1354 strbuf[len] = 0;
1355 qstr.name = strbuf;
1356 /* prevent compiler from optimizing out the temporary buffer */
1357 barrier();
1358 }
1359 ret = utf8_strncasecmp(um, name, &qstr);
1360 if (ret >= 0)
1361 return ret;
1362
1363 if (sb_has_strict_encoding(sb))
1364 return -EINVAL;
1365fallback:
1366 if (len != name->len)
1367 return 1;
1368 return !!memcmp(str, name->name, len);
1369}
1370
1371/**
1372 * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1373 * @dentry: dentry of the parent directory
1374 * @str: qstr of name whose hash we should fill in
1375 *
1376 * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1377 */
1378static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1379{
1380 const struct inode *dir = READ_ONCE(dentry->d_inode);
1381 struct super_block *sb = dentry->d_sb;
1382 const struct unicode_map *um = sb->s_encoding;
1383 int ret = 0;
1384
1385 if (!dir || !needs_casefold(dir))
1386 return 0;
1387
1388 ret = utf8_casefold_hash(um, dentry, str);
1389 if (ret < 0 && sb_has_strict_encoding(sb))
1390 return -EINVAL;
1391 return 0;
1392}
1393
1394static const struct dentry_operations generic_ci_dentry_ops = {
1395 .d_hash = generic_ci_d_hash,
1396 .d_compare = generic_ci_d_compare,
1397};
1398#endif
1399
1400#ifdef CONFIG_FS_ENCRYPTION
1401static const struct dentry_operations generic_encrypted_dentry_ops = {
1402 .d_revalidate = fscrypt_d_revalidate,
1403};
1404#endif
1405
1406#if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
1407static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1408 .d_hash = generic_ci_d_hash,
1409 .d_compare = generic_ci_d_compare,
1410 .d_revalidate = fscrypt_d_revalidate,
1411};
1412#endif
1413
1414/**
1415 * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1416 * @dentry: dentry to set ops on
1417 *
1418 * Casefolded directories need d_hash and d_compare set, so that the dentries
1419 * contained in them are handled case-insensitively. Note that these operations
1420 * are needed on the parent directory rather than on the dentries in it, and
1421 * while the casefolding flag can be toggled on and off on an empty directory,
1422 * dentry_operations can't be changed later. As a result, if the filesystem has
1423 * casefolding support enabled at all, we have to give all dentries the
1424 * casefolding operations even if their inode doesn't have the casefolding flag
1425 * currently (and thus the casefolding ops would be no-ops for now).
1426 *
1427 * Encryption works differently in that the only dentry operation it needs is
1428 * d_revalidate, which it only needs on dentries that have the no-key name flag.
1429 * The no-key flag can't be set "later", so we don't have to worry about that.
1430 *
1431 * Finally, to maximize compatibility with overlayfs (which isn't compatible
1432 * with certain dentry operations) and to avoid taking an unnecessary
1433 * performance hit, we use custom dentry_operations for each possible
1434 * combination rather than always installing all operations.
1435 */
1436void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
1437{
1438#ifdef CONFIG_FS_ENCRYPTION
1439 bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
1440#endif
1441#ifdef CONFIG_UNICODE
1442 bool needs_ci_ops = dentry->d_sb->s_encoding;
1443#endif
1444#if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
1445 if (needs_encrypt_ops && needs_ci_ops) {
1446 d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1447 return;
1448 }
1449#endif
1450#ifdef CONFIG_FS_ENCRYPTION
1451 if (needs_encrypt_ops) {
1452 d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1453 return;
1454 }
1455#endif
1456#ifdef CONFIG_UNICODE
1457 if (needs_ci_ops) {
1458 d_set_d_op(dentry, &generic_ci_dentry_ops);
1459 return;
1460 }
1461#endif
1462}
1463EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);