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 252*bbaef797SChuck Lever static struct lock_class_key simple_offset_xa_lock; 253*bbaef797SChuck 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); 262*bbaef797SChuck 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; 4106faddda6SChuck Lever spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 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 4286faddda6SChuck Lever static void offset_iterate_dir(struct dentry *dir, struct dir_context *ctx) 4296faddda6SChuck Lever { 4306faddda6SChuck Lever struct inode *inode = d_inode(dir); 4316faddda6SChuck Lever struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); 4326faddda6SChuck Lever XA_STATE(xas, &so_ctx->xa, ctx->pos); 4336faddda6SChuck Lever struct dentry *dentry; 4346faddda6SChuck Lever 4356faddda6SChuck Lever while (true) { 4366faddda6SChuck Lever spin_lock(&dir->d_lock); 4376faddda6SChuck Lever dentry = offset_find_next(&xas); 4386faddda6SChuck Lever spin_unlock(&dir->d_lock); 4396faddda6SChuck Lever if (!dentry) 4406faddda6SChuck Lever break; 4416faddda6SChuck Lever 4426faddda6SChuck Lever if (!offset_dir_emit(ctx, dentry)) { 4436faddda6SChuck Lever dput(dentry); 4446faddda6SChuck Lever break; 4456faddda6SChuck Lever } 4466faddda6SChuck Lever 4476faddda6SChuck Lever dput(dentry); 4486faddda6SChuck Lever ctx->pos = xas.xa_index + 1; 4496faddda6SChuck Lever } 4506faddda6SChuck Lever } 4516faddda6SChuck Lever 4526faddda6SChuck Lever /** 4536faddda6SChuck Lever * offset_readdir - Emit entries starting at offset @ctx->pos 4546faddda6SChuck Lever * @file: an open directory to iterate over 4556faddda6SChuck Lever * @ctx: directory iteration context 4566faddda6SChuck Lever * 4576faddda6SChuck Lever * Caller must hold @file's i_rwsem to prevent insertion or removal of 4586faddda6SChuck Lever * entries during this call. 4596faddda6SChuck Lever * 4606faddda6SChuck Lever * On entry, @ctx->pos contains an offset that represents the first entry 4616faddda6SChuck Lever * to be read from the directory. 4626faddda6SChuck Lever * 4636faddda6SChuck Lever * The operation continues until there are no more entries to read, or 4646faddda6SChuck Lever * until the ctx->actor indicates there is no more space in the caller's 4656faddda6SChuck Lever * output buffer. 4666faddda6SChuck Lever * 4676faddda6SChuck Lever * On return, @ctx->pos contains an offset that will read the next entry 4686faddda6SChuck Lever * in this directory when shmem_readdir() is called again with @ctx. 4696faddda6SChuck Lever * 4706faddda6SChuck Lever * Return values: 4716faddda6SChuck Lever * %0 - Complete 4726faddda6SChuck Lever */ 4736faddda6SChuck Lever static int offset_readdir(struct file *file, struct dir_context *ctx) 4746faddda6SChuck Lever { 4756faddda6SChuck Lever struct dentry *dir = file->f_path.dentry; 4766faddda6SChuck Lever 4776faddda6SChuck Lever lockdep_assert_held(&d_inode(dir)->i_rwsem); 4786faddda6SChuck Lever 4796faddda6SChuck Lever if (!dir_emit_dots(file, ctx)) 4806faddda6SChuck Lever return 0; 4816faddda6SChuck Lever 4826faddda6SChuck Lever offset_iterate_dir(dir, ctx); 4836faddda6SChuck Lever return 0; 4846faddda6SChuck Lever } 4856faddda6SChuck Lever 4866faddda6SChuck Lever const struct file_operations simple_offset_dir_operations = { 4876faddda6SChuck Lever .llseek = offset_dir_llseek, 4886faddda6SChuck Lever .iterate_shared = offset_readdir, 4896faddda6SChuck Lever .read = generic_read_dir, 4906faddda6SChuck Lever .fsync = noop_fsync, 4916faddda6SChuck Lever }; 4926faddda6SChuck Lever 493a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev) 494a3d1e7ebSAl Viro { 495a3d1e7ebSAl Viro struct dentry *child = NULL; 496a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs; 497a3d1e7ebSAl Viro 498a3d1e7ebSAl Viro spin_lock(&parent->d_lock); 499a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) { 500a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child); 501a3d1e7ebSAl Viro if (simple_positive(d)) { 502a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); 503a3d1e7ebSAl Viro if (simple_positive(d)) 504a3d1e7ebSAl Viro child = dget_dlock(d); 505a3d1e7ebSAl Viro spin_unlock(&d->d_lock); 506a3d1e7ebSAl Viro if (likely(child)) 507a3d1e7ebSAl Viro break; 508a3d1e7ebSAl Viro } 509a3d1e7ebSAl Viro } 510a3d1e7ebSAl Viro spin_unlock(&parent->d_lock); 511a3d1e7ebSAl Viro dput(prev); 512a3d1e7ebSAl Viro return child; 513a3d1e7ebSAl Viro } 514a3d1e7ebSAl Viro 515a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry, 516a3d1e7ebSAl Viro void (*callback)(struct dentry *)) 517a3d1e7ebSAl Viro { 518a3d1e7ebSAl Viro struct dentry *this = dget(dentry); 519a3d1e7ebSAl Viro while (true) { 520a3d1e7ebSAl Viro struct dentry *victim = NULL, *child; 521a3d1e7ebSAl Viro struct inode *inode = this->d_inode; 522a3d1e7ebSAl Viro 523a3d1e7ebSAl Viro inode_lock(inode); 524a3d1e7ebSAl Viro if (d_is_dir(this)) 525a3d1e7ebSAl Viro inode->i_flags |= S_DEAD; 526a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) { 527a3d1e7ebSAl Viro // kill and ascend 528a3d1e7ebSAl Viro // update metadata while it's still locked 529a3d1e7ebSAl Viro inode->i_ctime = current_time(inode); 530a3d1e7ebSAl Viro clear_nlink(inode); 531a3d1e7ebSAl Viro inode_unlock(inode); 532a3d1e7ebSAl Viro victim = this; 533a3d1e7ebSAl Viro this = this->d_parent; 534a3d1e7ebSAl Viro inode = this->d_inode; 535a3d1e7ebSAl Viro inode_lock(inode); 536a3d1e7ebSAl Viro if (simple_positive(victim)) { 537a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts 538a3d1e7ebSAl Viro if (d_is_dir(victim)) 539a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim); 540a3d1e7ebSAl Viro else 541a3d1e7ebSAl Viro fsnotify_unlink(inode, victim); 542a3d1e7ebSAl Viro if (callback) 543a3d1e7ebSAl Viro callback(victim); 544a3d1e7ebSAl Viro dput(victim); // unpin it 545a3d1e7ebSAl Viro } 546a3d1e7ebSAl Viro if (victim == dentry) { 547a3d1e7ebSAl Viro inode->i_ctime = inode->i_mtime = 548a3d1e7ebSAl Viro current_time(inode); 549a3d1e7ebSAl Viro if (d_is_dir(dentry)) 550a3d1e7ebSAl Viro drop_nlink(inode); 551a3d1e7ebSAl Viro inode_unlock(inode); 552a3d1e7ebSAl Viro dput(dentry); 553a3d1e7ebSAl Viro return; 554a3d1e7ebSAl Viro } 555a3d1e7ebSAl Viro } 556a3d1e7ebSAl Viro inode_unlock(inode); 557a3d1e7ebSAl Viro this = child; 558a3d1e7ebSAl Viro } 559a3d1e7ebSAl Viro } 560a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal); 561a3d1e7ebSAl Viro 562759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 563759b9775SHugh Dickins .statfs = simple_statfs, 564759b9775SHugh Dickins }; 565759b9775SHugh Dickins 566db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc) 5671da177e4SLinus Torvalds { 56831d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private; 5691da177e4SLinus Torvalds struct inode *root; 5701da177e4SLinus Torvalds 57189a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 5723971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 5733971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 5748d9e46d8SAl Viro s->s_magic = ctx->magic; 5758d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations; 5768d9e46d8SAl Viro s->s_xattr = ctx->xattr; 5771da177e4SLinus Torvalds s->s_time_gran = 1; 5781da177e4SLinus Torvalds root = new_inode(s); 5791da177e4SLinus Torvalds if (!root) 580db2c246aSDavid Howells return -ENOMEM; 581db2c246aSDavid Howells 5821a1c9bb4SJeff Layton /* 5831a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 5841a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 5851a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 5861a1c9bb4SJeff Layton */ 5871a1c9bb4SJeff Layton root->i_ino = 1; 5881da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 589078cd827SDeepa Dinamani root->i_atime = root->i_mtime = root->i_ctime = current_time(root); 5908d9e46d8SAl Viro s->s_root = d_make_root(root); 5918d9e46d8SAl Viro if (!s->s_root) 5928d9e46d8SAl Viro return -ENOMEM; 593db2c246aSDavid Howells s->s_d_op = ctx->dops; 594db2c246aSDavid Howells return 0; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 597db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc) 598db2c246aSDavid Howells { 5992ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super); 6001da177e4SLinus Torvalds } 60131d6d5ceSDavid Howells 60231d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc) 60331d6d5ceSDavid Howells { 60431d6d5ceSDavid Howells kfree(fc->fs_private); 60531d6d5ceSDavid Howells } 60631d6d5ceSDavid Howells 60731d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = { 60831d6d5ceSDavid Howells .free = pseudo_fs_free, 60931d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree, 61031d6d5ceSDavid Howells }; 61131d6d5ceSDavid Howells 61231d6d5ceSDavid Howells /* 61331d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 61431d6d5ceSDavid Howells * will never be mountable) 61531d6d5ceSDavid Howells */ 61631d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc, 61731d6d5ceSDavid Howells unsigned long magic) 61831d6d5ceSDavid Howells { 61931d6d5ceSDavid Howells struct pseudo_fs_context *ctx; 62031d6d5ceSDavid Howells 62131d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL); 62231d6d5ceSDavid Howells if (likely(ctx)) { 62331d6d5ceSDavid Howells ctx->magic = magic; 62431d6d5ceSDavid Howells fc->fs_private = ctx; 62531d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops; 626db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER; 627db2c246aSDavid Howells fc->global = true; 62831d6d5ceSDavid Howells } 62931d6d5ceSDavid Howells return ctx; 63031d6d5ceSDavid Howells } 63131d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo); 6321da177e4SLinus Torvalds 63320955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 63420955e89SStephen Boyd { 63520955e89SStephen Boyd if (inode->i_private) 63620955e89SStephen Boyd file->private_data = inode->i_private; 63720955e89SStephen Boyd return 0; 63820955e89SStephen Boyd } 63912f38872SAl Viro EXPORT_SYMBOL(simple_open); 64020955e89SStephen Boyd 6411da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 6421da177e4SLinus Torvalds { 643dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 6441da177e4SLinus Torvalds 645078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 646d8c76e6fSDave Hansen inc_nlink(inode); 6477de9c6eeSAl Viro ihold(inode); 6481da177e4SLinus Torvalds dget(dentry); 6491da177e4SLinus Torvalds d_instantiate(dentry, inode); 6501da177e4SLinus Torvalds return 0; 6511da177e4SLinus Torvalds } 65212f38872SAl Viro EXPORT_SYMBOL(simple_link); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds struct dentry *child; 6571da177e4SLinus Torvalds int ret = 0; 6581da177e4SLinus Torvalds 6592fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 660946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) { 661da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 662da502956SNick Piggin if (simple_positive(child)) { 663da502956SNick Piggin spin_unlock(&child->d_lock); 6641da177e4SLinus Torvalds goto out; 665da502956SNick Piggin } 666da502956SNick Piggin spin_unlock(&child->d_lock); 667da502956SNick Piggin } 6681da177e4SLinus Torvalds ret = 1; 6691da177e4SLinus Torvalds out: 6702fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 6711da177e4SLinus Torvalds return ret; 6721da177e4SLinus Torvalds } 67312f38872SAl Viro EXPORT_SYMBOL(simple_empty); 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 6761da177e4SLinus Torvalds { 677dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 6781da177e4SLinus Torvalds 679078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 6809a53c3a7SDave Hansen drop_nlink(inode); 6811da177e4SLinus Torvalds dput(dentry); 6821da177e4SLinus Torvalds return 0; 6831da177e4SLinus Torvalds } 68412f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 6871da177e4SLinus Torvalds { 6881da177e4SLinus Torvalds if (!simple_empty(dentry)) 6891da177e4SLinus Torvalds return -ENOTEMPTY; 6901da177e4SLinus Torvalds 691dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 6921da177e4SLinus Torvalds simple_unlink(dir, dentry); 6939a53c3a7SDave Hansen drop_nlink(dir); 6941da177e4SLinus Torvalds return 0; 6951da177e4SLinus Torvalds } 69612f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 6971da177e4SLinus Torvalds 6986429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry, 6996429e463SLorenz Bauer struct inode *new_dir, struct dentry *new_dentry) 7006429e463SLorenz Bauer { 7016429e463SLorenz Bauer bool old_is_dir = d_is_dir(old_dentry); 7026429e463SLorenz Bauer bool new_is_dir = d_is_dir(new_dentry); 7036429e463SLorenz Bauer 7046429e463SLorenz Bauer if (old_dir != new_dir && old_is_dir != new_is_dir) { 7056429e463SLorenz Bauer if (old_is_dir) { 7066429e463SLorenz Bauer drop_nlink(old_dir); 7076429e463SLorenz Bauer inc_nlink(new_dir); 7086429e463SLorenz Bauer } else { 7096429e463SLorenz Bauer drop_nlink(new_dir); 7106429e463SLorenz Bauer inc_nlink(old_dir); 7116429e463SLorenz Bauer } 7126429e463SLorenz Bauer } 7136429e463SLorenz Bauer old_dir->i_ctime = old_dir->i_mtime = 7146429e463SLorenz Bauer new_dir->i_ctime = new_dir->i_mtime = 7156429e463SLorenz Bauer d_inode(old_dentry)->i_ctime = 7166429e463SLorenz Bauer d_inode(new_dentry)->i_ctime = current_time(old_dir); 7176429e463SLorenz Bauer 7186429e463SLorenz Bauer return 0; 7196429e463SLorenz Bauer } 7206429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange); 7216429e463SLorenz Bauer 722e18275aeSChristian Brauner int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir, 723549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 724549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 7251da177e4SLinus Torvalds { 726dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 727e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 7281da177e4SLinus Torvalds 7293871cb8cSLorenz Bauer if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 730e0e0be8aSMiklos Szeredi return -EINVAL; 731e0e0be8aSMiklos Szeredi 7323871cb8cSLorenz Bauer if (flags & RENAME_EXCHANGE) 7333871cb8cSLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 7343871cb8cSLorenz Bauer 7351da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 7361da177e4SLinus Torvalds return -ENOTEMPTY; 7371da177e4SLinus Torvalds 738dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 7391da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 740841590ceSAl Viro if (they_are_dirs) { 741dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 7429a53c3a7SDave Hansen drop_nlink(old_dir); 743841590ceSAl Viro } 7441da177e4SLinus Torvalds } else if (they_are_dirs) { 7459a53c3a7SDave Hansen drop_nlink(old_dir); 746d8c76e6fSDave Hansen inc_nlink(new_dir); 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 750078cd827SDeepa Dinamani new_dir->i_mtime = inode->i_ctime = current_time(old_dir); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds return 0; 7531da177e4SLinus Torvalds } 75412f38872SAl Viro EXPORT_SYMBOL(simple_rename); 7551da177e4SLinus Torvalds 7567bb46a67Snpiggin@suse.de /** 757eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 758c1632a0fSChristian Brauner * @idmap: idmap of the target mount 7597bb46a67Snpiggin@suse.de * @dentry: dentry 7607bb46a67Snpiggin@suse.de * @iattr: iattr structure 7617bb46a67Snpiggin@suse.de * 7627bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 7637bb46a67Snpiggin@suse.de * 764eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 765eef2380cSChristoph Hellwig * implementation of size changes. 766eef2380cSChristoph Hellwig * 767eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 768eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 769eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 7707bb46a67Snpiggin@suse.de */ 771c1632a0fSChristian Brauner int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 772549c7297SChristian Brauner struct iattr *iattr) 7737bb46a67Snpiggin@suse.de { 774dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 7757bb46a67Snpiggin@suse.de int error; 7767bb46a67Snpiggin@suse.de 777c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, iattr); 7787bb46a67Snpiggin@suse.de if (error) 7797bb46a67Snpiggin@suse.de return error; 7807bb46a67Snpiggin@suse.de 7812c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 7822c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 783c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr); 784eef2380cSChristoph Hellwig mark_inode_dirty(inode); 785eef2380cSChristoph Hellwig return 0; 7867bb46a67Snpiggin@suse.de } 7877bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 7887bb46a67Snpiggin@suse.de 789a77f580aSMatthew Wilcox (Oracle) static int simple_read_folio(struct file *file, struct folio *folio) 7901da177e4SLinus Torvalds { 791a77f580aSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 792a77f580aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 793a77f580aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 794a77f580aSMatthew Wilcox (Oracle) folio_unlock(folio); 7951da177e4SLinus Torvalds return 0; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 798afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 7999d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 800afddba49SNick Piggin struct page **pagep, void **fsdata) 801afddba49SNick Piggin { 802afddba49SNick Piggin struct page *page; 803afddba49SNick Piggin pgoff_t index; 804afddba49SNick Piggin 80509cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 806afddba49SNick Piggin 807b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 808afddba49SNick Piggin if (!page) 809afddba49SNick Piggin return -ENOMEM; 810afddba49SNick Piggin 811afddba49SNick Piggin *pagep = page; 812afddba49SNick Piggin 81309cbfeafSKirill A. Shutemov if (!PageUptodate(page) && (len != PAGE_SIZE)) { 81409cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 815193cf4b9SBoaz Harrosh 81609cbfeafSKirill A. Shutemov zero_user_segments(page, 0, from, from + len, PAGE_SIZE); 817193cf4b9SBoaz Harrosh } 818193cf4b9SBoaz Harrosh return 0; 819afddba49SNick Piggin } 82012f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 821afddba49SNick Piggin 822ad2a722fSBoaz Harrosh /** 823ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 8248e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations 825ad2a722fSBoaz Harrosh * @mapping: " 826ad2a722fSBoaz Harrosh * @pos: " 827ad2a722fSBoaz Harrosh * @len: " 828ad2a722fSBoaz Harrosh * @copied: " 829ad2a722fSBoaz Harrosh * @page: " 830ad2a722fSBoaz Harrosh * @fsdata: " 831ad2a722fSBoaz Harrosh * 832ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 833ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 834ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 835ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 836ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 837ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 838ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 839ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 840ad2a722fSBoaz Harrosh * case that i_size has changed. 84104fff641SAl Viro * 842a77f580aSMatthew Wilcox (Oracle) * Use *ONLY* with simple_read_folio() 843ad2a722fSBoaz Harrosh */ 844c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping, 845ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 846ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 849ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 850ad2a722fSBoaz Harrosh 851ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 85204fff641SAl Viro if (!PageUptodate(page)) { 853ad2a722fSBoaz Harrosh if (copied < len) { 85409cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 855ad2a722fSBoaz Harrosh 856ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 857ad2a722fSBoaz Harrosh } 858955eff5aSNick Piggin SetPageUptodate(page); 85904fff641SAl Viro } 8601da177e4SLinus Torvalds /* 8611da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 8621b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 8631da177e4SLinus Torvalds */ 864ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 865ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 866ad2a722fSBoaz Harrosh 8671da177e4SLinus Torvalds set_page_dirty(page); 868afddba49SNick Piggin unlock_page(page); 86909cbfeafSKirill A. Shutemov put_page(page); 870afddba49SNick Piggin 871afddba49SNick Piggin return copied; 872afddba49SNick Piggin } 873c1e3dbe9SChristoph Hellwig 874c1e3dbe9SChristoph Hellwig /* 875c1e3dbe9SChristoph Hellwig * Provides ramfs-style behavior: data in the pagecache, but no writeback. 876c1e3dbe9SChristoph Hellwig */ 877c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = { 878a77f580aSMatthew Wilcox (Oracle) .read_folio = simple_read_folio, 879c1e3dbe9SChristoph Hellwig .write_begin = simple_write_begin, 880c1e3dbe9SChristoph Hellwig .write_end = simple_write_end, 88146de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 882c1e3dbe9SChristoph Hellwig }; 883c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops); 884afddba49SNick Piggin 8851a1c9bb4SJeff Layton /* 8861a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 8871a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 8881a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 8891a1c9bb4SJeff Layton */ 8907d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 891cda37124SEric Biggers const struct tree_descr *files) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds struct inode *inode; 8941da177e4SLinus Torvalds struct dentry *root; 8951da177e4SLinus Torvalds struct dentry *dentry; 8961da177e4SLinus Torvalds int i; 8971da177e4SLinus Torvalds 89809cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE; 89909cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT; 9001da177e4SLinus Torvalds s->s_magic = magic; 901759b9775SHugh Dickins s->s_op = &simple_super_operations; 9021da177e4SLinus Torvalds s->s_time_gran = 1; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds inode = new_inode(s); 9051da177e4SLinus Torvalds if (!inode) 9061da177e4SLinus Torvalds return -ENOMEM; 9071a1c9bb4SJeff Layton /* 9081a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 9091a1c9bb4SJeff Layton * entry at index 1 9101a1c9bb4SJeff Layton */ 9111a1c9bb4SJeff Layton inode->i_ino = 1; 9121da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 913078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9141da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 9151da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 916bfe86848SMiklos Szeredi set_nlink(inode, 2); 91748fde701SAl Viro root = d_make_root(inode); 91848fde701SAl Viro if (!root) 9191da177e4SLinus Torvalds return -ENOMEM; 9201da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 9211da177e4SLinus Torvalds if (!files->name) 9221da177e4SLinus Torvalds continue; 9231a1c9bb4SJeff Layton 9241a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 9251a1c9bb4SJeff Layton if (unlikely(i == 1)) 9261a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 9271a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 9281a1c9bb4SJeff Layton s->s_type->name); 9291a1c9bb4SJeff Layton 9301da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 9311da177e4SLinus Torvalds if (!dentry) 9321da177e4SLinus Torvalds goto out; 9331da177e4SLinus Torvalds inode = new_inode(s); 93432096ea1SKonstantin Khlebnikov if (!inode) { 93532096ea1SKonstantin Khlebnikov dput(dentry); 9361da177e4SLinus Torvalds goto out; 93732096ea1SKonstantin Khlebnikov } 9381da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 939078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9401da177e4SLinus Torvalds inode->i_fop = files->ops; 9411da177e4SLinus Torvalds inode->i_ino = i; 9421da177e4SLinus Torvalds d_add(dentry, inode); 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds s->s_root = root; 9451da177e4SLinus Torvalds return 0; 9461da177e4SLinus Torvalds out: 9471da177e4SLinus Torvalds d_genocide(root); 948640946f2SAl Viro shrink_dcache_parent(root); 9491da177e4SLinus Torvalds dput(root); 9501da177e4SLinus Torvalds return -ENOMEM; 9511da177e4SLinus Torvalds } 95212f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 9551da177e4SLinus Torvalds 9561f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 9591da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9601da177e4SLinus Torvalds if (unlikely(!*mount)) { 9611da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9621751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 9631da177e4SLinus Torvalds if (IS_ERR(mnt)) 9641da177e4SLinus Torvalds return PTR_ERR(mnt); 9651da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9661da177e4SLinus Torvalds if (!*mount) 9671da177e4SLinus Torvalds *mount = mnt; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds mntget(*mount); 9701da177e4SLinus Torvalds ++*count; 9711da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9721da177e4SLinus Torvalds mntput(mnt); 9731da177e4SLinus Torvalds return 0; 9741da177e4SLinus Torvalds } 97512f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct vfsmount *mnt; 9801da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 9811da177e4SLinus Torvalds mnt = *mount; 9821da177e4SLinus Torvalds if (!--*count) 9831da177e4SLinus Torvalds *mount = NULL; 9841da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 9851da177e4SLinus Torvalds mntput(mnt); 9861da177e4SLinus Torvalds } 98712f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 9881da177e4SLinus Torvalds 9896d1029b5SAkinobu Mita /** 9906d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 9916d1029b5SAkinobu Mita * @to: the user space buffer to read to 9926d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 9936d1029b5SAkinobu Mita * @ppos: the current position in the buffer 9946d1029b5SAkinobu Mita * @from: the buffer to read from 9956d1029b5SAkinobu Mita * @available: the size of the buffer 9966d1029b5SAkinobu Mita * 9976d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 9986d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 9996d1029b5SAkinobu Mita * 10006d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 10016d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 10026d1029b5SAkinobu Mita **/ 10031da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 10041da177e4SLinus Torvalds const void *from, size_t available) 10051da177e4SLinus Torvalds { 10061da177e4SLinus Torvalds loff_t pos = *ppos; 100714be2746SSteven Rostedt size_t ret; 100814be2746SSteven Rostedt 10091da177e4SLinus Torvalds if (pos < 0) 10101da177e4SLinus Torvalds return -EINVAL; 101114be2746SSteven Rostedt if (pos >= available || !count) 10121da177e4SLinus Torvalds return 0; 10131da177e4SLinus Torvalds if (count > available - pos) 10141da177e4SLinus Torvalds count = available - pos; 101514be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 101614be2746SSteven Rostedt if (ret == count) 10171da177e4SLinus Torvalds return -EFAULT; 101814be2746SSteven Rostedt count -= ret; 10191da177e4SLinus Torvalds *ppos = pos + count; 10201da177e4SLinus Torvalds return count; 10211da177e4SLinus Torvalds } 102212f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 10231da177e4SLinus Torvalds 10246d1029b5SAkinobu Mita /** 10256a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 10266a727b43SJiri Slaby * @to: the buffer to write to 10276a727b43SJiri Slaby * @available: the size of the buffer 10286a727b43SJiri Slaby * @ppos: the current position in the buffer 10296a727b43SJiri Slaby * @from: the user space buffer to read from 10306a727b43SJiri Slaby * @count: the maximum number of bytes to read 10316a727b43SJiri Slaby * 10326a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 10336a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 10346a727b43SJiri Slaby * 10356a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 10366a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 10376a727b43SJiri Slaby **/ 10386a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 10396a727b43SJiri Slaby const void __user *from, size_t count) 10406a727b43SJiri Slaby { 10416a727b43SJiri Slaby loff_t pos = *ppos; 10426a727b43SJiri Slaby size_t res; 10436a727b43SJiri Slaby 10446a727b43SJiri Slaby if (pos < 0) 10456a727b43SJiri Slaby return -EINVAL; 10466a727b43SJiri Slaby if (pos >= available || !count) 10476a727b43SJiri Slaby return 0; 10486a727b43SJiri Slaby if (count > available - pos) 10496a727b43SJiri Slaby count = available - pos; 10506a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 10516a727b43SJiri Slaby if (res == count) 10526a727b43SJiri Slaby return -EFAULT; 10536a727b43SJiri Slaby count -= res; 10546a727b43SJiri Slaby *ppos = pos + count; 10556a727b43SJiri Slaby return count; 10566a727b43SJiri Slaby } 105712f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 10586a727b43SJiri Slaby 10596a727b43SJiri Slaby /** 10606d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 10616d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 10626d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 10636d1029b5SAkinobu Mita * @ppos: the current position in the buffer 10646d1029b5SAkinobu Mita * @from: the buffer to read from 10656d1029b5SAkinobu Mita * @available: the size of the buffer 10666d1029b5SAkinobu Mita * 10676d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 10686d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 10696d1029b5SAkinobu Mita * 10706d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 10716d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 10726d1029b5SAkinobu Mita **/ 107393b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 107493b07113SAkinobu Mita const void *from, size_t available) 107593b07113SAkinobu Mita { 107693b07113SAkinobu Mita loff_t pos = *ppos; 107793b07113SAkinobu Mita 107893b07113SAkinobu Mita if (pos < 0) 107993b07113SAkinobu Mita return -EINVAL; 108093b07113SAkinobu Mita if (pos >= available) 108193b07113SAkinobu Mita return 0; 108293b07113SAkinobu Mita if (count > available - pos) 108393b07113SAkinobu Mita count = available - pos; 108493b07113SAkinobu Mita memcpy(to, from + pos, count); 108593b07113SAkinobu Mita *ppos = pos + count; 108693b07113SAkinobu Mita 108793b07113SAkinobu Mita return count; 108893b07113SAkinobu Mita } 108912f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 109093b07113SAkinobu Mita 10911da177e4SLinus Torvalds /* 10921da177e4SLinus Torvalds * Transaction based IO. 10931da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 10941da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 10951da177e4SLinus Torvalds * file-local buffer. 10961da177e4SLinus Torvalds */ 109776791ab2SIngo Molnar 109876791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 109976791ab2SIngo Molnar { 110076791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 110176791ab2SIngo Molnar 110276791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 110376791ab2SIngo Molnar 110476791ab2SIngo Molnar /* 110576791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 110676791ab2SIngo Molnar * ar->data is ready for reading. 110776791ab2SIngo Molnar */ 110876791ab2SIngo Molnar smp_mb(); 110976791ab2SIngo Molnar ar->size = n; 111076791ab2SIngo Molnar } 111112f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 111276791ab2SIngo Molnar 11131da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 11141da177e4SLinus Torvalds { 11151da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 11161da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 11191da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 11221da177e4SLinus Torvalds if (!ar) 11231da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds /* only one write allowed per open */ 11281da177e4SLinus Torvalds if (file->private_data) { 11291da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 11301da177e4SLinus Torvalds free_page((unsigned long)ar); 11311da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 11341da177e4SLinus Torvalds file->private_data = ar; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 11391da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds return ar->data; 11421da177e4SLinus Torvalds } 114312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 11461da177e4SLinus Torvalds { 11471da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds if (!ar) 11501da177e4SLinus Torvalds return 0; 11511da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 11521da177e4SLinus Torvalds } 115312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds } 116012f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 11611da177e4SLinus Torvalds 1162acaefc25SArnd Bergmann /* Simple attribute files */ 1163acaefc25SArnd Bergmann 1164acaefc25SArnd Bergmann struct simple_attr { 11658b88b099SChristoph Hellwig int (*get)(void *, u64 *); 11668b88b099SChristoph Hellwig int (*set)(void *, u64); 1167acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 1168acaefc25SArnd Bergmann char set_buf[24]; 1169acaefc25SArnd Bergmann void *data; 1170acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 11717cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 1172acaefc25SArnd Bergmann }; 1173acaefc25SArnd Bergmann 1174acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 1175acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 1176acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 11778b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 1178acaefc25SArnd Bergmann const char *fmt) 1179acaefc25SArnd Bergmann { 1180acaefc25SArnd Bergmann struct simple_attr *attr; 1181acaefc25SArnd Bergmann 1182a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1183acaefc25SArnd Bergmann if (!attr) 1184acaefc25SArnd Bergmann return -ENOMEM; 1185acaefc25SArnd Bergmann 1186acaefc25SArnd Bergmann attr->get = get; 1187acaefc25SArnd Bergmann attr->set = set; 11888e18e294STheodore Ts'o attr->data = inode->i_private; 1189acaefc25SArnd Bergmann attr->fmt = fmt; 11907cf34c76SIngo Molnar mutex_init(&attr->mutex); 1191acaefc25SArnd Bergmann 1192acaefc25SArnd Bergmann file->private_data = attr; 1193acaefc25SArnd Bergmann 1194acaefc25SArnd Bergmann return nonseekable_open(inode, file); 1195acaefc25SArnd Bergmann } 119612f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 1197acaefc25SArnd Bergmann 119874bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 1199acaefc25SArnd Bergmann { 1200acaefc25SArnd Bergmann kfree(file->private_data); 1201acaefc25SArnd Bergmann return 0; 1202acaefc25SArnd Bergmann } 120312f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 1204acaefc25SArnd Bergmann 1205acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 1206acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 1207acaefc25SArnd Bergmann size_t len, loff_t *ppos) 1208acaefc25SArnd Bergmann { 1209acaefc25SArnd Bergmann struct simple_attr *attr; 1210acaefc25SArnd Bergmann size_t size; 1211acaefc25SArnd Bergmann ssize_t ret; 1212acaefc25SArnd Bergmann 1213acaefc25SArnd Bergmann attr = file->private_data; 1214acaefc25SArnd Bergmann 1215acaefc25SArnd Bergmann if (!attr->get) 1216acaefc25SArnd Bergmann return -EACCES; 1217acaefc25SArnd Bergmann 12189261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 12199261303aSChristoph Hellwig if (ret) 12209261303aSChristoph Hellwig return ret; 12219261303aSChristoph Hellwig 1222a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) { 1223a65cab7dSEric Biggers /* continued read */ 1224acaefc25SArnd Bergmann size = strlen(attr->get_buf); 1225a65cab7dSEric Biggers } else { 1226a65cab7dSEric Biggers /* first read */ 12278b88b099SChristoph Hellwig u64 val; 12288b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 12298b88b099SChristoph Hellwig if (ret) 12308b88b099SChristoph Hellwig goto out; 12318b88b099SChristoph Hellwig 1232acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 12338b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 12348b88b099SChristoph Hellwig } 1235acaefc25SArnd Bergmann 1236acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 12378b88b099SChristoph Hellwig out: 12387cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1239acaefc25SArnd Bergmann return ret; 1240acaefc25SArnd Bergmann } 124112f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 1242acaefc25SArnd Bergmann 1243acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 12442e41f274SAkinobu Mita static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf, 12452e41f274SAkinobu Mita size_t len, loff_t *ppos, bool is_signed) 1246acaefc25SArnd Bergmann { 1247acaefc25SArnd Bergmann struct simple_attr *attr; 1248488dac0cSYicong Yang unsigned long long val; 1249acaefc25SArnd Bergmann size_t size; 1250acaefc25SArnd Bergmann ssize_t ret; 1251acaefc25SArnd Bergmann 1252acaefc25SArnd Bergmann attr = file->private_data; 1253acaefc25SArnd Bergmann if (!attr->set) 1254acaefc25SArnd Bergmann return -EACCES; 1255acaefc25SArnd Bergmann 12569261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 12579261303aSChristoph Hellwig if (ret) 12589261303aSChristoph Hellwig return ret; 12599261303aSChristoph Hellwig 1260acaefc25SArnd Bergmann ret = -EFAULT; 1261acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 1262acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 1263acaefc25SArnd Bergmann goto out; 1264acaefc25SArnd Bergmann 1265acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 12662e41f274SAkinobu Mita if (is_signed) 12672e41f274SAkinobu Mita ret = kstrtoll(attr->set_buf, 0, &val); 12682e41f274SAkinobu Mita else 1269488dac0cSYicong Yang ret = kstrtoull(attr->set_buf, 0, &val); 1270488dac0cSYicong Yang if (ret) 1271488dac0cSYicong Yang goto out; 127205cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 127305cc0ceeSWu Fengguang if (ret == 0) 127405cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 1275acaefc25SArnd Bergmann out: 12767cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1277acaefc25SArnd Bergmann return ret; 1278acaefc25SArnd Bergmann } 12792e41f274SAkinobu Mita 12802e41f274SAkinobu Mita ssize_t simple_attr_write(struct file *file, const char __user *buf, 12812e41f274SAkinobu Mita size_t len, loff_t *ppos) 12822e41f274SAkinobu Mita { 12832e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, false); 12842e41f274SAkinobu Mita } 128512f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 1286acaefc25SArnd Bergmann 12872e41f274SAkinobu Mita ssize_t simple_attr_write_signed(struct file *file, const char __user *buf, 12882e41f274SAkinobu Mita size_t len, loff_t *ppos) 12892e41f274SAkinobu Mita { 12902e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, true); 12912e41f274SAkinobu Mita } 12922e41f274SAkinobu Mita EXPORT_SYMBOL_GPL(simple_attr_write_signed); 12932e41f274SAkinobu Mita 12942596110aSChristoph Hellwig /** 12952596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 12962596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 12972596110aSChristoph Hellwig * @fid: file handle to convert 12982596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 12992596110aSChristoph Hellwig * @fh_type: type of file handle 13002596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 13012596110aSChristoph Hellwig * 13022596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 13032596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 13042596110aSChristoph Hellwig * inode for the object specified in the file handle. 13052596110aSChristoph Hellwig */ 13062596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 13072596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 13082596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 13092596110aSChristoph Hellwig { 13102596110aSChristoph Hellwig struct inode *inode = NULL; 13112596110aSChristoph Hellwig 13122596110aSChristoph Hellwig if (fh_len < 2) 13132596110aSChristoph Hellwig return NULL; 13142596110aSChristoph Hellwig 13152596110aSChristoph Hellwig switch (fh_type) { 13162596110aSChristoph Hellwig case FILEID_INO32_GEN: 13172596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 13182596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 13192596110aSChristoph Hellwig break; 13202596110aSChristoph Hellwig } 13212596110aSChristoph Hellwig 13224ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 13232596110aSChristoph Hellwig } 13242596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 13252596110aSChristoph Hellwig 13262596110aSChristoph Hellwig /** 1327ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 13282596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 13292596110aSChristoph Hellwig * @fid: file handle to convert 13302596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 13312596110aSChristoph Hellwig * @fh_type: type of file handle 13322596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 13332596110aSChristoph Hellwig * 13342596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 13352596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 13362596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 13372596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 13382596110aSChristoph Hellwig */ 13392596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 13402596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 13412596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 13422596110aSChristoph Hellwig { 13432596110aSChristoph Hellwig struct inode *inode = NULL; 13442596110aSChristoph Hellwig 13452596110aSChristoph Hellwig if (fh_len <= 2) 13462596110aSChristoph Hellwig return NULL; 13472596110aSChristoph Hellwig 13482596110aSChristoph Hellwig switch (fh_type) { 13492596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 13502596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 13512596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 13522596110aSChristoph Hellwig break; 13532596110aSChristoph Hellwig } 13542596110aSChristoph Hellwig 13554ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 13562596110aSChristoph Hellwig } 13572596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 13582596110aSChristoph Hellwig 13591b061d92SChristoph Hellwig /** 1360ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 1361ac13a829SFabian Frederick * 13621b061d92SChristoph Hellwig * @file: file to synchronize 1363ac13a829SFabian Frederick * @start: start offset in bytes 1364ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 13651b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 13661b061d92SChristoph Hellwig * 13671b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 13681b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 13691b061d92SChristoph Hellwig * hanging off the address_space structure. 13701b061d92SChristoph Hellwig */ 1371ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 137202c24a82SJosef Bacik int datasync) 1373d5aacad5SAl Viro { 13747ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 1375d5aacad5SAl Viro int err; 1376d5aacad5SAl Viro int ret; 1377d5aacad5SAl Viro 1378383aa543SJeff Layton err = file_write_and_wait_range(file, start, end); 137902c24a82SJosef Bacik if (err) 138002c24a82SJosef Bacik return err; 138102c24a82SJosef Bacik 13825955102cSAl Viro inode_lock(inode); 1383d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 13840ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 138502c24a82SJosef Bacik goto out; 1386d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 138702c24a82SJosef Bacik goto out; 1388d5aacad5SAl Viro 1389c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 1390d5aacad5SAl Viro if (ret == 0) 1391d5aacad5SAl Viro ret = err; 1392ac13a829SFabian Frederick 139302c24a82SJosef Bacik out: 13945955102cSAl Viro inode_unlock(inode); 1395383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */ 1396383aa543SJeff Layton err = file_check_and_advance_wb_err(file); 1397383aa543SJeff Layton if (ret == 0) 1398383aa543SJeff Layton ret = err; 1399383aa543SJeff Layton return ret; 1400d5aacad5SAl Viro } 1401ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 1402ac13a829SFabian Frederick 1403ac13a829SFabian Frederick /** 1404ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 1405ac13a829SFabian Frederick * with flush 1406ac13a829SFabian Frederick * @file: file to synchronize 1407ac13a829SFabian Frederick * @start: start offset in bytes 1408ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 1409ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 1410ac13a829SFabian Frederick * 1411ac13a829SFabian Frederick */ 1412ac13a829SFabian Frederick 1413ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 1414ac13a829SFabian Frederick int datasync) 1415ac13a829SFabian Frederick { 1416ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 1417ac13a829SFabian Frederick int err; 1418ac13a829SFabian Frederick 1419ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 1420ac13a829SFabian Frederick if (err) 1421ac13a829SFabian Frederick return err; 1422c6bf3f0eSChristoph Hellwig return blkdev_issue_flush(inode->i_sb->s_bdev); 1423ac13a829SFabian Frederick } 14241b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 14251b061d92SChristoph Hellwig 142630ca22c7SPatrick J. LoPresti /** 142730ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 142830ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 142930ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 143030ca22c7SPatrick J. LoPresti * 143130ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 143230ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 143330ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 143430ca22c7SPatrick J. LoPresti */ 143530ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 143630ca22c7SPatrick J. LoPresti { 143730ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 1438a33f13efSJoel Becker u64 last_fs_page = 143909cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits); 144030ca22c7SPatrick J. LoPresti 144130ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 144230ca22c7SPatrick J. LoPresti return 0; 144330ca22c7SPatrick J. LoPresti 144409cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT)) 144530ca22c7SPatrick J. LoPresti return -EINVAL; 144630ca22c7SPatrick J. LoPresti 1447a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1448a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 144930ca22c7SPatrick J. LoPresti return -EFBIG; 145030ca22c7SPatrick J. LoPresti } 145130ca22c7SPatrick J. LoPresti return 0; 145230ca22c7SPatrick J. LoPresti } 145330ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 145430ca22c7SPatrick J. LoPresti 14551b061d92SChristoph Hellwig /* 14561b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 14571b061d92SChristoph Hellwig */ 145802c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 14591b061d92SChristoph Hellwig { 14601b061d92SChristoph Hellwig return 0; 14611b061d92SChristoph Hellwig } 14621b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 146387dc800bSAl Viro 1464f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 1465f44c7763SDan Williams { 1466f44c7763SDan Williams /* 1467f44c7763SDan Williams * iomap based filesystems support direct I/O without need for 1468f44c7763SDan Williams * this callback. However, it still needs to be set in 1469f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is 1470f44c7763SDan Williams * generally supported. 1471f44c7763SDan Williams */ 1472f44c7763SDan Williams return -EINVAL; 1473f44c7763SDan Williams } 1474f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO); 1475f44c7763SDan Williams 1476fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */ 1477fceef393SAl Viro void kfree_link(void *p) 147887dc800bSAl Viro { 1479fceef393SAl Viro kfree(p); 148087dc800bSAl Viro } 1481fceef393SAl Viro EXPORT_SYMBOL(kfree_link); 14826987843fSAl Viro 14836987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 14846987843fSAl Viro { 14856987843fSAl Viro static const struct address_space_operations anon_aops = { 148646de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 14876987843fSAl Viro }; 14886987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 14896987843fSAl Viro 14906987843fSAl Viro if (!inode) 14916987843fSAl Viro return ERR_PTR(-ENOMEM); 14926987843fSAl Viro 14936987843fSAl Viro inode->i_ino = get_next_ino(); 14946987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 14956987843fSAl Viro 14966987843fSAl Viro /* 14976987843fSAl Viro * Mark the inode dirty from the very beginning, 14986987843fSAl Viro * that way it will never be moved to the dirty 14996987843fSAl Viro * list because mark_inode_dirty() will think 15006987843fSAl Viro * that it already _is_ on the dirty list. 15016987843fSAl Viro */ 15026987843fSAl Viro inode->i_state = I_DIRTY; 15036987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 15046987843fSAl Viro inode->i_uid = current_fsuid(); 15056987843fSAl Viro inode->i_gid = current_fsgid(); 15066987843fSAl Viro inode->i_flags |= S_PRIVATE; 1507078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 15086987843fSAl Viro return inode; 15096987843fSAl Viro } 15106987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 15111c994a09SJeff Layton 15121c994a09SJeff Layton /** 15131c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 15141c994a09SJeff Layton * @filp: file pointer 15151c994a09SJeff Layton * @arg: type of lease to obtain 15161c994a09SJeff Layton * @flp: new lease supplied for insertion 1517e6f5c789SJeff Layton * @priv: private data for lm_setup operation 15181c994a09SJeff Layton * 15191c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 15201c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 15211c994a09SJeff Layton */ 15221c994a09SJeff Layton int 1523e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1524e6f5c789SJeff Layton void **priv) 15251c994a09SJeff Layton { 15261c994a09SJeff Layton return -EINVAL; 15271c994a09SJeff Layton } 15281c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 152961ba64fcSAl Viro 15306ee9706aSEric Biggers /** 15316ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks 15326ee9706aSEric Biggers * @dentry: not used here 15336ee9706aSEric Biggers * @inode: the symlink inode 15346ee9706aSEric Biggers * @done: not used here 15356ee9706aSEric Biggers * 15366ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to 15376ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called, 15386ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link 15396ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set, 15406ee9706aSEric Biggers * to mark the inode_operations as being for a symlink. 15416ee9706aSEric Biggers * 15426ee9706aSEric Biggers * Return: the symlink target 15436ee9706aSEric Biggers */ 15446b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 1545fceef393SAl Viro struct delayed_call *done) 154661ba64fcSAl Viro { 15476b255391SAl Viro return inode->i_link; 154861ba64fcSAl Viro } 15496b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 155061ba64fcSAl Viro 155161ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 15526b255391SAl Viro .get_link = simple_get_link, 155361ba64fcSAl Viro }; 155461ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1555fbabfd0fSEric W. Biederman 1556fbabfd0fSEric W. Biederman /* 1557fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1558fbabfd0fSEric W. Biederman */ 1559fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1560fbabfd0fSEric W. Biederman { 1561fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1562fbabfd0fSEric W. Biederman } 1563fbabfd0fSEric W. Biederman 1564b74d24f7SChristian Brauner static int empty_dir_getattr(struct mnt_idmap *idmap, 1565549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1566a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1567fbabfd0fSEric W. Biederman { 1568a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 1569b74d24f7SChristian Brauner generic_fillattr(&nop_mnt_idmap, inode, stat); 1570fbabfd0fSEric W. Biederman return 0; 1571fbabfd0fSEric W. Biederman } 1572fbabfd0fSEric W. Biederman 1573c1632a0fSChristian Brauner static int empty_dir_setattr(struct mnt_idmap *idmap, 1574549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 1575fbabfd0fSEric W. Biederman { 1576fbabfd0fSEric W. Biederman return -EPERM; 1577fbabfd0fSEric W. Biederman } 1578fbabfd0fSEric W. Biederman 1579fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1580fbabfd0fSEric W. Biederman { 1581fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1582fbabfd0fSEric W. Biederman } 1583fbabfd0fSEric W. Biederman 1584fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1585fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1586fbabfd0fSEric W. Biederman .permission = generic_permission, 1587fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1588fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1589fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1590fbabfd0fSEric W. Biederman }; 1591fbabfd0fSEric W. Biederman 1592fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1593fbabfd0fSEric W. Biederman { 1594fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1595fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1596fbabfd0fSEric W. Biederman } 1597fbabfd0fSEric W. Biederman 1598fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1599fbabfd0fSEric W. Biederman { 1600fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1601fbabfd0fSEric W. Biederman return 0; 1602fbabfd0fSEric W. Biederman } 1603fbabfd0fSEric W. Biederman 1604fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1605fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1606fbabfd0fSEric W. Biederman .read = generic_read_dir, 1607c51da20cSAl Viro .iterate_shared = empty_dir_readdir, 1608fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1609fbabfd0fSEric W. Biederman }; 1610fbabfd0fSEric W. Biederman 1611fbabfd0fSEric W. Biederman 1612fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1613fbabfd0fSEric W. Biederman { 1614fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1615fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1616fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1617fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1618fbabfd0fSEric W. Biederman inode->i_rdev = 0; 16194b75de86SEric W. Biederman inode->i_size = 0; 1620fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1621fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1622fbabfd0fSEric W. Biederman 1623fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1624f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR; 1625fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1626fbabfd0fSEric W. Biederman } 1627fbabfd0fSEric W. Biederman 1628fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1629fbabfd0fSEric W. Biederman { 1630fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1631fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1632fbabfd0fSEric W. Biederman } 1633c843843eSDaniel Rosenberg 16345298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1635c843843eSDaniel Rosenberg /* 1636c843843eSDaniel Rosenberg * Determine if the name of a dentry should be casefolded. 1637c843843eSDaniel Rosenberg * 1638c843843eSDaniel Rosenberg * Return: if names will need casefolding 1639c843843eSDaniel Rosenberg */ 1640c843843eSDaniel Rosenberg static bool needs_casefold(const struct inode *dir) 1641c843843eSDaniel Rosenberg { 1642c843843eSDaniel Rosenberg return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding; 1643c843843eSDaniel Rosenberg } 1644c843843eSDaniel Rosenberg 1645c843843eSDaniel Rosenberg /** 1646c843843eSDaniel Rosenberg * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems 1647c843843eSDaniel Rosenberg * @dentry: dentry whose name we are checking against 1648c843843eSDaniel Rosenberg * @len: len of name of dentry 1649c843843eSDaniel Rosenberg * @str: str pointer to name of dentry 1650c843843eSDaniel Rosenberg * @name: Name to compare against 1651c843843eSDaniel Rosenberg * 1652c843843eSDaniel Rosenberg * Return: 0 if names match, 1 if mismatch, or -ERRNO 1653c843843eSDaniel Rosenberg */ 1654794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len, 1655c843843eSDaniel Rosenberg const char *str, const struct qstr *name) 1656c843843eSDaniel Rosenberg { 1657c843843eSDaniel Rosenberg const struct dentry *parent = READ_ONCE(dentry->d_parent); 1658c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(parent->d_inode); 1659c843843eSDaniel Rosenberg const struct super_block *sb = dentry->d_sb; 1660c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1661c843843eSDaniel Rosenberg struct qstr qstr = QSTR_INIT(str, len); 1662c843843eSDaniel Rosenberg char strbuf[DNAME_INLINE_LEN]; 1663c843843eSDaniel Rosenberg int ret; 1664c843843eSDaniel Rosenberg 1665c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1666c843843eSDaniel Rosenberg goto fallback; 1667c843843eSDaniel Rosenberg /* 1668c843843eSDaniel Rosenberg * If the dentry name is stored in-line, then it may be concurrently 1669c843843eSDaniel Rosenberg * modified by a rename. If this happens, the VFS will eventually retry 1670c843843eSDaniel Rosenberg * the lookup, so it doesn't matter what ->d_compare() returns. 1671c843843eSDaniel Rosenberg * However, it's unsafe to call utf8_strncasecmp() with an unstable 1672c843843eSDaniel Rosenberg * string. Therefore, we have to copy the name into a temporary buffer. 1673c843843eSDaniel Rosenberg */ 1674c843843eSDaniel Rosenberg if (len <= DNAME_INLINE_LEN - 1) { 1675c843843eSDaniel Rosenberg memcpy(strbuf, str, len); 1676c843843eSDaniel Rosenberg strbuf[len] = 0; 1677c843843eSDaniel Rosenberg qstr.name = strbuf; 1678c843843eSDaniel Rosenberg /* prevent compiler from optimizing out the temporary buffer */ 1679c843843eSDaniel Rosenberg barrier(); 1680c843843eSDaniel Rosenberg } 1681c843843eSDaniel Rosenberg ret = utf8_strncasecmp(um, name, &qstr); 1682c843843eSDaniel Rosenberg if (ret >= 0) 1683c843843eSDaniel Rosenberg return ret; 1684c843843eSDaniel Rosenberg 1685c843843eSDaniel Rosenberg if (sb_has_strict_encoding(sb)) 1686c843843eSDaniel Rosenberg return -EINVAL; 1687c843843eSDaniel Rosenberg fallback: 1688c843843eSDaniel Rosenberg if (len != name->len) 1689c843843eSDaniel Rosenberg return 1; 1690c843843eSDaniel Rosenberg return !!memcmp(str, name->name, len); 1691c843843eSDaniel Rosenberg } 1692c843843eSDaniel Rosenberg 1693c843843eSDaniel Rosenberg /** 1694c843843eSDaniel Rosenberg * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems 1695c843843eSDaniel Rosenberg * @dentry: dentry of the parent directory 1696c843843eSDaniel Rosenberg * @str: qstr of name whose hash we should fill in 1697c843843eSDaniel Rosenberg * 1698c843843eSDaniel Rosenberg * Return: 0 if hash was successful or unchanged, and -EINVAL on error 1699c843843eSDaniel Rosenberg */ 1700794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) 1701c843843eSDaniel Rosenberg { 1702c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(dentry->d_inode); 1703c843843eSDaniel Rosenberg struct super_block *sb = dentry->d_sb; 1704c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1705c843843eSDaniel Rosenberg int ret = 0; 1706c843843eSDaniel Rosenberg 1707c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1708c843843eSDaniel Rosenberg return 0; 1709c843843eSDaniel Rosenberg 1710c843843eSDaniel Rosenberg ret = utf8_casefold_hash(um, dentry, str); 1711c843843eSDaniel Rosenberg if (ret < 0 && sb_has_strict_encoding(sb)) 1712c843843eSDaniel Rosenberg return -EINVAL; 1713c843843eSDaniel Rosenberg return 0; 1714c843843eSDaniel Rosenberg } 1715608af703SDaniel Rosenberg 1716608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = { 1717608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1718608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1719608af703SDaniel Rosenberg }; 1720c843843eSDaniel Rosenberg #endif 1721608af703SDaniel Rosenberg 1722608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1723608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = { 1724608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1725608af703SDaniel Rosenberg }; 1726608af703SDaniel Rosenberg #endif 1727608af703SDaniel Rosenberg 17285298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1729608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = { 1730608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1731608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1732608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1733608af703SDaniel Rosenberg }; 1734608af703SDaniel Rosenberg #endif 1735608af703SDaniel Rosenberg 1736608af703SDaniel Rosenberg /** 1737608af703SDaniel Rosenberg * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry 1738608af703SDaniel Rosenberg * @dentry: dentry to set ops on 1739608af703SDaniel Rosenberg * 1740608af703SDaniel Rosenberg * Casefolded directories need d_hash and d_compare set, so that the dentries 1741608af703SDaniel Rosenberg * contained in them are handled case-insensitively. Note that these operations 1742608af703SDaniel Rosenberg * are needed on the parent directory rather than on the dentries in it, and 1743608af703SDaniel Rosenberg * while the casefolding flag can be toggled on and off on an empty directory, 1744608af703SDaniel Rosenberg * dentry_operations can't be changed later. As a result, if the filesystem has 1745608af703SDaniel Rosenberg * casefolding support enabled at all, we have to give all dentries the 1746608af703SDaniel Rosenberg * casefolding operations even if their inode doesn't have the casefolding flag 1747608af703SDaniel Rosenberg * currently (and thus the casefolding ops would be no-ops for now). 1748608af703SDaniel Rosenberg * 1749608af703SDaniel Rosenberg * Encryption works differently in that the only dentry operation it needs is 1750608af703SDaniel Rosenberg * d_revalidate, which it only needs on dentries that have the no-key name flag. 1751608af703SDaniel Rosenberg * The no-key flag can't be set "later", so we don't have to worry about that. 1752608af703SDaniel Rosenberg * 1753608af703SDaniel Rosenberg * Finally, to maximize compatibility with overlayfs (which isn't compatible 1754608af703SDaniel Rosenberg * with certain dentry operations) and to avoid taking an unnecessary 1755608af703SDaniel Rosenberg * performance hit, we use custom dentry_operations for each possible 1756608af703SDaniel Rosenberg * combination rather than always installing all operations. 1757608af703SDaniel Rosenberg */ 1758608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry) 1759608af703SDaniel Rosenberg { 1760608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1761608af703SDaniel Rosenberg bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME; 1762608af703SDaniel Rosenberg #endif 17635298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1764608af703SDaniel Rosenberg bool needs_ci_ops = dentry->d_sb->s_encoding; 1765608af703SDaniel Rosenberg #endif 17665298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1767608af703SDaniel Rosenberg if (needs_encrypt_ops && needs_ci_ops) { 1768608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops); 1769608af703SDaniel Rosenberg return; 1770608af703SDaniel Rosenberg } 1771608af703SDaniel Rosenberg #endif 1772608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1773608af703SDaniel Rosenberg if (needs_encrypt_ops) { 1774608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_dentry_ops); 1775608af703SDaniel Rosenberg return; 1776608af703SDaniel Rosenberg } 1777608af703SDaniel Rosenberg #endif 17785298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1779608af703SDaniel Rosenberg if (needs_ci_ops) { 1780608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_ci_dentry_ops); 1781608af703SDaniel Rosenberg return; 1782608af703SDaniel Rosenberg } 1783608af703SDaniel Rosenberg #endif 1784608af703SDaniel Rosenberg } 1785608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops); 17865ca14835SAndrew Morton 17875ca14835SAndrew Morton /** 17885ca14835SAndrew Morton * inode_maybe_inc_iversion - increments i_version 17895ca14835SAndrew Morton * @inode: inode with the i_version that should be updated 17905ca14835SAndrew Morton * @force: increment the counter even if it's not necessary? 17915ca14835SAndrew Morton * 17925ca14835SAndrew Morton * Every time the inode is modified, the i_version field must be seen to have 17935ca14835SAndrew Morton * changed by any observer. 17945ca14835SAndrew Morton * 17955ca14835SAndrew Morton * If "force" is set or the QUERIED flag is set, then ensure that we increment 17965ca14835SAndrew Morton * the value, and clear the queried flag. 17975ca14835SAndrew Morton * 17985ca14835SAndrew Morton * In the common case where neither is set, then we can return "false" without 17995ca14835SAndrew Morton * updating i_version. 18005ca14835SAndrew Morton * 18015ca14835SAndrew Morton * If this function returns false, and no other metadata has changed, then we 18025ca14835SAndrew Morton * can avoid logging the metadata. 18035ca14835SAndrew Morton */ 18045ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force) 18055ca14835SAndrew Morton { 18065ca14835SAndrew Morton u64 cur, new; 18075ca14835SAndrew Morton 18085ca14835SAndrew Morton /* 18095ca14835SAndrew Morton * The i_version field is not strictly ordered with any other inode 18105ca14835SAndrew Morton * information, but the legacy inode_inc_iversion code used a spinlock 18115ca14835SAndrew Morton * to serialize increments. 18125ca14835SAndrew Morton * 18135ca14835SAndrew Morton * Here, we add full memory barriers to ensure that any de-facto 18145ca14835SAndrew Morton * ordering with other info is preserved. 18155ca14835SAndrew Morton * 18165ca14835SAndrew Morton * This barrier pairs with the barrier in inode_query_iversion() 18175ca14835SAndrew Morton */ 18185ca14835SAndrew Morton smp_mb(); 18195ca14835SAndrew Morton cur = inode_peek_iversion_raw(inode); 18205ca14835SAndrew Morton do { 18215ca14835SAndrew Morton /* If flag is clear then we needn't do anything */ 18225ca14835SAndrew Morton if (!force && !(cur & I_VERSION_QUERIED)) 18235ca14835SAndrew Morton return false; 18245ca14835SAndrew Morton 18255ca14835SAndrew Morton /* Since lowest bit is flag, add 2 to avoid it */ 18265ca14835SAndrew Morton new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT; 18275ca14835SAndrew Morton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 18285ca14835SAndrew Morton return true; 18295ca14835SAndrew Morton } 18305ca14835SAndrew Morton EXPORT_SYMBOL(inode_maybe_inc_iversion); 1831c5bc1b3fSJeff Layton 1832c5bc1b3fSJeff Layton /** 1833c5bc1b3fSJeff Layton * inode_query_iversion - read i_version for later use 1834c5bc1b3fSJeff Layton * @inode: inode from which i_version should be read 1835c5bc1b3fSJeff Layton * 1836c5bc1b3fSJeff Layton * Read the inode i_version counter. This should be used by callers that wish 1837c5bc1b3fSJeff Layton * to store the returned i_version for later comparison. This will guarantee 1838c5bc1b3fSJeff Layton * that a later query of the i_version will result in a different value if 1839c5bc1b3fSJeff Layton * anything has changed. 1840c5bc1b3fSJeff Layton * 1841c5bc1b3fSJeff Layton * In this implementation, we fetch the current value, set the QUERIED flag and 1842c5bc1b3fSJeff Layton * then try to swap it into place with a cmpxchg, if it wasn't already set. If 1843c5bc1b3fSJeff Layton * that fails, we try again with the newly fetched value from the cmpxchg. 1844c5bc1b3fSJeff Layton */ 1845c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode) 1846c5bc1b3fSJeff Layton { 1847c5bc1b3fSJeff Layton u64 cur, new; 1848c5bc1b3fSJeff Layton 1849c5bc1b3fSJeff Layton cur = inode_peek_iversion_raw(inode); 1850c5bc1b3fSJeff Layton do { 1851c5bc1b3fSJeff Layton /* If flag is already set, then no need to swap */ 1852c5bc1b3fSJeff Layton if (cur & I_VERSION_QUERIED) { 1853c5bc1b3fSJeff Layton /* 1854c5bc1b3fSJeff Layton * This barrier (and the implicit barrier in the 1855c5bc1b3fSJeff Layton * cmpxchg below) pairs with the barrier in 1856c5bc1b3fSJeff Layton * inode_maybe_inc_iversion(). 1857c5bc1b3fSJeff Layton */ 1858c5bc1b3fSJeff Layton smp_mb(); 1859c5bc1b3fSJeff Layton break; 1860c5bc1b3fSJeff Layton } 1861c5bc1b3fSJeff Layton 1862c5bc1b3fSJeff Layton new = cur | I_VERSION_QUERIED; 1863c5bc1b3fSJeff Layton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 1864c5bc1b3fSJeff Layton return cur >> I_VERSION_QUERIED_SHIFT; 1865c5bc1b3fSJeff Layton } 1866c5bc1b3fSJeff Layton EXPORT_SYMBOL(inode_query_iversion); 186744fff0faSChristoph Hellwig 186844fff0faSChristoph Hellwig ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, 186944fff0faSChristoph Hellwig ssize_t direct_written, ssize_t buffered_written) 187044fff0faSChristoph Hellwig { 187144fff0faSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 187244fff0faSChristoph Hellwig loff_t pos = iocb->ki_pos - buffered_written; 187344fff0faSChristoph Hellwig loff_t end = iocb->ki_pos - 1; 187444fff0faSChristoph Hellwig int err; 187544fff0faSChristoph Hellwig 187644fff0faSChristoph Hellwig /* 187744fff0faSChristoph Hellwig * If the buffered write fallback returned an error, we want to return 187844fff0faSChristoph Hellwig * the number of bytes which were written by direct I/O, or the error 187944fff0faSChristoph Hellwig * code if that was zero. 188044fff0faSChristoph Hellwig * 188144fff0faSChristoph Hellwig * Note that this differs from normal direct-io semantics, which will 188244fff0faSChristoph Hellwig * return -EFOO even if some bytes were written. 188344fff0faSChristoph Hellwig */ 188444fff0faSChristoph Hellwig if (unlikely(buffered_written < 0)) { 188544fff0faSChristoph Hellwig if (direct_written) 188644fff0faSChristoph Hellwig return direct_written; 188744fff0faSChristoph Hellwig return buffered_written; 188844fff0faSChristoph Hellwig } 188944fff0faSChristoph Hellwig 189044fff0faSChristoph Hellwig /* 189144fff0faSChristoph Hellwig * We need to ensure that the page cache pages are written to disk and 189244fff0faSChristoph Hellwig * invalidated to preserve the expected O_DIRECT semantics. 189344fff0faSChristoph Hellwig */ 189444fff0faSChristoph Hellwig err = filemap_write_and_wait_range(mapping, pos, end); 189544fff0faSChristoph Hellwig if (err < 0) { 189644fff0faSChristoph Hellwig /* 189744fff0faSChristoph Hellwig * We don't know how much we wrote, so just return the number of 189844fff0faSChristoph Hellwig * bytes which were direct-written 189944fff0faSChristoph Hellwig */ 190044fff0faSChristoph Hellwig if (direct_written) 190144fff0faSChristoph Hellwig return direct_written; 190244fff0faSChristoph Hellwig return err; 190344fff0faSChristoph Hellwig } 190444fff0faSChristoph Hellwig invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT); 190544fff0faSChristoph Hellwig return direct_written + buffered_written; 190644fff0faSChristoph Hellwig } 190744fff0faSChristoph Hellwig EXPORT_SYMBOL_GPL(direct_write_fallback); 1908