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 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); 36b74d24f7SChristian Brauner generic_fillattr(&nop_mnt_idmap, 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 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 */ 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 */ 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 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 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 */ 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 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 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 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 2426faddda6SChuck Lever static void offset_set(struct dentry *dentry, u32 offset) 2436faddda6SChuck Lever { 2446faddda6SChuck Lever dentry->d_fsdata = (void *)((uintptr_t)(offset)); 2456faddda6SChuck Lever } 2466faddda6SChuck Lever 2476faddda6SChuck Lever static u32 dentry2offset(struct dentry *dentry) 2486faddda6SChuck Lever { 2496faddda6SChuck Lever return (u32)((uintptr_t)(dentry->d_fsdata)); 2506faddda6SChuck Lever } 2516faddda6SChuck Lever 252bbaef797SChuck Lever static struct lock_class_key simple_offset_xa_lock; 253bbaef797SChuck Lever 2546faddda6SChuck Lever /** 2556faddda6SChuck Lever * simple_offset_init - initialize an offset_ctx 2566faddda6SChuck Lever * @octx: directory offset map to be initialized 2576faddda6SChuck Lever * 2586faddda6SChuck Lever */ 2596faddda6SChuck Lever void simple_offset_init(struct offset_ctx *octx) 2606faddda6SChuck Lever { 2616faddda6SChuck Lever xa_init_flags(&octx->xa, XA_FLAGS_ALLOC1); 262bbaef797SChuck Lever lockdep_set_class(&octx->xa.xa_lock, &simple_offset_xa_lock); 2636faddda6SChuck Lever 2646faddda6SChuck Lever /* 0 is '.', 1 is '..', so always start with offset 2 */ 2656faddda6SChuck Lever octx->next_offset = 2; 2666faddda6SChuck Lever } 2676faddda6SChuck Lever 2686faddda6SChuck Lever /** 2696faddda6SChuck Lever * simple_offset_add - Add an entry to a directory's offset map 2706faddda6SChuck Lever * @octx: directory offset ctx to be updated 2716faddda6SChuck Lever * @dentry: new dentry being added 2726faddda6SChuck Lever * 2736faddda6SChuck Lever * Returns zero on success. @so_ctx and the dentry offset are updated. 2746faddda6SChuck Lever * Otherwise, a negative errno value is returned. 2756faddda6SChuck Lever */ 2766faddda6SChuck Lever int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) 2776faddda6SChuck Lever { 2786faddda6SChuck Lever static const struct xa_limit limit = XA_LIMIT(2, U32_MAX); 2796faddda6SChuck Lever u32 offset; 2806faddda6SChuck Lever int ret; 2816faddda6SChuck Lever 2826faddda6SChuck Lever if (dentry2offset(dentry) != 0) 2836faddda6SChuck Lever return -EBUSY; 2846faddda6SChuck Lever 2856faddda6SChuck Lever ret = xa_alloc_cyclic(&octx->xa, &offset, dentry, limit, 2866faddda6SChuck Lever &octx->next_offset, GFP_KERNEL); 2876faddda6SChuck Lever if (ret < 0) 2886faddda6SChuck Lever return ret; 2896faddda6SChuck Lever 2906faddda6SChuck Lever offset_set(dentry, offset); 2916faddda6SChuck Lever return 0; 2926faddda6SChuck Lever } 2936faddda6SChuck Lever 2946faddda6SChuck Lever /** 2956faddda6SChuck Lever * simple_offset_remove - Remove an entry to a directory's offset map 2966faddda6SChuck Lever * @octx: directory offset ctx to be updated 2976faddda6SChuck Lever * @dentry: dentry being removed 2986faddda6SChuck Lever * 2996faddda6SChuck Lever */ 3006faddda6SChuck Lever void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry) 3016faddda6SChuck Lever { 3026faddda6SChuck Lever u32 offset; 3036faddda6SChuck Lever 3046faddda6SChuck Lever offset = dentry2offset(dentry); 3056faddda6SChuck Lever if (offset == 0) 3066faddda6SChuck Lever return; 3076faddda6SChuck Lever 3086faddda6SChuck Lever xa_erase(&octx->xa, offset); 3096faddda6SChuck Lever offset_set(dentry, 0); 3106faddda6SChuck Lever } 3116faddda6SChuck Lever 3126faddda6SChuck Lever /** 3136faddda6SChuck Lever * simple_offset_rename_exchange - exchange rename with directory offsets 3146faddda6SChuck Lever * @old_dir: parent of dentry being moved 3156faddda6SChuck Lever * @old_dentry: dentry being moved 3166faddda6SChuck Lever * @new_dir: destination parent 3176faddda6SChuck Lever * @new_dentry: destination dentry 3186faddda6SChuck Lever * 3196faddda6SChuck Lever * Returns zero on success. Otherwise a negative errno is returned and the 3206faddda6SChuck Lever * rename is rolled back. 3216faddda6SChuck Lever */ 3226faddda6SChuck Lever int simple_offset_rename_exchange(struct inode *old_dir, 3236faddda6SChuck Lever struct dentry *old_dentry, 3246faddda6SChuck Lever struct inode *new_dir, 3256faddda6SChuck Lever struct dentry *new_dentry) 3266faddda6SChuck Lever { 3276faddda6SChuck Lever struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir); 3286faddda6SChuck Lever struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir); 3296faddda6SChuck Lever u32 old_index = dentry2offset(old_dentry); 3306faddda6SChuck Lever u32 new_index = dentry2offset(new_dentry); 3316faddda6SChuck Lever int ret; 3326faddda6SChuck Lever 3336faddda6SChuck Lever simple_offset_remove(old_ctx, old_dentry); 3346faddda6SChuck Lever simple_offset_remove(new_ctx, new_dentry); 3356faddda6SChuck Lever 3366faddda6SChuck Lever ret = simple_offset_add(new_ctx, old_dentry); 3376faddda6SChuck Lever if (ret) 3386faddda6SChuck Lever goto out_restore; 3396faddda6SChuck Lever 3406faddda6SChuck Lever ret = simple_offset_add(old_ctx, new_dentry); 3416faddda6SChuck Lever if (ret) { 3426faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry); 3436faddda6SChuck Lever goto out_restore; 3446faddda6SChuck Lever } 3456faddda6SChuck Lever 3466faddda6SChuck Lever ret = simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 3476faddda6SChuck Lever if (ret) { 3486faddda6SChuck Lever simple_offset_remove(new_ctx, old_dentry); 3496faddda6SChuck Lever simple_offset_remove(old_ctx, new_dentry); 3506faddda6SChuck Lever goto out_restore; 3516faddda6SChuck Lever } 3526faddda6SChuck Lever return 0; 3536faddda6SChuck Lever 3546faddda6SChuck Lever out_restore: 3556faddda6SChuck Lever offset_set(old_dentry, old_index); 3566faddda6SChuck Lever xa_store(&old_ctx->xa, old_index, old_dentry, GFP_KERNEL); 3576faddda6SChuck Lever offset_set(new_dentry, new_index); 3586faddda6SChuck Lever xa_store(&new_ctx->xa, new_index, new_dentry, GFP_KERNEL); 3596faddda6SChuck Lever return ret; 3606faddda6SChuck Lever } 3616faddda6SChuck Lever 3626faddda6SChuck Lever /** 3636faddda6SChuck Lever * simple_offset_destroy - Release offset map 3646faddda6SChuck Lever * @octx: directory offset ctx that is about to be destroyed 3656faddda6SChuck Lever * 3666faddda6SChuck Lever * During fs teardown (eg. umount), a directory's offset map might still 3676faddda6SChuck Lever * contain entries. xa_destroy() cleans out anything that remains. 3686faddda6SChuck Lever */ 3696faddda6SChuck Lever void simple_offset_destroy(struct offset_ctx *octx) 3706faddda6SChuck Lever { 3716faddda6SChuck Lever xa_destroy(&octx->xa); 3726faddda6SChuck Lever } 3736faddda6SChuck Lever 3746faddda6SChuck Lever /** 3756faddda6SChuck Lever * offset_dir_llseek - Advance the read position of a directory descriptor 3766faddda6SChuck Lever * @file: an open directory whose position is to be updated 3776faddda6SChuck Lever * @offset: a byte offset 3786faddda6SChuck Lever * @whence: enumerator describing the starting position for this update 3796faddda6SChuck Lever * 3806faddda6SChuck Lever * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories. 3816faddda6SChuck Lever * 3826faddda6SChuck Lever * Returns the updated read position if successful; otherwise a 3836faddda6SChuck Lever * negative errno is returned and the read position remains unchanged. 3846faddda6SChuck Lever */ 3856faddda6SChuck Lever static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) 3866faddda6SChuck Lever { 3876faddda6SChuck Lever switch (whence) { 3886faddda6SChuck Lever case SEEK_CUR: 3896faddda6SChuck Lever offset += file->f_pos; 3906faddda6SChuck Lever fallthrough; 3916faddda6SChuck Lever case SEEK_SET: 3926faddda6SChuck Lever if (offset >= 0) 3936faddda6SChuck Lever break; 3946faddda6SChuck Lever fallthrough; 3956faddda6SChuck Lever default: 3966faddda6SChuck Lever return -EINVAL; 3976faddda6SChuck Lever } 3986faddda6SChuck Lever 3996faddda6SChuck Lever return vfs_setpos(file, offset, U32_MAX); 4006faddda6SChuck Lever } 4016faddda6SChuck Lever 4026faddda6SChuck Lever static struct dentry *offset_find_next(struct xa_state *xas) 4036faddda6SChuck Lever { 4046faddda6SChuck Lever struct dentry *child, *found = NULL; 4056faddda6SChuck Lever 4066faddda6SChuck Lever rcu_read_lock(); 4076faddda6SChuck Lever child = xas_next_entry(xas, U32_MAX); 4086faddda6SChuck Lever if (!child) 4096faddda6SChuck Lever goto out; 410*2be4f05aSChuck Lever spin_lock(&child->d_lock); 4116faddda6SChuck Lever if (simple_positive(child)) 4126faddda6SChuck Lever found = dget_dlock(child); 4136faddda6SChuck Lever spin_unlock(&child->d_lock); 4146faddda6SChuck Lever out: 4156faddda6SChuck Lever rcu_read_unlock(); 4166faddda6SChuck Lever return found; 4176faddda6SChuck Lever } 4186faddda6SChuck Lever 4196faddda6SChuck Lever static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) 4206faddda6SChuck Lever { 4216faddda6SChuck Lever u32 offset = dentry2offset(dentry); 4226faddda6SChuck Lever struct inode *inode = d_inode(dentry); 4236faddda6SChuck Lever 4246faddda6SChuck Lever return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, 4256faddda6SChuck Lever inode->i_ino, fs_umode_to_dtype(inode->i_mode)); 4266faddda6SChuck Lever } 4276faddda6SChuck Lever 428*2be4f05aSChuck Lever static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) 4296faddda6SChuck Lever { 4306faddda6SChuck Lever struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); 4316faddda6SChuck Lever XA_STATE(xas, &so_ctx->xa, ctx->pos); 4326faddda6SChuck Lever struct dentry *dentry; 4336faddda6SChuck Lever 4346faddda6SChuck Lever while (true) { 4356faddda6SChuck Lever dentry = offset_find_next(&xas); 4366faddda6SChuck Lever if (!dentry) 4376faddda6SChuck Lever break; 4386faddda6SChuck Lever 4396faddda6SChuck Lever if (!offset_dir_emit(ctx, dentry)) { 4406faddda6SChuck Lever dput(dentry); 4416faddda6SChuck Lever break; 4426faddda6SChuck Lever } 4436faddda6SChuck Lever 4446faddda6SChuck Lever dput(dentry); 4456faddda6SChuck Lever ctx->pos = xas.xa_index + 1; 4466faddda6SChuck Lever } 4476faddda6SChuck Lever } 4486faddda6SChuck Lever 4496faddda6SChuck Lever /** 4506faddda6SChuck Lever * offset_readdir - Emit entries starting at offset @ctx->pos 4516faddda6SChuck Lever * @file: an open directory to iterate over 4526faddda6SChuck Lever * @ctx: directory iteration context 4536faddda6SChuck Lever * 4546faddda6SChuck Lever * Caller must hold @file's i_rwsem to prevent insertion or removal of 4556faddda6SChuck Lever * entries during this call. 4566faddda6SChuck Lever * 4576faddda6SChuck Lever * On entry, @ctx->pos contains an offset that represents the first entry 4586faddda6SChuck Lever * to be read from the directory. 4596faddda6SChuck Lever * 4606faddda6SChuck Lever * The operation continues until there are no more entries to read, or 4616faddda6SChuck Lever * until the ctx->actor indicates there is no more space in the caller's 4626faddda6SChuck Lever * output buffer. 4636faddda6SChuck Lever * 4646faddda6SChuck Lever * On return, @ctx->pos contains an offset that will read the next entry 465*2be4f05aSChuck Lever * in this directory when offset_readdir() is called again with @ctx. 4666faddda6SChuck Lever * 4676faddda6SChuck Lever * Return values: 4686faddda6SChuck Lever * %0 - Complete 4696faddda6SChuck Lever */ 4706faddda6SChuck Lever static int offset_readdir(struct file *file, struct dir_context *ctx) 4716faddda6SChuck Lever { 4726faddda6SChuck Lever struct dentry *dir = file->f_path.dentry; 4736faddda6SChuck Lever 4746faddda6SChuck Lever lockdep_assert_held(&d_inode(dir)->i_rwsem); 4756faddda6SChuck Lever 4766faddda6SChuck Lever if (!dir_emit_dots(file, ctx)) 4776faddda6SChuck Lever return 0; 4786faddda6SChuck Lever 479*2be4f05aSChuck Lever offset_iterate_dir(d_inode(dir), ctx); 4806faddda6SChuck Lever return 0; 4816faddda6SChuck Lever } 4826faddda6SChuck Lever 4836faddda6SChuck Lever const struct file_operations simple_offset_dir_operations = { 4846faddda6SChuck Lever .llseek = offset_dir_llseek, 4856faddda6SChuck Lever .iterate_shared = offset_readdir, 4866faddda6SChuck Lever .read = generic_read_dir, 4876faddda6SChuck Lever .fsync = noop_fsync, 4886faddda6SChuck Lever }; 4896faddda6SChuck Lever 490a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev) 491a3d1e7ebSAl Viro { 492a3d1e7ebSAl Viro struct dentry *child = NULL; 493a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs; 494a3d1e7ebSAl Viro 495a3d1e7ebSAl Viro spin_lock(&parent->d_lock); 496a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) { 497a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child); 498a3d1e7ebSAl Viro if (simple_positive(d)) { 499a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); 500a3d1e7ebSAl Viro if (simple_positive(d)) 501a3d1e7ebSAl Viro child = dget_dlock(d); 502a3d1e7ebSAl Viro spin_unlock(&d->d_lock); 503a3d1e7ebSAl Viro if (likely(child)) 504a3d1e7ebSAl Viro break; 505a3d1e7ebSAl Viro } 506a3d1e7ebSAl Viro } 507a3d1e7ebSAl Viro spin_unlock(&parent->d_lock); 508a3d1e7ebSAl Viro dput(prev); 509a3d1e7ebSAl Viro return child; 510a3d1e7ebSAl Viro } 511a3d1e7ebSAl Viro 512a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry, 513a3d1e7ebSAl Viro void (*callback)(struct dentry *)) 514a3d1e7ebSAl Viro { 515a3d1e7ebSAl Viro struct dentry *this = dget(dentry); 516a3d1e7ebSAl Viro while (true) { 517a3d1e7ebSAl Viro struct dentry *victim = NULL, *child; 518a3d1e7ebSAl Viro struct inode *inode = this->d_inode; 519a3d1e7ebSAl Viro 520a3d1e7ebSAl Viro inode_lock(inode); 521a3d1e7ebSAl Viro if (d_is_dir(this)) 522a3d1e7ebSAl Viro inode->i_flags |= S_DEAD; 523a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) { 524a3d1e7ebSAl Viro // kill and ascend 525a3d1e7ebSAl Viro // update metadata while it's still locked 526a3d1e7ebSAl Viro inode->i_ctime = current_time(inode); 527a3d1e7ebSAl Viro clear_nlink(inode); 528a3d1e7ebSAl Viro inode_unlock(inode); 529a3d1e7ebSAl Viro victim = this; 530a3d1e7ebSAl Viro this = this->d_parent; 531a3d1e7ebSAl Viro inode = this->d_inode; 532a3d1e7ebSAl Viro inode_lock(inode); 533a3d1e7ebSAl Viro if (simple_positive(victim)) { 534a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts 535a3d1e7ebSAl Viro if (d_is_dir(victim)) 536a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim); 537a3d1e7ebSAl Viro else 538a3d1e7ebSAl Viro fsnotify_unlink(inode, victim); 539a3d1e7ebSAl Viro if (callback) 540a3d1e7ebSAl Viro callback(victim); 541a3d1e7ebSAl Viro dput(victim); // unpin it 542a3d1e7ebSAl Viro } 543a3d1e7ebSAl Viro if (victim == dentry) { 544a3d1e7ebSAl Viro inode->i_ctime = inode->i_mtime = 545a3d1e7ebSAl Viro current_time(inode); 546a3d1e7ebSAl Viro if (d_is_dir(dentry)) 547a3d1e7ebSAl Viro drop_nlink(inode); 548a3d1e7ebSAl Viro inode_unlock(inode); 549a3d1e7ebSAl Viro dput(dentry); 550a3d1e7ebSAl Viro return; 551a3d1e7ebSAl Viro } 552a3d1e7ebSAl Viro } 553a3d1e7ebSAl Viro inode_unlock(inode); 554a3d1e7ebSAl Viro this = child; 555a3d1e7ebSAl Viro } 556a3d1e7ebSAl Viro } 557a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal); 558a3d1e7ebSAl Viro 559759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 560759b9775SHugh Dickins .statfs = simple_statfs, 561759b9775SHugh Dickins }; 562759b9775SHugh Dickins 563db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc) 5641da177e4SLinus Torvalds { 56531d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private; 5661da177e4SLinus Torvalds struct inode *root; 5671da177e4SLinus Torvalds 56889a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 5693971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 5703971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 5718d9e46d8SAl Viro s->s_magic = ctx->magic; 5728d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations; 5738d9e46d8SAl Viro s->s_xattr = ctx->xattr; 5741da177e4SLinus Torvalds s->s_time_gran = 1; 5751da177e4SLinus Torvalds root = new_inode(s); 5761da177e4SLinus Torvalds if (!root) 577db2c246aSDavid Howells return -ENOMEM; 578db2c246aSDavid Howells 5791a1c9bb4SJeff Layton /* 5801a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 5811a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 5821a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 5831a1c9bb4SJeff Layton */ 5841a1c9bb4SJeff Layton root->i_ino = 1; 5851da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 586078cd827SDeepa Dinamani root->i_atime = root->i_mtime = root->i_ctime = current_time(root); 5878d9e46d8SAl Viro s->s_root = d_make_root(root); 5888d9e46d8SAl Viro if (!s->s_root) 5898d9e46d8SAl Viro return -ENOMEM; 590db2c246aSDavid Howells s->s_d_op = ctx->dops; 591db2c246aSDavid Howells return 0; 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 594db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc) 595db2c246aSDavid Howells { 5962ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super); 5971da177e4SLinus Torvalds } 59831d6d5ceSDavid Howells 59931d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc) 60031d6d5ceSDavid Howells { 60131d6d5ceSDavid Howells kfree(fc->fs_private); 60231d6d5ceSDavid Howells } 60331d6d5ceSDavid Howells 60431d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = { 60531d6d5ceSDavid Howells .free = pseudo_fs_free, 60631d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree, 60731d6d5ceSDavid Howells }; 60831d6d5ceSDavid Howells 60931d6d5ceSDavid Howells /* 61031d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 61131d6d5ceSDavid Howells * will never be mountable) 61231d6d5ceSDavid Howells */ 61331d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc, 61431d6d5ceSDavid Howells unsigned long magic) 61531d6d5ceSDavid Howells { 61631d6d5ceSDavid Howells struct pseudo_fs_context *ctx; 61731d6d5ceSDavid Howells 61831d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL); 61931d6d5ceSDavid Howells if (likely(ctx)) { 62031d6d5ceSDavid Howells ctx->magic = magic; 62131d6d5ceSDavid Howells fc->fs_private = ctx; 62231d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops; 623db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER; 624db2c246aSDavid Howells fc->global = true; 62531d6d5ceSDavid Howells } 62631d6d5ceSDavid Howells return ctx; 62731d6d5ceSDavid Howells } 62831d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo); 6291da177e4SLinus Torvalds 63020955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 63120955e89SStephen Boyd { 63220955e89SStephen Boyd if (inode->i_private) 63320955e89SStephen Boyd file->private_data = inode->i_private; 63420955e89SStephen Boyd return 0; 63520955e89SStephen Boyd } 63612f38872SAl Viro EXPORT_SYMBOL(simple_open); 63720955e89SStephen Boyd 6381da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 6391da177e4SLinus Torvalds { 640dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 6411da177e4SLinus Torvalds 642078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 643d8c76e6fSDave Hansen inc_nlink(inode); 6447de9c6eeSAl Viro ihold(inode); 6451da177e4SLinus Torvalds dget(dentry); 6461da177e4SLinus Torvalds d_instantiate(dentry, inode); 6471da177e4SLinus Torvalds return 0; 6481da177e4SLinus Torvalds } 64912f38872SAl Viro EXPORT_SYMBOL(simple_link); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds struct dentry *child; 6541da177e4SLinus Torvalds int ret = 0; 6551da177e4SLinus Torvalds 6562fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 657946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) { 658da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 659da502956SNick Piggin if (simple_positive(child)) { 660da502956SNick Piggin spin_unlock(&child->d_lock); 6611da177e4SLinus Torvalds goto out; 662da502956SNick Piggin } 663da502956SNick Piggin spin_unlock(&child->d_lock); 664da502956SNick Piggin } 6651da177e4SLinus Torvalds ret = 1; 6661da177e4SLinus Torvalds out: 6672fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 6681da177e4SLinus Torvalds return ret; 6691da177e4SLinus Torvalds } 67012f38872SAl Viro EXPORT_SYMBOL(simple_empty); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 6731da177e4SLinus Torvalds { 674dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 6751da177e4SLinus Torvalds 676078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 6779a53c3a7SDave Hansen drop_nlink(inode); 6781da177e4SLinus Torvalds dput(dentry); 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds } 68112f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds if (!simple_empty(dentry)) 6861da177e4SLinus Torvalds return -ENOTEMPTY; 6871da177e4SLinus Torvalds 688dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 6891da177e4SLinus Torvalds simple_unlink(dir, dentry); 6909a53c3a7SDave Hansen drop_nlink(dir); 6911da177e4SLinus Torvalds return 0; 6921da177e4SLinus Torvalds } 69312f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 6941da177e4SLinus Torvalds 6956429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry, 6966429e463SLorenz Bauer struct inode *new_dir, struct dentry *new_dentry) 6976429e463SLorenz Bauer { 6986429e463SLorenz Bauer bool old_is_dir = d_is_dir(old_dentry); 6996429e463SLorenz Bauer bool new_is_dir = d_is_dir(new_dentry); 7006429e463SLorenz Bauer 7016429e463SLorenz Bauer if (old_dir != new_dir && old_is_dir != new_is_dir) { 7026429e463SLorenz Bauer if (old_is_dir) { 7036429e463SLorenz Bauer drop_nlink(old_dir); 7046429e463SLorenz Bauer inc_nlink(new_dir); 7056429e463SLorenz Bauer } else { 7066429e463SLorenz Bauer drop_nlink(new_dir); 7076429e463SLorenz Bauer inc_nlink(old_dir); 7086429e463SLorenz Bauer } 7096429e463SLorenz Bauer } 7106429e463SLorenz Bauer old_dir->i_ctime = old_dir->i_mtime = 7116429e463SLorenz Bauer new_dir->i_ctime = new_dir->i_mtime = 7126429e463SLorenz Bauer d_inode(old_dentry)->i_ctime = 7136429e463SLorenz Bauer d_inode(new_dentry)->i_ctime = current_time(old_dir); 7146429e463SLorenz Bauer 7156429e463SLorenz Bauer return 0; 7166429e463SLorenz Bauer } 7176429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange); 7186429e463SLorenz Bauer 719e18275aeSChristian Brauner int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir, 720549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 721549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 7221da177e4SLinus Torvalds { 723dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 724e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 7251da177e4SLinus Torvalds 7263871cb8cSLorenz Bauer if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 727e0e0be8aSMiklos Szeredi return -EINVAL; 728e0e0be8aSMiklos Szeredi 7293871cb8cSLorenz Bauer if (flags & RENAME_EXCHANGE) 7303871cb8cSLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 7313871cb8cSLorenz Bauer 7321da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 7331da177e4SLinus Torvalds return -ENOTEMPTY; 7341da177e4SLinus Torvalds 735dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 7361da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 737841590ceSAl Viro if (they_are_dirs) { 738dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 7399a53c3a7SDave Hansen drop_nlink(old_dir); 740841590ceSAl Viro } 7411da177e4SLinus Torvalds } else if (they_are_dirs) { 7429a53c3a7SDave Hansen drop_nlink(old_dir); 743d8c76e6fSDave Hansen inc_nlink(new_dir); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 747078cd827SDeepa Dinamani new_dir->i_mtime = inode->i_ctime = current_time(old_dir); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds return 0; 7501da177e4SLinus Torvalds } 75112f38872SAl Viro EXPORT_SYMBOL(simple_rename); 7521da177e4SLinus Torvalds 7537bb46a67Snpiggin@suse.de /** 754eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 755c1632a0fSChristian Brauner * @idmap: idmap of the target mount 7567bb46a67Snpiggin@suse.de * @dentry: dentry 7577bb46a67Snpiggin@suse.de * @iattr: iattr structure 7587bb46a67Snpiggin@suse.de * 7597bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 7607bb46a67Snpiggin@suse.de * 761eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 762eef2380cSChristoph Hellwig * implementation of size changes. 763eef2380cSChristoph Hellwig * 764eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 765eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 766eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 7677bb46a67Snpiggin@suse.de */ 768c1632a0fSChristian Brauner int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 769549c7297SChristian Brauner struct iattr *iattr) 7707bb46a67Snpiggin@suse.de { 771dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 7727bb46a67Snpiggin@suse.de int error; 7737bb46a67Snpiggin@suse.de 774c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, iattr); 7757bb46a67Snpiggin@suse.de if (error) 7767bb46a67Snpiggin@suse.de return error; 7777bb46a67Snpiggin@suse.de 7782c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 7792c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 780c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr); 781eef2380cSChristoph Hellwig mark_inode_dirty(inode); 782eef2380cSChristoph Hellwig return 0; 7837bb46a67Snpiggin@suse.de } 7847bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 7857bb46a67Snpiggin@suse.de 786a77f580aSMatthew Wilcox (Oracle) static int simple_read_folio(struct file *file, struct folio *folio) 7871da177e4SLinus Torvalds { 788a77f580aSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 789a77f580aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 790a77f580aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 791a77f580aSMatthew Wilcox (Oracle) folio_unlock(folio); 7921da177e4SLinus Torvalds return 0; 7931da177e4SLinus Torvalds } 7941da177e4SLinus Torvalds 795afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 7969d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 797afddba49SNick Piggin struct page **pagep, void **fsdata) 798afddba49SNick Piggin { 799afddba49SNick Piggin struct page *page; 800afddba49SNick Piggin pgoff_t index; 801afddba49SNick Piggin 80209cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 803afddba49SNick Piggin 804b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 805afddba49SNick Piggin if (!page) 806afddba49SNick Piggin return -ENOMEM; 807afddba49SNick Piggin 808afddba49SNick Piggin *pagep = page; 809afddba49SNick Piggin 81009cbfeafSKirill A. Shutemov if (!PageUptodate(page) && (len != PAGE_SIZE)) { 81109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 812193cf4b9SBoaz Harrosh 81309cbfeafSKirill A. Shutemov zero_user_segments(page, 0, from, from + len, PAGE_SIZE); 814193cf4b9SBoaz Harrosh } 815193cf4b9SBoaz Harrosh return 0; 816afddba49SNick Piggin } 81712f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 818afddba49SNick Piggin 819ad2a722fSBoaz Harrosh /** 820ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 8218e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations 822ad2a722fSBoaz Harrosh * @mapping: " 823ad2a722fSBoaz Harrosh * @pos: " 824ad2a722fSBoaz Harrosh * @len: " 825ad2a722fSBoaz Harrosh * @copied: " 826ad2a722fSBoaz Harrosh * @page: " 827ad2a722fSBoaz Harrosh * @fsdata: " 828ad2a722fSBoaz Harrosh * 829ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 830ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 831ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 832ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 833ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 834ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 835ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 836ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 837ad2a722fSBoaz Harrosh * case that i_size has changed. 83804fff641SAl Viro * 839a77f580aSMatthew Wilcox (Oracle) * Use *ONLY* with simple_read_folio() 840ad2a722fSBoaz Harrosh */ 841c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping, 842ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 843ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 846ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 847ad2a722fSBoaz Harrosh 848ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 84904fff641SAl Viro if (!PageUptodate(page)) { 850ad2a722fSBoaz Harrosh if (copied < len) { 85109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 852ad2a722fSBoaz Harrosh 853ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 854ad2a722fSBoaz Harrosh } 855955eff5aSNick Piggin SetPageUptodate(page); 85604fff641SAl Viro } 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 8591b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 8601da177e4SLinus Torvalds */ 861ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 862ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 863ad2a722fSBoaz Harrosh 8641da177e4SLinus Torvalds set_page_dirty(page); 865afddba49SNick Piggin unlock_page(page); 86609cbfeafSKirill A. Shutemov put_page(page); 867afddba49SNick Piggin 868afddba49SNick Piggin return copied; 869afddba49SNick Piggin } 870c1e3dbe9SChristoph Hellwig 871c1e3dbe9SChristoph Hellwig /* 872c1e3dbe9SChristoph Hellwig * Provides ramfs-style behavior: data in the pagecache, but no writeback. 873c1e3dbe9SChristoph Hellwig */ 874c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = { 875a77f580aSMatthew Wilcox (Oracle) .read_folio = simple_read_folio, 876c1e3dbe9SChristoph Hellwig .write_begin = simple_write_begin, 877c1e3dbe9SChristoph Hellwig .write_end = simple_write_end, 87846de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 879c1e3dbe9SChristoph Hellwig }; 880c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops); 881afddba49SNick Piggin 8821a1c9bb4SJeff Layton /* 8831a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 8841a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 8851a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 8861a1c9bb4SJeff Layton */ 8877d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 888cda37124SEric Biggers const struct tree_descr *files) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds struct inode *inode; 8911da177e4SLinus Torvalds struct dentry *root; 8921da177e4SLinus Torvalds struct dentry *dentry; 8931da177e4SLinus Torvalds int i; 8941da177e4SLinus Torvalds 89509cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE; 89609cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT; 8971da177e4SLinus Torvalds s->s_magic = magic; 898759b9775SHugh Dickins s->s_op = &simple_super_operations; 8991da177e4SLinus Torvalds s->s_time_gran = 1; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds inode = new_inode(s); 9021da177e4SLinus Torvalds if (!inode) 9031da177e4SLinus Torvalds return -ENOMEM; 9041a1c9bb4SJeff Layton /* 9051a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 9061a1c9bb4SJeff Layton * entry at index 1 9071a1c9bb4SJeff Layton */ 9081a1c9bb4SJeff Layton inode->i_ino = 1; 9091da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 910078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9111da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 9121da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 913bfe86848SMiklos Szeredi set_nlink(inode, 2); 91448fde701SAl Viro root = d_make_root(inode); 91548fde701SAl Viro if (!root) 9161da177e4SLinus Torvalds return -ENOMEM; 9171da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 9181da177e4SLinus Torvalds if (!files->name) 9191da177e4SLinus Torvalds continue; 9201a1c9bb4SJeff Layton 9211a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 9221a1c9bb4SJeff Layton if (unlikely(i == 1)) 9231a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 9241a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 9251a1c9bb4SJeff Layton s->s_type->name); 9261a1c9bb4SJeff Layton 9271da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 9281da177e4SLinus Torvalds if (!dentry) 9291da177e4SLinus Torvalds goto out; 9301da177e4SLinus Torvalds inode = new_inode(s); 93132096ea1SKonstantin Khlebnikov if (!inode) { 93232096ea1SKonstantin Khlebnikov dput(dentry); 9331da177e4SLinus Torvalds goto out; 93432096ea1SKonstantin Khlebnikov } 9351da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 936078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9371da177e4SLinus Torvalds inode->i_fop = files->ops; 9381da177e4SLinus Torvalds inode->i_ino = i; 9391da177e4SLinus Torvalds d_add(dentry, inode); 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds s->s_root = root; 9421da177e4SLinus Torvalds return 0; 9431da177e4SLinus Torvalds out: 9441da177e4SLinus Torvalds d_genocide(root); 945640946f2SAl Viro shrink_dcache_parent(root); 9461da177e4SLinus Torvalds dput(root); 9471da177e4SLinus Torvalds return -ENOMEM; 9481da177e4SLinus Torvalds } 94912f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 9521da177e4SLinus Torvalds 9531f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 9541da177e4SLinus Torvalds { 9551da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 9561da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9571da177e4SLinus Torvalds if (unlikely(!*mount)) { 9581da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9591751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 9601da177e4SLinus Torvalds if (IS_ERR(mnt)) 9611da177e4SLinus Torvalds return PTR_ERR(mnt); 9621da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9631da177e4SLinus Torvalds if (!*mount) 9641da177e4SLinus Torvalds *mount = mnt; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds mntget(*mount); 9671da177e4SLinus Torvalds ++*count; 9681da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9691da177e4SLinus Torvalds mntput(mnt); 9701da177e4SLinus Torvalds return 0; 9711da177e4SLinus Torvalds } 97212f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 9751da177e4SLinus Torvalds { 9761da177e4SLinus Torvalds struct vfsmount *mnt; 9771da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9781da177e4SLinus Torvalds mnt = *mount; 9791da177e4SLinus Torvalds if (!--*count) 9801da177e4SLinus Torvalds *mount = NULL; 9811da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9821da177e4SLinus Torvalds mntput(mnt); 9831da177e4SLinus Torvalds } 98412f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 9851da177e4SLinus Torvalds 9866d1029b5SAkinobu Mita /** 9876d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 9886d1029b5SAkinobu Mita * @to: the user space buffer to read to 9896d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 9906d1029b5SAkinobu Mita * @ppos: the current position in the buffer 9916d1029b5SAkinobu Mita * @from: the buffer to read from 9926d1029b5SAkinobu Mita * @available: the size of the buffer 9936d1029b5SAkinobu Mita * 9946d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 9956d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 9966d1029b5SAkinobu Mita * 9976d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 9986d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 9996d1029b5SAkinobu Mita **/ 10001da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 10011da177e4SLinus Torvalds const void *from, size_t available) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds loff_t pos = *ppos; 100414be2746SSteven Rostedt size_t ret; 100514be2746SSteven Rostedt 10061da177e4SLinus Torvalds if (pos < 0) 10071da177e4SLinus Torvalds return -EINVAL; 100814be2746SSteven Rostedt if (pos >= available || !count) 10091da177e4SLinus Torvalds return 0; 10101da177e4SLinus Torvalds if (count > available - pos) 10111da177e4SLinus Torvalds count = available - pos; 101214be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 101314be2746SSteven Rostedt if (ret == count) 10141da177e4SLinus Torvalds return -EFAULT; 101514be2746SSteven Rostedt count -= ret; 10161da177e4SLinus Torvalds *ppos = pos + count; 10171da177e4SLinus Torvalds return count; 10181da177e4SLinus Torvalds } 101912f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 10201da177e4SLinus Torvalds 10216d1029b5SAkinobu Mita /** 10226a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 10236a727b43SJiri Slaby * @to: the buffer to write to 10246a727b43SJiri Slaby * @available: the size of the buffer 10256a727b43SJiri Slaby * @ppos: the current position in the buffer 10266a727b43SJiri Slaby * @from: the user space buffer to read from 10276a727b43SJiri Slaby * @count: the maximum number of bytes to read 10286a727b43SJiri Slaby * 10296a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 10306a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 10316a727b43SJiri Slaby * 10326a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 10336a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 10346a727b43SJiri Slaby **/ 10356a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 10366a727b43SJiri Slaby const void __user *from, size_t count) 10376a727b43SJiri Slaby { 10386a727b43SJiri Slaby loff_t pos = *ppos; 10396a727b43SJiri Slaby size_t res; 10406a727b43SJiri Slaby 10416a727b43SJiri Slaby if (pos < 0) 10426a727b43SJiri Slaby return -EINVAL; 10436a727b43SJiri Slaby if (pos >= available || !count) 10446a727b43SJiri Slaby return 0; 10456a727b43SJiri Slaby if (count > available - pos) 10466a727b43SJiri Slaby count = available - pos; 10476a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 10486a727b43SJiri Slaby if (res == count) 10496a727b43SJiri Slaby return -EFAULT; 10506a727b43SJiri Slaby count -= res; 10516a727b43SJiri Slaby *ppos = pos + count; 10526a727b43SJiri Slaby return count; 10536a727b43SJiri Slaby } 105412f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 10556a727b43SJiri Slaby 10566a727b43SJiri Slaby /** 10576d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 10586d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 10596d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 10606d1029b5SAkinobu Mita * @ppos: the current position in the buffer 10616d1029b5SAkinobu Mita * @from: the buffer to read from 10626d1029b5SAkinobu Mita * @available: the size of the buffer 10636d1029b5SAkinobu Mita * 10646d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 10656d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 10666d1029b5SAkinobu Mita * 10676d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 10686d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 10696d1029b5SAkinobu Mita **/ 107093b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 107193b07113SAkinobu Mita const void *from, size_t available) 107293b07113SAkinobu Mita { 107393b07113SAkinobu Mita loff_t pos = *ppos; 107493b07113SAkinobu Mita 107593b07113SAkinobu Mita if (pos < 0) 107693b07113SAkinobu Mita return -EINVAL; 107793b07113SAkinobu Mita if (pos >= available) 107893b07113SAkinobu Mita return 0; 107993b07113SAkinobu Mita if (count > available - pos) 108093b07113SAkinobu Mita count = available - pos; 108193b07113SAkinobu Mita memcpy(to, from + pos, count); 108293b07113SAkinobu Mita *ppos = pos + count; 108393b07113SAkinobu Mita 108493b07113SAkinobu Mita return count; 108593b07113SAkinobu Mita } 108612f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 108793b07113SAkinobu Mita 10881da177e4SLinus Torvalds /* 10891da177e4SLinus Torvalds * Transaction based IO. 10901da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 10911da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 10921da177e4SLinus Torvalds * file-local buffer. 10931da177e4SLinus Torvalds */ 109476791ab2SIngo Molnar 109576791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 109676791ab2SIngo Molnar { 109776791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 109876791ab2SIngo Molnar 109976791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 110076791ab2SIngo Molnar 110176791ab2SIngo Molnar /* 110276791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 110376791ab2SIngo Molnar * ar->data is ready for reading. 110476791ab2SIngo Molnar */ 110576791ab2SIngo Molnar smp_mb(); 110676791ab2SIngo Molnar ar->size = n; 110776791ab2SIngo Molnar } 110812f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 110976791ab2SIngo Molnar 11101da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 11111da177e4SLinus Torvalds { 11121da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 11131da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 11161da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 11191da177e4SLinus Torvalds if (!ar) 11201da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /* only one write allowed per open */ 11251da177e4SLinus Torvalds if (file->private_data) { 11261da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 11271da177e4SLinus Torvalds free_page((unsigned long)ar); 11281da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds file->private_data = ar; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 11361da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds return ar->data; 11391da177e4SLinus Torvalds } 114012f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (!ar) 11471da177e4SLinus Torvalds return 0; 11481da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 11491da177e4SLinus Torvalds } 115012f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 11531da177e4SLinus Torvalds { 11541da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 11551da177e4SLinus Torvalds return 0; 11561da177e4SLinus Torvalds } 115712f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 11581da177e4SLinus Torvalds 1159acaefc25SArnd Bergmann /* Simple attribute files */ 1160acaefc25SArnd Bergmann 1161acaefc25SArnd Bergmann struct simple_attr { 11628b88b099SChristoph Hellwig int (*get)(void *, u64 *); 11638b88b099SChristoph Hellwig int (*set)(void *, u64); 1164acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 1165acaefc25SArnd Bergmann char set_buf[24]; 1166acaefc25SArnd Bergmann void *data; 1167acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 11687cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 1169acaefc25SArnd Bergmann }; 1170acaefc25SArnd Bergmann 1171acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 1172acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 1173acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 11748b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 1175acaefc25SArnd Bergmann const char *fmt) 1176acaefc25SArnd Bergmann { 1177acaefc25SArnd Bergmann struct simple_attr *attr; 1178acaefc25SArnd Bergmann 1179a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1180acaefc25SArnd Bergmann if (!attr) 1181acaefc25SArnd Bergmann return -ENOMEM; 1182acaefc25SArnd Bergmann 1183acaefc25SArnd Bergmann attr->get = get; 1184acaefc25SArnd Bergmann attr->set = set; 11858e18e294STheodore Ts'o attr->data = inode->i_private; 1186acaefc25SArnd Bergmann attr->fmt = fmt; 11877cf34c76SIngo Molnar mutex_init(&attr->mutex); 1188acaefc25SArnd Bergmann 1189acaefc25SArnd Bergmann file->private_data = attr; 1190acaefc25SArnd Bergmann 1191acaefc25SArnd Bergmann return nonseekable_open(inode, file); 1192acaefc25SArnd Bergmann } 119312f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 1194acaefc25SArnd Bergmann 119574bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 1196acaefc25SArnd Bergmann { 1197acaefc25SArnd Bergmann kfree(file->private_data); 1198acaefc25SArnd Bergmann return 0; 1199acaefc25SArnd Bergmann } 120012f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 1201acaefc25SArnd Bergmann 1202acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 1203acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 1204acaefc25SArnd Bergmann size_t len, loff_t *ppos) 1205acaefc25SArnd Bergmann { 1206acaefc25SArnd Bergmann struct simple_attr *attr; 1207acaefc25SArnd Bergmann size_t size; 1208acaefc25SArnd Bergmann ssize_t ret; 1209acaefc25SArnd Bergmann 1210acaefc25SArnd Bergmann attr = file->private_data; 1211acaefc25SArnd Bergmann 1212acaefc25SArnd Bergmann if (!attr->get) 1213acaefc25SArnd Bergmann return -EACCES; 1214acaefc25SArnd Bergmann 12159261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 12169261303aSChristoph Hellwig if (ret) 12179261303aSChristoph Hellwig return ret; 12189261303aSChristoph Hellwig 1219a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) { 1220a65cab7dSEric Biggers /* continued read */ 1221acaefc25SArnd Bergmann size = strlen(attr->get_buf); 1222a65cab7dSEric Biggers } else { 1223a65cab7dSEric Biggers /* first read */ 12248b88b099SChristoph Hellwig u64 val; 12258b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 12268b88b099SChristoph Hellwig if (ret) 12278b88b099SChristoph Hellwig goto out; 12288b88b099SChristoph Hellwig 1229acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 12308b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 12318b88b099SChristoph Hellwig } 1232acaefc25SArnd Bergmann 1233acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 12348b88b099SChristoph Hellwig out: 12357cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1236acaefc25SArnd Bergmann return ret; 1237acaefc25SArnd Bergmann } 123812f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 1239acaefc25SArnd Bergmann 1240acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 12412e41f274SAkinobu Mita static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf, 12422e41f274SAkinobu Mita size_t len, loff_t *ppos, bool is_signed) 1243acaefc25SArnd Bergmann { 1244acaefc25SArnd Bergmann struct simple_attr *attr; 1245488dac0cSYicong Yang unsigned long long val; 1246acaefc25SArnd Bergmann size_t size; 1247acaefc25SArnd Bergmann ssize_t ret; 1248acaefc25SArnd Bergmann 1249acaefc25SArnd Bergmann attr = file->private_data; 1250acaefc25SArnd Bergmann if (!attr->set) 1251acaefc25SArnd Bergmann return -EACCES; 1252acaefc25SArnd Bergmann 12539261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 12549261303aSChristoph Hellwig if (ret) 12559261303aSChristoph Hellwig return ret; 12569261303aSChristoph Hellwig 1257acaefc25SArnd Bergmann ret = -EFAULT; 1258acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 1259acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 1260acaefc25SArnd Bergmann goto out; 1261acaefc25SArnd Bergmann 1262acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 12632e41f274SAkinobu Mita if (is_signed) 12642e41f274SAkinobu Mita ret = kstrtoll(attr->set_buf, 0, &val); 12652e41f274SAkinobu Mita else 1266488dac0cSYicong Yang ret = kstrtoull(attr->set_buf, 0, &val); 1267488dac0cSYicong Yang if (ret) 1268488dac0cSYicong Yang goto out; 126905cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 127005cc0ceeSWu Fengguang if (ret == 0) 127105cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 1272acaefc25SArnd Bergmann out: 12737cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1274acaefc25SArnd Bergmann return ret; 1275acaefc25SArnd Bergmann } 12762e41f274SAkinobu Mita 12772e41f274SAkinobu Mita ssize_t simple_attr_write(struct file *file, const char __user *buf, 12782e41f274SAkinobu Mita size_t len, loff_t *ppos) 12792e41f274SAkinobu Mita { 12802e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, false); 12812e41f274SAkinobu Mita } 128212f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 1283acaefc25SArnd Bergmann 12842e41f274SAkinobu Mita ssize_t simple_attr_write_signed(struct file *file, const char __user *buf, 12852e41f274SAkinobu Mita size_t len, loff_t *ppos) 12862e41f274SAkinobu Mita { 12872e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, true); 12882e41f274SAkinobu Mita } 12892e41f274SAkinobu Mita EXPORT_SYMBOL_GPL(simple_attr_write_signed); 12902e41f274SAkinobu Mita 12912596110aSChristoph Hellwig /** 12922596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 12932596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 12942596110aSChristoph Hellwig * @fid: file handle to convert 12952596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 12962596110aSChristoph Hellwig * @fh_type: type of file handle 12972596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 12982596110aSChristoph Hellwig * 12992596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 13002596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 13012596110aSChristoph Hellwig * inode for the object specified in the file handle. 13022596110aSChristoph Hellwig */ 13032596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 13042596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 13052596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 13062596110aSChristoph Hellwig { 13072596110aSChristoph Hellwig struct inode *inode = NULL; 13082596110aSChristoph Hellwig 13092596110aSChristoph Hellwig if (fh_len < 2) 13102596110aSChristoph Hellwig return NULL; 13112596110aSChristoph Hellwig 13122596110aSChristoph Hellwig switch (fh_type) { 13132596110aSChristoph Hellwig case FILEID_INO32_GEN: 13142596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 13152596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 13162596110aSChristoph Hellwig break; 13172596110aSChristoph Hellwig } 13182596110aSChristoph Hellwig 13194ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 13202596110aSChristoph Hellwig } 13212596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 13222596110aSChristoph Hellwig 13232596110aSChristoph Hellwig /** 1324ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 13252596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 13262596110aSChristoph Hellwig * @fid: file handle to convert 13272596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 13282596110aSChristoph Hellwig * @fh_type: type of file handle 13292596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 13302596110aSChristoph Hellwig * 13312596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 13322596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 13332596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 13342596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 13352596110aSChristoph Hellwig */ 13362596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 13372596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 13382596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 13392596110aSChristoph Hellwig { 13402596110aSChristoph Hellwig struct inode *inode = NULL; 13412596110aSChristoph Hellwig 13422596110aSChristoph Hellwig if (fh_len <= 2) 13432596110aSChristoph Hellwig return NULL; 13442596110aSChristoph Hellwig 13452596110aSChristoph Hellwig switch (fh_type) { 13462596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 13472596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 13482596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 13492596110aSChristoph Hellwig break; 13502596110aSChristoph Hellwig } 13512596110aSChristoph Hellwig 13524ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 13532596110aSChristoph Hellwig } 13542596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 13552596110aSChristoph Hellwig 13561b061d92SChristoph Hellwig /** 1357ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 1358ac13a829SFabian Frederick * 13591b061d92SChristoph Hellwig * @file: file to synchronize 1360ac13a829SFabian Frederick * @start: start offset in bytes 1361ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 13621b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 13631b061d92SChristoph Hellwig * 13641b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 13651b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 13661b061d92SChristoph Hellwig * hanging off the address_space structure. 13671b061d92SChristoph Hellwig */ 1368ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 136902c24a82SJosef Bacik int datasync) 1370d5aacad5SAl Viro { 13717ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 1372d5aacad5SAl Viro int err; 1373d5aacad5SAl Viro int ret; 1374d5aacad5SAl Viro 1375383aa543SJeff Layton err = file_write_and_wait_range(file, start, end); 137602c24a82SJosef Bacik if (err) 137702c24a82SJosef Bacik return err; 137802c24a82SJosef Bacik 13795955102cSAl Viro inode_lock(inode); 1380d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 13810ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 138202c24a82SJosef Bacik goto out; 1383d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 138402c24a82SJosef Bacik goto out; 1385d5aacad5SAl Viro 1386c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 1387d5aacad5SAl Viro if (ret == 0) 1388d5aacad5SAl Viro ret = err; 1389ac13a829SFabian Frederick 139002c24a82SJosef Bacik out: 13915955102cSAl Viro inode_unlock(inode); 1392383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */ 1393383aa543SJeff Layton err = file_check_and_advance_wb_err(file); 1394383aa543SJeff Layton if (ret == 0) 1395383aa543SJeff Layton ret = err; 1396383aa543SJeff Layton return ret; 1397d5aacad5SAl Viro } 1398ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 1399ac13a829SFabian Frederick 1400ac13a829SFabian Frederick /** 1401ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 1402ac13a829SFabian Frederick * with flush 1403ac13a829SFabian Frederick * @file: file to synchronize 1404ac13a829SFabian Frederick * @start: start offset in bytes 1405ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 1406ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 1407ac13a829SFabian Frederick * 1408ac13a829SFabian Frederick */ 1409ac13a829SFabian Frederick 1410ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 1411ac13a829SFabian Frederick int datasync) 1412ac13a829SFabian Frederick { 1413ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 1414ac13a829SFabian Frederick int err; 1415ac13a829SFabian Frederick 1416ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 1417ac13a829SFabian Frederick if (err) 1418ac13a829SFabian Frederick return err; 1419c6bf3f0eSChristoph Hellwig return blkdev_issue_flush(inode->i_sb->s_bdev); 1420ac13a829SFabian Frederick } 14211b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 14221b061d92SChristoph Hellwig 142330ca22c7SPatrick J. LoPresti /** 142430ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 142530ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 142630ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 142730ca22c7SPatrick J. LoPresti * 142830ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 142930ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 143030ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 143130ca22c7SPatrick J. LoPresti */ 143230ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 143330ca22c7SPatrick J. LoPresti { 143430ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 1435a33f13efSJoel Becker u64 last_fs_page = 143609cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits); 143730ca22c7SPatrick J. LoPresti 143830ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 143930ca22c7SPatrick J. LoPresti return 0; 144030ca22c7SPatrick J. LoPresti 144109cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT)) 144230ca22c7SPatrick J. LoPresti return -EINVAL; 144330ca22c7SPatrick J. LoPresti 1444a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1445a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 144630ca22c7SPatrick J. LoPresti return -EFBIG; 144730ca22c7SPatrick J. LoPresti } 144830ca22c7SPatrick J. LoPresti return 0; 144930ca22c7SPatrick J. LoPresti } 145030ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 145130ca22c7SPatrick J. LoPresti 14521b061d92SChristoph Hellwig /* 14531b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 14541b061d92SChristoph Hellwig */ 145502c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 14561b061d92SChristoph Hellwig { 14571b061d92SChristoph Hellwig return 0; 14581b061d92SChristoph Hellwig } 14591b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 146087dc800bSAl Viro 1461f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 1462f44c7763SDan Williams { 1463f44c7763SDan Williams /* 1464f44c7763SDan Williams * iomap based filesystems support direct I/O without need for 1465f44c7763SDan Williams * this callback. However, it still needs to be set in 1466f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is 1467f44c7763SDan Williams * generally supported. 1468f44c7763SDan Williams */ 1469f44c7763SDan Williams return -EINVAL; 1470f44c7763SDan Williams } 1471f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO); 1472f44c7763SDan Williams 1473fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */ 1474fceef393SAl Viro void kfree_link(void *p) 147587dc800bSAl Viro { 1476fceef393SAl Viro kfree(p); 147787dc800bSAl Viro } 1478fceef393SAl Viro EXPORT_SYMBOL(kfree_link); 14796987843fSAl Viro 14806987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 14816987843fSAl Viro { 14826987843fSAl Viro static const struct address_space_operations anon_aops = { 148346de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 14846987843fSAl Viro }; 14856987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 14866987843fSAl Viro 14876987843fSAl Viro if (!inode) 14886987843fSAl Viro return ERR_PTR(-ENOMEM); 14896987843fSAl Viro 14906987843fSAl Viro inode->i_ino = get_next_ino(); 14916987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 14926987843fSAl Viro 14936987843fSAl Viro /* 14946987843fSAl Viro * Mark the inode dirty from the very beginning, 14956987843fSAl Viro * that way it will never be moved to the dirty 14966987843fSAl Viro * list because mark_inode_dirty() will think 14976987843fSAl Viro * that it already _is_ on the dirty list. 14986987843fSAl Viro */ 14996987843fSAl Viro inode->i_state = I_DIRTY; 15006987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 15016987843fSAl Viro inode->i_uid = current_fsuid(); 15026987843fSAl Viro inode->i_gid = current_fsgid(); 15036987843fSAl Viro inode->i_flags |= S_PRIVATE; 1504078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 15056987843fSAl Viro return inode; 15066987843fSAl Viro } 15076987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 15081c994a09SJeff Layton 15091c994a09SJeff Layton /** 15101c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 15111c994a09SJeff Layton * @filp: file pointer 15121c994a09SJeff Layton * @arg: type of lease to obtain 15131c994a09SJeff Layton * @flp: new lease supplied for insertion 1514e6f5c789SJeff Layton * @priv: private data for lm_setup operation 15151c994a09SJeff Layton * 15161c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 15171c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 15181c994a09SJeff Layton */ 15191c994a09SJeff Layton int 1520e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1521e6f5c789SJeff Layton void **priv) 15221c994a09SJeff Layton { 15231c994a09SJeff Layton return -EINVAL; 15241c994a09SJeff Layton } 15251c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 152661ba64fcSAl Viro 15276ee9706aSEric Biggers /** 15286ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks 15296ee9706aSEric Biggers * @dentry: not used here 15306ee9706aSEric Biggers * @inode: the symlink inode 15316ee9706aSEric Biggers * @done: not used here 15326ee9706aSEric Biggers * 15336ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to 15346ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called, 15356ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link 15366ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set, 15376ee9706aSEric Biggers * to mark the inode_operations as being for a symlink. 15386ee9706aSEric Biggers * 15396ee9706aSEric Biggers * Return: the symlink target 15406ee9706aSEric Biggers */ 15416b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 1542fceef393SAl Viro struct delayed_call *done) 154361ba64fcSAl Viro { 15446b255391SAl Viro return inode->i_link; 154561ba64fcSAl Viro } 15466b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 154761ba64fcSAl Viro 154861ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 15496b255391SAl Viro .get_link = simple_get_link, 155061ba64fcSAl Viro }; 155161ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1552fbabfd0fSEric W. Biederman 1553fbabfd0fSEric W. Biederman /* 1554fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1555fbabfd0fSEric W. Biederman */ 1556fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1557fbabfd0fSEric W. Biederman { 1558fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1559fbabfd0fSEric W. Biederman } 1560fbabfd0fSEric W. Biederman 1561b74d24f7SChristian Brauner static int empty_dir_getattr(struct mnt_idmap *idmap, 1562549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1563a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1564fbabfd0fSEric W. Biederman { 1565a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 1566b74d24f7SChristian Brauner generic_fillattr(&nop_mnt_idmap, inode, stat); 1567fbabfd0fSEric W. Biederman return 0; 1568fbabfd0fSEric W. Biederman } 1569fbabfd0fSEric W. Biederman 1570c1632a0fSChristian Brauner static int empty_dir_setattr(struct mnt_idmap *idmap, 1571549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 1572fbabfd0fSEric W. Biederman { 1573fbabfd0fSEric W. Biederman return -EPERM; 1574fbabfd0fSEric W. Biederman } 1575fbabfd0fSEric W. Biederman 1576fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1577fbabfd0fSEric W. Biederman { 1578fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1579fbabfd0fSEric W. Biederman } 1580fbabfd0fSEric W. Biederman 1581fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1582fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1583fbabfd0fSEric W. Biederman .permission = generic_permission, 1584fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1585fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1586fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1587fbabfd0fSEric W. Biederman }; 1588fbabfd0fSEric W. Biederman 1589fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1590fbabfd0fSEric W. Biederman { 1591fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1592fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1593fbabfd0fSEric W. Biederman } 1594fbabfd0fSEric W. Biederman 1595fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1596fbabfd0fSEric W. Biederman { 1597fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1598fbabfd0fSEric W. Biederman return 0; 1599fbabfd0fSEric W. Biederman } 1600fbabfd0fSEric W. Biederman 1601fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1602fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1603fbabfd0fSEric W. Biederman .read = generic_read_dir, 1604c51da20cSAl Viro .iterate_shared = empty_dir_readdir, 1605fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1606fbabfd0fSEric W. Biederman }; 1607fbabfd0fSEric W. Biederman 1608fbabfd0fSEric W. Biederman 1609fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1610fbabfd0fSEric W. Biederman { 1611fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1612fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1613fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1614fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1615fbabfd0fSEric W. Biederman inode->i_rdev = 0; 16164b75de86SEric W. Biederman inode->i_size = 0; 1617fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1618fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1619fbabfd0fSEric W. Biederman 1620fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1621f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR; 1622fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1623fbabfd0fSEric W. Biederman } 1624fbabfd0fSEric W. Biederman 1625fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1626fbabfd0fSEric W. Biederman { 1627fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1628fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1629fbabfd0fSEric W. Biederman } 1630c843843eSDaniel Rosenberg 16315298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1632c843843eSDaniel Rosenberg /* 1633c843843eSDaniel Rosenberg * Determine if the name of a dentry should be casefolded. 1634c843843eSDaniel Rosenberg * 1635c843843eSDaniel Rosenberg * Return: if names will need casefolding 1636c843843eSDaniel Rosenberg */ 1637c843843eSDaniel Rosenberg static bool needs_casefold(const struct inode *dir) 1638c843843eSDaniel Rosenberg { 1639c843843eSDaniel Rosenberg return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding; 1640c843843eSDaniel Rosenberg } 1641c843843eSDaniel Rosenberg 1642c843843eSDaniel Rosenberg /** 1643c843843eSDaniel Rosenberg * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems 1644c843843eSDaniel Rosenberg * @dentry: dentry whose name we are checking against 1645c843843eSDaniel Rosenberg * @len: len of name of dentry 1646c843843eSDaniel Rosenberg * @str: str pointer to name of dentry 1647c843843eSDaniel Rosenberg * @name: Name to compare against 1648c843843eSDaniel Rosenberg * 1649c843843eSDaniel Rosenberg * Return: 0 if names match, 1 if mismatch, or -ERRNO 1650c843843eSDaniel Rosenberg */ 1651794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len, 1652c843843eSDaniel Rosenberg const char *str, const struct qstr *name) 1653c843843eSDaniel Rosenberg { 1654c843843eSDaniel Rosenberg const struct dentry *parent = READ_ONCE(dentry->d_parent); 1655c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(parent->d_inode); 1656c843843eSDaniel Rosenberg const struct super_block *sb = dentry->d_sb; 1657c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1658c843843eSDaniel Rosenberg struct qstr qstr = QSTR_INIT(str, len); 1659c843843eSDaniel Rosenberg char strbuf[DNAME_INLINE_LEN]; 1660c843843eSDaniel Rosenberg int ret; 1661c843843eSDaniel Rosenberg 1662c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1663c843843eSDaniel Rosenberg goto fallback; 1664c843843eSDaniel Rosenberg /* 1665c843843eSDaniel Rosenberg * If the dentry name is stored in-line, then it may be concurrently 1666c843843eSDaniel Rosenberg * modified by a rename. If this happens, the VFS will eventually retry 1667c843843eSDaniel Rosenberg * the lookup, so it doesn't matter what ->d_compare() returns. 1668c843843eSDaniel Rosenberg * However, it's unsafe to call utf8_strncasecmp() with an unstable 1669c843843eSDaniel Rosenberg * string. Therefore, we have to copy the name into a temporary buffer. 1670c843843eSDaniel Rosenberg */ 1671c843843eSDaniel Rosenberg if (len <= DNAME_INLINE_LEN - 1) { 1672c843843eSDaniel Rosenberg memcpy(strbuf, str, len); 1673c843843eSDaniel Rosenberg strbuf[len] = 0; 1674c843843eSDaniel Rosenberg qstr.name = strbuf; 1675c843843eSDaniel Rosenberg /* prevent compiler from optimizing out the temporary buffer */ 1676c843843eSDaniel Rosenberg barrier(); 1677c843843eSDaniel Rosenberg } 1678c843843eSDaniel Rosenberg ret = utf8_strncasecmp(um, name, &qstr); 1679c843843eSDaniel Rosenberg if (ret >= 0) 1680c843843eSDaniel Rosenberg return ret; 1681c843843eSDaniel Rosenberg 1682c843843eSDaniel Rosenberg if (sb_has_strict_encoding(sb)) 1683c843843eSDaniel Rosenberg return -EINVAL; 1684c843843eSDaniel Rosenberg fallback: 1685c843843eSDaniel Rosenberg if (len != name->len) 1686c843843eSDaniel Rosenberg return 1; 1687c843843eSDaniel Rosenberg return !!memcmp(str, name->name, len); 1688c843843eSDaniel Rosenberg } 1689c843843eSDaniel Rosenberg 1690c843843eSDaniel Rosenberg /** 1691c843843eSDaniel Rosenberg * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems 1692c843843eSDaniel Rosenberg * @dentry: dentry of the parent directory 1693c843843eSDaniel Rosenberg * @str: qstr of name whose hash we should fill in 1694c843843eSDaniel Rosenberg * 1695c843843eSDaniel Rosenberg * Return: 0 if hash was successful or unchanged, and -EINVAL on error 1696c843843eSDaniel Rosenberg */ 1697794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) 1698c843843eSDaniel Rosenberg { 1699c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(dentry->d_inode); 1700c843843eSDaniel Rosenberg struct super_block *sb = dentry->d_sb; 1701c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1702c843843eSDaniel Rosenberg int ret = 0; 1703c843843eSDaniel Rosenberg 1704c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1705c843843eSDaniel Rosenberg return 0; 1706c843843eSDaniel Rosenberg 1707c843843eSDaniel Rosenberg ret = utf8_casefold_hash(um, dentry, str); 1708c843843eSDaniel Rosenberg if (ret < 0 && sb_has_strict_encoding(sb)) 1709c843843eSDaniel Rosenberg return -EINVAL; 1710c843843eSDaniel Rosenberg return 0; 1711c843843eSDaniel Rosenberg } 1712608af703SDaniel Rosenberg 1713608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = { 1714608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1715608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1716608af703SDaniel Rosenberg }; 1717c843843eSDaniel Rosenberg #endif 1718608af703SDaniel Rosenberg 1719608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1720608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = { 1721608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1722608af703SDaniel Rosenberg }; 1723608af703SDaniel Rosenberg #endif 1724608af703SDaniel Rosenberg 17255298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1726608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = { 1727608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1728608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1729608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1730608af703SDaniel Rosenberg }; 1731608af703SDaniel Rosenberg #endif 1732608af703SDaniel Rosenberg 1733608af703SDaniel Rosenberg /** 1734608af703SDaniel Rosenberg * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry 1735608af703SDaniel Rosenberg * @dentry: dentry to set ops on 1736608af703SDaniel Rosenberg * 1737608af703SDaniel Rosenberg * Casefolded directories need d_hash and d_compare set, so that the dentries 1738608af703SDaniel Rosenberg * contained in them are handled case-insensitively. Note that these operations 1739608af703SDaniel Rosenberg * are needed on the parent directory rather than on the dentries in it, and 1740608af703SDaniel Rosenberg * while the casefolding flag can be toggled on and off on an empty directory, 1741608af703SDaniel Rosenberg * dentry_operations can't be changed later. As a result, if the filesystem has 1742608af703SDaniel Rosenberg * casefolding support enabled at all, we have to give all dentries the 1743608af703SDaniel Rosenberg * casefolding operations even if their inode doesn't have the casefolding flag 1744608af703SDaniel Rosenberg * currently (and thus the casefolding ops would be no-ops for now). 1745608af703SDaniel Rosenberg * 1746608af703SDaniel Rosenberg * Encryption works differently in that the only dentry operation it needs is 1747608af703SDaniel Rosenberg * d_revalidate, which it only needs on dentries that have the no-key name flag. 1748608af703SDaniel Rosenberg * The no-key flag can't be set "later", so we don't have to worry about that. 1749608af703SDaniel Rosenberg * 1750608af703SDaniel Rosenberg * Finally, to maximize compatibility with overlayfs (which isn't compatible 1751608af703SDaniel Rosenberg * with certain dentry operations) and to avoid taking an unnecessary 1752608af703SDaniel Rosenberg * performance hit, we use custom dentry_operations for each possible 1753608af703SDaniel Rosenberg * combination rather than always installing all operations. 1754608af703SDaniel Rosenberg */ 1755608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry) 1756608af703SDaniel Rosenberg { 1757608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1758608af703SDaniel Rosenberg bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME; 1759608af703SDaniel Rosenberg #endif 17605298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1761608af703SDaniel Rosenberg bool needs_ci_ops = dentry->d_sb->s_encoding; 1762608af703SDaniel Rosenberg #endif 17635298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1764608af703SDaniel Rosenberg if (needs_encrypt_ops && needs_ci_ops) { 1765608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops); 1766608af703SDaniel Rosenberg return; 1767608af703SDaniel Rosenberg } 1768608af703SDaniel Rosenberg #endif 1769608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1770608af703SDaniel Rosenberg if (needs_encrypt_ops) { 1771608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_dentry_ops); 1772608af703SDaniel Rosenberg return; 1773608af703SDaniel Rosenberg } 1774608af703SDaniel Rosenberg #endif 17755298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1776608af703SDaniel Rosenberg if (needs_ci_ops) { 1777608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_ci_dentry_ops); 1778608af703SDaniel Rosenberg return; 1779608af703SDaniel Rosenberg } 1780608af703SDaniel Rosenberg #endif 1781608af703SDaniel Rosenberg } 1782608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops); 17835ca14835SAndrew Morton 17845ca14835SAndrew Morton /** 17855ca14835SAndrew Morton * inode_maybe_inc_iversion - increments i_version 17865ca14835SAndrew Morton * @inode: inode with the i_version that should be updated 17875ca14835SAndrew Morton * @force: increment the counter even if it's not necessary? 17885ca14835SAndrew Morton * 17895ca14835SAndrew Morton * Every time the inode is modified, the i_version field must be seen to have 17905ca14835SAndrew Morton * changed by any observer. 17915ca14835SAndrew Morton * 17925ca14835SAndrew Morton * If "force" is set or the QUERIED flag is set, then ensure that we increment 17935ca14835SAndrew Morton * the value, and clear the queried flag. 17945ca14835SAndrew Morton * 17955ca14835SAndrew Morton * In the common case where neither is set, then we can return "false" without 17965ca14835SAndrew Morton * updating i_version. 17975ca14835SAndrew Morton * 17985ca14835SAndrew Morton * If this function returns false, and no other metadata has changed, then we 17995ca14835SAndrew Morton * can avoid logging the metadata. 18005ca14835SAndrew Morton */ 18015ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force) 18025ca14835SAndrew Morton { 18035ca14835SAndrew Morton u64 cur, new; 18045ca14835SAndrew Morton 18055ca14835SAndrew Morton /* 18065ca14835SAndrew Morton * The i_version field is not strictly ordered with any other inode 18075ca14835SAndrew Morton * information, but the legacy inode_inc_iversion code used a spinlock 18085ca14835SAndrew Morton * to serialize increments. 18095ca14835SAndrew Morton * 18105ca14835SAndrew Morton * Here, we add full memory barriers to ensure that any de-facto 18115ca14835SAndrew Morton * ordering with other info is preserved. 18125ca14835SAndrew Morton * 18135ca14835SAndrew Morton * This barrier pairs with the barrier in inode_query_iversion() 18145ca14835SAndrew Morton */ 18155ca14835SAndrew Morton smp_mb(); 18165ca14835SAndrew Morton cur = inode_peek_iversion_raw(inode); 18175ca14835SAndrew Morton do { 18185ca14835SAndrew Morton /* If flag is clear then we needn't do anything */ 18195ca14835SAndrew Morton if (!force && !(cur & I_VERSION_QUERIED)) 18205ca14835SAndrew Morton return false; 18215ca14835SAndrew Morton 18225ca14835SAndrew Morton /* Since lowest bit is flag, add 2 to avoid it */ 18235ca14835SAndrew Morton new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT; 18245ca14835SAndrew Morton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 18255ca14835SAndrew Morton return true; 18265ca14835SAndrew Morton } 18275ca14835SAndrew Morton EXPORT_SYMBOL(inode_maybe_inc_iversion); 1828c5bc1b3fSJeff Layton 1829c5bc1b3fSJeff Layton /** 1830c5bc1b3fSJeff Layton * inode_query_iversion - read i_version for later use 1831c5bc1b3fSJeff Layton * @inode: inode from which i_version should be read 1832c5bc1b3fSJeff Layton * 1833c5bc1b3fSJeff Layton * Read the inode i_version counter. This should be used by callers that wish 1834c5bc1b3fSJeff Layton * to store the returned i_version for later comparison. This will guarantee 1835c5bc1b3fSJeff Layton * that a later query of the i_version will result in a different value if 1836c5bc1b3fSJeff Layton * anything has changed. 1837c5bc1b3fSJeff Layton * 1838c5bc1b3fSJeff Layton * In this implementation, we fetch the current value, set the QUERIED flag and 1839c5bc1b3fSJeff Layton * then try to swap it into place with a cmpxchg, if it wasn't already set. If 1840c5bc1b3fSJeff Layton * that fails, we try again with the newly fetched value from the cmpxchg. 1841c5bc1b3fSJeff Layton */ 1842c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode) 1843c5bc1b3fSJeff Layton { 1844c5bc1b3fSJeff Layton u64 cur, new; 1845c5bc1b3fSJeff Layton 1846c5bc1b3fSJeff Layton cur = inode_peek_iversion_raw(inode); 1847c5bc1b3fSJeff Layton do { 1848c5bc1b3fSJeff Layton /* If flag is already set, then no need to swap */ 1849c5bc1b3fSJeff Layton if (cur & I_VERSION_QUERIED) { 1850c5bc1b3fSJeff Layton /* 1851c5bc1b3fSJeff Layton * This barrier (and the implicit barrier in the 1852c5bc1b3fSJeff Layton * cmpxchg below) pairs with the barrier in 1853c5bc1b3fSJeff Layton * inode_maybe_inc_iversion(). 1854c5bc1b3fSJeff Layton */ 1855c5bc1b3fSJeff Layton smp_mb(); 1856c5bc1b3fSJeff Layton break; 1857c5bc1b3fSJeff Layton } 1858c5bc1b3fSJeff Layton 1859c5bc1b3fSJeff Layton new = cur | I_VERSION_QUERIED; 1860c5bc1b3fSJeff Layton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 1861c5bc1b3fSJeff Layton return cur >> I_VERSION_QUERIED_SHIFT; 1862c5bc1b3fSJeff Layton } 1863c5bc1b3fSJeff Layton EXPORT_SYMBOL(inode_query_iversion); 186444fff0faSChristoph Hellwig 186544fff0faSChristoph Hellwig ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, 186644fff0faSChristoph Hellwig ssize_t direct_written, ssize_t buffered_written) 186744fff0faSChristoph Hellwig { 186844fff0faSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 186944fff0faSChristoph Hellwig loff_t pos = iocb->ki_pos - buffered_written; 187044fff0faSChristoph Hellwig loff_t end = iocb->ki_pos - 1; 187144fff0faSChristoph Hellwig int err; 187244fff0faSChristoph Hellwig 187344fff0faSChristoph Hellwig /* 187444fff0faSChristoph Hellwig * If the buffered write fallback returned an error, we want to return 187544fff0faSChristoph Hellwig * the number of bytes which were written by direct I/O, or the error 187644fff0faSChristoph Hellwig * code if that was zero. 187744fff0faSChristoph Hellwig * 187844fff0faSChristoph Hellwig * Note that this differs from normal direct-io semantics, which will 187944fff0faSChristoph Hellwig * return -EFOO even if some bytes were written. 188044fff0faSChristoph Hellwig */ 188144fff0faSChristoph Hellwig if (unlikely(buffered_written < 0)) { 188244fff0faSChristoph Hellwig if (direct_written) 188344fff0faSChristoph Hellwig return direct_written; 188444fff0faSChristoph Hellwig return buffered_written; 188544fff0faSChristoph Hellwig } 188644fff0faSChristoph Hellwig 188744fff0faSChristoph Hellwig /* 188844fff0faSChristoph Hellwig * We need to ensure that the page cache pages are written to disk and 188944fff0faSChristoph Hellwig * invalidated to preserve the expected O_DIRECT semantics. 189044fff0faSChristoph Hellwig */ 189144fff0faSChristoph Hellwig err = filemap_write_and_wait_range(mapping, pos, end); 189244fff0faSChristoph Hellwig if (err < 0) { 189344fff0faSChristoph Hellwig /* 189444fff0faSChristoph Hellwig * We don't know how much we wrote, so just return the number of 189544fff0faSChristoph Hellwig * bytes which were direct-written 189644fff0faSChristoph Hellwig */ 189744fff0faSChristoph Hellwig if (direct_written) 189844fff0faSChristoph Hellwig return direct_written; 189944fff0faSChristoph Hellwig return err; 190044fff0faSChristoph Hellwig } 190144fff0faSChristoph Hellwig invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT); 190244fff0faSChristoph Hellwig return direct_written + buffered_written; 190344fff0faSChristoph Hellwig } 190444fff0faSChristoph Hellwig EXPORT_SYMBOL_GPL(direct_write_fallback); 1905