inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_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. const 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->i_private = 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 void spufs_prune_dir(struct dentry *dir)
  117. {
  118. struct dentry *dentry, *tmp;
  119. mutex_lock(&dir->d_inode->i_mutex);
  120. list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
  121. spin_lock(&dcache_lock);
  122. spin_lock(&dentry->d_lock);
  123. if (!(d_unhashed(dentry)) && dentry->d_inode) {
  124. dget_locked(dentry);
  125. __d_drop(dentry);
  126. spin_unlock(&dentry->d_lock);
  127. simple_unlink(dir->d_inode, dentry);
  128. spin_unlock(&dcache_lock);
  129. dput(dentry);
  130. } else {
  131. spin_unlock(&dentry->d_lock);
  132. spin_unlock(&dcache_lock);
  133. }
  134. }
  135. shrink_dcache_parent(dir);
  136. mutex_unlock(&dir->d_inode->i_mutex);
  137. }
  138. /* Caller must hold root->i_mutex */
  139. static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
  140. {
  141. /* remove all entries */
  142. spufs_prune_dir(dir_dentry);
  143. return simple_rmdir(root, dir_dentry);
  144. }
  145. static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
  146. int mode, struct spu_context *ctx)
  147. {
  148. struct dentry *dentry;
  149. int ret;
  150. while (files->name && files->name[0]) {
  151. ret = -ENOMEM;
  152. dentry = d_alloc_name(dir, files->name);
  153. if (!dentry)
  154. goto out;
  155. ret = spufs_new_file(dir->d_sb, dentry, files->ops,
  156. files->mode & mode, ctx);
  157. if (ret)
  158. goto out;
  159. files++;
  160. }
  161. return 0;
  162. out:
  163. spufs_prune_dir(dir);
  164. return ret;
  165. }
  166. static int spufs_dir_close(struct inode *inode, struct file *file)
  167. {
  168. struct spu_context *ctx;
  169. struct inode *dir;
  170. struct dentry *dentry;
  171. int ret;
  172. dentry = file->f_dentry;
  173. dir = dentry->d_parent->d_inode;
  174. ctx = SPUFS_I(dentry->d_inode)->i_ctx;
  175. mutex_lock(&dir->i_mutex);
  176. ret = spufs_rmdir(dir, dentry);
  177. mutex_unlock(&dir->i_mutex);
  178. WARN_ON(ret);
  179. /* We have to give up the mm_struct */
  180. spu_forget(ctx);
  181. return dcache_dir_close(inode, file);
  182. }
  183. struct inode_operations spufs_dir_inode_operations = {
  184. .lookup = simple_lookup,
  185. };
  186. struct file_operations spufs_context_fops = {
  187. .open = dcache_dir_open,
  188. .release = spufs_dir_close,
  189. .llseek = dcache_dir_lseek,
  190. .read = generic_read_dir,
  191. .readdir = dcache_readdir,
  192. .fsync = simple_sync_file,
  193. };
  194. static int
  195. spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  196. {
  197. int ret;
  198. struct inode *inode;
  199. struct spu_context *ctx;
  200. ret = -ENOSPC;
  201. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  202. if (!inode)
  203. goto out;
  204. if (dir->i_mode & S_ISGID) {
  205. inode->i_gid = dir->i_gid;
  206. inode->i_mode &= S_ISGID;
  207. }
  208. ctx = alloc_spu_context();
  209. SPUFS_I(inode)->i_ctx = ctx;
  210. if (!ctx)
  211. goto out_iput;
  212. inode->i_op = &spufs_dir_inode_operations;
  213. inode->i_fop = &simple_dir_operations;
  214. ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
  215. if (ret)
  216. goto out_free_ctx;
  217. d_instantiate(dentry, inode);
  218. dget(dentry);
  219. dir->i_nlink++;
  220. dentry->d_inode->i_nlink++;
  221. goto out;
  222. out_free_ctx:
  223. put_spu_context(ctx);
  224. out_iput:
  225. iput(inode);
  226. out:
  227. return ret;
  228. }
  229. static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
  230. {
  231. int ret;
  232. struct file *filp;
  233. ret = get_unused_fd();
  234. if (ret < 0) {
  235. dput(dentry);
  236. mntput(mnt);
  237. goto out;
  238. }
  239. filp = dentry_open(dentry, mnt, O_RDONLY);
  240. if (IS_ERR(filp)) {
  241. put_unused_fd(ret);
  242. ret = PTR_ERR(filp);
  243. goto out;
  244. }
  245. filp->f_op = &spufs_context_fops;
  246. fd_install(ret, filp);
  247. out:
  248. return ret;
  249. }
  250. static struct file_system_type spufs_type;
  251. long spufs_create_thread(struct nameidata *nd,
  252. unsigned int flags, mode_t mode)
  253. {
  254. struct dentry *dentry;
  255. int ret;
  256. /* need to be at the root of spufs */
  257. ret = -EINVAL;
  258. if (nd->dentry->d_sb->s_type != &spufs_type ||
  259. nd->dentry != nd->dentry->d_sb->s_root)
  260. goto out;
  261. /* all flags are reserved */
  262. if (flags)
  263. goto out;
  264. dentry = lookup_create(nd, 1);
  265. ret = PTR_ERR(dentry);
  266. if (IS_ERR(dentry))
  267. goto out_dir;
  268. ret = -EEXIST;
  269. if (dentry->d_inode)
  270. goto out_dput;
  271. mode &= ~current->fs->umask;
  272. ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
  273. if (ret)
  274. goto out_dput;
  275. /*
  276. * get references for dget and mntget, will be released
  277. * in error path of *_open().
  278. */
  279. ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
  280. if (ret < 0) {
  281. WARN_ON(spufs_rmdir(nd->dentry->d_inode, dentry));
  282. mutex_unlock(&nd->dentry->d_inode->i_mutex);
  283. spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
  284. dput(dentry);
  285. goto out;
  286. }
  287. out_dput:
  288. dput(dentry);
  289. out_dir:
  290. mutex_unlock(&nd->dentry->d_inode->i_mutex);
  291. out:
  292. return ret;
  293. }
  294. /* File system initialization */
  295. enum {
  296. Opt_uid, Opt_gid, Opt_err,
  297. };
  298. static match_table_t spufs_tokens = {
  299. { Opt_uid, "uid=%d" },
  300. { Opt_gid, "gid=%d" },
  301. { Opt_err, NULL },
  302. };
  303. static int
  304. spufs_parse_options(char *options, struct inode *root)
  305. {
  306. char *p;
  307. substring_t args[MAX_OPT_ARGS];
  308. while ((p = strsep(&options, ",")) != NULL) {
  309. int token, option;
  310. if (!*p)
  311. continue;
  312. token = match_token(p, spufs_tokens, args);
  313. switch (token) {
  314. case Opt_uid:
  315. if (match_int(&args[0], &option))
  316. return 0;
  317. root->i_uid = option;
  318. break;
  319. case Opt_gid:
  320. if (match_int(&args[0], &option))
  321. return 0;
  322. root->i_gid = option;
  323. break;
  324. default:
  325. return 0;
  326. }
  327. }
  328. return 1;
  329. }
  330. static int
  331. spufs_create_root(struct super_block *sb, void *data)
  332. {
  333. struct inode *inode;
  334. int ret;
  335. ret = -ENOMEM;
  336. inode = spufs_new_inode(sb, S_IFDIR | 0775);
  337. if (!inode)
  338. goto out;
  339. inode->i_op = &spufs_dir_inode_operations;
  340. inode->i_fop = &simple_dir_operations;
  341. SPUFS_I(inode)->i_ctx = NULL;
  342. ret = -EINVAL;
  343. if (!spufs_parse_options(data, inode))
  344. goto out_iput;
  345. ret = -ENOMEM;
  346. sb->s_root = d_alloc_root(inode);
  347. if (!sb->s_root)
  348. goto out_iput;
  349. return 0;
  350. out_iput:
  351. iput(inode);
  352. out:
  353. return ret;
  354. }
  355. static int
  356. spufs_fill_super(struct super_block *sb, void *data, int silent)
  357. {
  358. static struct super_operations s_ops = {
  359. .alloc_inode = spufs_alloc_inode,
  360. .destroy_inode = spufs_destroy_inode,
  361. .statfs = simple_statfs,
  362. .delete_inode = spufs_delete_inode,
  363. .drop_inode = generic_delete_inode,
  364. };
  365. sb->s_maxbytes = MAX_LFS_FILESIZE;
  366. sb->s_blocksize = PAGE_CACHE_SIZE;
  367. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  368. sb->s_magic = SPUFS_MAGIC;
  369. sb->s_op = &s_ops;
  370. return spufs_create_root(sb, data);
  371. }
  372. static int
  373. spufs_get_sb(struct file_system_type *fstype, int flags,
  374. const char *name, void *data, struct vfsmount *mnt)
  375. {
  376. return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
  377. }
  378. static struct file_system_type spufs_type = {
  379. .owner = THIS_MODULE,
  380. .name = "spufs",
  381. .get_sb = spufs_get_sb,
  382. .kill_sb = kill_litter_super,
  383. };
  384. static int __init spufs_init(void)
  385. {
  386. int ret;
  387. ret = -ENOMEM;
  388. spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
  389. sizeof(struct spufs_inode_info), 0,
  390. SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
  391. if (!spufs_inode_cache)
  392. goto out;
  393. if (spu_sched_init() != 0) {
  394. kmem_cache_destroy(spufs_inode_cache);
  395. goto out;
  396. }
  397. ret = register_filesystem(&spufs_type);
  398. if (ret)
  399. goto out_cache;
  400. ret = register_spu_syscalls(&spufs_calls);
  401. if (ret)
  402. goto out_fs;
  403. return 0;
  404. out_fs:
  405. unregister_filesystem(&spufs_type);
  406. out_cache:
  407. kmem_cache_destroy(spufs_inode_cache);
  408. out:
  409. return ret;
  410. }
  411. module_init(spufs_init);
  412. static void __exit spufs_exit(void)
  413. {
  414. spu_sched_exit();
  415. unregister_spu_syscalls(&spufs_calls);
  416. unregister_filesystem(&spufs_type);
  417. kmem_cache_destroy(spufs_inode_cache);
  418. }
  419. module_exit(spufs_exit);
  420. MODULE_LICENSE("GPL");
  421. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");