inode.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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/namei.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/poll.h>
  31. #include <linux/slab.h>
  32. #include <linux/parser.h>
  33. #include <asm/io.h>
  34. #include <asm/semaphore.h>
  35. #include <asm/spu.h>
  36. #include <asm/uaccess.h>
  37. #include "spufs.h"
  38. static kmem_cache_t *spufs_inode_cache;
  39. static struct inode *
  40. spufs_alloc_inode(struct super_block *sb)
  41. {
  42. struct spufs_inode_info *ei;
  43. ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
  44. if (!ei)
  45. return NULL;
  46. return &ei->vfs_inode;
  47. }
  48. static void
  49. spufs_destroy_inode(struct inode *inode)
  50. {
  51. kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
  52. }
  53. static void
  54. spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
  55. {
  56. struct spufs_inode_info *ei = p;
  57. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  58. SLAB_CTOR_CONSTRUCTOR) {
  59. inode_init_once(&ei->vfs_inode);
  60. }
  61. }
  62. static struct inode *
  63. spufs_new_inode(struct super_block *sb, int mode)
  64. {
  65. struct inode *inode;
  66. inode = new_inode(sb);
  67. if (!inode)
  68. goto out;
  69. inode->i_mode = mode;
  70. inode->i_uid = current->fsuid;
  71. inode->i_gid = current->fsgid;
  72. inode->i_blksize = PAGE_CACHE_SIZE;
  73. inode->i_blocks = 0;
  74. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  75. out:
  76. return inode;
  77. }
  78. static int
  79. spufs_setattr(struct dentry *dentry, struct iattr *attr)
  80. {
  81. struct inode *inode = dentry->d_inode;
  82. if ((attr->ia_valid & ATTR_SIZE) &&
  83. (attr->ia_size != inode->i_size))
  84. return -EINVAL;
  85. return inode_setattr(inode, attr);
  86. }
  87. static int
  88. spufs_new_file(struct super_block *sb, struct dentry *dentry,
  89. struct file_operations *fops, int mode,
  90. struct spu_context *ctx)
  91. {
  92. static struct inode_operations spufs_file_iops = {
  93. .setattr = spufs_setattr,
  94. };
  95. struct inode *inode;
  96. int ret;
  97. ret = -ENOSPC;
  98. inode = spufs_new_inode(sb, S_IFREG | mode);
  99. if (!inode)
  100. goto out;
  101. ret = 0;
  102. inode->i_op = &spufs_file_iops;
  103. inode->i_fop = fops;
  104. inode->u.generic_ip = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
  105. d_add(dentry, inode);
  106. out:
  107. return ret;
  108. }
  109. static void
  110. spufs_delete_inode(struct inode *inode)
  111. {
  112. if (SPUFS_I(inode)->i_ctx)
  113. put_spu_context(SPUFS_I(inode)->i_ctx);
  114. clear_inode(inode);
  115. }
  116. static int
  117. spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
  118. int mode, struct spu_context *ctx)
  119. {
  120. struct dentry *dentry;
  121. int ret;
  122. while (files->name && files->name[0]) {
  123. ret = -ENOMEM;
  124. dentry = d_alloc_name(dir, files->name);
  125. if (!dentry)
  126. goto out;
  127. ret = spufs_new_file(dir->d_sb, dentry, files->ops,
  128. files->mode & mode, ctx);
  129. if (ret)
  130. goto out;
  131. files++;
  132. }
  133. return 0;
  134. out:
  135. // FIXME: remove all files that are left
  136. return ret;
  137. }
  138. static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
  139. {
  140. struct dentry *dentry, *tmp;
  141. struct spu_context *ctx;
  142. /* remove all entries */
  143. down(&root->i_sem);
  144. down(&dir_dentry->d_inode->i_sem);
  145. list_for_each_entry_safe(dentry, tmp, &dir_dentry->d_subdirs, d_child) {
  146. spin_lock(&dcache_lock);
  147. spin_lock(&dentry->d_lock);
  148. if (!(d_unhashed(dentry)) && dentry->d_inode) {
  149. dget_locked(dentry);
  150. __d_drop(dentry);
  151. spin_unlock(&dentry->d_lock);
  152. simple_unlink(dir_dentry->d_inode, dentry);
  153. spin_unlock(&dcache_lock);
  154. dput(dentry);
  155. } else {
  156. spin_unlock(&dentry->d_lock);
  157. spin_unlock(&dcache_lock);
  158. }
  159. }
  160. shrink_dcache_parent(dir_dentry);
  161. up(&dir_dentry->d_inode->i_sem);
  162. up(&root->i_sem);
  163. /* We have to give up the mm_struct */
  164. ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
  165. spu_forget(ctx);
  166. /* XXX Do we need to hold i_sem here ? */
  167. return simple_rmdir(root, dir_dentry);
  168. }
  169. static int spufs_dir_close(struct inode *inode, struct file *file)
  170. {
  171. struct inode *dir;
  172. struct dentry *dentry;
  173. int ret;
  174. dentry = file->f_dentry;
  175. dir = dentry->d_parent->d_inode;
  176. ret = spufs_rmdir(dir, dentry);
  177. WARN_ON(ret);
  178. return dcache_dir_close(inode, file);
  179. }
  180. struct inode_operations spufs_dir_inode_operations = {
  181. .lookup = simple_lookup,
  182. };
  183. struct file_operations spufs_autodelete_dir_operations = {
  184. .open = dcache_dir_open,
  185. .release = spufs_dir_close,
  186. .llseek = dcache_dir_lseek,
  187. .read = generic_read_dir,
  188. .readdir = dcache_readdir,
  189. .fsync = simple_sync_file,
  190. };
  191. static int
  192. spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  193. {
  194. int ret;
  195. struct inode *inode;
  196. struct spu_context *ctx;
  197. ret = -ENOSPC;
  198. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  199. if (!inode)
  200. goto out;
  201. if (dir->i_mode & S_ISGID) {
  202. inode->i_gid = dir->i_gid;
  203. inode->i_mode &= S_ISGID;
  204. }
  205. ctx = alloc_spu_context(inode->i_mapping);
  206. SPUFS_I(inode)->i_ctx = ctx;
  207. if (!ctx)
  208. goto out_iput;
  209. inode->i_op = &spufs_dir_inode_operations;
  210. inode->i_fop = &simple_dir_operations;
  211. ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
  212. if (ret)
  213. goto out_free_ctx;
  214. d_instantiate(dentry, inode);
  215. dget(dentry);
  216. dir->i_nlink++;
  217. goto out;
  218. out_free_ctx:
  219. put_spu_context(ctx);
  220. out_iput:
  221. iput(inode);
  222. out:
  223. return ret;
  224. }
  225. long
  226. spufs_create_thread(struct nameidata *nd, const char *name,
  227. unsigned int flags, mode_t mode)
  228. {
  229. struct dentry *dentry;
  230. struct file *filp;
  231. int ret;
  232. /* need to be at the root of spufs */
  233. ret = -EINVAL;
  234. if (nd->dentry->d_sb->s_magic != SPUFS_MAGIC ||
  235. nd->dentry != nd->dentry->d_sb->s_root)
  236. goto out;
  237. dentry = lookup_create(nd, 1);
  238. ret = PTR_ERR(dentry);
  239. if (IS_ERR(dentry))
  240. goto out_dir;
  241. ret = -EEXIST;
  242. if (dentry->d_inode)
  243. goto out_dput;
  244. mode &= ~current->fs->umask;
  245. ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
  246. if (ret)
  247. goto out_dput;
  248. ret = get_unused_fd();
  249. if (ret < 0)
  250. goto out_dput;
  251. dentry->d_inode->i_nlink++;
  252. filp = filp_open(name, O_RDONLY, mode);
  253. if (IS_ERR(filp)) {
  254. // FIXME: remove directory again
  255. put_unused_fd(ret);
  256. ret = PTR_ERR(filp);
  257. } else {
  258. filp->f_op = &spufs_autodelete_dir_operations;
  259. fd_install(ret, filp);
  260. }
  261. out_dput:
  262. dput(dentry);
  263. out_dir:
  264. up(&nd->dentry->d_inode->i_sem);
  265. out:
  266. return ret;
  267. }
  268. /* File system initialization */
  269. enum {
  270. Opt_uid, Opt_gid, Opt_err,
  271. };
  272. static match_table_t spufs_tokens = {
  273. { Opt_uid, "uid=%d" },
  274. { Opt_gid, "gid=%d" },
  275. { Opt_err, NULL },
  276. };
  277. static int
  278. spufs_parse_options(char *options, struct inode *root)
  279. {
  280. char *p;
  281. substring_t args[MAX_OPT_ARGS];
  282. while ((p = strsep(&options, ",")) != NULL) {
  283. int token, option;
  284. if (!*p)
  285. continue;
  286. token = match_token(p, spufs_tokens, args);
  287. switch (token) {
  288. case Opt_uid:
  289. if (match_int(&args[0], &option))
  290. return 0;
  291. root->i_uid = option;
  292. break;
  293. case Opt_gid:
  294. if (match_int(&args[0], &option))
  295. return 0;
  296. root->i_gid = option;
  297. break;
  298. default:
  299. return 0;
  300. }
  301. }
  302. return 1;
  303. }
  304. static int
  305. spufs_create_root(struct super_block *sb, void *data)
  306. {
  307. struct inode *inode;
  308. int ret;
  309. ret = -ENOMEM;
  310. inode = spufs_new_inode(sb, S_IFDIR | 0775);
  311. if (!inode)
  312. goto out;
  313. inode->i_op = &spufs_dir_inode_operations;
  314. inode->i_fop = &simple_dir_operations;
  315. SPUFS_I(inode)->i_ctx = NULL;
  316. ret = -EINVAL;
  317. if (!spufs_parse_options(data, inode))
  318. goto out_iput;
  319. ret = -ENOMEM;
  320. sb->s_root = d_alloc_root(inode);
  321. if (!sb->s_root)
  322. goto out_iput;
  323. return 0;
  324. out_iput:
  325. iput(inode);
  326. out:
  327. return ret;
  328. }
  329. static int
  330. spufs_fill_super(struct super_block *sb, void *data, int silent)
  331. {
  332. static struct super_operations s_ops = {
  333. .alloc_inode = spufs_alloc_inode,
  334. .destroy_inode = spufs_destroy_inode,
  335. .statfs = simple_statfs,
  336. .delete_inode = spufs_delete_inode,
  337. .drop_inode = generic_delete_inode,
  338. };
  339. sb->s_maxbytes = MAX_LFS_FILESIZE;
  340. sb->s_blocksize = PAGE_CACHE_SIZE;
  341. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  342. sb->s_magic = SPUFS_MAGIC;
  343. sb->s_op = &s_ops;
  344. return spufs_create_root(sb, data);
  345. }
  346. static struct super_block *
  347. spufs_get_sb(struct file_system_type *fstype, int flags,
  348. const char *name, void *data)
  349. {
  350. return get_sb_single(fstype, flags, data, spufs_fill_super);
  351. }
  352. static struct file_system_type spufs_type = {
  353. .owner = THIS_MODULE,
  354. .name = "spufs",
  355. .get_sb = spufs_get_sb,
  356. .kill_sb = kill_litter_super,
  357. };
  358. static int spufs_init(void)
  359. {
  360. int ret;
  361. ret = -ENOMEM;
  362. spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
  363. sizeof(struct spufs_inode_info), 0,
  364. SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
  365. if (!spufs_inode_cache)
  366. goto out;
  367. if (spu_sched_init() != 0) {
  368. kmem_cache_destroy(spufs_inode_cache);
  369. goto out;
  370. }
  371. ret = register_filesystem(&spufs_type);
  372. if (ret)
  373. goto out_cache;
  374. ret = register_spu_syscalls(&spufs_calls);
  375. if (ret)
  376. goto out_fs;
  377. return 0;
  378. out_fs:
  379. unregister_filesystem(&spufs_type);
  380. out_cache:
  381. kmem_cache_destroy(spufs_inode_cache);
  382. out:
  383. return ret;
  384. }
  385. module_init(spufs_init);
  386. static void spufs_exit(void)
  387. {
  388. spu_sched_exit();
  389. unregister_spu_syscalls(&spufs_calls);
  390. unregister_filesystem(&spufs_type);
  391. kmem_cache_destroy(spufs_inode_cache);
  392. }
  393. module_exit(spufs_exit);
  394. MODULE_LICENSE("GPL");
  395. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");