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
2420f03dd06SChuck Lever /* simple_offset_add() never assigns these to a dentry */
2430f03dd06SChuck Lever enum {
244*850e696fSChuck Lever DIR_OFFSET_FIRST = 2, /* Find first real entry */
2450f03dd06SChuck Lever DIR_OFFSET_EOD = S32_MAX,
2460f03dd06SChuck Lever };
2470f03dd06SChuck Lever
2480f03dd06SChuck Lever /* simple_offset_add() allocation range */
249fc90bbccSChuck Lever enum {
250*850e696fSChuck Lever DIR_OFFSET_MIN = DIR_OFFSET_FIRST + 1,
2510f03dd06SChuck Lever DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1,
252fc90bbccSChuck Lever };
253fc90bbccSChuck Lever
offset_set(struct dentry * dentry,u32 offset)2546faddda6SChuck Lever static void offset_set(struct dentry *dentry, u32 offset)
2556faddda6SChuck Lever {
2566faddda6SChuck Lever dentry->d_fsdata = (void *)((uintptr_t)(offset));
2576faddda6SChuck Lever }
2586faddda6SChuck Lever
dentry2offset(struct dentry * dentry)2596faddda6SChuck Lever static u32 dentry2offset(struct dentry *dentry)
2606faddda6SChuck Lever {
2616faddda6SChuck Lever return (u32)((uintptr_t)(dentry->d_fsdata));
2626faddda6SChuck Lever }
2636faddda6SChuck Lever
264bbaef797SChuck Lever static struct lock_class_key simple_offset_xa_lock;
265bbaef797SChuck Lever
2666faddda6SChuck Lever /**
2676faddda6SChuck Lever * simple_offset_init - initialize an offset_ctx
2686faddda6SChuck Lever * @octx: directory offset map to be initialized
2696faddda6SChuck Lever *
2706faddda6SChuck Lever */
simple_offset_init(struct offset_ctx * octx)2716faddda6SChuck Lever void simple_offset_init(struct offset_ctx *octx)
2726faddda6SChuck Lever {
2736faddda6SChuck Lever xa_init_flags(&octx->xa, XA_FLAGS_ALLOC1);
274bbaef797SChuck Lever lockdep_set_class(&octx->xa.xa_lock, &simple_offset_xa_lock);
275fc90bbccSChuck Lever octx->next_offset = DIR_OFFSET_MIN;
2766faddda6SChuck Lever }
2776faddda6SChuck Lever
2786faddda6SChuck Lever /**
2796faddda6SChuck Lever * simple_offset_add - Add an entry to a directory's offset map
2806faddda6SChuck Lever * @octx: directory offset ctx to be updated
2816faddda6SChuck Lever * @dentry: new dentry being added
2826faddda6SChuck Lever *
2836faddda6SChuck Lever * Returns zero on success. @so_ctx and the dentry offset are updated.
2846faddda6SChuck Lever * Otherwise, a negative errno value is returned.
2856faddda6SChuck Lever */
simple_offset_add(struct offset_ctx * octx,struct dentry * dentry)2866faddda6SChuck Lever int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
2876faddda6SChuck Lever {
2880f03dd06SChuck Lever static const struct xa_limit limit = XA_LIMIT(DIR_OFFSET_MIN,
2890f03dd06SChuck Lever DIR_OFFSET_MAX);
2906faddda6SChuck Lever u32 offset;
2916faddda6SChuck Lever int ret;
2926faddda6SChuck Lever
2936faddda6SChuck Lever if (dentry2offset(dentry) != 0)
2946faddda6SChuck Lever return -EBUSY;
2956faddda6SChuck Lever
2966faddda6SChuck Lever ret = xa_alloc_cyclic(&octx->xa, &offset, dentry, limit,
2976faddda6SChuck Lever &octx->next_offset, GFP_KERNEL);
298a01bb1c5SChuck Lever if (unlikely(ret < 0))
299a01bb1c5SChuck Lever return ret == -EBUSY ? -ENOSPC : ret;
3006faddda6SChuck Lever
3016faddda6SChuck Lever offset_set(dentry, offset);
3026faddda6SChuck Lever return 0;
3036faddda6SChuck Lever }
3046faddda6SChuck Lever
simple_offset_replace(struct offset_ctx * octx,struct dentry * dentry,long offset)3053e716f31SChuck Lever static int simple_offset_replace(struct offset_ctx *octx, struct dentry *dentry,
3063e716f31SChuck Lever long offset)
3073e716f31SChuck Lever {
3083e716f31SChuck Lever void *ret;
3093e716f31SChuck Lever
3103e716f31SChuck Lever ret = xa_store(&octx->xa, offset, dentry, GFP_KERNEL);
3113e716f31SChuck Lever if (xa_is_err(ret))
3123e716f31SChuck Lever return xa_err(ret);
3133e716f31SChuck Lever offset_set(dentry, offset);
3143e716f31SChuck Lever return 0;
3153e716f31SChuck Lever }
3163e716f31SChuck Lever
3176faddda6SChuck Lever /**
3186faddda6SChuck Lever * simple_offset_remove - Remove an entry to a directory's offset map
3196faddda6SChuck Lever * @octx: directory offset ctx to be updated
3206faddda6SChuck Lever * @dentry: dentry being removed
3216faddda6SChuck Lever *
3226faddda6SChuck Lever */
simple_offset_remove(struct offset_ctx * octx,struct dentry * dentry)3236faddda6SChuck Lever void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)
3246faddda6SChuck Lever {
3256faddda6SChuck Lever u32 offset;
3266faddda6SChuck Lever
3276faddda6SChuck Lever offset = dentry2offset(dentry);
3286faddda6SChuck Lever if (offset == 0)
3296faddda6SChuck Lever return;
3306faddda6SChuck Lever
3316faddda6SChuck Lever xa_erase(&octx->xa, offset);
3326faddda6SChuck Lever offset_set(dentry, 0);
3336faddda6SChuck Lever }
3346faddda6SChuck Lever
3356faddda6SChuck Lever /**
336753828d6SChuck Lever * simple_offset_rename - handle directory offsets for rename
337753828d6SChuck Lever * @old_dir: parent directory of source entry
338753828d6SChuck Lever * @old_dentry: dentry of source entry
339753828d6SChuck Lever * @new_dir: parent_directory of destination entry
340753828d6SChuck Lever * @new_dentry: dentry of destination
341753828d6SChuck Lever *
342753828d6SChuck Lever * Caller provides appropriate serialization.
343753828d6SChuck Lever *
3442b6da3faSChuck Lever * User space expects the directory offset value of the replaced
3452b6da3faSChuck Lever * (new) directory entry to be unchanged after a rename.
3462b6da3faSChuck Lever *
347753828d6SChuck Lever * Returns zero on success, a negative errno value on failure.
348753828d6SChuck Lever */
simple_offset_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)349753828d6SChuck Lever int simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry,
350753828d6SChuck Lever struct inode *new_dir, struct dentry *new_dentry)
351753828d6SChuck Lever {
352753828d6SChuck Lever struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
353753828d6SChuck Lever struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
3542b6da3faSChuck Lever long new_offset = dentry2offset(new_dentry);
355753828d6SChuck Lever
356753828d6SChuck Lever simple_offset_remove(old_ctx, old_dentry);
3572b6da3faSChuck Lever
3582b6da3faSChuck Lever if (new_offset) {
3592b6da3faSChuck Lever offset_set(new_dentry, 0);
3602b6da3faSChuck Lever return simple_offset_replace(new_ctx, old_dentry, new_offset);
3612b6da3faSChuck Lever }
362753828d6SChuck Lever return simple_offset_add(new_ctx, old_dentry);
363753828d6SChuck Lever }
364753828d6SChuck Lever
365753828d6SChuck Lever /**
3666faddda6SChuck Lever * simple_offset_rename_exchange - exchange rename with directory offsets
3676faddda6SChuck Lever * @old_dir: parent of dentry being moved
3686faddda6SChuck Lever * @old_dentry: dentry being moved
3696faddda6SChuck Lever * @new_dir: destination parent
3706faddda6SChuck Lever * @new_dentry: destination dentry
3716faddda6SChuck Lever *
3723e716f31SChuck Lever * This API preserves the directory offset values. Caller provides
3733e716f31SChuck Lever * appropriate serialization.
3743e716f31SChuck Lever *
3756faddda6SChuck Lever * Returns zero on success. Otherwise a negative errno is returned and the
3766faddda6SChuck Lever * rename is rolled back.
3776faddda6SChuck Lever */
simple_offset_rename_exchange(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)3786faddda6SChuck Lever int simple_offset_rename_exchange(struct inode *old_dir,
3796faddda6SChuck Lever struct dentry *old_dentry,
3806faddda6SChuck Lever struct inode *new_dir,
3816faddda6SChuck Lever struct dentry *new_dentry)
3826faddda6SChuck Lever {
3836faddda6SChuck Lever struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
3846faddda6SChuck Lever struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
3856faddda6SChuck Lever u32 old_index = dentry2offset(old_dentry);
3866faddda6SChuck Lever u32 new_index = dentry2offset(new_dentry);
3876faddda6SChuck Lever int ret;
3886faddda6SChuck Lever
3896faddda6SChuck Lever simple_offset_remove(old_ctx, old_dentry);
3906faddda6SChuck Lever simple_offset_remove(new_ctx, new_dentry);
3916faddda6SChuck Lever
3923e716f31SChuck Lever ret = simple_offset_replace(new_ctx, old_dentry, new_index);
3936faddda6SChuck Lever if (ret)
3946faddda6SChuck Lever goto out_restore;
3956faddda6SChuck Lever
3963e716f31SChuck Lever ret = simple_offset_replace(old_ctx, new_dentry, old_index);
3976faddda6SChuck Lever if (ret) {
3986faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry);
3996faddda6SChuck Lever goto out_restore;
4006faddda6SChuck Lever }
4016faddda6SChuck Lever
4026faddda6SChuck Lever ret = simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
4036faddda6SChuck Lever if (ret) {
4046faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry);
4056faddda6SChuck Lever simple_offset_remove(old_ctx, new_dentry);
4066faddda6SChuck Lever goto out_restore;
4076faddda6SChuck Lever }
4086faddda6SChuck Lever return 0;
4096faddda6SChuck Lever
4106faddda6SChuck Lever out_restore:
4113e716f31SChuck Lever (void)simple_offset_replace(old_ctx, old_dentry, old_index);
4123e716f31SChuck Lever (void)simple_offset_replace(new_ctx, new_dentry, new_index);
4136faddda6SChuck Lever return ret;
4146faddda6SChuck Lever }
4156faddda6SChuck Lever
4166faddda6SChuck Lever /**
4176faddda6SChuck Lever * simple_offset_destroy - Release offset map
4186faddda6SChuck Lever * @octx: directory offset ctx that is about to be destroyed
4196faddda6SChuck Lever *
4206faddda6SChuck Lever * During fs teardown (eg. umount), a directory's offset map might still
4216faddda6SChuck Lever * contain entries. xa_destroy() cleans out anything that remains.
4226faddda6SChuck Lever */
simple_offset_destroy(struct offset_ctx * octx)4236faddda6SChuck Lever void simple_offset_destroy(struct offset_ctx *octx)
4246faddda6SChuck Lever {
4256faddda6SChuck Lever xa_destroy(&octx->xa);
4266faddda6SChuck Lever }
4276faddda6SChuck Lever
4286faddda6SChuck Lever /**
4296faddda6SChuck Lever * offset_dir_llseek - Advance the read position of a directory descriptor
4306faddda6SChuck Lever * @file: an open directory whose position is to be updated
4316faddda6SChuck Lever * @offset: a byte offset
4326faddda6SChuck Lever * @whence: enumerator describing the starting position for this update
4336faddda6SChuck Lever *
4346faddda6SChuck Lever * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories.
4356faddda6SChuck Lever *
4366faddda6SChuck Lever * Returns the updated read position if successful; otherwise a
4376faddda6SChuck Lever * negative errno is returned and the read position remains unchanged.
4386faddda6SChuck Lever */
offset_dir_llseek(struct file * file,loff_t offset,int whence)4396faddda6SChuck Lever static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
4406faddda6SChuck Lever {
4416faddda6SChuck Lever switch (whence) {
4426faddda6SChuck Lever case SEEK_CUR:
4436faddda6SChuck Lever offset += file->f_pos;
4446faddda6SChuck Lever fallthrough;
4456faddda6SChuck Lever case SEEK_SET:
4466faddda6SChuck Lever if (offset >= 0)
4476faddda6SChuck Lever break;
4486faddda6SChuck Lever fallthrough;
4496faddda6SChuck Lever default:
4506faddda6SChuck Lever return -EINVAL;
4516faddda6SChuck Lever }
4526faddda6SChuck Lever
4536faddda6SChuck Lever return vfs_setpos(file, offset, U32_MAX);
4546faddda6SChuck Lever }
4556faddda6SChuck Lever
find_positive_dentry(struct dentry * parent,struct dentry * dentry,bool next)456*850e696fSChuck Lever static struct dentry *find_positive_dentry(struct dentry *parent,
457*850e696fSChuck Lever struct dentry *dentry,
458*850e696fSChuck Lever bool next)
4596faddda6SChuck Lever {
460*850e696fSChuck Lever struct dentry *found = NULL;
461*850e696fSChuck Lever
462*850e696fSChuck Lever spin_lock(&parent->d_lock);
463*850e696fSChuck Lever if (next)
464*850e696fSChuck Lever dentry = list_next_entry(dentry, d_child);
465*850e696fSChuck Lever else if (!dentry)
466*850e696fSChuck Lever dentry = list_first_entry_or_null(&parent->d_subdirs,
467*850e696fSChuck Lever struct dentry, d_child);
468*850e696fSChuck Lever for (; dentry && !list_entry_is_head(dentry, &parent->d_subdirs, d_child);
469*850e696fSChuck Lever dentry = list_next_entry(dentry, d_child)) {
470*850e696fSChuck Lever if (!simple_positive(dentry))
471*850e696fSChuck Lever continue;
472*850e696fSChuck Lever spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
473*850e696fSChuck Lever if (simple_positive(dentry))
474*850e696fSChuck Lever found = dget_dlock(dentry);
475*850e696fSChuck Lever spin_unlock(&dentry->d_lock);
476*850e696fSChuck Lever if (likely(found))
477*850e696fSChuck Lever break;
478*850e696fSChuck Lever }
479*850e696fSChuck Lever spin_unlock(&parent->d_lock);
480*850e696fSChuck Lever return found;
481*850e696fSChuck Lever }
482*850e696fSChuck Lever
483*850e696fSChuck Lever static noinline_for_stack struct dentry *
offset_dir_lookup(struct dentry * parent,loff_t offset)484*850e696fSChuck Lever offset_dir_lookup(struct dentry *parent, loff_t offset)
485*850e696fSChuck Lever {
486*850e696fSChuck Lever struct inode *inode = d_inode(parent);
487*850e696fSChuck Lever struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
4886faddda6SChuck Lever struct dentry *child, *found = NULL;
489*850e696fSChuck Lever
4903bd97ebfSChuck Lever XA_STATE(xas, &octx->xa, offset);
4916faddda6SChuck Lever
492*850e696fSChuck Lever if (offset == DIR_OFFSET_FIRST)
493*850e696fSChuck Lever found = find_positive_dentry(parent, NULL, false);
494*850e696fSChuck Lever else {
4956faddda6SChuck Lever rcu_read_lock();
4960f03dd06SChuck Lever child = xas_next_entry(&xas, DIR_OFFSET_MAX);
497*850e696fSChuck Lever found = find_positive_dentry(parent, child, false);
4986faddda6SChuck Lever rcu_read_unlock();
499*850e696fSChuck Lever }
5006faddda6SChuck Lever return found;
5016faddda6SChuck Lever }
5026faddda6SChuck Lever
offset_dir_emit(struct dir_context * ctx,struct dentry * dentry)5036faddda6SChuck Lever static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
5046faddda6SChuck Lever {
5056faddda6SChuck Lever struct inode *inode = d_inode(dentry);
5066faddda6SChuck Lever
507*850e696fSChuck Lever return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len,
5086faddda6SChuck Lever inode->i_ino, fs_umode_to_dtype(inode->i_mode));
5096faddda6SChuck Lever }
5106faddda6SChuck Lever
offset_iterate_dir(struct file * file,struct dir_context * ctx)511*850e696fSChuck Lever static void offset_iterate_dir(struct file *file, struct dir_context *ctx)
5126faddda6SChuck Lever {
513*850e696fSChuck Lever struct dentry *dir = file->f_path.dentry;
5146faddda6SChuck Lever struct dentry *dentry;
5156faddda6SChuck Lever
516*850e696fSChuck Lever dentry = offset_dir_lookup(dir, ctx->pos);
5176faddda6SChuck Lever if (!dentry)
5180f03dd06SChuck Lever goto out_eod;
519*850e696fSChuck Lever while (true) {
520*850e696fSChuck Lever struct dentry *next;
5216faddda6SChuck Lever
522*850e696fSChuck Lever ctx->pos = dentry2offset(dentry);
523*850e696fSChuck Lever if (!offset_dir_emit(ctx, dentry))
5246faddda6SChuck Lever break;
5256faddda6SChuck Lever
526*850e696fSChuck Lever next = find_positive_dentry(dir, dentry, true);
5276faddda6SChuck Lever dput(dentry);
528*850e696fSChuck Lever
529*850e696fSChuck Lever if (!next)
530*850e696fSChuck Lever goto out_eod;
531*850e696fSChuck Lever dentry = next;
5326faddda6SChuck Lever }
533*850e696fSChuck Lever dput(dentry);
5340f03dd06SChuck Lever return;
5350f03dd06SChuck Lever
5360f03dd06SChuck Lever out_eod:
5370f03dd06SChuck Lever ctx->pos = DIR_OFFSET_EOD;
5386faddda6SChuck Lever }
5396faddda6SChuck Lever
5406faddda6SChuck Lever /**
5416faddda6SChuck Lever * offset_readdir - Emit entries starting at offset @ctx->pos
5426faddda6SChuck Lever * @file: an open directory to iterate over
5436faddda6SChuck Lever * @ctx: directory iteration context
5446faddda6SChuck Lever *
5456faddda6SChuck Lever * Caller must hold @file's i_rwsem to prevent insertion or removal of
5466faddda6SChuck Lever * entries during this call.
5476faddda6SChuck Lever *
5486faddda6SChuck Lever * On entry, @ctx->pos contains an offset that represents the first entry
5496faddda6SChuck Lever * to be read from the directory.
5506faddda6SChuck Lever *
5516faddda6SChuck Lever * The operation continues until there are no more entries to read, or
5526faddda6SChuck Lever * until the ctx->actor indicates there is no more space in the caller's
5536faddda6SChuck Lever * output buffer.
5546faddda6SChuck Lever *
5556faddda6SChuck Lever * On return, @ctx->pos contains an offset that will read the next entry
5562be4f05aSChuck Lever * in this directory when offset_readdir() is called again with @ctx.
5570f03dd06SChuck Lever * Caller places this value in the d_off field of the last entry in the
5580f03dd06SChuck Lever * user's buffer.
5596faddda6SChuck Lever *
5606faddda6SChuck Lever * Return values:
5616faddda6SChuck Lever * %0 - Complete
5626faddda6SChuck Lever */
offset_readdir(struct file * file,struct dir_context * ctx)5636faddda6SChuck Lever static int offset_readdir(struct file *file, struct dir_context *ctx)
5646faddda6SChuck Lever {
5656faddda6SChuck Lever struct dentry *dir = file->f_path.dentry;
5666faddda6SChuck Lever
5676faddda6SChuck Lever lockdep_assert_held(&d_inode(dir)->i_rwsem);
5686faddda6SChuck Lever
5696faddda6SChuck Lever if (!dir_emit_dots(file, ctx))
5706faddda6SChuck Lever return 0;
5710f03dd06SChuck Lever if (ctx->pos != DIR_OFFSET_EOD)
572*850e696fSChuck Lever offset_iterate_dir(file, ctx);
5736faddda6SChuck Lever return 0;
5746faddda6SChuck Lever }
5756faddda6SChuck Lever
5766faddda6SChuck Lever const struct file_operations simple_offset_dir_operations = {
5776faddda6SChuck Lever .llseek = offset_dir_llseek,
5786faddda6SChuck Lever .iterate_shared = offset_readdir,
5796faddda6SChuck Lever .read = generic_read_dir,
5806faddda6SChuck Lever .fsync = noop_fsync,
5816faddda6SChuck Lever };
5826faddda6SChuck Lever
find_next_child(struct dentry * parent,struct dentry * prev)583a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
584a3d1e7ebSAl Viro {
585a3d1e7ebSAl Viro struct dentry *child = NULL;
586a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
587a3d1e7ebSAl Viro
588a3d1e7ebSAl Viro spin_lock(&parent->d_lock);
589a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) {
590a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child);
591a3d1e7ebSAl Viro if (simple_positive(d)) {
592a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
593a3d1e7ebSAl Viro if (simple_positive(d))
594a3d1e7ebSAl Viro child = dget_dlock(d);
595a3d1e7ebSAl Viro spin_unlock(&d->d_lock);
596a3d1e7ebSAl Viro if (likely(child))
597a3d1e7ebSAl Viro break;
598a3d1e7ebSAl Viro }
599a3d1e7ebSAl Viro }
600a3d1e7ebSAl Viro spin_unlock(&parent->d_lock);
601a3d1e7ebSAl Viro dput(prev);
602a3d1e7ebSAl Viro return child;
603a3d1e7ebSAl Viro }
604a3d1e7ebSAl Viro
simple_recursive_removal(struct dentry * dentry,void (* callback)(struct dentry *))605a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry,
606a3d1e7ebSAl Viro void (*callback)(struct dentry *))
607a3d1e7ebSAl Viro {
608a3d1e7ebSAl Viro struct dentry *this = dget(dentry);
609a3d1e7ebSAl Viro while (true) {
610a3d1e7ebSAl Viro struct dentry *victim = NULL, *child;
611a3d1e7ebSAl Viro struct inode *inode = this->d_inode;
612a3d1e7ebSAl Viro
613a3d1e7ebSAl Viro inode_lock(inode);
614a3d1e7ebSAl Viro if (d_is_dir(this))
615a3d1e7ebSAl Viro inode->i_flags |= S_DEAD;
616a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) {
617a3d1e7ebSAl Viro // kill and ascend
618a3d1e7ebSAl Viro // update metadata while it's still locked
619f7f43858SJeff Layton inode_set_ctime_current(inode);
620a3d1e7ebSAl Viro clear_nlink(inode);
621a3d1e7ebSAl Viro inode_unlock(inode);
622a3d1e7ebSAl Viro victim = this;
623a3d1e7ebSAl Viro this = this->d_parent;
624a3d1e7ebSAl Viro inode = this->d_inode;
625a3d1e7ebSAl Viro inode_lock(inode);
626a3d1e7ebSAl Viro if (simple_positive(victim)) {
627a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts
628a3d1e7ebSAl Viro if (d_is_dir(victim))
629a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim);
630a3d1e7ebSAl Viro else
631a3d1e7ebSAl Viro fsnotify_unlink(inode, victim);
632a3d1e7ebSAl Viro if (callback)
633a3d1e7ebSAl Viro callback(victim);
634a3d1e7ebSAl Viro dput(victim); // unpin it
635a3d1e7ebSAl Viro }
636a3d1e7ebSAl Viro if (victim == dentry) {
6375b5599a7SJeff Layton inode_set_mtime_to_ts(inode,
6385b5599a7SJeff Layton inode_set_ctime_current(inode));
639a3d1e7ebSAl Viro if (d_is_dir(dentry))
640a3d1e7ebSAl Viro drop_nlink(inode);
641a3d1e7ebSAl Viro inode_unlock(inode);
642a3d1e7ebSAl Viro dput(dentry);
643a3d1e7ebSAl Viro return;
644a3d1e7ebSAl Viro }
645a3d1e7ebSAl Viro }
646a3d1e7ebSAl Viro inode_unlock(inode);
647a3d1e7ebSAl Viro this = child;
648a3d1e7ebSAl Viro }
649a3d1e7ebSAl Viro }
650a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal);
651a3d1e7ebSAl Viro
652759b9775SHugh Dickins static const struct super_operations simple_super_operations = {
653759b9775SHugh Dickins .statfs = simple_statfs,
654759b9775SHugh Dickins };
655759b9775SHugh Dickins
pseudo_fs_fill_super(struct super_block * s,struct fs_context * fc)656db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
6571da177e4SLinus Torvalds {
65831d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private;
6591da177e4SLinus Torvalds struct inode *root;
6601da177e4SLinus Torvalds
66189a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE;
6623971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE;
6633971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT;
6648d9e46d8SAl Viro s->s_magic = ctx->magic;
6658d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations;
6668d9e46d8SAl Viro s->s_xattr = ctx->xattr;
6671da177e4SLinus Torvalds s->s_time_gran = 1;
6681da177e4SLinus Torvalds root = new_inode(s);
6691da177e4SLinus Torvalds if (!root)
670db2c246aSDavid Howells return -ENOMEM;
671db2c246aSDavid Howells
6721a1c9bb4SJeff Layton /*
6731a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created
6741a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing
6751a1c9bb4SJeff Layton * max_reserved of 1 to iunique).
6761a1c9bb4SJeff Layton */
6771a1c9bb4SJeff Layton root->i_ino = 1;
6781da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
6795b5599a7SJeff Layton simple_inode_init_ts(root);
6808d9e46d8SAl Viro s->s_root = d_make_root(root);
6818d9e46d8SAl Viro if (!s->s_root)
6828d9e46d8SAl Viro return -ENOMEM;
683db2c246aSDavid Howells s->s_d_op = ctx->dops;
684db2c246aSDavid Howells return 0;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds
pseudo_fs_get_tree(struct fs_context * fc)687db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc)
688db2c246aSDavid Howells {
6892ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super);
6901da177e4SLinus Torvalds }
69131d6d5ceSDavid Howells
pseudo_fs_free(struct fs_context * fc)69231d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc)
69331d6d5ceSDavid Howells {
69431d6d5ceSDavid Howells kfree(fc->fs_private);
69531d6d5ceSDavid Howells }
69631d6d5ceSDavid Howells
69731d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = {
69831d6d5ceSDavid Howells .free = pseudo_fs_free,
69931d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree,
70031d6d5ceSDavid Howells };
70131d6d5ceSDavid Howells
70231d6d5ceSDavid Howells /*
70331d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
70431d6d5ceSDavid Howells * will never be mountable)
70531d6d5ceSDavid Howells */
init_pseudo(struct fs_context * fc,unsigned long magic)70631d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
70731d6d5ceSDavid Howells unsigned long magic)
70831d6d5ceSDavid Howells {
70931d6d5ceSDavid Howells struct pseudo_fs_context *ctx;
71031d6d5ceSDavid Howells
71131d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
71231d6d5ceSDavid Howells if (likely(ctx)) {
71331d6d5ceSDavid Howells ctx->magic = magic;
71431d6d5ceSDavid Howells fc->fs_private = ctx;
71531d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops;
716db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER;
717db2c246aSDavid Howells fc->global = true;
71831d6d5ceSDavid Howells }
71931d6d5ceSDavid Howells return ctx;
72031d6d5ceSDavid Howells }
72131d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo);
7221da177e4SLinus Torvalds
simple_open(struct inode * inode,struct file * file)72320955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file)
72420955e89SStephen Boyd {
72520955e89SStephen Boyd if (inode->i_private)
72620955e89SStephen Boyd file->private_data = inode->i_private;
72720955e89SStephen Boyd return 0;
72820955e89SStephen Boyd }
72912f38872SAl Viro EXPORT_SYMBOL(simple_open);
73020955e89SStephen Boyd
simple_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)7311da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
7321da177e4SLinus Torvalds {
733dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry);
7341da177e4SLinus Torvalds
7355b5599a7SJeff Layton inode_set_mtime_to_ts(dir,
7365b5599a7SJeff Layton inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
737d8c76e6fSDave Hansen inc_nlink(inode);
7387de9c6eeSAl Viro ihold(inode);
7391da177e4SLinus Torvalds dget(dentry);
7401da177e4SLinus Torvalds d_instantiate(dentry, inode);
7411da177e4SLinus Torvalds return 0;
7421da177e4SLinus Torvalds }
74312f38872SAl Viro EXPORT_SYMBOL(simple_link);
7441da177e4SLinus Torvalds
simple_empty(struct dentry * dentry)7451da177e4SLinus Torvalds int simple_empty(struct dentry *dentry)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds struct dentry *child;
7481da177e4SLinus Torvalds int ret = 0;
7491da177e4SLinus Torvalds
7502fd6b7f5SNick Piggin spin_lock(&dentry->d_lock);
751946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) {
752da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
753da502956SNick Piggin if (simple_positive(child)) {
754da502956SNick Piggin spin_unlock(&child->d_lock);
7551da177e4SLinus Torvalds goto out;
756da502956SNick Piggin }
757da502956SNick Piggin spin_unlock(&child->d_lock);
758da502956SNick Piggin }
7591da177e4SLinus Torvalds ret = 1;
7601da177e4SLinus Torvalds out:
7612fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock);
7621da177e4SLinus Torvalds return ret;
7631da177e4SLinus Torvalds }
76412f38872SAl Viro EXPORT_SYMBOL(simple_empty);
7651da177e4SLinus Torvalds
simple_unlink(struct inode * dir,struct dentry * dentry)7661da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry)
7671da177e4SLinus Torvalds {
768dea655c2SDavid Howells struct inode *inode = d_inode(dentry);
7691da177e4SLinus Torvalds
7705b5599a7SJeff Layton inode_set_mtime_to_ts(dir,
7715b5599a7SJeff Layton inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
7729a53c3a7SDave Hansen drop_nlink(inode);
7731da177e4SLinus Torvalds dput(dentry);
7741da177e4SLinus Torvalds return 0;
7751da177e4SLinus Torvalds }
77612f38872SAl Viro EXPORT_SYMBOL(simple_unlink);
7771da177e4SLinus Torvalds
simple_rmdir(struct inode * dir,struct dentry * dentry)7781da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds if (!simple_empty(dentry))
7811da177e4SLinus Torvalds return -ENOTEMPTY;
7821da177e4SLinus Torvalds
783dea655c2SDavid Howells drop_nlink(d_inode(dentry));
7841da177e4SLinus Torvalds simple_unlink(dir, dentry);
7859a53c3a7SDave Hansen drop_nlink(dir);
7861da177e4SLinus Torvalds return 0;
7871da177e4SLinus Torvalds }
78812f38872SAl Viro EXPORT_SYMBOL(simple_rmdir);
7891da177e4SLinus Torvalds
7900c476792SJeff Layton /**
7910c476792SJeff Layton * simple_rename_timestamp - update the various inode timestamps for rename
7920c476792SJeff Layton * @old_dir: old parent directory
7930c476792SJeff Layton * @old_dentry: dentry that is being renamed
7940c476792SJeff Layton * @new_dir: new parent directory
7950c476792SJeff Layton * @new_dentry: target for rename
7960c476792SJeff Layton *
7970c476792SJeff Layton * POSIX mandates that the old and new parent directories have their ctime and
7980c476792SJeff Layton * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have
7990c476792SJeff Layton * their ctime updated.
8000c476792SJeff Layton */
simple_rename_timestamp(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)8010c476792SJeff Layton void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
8020c476792SJeff Layton struct inode *new_dir, struct dentry *new_dentry)
8030c476792SJeff Layton {
8040c476792SJeff Layton struct inode *newino = d_inode(new_dentry);
8050c476792SJeff Layton
8065b5599a7SJeff Layton inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
8070c476792SJeff Layton if (new_dir != old_dir)
8085b5599a7SJeff Layton inode_set_mtime_to_ts(new_dir,
8095b5599a7SJeff Layton inode_set_ctime_current(new_dir));
8100c476792SJeff Layton inode_set_ctime_current(d_inode(old_dentry));
8110c476792SJeff Layton if (newino)
8120c476792SJeff Layton inode_set_ctime_current(newino);
8130c476792SJeff Layton }
8140c476792SJeff Layton EXPORT_SYMBOL_GPL(simple_rename_timestamp);
8150c476792SJeff Layton
simple_rename_exchange(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)8166429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
8176429e463SLorenz Bauer struct inode *new_dir, struct dentry *new_dentry)
8186429e463SLorenz Bauer {
8196429e463SLorenz Bauer bool old_is_dir = d_is_dir(old_dentry);
8206429e463SLorenz Bauer bool new_is_dir = d_is_dir(new_dentry);
8216429e463SLorenz Bauer
8226429e463SLorenz Bauer if (old_dir != new_dir && old_is_dir != new_is_dir) {
8236429e463SLorenz Bauer if (old_is_dir) {
8246429e463SLorenz Bauer drop_nlink(old_dir);
8256429e463SLorenz Bauer inc_nlink(new_dir);
8266429e463SLorenz Bauer } else {
8276429e463SLorenz Bauer drop_nlink(new_dir);
8286429e463SLorenz Bauer inc_nlink(old_dir);
8296429e463SLorenz Bauer }
8306429e463SLorenz Bauer }
8310c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
8326429e463SLorenz Bauer return 0;
8336429e463SLorenz Bauer }
8346429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange);
8356429e463SLorenz 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)836e18275aeSChristian Brauner int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir,
837549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir,
838549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags)
8391da177e4SLinus Torvalds {
840e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry);
8411da177e4SLinus Torvalds
8423871cb8cSLorenz Bauer if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
843e0e0be8aSMiklos Szeredi return -EINVAL;
844e0e0be8aSMiklos Szeredi
8453871cb8cSLorenz Bauer if (flags & RENAME_EXCHANGE)
8463871cb8cSLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
8473871cb8cSLorenz Bauer
8481da177e4SLinus Torvalds if (!simple_empty(new_dentry))
8491da177e4SLinus Torvalds return -ENOTEMPTY;
8501da177e4SLinus Torvalds
851dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) {
8521da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry);
853841590ceSAl Viro if (they_are_dirs) {
854dea655c2SDavid Howells drop_nlink(d_inode(new_dentry));
8559a53c3a7SDave Hansen drop_nlink(old_dir);
856841590ceSAl Viro }
8571da177e4SLinus Torvalds } else if (they_are_dirs) {
8589a53c3a7SDave Hansen drop_nlink(old_dir);
859d8c76e6fSDave Hansen inc_nlink(new_dir);
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds
8620c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
8631da177e4SLinus Torvalds return 0;
8641da177e4SLinus Torvalds }
86512f38872SAl Viro EXPORT_SYMBOL(simple_rename);
8661da177e4SLinus Torvalds
8677bb46a67Snpiggin@suse.de /**
868eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem
869c1632a0fSChristian Brauner * @idmap: idmap of the target mount
8707bb46a67Snpiggin@suse.de * @dentry: dentry
8717bb46a67Snpiggin@suse.de * @iattr: iattr structure
8727bb46a67Snpiggin@suse.de *
8737bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure.
8747bb46a67Snpiggin@suse.de *
875eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper
876eef2380cSChristoph Hellwig * implementation of size changes.
877eef2380cSChristoph Hellwig *
878eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files
879eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk
880eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method.
8817bb46a67Snpiggin@suse.de */
simple_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)882c1632a0fSChristian Brauner int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
883549c7297SChristian Brauner struct iattr *iattr)
8847bb46a67Snpiggin@suse.de {
885dea655c2SDavid Howells struct inode *inode = d_inode(dentry);
8867bb46a67Snpiggin@suse.de int error;
8877bb46a67Snpiggin@suse.de
888c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, iattr);
8897bb46a67Snpiggin@suse.de if (error)
8907bb46a67Snpiggin@suse.de return error;
8917bb46a67Snpiggin@suse.de
8922c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE)
8932c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size);
894c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr);
895eef2380cSChristoph Hellwig mark_inode_dirty(inode);
896eef2380cSChristoph Hellwig return 0;
8977bb46a67Snpiggin@suse.de }
8987bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr);
8997bb46a67Snpiggin@suse.de
simple_read_folio(struct file * file,struct folio * folio)900a77f580aSMatthew Wilcox (Oracle) static int simple_read_folio(struct file *file, struct folio *folio)
9011da177e4SLinus Torvalds {
902a77f580aSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio));
903a77f580aSMatthew Wilcox (Oracle) flush_dcache_folio(folio);
904a77f580aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio);
905a77f580aSMatthew Wilcox (Oracle) folio_unlock(folio);
9061da177e4SLinus Torvalds return 0;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds
simple_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)909afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping,
9109d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len,
911afddba49SNick Piggin struct page **pagep, void **fsdata)
912afddba49SNick Piggin {
9135522d9f7SMatthew Wilcox (Oracle) struct folio *folio;
914afddba49SNick Piggin
9155522d9f7SMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
9165522d9f7SMatthew Wilcox (Oracle) mapping_gfp_mask(mapping));
9175522d9f7SMatthew Wilcox (Oracle) if (IS_ERR(folio))
9185522d9f7SMatthew Wilcox (Oracle) return PTR_ERR(folio);
919afddba49SNick Piggin
9205522d9f7SMatthew Wilcox (Oracle) *pagep = &folio->page;
921afddba49SNick Piggin
9225522d9f7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio) && (len != folio_size(folio))) {
9235522d9f7SMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos);
924afddba49SNick Piggin
9255522d9f7SMatthew Wilcox (Oracle) folio_zero_segments(folio, 0, from,
9265522d9f7SMatthew Wilcox (Oracle) from + len, folio_size(folio));
927193cf4b9SBoaz Harrosh }
928193cf4b9SBoaz Harrosh return 0;
929afddba49SNick Piggin }
93012f38872SAl Viro EXPORT_SYMBOL(simple_write_begin);
931afddba49SNick Piggin
932ad2a722fSBoaz Harrosh /**
933ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes
9348e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations
935ad2a722fSBoaz Harrosh * @mapping: "
936ad2a722fSBoaz Harrosh * @pos: "
937ad2a722fSBoaz Harrosh * @len: "
938ad2a722fSBoaz Harrosh * @copied: "
939ad2a722fSBoaz Harrosh * @page: "
940ad2a722fSBoaz Harrosh * @fsdata: "
941ad2a722fSBoaz Harrosh *
942ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is
943ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of
944ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for
945ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held.
946ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end().
947ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
948ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode
949ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the
950ad2a722fSBoaz Harrosh * case that i_size has changed.
95104fff641SAl Viro *
952a77f580aSMatthew Wilcox (Oracle) * Use *ONLY* with simple_read_folio()
953ad2a722fSBoaz Harrosh */
simple_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)954c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping,
955ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied,
956ad2a722fSBoaz Harrosh struct page *page, void *fsdata)
9571da177e4SLinus Torvalds {
9585522d9f7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page);
9595522d9f7SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host;
960ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied;
961ad2a722fSBoaz Harrosh
9625522d9f7SMatthew Wilcox (Oracle) /* zero the stale part of the folio if we did a short copy */
9635522d9f7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) {
964ad2a722fSBoaz Harrosh if (copied < len) {
9655522d9f7SMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos);
966ad2a722fSBoaz Harrosh
9675522d9f7SMatthew Wilcox (Oracle) folio_zero_range(folio, from + copied, len - copied);
968ad2a722fSBoaz Harrosh }
9695522d9f7SMatthew Wilcox (Oracle) folio_mark_uptodate(folio);
97004fff641SAl Viro }
9711da177e4SLinus Torvalds /*
9721da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size
9731b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex.
9741da177e4SLinus Torvalds */
975ad2a722fSBoaz Harrosh if (last_pos > inode->i_size)
976ad2a722fSBoaz Harrosh i_size_write(inode, last_pos);
977ad2a722fSBoaz Harrosh
9785522d9f7SMatthew Wilcox (Oracle) folio_mark_dirty(folio);
9795522d9f7SMatthew Wilcox (Oracle) folio_unlock(folio);
9805522d9f7SMatthew Wilcox (Oracle) folio_put(folio);
981afddba49SNick Piggin
982afddba49SNick Piggin return copied;
983afddba49SNick Piggin }
984c1e3dbe9SChristoph Hellwig
985c1e3dbe9SChristoph Hellwig /*
986c1e3dbe9SChristoph Hellwig * Provides ramfs-style behavior: data in the pagecache, but no writeback.
987c1e3dbe9SChristoph Hellwig */
988c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = {
989a77f580aSMatthew Wilcox (Oracle) .read_folio = simple_read_folio,
990c1e3dbe9SChristoph Hellwig .write_begin = simple_write_begin,
991c1e3dbe9SChristoph Hellwig .write_end = simple_write_end,
99246de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio,
993c1e3dbe9SChristoph Hellwig };
994c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops);
995afddba49SNick Piggin
9961a1c9bb4SJeff Layton /*
9971a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate
9981a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care
9991a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions.
10001a1c9bb4SJeff Layton */
simple_fill_super(struct super_block * s,unsigned long magic,const struct tree_descr * files)10017d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic,
1002cda37124SEric Biggers const struct tree_descr *files)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds struct inode *inode;
10051da177e4SLinus Torvalds struct dentry *root;
10061da177e4SLinus Torvalds struct dentry *dentry;
10071da177e4SLinus Torvalds int i;
10081da177e4SLinus Torvalds
100909cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE;
101009cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT;
10111da177e4SLinus Torvalds s->s_magic = magic;
1012759b9775SHugh Dickins s->s_op = &simple_super_operations;
10131da177e4SLinus Torvalds s->s_time_gran = 1;
10141da177e4SLinus Torvalds
10151da177e4SLinus Torvalds inode = new_inode(s);
10161da177e4SLinus Torvalds if (!inode)
10171da177e4SLinus Torvalds return -ENOMEM;
10181a1c9bb4SJeff Layton /*
10191a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an
10201a1c9bb4SJeff Layton * entry at index 1
10211a1c9bb4SJeff Layton */
10221a1c9bb4SJeff Layton inode->i_ino = 1;
10231da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755;
10245b5599a7SJeff Layton simple_inode_init_ts(inode);
10251da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations;
10261da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations;
1027bfe86848SMiklos Szeredi set_nlink(inode, 2);
102848fde701SAl Viro root = d_make_root(inode);
102948fde701SAl Viro if (!root)
10301da177e4SLinus Torvalds return -ENOMEM;
10311da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) {
10321da177e4SLinus Torvalds if (!files->name)
10331da177e4SLinus Torvalds continue;
10341a1c9bb4SJeff Layton
10351a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */
10361a1c9bb4SJeff Layton if (unlikely(i == 1))
10371a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array"
10381a1c9bb4SJeff Layton "with an index of 1!\n", __func__,
10391a1c9bb4SJeff Layton s->s_type->name);
10401a1c9bb4SJeff Layton
10411da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name);
10421da177e4SLinus Torvalds if (!dentry)
10431da177e4SLinus Torvalds goto out;
10441da177e4SLinus Torvalds inode = new_inode(s);
104532096ea1SKonstantin Khlebnikov if (!inode) {
104632096ea1SKonstantin Khlebnikov dput(dentry);
10471da177e4SLinus Torvalds goto out;
104832096ea1SKonstantin Khlebnikov }
10491da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode;
10505b5599a7SJeff Layton simple_inode_init_ts(inode);
10511da177e4SLinus Torvalds inode->i_fop = files->ops;
10521da177e4SLinus Torvalds inode->i_ino = i;
10531da177e4SLinus Torvalds d_add(dentry, inode);
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds s->s_root = root;
10561da177e4SLinus Torvalds return 0;
10571da177e4SLinus Torvalds out:
10581da177e4SLinus Torvalds d_genocide(root);
1059640946f2SAl Viro shrink_dcache_parent(root);
10601da177e4SLinus Torvalds dput(root);
10611da177e4SLinus Torvalds return -ENOMEM;
10621da177e4SLinus Torvalds }
106312f38872SAl Viro EXPORT_SYMBOL(simple_fill_super);
10641da177e4SLinus Torvalds
10651da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock);
10661da177e4SLinus Torvalds
simple_pin_fs(struct file_system_type * type,struct vfsmount ** mount,int * count)10671f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds struct vfsmount *mnt = NULL;
10701da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
10711da177e4SLinus Torvalds if (unlikely(!*mount)) {
10721da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
10731751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
10741da177e4SLinus Torvalds if (IS_ERR(mnt))
10751da177e4SLinus Torvalds return PTR_ERR(mnt);
10761da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
10771da177e4SLinus Torvalds if (!*mount)
10781da177e4SLinus Torvalds *mount = mnt;
10791da177e4SLinus Torvalds }
10801da177e4SLinus Torvalds mntget(*mount);
10811da177e4SLinus Torvalds ++*count;
10821da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
10831da177e4SLinus Torvalds mntput(mnt);
10841da177e4SLinus Torvalds return 0;
10851da177e4SLinus Torvalds }
108612f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs);
10871da177e4SLinus Torvalds
simple_release_fs(struct vfsmount ** mount,int * count)10881da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count)
10891da177e4SLinus Torvalds {
10901da177e4SLinus Torvalds struct vfsmount *mnt;
10911da177e4SLinus Torvalds spin_lock(&pin_fs_lock);
10921da177e4SLinus Torvalds mnt = *mount;
10931da177e4SLinus Torvalds if (!--*count)
10941da177e4SLinus Torvalds *mount = NULL;
10951da177e4SLinus Torvalds spin_unlock(&pin_fs_lock);
10961da177e4SLinus Torvalds mntput(mnt);
10971da177e4SLinus Torvalds }
109812f38872SAl Viro EXPORT_SYMBOL(simple_release_fs);
10991da177e4SLinus Torvalds
11006d1029b5SAkinobu Mita /**
11016d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space
11026d1029b5SAkinobu Mita * @to: the user space buffer to read to
11036d1029b5SAkinobu Mita * @count: the maximum number of bytes to read
11046d1029b5SAkinobu Mita * @ppos: the current position in the buffer
11056d1029b5SAkinobu Mita * @from: the buffer to read from
11066d1029b5SAkinobu Mita * @available: the size of the buffer
11076d1029b5SAkinobu Mita *
11086d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the
11096d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to.
11106d1029b5SAkinobu Mita *
11116d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is
11126d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error.
11136d1029b5SAkinobu Mita **/
simple_read_from_buffer(void __user * to,size_t count,loff_t * ppos,const void * from,size_t available)11141da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
11151da177e4SLinus Torvalds const void *from, size_t available)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds loff_t pos = *ppos;
111814be2746SSteven Rostedt size_t ret;
111914be2746SSteven Rostedt
11201da177e4SLinus Torvalds if (pos < 0)
11211da177e4SLinus Torvalds return -EINVAL;
112214be2746SSteven Rostedt if (pos >= available || !count)
11231da177e4SLinus Torvalds return 0;
11241da177e4SLinus Torvalds if (count > available - pos)
11251da177e4SLinus Torvalds count = available - pos;
112614be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count);
112714be2746SSteven Rostedt if (ret == count)
11281da177e4SLinus Torvalds return -EFAULT;
112914be2746SSteven Rostedt count -= ret;
11301da177e4SLinus Torvalds *ppos = pos + count;
11311da177e4SLinus Torvalds return count;
11321da177e4SLinus Torvalds }
113312f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer);
11341da177e4SLinus Torvalds
11356d1029b5SAkinobu Mita /**
11366a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer
11376a727b43SJiri Slaby * @to: the buffer to write to
11386a727b43SJiri Slaby * @available: the size of the buffer
11396a727b43SJiri Slaby * @ppos: the current position in the buffer
11406a727b43SJiri Slaby * @from: the user space buffer to read from
11416a727b43SJiri Slaby * @count: the maximum number of bytes to read
11426a727b43SJiri Slaby *
11436a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user
11446a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos.
11456a727b43SJiri Slaby *
11466a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is
11476a727b43SJiri Slaby * advanced by this number, or negative value is returned on error.
11486a727b43SJiri Slaby **/
simple_write_to_buffer(void * to,size_t available,loff_t * ppos,const void __user * from,size_t count)11496a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
11506a727b43SJiri Slaby const void __user *from, size_t count)
11516a727b43SJiri Slaby {
11526a727b43SJiri Slaby loff_t pos = *ppos;
11536a727b43SJiri Slaby size_t res;
11546a727b43SJiri Slaby
11556a727b43SJiri Slaby if (pos < 0)
11566a727b43SJiri Slaby return -EINVAL;
11576a727b43SJiri Slaby if (pos >= available || !count)
11586a727b43SJiri Slaby return 0;
11596a727b43SJiri Slaby if (count > available - pos)
11606a727b43SJiri Slaby count = available - pos;
11616a727b43SJiri Slaby res = copy_from_user(to + pos, from, count);
11626a727b43SJiri Slaby if (res == count)
11636a727b43SJiri Slaby return -EFAULT;
11646a727b43SJiri Slaby count -= res;
11656a727b43SJiri Slaby *ppos = pos + count;
11666a727b43SJiri Slaby return count;
11676a727b43SJiri Slaby }
116812f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer);
11696a727b43SJiri Slaby
11706a727b43SJiri Slaby /**
11716d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer
11726d1029b5SAkinobu Mita * @to: the kernel space buffer to read to
11736d1029b5SAkinobu Mita * @count: the maximum number of bytes to read
11746d1029b5SAkinobu Mita * @ppos: the current position in the buffer
11756d1029b5SAkinobu Mita * @from: the buffer to read from
11766d1029b5SAkinobu Mita * @available: the size of the buffer
11776d1029b5SAkinobu Mita *
11786d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the
11796d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to.
11806d1029b5SAkinobu Mita *
11816d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is
11826d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error.
11836d1029b5SAkinobu Mita **/
memory_read_from_buffer(void * to,size_t count,loff_t * ppos,const void * from,size_t available)118493b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
118593b07113SAkinobu Mita const void *from, size_t available)
118693b07113SAkinobu Mita {
118793b07113SAkinobu Mita loff_t pos = *ppos;
118893b07113SAkinobu Mita
118993b07113SAkinobu Mita if (pos < 0)
119093b07113SAkinobu Mita return -EINVAL;
119193b07113SAkinobu Mita if (pos >= available)
119293b07113SAkinobu Mita return 0;
119393b07113SAkinobu Mita if (count > available - pos)
119493b07113SAkinobu Mita count = available - pos;
119593b07113SAkinobu Mita memcpy(to, from + pos, count);
119693b07113SAkinobu Mita *ppos = pos + count;
119793b07113SAkinobu Mita
119893b07113SAkinobu Mita return count;
119993b07113SAkinobu Mita }
120012f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer);
120193b07113SAkinobu Mita
12021da177e4SLinus Torvalds /*
12031da177e4SLinus Torvalds * Transaction based IO.
12041da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then
12051da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a
12061da177e4SLinus Torvalds * file-local buffer.
12071da177e4SLinus Torvalds */
120876791ab2SIngo Molnar
simple_transaction_set(struct file * file,size_t n)120976791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n)
121076791ab2SIngo Molnar {
121176791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data;
121276791ab2SIngo Molnar
121376791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
121476791ab2SIngo Molnar
121576791ab2SIngo Molnar /*
121676791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until
121776791ab2SIngo Molnar * ar->data is ready for reading.
121876791ab2SIngo Molnar */
121976791ab2SIngo Molnar smp_mb();
122076791ab2SIngo Molnar ar->size = n;
122176791ab2SIngo Molnar }
122212f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set);
122376791ab2SIngo Molnar
simple_transaction_get(struct file * file,const char __user * buf,size_t size)12241da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds struct simple_transaction_argresp *ar;
12271da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock);
12281da177e4SLinus Torvalds
12291da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1)
12301da177e4SLinus Torvalds return ERR_PTR(-EFBIG);
12311da177e4SLinus Torvalds
12321da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
12331da177e4SLinus Torvalds if (!ar)
12341da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
12351da177e4SLinus Torvalds
12361da177e4SLinus Torvalds spin_lock(&simple_transaction_lock);
12371da177e4SLinus Torvalds
12381da177e4SLinus Torvalds /* only one write allowed per open */
12391da177e4SLinus Torvalds if (file->private_data) {
12401da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock);
12411da177e4SLinus Torvalds free_page((unsigned long)ar);
12421da177e4SLinus Torvalds return ERR_PTR(-EBUSY);
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds
12451da177e4SLinus Torvalds file->private_data = ar;
12461da177e4SLinus Torvalds
12471da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock);
12481da177e4SLinus Torvalds
12491da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size))
12501da177e4SLinus Torvalds return ERR_PTR(-EFAULT);
12511da177e4SLinus Torvalds
12521da177e4SLinus Torvalds return ar->data;
12531da177e4SLinus Torvalds }
125412f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get);
12551da177e4SLinus Torvalds
simple_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)12561da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data;
12591da177e4SLinus Torvalds
12601da177e4SLinus Torvalds if (!ar)
12611da177e4SLinus Torvalds return 0;
12621da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
12631da177e4SLinus Torvalds }
126412f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read);
12651da177e4SLinus Torvalds
simple_transaction_release(struct inode * inode,struct file * file)12661da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file)
12671da177e4SLinus Torvalds {
12681da177e4SLinus Torvalds free_page((unsigned long)file->private_data);
12691da177e4SLinus Torvalds return 0;
12701da177e4SLinus Torvalds }
127112f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release);
12721da177e4SLinus Torvalds
1273acaefc25SArnd Bergmann /* Simple attribute files */
1274acaefc25SArnd Bergmann
1275acaefc25SArnd Bergmann struct simple_attr {
12768b88b099SChristoph Hellwig int (*get)(void *, u64 *);
12778b88b099SChristoph Hellwig int (*set)(void *, u64);
1278acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */
1279acaefc25SArnd Bergmann char set_buf[24];
1280acaefc25SArnd Bergmann void *data;
1281acaefc25SArnd Bergmann const char *fmt; /* format for read operation */
12827cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */
1283acaefc25SArnd Bergmann };
1284acaefc25SArnd Bergmann
1285acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation
1286acaefc25SArnd 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)1287acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file,
12888b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64),
1289acaefc25SArnd Bergmann const char *fmt)
1290acaefc25SArnd Bergmann {
1291acaefc25SArnd Bergmann struct simple_attr *attr;
1292acaefc25SArnd Bergmann
1293a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1294acaefc25SArnd Bergmann if (!attr)
1295acaefc25SArnd Bergmann return -ENOMEM;
1296acaefc25SArnd Bergmann
1297acaefc25SArnd Bergmann attr->get = get;
1298acaefc25SArnd Bergmann attr->set = set;
12998e18e294STheodore Ts'o attr->data = inode->i_private;
1300acaefc25SArnd Bergmann attr->fmt = fmt;
13017cf34c76SIngo Molnar mutex_init(&attr->mutex);
1302acaefc25SArnd Bergmann
1303acaefc25SArnd Bergmann file->private_data = attr;
1304acaefc25SArnd Bergmann
1305acaefc25SArnd Bergmann return nonseekable_open(inode, file);
1306acaefc25SArnd Bergmann }
130712f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open);
1308acaefc25SArnd Bergmann
simple_attr_release(struct inode * inode,struct file * file)130974bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file)
1310acaefc25SArnd Bergmann {
1311acaefc25SArnd Bergmann kfree(file->private_data);
1312acaefc25SArnd Bergmann return 0;
1313acaefc25SArnd Bergmann }
131412f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */
1315acaefc25SArnd Bergmann
1316acaefc25SArnd 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)1317acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf,
1318acaefc25SArnd Bergmann size_t len, loff_t *ppos)
1319acaefc25SArnd Bergmann {
1320acaefc25SArnd Bergmann struct simple_attr *attr;
1321acaefc25SArnd Bergmann size_t size;
1322acaefc25SArnd Bergmann ssize_t ret;
1323acaefc25SArnd Bergmann
1324acaefc25SArnd Bergmann attr = file->private_data;
1325acaefc25SArnd Bergmann
1326acaefc25SArnd Bergmann if (!attr->get)
1327acaefc25SArnd Bergmann return -EACCES;
1328acaefc25SArnd Bergmann
13299261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex);
13309261303aSChristoph Hellwig if (ret)
13319261303aSChristoph Hellwig return ret;
13329261303aSChristoph Hellwig
1333a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) {
1334a65cab7dSEric Biggers /* continued read */
1335acaefc25SArnd Bergmann size = strlen(attr->get_buf);
1336a65cab7dSEric Biggers } else {
1337a65cab7dSEric Biggers /* first read */
13388b88b099SChristoph Hellwig u64 val;
13398b88b099SChristoph Hellwig ret = attr->get(attr->data, &val);
13408b88b099SChristoph Hellwig if (ret)
13418b88b099SChristoph Hellwig goto out;
13428b88b099SChristoph Hellwig
1343acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
13448b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val);
13458b88b099SChristoph Hellwig }
1346acaefc25SArnd Bergmann
1347acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
13488b88b099SChristoph Hellwig out:
13497cf34c76SIngo Molnar mutex_unlock(&attr->mutex);
1350acaefc25SArnd Bergmann return ret;
1351acaefc25SArnd Bergmann }
135212f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read);
1353acaefc25SArnd Bergmann
1354acaefc25SArnd 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)13552e41f274SAkinobu Mita static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
13562e41f274SAkinobu Mita size_t len, loff_t *ppos, bool is_signed)
1357acaefc25SArnd Bergmann {
1358acaefc25SArnd Bergmann struct simple_attr *attr;
1359488dac0cSYicong Yang unsigned long long val;
1360acaefc25SArnd Bergmann size_t size;
1361acaefc25SArnd Bergmann ssize_t ret;
1362acaefc25SArnd Bergmann
1363acaefc25SArnd Bergmann attr = file->private_data;
1364acaefc25SArnd Bergmann if (!attr->set)
1365acaefc25SArnd Bergmann return -EACCES;
1366acaefc25SArnd Bergmann
13679261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex);
13689261303aSChristoph Hellwig if (ret)
13699261303aSChristoph Hellwig return ret;
13709261303aSChristoph Hellwig
1371acaefc25SArnd Bergmann ret = -EFAULT;
1372acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len);
1373acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size))
1374acaefc25SArnd Bergmann goto out;
1375acaefc25SArnd Bergmann
1376acaefc25SArnd Bergmann attr->set_buf[size] = '\0';
13772e41f274SAkinobu Mita if (is_signed)
13782e41f274SAkinobu Mita ret = kstrtoll(attr->set_buf, 0, &val);
13792e41f274SAkinobu Mita else
1380488dac0cSYicong Yang ret = kstrtoull(attr->set_buf, 0, &val);
1381488dac0cSYicong Yang if (ret)
1382488dac0cSYicong Yang goto out;
138305cc0ceeSWu Fengguang ret = attr->set(attr->data, val);
138405cc0ceeSWu Fengguang if (ret == 0)
138505cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */
1386acaefc25SArnd Bergmann out:
13877cf34c76SIngo Molnar mutex_unlock(&attr->mutex);
1388acaefc25SArnd Bergmann return ret;
1389acaefc25SArnd Bergmann }
13902e41f274SAkinobu Mita
simple_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)13912e41f274SAkinobu Mita ssize_t simple_attr_write(struct file *file, const char __user *buf,
13922e41f274SAkinobu Mita size_t len, loff_t *ppos)
13932e41f274SAkinobu Mita {
13942e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, false);
13952e41f274SAkinobu Mita }
139612f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write);
1397acaefc25SArnd Bergmann
simple_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)13982e41f274SAkinobu Mita ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
13992e41f274SAkinobu Mita size_t len, loff_t *ppos)
14002e41f274SAkinobu Mita {
14012e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, true);
14022e41f274SAkinobu Mita }
14032e41f274SAkinobu Mita EXPORT_SYMBOL_GPL(simple_attr_write_signed);
14042e41f274SAkinobu Mita
14052596110aSChristoph Hellwig /**
14062596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
14072596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on
14082596110aSChristoph Hellwig * @fid: file handle to convert
14092596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes
14102596110aSChristoph Hellwig * @fh_type: type of file handle
14112596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode
14122596110aSChristoph Hellwig *
14132596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known
14142596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the
14152596110aSChristoph Hellwig * inode for the object specified in the file handle.
14162596110aSChristoph 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))14172596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
14182596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode)
14192596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen))
14202596110aSChristoph Hellwig {
14212596110aSChristoph Hellwig struct inode *inode = NULL;
14222596110aSChristoph Hellwig
14232596110aSChristoph Hellwig if (fh_len < 2)
14242596110aSChristoph Hellwig return NULL;
14252596110aSChristoph Hellwig
14262596110aSChristoph Hellwig switch (fh_type) {
14272596110aSChristoph Hellwig case FILEID_INO32_GEN:
14282596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT:
14292596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
14302596110aSChristoph Hellwig break;
14312596110aSChristoph Hellwig }
14322596110aSChristoph Hellwig
14334ea3ada2SChristoph Hellwig return d_obtain_alias(inode);
14342596110aSChristoph Hellwig }
14352596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
14362596110aSChristoph Hellwig
14372596110aSChristoph Hellwig /**
1438ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation
14392596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on
14402596110aSChristoph Hellwig * @fid: file handle to convert
14412596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes
14422596110aSChristoph Hellwig * @fh_type: type of file handle
14432596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode
14442596110aSChristoph Hellwig *
14452596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known
14462596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the
14472596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it
14482596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise.
14492596110aSChristoph 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))14502596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
14512596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode)
14522596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen))
14532596110aSChristoph Hellwig {
14542596110aSChristoph Hellwig struct inode *inode = NULL;
14552596110aSChristoph Hellwig
14562596110aSChristoph Hellwig if (fh_len <= 2)
14572596110aSChristoph Hellwig return NULL;
14582596110aSChristoph Hellwig
14592596110aSChristoph Hellwig switch (fh_type) {
14602596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT:
14612596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino,
14622596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0));
14632596110aSChristoph Hellwig break;
14642596110aSChristoph Hellwig }
14652596110aSChristoph Hellwig
14664ea3ada2SChristoph Hellwig return d_obtain_alias(inode);
14672596110aSChristoph Hellwig }
14682596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent);
14692596110aSChristoph Hellwig
14701b061d92SChristoph Hellwig /**
1471ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems
1472ac13a829SFabian Frederick *
14731b061d92SChristoph Hellwig * @file: file to synchronize
1474ac13a829SFabian Frederick * @start: start offset in bytes
1475ac13a829SFabian Frederick * @end: end offset in bytes (inclusive)
14761b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true
14771b061d92SChristoph Hellwig *
14781b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple
14791b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list
14801b061d92SChristoph Hellwig * hanging off the address_space structure.
14811b061d92SChristoph Hellwig */
__generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1482ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
148302c24a82SJosef Bacik int datasync)
1484d5aacad5SAl Viro {
14857ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host;
1486d5aacad5SAl Viro int err;
1487d5aacad5SAl Viro int ret;
1488d5aacad5SAl Viro
1489383aa543SJeff Layton err = file_write_and_wait_range(file, start, end);
149002c24a82SJosef Bacik if (err)
149102c24a82SJosef Bacik return err;
149202c24a82SJosef Bacik
14935955102cSAl Viro inode_lock(inode);
1494d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping);
14950ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL))
149602c24a82SJosef Bacik goto out;
1497d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
149802c24a82SJosef Bacik goto out;
1499d5aacad5SAl Viro
1500c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1);
1501d5aacad5SAl Viro if (ret == 0)
1502d5aacad5SAl Viro ret = err;
1503ac13a829SFabian Frederick
150402c24a82SJosef Bacik out:
15055955102cSAl Viro inode_unlock(inode);
1506383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */
1507383aa543SJeff Layton err = file_check_and_advance_wb_err(file);
1508383aa543SJeff Layton if (ret == 0)
1509383aa543SJeff Layton ret = err;
1510383aa543SJeff Layton return ret;
1511d5aacad5SAl Viro }
1512ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync);
1513ac13a829SFabian Frederick
1514ac13a829SFabian Frederick /**
1515ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems
1516ac13a829SFabian Frederick * with flush
1517ac13a829SFabian Frederick * @file: file to synchronize
1518ac13a829SFabian Frederick * @start: start offset in bytes
1519ac13a829SFabian Frederick * @end: end offset in bytes (inclusive)
1520ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true
1521ac13a829SFabian Frederick *
1522ac13a829SFabian Frederick */
1523ac13a829SFabian Frederick
generic_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)1524ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1525ac13a829SFabian Frederick int datasync)
1526ac13a829SFabian Frederick {
1527ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host;
1528ac13a829SFabian Frederick int err;
1529ac13a829SFabian Frederick
1530ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync);
1531ac13a829SFabian Frederick if (err)
1532ac13a829SFabian Frederick return err;
1533c6bf3f0eSChristoph Hellwig return blkdev_issue_flush(inode->i_sb->s_bdev);
1534ac13a829SFabian Frederick }
15351b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync);
15361b061d92SChristoph Hellwig
153730ca22c7SPatrick J. LoPresti /**
153830ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system
153930ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size
154030ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system
154130ca22c7SPatrick J. LoPresti *
154230ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a
154330ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t
154430ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise.
154530ca22c7SPatrick J. LoPresti */
generic_check_addressable(unsigned blocksize_bits,u64 num_blocks)154630ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
154730ca22c7SPatrick J. LoPresti {
154830ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1;
1549a33f13efSJoel Becker u64 last_fs_page =
155009cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits);
155130ca22c7SPatrick J. LoPresti
155230ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0))
155330ca22c7SPatrick J. LoPresti return 0;
155430ca22c7SPatrick J. LoPresti
155509cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
155630ca22c7SPatrick J. LoPresti return -EINVAL;
155730ca22c7SPatrick J. LoPresti
1558a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1559a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) {
156030ca22c7SPatrick J. LoPresti return -EFBIG;
156130ca22c7SPatrick J. LoPresti }
156230ca22c7SPatrick J. LoPresti return 0;
156330ca22c7SPatrick J. LoPresti }
156430ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable);
156530ca22c7SPatrick J. LoPresti
15661b061d92SChristoph Hellwig /*
15671b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems.
15681b061d92SChristoph Hellwig */
noop_fsync(struct file * file,loff_t start,loff_t end,int datasync)156902c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
15701b061d92SChristoph Hellwig {
15711b061d92SChristoph Hellwig return 0;
15721b061d92SChristoph Hellwig }
15731b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync);
157487dc800bSAl Viro
noop_direct_IO(struct kiocb * iocb,struct iov_iter * iter)1575f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1576f44c7763SDan Williams {
1577f44c7763SDan Williams /*
1578f44c7763SDan Williams * iomap based filesystems support direct I/O without need for
1579f44c7763SDan Williams * this callback. However, it still needs to be set in
1580f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is
1581f44c7763SDan Williams * generally supported.
1582f44c7763SDan Williams */
1583f44c7763SDan Williams return -EINVAL;
1584f44c7763SDan Williams }
1585f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO);
1586f44c7763SDan Williams
1587fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
kfree_link(void * p)1588fceef393SAl Viro void kfree_link(void *p)
158987dc800bSAl Viro {
1590fceef393SAl Viro kfree(p);
159187dc800bSAl Viro }
1592fceef393SAl Viro EXPORT_SYMBOL(kfree_link);
15936987843fSAl Viro
alloc_anon_inode(struct super_block * s)15946987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s)
15956987843fSAl Viro {
15966987843fSAl Viro static const struct address_space_operations anon_aops = {
159746de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio,
15986987843fSAl Viro };
15996987843fSAl Viro struct inode *inode = new_inode_pseudo(s);
16006987843fSAl Viro
16016987843fSAl Viro if (!inode)
16026987843fSAl Viro return ERR_PTR(-ENOMEM);
16036987843fSAl Viro
16046987843fSAl Viro inode->i_ino = get_next_ino();
16056987843fSAl Viro inode->i_mapping->a_ops = &anon_aops;
16066987843fSAl Viro
16076987843fSAl Viro /*
16086987843fSAl Viro * Mark the inode dirty from the very beginning,
16096987843fSAl Viro * that way it will never be moved to the dirty
16106987843fSAl Viro * list because mark_inode_dirty() will think
16116987843fSAl Viro * that it already _is_ on the dirty list.
16126987843fSAl Viro */
16136987843fSAl Viro inode->i_state = I_DIRTY;
16146987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR;
16156987843fSAl Viro inode->i_uid = current_fsuid();
16166987843fSAl Viro inode->i_gid = current_fsgid();
16176987843fSAl Viro inode->i_flags |= S_PRIVATE;
16185b5599a7SJeff Layton simple_inode_init_ts(inode);
16196987843fSAl Viro return inode;
16206987843fSAl Viro }
16216987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode);
16221c994a09SJeff Layton
16231c994a09SJeff Layton /**
16241c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases
16251c994a09SJeff Layton * @filp: file pointer
16261c994a09SJeff Layton * @arg: type of lease to obtain
16271c994a09SJeff Layton * @flp: new lease supplied for insertion
1628e6f5c789SJeff Layton * @priv: private data for lm_setup operation
16291c994a09SJeff Layton *
16301c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set.
16311c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL.
16321c994a09SJeff Layton */
16331c994a09SJeff Layton int
simple_nosetlease(struct file * filp,int arg,struct file_lock ** flp,void ** priv)1634ed5f17f6SLuca Vizzarro simple_nosetlease(struct file *filp, int arg, struct file_lock **flp,
1635e6f5c789SJeff Layton void **priv)
16361c994a09SJeff Layton {
16371c994a09SJeff Layton return -EINVAL;
16381c994a09SJeff Layton }
16391c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease);
164061ba64fcSAl Viro
16416ee9706aSEric Biggers /**
16426ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks
16436ee9706aSEric Biggers * @dentry: not used here
16446ee9706aSEric Biggers * @inode: the symlink inode
16456ee9706aSEric Biggers * @done: not used here
16466ee9706aSEric Biggers *
16476ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to
16486ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called,
16496ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link
16506ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set,
16516ee9706aSEric Biggers * to mark the inode_operations as being for a symlink.
16526ee9706aSEric Biggers *
16536ee9706aSEric Biggers * Return: the symlink target
16546ee9706aSEric Biggers */
simple_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)16556b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1656fceef393SAl Viro struct delayed_call *done)
165761ba64fcSAl Viro {
16586b255391SAl Viro return inode->i_link;
165961ba64fcSAl Viro }
16606b255391SAl Viro EXPORT_SYMBOL(simple_get_link);
166161ba64fcSAl Viro
166261ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = {
16636b255391SAl Viro .get_link = simple_get_link,
166461ba64fcSAl Viro };
166561ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations);
1666fbabfd0fSEric W. Biederman
1667fbabfd0fSEric W. Biederman /*
1668fbabfd0fSEric W. Biederman * Operations for a permanently empty directory.
1669fbabfd0fSEric W. Biederman */
empty_dir_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1670fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1671fbabfd0fSEric W. Biederman {
1672fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT);
1673fbabfd0fSEric W. Biederman }
1674fbabfd0fSEric W. Biederman
empty_dir_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)1675b74d24f7SChristian Brauner static int empty_dir_getattr(struct mnt_idmap *idmap,
1676549c7297SChristian Brauner const struct path *path, struct kstat *stat,
1677a528d35eSDavid Howells u32 request_mask, unsigned int query_flags)
1678fbabfd0fSEric W. Biederman {
1679a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry);
16800d72b928SJeff Layton generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
1681fbabfd0fSEric W. Biederman return 0;
1682fbabfd0fSEric W. Biederman }
1683fbabfd0fSEric W. Biederman
empty_dir_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)1684c1632a0fSChristian Brauner static int empty_dir_setattr(struct mnt_idmap *idmap,
1685549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr)
1686fbabfd0fSEric W. Biederman {
1687fbabfd0fSEric W. Biederman return -EPERM;
1688fbabfd0fSEric W. Biederman }
1689fbabfd0fSEric W. Biederman
empty_dir_listxattr(struct dentry * dentry,char * list,size_t size)1690fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1691fbabfd0fSEric W. Biederman {
1692fbabfd0fSEric W. Biederman return -EOPNOTSUPP;
1693fbabfd0fSEric W. Biederman }
1694fbabfd0fSEric W. Biederman
1695fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = {
1696fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup,
1697fbabfd0fSEric W. Biederman .permission = generic_permission,
1698fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr,
1699fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr,
1700fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr,
1701fbabfd0fSEric W. Biederman };
1702fbabfd0fSEric W. Biederman
empty_dir_llseek(struct file * file,loff_t offset,int whence)1703fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1704fbabfd0fSEric W. Biederman {
1705fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */
1706fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2);
1707fbabfd0fSEric W. Biederman }
1708fbabfd0fSEric W. Biederman
empty_dir_readdir(struct file * file,struct dir_context * ctx)1709fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1710fbabfd0fSEric W. Biederman {
1711fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx);
1712fbabfd0fSEric W. Biederman return 0;
1713fbabfd0fSEric W. Biederman }
1714fbabfd0fSEric W. Biederman
1715fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = {
1716fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek,
1717fbabfd0fSEric W. Biederman .read = generic_read_dir,
1718c51da20cSAl Viro .iterate_shared = empty_dir_readdir,
1719fbabfd0fSEric W. Biederman .fsync = noop_fsync,
1720fbabfd0fSEric W. Biederman };
1721fbabfd0fSEric W. Biederman
1722fbabfd0fSEric W. Biederman
make_empty_dir_inode(struct inode * inode)1723fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode)
1724fbabfd0fSEric W. Biederman {
1725fbabfd0fSEric W. Biederman set_nlink(inode, 2);
1726fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1727fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID;
1728fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID;
1729fbabfd0fSEric W. Biederman inode->i_rdev = 0;
17304b75de86SEric W. Biederman inode->i_size = 0;
1731fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT;
1732fbabfd0fSEric W. Biederman inode->i_blocks = 0;
1733fbabfd0fSEric W. Biederman
1734fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations;
1735f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR;
1736fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations;
1737fbabfd0fSEric W. Biederman }
1738fbabfd0fSEric W. Biederman
is_empty_dir_inode(struct inode * inode)1739fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode)
1740fbabfd0fSEric W. Biederman {
1741fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) &&
1742fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations);
1743fbabfd0fSEric W. Biederman }
1744c843843eSDaniel Rosenberg
17455298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1746c843843eSDaniel Rosenberg /**
1747c843843eSDaniel Rosenberg * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1748c843843eSDaniel Rosenberg * @dentry: dentry whose name we are checking against
1749c843843eSDaniel Rosenberg * @len: len of name of dentry
1750c843843eSDaniel Rosenberg * @str: str pointer to name of dentry
1751c843843eSDaniel Rosenberg * @name: Name to compare against
1752c843843eSDaniel Rosenberg *
1753c843843eSDaniel Rosenberg * Return: 0 if names match, 1 if mismatch, or -ERRNO
1754c843843eSDaniel Rosenberg */
generic_ci_d_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)1755794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1756c843843eSDaniel Rosenberg const char *str, const struct qstr *name)
1757c843843eSDaniel Rosenberg {
1758c843843eSDaniel Rosenberg const struct dentry *parent = READ_ONCE(dentry->d_parent);
1759c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(parent->d_inode);
1760c843843eSDaniel Rosenberg const struct super_block *sb = dentry->d_sb;
1761c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding;
1762c843843eSDaniel Rosenberg struct qstr qstr = QSTR_INIT(str, len);
1763c843843eSDaniel Rosenberg char strbuf[DNAME_INLINE_LEN];
1764c843843eSDaniel Rosenberg int ret;
1765c843843eSDaniel Rosenberg
1766af494af3SEric Biggers if (!dir || !IS_CASEFOLDED(dir))
1767c843843eSDaniel Rosenberg goto fallback;
1768c843843eSDaniel Rosenberg /*
1769c843843eSDaniel Rosenberg * If the dentry name is stored in-line, then it may be concurrently
1770c843843eSDaniel Rosenberg * modified by a rename. If this happens, the VFS will eventually retry
1771c843843eSDaniel Rosenberg * the lookup, so it doesn't matter what ->d_compare() returns.
1772c843843eSDaniel Rosenberg * However, it's unsafe to call utf8_strncasecmp() with an unstable
1773c843843eSDaniel Rosenberg * string. Therefore, we have to copy the name into a temporary buffer.
1774c843843eSDaniel Rosenberg */
1775c843843eSDaniel Rosenberg if (len <= DNAME_INLINE_LEN - 1) {
1776c843843eSDaniel Rosenberg memcpy(strbuf, str, len);
1777c843843eSDaniel Rosenberg strbuf[len] = 0;
1778c843843eSDaniel Rosenberg qstr.name = strbuf;
1779c843843eSDaniel Rosenberg /* prevent compiler from optimizing out the temporary buffer */
1780c843843eSDaniel Rosenberg barrier();
1781c843843eSDaniel Rosenberg }
1782c843843eSDaniel Rosenberg ret = utf8_strncasecmp(um, name, &qstr);
1783c843843eSDaniel Rosenberg if (ret >= 0)
1784c843843eSDaniel Rosenberg return ret;
1785c843843eSDaniel Rosenberg
1786c843843eSDaniel Rosenberg if (sb_has_strict_encoding(sb))
1787c843843eSDaniel Rosenberg return -EINVAL;
1788c843843eSDaniel Rosenberg fallback:
1789c843843eSDaniel Rosenberg if (len != name->len)
1790c843843eSDaniel Rosenberg return 1;
1791c843843eSDaniel Rosenberg return !!memcmp(str, name->name, len);
1792c843843eSDaniel Rosenberg }
1793c843843eSDaniel Rosenberg
1794c843843eSDaniel Rosenberg /**
1795c843843eSDaniel Rosenberg * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1796c843843eSDaniel Rosenberg * @dentry: dentry of the parent directory
1797c843843eSDaniel Rosenberg * @str: qstr of name whose hash we should fill in
1798c843843eSDaniel Rosenberg *
1799c843843eSDaniel Rosenberg * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1800c843843eSDaniel Rosenberg */
generic_ci_d_hash(const struct dentry * dentry,struct qstr * str)1801794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1802c843843eSDaniel Rosenberg {
1803c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(dentry->d_inode);
1804c843843eSDaniel Rosenberg struct super_block *sb = dentry->d_sb;
1805c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding;
1806c843843eSDaniel Rosenberg int ret = 0;
1807c843843eSDaniel Rosenberg
1808af494af3SEric Biggers if (!dir || !IS_CASEFOLDED(dir))
1809c843843eSDaniel Rosenberg return 0;
1810c843843eSDaniel Rosenberg
1811c843843eSDaniel Rosenberg ret = utf8_casefold_hash(um, dentry, str);
1812c843843eSDaniel Rosenberg if (ret < 0 && sb_has_strict_encoding(sb))
1813c843843eSDaniel Rosenberg return -EINVAL;
1814c843843eSDaniel Rosenberg return 0;
1815c843843eSDaniel Rosenberg }
1816608af703SDaniel Rosenberg
1817608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = {
1818608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash,
1819608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare,
1820608af703SDaniel Rosenberg };
1821c843843eSDaniel Rosenberg #endif
1822608af703SDaniel Rosenberg
1823608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1824608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = {
1825608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate,
1826608af703SDaniel Rosenberg };
1827608af703SDaniel Rosenberg #endif
1828608af703SDaniel Rosenberg
18295298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1830608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1831608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash,
1832608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare,
1833608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate,
1834608af703SDaniel Rosenberg };
1835608af703SDaniel Rosenberg #endif
1836608af703SDaniel Rosenberg
1837608af703SDaniel Rosenberg /**
1838608af703SDaniel Rosenberg * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1839608af703SDaniel Rosenberg * @dentry: dentry to set ops on
1840608af703SDaniel Rosenberg *
1841608af703SDaniel Rosenberg * Casefolded directories need d_hash and d_compare set, so that the dentries
1842608af703SDaniel Rosenberg * contained in them are handled case-insensitively. Note that these operations
1843608af703SDaniel Rosenberg * are needed on the parent directory rather than on the dentries in it, and
1844608af703SDaniel Rosenberg * while the casefolding flag can be toggled on and off on an empty directory,
1845608af703SDaniel Rosenberg * dentry_operations can't be changed later. As a result, if the filesystem has
1846608af703SDaniel Rosenberg * casefolding support enabled at all, we have to give all dentries the
1847608af703SDaniel Rosenberg * casefolding operations even if their inode doesn't have the casefolding flag
1848608af703SDaniel Rosenberg * currently (and thus the casefolding ops would be no-ops for now).
1849608af703SDaniel Rosenberg *
1850608af703SDaniel Rosenberg * Encryption works differently in that the only dentry operation it needs is
1851608af703SDaniel Rosenberg * d_revalidate, which it only needs on dentries that have the no-key name flag.
1852608af703SDaniel Rosenberg * The no-key flag can't be set "later", so we don't have to worry about that.
1853608af703SDaniel Rosenberg *
1854608af703SDaniel Rosenberg * Finally, to maximize compatibility with overlayfs (which isn't compatible
1855608af703SDaniel Rosenberg * with certain dentry operations) and to avoid taking an unnecessary
1856608af703SDaniel Rosenberg * performance hit, we use custom dentry_operations for each possible
1857608af703SDaniel Rosenberg * combination rather than always installing all operations.
1858608af703SDaniel Rosenberg */
generic_set_encrypted_ci_d_ops(struct dentry * dentry)1859608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
1860608af703SDaniel Rosenberg {
1861608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1862608af703SDaniel Rosenberg bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
1863608af703SDaniel Rosenberg #endif
18645298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1865608af703SDaniel Rosenberg bool needs_ci_ops = dentry->d_sb->s_encoding;
1866608af703SDaniel Rosenberg #endif
18675298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE)
1868608af703SDaniel Rosenberg if (needs_encrypt_ops && needs_ci_ops) {
1869608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1870608af703SDaniel Rosenberg return;
1871608af703SDaniel Rosenberg }
1872608af703SDaniel Rosenberg #endif
1873608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1874608af703SDaniel Rosenberg if (needs_encrypt_ops) {
1875608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1876608af703SDaniel Rosenberg return;
1877608af703SDaniel Rosenberg }
1878608af703SDaniel Rosenberg #endif
18795298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
1880608af703SDaniel Rosenberg if (needs_ci_ops) {
1881608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_ci_dentry_ops);
1882608af703SDaniel Rosenberg return;
1883608af703SDaniel Rosenberg }
1884608af703SDaniel Rosenberg #endif
1885608af703SDaniel Rosenberg }
1886608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);
18875ca14835SAndrew Morton
18885ca14835SAndrew Morton /**
18895ca14835SAndrew Morton * inode_maybe_inc_iversion - increments i_version
18905ca14835SAndrew Morton * @inode: inode with the i_version that should be updated
18915ca14835SAndrew Morton * @force: increment the counter even if it's not necessary?
18925ca14835SAndrew Morton *
18935ca14835SAndrew Morton * Every time the inode is modified, the i_version field must be seen to have
18945ca14835SAndrew Morton * changed by any observer.
18955ca14835SAndrew Morton *
18965ca14835SAndrew Morton * If "force" is set or the QUERIED flag is set, then ensure that we increment
18975ca14835SAndrew Morton * the value, and clear the queried flag.
18985ca14835SAndrew Morton *
18995ca14835SAndrew Morton * In the common case where neither is set, then we can return "false" without
19005ca14835SAndrew Morton * updating i_version.
19015ca14835SAndrew Morton *
19025ca14835SAndrew Morton * If this function returns false, and no other metadata has changed, then we
19035ca14835SAndrew Morton * can avoid logging the metadata.
19045ca14835SAndrew Morton */
inode_maybe_inc_iversion(struct inode * inode,bool force)19055ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force)
19065ca14835SAndrew Morton {
19075ca14835SAndrew Morton u64 cur, new;
19085ca14835SAndrew Morton
19095ca14835SAndrew Morton /*
19105ca14835SAndrew Morton * The i_version field is not strictly ordered with any other inode
19115ca14835SAndrew Morton * information, but the legacy inode_inc_iversion code used a spinlock
19125ca14835SAndrew Morton * to serialize increments.
19135ca14835SAndrew Morton *
19145ca14835SAndrew Morton * Here, we add full memory barriers to ensure that any de-facto
19155ca14835SAndrew Morton * ordering with other info is preserved.
19165ca14835SAndrew Morton *
19175ca14835SAndrew Morton * This barrier pairs with the barrier in inode_query_iversion()
19185ca14835SAndrew Morton */
19195ca14835SAndrew Morton smp_mb();
19205ca14835SAndrew Morton cur = inode_peek_iversion_raw(inode);
19215ca14835SAndrew Morton do {
19225ca14835SAndrew Morton /* If flag is clear then we needn't do anything */
19235ca14835SAndrew Morton if (!force && !(cur & I_VERSION_QUERIED))
19245ca14835SAndrew Morton return false;
19255ca14835SAndrew Morton
19265ca14835SAndrew Morton /* Since lowest bit is flag, add 2 to avoid it */
19275ca14835SAndrew Morton new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT;
19285ca14835SAndrew Morton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
19295ca14835SAndrew Morton return true;
19305ca14835SAndrew Morton }
19315ca14835SAndrew Morton EXPORT_SYMBOL(inode_maybe_inc_iversion);
1932c5bc1b3fSJeff Layton
1933c5bc1b3fSJeff Layton /**
1934c5bc1b3fSJeff Layton * inode_query_iversion - read i_version for later use
1935c5bc1b3fSJeff Layton * @inode: inode from which i_version should be read
1936c5bc1b3fSJeff Layton *
1937c5bc1b3fSJeff Layton * Read the inode i_version counter. This should be used by callers that wish
1938c5bc1b3fSJeff Layton * to store the returned i_version for later comparison. This will guarantee
1939c5bc1b3fSJeff Layton * that a later query of the i_version will result in a different value if
1940c5bc1b3fSJeff Layton * anything has changed.
1941c5bc1b3fSJeff Layton *
1942c5bc1b3fSJeff Layton * In this implementation, we fetch the current value, set the QUERIED flag and
1943c5bc1b3fSJeff Layton * then try to swap it into place with a cmpxchg, if it wasn't already set. If
1944c5bc1b3fSJeff Layton * that fails, we try again with the newly fetched value from the cmpxchg.
1945c5bc1b3fSJeff Layton */
inode_query_iversion(struct inode * inode)1946c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode)
1947c5bc1b3fSJeff Layton {
1948c5bc1b3fSJeff Layton u64 cur, new;
1949c5bc1b3fSJeff Layton
1950c5bc1b3fSJeff Layton cur = inode_peek_iversion_raw(inode);
1951c5bc1b3fSJeff Layton do {
1952c5bc1b3fSJeff Layton /* If flag is already set, then no need to swap */
1953c5bc1b3fSJeff Layton if (cur & I_VERSION_QUERIED) {
1954c5bc1b3fSJeff Layton /*
1955c5bc1b3fSJeff Layton * This barrier (and the implicit barrier in the
1956c5bc1b3fSJeff Layton * cmpxchg below) pairs with the barrier in
1957c5bc1b3fSJeff Layton * inode_maybe_inc_iversion().
1958c5bc1b3fSJeff Layton */
1959c5bc1b3fSJeff Layton smp_mb();
1960c5bc1b3fSJeff Layton break;
1961c5bc1b3fSJeff Layton }
1962c5bc1b3fSJeff Layton
1963c5bc1b3fSJeff Layton new = cur | I_VERSION_QUERIED;
1964c5bc1b3fSJeff Layton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
1965c5bc1b3fSJeff Layton return cur >> I_VERSION_QUERIED_SHIFT;
1966c5bc1b3fSJeff Layton }
1967c5bc1b3fSJeff Layton EXPORT_SYMBOL(inode_query_iversion);
196844fff0faSChristoph Hellwig
direct_write_fallback(struct kiocb * iocb,struct iov_iter * iter,ssize_t direct_written,ssize_t buffered_written)196944fff0faSChristoph Hellwig ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
197044fff0faSChristoph Hellwig ssize_t direct_written, ssize_t buffered_written)
197144fff0faSChristoph Hellwig {
197244fff0faSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping;
197344fff0faSChristoph Hellwig loff_t pos = iocb->ki_pos - buffered_written;
197444fff0faSChristoph Hellwig loff_t end = iocb->ki_pos - 1;
197544fff0faSChristoph Hellwig int err;
197644fff0faSChristoph Hellwig
197744fff0faSChristoph Hellwig /*
197844fff0faSChristoph Hellwig * If the buffered write fallback returned an error, we want to return
197944fff0faSChristoph Hellwig * the number of bytes which were written by direct I/O, or the error
198044fff0faSChristoph Hellwig * code if that was zero.
198144fff0faSChristoph Hellwig *
198244fff0faSChristoph Hellwig * Note that this differs from normal direct-io semantics, which will
198344fff0faSChristoph Hellwig * return -EFOO even if some bytes were written.
198444fff0faSChristoph Hellwig */
198544fff0faSChristoph Hellwig if (unlikely(buffered_written < 0)) {
198644fff0faSChristoph Hellwig if (direct_written)
198744fff0faSChristoph Hellwig return direct_written;
198844fff0faSChristoph Hellwig return buffered_written;
198944fff0faSChristoph Hellwig }
199044fff0faSChristoph Hellwig
199144fff0faSChristoph Hellwig /*
199244fff0faSChristoph Hellwig * We need to ensure that the page cache pages are written to disk and
199344fff0faSChristoph Hellwig * invalidated to preserve the expected O_DIRECT semantics.
199444fff0faSChristoph Hellwig */
199544fff0faSChristoph Hellwig err = filemap_write_and_wait_range(mapping, pos, end);
199644fff0faSChristoph Hellwig if (err < 0) {
199744fff0faSChristoph Hellwig /*
199844fff0faSChristoph Hellwig * We don't know how much we wrote, so just return the number of
199944fff0faSChristoph Hellwig * bytes which were direct-written
200044fff0faSChristoph Hellwig */
20018287474aSAl Viro iocb->ki_pos -= buffered_written;
200244fff0faSChristoph Hellwig if (direct_written)
200344fff0faSChristoph Hellwig return direct_written;
200444fff0faSChristoph Hellwig return err;
200544fff0faSChristoph Hellwig }
200644fff0faSChristoph Hellwig invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
200744fff0faSChristoph Hellwig return direct_written + buffered_written;
200844fff0faSChristoph Hellwig }
200944fff0faSChristoph Hellwig EXPORT_SYMBOL_GPL(direct_write_fallback);
20105b5599a7SJeff Layton
20115b5599a7SJeff Layton /**
20125b5599a7SJeff Layton * simple_inode_init_ts - initialize the timestamps for a new inode
20135b5599a7SJeff Layton * @inode: inode to be initialized
20145b5599a7SJeff Layton *
20155b5599a7SJeff Layton * When a new inode is created, most filesystems set the timestamps to the
20165b5599a7SJeff Layton * current time. Add a helper to do this.
20175b5599a7SJeff Layton */
simple_inode_init_ts(struct inode * inode)20185b5599a7SJeff Layton struct timespec64 simple_inode_init_ts(struct inode *inode)
20195b5599a7SJeff Layton {
20205b5599a7SJeff Layton struct timespec64 ts = inode_set_ctime_current(inode);
20215b5599a7SJeff Layton
20225b5599a7SJeff Layton inode_set_atime_to_ts(inode, ts);
20235b5599a7SJeff Layton inode_set_mtime_to_ts(inode, ts);
20245b5599a7SJeff Layton return ts;
20255b5599a7SJeff Layton }
20265b5599a7SJeff Layton EXPORT_SYMBOL(simple_inode_init_ts);
2027