inode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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/fsnotify.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/init.h>
  27. #include <linux/ioctl.h>
  28. #include <linux/module.h>
  29. #include <linux/mount.h>
  30. #include <linux/namei.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/poll.h>
  33. #include <linux/slab.h>
  34. #include <linux/parser.h>
  35. #include <asm/prom.h>
  36. #include <asm/spu.h>
  37. #include <asm/spu_priv1.h>
  38. #include <asm/uaccess.h>
  39. #include "spufs.h"
  40. struct spufs_sb_info {
  41. int debug;
  42. };
  43. static struct kmem_cache *spufs_inode_cache;
  44. char *isolated_loader;
  45. static int isolated_loader_size;
  46. static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
  47. {
  48. return sb->s_fs_info;
  49. }
  50. static struct inode *
  51. spufs_alloc_inode(struct super_block *sb)
  52. {
  53. struct spufs_inode_info *ei;
  54. ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
  55. if (!ei)
  56. return NULL;
  57. ei->i_gang = NULL;
  58. ei->i_ctx = NULL;
  59. ei->i_openers = 0;
  60. return &ei->vfs_inode;
  61. }
  62. static void
  63. spufs_destroy_inode(struct inode *inode)
  64. {
  65. kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
  66. }
  67. static void
  68. spufs_init_once(void *p)
  69. {
  70. struct spufs_inode_info *ei = p;
  71. inode_init_once(&ei->vfs_inode);
  72. }
  73. static struct inode *
  74. spufs_new_inode(struct super_block *sb, int mode)
  75. {
  76. struct inode *inode;
  77. inode = new_inode(sb);
  78. if (!inode)
  79. goto out;
  80. inode->i_mode = mode;
  81. inode->i_uid = current_fsuid();
  82. inode->i_gid = current_fsgid();
  83. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  84. out:
  85. return inode;
  86. }
  87. static int
  88. spufs_setattr(struct dentry *dentry, struct iattr *attr)
  89. {
  90. struct inode *inode = dentry->d_inode;
  91. if ((attr->ia_valid & ATTR_SIZE) &&
  92. (attr->ia_size != inode->i_size))
  93. return -EINVAL;
  94. setattr_copy(inode, attr);
  95. mark_inode_dirty(inode);
  96. return 0;
  97. }
  98. static int
  99. spufs_new_file(struct super_block *sb, struct dentry *dentry,
  100. const struct file_operations *fops, int mode,
  101. size_t size, struct spu_context *ctx)
  102. {
  103. static const struct inode_operations spufs_file_iops = {
  104. .setattr = spufs_setattr,
  105. };
  106. struct inode *inode;
  107. int ret;
  108. ret = -ENOSPC;
  109. inode = spufs_new_inode(sb, S_IFREG | mode);
  110. if (!inode)
  111. goto out;
  112. ret = 0;
  113. inode->i_op = &spufs_file_iops;
  114. inode->i_fop = fops;
  115. inode->i_size = size;
  116. inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
  117. d_add(dentry, inode);
  118. out:
  119. return ret;
  120. }
  121. static void
  122. spufs_evict_inode(struct inode *inode)
  123. {
  124. struct spufs_inode_info *ei = SPUFS_I(inode);
  125. end_writeback(inode);
  126. if (ei->i_ctx)
  127. put_spu_context(ei->i_ctx);
  128. if (ei->i_gang)
  129. put_spu_gang(ei->i_gang);
  130. }
  131. static void spufs_prune_dir(struct dentry *dir)
  132. {
  133. struct dentry *dentry, *tmp;
  134. mutex_lock(&dir->d_inode->i_mutex);
  135. list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
  136. spin_lock(&dcache_lock);
  137. spin_lock(&dentry->d_lock);
  138. if (!(d_unhashed(dentry)) && dentry->d_inode) {
  139. dget_locked(dentry);
  140. __d_drop(dentry);
  141. spin_unlock(&dentry->d_lock);
  142. simple_unlink(dir->d_inode, dentry);
  143. spin_unlock(&dcache_lock);
  144. dput(dentry);
  145. } else {
  146. spin_unlock(&dentry->d_lock);
  147. spin_unlock(&dcache_lock);
  148. }
  149. }
  150. shrink_dcache_parent(dir);
  151. mutex_unlock(&dir->d_inode->i_mutex);
  152. }
  153. /* Caller must hold parent->i_mutex */
  154. static int spufs_rmdir(struct inode *parent, struct dentry *dir)
  155. {
  156. /* remove all entries */
  157. spufs_prune_dir(dir);
  158. d_drop(dir);
  159. return simple_rmdir(parent, dir);
  160. }
  161. static int spufs_fill_dir(struct dentry *dir,
  162. const struct spufs_tree_descr *files, int mode,
  163. struct spu_context *ctx)
  164. {
  165. struct dentry *dentry, *tmp;
  166. int ret;
  167. while (files->name && files->name[0]) {
  168. ret = -ENOMEM;
  169. dentry = d_alloc_name(dir, files->name);
  170. if (!dentry)
  171. goto out;
  172. ret = spufs_new_file(dir->d_sb, dentry, files->ops,
  173. files->mode & mode, files->size, ctx);
  174. if (ret)
  175. goto out;
  176. files++;
  177. }
  178. return 0;
  179. out:
  180. /*
  181. * remove all children from dir. dir->inode is not set so don't
  182. * just simply use spufs_prune_dir() and panic afterwards :)
  183. * dput() looks like it will do the right thing:
  184. * - dec parent's ref counter
  185. * - remove child from parent's child list
  186. * - free child's inode if possible
  187. * - free child
  188. */
  189. list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
  190. dput(dentry);
  191. }
  192. shrink_dcache_parent(dir);
  193. return ret;
  194. }
  195. static int spufs_dir_close(struct inode *inode, struct file *file)
  196. {
  197. struct spu_context *ctx;
  198. struct inode *parent;
  199. struct dentry *dir;
  200. int ret;
  201. dir = file->f_path.dentry;
  202. parent = dir->d_parent->d_inode;
  203. ctx = SPUFS_I(dir->d_inode)->i_ctx;
  204. mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
  205. ret = spufs_rmdir(parent, dir);
  206. mutex_unlock(&parent->i_mutex);
  207. WARN_ON(ret);
  208. /* We have to give up the mm_struct */
  209. spu_forget(ctx);
  210. return dcache_dir_close(inode, file);
  211. }
  212. const struct file_operations spufs_context_fops = {
  213. .open = dcache_dir_open,
  214. .release = spufs_dir_close,
  215. .llseek = dcache_dir_lseek,
  216. .read = generic_read_dir,
  217. .readdir = dcache_readdir,
  218. .fsync = noop_fsync,
  219. };
  220. EXPORT_SYMBOL_GPL(spufs_context_fops);
  221. static int
  222. spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
  223. int mode)
  224. {
  225. int ret;
  226. struct inode *inode;
  227. struct spu_context *ctx;
  228. ret = -ENOSPC;
  229. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  230. if (!inode)
  231. goto out;
  232. if (dir->i_mode & S_ISGID) {
  233. inode->i_gid = dir->i_gid;
  234. inode->i_mode &= S_ISGID;
  235. }
  236. ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
  237. SPUFS_I(inode)->i_ctx = ctx;
  238. if (!ctx)
  239. goto out_iput;
  240. ctx->flags = flags;
  241. inode->i_op = &simple_dir_inode_operations;
  242. inode->i_fop = &simple_dir_operations;
  243. if (flags & SPU_CREATE_NOSCHED)
  244. ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
  245. mode, ctx);
  246. else
  247. ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
  248. if (ret)
  249. goto out_free_ctx;
  250. if (spufs_get_sb_info(dir->i_sb)->debug)
  251. ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
  252. mode, ctx);
  253. if (ret)
  254. goto out_free_ctx;
  255. d_instantiate(dentry, inode);
  256. dget(dentry);
  257. inc_nlink(dir);
  258. inc_nlink(dentry->d_inode);
  259. goto out;
  260. out_free_ctx:
  261. spu_forget(ctx);
  262. put_spu_context(ctx);
  263. out_iput:
  264. iput(inode);
  265. out:
  266. return ret;
  267. }
  268. static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
  269. {
  270. int ret;
  271. struct file *filp;
  272. ret = get_unused_fd();
  273. if (ret < 0) {
  274. dput(dentry);
  275. mntput(mnt);
  276. goto out;
  277. }
  278. filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
  279. if (IS_ERR(filp)) {
  280. put_unused_fd(ret);
  281. ret = PTR_ERR(filp);
  282. goto out;
  283. }
  284. filp->f_op = &spufs_context_fops;
  285. fd_install(ret, filp);
  286. out:
  287. return ret;
  288. }
  289. static struct spu_context *
  290. spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
  291. struct file *filp)
  292. {
  293. struct spu_context *tmp, *neighbor, *err;
  294. int count, node;
  295. int aff_supp;
  296. aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
  297. struct spu, cbe_list))->aff_list);
  298. if (!aff_supp)
  299. return ERR_PTR(-EINVAL);
  300. if (flags & SPU_CREATE_GANG)
  301. return ERR_PTR(-EINVAL);
  302. if (flags & SPU_CREATE_AFFINITY_MEM &&
  303. gang->aff_ref_ctx &&
  304. gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
  305. return ERR_PTR(-EEXIST);
  306. if (gang->aff_flags & AFF_MERGED)
  307. return ERR_PTR(-EBUSY);
  308. neighbor = NULL;
  309. if (flags & SPU_CREATE_AFFINITY_SPU) {
  310. if (!filp || filp->f_op != &spufs_context_fops)
  311. return ERR_PTR(-EINVAL);
  312. neighbor = get_spu_context(
  313. SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
  314. if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
  315. !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
  316. !list_entry(neighbor->aff_list.next, struct spu_context,
  317. aff_list)->aff_head) {
  318. err = ERR_PTR(-EEXIST);
  319. goto out_put_neighbor;
  320. }
  321. if (gang != neighbor->gang) {
  322. err = ERR_PTR(-EINVAL);
  323. goto out_put_neighbor;
  324. }
  325. count = 1;
  326. list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
  327. count++;
  328. if (list_empty(&neighbor->aff_list))
  329. count++;
  330. for (node = 0; node < MAX_NUMNODES; node++) {
  331. if ((cbe_spu_info[node].n_spus - atomic_read(
  332. &cbe_spu_info[node].reserved_spus)) >= count)
  333. break;
  334. }
  335. if (node == MAX_NUMNODES) {
  336. err = ERR_PTR(-EEXIST);
  337. goto out_put_neighbor;
  338. }
  339. }
  340. return neighbor;
  341. out_put_neighbor:
  342. put_spu_context(neighbor);
  343. return err;
  344. }
  345. static void
  346. spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
  347. struct spu_context *neighbor)
  348. {
  349. if (flags & SPU_CREATE_AFFINITY_MEM)
  350. ctx->gang->aff_ref_ctx = ctx;
  351. if (flags & SPU_CREATE_AFFINITY_SPU) {
  352. if (list_empty(&neighbor->aff_list)) {
  353. list_add_tail(&neighbor->aff_list,
  354. &ctx->gang->aff_list_head);
  355. neighbor->aff_head = 1;
  356. }
  357. if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
  358. || list_entry(neighbor->aff_list.next, struct spu_context,
  359. aff_list)->aff_head) {
  360. list_add(&ctx->aff_list, &neighbor->aff_list);
  361. } else {
  362. list_add_tail(&ctx->aff_list, &neighbor->aff_list);
  363. if (neighbor->aff_head) {
  364. neighbor->aff_head = 0;
  365. ctx->aff_head = 1;
  366. }
  367. }
  368. if (!ctx->gang->aff_ref_ctx)
  369. ctx->gang->aff_ref_ctx = ctx;
  370. }
  371. }
  372. static int
  373. spufs_create_context(struct inode *inode, struct dentry *dentry,
  374. struct vfsmount *mnt, int flags, int mode,
  375. struct file *aff_filp)
  376. {
  377. int ret;
  378. int affinity;
  379. struct spu_gang *gang;
  380. struct spu_context *neighbor;
  381. ret = -EPERM;
  382. if ((flags & SPU_CREATE_NOSCHED) &&
  383. !capable(CAP_SYS_NICE))
  384. goto out_unlock;
  385. ret = -EINVAL;
  386. if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
  387. == SPU_CREATE_ISOLATE)
  388. goto out_unlock;
  389. ret = -ENODEV;
  390. if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
  391. goto out_unlock;
  392. gang = NULL;
  393. neighbor = NULL;
  394. affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
  395. if (affinity) {
  396. gang = SPUFS_I(inode)->i_gang;
  397. ret = -EINVAL;
  398. if (!gang)
  399. goto out_unlock;
  400. mutex_lock(&gang->aff_mutex);
  401. neighbor = spufs_assert_affinity(flags, gang, aff_filp);
  402. if (IS_ERR(neighbor)) {
  403. ret = PTR_ERR(neighbor);
  404. goto out_aff_unlock;
  405. }
  406. }
  407. ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
  408. if (ret)
  409. goto out_aff_unlock;
  410. if (affinity) {
  411. spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
  412. neighbor);
  413. if (neighbor)
  414. put_spu_context(neighbor);
  415. }
  416. /*
  417. * get references for dget and mntget, will be released
  418. * in error path of *_open().
  419. */
  420. ret = spufs_context_open(dget(dentry), mntget(mnt));
  421. if (ret < 0) {
  422. WARN_ON(spufs_rmdir(inode, dentry));
  423. if (affinity)
  424. mutex_unlock(&gang->aff_mutex);
  425. mutex_unlock(&inode->i_mutex);
  426. spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
  427. goto out;
  428. }
  429. out_aff_unlock:
  430. if (affinity)
  431. mutex_unlock(&gang->aff_mutex);
  432. out_unlock:
  433. mutex_unlock(&inode->i_mutex);
  434. out:
  435. dput(dentry);
  436. return ret;
  437. }
  438. static int
  439. spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
  440. {
  441. int ret;
  442. struct inode *inode;
  443. struct spu_gang *gang;
  444. ret = -ENOSPC;
  445. inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
  446. if (!inode)
  447. goto out;
  448. ret = 0;
  449. if (dir->i_mode & S_ISGID) {
  450. inode->i_gid = dir->i_gid;
  451. inode->i_mode &= S_ISGID;
  452. }
  453. gang = alloc_spu_gang();
  454. SPUFS_I(inode)->i_ctx = NULL;
  455. SPUFS_I(inode)->i_gang = gang;
  456. if (!gang)
  457. goto out_iput;
  458. inode->i_op = &simple_dir_inode_operations;
  459. inode->i_fop = &simple_dir_operations;
  460. d_instantiate(dentry, inode);
  461. inc_nlink(dir);
  462. inc_nlink(dentry->d_inode);
  463. return ret;
  464. out_iput:
  465. iput(inode);
  466. out:
  467. return ret;
  468. }
  469. static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
  470. {
  471. int ret;
  472. struct file *filp;
  473. ret = get_unused_fd();
  474. if (ret < 0) {
  475. dput(dentry);
  476. mntput(mnt);
  477. goto out;
  478. }
  479. filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
  480. if (IS_ERR(filp)) {
  481. put_unused_fd(ret);
  482. ret = PTR_ERR(filp);
  483. goto out;
  484. }
  485. filp->f_op = &simple_dir_operations;
  486. fd_install(ret, filp);
  487. out:
  488. return ret;
  489. }
  490. static int spufs_create_gang(struct inode *inode,
  491. struct dentry *dentry,
  492. struct vfsmount *mnt, int mode)
  493. {
  494. int ret;
  495. ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
  496. if (ret)
  497. goto out;
  498. /*
  499. * get references for dget and mntget, will be released
  500. * in error path of *_open().
  501. */
  502. ret = spufs_gang_open(dget(dentry), mntget(mnt));
  503. if (ret < 0) {
  504. int err = simple_rmdir(inode, dentry);
  505. WARN_ON(err);
  506. }
  507. out:
  508. mutex_unlock(&inode->i_mutex);
  509. dput(dentry);
  510. return ret;
  511. }
  512. static struct file_system_type spufs_type;
  513. long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
  514. struct file *filp)
  515. {
  516. struct dentry *dentry;
  517. int ret;
  518. ret = -EINVAL;
  519. /* check if we are on spufs */
  520. if (nd->path.dentry->d_sb->s_type != &spufs_type)
  521. goto out;
  522. /* don't accept undefined flags */
  523. if (flags & (~SPU_CREATE_FLAG_ALL))
  524. goto out;
  525. /* only threads can be underneath a gang */
  526. if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
  527. if ((flags & SPU_CREATE_GANG) ||
  528. !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
  529. goto out;
  530. }
  531. dentry = lookup_create(nd, 1);
  532. ret = PTR_ERR(dentry);
  533. if (IS_ERR(dentry))
  534. goto out_dir;
  535. mode &= ~current_umask();
  536. if (flags & SPU_CREATE_GANG)
  537. ret = spufs_create_gang(nd->path.dentry->d_inode,
  538. dentry, nd->path.mnt, mode);
  539. else
  540. ret = spufs_create_context(nd->path.dentry->d_inode,
  541. dentry, nd->path.mnt, flags, mode,
  542. filp);
  543. if (ret >= 0)
  544. fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
  545. return ret;
  546. out_dir:
  547. mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
  548. out:
  549. return ret;
  550. }
  551. /* File system initialization */
  552. enum {
  553. Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
  554. };
  555. static const match_table_t spufs_tokens = {
  556. { Opt_uid, "uid=%d" },
  557. { Opt_gid, "gid=%d" },
  558. { Opt_mode, "mode=%o" },
  559. { Opt_debug, "debug" },
  560. { Opt_err, NULL },
  561. };
  562. static int
  563. spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
  564. {
  565. char *p;
  566. substring_t args[MAX_OPT_ARGS];
  567. while ((p = strsep(&options, ",")) != NULL) {
  568. int token, option;
  569. if (!*p)
  570. continue;
  571. token = match_token(p, spufs_tokens, args);
  572. switch (token) {
  573. case Opt_uid:
  574. if (match_int(&args[0], &option))
  575. return 0;
  576. root->i_uid = option;
  577. break;
  578. case Opt_gid:
  579. if (match_int(&args[0], &option))
  580. return 0;
  581. root->i_gid = option;
  582. break;
  583. case Opt_mode:
  584. if (match_octal(&args[0], &option))
  585. return 0;
  586. root->i_mode = option | S_IFDIR;
  587. break;
  588. case Opt_debug:
  589. spufs_get_sb_info(sb)->debug = 1;
  590. break;
  591. default:
  592. return 0;
  593. }
  594. }
  595. return 1;
  596. }
  597. static void spufs_exit_isolated_loader(void)
  598. {
  599. free_pages((unsigned long) isolated_loader,
  600. get_order(isolated_loader_size));
  601. }
  602. static void
  603. spufs_init_isolated_loader(void)
  604. {
  605. struct device_node *dn;
  606. const char *loader;
  607. int size;
  608. dn = of_find_node_by_path("/spu-isolation");
  609. if (!dn)
  610. return;
  611. loader = of_get_property(dn, "loader", &size);
  612. if (!loader)
  613. return;
  614. /* the loader must be align on a 16 byte boundary */
  615. isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
  616. if (!isolated_loader)
  617. return;
  618. isolated_loader_size = size;
  619. memcpy(isolated_loader, loader, size);
  620. printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
  621. }
  622. static int
  623. spufs_create_root(struct super_block *sb, void *data)
  624. {
  625. struct inode *inode;
  626. int ret;
  627. ret = -ENODEV;
  628. if (!spu_management_ops)
  629. goto out;
  630. ret = -ENOMEM;
  631. inode = spufs_new_inode(sb, S_IFDIR | 0775);
  632. if (!inode)
  633. goto out;
  634. inode->i_op = &simple_dir_inode_operations;
  635. inode->i_fop = &simple_dir_operations;
  636. SPUFS_I(inode)->i_ctx = NULL;
  637. inc_nlink(inode);
  638. ret = -EINVAL;
  639. if (!spufs_parse_options(sb, data, inode))
  640. goto out_iput;
  641. ret = -ENOMEM;
  642. sb->s_root = d_alloc_root(inode);
  643. if (!sb->s_root)
  644. goto out_iput;
  645. return 0;
  646. out_iput:
  647. iput(inode);
  648. out:
  649. return ret;
  650. }
  651. static int
  652. spufs_fill_super(struct super_block *sb, void *data, int silent)
  653. {
  654. struct spufs_sb_info *info;
  655. static const struct super_operations s_ops = {
  656. .alloc_inode = spufs_alloc_inode,
  657. .destroy_inode = spufs_destroy_inode,
  658. .statfs = simple_statfs,
  659. .evict_inode = spufs_evict_inode,
  660. .show_options = generic_show_options,
  661. };
  662. save_mount_options(sb, data);
  663. info = kzalloc(sizeof(*info), GFP_KERNEL);
  664. if (!info)
  665. return -ENOMEM;
  666. sb->s_maxbytes = MAX_LFS_FILESIZE;
  667. sb->s_blocksize = PAGE_CACHE_SIZE;
  668. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  669. sb->s_magic = SPUFS_MAGIC;
  670. sb->s_op = &s_ops;
  671. sb->s_fs_info = info;
  672. return spufs_create_root(sb, data);
  673. }
  674. static int
  675. spufs_get_sb(struct file_system_type *fstype, int flags,
  676. const char *name, void *data, struct vfsmount *mnt)
  677. {
  678. return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
  679. }
  680. static struct file_system_type spufs_type = {
  681. .owner = THIS_MODULE,
  682. .name = "spufs",
  683. .get_sb = spufs_get_sb,
  684. .kill_sb = kill_litter_super,
  685. };
  686. static int __init spufs_init(void)
  687. {
  688. int ret;
  689. ret = -ENODEV;
  690. if (!spu_management_ops)
  691. goto out;
  692. ret = -ENOMEM;
  693. spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
  694. sizeof(struct spufs_inode_info), 0,
  695. SLAB_HWCACHE_ALIGN, spufs_init_once);
  696. if (!spufs_inode_cache)
  697. goto out;
  698. ret = spu_sched_init();
  699. if (ret)
  700. goto out_cache;
  701. ret = register_filesystem(&spufs_type);
  702. if (ret)
  703. goto out_sched;
  704. ret = register_spu_syscalls(&spufs_calls);
  705. if (ret)
  706. goto out_fs;
  707. spufs_init_isolated_loader();
  708. return 0;
  709. out_fs:
  710. unregister_filesystem(&spufs_type);
  711. out_sched:
  712. spu_sched_exit();
  713. out_cache:
  714. kmem_cache_destroy(spufs_inode_cache);
  715. out:
  716. return ret;
  717. }
  718. module_init(spufs_init);
  719. static void __exit spufs_exit(void)
  720. {
  721. spu_sched_exit();
  722. spufs_exit_isolated_loader();
  723. unregister_spu_syscalls(&spufs_calls);
  724. unregister_filesystem(&spufs_type);
  725. kmem_cache_destroy(spufs_inode_cache);
  726. }
  727. module_exit(spufs_exit);
  728. MODULE_LICENSE("GPL");
  729. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");