Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/libfs.c |
| 3 | * Library for filesystems writers. |
| 4 | */ |
| 5 | |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 6 | #include <linux/blkdev.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 7 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/pagemap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mount.h> |
| 11 | #include <linux/vfs.h> |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 12 | #include <linux/quotaops.h> |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 13 | #include <linux/mutex.h> |
Al Viro | 87dc800 | 2013-09-16 10:30:04 -0400 | [diff] [blame] | 14 | #include <linux/namei.h> |
Christoph Hellwig | 2596110 | 2007-10-21 16:42:05 -0700 | [diff] [blame] | 15 | #include <linux/exportfs.h> |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 16 | #include <linux/writeback.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 17 | #include <linux/buffer_head.h> /* sync_mapping_buffers */ |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> |
| 20 | |
Al Viro | a4464db | 2011-07-07 15:03:58 -0400 | [diff] [blame] | 21 | #include "internal.h" |
| 22 | |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 23 | static inline int simple_positive(struct dentry *dentry) |
| 24 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 25 | return d_really_is_positive(dentry) && !d_unhashed(dentry); |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 26 | } |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | int simple_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 29 | struct kstat *stat) |
| 30 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 31 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | generic_fillattr(inode, stat); |
| 33 | stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9); |
| 34 | return 0; |
| 35 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 36 | EXPORT_SYMBOL(simple_getattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 38 | int simple_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 40 | buf->f_type = dentry->d_sb->s_magic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | buf->f_bsize = PAGE_CACHE_SIZE; |
| 42 | buf->f_namelen = NAME_MAX; |
| 43 | return 0; |
| 44 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 45 | EXPORT_SYMBOL(simple_statfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Retaining negative dentries for an in-memory filesystem just wastes |
| 49 | * memory and lookup time: arrange for them to be deleted immediately. |
| 50 | */ |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 51 | int always_delete_dentry(const struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | return 1; |
| 54 | } |
Al Viro | b26d4cd | 2013-10-25 18:47:37 -0400 | [diff] [blame] | 55 | EXPORT_SYMBOL(always_delete_dentry); |
| 56 | |
| 57 | const struct dentry_operations simple_dentry_operations = { |
| 58 | .d_delete = always_delete_dentry, |
| 59 | }; |
| 60 | EXPORT_SYMBOL(simple_dentry_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Lookup the data. This is trivial - if the dentry didn't already |
| 64 | * exist, we know it is negative. Set d_op to delete negative dentries. |
| 65 | */ |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 66 | struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (dentry->d_name.len > NAME_MAX) |
| 69 | return ERR_PTR(-ENAMETOOLONG); |
Al Viro | 74931da | 2013-07-14 17:43:25 +0400 | [diff] [blame] | 70 | if (!dentry->d_sb->s_d_op) |
| 71 | d_set_d_op(dentry, &simple_dentry_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | d_add(dentry, NULL); |
| 73 | return NULL; |
| 74 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 75 | EXPORT_SYMBOL(simple_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | int dcache_dir_open(struct inode *inode, struct file *file) |
| 78 | { |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 79 | static struct qstr cursor_name = QSTR_INIT(".", 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 81 | file->private_data = d_alloc(file->f_path.dentry, &cursor_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | return file->private_data ? 0 : -ENOMEM; |
| 84 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 85 | EXPORT_SYMBOL(dcache_dir_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | int dcache_dir_close(struct inode *inode, struct file *file) |
| 88 | { |
| 89 | dput(file->private_data); |
| 90 | return 0; |
| 91 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 92 | EXPORT_SYMBOL(dcache_dir_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 94 | loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 96 | struct dentry *dentry = file->f_path.dentry; |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 97 | mutex_lock(&d_inode(dentry)->i_mutex); |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 98 | switch (whence) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | case 1: |
| 100 | offset += file->f_pos; |
| 101 | case 0: |
| 102 | if (offset >= 0) |
| 103 | break; |
| 104 | default: |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 105 | mutex_unlock(&d_inode(dentry)->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EINVAL; |
| 107 | } |
| 108 | if (offset != file->f_pos) { |
| 109 | file->f_pos = offset; |
| 110 | if (file->f_pos >= 2) { |
| 111 | struct list_head *p; |
| 112 | struct dentry *cursor = file->private_data; |
| 113 | loff_t n = file->f_pos - 2; |
| 114 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 115 | spin_lock(&dentry->d_lock); |
| 116 | /* d_lock not required for cursor */ |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 117 | list_del(&cursor->d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 118 | p = dentry->d_subdirs.next; |
| 119 | while (n && p != &dentry->d_subdirs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct dentry *next; |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 121 | next = list_entry(p, struct dentry, d_child); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 122 | spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 123 | if (simple_positive(next)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | n--; |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 125 | spin_unlock(&next->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | p = p->next; |
| 127 | } |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 128 | list_add_tail(&cursor->d_child, p); |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 129 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 132 | mutex_unlock(&d_inode(dentry)->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return offset; |
| 134 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 135 | EXPORT_SYMBOL(dcache_dir_lseek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* Relationship between i_mode and the DT_xxx types */ |
| 138 | static inline unsigned char dt_type(struct inode *inode) |
| 139 | { |
| 140 | return (inode->i_mode >> 12) & 15; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Directory is locked and all positive dentries in it are safe, since |
| 145 | * for ramfs-type trees they can't go away without unlink() or rmdir(), |
| 146 | * both impossible due to the lock on directory. |
| 147 | */ |
| 148 | |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 149 | int dcache_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 151 | struct dentry *dentry = file->f_path.dentry; |
| 152 | struct dentry *cursor = file->private_data; |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 153 | struct list_head *p, *q = &cursor->d_child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 155 | if (!dir_emit_dots(file, ctx)) |
| 156 | return 0; |
| 157 | spin_lock(&dentry->d_lock); |
| 158 | if (ctx->pos == 2) |
| 159 | list_move(q, &dentry->d_subdirs); |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 160 | |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 161 | for (p = q->next; p != &dentry->d_subdirs; p = p->next) { |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 162 | struct dentry *next = list_entry(p, struct dentry, d_child); |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 163 | spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); |
| 164 | if (!simple_positive(next)) { |
| 165 | spin_unlock(&next->d_lock); |
| 166 | continue; |
| 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 169 | spin_unlock(&next->d_lock); |
| 170 | spin_unlock(&dentry->d_lock); |
| 171 | if (!dir_emit(ctx, next->d_name.name, next->d_name.len, |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 172 | d_inode(next)->i_ino, dt_type(d_inode(next)))) |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 173 | return 0; |
| 174 | spin_lock(&dentry->d_lock); |
| 175 | spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED); |
| 176 | /* next is still alive */ |
| 177 | list_move(q, p); |
| 178 | spin_unlock(&next->d_lock); |
| 179 | p = q; |
| 180 | ctx->pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 182 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 185 | EXPORT_SYMBOL(dcache_readdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos) |
| 188 | { |
| 189 | return -EISDIR; |
| 190 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 191 | EXPORT_SYMBOL(generic_read_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 193 | const struct file_operations simple_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | .open = dcache_dir_open, |
| 195 | .release = dcache_dir_close, |
| 196 | .llseek = dcache_dir_lseek, |
| 197 | .read = generic_read_dir, |
Al Viro | 5f99f4e | 2013-05-15 20:23:06 -0400 | [diff] [blame] | 198 | .iterate = dcache_readdir, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 199 | .fsync = noop_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | }; |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 201 | EXPORT_SYMBOL(simple_dir_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 203 | const struct inode_operations simple_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | .lookup = simple_lookup, |
| 205 | }; |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 206 | EXPORT_SYMBOL(simple_dir_inode_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 208 | static const struct super_operations simple_super_operations = { |
| 209 | .statfs = simple_statfs, |
| 210 | }; |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* |
| 213 | * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that |
| 214 | * will never be mountable) |
| 215 | */ |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 216 | struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name, |
Al Viro | c74a1cb | 2011-01-12 16:59:34 -0500 | [diff] [blame] | 217 | const struct super_operations *ops, |
| 218 | const struct dentry_operations *dops, unsigned long magic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 220 | struct super_block *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | struct dentry *dentry; |
| 222 | struct inode *root; |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 223 | struct qstr d_name = QSTR_INIT(name, strlen(name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
David Howells | 9249e17 | 2012-06-25 12:55:37 +0100 | [diff] [blame] | 225 | s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (IS_ERR(s)) |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 227 | return ERR_CAST(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Jeff Layton | 89a4eb4 | 2009-08-18 14:11:08 -0700 | [diff] [blame] | 229 | s->s_maxbytes = MAX_LFS_FILESIZE; |
Alex Nixon | 3971e1a | 2008-07-29 22:33:03 -0700 | [diff] [blame] | 230 | s->s_blocksize = PAGE_SIZE; |
| 231 | s->s_blocksize_bits = PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | s->s_magic = magic; |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 233 | s->s_op = ops ? ops : &simple_super_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | s->s_time_gran = 1; |
| 235 | root = new_inode(s); |
| 236 | if (!root) |
| 237 | goto Enomem; |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 238 | /* |
| 239 | * since this is the first inode, make it number 1. New inodes created |
| 240 | * after this must take care not to collide with it (by passing |
| 241 | * max_reserved of 1 to iunique). |
| 242 | */ |
| 243 | root->i_ino = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME; |
Al Viro | a4464db | 2011-07-07 15:03:58 -0400 | [diff] [blame] | 246 | dentry = __d_alloc(s, &d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (!dentry) { |
| 248 | iput(root); |
| 249 | goto Enomem; |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | d_instantiate(dentry, root); |
| 252 | s->s_root = dentry; |
Al Viro | c74a1cb | 2011-01-12 16:59:34 -0500 | [diff] [blame] | 253 | s->s_d_op = dops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | s->s_flags |= MS_ACTIVE; |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 255 | return dget(s->s_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | Enomem: |
Al Viro | 6f5bbff | 2009-05-06 01:34:22 -0400 | [diff] [blame] | 258 | deactivate_locked_super(s); |
Al Viro | 51139ad | 2010-07-25 23:47:46 +0400 | [diff] [blame] | 259 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 261 | EXPORT_SYMBOL(mount_pseudo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Stephen Boyd | 20955e8 | 2012-04-05 14:25:09 -0700 | [diff] [blame] | 263 | int simple_open(struct inode *inode, struct file *file) |
| 264 | { |
| 265 | if (inode->i_private) |
| 266 | file->private_data = inode->i_private; |
| 267 | return 0; |
| 268 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 269 | EXPORT_SYMBOL(simple_open); |
Stephen Boyd | 20955e8 | 2012-04-05 14:25:09 -0700 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 272 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 273 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 276 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 277 | ihold(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | dget(dentry); |
| 279 | d_instantiate(dentry, inode); |
| 280 | return 0; |
| 281 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 282 | EXPORT_SYMBOL(simple_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int simple_empty(struct dentry *dentry) |
| 285 | { |
| 286 | struct dentry *child; |
| 287 | int ret = 0; |
| 288 | |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 289 | spin_lock(&dentry->d_lock); |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 290 | list_for_each_entry(child, &dentry->d_subdirs, d_child) { |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 291 | spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); |
| 292 | if (simple_positive(child)) { |
| 293 | spin_unlock(&child->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | goto out; |
Nick Piggin | da50295 | 2011-01-07 17:49:33 +1100 | [diff] [blame] | 295 | } |
| 296 | spin_unlock(&child->d_lock); |
| 297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | ret = 1; |
| 299 | out: |
Nick Piggin | 2fd6b7f | 2011-01-07 17:49:34 +1100 | [diff] [blame] | 300 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return ret; |
| 302 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 303 | EXPORT_SYMBOL(simple_empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | int simple_unlink(struct inode *dir, struct dentry *dentry) |
| 306 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 307 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 310 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | dput(dentry); |
| 312 | return 0; |
| 313 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 314 | EXPORT_SYMBOL(simple_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | int simple_rmdir(struct inode *dir, struct dentry *dentry) |
| 317 | { |
| 318 | if (!simple_empty(dentry)) |
| 319 | return -ENOTEMPTY; |
| 320 | |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 321 | drop_nlink(d_inode(dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | simple_unlink(dir, dentry); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 323 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return 0; |
| 325 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 326 | EXPORT_SYMBOL(simple_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | int simple_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 329 | struct inode *new_dir, struct dentry *new_dentry) |
| 330 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 331 | struct inode *inode = d_inode(old_dentry); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 332 | int they_are_dirs = d_is_dir(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | if (!simple_empty(new_dentry)) |
| 335 | return -ENOTEMPTY; |
| 336 | |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 337 | if (d_really_is_positive(new_dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | simple_unlink(new_dir, new_dentry); |
Al Viro | 841590c | 2011-07-21 15:49:09 -0400 | [diff] [blame] | 339 | if (they_are_dirs) { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 340 | drop_nlink(d_inode(new_dentry)); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 341 | drop_nlink(old_dir); |
Al Viro | 841590c | 2011-07-21 15:49:09 -0400 | [diff] [blame] | 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } else if (they_are_dirs) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 344 | drop_nlink(old_dir); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 345 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime = |
| 349 | new_dir->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 350 | |
| 351 | return 0; |
| 352 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 353 | EXPORT_SYMBOL(simple_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 355 | /** |
Christoph Hellwig | eef2380 | 2010-06-04 11:30:01 +0200 | [diff] [blame] | 356 | * simple_setattr - setattr for simple filesystem |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 357 | * @dentry: dentry |
| 358 | * @iattr: iattr structure |
| 359 | * |
| 360 | * Returns 0 on success, -error on failure. |
| 361 | * |
Christoph Hellwig | eef2380 | 2010-06-04 11:30:01 +0200 | [diff] [blame] | 362 | * simple_setattr is a simple ->setattr implementation without a proper |
| 363 | * implementation of size changes. |
| 364 | * |
| 365 | * It can either be used for in-memory filesystems or special files |
| 366 | * on simple regular filesystems. Anything that needs to change on-disk |
| 367 | * or wire state on size changes needs its own setattr method. |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 368 | */ |
| 369 | int simple_setattr(struct dentry *dentry, struct iattr *iattr) |
| 370 | { |
David Howells | dea655c | 2015-03-17 22:26:15 +0000 | [diff] [blame] | 371 | struct inode *inode = d_inode(dentry); |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 372 | int error; |
| 373 | |
| 374 | error = inode_change_ok(inode, iattr); |
| 375 | if (error) |
| 376 | return error; |
| 377 | |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 378 | if (iattr->ia_valid & ATTR_SIZE) |
| 379 | truncate_setsize(inode, iattr->ia_size); |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 380 | setattr_copy(inode, iattr); |
Christoph Hellwig | eef2380 | 2010-06-04 11:30:01 +0200 | [diff] [blame] | 381 | mark_inode_dirty(inode); |
| 382 | return 0; |
npiggin@suse.de | 7bb46a6 | 2010-05-27 01:05:33 +1000 | [diff] [blame] | 383 | } |
| 384 | EXPORT_SYMBOL(simple_setattr); |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | int simple_readpage(struct file *file, struct page *page) |
| 387 | { |
Pekka J Enberg | c0d92cb | 2006-09-29 01:59:09 -0700 | [diff] [blame] | 388 | clear_highpage(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | flush_dcache_page(page); |
| 390 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | unlock_page(page); |
| 392 | return 0; |
| 393 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 394 | EXPORT_SYMBOL(simple_readpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 396 | int simple_write_begin(struct file *file, struct address_space *mapping, |
| 397 | loff_t pos, unsigned len, unsigned flags, |
| 398 | struct page **pagep, void **fsdata) |
| 399 | { |
| 400 | struct page *page; |
| 401 | pgoff_t index; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 402 | |
| 403 | index = pos >> PAGE_CACHE_SHIFT; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 404 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 405 | page = grab_cache_page_write_begin(mapping, index, flags); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 406 | if (!page) |
| 407 | return -ENOMEM; |
| 408 | |
| 409 | *pagep = page; |
| 410 | |
Boaz Harrosh | 193cf4b | 2010-01-12 16:18:08 +0200 | [diff] [blame] | 411 | if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) { |
| 412 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
| 413 | |
| 414 | zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE); |
| 415 | } |
| 416 | return 0; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 417 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 418 | EXPORT_SYMBOL(simple_write_begin); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 419 | |
Boaz Harrosh | ad2a722f | 2010-01-12 15:13:47 +0200 | [diff] [blame] | 420 | /** |
| 421 | * simple_write_end - .write_end helper for non-block-device FSes |
| 422 | * @available: See .write_end of address_space_operations |
| 423 | * @file: " |
| 424 | * @mapping: " |
| 425 | * @pos: " |
| 426 | * @len: " |
| 427 | * @copied: " |
| 428 | * @page: " |
| 429 | * @fsdata: " |
| 430 | * |
| 431 | * simple_write_end does the minimum needed for updating a page after writing is |
| 432 | * done. It has the same API signature as the .write_end of |
| 433 | * address_space_operations vector. So it can just be set onto .write_end for |
| 434 | * FSes that don't need any other processing. i_mutex is assumed to be held. |
| 435 | * Block based filesystems should use generic_write_end(). |
| 436 | * NOTE: Even though i_size might get updated by this function, mark_inode_dirty |
| 437 | * is not called, so a filesystem that actually does store data in .write_inode |
| 438 | * should extend on what's done here with a call to mark_inode_dirty() in the |
| 439 | * case that i_size has changed. |
| 440 | */ |
| 441 | int simple_write_end(struct file *file, struct address_space *mapping, |
| 442 | loff_t pos, unsigned len, unsigned copied, |
| 443 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | struct inode *inode = page->mapping->host; |
Boaz Harrosh | ad2a722f | 2010-01-12 15:13:47 +0200 | [diff] [blame] | 446 | loff_t last_pos = pos + copied; |
| 447 | |
| 448 | /* zero the stale part of the page if we did a short copy */ |
| 449 | if (copied < len) { |
| 450 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
| 451 | |
| 452 | zero_user(page, from + copied, len - copied); |
| 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Nick Piggin | 955eff5 | 2007-02-20 13:58:08 -0800 | [diff] [blame] | 455 | if (!PageUptodate(page)) |
| 456 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | /* |
| 458 | * No need to use i_size_read() here, the i_size |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 459 | * cannot change under us because we hold the i_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | */ |
Boaz Harrosh | ad2a722f | 2010-01-12 15:13:47 +0200 | [diff] [blame] | 461 | if (last_pos > inode->i_size) |
| 462 | i_size_write(inode, last_pos); |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | set_page_dirty(page); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 465 | unlock_page(page); |
| 466 | page_cache_release(page); |
| 467 | |
| 468 | return copied; |
| 469 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 470 | EXPORT_SYMBOL(simple_write_end); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 471 | |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 472 | /* |
| 473 | * the inodes created here are not hashed. If you use iunique to generate |
| 474 | * unique inode values later for this filesystem, then you must take care |
| 475 | * to pass it an appropriate max_reserved value to avoid collisions. |
| 476 | */ |
Roberto Sassu | 7d683a0 | 2010-06-03 11:58:28 +0200 | [diff] [blame] | 477 | int simple_fill_super(struct super_block *s, unsigned long magic, |
| 478 | struct tree_descr *files) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | struct inode *inode; |
| 481 | struct dentry *root; |
| 482 | struct dentry *dentry; |
| 483 | int i; |
| 484 | |
| 485 | s->s_blocksize = PAGE_CACHE_SIZE; |
| 486 | s->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 487 | s->s_magic = magic; |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 488 | s->s_op = &simple_super_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | s->s_time_gran = 1; |
| 490 | |
| 491 | inode = new_inode(s); |
| 492 | if (!inode) |
| 493 | return -ENOMEM; |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 494 | /* |
| 495 | * because the root inode is 1, the files array must not contain an |
| 496 | * entry at index 1 |
| 497 | */ |
| 498 | inode->i_ino = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | inode->i_mode = S_IFDIR | 0755; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 501 | inode->i_op = &simple_dir_inode_operations; |
| 502 | inode->i_fop = &simple_dir_operations; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 503 | set_nlink(inode, 2); |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 504 | root = d_make_root(inode); |
| 505 | if (!root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | for (i = 0; !files->name || files->name[0]; i++, files++) { |
| 508 | if (!files->name) |
| 509 | continue; |
Jeff Layton | 1a1c9bb | 2007-05-08 00:32:31 -0700 | [diff] [blame] | 510 | |
| 511 | /* warn if it tries to conflict with the root inode */ |
| 512 | if (unlikely(i == 1)) |
| 513 | printk(KERN_WARNING "%s: %s passed in a files array" |
| 514 | "with an index of 1!\n", __func__, |
| 515 | s->s_type->name); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | dentry = d_alloc_name(root, files->name); |
| 518 | if (!dentry) |
| 519 | goto out; |
| 520 | inode = new_inode(s); |
Konstantin Khlebnikov | 32096ea | 2011-11-01 16:12:33 +0300 | [diff] [blame] | 521 | if (!inode) { |
| 522 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | goto out; |
Konstantin Khlebnikov | 32096ea | 2011-11-01 16:12:33 +0300 | [diff] [blame] | 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | inode->i_mode = S_IFREG | files->mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 527 | inode->i_fop = files->ops; |
| 528 | inode->i_ino = i; |
| 529 | d_add(dentry, inode); |
| 530 | } |
| 531 | s->s_root = root; |
| 532 | return 0; |
| 533 | out: |
| 534 | d_genocide(root); |
Al Viro | 640946f | 2012-04-02 19:22:25 -0400 | [diff] [blame] | 535 | shrink_dcache_parent(root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | dput(root); |
| 537 | return -ENOMEM; |
| 538 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 539 | EXPORT_SYMBOL(simple_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | static DEFINE_SPINLOCK(pin_fs_lock); |
| 542 | |
Trond Myklebust | 1f5ce9e | 2006-06-09 09:34:16 -0400 | [diff] [blame] | 543 | int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
| 545 | struct vfsmount *mnt = NULL; |
| 546 | spin_lock(&pin_fs_lock); |
| 547 | if (unlikely(!*mount)) { |
| 548 | spin_unlock(&pin_fs_lock); |
Al Viro | 2452992 | 2012-03-17 02:13:52 -0400 | [diff] [blame] | 549 | mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | if (IS_ERR(mnt)) |
| 551 | return PTR_ERR(mnt); |
| 552 | spin_lock(&pin_fs_lock); |
| 553 | if (!*mount) |
| 554 | *mount = mnt; |
| 555 | } |
| 556 | mntget(*mount); |
| 557 | ++*count; |
| 558 | spin_unlock(&pin_fs_lock); |
| 559 | mntput(mnt); |
| 560 | return 0; |
| 561 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 562 | EXPORT_SYMBOL(simple_pin_fs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | void simple_release_fs(struct vfsmount **mount, int *count) |
| 565 | { |
| 566 | struct vfsmount *mnt; |
| 567 | spin_lock(&pin_fs_lock); |
| 568 | mnt = *mount; |
| 569 | if (!--*count) |
| 570 | *mount = NULL; |
| 571 | spin_unlock(&pin_fs_lock); |
| 572 | mntput(mnt); |
| 573 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 574 | EXPORT_SYMBOL(simple_release_fs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Akinobu Mita | 6d1029b | 2008-07-04 09:59:51 -0700 | [diff] [blame] | 576 | /** |
| 577 | * simple_read_from_buffer - copy data from the buffer to user space |
| 578 | * @to: the user space buffer to read to |
| 579 | * @count: the maximum number of bytes to read |
| 580 | * @ppos: the current position in the buffer |
| 581 | * @from: the buffer to read from |
| 582 | * @available: the size of the buffer |
| 583 | * |
| 584 | * The simple_read_from_buffer() function reads up to @count bytes from the |
| 585 | * buffer @from at offset @ppos into the user space address starting at @to. |
| 586 | * |
| 587 | * On success, the number of bytes read is returned and the offset @ppos is |
| 588 | * advanced by this number, or negative value is returned on error. |
| 589 | **/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos, |
| 591 | const void *from, size_t available) |
| 592 | { |
| 593 | loff_t pos = *ppos; |
Steven Rostedt | 14be274 | 2009-09-18 13:05:42 -0700 | [diff] [blame] | 594 | size_t ret; |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | if (pos < 0) |
| 597 | return -EINVAL; |
Steven Rostedt | 14be274 | 2009-09-18 13:05:42 -0700 | [diff] [blame] | 598 | if (pos >= available || !count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | return 0; |
| 600 | if (count > available - pos) |
| 601 | count = available - pos; |
Steven Rostedt | 14be274 | 2009-09-18 13:05:42 -0700 | [diff] [blame] | 602 | ret = copy_to_user(to, from + pos, count); |
| 603 | if (ret == count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return -EFAULT; |
Steven Rostedt | 14be274 | 2009-09-18 13:05:42 -0700 | [diff] [blame] | 605 | count -= ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | *ppos = pos + count; |
| 607 | return count; |
| 608 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 609 | EXPORT_SYMBOL(simple_read_from_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Akinobu Mita | 6d1029b | 2008-07-04 09:59:51 -0700 | [diff] [blame] | 611 | /** |
Jiri Slaby | 6a727b4 | 2010-05-01 23:51:22 +0200 | [diff] [blame] | 612 | * simple_write_to_buffer - copy data from user space to the buffer |
| 613 | * @to: the buffer to write to |
| 614 | * @available: the size of the buffer |
| 615 | * @ppos: the current position in the buffer |
| 616 | * @from: the user space buffer to read from |
| 617 | * @count: the maximum number of bytes to read |
| 618 | * |
| 619 | * The simple_write_to_buffer() function reads up to @count bytes from the user |
| 620 | * space address starting at @from into the buffer @to at offset @ppos. |
| 621 | * |
| 622 | * On success, the number of bytes written is returned and the offset @ppos is |
| 623 | * advanced by this number, or negative value is returned on error. |
| 624 | **/ |
| 625 | ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, |
| 626 | const void __user *from, size_t count) |
| 627 | { |
| 628 | loff_t pos = *ppos; |
| 629 | size_t res; |
| 630 | |
| 631 | if (pos < 0) |
| 632 | return -EINVAL; |
| 633 | if (pos >= available || !count) |
| 634 | return 0; |
| 635 | if (count > available - pos) |
| 636 | count = available - pos; |
| 637 | res = copy_from_user(to + pos, from, count); |
| 638 | if (res == count) |
| 639 | return -EFAULT; |
| 640 | count -= res; |
| 641 | *ppos = pos + count; |
| 642 | return count; |
| 643 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 644 | EXPORT_SYMBOL(simple_write_to_buffer); |
Jiri Slaby | 6a727b4 | 2010-05-01 23:51:22 +0200 | [diff] [blame] | 645 | |
| 646 | /** |
Akinobu Mita | 6d1029b | 2008-07-04 09:59:51 -0700 | [diff] [blame] | 647 | * memory_read_from_buffer - copy data from the buffer |
| 648 | * @to: the kernel space buffer to read to |
| 649 | * @count: the maximum number of bytes to read |
| 650 | * @ppos: the current position in the buffer |
| 651 | * @from: the buffer to read from |
| 652 | * @available: the size of the buffer |
| 653 | * |
| 654 | * The memory_read_from_buffer() function reads up to @count bytes from the |
| 655 | * buffer @from at offset @ppos into the kernel space address starting at @to. |
| 656 | * |
| 657 | * On success, the number of bytes read is returned and the offset @ppos is |
| 658 | * advanced by this number, or negative value is returned on error. |
| 659 | **/ |
Akinobu Mita | 93b0711 | 2008-06-05 22:46:21 -0700 | [diff] [blame] | 660 | ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos, |
| 661 | const void *from, size_t available) |
| 662 | { |
| 663 | loff_t pos = *ppos; |
| 664 | |
| 665 | if (pos < 0) |
| 666 | return -EINVAL; |
| 667 | if (pos >= available) |
| 668 | return 0; |
| 669 | if (count > available - pos) |
| 670 | count = available - pos; |
| 671 | memcpy(to, from + pos, count); |
| 672 | *ppos = pos + count; |
| 673 | |
| 674 | return count; |
| 675 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 676 | EXPORT_SYMBOL(memory_read_from_buffer); |
Akinobu Mita | 93b0711 | 2008-06-05 22:46:21 -0700 | [diff] [blame] | 677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | /* |
| 679 | * Transaction based IO. |
| 680 | * The file expects a single write which triggers the transaction, and then |
| 681 | * possibly a read which collects the result - which is stored in a |
| 682 | * file-local buffer. |
| 683 | */ |
Ingo Molnar | 76791ab | 2009-03-25 16:48:35 +0100 | [diff] [blame] | 684 | |
| 685 | void simple_transaction_set(struct file *file, size_t n) |
| 686 | { |
| 687 | struct simple_transaction_argresp *ar = file->private_data; |
| 688 | |
| 689 | BUG_ON(n > SIMPLE_TRANSACTION_LIMIT); |
| 690 | |
| 691 | /* |
| 692 | * The barrier ensures that ar->size will really remain zero until |
| 693 | * ar->data is ready for reading. |
| 694 | */ |
| 695 | smp_mb(); |
| 696 | ar->size = n; |
| 697 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 698 | EXPORT_SYMBOL(simple_transaction_set); |
Ingo Molnar | 76791ab | 2009-03-25 16:48:35 +0100 | [diff] [blame] | 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | char *simple_transaction_get(struct file *file, const char __user *buf, size_t size) |
| 701 | { |
| 702 | struct simple_transaction_argresp *ar; |
| 703 | static DEFINE_SPINLOCK(simple_transaction_lock); |
| 704 | |
| 705 | if (size > SIMPLE_TRANSACTION_LIMIT - 1) |
| 706 | return ERR_PTR(-EFBIG); |
| 707 | |
| 708 | ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL); |
| 709 | if (!ar) |
| 710 | return ERR_PTR(-ENOMEM); |
| 711 | |
| 712 | spin_lock(&simple_transaction_lock); |
| 713 | |
| 714 | /* only one write allowed per open */ |
| 715 | if (file->private_data) { |
| 716 | spin_unlock(&simple_transaction_lock); |
| 717 | free_page((unsigned long)ar); |
| 718 | return ERR_PTR(-EBUSY); |
| 719 | } |
| 720 | |
| 721 | file->private_data = ar; |
| 722 | |
| 723 | spin_unlock(&simple_transaction_lock); |
| 724 | |
| 725 | if (copy_from_user(ar->data, buf, size)) |
| 726 | return ERR_PTR(-EFAULT); |
| 727 | |
| 728 | return ar->data; |
| 729 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 730 | EXPORT_SYMBOL(simple_transaction_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) |
| 733 | { |
| 734 | struct simple_transaction_argresp *ar = file->private_data; |
| 735 | |
| 736 | if (!ar) |
| 737 | return 0; |
| 738 | return simple_read_from_buffer(buf, size, pos, ar->data, ar->size); |
| 739 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 740 | EXPORT_SYMBOL(simple_transaction_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | int simple_transaction_release(struct inode *inode, struct file *file) |
| 743 | { |
| 744 | free_page((unsigned long)file->private_data); |
| 745 | return 0; |
| 746 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 747 | EXPORT_SYMBOL(simple_transaction_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 749 | /* Simple attribute files */ |
| 750 | |
| 751 | struct simple_attr { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 752 | int (*get)(void *, u64 *); |
| 753 | int (*set)(void *, u64); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 754 | char get_buf[24]; /* enough to store a u64 and "\n\0" */ |
| 755 | char set_buf[24]; |
| 756 | void *data; |
| 757 | const char *fmt; /* format for read operation */ |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 758 | struct mutex mutex; /* protects access to these buffers */ |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | /* simple_attr_open is called by an actual attribute open file operation |
| 762 | * to set the attribute specific access operations. */ |
| 763 | int simple_attr_open(struct inode *inode, struct file *file, |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 764 | int (*get)(void *, u64 *), int (*set)(void *, u64), |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 765 | const char *fmt) |
| 766 | { |
| 767 | struct simple_attr *attr; |
| 768 | |
| 769 | attr = kmalloc(sizeof(*attr), GFP_KERNEL); |
| 770 | if (!attr) |
| 771 | return -ENOMEM; |
| 772 | |
| 773 | attr->get = get; |
| 774 | attr->set = set; |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 775 | attr->data = inode->i_private; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 776 | attr->fmt = fmt; |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 777 | mutex_init(&attr->mutex); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 778 | |
| 779 | file->private_data = attr; |
| 780 | |
| 781 | return nonseekable_open(inode, file); |
| 782 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 783 | EXPORT_SYMBOL_GPL(simple_attr_open); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 784 | |
Christoph Hellwig | 74bedc4 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 785 | int simple_attr_release(struct inode *inode, struct file *file) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 786 | { |
| 787 | kfree(file->private_data); |
| 788 | return 0; |
| 789 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 790 | EXPORT_SYMBOL_GPL(simple_attr_release); /* GPL-only? This? Really? */ |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 791 | |
| 792 | /* read from the buffer that is filled with the get function */ |
| 793 | ssize_t simple_attr_read(struct file *file, char __user *buf, |
| 794 | size_t len, loff_t *ppos) |
| 795 | { |
| 796 | struct simple_attr *attr; |
| 797 | size_t size; |
| 798 | ssize_t ret; |
| 799 | |
| 800 | attr = file->private_data; |
| 801 | |
| 802 | if (!attr->get) |
| 803 | return -EACCES; |
| 804 | |
Christoph Hellwig | 9261303 | 2008-02-08 04:20:27 -0800 | [diff] [blame] | 805 | ret = mutex_lock_interruptible(&attr->mutex); |
| 806 | if (ret) |
| 807 | return ret; |
| 808 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 809 | if (*ppos) { /* continued read */ |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 810 | size = strlen(attr->get_buf); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 811 | } else { /* first read */ |
| 812 | u64 val; |
| 813 | ret = attr->get(attr->data, &val); |
| 814 | if (ret) |
| 815 | goto out; |
| 816 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 817 | size = scnprintf(attr->get_buf, sizeof(attr->get_buf), |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 818 | attr->fmt, (unsigned long long)val); |
| 819 | } |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 820 | |
| 821 | ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 822 | out: |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 823 | mutex_unlock(&attr->mutex); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 824 | return ret; |
| 825 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 826 | EXPORT_SYMBOL_GPL(simple_attr_read); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 827 | |
| 828 | /* interpret the buffer as a number to call the set function with */ |
| 829 | ssize_t simple_attr_write(struct file *file, const char __user *buf, |
| 830 | size_t len, loff_t *ppos) |
| 831 | { |
| 832 | struct simple_attr *attr; |
| 833 | u64 val; |
| 834 | size_t size; |
| 835 | ssize_t ret; |
| 836 | |
| 837 | attr = file->private_data; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 838 | if (!attr->set) |
| 839 | return -EACCES; |
| 840 | |
Christoph Hellwig | 9261303 | 2008-02-08 04:20:27 -0800 | [diff] [blame] | 841 | ret = mutex_lock_interruptible(&attr->mutex); |
| 842 | if (ret) |
| 843 | return ret; |
| 844 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 845 | ret = -EFAULT; |
| 846 | size = min(sizeof(attr->set_buf) - 1, len); |
| 847 | if (copy_from_user(attr->set_buf, buf, size)) |
| 848 | goto out; |
| 849 | |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 850 | attr->set_buf[size] = '\0'; |
Akinobu Mita | f7b8863 | 2011-07-19 08:49:25 -0700 | [diff] [blame] | 851 | val = simple_strtoll(attr->set_buf, NULL, 0); |
Wu Fengguang | 05cc0ce | 2009-09-18 13:06:03 -0700 | [diff] [blame] | 852 | ret = attr->set(attr->data, val); |
| 853 | if (ret == 0) |
| 854 | ret = len; /* on success, claim we got the whole input */ |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 855 | out: |
Ingo Molnar | 7cf34c7 | 2006-03-23 03:00:36 -0800 | [diff] [blame] | 856 | mutex_unlock(&attr->mutex); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 857 | return ret; |
| 858 | } |
Al Viro | 12f3887 | 2013-09-15 21:20:49 -0400 | [diff] [blame] | 859 | EXPORT_SYMBOL_GPL(simple_attr_write); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 860 | |
Christoph Hellwig | 2596110 | 2007-10-21 16:42:05 -0700 | [diff] [blame] | 861 | /** |
| 862 | * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation |
| 863 | * @sb: filesystem to do the file handle conversion on |
| 864 | * @fid: file handle to convert |
| 865 | * @fh_len: length of the file handle in bytes |
| 866 | * @fh_type: type of file handle |
| 867 | * @get_inode: filesystem callback to retrieve inode |
| 868 | * |
| 869 | * This function decodes @fid as long as it has one of the well-known |
| 870 | * Linux filehandle types and calls @get_inode on it to retrieve the |
| 871 | * inode for the object specified in the file handle. |
| 872 | */ |
| 873 | struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 874 | int fh_len, int fh_type, struct inode *(*get_inode) |
| 875 | (struct super_block *sb, u64 ino, u32 gen)) |
| 876 | { |
| 877 | struct inode *inode = NULL; |
| 878 | |
| 879 | if (fh_len < 2) |
| 880 | return NULL; |
| 881 | |
| 882 | switch (fh_type) { |
| 883 | case FILEID_INO32_GEN: |
| 884 | case FILEID_INO32_GEN_PARENT: |
| 885 | inode = get_inode(sb, fid->i32.ino, fid->i32.gen); |
| 886 | break; |
| 887 | } |
| 888 | |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 889 | return d_obtain_alias(inode); |
Christoph Hellwig | 2596110 | 2007-10-21 16:42:05 -0700 | [diff] [blame] | 890 | } |
| 891 | EXPORT_SYMBOL_GPL(generic_fh_to_dentry); |
| 892 | |
| 893 | /** |
Yanchuan Nian | ca18683 | 2012-09-05 16:31:29 +0800 | [diff] [blame] | 894 | * generic_fh_to_parent - generic helper for the fh_to_parent export operation |
Christoph Hellwig | 2596110 | 2007-10-21 16:42:05 -0700 | [diff] [blame] | 895 | * @sb: filesystem to do the file handle conversion on |
| 896 | * @fid: file handle to convert |
| 897 | * @fh_len: length of the file handle in bytes |
| 898 | * @fh_type: type of file handle |
| 899 | * @get_inode: filesystem callback to retrieve inode |
| 900 | * |
| 901 | * This function decodes @fid as long as it has one of the well-known |
| 902 | * Linux filehandle types and calls @get_inode on it to retrieve the |
| 903 | * inode for the _parent_ object specified in the file handle if it |
| 904 | * is specified in the file handle, or NULL otherwise. |
| 905 | */ |
| 906 | struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 907 | int fh_len, int fh_type, struct inode *(*get_inode) |
| 908 | (struct super_block *sb, u64 ino, u32 gen)) |
| 909 | { |
| 910 | struct inode *inode = NULL; |
| 911 | |
| 912 | if (fh_len <= 2) |
| 913 | return NULL; |
| 914 | |
| 915 | switch (fh_type) { |
| 916 | case FILEID_INO32_GEN_PARENT: |
| 917 | inode = get_inode(sb, fid->i32.parent_ino, |
| 918 | (fh_len > 3 ? fid->i32.parent_gen : 0)); |
| 919 | break; |
| 920 | } |
| 921 | |
Christoph Hellwig | 4ea3ada | 2008-08-11 15:48:57 +0200 | [diff] [blame] | 922 | return d_obtain_alias(inode); |
Christoph Hellwig | 2596110 | 2007-10-21 16:42:05 -0700 | [diff] [blame] | 923 | } |
| 924 | EXPORT_SYMBOL_GPL(generic_fh_to_parent); |
| 925 | |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 926 | /** |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 927 | * __generic_file_fsync - generic fsync implementation for simple filesystems |
| 928 | * |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 929 | * @file: file to synchronize |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 930 | * @start: start offset in bytes |
| 931 | * @end: end offset in bytes (inclusive) |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 932 | * @datasync: only synchronize essential metadata if true |
| 933 | * |
| 934 | * This is a generic implementation of the fsync method for simple |
| 935 | * filesystems which track all non-inode metadata in the buffers list |
| 936 | * hanging off the address_space structure. |
| 937 | */ |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 938 | int __generic_file_fsync(struct file *file, loff_t start, loff_t end, |
| 939 | int datasync) |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 940 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 941 | struct inode *inode = file->f_mapping->host; |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 942 | int err; |
| 943 | int ret; |
| 944 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 945 | err = filemap_write_and_wait_range(inode->i_mapping, start, end); |
| 946 | if (err) |
| 947 | return err; |
| 948 | |
| 949 | mutex_lock(&inode->i_mutex); |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 950 | ret = sync_mapping_buffers(inode->i_mapping); |
Theodore Ts'o | 0ae45f6 | 2015-02-02 00:37:00 -0500 | [diff] [blame] | 951 | if (!(inode->i_state & I_DIRTY_ALL)) |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 952 | goto out; |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 953 | if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 954 | goto out; |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 955 | |
Christoph Hellwig | c3765016 | 2010-10-06 10:48:20 +0200 | [diff] [blame] | 956 | err = sync_inode_metadata(inode, 1); |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 957 | if (ret == 0) |
| 958 | ret = err; |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 959 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 960 | out: |
| 961 | mutex_unlock(&inode->i_mutex); |
Al Viro | d5aacad | 2009-06-07 14:56:44 -0400 | [diff] [blame] | 962 | return ret; |
| 963 | } |
Fabian Frederick | ac13a82 | 2014-06-04 16:06:27 -0700 | [diff] [blame] | 964 | EXPORT_SYMBOL(__generic_file_fsync); |
| 965 | |
| 966 | /** |
| 967 | * generic_file_fsync - generic fsync implementation for simple filesystems |
| 968 | * with flush |
| 969 | * @file: file to synchronize |
| 970 | * @start: start offset in bytes |
| 971 | * @end: end offset in bytes (inclusive) |
| 972 | * @datasync: only synchronize essential metadata if true |
| 973 | * |
| 974 | */ |
| 975 | |
| 976 | int generic_file_fsync(struct file *file, loff_t start, loff_t end, |
| 977 | int datasync) |
| 978 | { |
| 979 | struct inode *inode = file->f_mapping->host; |
| 980 | int err; |
| 981 | |
| 982 | err = __generic_file_fsync(file, start, end, datasync); |
| 983 | if (err) |
| 984 | return err; |
| 985 | return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); |
| 986 | } |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 987 | EXPORT_SYMBOL(generic_file_fsync); |
| 988 | |
Patrick J. LoPresti | 30ca22c | 2010-07-22 15:03:41 -0700 | [diff] [blame] | 989 | /** |
| 990 | * generic_check_addressable - Check addressability of file system |
| 991 | * @blocksize_bits: log of file system block size |
| 992 | * @num_blocks: number of blocks in file system |
| 993 | * |
| 994 | * Determine whether a file system with @num_blocks blocks (and a |
| 995 | * block size of 2**@blocksize_bits) is addressable by the sector_t |
| 996 | * and page cache of the system. Return 0 if so and -EFBIG otherwise. |
| 997 | */ |
| 998 | int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks) |
| 999 | { |
| 1000 | u64 last_fs_block = num_blocks - 1; |
Joel Becker | a33f13e | 2010-08-16 12:10:17 -0700 | [diff] [blame] | 1001 | u64 last_fs_page = |
| 1002 | last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits); |
Patrick J. LoPresti | 30ca22c | 2010-07-22 15:03:41 -0700 | [diff] [blame] | 1003 | |
| 1004 | if (unlikely(num_blocks == 0)) |
| 1005 | return 0; |
| 1006 | |
| 1007 | if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT)) |
| 1008 | return -EINVAL; |
| 1009 | |
Joel Becker | a33f13e | 2010-08-16 12:10:17 -0700 | [diff] [blame] | 1010 | if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) || |
| 1011 | (last_fs_page > (pgoff_t)(~0ULL))) { |
Patrick J. LoPresti | 30ca22c | 2010-07-22 15:03:41 -0700 | [diff] [blame] | 1012 | return -EFBIG; |
| 1013 | } |
| 1014 | return 0; |
| 1015 | } |
| 1016 | EXPORT_SYMBOL(generic_check_addressable); |
| 1017 | |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 1018 | /* |
| 1019 | * No-op implementation of ->fsync for in-memory filesystems. |
| 1020 | */ |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 1021 | int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 1022 | { |
| 1023 | return 0; |
| 1024 | } |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 1025 | EXPORT_SYMBOL(noop_fsync); |
Al Viro | 87dc800 | 2013-09-16 10:30:04 -0400 | [diff] [blame] | 1026 | |
Al Viro | 5f2c417 | 2015-05-07 11:14:26 -0400 | [diff] [blame] | 1027 | void kfree_put_link(struct inode *unused, void *cookie) |
Al Viro | 87dc800 | 2013-09-16 10:30:04 -0400 | [diff] [blame] | 1028 | { |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 1029 | kfree(cookie); |
Al Viro | 87dc800 | 2013-09-16 10:30:04 -0400 | [diff] [blame] | 1030 | } |
| 1031 | EXPORT_SYMBOL(kfree_put_link); |
Al Viro | 6987843 | 2013-10-02 22:35:11 -0400 | [diff] [blame] | 1032 | |
Al Viro | ecc087f | 2015-05-07 11:19:14 -0400 | [diff] [blame] | 1033 | void free_page_put_link(struct inode *unused, void *cookie) |
| 1034 | { |
| 1035 | free_page((unsigned long) cookie); |
| 1036 | } |
| 1037 | EXPORT_SYMBOL(free_page_put_link); |
| 1038 | |
Al Viro | 6987843 | 2013-10-02 22:35:11 -0400 | [diff] [blame] | 1039 | /* |
| 1040 | * nop .set_page_dirty method so that people can use .page_mkwrite on |
| 1041 | * anon inodes. |
| 1042 | */ |
| 1043 | static int anon_set_page_dirty(struct page *page) |
| 1044 | { |
| 1045 | return 0; |
| 1046 | }; |
| 1047 | |
| 1048 | /* |
| 1049 | * A single inode exists for all anon_inode files. Contrary to pipes, |
| 1050 | * anon_inode inodes have no associated per-instance data, so we need |
| 1051 | * only allocate one of them. |
| 1052 | */ |
| 1053 | struct inode *alloc_anon_inode(struct super_block *s) |
| 1054 | { |
| 1055 | static const struct address_space_operations anon_aops = { |
| 1056 | .set_page_dirty = anon_set_page_dirty, |
| 1057 | }; |
| 1058 | struct inode *inode = new_inode_pseudo(s); |
| 1059 | |
| 1060 | if (!inode) |
| 1061 | return ERR_PTR(-ENOMEM); |
| 1062 | |
| 1063 | inode->i_ino = get_next_ino(); |
| 1064 | inode->i_mapping->a_ops = &anon_aops; |
| 1065 | |
| 1066 | /* |
| 1067 | * Mark the inode dirty from the very beginning, |
| 1068 | * that way it will never be moved to the dirty |
| 1069 | * list because mark_inode_dirty() will think |
| 1070 | * that it already _is_ on the dirty list. |
| 1071 | */ |
| 1072 | inode->i_state = I_DIRTY; |
| 1073 | inode->i_mode = S_IRUSR | S_IWUSR; |
| 1074 | inode->i_uid = current_fsuid(); |
| 1075 | inode->i_gid = current_fsgid(); |
| 1076 | inode->i_flags |= S_PRIVATE; |
| 1077 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 1078 | return inode; |
| 1079 | } |
| 1080 | EXPORT_SYMBOL(alloc_anon_inode); |
Jeff Layton | 1c994a0 | 2014-08-27 06:49:41 -0400 | [diff] [blame] | 1081 | |
| 1082 | /** |
| 1083 | * simple_nosetlease - generic helper for prohibiting leases |
| 1084 | * @filp: file pointer |
| 1085 | * @arg: type of lease to obtain |
| 1086 | * @flp: new lease supplied for insertion |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 1087 | * @priv: private data for lm_setup operation |
Jeff Layton | 1c994a0 | 2014-08-27 06:49:41 -0400 | [diff] [blame] | 1088 | * |
| 1089 | * Generic helper for filesystems that do not wish to allow leases to be set. |
| 1090 | * All arguments are ignored and it just returns -EINVAL. |
| 1091 | */ |
| 1092 | int |
Jeff Layton | e6f5c78 | 2014-08-22 10:40:25 -0400 | [diff] [blame] | 1093 | simple_nosetlease(struct file *filp, long arg, struct file_lock **flp, |
| 1094 | void **priv) |
Jeff Layton | 1c994a0 | 2014-08-27 06:49:41 -0400 | [diff] [blame] | 1095 | { |
| 1096 | return -EINVAL; |
| 1097 | } |
| 1098 | EXPORT_SYMBOL(simple_nosetlease); |
Al Viro | 61ba64f | 2015-05-02 09:54:06 -0400 | [diff] [blame] | 1099 | |
Al Viro | 6e77137 | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 1100 | const char *simple_follow_link(struct dentry *dentry, void **cookie) |
Al Viro | 61ba64f | 2015-05-02 09:54:06 -0400 | [diff] [blame] | 1101 | { |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 1102 | return d_inode(dentry)->i_link; |
Al Viro | 61ba64f | 2015-05-02 09:54:06 -0400 | [diff] [blame] | 1103 | } |
| 1104 | EXPORT_SYMBOL(simple_follow_link); |
| 1105 | |
| 1106 | const struct inode_operations simple_symlink_inode_operations = { |
| 1107 | .follow_link = simple_follow_link, |
| 1108 | .readlink = generic_readlink |
| 1109 | }; |
| 1110 | EXPORT_SYMBOL(simple_symlink_inode_operations); |