fs_struct.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <linux/export.h>
  2. #include <linux/sched/signal.h>
  3. #include <linux/sched/task.h>
  4. #include <linux/fs.h>
  5. #include <linux/path.h>
  6. #include <linux/slab.h>
  7. #include <linux/fs_struct.h>
  8. #include "internal.h"
  9. /*
  10. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  11. * It can block.
  12. */
  13. void set_fs_root(struct fs_struct *fs, const struct path *path)
  14. {
  15. struct path old_root;
  16. path_get(path);
  17. spin_lock(&fs->lock);
  18. write_seqcount_begin(&fs->seq);
  19. old_root = fs->root;
  20. fs->root = *path;
  21. write_seqcount_end(&fs->seq);
  22. spin_unlock(&fs->lock);
  23. if (old_root.dentry)
  24. path_put(&old_root);
  25. }
  26. /*
  27. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  28. * It can block.
  29. */
  30. void set_fs_pwd(struct fs_struct *fs, const struct path *path)
  31. {
  32. struct path old_pwd;
  33. path_get(path);
  34. spin_lock(&fs->lock);
  35. write_seqcount_begin(&fs->seq);
  36. old_pwd = fs->pwd;
  37. fs->pwd = *path;
  38. write_seqcount_end(&fs->seq);
  39. spin_unlock(&fs->lock);
  40. if (old_pwd.dentry)
  41. path_put(&old_pwd);
  42. }
  43. static inline int replace_path(struct path *p, const struct path *old, const struct path *new)
  44. {
  45. if (likely(p->dentry != old->dentry || p->mnt != old->mnt))
  46. return 0;
  47. *p = *new;
  48. return 1;
  49. }
  50. void chroot_fs_refs(const struct path *old_root, const struct path *new_root)
  51. {
  52. struct task_struct *g, *p;
  53. struct fs_struct *fs;
  54. int count = 0;
  55. read_lock(&tasklist_lock);
  56. do_each_thread(g, p) {
  57. task_lock(p);
  58. fs = p->fs;
  59. if (fs) {
  60. int hits = 0;
  61. spin_lock(&fs->lock);
  62. write_seqcount_begin(&fs->seq);
  63. hits += replace_path(&fs->root, old_root, new_root);
  64. hits += replace_path(&fs->pwd, old_root, new_root);
  65. write_seqcount_end(&fs->seq);
  66. while (hits--) {
  67. count++;
  68. path_get(new_root);
  69. }
  70. spin_unlock(&fs->lock);
  71. }
  72. task_unlock(p);
  73. } while_each_thread(g, p);
  74. read_unlock(&tasklist_lock);
  75. while (count--)
  76. path_put(old_root);
  77. }
  78. void free_fs_struct(struct fs_struct *fs)
  79. {
  80. path_put(&fs->root);
  81. path_put(&fs->pwd);
  82. kmem_cache_free(fs_cachep, fs);
  83. }
  84. void exit_fs(struct task_struct *tsk)
  85. {
  86. struct fs_struct *fs = tsk->fs;
  87. if (fs) {
  88. int kill;
  89. task_lock(tsk);
  90. spin_lock(&fs->lock);
  91. tsk->fs = NULL;
  92. kill = !--fs->users;
  93. spin_unlock(&fs->lock);
  94. task_unlock(tsk);
  95. if (kill)
  96. free_fs_struct(fs);
  97. }
  98. }
  99. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  100. {
  101. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  102. /* We don't need to lock fs - think why ;-) */
  103. if (fs) {
  104. fs->users = 1;
  105. fs->in_exec = 0;
  106. spin_lock_init(&fs->lock);
  107. seqcount_init(&fs->seq);
  108. fs->umask = old->umask;
  109. spin_lock(&old->lock);
  110. fs->root = old->root;
  111. path_get(&fs->root);
  112. fs->pwd = old->pwd;
  113. path_get(&fs->pwd);
  114. spin_unlock(&old->lock);
  115. }
  116. return fs;
  117. }
  118. int unshare_fs_struct(void)
  119. {
  120. struct fs_struct *fs = current->fs;
  121. struct fs_struct *new_fs = copy_fs_struct(fs);
  122. int kill;
  123. if (!new_fs)
  124. return -ENOMEM;
  125. task_lock(current);
  126. spin_lock(&fs->lock);
  127. kill = !--fs->users;
  128. current->fs = new_fs;
  129. spin_unlock(&fs->lock);
  130. task_unlock(current);
  131. if (kill)
  132. free_fs_struct(fs);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(unshare_fs_struct);
  136. int current_umask(void)
  137. {
  138. return current->fs->umask;
  139. }
  140. EXPORT_SYMBOL(current_umask);
  141. /* to be mentioned only in INIT_TASK */
  142. struct fs_struct init_fs = {
  143. .users = 1,
  144. .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
  145. .seq = SEQCNT_ZERO(init_fs.seq),
  146. .umask = 0022,
  147. };