inode.c 18 KB

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