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 242a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev) 243a3d1e7ebSAl Viro { 244a3d1e7ebSAl Viro struct dentry *child = NULL; 245a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs; 246a3d1e7ebSAl Viro 247a3d1e7ebSAl Viro spin_lock(&parent->d_lock); 248a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) { 249a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child); 250a3d1e7ebSAl Viro if (simple_positive(d)) { 251a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); 252a3d1e7ebSAl Viro if (simple_positive(d)) 253a3d1e7ebSAl Viro child = dget_dlock(d); 254a3d1e7ebSAl Viro spin_unlock(&d->d_lock); 255a3d1e7ebSAl Viro if (likely(child)) 256a3d1e7ebSAl Viro break; 257a3d1e7ebSAl Viro } 258a3d1e7ebSAl Viro } 259a3d1e7ebSAl Viro spin_unlock(&parent->d_lock); 260a3d1e7ebSAl Viro dput(prev); 261a3d1e7ebSAl Viro return child; 262a3d1e7ebSAl Viro } 263a3d1e7ebSAl Viro 264a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry, 265a3d1e7ebSAl Viro void (*callback)(struct dentry *)) 266a3d1e7ebSAl Viro { 267a3d1e7ebSAl Viro struct dentry *this = dget(dentry); 268a3d1e7ebSAl Viro while (true) { 269a3d1e7ebSAl Viro struct dentry *victim = NULL, *child; 270a3d1e7ebSAl Viro struct inode *inode = this->d_inode; 271a3d1e7ebSAl Viro 272a3d1e7ebSAl Viro inode_lock(inode); 273a3d1e7ebSAl Viro if (d_is_dir(this)) 274a3d1e7ebSAl Viro inode->i_flags |= S_DEAD; 275a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) { 276a3d1e7ebSAl Viro // kill and ascend 277a3d1e7ebSAl Viro // update metadata while it's still locked 278a3d1e7ebSAl Viro inode->i_ctime = current_time(inode); 279a3d1e7ebSAl Viro clear_nlink(inode); 280a3d1e7ebSAl Viro inode_unlock(inode); 281a3d1e7ebSAl Viro victim = this; 282a3d1e7ebSAl Viro this = this->d_parent; 283a3d1e7ebSAl Viro inode = this->d_inode; 284a3d1e7ebSAl Viro inode_lock(inode); 285a3d1e7ebSAl Viro if (simple_positive(victim)) { 286a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts 287a3d1e7ebSAl Viro if (d_is_dir(victim)) 288a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim); 289a3d1e7ebSAl Viro else 290a3d1e7ebSAl Viro fsnotify_unlink(inode, victim); 291a3d1e7ebSAl Viro if (callback) 292a3d1e7ebSAl Viro callback(victim); 293a3d1e7ebSAl Viro dput(victim); // unpin it 294a3d1e7ebSAl Viro } 295a3d1e7ebSAl Viro if (victim == dentry) { 296a3d1e7ebSAl Viro inode->i_ctime = inode->i_mtime = 297a3d1e7ebSAl Viro current_time(inode); 298a3d1e7ebSAl Viro if (d_is_dir(dentry)) 299a3d1e7ebSAl Viro drop_nlink(inode); 300a3d1e7ebSAl Viro inode_unlock(inode); 301a3d1e7ebSAl Viro dput(dentry); 302a3d1e7ebSAl Viro return; 303a3d1e7ebSAl Viro } 304a3d1e7ebSAl Viro } 305a3d1e7ebSAl Viro inode_unlock(inode); 306a3d1e7ebSAl Viro this = child; 307a3d1e7ebSAl Viro } 308a3d1e7ebSAl Viro } 309a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal); 310a3d1e7ebSAl Viro 311759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 312759b9775SHugh Dickins .statfs = simple_statfs, 313759b9775SHugh Dickins }; 314759b9775SHugh Dickins 315db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc) 3161da177e4SLinus Torvalds { 31731d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private; 3181da177e4SLinus Torvalds struct inode *root; 3191da177e4SLinus Torvalds 32089a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 3213971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 3223971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 3238d9e46d8SAl Viro s->s_magic = ctx->magic; 3248d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations; 3258d9e46d8SAl Viro s->s_xattr = ctx->xattr; 3261da177e4SLinus Torvalds s->s_time_gran = 1; 3271da177e4SLinus Torvalds root = new_inode(s); 3281da177e4SLinus Torvalds if (!root) 329db2c246aSDavid Howells return -ENOMEM; 330db2c246aSDavid Howells 3311a1c9bb4SJeff Layton /* 3321a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 3331a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 3341a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 3351a1c9bb4SJeff Layton */ 3361a1c9bb4SJeff Layton root->i_ino = 1; 3371da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 338078cd827SDeepa Dinamani root->i_atime = root->i_mtime = root->i_ctime = current_time(root); 3398d9e46d8SAl Viro s->s_root = d_make_root(root); 3408d9e46d8SAl Viro if (!s->s_root) 3418d9e46d8SAl Viro return -ENOMEM; 342db2c246aSDavid Howells s->s_d_op = ctx->dops; 343db2c246aSDavid Howells return 0; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 346db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc) 347db2c246aSDavid Howells { 3482ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super); 3491da177e4SLinus Torvalds } 35031d6d5ceSDavid Howells 35131d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc) 35231d6d5ceSDavid Howells { 35331d6d5ceSDavid Howells kfree(fc->fs_private); 35431d6d5ceSDavid Howells } 35531d6d5ceSDavid Howells 35631d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = { 35731d6d5ceSDavid Howells .free = pseudo_fs_free, 35831d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree, 35931d6d5ceSDavid Howells }; 36031d6d5ceSDavid Howells 36131d6d5ceSDavid Howells /* 36231d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 36331d6d5ceSDavid Howells * will never be mountable) 36431d6d5ceSDavid Howells */ 36531d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc, 36631d6d5ceSDavid Howells unsigned long magic) 36731d6d5ceSDavid Howells { 36831d6d5ceSDavid Howells struct pseudo_fs_context *ctx; 36931d6d5ceSDavid Howells 37031d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL); 37131d6d5ceSDavid Howells if (likely(ctx)) { 37231d6d5ceSDavid Howells ctx->magic = magic; 37331d6d5ceSDavid Howells fc->fs_private = ctx; 37431d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops; 375db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER; 376db2c246aSDavid Howells fc->global = true; 37731d6d5ceSDavid Howells } 37831d6d5ceSDavid Howells return ctx; 37931d6d5ceSDavid Howells } 38031d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo); 3811da177e4SLinus Torvalds 38220955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 38320955e89SStephen Boyd { 38420955e89SStephen Boyd if (inode->i_private) 38520955e89SStephen Boyd file->private_data = inode->i_private; 38620955e89SStephen Boyd return 0; 38720955e89SStephen Boyd } 38812f38872SAl Viro EXPORT_SYMBOL(simple_open); 38920955e89SStephen Boyd 3901da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 3911da177e4SLinus Torvalds { 392dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 3931da177e4SLinus Torvalds 394078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 395d8c76e6fSDave Hansen inc_nlink(inode); 3967de9c6eeSAl Viro ihold(inode); 3971da177e4SLinus Torvalds dget(dentry); 3981da177e4SLinus Torvalds d_instantiate(dentry, inode); 3991da177e4SLinus Torvalds return 0; 4001da177e4SLinus Torvalds } 40112f38872SAl Viro EXPORT_SYMBOL(simple_link); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 4041da177e4SLinus Torvalds { 4051da177e4SLinus Torvalds struct dentry *child; 4061da177e4SLinus Torvalds int ret = 0; 4071da177e4SLinus Torvalds 4082fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 409946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) { 410da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 411da502956SNick Piggin if (simple_positive(child)) { 412da502956SNick Piggin spin_unlock(&child->d_lock); 4131da177e4SLinus Torvalds goto out; 414da502956SNick Piggin } 415da502956SNick Piggin spin_unlock(&child->d_lock); 416da502956SNick Piggin } 4171da177e4SLinus Torvalds ret = 1; 4181da177e4SLinus Torvalds out: 4192fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 4201da177e4SLinus Torvalds return ret; 4211da177e4SLinus Torvalds } 42212f38872SAl Viro EXPORT_SYMBOL(simple_empty); 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 4251da177e4SLinus Torvalds { 426dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 4271da177e4SLinus Torvalds 428078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 4299a53c3a7SDave Hansen drop_nlink(inode); 4301da177e4SLinus Torvalds dput(dentry); 4311da177e4SLinus Torvalds return 0; 4321da177e4SLinus Torvalds } 43312f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds if (!simple_empty(dentry)) 4381da177e4SLinus Torvalds return -ENOTEMPTY; 4391da177e4SLinus Torvalds 440dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 4411da177e4SLinus Torvalds simple_unlink(dir, dentry); 4429a53c3a7SDave Hansen drop_nlink(dir); 4431da177e4SLinus Torvalds return 0; 4441da177e4SLinus Torvalds } 44512f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 4461da177e4SLinus Torvalds 447*0c476792SJeff Layton /** 448*0c476792SJeff Layton * simple_rename_timestamp - update the various inode timestamps for rename 449*0c476792SJeff Layton * @old_dir: old parent directory 450*0c476792SJeff Layton * @old_dentry: dentry that is being renamed 451*0c476792SJeff Layton * @new_dir: new parent directory 452*0c476792SJeff Layton * @new_dentry: target for rename 453*0c476792SJeff Layton * 454*0c476792SJeff Layton * POSIX mandates that the old and new parent directories have their ctime and 455*0c476792SJeff Layton * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have 456*0c476792SJeff Layton * their ctime updated. 457*0c476792SJeff Layton */ 458*0c476792SJeff Layton void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry, 459*0c476792SJeff Layton struct inode *new_dir, struct dentry *new_dentry) 460*0c476792SJeff Layton { 461*0c476792SJeff Layton struct inode *newino = d_inode(new_dentry); 462*0c476792SJeff Layton 463*0c476792SJeff Layton old_dir->i_mtime = inode_set_ctime_current(old_dir); 464*0c476792SJeff Layton if (new_dir != old_dir) 465*0c476792SJeff Layton new_dir->i_mtime = inode_set_ctime_current(new_dir); 466*0c476792SJeff Layton inode_set_ctime_current(d_inode(old_dentry)); 467*0c476792SJeff Layton if (newino) 468*0c476792SJeff Layton inode_set_ctime_current(newino); 469*0c476792SJeff Layton } 470*0c476792SJeff Layton EXPORT_SYMBOL_GPL(simple_rename_timestamp); 471*0c476792SJeff Layton 4726429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry, 4736429e463SLorenz Bauer struct inode *new_dir, struct dentry *new_dentry) 4746429e463SLorenz Bauer { 4756429e463SLorenz Bauer bool old_is_dir = d_is_dir(old_dentry); 4766429e463SLorenz Bauer bool new_is_dir = d_is_dir(new_dentry); 4776429e463SLorenz Bauer 4786429e463SLorenz Bauer if (old_dir != new_dir && old_is_dir != new_is_dir) { 4796429e463SLorenz Bauer if (old_is_dir) { 4806429e463SLorenz Bauer drop_nlink(old_dir); 4816429e463SLorenz Bauer inc_nlink(new_dir); 4826429e463SLorenz Bauer } else { 4836429e463SLorenz Bauer drop_nlink(new_dir); 4846429e463SLorenz Bauer inc_nlink(old_dir); 4856429e463SLorenz Bauer } 4866429e463SLorenz Bauer } 487*0c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 4886429e463SLorenz Bauer return 0; 4896429e463SLorenz Bauer } 4906429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange); 4916429e463SLorenz Bauer 492e18275aeSChristian Brauner int simple_rename(struct mnt_idmap *idmap, struct inode *old_dir, 493549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 494549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 4951da177e4SLinus Torvalds { 496e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 4971da177e4SLinus Torvalds 4983871cb8cSLorenz Bauer if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 499e0e0be8aSMiklos Szeredi return -EINVAL; 500e0e0be8aSMiklos Szeredi 5013871cb8cSLorenz Bauer if (flags & RENAME_EXCHANGE) 5023871cb8cSLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 5033871cb8cSLorenz Bauer 5041da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 5051da177e4SLinus Torvalds return -ENOTEMPTY; 5061da177e4SLinus Torvalds 507dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 5081da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 509841590ceSAl Viro if (they_are_dirs) { 510dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 5119a53c3a7SDave Hansen drop_nlink(old_dir); 512841590ceSAl Viro } 5131da177e4SLinus Torvalds } else if (they_are_dirs) { 5149a53c3a7SDave Hansen drop_nlink(old_dir); 515d8c76e6fSDave Hansen inc_nlink(new_dir); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 518*0c476792SJeff Layton simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 5191da177e4SLinus Torvalds return 0; 5201da177e4SLinus Torvalds } 52112f38872SAl Viro EXPORT_SYMBOL(simple_rename); 5221da177e4SLinus Torvalds 5237bb46a67Snpiggin@suse.de /** 524eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 525c1632a0fSChristian Brauner * @idmap: idmap of the target mount 5267bb46a67Snpiggin@suse.de * @dentry: dentry 5277bb46a67Snpiggin@suse.de * @iattr: iattr structure 5287bb46a67Snpiggin@suse.de * 5297bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 5307bb46a67Snpiggin@suse.de * 531eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 532eef2380cSChristoph Hellwig * implementation of size changes. 533eef2380cSChristoph Hellwig * 534eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 535eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 536eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 5377bb46a67Snpiggin@suse.de */ 538c1632a0fSChristian Brauner int simple_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 539549c7297SChristian Brauner struct iattr *iattr) 5407bb46a67Snpiggin@suse.de { 541dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 5427bb46a67Snpiggin@suse.de int error; 5437bb46a67Snpiggin@suse.de 544c1632a0fSChristian Brauner error = setattr_prepare(idmap, dentry, iattr); 5457bb46a67Snpiggin@suse.de if (error) 5467bb46a67Snpiggin@suse.de return error; 5477bb46a67Snpiggin@suse.de 5482c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 5492c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 550c1632a0fSChristian Brauner setattr_copy(idmap, inode, iattr); 551eef2380cSChristoph Hellwig mark_inode_dirty(inode); 552eef2380cSChristoph Hellwig return 0; 5537bb46a67Snpiggin@suse.de } 5547bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 5557bb46a67Snpiggin@suse.de 556a77f580aSMatthew Wilcox (Oracle) static int simple_read_folio(struct file *file, struct folio *folio) 5571da177e4SLinus Torvalds { 558a77f580aSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 559a77f580aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 560a77f580aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 561a77f580aSMatthew Wilcox (Oracle) folio_unlock(folio); 5621da177e4SLinus Torvalds return 0; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 565afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 5669d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 567afddba49SNick Piggin struct page **pagep, void **fsdata) 568afddba49SNick Piggin { 569afddba49SNick Piggin struct page *page; 570afddba49SNick Piggin pgoff_t index; 571afddba49SNick Piggin 57209cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 573afddba49SNick Piggin 574b7446e7cSMatthew Wilcox (Oracle) page = grab_cache_page_write_begin(mapping, index); 575afddba49SNick Piggin if (!page) 576afddba49SNick Piggin return -ENOMEM; 577afddba49SNick Piggin 578afddba49SNick Piggin *pagep = page; 579afddba49SNick Piggin 58009cbfeafSKirill A. Shutemov if (!PageUptodate(page) && (len != PAGE_SIZE)) { 58109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 582193cf4b9SBoaz Harrosh 58309cbfeafSKirill A. Shutemov zero_user_segments(page, 0, from, from + len, PAGE_SIZE); 584193cf4b9SBoaz Harrosh } 585193cf4b9SBoaz Harrosh return 0; 586afddba49SNick Piggin } 58712f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 588afddba49SNick Piggin 589ad2a722fSBoaz Harrosh /** 590ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 5918e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations 592ad2a722fSBoaz Harrosh * @mapping: " 593ad2a722fSBoaz Harrosh * @pos: " 594ad2a722fSBoaz Harrosh * @len: " 595ad2a722fSBoaz Harrosh * @copied: " 596ad2a722fSBoaz Harrosh * @page: " 597ad2a722fSBoaz Harrosh * @fsdata: " 598ad2a722fSBoaz Harrosh * 599ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 600ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 601ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 602ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 603ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 604ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 605ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 606ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 607ad2a722fSBoaz Harrosh * case that i_size has changed. 60804fff641SAl Viro * 609a77f580aSMatthew Wilcox (Oracle) * Use *ONLY* with simple_read_folio() 610ad2a722fSBoaz Harrosh */ 611c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping, 612ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 613ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 616ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 617ad2a722fSBoaz Harrosh 618ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 61904fff641SAl Viro if (!PageUptodate(page)) { 620ad2a722fSBoaz Harrosh if (copied < len) { 62109cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 622ad2a722fSBoaz Harrosh 623ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 624ad2a722fSBoaz Harrosh } 625955eff5aSNick Piggin SetPageUptodate(page); 62604fff641SAl Viro } 6271da177e4SLinus Torvalds /* 6281da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 6291b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 6301da177e4SLinus Torvalds */ 631ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 632ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 633ad2a722fSBoaz Harrosh 6341da177e4SLinus Torvalds set_page_dirty(page); 635afddba49SNick Piggin unlock_page(page); 63609cbfeafSKirill A. Shutemov put_page(page); 637afddba49SNick Piggin 638afddba49SNick Piggin return copied; 639afddba49SNick Piggin } 640c1e3dbe9SChristoph Hellwig 641c1e3dbe9SChristoph Hellwig /* 642c1e3dbe9SChristoph Hellwig * Provides ramfs-style behavior: data in the pagecache, but no writeback. 643c1e3dbe9SChristoph Hellwig */ 644c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = { 645a77f580aSMatthew Wilcox (Oracle) .read_folio = simple_read_folio, 646c1e3dbe9SChristoph Hellwig .write_begin = simple_write_begin, 647c1e3dbe9SChristoph Hellwig .write_end = simple_write_end, 64846de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 649c1e3dbe9SChristoph Hellwig }; 650c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops); 651afddba49SNick Piggin 6521a1c9bb4SJeff Layton /* 6531a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 6541a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 6551a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 6561a1c9bb4SJeff Layton */ 6577d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 658cda37124SEric Biggers const struct tree_descr *files) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct inode *inode; 6611da177e4SLinus Torvalds struct dentry *root; 6621da177e4SLinus Torvalds struct dentry *dentry; 6631da177e4SLinus Torvalds int i; 6641da177e4SLinus Torvalds 66509cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE; 66609cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT; 6671da177e4SLinus Torvalds s->s_magic = magic; 668759b9775SHugh Dickins s->s_op = &simple_super_operations; 6691da177e4SLinus Torvalds s->s_time_gran = 1; 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds inode = new_inode(s); 6721da177e4SLinus Torvalds if (!inode) 6731da177e4SLinus Torvalds return -ENOMEM; 6741a1c9bb4SJeff Layton /* 6751a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 6761a1c9bb4SJeff Layton * entry at index 1 6771a1c9bb4SJeff Layton */ 6781a1c9bb4SJeff Layton inode->i_ino = 1; 6791da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 680078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 6811da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 6821da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 683bfe86848SMiklos Szeredi set_nlink(inode, 2); 68448fde701SAl Viro root = d_make_root(inode); 68548fde701SAl Viro if (!root) 6861da177e4SLinus Torvalds return -ENOMEM; 6871da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 6881da177e4SLinus Torvalds if (!files->name) 6891da177e4SLinus Torvalds continue; 6901a1c9bb4SJeff Layton 6911a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 6921a1c9bb4SJeff Layton if (unlikely(i == 1)) 6931a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 6941a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 6951a1c9bb4SJeff Layton s->s_type->name); 6961a1c9bb4SJeff Layton 6971da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 6981da177e4SLinus Torvalds if (!dentry) 6991da177e4SLinus Torvalds goto out; 7001da177e4SLinus Torvalds inode = new_inode(s); 70132096ea1SKonstantin Khlebnikov if (!inode) { 70232096ea1SKonstantin Khlebnikov dput(dentry); 7031da177e4SLinus Torvalds goto out; 70432096ea1SKonstantin Khlebnikov } 7051da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 706078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 7071da177e4SLinus Torvalds inode->i_fop = files->ops; 7081da177e4SLinus Torvalds inode->i_ino = i; 7091da177e4SLinus Torvalds d_add(dentry, inode); 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds s->s_root = root; 7121da177e4SLinus Torvalds return 0; 7131da177e4SLinus Torvalds out: 7141da177e4SLinus Torvalds d_genocide(root); 715640946f2SAl Viro shrink_dcache_parent(root); 7161da177e4SLinus Torvalds dput(root); 7171da177e4SLinus Torvalds return -ENOMEM; 7181da177e4SLinus Torvalds } 71912f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 7221da177e4SLinus Torvalds 7231f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 7241da177e4SLinus Torvalds { 7251da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 7261da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 7271da177e4SLinus Torvalds if (unlikely(!*mount)) { 7281da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 7291751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 7301da177e4SLinus Torvalds if (IS_ERR(mnt)) 7311da177e4SLinus Torvalds return PTR_ERR(mnt); 7321da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 7331da177e4SLinus Torvalds if (!*mount) 7341da177e4SLinus Torvalds *mount = mnt; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds mntget(*mount); 7371da177e4SLinus Torvalds ++*count; 7381da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 7391da177e4SLinus Torvalds mntput(mnt); 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds } 74212f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 7451da177e4SLinus Torvalds { 7461da177e4SLinus Torvalds struct vfsmount *mnt; 7471da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 7481da177e4SLinus Torvalds mnt = *mount; 7491da177e4SLinus Torvalds if (!--*count) 7501da177e4SLinus Torvalds *mount = NULL; 7511da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 7521da177e4SLinus Torvalds mntput(mnt); 7531da177e4SLinus Torvalds } 75412f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 7551da177e4SLinus Torvalds 7566d1029b5SAkinobu Mita /** 7576d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 7586d1029b5SAkinobu Mita * @to: the user space buffer to read to 7596d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 7606d1029b5SAkinobu Mita * @ppos: the current position in the buffer 7616d1029b5SAkinobu Mita * @from: the buffer to read from 7626d1029b5SAkinobu Mita * @available: the size of the buffer 7636d1029b5SAkinobu Mita * 7646d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 7656d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 7666d1029b5SAkinobu Mita * 7676d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 7686d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 7696d1029b5SAkinobu Mita **/ 7701da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 7711da177e4SLinus Torvalds const void *from, size_t available) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds loff_t pos = *ppos; 77414be2746SSteven Rostedt size_t ret; 77514be2746SSteven Rostedt 7761da177e4SLinus Torvalds if (pos < 0) 7771da177e4SLinus Torvalds return -EINVAL; 77814be2746SSteven Rostedt if (pos >= available || !count) 7791da177e4SLinus Torvalds return 0; 7801da177e4SLinus Torvalds if (count > available - pos) 7811da177e4SLinus Torvalds count = available - pos; 78214be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 78314be2746SSteven Rostedt if (ret == count) 7841da177e4SLinus Torvalds return -EFAULT; 78514be2746SSteven Rostedt count -= ret; 7861da177e4SLinus Torvalds *ppos = pos + count; 7871da177e4SLinus Torvalds return count; 7881da177e4SLinus Torvalds } 78912f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 7901da177e4SLinus Torvalds 7916d1029b5SAkinobu Mita /** 7926a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 7936a727b43SJiri Slaby * @to: the buffer to write to 7946a727b43SJiri Slaby * @available: the size of the buffer 7956a727b43SJiri Slaby * @ppos: the current position in the buffer 7966a727b43SJiri Slaby * @from: the user space buffer to read from 7976a727b43SJiri Slaby * @count: the maximum number of bytes to read 7986a727b43SJiri Slaby * 7996a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 8006a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 8016a727b43SJiri Slaby * 8026a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 8036a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 8046a727b43SJiri Slaby **/ 8056a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 8066a727b43SJiri Slaby const void __user *from, size_t count) 8076a727b43SJiri Slaby { 8086a727b43SJiri Slaby loff_t pos = *ppos; 8096a727b43SJiri Slaby size_t res; 8106a727b43SJiri Slaby 8116a727b43SJiri Slaby if (pos < 0) 8126a727b43SJiri Slaby return -EINVAL; 8136a727b43SJiri Slaby if (pos >= available || !count) 8146a727b43SJiri Slaby return 0; 8156a727b43SJiri Slaby if (count > available - pos) 8166a727b43SJiri Slaby count = available - pos; 8176a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 8186a727b43SJiri Slaby if (res == count) 8196a727b43SJiri Slaby return -EFAULT; 8206a727b43SJiri Slaby count -= res; 8216a727b43SJiri Slaby *ppos = pos + count; 8226a727b43SJiri Slaby return count; 8236a727b43SJiri Slaby } 82412f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 8256a727b43SJiri Slaby 8266a727b43SJiri Slaby /** 8276d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 8286d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 8296d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 8306d1029b5SAkinobu Mita * @ppos: the current position in the buffer 8316d1029b5SAkinobu Mita * @from: the buffer to read from 8326d1029b5SAkinobu Mita * @available: the size of the buffer 8336d1029b5SAkinobu Mita * 8346d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 8356d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 8366d1029b5SAkinobu Mita * 8376d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 8386d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 8396d1029b5SAkinobu Mita **/ 84093b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 84193b07113SAkinobu Mita const void *from, size_t available) 84293b07113SAkinobu Mita { 84393b07113SAkinobu Mita loff_t pos = *ppos; 84493b07113SAkinobu Mita 84593b07113SAkinobu Mita if (pos < 0) 84693b07113SAkinobu Mita return -EINVAL; 84793b07113SAkinobu Mita if (pos >= available) 84893b07113SAkinobu Mita return 0; 84993b07113SAkinobu Mita if (count > available - pos) 85093b07113SAkinobu Mita count = available - pos; 85193b07113SAkinobu Mita memcpy(to, from + pos, count); 85293b07113SAkinobu Mita *ppos = pos + count; 85393b07113SAkinobu Mita 85493b07113SAkinobu Mita return count; 85593b07113SAkinobu Mita } 85612f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 85793b07113SAkinobu Mita 8581da177e4SLinus Torvalds /* 8591da177e4SLinus Torvalds * Transaction based IO. 8601da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 8611da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 8621da177e4SLinus Torvalds * file-local buffer. 8631da177e4SLinus Torvalds */ 86476791ab2SIngo Molnar 86576791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 86676791ab2SIngo Molnar { 86776791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 86876791ab2SIngo Molnar 86976791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 87076791ab2SIngo Molnar 87176791ab2SIngo Molnar /* 87276791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 87376791ab2SIngo Molnar * ar->data is ready for reading. 87476791ab2SIngo Molnar */ 87576791ab2SIngo Molnar smp_mb(); 87676791ab2SIngo Molnar ar->size = n; 87776791ab2SIngo Molnar } 87812f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 87976791ab2SIngo Molnar 8801da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 8811da177e4SLinus Torvalds { 8821da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 8831da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 8861da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 8891da177e4SLinus Torvalds if (!ar) 8901da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* only one write allowed per open */ 8951da177e4SLinus Torvalds if (file->private_data) { 8961da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 8971da177e4SLinus Torvalds free_page((unsigned long)ar); 8981da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds file->private_data = ar; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 9061da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds return ar->data; 9091da177e4SLinus Torvalds } 91012f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 9131da177e4SLinus Torvalds { 9141da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 9151da177e4SLinus Torvalds 9161da177e4SLinus Torvalds if (!ar) 9171da177e4SLinus Torvalds return 0; 9181da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 9191da177e4SLinus Torvalds } 92012f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 9231da177e4SLinus Torvalds { 9241da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 9251da177e4SLinus Torvalds return 0; 9261da177e4SLinus Torvalds } 92712f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 9281da177e4SLinus Torvalds 929acaefc25SArnd Bergmann /* Simple attribute files */ 930acaefc25SArnd Bergmann 931acaefc25SArnd Bergmann struct simple_attr { 9328b88b099SChristoph Hellwig int (*get)(void *, u64 *); 9338b88b099SChristoph Hellwig int (*set)(void *, u64); 934acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 935acaefc25SArnd Bergmann char set_buf[24]; 936acaefc25SArnd Bergmann void *data; 937acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 9387cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 939acaefc25SArnd Bergmann }; 940acaefc25SArnd Bergmann 941acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 942acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 943acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 9448b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 945acaefc25SArnd Bergmann const char *fmt) 946acaefc25SArnd Bergmann { 947acaefc25SArnd Bergmann struct simple_attr *attr; 948acaefc25SArnd Bergmann 949a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL); 950acaefc25SArnd Bergmann if (!attr) 951acaefc25SArnd Bergmann return -ENOMEM; 952acaefc25SArnd Bergmann 953acaefc25SArnd Bergmann attr->get = get; 954acaefc25SArnd Bergmann attr->set = set; 9558e18e294STheodore Ts'o attr->data = inode->i_private; 956acaefc25SArnd Bergmann attr->fmt = fmt; 9577cf34c76SIngo Molnar mutex_init(&attr->mutex); 958acaefc25SArnd Bergmann 959acaefc25SArnd Bergmann file->private_data = attr; 960acaefc25SArnd Bergmann 961acaefc25SArnd Bergmann return nonseekable_open(inode, file); 962acaefc25SArnd Bergmann } 96312f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 964acaefc25SArnd Bergmann 96574bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 966acaefc25SArnd Bergmann { 967acaefc25SArnd Bergmann kfree(file->private_data); 968acaefc25SArnd Bergmann return 0; 969acaefc25SArnd Bergmann } 97012f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 971acaefc25SArnd Bergmann 972acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 973acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 974acaefc25SArnd Bergmann size_t len, loff_t *ppos) 975acaefc25SArnd Bergmann { 976acaefc25SArnd Bergmann struct simple_attr *attr; 977acaefc25SArnd Bergmann size_t size; 978acaefc25SArnd Bergmann ssize_t ret; 979acaefc25SArnd Bergmann 980acaefc25SArnd Bergmann attr = file->private_data; 981acaefc25SArnd Bergmann 982acaefc25SArnd Bergmann if (!attr->get) 983acaefc25SArnd Bergmann return -EACCES; 984acaefc25SArnd Bergmann 9859261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 9869261303aSChristoph Hellwig if (ret) 9879261303aSChristoph Hellwig return ret; 9889261303aSChristoph Hellwig 989a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) { 990a65cab7dSEric Biggers /* continued read */ 991acaefc25SArnd Bergmann size = strlen(attr->get_buf); 992a65cab7dSEric Biggers } else { 993a65cab7dSEric Biggers /* first read */ 9948b88b099SChristoph Hellwig u64 val; 9958b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 9968b88b099SChristoph Hellwig if (ret) 9978b88b099SChristoph Hellwig goto out; 9988b88b099SChristoph Hellwig 999acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 10008b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 10018b88b099SChristoph Hellwig } 1002acaefc25SArnd Bergmann 1003acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 10048b88b099SChristoph Hellwig out: 10057cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1006acaefc25SArnd Bergmann return ret; 1007acaefc25SArnd Bergmann } 100812f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 1009acaefc25SArnd Bergmann 1010acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 10112e41f274SAkinobu Mita static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf, 10122e41f274SAkinobu Mita size_t len, loff_t *ppos, bool is_signed) 1013acaefc25SArnd Bergmann { 1014acaefc25SArnd Bergmann struct simple_attr *attr; 1015488dac0cSYicong Yang unsigned long long val; 1016acaefc25SArnd Bergmann size_t size; 1017acaefc25SArnd Bergmann ssize_t ret; 1018acaefc25SArnd Bergmann 1019acaefc25SArnd Bergmann attr = file->private_data; 1020acaefc25SArnd Bergmann if (!attr->set) 1021acaefc25SArnd Bergmann return -EACCES; 1022acaefc25SArnd Bergmann 10239261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 10249261303aSChristoph Hellwig if (ret) 10259261303aSChristoph Hellwig return ret; 10269261303aSChristoph Hellwig 1027acaefc25SArnd Bergmann ret = -EFAULT; 1028acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 1029acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 1030acaefc25SArnd Bergmann goto out; 1031acaefc25SArnd Bergmann 1032acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 10332e41f274SAkinobu Mita if (is_signed) 10342e41f274SAkinobu Mita ret = kstrtoll(attr->set_buf, 0, &val); 10352e41f274SAkinobu Mita else 1036488dac0cSYicong Yang ret = kstrtoull(attr->set_buf, 0, &val); 1037488dac0cSYicong Yang if (ret) 1038488dac0cSYicong Yang goto out; 103905cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 104005cc0ceeSWu Fengguang if (ret == 0) 104105cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 1042acaefc25SArnd Bergmann out: 10437cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 1044acaefc25SArnd Bergmann return ret; 1045acaefc25SArnd Bergmann } 10462e41f274SAkinobu Mita 10472e41f274SAkinobu Mita ssize_t simple_attr_write(struct file *file, const char __user *buf, 10482e41f274SAkinobu Mita size_t len, loff_t *ppos) 10492e41f274SAkinobu Mita { 10502e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, false); 10512e41f274SAkinobu Mita } 105212f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 1053acaefc25SArnd Bergmann 10542e41f274SAkinobu Mita ssize_t simple_attr_write_signed(struct file *file, const char __user *buf, 10552e41f274SAkinobu Mita size_t len, loff_t *ppos) 10562e41f274SAkinobu Mita { 10572e41f274SAkinobu Mita return simple_attr_write_xsigned(file, buf, len, ppos, true); 10582e41f274SAkinobu Mita } 10592e41f274SAkinobu Mita EXPORT_SYMBOL_GPL(simple_attr_write_signed); 10602e41f274SAkinobu Mita 10612596110aSChristoph Hellwig /** 10622596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 10632596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 10642596110aSChristoph Hellwig * @fid: file handle to convert 10652596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 10662596110aSChristoph Hellwig * @fh_type: type of file handle 10672596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 10682596110aSChristoph Hellwig * 10692596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 10702596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 10712596110aSChristoph Hellwig * inode for the object specified in the file handle. 10722596110aSChristoph Hellwig */ 10732596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 10742596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 10752596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 10762596110aSChristoph Hellwig { 10772596110aSChristoph Hellwig struct inode *inode = NULL; 10782596110aSChristoph Hellwig 10792596110aSChristoph Hellwig if (fh_len < 2) 10802596110aSChristoph Hellwig return NULL; 10812596110aSChristoph Hellwig 10822596110aSChristoph Hellwig switch (fh_type) { 10832596110aSChristoph Hellwig case FILEID_INO32_GEN: 10842596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 10852596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 10862596110aSChristoph Hellwig break; 10872596110aSChristoph Hellwig } 10882596110aSChristoph Hellwig 10894ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 10902596110aSChristoph Hellwig } 10912596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 10922596110aSChristoph Hellwig 10932596110aSChristoph Hellwig /** 1094ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 10952596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 10962596110aSChristoph Hellwig * @fid: file handle to convert 10972596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 10982596110aSChristoph Hellwig * @fh_type: type of file handle 10992596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 11002596110aSChristoph Hellwig * 11012596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 11022596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 11032596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 11042596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 11052596110aSChristoph Hellwig */ 11062596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 11072596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 11082596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 11092596110aSChristoph Hellwig { 11102596110aSChristoph Hellwig struct inode *inode = NULL; 11112596110aSChristoph Hellwig 11122596110aSChristoph Hellwig if (fh_len <= 2) 11132596110aSChristoph Hellwig return NULL; 11142596110aSChristoph Hellwig 11152596110aSChristoph Hellwig switch (fh_type) { 11162596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 11172596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 11182596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 11192596110aSChristoph Hellwig break; 11202596110aSChristoph Hellwig } 11212596110aSChristoph Hellwig 11224ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 11232596110aSChristoph Hellwig } 11242596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 11252596110aSChristoph Hellwig 11261b061d92SChristoph Hellwig /** 1127ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 1128ac13a829SFabian Frederick * 11291b061d92SChristoph Hellwig * @file: file to synchronize 1130ac13a829SFabian Frederick * @start: start offset in bytes 1131ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 11321b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 11331b061d92SChristoph Hellwig * 11341b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 11351b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 11361b061d92SChristoph Hellwig * hanging off the address_space structure. 11371b061d92SChristoph Hellwig */ 1138ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 113902c24a82SJosef Bacik int datasync) 1140d5aacad5SAl Viro { 11417ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 1142d5aacad5SAl Viro int err; 1143d5aacad5SAl Viro int ret; 1144d5aacad5SAl Viro 1145383aa543SJeff Layton err = file_write_and_wait_range(file, start, end); 114602c24a82SJosef Bacik if (err) 114702c24a82SJosef Bacik return err; 114802c24a82SJosef Bacik 11495955102cSAl Viro inode_lock(inode); 1150d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 11510ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 115202c24a82SJosef Bacik goto out; 1153d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 115402c24a82SJosef Bacik goto out; 1155d5aacad5SAl Viro 1156c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 1157d5aacad5SAl Viro if (ret == 0) 1158d5aacad5SAl Viro ret = err; 1159ac13a829SFabian Frederick 116002c24a82SJosef Bacik out: 11615955102cSAl Viro inode_unlock(inode); 1162383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */ 1163383aa543SJeff Layton err = file_check_and_advance_wb_err(file); 1164383aa543SJeff Layton if (ret == 0) 1165383aa543SJeff Layton ret = err; 1166383aa543SJeff Layton return ret; 1167d5aacad5SAl Viro } 1168ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 1169ac13a829SFabian Frederick 1170ac13a829SFabian Frederick /** 1171ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 1172ac13a829SFabian Frederick * with flush 1173ac13a829SFabian Frederick * @file: file to synchronize 1174ac13a829SFabian Frederick * @start: start offset in bytes 1175ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 1176ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 1177ac13a829SFabian Frederick * 1178ac13a829SFabian Frederick */ 1179ac13a829SFabian Frederick 1180ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 1181ac13a829SFabian Frederick int datasync) 1182ac13a829SFabian Frederick { 1183ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 1184ac13a829SFabian Frederick int err; 1185ac13a829SFabian Frederick 1186ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 1187ac13a829SFabian Frederick if (err) 1188ac13a829SFabian Frederick return err; 1189c6bf3f0eSChristoph Hellwig return blkdev_issue_flush(inode->i_sb->s_bdev); 1190ac13a829SFabian Frederick } 11911b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 11921b061d92SChristoph Hellwig 119330ca22c7SPatrick J. LoPresti /** 119430ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 119530ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 119630ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 119730ca22c7SPatrick J. LoPresti * 119830ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 119930ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 120030ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 120130ca22c7SPatrick J. LoPresti */ 120230ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 120330ca22c7SPatrick J. LoPresti { 120430ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 1205a33f13efSJoel Becker u64 last_fs_page = 120609cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits); 120730ca22c7SPatrick J. LoPresti 120830ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 120930ca22c7SPatrick J. LoPresti return 0; 121030ca22c7SPatrick J. LoPresti 121109cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT)) 121230ca22c7SPatrick J. LoPresti return -EINVAL; 121330ca22c7SPatrick J. LoPresti 1214a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1215a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 121630ca22c7SPatrick J. LoPresti return -EFBIG; 121730ca22c7SPatrick J. LoPresti } 121830ca22c7SPatrick J. LoPresti return 0; 121930ca22c7SPatrick J. LoPresti } 122030ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 122130ca22c7SPatrick J. LoPresti 12221b061d92SChristoph Hellwig /* 12231b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 12241b061d92SChristoph Hellwig */ 122502c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 12261b061d92SChristoph Hellwig { 12271b061d92SChristoph Hellwig return 0; 12281b061d92SChristoph Hellwig } 12291b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 123087dc800bSAl Viro 1231f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 1232f44c7763SDan Williams { 1233f44c7763SDan Williams /* 1234f44c7763SDan Williams * iomap based filesystems support direct I/O without need for 1235f44c7763SDan Williams * this callback. However, it still needs to be set in 1236f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is 1237f44c7763SDan Williams * generally supported. 1238f44c7763SDan Williams */ 1239f44c7763SDan Williams return -EINVAL; 1240f44c7763SDan Williams } 1241f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO); 1242f44c7763SDan Williams 1243fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */ 1244fceef393SAl Viro void kfree_link(void *p) 124587dc800bSAl Viro { 1246fceef393SAl Viro kfree(p); 124787dc800bSAl Viro } 1248fceef393SAl Viro EXPORT_SYMBOL(kfree_link); 12496987843fSAl Viro 12506987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 12516987843fSAl Viro { 12526987843fSAl Viro static const struct address_space_operations anon_aops = { 125346de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 12546987843fSAl Viro }; 12556987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 12566987843fSAl Viro 12576987843fSAl Viro if (!inode) 12586987843fSAl Viro return ERR_PTR(-ENOMEM); 12596987843fSAl Viro 12606987843fSAl Viro inode->i_ino = get_next_ino(); 12616987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 12626987843fSAl Viro 12636987843fSAl Viro /* 12646987843fSAl Viro * Mark the inode dirty from the very beginning, 12656987843fSAl Viro * that way it will never be moved to the dirty 12666987843fSAl Viro * list because mark_inode_dirty() will think 12676987843fSAl Viro * that it already _is_ on the dirty list. 12686987843fSAl Viro */ 12696987843fSAl Viro inode->i_state = I_DIRTY; 12706987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 12716987843fSAl Viro inode->i_uid = current_fsuid(); 12726987843fSAl Viro inode->i_gid = current_fsgid(); 12736987843fSAl Viro inode->i_flags |= S_PRIVATE; 1274078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 12756987843fSAl Viro return inode; 12766987843fSAl Viro } 12776987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 12781c994a09SJeff Layton 12791c994a09SJeff Layton /** 12801c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 12811c994a09SJeff Layton * @filp: file pointer 12821c994a09SJeff Layton * @arg: type of lease to obtain 12831c994a09SJeff Layton * @flp: new lease supplied for insertion 1284e6f5c789SJeff Layton * @priv: private data for lm_setup operation 12851c994a09SJeff Layton * 12861c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 12871c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 12881c994a09SJeff Layton */ 12891c994a09SJeff Layton int 1290e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1291e6f5c789SJeff Layton void **priv) 12921c994a09SJeff Layton { 12931c994a09SJeff Layton return -EINVAL; 12941c994a09SJeff Layton } 12951c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 129661ba64fcSAl Viro 12976ee9706aSEric Biggers /** 12986ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks 12996ee9706aSEric Biggers * @dentry: not used here 13006ee9706aSEric Biggers * @inode: the symlink inode 13016ee9706aSEric Biggers * @done: not used here 13026ee9706aSEric Biggers * 13036ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to 13046ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called, 13056ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link 13066ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set, 13076ee9706aSEric Biggers * to mark the inode_operations as being for a symlink. 13086ee9706aSEric Biggers * 13096ee9706aSEric Biggers * Return: the symlink target 13106ee9706aSEric Biggers */ 13116b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 1312fceef393SAl Viro struct delayed_call *done) 131361ba64fcSAl Viro { 13146b255391SAl Viro return inode->i_link; 131561ba64fcSAl Viro } 13166b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 131761ba64fcSAl Viro 131861ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 13196b255391SAl Viro .get_link = simple_get_link, 132061ba64fcSAl Viro }; 132161ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1322fbabfd0fSEric W. Biederman 1323fbabfd0fSEric W. Biederman /* 1324fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1325fbabfd0fSEric W. Biederman */ 1326fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1327fbabfd0fSEric W. Biederman { 1328fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1329fbabfd0fSEric W. Biederman } 1330fbabfd0fSEric W. Biederman 1331b74d24f7SChristian Brauner static int empty_dir_getattr(struct mnt_idmap *idmap, 1332549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1333a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1334fbabfd0fSEric W. Biederman { 1335a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 1336b74d24f7SChristian Brauner generic_fillattr(&nop_mnt_idmap, inode, stat); 1337fbabfd0fSEric W. Biederman return 0; 1338fbabfd0fSEric W. Biederman } 1339fbabfd0fSEric W. Biederman 1340c1632a0fSChristian Brauner static int empty_dir_setattr(struct mnt_idmap *idmap, 1341549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 1342fbabfd0fSEric W. Biederman { 1343fbabfd0fSEric W. Biederman return -EPERM; 1344fbabfd0fSEric W. Biederman } 1345fbabfd0fSEric W. Biederman 1346fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1347fbabfd0fSEric W. Biederman { 1348fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1349fbabfd0fSEric W. Biederman } 1350fbabfd0fSEric W. Biederman 1351fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1352fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1353fbabfd0fSEric W. Biederman .permission = generic_permission, 1354fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1355fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1356fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1357fbabfd0fSEric W. Biederman }; 1358fbabfd0fSEric W. Biederman 1359fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1360fbabfd0fSEric W. Biederman { 1361fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1362fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1363fbabfd0fSEric W. Biederman } 1364fbabfd0fSEric W. Biederman 1365fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1366fbabfd0fSEric W. Biederman { 1367fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1368fbabfd0fSEric W. Biederman return 0; 1369fbabfd0fSEric W. Biederman } 1370fbabfd0fSEric W. Biederman 1371fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1372fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1373fbabfd0fSEric W. Biederman .read = generic_read_dir, 1374c51da20cSAl Viro .iterate_shared = empty_dir_readdir, 1375fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1376fbabfd0fSEric W. Biederman }; 1377fbabfd0fSEric W. Biederman 1378fbabfd0fSEric W. Biederman 1379fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1380fbabfd0fSEric W. Biederman { 1381fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1382fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1383fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1384fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1385fbabfd0fSEric W. Biederman inode->i_rdev = 0; 13864b75de86SEric W. Biederman inode->i_size = 0; 1387fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1388fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1389fbabfd0fSEric W. Biederman 1390fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1391f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR; 1392fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1393fbabfd0fSEric W. Biederman } 1394fbabfd0fSEric W. Biederman 1395fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1396fbabfd0fSEric W. Biederman { 1397fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1398fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1399fbabfd0fSEric W. Biederman } 1400c843843eSDaniel Rosenberg 14015298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1402c843843eSDaniel Rosenberg /* 1403c843843eSDaniel Rosenberg * Determine if the name of a dentry should be casefolded. 1404c843843eSDaniel Rosenberg * 1405c843843eSDaniel Rosenberg * Return: if names will need casefolding 1406c843843eSDaniel Rosenberg */ 1407c843843eSDaniel Rosenberg static bool needs_casefold(const struct inode *dir) 1408c843843eSDaniel Rosenberg { 1409c843843eSDaniel Rosenberg return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding; 1410c843843eSDaniel Rosenberg } 1411c843843eSDaniel Rosenberg 1412c843843eSDaniel Rosenberg /** 1413c843843eSDaniel Rosenberg * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems 1414c843843eSDaniel Rosenberg * @dentry: dentry whose name we are checking against 1415c843843eSDaniel Rosenberg * @len: len of name of dentry 1416c843843eSDaniel Rosenberg * @str: str pointer to name of dentry 1417c843843eSDaniel Rosenberg * @name: Name to compare against 1418c843843eSDaniel Rosenberg * 1419c843843eSDaniel Rosenberg * Return: 0 if names match, 1 if mismatch, or -ERRNO 1420c843843eSDaniel Rosenberg */ 1421794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len, 1422c843843eSDaniel Rosenberg const char *str, const struct qstr *name) 1423c843843eSDaniel Rosenberg { 1424c843843eSDaniel Rosenberg const struct dentry *parent = READ_ONCE(dentry->d_parent); 1425c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(parent->d_inode); 1426c843843eSDaniel Rosenberg const struct super_block *sb = dentry->d_sb; 1427c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1428c843843eSDaniel Rosenberg struct qstr qstr = QSTR_INIT(str, len); 1429c843843eSDaniel Rosenberg char strbuf[DNAME_INLINE_LEN]; 1430c843843eSDaniel Rosenberg int ret; 1431c843843eSDaniel Rosenberg 1432c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1433c843843eSDaniel Rosenberg goto fallback; 1434c843843eSDaniel Rosenberg /* 1435c843843eSDaniel Rosenberg * If the dentry name is stored in-line, then it may be concurrently 1436c843843eSDaniel Rosenberg * modified by a rename. If this happens, the VFS will eventually retry 1437c843843eSDaniel Rosenberg * the lookup, so it doesn't matter what ->d_compare() returns. 1438c843843eSDaniel Rosenberg * However, it's unsafe to call utf8_strncasecmp() with an unstable 1439c843843eSDaniel Rosenberg * string. Therefore, we have to copy the name into a temporary buffer. 1440c843843eSDaniel Rosenberg */ 1441c843843eSDaniel Rosenberg if (len <= DNAME_INLINE_LEN - 1) { 1442c843843eSDaniel Rosenberg memcpy(strbuf, str, len); 1443c843843eSDaniel Rosenberg strbuf[len] = 0; 1444c843843eSDaniel Rosenberg qstr.name = strbuf; 1445c843843eSDaniel Rosenberg /* prevent compiler from optimizing out the temporary buffer */ 1446c843843eSDaniel Rosenberg barrier(); 1447c843843eSDaniel Rosenberg } 1448c843843eSDaniel Rosenberg ret = utf8_strncasecmp(um, name, &qstr); 1449c843843eSDaniel Rosenberg if (ret >= 0) 1450c843843eSDaniel Rosenberg return ret; 1451c843843eSDaniel Rosenberg 1452c843843eSDaniel Rosenberg if (sb_has_strict_encoding(sb)) 1453c843843eSDaniel Rosenberg return -EINVAL; 1454c843843eSDaniel Rosenberg fallback: 1455c843843eSDaniel Rosenberg if (len != name->len) 1456c843843eSDaniel Rosenberg return 1; 1457c843843eSDaniel Rosenberg return !!memcmp(str, name->name, len); 1458c843843eSDaniel Rosenberg } 1459c843843eSDaniel Rosenberg 1460c843843eSDaniel Rosenberg /** 1461c843843eSDaniel Rosenberg * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems 1462c843843eSDaniel Rosenberg * @dentry: dentry of the parent directory 1463c843843eSDaniel Rosenberg * @str: qstr of name whose hash we should fill in 1464c843843eSDaniel Rosenberg * 1465c843843eSDaniel Rosenberg * Return: 0 if hash was successful or unchanged, and -EINVAL on error 1466c843843eSDaniel Rosenberg */ 1467794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) 1468c843843eSDaniel Rosenberg { 1469c843843eSDaniel Rosenberg const struct inode *dir = READ_ONCE(dentry->d_inode); 1470c843843eSDaniel Rosenberg struct super_block *sb = dentry->d_sb; 1471c843843eSDaniel Rosenberg const struct unicode_map *um = sb->s_encoding; 1472c843843eSDaniel Rosenberg int ret = 0; 1473c843843eSDaniel Rosenberg 1474c843843eSDaniel Rosenberg if (!dir || !needs_casefold(dir)) 1475c843843eSDaniel Rosenberg return 0; 1476c843843eSDaniel Rosenberg 1477c843843eSDaniel Rosenberg ret = utf8_casefold_hash(um, dentry, str); 1478c843843eSDaniel Rosenberg if (ret < 0 && sb_has_strict_encoding(sb)) 1479c843843eSDaniel Rosenberg return -EINVAL; 1480c843843eSDaniel Rosenberg return 0; 1481c843843eSDaniel Rosenberg } 1482608af703SDaniel Rosenberg 1483608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = { 1484608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1485608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1486608af703SDaniel Rosenberg }; 1487c843843eSDaniel Rosenberg #endif 1488608af703SDaniel Rosenberg 1489608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1490608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = { 1491608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1492608af703SDaniel Rosenberg }; 1493608af703SDaniel Rosenberg #endif 1494608af703SDaniel Rosenberg 14955298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1496608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = { 1497608af703SDaniel Rosenberg .d_hash = generic_ci_d_hash, 1498608af703SDaniel Rosenberg .d_compare = generic_ci_d_compare, 1499608af703SDaniel Rosenberg .d_revalidate = fscrypt_d_revalidate, 1500608af703SDaniel Rosenberg }; 1501608af703SDaniel Rosenberg #endif 1502608af703SDaniel Rosenberg 1503608af703SDaniel Rosenberg /** 1504608af703SDaniel Rosenberg * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry 1505608af703SDaniel Rosenberg * @dentry: dentry to set ops on 1506608af703SDaniel Rosenberg * 1507608af703SDaniel Rosenberg * Casefolded directories need d_hash and d_compare set, so that the dentries 1508608af703SDaniel Rosenberg * contained in them are handled case-insensitively. Note that these operations 1509608af703SDaniel Rosenberg * are needed on the parent directory rather than on the dentries in it, and 1510608af703SDaniel Rosenberg * while the casefolding flag can be toggled on and off on an empty directory, 1511608af703SDaniel Rosenberg * dentry_operations can't be changed later. As a result, if the filesystem has 1512608af703SDaniel Rosenberg * casefolding support enabled at all, we have to give all dentries the 1513608af703SDaniel Rosenberg * casefolding operations even if their inode doesn't have the casefolding flag 1514608af703SDaniel Rosenberg * currently (and thus the casefolding ops would be no-ops for now). 1515608af703SDaniel Rosenberg * 1516608af703SDaniel Rosenberg * Encryption works differently in that the only dentry operation it needs is 1517608af703SDaniel Rosenberg * d_revalidate, which it only needs on dentries that have the no-key name flag. 1518608af703SDaniel Rosenberg * The no-key flag can't be set "later", so we don't have to worry about that. 1519608af703SDaniel Rosenberg * 1520608af703SDaniel Rosenberg * Finally, to maximize compatibility with overlayfs (which isn't compatible 1521608af703SDaniel Rosenberg * with certain dentry operations) and to avoid taking an unnecessary 1522608af703SDaniel Rosenberg * performance hit, we use custom dentry_operations for each possible 1523608af703SDaniel Rosenberg * combination rather than always installing all operations. 1524608af703SDaniel Rosenberg */ 1525608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry) 1526608af703SDaniel Rosenberg { 1527608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1528608af703SDaniel Rosenberg bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME; 1529608af703SDaniel Rosenberg #endif 15305298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1531608af703SDaniel Rosenberg bool needs_ci_ops = dentry->d_sb->s_encoding; 1532608af703SDaniel Rosenberg #endif 15335298d4bfSChristoph Hellwig #if defined(CONFIG_FS_ENCRYPTION) && IS_ENABLED(CONFIG_UNICODE) 1534608af703SDaniel Rosenberg if (needs_encrypt_ops && needs_ci_ops) { 1535608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops); 1536608af703SDaniel Rosenberg return; 1537608af703SDaniel Rosenberg } 1538608af703SDaniel Rosenberg #endif 1539608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION 1540608af703SDaniel Rosenberg if (needs_encrypt_ops) { 1541608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_encrypted_dentry_ops); 1542608af703SDaniel Rosenberg return; 1543608af703SDaniel Rosenberg } 1544608af703SDaniel Rosenberg #endif 15455298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE) 1546608af703SDaniel Rosenberg if (needs_ci_ops) { 1547608af703SDaniel Rosenberg d_set_d_op(dentry, &generic_ci_dentry_ops); 1548608af703SDaniel Rosenberg return; 1549608af703SDaniel Rosenberg } 1550608af703SDaniel Rosenberg #endif 1551608af703SDaniel Rosenberg } 1552608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops); 15535ca14835SAndrew Morton 15545ca14835SAndrew Morton /** 15555ca14835SAndrew Morton * inode_maybe_inc_iversion - increments i_version 15565ca14835SAndrew Morton * @inode: inode with the i_version that should be updated 15575ca14835SAndrew Morton * @force: increment the counter even if it's not necessary? 15585ca14835SAndrew Morton * 15595ca14835SAndrew Morton * Every time the inode is modified, the i_version field must be seen to have 15605ca14835SAndrew Morton * changed by any observer. 15615ca14835SAndrew Morton * 15625ca14835SAndrew Morton * If "force" is set or the QUERIED flag is set, then ensure that we increment 15635ca14835SAndrew Morton * the value, and clear the queried flag. 15645ca14835SAndrew Morton * 15655ca14835SAndrew Morton * In the common case where neither is set, then we can return "false" without 15665ca14835SAndrew Morton * updating i_version. 15675ca14835SAndrew Morton * 15685ca14835SAndrew Morton * If this function returns false, and no other metadata has changed, then we 15695ca14835SAndrew Morton * can avoid logging the metadata. 15705ca14835SAndrew Morton */ 15715ca14835SAndrew Morton bool inode_maybe_inc_iversion(struct inode *inode, bool force) 15725ca14835SAndrew Morton { 15735ca14835SAndrew Morton u64 cur, new; 15745ca14835SAndrew Morton 15755ca14835SAndrew Morton /* 15765ca14835SAndrew Morton * The i_version field is not strictly ordered with any other inode 15775ca14835SAndrew Morton * information, but the legacy inode_inc_iversion code used a spinlock 15785ca14835SAndrew Morton * to serialize increments. 15795ca14835SAndrew Morton * 15805ca14835SAndrew Morton * Here, we add full memory barriers to ensure that any de-facto 15815ca14835SAndrew Morton * ordering with other info is preserved. 15825ca14835SAndrew Morton * 15835ca14835SAndrew Morton * This barrier pairs with the barrier in inode_query_iversion() 15845ca14835SAndrew Morton */ 15855ca14835SAndrew Morton smp_mb(); 15865ca14835SAndrew Morton cur = inode_peek_iversion_raw(inode); 15875ca14835SAndrew Morton do { 15885ca14835SAndrew Morton /* If flag is clear then we needn't do anything */ 15895ca14835SAndrew Morton if (!force && !(cur & I_VERSION_QUERIED)) 15905ca14835SAndrew Morton return false; 15915ca14835SAndrew Morton 15925ca14835SAndrew Morton /* Since lowest bit is flag, add 2 to avoid it */ 15935ca14835SAndrew Morton new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT; 15945ca14835SAndrew Morton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 15955ca14835SAndrew Morton return true; 15965ca14835SAndrew Morton } 15975ca14835SAndrew Morton EXPORT_SYMBOL(inode_maybe_inc_iversion); 1598c5bc1b3fSJeff Layton 1599c5bc1b3fSJeff Layton /** 1600c5bc1b3fSJeff Layton * inode_query_iversion - read i_version for later use 1601c5bc1b3fSJeff Layton * @inode: inode from which i_version should be read 1602c5bc1b3fSJeff Layton * 1603c5bc1b3fSJeff Layton * Read the inode i_version counter. This should be used by callers that wish 1604c5bc1b3fSJeff Layton * to store the returned i_version for later comparison. This will guarantee 1605c5bc1b3fSJeff Layton * that a later query of the i_version will result in a different value if 1606c5bc1b3fSJeff Layton * anything has changed. 1607c5bc1b3fSJeff Layton * 1608c5bc1b3fSJeff Layton * In this implementation, we fetch the current value, set the QUERIED flag and 1609c5bc1b3fSJeff Layton * then try to swap it into place with a cmpxchg, if it wasn't already set. If 1610c5bc1b3fSJeff Layton * that fails, we try again with the newly fetched value from the cmpxchg. 1611c5bc1b3fSJeff Layton */ 1612c5bc1b3fSJeff Layton u64 inode_query_iversion(struct inode *inode) 1613c5bc1b3fSJeff Layton { 1614c5bc1b3fSJeff Layton u64 cur, new; 1615c5bc1b3fSJeff Layton 1616c5bc1b3fSJeff Layton cur = inode_peek_iversion_raw(inode); 1617c5bc1b3fSJeff Layton do { 1618c5bc1b3fSJeff Layton /* If flag is already set, then no need to swap */ 1619c5bc1b3fSJeff Layton if (cur & I_VERSION_QUERIED) { 1620c5bc1b3fSJeff Layton /* 1621c5bc1b3fSJeff Layton * This barrier (and the implicit barrier in the 1622c5bc1b3fSJeff Layton * cmpxchg below) pairs with the barrier in 1623c5bc1b3fSJeff Layton * inode_maybe_inc_iversion(). 1624c5bc1b3fSJeff Layton */ 1625c5bc1b3fSJeff Layton smp_mb(); 1626c5bc1b3fSJeff Layton break; 1627c5bc1b3fSJeff Layton } 1628c5bc1b3fSJeff Layton 1629c5bc1b3fSJeff Layton new = cur | I_VERSION_QUERIED; 1630c5bc1b3fSJeff Layton } while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new)); 1631c5bc1b3fSJeff Layton return cur >> I_VERSION_QUERIED_SHIFT; 1632c5bc1b3fSJeff Layton } 1633c5bc1b3fSJeff Layton EXPORT_SYMBOL(inode_query_iversion); 163444fff0faSChristoph Hellwig 163544fff0faSChristoph Hellwig ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, 163644fff0faSChristoph Hellwig ssize_t direct_written, ssize_t buffered_written) 163744fff0faSChristoph Hellwig { 163844fff0faSChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 163944fff0faSChristoph Hellwig loff_t pos = iocb->ki_pos - buffered_written; 164044fff0faSChristoph Hellwig loff_t end = iocb->ki_pos - 1; 164144fff0faSChristoph Hellwig int err; 164244fff0faSChristoph Hellwig 164344fff0faSChristoph Hellwig /* 164444fff0faSChristoph Hellwig * If the buffered write fallback returned an error, we want to return 164544fff0faSChristoph Hellwig * the number of bytes which were written by direct I/O, or the error 164644fff0faSChristoph Hellwig * code if that was zero. 164744fff0faSChristoph Hellwig * 164844fff0faSChristoph Hellwig * Note that this differs from normal direct-io semantics, which will 164944fff0faSChristoph Hellwig * return -EFOO even if some bytes were written. 165044fff0faSChristoph Hellwig */ 165144fff0faSChristoph Hellwig if (unlikely(buffered_written < 0)) { 165244fff0faSChristoph Hellwig if (direct_written) 165344fff0faSChristoph Hellwig return direct_written; 165444fff0faSChristoph Hellwig return buffered_written; 165544fff0faSChristoph Hellwig } 165644fff0faSChristoph Hellwig 165744fff0faSChristoph Hellwig /* 165844fff0faSChristoph Hellwig * We need to ensure that the page cache pages are written to disk and 165944fff0faSChristoph Hellwig * invalidated to preserve the expected O_DIRECT semantics. 166044fff0faSChristoph Hellwig */ 166144fff0faSChristoph Hellwig err = filemap_write_and_wait_range(mapping, pos, end); 166244fff0faSChristoph Hellwig if (err < 0) { 166344fff0faSChristoph Hellwig /* 166444fff0faSChristoph Hellwig * We don't know how much we wrote, so just return the number of 166544fff0faSChristoph Hellwig * bytes which were direct-written 166644fff0faSChristoph Hellwig */ 166744fff0faSChristoph Hellwig if (direct_written) 166844fff0faSChristoph Hellwig return direct_written; 166944fff0faSChristoph Hellwig return err; 167044fff0faSChristoph Hellwig } 167144fff0faSChristoph Hellwig invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT); 167244fff0faSChristoph Hellwig return direct_written + buffered_written; 167344fff0faSChristoph Hellwig } 167444fff0faSChristoph Hellwig EXPORT_SYMBOL_GPL(direct_write_fallback); 1675