1 // 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
simple_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)29 int 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 }
37 EXPORT_SYMBOL(simple_getattr);
38
simple_statfs(struct dentry * dentry,struct kstatfs * buf)39 int 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 }
46 EXPORT_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 */
always_delete_dentry(const struct dentry * dentry)52 int always_delete_dentry(const struct dentry *dentry)
53 {
54 return 1;
55 }
56 EXPORT_SYMBOL(always_delete_dentry);
57
58 const struct dentry_operations simple_dentry_operations = {
59 .d_delete = always_delete_dentry,
60 };
61 EXPORT_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 */
simple_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)67 struct 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 }
76 EXPORT_SYMBOL(simple_lookup);
77
dcache_dir_open(struct inode * inode,struct file * file)78 int 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 }
84 EXPORT_SYMBOL(dcache_dir_open);
85
dcache_dir_close(struct inode * inode,struct file * file)86 int dcache_dir_close(struct inode *inode, struct file *file)
87 {
88 dput(file->private_data);
89 return 0;
90 }
91 EXPORT_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 */
scan_positives(struct dentry * cursor,struct list_head * p,loff_t count,struct dentry * last)100 static 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
dcache_dir_lseek(struct file * file,loff_t offset,int whence)135 loff_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 }
172 EXPORT_SYMBOL(dcache_dir_lseek);
173
174 /* Relationship between i_mode and the DT_xxx types */
dt_type(struct inode * inode)175 static 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
dcache_readdir(struct file * file,struct dir_context * ctx)186 int 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 }
221 EXPORT_SYMBOL(dcache_readdir);
222
generic_read_dir(struct file * filp,char __user * buf,size_t siz,loff_t * ppos)223 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
224 {
225 return -EISDIR;
226 }
227 EXPORT_SYMBOL(generic_read_dir);
228
229 const 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 };
237 EXPORT_SYMBOL(simple_dir_operations);
238
239 const struct inode_operations simple_dir_inode_operations = {
240 .lookup = simple_lookup,
241 };
242 EXPORT_SYMBOL(simple_dir_inode_operations);
243
244 static const struct super_operations simple_super_operations = {
245 .statfs = simple_statfs,
246 };
247
pseudo_fs_fill_super(struct super_block * s,struct fs_context * fc)248 static 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
pseudo_fs_get_tree(struct fs_context * fc)279 static int pseudo_fs_get_tree(struct fs_context *fc)
280 {
281 return get_tree_nodev(fc, pseudo_fs_fill_super);
282 }
283
pseudo_fs_free(struct fs_context * fc)284 static void pseudo_fs_free(struct fs_context *fc)
285 {
286 kfree(fc->fs_private);
287 }
288
289 static 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 */
init_pseudo(struct fs_context * fc,unsigned long magic)298 struct 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 }
313 EXPORT_SYMBOL(init_pseudo);
314
simple_open(struct inode * inode,struct file * file)315 int 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 }
321 EXPORT_SYMBOL(simple_open);
322
simple_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)323 int 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 }
334 EXPORT_SYMBOL(simple_link);
335
simple_empty(struct dentry * dentry)336 int 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;
351 out:
352 spin_unlock(&dentry->d_lock);
353 return ret;
354 }
355 EXPORT_SYMBOL(simple_empty);
356
simple_unlink(struct inode * dir,struct dentry * dentry)357 int 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 }
366 EXPORT_SYMBOL(simple_unlink);
367
simple_rmdir(struct inode * dir,struct dentry * dentry)368 int 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 }
378 EXPORT_SYMBOL(simple_rmdir);
379
simple_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)380 int 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 }
409 EXPORT_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 */
simple_setattr(struct dentry * dentry,struct iattr * iattr)425 int 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 }
440 EXPORT_SYMBOL(simple_setattr);
441
simple_readpage(struct file * file,struct page * page)442 int 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 }
450 EXPORT_SYMBOL(simple_readpage);
451
simple_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)452 int 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 }
474 EXPORT_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 */
simple_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)498 int 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 }
527 EXPORT_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 */
simple_fill_super(struct super_block * s,unsigned long magic,const struct tree_descr * files)534 int 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;
590 out:
591 d_genocide(root);
592 shrink_dcache_parent(root);
593 dput(root);
594 return -ENOMEM;
595 }
596 EXPORT_SYMBOL(simple_fill_super);
597
598 static DEFINE_SPINLOCK(pin_fs_lock);
599
simple_pin_fs(struct file_system_type * type,struct vfsmount ** mount,int * count)600 int 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 }
619 EXPORT_SYMBOL(simple_pin_fs);
620
simple_release_fs(struct vfsmount ** mount,int * count)621 void 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 }
631 EXPORT_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 **/
simple_read_from_buffer(void __user * to,size_t count,loff_t * ppos,const void * from,size_t available)647 ssize_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 }
666 EXPORT_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 **/
simple_write_to_buffer(void * to,size_t available,loff_t * ppos,const void __user * from,size_t count)682 ssize_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 }
701 EXPORT_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 **/
memory_read_from_buffer(void * to,size_t count,loff_t * ppos,const void * from,size_t available)717 ssize_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 }
733 EXPORT_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
simple_transaction_set(struct file * file,size_t n)742 void 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 }
755 EXPORT_SYMBOL(simple_transaction_set);
756
simple_transaction_get(struct file * file,const char __user * buf,size_t size)757 char *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 }
787 EXPORT_SYMBOL(simple_transaction_get);
788
simple_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)789 ssize_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 }
797 EXPORT_SYMBOL(simple_transaction_read);
798
simple_transaction_release(struct inode * inode,struct file * file)799 int simple_transaction_release(struct inode *inode, struct file *file)
800 {
801 free_page((unsigned long)file->private_data);
802 return 0;
803 }
804 EXPORT_SYMBOL(simple_transaction_release);
805
806 /* Simple attribute files */
807
808 struct 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. */
simple_attr_open(struct inode * inode,struct file * file,int (* get)(void *,u64 *),int (* set)(void *,u64),const char * fmt)820 int 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 }
840 EXPORT_SYMBOL_GPL(simple_attr_open);
841
simple_attr_release(struct inode * inode,struct file * file)842 int simple_attr_release(struct inode *inode, struct file *file)
843 {
844 kfree(file->private_data);
845 return 0;
846 }
847 EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
848
849 /* read from the buffer that is filled with the get function */
simple_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)850 ssize_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);
881 out:
882 mutex_unlock(&attr->mutex);
883 return ret;
884 }
885 EXPORT_SYMBOL_GPL(simple_attr_read);
886
887 /* interpret the buffer as a number to call the set function with */
simple_attr_write_xsigned(struct file * file,const char __user * buf,size_t len,loff_t * ppos,bool is_signed)888 static 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 */
919 out:
920 mutex_unlock(&attr->mutex);
921 return ret;
922 }
923
simple_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)924 ssize_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 }
929 EXPORT_SYMBOL_GPL(simple_attr_write);
930
simple_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)931 ssize_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 }
936 EXPORT_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 */
generic_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type,struct inode * (* get_inode)(struct super_block * sb,u64 ino,u32 gen))950 struct 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 }
968 EXPORT_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 */
generic_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type,struct inode * (* get_inode)(struct super_block * sb,u64 ino,u32 gen))983 struct 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 }
1001 EXPORT_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 */
__generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1015 int __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
1037 out:
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 }
1045 EXPORT_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
generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1057 int 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 }
1068 EXPORT_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 */
generic_check_addressable(unsigned blocksize_bits,u64 num_blocks)1079 int 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 }
1097 EXPORT_SYMBOL(generic_check_addressable);
1098
1099 /*
1100 * No-op implementation of ->fsync for in-memory filesystems.
1101 */
noop_fsync(struct file * file,loff_t start,loff_t end,int datasync)1102 int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1103 {
1104 return 0;
1105 }
1106 EXPORT_SYMBOL(noop_fsync);
1107
noop_set_page_dirty(struct page * page)1108 int 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 }
1122 EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1123
noop_invalidatepage(struct page * page,unsigned int offset,unsigned int length)1124 void 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 }
1133 EXPORT_SYMBOL_GPL(noop_invalidatepage);
1134
noop_direct_IO(struct kiocb * iocb,struct iov_iter * iter)1135 ssize_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 }
1145 EXPORT_SYMBOL_GPL(noop_direct_IO);
1146
1147 /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
kfree_link(void * p)1148 void kfree_link(void *p)
1149 {
1150 kfree(p);
1151 }
1152 EXPORT_SYMBOL(kfree_link);
1153
1154 /*
1155 * nop .set_page_dirty method so that people can use .page_mkwrite on
1156 * anon inodes.
1157 */
anon_set_page_dirty(struct page * page)1158 static int anon_set_page_dirty(struct page *page)
1159 {
1160 return 0;
1161 };
1162
1163 /*
1164 * A single inode exists for all anon_inode files. Contrary to pipes,
1165 * anon_inode inodes have no associated per-instance data, so we need
1166 * only allocate one of them.
1167 */
alloc_anon_inode(struct super_block * s)1168 struct inode *alloc_anon_inode(struct super_block *s)
1169 {
1170 static const struct address_space_operations anon_aops = {
1171 .set_page_dirty = anon_set_page_dirty,
1172 };
1173 struct inode *inode = new_inode_pseudo(s);
1174
1175 if (!inode)
1176 return ERR_PTR(-ENOMEM);
1177
1178 inode->i_ino = get_next_ino();
1179 inode->i_mapping->a_ops = &anon_aops;
1180
1181 /*
1182 * Mark the inode dirty from the very beginning,
1183 * that way it will never be moved to the dirty
1184 * list because mark_inode_dirty() will think
1185 * that it already _is_ on the dirty list.
1186 */
1187 inode->i_state = I_DIRTY;
1188 inode->i_mode = S_IRUSR | S_IWUSR;
1189 inode->i_uid = current_fsuid();
1190 inode->i_gid = current_fsgid();
1191 inode->i_flags |= S_PRIVATE;
1192 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1193 return inode;
1194 }
1195 EXPORT_SYMBOL(alloc_anon_inode);
1196
1197 /**
1198 * simple_nosetlease - generic helper for prohibiting leases
1199 * @filp: file pointer
1200 * @arg: type of lease to obtain
1201 * @flp: new lease supplied for insertion
1202 * @priv: private data for lm_setup operation
1203 *
1204 * Generic helper for filesystems that do not wish to allow leases to be set.
1205 * All arguments are ignored and it just returns -EINVAL.
1206 */
1207 int
simple_nosetlease(struct file * filp,long arg,struct file_lock ** flp,void ** priv)1208 simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1209 void **priv)
1210 {
1211 return -EINVAL;
1212 }
1213 EXPORT_SYMBOL(simple_nosetlease);
1214
1215 /**
1216 * simple_get_link - generic helper to get the target of "fast" symlinks
1217 * @dentry: not used here
1218 * @inode: the symlink inode
1219 * @done: not used here
1220 *
1221 * Generic helper for filesystems to use for symlink inodes where a pointer to
1222 * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
1223 * since as an optimization the path lookup code uses any non-NULL ->i_link
1224 * directly, without calling ->get_link(). But ->get_link() still must be set,
1225 * to mark the inode_operations as being for a symlink.
1226 *
1227 * Return: the symlink target
1228 */
simple_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1229 const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1230 struct delayed_call *done)
1231 {
1232 return inode->i_link;
1233 }
1234 EXPORT_SYMBOL(simple_get_link);
1235
1236 const struct inode_operations simple_symlink_inode_operations = {
1237 .get_link = simple_get_link,
1238 };
1239 EXPORT_SYMBOL(simple_symlink_inode_operations);
1240
1241 /*
1242 * Operations for a permanently empty directory.
1243 */
empty_dir_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1244 static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1245 {
1246 return ERR_PTR(-ENOENT);
1247 }
1248
empty_dir_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)1249 static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1250 u32 request_mask, unsigned int query_flags)
1251 {
1252 struct inode *inode = d_inode(path->dentry);
1253 generic_fillattr(inode, stat);
1254 return 0;
1255 }
1256
empty_dir_setattr(struct dentry * dentry,struct iattr * attr)1257 static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1258 {
1259 return -EPERM;
1260 }
1261
empty_dir_listxattr(struct dentry * dentry,char * list,size_t size)1262 static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1263 {
1264 return -EOPNOTSUPP;
1265 }
1266
1267 static const struct inode_operations empty_dir_inode_operations = {
1268 .lookup = empty_dir_lookup,
1269 .permission = generic_permission,
1270 .setattr = empty_dir_setattr,
1271 .getattr = empty_dir_getattr,
1272 .listxattr = empty_dir_listxattr,
1273 };
1274
empty_dir_llseek(struct file * file,loff_t offset,int whence)1275 static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1276 {
1277 /* An empty directory has two entries . and .. at offsets 0 and 1 */
1278 return generic_file_llseek_size(file, offset, whence, 2, 2);
1279 }
1280
empty_dir_readdir(struct file * file,struct dir_context * ctx)1281 static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1282 {
1283 dir_emit_dots(file, ctx);
1284 return 0;
1285 }
1286
1287 static const struct file_operations empty_dir_operations = {
1288 .llseek = empty_dir_llseek,
1289 .read = generic_read_dir,
1290 .iterate_shared = empty_dir_readdir,
1291 .fsync = noop_fsync,
1292 };
1293
1294
make_empty_dir_inode(struct inode * inode)1295 void make_empty_dir_inode(struct inode *inode)
1296 {
1297 set_nlink(inode, 2);
1298 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1299 inode->i_uid = GLOBAL_ROOT_UID;
1300 inode->i_gid = GLOBAL_ROOT_GID;
1301 inode->i_rdev = 0;
1302 inode->i_size = 0;
1303 inode->i_blkbits = PAGE_SHIFT;
1304 inode->i_blocks = 0;
1305
1306 inode->i_op = &empty_dir_inode_operations;
1307 inode->i_opflags &= ~IOP_XATTR;
1308 inode->i_fop = &empty_dir_operations;
1309 }
1310
is_empty_dir_inode(struct inode * inode)1311 bool is_empty_dir_inode(struct inode *inode)
1312 {
1313 return (inode->i_fop == &empty_dir_operations) &&
1314 (inode->i_op == &empty_dir_inode_operations);
1315 }
1316
1317 #ifdef CONFIG_UNICODE
needs_casefold(const struct inode * dir)1318 bool needs_casefold(const struct inode *dir)
1319 {
1320 return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding &&
1321 (!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir));
1322 }
1323 EXPORT_SYMBOL(needs_casefold);
1324
generic_ci_d_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)1325 int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1326 const char *str, const struct qstr *name)
1327 {
1328 const struct dentry *parent = READ_ONCE(dentry->d_parent);
1329 const struct inode *inode = READ_ONCE(parent->d_inode);
1330 const struct super_block *sb = dentry->d_sb;
1331 const struct unicode_map *um = sb->s_encoding;
1332 struct qstr entry = QSTR_INIT(str, len);
1333 char strbuf[DNAME_INLINE_LEN];
1334 int ret;
1335
1336 if (!inode || !needs_casefold(inode))
1337 goto fallback;
1338
1339 /*
1340 * If the dentry name is stored in-line, then it may be concurrently
1341 * modified by a rename. If this happens, the VFS will eventually retry
1342 * the lookup, so it doesn't matter what ->d_compare() returns.
1343 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1344 * string. Therefore, we have to copy the name into a temporary buffer.
1345 */
1346 if (len <= DNAME_INLINE_LEN - 1) {
1347 memcpy(strbuf, str, len);
1348 strbuf[len] = 0;
1349 entry.name = strbuf;
1350 /* prevent compiler from optimizing out the temporary buffer */
1351 barrier();
1352 }
1353
1354 ret = utf8_strncasecmp(um, name, &entry);
1355 if (ret >= 0)
1356 return ret;
1357
1358 if (sb_has_enc_strict_mode(sb))
1359 return -EINVAL;
1360 fallback:
1361 if (len != name->len)
1362 return 1;
1363 return !!memcmp(str, name->name, len);
1364 }
1365 EXPORT_SYMBOL(generic_ci_d_compare);
1366
generic_ci_d_hash(const struct dentry * dentry,struct qstr * str)1367 int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1368 {
1369 const struct inode *inode = READ_ONCE(dentry->d_inode);
1370 struct super_block *sb = dentry->d_sb;
1371 const struct unicode_map *um = sb->s_encoding;
1372 int ret = 0;
1373
1374 if (!inode || !needs_casefold(inode))
1375 return 0;
1376
1377 ret = utf8_casefold_hash(um, dentry, str);
1378 if (ret < 0)
1379 goto err;
1380
1381 return 0;
1382 err:
1383 if (sb_has_enc_strict_mode(sb))
1384 ret = -EINVAL;
1385 else
1386 ret = 0;
1387 return ret;
1388 }
1389 EXPORT_SYMBOL(generic_ci_d_hash);
1390
1391 static const struct dentry_operations generic_ci_dentry_ops = {
1392 .d_hash = generic_ci_d_hash,
1393 .d_compare = generic_ci_d_compare,
1394 };
1395 #endif
1396
1397 #ifdef CONFIG_FS_ENCRYPTION
1398 static const struct dentry_operations generic_encrypted_dentry_ops = {
1399 .d_revalidate = fscrypt_d_revalidate,
1400 };
1401 #endif
1402
1403 #if IS_ENABLED(CONFIG_UNICODE) && IS_ENABLED(CONFIG_FS_ENCRYPTION)
1404 static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1405 .d_hash = generic_ci_d_hash,
1406 .d_compare = generic_ci_d_compare,
1407 .d_revalidate = fscrypt_d_revalidate,
1408 };
1409 #endif
1410
1411 /**
1412 * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1413 * @dir: parent of dentry whose ops to set
1414 * @dentry: detnry to set ops on
1415 *
1416 * This function sets the dentry ops for the given dentry to handle both
1417 * casefolding and encryption of the dentry name.
1418 */
generic_set_encrypted_ci_d_ops(struct inode * dir,struct dentry * dentry)1419 void generic_set_encrypted_ci_d_ops(struct inode *dir, struct dentry *dentry)
1420 {
1421 #ifdef CONFIG_FS_ENCRYPTION
1422 if (dentry->d_flags & DCACHE_ENCRYPTED_NAME) {
1423 #ifdef CONFIG_UNICODE
1424 if (dir->i_sb->s_encoding) {
1425 d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1426 return;
1427 }
1428 #endif
1429 d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1430 return;
1431 }
1432 #endif
1433 #ifdef CONFIG_UNICODE
1434 if (dir->i_sb->s_encoding) {
1435 d_set_d_op(dentry, &generic_ci_dentry_ops);
1436 return;
1437 }
1438 #endif
1439 }
1440 EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);
1441