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> 18d5aacad5SAl Viro #include <linux/writeback.h> 19ff01bb48SAl Viro #include <linux/buffer_head.h> /* sync_mapping_buffers */ 207cf34c76SIngo Molnar 217c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 221da177e4SLinus Torvalds 23a4464dbcSAl Viro #include "internal.h" 24a4464dbcSAl Viro 25a528d35eSDavid Howells int simple_getattr(const struct path *path, struct kstat *stat, 26a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 271da177e4SLinus Torvalds { 28a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 291da177e4SLinus Torvalds generic_fillattr(inode, stat); 3009cbfeafSKirill A. Shutemov stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9); 311da177e4SLinus Torvalds return 0; 321da177e4SLinus Torvalds } 3312f38872SAl Viro EXPORT_SYMBOL(simple_getattr); 341da177e4SLinus Torvalds 35726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf) 361da177e4SLinus Torvalds { 37726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic; 3809cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 391da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 401da177e4SLinus Torvalds return 0; 411da177e4SLinus Torvalds } 4212f38872SAl Viro EXPORT_SYMBOL(simple_statfs); 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* 451da177e4SLinus Torvalds * Retaining negative dentries for an in-memory filesystem just wastes 461da177e4SLinus Torvalds * memory and lookup time: arrange for them to be deleted immediately. 471da177e4SLinus Torvalds */ 48b26d4cd3SAl Viro int always_delete_dentry(const struct dentry *dentry) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds return 1; 511da177e4SLinus Torvalds } 52b26d4cd3SAl Viro EXPORT_SYMBOL(always_delete_dentry); 53b26d4cd3SAl Viro 54b26d4cd3SAl Viro const struct dentry_operations simple_dentry_operations = { 55b26d4cd3SAl Viro .d_delete = always_delete_dentry, 56b26d4cd3SAl Viro }; 57b26d4cd3SAl Viro EXPORT_SYMBOL(simple_dentry_operations); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * Lookup the data. This is trivial - if the dentry didn't already 611da177e4SLinus Torvalds * exist, we know it is negative. Set d_op to delete negative dentries. 621da177e4SLinus Torvalds */ 6300cd8dd3SAl Viro struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds if (dentry->d_name.len > NAME_MAX) 661da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 6774931da7SAl Viro if (!dentry->d_sb->s_d_op) 68fb045adbSNick Piggin d_set_d_op(dentry, &simple_dentry_operations); 691da177e4SLinus Torvalds d_add(dentry, NULL); 701da177e4SLinus Torvalds return NULL; 711da177e4SLinus Torvalds } 7212f38872SAl Viro EXPORT_SYMBOL(simple_lookup); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file) 751da177e4SLinus Torvalds { 76ba65dc5eSAl Viro file->private_data = d_alloc_cursor(file->f_path.dentry); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds return file->private_data ? 0 : -ENOMEM; 791da177e4SLinus Torvalds } 8012f38872SAl Viro EXPORT_SYMBOL(dcache_dir_open); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds dput(file->private_data); 851da177e4SLinus Torvalds return 0; 861da177e4SLinus Torvalds } 8712f38872SAl Viro EXPORT_SYMBOL(dcache_dir_close); 881da177e4SLinus Torvalds 894f42c1b5SAl Viro /* parent is locked at least shared */ 904f42c1b5SAl Viro static struct dentry *next_positive(struct dentry *parent, 914f42c1b5SAl Viro struct list_head *from, 924f42c1b5SAl Viro int count) 934f42c1b5SAl Viro { 94ebaaa80eSAl Viro unsigned *seq = &parent->d_inode->i_dir_seq, n; 95ebaaa80eSAl Viro struct dentry *res; 964f42c1b5SAl Viro struct list_head *p; 97ebaaa80eSAl Viro bool skipped; 98ebaaa80eSAl Viro int i; 994f42c1b5SAl Viro 100ebaaa80eSAl Viro retry: 101ebaaa80eSAl Viro i = count; 102ebaaa80eSAl Viro skipped = false; 103ebaaa80eSAl Viro n = smp_load_acquire(seq) & ~1; 104ebaaa80eSAl Viro res = NULL; 105ebaaa80eSAl Viro rcu_read_lock(); 1064f42c1b5SAl Viro for (p = from->next; p != &parent->d_subdirs; p = p->next) { 1074f42c1b5SAl Viro struct dentry *d = list_entry(p, struct dentry, d_child); 108ebaaa80eSAl Viro if (!simple_positive(d)) { 109ebaaa80eSAl Viro skipped = true; 110ebaaa80eSAl Viro } else if (!--i) { 1114f42c1b5SAl Viro res = d; 1124f42c1b5SAl Viro break; 1134f42c1b5SAl Viro } 1144f42c1b5SAl Viro } 115ebaaa80eSAl Viro rcu_read_unlock(); 116ebaaa80eSAl Viro if (skipped) { 117ebaaa80eSAl Viro smp_rmb(); 118ebaaa80eSAl Viro if (unlikely(*seq != n)) 119ebaaa80eSAl Viro goto retry; 120ebaaa80eSAl Viro } 1214f42c1b5SAl Viro return res; 1224f42c1b5SAl Viro } 1234f42c1b5SAl Viro 1244f42c1b5SAl Viro static void move_cursor(struct dentry *cursor, struct list_head *after) 1254f42c1b5SAl Viro { 1264f42c1b5SAl Viro struct dentry *parent = cursor->d_parent; 127ebaaa80eSAl Viro unsigned n, *seq = &parent->d_inode->i_dir_seq; 1284f42c1b5SAl Viro spin_lock(&parent->d_lock); 129ebaaa80eSAl Viro for (;;) { 130ebaaa80eSAl Viro n = *seq; 131ebaaa80eSAl Viro if (!(n & 1) && cmpxchg(seq, n, n + 1) == n) 132ebaaa80eSAl Viro break; 133ebaaa80eSAl Viro cpu_relax(); 134ebaaa80eSAl Viro } 1354f42c1b5SAl Viro __list_del(cursor->d_child.prev, cursor->d_child.next); 1364f42c1b5SAl Viro if (after) 1374f42c1b5SAl Viro list_add(&cursor->d_child, after); 1384f42c1b5SAl Viro else 1394f42c1b5SAl Viro list_add_tail(&cursor->d_child, &parent->d_subdirs); 140ebaaa80eSAl Viro smp_store_release(seq, n + 2); 1414f42c1b5SAl Viro spin_unlock(&parent->d_lock); 1424f42c1b5SAl Viro } 1434f42c1b5SAl Viro 144965c8e59SAndrew Morton loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) 1451da177e4SLinus Torvalds { 1462fd6b7f5SNick Piggin struct dentry *dentry = file->f_path.dentry; 147965c8e59SAndrew Morton switch (whence) { 1481da177e4SLinus Torvalds case 1: 1491da177e4SLinus Torvalds offset += file->f_pos; 1500a4c9265SGustavo A. R. Silva /* fall through */ 1511da177e4SLinus Torvalds case 0: 1521da177e4SLinus Torvalds if (offset >= 0) 1531da177e4SLinus Torvalds break; 1540a4c9265SGustavo A. R. Silva /* fall through */ 1551da177e4SLinus Torvalds default: 1561da177e4SLinus Torvalds return -EINVAL; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds if (offset != file->f_pos) { 1591da177e4SLinus Torvalds file->f_pos = offset; 1601da177e4SLinus Torvalds if (file->f_pos >= 2) { 1611da177e4SLinus Torvalds struct dentry *cursor = file->private_data; 1624f42c1b5SAl Viro struct dentry *to; 1631da177e4SLinus Torvalds loff_t n = file->f_pos - 2; 1641da177e4SLinus Torvalds 165274f5b04SAl Viro inode_lock_shared(dentry->d_inode); 1664f42c1b5SAl Viro to = next_positive(dentry, &dentry->d_subdirs, n); 1674f42c1b5SAl Viro move_cursor(cursor, to ? &to->d_child : NULL); 168274f5b04SAl Viro inode_unlock_shared(dentry->d_inode); 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds return offset; 1721da177e4SLinus Torvalds } 17312f38872SAl Viro EXPORT_SYMBOL(dcache_dir_lseek); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */ 1761da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /* 1821da177e4SLinus Torvalds * Directory is locked and all positive dentries in it are safe, since 1831da177e4SLinus Torvalds * for ramfs-type trees they can't go away without unlink() or rmdir(), 1841da177e4SLinus Torvalds * both impossible due to the lock on directory. 1851da177e4SLinus Torvalds */ 1861da177e4SLinus Torvalds 1875f99f4e7SAl Viro int dcache_readdir(struct file *file, struct dir_context *ctx) 1881da177e4SLinus Torvalds { 1895f99f4e7SAl Viro struct dentry *dentry = file->f_path.dentry; 1905f99f4e7SAl Viro struct dentry *cursor = file->private_data; 1914f42c1b5SAl Viro struct list_head *p = &cursor->d_child; 1924f42c1b5SAl Viro struct dentry *next; 1934f42c1b5SAl Viro bool moved = false; 1941da177e4SLinus Torvalds 1955f99f4e7SAl Viro if (!dir_emit_dots(file, ctx)) 1965f99f4e7SAl Viro return 0; 1974f42c1b5SAl Viro 1985f99f4e7SAl Viro if (ctx->pos == 2) 1994f42c1b5SAl Viro p = &dentry->d_subdirs; 2004f42c1b5SAl Viro while ((next = next_positive(dentry, p, 1)) != NULL) { 2015f99f4e7SAl Viro if (!dir_emit(ctx, next->d_name.name, next->d_name.len, 202dea655c2SDavid Howells d_inode(next)->i_ino, dt_type(d_inode(next)))) 2034f42c1b5SAl Viro break; 2044f42c1b5SAl Viro moved = true; 2054f42c1b5SAl Viro p = &next->d_child; 2065f99f4e7SAl Viro ctx->pos++; 2071da177e4SLinus Torvalds } 2084f42c1b5SAl Viro if (moved) 2094f42c1b5SAl Viro move_cursor(cursor, p); 2101da177e4SLinus Torvalds return 0; 2111da177e4SLinus Torvalds } 21212f38872SAl Viro EXPORT_SYMBOL(dcache_readdir); 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds return -EISDIR; 2171da177e4SLinus Torvalds } 21812f38872SAl Viro EXPORT_SYMBOL(generic_read_dir); 2191da177e4SLinus Torvalds 2204b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = { 2211da177e4SLinus Torvalds .open = dcache_dir_open, 2221da177e4SLinus Torvalds .release = dcache_dir_close, 2231da177e4SLinus Torvalds .llseek = dcache_dir_lseek, 2241da177e4SLinus Torvalds .read = generic_read_dir, 2254e82901cSAl Viro .iterate_shared = dcache_readdir, 2261b061d92SChristoph Hellwig .fsync = noop_fsync, 2271da177e4SLinus Torvalds }; 22812f38872SAl Viro EXPORT_SYMBOL(simple_dir_operations); 2291da177e4SLinus Torvalds 23092e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = { 2311da177e4SLinus Torvalds .lookup = simple_lookup, 2321da177e4SLinus Torvalds }; 23312f38872SAl Viro EXPORT_SYMBOL(simple_dir_inode_operations); 2341da177e4SLinus Torvalds 235759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 236759b9775SHugh Dickins .statfs = simple_statfs, 237759b9775SHugh Dickins }; 238759b9775SHugh Dickins 2391da177e4SLinus Torvalds /* 2401da177e4SLinus Torvalds * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 2411da177e4SLinus Torvalds * will never be mountable) 2421da177e4SLinus Torvalds */ 243bba0bd31SAndreas Gruenbacher struct dentry *mount_pseudo_xattr(struct file_system_type *fs_type, char *name, 244bba0bd31SAndreas Gruenbacher const struct super_operations *ops, const struct xattr_handler **xattr, 245c74a1cbbSAl Viro const struct dentry_operations *dops, unsigned long magic) 2461da177e4SLinus Torvalds { 2479249e17fSDavid Howells struct super_block *s; 2481da177e4SLinus Torvalds struct dentry *dentry; 2491da177e4SLinus Torvalds struct inode *root; 25026fe5750SLinus Torvalds struct qstr d_name = QSTR_INIT(name, strlen(name)); 2511da177e4SLinus Torvalds 2521751e8a6SLinus Torvalds s = sget_userns(fs_type, NULL, set_anon_super, SB_KERNMOUNT|SB_NOUSER, 25375422726SEric W. Biederman &init_user_ns, NULL); 2541da177e4SLinus Torvalds if (IS_ERR(s)) 25551139adaSAl Viro return ERR_CAST(s); 2561da177e4SLinus Torvalds 25789a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 2583971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 2593971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 2601da177e4SLinus Torvalds s->s_magic = magic; 261759b9775SHugh Dickins s->s_op = ops ? ops : &simple_super_operations; 262bba0bd31SAndreas Gruenbacher s->s_xattr = xattr; 2631da177e4SLinus Torvalds s->s_time_gran = 1; 2641da177e4SLinus Torvalds root = new_inode(s); 2651da177e4SLinus Torvalds if (!root) 2661da177e4SLinus Torvalds goto Enomem; 2671a1c9bb4SJeff Layton /* 2681a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 2691a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 2701a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 2711a1c9bb4SJeff Layton */ 2721a1c9bb4SJeff Layton root->i_ino = 1; 2731da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 274078cd827SDeepa Dinamani root->i_atime = root->i_mtime = root->i_ctime = current_time(root); 275a4464dbcSAl Viro dentry = __d_alloc(s, &d_name); 2761da177e4SLinus Torvalds if (!dentry) { 2771da177e4SLinus Torvalds iput(root); 2781da177e4SLinus Torvalds goto Enomem; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds d_instantiate(dentry, root); 2811da177e4SLinus Torvalds s->s_root = dentry; 282c74a1cbbSAl Viro s->s_d_op = dops; 2831751e8a6SLinus Torvalds s->s_flags |= SB_ACTIVE; 28451139adaSAl Viro return dget(s->s_root); 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds Enomem: 2876f5bbff9SAl Viro deactivate_locked_super(s); 28851139adaSAl Viro return ERR_PTR(-ENOMEM); 2891da177e4SLinus Torvalds } 290bba0bd31SAndreas Gruenbacher EXPORT_SYMBOL(mount_pseudo_xattr); 2911da177e4SLinus Torvalds 29220955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 29320955e89SStephen Boyd { 29420955e89SStephen Boyd if (inode->i_private) 29520955e89SStephen Boyd file->private_data = inode->i_private; 29620955e89SStephen Boyd return 0; 29720955e89SStephen Boyd } 29812f38872SAl Viro EXPORT_SYMBOL(simple_open); 29920955e89SStephen Boyd 3001da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 3011da177e4SLinus Torvalds { 302dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 3031da177e4SLinus Torvalds 304078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 305d8c76e6fSDave Hansen inc_nlink(inode); 3067de9c6eeSAl Viro ihold(inode); 3071da177e4SLinus Torvalds dget(dentry); 3081da177e4SLinus Torvalds d_instantiate(dentry, inode); 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds } 31112f38872SAl Viro EXPORT_SYMBOL(simple_link); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 3141da177e4SLinus Torvalds { 3151da177e4SLinus Torvalds struct dentry *child; 3161da177e4SLinus Torvalds int ret = 0; 3171da177e4SLinus Torvalds 3182fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 319946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) { 320da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 321da502956SNick Piggin if (simple_positive(child)) { 322da502956SNick Piggin spin_unlock(&child->d_lock); 3231da177e4SLinus Torvalds goto out; 324da502956SNick Piggin } 325da502956SNick Piggin spin_unlock(&child->d_lock); 326da502956SNick Piggin } 3271da177e4SLinus Torvalds ret = 1; 3281da177e4SLinus Torvalds out: 3292fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 3301da177e4SLinus Torvalds return ret; 3311da177e4SLinus Torvalds } 33212f38872SAl Viro EXPORT_SYMBOL(simple_empty); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 3351da177e4SLinus Torvalds { 336dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 3371da177e4SLinus Torvalds 338078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 3399a53c3a7SDave Hansen drop_nlink(inode); 3401da177e4SLinus Torvalds dput(dentry); 3411da177e4SLinus Torvalds return 0; 3421da177e4SLinus Torvalds } 34312f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds if (!simple_empty(dentry)) 3481da177e4SLinus Torvalds return -ENOTEMPTY; 3491da177e4SLinus Torvalds 350dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 3511da177e4SLinus Torvalds simple_unlink(dir, dentry); 3529a53c3a7SDave Hansen drop_nlink(dir); 3531da177e4SLinus Torvalds return 0; 3541da177e4SLinus Torvalds } 35512f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry, 358e0e0be8aSMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 359e0e0be8aSMiklos Szeredi unsigned int flags) 3601da177e4SLinus Torvalds { 361dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 362e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 3631da177e4SLinus Torvalds 364e0e0be8aSMiklos Szeredi if (flags & ~RENAME_NOREPLACE) 365e0e0be8aSMiklos Szeredi return -EINVAL; 366e0e0be8aSMiklos Szeredi 3671da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 3681da177e4SLinus Torvalds return -ENOTEMPTY; 3691da177e4SLinus Torvalds 370dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 3711da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 372841590ceSAl Viro if (they_are_dirs) { 373dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 3749a53c3a7SDave Hansen drop_nlink(old_dir); 375841590ceSAl Viro } 3761da177e4SLinus Torvalds } else if (they_are_dirs) { 3779a53c3a7SDave Hansen drop_nlink(old_dir); 378d8c76e6fSDave Hansen inc_nlink(new_dir); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 382078cd827SDeepa Dinamani new_dir->i_mtime = inode->i_ctime = current_time(old_dir); 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds return 0; 3851da177e4SLinus Torvalds } 38612f38872SAl Viro EXPORT_SYMBOL(simple_rename); 3871da177e4SLinus Torvalds 3887bb46a67Snpiggin@suse.de /** 389eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 3907bb46a67Snpiggin@suse.de * @dentry: dentry 3917bb46a67Snpiggin@suse.de * @iattr: iattr structure 3927bb46a67Snpiggin@suse.de * 3937bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 3947bb46a67Snpiggin@suse.de * 395eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 396eef2380cSChristoph Hellwig * implementation of size changes. 397eef2380cSChristoph Hellwig * 398eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 399eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 400eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 4017bb46a67Snpiggin@suse.de */ 4027bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr) 4037bb46a67Snpiggin@suse.de { 404dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 4057bb46a67Snpiggin@suse.de int error; 4067bb46a67Snpiggin@suse.de 40731051c85SJan Kara error = setattr_prepare(dentry, iattr); 4087bb46a67Snpiggin@suse.de if (error) 4097bb46a67Snpiggin@suse.de return error; 4107bb46a67Snpiggin@suse.de 4112c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 4122c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 4136a1a90adSChristoph Hellwig setattr_copy(inode, iattr); 414eef2380cSChristoph Hellwig mark_inode_dirty(inode); 415eef2380cSChristoph Hellwig return 0; 4167bb46a67Snpiggin@suse.de } 4177bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 4187bb46a67Snpiggin@suse.de 4191da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page) 4201da177e4SLinus Torvalds { 421c0d92cbcSPekka J Enberg clear_highpage(page); 4221da177e4SLinus Torvalds flush_dcache_page(page); 4231da177e4SLinus Torvalds SetPageUptodate(page); 4241da177e4SLinus Torvalds unlock_page(page); 4251da177e4SLinus Torvalds return 0; 4261da177e4SLinus Torvalds } 42712f38872SAl Viro EXPORT_SYMBOL(simple_readpage); 4281da177e4SLinus Torvalds 429afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 430afddba49SNick Piggin loff_t pos, unsigned len, unsigned flags, 431afddba49SNick Piggin struct page **pagep, void **fsdata) 432afddba49SNick Piggin { 433afddba49SNick Piggin struct page *page; 434afddba49SNick Piggin pgoff_t index; 435afddba49SNick Piggin 43609cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 437afddba49SNick Piggin 43854566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 439afddba49SNick Piggin if (!page) 440afddba49SNick Piggin return -ENOMEM; 441afddba49SNick Piggin 442afddba49SNick Piggin *pagep = page; 443afddba49SNick Piggin 44409cbfeafSKirill A. Shutemov if (!PageUptodate(page) && (len != PAGE_SIZE)) { 44509cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 446193cf4b9SBoaz Harrosh 44709cbfeafSKirill A. Shutemov zero_user_segments(page, 0, from, from + len, PAGE_SIZE); 448193cf4b9SBoaz Harrosh } 449193cf4b9SBoaz Harrosh return 0; 450afddba49SNick Piggin } 45112f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 452afddba49SNick Piggin 453ad2a722fSBoaz Harrosh /** 454ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 455ad2a722fSBoaz Harrosh * @available: See .write_end of address_space_operations 456ad2a722fSBoaz Harrosh * @file: " 457ad2a722fSBoaz Harrosh * @mapping: " 458ad2a722fSBoaz Harrosh * @pos: " 459ad2a722fSBoaz Harrosh * @len: " 460ad2a722fSBoaz Harrosh * @copied: " 461ad2a722fSBoaz Harrosh * @page: " 462ad2a722fSBoaz Harrosh * @fsdata: " 463ad2a722fSBoaz Harrosh * 464ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 465ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 466ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 467ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 468ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 469ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 470ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 471ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 472ad2a722fSBoaz Harrosh * case that i_size has changed. 47304fff641SAl Viro * 47404fff641SAl Viro * Use *ONLY* with simple_readpage() 475ad2a722fSBoaz Harrosh */ 476ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping, 477ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 478ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 4791da177e4SLinus Torvalds { 4801da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 481ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 482ad2a722fSBoaz Harrosh 483ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 48404fff641SAl Viro if (!PageUptodate(page)) { 485ad2a722fSBoaz Harrosh if (copied < len) { 48609cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 487ad2a722fSBoaz Harrosh 488ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 489ad2a722fSBoaz Harrosh } 490955eff5aSNick Piggin SetPageUptodate(page); 49104fff641SAl Viro } 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 4941b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 4951da177e4SLinus Torvalds */ 496ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 497ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 498ad2a722fSBoaz Harrosh 4991da177e4SLinus Torvalds set_page_dirty(page); 500afddba49SNick Piggin unlock_page(page); 50109cbfeafSKirill A. Shutemov put_page(page); 502afddba49SNick Piggin 503afddba49SNick Piggin return copied; 504afddba49SNick Piggin } 50512f38872SAl Viro EXPORT_SYMBOL(simple_write_end); 506afddba49SNick Piggin 5071a1c9bb4SJeff Layton /* 5081a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 5091a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 5101a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 5111a1c9bb4SJeff Layton */ 5127d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 513cda37124SEric Biggers const struct tree_descr *files) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds struct inode *inode; 5161da177e4SLinus Torvalds struct dentry *root; 5171da177e4SLinus Torvalds struct dentry *dentry; 5181da177e4SLinus Torvalds int i; 5191da177e4SLinus Torvalds 52009cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE; 52109cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT; 5221da177e4SLinus Torvalds s->s_magic = magic; 523759b9775SHugh Dickins s->s_op = &simple_super_operations; 5241da177e4SLinus Torvalds s->s_time_gran = 1; 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds inode = new_inode(s); 5271da177e4SLinus Torvalds if (!inode) 5281da177e4SLinus Torvalds return -ENOMEM; 5291a1c9bb4SJeff Layton /* 5301a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 5311a1c9bb4SJeff Layton * entry at index 1 5321a1c9bb4SJeff Layton */ 5331a1c9bb4SJeff Layton inode->i_ino = 1; 5341da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 535078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 5361da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 5371da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 538bfe86848SMiklos Szeredi set_nlink(inode, 2); 53948fde701SAl Viro root = d_make_root(inode); 54048fde701SAl Viro if (!root) 5411da177e4SLinus Torvalds return -ENOMEM; 5421da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 5431da177e4SLinus Torvalds if (!files->name) 5441da177e4SLinus Torvalds continue; 5451a1c9bb4SJeff Layton 5461a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 5471a1c9bb4SJeff Layton if (unlikely(i == 1)) 5481a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 5491a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 5501a1c9bb4SJeff Layton s->s_type->name); 5511a1c9bb4SJeff Layton 5521da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 5531da177e4SLinus Torvalds if (!dentry) 5541da177e4SLinus Torvalds goto out; 5551da177e4SLinus Torvalds inode = new_inode(s); 55632096ea1SKonstantin Khlebnikov if (!inode) { 55732096ea1SKonstantin Khlebnikov dput(dentry); 5581da177e4SLinus Torvalds goto out; 55932096ea1SKonstantin Khlebnikov } 5601da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 561078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 5621da177e4SLinus Torvalds inode->i_fop = files->ops; 5631da177e4SLinus Torvalds inode->i_ino = i; 5641da177e4SLinus Torvalds d_add(dentry, inode); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds s->s_root = root; 5671da177e4SLinus Torvalds return 0; 5681da177e4SLinus Torvalds out: 5691da177e4SLinus Torvalds d_genocide(root); 570640946f2SAl Viro shrink_dcache_parent(root); 5711da177e4SLinus Torvalds dput(root); 5721da177e4SLinus Torvalds return -ENOMEM; 5731da177e4SLinus Torvalds } 57412f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 5771da177e4SLinus Torvalds 5781f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 5811da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5821da177e4SLinus Torvalds if (unlikely(!*mount)) { 5831da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5841751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 5851da177e4SLinus Torvalds if (IS_ERR(mnt)) 5861da177e4SLinus Torvalds return PTR_ERR(mnt); 5871da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5881da177e4SLinus Torvalds if (!*mount) 5891da177e4SLinus Torvalds *mount = mnt; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds mntget(*mount); 5921da177e4SLinus Torvalds ++*count; 5931da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5941da177e4SLinus Torvalds mntput(mnt); 5951da177e4SLinus Torvalds return 0; 5961da177e4SLinus Torvalds } 59712f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 6001da177e4SLinus Torvalds { 6011da177e4SLinus Torvalds struct vfsmount *mnt; 6021da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 6031da177e4SLinus Torvalds mnt = *mount; 6041da177e4SLinus Torvalds if (!--*count) 6051da177e4SLinus Torvalds *mount = NULL; 6061da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 6071da177e4SLinus Torvalds mntput(mnt); 6081da177e4SLinus Torvalds } 60912f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 6101da177e4SLinus Torvalds 6116d1029b5SAkinobu Mita /** 6126d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 6136d1029b5SAkinobu Mita * @to: the user space buffer to read to 6146d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 6156d1029b5SAkinobu Mita * @ppos: the current position in the buffer 6166d1029b5SAkinobu Mita * @from: the buffer to read from 6176d1029b5SAkinobu Mita * @available: the size of the buffer 6186d1029b5SAkinobu Mita * 6196d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 6206d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 6216d1029b5SAkinobu Mita * 6226d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 6236d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 6246d1029b5SAkinobu Mita **/ 6251da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 6261da177e4SLinus Torvalds const void *from, size_t available) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds loff_t pos = *ppos; 62914be2746SSteven Rostedt size_t ret; 63014be2746SSteven Rostedt 6311da177e4SLinus Torvalds if (pos < 0) 6321da177e4SLinus Torvalds return -EINVAL; 63314be2746SSteven Rostedt if (pos >= available || !count) 6341da177e4SLinus Torvalds return 0; 6351da177e4SLinus Torvalds if (count > available - pos) 6361da177e4SLinus Torvalds count = available - pos; 63714be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 63814be2746SSteven Rostedt if (ret == count) 6391da177e4SLinus Torvalds return -EFAULT; 64014be2746SSteven Rostedt count -= ret; 6411da177e4SLinus Torvalds *ppos = pos + count; 6421da177e4SLinus Torvalds return count; 6431da177e4SLinus Torvalds } 64412f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 6451da177e4SLinus Torvalds 6466d1029b5SAkinobu Mita /** 6476a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 6486a727b43SJiri Slaby * @to: the buffer to write to 6496a727b43SJiri Slaby * @available: the size of the buffer 6506a727b43SJiri Slaby * @ppos: the current position in the buffer 6516a727b43SJiri Slaby * @from: the user space buffer to read from 6526a727b43SJiri Slaby * @count: the maximum number of bytes to read 6536a727b43SJiri Slaby * 6546a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 6556a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 6566a727b43SJiri Slaby * 6576a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 6586a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 6596a727b43SJiri Slaby **/ 6606a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 6616a727b43SJiri Slaby const void __user *from, size_t count) 6626a727b43SJiri Slaby { 6636a727b43SJiri Slaby loff_t pos = *ppos; 6646a727b43SJiri Slaby size_t res; 6656a727b43SJiri Slaby 6666a727b43SJiri Slaby if (pos < 0) 6676a727b43SJiri Slaby return -EINVAL; 6686a727b43SJiri Slaby if (pos >= available || !count) 6696a727b43SJiri Slaby return 0; 6706a727b43SJiri Slaby if (count > available - pos) 6716a727b43SJiri Slaby count = available - pos; 6726a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 6736a727b43SJiri Slaby if (res == count) 6746a727b43SJiri Slaby return -EFAULT; 6756a727b43SJiri Slaby count -= res; 6766a727b43SJiri Slaby *ppos = pos + count; 6776a727b43SJiri Slaby return count; 6786a727b43SJiri Slaby } 67912f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 6806a727b43SJiri Slaby 6816a727b43SJiri Slaby /** 6826d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 6836d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 6846d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 6856d1029b5SAkinobu Mita * @ppos: the current position in the buffer 6866d1029b5SAkinobu Mita * @from: the buffer to read from 6876d1029b5SAkinobu Mita * @available: the size of the buffer 6886d1029b5SAkinobu Mita * 6896d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 6906d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 6916d1029b5SAkinobu Mita * 6926d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 6936d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 6946d1029b5SAkinobu Mita **/ 69593b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 69693b07113SAkinobu Mita const void *from, size_t available) 69793b07113SAkinobu Mita { 69893b07113SAkinobu Mita loff_t pos = *ppos; 69993b07113SAkinobu Mita 70093b07113SAkinobu Mita if (pos < 0) 70193b07113SAkinobu Mita return -EINVAL; 70293b07113SAkinobu Mita if (pos >= available) 70393b07113SAkinobu Mita return 0; 70493b07113SAkinobu Mita if (count > available - pos) 70593b07113SAkinobu Mita count = available - pos; 70693b07113SAkinobu Mita memcpy(to, from + pos, count); 70793b07113SAkinobu Mita *ppos = pos + count; 70893b07113SAkinobu Mita 70993b07113SAkinobu Mita return count; 71093b07113SAkinobu Mita } 71112f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 71293b07113SAkinobu Mita 7131da177e4SLinus Torvalds /* 7141da177e4SLinus Torvalds * Transaction based IO. 7151da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 7161da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 7171da177e4SLinus Torvalds * file-local buffer. 7181da177e4SLinus Torvalds */ 71976791ab2SIngo Molnar 72076791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 72176791ab2SIngo Molnar { 72276791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 72376791ab2SIngo Molnar 72476791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 72576791ab2SIngo Molnar 72676791ab2SIngo Molnar /* 72776791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 72876791ab2SIngo Molnar * ar->data is ready for reading. 72976791ab2SIngo Molnar */ 73076791ab2SIngo Molnar smp_mb(); 73176791ab2SIngo Molnar ar->size = n; 73276791ab2SIngo Molnar } 73312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 73476791ab2SIngo Molnar 7351da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 7361da177e4SLinus Torvalds { 7371da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 7381da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 7411da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 7441da177e4SLinus Torvalds if (!ar) 7451da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* only one write allowed per open */ 7501da177e4SLinus Torvalds if (file->private_data) { 7511da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7521da177e4SLinus Torvalds free_page((unsigned long)ar); 7531da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds file->private_data = ar; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 7611da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds return ar->data; 7641da177e4SLinus Torvalds } 76512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds if (!ar) 7721da177e4SLinus Torvalds return 0; 7731da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 7741da177e4SLinus Torvalds } 77512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 7781da177e4SLinus Torvalds { 7791da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 7801da177e4SLinus Torvalds return 0; 7811da177e4SLinus Torvalds } 78212f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 7831da177e4SLinus Torvalds 784acaefc25SArnd Bergmann /* Simple attribute files */ 785acaefc25SArnd Bergmann 786acaefc25SArnd Bergmann struct simple_attr { 7878b88b099SChristoph Hellwig int (*get)(void *, u64 *); 7888b88b099SChristoph Hellwig int (*set)(void *, u64); 789acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 790acaefc25SArnd Bergmann char set_buf[24]; 791acaefc25SArnd Bergmann void *data; 792acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 7937cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 794acaefc25SArnd Bergmann }; 795acaefc25SArnd Bergmann 796acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 797acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 798acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 7998b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 800acaefc25SArnd Bergmann const char *fmt) 801acaefc25SArnd Bergmann { 802acaefc25SArnd Bergmann struct simple_attr *attr; 803acaefc25SArnd Bergmann 804acaefc25SArnd Bergmann attr = kmalloc(sizeof(*attr), GFP_KERNEL); 805acaefc25SArnd Bergmann if (!attr) 806acaefc25SArnd Bergmann return -ENOMEM; 807acaefc25SArnd Bergmann 808acaefc25SArnd Bergmann attr->get = get; 809acaefc25SArnd Bergmann attr->set = set; 8108e18e294STheodore Ts'o attr->data = inode->i_private; 811acaefc25SArnd Bergmann attr->fmt = fmt; 8127cf34c76SIngo Molnar mutex_init(&attr->mutex); 813acaefc25SArnd Bergmann 814acaefc25SArnd Bergmann file->private_data = attr; 815acaefc25SArnd Bergmann 816acaefc25SArnd Bergmann return nonseekable_open(inode, file); 817acaefc25SArnd Bergmann } 81812f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 819acaefc25SArnd Bergmann 82074bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 821acaefc25SArnd Bergmann { 822acaefc25SArnd Bergmann kfree(file->private_data); 823acaefc25SArnd Bergmann return 0; 824acaefc25SArnd Bergmann } 82512f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 826acaefc25SArnd Bergmann 827acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 828acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 829acaefc25SArnd Bergmann size_t len, loff_t *ppos) 830acaefc25SArnd Bergmann { 831acaefc25SArnd Bergmann struct simple_attr *attr; 832acaefc25SArnd Bergmann size_t size; 833acaefc25SArnd Bergmann ssize_t ret; 834acaefc25SArnd Bergmann 835acaefc25SArnd Bergmann attr = file->private_data; 836acaefc25SArnd Bergmann 837acaefc25SArnd Bergmann if (!attr->get) 838acaefc25SArnd Bergmann return -EACCES; 839acaefc25SArnd Bergmann 8409261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 8419261303aSChristoph Hellwig if (ret) 8429261303aSChristoph Hellwig return ret; 8439261303aSChristoph Hellwig 8448b88b099SChristoph Hellwig if (*ppos) { /* continued read */ 845acaefc25SArnd Bergmann size = strlen(attr->get_buf); 8468b88b099SChristoph Hellwig } else { /* first read */ 8478b88b099SChristoph Hellwig u64 val; 8488b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 8498b88b099SChristoph Hellwig if (ret) 8508b88b099SChristoph Hellwig goto out; 8518b88b099SChristoph Hellwig 852acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 8538b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 8548b88b099SChristoph Hellwig } 855acaefc25SArnd Bergmann 856acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 8578b88b099SChristoph Hellwig out: 8587cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 859acaefc25SArnd Bergmann return ret; 860acaefc25SArnd Bergmann } 86112f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 862acaefc25SArnd Bergmann 863acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 864acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf, 865acaefc25SArnd Bergmann size_t len, loff_t *ppos) 866acaefc25SArnd Bergmann { 867acaefc25SArnd Bergmann struct simple_attr *attr; 868acaefc25SArnd Bergmann u64 val; 869acaefc25SArnd Bergmann size_t size; 870acaefc25SArnd Bergmann ssize_t ret; 871acaefc25SArnd Bergmann 872acaefc25SArnd Bergmann attr = file->private_data; 873acaefc25SArnd Bergmann if (!attr->set) 874acaefc25SArnd Bergmann return -EACCES; 875acaefc25SArnd Bergmann 8769261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 8779261303aSChristoph Hellwig if (ret) 8789261303aSChristoph Hellwig return ret; 8799261303aSChristoph Hellwig 880acaefc25SArnd Bergmann ret = -EFAULT; 881acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 882acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 883acaefc25SArnd Bergmann goto out; 884acaefc25SArnd Bergmann 885acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 886f7b88631SAkinobu Mita val = simple_strtoll(attr->set_buf, NULL, 0); 88705cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 88805cc0ceeSWu Fengguang if (ret == 0) 88905cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 890acaefc25SArnd Bergmann out: 8917cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 892acaefc25SArnd Bergmann return ret; 893acaefc25SArnd Bergmann } 89412f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 895acaefc25SArnd Bergmann 8962596110aSChristoph Hellwig /** 8972596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 8982596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 8992596110aSChristoph Hellwig * @fid: file handle to convert 9002596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 9012596110aSChristoph Hellwig * @fh_type: type of file handle 9022596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 9032596110aSChristoph Hellwig * 9042596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 9052596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 9062596110aSChristoph Hellwig * inode for the object specified in the file handle. 9072596110aSChristoph Hellwig */ 9082596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 9092596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 9102596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 9112596110aSChristoph Hellwig { 9122596110aSChristoph Hellwig struct inode *inode = NULL; 9132596110aSChristoph Hellwig 9142596110aSChristoph Hellwig if (fh_len < 2) 9152596110aSChristoph Hellwig return NULL; 9162596110aSChristoph Hellwig 9172596110aSChristoph Hellwig switch (fh_type) { 9182596110aSChristoph Hellwig case FILEID_INO32_GEN: 9192596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 9202596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 9212596110aSChristoph Hellwig break; 9222596110aSChristoph Hellwig } 9232596110aSChristoph Hellwig 9244ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 9252596110aSChristoph Hellwig } 9262596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 9272596110aSChristoph Hellwig 9282596110aSChristoph Hellwig /** 929ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 9302596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 9312596110aSChristoph Hellwig * @fid: file handle to convert 9322596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 9332596110aSChristoph Hellwig * @fh_type: type of file handle 9342596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 9352596110aSChristoph Hellwig * 9362596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 9372596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 9382596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 9392596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 9402596110aSChristoph Hellwig */ 9412596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 9422596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 9432596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 9442596110aSChristoph Hellwig { 9452596110aSChristoph Hellwig struct inode *inode = NULL; 9462596110aSChristoph Hellwig 9472596110aSChristoph Hellwig if (fh_len <= 2) 9482596110aSChristoph Hellwig return NULL; 9492596110aSChristoph Hellwig 9502596110aSChristoph Hellwig switch (fh_type) { 9512596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 9522596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 9532596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 9542596110aSChristoph Hellwig break; 9552596110aSChristoph Hellwig } 9562596110aSChristoph Hellwig 9574ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 9582596110aSChristoph Hellwig } 9592596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 9602596110aSChristoph Hellwig 9611b061d92SChristoph Hellwig /** 962ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 963ac13a829SFabian Frederick * 9641b061d92SChristoph Hellwig * @file: file to synchronize 965ac13a829SFabian Frederick * @start: start offset in bytes 966ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 9671b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 9681b061d92SChristoph Hellwig * 9691b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 9701b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 9711b061d92SChristoph Hellwig * hanging off the address_space structure. 9721b061d92SChristoph Hellwig */ 973ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 97402c24a82SJosef Bacik int datasync) 975d5aacad5SAl Viro { 9767ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 977d5aacad5SAl Viro int err; 978d5aacad5SAl Viro int ret; 979d5aacad5SAl Viro 980383aa543SJeff Layton err = file_write_and_wait_range(file, start, end); 98102c24a82SJosef Bacik if (err) 98202c24a82SJosef Bacik return err; 98302c24a82SJosef Bacik 9845955102cSAl Viro inode_lock(inode); 985d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 9860ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 98702c24a82SJosef Bacik goto out; 988d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 98902c24a82SJosef Bacik goto out; 990d5aacad5SAl Viro 991c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 992d5aacad5SAl Viro if (ret == 0) 993d5aacad5SAl Viro ret = err; 994ac13a829SFabian Frederick 99502c24a82SJosef Bacik out: 9965955102cSAl Viro inode_unlock(inode); 997383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */ 998383aa543SJeff Layton err = file_check_and_advance_wb_err(file); 999383aa543SJeff Layton if (ret == 0) 1000383aa543SJeff Layton ret = err; 1001383aa543SJeff Layton return ret; 1002d5aacad5SAl Viro } 1003ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 1004ac13a829SFabian Frederick 1005ac13a829SFabian Frederick /** 1006ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 1007ac13a829SFabian Frederick * with flush 1008ac13a829SFabian Frederick * @file: file to synchronize 1009ac13a829SFabian Frederick * @start: start offset in bytes 1010ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 1011ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 1012ac13a829SFabian Frederick * 1013ac13a829SFabian Frederick */ 1014ac13a829SFabian Frederick 1015ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 1016ac13a829SFabian Frederick int datasync) 1017ac13a829SFabian Frederick { 1018ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 1019ac13a829SFabian Frederick int err; 1020ac13a829SFabian Frederick 1021ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 1022ac13a829SFabian Frederick if (err) 1023ac13a829SFabian Frederick return err; 1024ac13a829SFabian Frederick return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); 1025ac13a829SFabian Frederick } 10261b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 10271b061d92SChristoph Hellwig 102830ca22c7SPatrick J. LoPresti /** 102930ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 103030ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 103130ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 103230ca22c7SPatrick J. LoPresti * 103330ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 103430ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 103530ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 103630ca22c7SPatrick J. LoPresti */ 103730ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 103830ca22c7SPatrick J. LoPresti { 103930ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 1040a33f13efSJoel Becker u64 last_fs_page = 104109cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits); 104230ca22c7SPatrick J. LoPresti 104330ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 104430ca22c7SPatrick J. LoPresti return 0; 104530ca22c7SPatrick J. LoPresti 104609cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT)) 104730ca22c7SPatrick J. LoPresti return -EINVAL; 104830ca22c7SPatrick J. LoPresti 1049a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1050a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 105130ca22c7SPatrick J. LoPresti return -EFBIG; 105230ca22c7SPatrick J. LoPresti } 105330ca22c7SPatrick J. LoPresti return 0; 105430ca22c7SPatrick J. LoPresti } 105530ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 105630ca22c7SPatrick J. LoPresti 10571b061d92SChristoph Hellwig /* 10581b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 10591b061d92SChristoph Hellwig */ 106002c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 10611b061d92SChristoph Hellwig { 10621b061d92SChristoph Hellwig return 0; 10631b061d92SChristoph Hellwig } 10641b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 106587dc800bSAl Viro 1066f44c7763SDan Williams int noop_set_page_dirty(struct page *page) 1067f44c7763SDan Williams { 1068f44c7763SDan Williams /* 1069f44c7763SDan Williams * Unlike __set_page_dirty_no_writeback that handles dirty page 1070f44c7763SDan Williams * tracking in the page object, dax does all dirty tracking in 1071f44c7763SDan Williams * the inode address_space in response to mkwrite faults. In the 1072f44c7763SDan Williams * dax case we only need to worry about potentially dirty CPU 1073f44c7763SDan Williams * caches, not dirty page cache pages to write back. 1074f44c7763SDan Williams * 1075f44c7763SDan Williams * This callback is defined to prevent fallback to 1076f44c7763SDan Williams * __set_page_dirty_buffers() in set_page_dirty(). 1077f44c7763SDan Williams */ 1078f44c7763SDan Williams return 0; 1079f44c7763SDan Williams } 1080f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_set_page_dirty); 1081f44c7763SDan Williams 1082f44c7763SDan Williams void noop_invalidatepage(struct page *page, unsigned int offset, 1083f44c7763SDan Williams unsigned int length) 1084f44c7763SDan Williams { 1085f44c7763SDan Williams /* 1086f44c7763SDan Williams * There is no page cache to invalidate in the dax case, however 1087f44c7763SDan Williams * we need this callback defined to prevent falling back to 1088f44c7763SDan Williams * block_invalidatepage() in do_invalidatepage(). 1089f44c7763SDan Williams */ 1090f44c7763SDan Williams } 1091f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_invalidatepage); 1092f44c7763SDan Williams 1093f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 1094f44c7763SDan Williams { 1095f44c7763SDan Williams /* 1096f44c7763SDan Williams * iomap based filesystems support direct I/O without need for 1097f44c7763SDan Williams * this callback. However, it still needs to be set in 1098f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is 1099f44c7763SDan Williams * generally supported. 1100f44c7763SDan Williams */ 1101f44c7763SDan Williams return -EINVAL; 1102f44c7763SDan Williams } 1103f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO); 1104f44c7763SDan Williams 1105fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */ 1106fceef393SAl Viro void kfree_link(void *p) 110787dc800bSAl Viro { 1108fceef393SAl Viro kfree(p); 110987dc800bSAl Viro } 1110fceef393SAl Viro EXPORT_SYMBOL(kfree_link); 11116987843fSAl Viro 11126987843fSAl Viro /* 11136987843fSAl Viro * nop .set_page_dirty method so that people can use .page_mkwrite on 11146987843fSAl Viro * anon inodes. 11156987843fSAl Viro */ 11166987843fSAl Viro static int anon_set_page_dirty(struct page *page) 11176987843fSAl Viro { 11186987843fSAl Viro return 0; 11196987843fSAl Viro }; 11206987843fSAl Viro 11216987843fSAl Viro /* 11226987843fSAl Viro * A single inode exists for all anon_inode files. Contrary to pipes, 11236987843fSAl Viro * anon_inode inodes have no associated per-instance data, so we need 11246987843fSAl Viro * only allocate one of them. 11256987843fSAl Viro */ 11266987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 11276987843fSAl Viro { 11286987843fSAl Viro static const struct address_space_operations anon_aops = { 11296987843fSAl Viro .set_page_dirty = anon_set_page_dirty, 11306987843fSAl Viro }; 11316987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 11326987843fSAl Viro 11336987843fSAl Viro if (!inode) 11346987843fSAl Viro return ERR_PTR(-ENOMEM); 11356987843fSAl Viro 11366987843fSAl Viro inode->i_ino = get_next_ino(); 11376987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 11386987843fSAl Viro 11396987843fSAl Viro /* 11406987843fSAl Viro * Mark the inode dirty from the very beginning, 11416987843fSAl Viro * that way it will never be moved to the dirty 11426987843fSAl Viro * list because mark_inode_dirty() will think 11436987843fSAl Viro * that it already _is_ on the dirty list. 11446987843fSAl Viro */ 11456987843fSAl Viro inode->i_state = I_DIRTY; 11466987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 11476987843fSAl Viro inode->i_uid = current_fsuid(); 11486987843fSAl Viro inode->i_gid = current_fsgid(); 11496987843fSAl Viro inode->i_flags |= S_PRIVATE; 1150078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 11516987843fSAl Viro return inode; 11526987843fSAl Viro } 11536987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 11541c994a09SJeff Layton 11551c994a09SJeff Layton /** 11561c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 11571c994a09SJeff Layton * @filp: file pointer 11581c994a09SJeff Layton * @arg: type of lease to obtain 11591c994a09SJeff Layton * @flp: new lease supplied for insertion 1160e6f5c789SJeff Layton * @priv: private data for lm_setup operation 11611c994a09SJeff Layton * 11621c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 11631c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 11641c994a09SJeff Layton */ 11651c994a09SJeff Layton int 1166e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1167e6f5c789SJeff Layton void **priv) 11681c994a09SJeff Layton { 11691c994a09SJeff Layton return -EINVAL; 11701c994a09SJeff Layton } 11711c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 117261ba64fcSAl Viro 11736ee9706aSEric Biggers /** 11746ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks 11756ee9706aSEric Biggers * @dentry: not used here 11766ee9706aSEric Biggers * @inode: the symlink inode 11776ee9706aSEric Biggers * @done: not used here 11786ee9706aSEric Biggers * 11796ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to 11806ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called, 11816ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link 11826ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set, 11836ee9706aSEric Biggers * to mark the inode_operations as being for a symlink. 11846ee9706aSEric Biggers * 11856ee9706aSEric Biggers * Return: the symlink target 11866ee9706aSEric Biggers */ 11876b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 1188fceef393SAl Viro struct delayed_call *done) 118961ba64fcSAl Viro { 11906b255391SAl Viro return inode->i_link; 119161ba64fcSAl Viro } 11926b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 119361ba64fcSAl Viro 119461ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 11956b255391SAl Viro .get_link = simple_get_link, 119661ba64fcSAl Viro }; 119761ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1198fbabfd0fSEric W. Biederman 1199fbabfd0fSEric W. Biederman /* 1200fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1201fbabfd0fSEric W. Biederman */ 1202fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1203fbabfd0fSEric W. Biederman { 1204fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1205fbabfd0fSEric W. Biederman } 1206fbabfd0fSEric W. Biederman 1207a528d35eSDavid Howells static int empty_dir_getattr(const struct path *path, struct kstat *stat, 1208a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1209fbabfd0fSEric W. Biederman { 1210a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 1211fbabfd0fSEric W. Biederman generic_fillattr(inode, stat); 1212fbabfd0fSEric W. Biederman return 0; 1213fbabfd0fSEric W. Biederman } 1214fbabfd0fSEric W. Biederman 1215fbabfd0fSEric W. Biederman static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr) 1216fbabfd0fSEric W. Biederman { 1217fbabfd0fSEric W. Biederman return -EPERM; 1218fbabfd0fSEric W. Biederman } 1219fbabfd0fSEric W. Biederman 1220fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1221fbabfd0fSEric W. Biederman { 1222fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1223fbabfd0fSEric W. Biederman } 1224fbabfd0fSEric W. Biederman 1225fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1226fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1227fbabfd0fSEric W. Biederman .permission = generic_permission, 1228fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1229fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1230fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1231fbabfd0fSEric W. Biederman }; 1232fbabfd0fSEric W. Biederman 1233fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1234fbabfd0fSEric W. Biederman { 1235fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1236fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1237fbabfd0fSEric W. Biederman } 1238fbabfd0fSEric W. Biederman 1239fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1240fbabfd0fSEric W. Biederman { 1241fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1242fbabfd0fSEric W. Biederman return 0; 1243fbabfd0fSEric W. Biederman } 1244fbabfd0fSEric W. Biederman 1245fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1246fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1247fbabfd0fSEric W. Biederman .read = generic_read_dir, 1248c51da20cSAl Viro .iterate_shared = empty_dir_readdir, 1249fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1250fbabfd0fSEric W. Biederman }; 1251fbabfd0fSEric W. Biederman 1252fbabfd0fSEric W. Biederman 1253fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1254fbabfd0fSEric W. Biederman { 1255fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1256fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1257fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1258fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1259fbabfd0fSEric W. Biederman inode->i_rdev = 0; 12604b75de86SEric W. Biederman inode->i_size = 0; 1261fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1262fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1263fbabfd0fSEric W. Biederman 1264fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1265f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR; 1266fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1267fbabfd0fSEric W. Biederman } 1268fbabfd0fSEric W. Biederman 1269fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1270fbabfd0fSEric W. Biederman { 1271fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1272fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1273fbabfd0fSEric W. Biederman } 1274