inode.c 18 KB

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