1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * fs/libfs.c
41da177e4SLinus Torvalds * Library for filesystems writers.
51da177e4SLinus Torvalds */
61da177e4SLinus Torvalds
7ac13a829SFabian Frederick #include <linux/blkdev.h>
8630d9c47SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/pagemap.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
115b825c3aSIngo Molnar #include <linux/cred.h>
121da177e4SLinus Torvalds #include <linux/mount.h>
131da177e4SLinus Torvalds #include <linux/vfs.h>
147bb46a67Snpiggin@suse.de #include <linux/quotaops.h>
157cf34c76SIngo Molnar #include <linux/mutex.h>
1687dc800bSAl Viro #include <linux/namei.h>
172596110aSChristoph Hellwig #include <linux/exportfs.h>
185ca14835SAndrew Morton #include <linux/iversion.h>
19d5aacad5SAl Viro #include <linux/writeback.h>
20ff01bb48SAl Viro #include <linux/buffer_head.h> /* sync_mapping_buffers */
2131d6d5ceSDavid Howells #include <linux/fs_context.h>
2231d6d5ceSDavid Howells #include <linux/pseudo_fs.h>
23a3d1e7ebSAl Viro #include <linux/fsnotify.h>
24c843843eSDaniel Rosenberg #include <linux/unicode.h>
25c843843eSDaniel Rosenberg #include <linux/fscrypt.h>
267cf34c76SIngo Molnar
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
281da177e4SLinus Torvalds
29a4464dbcSAl Viro #include "internal.h"
30a4464dbcSAl Viro
simple_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)31b74d24f7SChristian Brauner int simple_getattr(struct mnt_idmap *idmap, const struct path *path,
32549c7297SChristian Brauner struct kstat *stat, u32 request_mask,
33549c7297SChristian Brauner unsigned int query_flags)
341da177e4SLinus Torvalds {
35a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
360d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
3709cbfeafSKirill A. Shutemov stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
381da177e4SLinus Torvalds return 0;
391da177e4SLinus Torvalds }
4012f38872SAl Viro EXPORT_SYMBOL(simple_getattr);
411da177e4SLinus Torvalds
simple_statfs(struct dentry * dentry,struct kstatfs * buf)42726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
431da177e4SLinus Torvalds {
44726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic;
4509cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE;
461da177e4SLinus Torvalds buf->f_namelen = NAME_MAX;
471da177e4SLinus Torvalds return 0;
481da177e4SLinus Torvalds }
4912f38872SAl Viro EXPORT_SYMBOL(simple_statfs);
501da177e4SLinus Torvalds
511da177e4SLinus Torvalds /*
521da177e4SLinus Torvalds * Retaining negative dentries for an in-memory filesystem just wastes
531da177e4SLinus Torvalds * memory and lookup time: arrange for them to be deleted immediately.
541da177e4SLinus Torvalds */
always_delete_dentry(const struct dentry * dentry)55b26d4cd3SAl Viro int always_delete_dentry(const struct dentry *dentry)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds return 1;
581da177e4SLinus Torvalds }
59b26d4cd3SAl Viro EXPORT_SYMBOL(always_delete_dentry);
60b26d4cd3SAl Viro
61b26d4cd3SAl Viro const struct dentry_operations simple_dentry_operations = {
62b26d4cd3SAl Viro .d_delete = always_delete_dentry,
63b26d4cd3SAl Viro };
64b26d4cd3SAl Viro EXPORT_SYMBOL(simple_dentry_operations);
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds /*
671da177e4SLinus Torvalds * Lookup the data. This is trivial - if the dentry didn't already
681da177e4SLinus Torvalds * exist, we know it is negative. Set d_op to delete negative dentries.
691da177e4SLinus Torvalds */
simple_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)7000cd8dd3SAl Viro struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds if (dentry->d_name.len > NAME_MAX)
731da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG);
7474931da7SAl Viro if (!dentry->d_sb->s_d_op)
75fb045adbSNick Piggin d_set_d_op(dentry, &simple_dentry_operations);
761da177e4SLinus Torvalds d_add(dentry, NULL);
771da177e4SLinus Torvalds return NULL;
781da177e4SLinus Torvalds }
7912f38872SAl Viro EXPORT_SYMBOL(simple_lookup);
801da177e4SLinus Torvalds
dcache_dir_open(struct inode * inode,struct file * file)811da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file)
821da177e4SLinus Torvalds {
83ba65dc5eSAl Viro file->private_data = d_alloc_cursor(file->f_path.dentry);
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds return file->private_data ? 0 : -ENOMEM;
861da177e4SLinus Torvalds }
8712f38872SAl Viro EXPORT_SYMBOL(dcache_dir_open);
881da177e4SLinus Torvalds
dcache_dir_close(struct inode * inode,struct file * file)891da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds dput(file->private_data);
921da177e4SLinus Torvalds return 0;
931da177e4SLinus Torvalds }
9412f38872SAl Viro EXPORT_SYMBOL(dcache_dir_close);
951da177e4SLinus Torvalds
964f42c1b5SAl Viro /* parent is locked at least shared */
97d4f4de5eSAl Viro /*
98d4f4de5eSAl Viro * Returns an element of siblings' list.
99d4f4de5eSAl Viro * We are looking for <count>th positive after <p>; if
10026b6c984SAl Viro * found, dentry is grabbed and returned to caller.
10126b6c984SAl Viro * If no such element exists, NULL is returned.
102d4f4de5eSAl Viro */
scan_positives(struct dentry * cursor,struct list_head * p,loff_t count,struct dentry * last)10326b6c984SAl Viro static struct dentry *scan_positives(struct dentry *cursor,
104d4f4de5eSAl Viro struct list_head *p,
105d4f4de5eSAl Viro loff_t count,
10626b6c984SAl Viro struct dentry *last)
1074f42c1b5SAl Viro {
108d4f4de5eSAl Viro struct dentry *dentry = cursor->d_parent, *found = NULL;
1094f42c1b5SAl Viro
110d4f4de5eSAl Viro spin_lock(&dentry->d_lock);
111d4f4de5eSAl Viro while ((p = p->next) != &dentry->d_subdirs) {
1124f42c1b5SAl Viro struct dentry *d = list_entry(p, struct dentry, d_child);
113d4f4de5eSAl Viro // we must at least skip cursors, to avoid livelocks
114d4f4de5eSAl Viro if (d->d_flags & DCACHE_DENTRY_CURSOR)
115d4f4de5eSAl Viro continue;
116d4f4de5eSAl Viro if (simple_positive(d) && !--count) {
117d4f4de5eSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
118d4f4de5eSAl Viro if (simple_positive(d))
119d4f4de5eSAl Viro found = dget_dlock(d);
120d4f4de5eSAl Viro spin_unlock(&d->d_lock);
121d4f4de5eSAl Viro if (likely(found))
1224f42c1b5SAl Viro break;
123d4f4de5eSAl Viro count = 1;
124d4f4de5eSAl Viro }
125d4f4de5eSAl Viro if (need_resched()) {
126d4f4de5eSAl Viro list_move(&cursor->d_child, p);
127d4f4de5eSAl Viro p = &cursor->d_child;
128d4f4de5eSAl Viro spin_unlock(&dentry->d_lock);
129d4f4de5eSAl Viro cond_resched();
130d4f4de5eSAl Viro spin_lock(&dentry->d_lock);
1314f42c1b5SAl Viro }
1324f42c1b5SAl Viro }
133d4f4de5eSAl Viro spin_unlock(&dentry->d_lock);
13426b6c984SAl Viro dput(last);
13526b6c984SAl Viro return found;
1364f42c1b5SAl Viro }
1374f42c1b5SAl Viro
dcache_dir_lseek(struct file * file,loff_t offset,int whence)138965c8e59SAndrew Morton loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
1391da177e4SLinus Torvalds {
1402fd6b7f5SNick Piggin struct dentry *dentry = file->f_path.dentry;
141965c8e59SAndrew Morton switch (whence) {
1421da177e4SLinus Torvalds case 1:
1431da177e4SLinus Torvalds offset += file->f_pos;
144df561f66SGustavo A. R. Silva fallthrough;
1451da177e4SLinus Torvalds case 0:
1461da177e4SLinus Torvalds if (offset >= 0)
1471da177e4SLinus Torvalds break;
148df561f66SGustavo A. R. Silva fallthrough;
1491da177e4SLinus Torvalds default:
1501da177e4SLinus Torvalds return -EINVAL;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds if (offset != file->f_pos) {
1531da177e4SLinus Torvalds struct dentry *cursor = file->private_data;
154d4f4de5eSAl Viro struct dentry *to = NULL;
1551da177e4SLinus Torvalds
156274f5b04SAl Viro inode_lock_shared(dentry->d_inode);
157d4f4de5eSAl Viro
15826b6c984SAl Viro if (offset > 2)
15926b6c984SAl Viro to = scan_positives(cursor, &dentry->d_subdirs,
16026b6c984SAl Viro offset - 2, NULL);
161d4f4de5eSAl Viro spin_lock(&dentry->d_lock);
16226b6c984SAl Viro if (to)
16326b6c984SAl Viro list_move(&cursor->d_child, &to->d_child);
16426b6c984SAl Viro else
165d4f4de5eSAl Viro list_del_init(&cursor->d_child);
166d4f4de5eSAl Viro spin_unlock(&dentry->d_lock);
167d4f4de5eSAl Viro dput(to);
168d4f4de5eSAl Viro
16926b6c984SAl Viro file->f_pos = offset;
17026b6c984SAl Viro
171d4f4de5eSAl Viro inode_unlock_shared(dentry->d_inode);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds return offset;
1741da177e4SLinus Torvalds }
17512f38872SAl Viro EXPORT_SYMBOL(dcache_dir_lseek);
1761da177e4SLinus Torvalds
1771da177e4SLinus Torvalds /*
1781da177e4SLinus Torvalds * Directory is locked and all positive dentries in it are safe, since
1791da177e4SLinus Torvalds * for ramfs-type trees they can't go away without unlink() or rmdir(),
1801da177e4SLinus Torvalds * both impossible due to the lock on directory.
1811da177e4SLinus Torvalds */
1821da177e4SLinus Torvalds
dcache_readdir(struct file * file,struct dir_context * ctx)1835f99f4e7SAl Viro int dcache_readdir(struct file *file, struct dir_context *ctx)
1841da177e4SLinus Torvalds {
1855f99f4e7SAl Viro struct dentry *dentry = file->f_path.dentry;
1865f99f4e7SAl Viro struct dentry *cursor = file->private_data;
187d4f4de5eSAl Viro struct list_head *anchor = &dentry->d_subdirs;
188d4f4de5eSAl Viro struct dentry *next = NULL;
189d4f4de5eSAl Viro struct list_head *p;
1901da177e4SLinus Torvalds
1915f99f4e7SAl Viro if (!dir_emit_dots(file, ctx))
1925f99f4e7SAl Viro return 0;
1934f42c1b5SAl Viro
1945f99f4e7SAl Viro if (ctx->pos == 2)
195d4f4de5eSAl Viro p = anchor;
19626b6c984SAl Viro else if (!list_empty(&cursor->d_child))
197d4f4de5eSAl Viro p = &cursor->d_child;
19826b6c984SAl Viro else
19926b6c984SAl Viro return 0;
200d4f4de5eSAl Viro
20126b6c984SAl Viro while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
2025f99f4e7SAl Viro if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
203364595a6SJeff Layton d_inode(next)->i_ino,
204364595a6SJeff Layton fs_umode_to_dtype(d_inode(next)->i_mode)))
2054f42c1b5SAl Viro break;
2065f99f4e7SAl Viro ctx->pos++;
20726b6c984SAl Viro p = &next->d_child;
2081da177e4SLinus Torvalds }
209d4f4de5eSAl Viro spin_lock(&dentry->d_lock);
21026b6c984SAl Viro if (next)
21126b6c984SAl Viro list_move_tail(&cursor->d_child, &next->d_child);
21226b6c984SAl Viro else
21326b6c984SAl Viro list_del_init(&cursor->d_child);
214d4f4de5eSAl Viro spin_unlock(&dentry->d_lock);
215d4f4de5eSAl Viro dput(next);
216d4f4de5eSAl Viro
2171da177e4SLinus Torvalds return 0;
2181da177e4SLinus Torvalds }
21912f38872SAl Viro EXPORT_SYMBOL(dcache_readdir);
2201da177e4SLinus Torvalds
generic_read_dir(struct file * filp,char __user * buf,size_t siz,loff_t * ppos)2211da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds return -EISDIR;
2241da177e4SLinus Torvalds }
22512f38872SAl Viro EXPORT_SYMBOL(generic_read_dir);
2261da177e4SLinus Torvalds
2274b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = {
2281da177e4SLinus Torvalds .open = dcache_dir_open,
2291da177e4SLinus Torvalds .release = dcache_dir_close,
2301da177e4SLinus Torvalds .llseek = dcache_dir_lseek,
2311da177e4SLinus Torvalds .read = generic_read_dir,
2324e82901cSAl Viro .iterate_shared = dcache_readdir,
2331b061d92SChristoph Hellwig .fsync = noop_fsync,
2341da177e4SLinus Torvalds };
23512f38872SAl Viro EXPORT_SYMBOL(simple_dir_operations);
2361da177e4SLinus Torvalds
23792e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = {
2381da177e4SLinus Torvalds .lookup = simple_lookup,
2391da177e4SLinus Torvalds };
24012f38872SAl Viro EXPORT_SYMBOL(simple_dir_inode_operations);
2411da177e4SLinus Torvalds
offset_set(struct dentry * dentry,u32 offset)2426faddda6SChuck Lever static void offset_set(struct dentry *dentry, u32 offset)
2436faddda6SChuck Lever {
2446faddda6SChuck Lever dentry->d_fsdata = (void *)((uintptr_t)(offset));
2456faddda6SChuck Lever }
2466faddda6SChuck Lever
dentry2offset(struct dentry * dentry)2476faddda6SChuck Lever static u32 dentry2offset(struct dentry *dentry)
2486faddda6SChuck Lever {
2496faddda6SChuck Lever return (u32)((uintptr_t)(dentry->d_fsdata));
2506faddda6SChuck Lever }
2516faddda6SChuck Lever
252bbaef797SChuck Lever static struct lock_class_key simple_offset_xa_lock;
253bbaef797SChuck Lever
2546faddda6SChuck Lever /**
2556faddda6SChuck Lever * simple_offset_init - initialize an offset_ctx
2566faddda6SChuck Lever * @octx: directory offset map to be initialized
2576faddda6SChuck Lever *
2586faddda6SChuck Lever */
simple_offset_init(struct offset_ctx * octx)2596faddda6SChuck Lever void simple_offset_init(struct offset_ctx *octx)
2606faddda6SChuck Lever {
2616faddda6SChuck Lever xa_init_flags(&octx->xa, XA_FLAGS_ALLOC1);
262bbaef797SChuck Lever lockdep_set_class(&octx->xa.xa_lock, &simple_offset_xa_lock);
2636faddda6SChuck Lever
2646faddda6SChuck Lever /* 0 is '.', 1 is '..', so always start with offset 2 */
2656faddda6SChuck Lever octx->next_offset = 2;
2666faddda6SChuck Lever }
2676faddda6SChuck Lever
2686faddda6SChuck Lever /**
2696faddda6SChuck Lever * simple_offset_add - Add an entry to a directory's offset map
2706faddda6SChuck Lever * @octx: directory offset ctx to be updated
2716faddda6SChuck Lever * @dentry: new dentry being added
2726faddda6SChuck Lever *
2736faddda6SChuck Lever * Returns zero on success. @so_ctx and the dentry offset are updated.
2746faddda6SChuck Lever * Otherwise, a negative errno value is returned.
2756faddda6SChuck Lever */
simple_offset_add(struct offset_ctx * octx,struct dentry * dentry)2766faddda6SChuck Lever int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
2776faddda6SChuck Lever {
2786faddda6SChuck Lever static const struct xa_limit limit = XA_LIMIT(2, U32_MAX);
2796faddda6SChuck Lever u32 offset;
2806faddda6SChuck Lever int ret;
2816faddda6SChuck Lever
2826faddda6SChuck Lever if (dentry2offset(dentry) != 0)
2836faddda6SChuck Lever return -EBUSY;
2846faddda6SChuck Lever
2856faddda6SChuck Lever ret = xa_alloc_cyclic(&octx->xa, &offset, dentry, limit,
2866faddda6SChuck Lever &octx->next_offset, GFP_KERNEL);
2876faddda6SChuck Lever if (ret < 0)
2886faddda6SChuck Lever return ret;
2896faddda6SChuck Lever
2906faddda6SChuck Lever offset_set(dentry, offset);
2916faddda6SChuck Lever return 0;
2926faddda6SChuck Lever }
2936faddda6SChuck Lever
2946faddda6SChuck Lever /**
2956faddda6SChuck Lever * simple_offset_remove - Remove an entry to a directory's offset map
2966faddda6SChuck Lever * @octx: directory offset ctx to be updated
2976faddda6SChuck Lever * @dentry: dentry being removed
2986faddda6SChuck Lever *
2996faddda6SChuck Lever */
simple_offset_remove(struct offset_ctx * octx,struct dentry * dentry)3006faddda6SChuck Lever void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)
3016faddda6SChuck Lever {
3026faddda6SChuck Lever u32 offset;
3036faddda6SChuck Lever
3046faddda6SChuck Lever offset = dentry2offset(dentry);
3056faddda6SChuck Lever if (offset == 0)
3066faddda6SChuck Lever return;
3076faddda6SChuck Lever
3086faddda6SChuck Lever xa_erase(&octx->xa, offset);
3096faddda6SChuck Lever offset_set(dentry, 0);
3106faddda6SChuck Lever }
3116faddda6SChuck Lever
3126faddda6SChuck Lever /**
3136faddda6SChuck Lever * simple_offset_rename_exchange - exchange rename with directory offsets
3146faddda6SChuck Lever * @old_dir: parent of dentry being moved
3156faddda6SChuck Lever * @old_dentry: dentry being moved
3166faddda6SChuck Lever * @new_dir: destination parent
3176faddda6SChuck Lever * @new_dentry: destination dentry
3186faddda6SChuck Lever *
3196faddda6SChuck Lever * Returns zero on success. Otherwise a negative errno is returned and the
3206faddda6SChuck Lever * rename is rolled back.
3216faddda6SChuck Lever */
simple_offset_rename_exchange(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)3226faddda6SChuck Lever int simple_offset_rename_exchange(struct inode *old_dir,
3236faddda6SChuck Lever struct dentry *old_dentry,
3246faddda6SChuck Lever struct inode *new_dir,
3256faddda6SChuck Lever struct dentry *new_dentry)
3266faddda6SChuck Lever {
3276faddda6SChuck Lever struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
3286faddda6SChuck Lever struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
3296faddda6SChuck Lever u32 old_index = dentry2offset(old_dentry);
3306faddda6SChuck Lever u32 new_index = dentry2offset(new_dentry);
3316faddda6SChuck Lever int ret;
3326faddda6SChuck Lever
3336faddda6SChuck Lever simple_offset_remove(old_ctx, old_dentry);
3346faddda6SChuck Lever simple_offset_remove(new_ctx, new_dentry);
3356faddda6SChuck Lever
3366faddda6SChuck Lever ret = simple_offset_add(new_ctx, old_dentry);
3376faddda6SChuck Lever if (ret)
3386faddda6SChuck Lever goto out_restore;
3396faddda6SChuck Lever
3406faddda6SChuck Lever ret = simple_offset_add(old_ctx, new_dentry);
3416faddda6SChuck Lever if (ret) {
3426faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry);
3436faddda6SChuck Lever goto out_restore;
3446faddda6SChuck Lever }
3456faddda6SChuck Lever
3466faddda6SChuck Lever ret = simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
3476faddda6SChuck Lever if (ret) {
3486faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry);
3496faddda6SChuck Lever simple_offset_remove(old_ctx, new_dentry);
3506faddda6SChuck Lever goto out_restore;
3516faddda6SChuck Lever }
3526faddda6SChuck Lever return 0;
3536faddda6SChuck Lever
3546faddda6SChuck Lever out_restore:
3556faddda6SChuck Lever offset_set(old_dentry, old_index);
3566faddda6SChuck Lever xa_store(&old_ctx->xa, old_index, old_dentry, GFP_KERNEL);
3576faddda6SChuck Lever offset_set(new_dentry, new_index);
3586faddda6SChuck Lever xa_store(&new_ctx->xa, new_index, new_dentry, GFP_KERNEL);
3596faddda6SChuck Lever return ret;
3606faddda6SChuck Lever }
3616faddda6SChuck Lever
3626faddda6SChuck Lever /**
3636faddda6SChuck Lever * simple_offset_destroy - Release offset map
3646faddda6SChuck Lever * @octx: directory offset ctx that is about to be destroyed
3656faddda6SChuck Lever *
3666faddda6SChuck Lever * During fs teardown (eg. umount), a directory's offset map might still
3676faddda6SChuck Lever * contain entries. xa_destroy() cleans out anything that remains.
3686faddda6SChuck Lever */
simple_offset_destroy(struct offset_ctx * octx)3696faddda6SChuck Lever void simple_offset_destroy(struct offset_ctx *octx)
3706faddda6SChuck Lever {
3716faddda6SChuck Lever xa_destroy(&octx->xa);
3726faddda6SChuck Lever }
3736faddda6SChuck Lever
3746faddda6SChuck Lever /**
3756faddda6SChuck Lever * offset_dir_llseek - Advance the read position of a directory descriptor
3766faddda6SChuck Lever * @file: an open directory whose position is to be updated
3776faddda6SChuck Lever * @offset: a byte offset
3786faddda6SChuck Lever * @whence: enumerator describing the starting position for this update
3796faddda6SChuck Lever *
3806faddda6SChuck Lever * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories.
3816faddda6SChuck Lever *
3826faddda6SChuck Lever * Returns the updated read position if successful; otherwise a
3836faddda6SChuck Lever * negative errno is returned and the read position remains unchanged.
3846faddda6SChuck Lever */
offset_dir_llseek(struct file * file,loff_t offset,int whence)3856faddda6SChuck Lever static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
3866faddda6SChuck Lever {
3876faddda6SChuck Lever switch (whence) {
3886faddda6SChuck Lever case SEEK_CUR:
3896faddda6SChuck Lever offset += file->f_pos;
3906faddda6SChuck Lever fallthrough;
3916faddda6SChuck Lever case SEEK_SET:
3926faddda6SChuck Lever if (offset >= 0)
3936faddda6SChuck Lever break;
3946faddda6SChuck Lever fallthrough;
3956faddda6SChuck Lever default:
3966faddda6SChuck Lever return -EINVAL;
3976faddda6SChuck Lever }
3986faddda6SChuck Lever
39953da7fe8SChuck Lever /* In this case, ->private_data is protected by f_pos_lock */
40053da7fe8SChuck Lever file->private_data = NULL;
4016faddda6SChuck Lever return vfs_setpos(file, offset, U32_MAX);
4026faddda6SChuck Lever }
4036faddda6SChuck Lever
offset_find_next(struct xa_state * xas)4046faddda6SChuck Lever static struct dentry *offset_find_next(struct xa_state *xas)
4056faddda6SChuck Lever {
4066faddda6SChuck Lever struct dentry *child, *found = NULL;
4076faddda6SChuck Lever
4086faddda6SChuck Lever rcu_read_lock();
4096faddda6SChuck Lever child = xas_next_entry(xas, U32_MAX);
4106faddda6SChuck Lever if (!child)
4116faddda6SChuck Lever goto out;
4122be4f05aSChuck Lever spin_lock(&child->d_lock);
4136faddda6SChuck Lever if (simple_positive(child))
4146faddda6SChuck Lever found = dget_dlock(child);
4156faddda6SChuck Lever spin_unlock(&child->d_lock);
4166faddda6SChuck Lever out:
4176faddda6SChuck Lever rcu_read_unlock();
4186faddda6SChuck Lever return found;
4196faddda6SChuck Lever }
4206faddda6SChuck Lever
offset_dir_emit(struct dir_context * ctx,struct dentry * dentry)4216faddda6SChuck Lever static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
4226faddda6SChuck Lever {
4236faddda6SChuck Lever u32 offset = dentry2offset(dentry);
4246faddda6SChuck Lever struct inode *inode = d_inode(dentry);
4256faddda6SChuck Lever
4266faddda6SChuck Lever return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset,
4276faddda6SChuck Lever inode->i_ino, fs_umode_to_dtype(inode->i_mode));
4286faddda6SChuck Lever }
4296faddda6SChuck Lever
offset_iterate_dir(struct inode * inode,struct dir_context * ctx)43053da7fe8SChuck Lever static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
4316faddda6SChuck Lever {
4326faddda6SChuck Lever struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode);
4336faddda6SChuck Lever XA_STATE(xas, &so_ctx->xa, ctx->pos);
4346faddda6SChuck Lever struct dentry *dentry;
4356faddda6SChuck Lever
4366faddda6SChuck Lever while (true) {
4376faddda6SChuck Lever dentry = offset_find_next(&xas);
4386faddda6SChuck Lever if (!dentry)
43953da7fe8SChuck Lever return ERR_PTR(-ENOENT);
4406faddda6SChuck Lever
4416faddda6SChuck Lever if (!offset_dir_emit(ctx, dentry)) {
4426faddda6SChuck Lever dput(dentry);
4436faddda6SChuck Lever break;
4446faddda6SChuck Lever }
4456faddda6SChuck Lever
4466faddda6SChuck Lever dput(dentry);
4476faddda6SChuck Lever ctx->pos = xas.xa_index + 1;
4486faddda6SChuck Lever }
44953da7fe8SChuck Lever return NULL;
4506faddda6SChuck Lever }
4516faddda6SChuck Lever
4526faddda6SChuck Lever /**
4536faddda6SChuck Lever * offset_readdir - Emit entries starting at offset @ctx->pos
4546faddda6SChuck Lever * @file: an open directory to iterate over
4556faddda6SChuck Lever * @ctx: directory iteration context
4566faddda6SChuck Lever *
4576faddda6SChuck Lever * Caller must hold @file's i_rwsem to prevent insertion or removal of
4586faddda6SChuck Lever * entries during this call.
4596faddda6SChuck Lever *
4606faddda6SChuck Lever * On entry, @ctx->pos contains an offset that represents the first entry
4616faddda6SChuck Lever * to be read from the directory.
4626faddda6SChuck Lever *
4636faddda6SChuck Lever * The operation continues until there are no more entries to read, or
4646faddda6SChuck Lever * until the ctx->actor indicates there is no more space in the caller's
4656faddda6SChuck Lever * output buffer.
4666faddda6SChuck Lever *
4676faddda6SChuck Lever * On return, @ctx->pos contains an offset that will read the next entry
4682be4f05aSChuck Lever * in this directory when offset_readdir() is called again with @ctx.
4696faddda6SChuck Lever *
4706faddda6SChuck Lever * Return values:
4716faddda6SChuck Lever * %0 - Complete
4726faddda6SChuck Lever */
offset_readdir(struct file * file,struct dir_context * ctx)4736faddda6SChuck Lever static int offset_readdir(struct file *file, struct dir_context *ctx)
4746faddda6SChuck Lever {
4756faddda6SChuck Lever struct dentry *dir = file->f_path.dentry;
4766faddda6SChuck Lever
4776faddda6SChuck Lever lockdep_assert_held(&d_inode(dir)->i_rwsem);
4786faddda6SChuck Lever
4796faddda6SChuck Lever if (!dir_emit_dots(file, ctx))
4806faddda6SChuck Lever return 0;
4816faddda6SChuck Lever
48253da7fe8SChuck Lever /* In this case, ->private_data is protected by f_pos_lock */
48353da7fe8SChuck Lever if (ctx->pos == 2)
48453da7fe8SChuck Lever file->private_data = NULL;
48553da7fe8SChuck Lever else if (file->private_data == ERR_PTR(-ENOENT))
48653da7fe8SChuck Lever return 0;
48753da7fe8SChuck Lever file->private_data = offset_iterate_dir(d_inode(dir), ctx);
4886faddda6SChuck Lever return 0;
4896faddda6SChuck Lever }
4906faddda6SChuck Lever
4916faddda6SChuck Lever const struct file_operations simple_offset_dir_operations = {
4926faddda6SChuck Lever .llseek = offset_dir_llseek,
4936faddda6SChuck Lever .iterate_shared = offset_readdir,
4946faddda6SChuck Lever .read = generic_read_dir,
4956faddda6SChuck Lever .fsync = noop_fsync,
4966faddda6SChuck Lever };
4976faddda6SChuck Lever
find_next_child(struct dentry * parent,struct dentry * prev)498a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
499a3d1e7ebSAl Viro {
500a3d1e7ebSAl Viro struct dentry *child = NULL;
501a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
502a3d1e7ebSAl Viro
503a3d1e7ebSAl Viro spin_lock(&parent->d_lock);
504a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) {
505a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child);
506a3d1e7ebSAl Viro if (simple_positive(d)) {
507a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
508a3d1e7ebSAl Viro if (simple_positive(d))
509a3d1e7ebSAl Viro child = dget_dlock(d);
510a3d1e7ebSAl Viro spin_unlock(&d->d_lock);
511a3d1e7ebSAl Viro if (likely(child))
512a3d1e7ebSAl Viro break;
513a3d1e7ebSAl Viro }
514a3d1e7ebSAl Viro }
515a3d1e7ebSAl Viro spin_unlock(&parent->d_lock);
516a3d1e7ebSAl Viro dput(prev);
517a3d1e7ebSAl Viro return child;
518a3d1e7ebSAl Viro }
519a3d1e7ebSAl Viro
simple_recursive_removal(struct dentry * dentry,void (* callback)(struct dentry *))520a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry,
521a3d1e7ebSAl Viro void (*callback)(struct dentry *))
522a3d1e7ebSAl Viro {
523a3d1e7ebSAl Viro struct dentry *this = dget(dentry);
524a3d1e7ebSAl Viro while (true) {
525a3d1e7ebSAl Viro struct dentry *victim = NULL, *child;
526a3d1e7ebSAl Viro struct inode *inode = this->d_inode;
527a3d1e7ebSAl Viro
528a3d1e7ebSAl Viro inode_lock(inode);
529a3d1e7ebSAl Viro if (d_is_dir(this))
530a3d1e7ebSAl Viro inode->i_flags |= S_DEAD;
531a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) {
532a3d1e7ebSAl Viro // kill and ascend
533a3d1e7ebSAl Viro // update metadata while it's still locked
534f7f43858SJeff Layton inode_set_ctime_current(inode);
535a3d1e7ebSAl Viro clear_nlink(inode);
536a3d1e7ebSAl Viro inode_unlock(inode);
537a3d1e7ebSAl Viro victim = this;
538a3d1e7ebSAl Viro this = this->d_parent;
539a3d1e7ebSAl Viro inode = this->d_inode;
540a3d1e7ebSAl Viro inode_lock(inode);
541a3d1e7ebSAl Viro if (simple_positive(victim)) {
542a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts
543a3d1e7ebSAl Viro if (d_is_dir(victim))
544a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim);
545a3d1e7ebSAl Viro else
546a3d1e7ebSAl Viro fsnotify_unlink(inode, victim);
547a3d1e7ebSAl Viro if (callback)
548a3d1e7ebSAl Viro callback(victim);
549a3d1e7ebSAl Viro dput(victim); // unpin it
550a3d1e7ebSAl Viro }
551a3d1e7ebSAl Viro if (victim == dentry) {
552*5b5599a7SJeff Layton inode_set_mtime_to_ts(inode,
553*5b5599a7SJeff Layton inode_set_ctime_current(inode));
554a3d1e7ebSAl Viro if (d_is_dir(dentry))
555a3d1e7ebSAl Viro drop_nlink(inode);
556a3d1e7ebSAl Viro inode_unlock(inode);
557a3d1e7ebSAl Viro dput(dentry);
558a3d1e7ebSAl Viro return;
559a3d1e7ebSAl Viro }
560a3d1e7ebSAl Viro }
561a3d1e7ebSAl Viro inode_unlock(inode);
562a3d1e7ebSAl Viro this = child;
563a3d1e7ebSAl Viro }
564a3d1e7ebSAl Viro }
565a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal);
566a3d1e7ebSAl Viro
567759b9775SHugh Dickins static const struct super_operations simple_super_operations = {
568759b9775SHugh Dickins .statfs = simple_statfs,
569759b9775SHugh Dickins };
570759b9775SHugh Dickins
pseudo_fs_fill_super(struct super_block * s,struct fs_context * fc)571db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
5721da177e4SLinus Torvalds {
57331d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private;
5741da177e4SLinus Torvalds struct inode *root;
5751da177e4SLinus Torvalds
57689a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE;
5773971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE;
5783971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT;
5798d9e46d8SAl Viro s->s_magic = ctx->magic;
5808d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations;
5818d9e46d8SAl Viro s->s_xattr = ctx->xattr;
5821da177e4SLinus Torvalds s->s_time_gran = 1;
5831da177e4SLinus Torvalds root = new_inode(s);
5841da177e4SLinus Torvalds if (!root)
585db2c246aSDavid Howells return -ENOMEM;
586db2c246aSDavid Howells
5871a1c9bb4SJeff Layton /*
5881a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created
5891a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing
5901a1c9bb4SJeff Layton * max_reserved of 1 to iunique).
5911a1c9bb4SJeff Layton */
5921a1c9bb4SJeff Layton root->i_ino = 1;
5931da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
594*5b5599a7SJeff Layton simple_inode_init_ts(root);
5958d9e46d8SAl Viro s->s_root = d_make_root(root);
5968d9e46d8SAl Viro if (!s->s_root)
5978d9e46d8SAl Viro return -ENOMEM;
598db2c246aSDavid Howells s->s_d_op = ctx->dops;
599db2c246aSDavid Howells return 0;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds
pseudo_fs_get_tree(struct fs_context * fc)602db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc)
603db2c246aSDavid Howells {
6042ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super);
6051da177e4SLinus Torvalds }
60631d6d5ceSDavid Howells
pseudo_fs_free(struct fs_context * fc)60731d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc)
60831d6d5ceSDavid Howells {
60931d6d5ceSDavid Howells kfree(fc->fs_private);
61031d6d5ceSDavid Howells }
61131d6d5ceSDavid Howells
61231d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = {
61331d6d5ceSDavid Howells .free = pseudo_fs_free,
61431d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree,
61531d6d5ceSDavid Howells };
61631d6d5ceSDavid Howells
61731d6d5ceSDavid Howells /*
61831d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
61931d6d5ceSDavid Howells * will never be mountable)
62031d6d5ceSDavid Howells */
init_pseudo(struct fs_context * fc,unsigned long magic)62131d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
62231d6d5ceSDavid Howells unsigned long magic)
62331d6d5ceSDavid Howells {
62431d6d5ceSDavid Howells struct pseudo_fs_context *ctx;
62531d6d5ceSDavid Howells
62631d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
62731d6d5ceSDavid Howells if (likely(ctx)) {
62831d6d5ceSDavid Howells ctx->magic = magic;
62931d6d5ceSDavid Howells fc->fs_private = ctx;
63031d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops;
631db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER;
632db2c246aSDavid Howells fc->global = true;
63331d6d5ceSDavid Howells }
63431d6d5ceSDavid Howells return ctx;
63531d6d5ceSDavid Howells }
63631d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo);
6371da177e4SLinus Torvalds
simple_open(struct inode * inode,struct file * file)63820955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file)
63920955e89SStephen Boyd {
64020955e89SStephen Boyd if (inode->i_private)
64120955e89SStephen Boyd file->private_data = inode->i_private;
64220955e89SStephen Boyd return 0;
64320955e89SStephen Boyd }
64412f38872SAl Viro EXPORT_SYMBOL(simple_open);
64520955e89SStephen Boyd
simple_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)6461da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
6471da177e4SLinus Torvalds {
648dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry);
6491da177e4SLinus Torvalds
650*5b5599a7SJeff Layton inode_set_mtime_to_ts(dir,
651*5b5599a7SJeff Layton inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
652d8c76e6fSDave Hansen inc_nlink(inode);
6537de9c6eeSAl Viro ihold(inode);
6541da177e4SLinus Torvalds dget(dentry);
6551da177e4SLinus Torvalds d_instantiate(dentry, inode);
6561da177e4SLinus Torvalds return 0;
6571da177e4SLinus Torvalds }
65812f38872SAl Viro EXPORT_SYMBOL(simple_link);
6591da177e4SLinus Torvalds
simple_empty(struct dentry * dentry)6601da177e4SLinus Torvalds int simple_empty(struct dentry *dentry)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds struct dentry *child;
6631da177e4SLinus Torvalds int ret = 0;
6641da177e4SLinus Torvalds
6652fd6b7f5SNick Piggin spin_lock(&dentry->d_lock);
666946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) {
667da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
668da502956SNick Piggin if (simple_positive(child)) {
669da502956SNick Piggin spin_unlock(&child->d_lock);
6701da177e4SLinus Torvalds goto out;
671da502956SNick Piggin }
672da502956SNick Piggin spin_unlock(&child->d_lock);
673da502956SNick Piggin }
6741da177e4SLinus Torvalds ret = 1;
6751da177e4SLinus Torvalds out:
6762fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock);
6771da177e4SLinus Torvalds return ret;
6781da177e4SLinus Torvalds }
67912f38872SAl Viro EXPORT_SYMBOL(simple_empty);
6801da177e4SLinus Torvalds
simple_unlink(struct inode * dir,struct dentry * dentry)6811da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry)
6821da177e4SLinus Torvalds {
683dea655c2SDavid Howells struct inode *inode = d_inode(dentry);
6841da177e4SLinus Torvalds
685*5b5599a7SJeff Layton inode_set_mtime_to_ts(dir,
686*5b5599a7SJeff Layton inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
6879a53c3a7SDave Hansen drop_nlink(inode);
6881da177e4SLinus Torvalds dput(dentry);
6891da177e4SLinus Torvalds return 0;
6901da177e4SLinus Torvalds }
69112f38872SAl Viro EXPORT_SYMBOL(simple_unlink);
6921da177e4SLinus Torvalds
simple_rmdir(struct inode * dir,struct dentry * dentry)6931da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds if (!simple_empty(dentry))
6961da177e4SLinus Torvalds return -ENOTEMPTY;
6971da177e4SLinus Torvalds
698dea655c2SDavid Howells drop_nlink(d_inode(dentry));
6991da177e4SLinus Torvalds simple_unlink(dir, dentry);
7009a53c3a7SDave Hansen drop_nlink(dir);
7011da177e4SLinus Torvalds return 0;
7021da177e4SLinus Torvalds }
70312f38872SAl Viro EXPORT_SYMBOL(simple_rmdir);
7041da177e4SLinus Torvalds
7050c476792SJeff Layton /**
7060c476792SJeff Layton * simple_rename_timestamp - update the various inode timestamps for rename
7070c476792SJeff Layton * @old_dir: old parent directory
7080c476792SJeff Layton * @old_dentry: dentry that is being renamed
7090c476792SJeff Layton * @new_dir: new parent directory
7100c476792SJeff Layton * @new_dentry: target for rename
7110c476792SJeff Layton *
7120c476792SJeff Layton * POSIX mandates that the old and new parent directories have their ctime and
7130c476792SJeff Layton * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have
7140c476792SJeff Layton * their ctime updated.
7150c476792SJeff Layton */
simple_rename_timestamp(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)7160c476792SJeff Layton void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
7170c476792SJeff Layton struct inode *new_dir, struct dentry *new_dentry)
7180c476792SJeff Layton {
7190c476792SJeff Layton struct inode *newino = d_inode(new_dentry);
7200c476792SJeff Layton
721*5b5599a7SJeff Layton inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
7220c476792SJeff Layton if (new_dir != old_dir)
723*5b5599a7SJeff Layton inode_set_mtime_to_ts(new_dir,
724*5b5599a7SJeff Layton inode_set_ctime_current(new_dir));
7250c476792SJeff Layton inode_set_ctime_current(d_inode(old_dentry));
7260c476792SJeff Layton if (newino)
7270c476792SJeff Layton inode_set_ctime_current(newino);
7280c476792SJeff Layton }
7290c476792SJeff Layton EXPORT_SYMBOL_GPL(simple_rename_timestamp);
7300c476792SJeff Layton
simple_rename_exchange(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)7316429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
7326429e463SLorenz Bauer struct inode *new_dir, struct dentry *new_dentry)
7336429e463SLorenz Bauer {
7346429e463SLorenz Bauer bool old_is_dir = d_is_dir(old_dentry);
7356429e463SLorenz Bauer bool new_is_dir = d_is_dir(new_dentry);
7366429e463SLorenz Bauer
7376429e463SLorenz Bauer if (old_dir != new_dir && old_is_dir != new_is_dir) {
7386429e463SLorenz Bauer if (old_is_dir) {
7396429e463SLorenz Bauer drop_nlink(old_dir);
7406429e463SLorenz Bauer inc_nlink(new_dir);
7416429e463SLorenz Bauer } else {
7426429e463SLorenz Bauer drop_nlink(new_dir);
7436429e463SLorenz Bauer inc_nlink(old_dir);
7446429e463SLorenz Bauer }
7456429e463SLorenz Bauer }
7460c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
7476429e463SLorenz Bauer return 0;
7486429e463SLorenz Bauer }
7496429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange);
7506429e463SLorenz Bauer
simple_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)751e18275aeSChristian Brauner int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir,
752549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir,
753549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags)
7541da177e4SLinus Torvalds {
755e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry);
7561da177e4SLinus Torvalds
7573871cb8cSLorenz Bauer if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
758e0e0be8aSMiklos Szeredi return -EINVAL;
759e0e0be8aSMiklos Szeredi
7603871cb8cSLorenz Bauer if (flags & RENAME_EXCHANGE)
7613871cb8cSLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
7623871cb8cSLorenz Bauer
7631da177e4SLinus Torvalds if (!simple_empty(new_dentry))
7641da177e4SLinus Torvalds return -ENOTEMPTY;
7651da177e4SLinus Torvalds
766dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) {
7671da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry);
768841590ceSAl Viro if (they_are_dirs) {
769dea655c2SDavid Howells drop_nlink(d_inode(new_dentry));
7709a53c3a7SDave Hansen drop_nlink(old_dir);
771841590ceSAl Viro }
7721da177e4SLinus Torvalds } else if (they_are_dirs) {
7739a53c3a7SDave Hansen drop_nlink(old_dir);
774d8c76e6fSDave Hansen inc_nlink(new_dir);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds
7770c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
7781da177e4SLinus Torvalds return 0;
7791da177e4SLinus Torvalds }
78012f38872SAl Viro EXPORT_SYMBOL(simple_rename);
7811da177e4SLinus Torvalds
7827bb46a67Snpiggin@suse.de /**
783eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem
784c1632a0fSChristian Brauner * @idmap: idmap of the target mount
7857bb46a67Snpiggin@suse.de * @dentry: dentry
7867bb46a67Snpiggin@suse.de * @iattr: iattr structure
7877bb46a67Snpiggin@suse.de *
7887bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure.
7897bb46a67Snpiggin@suse.de *
790eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper
791eef2380cSChristoph Hellwig * implementation of size changes.
792eef2380cSChristoph Hellwig *
793eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files
794eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk
795eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method.
7967bb46a67Snpiggin@suse.de */
simple_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)797c1632a0fSChristian Brauner int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
798549c7297SChristian Brauner struct iattr *iattr)
7997bb46a67Snpiggin@suse.de {
800dea655c2SDavid Howells struct inode *inode = d_inode(dentry);
8017bb46a67Snpiggin@suse.de int error;
8027bb46a67Snpiggin@suse.de
803c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, iattr);
8047bb46a67Snpiggin@suse.de if (error)
8057bb46a67Snpiggin@suse.de return error;
8067bb46a67Snpiggin@suse.de
8072c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE)
8082c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size);
809c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr);
810eef2380cSChristoph Hellwig mark_inode_dirty(inode);
811eef2380cSChristoph Hellwig return 0;
8127bb46a67Snpiggin@suse.de }
8137bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr);
8147bb46a67Snpiggin@suse.de
simple_read_folio(struct file * file,struct folio * folio)815a77f580aSMatthew Wilcox (Oracle) static int simple_read_folio(struct file *file, struct folio *folio)
8161da177e4SLinus Torvalds {
817a77f580aSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio));
818a77f580aSMatthew Wilcox (Oracle) flush_dcache_folio(folio);
819a77f580aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio);
820a77f580aSMatthew Wilcox (Oracle) folio_unlock(folio);
8211da177e4SLinus Torvalds return 0;
8221da177e4SLinus Torvalds }
8231da177e4SLinus Torvalds
simple_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)824afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping,
8259d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len,
826afddba49SNick Piggin struct page **pagep, void **fsdata)
827afddba49SNick Piggin {
8285522d9f7SMatthew Wilcox (Oracle) struct folio *folio;
829afddba49SNick Piggin
8305522d9f7SMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
8315522d9f7SMatthew Wilcox (Oracle) mapping_gfp_mask(mapping));
8325522d9f7SMatthew Wilcox (Oracle) if (IS_ERR(folio))
8335522d9f7SMatthew Wilcox (Oracle) return PTR_ERR(folio);
834afddba49SNick Piggin
8355522d9f7SMatthew Wilcox (Oracle) *pagep = &folio->page;
836afddba49SNick Piggin
8375522d9f7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio) && (len != folio_size(folio))) {
8385522d9f7SMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos);
839afddba49SNick Piggin
8405522d9f7SMatthew Wilcox (Oracle) folio_zero_segments(folio, 0, from,
8415522d9f7SMatthew Wilcox (Oracle) from + len, folio_size(folio));
842193cf4b9SBoaz Harrosh }
843193cf4b9SBoaz Harrosh return 0;
844afddba49SNick Piggin }
84512f38872SAl Viro EXPORT_SYMBOL(simple_write_begin);
846afddba49SNick Piggin
847ad2a722fSBoaz Harrosh /**
848ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes
8498e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations
850ad2a722fSBoaz Harrosh * @mapping: "
851ad2a722fSBoaz Harrosh * @pos: "
852ad2a722fSBoaz Harrosh * @len: "
853ad2a722fSBoaz Harrosh * @copied: "
854ad2a722fSBoaz Harrosh * @page: "
855ad2a722fSBoaz Harrosh * @fsdata: "
856ad2a722fSBoaz Harrosh *
857ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is
858ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of
859ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for
860ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held.
861ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end().
862ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
863ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode
864ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the
865ad2a722fSBoaz Harrosh * case that i_size has changed.
86604fff641SAl Viro *
867a77f580aSMatthew Wilcox (Oracle) * Use *ONLY* with simple_read_folio()
868ad2a722fSBoaz Harrosh */
simple_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)869c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping,
870ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied,
871ad2a722fSBoaz Harrosh struct page *page, void *fsdata)
8721da177e4SLinus Torvalds {
8735522d9f7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page);
8745522d9f7SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host;
875ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied;
876ad2a722fSBoaz Harrosh
8775522d9f7SMatthew Wilcox (Oracle) /* zero the stale part of the folio if we did a short copy */
8785522d9f7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) {
879ad2a722fSBoaz Harrosh if (copied < len) {
8805522d9f7SMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos);
881ad2a722fSBoaz Harrosh
8825522d9f7SMatthew Wilcox (Oracle) folio_zero_range(folio, from + copied, len - copied);
883ad2a722fSBoaz Harrosh }
8845522d9f7SMatthew Wilcox (Oracle) folio_mark_uptodate(folio);
88504fff641SAl Viro }
8861da177e4SLinus Torvalds /*
8871da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size
8881b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex.
8891da177e4SLinus Torvalds */
890ad2a722fSBoaz Harrosh if (last_pos > inode->i_size)
891ad2a722fSBoaz Harrosh i_size_write(inode, last_pos);
892ad2a722fSBoaz Harrosh
8935522d9f7SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
8945522d9f7SMatthew Wilcox (Oracle) folio_unlock(folio);
8955522d9f7SMatthew Wilcox (Oracle) folio_put(folio);
896afddba49SNick Piggin
897afddba49SNick Piggin return copied;
898afddba49SNick Piggin }
899c1e3dbe9SChristoph Hellwig
900c1e3dbe9SChristoph Hellwig /*
901c1e3dbe9SChristoph Hellwig * Provides ramfs-style behavior: data in the pagecache, but no writeback.
902c1e3dbe9SChristoph Hellwig */
903c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = {
904a77f580aSMatthew Wilcox (Oracle) .read_folio = simple_read_folio,
905c1e3dbe9SChristoph Hellwig .write_begin = simple_write_begin,
906c1e3dbe9SChristoph Hellwig .write_end = simple_write_end,
90746de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio,
908c1e3dbe9SChristoph Hellwig };
909c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops);
910afddba49SNick Piggin
9111a1c9bb4SJeff Layton /*
9121a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate
9131a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care
9141a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions.
9151a1c9bb4SJeff Layton */
simple_fill_super(struct super_block * s,unsigned long magic,const struct tree_descr * files)9167d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic,
917cda37124SEric Biggers const struct tree_descr *files)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds struct inode *inode;
9201da177e4SLinus Torvalds struct dentry *root;
9211da177e4SLinus Torvalds struct dentry *dentry;
9221da177e4SLinus Torvalds int i;
9231da177e4SLinus Torvalds
92409cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE;
92509cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT;
9261da177e4SLinus Torvalds s->s_magic = magic;
927759b9775SHugh Dickins s->s_op = &simple_super_operations;
9281da177e4SLinus Torvalds s->s_time_gran = 1;
9291da177e4SLinus Torvalds
9301da177e4SLinus Torvalds inode = new_inode(s);
9311da177e4SLinus Torvalds if (!inode)
9321da177e4SLinus Torvalds return -ENOMEM;
9331a1c9bb4SJeff Layton /*
9341a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an
9351a1c9bb4SJeff Layton * entry at index 1
9361a1c9bb4SJeff Layton */
9371a1c9bb4SJeff Layton inode->i_ino = 1;
9381da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755;
939*5b5599a7SJeff Layton simple_inode_init_ts(inode);
9401da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations;
9411da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations;
942bfe86848SMiklos Szeredi set_nlink(inode, 2);
94348fde701SAl Viro root = d_make_root(inode);
94448fde701SAl Viro if (!root)
9451da177e4SLinus Torvalds return -ENOMEM;
9461da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) {
9471da177e4SLinus Torvalds if (!files->name)
9481da177e4SLinus Torvalds continue;
9491a1c9bb4SJeff Layton
9501a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */
9511a1c9bb4SJeff Layton if (unlikely(i == 1))
9521a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array"
9531a1c9bb4SJeff Layton "with an index of 1!\n", __func__,
9541a1c9bb4SJeff Layton s->s_type->name);
9551a1c9bb4SJeff Layton
9561da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name);
9571da177e4SLinus Torvalds if (!dentry)
9581da177e4SLinus Torvalds goto out;
9591da177e4SLinus Torvalds inode = new_inode(s);
96032096ea1SKonstantin Khlebnikov if (!inode) {
96132096ea1SKonstantin Khlebnikov dput(dentry);
9621da177e4SLinus Torvalds goto out;
96332096ea1SKonstantin Khlebnikov }
9641da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode;
965*5b5599a7SJeff Layton simple_inode_init_ts(inode);
9661da177e4SLinus Torvalds inode->i_fop = files->ops;
9671da177e4SLinus Torvalds inode->i_ino = i;
9681da177e4SLinus Torvalds d_add(dentry, inode);
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds s->s_root = root;
9711da177e4SLinus Torvalds return 0;
9721da177e4SLinus Torvalds out:
9731da177e4SLinus Torvalds d_genocide(root);
974640946f2SAl Viro shrink_dcache_parent(root);
9751da177e4SLinus Torvalds dput(root);
9761da177e4SLinus Torvalds return -ENOMEM;
9771da177e4SLinus Torvalds }
97812f38872SAl Viro EXPORT_SYMBOL(simple_fill_super);
9791da177e4SLinus Torvalds
9801da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock);
9811da177e4SLinus Torvalds
simple_pin_fs(struct file_system_type * type,struct vfsmount ** mount,int * count)9821f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds struct vfsmount *mnt = NULL;
9851da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
9861da177e4SLinus Torvalds if (unlikely(!*mount)) {
9871da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
9881751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
9891da177e4SLinus Torvalds if (IS_ERR(mnt))
9901da177e4SLinus Torvalds return PTR_ERR(mnt);
9911da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
9921da177e4SLinus Torvalds if (!*mount)
9931da177e4SLinus Torvalds *mount = mnt;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds mntget(*mount);
9961da177e4SLinus Torvalds ++*count;
9971da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
9981da177e4SLinus Torvalds mntput(mnt);
9991da177e4SLinus Torvalds return 0;
10001da177e4SLinus Torvalds }
100112f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs);
10021da177e4SLinus Torvalds
simple_release_fs(struct vfsmount ** mount,int * count)10031da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count)
10041da177e4SLinus Torvalds {
10051da177e4SLinus Torvalds struct vfsmount *mnt;
10061da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
10071da177e4SLinus Torvalds mnt = *mount;
10081da177e4SLinus Torvalds if (!--*count)
10091da177e4SLinus Torvalds *mount = NULL;
10101da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
10111da177e4SLinus Torvalds mntput(mnt);
10121da177e4SLinus Torvalds }
101312f38872SAl Viro EXPORT_SYMBOL(simple_release_fs);
10141da177e4SLinus Torvalds
10156d1029b5SAkinobu Mita /**
10166d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space
10176d1029b5SAkinobu Mita * @to: the user space buffer to read to
10186d1029b5SAkinobu Mita * @count: the maximum number of bytes to read
10196d1029b5SAkinobu Mita * @ppos: the current position in the buffer
10206d1029b5SAkinobu Mita * @from: the buffer to read from
10216d1029b5SAkinobu Mita * @available: the size of the buffer
10226d1029b5SAkinobu Mita *
10236d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the
10246d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to.
10256d1029b5SAkinobu Mita *
10266d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is
10276d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error.
10286d1029b5SAkinobu Mita **/
simple_read_from_buffer(void __user * to,size_t count,loff_t * ppos,const void * from,size_t available)10291da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
10301da177e4SLinus Torvalds const void *from, size_t available)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds loff_t pos = *ppos;
103314be2746SSteven Rostedt size_t ret;
103414be2746SSteven Rostedt
10351da177e4SLinus Torvalds if (pos < 0)
10361da177e4SLinus Torvalds return -EINVAL;
103714be2746SSteven Rostedt if (pos >= available || !count)
10381da177e4SLinus Torvalds return 0;
10391da177e4SLinus Torvalds if (count > available - pos)
10401da177e4SLinus Torvalds count = available - pos;
104114be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count);
104214be2746SSteven Rostedt if (ret == count)
10431da177e4SLinus Torvalds return -EFAULT;
104414be2746SSteven Rostedt count -= ret;
10451da177e4SLinus Torvalds *ppos = pos + count;
10461da177e4SLinus Torvalds return count;
10471da177e4SLinus Torvalds }
104812f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer);
10491da177e4SLinus Torvalds
10506d1029b5SAkinobu Mita /**
10516a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer
10526a727b43SJiri Slaby * @to: the buffer to write to
10536a727b43SJiri Slaby * @available: the size of the buffer
10546a727b43SJiri Slaby * @ppos: the current position in the buffer
10556a727b43SJiri Slaby * @from: the user space buffer to read from
10566a727b43SJiri Slaby * @count: the maximum number of bytes to read
10576a727b43SJiri Slaby *
10586a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user
10596a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos.
10606a727b43SJiri Slaby *
10616a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is
10626a727b43SJiri Slaby * advanced by this number, or negative value is returned on error.
10636a727b43SJiri Slaby **/
simple_write_to_buffer(void * to,size_t available,loff_t * ppos,const void __user * from,size_t count)10646a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
10656a727b43SJiri Slaby const void __user *from, size_t count)
10666a727b43SJiri Slaby {
10676a727b43SJiri Slaby loff_t pos = *ppos;
10686a727b43SJiri Slaby size_t res;
10696a727b43SJiri Slaby
10706a727b43SJiri Slaby if (pos < 0)
10716a727b43SJiri Slaby return -EINVAL;
10726a727b43SJiri Slaby if (pos >= available || !count)
10736a727b43SJiri Slaby return 0;
10746a727b43SJiri Slaby if (count > available - pos)
10756a727b43SJiri Slaby count = available - pos;
10766a727b43SJiri Slaby res = copy_from_user(to + pos, from, count);
10776a727b43SJiri Slaby if (res == count)
10786a727b43SJiri Slaby return -EFAULT;
10796a727b43SJiri Slaby count -= res;
10806a727b43SJiri Slaby *ppos = pos + count;
10816a727b43SJiri Slaby return count;
10826a727b43SJiri Slaby }
108312f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer);
10846a727b43SJiri Slaby
10856a727b43SJiri Slaby /**
10866d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer
10876d1029b5SAkinobu Mita * @to: the kernel space buffer to read to
10886d1029b5SAkinobu Mita * @count: the maximum number of bytes to read
10896d1029b5SAkinobu Mita * @ppos: the current position in the buffer
10906d1029b5SAkinobu Mita * @from: the buffer to read from
10916d1029b5SAkinobu Mita * @available: the size of the buffer
10926d1029b5SAkinobu Mita *
10936d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the
10946d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to.
10956d1029b5SAkinobu Mita *
10966d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is
10976d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error.
10986d1029b5SAkinobu Mita **/
memory_read_from_buffer(void * to,size_t count,loff_t * ppos,const void * from,size_t available)109993b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
110093b07113SAkinobu Mita const void *from, size_t available)
110193b07113SAkinobu Mita {
110293b07113SAkinobu Mita loff_t pos = *ppos;
110393b07113SAkinobu Mita
110493b07113SAkinobu Mita if (pos < 0)
110593b07113SAkinobu Mita return -EINVAL;
110693b07113SAkinobu Mita if (pos >= available)
110793b07113SAkinobu Mita return 0;
110893b07113SAkinobu Mita if (count > available - pos)
110993b07113SAkinobu Mita count = available - pos;
111093b07113SAkinobu Mita memcpy(to, from + pos, count);
111193b07113SAkinobu Mita *ppos = pos + count;
111293b07113SAkinobu Mita
111393b07113SAkinobu Mita return count;
111493b07113SAkinobu Mita }
111512f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer);
111693b07113SAkinobu Mita
11171da177e4SLinus Torvalds /*
11181da177e4SLinus Torvalds * Transaction based IO.
11191da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then
11201da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a
11211da177e4SLinus Torvalds * file-local buffer.
11221da177e4SLinus Torvalds */
112376791ab2SIngo Molnar
simple_transaction_set(struct file * file,size_t n)112476791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n)
112576791ab2SIngo Molnar {
112676791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data;
112776791ab2SIngo Molnar
112876791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
112976791ab2SIngo Molnar
113076791ab2SIngo Molnar /*
113176791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until
113276791ab2SIngo Molnar * ar->data is ready for reading.
113376791ab2SIngo Molnar */
113476791ab2SIngo Molnar smp_mb();
113576791ab2SIngo Molnar ar->size = n;
113676791ab2SIngo Molnar }
113712f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set);
113876791ab2SIngo Molnar
simple_transaction_get(struct file * file,const char __user * buf,size_t size)11391da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
11401da177e4SLinus Torvalds {
11411da177e4SLinus Torvalds struct simple_transaction_argresp *ar;
11421da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock);
11431da177e4SLinus Torvalds
11441da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1)
11451da177e4SLinus Torvalds return ERR_PTR(-EFBIG);
11461da177e4SLinus Torvalds
11471da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
11481da177e4SLinus Torvalds if (!ar)
11491da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
11501da177e4SLinus Torvalds
11511da177e4SLinus Torvalds spin_lock(&simple_transaction_lock);
11521da177e4SLinus Torvalds
11531da177e4SLinus Torvalds /* only one write allowed per open */
11541da177e4SLinus Torvalds if (file->private_data) {
11551da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock);
11561da177e4SLinus Torvalds free_page((unsigned long)ar);
11571da177e4SLinus Torvalds return ERR_PTR(-EBUSY);
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds
11601da177e4SLinus Torvalds file->private_data = ar;
11611da177e4SLinus Torvalds
11621da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock);
11631da177e4SLinus Torvalds
11641da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size))
11651da177e4SLinus Torvalds return ERR_PTR(-EFAULT);
11661da177e4SLinus Torvalds
11671da177e4SLinus Torvalds return ar->data;
11681da177e4SLinus Torvalds }
116912f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get);
11701da177e4SLinus Torvalds
simple_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)11711da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
11721da177e4SLinus Torvalds {
11731da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data;
11741da177e4SLinus Torvalds
11751da177e4SLinus Torvalds if (!ar)
11761da177e4SLinus Torvalds return 0;
11771da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
11781da177e4SLinus Torvalds }
117912f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read);
11801da177e4SLinus Torvalds
simple_transaction_release(struct inode * inode,struct file * file)11811da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds free_page((unsigned long)file->private_data);
11841da177e4SLinus Torvalds return 0;
11851da177e4SLinus Torvalds }
118612f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release);
11871da177e4SLinus Torvalds
1188acaefc25SArnd Bergmann /* Simple attribute files */
1189acaefc25SArnd Bergmann
1190acaefc25SArnd Bergmann struct simple_attr {
11918b88b099SChristoph Hellwig int (*get)(void *, u64 *);
11928b88b099SChristoph Hellwig int (*set)(void *, u64);
1193acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */
1194acaefc25SArnd Bergmann char set_buf[24];
1195acaefc25SArnd Bergmann void *data;
1196acaefc25SArnd Bergmann const char *fmt; /* format for read operation */
11977cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */
1198acaefc25SArnd Bergmann };
1199acaefc25SArnd Bergmann
1200acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation
1201acaefc25SArnd Bergmann * 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)1202acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file,
12038b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64),
1204acaefc25SArnd Bergmann const char *fmt)
1205acaefc25SArnd Bergmann {
1206acaefc25SArnd Bergmann struct simple_attr *attr;
1207acaefc25SArnd Bergmann
1208a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1209acaefc25SArnd Bergmann if (!attr)
1210acaefc25SArnd Bergmann return -ENOMEM;
1211acaefc25SArnd Bergmann
1212acaefc25SArnd Bergmann attr->get = get;
1213acaefc25SArnd Bergmann attr->set = set;
12148e18e294STheodore Ts'o attr->data = inode->i_private;
1215acaefc25SArnd Bergmann attr->fmt = fmt;
12167cf34c76SIngo Molnar mutex_init(&attr->mutex);
1217acaefc25SArnd Bergmann
1218acaefc25SArnd Bergmann file->private_data = attr;
1219acaefc25SArnd Bergmann
1220acaefc25SArnd Bergmann return nonseekable_open(inode, file);
1221acaefc25SArnd Bergmann }
122212f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open);
1223acaefc25SArnd Bergmann
simple_attr_release(struct inode * inode,struct file * file)122474bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file)
1225acaefc25SArnd Bergmann {
1226acaefc25SArnd Bergmann kfree(file->private_data);
1227acaefc25SArnd Bergmann return 0;
1228acaefc25SArnd Bergmann }
122912f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
1230acaefc25SArnd Bergmann
1231acaefc25SArnd Bergmann /* 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)1232acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf,
1233acaefc25SArnd Bergmann size_t len, loff_t *ppos)
1234acaefc25SArnd Bergmann {
1235acaefc25SArnd Bergmann struct simple_attr *attr;
1236acaefc25SArnd Bergmann size_t size;
1237acaefc25SArnd Bergmann ssize_t ret;
1238acaefc25SArnd Bergmann
1239acaefc25SArnd Bergmann attr = file->private_data;
1240acaefc25SArnd Bergmann
1241acaefc25SArnd Bergmann if (!attr->get)
1242acaefc25SArnd Bergmann return -EACCES;
1243acaefc25SArnd Bergmann
12449261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex);
12459261303aSChristoph Hellwig if (ret)
12469261303aSChristoph Hellwig return ret;
12479261303aSChristoph Hellwig
1248a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) {
1249a65cab7dSEric Biggers /* continued read */
1250acaefc25SArnd Bergmann size = strlen(attr->get_buf);
1251a65cab7dSEric Biggers } else {
1252a65cab7dSEric Biggers /* first read */
12538b88b099SChristoph Hellwig u64 val;
12548b88b099SChristoph Hellwig ret = attr->get(attr->data, &val);
12558b88b099SChristoph Hellwig if (ret)
12568b88b099SChristoph Hellwig goto out;
12578b88b099SChristoph Hellwig
1258acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
12598b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val);
12608b88b099SChristoph Hellwig }
1261acaefc25SArnd Bergmann
1262acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
12638b88b099SChristoph Hellwig out:
12647cf34c76SIngo Molnar mutex_unlock(&attr->mutex);
1265acaefc25SArnd Bergmann return ret;
1266acaefc25SArnd Bergmann }
126712f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read);
1268acaefc25SArnd Bergmann
1269acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */
simple_attr_write_xsigned(struct file * file,const char __user * buf,size_t len,loff_t * ppos,bool is_signed)12702e41f274SAkinobu Mita static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
12712e41f274SAkinobu Mita size_t len, loff_t *ppos, bool is_signed)
1272acaefc25SArnd Bergmann {
1273acaefc25SArnd Bergmann struct simple_attr *attr;
1274488dac0cSYicong Yang unsigned long long val;
1275acaefc25SArnd Bergmann size_t size;
1276acaefc25SArnd Bergmann ssize_t ret;
1277acaefc25SArnd Bergmann
1278acaefc25SArnd Bergmann attr = file->private_data;
1279acaefc25SArnd Bergmann if (!attr->set)
1280acaefc25SArnd Bergmann return -EACCES;
1281acaefc25SArnd Bergmann
12829261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex);
12839261303aSChristoph Hellwig if (ret)
12849261303aSChristoph Hellwig return ret;
12859261303aSChristoph Hellwig
1286acaefc25SArnd Bergmann ret = -EFAULT;
1287acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len);
1288acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size))
1289acaefc25SArnd Bergmann goto out;
1290acaefc25SArnd Bergmann
1291acaefc25SArnd Bergmann attr->set_buf[size] = '\0';
12922e41f274SAkinobu Mita if (is_signed)
12932e41f274SAkinobu Mita ret = kstrtoll(attr->set_buf, 0, &val);
12942e41f274SAkinobu Mita else
1295488dac0cSYicong Yang ret = kstrtoull(attr->set_buf, 0, &val);
1296488dac0cSYicong Yang if (ret)
1297488dac0cSYicong Yang goto out;
129805cc0ceeSWu Fengguang ret = attr->set(attr->data, val);
129905cc0ceeSWu Fengguang if (ret == 0)
130005cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */
1301acaefc25SArnd Bergmann out:
13027cf34c76SIngo Molnar mutex_unlock(&attr->mutex);
1303acaefc25SArnd Bergmann return ret;
1304acaefc25SArnd Bergmann }
13052e41f274SAkinobu Mita
simple_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)13062e41f274SAkinobu Mita ssize_t simple_attr_write(struct file *file, const char __user *buf,
13072e41f274SAkinobu Mita size_t len, loff_t *ppos)
13082e41f274SAkinobu Mita {
13092e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, false);
13102e41f274SAkinobu Mita }
131112f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write);
1312acaefc25SArnd Bergmann
simple_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)13132e41f274SAkinobu Mita ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
13142e41f274SAkinobu Mita size_t len, loff_t *ppos)
13152e41f274SAkinobu Mita {
13162e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, true);
13172e41f274SAkinobu Mita }
13182e41f274SAkinobu Mita EXPORT_SYMBOL_GPL(simple_attr_write_signed);
13192e41f274SAkinobu Mita
13202596110aSChristoph Hellwig /**
13212596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
13222596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on
13232596110aSChristoph Hellwig * @fid: file handle to convert
13242596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes
13252596110aSChristoph Hellwig * @fh_type: type of file handle
13262596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode
13272596110aSChristoph Hellwig *
13282596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known
13292596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the
13302596110aSChristoph Hellwig * inode for the object specified in the file handle.
13312596110aSChristoph Hellwig */
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))13322596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
13332596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode)
13342596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen))
13352596110aSChristoph Hellwig {
13362596110aSChristoph Hellwig struct inode *inode = NULL;
13372596110aSChristoph Hellwig
13382596110aSChristoph Hellwig if (fh_len < 2)
13392596110aSChristoph Hellwig return NULL;
13402596110aSChristoph Hellwig
13412596110aSChristoph Hellwig switch (fh_type) {
13422596110aSChristoph Hellwig case FILEID_INO32_GEN:
13432596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT:
13442596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
13452596110aSChristoph Hellwig break;
13462596110aSChristoph Hellwig }
13472596110aSChristoph Hellwig
13484ea3ada2SChristoph Hellwig return d_obtain_alias(inode);
13492596110aSChristoph Hellwig }
13502596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
13512596110aSChristoph Hellwig
13522596110aSChristoph Hellwig /**
1353ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation
13542596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on
13552596110aSChristoph Hellwig * @fid: file handle to convert
13562596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes
13572596110aSChristoph Hellwig * @fh_type: type of file handle
13582596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode
13592596110aSChristoph Hellwig *
13602596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known
13612596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the
13622596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it
13632596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise.
13642596110aSChristoph Hellwig */
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))13652596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
13662596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode)
13672596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen))
13682596110aSChristoph Hellwig {
13692596110aSChristoph Hellwig struct inode *inode = NULL;
13702596110aSChristoph Hellwig
13712596110aSChristoph Hellwig if (fh_len <= 2)
13722596110aSChristoph Hellwig return NULL;
13732596110aSChristoph Hellwig
13742596110aSChristoph Hellwig switch (fh_type) {
13752596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT:
13762596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino,
13772596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0));
13782596110aSChristoph Hellwig break;
13792596110aSChristoph Hellwig }
13802596110aSChristoph Hellwig
13814ea3ada2SChristoph Hellwig return d_obtain_alias(inode);
13822596110aSChristoph Hellwig }
13832596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent);
13842596110aSChristoph Hellwig
13851b061d92SChristoph Hellwig /**
1386ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems
1387ac13a829SFabian Frederick *
13881b061d92SChristoph Hellwig * @file: file to synchronize
1389ac13a829SFabian Frederick * @start: start offset in bytes
1390ac13a829SFabian Frederick * @end: end offset in bytes (inclusive)
13911b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true
13921b061d92SChristoph Hellwig *
13931b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple
13941b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list
13951b061d92SChristoph Hellwig * hanging off the address_space structure.
13961b061d92SChristoph Hellwig */
__generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1397ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
139802c24a82SJosef Bacik int datasync)
1399d5aacad5SAl Viro {
14007ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host;
1401d5aacad5SAl Viro int err;
1402d5aacad5SAl Viro int ret;
1403d5aacad5SAl Viro
1404383aa543SJeff Layton err = file_write_and_wait_range(file, start, end);
140502c24a82SJosef Bacik if (err)
140602c24a82SJosef Bacik return err;
140702c24a82SJosef Bacik
14085955102cSAl Viro inode_lock(inode);
1409d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping);
14100ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL))
141102c24a82SJosef Bacik goto out;
1412d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
141302c24a82SJosef Bacik goto out;
1414d5aacad5SAl Viro
1415c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1);
1416d5aacad5SAl Viro if (ret == 0)
1417d5aacad5SAl Viro ret = err;
1418ac13a829SFabian Frederick
141902c24a82SJosef Bacik out:
14205955102cSAl Viro inode_unlock(inode);
1421383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */
1422383aa543SJeff Layton err = file_check_and_advance_wb_err(file);
1423383aa543SJeff Layton if (ret == 0)
1424383aa543SJeff Layton ret = err;
1425383aa543SJeff Layton return ret;
1426d5aacad5SAl Viro }
1427ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync);
1428ac13a829SFabian Frederick
1429ac13a829SFabian Frederick /**
1430ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems
1431ac13a829SFabian Frederick * with flush
1432ac13a829SFabian Frederick * @file: file to synchronize
1433ac13a829SFabian Frederick * @start: start offset in bytes
1434ac13a829SFabian Frederick * @end: end offset in bytes (inclusive)
1435ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true
1436ac13a829SFabian Frederick *
1437ac13a829SFabian Frederick */
1438ac13a829SFabian Frederick
generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1439ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1440ac13a829SFabian Frederick int datasync)
1441ac13a829SFabian Frederick {
1442ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host;
1443ac13a829SFabian Frederick int err;
1444ac13a829SFabian Frederick
1445ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync);
1446ac13a829SFabian Frederick if (err)
1447ac13a829SFabian Frederick return err;
1448c6bf3f0eSChristoph Hellwig return blkdev_issue_flush(inode->i_sb->s_bdev);
1449ac13a829SFabian Frederick }
14501b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync);
14511b061d92SChristoph Hellwig
145230ca22c7SPatrick J. LoPresti /**
145330ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system
145430ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size
145530ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system
145630ca22c7SPatrick J. LoPresti *
145730ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a
145830ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t
145930ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise.
146030ca22c7SPatrick J. LoPresti */
generic_check_addressable(unsigned blocksize_bits,u64 num_blocks)146130ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
146230ca22c7SPatrick J. LoPresti {
146330ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1;
1464a33f13efSJoel Becker u64 last_fs_page =
146509cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits);
146630ca22c7SPatrick J. LoPresti
146730ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0))
146830ca22c7SPatrick J. LoPresti return 0;
146930ca22c7SPatrick J. LoPresti
147009cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
147130ca22c7SPatrick J. LoPresti return -EINVAL;
147230ca22c7SPatrick J. LoPresti
1473a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1474a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) {
147530ca22c7SPatrick J. LoPresti return -EFBIG;
147630ca22c7SPatrick J. LoPresti }
147730ca22c7SPatrick J. LoPresti return 0;
147830ca22c7SPatrick J. LoPresti }
147930ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable);
148030ca22c7SPatrick J. LoPresti
14811b061d92SChristoph Hellwig /*
14821b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems.
14831b061d92SChristoph Hellwig */
noop_fsync(struct file * file,loff_t start,loff_t end,int datasync)148402c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
14851b061d92SChristoph Hellwig {
14861b061d92SChristoph Hellwig return 0;
14871b061d92SChristoph Hellwig }
14881b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync);
148987dc800bSAl Viro
noop_direct_IO(struct kiocb * iocb,struct iov_iter * iter)1490f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1491f44c7763SDan Williams {
1492f44c7763SDan Williams /*
1493f44c7763SDan Williams * iomap based filesystems support direct I/O without need for
1494f44c7763SDan Williams * this callback. However, it still needs to be set in
1495f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is
1496f44c7763SDan Williams * generally supported.
1497f44c7763SDan Williams */
1498f44c7763SDan Williams return -EINVAL;
1499f44c7763SDan Williams }
1500f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO);
1501f44c7763SDan Williams
1502fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
kfree_link(void * p)1503fceef393SAl Viro void kfree_link(void *p)
150487dc800bSAl Viro {
1505fceef393SAl Viro kfree(p);
150687dc800bSAl Viro }
1507fceef393SAl Viro EXPORT_SYMBOL(kfree_link);
15086987843fSAl Viro
alloc_anon_inode(struct super_block * s)15096987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s)
15106987843fSAl Viro {
15116987843fSAl Viro static const struct address_space_operations anon_aops = {
151246de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio,
15136987843fSAl Viro };
15146987843fSAl Viro struct inode *inode = new_inode_pseudo(s);
15156987843fSAl Viro
15166987843fSAl Viro if (!inode)
15176987843fSAl Viro return ERR_PTR(-ENOMEM);
15186987843fSAl Viro
15196987843fSAl Viro inode->i_ino = get_next_ino();
15206987843fSAl Viro inode->i_mapping->a_ops = &anon_aops;
15216987843fSAl Viro
15226987843fSAl Viro /*
15236987843fSAl Viro * Mark the inode dirty from the very beginning,
15246987843fSAl Viro * that way it will never be moved to the dirty
15256987843fSAl Viro * list because mark_inode_dirty() will think
15266987843fSAl Viro * that it already _is_ on the dirty list.
15276987843fSAl Viro */
15286987843fSAl Viro inode->i_state = I_DIRTY;
15296987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR;
15306987843fSAl Viro inode->i_uid = current_fsuid();
15316987843fSAl Viro inode->i_gid = current_fsgid();
15326987843fSAl Viro inode->i_flags |= S_PRIVATE;
1533*5b5599a7SJeff Layton simple_inode_init_ts(inode);
15346987843fSAl Viro return inode;
15356987843fSAl Viro }
15366987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode);
15371c994a09SJeff Layton
15381c994a09SJeff Layton /**
15391c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases
15401c994a09SJeff Layton * @filp: file pointer
15411c994a09SJeff Layton * @arg: type of lease to obtain
15421c994a09SJeff Layton * @flp: new lease supplied for insertion
1543e6f5c789SJeff Layton * @priv: private data for lm_setup operation
15441c994a09SJeff Layton *
15451c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set.
15461c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL.
15471c994a09SJeff Layton */
15481c994a09SJeff Layton int
simple_nosetlease(struct file * filp,int arg,struct file_lock ** flp,void ** priv)1549ed5f17f6SLuca Vizzarro simple_nosetlease(struct file *filp, int arg, struct file_lock **flp,
1550e6f5c789SJeff Layton void **priv)
15511c994a09SJeff Layton {
15521c994a09SJeff Layton return -EINVAL;
15531c994a09SJeff Layton }
15541c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease);
155561ba64fcSAl Viro
15566ee9706aSEric Biggers /**
15576ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks
15586ee9706aSEric Biggers * @dentry: not used here
15596ee9706aSEric Biggers * @inode: the symlink inode
15606ee9706aSEric Biggers * @done: not used here
15616ee9706aSEric Biggers *
15626ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to
15636ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
15646ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link
15656ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set,
15666ee9706aSEric Biggers * to mark the inode_operations as being for a symlink.
15676ee9706aSEric Biggers *
15686ee9706aSEric Biggers * Return: the symlink target
15696ee9706aSEric Biggers */
simple_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)15706b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1571fceef393SAl Viro struct delayed_call *done)
157261ba64fcSAl Viro {
15736b255391SAl Viro return inode->i_link;
157461ba64fcSAl Viro }
15756b255391SAl Viro EXPORT_SYMBOL(simple_get_link);
157661ba64fcSAl Viro
157761ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = {
15786b255391SAl Viro .get_link = simple_get_link,
157961ba64fcSAl Viro };
158061ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations);
1581fbabfd0fSEric W. Biederman
1582fbabfd0fSEric W. Biederman /*
1583fbabfd0fSEric W. Biederman * Operations for a permanently empty directory.
1584fbabfd0fSEric W. Biederman */
empty_dir_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1585fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1586fbabfd0fSEric W. Biederman {
1587fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT);
1588fbabfd0fSEric W. Biederman }
1589fbabfd0fSEric W. Biederman
empty_dir_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)1590b74d24f7SChristian Brauner static int empty_dir_getattr(struct mnt_idmap *idmap,
1591549c7297SChristian Brauner const struct path *path, struct kstat *stat,
1592a528d35eSDavid Howells u32 request_mask, unsigned int query_flags)
1593fbabfd0fSEric W. Biederman {
1594a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
15950d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1596fbabfd0fSEric W. Biederman return 0;
1597fbabfd0fSEric W. Biederman }
1598fbabfd0fSEric W. Biederman
empty_dir_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)1599c1632a0fSChristian Brauner static int empty_dir_setattr(struct mnt_idmap *idmap,
1600549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr)
1601fbabfd0fSEric W. Biederman {
1602fbabfd0fSEric W. Biederman return -EPERM;
1603fbabfd0fSEric W. Biederman }
1604fbabfd0fSEric W. Biederman
empty_dir_listxattr(struct dentry * dentry,char * list,size_t size)1605fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1606fbabfd0fSEric W. Biederman {
1607fbabfd0fSEric W. Biederman return -EOPNOTSUPP;
1608fbabfd0fSEric W. Biederman }
1609fbabfd0fSEric W. Biederman
1610fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = {
1611fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup,
1612fbabfd0fSEric W. Biederman .permission = generic_permission,
1613fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr,
1614fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr,
1615fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr,
1616fbabfd0fSEric W. Biederman };
1617fbabfd0fSEric W. Biederman
empty_dir_llseek(struct file * file,loff_t offset,int whence)1618fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1619fbabfd0fSEric W. Biederman {
1620fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */
1621fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2);
1622fbabfd0fSEric W. Biederman }
1623fbabfd0fSEric W. Biederman
empty_dir_readdir(struct file * file,struct dir_context * ctx)1624fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1625fbabfd0fSEric W. Biederman {
1626fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx);
1627fbabfd0fSEric W. Biederman return 0;
1628fbabfd0fSEric W. Biederman }
1629fbabfd0fSEric W. Biederman
1630fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = {
1631fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek,
1632fbabfd0fSEric W. Biederman .read = generic_read_dir,
1633c51da20cSAl Viro .iterate_shared = empty_dir_readdir,
1634fbabfd0fSEric W. Biederman .fsync = noop_fsync,
1635fbabfd0fSEric W. Biederman };
1636fbabfd0fSEric W. Biederman
1637fbabfd0fSEric W. Biederman
make_empty_dir_inode(struct inode * inode)1638fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode)
1639fbabfd0fSEric W. Biederman {
1640fbabfd0fSEric W. Biederman set_nlink(inode, 2);
1641fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1642fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID;
1643fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID;
1644fbabfd0fSEric W. Biederman inode->i_rdev = 0;
16454b75de86SEric W. Biederman inode->i_size = 0;
1646fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT;
1647fbabfd0fSEric W. Biederman inode->i_blocks = 0;
1648fbabfd0fSEric W. Biederman
1649fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations;
1650f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR;
1651fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations;
1652fbabfd0fSEric W. Biederman }
1653fbabfd0fSEric W. Biederman
is_empty_dir_inode(struct inode * inode)1654fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode)
1655fbabfd0fSEric W. Biederman {
1656fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) &&
1657fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations);
1658fbabfd0fSEric W. Biederman }
1659c843843eSDaniel Rosenberg
16605298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1661c843843eSDaniel Rosenberg /**
1662c843843eSDaniel Rosenberg * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1663c843843eSDaniel Rosenberg * @dentry: dentry whose name we are checking against
1664c843843eSDaniel Rosenberg * @len: len of name of dentry
1665c843843eSDaniel Rosenberg * @str: str pointer to name of dentry
1666c843843eSDaniel Rosenberg * @name: Name to compare against
1667c843843eSDaniel Rosenberg *
1668c843843eSDaniel Rosenberg * Return: 0 if names match, 1 if mismatch, or -ERRNO
1669c843843eSDaniel Rosenberg */
generic_ci_d_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)1670794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1671c843843eSDaniel Rosenberg const char *str, const struct qstr *name)
1672c843843eSDaniel Rosenberg {
1673c843843eSDaniel Rosenberg const struct dentry *parent = READ_ONCE(dentry->d_parent);
1674c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(parent->d_inode);
1675c843843eSDaniel Rosenberg const struct super_block *sb = dentry->d_sb;
1676c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding;
1677c843843eSDaniel Rosenberg struct qstr qstr = QSTR_INIT(str, len);
1678c843843eSDaniel Rosenberg char strbuf[DNAME_INLINE_LEN];
1679c843843eSDaniel Rosenberg int ret;
1680c843843eSDaniel Rosenberg
1681af494af3SEric Biggers if (!dir || !IS_CASEFOLDED(dir))
1682c843843eSDaniel Rosenberg goto fallback;
1683c843843eSDaniel Rosenberg /*
1684c843843eSDaniel Rosenberg * If the dentry name is stored in-line, then it may be concurrently
1685c843843eSDaniel Rosenberg * modified by a rename. If this happens, the VFS will eventually retry
1686c843843eSDaniel Rosenberg * the lookup, so it doesn't matter what ->d_compare() returns.
1687c843843eSDaniel Rosenberg * However, it's unsafe to call utf8_strncasecmp() with an unstable
1688c843843eSDaniel Rosenberg * string. Therefore, we have to copy the name into a temporary buffer.
1689c843843eSDaniel Rosenberg */
1690c843843eSDaniel Rosenberg if (len <= DNAME_INLINE_LEN - 1) {
1691c843843eSDaniel Rosenberg memcpy(strbuf, str, len);
1692c843843eSDaniel Rosenberg strbuf[len] = 0;
1693c843843eSDaniel Rosenberg qstr.name = strbuf;
1694c843843eSDaniel Rosenberg /* prevent compiler from optimizing out the temporary buffer */
1695c843843eSDaniel Rosenberg barrier();
1696c843843eSDaniel Rosenberg }
1697c843843eSDaniel Rosenberg ret = utf8_strncasecmp(um, name, &qstr);
1698c843843eSDaniel Rosenberg if (ret >= 0)
1699c843843eSDaniel Rosenberg return ret;
1700c843843eSDaniel Rosenberg
1701c843843eSDaniel Rosenberg if (sb_has_strict_encoding(sb))
1702c843843eSDaniel Rosenberg return -EINVAL;
1703c843843eSDaniel Rosenberg fallback:
1704c843843eSDaniel Rosenberg if (len != name->len)
1705c843843eSDaniel Rosenberg return 1;
1706c843843eSDaniel Rosenberg return !!memcmp(str, name->name, len);
1707c843843eSDaniel Rosenberg }
1708c843843eSDaniel Rosenberg
1709c843843eSDaniel Rosenberg /**
1710c843843eSDaniel Rosenberg * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1711c843843eSDaniel Rosenberg * @dentry: dentry of the parent directory
1712c843843eSDaniel Rosenberg * @str: qstr of name whose hash we should fill in
1713c843843eSDaniel Rosenberg *
1714c843843eSDaniel Rosenberg * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1715c843843eSDaniel Rosenberg */
generic_ci_d_hash(const struct dentry * dentry,struct qstr * str)1716794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1717c843843eSDaniel Rosenberg {
1718c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(dentry->d_inode);
1719c843843eSDaniel Rosenberg struct super_block *sb = dentry->d_sb;
1720c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding;
1721c843843eSDaniel Rosenberg int ret = 0;
1722c843843eSDaniel Rosenberg
1723af494af3SEric Biggers if (!dir || !IS_CASEFOLDED(dir))
1724c843843eSDaniel Rosenberg return 0;
1725c843843eSDaniel Rosenberg
1726c843843eSDaniel Rosenberg ret = utf8_casefold_hash(um, dentry, str);
1727c843843eSDaniel Rosenberg if (ret < 0 && sb_has_strict_encoding(sb))
1728c843843eSDaniel Rosenberg return -EINVAL;
1729c843843eSDaniel Rosenberg return 0;
1730c843843eSDaniel Rosenberg }
1731608af703SDaniel Rosenberg
1732608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = {
1733608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash,
1734608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare,
1735608af703SDaniel Rosenberg };
1736c843843eSDaniel Rosenberg #endif
1737608af703SDaniel Rosenberg
1738608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1739608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = {
1740608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate,
1741608af703SDaniel Rosenberg };
1742608af703SDaniel Rosenberg #endif
1743608af703SDaniel Rosenberg
17445298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1745608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1746608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash,
1747608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare,
1748608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate,
1749608af703SDaniel Rosenberg };
1750608af703SDaniel Rosenberg #endif
1751608af703SDaniel Rosenberg
1752608af703SDaniel Rosenberg /**
1753608af703SDaniel Rosenberg * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1754608af703SDaniel Rosenberg * @dentry: dentry to set ops on
1755608af703SDaniel Rosenberg *
1756608af703SDaniel Rosenberg * Casefolded directories need d_hash and d_compare set, so that the dentries
1757608af703SDaniel Rosenberg * contained in them are handled case-insensitively. Note that these operations
1758608af703SDaniel Rosenberg * are needed on the parent directory rather than on the dentries in it, and
1759608af703SDaniel Rosenberg * while the casefolding flag can be toggled on and off on an empty directory,
1760608af703SDaniel Rosenberg * dentry_operations can't be changed later. As a result, if the filesystem has
1761608af703SDaniel Rosenberg * casefolding support enabled at all, we have to give all dentries the
1762608af703SDaniel Rosenberg * casefolding operations even if their inode doesn't have the casefolding flag
1763608af703SDaniel Rosenberg * currently (and thus the casefolding ops would be no-ops for now).
1764608af703SDaniel Rosenberg *
1765608af703SDaniel Rosenberg * Encryption works differently in that the only dentry operation it needs is
1766608af703SDaniel Rosenberg * d_revalidate, which it only needs on dentries that have the no-key name flag.
1767608af703SDaniel Rosenberg * The no-key flag can't be set "later", so we don't have to worry about that.
1768608af703SDaniel Rosenberg *
1769608af703SDaniel Rosenberg * Finally, to maximize compatibility with overlayfs (which isn't compatible
1770608af703SDaniel Rosenberg * with certain dentry operations) and to avoid taking an unnecessary
1771608af703SDaniel Rosenberg * performance hit, we use custom dentry_operations for each possible
1772608af703SDaniel Rosenberg * combination rather than always installing all operations.
1773608af703SDaniel Rosenberg */
generic_set_encrypted_ci_d_ops(struct dentry * dentry)1774608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
1775608af703SDaniel Rosenberg {
1776608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1777608af703SDaniel Rosenberg bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
1778608af703SDaniel Rosenberg #endif
17795298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1780608af703SDaniel Rosenberg bool needs_ci_ops = dentry->d_sb->s_encoding;
1781608af703SDaniel Rosenberg #endif
17825298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1783608af703SDaniel Rosenberg if (needs_encrypt_ops && needs_ci_ops) {
1784608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1785608af703SDaniel Rosenberg return;
1786608af703SDaniel Rosenberg }
1787608af703SDaniel Rosenberg #endif
1788608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1789608af703SDaniel Rosenberg if (needs_encrypt_ops) {
1790608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1791608af703SDaniel Rosenberg return;
1792608af703SDaniel Rosenberg }
1793608af703SDaniel Rosenberg #endif
17945298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1795608af703SDaniel Rosenberg if (needs_ci_ops) {
1796608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_ci_dentry_ops);
1797608af703SDaniel Rosenberg return;
1798608af703SDaniel Rosenberg }
1799608af703SDaniel Rosenberg #endif
1800608af703SDaniel Rosenberg }
1801608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);
18025ca14835SAndrew Morton
18035ca14835SAndrew Morton /**
18045ca14835SAndrew Morton * inode_maybe_inc_iversion - increments i_version
18055ca14835SAndrew Morton * @inode: inode with the i_version that should be updated
18065ca14835SAndrew Morton * @force: increment the counter even if it's not necessary?
18075ca14835SAndrew Morton *
18085ca14835SAndrew Morton * Every time the inode is modified, the i_version field must be seen to have
18095ca14835SAndrew Morton * changed by any observer.
18105ca14835SAndrew Morton *
18115ca14835SAndrew Morton * If "force" is set or the QUERIED flag is set, then ensure that we increment
18125ca14835SAndrew Morton * the value, and clear the queried flag.
18135ca14835SAndrew Morton *
18145ca14835SAndrew Morton * In the common case where neither is set, then we can return "false" without
18155ca14835SAndrew Morton * updating i_version.
18165ca14835SAndrew Morton *
18175ca14835SAndrew Morton * If this function returns false, and no other metadata has changed, then we
18185ca14835SAndrew Morton * can avoid logging the metadata.
18195ca14835SAndrew Morton */
inode_maybe_inc_iversion(struct inode * inode,bool force)18205ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force)
18215ca14835SAndrew Morton {
18225ca14835SAndrew Morton u64 cur, new;
18235ca14835SAndrew Morton
18245ca14835SAndrew Morton /*
18255ca14835SAndrew Morton * The i_version field is not strictly ordered with any other inode
18265ca14835SAndrew Morton * information, but the legacy inode_inc_iversion code used a spinlock
18275ca14835SAndrew Morton * to serialize increments.
18285ca14835SAndrew Morton *
18295ca14835SAndrew Morton * Here, we add full memory barriers to ensure that any de-facto
18305ca14835SAndrew Morton * ordering with other info is preserved.
18315ca14835SAndrew Morton *
18325ca14835SAndrew Morton * This barrier pairs with the barrier in inode_query_iversion()
18335ca14835SAndrew Morton */
18345ca14835SAndrew Morton smp_mb();
18355ca14835SAndrew Morton cur = inode_peek_iversion_raw(inode);
18365ca14835SAndrew Morton do {
18375ca14835SAndrew Morton /* If flag is clear then we needn't do anything */
18385ca14835SAndrew Morton if (!force && !(cur & I_VERSION_QUERIED))
18395ca14835SAndrew Morton return false;
18405ca14835SAndrew Morton
18415ca14835SAndrew Morton /* Since lowest bit is flag, add 2 to avoid it */
18425ca14835SAndrew Morton new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT;
18435ca14835SAndrew Morton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
18445ca14835SAndrew Morton return true;
18455ca14835SAndrew Morton }
18465ca14835SAndrew Morton EXPORT_SYMBOL(inode_maybe_inc_iversion);
1847c5bc1b3fSJeff Layton
1848c5bc1b3fSJeff Layton /**
1849c5bc1b3fSJeff Layton * inode_query_iversion - read i_version for later use
1850c5bc1b3fSJeff Layton * @inode: inode from which i_version should be read
1851c5bc1b3fSJeff Layton *
1852c5bc1b3fSJeff Layton * Read the inode i_version counter. This should be used by callers that wish
1853c5bc1b3fSJeff Layton * to store the returned i_version for later comparison. This will guarantee
1854c5bc1b3fSJeff Layton * that a later query of the i_version will result in a different value if
1855c5bc1b3fSJeff Layton * anything has changed.
1856c5bc1b3fSJeff Layton *
1857c5bc1b3fSJeff Layton * In this implementation, we fetch the current value, set the QUERIED flag and
1858c5bc1b3fSJeff Layton * then try to swap it into place with a cmpxchg, if it wasn't already set. If
1859c5bc1b3fSJeff Layton * that fails, we try again with the newly fetched value from the cmpxchg.
1860c5bc1b3fSJeff Layton */
inode_query_iversion(struct inode * inode)1861c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode)
1862c5bc1b3fSJeff Layton {
1863c5bc1b3fSJeff Layton u64 cur, new;
1864c5bc1b3fSJeff Layton
1865c5bc1b3fSJeff Layton cur = inode_peek_iversion_raw(inode);
1866c5bc1b3fSJeff Layton do {
1867c5bc1b3fSJeff Layton /* If flag is already set, then no need to swap */
1868c5bc1b3fSJeff Layton if (cur & I_VERSION_QUERIED) {
1869c5bc1b3fSJeff Layton /*
1870c5bc1b3fSJeff Layton * This barrier (and the implicit barrier in the
1871c5bc1b3fSJeff Layton * cmpxchg below) pairs with the barrier in
1872c5bc1b3fSJeff Layton * inode_maybe_inc_iversion().
1873c5bc1b3fSJeff Layton */
1874c5bc1b3fSJeff Layton smp_mb();
1875c5bc1b3fSJeff Layton break;
1876c5bc1b3fSJeff Layton }
1877c5bc1b3fSJeff Layton
1878c5bc1b3fSJeff Layton new = cur | I_VERSION_QUERIED;
1879c5bc1b3fSJeff Layton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
1880c5bc1b3fSJeff Layton return cur >> I_VERSION_QUERIED_SHIFT;
1881c5bc1b3fSJeff Layton }
1882c5bc1b3fSJeff Layton EXPORT_SYMBOL(inode_query_iversion);
188344fff0faSChristoph Hellwig
direct_write_fallback(struct kiocb * iocb,struct iov_iter * iter,ssize_t direct_written,ssize_t buffered_written)188444fff0faSChristoph Hellwig ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
188544fff0faSChristoph Hellwig ssize_t direct_written, ssize_t buffered_written)
188644fff0faSChristoph Hellwig {
188744fff0faSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
188844fff0faSChristoph Hellwig loff_t pos = iocb->ki_pos - buffered_written;
188944fff0faSChristoph Hellwig loff_t end = iocb->ki_pos - 1;
189044fff0faSChristoph Hellwig int err;
189144fff0faSChristoph Hellwig
189244fff0faSChristoph Hellwig /*
189344fff0faSChristoph Hellwig * If the buffered write fallback returned an error, we want to return
189444fff0faSChristoph Hellwig * the number of bytes which were written by direct I/O, or the error
189544fff0faSChristoph Hellwig * code if that was zero.
189644fff0faSChristoph Hellwig *
189744fff0faSChristoph Hellwig * Note that this differs from normal direct-io semantics, which will
189844fff0faSChristoph Hellwig * return -EFOO even if some bytes were written.
189944fff0faSChristoph Hellwig */
190044fff0faSChristoph Hellwig if (unlikely(buffered_written < 0)) {
190144fff0faSChristoph Hellwig if (direct_written)
190244fff0faSChristoph Hellwig return direct_written;
190344fff0faSChristoph Hellwig return buffered_written;
190444fff0faSChristoph Hellwig }
190544fff0faSChristoph Hellwig
190644fff0faSChristoph Hellwig /*
190744fff0faSChristoph Hellwig * We need to ensure that the page cache pages are written to disk and
190844fff0faSChristoph Hellwig * invalidated to preserve the expected O_DIRECT semantics.
190944fff0faSChristoph Hellwig */
191044fff0faSChristoph Hellwig err = filemap_write_and_wait_range(mapping, pos, end);
191144fff0faSChristoph Hellwig if (err < 0) {
191244fff0faSChristoph Hellwig /*
191344fff0faSChristoph Hellwig * We don't know how much we wrote, so just return the number of
191444fff0faSChristoph Hellwig * bytes which were direct-written
191544fff0faSChristoph Hellwig */
19168287474aSAl Viro iocb->ki_pos -= buffered_written;
191744fff0faSChristoph Hellwig if (direct_written)
191844fff0faSChristoph Hellwig return direct_written;
191944fff0faSChristoph Hellwig return err;
192044fff0faSChristoph Hellwig }
192144fff0faSChristoph Hellwig invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
192244fff0faSChristoph Hellwig return direct_written + buffered_written;
192344fff0faSChristoph Hellwig }
192444fff0faSChristoph Hellwig EXPORT_SYMBOL_GPL(direct_write_fallback);
1925*5b5599a7SJeff Layton
1926*5b5599a7SJeff Layton /**
1927*5b5599a7SJeff Layton * simple_inode_init_ts - initialize the timestamps for a new inode
1928*5b5599a7SJeff Layton * @inode: inode to be initialized
1929*5b5599a7SJeff Layton *
1930*5b5599a7SJeff Layton * When a new inode is created, most filesystems set the timestamps to the
1931*5b5599a7SJeff Layton * current time. Add a helper to do this.
1932*5b5599a7SJeff Layton */
simple_inode_init_ts(struct inode * inode)1933*5b5599a7SJeff Layton struct timespec64 simple_inode_init_ts(struct inode *inode)
1934*5b5599a7SJeff Layton {
1935*5b5599a7SJeff Layton struct timespec64 ts = inode_set_ctime_current(inode);
1936*5b5599a7SJeff Layton
1937*5b5599a7SJeff Layton inode_set_atime_to_ts(inode, ts);
1938*5b5599a7SJeff Layton inode_set_mtime_to_ts(inode, ts);
1939*5b5599a7SJeff Layton return ts;
1940*5b5599a7SJeff Layton }
1941*5b5599a7SJeff Layton EXPORT_SYMBOL(simple_inode_init_ts);
1942