inode.c 10 KB

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