11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * fs/libfs.c 31da177e4SLinus Torvalds * Library for filesystems writers. 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 6ac13a829SFabian Frederick #include <linux/blkdev.h> 7630d9c47SPaul Gortmaker #include <linux/export.h> 81da177e4SLinus Torvalds #include <linux/pagemap.h> 95a0e3ad6STejun Heo #include <linux/slab.h> 101da177e4SLinus Torvalds #include <linux/mount.h> 111da177e4SLinus Torvalds #include <linux/vfs.h> 127bb46a67Snpiggin@suse.de #include <linux/quotaops.h> 137cf34c76SIngo Molnar #include <linux/mutex.h> 1487dc800bSAl Viro #include <linux/namei.h> 152596110aSChristoph Hellwig #include <linux/exportfs.h> 16d5aacad5SAl Viro #include <linux/writeback.h> 17ff01bb48SAl Viro #include <linux/buffer_head.h> /* sync_mapping_buffers */ 187cf34c76SIngo Molnar 191da177e4SLinus Torvalds #include <asm/uaccess.h> 201da177e4SLinus Torvalds 21a4464dbcSAl Viro #include "internal.h" 22a4464dbcSAl Viro 231da177e4SLinus Torvalds int simple_getattr(struct vfsmount *mnt, struct dentry *dentry, 241da177e4SLinus Torvalds struct kstat *stat) 251da177e4SLinus Torvalds { 26dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 271da177e4SLinus Torvalds generic_fillattr(inode, stat); 281da177e4SLinus Torvalds stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9); 291da177e4SLinus Torvalds return 0; 301da177e4SLinus Torvalds } 3112f38872SAl Viro EXPORT_SYMBOL(simple_getattr); 321da177e4SLinus Torvalds 33726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf) 341da177e4SLinus Torvalds { 35726c3342SDavid Howells buf->f_type = dentry->d_sb->s_magic; 361da177e4SLinus Torvalds buf->f_bsize = PAGE_CACHE_SIZE; 371da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 381da177e4SLinus Torvalds return 0; 391da177e4SLinus Torvalds } 4012f38872SAl Viro EXPORT_SYMBOL(simple_statfs); 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* 431da177e4SLinus Torvalds * Retaining negative dentries for an in-memory filesystem just wastes 441da177e4SLinus Torvalds * memory and lookup time: arrange for them to be deleted immediately. 451da177e4SLinus Torvalds */ 46b26d4cd3SAl Viro int always_delete_dentry(const struct dentry *dentry) 471da177e4SLinus Torvalds { 481da177e4SLinus Torvalds return 1; 491da177e4SLinus Torvalds } 50b26d4cd3SAl Viro EXPORT_SYMBOL(always_delete_dentry); 51b26d4cd3SAl Viro 52b26d4cd3SAl Viro const struct dentry_operations simple_dentry_operations = { 53b26d4cd3SAl Viro .d_delete = always_delete_dentry, 54b26d4cd3SAl Viro }; 55b26d4cd3SAl Viro EXPORT_SYMBOL(simple_dentry_operations); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * Lookup the data. This is trivial - if the dentry didn't already 591da177e4SLinus Torvalds * exist, we know it is negative. Set d_op to delete negative dentries. 601da177e4SLinus Torvalds */ 6100cd8dd3SAl Viro struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds if (dentry->d_name.len > NAME_MAX) 641da177e4SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 6574931da7SAl Viro if (!dentry->d_sb->s_d_op) 66fb045adbSNick Piggin d_set_d_op(dentry, &simple_dentry_operations); 671da177e4SLinus Torvalds d_add(dentry, NULL); 681da177e4SLinus Torvalds return NULL; 691da177e4SLinus Torvalds } 7012f38872SAl Viro EXPORT_SYMBOL(simple_lookup); 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file) 731da177e4SLinus Torvalds { 7426fe5750SLinus Torvalds static struct qstr cursor_name = QSTR_INIT(".", 1); 751da177e4SLinus Torvalds 760f7fc9e4SJosef "Jeff" Sipek file->private_data = d_alloc(file->f_path.dentry, &cursor_name); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds return file->private_data ? 0 : -ENOMEM; 791da177e4SLinus Torvalds } 8012f38872SAl Viro EXPORT_SYMBOL(dcache_dir_open); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds dput(file->private_data); 851da177e4SLinus Torvalds return 0; 861da177e4SLinus Torvalds } 8712f38872SAl Viro EXPORT_SYMBOL(dcache_dir_close); 881da177e4SLinus Torvalds 89965c8e59SAndrew Morton loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) 901da177e4SLinus Torvalds { 912fd6b7f5SNick Piggin struct dentry *dentry = file->f_path.dentry; 92dea655c2SDavid Howells mutex_lock(&d_inode(dentry)->i_mutex); 93965c8e59SAndrew Morton switch (whence) { 941da177e4SLinus Torvalds case 1: 951da177e4SLinus Torvalds offset += file->f_pos; 961da177e4SLinus Torvalds case 0: 971da177e4SLinus Torvalds if (offset >= 0) 981da177e4SLinus Torvalds break; 991da177e4SLinus Torvalds default: 100dea655c2SDavid Howells mutex_unlock(&d_inode(dentry)->i_mutex); 1011da177e4SLinus Torvalds return -EINVAL; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds if (offset != file->f_pos) { 1041da177e4SLinus Torvalds file->f_pos = offset; 1051da177e4SLinus Torvalds if (file->f_pos >= 2) { 1061da177e4SLinus Torvalds struct list_head *p; 1071da177e4SLinus Torvalds struct dentry *cursor = file->private_data; 1081da177e4SLinus Torvalds loff_t n = file->f_pos - 2; 1091da177e4SLinus Torvalds 1102fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1112fd6b7f5SNick Piggin /* d_lock not required for cursor */ 112946e51f2SAl Viro list_del(&cursor->d_child); 1132fd6b7f5SNick Piggin p = dentry->d_subdirs.next; 1142fd6b7f5SNick Piggin while (n && p != &dentry->d_subdirs) { 1151da177e4SLinus Torvalds struct dentry *next; 116946e51f2SAl Viro next = list_entry(p, struct dentry, d_child); 1172fd6b7f5SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 118da502956SNick Piggin if (simple_positive(next)) 1191da177e4SLinus Torvalds n--; 120da502956SNick Piggin spin_unlock(&next->d_lock); 1211da177e4SLinus Torvalds p = p->next; 1221da177e4SLinus Torvalds } 123946e51f2SAl Viro list_add_tail(&cursor->d_child, p); 1242fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds } 127dea655c2SDavid Howells mutex_unlock(&d_inode(dentry)->i_mutex); 1281da177e4SLinus Torvalds return offset; 1291da177e4SLinus Torvalds } 13012f38872SAl Viro EXPORT_SYMBOL(dcache_dir_lseek); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */ 1331da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds return (inode->i_mode >> 12) & 15; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* 1391da177e4SLinus Torvalds * Directory is locked and all positive dentries in it are safe, since 1401da177e4SLinus Torvalds * for ramfs-type trees they can't go away without unlink() or rmdir(), 1411da177e4SLinus Torvalds * both impossible due to the lock on directory. 1421da177e4SLinus Torvalds */ 1431da177e4SLinus Torvalds 1445f99f4e7SAl Viro int dcache_readdir(struct file *file, struct dir_context *ctx) 1451da177e4SLinus Torvalds { 1465f99f4e7SAl Viro struct dentry *dentry = file->f_path.dentry; 1475f99f4e7SAl Viro struct dentry *cursor = file->private_data; 148946e51f2SAl Viro struct list_head *p, *q = &cursor->d_child; 1491da177e4SLinus Torvalds 1505f99f4e7SAl Viro if (!dir_emit_dots(file, ctx)) 1515f99f4e7SAl Viro return 0; 1522fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1535f99f4e7SAl Viro if (ctx->pos == 2) 1541bfba4e8SAkinobu Mita list_move(q, &dentry->d_subdirs); 1551bfba4e8SAkinobu Mita 1561da177e4SLinus Torvalds for (p = q->next; p != &dentry->d_subdirs; p = p->next) { 157946e51f2SAl Viro struct dentry *next = list_entry(p, struct dentry, d_child); 158da502956SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 159da502956SNick Piggin if (!simple_positive(next)) { 160da502956SNick Piggin spin_unlock(&next->d_lock); 1611da177e4SLinus Torvalds continue; 162da502956SNick Piggin } 1631da177e4SLinus Torvalds 164da502956SNick Piggin spin_unlock(&next->d_lock); 1652fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1665f99f4e7SAl Viro if (!dir_emit(ctx, next->d_name.name, next->d_name.len, 167dea655c2SDavid Howells d_inode(next)->i_ino, dt_type(d_inode(next)))) 1681da177e4SLinus Torvalds return 0; 1692fd6b7f5SNick Piggin spin_lock(&dentry->d_lock); 1702fd6b7f5SNick Piggin spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); 1711da177e4SLinus Torvalds /* next is still alive */ 1721bfba4e8SAkinobu Mita list_move(q, p); 1732fd6b7f5SNick Piggin spin_unlock(&next->d_lock); 1741da177e4SLinus Torvalds p = q; 1755f99f4e7SAl Viro ctx->pos++; 1761da177e4SLinus Torvalds } 1772fd6b7f5SNick Piggin spin_unlock(&dentry->d_lock); 1781da177e4SLinus Torvalds return 0; 1791da177e4SLinus Torvalds } 18012f38872SAl Viro EXPORT_SYMBOL(dcache_readdir); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds return -EISDIR; 1851da177e4SLinus Torvalds } 18612f38872SAl Viro EXPORT_SYMBOL(generic_read_dir); 1871da177e4SLinus Torvalds 1884b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = { 1891da177e4SLinus Torvalds .open = dcache_dir_open, 1901da177e4SLinus Torvalds .release = dcache_dir_close, 1911da177e4SLinus Torvalds .llseek = dcache_dir_lseek, 1921da177e4SLinus Torvalds .read = generic_read_dir, 1935f99f4e7SAl Viro .iterate = dcache_readdir, 1941b061d92SChristoph Hellwig .fsync = noop_fsync, 1951da177e4SLinus Torvalds }; 19612f38872SAl Viro EXPORT_SYMBOL(simple_dir_operations); 1971da177e4SLinus Torvalds 19892e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = { 1991da177e4SLinus Torvalds .lookup = simple_lookup, 2001da177e4SLinus Torvalds }; 20112f38872SAl Viro EXPORT_SYMBOL(simple_dir_inode_operations); 2021da177e4SLinus Torvalds 203759b9775SHugh Dickins static const struct super_operations simple_super_operations = { 204759b9775SHugh Dickins .statfs = simple_statfs, 205759b9775SHugh Dickins }; 206759b9775SHugh Dickins 2071da177e4SLinus Torvalds /* 2081da177e4SLinus Torvalds * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that 2091da177e4SLinus Torvalds * will never be mountable) 2101da177e4SLinus Torvalds */ 21151139adaSAl Viro struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name, 212c74a1cbbSAl Viro const struct super_operations *ops, 213c74a1cbbSAl Viro const struct dentry_operations *dops, unsigned long magic) 2141da177e4SLinus Torvalds { 2159249e17fSDavid Howells struct super_block *s; 2161da177e4SLinus Torvalds struct dentry *dentry; 2171da177e4SLinus Torvalds struct inode *root; 21826fe5750SLinus Torvalds struct qstr d_name = QSTR_INIT(name, strlen(name)); 2191da177e4SLinus Torvalds 2209249e17fSDavid Howells s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL); 2211da177e4SLinus Torvalds if (IS_ERR(s)) 22251139adaSAl Viro return ERR_CAST(s); 2231da177e4SLinus Torvalds 22489a4eb4bSJeff Layton s->s_maxbytes = MAX_LFS_FILESIZE; 2253971e1a9SAlex Nixon s->s_blocksize = PAGE_SIZE; 2263971e1a9SAlex Nixon s->s_blocksize_bits = PAGE_SHIFT; 2271da177e4SLinus Torvalds s->s_magic = magic; 228759b9775SHugh Dickins s->s_op = ops ? ops : &simple_super_operations; 2291da177e4SLinus Torvalds s->s_time_gran = 1; 2301da177e4SLinus Torvalds root = new_inode(s); 2311da177e4SLinus Torvalds if (!root) 2321da177e4SLinus Torvalds goto Enomem; 2331a1c9bb4SJeff Layton /* 2341a1c9bb4SJeff Layton * since this is the first inode, make it number 1. New inodes created 2351a1c9bb4SJeff Layton * after this must take care not to collide with it (by passing 2361a1c9bb4SJeff Layton * max_reserved of 1 to iunique). 2371a1c9bb4SJeff Layton */ 2381a1c9bb4SJeff Layton root->i_ino = 1; 2391da177e4SLinus Torvalds root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; 2401da177e4SLinus Torvalds root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME; 241a4464dbcSAl Viro dentry = __d_alloc(s, &d_name); 2421da177e4SLinus Torvalds if (!dentry) { 2431da177e4SLinus Torvalds iput(root); 2441da177e4SLinus Torvalds goto Enomem; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds d_instantiate(dentry, root); 2471da177e4SLinus Torvalds s->s_root = dentry; 248c74a1cbbSAl Viro s->s_d_op = dops; 2491da177e4SLinus Torvalds s->s_flags |= MS_ACTIVE; 25051139adaSAl Viro return dget(s->s_root); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds Enomem: 2536f5bbff9SAl Viro deactivate_locked_super(s); 25451139adaSAl Viro return ERR_PTR(-ENOMEM); 2551da177e4SLinus Torvalds } 25612f38872SAl Viro EXPORT_SYMBOL(mount_pseudo); 2571da177e4SLinus Torvalds 25820955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file) 25920955e89SStephen Boyd { 26020955e89SStephen Boyd if (inode->i_private) 26120955e89SStephen Boyd file->private_data = inode->i_private; 26220955e89SStephen Boyd return 0; 26320955e89SStephen Boyd } 26412f38872SAl Viro EXPORT_SYMBOL(simple_open); 26520955e89SStephen Boyd 2661da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 2671da177e4SLinus Torvalds { 268dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 271d8c76e6fSDave Hansen inc_nlink(inode); 2727de9c6eeSAl Viro ihold(inode); 2731da177e4SLinus Torvalds dget(dentry); 2741da177e4SLinus Torvalds d_instantiate(dentry, inode); 2751da177e4SLinus Torvalds return 0; 2761da177e4SLinus Torvalds } 27712f38872SAl Viro EXPORT_SYMBOL(simple_link); 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); 285946e51f2SAl Viro list_for_each_entry(child, &dentry->d_subdirs, 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 } 29812f38872SAl Viro EXPORT_SYMBOL(simple_empty); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry) 3011da177e4SLinus Torvalds { 302dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; 3059a53c3a7SDave Hansen drop_nlink(inode); 3061da177e4SLinus Torvalds dput(dentry); 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds } 30912f38872SAl Viro EXPORT_SYMBOL(simple_unlink); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds if (!simple_empty(dentry)) 3141da177e4SLinus Torvalds return -ENOTEMPTY; 3151da177e4SLinus Torvalds 316dea655c2SDavid Howells drop_nlink(d_inode(dentry)); 3171da177e4SLinus Torvalds simple_unlink(dir, dentry); 3189a53c3a7SDave Hansen drop_nlink(dir); 3191da177e4SLinus Torvalds return 0; 3201da177e4SLinus Torvalds } 32112f38872SAl Viro EXPORT_SYMBOL(simple_rmdir); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry, 3241da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 3251da177e4SLinus Torvalds { 326dea655c2SDavid Howells struct inode *inode = d_inode(old_dentry); 327e36cb0b8SDavid Howells int they_are_dirs = d_is_dir(old_dentry); 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 3301da177e4SLinus Torvalds return -ENOTEMPTY; 3311da177e4SLinus Torvalds 332dea655c2SDavid Howells if (d_really_is_positive(new_dentry)) { 3331da177e4SLinus Torvalds simple_unlink(new_dir, new_dentry); 334841590ceSAl Viro if (they_are_dirs) { 335dea655c2SDavid Howells drop_nlink(d_inode(new_dentry)); 3369a53c3a7SDave Hansen drop_nlink(old_dir); 337841590ceSAl Viro } 3381da177e4SLinus Torvalds } else if (they_are_dirs) { 3399a53c3a7SDave Hansen drop_nlink(old_dir); 340d8c76e6fSDave Hansen inc_nlink(new_dir); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = 3441da177e4SLinus Torvalds new_dir->i_mtime = inode->i_ctime = CURRENT_TIME; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds return 0; 3471da177e4SLinus Torvalds } 34812f38872SAl Viro EXPORT_SYMBOL(simple_rename); 3491da177e4SLinus Torvalds 3507bb46a67Snpiggin@suse.de /** 351eef2380cSChristoph Hellwig * simple_setattr - setattr for simple filesystem 3527bb46a67Snpiggin@suse.de * @dentry: dentry 3537bb46a67Snpiggin@suse.de * @iattr: iattr structure 3547bb46a67Snpiggin@suse.de * 3557bb46a67Snpiggin@suse.de * Returns 0 on success, -error on failure. 3567bb46a67Snpiggin@suse.de * 357eef2380cSChristoph Hellwig * simple_setattr is a simple ->setattr implementation without a proper 358eef2380cSChristoph Hellwig * implementation of size changes. 359eef2380cSChristoph Hellwig * 360eef2380cSChristoph Hellwig * It can either be used for in-memory filesystems or special files 361eef2380cSChristoph Hellwig * on simple regular filesystems. Anything that needs to change on-disk 362eef2380cSChristoph Hellwig * or wire state on size changes needs its own setattr method. 3637bb46a67Snpiggin@suse.de */ 3647bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr) 3657bb46a67Snpiggin@suse.de { 366dea655c2SDavid Howells struct inode *inode = d_inode(dentry); 3677bb46a67Snpiggin@suse.de int error; 3687bb46a67Snpiggin@suse.de 3697bb46a67Snpiggin@suse.de error = inode_change_ok(inode, iattr); 3707bb46a67Snpiggin@suse.de if (error) 3717bb46a67Snpiggin@suse.de return error; 3727bb46a67Snpiggin@suse.de 3732c27c65eSChristoph Hellwig if (iattr->ia_valid & ATTR_SIZE) 3742c27c65eSChristoph Hellwig truncate_setsize(inode, iattr->ia_size); 3756a1a90adSChristoph Hellwig setattr_copy(inode, iattr); 376eef2380cSChristoph Hellwig mark_inode_dirty(inode); 377eef2380cSChristoph Hellwig return 0; 3787bb46a67Snpiggin@suse.de } 3797bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr); 3807bb46a67Snpiggin@suse.de 3811da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page) 3821da177e4SLinus Torvalds { 383c0d92cbcSPekka J Enberg clear_highpage(page); 3841da177e4SLinus Torvalds flush_dcache_page(page); 3851da177e4SLinus Torvalds SetPageUptodate(page); 3861da177e4SLinus Torvalds unlock_page(page); 3871da177e4SLinus Torvalds return 0; 3881da177e4SLinus Torvalds } 38912f38872SAl Viro EXPORT_SYMBOL(simple_readpage); 3901da177e4SLinus Torvalds 391afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping, 392afddba49SNick Piggin loff_t pos, unsigned len, unsigned flags, 393afddba49SNick Piggin struct page **pagep, void **fsdata) 394afddba49SNick Piggin { 395afddba49SNick Piggin struct page *page; 396afddba49SNick Piggin pgoff_t index; 397afddba49SNick Piggin 398afddba49SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 399afddba49SNick Piggin 40054566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 401afddba49SNick Piggin if (!page) 402afddba49SNick Piggin return -ENOMEM; 403afddba49SNick Piggin 404afddba49SNick Piggin *pagep = page; 405afddba49SNick Piggin 406193cf4b9SBoaz Harrosh if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) { 407193cf4b9SBoaz Harrosh unsigned from = pos & (PAGE_CACHE_SIZE - 1); 408193cf4b9SBoaz Harrosh 409193cf4b9SBoaz Harrosh zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE); 410193cf4b9SBoaz Harrosh } 411193cf4b9SBoaz Harrosh return 0; 412afddba49SNick Piggin } 41312f38872SAl Viro EXPORT_SYMBOL(simple_write_begin); 414afddba49SNick Piggin 415ad2a722fSBoaz Harrosh /** 416ad2a722fSBoaz Harrosh * simple_write_end - .write_end helper for non-block-device FSes 417ad2a722fSBoaz Harrosh * @available: See .write_end of address_space_operations 418ad2a722fSBoaz Harrosh * @file: " 419ad2a722fSBoaz Harrosh * @mapping: " 420ad2a722fSBoaz Harrosh * @pos: " 421ad2a722fSBoaz Harrosh * @len: " 422ad2a722fSBoaz Harrosh * @copied: " 423ad2a722fSBoaz Harrosh * @page: " 424ad2a722fSBoaz Harrosh * @fsdata: " 425ad2a722fSBoaz Harrosh * 426ad2a722fSBoaz Harrosh * simple_write_end does the minimum needed for updating a page after writing is 427ad2a722fSBoaz Harrosh * done. It has the same API signature as the .write_end of 428ad2a722fSBoaz Harrosh * address_space_operations vector. So it can just be set onto .write_end for 429ad2a722fSBoaz Harrosh * FSes that don't need any other processing. i_mutex is assumed to be held. 430ad2a722fSBoaz Harrosh * Block based filesystems should use generic_write_end(). 431ad2a722fSBoaz Harrosh * NOTE: Even though i_size might get updated by this function, mark_inode_dirty 432ad2a722fSBoaz Harrosh * is not called, so a filesystem that actually does store data in .write_inode 433ad2a722fSBoaz Harrosh * should extend on what's done here with a call to mark_inode_dirty() in the 434ad2a722fSBoaz Harrosh * case that i_size has changed. 435ad2a722fSBoaz Harrosh */ 436ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping, 437ad2a722fSBoaz Harrosh loff_t pos, unsigned len, unsigned copied, 438ad2a722fSBoaz Harrosh struct page *page, void *fsdata) 4391da177e4SLinus Torvalds { 4401da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 441ad2a722fSBoaz Harrosh loff_t last_pos = pos + copied; 442ad2a722fSBoaz Harrosh 443ad2a722fSBoaz Harrosh /* zero the stale part of the page if we did a short copy */ 444ad2a722fSBoaz Harrosh if (copied < len) { 445ad2a722fSBoaz Harrosh unsigned from = pos & (PAGE_CACHE_SIZE - 1); 446ad2a722fSBoaz Harrosh 447ad2a722fSBoaz Harrosh zero_user(page, from + copied, len - copied); 448ad2a722fSBoaz Harrosh } 4491da177e4SLinus Torvalds 450955eff5aSNick Piggin if (!PageUptodate(page)) 451955eff5aSNick Piggin SetPageUptodate(page); 4521da177e4SLinus Torvalds /* 4531da177e4SLinus Torvalds * No need to use i_size_read() here, the i_size 4541b1dcc1bSJes Sorensen * cannot change under us because we hold the i_mutex. 4551da177e4SLinus Torvalds */ 456ad2a722fSBoaz Harrosh if (last_pos > inode->i_size) 457ad2a722fSBoaz Harrosh i_size_write(inode, last_pos); 458ad2a722fSBoaz Harrosh 4591da177e4SLinus Torvalds set_page_dirty(page); 460afddba49SNick Piggin unlock_page(page); 461afddba49SNick Piggin page_cache_release(page); 462afddba49SNick Piggin 463afddba49SNick Piggin return copied; 464afddba49SNick Piggin } 46512f38872SAl Viro EXPORT_SYMBOL(simple_write_end); 466afddba49SNick Piggin 4671a1c9bb4SJeff Layton /* 4681a1c9bb4SJeff Layton * the inodes created here are not hashed. If you use iunique to generate 4691a1c9bb4SJeff Layton * unique inode values later for this filesystem, then you must take care 4701a1c9bb4SJeff Layton * to pass it an appropriate max_reserved value to avoid collisions. 4711a1c9bb4SJeff Layton */ 4727d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic, 4737d683a09SRoberto Sassu struct tree_descr *files) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds struct inode *inode; 4761da177e4SLinus Torvalds struct dentry *root; 4771da177e4SLinus Torvalds struct dentry *dentry; 4781da177e4SLinus Torvalds int i; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds s->s_blocksize = PAGE_CACHE_SIZE; 4811da177e4SLinus Torvalds s->s_blocksize_bits = PAGE_CACHE_SHIFT; 4821da177e4SLinus Torvalds s->s_magic = magic; 483759b9775SHugh Dickins s->s_op = &simple_super_operations; 4841da177e4SLinus Torvalds s->s_time_gran = 1; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds inode = new_inode(s); 4871da177e4SLinus Torvalds if (!inode) 4881da177e4SLinus Torvalds return -ENOMEM; 4891a1c9bb4SJeff Layton /* 4901a1c9bb4SJeff Layton * because the root inode is 1, the files array must not contain an 4911a1c9bb4SJeff Layton * entry at index 1 4921a1c9bb4SJeff Layton */ 4931a1c9bb4SJeff Layton inode->i_ino = 1; 4941da177e4SLinus Torvalds inode->i_mode = S_IFDIR | 0755; 4951da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 4961da177e4SLinus Torvalds inode->i_op = &simple_dir_inode_operations; 4971da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 498bfe86848SMiklos Szeredi set_nlink(inode, 2); 49948fde701SAl Viro root = d_make_root(inode); 50048fde701SAl Viro if (!root) 5011da177e4SLinus Torvalds return -ENOMEM; 5021da177e4SLinus Torvalds for (i = 0; !files->name || files->name[0]; i++, files++) { 5031da177e4SLinus Torvalds if (!files->name) 5041da177e4SLinus Torvalds continue; 5051a1c9bb4SJeff Layton 5061a1c9bb4SJeff Layton /* warn if it tries to conflict with the root inode */ 5071a1c9bb4SJeff Layton if (unlikely(i == 1)) 5081a1c9bb4SJeff Layton printk(KERN_WARNING "%s: %s passed in a files array" 5091a1c9bb4SJeff Layton "with an index of 1!\n", __func__, 5101a1c9bb4SJeff Layton s->s_type->name); 5111a1c9bb4SJeff Layton 5121da177e4SLinus Torvalds dentry = d_alloc_name(root, files->name); 5131da177e4SLinus Torvalds if (!dentry) 5141da177e4SLinus Torvalds goto out; 5151da177e4SLinus Torvalds inode = new_inode(s); 51632096ea1SKonstantin Khlebnikov if (!inode) { 51732096ea1SKonstantin Khlebnikov dput(dentry); 5181da177e4SLinus Torvalds goto out; 51932096ea1SKonstantin Khlebnikov } 5201da177e4SLinus Torvalds inode->i_mode = S_IFREG | files->mode; 5211da177e4SLinus Torvalds inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 5221da177e4SLinus Torvalds inode->i_fop = files->ops; 5231da177e4SLinus Torvalds inode->i_ino = i; 5241da177e4SLinus Torvalds d_add(dentry, inode); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds s->s_root = root; 5271da177e4SLinus Torvalds return 0; 5281da177e4SLinus Torvalds out: 5291da177e4SLinus Torvalds d_genocide(root); 530640946f2SAl Viro shrink_dcache_parent(root); 5311da177e4SLinus Torvalds dput(root); 5321da177e4SLinus Torvalds return -ENOMEM; 5331da177e4SLinus Torvalds } 53412f38872SAl Viro EXPORT_SYMBOL(simple_fill_super); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock); 5371da177e4SLinus Torvalds 5381f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 5411da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5421da177e4SLinus Torvalds if (unlikely(!*mount)) { 5431da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5442452992aSAl Viro mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL); 5451da177e4SLinus Torvalds if (IS_ERR(mnt)) 5461da177e4SLinus Torvalds return PTR_ERR(mnt); 5471da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5481da177e4SLinus Torvalds if (!*mount) 5491da177e4SLinus Torvalds *mount = mnt; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds mntget(*mount); 5521da177e4SLinus Torvalds ++*count; 5531da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5541da177e4SLinus Torvalds mntput(mnt); 5551da177e4SLinus Torvalds return 0; 5561da177e4SLinus Torvalds } 55712f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs); 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count) 5601da177e4SLinus Torvalds { 5611da177e4SLinus Torvalds struct vfsmount *mnt; 5621da177e4SLinus Torvalds spin_lock(&pin_fs_lock); 5631da177e4SLinus Torvalds mnt = *mount; 5641da177e4SLinus Torvalds if (!--*count) 5651da177e4SLinus Torvalds *mount = NULL; 5661da177e4SLinus Torvalds spin_unlock(&pin_fs_lock); 5671da177e4SLinus Torvalds mntput(mnt); 5681da177e4SLinus Torvalds } 56912f38872SAl Viro EXPORT_SYMBOL(simple_release_fs); 5701da177e4SLinus Torvalds 5716d1029b5SAkinobu Mita /** 5726d1029b5SAkinobu Mita * simple_read_from_buffer - copy data from the buffer to user space 5736d1029b5SAkinobu Mita * @to: the user space buffer to read to 5746d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 5756d1029b5SAkinobu Mita * @ppos: the current position in the buffer 5766d1029b5SAkinobu Mita * @from: the buffer to read from 5776d1029b5SAkinobu Mita * @available: the size of the buffer 5786d1029b5SAkinobu Mita * 5796d1029b5SAkinobu Mita * The simple_read_from_buffer() function reads up to @count bytes from the 5806d1029b5SAkinobu Mita * buffer @from at offset @ppos into the user space address starting at @to. 5816d1029b5SAkinobu Mita * 5826d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 5836d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 5846d1029b5SAkinobu Mita **/ 5851da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, 5861da177e4SLinus Torvalds const void *from, size_t available) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds loff_t pos = *ppos; 58914be2746SSteven Rostedt size_t ret; 59014be2746SSteven Rostedt 5911da177e4SLinus Torvalds if (pos < 0) 5921da177e4SLinus Torvalds return -EINVAL; 59314be2746SSteven Rostedt if (pos >= available || !count) 5941da177e4SLinus Torvalds return 0; 5951da177e4SLinus Torvalds if (count > available - pos) 5961da177e4SLinus Torvalds count = available - pos; 59714be2746SSteven Rostedt ret = copy_to_user(to, from + pos, count); 59814be2746SSteven Rostedt if (ret == count) 5991da177e4SLinus Torvalds return -EFAULT; 60014be2746SSteven Rostedt count -= ret; 6011da177e4SLinus Torvalds *ppos = pos + count; 6021da177e4SLinus Torvalds return count; 6031da177e4SLinus Torvalds } 60412f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer); 6051da177e4SLinus Torvalds 6066d1029b5SAkinobu Mita /** 6076a727b43SJiri Slaby * simple_write_to_buffer - copy data from user space to the buffer 6086a727b43SJiri Slaby * @to: the buffer to write to 6096a727b43SJiri Slaby * @available: the size of the buffer 6106a727b43SJiri Slaby * @ppos: the current position in the buffer 6116a727b43SJiri Slaby * @from: the user space buffer to read from 6126a727b43SJiri Slaby * @count: the maximum number of bytes to read 6136a727b43SJiri Slaby * 6146a727b43SJiri Slaby * The simple_write_to_buffer() function reads up to @count bytes from the user 6156a727b43SJiri Slaby * space address starting at @from into the buffer @to at offset @ppos. 6166a727b43SJiri Slaby * 6176a727b43SJiri Slaby * On success, the number of bytes written is returned and the offset @ppos is 6186a727b43SJiri Slaby * advanced by this number, or negative value is returned on error. 6196a727b43SJiri Slaby **/ 6206a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, 6216a727b43SJiri Slaby const void __user *from, size_t count) 6226a727b43SJiri Slaby { 6236a727b43SJiri Slaby loff_t pos = *ppos; 6246a727b43SJiri Slaby size_t res; 6256a727b43SJiri Slaby 6266a727b43SJiri Slaby if (pos < 0) 6276a727b43SJiri Slaby return -EINVAL; 6286a727b43SJiri Slaby if (pos >= available || !count) 6296a727b43SJiri Slaby return 0; 6306a727b43SJiri Slaby if (count > available - pos) 6316a727b43SJiri Slaby count = available - pos; 6326a727b43SJiri Slaby res = copy_from_user(to + pos, from, count); 6336a727b43SJiri Slaby if (res == count) 6346a727b43SJiri Slaby return -EFAULT; 6356a727b43SJiri Slaby count -= res; 6366a727b43SJiri Slaby *ppos = pos + count; 6376a727b43SJiri Slaby return count; 6386a727b43SJiri Slaby } 63912f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer); 6406a727b43SJiri Slaby 6416a727b43SJiri Slaby /** 6426d1029b5SAkinobu Mita * memory_read_from_buffer - copy data from the buffer 6436d1029b5SAkinobu Mita * @to: the kernel space buffer to read to 6446d1029b5SAkinobu Mita * @count: the maximum number of bytes to read 6456d1029b5SAkinobu Mita * @ppos: the current position in the buffer 6466d1029b5SAkinobu Mita * @from: the buffer to read from 6476d1029b5SAkinobu Mita * @available: the size of the buffer 6486d1029b5SAkinobu Mita * 6496d1029b5SAkinobu Mita * The memory_read_from_buffer() function reads up to @count bytes from the 6506d1029b5SAkinobu Mita * buffer @from at offset @ppos into the kernel space address starting at @to. 6516d1029b5SAkinobu Mita * 6526d1029b5SAkinobu Mita * On success, the number of bytes read is returned and the offset @ppos is 6536d1029b5SAkinobu Mita * advanced by this number, or negative value is returned on error. 6546d1029b5SAkinobu Mita **/ 65593b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, 65693b07113SAkinobu Mita const void *from, size_t available) 65793b07113SAkinobu Mita { 65893b07113SAkinobu Mita loff_t pos = *ppos; 65993b07113SAkinobu Mita 66093b07113SAkinobu Mita if (pos < 0) 66193b07113SAkinobu Mita return -EINVAL; 66293b07113SAkinobu Mita if (pos >= available) 66393b07113SAkinobu Mita return 0; 66493b07113SAkinobu Mita if (count > available - pos) 66593b07113SAkinobu Mita count = available - pos; 66693b07113SAkinobu Mita memcpy(to, from + pos, count); 66793b07113SAkinobu Mita *ppos = pos + count; 66893b07113SAkinobu Mita 66993b07113SAkinobu Mita return count; 67093b07113SAkinobu Mita } 67112f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer); 67293b07113SAkinobu Mita 6731da177e4SLinus Torvalds /* 6741da177e4SLinus Torvalds * Transaction based IO. 6751da177e4SLinus Torvalds * The file expects a single write which triggers the transaction, and then 6761da177e4SLinus Torvalds * possibly a read which collects the result - which is stored in a 6771da177e4SLinus Torvalds * file-local buffer. 6781da177e4SLinus Torvalds */ 67976791ab2SIngo Molnar 68076791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n) 68176791ab2SIngo Molnar { 68276791ab2SIngo Molnar struct simple_transaction_argresp *ar = file->private_data; 68376791ab2SIngo Molnar 68476791ab2SIngo Molnar BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); 68576791ab2SIngo Molnar 68676791ab2SIngo Molnar /* 68776791ab2SIngo Molnar * The barrier ensures that ar->size will really remain zero until 68876791ab2SIngo Molnar * ar->data is ready for reading. 68976791ab2SIngo Molnar */ 69076791ab2SIngo Molnar smp_mb(); 69176791ab2SIngo Molnar ar->size = n; 69276791ab2SIngo Molnar } 69312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set); 69476791ab2SIngo Molnar 6951da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) 6961da177e4SLinus Torvalds { 6971da177e4SLinus Torvalds struct simple_transaction_argresp *ar; 6981da177e4SLinus Torvalds static DEFINE_SPINLOCK(simple_transaction_lock); 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds if (size > SIMPLE_TRANSACTION_LIMIT - 1) 7011da177e4SLinus Torvalds return ERR_PTR(-EFBIG); 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); 7041da177e4SLinus Torvalds if (!ar) 7051da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds spin_lock(&simple_transaction_lock); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* only one write allowed per open */ 7101da177e4SLinus Torvalds if (file->private_data) { 7111da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7121da177e4SLinus Torvalds free_page((unsigned long)ar); 7131da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds file->private_data = ar; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds spin_unlock(&simple_transaction_lock); 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds if (copy_from_user(ar->data, buf, size)) 7211da177e4SLinus Torvalds return ERR_PTR(-EFAULT); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds return ar->data; 7241da177e4SLinus Torvalds } 72512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get); 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 7281da177e4SLinus Torvalds { 7291da177e4SLinus Torvalds struct simple_transaction_argresp *ar = file->private_data; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds if (!ar) 7321da177e4SLinus Torvalds return 0; 7331da177e4SLinus Torvalds return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); 7341da177e4SLinus Torvalds } 73512f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read); 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds free_page((unsigned long)file->private_data); 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds } 74212f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release); 7431da177e4SLinus Torvalds 744acaefc25SArnd Bergmann /* Simple attribute files */ 745acaefc25SArnd Bergmann 746acaefc25SArnd Bergmann struct simple_attr { 7478b88b099SChristoph Hellwig int (*get)(void *, u64 *); 7488b88b099SChristoph Hellwig int (*set)(void *, u64); 749acaefc25SArnd Bergmann char get_buf[24]; /* enough to store a u64 and "\n\0" */ 750acaefc25SArnd Bergmann char set_buf[24]; 751acaefc25SArnd Bergmann void *data; 752acaefc25SArnd Bergmann const char *fmt; /* format for read operation */ 7537cf34c76SIngo Molnar struct mutex mutex; /* protects access to these buffers */ 754acaefc25SArnd Bergmann }; 755acaefc25SArnd Bergmann 756acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation 757acaefc25SArnd Bergmann * to set the attribute specific access operations. */ 758acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file, 7598b88b099SChristoph Hellwig int (*get)(void *, u64 *), int (*set)(void *, u64), 760acaefc25SArnd Bergmann const char *fmt) 761acaefc25SArnd Bergmann { 762acaefc25SArnd Bergmann struct simple_attr *attr; 763acaefc25SArnd Bergmann 764acaefc25SArnd Bergmann attr = kmalloc(sizeof(*attr), GFP_KERNEL); 765acaefc25SArnd Bergmann if (!attr) 766acaefc25SArnd Bergmann return -ENOMEM; 767acaefc25SArnd Bergmann 768acaefc25SArnd Bergmann attr->get = get; 769acaefc25SArnd Bergmann attr->set = set; 7708e18e294STheodore Ts'o attr->data = inode->i_private; 771acaefc25SArnd Bergmann attr->fmt = fmt; 7727cf34c76SIngo Molnar mutex_init(&attr->mutex); 773acaefc25SArnd Bergmann 774acaefc25SArnd Bergmann file->private_data = attr; 775acaefc25SArnd Bergmann 776acaefc25SArnd Bergmann return nonseekable_open(inode, file); 777acaefc25SArnd Bergmann } 77812f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open); 779acaefc25SArnd Bergmann 78074bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file) 781acaefc25SArnd Bergmann { 782acaefc25SArnd Bergmann kfree(file->private_data); 783acaefc25SArnd Bergmann return 0; 784acaefc25SArnd Bergmann } 78512f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ 786acaefc25SArnd Bergmann 787acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */ 788acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf, 789acaefc25SArnd Bergmann size_t len, loff_t *ppos) 790acaefc25SArnd Bergmann { 791acaefc25SArnd Bergmann struct simple_attr *attr; 792acaefc25SArnd Bergmann size_t size; 793acaefc25SArnd Bergmann ssize_t ret; 794acaefc25SArnd Bergmann 795acaefc25SArnd Bergmann attr = file->private_data; 796acaefc25SArnd Bergmann 797acaefc25SArnd Bergmann if (!attr->get) 798acaefc25SArnd Bergmann return -EACCES; 799acaefc25SArnd Bergmann 8009261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 8019261303aSChristoph Hellwig if (ret) 8029261303aSChristoph Hellwig return ret; 8039261303aSChristoph Hellwig 8048b88b099SChristoph Hellwig if (*ppos) { /* continued read */ 805acaefc25SArnd Bergmann size = strlen(attr->get_buf); 8068b88b099SChristoph Hellwig } else { /* first read */ 8078b88b099SChristoph Hellwig u64 val; 8088b88b099SChristoph Hellwig ret = attr->get(attr->data, &val); 8098b88b099SChristoph Hellwig if (ret) 8108b88b099SChristoph Hellwig goto out; 8118b88b099SChristoph Hellwig 812acaefc25SArnd Bergmann size = scnprintf(attr->get_buf, sizeof(attr->get_buf), 8138b88b099SChristoph Hellwig attr->fmt, (unsigned long long)val); 8148b88b099SChristoph Hellwig } 815acaefc25SArnd Bergmann 816acaefc25SArnd Bergmann ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); 8178b88b099SChristoph Hellwig out: 8187cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 819acaefc25SArnd Bergmann return ret; 820acaefc25SArnd Bergmann } 82112f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read); 822acaefc25SArnd Bergmann 823acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */ 824acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf, 825acaefc25SArnd Bergmann size_t len, loff_t *ppos) 826acaefc25SArnd Bergmann { 827acaefc25SArnd Bergmann struct simple_attr *attr; 828acaefc25SArnd Bergmann u64 val; 829acaefc25SArnd Bergmann size_t size; 830acaefc25SArnd Bergmann ssize_t ret; 831acaefc25SArnd Bergmann 832acaefc25SArnd Bergmann attr = file->private_data; 833acaefc25SArnd Bergmann if (!attr->set) 834acaefc25SArnd Bergmann return -EACCES; 835acaefc25SArnd Bergmann 8369261303aSChristoph Hellwig ret = mutex_lock_interruptible(&attr->mutex); 8379261303aSChristoph Hellwig if (ret) 8389261303aSChristoph Hellwig return ret; 8399261303aSChristoph Hellwig 840acaefc25SArnd Bergmann ret = -EFAULT; 841acaefc25SArnd Bergmann size = min(sizeof(attr->set_buf) - 1, len); 842acaefc25SArnd Bergmann if (copy_from_user(attr->set_buf, buf, size)) 843acaefc25SArnd Bergmann goto out; 844acaefc25SArnd Bergmann 845acaefc25SArnd Bergmann attr->set_buf[size] = '\0'; 846f7b88631SAkinobu Mita val = simple_strtoll(attr->set_buf, NULL, 0); 84705cc0ceeSWu Fengguang ret = attr->set(attr->data, val); 84805cc0ceeSWu Fengguang if (ret == 0) 84905cc0ceeSWu Fengguang ret = len; /* on success, claim we got the whole input */ 850acaefc25SArnd Bergmann out: 8517cf34c76SIngo Molnar mutex_unlock(&attr->mutex); 852acaefc25SArnd Bergmann return ret; 853acaefc25SArnd Bergmann } 85412f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write); 855acaefc25SArnd Bergmann 8562596110aSChristoph Hellwig /** 8572596110aSChristoph Hellwig * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation 8582596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 8592596110aSChristoph Hellwig * @fid: file handle to convert 8602596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 8612596110aSChristoph Hellwig * @fh_type: type of file handle 8622596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 8632596110aSChristoph Hellwig * 8642596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 8652596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 8662596110aSChristoph Hellwig * inode for the object specified in the file handle. 8672596110aSChristoph Hellwig */ 8682596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, 8692596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 8702596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 8712596110aSChristoph Hellwig { 8722596110aSChristoph Hellwig struct inode *inode = NULL; 8732596110aSChristoph Hellwig 8742596110aSChristoph Hellwig if (fh_len < 2) 8752596110aSChristoph Hellwig return NULL; 8762596110aSChristoph Hellwig 8772596110aSChristoph Hellwig switch (fh_type) { 8782596110aSChristoph Hellwig case FILEID_INO32_GEN: 8792596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 8802596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.ino, fid->i32.gen); 8812596110aSChristoph Hellwig break; 8822596110aSChristoph Hellwig } 8832596110aSChristoph Hellwig 8844ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 8852596110aSChristoph Hellwig } 8862596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry); 8872596110aSChristoph Hellwig 8882596110aSChristoph Hellwig /** 889ca186830SYanchuan Nian * generic_fh_to_parent - generic helper for the fh_to_parent export operation 8902596110aSChristoph Hellwig * @sb: filesystem to do the file handle conversion on 8912596110aSChristoph Hellwig * @fid: file handle to convert 8922596110aSChristoph Hellwig * @fh_len: length of the file handle in bytes 8932596110aSChristoph Hellwig * @fh_type: type of file handle 8942596110aSChristoph Hellwig * @get_inode: filesystem callback to retrieve inode 8952596110aSChristoph Hellwig * 8962596110aSChristoph Hellwig * This function decodes @fid as long as it has one of the well-known 8972596110aSChristoph Hellwig * Linux filehandle types and calls @get_inode on it to retrieve the 8982596110aSChristoph Hellwig * inode for the _parent_ object specified in the file handle if it 8992596110aSChristoph Hellwig * is specified in the file handle, or NULL otherwise. 9002596110aSChristoph Hellwig */ 9012596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, 9022596110aSChristoph Hellwig int fh_len, int fh_type, struct inode *(*get_inode) 9032596110aSChristoph Hellwig (struct super_block *sb, u64 ino, u32 gen)) 9042596110aSChristoph Hellwig { 9052596110aSChristoph Hellwig struct inode *inode = NULL; 9062596110aSChristoph Hellwig 9072596110aSChristoph Hellwig if (fh_len <= 2) 9082596110aSChristoph Hellwig return NULL; 9092596110aSChristoph Hellwig 9102596110aSChristoph Hellwig switch (fh_type) { 9112596110aSChristoph Hellwig case FILEID_INO32_GEN_PARENT: 9122596110aSChristoph Hellwig inode = get_inode(sb, fid->i32.parent_ino, 9132596110aSChristoph Hellwig (fh_len > 3 ? fid->i32.parent_gen : 0)); 9142596110aSChristoph Hellwig break; 9152596110aSChristoph Hellwig } 9162596110aSChristoph Hellwig 9174ea3ada2SChristoph Hellwig return d_obtain_alias(inode); 9182596110aSChristoph Hellwig } 9192596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent); 9202596110aSChristoph Hellwig 9211b061d92SChristoph Hellwig /** 922ac13a829SFabian Frederick * __generic_file_fsync - generic fsync implementation for simple filesystems 923ac13a829SFabian Frederick * 9241b061d92SChristoph Hellwig * @file: file to synchronize 925ac13a829SFabian Frederick * @start: start offset in bytes 926ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 9271b061d92SChristoph Hellwig * @datasync: only synchronize essential metadata if true 9281b061d92SChristoph Hellwig * 9291b061d92SChristoph Hellwig * This is a generic implementation of the fsync method for simple 9301b061d92SChristoph Hellwig * filesystems which track all non-inode metadata in the buffers list 9311b061d92SChristoph Hellwig * hanging off the address_space structure. 9321b061d92SChristoph Hellwig */ 933ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end, 93402c24a82SJosef Bacik int datasync) 935d5aacad5SAl Viro { 9367ea80859SChristoph Hellwig struct inode *inode = file->f_mapping->host; 937d5aacad5SAl Viro int err; 938d5aacad5SAl Viro int ret; 939d5aacad5SAl Viro 94002c24a82SJosef Bacik err = filemap_write_and_wait_range(inode->i_mapping, start, end); 94102c24a82SJosef Bacik if (err) 94202c24a82SJosef Bacik return err; 94302c24a82SJosef Bacik 94402c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 945d5aacad5SAl Viro ret = sync_mapping_buffers(inode->i_mapping); 9460ae45f63STheodore Ts'o if (!(inode->i_state & I_DIRTY_ALL)) 94702c24a82SJosef Bacik goto out; 948d5aacad5SAl Viro if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) 94902c24a82SJosef Bacik goto out; 950d5aacad5SAl Viro 951c3765016SChristoph Hellwig err = sync_inode_metadata(inode, 1); 952d5aacad5SAl Viro if (ret == 0) 953d5aacad5SAl Viro ret = err; 954ac13a829SFabian Frederick 95502c24a82SJosef Bacik out: 95602c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 957d5aacad5SAl Viro return ret; 958d5aacad5SAl Viro } 959ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync); 960ac13a829SFabian Frederick 961ac13a829SFabian Frederick /** 962ac13a829SFabian Frederick * generic_file_fsync - generic fsync implementation for simple filesystems 963ac13a829SFabian Frederick * with flush 964ac13a829SFabian Frederick * @file: file to synchronize 965ac13a829SFabian Frederick * @start: start offset in bytes 966ac13a829SFabian Frederick * @end: end offset in bytes (inclusive) 967ac13a829SFabian Frederick * @datasync: only synchronize essential metadata if true 968ac13a829SFabian Frederick * 969ac13a829SFabian Frederick */ 970ac13a829SFabian Frederick 971ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end, 972ac13a829SFabian Frederick int datasync) 973ac13a829SFabian Frederick { 974ac13a829SFabian Frederick struct inode *inode = file->f_mapping->host; 975ac13a829SFabian Frederick int err; 976ac13a829SFabian Frederick 977ac13a829SFabian Frederick err = __generic_file_fsync(file, start, end, datasync); 978ac13a829SFabian Frederick if (err) 979ac13a829SFabian Frederick return err; 980ac13a829SFabian Frederick return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); 981ac13a829SFabian Frederick } 9821b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync); 9831b061d92SChristoph Hellwig 98430ca22c7SPatrick J. LoPresti /** 98530ca22c7SPatrick J. LoPresti * generic_check_addressable - Check addressability of file system 98630ca22c7SPatrick J. LoPresti * @blocksize_bits: log of file system block size 98730ca22c7SPatrick J. LoPresti * @num_blocks: number of blocks in file system 98830ca22c7SPatrick J. LoPresti * 98930ca22c7SPatrick J. LoPresti * Determine whether a file system with @num_blocks blocks (and a 99030ca22c7SPatrick J. LoPresti * block size of 2**@blocksize_bits) is addressable by the sector_t 99130ca22c7SPatrick J. LoPresti * and page cache of the system. Return 0 if so and -EFBIG otherwise. 99230ca22c7SPatrick J. LoPresti */ 99330ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) 99430ca22c7SPatrick J. LoPresti { 99530ca22c7SPatrick J. LoPresti u64 last_fs_block = num_blocks - 1; 996a33f13efSJoel Becker u64 last_fs_page = 997a33f13efSJoel Becker last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits); 99830ca22c7SPatrick J. LoPresti 99930ca22c7SPatrick J. LoPresti if (unlikely(num_blocks == 0)) 100030ca22c7SPatrick J. LoPresti return 0; 100130ca22c7SPatrick J. LoPresti 100230ca22c7SPatrick J. LoPresti if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT)) 100330ca22c7SPatrick J. LoPresti return -EINVAL; 100430ca22c7SPatrick J. LoPresti 1005a33f13efSJoel Becker if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || 1006a33f13efSJoel Becker (last_fs_page > (pgoff_t)(~0ULL))) { 100730ca22c7SPatrick J. LoPresti return -EFBIG; 100830ca22c7SPatrick J. LoPresti } 100930ca22c7SPatrick J. LoPresti return 0; 101030ca22c7SPatrick J. LoPresti } 101130ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable); 101230ca22c7SPatrick J. LoPresti 10131b061d92SChristoph Hellwig /* 10141b061d92SChristoph Hellwig * No-op implementation of ->fsync for in-memory filesystems. 10151b061d92SChristoph Hellwig */ 101602c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) 10171b061d92SChristoph Hellwig { 10181b061d92SChristoph Hellwig return 0; 10191b061d92SChristoph Hellwig } 10201b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync); 102187dc800bSAl Viro 10225f2c4179SAl Viro void kfree_put_link(struct inode *unused, void *cookie) 102387dc800bSAl Viro { 1024680baacbSAl Viro kfree(cookie); 102587dc800bSAl Viro } 102687dc800bSAl Viro EXPORT_SYMBOL(kfree_put_link); 10276987843fSAl Viro 1028ecc087ffSAl Viro void free_page_put_link(struct inode *unused, void *cookie) 1029ecc087ffSAl Viro { 1030ecc087ffSAl Viro free_page((unsigned long) cookie); 1031ecc087ffSAl Viro } 1032ecc087ffSAl Viro EXPORT_SYMBOL(free_page_put_link); 1033ecc087ffSAl Viro 10346987843fSAl Viro /* 10356987843fSAl Viro * nop .set_page_dirty method so that people can use .page_mkwrite on 10366987843fSAl Viro * anon inodes. 10376987843fSAl Viro */ 10386987843fSAl Viro static int anon_set_page_dirty(struct page *page) 10396987843fSAl Viro { 10406987843fSAl Viro return 0; 10416987843fSAl Viro }; 10426987843fSAl Viro 10436987843fSAl Viro /* 10446987843fSAl Viro * A single inode exists for all anon_inode files. Contrary to pipes, 10456987843fSAl Viro * anon_inode inodes have no associated per-instance data, so we need 10466987843fSAl Viro * only allocate one of them. 10476987843fSAl Viro */ 10486987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s) 10496987843fSAl Viro { 10506987843fSAl Viro static const struct address_space_operations anon_aops = { 10516987843fSAl Viro .set_page_dirty = anon_set_page_dirty, 10526987843fSAl Viro }; 10536987843fSAl Viro struct inode *inode = new_inode_pseudo(s); 10546987843fSAl Viro 10556987843fSAl Viro if (!inode) 10566987843fSAl Viro return ERR_PTR(-ENOMEM); 10576987843fSAl Viro 10586987843fSAl Viro inode->i_ino = get_next_ino(); 10596987843fSAl Viro inode->i_mapping->a_ops = &anon_aops; 10606987843fSAl Viro 10616987843fSAl Viro /* 10626987843fSAl Viro * Mark the inode dirty from the very beginning, 10636987843fSAl Viro * that way it will never be moved to the dirty 10646987843fSAl Viro * list because mark_inode_dirty() will think 10656987843fSAl Viro * that it already _is_ on the dirty list. 10666987843fSAl Viro */ 10676987843fSAl Viro inode->i_state = I_DIRTY; 10686987843fSAl Viro inode->i_mode = S_IRUSR | S_IWUSR; 10696987843fSAl Viro inode->i_uid = current_fsuid(); 10706987843fSAl Viro inode->i_gid = current_fsgid(); 10716987843fSAl Viro inode->i_flags |= S_PRIVATE; 10726987843fSAl Viro inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 10736987843fSAl Viro return inode; 10746987843fSAl Viro } 10756987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode); 10761c994a09SJeff Layton 10771c994a09SJeff Layton /** 10781c994a09SJeff Layton * simple_nosetlease - generic helper for prohibiting leases 10791c994a09SJeff Layton * @filp: file pointer 10801c994a09SJeff Layton * @arg: type of lease to obtain 10811c994a09SJeff Layton * @flp: new lease supplied for insertion 1082e6f5c789SJeff Layton * @priv: private data for lm_setup operation 10831c994a09SJeff Layton * 10841c994a09SJeff Layton * Generic helper for filesystems that do not wish to allow leases to be set. 10851c994a09SJeff Layton * All arguments are ignored and it just returns -EINVAL. 10861c994a09SJeff Layton */ 10871c994a09SJeff Layton int 1088e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, 1089e6f5c789SJeff Layton void **priv) 10901c994a09SJeff Layton { 10911c994a09SJeff Layton return -EINVAL; 10921c994a09SJeff Layton } 10931c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease); 109461ba64fcSAl Viro 10956b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode, 10966b255391SAl Viro void **cookie) 109761ba64fcSAl Viro { 10986b255391SAl Viro return inode->i_link; 109961ba64fcSAl Viro } 11006b255391SAl Viro EXPORT_SYMBOL(simple_get_link); 110161ba64fcSAl Viro 110261ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = { 11036b255391SAl Viro .get_link = simple_get_link, 110461ba64fcSAl Viro .readlink = generic_readlink 110561ba64fcSAl Viro }; 110661ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations); 1107fbabfd0fSEric W. Biederman 1108fbabfd0fSEric W. Biederman /* 1109fbabfd0fSEric W. Biederman * Operations for a permanently empty directory. 1110fbabfd0fSEric W. Biederman */ 1111fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1112fbabfd0fSEric W. Biederman { 1113fbabfd0fSEric W. Biederman return ERR_PTR(-ENOENT); 1114fbabfd0fSEric W. Biederman } 1115fbabfd0fSEric W. Biederman 1116fbabfd0fSEric W. Biederman static int empty_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, 1117fbabfd0fSEric W. Biederman struct kstat *stat) 1118fbabfd0fSEric W. Biederman { 1119fbabfd0fSEric W. Biederman struct inode *inode = d_inode(dentry); 1120fbabfd0fSEric W. Biederman generic_fillattr(inode, stat); 1121fbabfd0fSEric W. Biederman return 0; 1122fbabfd0fSEric W. Biederman } 1123fbabfd0fSEric W. Biederman 1124fbabfd0fSEric W. Biederman static int empty_dir_setattr(struct dentry *dentry, struct iattr *attr) 1125fbabfd0fSEric W. Biederman { 1126fbabfd0fSEric W. Biederman return -EPERM; 1127fbabfd0fSEric W. Biederman } 1128fbabfd0fSEric W. Biederman 1129fbabfd0fSEric W. Biederman static int empty_dir_setxattr(struct dentry *dentry, const char *name, 1130fbabfd0fSEric W. Biederman const void *value, size_t size, int flags) 1131fbabfd0fSEric W. Biederman { 1132fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1133fbabfd0fSEric W. Biederman } 1134fbabfd0fSEric W. Biederman 1135fbabfd0fSEric W. Biederman static ssize_t empty_dir_getxattr(struct dentry *dentry, const char *name, 1136fbabfd0fSEric W. Biederman void *value, size_t size) 1137fbabfd0fSEric W. Biederman { 1138fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1139fbabfd0fSEric W. Biederman } 1140fbabfd0fSEric W. Biederman 1141fbabfd0fSEric W. Biederman static int empty_dir_removexattr(struct dentry *dentry, const char *name) 1142fbabfd0fSEric W. Biederman { 1143fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1144fbabfd0fSEric W. Biederman } 1145fbabfd0fSEric W. Biederman 1146fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size) 1147fbabfd0fSEric W. Biederman { 1148fbabfd0fSEric W. Biederman return -EOPNOTSUPP; 1149fbabfd0fSEric W. Biederman } 1150fbabfd0fSEric W. Biederman 1151fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = { 1152fbabfd0fSEric W. Biederman .lookup = empty_dir_lookup, 1153fbabfd0fSEric W. Biederman .permission = generic_permission, 1154fbabfd0fSEric W. Biederman .setattr = empty_dir_setattr, 1155fbabfd0fSEric W. Biederman .getattr = empty_dir_getattr, 1156fbabfd0fSEric W. Biederman .setxattr = empty_dir_setxattr, 1157fbabfd0fSEric W. Biederman .getxattr = empty_dir_getxattr, 1158fbabfd0fSEric W. Biederman .removexattr = empty_dir_removexattr, 1159fbabfd0fSEric W. Biederman .listxattr = empty_dir_listxattr, 1160fbabfd0fSEric W. Biederman }; 1161fbabfd0fSEric W. Biederman 1162fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence) 1163fbabfd0fSEric W. Biederman { 1164fbabfd0fSEric W. Biederman /* An empty directory has two entries . and .. at offsets 0 and 1 */ 1165fbabfd0fSEric W. Biederman return generic_file_llseek_size(file, offset, whence, 2, 2); 1166fbabfd0fSEric W. Biederman } 1167fbabfd0fSEric W. Biederman 1168fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx) 1169fbabfd0fSEric W. Biederman { 1170fbabfd0fSEric W. Biederman dir_emit_dots(file, ctx); 1171fbabfd0fSEric W. Biederman return 0; 1172fbabfd0fSEric W. Biederman } 1173fbabfd0fSEric W. Biederman 1174fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = { 1175fbabfd0fSEric W. Biederman .llseek = empty_dir_llseek, 1176fbabfd0fSEric W. Biederman .read = generic_read_dir, 1177fbabfd0fSEric W. Biederman .iterate = empty_dir_readdir, 1178fbabfd0fSEric W. Biederman .fsync = noop_fsync, 1179fbabfd0fSEric W. Biederman }; 1180fbabfd0fSEric W. Biederman 1181fbabfd0fSEric W. Biederman 1182fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode) 1183fbabfd0fSEric W. Biederman { 1184fbabfd0fSEric W. Biederman set_nlink(inode, 2); 1185fbabfd0fSEric W. Biederman inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; 1186fbabfd0fSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1187fbabfd0fSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1188fbabfd0fSEric W. Biederman inode->i_rdev = 0; 11894b75de86SEric W. Biederman inode->i_size = 0; 1190fbabfd0fSEric W. Biederman inode->i_blkbits = PAGE_SHIFT; 1191fbabfd0fSEric W. Biederman inode->i_blocks = 0; 1192fbabfd0fSEric W. Biederman 1193fbabfd0fSEric W. Biederman inode->i_op = &empty_dir_inode_operations; 1194fbabfd0fSEric W. Biederman inode->i_fop = &empty_dir_operations; 1195fbabfd0fSEric W. Biederman } 1196fbabfd0fSEric W. Biederman 1197fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode) 1198fbabfd0fSEric W. Biederman { 1199fbabfd0fSEric W. Biederman return (inode->i_fop == &empty_dir_operations) && 1200fbabfd0fSEric W. Biederman (inode->i_op == &empty_dir_inode_operations); 1201fbabfd0fSEric W. Biederman } 1202