fs_struct.c 3.3 KB

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