Arnd Bergmann | 67207b9 | 2005-11-15 15:53:48 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * SPU file system |
| 3 | * |
| 4 | * (C) Copyright IBM Deutschland Entwicklung GmbH 2005 |
| 5 | * |
| 6 | * Author: Arnd Bergmann <arndb@de.ibm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/file.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/backing-dev.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/ioctl.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/namei.h> |
| 30 | #include <linux/pagemap.h> |
| 31 | #include <linux/poll.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/parser.h> |
| 34 | |
| 35 | #include <asm/io.h> |
| 36 | #include <asm/semaphore.h> |
| 37 | #include <asm/spu.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | #include "spufs.h" |
| 41 | |
| 42 | static kmem_cache_t *spufs_inode_cache; |
| 43 | |
| 44 | /* Information about the backing dev, same as ramfs */ |
| 45 | #if 0 |
| 46 | static struct backing_dev_info spufs_backing_dev_info = { |
| 47 | .ra_pages = 0, /* No readahead */ |
| 48 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK | |
| 49 | BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY | BDI_CAP_READ_MAP | |
| 50 | BDI_CAP_WRITE_MAP, |
| 51 | }; |
| 52 | |
| 53 | static struct address_space_operations spufs_aops = { |
| 54 | .readpage = simple_readpage, |
| 55 | .prepare_write = simple_prepare_write, |
| 56 | .commit_write = simple_commit_write, |
| 57 | }; |
| 58 | #endif |
| 59 | |
| 60 | /* Inode operations */ |
| 61 | |
| 62 | static struct inode * |
| 63 | spufs_alloc_inode(struct super_block *sb) |
| 64 | { |
| 65 | struct spufs_inode_info *ei; |
| 66 | |
| 67 | ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL); |
| 68 | if (!ei) |
| 69 | return NULL; |
| 70 | return &ei->vfs_inode; |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | spufs_destroy_inode(struct inode *inode) |
| 75 | { |
| 76 | kmem_cache_free(spufs_inode_cache, SPUFS_I(inode)); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags) |
| 81 | { |
| 82 | struct spufs_inode_info *ei = p; |
| 83 | |
| 84 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 85 | SLAB_CTOR_CONSTRUCTOR) { |
| 86 | inode_init_once(&ei->vfs_inode); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static struct inode * |
| 91 | spufs_new_inode(struct super_block *sb, int mode) |
| 92 | { |
| 93 | struct inode *inode; |
| 94 | |
| 95 | inode = new_inode(sb); |
| 96 | if (!inode) |
| 97 | goto out; |
| 98 | |
| 99 | inode->i_mode = mode; |
| 100 | inode->i_uid = current->fsuid; |
| 101 | inode->i_gid = current->fsgid; |
| 102 | inode->i_blksize = PAGE_CACHE_SIZE; |
| 103 | inode->i_blocks = 0; |
| 104 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 105 | out: |
| 106 | return inode; |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | spufs_setattr(struct dentry *dentry, struct iattr *attr) |
| 111 | { |
| 112 | struct inode *inode = dentry->d_inode; |
| 113 | |
| 114 | /* dump_stack(); |
| 115 | pr_debug("ia_size %lld, i_size:%lld\n", attr->ia_size, inode->i_size); |
| 116 | */ |
| 117 | if ((attr->ia_valid & ATTR_SIZE) && |
| 118 | (attr->ia_size != inode->i_size)) |
| 119 | return -EINVAL; |
| 120 | return inode_setattr(inode, attr); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static int |
| 125 | spufs_new_file(struct super_block *sb, struct dentry *dentry, |
| 126 | struct file_operations *fops, int mode, |
| 127 | struct spu_context *ctx) |
| 128 | { |
| 129 | static struct inode_operations spufs_file_iops = { |
| 130 | .getattr = simple_getattr, |
| 131 | .setattr = spufs_setattr, |
| 132 | .unlink = simple_unlink, |
| 133 | }; |
| 134 | struct inode *inode; |
| 135 | int ret; |
| 136 | |
| 137 | ret = -ENOSPC; |
| 138 | inode = spufs_new_inode(sb, S_IFREG | mode); |
| 139 | if (!inode) |
| 140 | goto out; |
| 141 | |
| 142 | ret = 0; |
| 143 | inode->i_op = &spufs_file_iops; |
| 144 | inode->i_fop = fops; |
| 145 | inode->u.generic_ip = SPUFS_I(inode)->i_ctx = get_spu_context(ctx); |
| 146 | d_add(dentry, inode); |
| 147 | out: |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | spufs_delete_inode(struct inode *inode) |
| 153 | { |
| 154 | if (SPUFS_I(inode)->i_ctx) |
| 155 | put_spu_context(SPUFS_I(inode)->i_ctx); |
| 156 | clear_inode(inode); |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | spufs_fill_dir(struct dentry *dir, struct tree_descr *files, |
| 161 | int mode, struct spu_context *ctx) |
| 162 | { |
| 163 | struct dentry *dentry; |
| 164 | int ret; |
| 165 | |
| 166 | while (files->name && files->name[0]) { |
| 167 | ret = -ENOMEM; |
| 168 | dentry = d_alloc_name(dir, files->name); |
| 169 | if (!dentry) |
| 170 | goto out; |
| 171 | ret = spufs_new_file(dir->d_sb, dentry, files->ops, |
| 172 | files->mode & mode, ctx); |
| 173 | if (ret) |
| 174 | goto out; |
| 175 | files++; |
| 176 | } |
| 177 | return 0; |
| 178 | out: |
| 179 | // FIXME: remove all files that are left |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry) |
| 185 | { |
| 186 | struct dentry *dentry; |
| 187 | int err; |
| 188 | |
| 189 | spin_lock(&dcache_lock); |
| 190 | /* remove all entries */ |
| 191 | err = 0; |
| 192 | list_for_each_entry(dentry, &dir_dentry->d_subdirs, d_child) { |
| 193 | if (d_unhashed(dentry) || !dentry->d_inode) |
| 194 | continue; |
| 195 | atomic_dec(&dentry->d_count); |
| 196 | spin_lock(&dentry->d_lock); |
| 197 | __d_drop(dentry); |
| 198 | spin_unlock(&dentry->d_lock); |
| 199 | } |
| 200 | spin_unlock(&dcache_lock); |
| 201 | if (!err) { |
| 202 | shrink_dcache_parent(dir_dentry); |
| 203 | err = simple_rmdir(root, dir_dentry); |
| 204 | } |
| 205 | return err; |
| 206 | } |
| 207 | |
| 208 | static int spufs_dir_close(struct inode *inode, struct file *file) |
| 209 | { |
| 210 | struct inode *dir; |
| 211 | struct dentry *dentry; |
| 212 | int ret; |
| 213 | |
| 214 | dentry = file->f_dentry; |
| 215 | dir = dentry->d_parent->d_inode; |
| 216 | down(&dir->i_sem); |
| 217 | ret = spufs_rmdir(dir, file->f_dentry); |
| 218 | WARN_ON(ret); |
| 219 | up(&dir->i_sem); |
| 220 | return dcache_dir_close(inode, file); |
| 221 | } |
| 222 | |
| 223 | struct inode_operations spufs_dir_inode_operations = { |
| 224 | .lookup = simple_lookup, |
| 225 | }; |
| 226 | |
| 227 | struct file_operations spufs_autodelete_dir_operations = { |
| 228 | .open = dcache_dir_open, |
| 229 | .release = spufs_dir_close, |
| 230 | .llseek = dcache_dir_lseek, |
| 231 | .read = generic_read_dir, |
| 232 | .readdir = dcache_readdir, |
| 233 | .fsync = simple_sync_file, |
| 234 | }; |
| 235 | |
| 236 | static int |
| 237 | spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 238 | { |
| 239 | int ret; |
| 240 | struct inode *inode; |
| 241 | struct spu_context *ctx; |
| 242 | |
| 243 | ret = -ENOSPC; |
| 244 | inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR); |
| 245 | if (!inode) |
| 246 | goto out; |
| 247 | |
| 248 | if (dir->i_mode & S_ISGID) { |
| 249 | inode->i_gid = dir->i_gid; |
| 250 | inode->i_mode &= S_ISGID; |
| 251 | } |
| 252 | ctx = alloc_spu_context(); |
| 253 | SPUFS_I(inode)->i_ctx = ctx; |
| 254 | if (!ctx) |
| 255 | goto out_iput; |
| 256 | |
| 257 | inode->i_op = &spufs_dir_inode_operations; |
| 258 | inode->i_fop = &simple_dir_operations; |
| 259 | ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx); |
| 260 | if (ret) |
| 261 | goto out_free_ctx; |
| 262 | |
| 263 | d_instantiate(dentry, inode); |
| 264 | dget(dentry); |
| 265 | dir->i_nlink++; |
| 266 | goto out; |
| 267 | |
| 268 | out_free_ctx: |
| 269 | put_spu_context(ctx); |
| 270 | out_iput: |
| 271 | iput(inode); |
| 272 | out: |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | long |
| 277 | spufs_create_thread(struct nameidata *nd, const char *name, |
| 278 | unsigned int flags, mode_t mode) |
| 279 | { |
| 280 | struct dentry *dentry; |
| 281 | struct file *filp; |
| 282 | int ret; |
| 283 | |
| 284 | /* need to be at the root of spufs */ |
| 285 | ret = -EINVAL; |
| 286 | if (nd->dentry->d_sb->s_magic != SPUFS_MAGIC || |
| 287 | nd->dentry != nd->dentry->d_sb->s_root) |
| 288 | goto out; |
| 289 | |
| 290 | dentry = lookup_create(nd, 1); |
| 291 | ret = PTR_ERR(dentry); |
| 292 | if (IS_ERR(dentry)) |
| 293 | goto out_dir; |
| 294 | |
| 295 | ret = -EEXIST; |
| 296 | if (dentry->d_inode) |
| 297 | goto out_dput; |
| 298 | |
| 299 | mode &= ~current->fs->umask; |
| 300 | ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO); |
| 301 | if (ret) |
| 302 | goto out_dput; |
| 303 | |
| 304 | ret = get_unused_fd(); |
| 305 | if (ret < 0) |
| 306 | goto out_dput; |
| 307 | |
| 308 | dentry->d_inode->i_nlink++; |
| 309 | |
| 310 | filp = filp_open(name, O_RDONLY, mode); |
| 311 | if (IS_ERR(filp)) { |
| 312 | // FIXME: remove directory again |
| 313 | put_unused_fd(ret); |
| 314 | ret = PTR_ERR(filp); |
| 315 | } else { |
| 316 | filp->f_op = &spufs_autodelete_dir_operations; |
| 317 | fd_install(ret, filp); |
| 318 | } |
| 319 | |
| 320 | out_dput: |
| 321 | dput(dentry); |
| 322 | out_dir: |
| 323 | up(&nd->dentry->d_inode->i_sem); |
| 324 | out: |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | /* File system initialization */ |
| 329 | enum { |
| 330 | Opt_uid, Opt_gid, Opt_err, |
| 331 | }; |
| 332 | |
| 333 | static match_table_t spufs_tokens = { |
| 334 | { Opt_uid, "uid=%d" }, |
| 335 | { Opt_gid, "gid=%d" }, |
| 336 | { Opt_err, NULL }, |
| 337 | }; |
| 338 | |
| 339 | static int |
| 340 | spufs_parse_options(char *options, struct inode *root) |
| 341 | { |
| 342 | char *p; |
| 343 | substring_t args[MAX_OPT_ARGS]; |
| 344 | |
| 345 | while ((p = strsep(&options, ",")) != NULL) { |
| 346 | int token, option; |
| 347 | |
| 348 | if (!*p) |
| 349 | continue; |
| 350 | |
| 351 | token = match_token(p, spufs_tokens, args); |
| 352 | switch (token) { |
| 353 | case Opt_uid: |
| 354 | if (match_int(&args[0], &option)) |
| 355 | return 0; |
| 356 | root->i_uid = option; |
| 357 | break; |
| 358 | case Opt_gid: |
| 359 | if (match_int(&args[0], &option)) |
| 360 | return 0; |
| 361 | root->i_gid = option; |
| 362 | break; |
| 363 | default: |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | return 1; |
| 368 | } |
| 369 | |
| 370 | static int |
| 371 | spufs_create_root(struct super_block *sb, void *data) { |
| 372 | struct inode *inode; |
| 373 | int ret; |
| 374 | |
| 375 | ret = -ENOMEM; |
| 376 | inode = spufs_new_inode(sb, S_IFDIR | 0775); |
| 377 | if (!inode) |
| 378 | goto out; |
| 379 | |
| 380 | inode->i_op = &spufs_dir_inode_operations; |
| 381 | inode->i_fop = &simple_dir_operations; |
| 382 | SPUFS_I(inode)->i_ctx = NULL; |
| 383 | |
| 384 | ret = -EINVAL; |
| 385 | if (!spufs_parse_options(data, inode)) |
| 386 | goto out_iput; |
| 387 | |
| 388 | ret = -ENOMEM; |
| 389 | sb->s_root = d_alloc_root(inode); |
| 390 | if (!sb->s_root) |
| 391 | goto out_iput; |
| 392 | |
| 393 | return 0; |
| 394 | out_iput: |
| 395 | iput(inode); |
| 396 | out: |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int |
| 401 | spufs_fill_super(struct super_block *sb, void *data, int silent) |
| 402 | { |
| 403 | static struct super_operations s_ops = { |
| 404 | .alloc_inode = spufs_alloc_inode, |
| 405 | .destroy_inode = spufs_destroy_inode, |
| 406 | .statfs = simple_statfs, |
| 407 | .delete_inode = spufs_delete_inode, |
| 408 | .drop_inode = generic_delete_inode, |
| 409 | }; |
| 410 | |
| 411 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 412 | sb->s_blocksize = PAGE_CACHE_SIZE; |
| 413 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
| 414 | sb->s_magic = SPUFS_MAGIC; |
| 415 | sb->s_op = &s_ops; |
| 416 | |
| 417 | return spufs_create_root(sb, data); |
| 418 | } |
| 419 | |
| 420 | static struct super_block * |
| 421 | spufs_get_sb(struct file_system_type *fstype, int flags, |
| 422 | const char *name, void *data) |
| 423 | { |
| 424 | return get_sb_single(fstype, flags, data, spufs_fill_super); |
| 425 | } |
| 426 | |
| 427 | static struct file_system_type spufs_type = { |
| 428 | .owner = THIS_MODULE, |
| 429 | .name = "spufs", |
| 430 | .get_sb = spufs_get_sb, |
| 431 | .kill_sb = kill_litter_super, |
| 432 | }; |
| 433 | |
| 434 | static int spufs_init(void) |
| 435 | { |
| 436 | int ret; |
| 437 | ret = -ENOMEM; |
| 438 | spufs_inode_cache = kmem_cache_create("spufs_inode_cache", |
| 439 | sizeof(struct spufs_inode_info), 0, |
| 440 | SLAB_HWCACHE_ALIGN, spufs_init_once, NULL); |
| 441 | |
| 442 | if (!spufs_inode_cache) |
| 443 | goto out; |
| 444 | ret = register_filesystem(&spufs_type); |
| 445 | if (ret) |
| 446 | goto out_cache; |
| 447 | ret = register_spu_syscalls(&spufs_calls); |
| 448 | if (ret) |
| 449 | goto out_fs; |
| 450 | return 0; |
| 451 | out_fs: |
| 452 | unregister_filesystem(&spufs_type); |
| 453 | out_cache: |
| 454 | kmem_cache_destroy(spufs_inode_cache); |
| 455 | out: |
| 456 | return ret; |
| 457 | } |
| 458 | module_init(spufs_init); |
| 459 | |
| 460 | static void spufs_exit(void) |
| 461 | { |
| 462 | unregister_spu_syscalls(&spufs_calls); |
| 463 | unregister_filesystem(&spufs_type); |
| 464 | kmem_cache_destroy(spufs_inode_cache); |
| 465 | } |
| 466 | module_exit(spufs_exit); |
| 467 | |
| 468 | MODULE_LICENSE("GPL"); |
| 469 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); |
| 470 | |