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 */ 2031d6d5ceSDavid Howells #include <linux/fs_context.h> 2131d6d5ceSDavid Howells #include <linux/pseudo_fs.h> 22a3d1e7ebSAl Viro #include <linux/fsnotify.h> 237cf34c76SIngo Molnar 247c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 251da177e4SLinus Torvalds 26a4464dbcSAl Viro #include "internal.h" 27a4464dbcSAl Viro 28a528d35eSDavid Howells int simple_getattr(const struct path *path, struct kstat *stat, 29a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 301da177e4SLinus Torvalds { 31a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 321da177e4SLinus Torvalds generic_fillattr(inode, stat); 3309cbfeafSKirill A. Shutemov stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9); 341da177e4SLinus Torvalds return 0; 351da177e4SLinus Torvalds } 3612f38872SAl Viro EXPORT_SYMBOL(simple_getattr); 371da177e4SLinus Torvalds 38726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf) 391da177e4SLinus Torvalds { 40726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic; 4109cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 421da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 431da177e4SLinus Torvalds return 0; 441da177e4SLinus Torvalds } 4512f38872SAl Viro EXPORT_SYMBOL(simple_statfs); 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* 481da177e4SLinus Torvalds * Retaining negative dentries for an in-memory filesystem just wastes 491da177e4SLinus Torvalds * memory and lookup time: arrange for them to be deleted immediately. 501da177e4SLinus Torvalds */ 51b26d4cd3SAl Viro int always_delete_dentry(const struct dentry *dentry) 521da177e4SLinus Torvalds { 531da177e4SLinus Torvalds return 1; 541da177e4SLinus Torvalds } 55b26d4cd3SAl Viro EXPORT_SYMBOL(always_delete_dentry); 56b26d4cd3SAl Viro 57b26d4cd3SAl Viro const struct dentry_operations simple_dentry_operations = { 58b26d4cd3SAl Viro .d_delete = always_delete_dentry, 59b26d4cd3SAl Viro }; 60b26d4cd3SAl Viro EXPORT_SYMBOL(simple_dentry_operations); 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* 631da177e4SLinus Torvalds * Lookup the data. This is trivial - if the dentry didn't already 641da177e4SLinus Torvalds * exist, we know it is negative. Set d_op to delete negative dentries. 651da177e4SLinus Torvalds */ 6600cd8dd3SAl Viro struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 671da177e4SLinus Torvalds { 681da177e4SLinus Torvalds if (dentry->d_name.len > NAME_MAX) 691da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 7074931da7SAl Viro if (!dentry->d_sb->s_d_op) 71fb045adbSNick Piggin d_set_d_op(dentry, &simple_dentry_operations); 721da177e4SLinus Torvalds d_add(dentry, NULL); 731da177e4SLinus Torvalds return NULL; 741da177e4SLinus Torvalds } 7512f38872SAl Viro EXPORT_SYMBOL(simple_lookup); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file) 781da177e4SLinus Torvalds { 79ba65dc5eSAl Viro file->private_data = d_alloc_cursor(file->f_path.dentry); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds return file->private_data ? 0 : -ENOMEM; 821da177e4SLinus Torvalds } 8312f38872SAl Viro EXPORT_SYMBOL(dcache_dir_open); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds dput(file->private_data); 881da177e4SLinus Torvalds return 0; 891da177e4SLinus Torvalds } 9012f38872SAl Viro EXPORT_SYMBOL(dcache_dir_close); 911da177e4SLinus Torvalds 924f42c1b5SAl Viro /* parent is locked at least shared */ 93d4f4de5eSAl Viro /* 94d4f4de5eSAl Viro * Returns an element of siblings' list. 95d4f4de5eSAl Viro * We are looking for <count>th positive after <p>; if 9626b6c984SAl Viro * found, dentry is grabbed and returned to caller. 9726b6c984SAl Viro * If no such element exists, NULL is returned. 98d4f4de5eSAl Viro */ 9926b6c984SAl Viro static struct dentry *scan_positives(struct dentry *cursor, 100d4f4de5eSAl Viro struct list_head *p, 101d4f4de5eSAl Viro loff_t count, 10226b6c984SAl Viro struct dentry *last) 1034f42c1b5SAl Viro { 104d4f4de5eSAl Viro struct dentry *dentry = cursor->d_parent, *found = NULL; 1054f42c1b5SAl Viro 106d4f4de5eSAl Viro spin_lock(&dentry->d_lock); 107d4f4de5eSAl Viro while ((p = p->next) != &dentry->d_subdirs) { 1084f42c1b5SAl Viro struct dentry *d = list_entry(p, struct dentry, d_child); 109d4f4de5eSAl Viro // we must at least skip cursors, to avoid livelocks 110d4f4de5eSAl Viro if (d->d_flags & DCACHE_DENTRY_CURSOR) 111d4f4de5eSAl Viro continue; 112d4f4de5eSAl Viro if (simple_positive(d) && !--count) { 113d4f4de5eSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); 114d4f4de5eSAl Viro if (simple_positive(d)) 115d4f4de5eSAl Viro found = dget_dlock(d); 116d4f4de5eSAl Viro spin_unlock(&d->d_lock); 117d4f4de5eSAl Viro if (likely(found)) 1184f42c1b5SAl Viro break; 119d4f4de5eSAl Viro count = 1; 120d4f4de5eSAl Viro } 121d4f4de5eSAl Viro if (need_resched()) { 122d4f4de5eSAl Viro list_move(&cursor->d_child, p); 123d4f4de5eSAl Viro p = &cursor->d_child; 124d4f4de5eSAl Viro spin_unlock(&dentry->d_lock); 125d4f4de5eSAl Viro cond_resched(); 126d4f4de5eSAl Viro spin_lock(&dentry->d_lock); 1274f42c1b5SAl Viro } 1284f42c1b5SAl Viro } 129d4f4de5eSAl Viro spin_unlock(&dentry->d_lock); 13026b6c984SAl Viro dput(last); 13126b6c984SAl Viro return found; 1324f42c1b5SAl Viro } 1334f42c1b5SAl Viro 134965c8e59SAndrew Morton loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) 1351da177e4SLinus Torvalds { 1362fd6b7f5SNick Piggin struct dentry *dentry = file->f_path.dentry; 137965c8e59SAndrew Morton switch (whence) { 1381da177e4SLinus Torvalds case 1: 1391da177e4SLinus Torvalds offset += file->f_pos; 1400a4c9265SGustavo A. R. Silva /* fall through */ 1411da177e4SLinus Torvalds case 0: 1421da177e4SLinus Torvalds if (offset >= 0) 1431da177e4SLinus Torvalds break; 1440a4c9265SGustavo A. R. Silva /* fall through */ 1451da177e4SLinus Torvalds default: 1461da177e4SLinus Torvalds return -EINVAL; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds if (offset != file->f_pos) { 1491da177e4SLinus Torvalds struct dentry *cursor = file->private_data; 150d4f4de5eSAl Viro struct dentry *to = NULL; 1511da177e4SLinus Torvalds 152274f5b04SAl Viro inode_lock_shared(dentry->d_inode); 153d4f4de5eSAl Viro 15426b6c984SAl Viro if (offset > 2) 15526b6c984SAl Viro to = scan_positives(cursor, &dentry->d_subdirs, 15626b6c984SAl Viro offset - 2, NULL); 157d4f4de5eSAl Viro spin_lock(&dentry->d_lock); 15826b6c984SAl Viro if (to) 15926b6c984SAl Viro list_move(&cursor->d_child, &to->d_child); 16026b6c984SAl Viro else 161d4f4de5eSAl Viro list_del_init(&cursor->d_child); 162d4f4de5eSAl Viro spin_unlock(&dentry->d_lock); 163d4f4de5eSAl Viro dput(to); 164d4f4de5eSAl Viro 16526b6c984SAl Viro file->f_pos = offset; 16626b6c984SAl Viro 167d4f4de5eSAl Viro inode_unlock_shared(dentry->d_inode); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds return offset; 1701da177e4SLinus Torvalds } 17112f38872SAl Viro EXPORT_SYMBOL(dcache_dir_lseek); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */ 1741da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* 1801da177e4SLinus Torvalds * Directory is locked and all positive dentries in it are safe, since 1811da177e4SLinus Torvalds * for ramfs-type trees they can't go away without unlink() or rmdir(), 1821da177e4SLinus Torvalds * both impossible due to the lock on directory. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds 1855f99f4e7SAl Viro int dcache_readdir(struct file *file, struct dir_context *ctx) 1861da177e4SLinus Torvalds { 1875f99f4e7SAl Viro struct dentry *dentry = file->f_path.dentry; 1885f99f4e7SAl Viro struct dentry *cursor = file->private_data; 189d4f4de5eSAl Viro struct list_head *anchor = &dentry->d_subdirs; 190d4f4de5eSAl Viro struct dentry *next = NULL; 191d4f4de5eSAl Viro struct list_head *p; 1921da177e4SLinus Torvalds 1935f99f4e7SAl Viro if (!dir_emit_dots(file, ctx)) 1945f99f4e7SAl Viro return 0; 1954f42c1b5SAl Viro 1965f99f4e7SAl Viro if (ctx->pos == 2) 197d4f4de5eSAl Viro p = anchor; 19826b6c984SAl Viro else if (!list_empty(&cursor->d_child)) 199d4f4de5eSAl Viro p = &cursor->d_child; 20026b6c984SAl Viro else 20126b6c984SAl Viro return 0; 202d4f4de5eSAl Viro 20326b6c984SAl Viro while ((next = scan_positives(cursor, p, 1, next)) != NULL) { 2045f99f4e7SAl Viro if (!dir_emit(ctx, next->d_name.name, next->d_name.len, 205dea655c2SDavid Howells d_inode(next)->i_ino, dt_type(d_inode(next)))) 2064f42c1b5SAl Viro break; 2075f99f4e7SAl Viro ctx->pos++; 20826b6c984SAl Viro p = &next->d_child; 2091da177e4SLinus Torvalds } 210d4f4de5eSAl Viro spin_lock(&dentry->d_lock); 21126b6c984SAl Viro if (next) 21226b6c984SAl Viro list_move_tail(&cursor->d_child, &next->d_child); 21326b6c984SAl Viro else 21426b6c984SAl Viro list_del_init(&cursor->d_child); 215d4f4de5eSAl Viro spin_unlock(&dentry->d_lock); 216d4f4de5eSAl Viro dput(next); 217d4f4de5eSAl Viro 2181da177e4SLinus Torvalds return 0; 2191da177e4SLinus Torvalds } 22012f38872SAl Viro EXPORT_SYMBOL(dcache_readdir); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds return -EISDIR; 2251da177e4SLinus Torvalds } 22612f38872SAl Viro EXPORT_SYMBOL(generic_read_dir); 2271da177e4SLinus Torvalds 2284b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = { 2291da177e4SLinus Torvalds .open = dcache_dir_open, 2301da177e4SLinus Torvalds .release = dcache_dir_close, 2311da177e4SLinus Torvalds .llseek = dcache_dir_lseek, 2321da177e4SLinus Torvalds .read = generic_read_dir, 2334e82901cSAl Viro .iterate_shared = dcache_readdir, 2341b061d92SChristoph Hellwig .fsync = noop_fsync, 2351da177e4SLinus Torvalds }; 23612f38872SAl Viro EXPORT_SYMBOL(simple_dir_operations); 2371da177e4SLinus Torvalds 23892e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = { 2391da177e4SLinus Torvalds .lookup = simple_lookup, 2401da177e4SLinus Torvalds }; 24112f38872SAl Viro EXPORT_SYMBOL(simple_dir_inode_operations); 2421da177e4SLinus Torvalds 243a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev) 244a3d1e7ebSAl Viro { 245a3d1e7ebSAl Viro struct dentry *child = NULL; 246a3d1e7ebSAl Viro struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs; 247a3d1e7ebSAl Viro 248a3d1e7ebSAl Viro spin_lock(&parent->d_lock); 249a3d1e7ebSAl Viro while ((p = p->next) != &parent->d_subdirs) { 250a3d1e7ebSAl Viro struct dentry *d = container_of(p, struct dentry, d_child); 251a3d1e7ebSAl Viro if (simple_positive(d)) { 252a3d1e7ebSAl Viro spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED); 253a3d1e7ebSAl Viro if (simple_positive(d)) 254a3d1e7ebSAl Viro child = dget_dlock(d); 255a3d1e7ebSAl Viro spin_unlock(&d->d_lock); 256a3d1e7ebSAl Viro if (likely(child)) 257a3d1e7ebSAl Viro break; 258a3d1e7ebSAl Viro } 259a3d1e7ebSAl Viro } 260a3d1e7ebSAl Viro spin_unlock(&parent->d_lock); 261a3d1e7ebSAl Viro dput(prev); 262a3d1e7ebSAl Viro return child; 263a3d1e7ebSAl Viro } 264a3d1e7ebSAl Viro 265a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry, 266a3d1e7ebSAl Viro void (*callback)(struct dentry *)) 267a3d1e7ebSAl Viro { 268a3d1e7ebSAl Viro struct dentry *this = dget(dentry); 269a3d1e7ebSAl Viro while (true) { 270a3d1e7ebSAl Viro struct dentry *victim = NULL, *child; 271a3d1e7ebSAl Viro struct inode *inode = this->d_inode; 272a3d1e7ebSAl Viro 273a3d1e7ebSAl Viro inode_lock(inode); 274a3d1e7ebSAl Viro if (d_is_dir(this)) 275a3d1e7ebSAl Viro inode->i_flags |= S_DEAD; 276a3d1e7ebSAl Viro while ((child = find_next_child(this, victim)) == NULL) { 277a3d1e7ebSAl Viro // kill and ascend 278a3d1e7ebSAl Viro // update metadata while it's still locked 279a3d1e7ebSAl Viro inode->i_ctime = current_time(inode); 280a3d1e7ebSAl Viro clear_nlink(inode); 281a3d1e7ebSAl Viro inode_unlock(inode); 282a3d1e7ebSAl Viro victim = this; 283a3d1e7ebSAl Viro this = this->d_parent; 284a3d1e7ebSAl Viro inode = this->d_inode; 285a3d1e7ebSAl Viro inode_lock(inode); 286a3d1e7ebSAl Viro if (simple_positive(victim)) { 287a3d1e7ebSAl Viro d_invalidate(victim); // avoid lost mounts 288a3d1e7ebSAl Viro if (d_is_dir(victim)) 289a3d1e7ebSAl Viro fsnotify_rmdir(inode, victim); 290a3d1e7ebSAl Viro else 291a3d1e7ebSAl Viro fsnotify_unlink(inode, victim); 292a3d1e7ebSAl Viro if (callback) 293a3d1e7ebSAl Viro callback(victim); 294a3d1e7ebSAl Viro dput(victim); // unpin it 295a3d1e7ebSAl Viro } 296a3d1e7ebSAl Viro if (victim == dentry) { 297a3d1e7ebSAl Viro inode->i_ctime = inode->i_mtime = 298a3d1e7ebSAl Viro current_time(inode); 299a3d1e7ebSAl Viro if (d_is_dir(dentry)) 300a3d1e7ebSAl Viro drop_nlink(inode); 301a3d1e7ebSAl Viro inode_unlock(inode); 302a3d1e7ebSAl Viro dput(dentry); 303a3d1e7ebSAl Viro return; 304a3d1e7ebSAl Viro } 305a3d1e7ebSAl Viro } 306a3d1e7ebSAl Viro inode_unlock(inode); 307a3d1e7ebSAl Viro this = child; 308a3d1e7ebSAl Viro } 309a3d1e7ebSAl Viro } 310a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal); 311a3d1e7ebSAl Viro 312759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 313759b9775SHugh Dickins .statfs = simple_statfs, 314759b9775SHugh Dickins }; 315759b9775SHugh Dickins 316db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc) 3171da177e4SLinus Torvalds { 31831d6d5ceSDavid Howells struct pseudo_fs_context *ctx = fc->fs_private; 3191da177e4SLinus Torvalds struct inode *root; 3201da177e4SLinus Torvalds 32189a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 3223971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 3233971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 3248d9e46d8SAl Viro s->s_magic = ctx->magic; 3258d9e46d8SAl Viro s->s_op = ctx->ops ?: &simple_super_operations; 3268d9e46d8SAl Viro s->s_xattr = ctx->xattr; 3271da177e4SLinus Torvalds s->s_time_gran = 1; 3281da177e4SLinus Torvalds root = new_inode(s); 3291da177e4SLinus Torvalds if (!root) 330db2c246aSDavid Howells return -ENOMEM; 331db2c246aSDavid Howells 3321a1c9bb4SJeff Layton /* 3331a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 3341a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 3351a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 3361a1c9bb4SJeff Layton */ 3371a1c9bb4SJeff Layton root->i_ino = 1; 3381da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 339078cd827SDeepa Dinamani root->i_atime = root->i_mtime = root->i_ctime = current_time(root); 3408d9e46d8SAl Viro s->s_root = d_make_root(root); 3418d9e46d8SAl Viro if (!s->s_root) 3428d9e46d8SAl Viro return -ENOMEM; 343db2c246aSDavid Howells s->s_d_op = ctx->dops; 344db2c246aSDavid Howells return 0; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc) 348db2c246aSDavid Howells { 3492ac295d4SAl Viro return get_tree_nodev(fc, pseudo_fs_fill_super); 3501da177e4SLinus Torvalds } 35131d6d5ceSDavid Howells 35231d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc) 35331d6d5ceSDavid Howells { 35431d6d5ceSDavid Howells kfree(fc->fs_private); 35531d6d5ceSDavid Howells } 35631d6d5ceSDavid Howells 35731d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = { 35831d6d5ceSDavid Howells .free = pseudo_fs_free, 35931d6d5ceSDavid Howells .get_tree = pseudo_fs_get_tree, 36031d6d5ceSDavid Howells }; 36131d6d5ceSDavid Howells 36231d6d5ceSDavid Howells /* 36331d6d5ceSDavid Howells * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 36431d6d5ceSDavid Howells * will never be mountable) 36531d6d5ceSDavid Howells */ 36631d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc, 36731d6d5ceSDavid Howells unsigned long magic) 36831d6d5ceSDavid Howells { 36931d6d5ceSDavid Howells struct pseudo_fs_context *ctx; 37031d6d5ceSDavid Howells 37131d6d5ceSDavid Howells ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL); 37231d6d5ceSDavid Howells if (likely(ctx)) { 37331d6d5ceSDavid Howells ctx->magic = magic; 37431d6d5ceSDavid Howells fc->fs_private = ctx; 37531d6d5ceSDavid Howells fc->ops = &pseudo_fs_context_ops; 376db2c246aSDavid Howells fc->sb_flags |= SB_NOUSER; 377db2c246aSDavid Howells fc->global = true; 37831d6d5ceSDavid Howells } 37931d6d5ceSDavid Howells return ctx; 38031d6d5ceSDavid Howells } 38131d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo); 3821da177e4SLinus Torvalds 38320955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 38420955e89SStephen Boyd { 38520955e89SStephen Boyd if (inode->i_private) 38620955e89SStephen Boyd file->private_data = inode->i_private; 38720955e89SStephen Boyd return 0; 38820955e89SStephen Boyd } 38912f38872SAl Viro EXPORT_SYMBOL(simple_open); 39020955e89SStephen Boyd 3911da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 3921da177e4SLinus Torvalds { 393dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 3941da177e4SLinus Torvalds 395078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 396d8c76e6fSDave Hansen inc_nlink(inode); 3977de9c6eeSAl Viro ihold(inode); 3981da177e4SLinus Torvalds dget(dentry); 3991da177e4SLinus Torvalds d_instantiate(dentry, inode); 4001da177e4SLinus Torvalds return 0; 4011da177e4SLinus Torvalds } 40212f38872SAl Viro EXPORT_SYMBOL(simple_link); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct dentry *child; 4071da177e4SLinus Torvalds int ret = 0; 4081da177e4SLinus Torvalds 4092fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 410946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, d_child) { 411da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 412da502956SNick Piggin if (simple_positive(child)) { 413da502956SNick Piggin spin_unlock(&child->d_lock); 4141da177e4SLinus Torvalds goto out; 415da502956SNick Piggin } 416da502956SNick Piggin spin_unlock(&child->d_lock); 417da502956SNick Piggin } 4181da177e4SLinus Torvalds ret = 1; 4191da177e4SLinus Torvalds out: 4202fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 4211da177e4SLinus Torvalds return ret; 4221da177e4SLinus Torvalds } 42312f38872SAl Viro EXPORT_SYMBOL(simple_empty); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 4261da177e4SLinus Torvalds { 427dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 4281da177e4SLinus Torvalds 429078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 4309a53c3a7SDave Hansen drop_nlink(inode); 4311da177e4SLinus Torvalds dput(dentry); 4321da177e4SLinus Torvalds return 0; 4331da177e4SLinus Torvalds } 43412f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 4371da177e4SLinus Torvalds { 4381da177e4SLinus Torvalds if (!simple_empty(dentry)) 4391da177e4SLinus Torvalds return -ENOTEMPTY; 4401da177e4SLinus Torvalds 441dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 4421da177e4SLinus Torvalds simple_unlink(dir, dentry); 4439a53c3a7SDave Hansen drop_nlink(dir); 4441da177e4SLinus Torvalds return 0; 4451da177e4SLinus Torvalds } 44612f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry, 449e0e0be8aSMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry, 450e0e0be8aSMiklos Szeredi unsigned int flags) 4511da177e4SLinus Torvalds { 452dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 453e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 4541da177e4SLinus Torvalds 455e0e0be8aSMiklos Szeredi if (flags & ~RENAME_NOREPLACE) 456e0e0be8aSMiklos Szeredi return -EINVAL; 457e0e0be8aSMiklos Szeredi 4581da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 4591da177e4SLinus Torvalds return -ENOTEMPTY; 4601da177e4SLinus Torvalds 461dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 4621da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 463841590ceSAl Viro if (they_are_dirs) { 464dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 4659a53c3a7SDave Hansen drop_nlink(old_dir); 466841590ceSAl Viro } 4671da177e4SLinus Torvalds } else if (they_are_dirs) { 4689a53c3a7SDave Hansen drop_nlink(old_dir); 469d8c76e6fSDave Hansen inc_nlink(new_dir); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 473078cd827SDeepa Dinamani new_dir->i_mtime = inode->i_ctime = current_time(old_dir); 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds return 0; 4761da177e4SLinus Torvalds } 47712f38872SAl Viro EXPORT_SYMBOL(simple_rename); 4781da177e4SLinus Torvalds 4797bb46a67Snpiggin@suse.de /** 480eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 4817bb46a67Snpiggin@suse.de * @dentry: dentry 4827bb46a67Snpiggin@suse.de * @iattr: iattr structure 4837bb46a67Snpiggin@suse.de * 4847bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 4857bb46a67Snpiggin@suse.de * 486eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 487eef2380cSChristoph Hellwig * implementation of size changes. 488eef2380cSChristoph Hellwig * 489eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 490eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 491eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 4927bb46a67Snpiggin@suse.de */ 4937bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr) 4947bb46a67Snpiggin@suse.de { 495dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 4967bb46a67Snpiggin@suse.de int error; 4977bb46a67Snpiggin@suse.de 49831051c85SJan Kara error = setattr_prepare(dentry, iattr); 4997bb46a67Snpiggin@suse.de if (error) 5007bb46a67Snpiggin@suse.de return error; 5017bb46a67Snpiggin@suse.de 5022c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 5032c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 5046a1a90adSChristoph Hellwig setattr_copy(inode, iattr); 505eef2380cSChristoph Hellwig mark_inode_dirty(inode); 506eef2380cSChristoph Hellwig return 0; 5077bb46a67Snpiggin@suse.de } 5087bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 5097bb46a67Snpiggin@suse.de 5101da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page) 5111da177e4SLinus Torvalds { 512c0d92cbcSPekka J Enberg clear_highpage(page); 5131da177e4SLinus Torvalds flush_dcache_page(page); 5141da177e4SLinus Torvalds SetPageUptodate(page); 5151da177e4SLinus Torvalds unlock_page(page); 5161da177e4SLinus Torvalds return 0; 5171da177e4SLinus Torvalds } 51812f38872SAl Viro EXPORT_SYMBOL(simple_readpage); 5191da177e4SLinus Torvalds 520afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 521afddba49SNick Piggin loff_t pos, unsigned len, unsigned flags, 522afddba49SNick Piggin struct page **pagep, void **fsdata) 523afddba49SNick Piggin { 524afddba49SNick Piggin struct page *page; 525afddba49SNick Piggin pgoff_t index; 526afddba49SNick Piggin 52709cbfeafSKirill A. Shutemov index = pos >> PAGE_SHIFT; 528afddba49SNick Piggin 52954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 530afddba49SNick Piggin if (!page) 531afddba49SNick Piggin return -ENOMEM; 532afddba49SNick Piggin 533afddba49SNick Piggin *pagep = page; 534afddba49SNick Piggin 53509cbfeafSKirill A. Shutemov if (!PageUptodate(page) && (len != PAGE_SIZE)) { 53609cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 537193cf4b9SBoaz Harrosh 53809cbfeafSKirill A. Shutemov zero_user_segments(page, 0, from, from + len, PAGE_SIZE); 539193cf4b9SBoaz Harrosh } 540193cf4b9SBoaz Harrosh return 0; 541afddba49SNick Piggin } 54212f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 543afddba49SNick Piggin 544ad2a722fSBoaz Harrosh /** 545ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 5468e88bfbaSRandy Dunlap * @file: See .write_end of address_space_operations 547ad2a722fSBoaz Harrosh * @mapping: " 548ad2a722fSBoaz Harrosh * @pos: " 549ad2a722fSBoaz Harrosh * @len: " 550ad2a722fSBoaz Harrosh * @copied: " 551ad2a722fSBoaz Harrosh * @page: " 552ad2a722fSBoaz Harrosh * @fsdata: " 553ad2a722fSBoaz Harrosh * 554ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 555ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 556ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 557ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 558ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 559ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 560ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 561ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 562ad2a722fSBoaz Harrosh * case that i_size has changed. 56304fff641SAl Viro * 56404fff641SAl Viro * Use *ONLY* with simple_readpage() 565ad2a722fSBoaz Harrosh */ 566ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping, 567ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 568ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 5691da177e4SLinus Torvalds { 5701da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 571ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 572ad2a722fSBoaz Harrosh 573ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 57404fff641SAl Viro if (!PageUptodate(page)) { 575ad2a722fSBoaz Harrosh if (copied < len) { 57609cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 577ad2a722fSBoaz Harrosh 578ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 579ad2a722fSBoaz Harrosh } 580955eff5aSNick Piggin SetPageUptodate(page); 58104fff641SAl Viro } 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 5841b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 5851da177e4SLinus Torvalds */ 586ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 587ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 588ad2a722fSBoaz Harrosh 5891da177e4SLinus Torvalds set_page_dirty(page); 590afddba49SNick Piggin unlock_page(page); 59109cbfeafSKirill A. Shutemov put_page(page); 592afddba49SNick Piggin 593afddba49SNick Piggin return copied; 594afddba49SNick Piggin } 59512f38872SAl Viro EXPORT_SYMBOL(simple_write_end); 596afddba49SNick Piggin 5971a1c9bb4SJeff Layton /* 5981a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 5991a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 6001a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 6011a1c9bb4SJeff Layton */ 6027d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 603cda37124SEric Biggers const struct tree_descr *files) 6041da177e4SLinus Torvalds { 6051da177e4SLinus Torvalds struct inode *inode; 6061da177e4SLinus Torvalds struct dentry *root; 6071da177e4SLinus Torvalds struct dentry *dentry; 6081da177e4SLinus Torvalds int i; 6091da177e4SLinus Torvalds 61009cbfeafSKirill A. Shutemov s->s_blocksize = PAGE_SIZE; 61109cbfeafSKirill A. Shutemov s->s_blocksize_bits = PAGE_SHIFT; 6121da177e4SLinus Torvalds s->s_magic = magic; 613759b9775SHugh Dickins s->s_op = &simple_super_operations; 6141da177e4SLinus Torvalds s->s_time_gran = 1; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds inode = new_inode(s); 6171da177e4SLinus Torvalds if (!inode) 6181da177e4SLinus Torvalds return -ENOMEM; 6191a1c9bb4SJeff Layton /* 6201a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 6211a1c9bb4SJeff Layton * entry at index 1 6221a1c9bb4SJeff Layton */ 6231a1c9bb4SJeff Layton inode->i_ino = 1; 6241da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 625078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 6261da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 6271da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 628bfe86848SMiklos Szeredi set_nlink(inode, 2); 62948fde701SAl Viro root = d_make_root(inode); 63048fde701SAl Viro if (!root) 6311da177e4SLinus Torvalds return -ENOMEM; 6321da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 6331da177e4SLinus Torvalds if (!files->name) 6341da177e4SLinus Torvalds continue; 6351a1c9bb4SJeff Layton 6361a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 6371a1c9bb4SJeff Layton if (unlikely(i == 1)) 6381a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 6391a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 6401a1c9bb4SJeff Layton s->s_type->name); 6411a1c9bb4SJeff Layton 6421da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 6431da177e4SLinus Torvalds if (!dentry) 6441da177e4SLinus Torvalds goto out; 6451da177e4SLinus Torvalds inode = new_inode(s); 64632096ea1SKonstantin Khlebnikov if (!inode) { 64732096ea1SKonstantin Khlebnikov dput(dentry); 6481da177e4SLinus Torvalds goto out; 64932096ea1SKonstantin Khlebnikov } 6501da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 651078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 6521da177e4SLinus Torvalds inode->i_fop = files->ops; 6531da177e4SLinus Torvalds inode->i_ino = i; 6541da177e4SLinus Torvalds d_add(dentry, inode); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds s->s_root = root; 6571da177e4SLinus Torvalds return 0; 6581da177e4SLinus Torvalds out: 6591da177e4SLinus Torvalds d_genocide(root); 660640946f2SAl Viro shrink_dcache_parent(root); 6611da177e4SLinus Torvalds dput(root); 6621da177e4SLinus Torvalds return -ENOMEM; 6631da177e4SLinus Torvalds } 66412f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 6671da177e4SLinus Torvalds 6681f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 6691da177e4SLinus Torvalds { 6701da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 6711da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 6721da177e4SLinus Torvalds if (unlikely(!*mount)) { 6731da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 6741751e8a6SLinus Torvalds mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL); 6751da177e4SLinus Torvalds if (IS_ERR(mnt)) 6761da177e4SLinus Torvalds return PTR_ERR(mnt); 6771da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 6781da177e4SLinus Torvalds if (!*mount) 6791da177e4SLinus Torvalds *mount = mnt; 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds mntget(*mount); 6821da177e4SLinus Torvalds ++*count; 6831da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 6841da177e4SLinus Torvalds mntput(mnt); 6851da177e4SLinus Torvalds return 0; 6861da177e4SLinus Torvalds } 68712f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds struct vfsmount *mnt; 6921da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 6931da177e4SLinus Torvalds mnt = *mount; 6941da177e4SLinus Torvalds if (!--*count) 6951da177e4SLinus Torvalds *mount = NULL; 6961da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 6971da177e4SLinus Torvalds mntput(mnt); 6981da177e4SLinus Torvalds } 69912f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 7001da177e4SLinus Torvalds 7016d1029b5SAkinobu Mita /** 7026d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 7036d1029b5SAkinobu Mita * @to: the user space buffer to read to 7046d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 7056d1029b5SAkinobu Mita * @ppos: the current position in the buffer 7066d1029b5SAkinobu Mita * @from: the buffer to read from 7076d1029b5SAkinobu Mita * @available: the size of the buffer 7086d1029b5SAkinobu Mita * 7096d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 7106d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 7116d1029b5SAkinobu Mita * 7126d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 7136d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 7146d1029b5SAkinobu Mita **/ 7151da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 7161da177e4SLinus Torvalds const void *from, size_t available) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds loff_t pos = *ppos; 71914be2746SSteven Rostedt size_t ret; 72014be2746SSteven Rostedt 7211da177e4SLinus Torvalds if (pos < 0) 7221da177e4SLinus Torvalds return -EINVAL; 72314be2746SSteven Rostedt if (pos >= available || !count) 7241da177e4SLinus Torvalds return 0; 7251da177e4SLinus Torvalds if (count > available - pos) 7261da177e4SLinus Torvalds count = available - pos; 72714be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 72814be2746SSteven Rostedt if (ret == count) 7291da177e4SLinus Torvalds return -EFAULT; 73014be2746SSteven Rostedt count -= ret; 7311da177e4SLinus Torvalds *ppos = pos + count; 7321da177e4SLinus Torvalds return count; 7331da177e4SLinus Torvalds } 73412f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 7351da177e4SLinus Torvalds 7366d1029b5SAkinobu Mita /** 7376a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 7386a727b43SJiri Slaby * @to: the buffer to write to 7396a727b43SJiri Slaby * @available: the size of the buffer 7406a727b43SJiri Slaby * @ppos: the current position in the buffer 7416a727b43SJiri Slaby * @from: the user space buffer to read from 7426a727b43SJiri Slaby * @count: the maximum number of bytes to read 7436a727b43SJiri Slaby * 7446a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 7456a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 7466a727b43SJiri Slaby * 7476a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 7486a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 7496a727b43SJiri Slaby **/ 7506a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 7516a727b43SJiri Slaby const void __user *from, size_t count) 7526a727b43SJiri Slaby { 7536a727b43SJiri Slaby loff_t pos = *ppos; 7546a727b43SJiri Slaby size_t res; 7556a727b43SJiri Slaby 7566a727b43SJiri Slaby if (pos < 0) 7576a727b43SJiri Slaby return -EINVAL; 7586a727b43SJiri Slaby if (pos >= available || !count) 7596a727b43SJiri Slaby return 0; 7606a727b43SJiri Slaby if (count > available - pos) 7616a727b43SJiri Slaby count = available - pos; 7626a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 7636a727b43SJiri Slaby if (res == count) 7646a727b43SJiri Slaby return -EFAULT; 7656a727b43SJiri Slaby count -= res; 7666a727b43SJiri Slaby *ppos = pos + count; 7676a727b43SJiri Slaby return count; 7686a727b43SJiri Slaby } 76912f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 7706a727b43SJiri Slaby 7716a727b43SJiri Slaby /** 7726d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 7736d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 7746d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 7756d1029b5SAkinobu Mita * @ppos: the current position in the buffer 7766d1029b5SAkinobu Mita * @from: the buffer to read from 7776d1029b5SAkinobu Mita * @available: the size of the buffer 7786d1029b5SAkinobu Mita * 7796d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 7806d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 7816d1029b5SAkinobu Mita * 7826d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 7836d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 7846d1029b5SAkinobu Mita **/ 78593b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 78693b07113SAkinobu Mita const void *from, size_t available) 78793b07113SAkinobu Mita { 78893b07113SAkinobu Mita loff_t pos = *ppos; 78993b07113SAkinobu Mita 79093b07113SAkinobu Mita if (pos < 0) 79193b07113SAkinobu Mita return -EINVAL; 79293b07113SAkinobu Mita if (pos >= available) 79393b07113SAkinobu Mita return 0; 79493b07113SAkinobu Mita if (count > available - pos) 79593b07113SAkinobu Mita count = available - pos; 79693b07113SAkinobu Mita memcpy(to, from + pos, count); 79793b07113SAkinobu Mita *ppos = pos + count; 79893b07113SAkinobu Mita 79993b07113SAkinobu Mita return count; 80093b07113SAkinobu Mita } 80112f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 80293b07113SAkinobu Mita 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * Transaction based IO. 8051da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 8061da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 8071da177e4SLinus Torvalds * file-local buffer. 8081da177e4SLinus Torvalds */ 80976791ab2SIngo Molnar 81076791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 81176791ab2SIngo Molnar { 81276791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 81376791ab2SIngo Molnar 81476791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 81576791ab2SIngo Molnar 81676791ab2SIngo Molnar /* 81776791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 81876791ab2SIngo Molnar * ar->data is ready for reading. 81976791ab2SIngo Molnar */ 82076791ab2SIngo Molnar smp_mb(); 82176791ab2SIngo Molnar ar->size = n; 82276791ab2SIngo Molnar } 82312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 82476791ab2SIngo Molnar 8251da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 8281da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 8311da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 8321da177e4SLinus Torvalds 8331da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 8341da177e4SLinus Torvalds if (!ar) 8351da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* only one write allowed per open */ 8401da177e4SLinus Torvalds if (file->private_data) { 8411da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 8421da177e4SLinus Torvalds free_page((unsigned long)ar); 8431da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds file->private_data = ar; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 8511da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds return ar->data; 8541da177e4SLinus Torvalds } 85512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds if (!ar) 8621da177e4SLinus Torvalds return 0; 8631da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 8641da177e4SLinus Torvalds } 86512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 8701da177e4SLinus Torvalds return 0; 8711da177e4SLinus Torvalds } 87212f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 8731da177e4SLinus Torvalds 874acaefc25SArnd Bergmann /* Simple attribute files */ 875acaefc25SArnd Bergmann 876acaefc25SArnd Bergmann struct simple_attr { 8778b88b099SChristoph Hellwig int (*get)(void *, u64 *); 8788b88b099SChristoph Hellwig int (*set)(void *, u64); 879acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 880acaefc25SArnd Bergmann char set_buf[24]; 881acaefc25SArnd Bergmann void *data; 882acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 8837cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 884acaefc25SArnd Bergmann }; 885acaefc25SArnd Bergmann 886acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 887acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 888acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 8898b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 890acaefc25SArnd Bergmann const char *fmt) 891acaefc25SArnd Bergmann { 892acaefc25SArnd Bergmann struct simple_attr *attr; 893acaefc25SArnd Bergmann 894a65cab7dSEric Biggers attr = kzalloc(sizeof(*attr), GFP_KERNEL); 895acaefc25SArnd Bergmann if (!attr) 896acaefc25SArnd Bergmann return -ENOMEM; 897acaefc25SArnd Bergmann 898acaefc25SArnd Bergmann attr->get = get; 899acaefc25SArnd Bergmann attr->set = set; 9008e18e294STheodore Ts'o attr->data = inode->i_private; 901acaefc25SArnd Bergmann attr->fmt = fmt; 9027cf34c76SIngo Molnar mutex_init(&attr->mutex); 903acaefc25SArnd Bergmann 904acaefc25SArnd Bergmann file->private_data = attr; 905acaefc25SArnd Bergmann 906acaefc25SArnd Bergmann return nonseekable_open(inode, file); 907acaefc25SArnd Bergmann } 90812f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 909acaefc25SArnd Bergmann 91074bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 911acaefc25SArnd Bergmann { 912acaefc25SArnd Bergmann kfree(file->private_data); 913acaefc25SArnd Bergmann return 0; 914acaefc25SArnd Bergmann } 91512f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 916acaefc25SArnd Bergmann 917acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 918acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 919acaefc25SArnd Bergmann size_t len, loff_t *ppos) 920acaefc25SArnd Bergmann { 921acaefc25SArnd Bergmann struct simple_attr *attr; 922acaefc25SArnd Bergmann size_t size; 923acaefc25SArnd Bergmann ssize_t ret; 924acaefc25SArnd Bergmann 925acaefc25SArnd Bergmann attr = file->private_data; 926acaefc25SArnd Bergmann 927acaefc25SArnd Bergmann if (!attr->get) 928acaefc25SArnd Bergmann return -EACCES; 929acaefc25SArnd Bergmann 9309261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 9319261303aSChristoph Hellwig if (ret) 9329261303aSChristoph Hellwig return ret; 9339261303aSChristoph Hellwig 934a65cab7dSEric Biggers if (*ppos && attr->get_buf[0]) { 935a65cab7dSEric Biggers /* continued read */ 936acaefc25SArnd Bergmann size = strlen(attr->get_buf); 937a65cab7dSEric Biggers } else { 938a65cab7dSEric Biggers /* first read */ 9398b88b099SChristoph Hellwig u64 val; 9408b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 9418b88b099SChristoph Hellwig if (ret) 9428b88b099SChristoph Hellwig goto out; 9438b88b099SChristoph Hellwig 944acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 9458b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 9468b88b099SChristoph Hellwig } 947acaefc25SArnd Bergmann 948acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 9498b88b099SChristoph Hellwig out: 9507cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 951acaefc25SArnd Bergmann return ret; 952acaefc25SArnd Bergmann } 95312f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 954acaefc25SArnd Bergmann 955acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 956acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf, 957acaefc25SArnd Bergmann size_t len, loff_t *ppos) 958acaefc25SArnd Bergmann { 959acaefc25SArnd Bergmann struct simple_attr *attr; 960acaefc25SArnd Bergmann u64 val; 961acaefc25SArnd Bergmann size_t size; 962acaefc25SArnd Bergmann ssize_t ret; 963acaefc25SArnd Bergmann 964acaefc25SArnd Bergmann attr = file->private_data; 965acaefc25SArnd Bergmann if (!attr->set) 966acaefc25SArnd Bergmann return -EACCES; 967acaefc25SArnd Bergmann 9689261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 9699261303aSChristoph Hellwig if (ret) 9709261303aSChristoph Hellwig return ret; 9719261303aSChristoph Hellwig 972acaefc25SArnd Bergmann ret = -EFAULT; 973acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 974acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 975acaefc25SArnd Bergmann goto out; 976acaefc25SArnd Bergmann 977acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 978f7b88631SAkinobu Mita val = simple_strtoll(attr->set_buf, NULL, 0); 97905cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 98005cc0ceeSWu Fengguang if (ret == 0) 98105cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 982acaefc25SArnd Bergmann out: 9837cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 984acaefc25SArnd Bergmann return ret; 985acaefc25SArnd Bergmann } 98612f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 987acaefc25SArnd Bergmann 9882596110aSChristoph Hellwig /** 9892596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 9902596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 9912596110aSChristoph Hellwig * @fid: file handle to convert 9922596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 9932596110aSChristoph Hellwig * @fh_type: type of file handle 9942596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 9952596110aSChristoph Hellwig * 9962596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 9972596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 9982596110aSChristoph Hellwig * inode for the object specified in the file handle. 9992596110aSChristoph Hellwig */ 10002596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 10012596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 10022596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 10032596110aSChristoph Hellwig { 10042596110aSChristoph Hellwig struct inode *inode = NULL; 10052596110aSChristoph Hellwig 10062596110aSChristoph Hellwig if (fh_len < 2) 10072596110aSChristoph Hellwig return NULL; 10082596110aSChristoph Hellwig 10092596110aSChristoph Hellwig switch (fh_type) { 10102596110aSChristoph Hellwig case FILEID_INO32_GEN: 10112596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 10122596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 10132596110aSChristoph Hellwig break; 10142596110aSChristoph Hellwig } 10152596110aSChristoph Hellwig 10164ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 10172596110aSChristoph Hellwig } 10182596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 10192596110aSChristoph Hellwig 10202596110aSChristoph Hellwig /** 1021ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 10222596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 10232596110aSChristoph Hellwig * @fid: file handle to convert 10242596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 10252596110aSChristoph Hellwig * @fh_type: type of file handle 10262596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 10272596110aSChristoph Hellwig * 10282596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 10292596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 10302596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 10312596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 10322596110aSChristoph Hellwig */ 10332596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 10342596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 10352596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 10362596110aSChristoph Hellwig { 10372596110aSChristoph Hellwig struct inode *inode = NULL; 10382596110aSChristoph Hellwig 10392596110aSChristoph Hellwig if (fh_len <= 2) 10402596110aSChristoph Hellwig return NULL; 10412596110aSChristoph Hellwig 10422596110aSChristoph Hellwig switch (fh_type) { 10432596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 10442596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 10452596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 10462596110aSChristoph Hellwig break; 10472596110aSChristoph Hellwig } 10482596110aSChristoph Hellwig 10494ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 10502596110aSChristoph Hellwig } 10512596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 10522596110aSChristoph Hellwig 10531b061d92SChristoph Hellwig /** 1054ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 1055ac13a829SFabian Frederick * 10561b061d92SChristoph Hellwig * @file: file to synchronize 1057ac13a829SFabian Frederick * @start: start offset in bytes 1058ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 10591b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 10601b061d92SChristoph Hellwig * 10611b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 10621b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 10631b061d92SChristoph Hellwig * hanging off the address_space structure. 10641b061d92SChristoph Hellwig */ 1065ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 106602c24a82SJosef Bacik int datasync) 1067d5aacad5SAl Viro { 10687ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 1069d5aacad5SAl Viro int err; 1070d5aacad5SAl Viro int ret; 1071d5aacad5SAl Viro 1072383aa543SJeff Layton err = file_write_and_wait_range(file, start, end); 107302c24a82SJosef Bacik if (err) 107402c24a82SJosef Bacik return err; 107502c24a82SJosef Bacik 10765955102cSAl Viro inode_lock(inode); 1077d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 10780ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 107902c24a82SJosef Bacik goto out; 1080d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 108102c24a82SJosef Bacik goto out; 1082d5aacad5SAl Viro 1083c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 1084d5aacad5SAl Viro if (ret == 0) 1085d5aacad5SAl Viro ret = err; 1086ac13a829SFabian Frederick 108702c24a82SJosef Bacik out: 10885955102cSAl Viro inode_unlock(inode); 1089383aa543SJeff Layton /* check and advance again to catch errors after syncing out buffers */ 1090383aa543SJeff Layton err = file_check_and_advance_wb_err(file); 1091383aa543SJeff Layton if (ret == 0) 1092383aa543SJeff Layton ret = err; 1093383aa543SJeff Layton return ret; 1094d5aacad5SAl Viro } 1095ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 1096ac13a829SFabian Frederick 1097ac13a829SFabian Frederick /** 1098ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 1099ac13a829SFabian Frederick * with flush 1100ac13a829SFabian Frederick * @file: file to synchronize 1101ac13a829SFabian Frederick * @start: start offset in bytes 1102ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 1103ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 1104ac13a829SFabian Frederick * 1105ac13a829SFabian Frederick */ 1106ac13a829SFabian Frederick 1107ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 1108ac13a829SFabian Frederick int datasync) 1109ac13a829SFabian Frederick { 1110ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 1111ac13a829SFabian Frederick int err; 1112ac13a829SFabian Frederick 1113ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 1114ac13a829SFabian Frederick if (err) 1115ac13a829SFabian Frederick return err; 1116ac13a829SFabian Frederick return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); 1117ac13a829SFabian Frederick } 11181b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 11191b061d92SChristoph Hellwig 112030ca22c7SPatrick J. LoPresti /** 112130ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 112230ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 112330ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 112430ca22c7SPatrick J. LoPresti * 112530ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 112630ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 112730ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 112830ca22c7SPatrick J. LoPresti */ 112930ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 113030ca22c7SPatrick J. LoPresti { 113130ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 1132a33f13efSJoel Becker u64 last_fs_page = 113309cbfeafSKirill A. Shutemov last_fs_block >> (PAGE_SHIFT - blocksize_bits); 113430ca22c7SPatrick J. LoPresti 113530ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 113630ca22c7SPatrick J. LoPresti return 0; 113730ca22c7SPatrick J. LoPresti 113809cbfeafSKirill A. Shutemov if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT)) 113930ca22c7SPatrick J. LoPresti return -EINVAL; 114030ca22c7SPatrick J. LoPresti 1141a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1142a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 114330ca22c7SPatrick J. LoPresti return -EFBIG; 114430ca22c7SPatrick J. LoPresti } 114530ca22c7SPatrick J. LoPresti return 0; 114630ca22c7SPatrick J. LoPresti } 114730ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 114830ca22c7SPatrick J. LoPresti 11491b061d92SChristoph Hellwig /* 11501b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 11511b061d92SChristoph Hellwig */ 115202c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 11531b061d92SChristoph Hellwig { 11541b061d92SChristoph Hellwig return 0; 11551b061d92SChristoph Hellwig } 11561b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 115787dc800bSAl Viro 1158f44c7763SDan Williams int noop_set_page_dirty(struct page *page) 1159f44c7763SDan Williams { 1160f44c7763SDan Williams /* 1161f44c7763SDan Williams * Unlike __set_page_dirty_no_writeback that handles dirty page 1162f44c7763SDan Williams * tracking in the page object, dax does all dirty tracking in 1163f44c7763SDan Williams * the inode address_space in response to mkwrite faults. In the 1164f44c7763SDan Williams * dax case we only need to worry about potentially dirty CPU 1165f44c7763SDan Williams * caches, not dirty page cache pages to write back. 1166f44c7763SDan Williams * 1167f44c7763SDan Williams * This callback is defined to prevent fallback to 1168f44c7763SDan Williams * __set_page_dirty_buffers() in set_page_dirty(). 1169f44c7763SDan Williams */ 1170f44c7763SDan Williams return 0; 1171f44c7763SDan Williams } 1172f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_set_page_dirty); 1173f44c7763SDan Williams 1174f44c7763SDan Williams void noop_invalidatepage(struct page *page, unsigned int offset, 1175f44c7763SDan Williams unsigned int length) 1176f44c7763SDan Williams { 1177f44c7763SDan Williams /* 1178f44c7763SDan Williams * There is no page cache to invalidate in the dax case, however 1179f44c7763SDan Williams * we need this callback defined to prevent falling back to 1180f44c7763SDan Williams * block_invalidatepage() in do_invalidatepage(). 1181f44c7763SDan Williams */ 1182f44c7763SDan Williams } 1183f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_invalidatepage); 1184f44c7763SDan Williams 1185f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 1186f44c7763SDan Williams { 1187f44c7763SDan Williams /* 1188f44c7763SDan Williams * iomap based filesystems support direct I/O without need for 1189f44c7763SDan Williams * this callback. However, it still needs to be set in 1190f44c7763SDan Williams * inode->a_ops so that open/fcntl know that direct I/O is 1191f44c7763SDan Williams * generally supported. 1192f44c7763SDan Williams */ 1193f44c7763SDan Williams return -EINVAL; 1194f44c7763SDan Williams } 1195f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO); 1196f44c7763SDan Williams 1197fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */ 1198fceef393SAl Viro void kfree_link(void *p) 119987dc800bSAl Viro { 1200fceef393SAl Viro kfree(p); 120187dc800bSAl Viro } 1202fceef393SAl Viro EXPORT_SYMBOL(kfree_link); 12036987843fSAl Viro 12046987843fSAl Viro /* 12056987843fSAl Viro * nop .set_page_dirty method so that people can use .page_mkwrite on 12066987843fSAl Viro * anon inodes. 12076987843fSAl Viro */ 12086987843fSAl Viro static int anon_set_page_dirty(struct page *page) 12096987843fSAl Viro { 12106987843fSAl Viro return 0; 12116987843fSAl Viro }; 12126987843fSAl Viro 12136987843fSAl Viro /* 12146987843fSAl Viro * A single inode exists for all anon_inode files. Contrary to pipes, 12156987843fSAl Viro * anon_inode inodes have no associated per-instance data, so we need 12166987843fSAl Viro * only allocate one of them. 12176987843fSAl Viro */ 12186987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 12196987843fSAl Viro { 12206987843fSAl Viro static const struct address_space_operations anon_aops = { 12216987843fSAl Viro .set_page_dirty = anon_set_page_dirty, 12226987843fSAl Viro }; 12236987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 12246987843fSAl Viro 12256987843fSAl Viro if (!inode) 12266987843fSAl Viro return ERR_PTR(-ENOMEM); 12276987843fSAl Viro 12286987843fSAl Viro inode->i_ino = get_next_ino(); 12296987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 12306987843fSAl Viro 12316987843fSAl Viro /* 12326987843fSAl Viro * Mark the inode dirty from the very beginning, 12336987843fSAl Viro * that way it will never be moved to the dirty 12346987843fSAl Viro * list because mark_inode_dirty() will think 12356987843fSAl Viro * that it already _is_ on the dirty list. 12366987843fSAl Viro */ 12376987843fSAl Viro inode->i_state = I_DIRTY; 12386987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 12396987843fSAl Viro inode->i_uid = current_fsuid(); 12406987843fSAl Viro inode->i_gid = current_fsgid(); 12416987843fSAl Viro inode->i_flags |= S_PRIVATE; 1242078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 12436987843fSAl Viro return inode; 12446987843fSAl Viro } 12456987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 12461c994a09SJeff Layton 12471c994a09SJeff Layton /** 12481c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 12491c994a09SJeff Layton * @filp: file pointer 12501c994a09SJeff Layton * @arg: type of lease to obtain 12511c994a09SJeff Layton * @flp: new lease supplied for insertion 1252e6f5c789SJeff Layton * @priv: private data for lm_setup operation 12531c994a09SJeff Layton * 12541c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 12551c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 12561c994a09SJeff Layton */ 12571c994a09SJeff Layton int 1258e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1259e6f5c789SJeff Layton void **priv) 12601c994a09SJeff Layton { 12611c994a09SJeff Layton return -EINVAL; 12621c994a09SJeff Layton } 12631c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 126461ba64fcSAl Viro 12656ee9706aSEric Biggers /** 12666ee9706aSEric Biggers * simple_get_link - generic helper to get the target of "fast" symlinks 12676ee9706aSEric Biggers * @dentry: not used here 12686ee9706aSEric Biggers * @inode: the symlink inode 12696ee9706aSEric Biggers * @done: not used here 12706ee9706aSEric Biggers * 12716ee9706aSEric Biggers * Generic helper for filesystems to use for symlink inodes where a pointer to 12726ee9706aSEric Biggers * the symlink target is stored in ->i_link. NOTE: this isn't normally called, 12736ee9706aSEric Biggers * since as an optimization the path lookup code uses any non-NULL ->i_link 12746ee9706aSEric Biggers * directly, without calling ->get_link(). But ->get_link() still must be set, 12756ee9706aSEric Biggers * to mark the inode_operations as being for a symlink. 12766ee9706aSEric Biggers * 12776ee9706aSEric Biggers * Return: the symlink target 12786ee9706aSEric Biggers */ 12796b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 1280fceef393SAl Viro struct delayed_call *done) 128161ba64fcSAl Viro { 12826b255391SAl Viro return inode->i_link; 128361ba64fcSAl Viro } 12846b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 128561ba64fcSAl Viro 128661ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 12876b255391SAl Viro .get_link = simple_get_link, 128861ba64fcSAl Viro }; 128961ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1290fbabfd0fSEric W. Biederman 1291fbabfd0fSEric W. Biederman /* 1292fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1293fbabfd0fSEric W. Biederman */ 1294fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1295fbabfd0fSEric W. Biederman { 1296fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1297fbabfd0fSEric W. Biederman } 1298fbabfd0fSEric W. Biederman 1299a528d35eSDavid Howells static int empty_dir_getattr(const struct path *path, struct kstat *stat, 1300a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 1301fbabfd0fSEric W. Biederman { 1302a528d35eSDavid Howells struct inode *inode = d_inode(path->dentry); 1303fbabfd0fSEric W. Biederman generic_fillattr(inode, stat); 1304fbabfd0fSEric W. Biederman return 0; 1305fbabfd0fSEric W. Biederman } 1306fbabfd0fSEric W. Biederman 1307fbabfd0fSEric W. Biederman static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr) 1308fbabfd0fSEric W. Biederman { 1309fbabfd0fSEric W. Biederman return -EPERM; 1310fbabfd0fSEric W. Biederman } 1311fbabfd0fSEric W. Biederman 1312fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1313fbabfd0fSEric W. Biederman { 1314fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1315fbabfd0fSEric W. Biederman } 1316fbabfd0fSEric W. Biederman 1317fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1318fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1319fbabfd0fSEric W. Biederman .permission = generic_permission, 1320fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1321fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1322fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1323fbabfd0fSEric W. Biederman }; 1324fbabfd0fSEric W. Biederman 1325fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1326fbabfd0fSEric W. Biederman { 1327fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1328fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1329fbabfd0fSEric W. Biederman } 1330fbabfd0fSEric W. Biederman 1331fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1332fbabfd0fSEric W. Biederman { 1333fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1334fbabfd0fSEric W. Biederman return 0; 1335fbabfd0fSEric W. Biederman } 1336fbabfd0fSEric W. Biederman 1337fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1338fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1339fbabfd0fSEric W. Biederman .read = generic_read_dir, 1340c51da20cSAl Viro .iterate_shared = empty_dir_readdir, 1341fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1342fbabfd0fSEric W. Biederman }; 1343fbabfd0fSEric W. Biederman 1344fbabfd0fSEric W. Biederman 1345fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1346fbabfd0fSEric W. Biederman { 1347fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1348fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1349fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1350fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1351fbabfd0fSEric W. Biederman inode->i_rdev = 0; 13524b75de86SEric W. Biederman inode->i_size = 0; 1353fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1354fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1355fbabfd0fSEric W. Biederman 1356fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1357f5c24438SAndreas Gruenbacher inode->i_opflags &= ~IOP_XATTR; 1358fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1359fbabfd0fSEric W. Biederman } 1360fbabfd0fSEric W. Biederman 1361fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1362fbabfd0fSEric W. Biederman { 1363fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1364fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1365fbabfd0fSEric W. Biederman } 1366