fs_struct.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <linux/export.h>
  2. #include <linux/sched.h>
  3. #include <linux/fs.h>
  4. #include <linux/path.h>
  5. #include <linux/slab.h>
  6. #include <linux/fs_struct.h>
  7. #include "internal.h"
  8. /*
  9. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  10. * It can block.
  11. */
  12. void set_fs_root(struct fs_struct *fs, const struct path *path)
  13. {
  14. struct path old_root;
  15. path_get(path);
  16. spin_lock(&fs->lock);
  17. write_seqcount_begin(&fs->seq);
  18. old_root = fs->root;
  19. fs->root = *path;
  20. write_seqcount_end(&fs->seq);
  21. spin_unlock(&fs->lock);
  22. if (old_root.dentry)
  23. path_put(&old_root);
  24. }
  25. /*
  26. * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
  27. * It can block.
  28. */
  29. void set_fs_pwd(struct fs_struct *fs, const struct path *path)
  30. {
  31. struct path old_pwd;
  32. path_get(path);
  33. spin_lock(&fs->lock);
  34. write_seqcount_begin(&fs->seq);
  35. old_pwd = fs->pwd;
  36. fs->pwd = *path;
  37. write_seqcount_end(&fs->seq);
  38. spin_unlock(&fs->lock);
  39. if (old_pwd.dentry)
  40. path_put(&old_pwd);
  41. }
  42. EXPORT_SYMBOL(set_fs_pwd);
  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. EXPORT_SYMBOL(free_fs_struct);
  85. void exit_fs(struct task_struct *tsk)
  86. {
  87. struct fs_struct *fs = tsk->fs;
  88. if (fs) {
  89. int kill;
  90. task_lock(tsk);
  91. spin_lock(&fs->lock);
  92. tsk->fs = NULL;
  93. kill = !--fs->users;
  94. spin_unlock(&fs->lock);
  95. task_unlock(tsk);
  96. if (kill)
  97. free_fs_struct(fs);
  98. }
  99. }
  100. struct fs_struct *copy_fs_struct(struct fs_struct *old)
  101. {
  102. struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
  103. /* We don't need to lock fs - think why ;-) */
  104. if (fs) {
  105. fs->users = 1;
  106. fs->in_exec = 0;
  107. spin_lock_init(&fs->lock);
  108. seqcount_init(&fs->seq);
  109. fs->umask = old->umask;
  110. spin_lock(&old->lock);
  111. fs->root = old->root;
  112. path_get(&fs->root);
  113. fs->pwd = old->pwd;
  114. path_get(&fs->pwd);
  115. spin_unlock(&old->lock);
  116. }
  117. return fs;
  118. }
  119. EXPORT_SYMBOL_GPL(copy_fs_struct);
  120. int unshare_fs_struct(void)
  121. {
  122. struct fs_struct *fs = current->fs;
  123. struct fs_struct *new_fs = copy_fs_struct(fs);
  124. int kill;
  125. if (!new_fs)
  126. return -ENOMEM;
  127. task_lock(current);
  128. spin_lock(&fs->lock);
  129. kill = !--fs->users;
  130. current->fs = new_fs;
  131. spin_unlock(&fs->lock);
  132. task_unlock(current);
  133. if (kill)
  134. free_fs_struct(fs);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(unshare_fs_struct);
  138. int current_umask(void)
  139. {
  140. return current->fs->umask;
  141. }
  142. EXPORT_SYMBOL(current_umask);
  143. /* to be mentioned only in INIT_TASK */
  144. struct fs_struct init_fs = {
  145. .users = 1,
  146. .lock = __SPIN_LOCK_UNLOCKED(init_fs.lock),
  147. .seq = SEQCNT_ZERO(init_fs.seq),
  148. .umask = 0022,
  149. };