inode.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. #include <linux/file.h>
  23. #include <linux/fs.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/init.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/module.h>
  28. #include <linux/mount.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. #include <asm/prom.h>
  35. #include <asm/semaphore.h>
  36. #include <asm/spu.h>
  37. #include <asm/uaccess.h>
  38. #include "spufs.h"
  39. static struct kmem_cache *spufs_inode_cache;
  40. char *isolated_loader;
  41. static struct inode *
  42. spufs_alloc_inode(struct super_block *sb)
  43. {
  44. struct spufs_inode_info *ei;
  45. ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
  46. if (!ei)
  47. return NULL;
  48. ei->i_gang = NULL;
  49. ei->i_ctx = NULL;
  50. return &ei->vfs_inode;
  51. }
  52. static void
  53. spufs_destroy_inode(struct inode *inode)
  54. {
  55. kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
  56. }
  57. static void
  58. spufs_init_once(void *p, struct kmem_cache * cachep, unsigned long flags)
  59. {
  60. struct spufs_inode_info *ei = p;
  61. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  62. SLAB_CTOR_CONSTRUCTOR) {
  63. inode_init_once(&ei->vfs_inode);
  64. }
  65. }
  66. static struct inode *
  67. spufs_new_inode(struct super_block *sb, int mode)
  68. {
  69. struct inode *inode;
  70. inode = new_inode(sb);
  71. if (!inode)
  72. goto out;
  73. inode->i_mode = mode;
  74. inode->i_uid = current->fsuid;
  75. inode->i_gid = current->fsgid;
  76. inode->i_blocks = 0;
  77. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  78. out:
  79. return inode;
  80. }
  81. static int
  82. spufs_setattr(struct dentry *dentry, struct iattr *attr)
  83. {
  84. struct inode *inode = dentry->d_inode;
  85. if ((attr->ia_valid & ATTR_SIZE) &&
  86. (attr->ia_size != inode->i_size))
  87. return -EINVAL;
  88. return inode_setattr(inode, attr);
  89. }
  90. static int
  91. spufs_new_file(struct super_block *sb, struct dentry *dentry,
  92. const struct file_operations *fops, int mode,
  93. struct spu_context *ctx)
  94. {
  95. static struct inode_operations spufs_file_iops = {
  96. .setattr = spufs_setattr,
  97. };
  98. struct inode *inode;
  99. int ret;
  100. ret = -ENOSPC;
  101. inode = spufs_new_inode(sb, S_IFREG | mode);
  102. if (!inode)
  103. goto out;
  104. ret = 0;
  105. inode->i_op = &spufs_file_iops;
  106. inode->i_fop = fops;
  107. inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
  108. d_add(dentry, inode);
  109. out:
  110. return ret;
  111. }
  112. static void
  113. spufs_delete_inode(struct inode *inode)
  114. {
  115. struct spufs_inode_info *ei = SPUFS_I(inode);
  116. if (ei->i_ctx)
  117. put_spu_context(ei->i_ctx);
  118. if (ei->i_gang)
  119. put_spu_gang(ei->i_gang);
  120. clear_inode(inode);
  121. }
  122. static void spufs_prune_dir(struct dentry *dir)
  123. {
  124. struct dentry *dentry, *tmp;
  125. mutex_lock(&dir->d_inode->i_mutex);
  126. list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
  127. spin_lock(&dcache_lock);
  128. spin_lock(&dentry->d_lock);
  129. if (!(d_unhashed(dentry)) && dentry->d_inode) {
  130. dget_locked(dentry);
  131. __d_drop(dentry);
  132. spin_unlock(&dentry->d_lock);
  133. simple_unlink(dir->d_inode, dentry);
  134. spin_unlock(&dcache_lock);
  135. dput(dentry);
  136. } else {
  137. spin_unlock(&dentry->d_lock);
  138. spin_unlock(&dcache_lock);
  139. }
  140. }
  141. shrink_dcache_parent(dir);
  142. mutex_unlock(&dir->d_inode->i_mutex);
  143. }
  144. /* Caller must hold parent->i_mutex */
  145. static int spufs_rmdir(struct inode *parent, struct dentry *dir)
  146. {
  147. /* remove all entries */
  148. spufs_prune_dir(dir);
  149. return simple_rmdir(parent, dir);
  150. }
  151. static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
  152. int mode, struct spu_context *ctx)
  153. {
  154. struct dentry *dentry;
  155. int ret;
  156. while (files->name && files->name[0]) {
  157. ret = -ENOMEM;
  158. dentry = d_alloc_name(dir, files->name);
  159. if (!dentry)
  160. goto out;
  161. ret = spufs_new_file(dir->d_sb, dentry, files->ops,
  162. files->mode & mode, ctx);
  163. if (ret)
  164. goto out;
  165. files++;
  166. }
  167. return 0;
  168. out:
  169. spufs_prune_dir(dir);
  170. return ret;
  171. }
  172. static int spufs_dir_close(struct inode *inode, struct file *file)
  173. {
  174. struct spu_context *ctx;
  175. struct inode *parent;
  176. struct dentry *dir;
  177. int ret;
  178. dir = file->f_path.dentry;
  179. parent = dir->d_parent->d_inode;
  180. ctx = SPUFS_I(dir->d_inode)->i_ctx;
  181. mutex_lock(&parent->i_mutex);
  182. ret = spufs_rmdir(parent, dir);
  183. mutex_unlock(&parent->i_mutex);
  184. WARN_ON(ret);
  185. /* We have to give up the mm_struct */
  186. spu_forget(ctx);
  187. return dcache_dir_close(inode, file);
  188. }
  189. struct inode_operations spufs_dir_inode_operations = {
  190. .lookup = simple_lookup,
  191. };
  192. struct file_operations spufs_context_fops = {
  193. .open = dcache_dir_open,
  194. .release = spufs_dir_close,
  195. .llseek = dcache_dir_lseek,
  196. .read = generic_read_dir,
  197. .readdir = dcache_readdir,
  198. .fsync = simple_sync_file,
  199. };
  200. EXPORT_SYMBOL_GPL(spufs_context_fops);
  201. static int
  202. spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
  203. int mode)
  204. {
  205. int ret;
  206. struct inode *inode;
  207. struct spu_context *ctx;
  208. ret = -ENOSPC;
  209. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  210. if (!inode)
  211. goto out;
  212. if (dir->i_mode & S_ISGID) {
  213. inode->i_gid = dir->i_gid;
  214. inode->i_mode &= S_ISGID;
  215. }
  216. ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
  217. SPUFS_I(inode)->i_ctx = ctx;
  218. if (!ctx)
  219. goto out_iput;
  220. ctx->flags = flags;
  221. inode->i_op = &spufs_dir_inode_operations;
  222. inode->i_fop = &simple_dir_operations;
  223. if (flags & SPU_CREATE_NOSCHED)
  224. ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
  225. mode, ctx);
  226. else
  227. ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
  228. if (ret)
  229. goto out_free_ctx;
  230. d_instantiate(dentry, inode);
  231. dget(dentry);
  232. dir->i_nlink++;
  233. dentry->d_inode->i_nlink++;
  234. goto out;
  235. out_free_ctx:
  236. put_spu_context(ctx);
  237. out_iput:
  238. iput(inode);
  239. out:
  240. return ret;
  241. }
  242. static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
  243. {
  244. int ret;
  245. struct file *filp;
  246. ret = get_unused_fd();
  247. if (ret < 0) {
  248. dput(dentry);
  249. mntput(mnt);
  250. goto out;
  251. }
  252. filp = dentry_open(dentry, mnt, O_RDONLY);
  253. if (IS_ERR(filp)) {
  254. put_unused_fd(ret);
  255. ret = PTR_ERR(filp);
  256. goto out;
  257. }
  258. filp->f_op = &spufs_context_fops;
  259. fd_install(ret, filp);
  260. out:
  261. return ret;
  262. }
  263. static int spufs_create_context(struct inode *inode,
  264. struct dentry *dentry,
  265. struct vfsmount *mnt, int flags, int mode)
  266. {
  267. int ret;
  268. ret = -EPERM;
  269. if ((flags & SPU_CREATE_NOSCHED) &&
  270. !capable(CAP_SYS_NICE))
  271. goto out_unlock;
  272. ret = -EINVAL;
  273. if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
  274. == SPU_CREATE_ISOLATE)
  275. goto out_unlock;
  276. ret = -ENODEV;
  277. if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
  278. goto out_unlock;
  279. ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
  280. if (ret)
  281. goto out_unlock;
  282. /*
  283. * get references for dget and mntget, will be released
  284. * in error path of *_open().
  285. */
  286. ret = spufs_context_open(dget(dentry), mntget(mnt));
  287. if (ret < 0) {
  288. WARN_ON(spufs_rmdir(inode, dentry));
  289. mutex_unlock(&inode->i_mutex);
  290. spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
  291. goto out;
  292. }
  293. out_unlock:
  294. mutex_unlock(&inode->i_mutex);
  295. out:
  296. dput(dentry);
  297. return ret;
  298. }
  299. static int spufs_rmgang(struct inode *root, struct dentry *dir)
  300. {
  301. /* FIXME: this fails if the dir is not empty,
  302. which causes a leak of gangs. */
  303. return simple_rmdir(root, dir);
  304. }
  305. static int spufs_gang_close(struct inode *inode, struct file *file)
  306. {
  307. struct inode *parent;
  308. struct dentry *dir;
  309. int ret;
  310. dir = file->f_path.dentry;
  311. parent = dir->d_parent->d_inode;
  312. ret = spufs_rmgang(parent, dir);
  313. WARN_ON(ret);
  314. return dcache_dir_close(inode, file);
  315. }
  316. struct file_operations spufs_gang_fops = {
  317. .open = dcache_dir_open,
  318. .release = spufs_gang_close,
  319. .llseek = dcache_dir_lseek,
  320. .read = generic_read_dir,
  321. .readdir = dcache_readdir,
  322. .fsync = simple_sync_file,
  323. };
  324. static int
  325. spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
  326. {
  327. int ret;
  328. struct inode *inode;
  329. struct spu_gang *gang;
  330. ret = -ENOSPC;
  331. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  332. if (!inode)
  333. goto out;
  334. ret = 0;
  335. if (dir->i_mode & S_ISGID) {
  336. inode->i_gid = dir->i_gid;
  337. inode->i_mode &= S_ISGID;
  338. }
  339. gang = alloc_spu_gang();
  340. SPUFS_I(inode)->i_ctx = NULL;
  341. SPUFS_I(inode)->i_gang = gang;
  342. if (!gang)
  343. goto out_iput;
  344. inode->i_op = &spufs_dir_inode_operations;
  345. inode->i_fop = &simple_dir_operations;
  346. d_instantiate(dentry, inode);
  347. dget(dentry);
  348. dir->i_nlink++;
  349. dentry->d_inode->i_nlink++;
  350. return ret;
  351. out_iput:
  352. iput(inode);
  353. out:
  354. return ret;
  355. }
  356. static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
  357. {
  358. int ret;
  359. struct file *filp;
  360. ret = get_unused_fd();
  361. if (ret < 0) {
  362. dput(dentry);
  363. mntput(mnt);
  364. goto out;
  365. }
  366. filp = dentry_open(dentry, mnt, O_RDONLY);
  367. if (IS_ERR(filp)) {
  368. put_unused_fd(ret);
  369. ret = PTR_ERR(filp);
  370. goto out;
  371. }
  372. filp->f_op = &spufs_gang_fops;
  373. fd_install(ret, filp);
  374. out:
  375. return ret;
  376. }
  377. static int spufs_create_gang(struct inode *inode,
  378. struct dentry *dentry,
  379. struct vfsmount *mnt, int mode)
  380. {
  381. int ret;
  382. ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
  383. if (ret)
  384. goto out;
  385. /*
  386. * get references for dget and mntget, will be released
  387. * in error path of *_open().
  388. */
  389. ret = spufs_gang_open(dget(dentry), mntget(mnt));
  390. if (ret < 0)
  391. WARN_ON(spufs_rmgang(inode, dentry));
  392. out:
  393. mutex_unlock(&inode->i_mutex);
  394. dput(dentry);
  395. return ret;
  396. }
  397. static struct file_system_type spufs_type;
  398. long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
  399. {
  400. struct dentry *dentry;
  401. int ret;
  402. ret = -EINVAL;
  403. /* check if we are on spufs */
  404. if (nd->dentry->d_sb->s_type != &spufs_type)
  405. goto out;
  406. /* don't accept undefined flags */
  407. if (flags & (~SPU_CREATE_FLAG_ALL))
  408. goto out;
  409. /* only threads can be underneath a gang */
  410. if (nd->dentry != nd->dentry->d_sb->s_root) {
  411. if ((flags & SPU_CREATE_GANG) ||
  412. !SPUFS_I(nd->dentry->d_inode)->i_gang)
  413. goto out;
  414. }
  415. dentry = lookup_create(nd, 1);
  416. ret = PTR_ERR(dentry);
  417. if (IS_ERR(dentry))
  418. goto out_dir;
  419. ret = -EEXIST;
  420. if (dentry->d_inode)
  421. goto out_dput;
  422. mode &= ~current->fs->umask;
  423. if (flags & SPU_CREATE_GANG)
  424. return spufs_create_gang(nd->dentry->d_inode,
  425. dentry, nd->mnt, mode);
  426. else
  427. return spufs_create_context(nd->dentry->d_inode,
  428. dentry, nd->mnt, flags, mode);
  429. out_dput:
  430. dput(dentry);
  431. out_dir:
  432. mutex_unlock(&nd->dentry->d_inode->i_mutex);
  433. out:
  434. return ret;
  435. }
  436. /* File system initialization */
  437. enum {
  438. Opt_uid, Opt_gid, Opt_err,
  439. };
  440. static match_table_t spufs_tokens = {
  441. { Opt_uid, "uid=%d" },
  442. { Opt_gid, "gid=%d" },
  443. { Opt_err, NULL },
  444. };
  445. static int
  446. spufs_parse_options(char *options, struct inode *root)
  447. {
  448. char *p;
  449. substring_t args[MAX_OPT_ARGS];
  450. while ((p = strsep(&options, ",")) != NULL) {
  451. int token, option;
  452. if (!*p)
  453. continue;
  454. token = match_token(p, spufs_tokens, args);
  455. switch (token) {
  456. case Opt_uid:
  457. if (match_int(&args[0], &option))
  458. return 0;
  459. root->i_uid = option;
  460. break;
  461. case Opt_gid:
  462. if (match_int(&args[0], &option))
  463. return 0;
  464. root->i_gid = option;
  465. break;
  466. default:
  467. return 0;
  468. }
  469. }
  470. return 1;
  471. }
  472. static void
  473. spufs_init_isolated_loader(void)
  474. {
  475. struct device_node *dn;
  476. const char *loader;
  477. int size;
  478. dn = of_find_node_by_path("/spu-isolation");
  479. if (!dn)
  480. return;
  481. loader = get_property(dn, "loader", &size);
  482. if (!loader)
  483. return;
  484. /* kmalloc should align on a 16 byte boundary..* */
  485. isolated_loader = kmalloc(size, GFP_KERNEL);
  486. if (!isolated_loader)
  487. return;
  488. memcpy(isolated_loader, loader, size);
  489. printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
  490. }
  491. static int
  492. spufs_create_root(struct super_block *sb, void *data)
  493. {
  494. struct inode *inode;
  495. int ret;
  496. ret = -ENOMEM;
  497. inode = spufs_new_inode(sb, S_IFDIR | 0775);
  498. if (!inode)
  499. goto out;
  500. inode->i_op = &spufs_dir_inode_operations;
  501. inode->i_fop = &simple_dir_operations;
  502. SPUFS_I(inode)->i_ctx = NULL;
  503. ret = -EINVAL;
  504. if (!spufs_parse_options(data, inode))
  505. goto out_iput;
  506. ret = -ENOMEM;
  507. sb->s_root = d_alloc_root(inode);
  508. if (!sb->s_root)
  509. goto out_iput;
  510. return 0;
  511. out_iput:
  512. iput(inode);
  513. out:
  514. return ret;
  515. }
  516. static int
  517. spufs_fill_super(struct super_block *sb, void *data, int silent)
  518. {
  519. static struct super_operations s_ops = {
  520. .alloc_inode = spufs_alloc_inode,
  521. .destroy_inode = spufs_destroy_inode,
  522. .statfs = simple_statfs,
  523. .delete_inode = spufs_delete_inode,
  524. .drop_inode = generic_delete_inode,
  525. };
  526. sb->s_maxbytes = MAX_LFS_FILESIZE;
  527. sb->s_blocksize = PAGE_CACHE_SIZE;
  528. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  529. sb->s_magic = SPUFS_MAGIC;
  530. sb->s_op = &s_ops;
  531. return spufs_create_root(sb, data);
  532. }
  533. static int
  534. spufs_get_sb(struct file_system_type *fstype, int flags,
  535. const char *name, void *data, struct vfsmount *mnt)
  536. {
  537. return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
  538. }
  539. static struct file_system_type spufs_type = {
  540. .owner = THIS_MODULE,
  541. .name = "spufs",
  542. .get_sb = spufs_get_sb,
  543. .kill_sb = kill_litter_super,
  544. };
  545. static int __init spufs_init(void)
  546. {
  547. int ret;
  548. ret = -ENOMEM;
  549. spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
  550. sizeof(struct spufs_inode_info), 0,
  551. SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
  552. if (!spufs_inode_cache)
  553. goto out;
  554. if (spu_sched_init() != 0) {
  555. kmem_cache_destroy(spufs_inode_cache);
  556. goto out;
  557. }
  558. ret = register_filesystem(&spufs_type);
  559. if (ret)
  560. goto out_cache;
  561. ret = register_spu_syscalls(&spufs_calls);
  562. if (ret)
  563. goto out_fs;
  564. ret = register_arch_coredump_calls(&spufs_coredump_calls);
  565. if (ret)
  566. goto out_fs;
  567. spufs_init_isolated_loader();
  568. return 0;
  569. out_fs:
  570. unregister_filesystem(&spufs_type);
  571. out_cache:
  572. kmem_cache_destroy(spufs_inode_cache);
  573. out:
  574. return ret;
  575. }
  576. module_init(spufs_init);
  577. static void __exit spufs_exit(void)
  578. {
  579. spu_sched_exit();
  580. unregister_arch_coredump_calls(&spufs_coredump_calls);
  581. unregister_spu_syscalls(&spufs_calls);
  582. unregister_filesystem(&spufs_type);
  583. kmem_cache_destroy(spufs_inode_cache);
  584. }
  585. module_exit(spufs_exit);
  586. MODULE_LICENSE("GPL");
  587. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");