inode.c 15 KB

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