fs_struct.c 3.9 KB

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