11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/libfs.c 31da177e4SLinus Torvalds * Library for filesystems writers. 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 61da177e4SLinus Torvalds #include <linux/module.h> 71da177e4SLinus Torvalds #include <linux/pagemap.h> 85a0e3ad6STejun Heo #include <linux/slab.h> 91da177e4SLinus Torvalds #include <linux/mount.h> 101da177e4SLinus Torvalds #include <linux/vfs.h> 117bb46a67Snpiggin@suse.de #include <linux/quotaops.h> 127cf34c76SIngo Molnar #include <linux/mutex.h> 132596110aSChristoph Hellwig #include <linux/exportfs.h> 14d5aacad5SAl Viro #include <linux/writeback.h> 15d5aacad5SAl Viro #include <linux/buffer_head.h> 167cf34c76SIngo Molnar 171da177e4SLinus Torvalds #include <asm/uaccess.h> 181da177e4SLinus Torvalds 19a4464dbcSAl Viro #include "internal.h" 20a4464dbcSAl Viro 21da502956SNick Piggin static inline int simple_positive(struct dentry *dentry) 22da502956SNick Piggin { 23da502956SNick Piggin return dentry->d_inode && !d_unhashed(dentry); 24da502956SNick Piggin } 25da502956SNick Piggin 261da177e4SLinus Torvalds int simple_getattr(struct vfsmount *mnt, struct dentry *dentry, 271da177e4SLinus Torvalds struct kstat *stat) 281da177e4SLinus Torvalds { 291da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 301da177e4SLinus Torvalds generic_fillattr(inode, stat); 311da177e4SLinus Torvalds stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9); 321da177e4SLinus Torvalds return 0; 331da177e4SLinus Torvalds } 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; 381da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 391da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 401da177e4SLinus Torvalds return 0; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* 441da177e4SLinus Torvalds * Retaining negative dentries for an in-memory filesystem just wastes 451da177e4SLinus Torvalds * memory and lookup time: arrange for them to be deleted immediately. 461da177e4SLinus Torvalds */ 47fe15ce44SNick Piggin static int simple_delete_dentry(const struct dentry *dentry) 481da177e4SLinus Torvalds { 491da177e4SLinus Torvalds return 1; 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * Lookup the data. This is trivial - if the dentry didn't already 541da177e4SLinus Torvalds * exist, we know it is negative. Set d_op to delete negative dentries. 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) 571da177e4SLinus Torvalds { 583ba13d17SAl Viro static const struct dentry_operations simple_dentry_operations = { 591da177e4SLinus Torvalds .d_delete = simple_delete_dentry, 601da177e4SLinus Torvalds }; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds if (dentry->d_name.len > NAME_MAX) 631da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 64fb045adbSNick Piggin d_set_d_op(dentry, &simple_dentry_operations); 651da177e4SLinus Torvalds d_add(dentry, NULL); 661da177e4SLinus Torvalds return NULL; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file) 701da177e4SLinus Torvalds { 711da177e4SLinus Torvalds static struct qstr cursor_name = {.len = 1, .name = "."}; 721da177e4SLinus Torvalds 730f7fc9e4SJosef "Jeff" Sipek file->private_data = d_alloc(file->f_path.dentry, &cursor_name); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds return file->private_data ? 0 : -ENOMEM; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file) 791da177e4SLinus Torvalds { 801da177e4SLinus Torvalds dput(file->private_data); 811da177e4SLinus Torvalds return 0; 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin) 851da177e4SLinus Torvalds { 862fd6b7f5SNick Piggin struct dentry *dentry = file->f_path.dentry; 872fd6b7f5SNick Piggin mutex_lock(&dentry->d_inode->i_mutex); 881da177e4SLinus Torvalds switch (origin) { 891da177e4SLinus Torvalds case 1: 901da177e4SLinus Torvalds offset += file->f_pos; 911da177e4SLinus Torvalds case 0: 921da177e4SLinus Torvalds if (offset >= 0) 931da177e4SLinus Torvalds break; 941da177e4SLinus Torvalds default: 952fd6b7f5SNick Piggin mutex_unlock(&dentry->d_inode->i_mutex); 961da177e4SLinus Torvalds return -EINVAL; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds if (offset != file->f_pos) { 991da177e4SLinus Torvalds file->f_pos = offset; 1001da177e4SLinus Torvalds if (file->f_pos >= 2) { 1011da177e4SLinus Torvalds struct list_head *p; 1021da177e4SLinus Torvalds struct dentry *cursor = file->private_data; 1031da177e4SLinus Torvalds loff_t n = file->f_pos - 2; 1041da177e4SLinus Torvalds 1052fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1062fd6b7f5SNick Piggin /* d_lock not required for cursor */ 1075160ee6fSEric Dumazet list_del(&cursor->d_u.d_child); 1082fd6b7f5SNick Piggin p = dentry->d_subdirs.next; 1092fd6b7f5SNick Piggin while (n && p != &dentry->d_subdirs) { 1101da177e4SLinus Torvalds struct dentry *next; 1115160ee6fSEric Dumazet next = list_entry(p, struct dentry, d_u.d_child); 1122fd6b7f5SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 113da502956SNick Piggin if (simple_positive(next)) 1141da177e4SLinus Torvalds n--; 115da502956SNick Piggin spin_unlock(&next->d_lock); 1161da177e4SLinus Torvalds p = p->next; 1171da177e4SLinus Torvalds } 1185160ee6fSEric Dumazet list_add_tail(&cursor->d_u.d_child, p); 1192fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds } 1222fd6b7f5SNick Piggin mutex_unlock(&dentry->d_inode->i_mutex); 1231da177e4SLinus Torvalds return offset; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */ 1271da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* 1331da177e4SLinus Torvalds * Directory is locked and all positive dentries in it are safe, since 1341da177e4SLinus Torvalds * for ramfs-type trees they can't go away without unlink() or rmdir(), 1351da177e4SLinus Torvalds * both impossible due to the lock on directory. 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir) 1391da177e4SLinus Torvalds { 1400f7fc9e4SJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 1411da177e4SLinus Torvalds struct dentry *cursor = filp->private_data; 1425160ee6fSEric Dumazet struct list_head *p, *q = &cursor->d_u.d_child; 1431da177e4SLinus Torvalds ino_t ino; 1441da177e4SLinus Torvalds int i = filp->f_pos; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds switch (i) { 1471da177e4SLinus Torvalds case 0: 1481da177e4SLinus Torvalds ino = dentry->d_inode->i_ino; 1491da177e4SLinus Torvalds if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 1501da177e4SLinus Torvalds break; 1511da177e4SLinus Torvalds filp->f_pos++; 1521da177e4SLinus Torvalds i++; 1531da177e4SLinus Torvalds /* fallthrough */ 1541da177e4SLinus Torvalds case 1: 1551da177e4SLinus Torvalds ino = parent_ino(dentry); 1561da177e4SLinus Torvalds if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 1571da177e4SLinus Torvalds break; 1581da177e4SLinus Torvalds filp->f_pos++; 1591da177e4SLinus Torvalds i++; 1601da177e4SLinus Torvalds /* fallthrough */ 1611da177e4SLinus Torvalds default: 1622fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1631bfba4e8SAkinobu Mita if (filp->f_pos == 2) 1641bfba4e8SAkinobu Mita list_move(q, &dentry->d_subdirs); 1651bfba4e8SAkinobu Mita 1661da177e4SLinus Torvalds for (p=q->next; p != &dentry->d_subdirs; p=p->next) { 1671da177e4SLinus Torvalds struct dentry *next; 1685160ee6fSEric Dumazet next = list_entry(p, struct dentry, d_u.d_child); 169da502956SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 170da502956SNick Piggin if (!simple_positive(next)) { 171da502956SNick Piggin spin_unlock(&next->d_lock); 1721da177e4SLinus Torvalds continue; 173da502956SNick Piggin } 1741da177e4SLinus Torvalds 175da502956SNick Piggin spin_unlock(&next->d_lock); 1762fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1770f8952c2SRonni Nielsen if (filldir(dirent, next->d_name.name, 1780f8952c2SRonni Nielsen next->d_name.len, filp->f_pos, 1790f8952c2SRonni Nielsen next->d_inode->i_ino, 1800f8952c2SRonni Nielsen dt_type(next->d_inode)) < 0) 1811da177e4SLinus Torvalds return 0; 1822fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1832fd6b7f5SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 1841da177e4SLinus Torvalds /* next is still alive */ 1851bfba4e8SAkinobu Mita list_move(q, p); 1862fd6b7f5SNick Piggin spin_unlock(&next->d_lock); 1871da177e4SLinus Torvalds p = q; 1881da177e4SLinus Torvalds filp->f_pos++; 1891da177e4SLinus Torvalds } 1902fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds return 0; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds return -EISDIR; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2004b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = { 2011da177e4SLinus Torvalds .open = dcache_dir_open, 2021da177e4SLinus Torvalds .release = dcache_dir_close, 2031da177e4SLinus Torvalds .llseek = dcache_dir_lseek, 2041da177e4SLinus Torvalds .read = generic_read_dir, 2051da177e4SLinus Torvalds .readdir = dcache_readdir, 2061b061d92SChristoph Hellwig .fsync = noop_fsync, 2071da177e4SLinus Torvalds }; 2081da177e4SLinus Torvalds 20992e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = { 2101da177e4SLinus Torvalds .lookup = simple_lookup, 2111da177e4SLinus Torvalds }; 2121da177e4SLinus Torvalds 213759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 214759b9775SHugh Dickins .statfs = simple_statfs, 215759b9775SHugh Dickins }; 216759b9775SHugh Dickins 2171da177e4SLinus Torvalds /* 2181da177e4SLinus Torvalds * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 2191da177e4SLinus Torvalds * will never be mountable) 2201da177e4SLinus Torvalds */ 22151139adaSAl Viro struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name, 222c74a1cbbSAl Viro const struct super_operations *ops, 223c74a1cbbSAl Viro const struct dentry_operations *dops, unsigned long magic) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL); 2261da177e4SLinus Torvalds struct dentry *dentry; 2271da177e4SLinus Torvalds struct inode *root; 2281da177e4SLinus Torvalds struct qstr d_name = {.name = name, .len = strlen(name)}; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds if (IS_ERR(s)) 23151139adaSAl Viro return ERR_CAST(s); 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds s->s_flags = MS_NOUSER; 23489a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 2353971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 2363971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 2371da177e4SLinus Torvalds s->s_magic = magic; 238759b9775SHugh Dickins s->s_op = ops ? ops : &simple_super_operations; 2391da177e4SLinus Torvalds s->s_time_gran = 1; 2401da177e4SLinus Torvalds root = new_inode(s); 2411da177e4SLinus Torvalds if (!root) 2421da177e4SLinus Torvalds goto Enomem; 2431a1c9bb4SJeff Layton /* 2441a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 2451a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 2461a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 2471a1c9bb4SJeff Layton */ 2481a1c9bb4SJeff Layton root->i_ino = 1; 2491da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 2501da177e4SLinus Torvalds root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME; 251a4464dbcSAl Viro dentry = __d_alloc(s, &d_name); 2521da177e4SLinus Torvalds if (!dentry) { 2531da177e4SLinus Torvalds iput(root); 2541da177e4SLinus Torvalds goto Enomem; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds d_instantiate(dentry, root); 2571da177e4SLinus Torvalds s->s_root = dentry; 258c74a1cbbSAl Viro s->s_d_op = dops; 2591da177e4SLinus Torvalds s->s_flags |= MS_ACTIVE; 26051139adaSAl Viro return dget(s->s_root); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds Enomem: 2636f5bbff9SAl Viro deactivate_locked_super(s); 26451139adaSAl Viro return ERR_PTR(-ENOMEM); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 272d8c76e6fSDave Hansen inc_nlink(inode); 2737de9c6eeSAl Viro ihold(inode); 2741da177e4SLinus Torvalds dget(dentry); 2751da177e4SLinus Torvalds d_instantiate(dentry, inode); 2761da177e4SLinus Torvalds return 0; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds int simple_empty(struct dentry *dentry) 2801da177e4SLinus Torvalds { 2811da177e4SLinus Torvalds struct dentry *child; 2821da177e4SLinus Torvalds int ret = 0; 2831da177e4SLinus Torvalds 2842fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 285da502956SNick Piggin list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) { 286da502956SNick Piggin spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); 287da502956SNick Piggin if (simple_positive(child)) { 288da502956SNick Piggin spin_unlock(&child->d_lock); 2891da177e4SLinus Torvalds goto out; 290da502956SNick Piggin } 291da502956SNick Piggin spin_unlock(&child->d_lock); 292da502956SNick Piggin } 2931da177e4SLinus Torvalds ret = 1; 2941da177e4SLinus Torvalds out: 2952fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 2961da177e4SLinus Torvalds return ret; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 3049a53c3a7SDave Hansen drop_nlink(inode); 3051da177e4SLinus Torvalds dput(dentry); 3061da177e4SLinus Torvalds return 0; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds if (!simple_empty(dentry)) 3121da177e4SLinus Torvalds return -ENOTEMPTY; 3131da177e4SLinus Torvalds 3149a53c3a7SDave Hansen drop_nlink(dentry->d_inode); 3151da177e4SLinus Torvalds simple_unlink(dir, dentry); 3169a53c3a7SDave Hansen drop_nlink(dir); 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry, 3211da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 3241da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 3271da177e4SLinus Torvalds return -ENOTEMPTY; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (new_dentry->d_inode) { 3301da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 331841590ceSAl Viro if (they_are_dirs) { 332841590ceSAl Viro drop_nlink(new_dentry->d_inode); 3339a53c3a7SDave Hansen drop_nlink(old_dir); 334841590ceSAl Viro } 3351da177e4SLinus Torvalds } else if (they_are_dirs) { 3369a53c3a7SDave Hansen drop_nlink(old_dir); 337d8c76e6fSDave Hansen inc_nlink(new_dir); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 3411da177e4SLinus Torvalds new_dir->i_mtime = inode->i_ctime = CURRENT_TIME; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds return 0; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3467bb46a67Snpiggin@suse.de /** 347eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 3487bb46a67Snpiggin@suse.de * @dentry: dentry 3497bb46a67Snpiggin@suse.de * @iattr: iattr structure 3507bb46a67Snpiggin@suse.de * 3517bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 3527bb46a67Snpiggin@suse.de * 353eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 354eef2380cSChristoph Hellwig * implementation of size changes. 355eef2380cSChristoph Hellwig * 356eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 357eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 358eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 3597bb46a67Snpiggin@suse.de */ 3607bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr) 3617bb46a67Snpiggin@suse.de { 3627bb46a67Snpiggin@suse.de struct inode *inode = dentry->d_inode; 3637bb46a67Snpiggin@suse.de int error; 3647bb46a67Snpiggin@suse.de 365eef2380cSChristoph Hellwig WARN_ON_ONCE(inode->i_op->truncate); 366eef2380cSChristoph Hellwig 3677bb46a67Snpiggin@suse.de error = inode_change_ok(inode, iattr); 3687bb46a67Snpiggin@suse.de if (error) 3697bb46a67Snpiggin@suse.de return error; 3707bb46a67Snpiggin@suse.de 3712c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 3722c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 3736a1a90adSChristoph Hellwig setattr_copy(inode, iattr); 374eef2380cSChristoph Hellwig mark_inode_dirty(inode); 375eef2380cSChristoph Hellwig return 0; 3767bb46a67Snpiggin@suse.de } 3777bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 3787bb46a67Snpiggin@suse.de 3791da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page) 3801da177e4SLinus Torvalds { 381c0d92cbcSPekka J Enberg clear_highpage(page); 3821da177e4SLinus Torvalds flush_dcache_page(page); 3831da177e4SLinus Torvalds SetPageUptodate(page); 3841da177e4SLinus Torvalds unlock_page(page); 3851da177e4SLinus Torvalds return 0; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 388afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 389afddba49SNick Piggin loff_t pos, unsigned len, unsigned flags, 390afddba49SNick Piggin struct page **pagep, void **fsdata) 391afddba49SNick Piggin { 392afddba49SNick Piggin struct page *page; 393afddba49SNick Piggin pgoff_t index; 394afddba49SNick Piggin 395afddba49SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 396afddba49SNick Piggin 39754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 398afddba49SNick Piggin if (!page) 399afddba49SNick Piggin return -ENOMEM; 400afddba49SNick Piggin 401afddba49SNick Piggin *pagep = page; 402afddba49SNick Piggin 403193cf4b9SBoaz Harrosh if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) { 404193cf4b9SBoaz Harrosh unsigned from = pos & (PAGE_CACHE_SIZE - 1); 405193cf4b9SBoaz Harrosh 406193cf4b9SBoaz Harrosh zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE); 407193cf4b9SBoaz Harrosh } 408193cf4b9SBoaz Harrosh return 0; 409afddba49SNick Piggin } 410afddba49SNick Piggin 411ad2a722fSBoaz Harrosh /** 412ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 413ad2a722fSBoaz Harrosh * @available: See .write_end of address_space_operations 414ad2a722fSBoaz Harrosh * @file: " 415ad2a722fSBoaz Harrosh * @mapping: " 416ad2a722fSBoaz Harrosh * @pos: " 417ad2a722fSBoaz Harrosh * @len: " 418ad2a722fSBoaz Harrosh * @copied: " 419ad2a722fSBoaz Harrosh * @page: " 420ad2a722fSBoaz Harrosh * @fsdata: " 421ad2a722fSBoaz Harrosh * 422ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 423ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 424ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 425ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 426ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 427ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 428ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 429ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 430ad2a722fSBoaz Harrosh * case that i_size has changed. 431ad2a722fSBoaz Harrosh */ 432ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping, 433ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 434ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 437ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 438ad2a722fSBoaz Harrosh 439ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 440ad2a722fSBoaz Harrosh if (copied < len) { 441ad2a722fSBoaz Harrosh unsigned from = pos & (PAGE_CACHE_SIZE - 1); 442ad2a722fSBoaz Harrosh 443ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 444ad2a722fSBoaz Harrosh } 4451da177e4SLinus Torvalds 446955eff5aSNick Piggin if (!PageUptodate(page)) 447955eff5aSNick Piggin SetPageUptodate(page); 4481da177e4SLinus Torvalds /* 4491da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 4501b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 4511da177e4SLinus Torvalds */ 452ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 453ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 454ad2a722fSBoaz Harrosh 4551da177e4SLinus Torvalds set_page_dirty(page); 456afddba49SNick Piggin unlock_page(page); 457afddba49SNick Piggin page_cache_release(page); 458afddba49SNick Piggin 459afddba49SNick Piggin return copied; 460afddba49SNick Piggin } 461afddba49SNick Piggin 4621a1c9bb4SJeff Layton /* 4631a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 4641a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 4651a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 4661a1c9bb4SJeff Layton */ 4677d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 4687d683a09SRoberto Sassu struct tree_descr *files) 4691da177e4SLinus Torvalds { 4701da177e4SLinus Torvalds struct inode *inode; 4711da177e4SLinus Torvalds struct dentry *root; 4721da177e4SLinus Torvalds struct dentry *dentry; 4731da177e4SLinus Torvalds int i; 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds s->s_blocksize = PAGE_CACHE_SIZE; 4761da177e4SLinus Torvalds s->s_blocksize_bits = PAGE_CACHE_SHIFT; 4771da177e4SLinus Torvalds s->s_magic = magic; 478759b9775SHugh Dickins s->s_op = &simple_super_operations; 4791da177e4SLinus Torvalds s->s_time_gran = 1; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds inode = new_inode(s); 4821da177e4SLinus Torvalds if (!inode) 4831da177e4SLinus Torvalds return -ENOMEM; 4841a1c9bb4SJeff Layton /* 4851a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 4861a1c9bb4SJeff Layton * entry at index 1 4871a1c9bb4SJeff Layton */ 4881a1c9bb4SJeff Layton inode->i_ino = 1; 4891da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 4901da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 4911da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 4921da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 493bfe86848SMiklos Szeredi set_nlink(inode, 2); 4941da177e4SLinus Torvalds root = d_alloc_root(inode); 4951da177e4SLinus Torvalds if (!root) { 4961da177e4SLinus Torvalds iput(inode); 4971da177e4SLinus Torvalds return -ENOMEM; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 5001da177e4SLinus Torvalds if (!files->name) 5011da177e4SLinus Torvalds continue; 5021a1c9bb4SJeff Layton 5031a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 5041a1c9bb4SJeff Layton if (unlikely(i == 1)) 5051a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 5061a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 5071a1c9bb4SJeff Layton s->s_type->name); 5081a1c9bb4SJeff Layton 5091da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 5101da177e4SLinus Torvalds if (!dentry) 5111da177e4SLinus Torvalds goto out; 5121da177e4SLinus Torvalds inode = new_inode(s); 51332096ea1SKonstantin Khlebnikov if (!inode) { 51432096ea1SKonstantin Khlebnikov dput(dentry); 5151da177e4SLinus Torvalds goto out; 51632096ea1SKonstantin Khlebnikov } 5171da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 5181da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 5191da177e4SLinus Torvalds inode->i_fop = files->ops; 5201da177e4SLinus Torvalds inode->i_ino = i; 5211da177e4SLinus Torvalds d_add(dentry, inode); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds s->s_root = root; 5241da177e4SLinus Torvalds return 0; 5251da177e4SLinus Torvalds out: 5261da177e4SLinus Torvalds d_genocide(root); 5271da177e4SLinus Torvalds dput(root); 5281da177e4SLinus Torvalds return -ENOMEM; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 5321da177e4SLinus Torvalds 5331f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 5361da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5371da177e4SLinus Torvalds if (unlikely(!*mount)) { 5381da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5391f5ce9e9STrond Myklebust mnt = vfs_kern_mount(type, 0, type->name, NULL); 5401da177e4SLinus Torvalds if (IS_ERR(mnt)) 5411da177e4SLinus Torvalds return PTR_ERR(mnt); 5421da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5431da177e4SLinus Torvalds if (!*mount) 5441da177e4SLinus Torvalds *mount = mnt; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds mntget(*mount); 5471da177e4SLinus Torvalds ++*count; 5481da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5491da177e4SLinus Torvalds mntput(mnt); 5501da177e4SLinus Torvalds return 0; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds struct vfsmount *mnt; 5561da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5571da177e4SLinus Torvalds mnt = *mount; 5581da177e4SLinus Torvalds if (!--*count) 5591da177e4SLinus Torvalds *mount = NULL; 5601da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5611da177e4SLinus Torvalds mntput(mnt); 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds 5646d1029b5SAkinobu Mita /** 5656d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 5666d1029b5SAkinobu Mita * @to: the user space buffer to read to 5676d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 5686d1029b5SAkinobu Mita * @ppos: the current position in the buffer 5696d1029b5SAkinobu Mita * @from: the buffer to read from 5706d1029b5SAkinobu Mita * @available: the size of the buffer 5716d1029b5SAkinobu Mita * 5726d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 5736d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 5746d1029b5SAkinobu Mita * 5756d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 5766d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 5776d1029b5SAkinobu Mita **/ 5781da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 5791da177e4SLinus Torvalds const void *from, size_t available) 5801da177e4SLinus Torvalds { 5811da177e4SLinus Torvalds loff_t pos = *ppos; 58214be2746SSteven Rostedt size_t ret; 58314be2746SSteven Rostedt 5841da177e4SLinus Torvalds if (pos < 0) 5851da177e4SLinus Torvalds return -EINVAL; 58614be2746SSteven Rostedt if (pos >= available || !count) 5871da177e4SLinus Torvalds return 0; 5881da177e4SLinus Torvalds if (count > available - pos) 5891da177e4SLinus Torvalds count = available - pos; 59014be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 59114be2746SSteven Rostedt if (ret == count) 5921da177e4SLinus Torvalds return -EFAULT; 59314be2746SSteven Rostedt count -= ret; 5941da177e4SLinus Torvalds *ppos = pos + count; 5951da177e4SLinus Torvalds return count; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5986d1029b5SAkinobu Mita /** 5996a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 6006a727b43SJiri Slaby * @to: the buffer to write to 6016a727b43SJiri Slaby * @available: the size of the buffer 6026a727b43SJiri Slaby * @ppos: the current position in the buffer 6036a727b43SJiri Slaby * @from: the user space buffer to read from 6046a727b43SJiri Slaby * @count: the maximum number of bytes to read 6056a727b43SJiri Slaby * 6066a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 6076a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 6086a727b43SJiri Slaby * 6096a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 6106a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 6116a727b43SJiri Slaby **/ 6126a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 6136a727b43SJiri Slaby const void __user *from, size_t count) 6146a727b43SJiri Slaby { 6156a727b43SJiri Slaby loff_t pos = *ppos; 6166a727b43SJiri Slaby size_t res; 6176a727b43SJiri Slaby 6186a727b43SJiri Slaby if (pos < 0) 6196a727b43SJiri Slaby return -EINVAL; 6206a727b43SJiri Slaby if (pos >= available || !count) 6216a727b43SJiri Slaby return 0; 6226a727b43SJiri Slaby if (count > available - pos) 6236a727b43SJiri Slaby count = available - pos; 6246a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 6256a727b43SJiri Slaby if (res == count) 6266a727b43SJiri Slaby return -EFAULT; 6276a727b43SJiri Slaby count -= res; 6286a727b43SJiri Slaby *ppos = pos + count; 6296a727b43SJiri Slaby return count; 6306a727b43SJiri Slaby } 6316a727b43SJiri Slaby 6326a727b43SJiri Slaby /** 6336d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 6346d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 6356d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 6366d1029b5SAkinobu Mita * @ppos: the current position in the buffer 6376d1029b5SAkinobu Mita * @from: the buffer to read from 6386d1029b5SAkinobu Mita * @available: the size of the buffer 6396d1029b5SAkinobu Mita * 6406d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 6416d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 6426d1029b5SAkinobu Mita * 6436d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 6446d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 6456d1029b5SAkinobu Mita **/ 64693b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 64793b07113SAkinobu Mita const void *from, size_t available) 64893b07113SAkinobu Mita { 64993b07113SAkinobu Mita loff_t pos = *ppos; 65093b07113SAkinobu Mita 65193b07113SAkinobu Mita if (pos < 0) 65293b07113SAkinobu Mita return -EINVAL; 65393b07113SAkinobu Mita if (pos >= available) 65493b07113SAkinobu Mita return 0; 65593b07113SAkinobu Mita if (count > available - pos) 65693b07113SAkinobu Mita count = available - pos; 65793b07113SAkinobu Mita memcpy(to, from + pos, count); 65893b07113SAkinobu Mita *ppos = pos + count; 65993b07113SAkinobu Mita 66093b07113SAkinobu Mita return count; 66193b07113SAkinobu Mita } 66293b07113SAkinobu Mita 6631da177e4SLinus Torvalds /* 6641da177e4SLinus Torvalds * Transaction based IO. 6651da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 6661da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 6671da177e4SLinus Torvalds * file-local buffer. 6681da177e4SLinus Torvalds */ 66976791ab2SIngo Molnar 67076791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 67176791ab2SIngo Molnar { 67276791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 67376791ab2SIngo Molnar 67476791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 67576791ab2SIngo Molnar 67676791ab2SIngo Molnar /* 67776791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 67876791ab2SIngo Molnar * ar->data is ready for reading. 67976791ab2SIngo Molnar */ 68076791ab2SIngo Molnar smp_mb(); 68176791ab2SIngo Molnar ar->size = n; 68276791ab2SIngo Molnar } 68376791ab2SIngo Molnar 6841da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 6851da177e4SLinus Torvalds { 6861da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 6871da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 6901da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 6931da177e4SLinus Torvalds if (!ar) 6941da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /* only one write allowed per open */ 6991da177e4SLinus Torvalds if (file->private_data) { 7001da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7011da177e4SLinus Torvalds free_page((unsigned long)ar); 7021da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds file->private_data = ar; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 7101da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds return ar->data; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds if (!ar) 7201da177e4SLinus Torvalds return 0; 7211da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 7271da177e4SLinus Torvalds return 0; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730acaefc25SArnd Bergmann /* Simple attribute files */ 731acaefc25SArnd Bergmann 732acaefc25SArnd Bergmann struct simple_attr { 7338b88b099SChristoph Hellwig int (*get)(void *, u64 *); 7348b88b099SChristoph Hellwig int (*set)(void *, u64); 735acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 736acaefc25SArnd Bergmann char set_buf[24]; 737acaefc25SArnd Bergmann void *data; 738acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 7397cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 740acaefc25SArnd Bergmann }; 741acaefc25SArnd Bergmann 742acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 743acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 744acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 7458b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 746acaefc25SArnd Bergmann const char *fmt) 747acaefc25SArnd Bergmann { 748acaefc25SArnd Bergmann struct simple_attr *attr; 749acaefc25SArnd Bergmann 750acaefc25SArnd Bergmann attr = kmalloc(sizeof(*attr), GFP_KERNEL); 751acaefc25SArnd Bergmann if (!attr) 752acaefc25SArnd Bergmann return -ENOMEM; 753acaefc25SArnd Bergmann 754acaefc25SArnd Bergmann attr->get = get; 755acaefc25SArnd Bergmann attr->set = set; 7568e18e294STheodore Ts'o attr->data = inode->i_private; 757acaefc25SArnd Bergmann attr->fmt = fmt; 7587cf34c76SIngo Molnar mutex_init(&attr->mutex); 759acaefc25SArnd Bergmann 760acaefc25SArnd Bergmann file->private_data = attr; 761acaefc25SArnd Bergmann 762acaefc25SArnd Bergmann return nonseekable_open(inode, file); 763acaefc25SArnd Bergmann } 764acaefc25SArnd Bergmann 76574bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 766acaefc25SArnd Bergmann { 767acaefc25SArnd Bergmann kfree(file->private_data); 768acaefc25SArnd Bergmann return 0; 769acaefc25SArnd Bergmann } 770acaefc25SArnd Bergmann 771acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 772acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 773acaefc25SArnd Bergmann size_t len, loff_t *ppos) 774acaefc25SArnd Bergmann { 775acaefc25SArnd Bergmann struct simple_attr *attr; 776acaefc25SArnd Bergmann size_t size; 777acaefc25SArnd Bergmann ssize_t ret; 778acaefc25SArnd Bergmann 779acaefc25SArnd Bergmann attr = file->private_data; 780acaefc25SArnd Bergmann 781acaefc25SArnd Bergmann if (!attr->get) 782acaefc25SArnd Bergmann return -EACCES; 783acaefc25SArnd Bergmann 7849261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 7859261303aSChristoph Hellwig if (ret) 7869261303aSChristoph Hellwig return ret; 7879261303aSChristoph Hellwig 7888b88b099SChristoph Hellwig if (*ppos) { /* continued read */ 789acaefc25SArnd Bergmann size = strlen(attr->get_buf); 7908b88b099SChristoph Hellwig } else { /* first read */ 7918b88b099SChristoph Hellwig u64 val; 7928b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 7938b88b099SChristoph Hellwig if (ret) 7948b88b099SChristoph Hellwig goto out; 7958b88b099SChristoph Hellwig 796acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 7978b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 7988b88b099SChristoph Hellwig } 799acaefc25SArnd Bergmann 800acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 8018b88b099SChristoph Hellwig out: 8027cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 803acaefc25SArnd Bergmann return ret; 804acaefc25SArnd Bergmann } 805acaefc25SArnd Bergmann 806acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 807acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf, 808acaefc25SArnd Bergmann size_t len, loff_t *ppos) 809acaefc25SArnd Bergmann { 810acaefc25SArnd Bergmann struct simple_attr *attr; 811acaefc25SArnd Bergmann u64 val; 812acaefc25SArnd Bergmann size_t size; 813acaefc25SArnd Bergmann ssize_t ret; 814acaefc25SArnd Bergmann 815acaefc25SArnd Bergmann attr = file->private_data; 816acaefc25SArnd Bergmann if (!attr->set) 817acaefc25SArnd Bergmann return -EACCES; 818acaefc25SArnd Bergmann 8199261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 8209261303aSChristoph Hellwig if (ret) 8219261303aSChristoph Hellwig return ret; 8229261303aSChristoph Hellwig 823acaefc25SArnd Bergmann ret = -EFAULT; 824acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 825acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 826acaefc25SArnd Bergmann goto out; 827acaefc25SArnd Bergmann 828acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 829f7b88631SAkinobu Mita val = simple_strtoll(attr->set_buf, NULL, 0); 83005cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 83105cc0ceeSWu Fengguang if (ret == 0) 83205cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 833acaefc25SArnd Bergmann out: 8347cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 835acaefc25SArnd Bergmann return ret; 836acaefc25SArnd Bergmann } 837acaefc25SArnd Bergmann 8382596110aSChristoph Hellwig /** 8392596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 8402596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 8412596110aSChristoph Hellwig * @fid: file handle to convert 8422596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 8432596110aSChristoph Hellwig * @fh_type: type of file handle 8442596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 8452596110aSChristoph Hellwig * 8462596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 8472596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 8482596110aSChristoph Hellwig * inode for the object specified in the file handle. 8492596110aSChristoph Hellwig */ 8502596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 8512596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 8522596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 8532596110aSChristoph Hellwig { 8542596110aSChristoph Hellwig struct inode *inode = NULL; 8552596110aSChristoph Hellwig 8562596110aSChristoph Hellwig if (fh_len < 2) 8572596110aSChristoph Hellwig return NULL; 8582596110aSChristoph Hellwig 8592596110aSChristoph Hellwig switch (fh_type) { 8602596110aSChristoph Hellwig case FILEID_INO32_GEN: 8612596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 8622596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 8632596110aSChristoph Hellwig break; 8642596110aSChristoph Hellwig } 8652596110aSChristoph Hellwig 8664ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 8672596110aSChristoph Hellwig } 8682596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 8692596110aSChristoph Hellwig 8702596110aSChristoph Hellwig /** 8712596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_parent export operation 8722596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 8732596110aSChristoph Hellwig * @fid: file handle to convert 8742596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 8752596110aSChristoph Hellwig * @fh_type: type of file handle 8762596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 8772596110aSChristoph Hellwig * 8782596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 8792596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 8802596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 8812596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 8822596110aSChristoph Hellwig */ 8832596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 8842596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 8852596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 8862596110aSChristoph Hellwig { 8872596110aSChristoph Hellwig struct inode *inode = NULL; 8882596110aSChristoph Hellwig 8892596110aSChristoph Hellwig if (fh_len <= 2) 8902596110aSChristoph Hellwig return NULL; 8912596110aSChristoph Hellwig 8922596110aSChristoph Hellwig switch (fh_type) { 8932596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 8942596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 8952596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 8962596110aSChristoph Hellwig break; 8972596110aSChristoph Hellwig } 8982596110aSChristoph Hellwig 8994ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 9002596110aSChristoph Hellwig } 9012596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 9022596110aSChristoph Hellwig 9031b061d92SChristoph Hellwig /** 9041b061d92SChristoph Hellwig * generic_file_fsync - generic fsync implementation for simple filesystems 9051b061d92SChristoph Hellwig * @file: file to synchronize 9061b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 9071b061d92SChristoph Hellwig * 9081b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 9091b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 9101b061d92SChristoph Hellwig * hanging off the address_space structure. 9111b061d92SChristoph Hellwig */ 91202c24a82SJosef Bacik int generic_file_fsync(struct file *file, loff_t start, loff_t end, 91302c24a82SJosef Bacik int datasync) 914d5aacad5SAl Viro { 9157ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 916d5aacad5SAl Viro int err; 917d5aacad5SAl Viro int ret; 918d5aacad5SAl Viro 91902c24a82SJosef Bacik err = filemap_write_and_wait_range(inode->i_mapping, start, end); 92002c24a82SJosef Bacik if (err) 92102c24a82SJosef Bacik return err; 92202c24a82SJosef Bacik 92302c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 924d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 925d5aacad5SAl Viro if (!(inode->i_state & I_DIRTY)) 92602c24a82SJosef Bacik goto out; 927d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 92802c24a82SJosef Bacik goto out; 929d5aacad5SAl Viro 930c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 931d5aacad5SAl Viro if (ret == 0) 932d5aacad5SAl Viro ret = err; 93302c24a82SJosef Bacik out: 93402c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 935d5aacad5SAl Viro return ret; 936d5aacad5SAl Viro } 9371b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 9381b061d92SChristoph Hellwig 93930ca22c7SPatrick J. LoPresti /** 94030ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 94130ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 94230ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 94330ca22c7SPatrick J. LoPresti * 94430ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 94530ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 94630ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 94730ca22c7SPatrick J. LoPresti */ 94830ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 94930ca22c7SPatrick J. LoPresti { 95030ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 951a33f13efSJoel Becker u64 last_fs_page = 952a33f13efSJoel Becker last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits); 95330ca22c7SPatrick J. LoPresti 95430ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 95530ca22c7SPatrick J. LoPresti return 0; 95630ca22c7SPatrick J. LoPresti 95730ca22c7SPatrick J. LoPresti if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT)) 95830ca22c7SPatrick J. LoPresti return -EINVAL; 95930ca22c7SPatrick J. LoPresti 960a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 961a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 96230ca22c7SPatrick J. LoPresti return -EFBIG; 96330ca22c7SPatrick J. LoPresti } 96430ca22c7SPatrick J. LoPresti return 0; 96530ca22c7SPatrick J. LoPresti } 96630ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 96730ca22c7SPatrick J. LoPresti 9681b061d92SChristoph Hellwig /* 9691b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 9701b061d92SChristoph Hellwig */ 97102c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 9721b061d92SChristoph Hellwig { 9731b061d92SChristoph Hellwig return 0; 9741b061d92SChristoph Hellwig } 975d5aacad5SAl Viro 9761da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_close); 9771da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_lseek); 9781da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_open); 9791da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_readdir); 9801da177e4SLinus Torvalds EXPORT_SYMBOL(generic_read_dir); 98151139adaSAl Viro EXPORT_SYMBOL(mount_pseudo); 982afddba49SNick Piggin EXPORT_SYMBOL(simple_write_begin); 983afddba49SNick Piggin EXPORT_SYMBOL(simple_write_end); 9841da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_inode_operations); 9851da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_operations); 9861da177e4SLinus Torvalds EXPORT_SYMBOL(simple_empty); 9871da177e4SLinus Torvalds EXPORT_SYMBOL(simple_fill_super); 9881da177e4SLinus Torvalds EXPORT_SYMBOL(simple_getattr); 9891da177e4SLinus Torvalds EXPORT_SYMBOL(simple_link); 9901da177e4SLinus Torvalds EXPORT_SYMBOL(simple_lookup); 9911da177e4SLinus Torvalds EXPORT_SYMBOL(simple_pin_fs); 9921da177e4SLinus Torvalds EXPORT_SYMBOL(simple_readpage); 9931da177e4SLinus Torvalds EXPORT_SYMBOL(simple_release_fs); 9941da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rename); 9951da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rmdir); 9961da177e4SLinus Torvalds EXPORT_SYMBOL(simple_statfs); 9971b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 9981da177e4SLinus Torvalds EXPORT_SYMBOL(simple_unlink); 9991da177e4SLinus Torvalds EXPORT_SYMBOL(simple_read_from_buffer); 10006a727b43SJiri Slaby EXPORT_SYMBOL(simple_write_to_buffer); 100193b07113SAkinobu Mita EXPORT_SYMBOL(memory_read_from_buffer); 100276791ab2SIngo Molnar EXPORT_SYMBOL(simple_transaction_set); 10031da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_get); 10041da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_read); 10051da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_release); 1006acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_open); 100774bedc4dSChristoph Hellwig EXPORT_SYMBOL_GPL(simple_attr_release); 1008acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_read); 1009acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_write); 1010