inode.c 9.5 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. int err;
  143. /* remove all entries */
  144. err = 0;
  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. /* We have to give up the mm_struct */
  161. ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
  162. spu_forget(ctx);
  163. if (!err) {
  164. shrink_dcache_parent(dir_dentry);
  165. err = simple_rmdir(root, dir_dentry);
  166. }
  167. return err;
  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. down(&dir->i_sem);
  177. ret = spufs_rmdir(dir, file->f_dentry);
  178. WARN_ON(ret);
  179. up(&dir->i_sem);
  180. return dcache_dir_close(inode, file);
  181. }
  182. struct inode_operations spufs_dir_inode_operations = {
  183. .lookup = simple_lookup,
  184. };
  185. struct file_operations spufs_autodelete_dir_operations = {
  186. .open = dcache_dir_open,
  187. .release = spufs_dir_close,
  188. .llseek = dcache_dir_lseek,
  189. .read = generic_read_dir,
  190. .readdir = dcache_readdir,
  191. .fsync = simple_sync_file,
  192. };
  193. static int
  194. spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  195. {
  196. int ret;
  197. struct inode *inode;
  198. struct spu_context *ctx;
  199. ret = -ENOSPC;
  200. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  201. if (!inode)
  202. goto out;
  203. if (dir->i_mode & S_ISGID) {
  204. inode->i_gid = dir->i_gid;
  205. inode->i_mode &= S_ISGID;
  206. }
  207. ctx = alloc_spu_context(inode->i_mapping);
  208. SPUFS_I(inode)->i_ctx = ctx;
  209. if (!ctx)
  210. goto out_iput;
  211. inode->i_op = &spufs_dir_inode_operations;
  212. inode->i_fop = &simple_dir_operations;
  213. ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
  214. if (ret)
  215. goto out_free_ctx;
  216. d_instantiate(dentry, inode);
  217. dget(dentry);
  218. dir->i_nlink++;
  219. goto out;
  220. out_free_ctx:
  221. put_spu_context(ctx);
  222. out_iput:
  223. iput(inode);
  224. out:
  225. return ret;
  226. }
  227. long
  228. spufs_create_thread(struct nameidata *nd, const char *name,
  229. unsigned int flags, mode_t mode)
  230. {
  231. struct dentry *dentry;
  232. struct file *filp;
  233. int ret;
  234. /* need to be at the root of spufs */
  235. ret = -EINVAL;
  236. if (nd->dentry->d_sb->s_magic != SPUFS_MAGIC ||
  237. nd->dentry != nd->dentry->d_sb->s_root)
  238. goto out;
  239. dentry = lookup_create(nd, 1);
  240. ret = PTR_ERR(dentry);
  241. if (IS_ERR(dentry))
  242. goto out_dir;
  243. ret = -EEXIST;
  244. if (dentry->d_inode)
  245. goto out_dput;
  246. mode &= ~current->fs->umask;
  247. ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
  248. if (ret)
  249. goto out_dput;
  250. ret = get_unused_fd();
  251. if (ret < 0)
  252. goto out_dput;
  253. dentry->d_inode->i_nlink++;
  254. filp = filp_open(name, O_RDONLY, mode);
  255. if (IS_ERR(filp)) {
  256. // FIXME: remove directory again
  257. put_unused_fd(ret);
  258. ret = PTR_ERR(filp);
  259. } else {
  260. filp->f_op = &spufs_autodelete_dir_operations;
  261. fd_install(ret, filp);
  262. }
  263. out_dput:
  264. dput(dentry);
  265. out_dir:
  266. up(&nd->dentry->d_inode->i_sem);
  267. out:
  268. return ret;
  269. }
  270. /* File system initialization */
  271. enum {
  272. Opt_uid, Opt_gid, Opt_err,
  273. };
  274. static match_table_t spufs_tokens = {
  275. { Opt_uid, "uid=%d" },
  276. { Opt_gid, "gid=%d" },
  277. { Opt_err, NULL },
  278. };
  279. static int
  280. spufs_parse_options(char *options, struct inode *root)
  281. {
  282. char *p;
  283. substring_t args[MAX_OPT_ARGS];
  284. while ((p = strsep(&options, ",")) != NULL) {
  285. int token, option;
  286. if (!*p)
  287. continue;
  288. token = match_token(p, spufs_tokens, args);
  289. switch (token) {
  290. case Opt_uid:
  291. if (match_int(&args[0], &option))
  292. return 0;
  293. root->i_uid = option;
  294. break;
  295. case Opt_gid:
  296. if (match_int(&args[0], &option))
  297. return 0;
  298. root->i_gid = option;
  299. break;
  300. default:
  301. return 0;
  302. }
  303. }
  304. return 1;
  305. }
  306. static int
  307. spufs_create_root(struct super_block *sb, void *data)
  308. {
  309. struct inode *inode;
  310. int ret;
  311. ret = -ENOMEM;
  312. inode = spufs_new_inode(sb, S_IFDIR | 0775);
  313. if (!inode)
  314. goto out;
  315. inode->i_op = &spufs_dir_inode_operations;
  316. inode->i_fop = &simple_dir_operations;
  317. SPUFS_I(inode)->i_ctx = NULL;
  318. ret = -EINVAL;
  319. if (!spufs_parse_options(data, inode))
  320. goto out_iput;
  321. ret = -ENOMEM;
  322. sb->s_root = d_alloc_root(inode);
  323. if (!sb->s_root)
  324. goto out_iput;
  325. return 0;
  326. out_iput:
  327. iput(inode);
  328. out:
  329. return ret;
  330. }
  331. static int
  332. spufs_fill_super(struct super_block *sb, void *data, int silent)
  333. {
  334. static struct super_operations s_ops = {
  335. .alloc_inode = spufs_alloc_inode,
  336. .destroy_inode = spufs_destroy_inode,
  337. .statfs = simple_statfs,
  338. .delete_inode = spufs_delete_inode,
  339. .drop_inode = generic_delete_inode,
  340. };
  341. sb->s_maxbytes = MAX_LFS_FILESIZE;
  342. sb->s_blocksize = PAGE_CACHE_SIZE;
  343. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  344. sb->s_magic = SPUFS_MAGIC;
  345. sb->s_op = &s_ops;
  346. return spufs_create_root(sb, data);
  347. }
  348. static struct super_block *
  349. spufs_get_sb(struct file_system_type *fstype, int flags,
  350. const char *name, void *data)
  351. {
  352. return get_sb_single(fstype, flags, data, spufs_fill_super);
  353. }
  354. static struct file_system_type spufs_type = {
  355. .owner = THIS_MODULE,
  356. .name = "spufs",
  357. .get_sb = spufs_get_sb,
  358. .kill_sb = kill_litter_super,
  359. };
  360. static int spufs_init(void)
  361. {
  362. int ret;
  363. ret = -ENOMEM;
  364. spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
  365. sizeof(struct spufs_inode_info), 0,
  366. SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
  367. if (!spufs_inode_cache)
  368. goto out;
  369. if (spu_sched_init() != 0) {
  370. kmem_cache_destroy(spufs_inode_cache);
  371. goto out;
  372. }
  373. ret = register_filesystem(&spufs_type);
  374. if (ret)
  375. goto out_cache;
  376. ret = register_spu_syscalls(&spufs_calls);
  377. if (ret)
  378. goto out_fs;
  379. return 0;
  380. out_fs:
  381. unregister_filesystem(&spufs_type);
  382. out_cache:
  383. kmem_cache_destroy(spufs_inode_cache);
  384. out:
  385. return ret;
  386. }
  387. module_init(spufs_init);
  388. static void spufs_exit(void)
  389. {
  390. spu_sched_exit();
  391. unregister_spu_syscalls(&spufs_calls);
  392. unregister_filesystem(&spufs_type);
  393. kmem_cache_destroy(spufs_inode_cache);
  394. }
  395. module_exit(spufs_exit);
  396. MODULE_LICENSE("GPL");
  397. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");