inode.c 13 KB

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