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