1
2
3
4
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>
20#include <linux/fs_context.h>
21#include <linux/pseudo_fs.h>
22
23#include <linux/uaccess.h>
24
25#include "internal.h"
26
27int simple_getattr(const struct path *path, struct kstat *stat,
28 u32 request_mask, unsigned int query_flags)
29{
30 struct inode *inode = d_inode(path->dentry);
31 generic_fillattr(inode, stat);
32 stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
33 return 0;
34}
35EXPORT_SYMBOL(simple_getattr);
36
37int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
38{
39 buf->f_type = dentry->d_sb->s_magic;
40 buf->f_bsize = PAGE_SIZE;
41 buf->f_namelen = NAME_MAX;
42 return 0;
43}
44EXPORT_SYMBOL(simple_statfs);
45
46
47
48
49
50int always_delete_dentry(const struct dentry *dentry)
51{
52 return 1;
53}
54EXPORT_SYMBOL(always_delete_dentry);
55
56const struct dentry_operations simple_dentry_operations = {
57 .d_delete = always_delete_dentry,
58};
59EXPORT_SYMBOL(simple_dentry_operations);
60
61
62
63
64
65struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
66{
67 if (dentry->d_name.len > NAME_MAX)
68 return ERR_PTR(-ENAMETOOLONG);
69 if (!dentry->d_sb->s_d_op)
70 d_set_d_op(dentry, &simple_dentry_operations);
71 d_add(dentry, NULL);
72 return NULL;
73}
74EXPORT_SYMBOL(simple_lookup);
75
76int dcache_dir_open(struct inode *inode, struct file *file)
77{
78 file->private_data = d_alloc_cursor(file->f_path.dentry);
79
80 return file->private_data ? 0 : -ENOMEM;
81}
82EXPORT_SYMBOL(dcache_dir_open);
83
84int dcache_dir_close(struct inode *inode, struct file *file)
85{
86 dput(file->private_data);
87 return 0;
88}
89EXPORT_SYMBOL(dcache_dir_close);
90
91
92static struct dentry *next_positive(struct dentry *parent,
93 struct list_head *from,
94 int count)
95{
96 unsigned *seq = &parent->d_inode->i_dir_seq, n;
97 struct dentry *res;
98 struct list_head *p;
99 bool skipped;
100 int i;
101
102retry:
103 i = count;
104 skipped = false;
105 n = smp_load_acquire(seq) & ~1;
106 res = NULL;
107 rcu_read_lock();
108 for (p = from->next; p != &parent->d_subdirs; p = p->next) {
109 struct dentry *d = list_entry(p, struct dentry, d_child);
110 if (!simple_positive(d)) {
111 skipped = true;
112 } else if (!--i) {
113 res = d;
114 break;
115 }
116 }
117 rcu_read_unlock();
118 if (skipped) {
119 smp_rmb();
120 if (unlikely(*seq != n))
121 goto retry;
122 }
123 return res;
124}
125
126static void move_cursor(struct dentry *cursor, struct list_head *after)
127{
128 struct dentry *parent = cursor->d_parent;
129 unsigned n, *seq = &parent->d_inode->i_dir_seq;
130 spin_lock(&parent->d_lock);
131 for (;;) {
132 n = *seq;
133 if (!(n & 1) && cmpxchg(seq, n, n + 1) == n)
134 break;
135 cpu_relax();
136 }
137 __list_del(cursor->d_child.prev, cursor->d_child.next);
138 if (after)
139 list_add(&cursor->d_child, after);
140 else
141 list_add_tail(&cursor->d_child, &parent->d_subdirs);
142 smp_store_release(seq, n + 2);
143 spin_unlock(&parent->d_lock);
144}
145
146loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
147{
148 struct dentry *dentry = file->f_path.dentry;
149 switch (whence) {
150 case 1:
151 offset += file->f_pos;
152
153 case 0:
154 if (offset >= 0)
155 break;
156
157 default:
158 return -EINVAL;
159 }
160 if (offset != file->f_pos) {
161 file->f_pos = offset;
162 if (file->f_pos >= 2) {
163 struct dentry *cursor = file->private_data;
164 struct dentry *to;
165 loff_t n = file->f_pos - 2;
166
167 inode_lock_shared(dentry->d_inode);
168 to = next_positive(dentry, &dentry->d_subdirs, n);
169 move_cursor(cursor, to ? &to->d_child : NULL);
170 inode_unlock_shared(dentry->d_inode);
171 }
172 }
173 return offset;
174}
175EXPORT_SYMBOL(dcache_dir_lseek);
176
177
178static inline unsigned char dt_type(struct inode *inode)
179{
180 return (inode->i_mode >> 12) & 15;
181}
182
183
184
185
186
187
188
189int dcache_readdir(struct file *file, struct dir_context *ctx)
190{
191 struct dentry *dentry = file->f_path.dentry;
192 struct dentry *cursor = file->private_data;
193 struct list_head *p = &cursor->d_child;
194 struct dentry *next;
195 bool moved = false;
196
197 if (!dir_emit_dots(file, ctx))
198 return 0;
199
200 if (ctx->pos == 2)
201 p = &dentry->d_subdirs;
202 while ((next = next_positive(dentry, p, 1)) != NULL) {
203 if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
204 d_inode(next)->i_ino, dt_type(d_inode(next))))
205 break;
206 moved = true;
207 p = &next->d_child;
208 ctx->pos++;
209 }
210 if (moved)
211 move_cursor(cursor, p);
212 return 0;
213}
214EXPORT_SYMBOL(dcache_readdir);
215
216ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
217{
218 return -EISDIR;
219}
220EXPORT_SYMBOL(generic_read_dir);
221
222const struct file_operations simple_dir_operations = {
223 .open = dcache_dir_open,
224 .release = dcache_dir_close,
225 .llseek = dcache_dir_lseek,
226 .read = generic_read_dir,
227 .iterate_shared = dcache_readdir,
228 .fsync = noop_fsync,
229};
230EXPORT_SYMBOL(simple_dir_operations);
231
232const struct inode_operations simple_dir_inode_operations = {
233 .lookup = simple_lookup,
234};
235EXPORT_SYMBOL(simple_dir_inode_operations);
236
237static const struct super_operations simple_super_operations = {
238 .statfs = simple_statfs,
239};
240
241static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
242{
243 struct pseudo_fs_context *ctx = fc->fs_private;
244 struct inode *root;
245
246 s->s_maxbytes = MAX_LFS_FILESIZE;
247 s->s_blocksize = PAGE_SIZE;
248 s->s_blocksize_bits = PAGE_SHIFT;
249 s->s_magic = ctx->magic;
250 s->s_op = ctx->ops ?: &simple_super_operations;
251 s->s_xattr = ctx->xattr;
252 s->s_time_gran = 1;
253 root = new_inode(s);
254 if (!root)
255 return -ENOMEM;
256
257
258
259
260
261
262 root->i_ino = 1;
263 root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
264 root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
265 s->s_root = d_make_root(root);
266 if (!s->s_root)
267 return -ENOMEM;
268 s->s_d_op = ctx->dops;
269 return 0;
270}
271
272static int pseudo_fs_get_tree(struct fs_context *fc)
273{
274 return get_tree_nodev(fc, pseudo_fs_fill_super);
275}
276
277static void pseudo_fs_free(struct fs_context *fc)
278{
279 kfree(fc->fs_private);
280}
281
282static const struct fs_context_operations pseudo_fs_context_ops = {
283 .free = pseudo_fs_free,
284 .get_tree = pseudo_fs_get_tree,
285};
286
287
288
289
290
291struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
292 unsigned long magic)
293{
294 struct pseudo_fs_context *ctx;
295
296 ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
297 if (likely(ctx)) {
298 ctx->magic = magic;
299 fc->fs_private = ctx;
300 fc->ops = &pseudo_fs_context_ops;
301 fc->sb_flags |= SB_NOUSER;
302 fc->global = true;
303 }
304 return ctx;
305}
306EXPORT_SYMBOL(init_pseudo);
307
308int simple_open(struct inode *inode, struct file *file)
309{
310 if (inode->i_private)
311 file->private_data = inode->i_private;
312 return 0;
313}
314EXPORT_SYMBOL(simple_open);
315
316int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
317{
318 struct inode *inode = d_inode(old_dentry);
319
320 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
321 inc_nlink(inode);
322 ihold(inode);
323 dget(dentry);
324 d_instantiate(dentry, inode);
325 return 0;
326}
327EXPORT_SYMBOL(simple_link);
328
329int simple_empty(struct dentry *dentry)
330{
331 struct dentry *child;
332 int ret = 0;
333
334 spin_lock(&dentry->d_lock);
335 list_for_each_entry(child, &dentry->d_subdirs, d_child) {
336 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
337 if (simple_positive(child)) {
338 spin_unlock(&child->d_lock);
339 goto out;
340 }
341 spin_unlock(&child->d_lock);
342 }
343 ret = 1;
344out:
345 spin_unlock(&dentry->d_lock);
346 return ret;
347}
348EXPORT_SYMBOL(simple_empty);
349
350int simple_unlink(struct inode *dir, struct dentry *dentry)
351{
352 struct inode *inode = d_inode(dentry);
353
354 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
355 drop_nlink(inode);
356 dput(dentry);
357 return 0;
358}
359EXPORT_SYMBOL(simple_unlink);
360
361int simple_rmdir(struct inode *dir, struct dentry *dentry)
362{
363 if (!simple_empty(dentry))
364 return -ENOTEMPTY;
365
366 drop_nlink(d_inode(dentry));
367 simple_unlink(dir, dentry);
368 drop_nlink(dir);
369 return 0;
370}
371EXPORT_SYMBOL(simple_rmdir);
372
373int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
374 struct inode *new_dir, struct dentry *new_dentry,
375 unsigned int flags)
376{
377 struct inode *inode = d_inode(old_dentry);
378 int they_are_dirs = d_is_dir(old_dentry);
379
380 if (flags & ~RENAME_NOREPLACE)
381 return -EINVAL;
382
383 if (!simple_empty(new_dentry))
384 return -ENOTEMPTY;
385
386 if (d_really_is_positive(new_dentry)) {
387 simple_unlink(new_dir, new_dentry);
388 if (they_are_dirs) {
389 drop_nlink(d_inode(new_dentry));
390 drop_nlink(old_dir);
391 }
392 } else if (they_are_dirs) {
393 drop_nlink(old_dir);
394 inc_nlink(new_dir);
395 }
396
397 old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
398 new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
399
400 return 0;
401}
402EXPORT_SYMBOL(simple_rename);
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418int simple_setattr(struct dentry *dentry, struct iattr *iattr)
419{
420 struct inode *inode = d_inode(dentry);
421 int error;
422
423 error = setattr_prepare(dentry, iattr);
424 if (error)
425 return error;
426
427 if (iattr->ia_valid & ATTR_SIZE)
428 truncate_setsize(inode, iattr->ia_size);
429 setattr_copy(inode, iattr);
430 mark_inode_dirty(inode);
431 return 0;
432}
433EXPORT_SYMBOL(simple_setattr);
434
435int simple_readpage(struct file *file, struct page *page)
436{
437 clear_highpage(page);
438 flush_dcache_page(page);
439 SetPageUptodate(page);
440 unlock_page(page);
441 return 0;
442}
443EXPORT_SYMBOL(simple_readpage);
444
445int simple_write_begin(struct file *file, struct address_space *mapping,
446 loff_t pos, unsigned len, unsigned flags,
447 struct page **pagep, void **fsdata)
448{
449 struct page *page;
450 pgoff_t index;
451
452 index = pos >> PAGE_SHIFT;
453
454 page = grab_cache_page_write_begin(mapping, index, flags);
455 if (!page)
456 return -ENOMEM;
457
458 *pagep = page;
459
460 if (!PageUptodate(page) && (len != PAGE_SIZE)) {
461 unsigned from = pos & (PAGE_SIZE - 1);
462
463 zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
464 }
465 return 0;
466}
467EXPORT_SYMBOL(simple_write_begin);
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492int simple_write_end(struct file *file, struct address_space *mapping,
493 loff_t pos, unsigned len, unsigned copied,
494 struct page *page, void *fsdata)
495{
496 struct inode *inode = page->mapping->host;
497 loff_t last_pos = pos + copied;
498
499
500 if (!PageUptodate(page)) {
501 if (copied < len) {
502 unsigned from = pos & (PAGE_SIZE - 1);
503
504 zero_user(page, from + copied, len - copied);
505 }
506 SetPageUptodate(page);
507 }
508
509
510
511
512 if (last_pos > inode->i_size)
513 i_size_write(inode, last_pos);
514
515 set_page_dirty(page);
516 unlock_page(page);
517 put_page(page);
518
519 return copied;
520}
521EXPORT_SYMBOL(simple_write_end);
522
523
524
525
526
527
528int simple_fill_super(struct super_block *s, unsigned long magic,
529 const struct tree_descr *files)
530{
531 struct inode *inode;
532 struct dentry *root;
533 struct dentry *dentry;
534 int i;
535
536 s->s_blocksize = PAGE_SIZE;
537 s->s_blocksize_bits = PAGE_SHIFT;
538 s->s_magic = magic;
539 s->s_op = &simple_super_operations;
540 s->s_time_gran = 1;
541
542 inode = new_inode(s);
543 if (!inode)
544 return -ENOMEM;
545
546
547
548
549 inode->i_ino = 1;
550 inode->i_mode = S_IFDIR | 0755;
551 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
552 inode->i_op = &simple_dir_inode_operations;
553 inode->i_fop = &simple_dir_operations;
554 set_nlink(inode, 2);
555 root = d_make_root(inode);
556 if (!root)
557 return -ENOMEM;
558 for (i = 0; !files->name || files->name[0]; i++, files++) {
559 if (!files->name)
560 continue;
561
562
563 if (unlikely(i == 1))
564 printk(KERN_WARNING "%s: %s passed in a files array"
565 "with an index of 1!\n", __func__,
566 s->s_type->name);
567
568 dentry = d_alloc_name(root, files->name);
569 if (!dentry)
570 goto out;
571 inode = new_inode(s);
572 if (!inode) {
573 dput(dentry);
574 goto out;
575 }
576 inode->i_mode = S_IFREG | files->mode;
577 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
578 inode->i_fop = files->ops;
579 inode->i_ino = i;
580 d_add(dentry, inode);
581 }
582 s->s_root = root;
583 return 0;
584out:
585 d_genocide(root);
586 shrink_dcache_parent(root);
587 dput(root);
588 return -ENOMEM;
589}
590EXPORT_SYMBOL(simple_fill_super);
591
592static DEFINE_SPINLOCK(pin_fs_lock);
593
594int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
595{
596 struct vfsmount *mnt = NULL;
597 spin_lock(&pin_fs_lock);
598 if (unlikely(!*mount)) {
599 spin_unlock(&pin_fs_lock);
600 mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
601 if (IS_ERR(mnt))
602 return PTR_ERR(mnt);
603 spin_lock(&pin_fs_lock);
604 if (!*mount)
605 *mount = mnt;
606 }
607 mntget(*mount);
608 ++*count;
609 spin_unlock(&pin_fs_lock);
610 mntput(mnt);
611 return 0;
612}
613EXPORT_SYMBOL(simple_pin_fs);
614
615void simple_release_fs(struct vfsmount **mount, int *count)
616{
617 struct vfsmount *mnt;
618 spin_lock(&pin_fs_lock);
619 mnt = *mount;
620 if (!--*count)
621 *mount = NULL;
622 spin_unlock(&pin_fs_lock);
623 mntput(mnt);
624}
625EXPORT_SYMBOL(simple_release_fs);
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
642 const void *from, size_t available)
643{
644 loff_t pos = *ppos;
645 size_t ret;
646
647 if (pos < 0)
648 return -EINVAL;
649 if (pos >= available || !count)
650 return 0;
651 if (count > available - pos)
652 count = available - pos;
653 ret = copy_to_user(to, from + pos, count);
654 if (ret == count)
655 return -EFAULT;
656 count -= ret;
657 *ppos = pos + count;
658 return count;
659}
660EXPORT_SYMBOL(simple_read_from_buffer);
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
677 const void __user *from, size_t count)
678{
679 loff_t pos = *ppos;
680 size_t res;
681
682 if (pos < 0)
683 return -EINVAL;
684 if (pos >= available || !count)
685 return 0;
686 if (count > available - pos)
687 count = available - pos;
688 res = copy_from_user(to + pos, from, count);
689 if (res == count)
690 return -EFAULT;
691 count -= res;
692 *ppos = pos + count;
693 return count;
694}
695EXPORT_SYMBOL(simple_write_to_buffer);
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
712 const void *from, size_t available)
713{
714 loff_t pos = *ppos;
715
716 if (pos < 0)
717 return -EINVAL;
718 if (pos >= available)
719 return 0;
720 if (count > available - pos)
721 count = available - pos;
722 memcpy(to, from + pos, count);
723 *ppos = pos + count;
724
725 return count;
726}
727EXPORT_SYMBOL(memory_read_from_buffer);
728
729
730
731
732
733
734
735
736void simple_transaction_set(struct file *file, size_t n)
737{
738 struct simple_transaction_argresp *ar = file->private_data;
739
740 BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
741
742
743
744
745
746 smp_mb();
747 ar->size = n;
748}
749EXPORT_SYMBOL(simple_transaction_set);
750
751char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
752{
753 struct simple_transaction_argresp *ar;
754 static DEFINE_SPINLOCK(simple_transaction_lock);
755
756 if (size > SIMPLE_TRANSACTION_LIMIT - 1)
757 return ERR_PTR(-EFBIG);
758
759 ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
760 if (!ar)
761 return ERR_PTR(-ENOMEM);
762
763 spin_lock(&simple_transaction_lock);
764
765
766 if (file->private_data) {
767 spin_unlock(&simple_transaction_lock);
768 free_page((unsigned long)ar);
769 return ERR_PTR(-EBUSY);
770 }
771
772 file->private_data = ar;
773
774 spin_unlock(&simple_transaction_lock);
775
776 if (copy_from_user(ar->data, buf, size))
777 return ERR_PTR(-EFAULT);
778
779 return ar->data;
780}
781EXPORT_SYMBOL(simple_transaction_get);
782
783ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
784{
785 struct simple_transaction_argresp *ar = file->private_data;
786
787 if (!ar)
788 return 0;
789 return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
790}
791EXPORT_SYMBOL(simple_transaction_read);
792
793int simple_transaction_release(struct inode *inode, struct file *file)
794{
795 free_page((unsigned long)file->private_data);
796 return 0;
797}
798EXPORT_SYMBOL(simple_transaction_release);
799
800
801
802struct simple_attr {
803 int (*get)(void *, u64 *);
804 int (*set)(void *, u64);
805 char get_buf[24];
806 char set_buf[24];
807 void *data;
808 const char *fmt;
809 struct mutex mutex;
810};
811
812
813
814int simple_attr_open(struct inode *inode, struct file *file,
815 int (*get)(void *, u64 *), int (*set)(void *, u64),
816 const char *fmt)
817{
818 struct simple_attr *attr;
819
820 attr = kmalloc(sizeof(*attr), GFP_KERNEL);
821 if (!attr)
822 return -ENOMEM;
823
824 attr->get = get;
825 attr->set = set;
826 attr->data = inode->i_private;
827 attr->fmt = fmt;
828 mutex_init(&attr->mutex);
829
830 file->private_data = attr;
831
832 return nonseekable_open(inode, file);
833}
834EXPORT_SYMBOL_GPL(simple_attr_open);
835
836int simple_attr_release(struct inode *inode, struct file *file)
837{
838 kfree(file->private_data);
839 return 0;
840}
841EXPORT_SYMBOL_GPL(simple_attr_release);
842
843
844ssize_t simple_attr_read(struct file *file, char __user *buf,
845 size_t len, loff_t *ppos)
846{
847 struct simple_attr *attr;
848 size_t size;
849 ssize_t ret;
850
851 attr = file->private_data;
852
853 if (!attr->get)
854 return -EACCES;
855
856 ret = mutex_lock_interruptible(&attr->mutex);
857 if (ret)
858 return ret;
859
860 if (*ppos) {
861 size = strlen(attr->get_buf);
862 } else {
863 u64 val;
864 ret = attr->get(attr->data, &val);
865 if (ret)
866 goto out;
867
868 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
869 attr->fmt, (unsigned long long)val);
870 }
871
872 ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
873out:
874 mutex_unlock(&attr->mutex);
875 return ret;
876}
877EXPORT_SYMBOL_GPL(simple_attr_read);
878
879
880ssize_t simple_attr_write(struct file *file, const char __user *buf,
881 size_t len, loff_t *ppos)
882{
883 struct simple_attr *attr;
884 u64 val;
885 size_t size;
886 ssize_t ret;
887
888 attr = file->private_data;
889 if (!attr->set)
890 return -EACCES;
891
892 ret = mutex_lock_interruptible(&attr->mutex);
893 if (ret)
894 return ret;
895
896 ret = -EFAULT;
897 size = min(sizeof(attr->set_buf) - 1, len);
898 if (copy_from_user(attr->set_buf, buf, size))
899 goto out;
900
901 attr->set_buf[size] = '\0';
902 val = simple_strtoll(attr->set_buf, NULL, 0);
903 ret = attr->set(attr->data, val);
904 if (ret == 0)
905 ret = len;
906out:
907 mutex_unlock(&attr->mutex);
908 return ret;
909}
910EXPORT_SYMBOL_GPL(simple_attr_write);
911
912
913
914
915
916
917
918
919
920
921
922
923
924struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
925 int fh_len, int fh_type, struct inode *(*get_inode)
926 (struct super_block *sb, u64 ino, u32 gen))
927{
928 struct inode *inode = NULL;
929
930 if (fh_len < 2)
931 return NULL;
932
933 switch (fh_type) {
934 case FILEID_INO32_GEN:
935 case FILEID_INO32_GEN_PARENT:
936 inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
937 break;
938 }
939
940 return d_obtain_alias(inode);
941}
942EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
958 int fh_len, int fh_type, struct inode *(*get_inode)
959 (struct super_block *sb, u64 ino, u32 gen))
960{
961 struct inode *inode = NULL;
962
963 if (fh_len <= 2)
964 return NULL;
965
966 switch (fh_type) {
967 case FILEID_INO32_GEN_PARENT:
968 inode = get_inode(sb, fid->i32.parent_ino,
969 (fh_len > 3 ? fid->i32.parent_gen : 0));
970 break;
971 }
972
973 return d_obtain_alias(inode);
974}
975EXPORT_SYMBOL_GPL(generic_fh_to_parent);
976
977
978
979
980
981
982
983
984
985
986
987
988
989int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
990 int datasync)
991{
992 struct inode *inode = file->f_mapping->host;
993 int err;
994 int ret;
995
996 err = file_write_and_wait_range(file, start, end);
997 if (err)
998 return err;
999
1000 inode_lock(inode);
1001 ret = sync_mapping_buffers(inode->i_mapping);
1002 if (!(inode->i_state & I_DIRTY_ALL))
1003 goto out;
1004 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
1005 goto out;
1006
1007 err = sync_inode_metadata(inode, 1);
1008 if (ret == 0)
1009 ret = err;
1010
1011out:
1012 inode_unlock(inode);
1013
1014 err = file_check_and_advance_wb_err(file);
1015 if (ret == 0)
1016 ret = err;
1017 return ret;
1018}
1019EXPORT_SYMBOL(__generic_file_fsync);
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1032 int datasync)
1033{
1034 struct inode *inode = file->f_mapping->host;
1035 int err;
1036
1037 err = __generic_file_fsync(file, start, end, datasync);
1038 if (err)
1039 return err;
1040 return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
1041}
1042EXPORT_SYMBOL(generic_file_fsync);
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1054{
1055 u64 last_fs_block = num_blocks - 1;
1056 u64 last_fs_page =
1057 last_fs_block >> (PAGE_SHIFT - blocksize_bits);
1058
1059 if (unlikely(num_blocks == 0))
1060 return 0;
1061
1062 if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
1063 return -EINVAL;
1064
1065 if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1066 (last_fs_page > (pgoff_t)(~0ULL))) {
1067 return -EFBIG;
1068 }
1069 return 0;
1070}
1071EXPORT_SYMBOL(generic_check_addressable);
1072
1073
1074
1075
1076int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1077{
1078 return 0;
1079}
1080EXPORT_SYMBOL(noop_fsync);
1081
1082int noop_set_page_dirty(struct page *page)
1083{
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 return 0;
1095}
1096EXPORT_SYMBOL_GPL(noop_set_page_dirty);
1097
1098void noop_invalidatepage(struct page *page, unsigned int offset,
1099 unsigned int length)
1100{
1101
1102
1103
1104
1105
1106}
1107EXPORT_SYMBOL_GPL(noop_invalidatepage);
1108
1109ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1110{
1111
1112
1113
1114
1115
1116
1117 return -EINVAL;
1118}
1119EXPORT_SYMBOL_GPL(noop_direct_IO);
1120
1121
1122void kfree_link(void *p)
1123{
1124 kfree(p);
1125}
1126EXPORT_SYMBOL(kfree_link);
1127
1128
1129
1130
1131
1132static int anon_set_page_dirty(struct page *page)
1133{
1134 return 0;
1135};
1136
1137
1138
1139
1140
1141
1142struct inode *alloc_anon_inode(struct super_block *s)
1143{
1144 static const struct address_space_operations anon_aops = {
1145 .set_page_dirty = anon_set_page_dirty,
1146 };
1147 struct inode *inode = new_inode_pseudo(s);
1148
1149 if (!inode)
1150 return ERR_PTR(-ENOMEM);
1151
1152 inode->i_ino = get_next_ino();
1153 inode->i_mapping->a_ops = &anon_aops;
1154
1155
1156
1157
1158
1159
1160
1161 inode->i_state = I_DIRTY;
1162 inode->i_mode = S_IRUSR | S_IWUSR;
1163 inode->i_uid = current_fsuid();
1164 inode->i_gid = current_fsgid();
1165 inode->i_flags |= S_PRIVATE;
1166 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1167 return inode;
1168}
1169EXPORT_SYMBOL(alloc_anon_inode);
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181int
1182simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1183 void **priv)
1184{
1185 return -EINVAL;
1186}
1187EXPORT_SYMBOL(simple_nosetlease);
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1204 struct delayed_call *done)
1205{
1206 return inode->i_link;
1207}
1208EXPORT_SYMBOL(simple_get_link);
1209
1210const struct inode_operations simple_symlink_inode_operations = {
1211 .get_link = simple_get_link,
1212};
1213EXPORT_SYMBOL(simple_symlink_inode_operations);
1214
1215
1216
1217
1218static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1219{
1220 return ERR_PTR(-ENOENT);
1221}
1222
1223static int empty_dir_getattr(const struct path *path, struct kstat *stat,
1224 u32 request_mask, unsigned int query_flags)
1225{
1226 struct inode *inode = d_inode(path->dentry);
1227 generic_fillattr(inode, stat);
1228 return 0;
1229}
1230
1231static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
1232{
1233 return -EPERM;
1234}
1235
1236static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1237{
1238 return -EOPNOTSUPP;
1239}
1240
1241static const struct inode_operations empty_dir_inode_operations = {
1242 .lookup = empty_dir_lookup,
1243 .permission = generic_permission,
1244 .setattr = empty_dir_setattr,
1245 .getattr = empty_dir_getattr,
1246 .listxattr = empty_dir_listxattr,
1247};
1248
1249static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1250{
1251
1252 return generic_file_llseek_size(file, offset, whence, 2, 2);
1253}
1254
1255static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1256{
1257 dir_emit_dots(file, ctx);
1258 return 0;
1259}
1260
1261static const struct file_operations empty_dir_operations = {
1262 .llseek = empty_dir_llseek,
1263 .read = generic_read_dir,
1264 .iterate_shared = empty_dir_readdir,
1265 .fsync = noop_fsync,
1266};
1267
1268
1269void make_empty_dir_inode(struct inode *inode)
1270{
1271 set_nlink(inode, 2);
1272 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1273 inode->i_uid = GLOBAL_ROOT_UID;
1274 inode->i_gid = GLOBAL_ROOT_GID;
1275 inode->i_rdev = 0;
1276 inode->i_size = 0;
1277 inode->i_blkbits = PAGE_SHIFT;
1278 inode->i_blocks = 0;
1279
1280 inode->i_op = &empty_dir_inode_operations;
1281 inode->i_opflags &= ~IOP_XATTR;
1282 inode->i_fop = &empty_dir_operations;
1283}
1284
1285bool is_empty_dir_inode(struct inode *inode)
1286{
1287 return (inode->i_fop == &empty_dir_operations) &&
1288 (inode->i_op == &empty_dir_inode_operations);
1289}
1290