fs_struct.c 3.2 KB

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