dnotify.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Directory notifications for Linux.
  3. *
  4. * Copyright (C) 2000,2001,2002 Stephen Rothwell
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/dnotify.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/slab.h>
  23. int dir_notify_enable __read_mostly = 1;
  24. static struct kmem_cache *dn_cache __read_mostly;
  25. static void redo_inode_mask(struct inode *inode)
  26. {
  27. unsigned long new_mask;
  28. struct dnotify_struct *dn;
  29. new_mask = 0;
  30. for (dn = inode->i_dnotify; dn != NULL; dn = dn->dn_next)
  31. new_mask |= dn->dn_mask & ~DN_MULTISHOT;
  32. inode->i_dnotify_mask = new_mask;
  33. }
  34. void dnotify_flush(struct file *filp, fl_owner_t id)
  35. {
  36. struct dnotify_struct *dn;
  37. struct dnotify_struct **prev;
  38. struct inode *inode;
  39. inode = filp->f_path.dentry->d_inode;
  40. if (!S_ISDIR(inode->i_mode))
  41. return;
  42. spin_lock(&inode->i_lock);
  43. prev = &inode->i_dnotify;
  44. while ((dn = *prev) != NULL) {
  45. if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
  46. *prev = dn->dn_next;
  47. redo_inode_mask(inode);
  48. kmem_cache_free(dn_cache, dn);
  49. break;
  50. }
  51. prev = &dn->dn_next;
  52. }
  53. spin_unlock(&inode->i_lock);
  54. }
  55. int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
  56. {
  57. struct dnotify_struct *dn;
  58. struct dnotify_struct *odn;
  59. struct dnotify_struct **prev;
  60. struct inode *inode;
  61. fl_owner_t id = current->files;
  62. int error = 0;
  63. if ((arg & ~DN_MULTISHOT) == 0) {
  64. dnotify_flush(filp, id);
  65. return 0;
  66. }
  67. if (!dir_notify_enable)
  68. return -EINVAL;
  69. inode = filp->f_path.dentry->d_inode;
  70. if (!S_ISDIR(inode->i_mode))
  71. return -ENOTDIR;
  72. dn = kmem_cache_alloc(dn_cache, GFP_KERNEL);
  73. if (dn == NULL)
  74. return -ENOMEM;
  75. spin_lock(&inode->i_lock);
  76. prev = &inode->i_dnotify;
  77. while ((odn = *prev) != NULL) {
  78. if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
  79. odn->dn_fd = fd;
  80. odn->dn_mask |= arg;
  81. inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
  82. goto out_free;
  83. }
  84. prev = &odn->dn_next;
  85. }
  86. error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  87. if (error)
  88. goto out_free;
  89. dn->dn_mask = arg;
  90. dn->dn_fd = fd;
  91. dn->dn_filp = filp;
  92. dn->dn_owner = id;
  93. inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
  94. dn->dn_next = inode->i_dnotify;
  95. inode->i_dnotify = dn;
  96. spin_unlock(&inode->i_lock);
  97. if (filp->f_op && filp->f_op->dir_notify)
  98. return filp->f_op->dir_notify(filp, arg);
  99. return 0;
  100. out_free:
  101. spin_unlock(&inode->i_lock);
  102. kmem_cache_free(dn_cache, dn);
  103. return error;
  104. }
  105. void __inode_dir_notify(struct inode *inode, unsigned long event)
  106. {
  107. struct dnotify_struct * dn;
  108. struct dnotify_struct **prev;
  109. struct fown_struct * fown;
  110. int changed = 0;
  111. spin_lock(&inode->i_lock);
  112. prev = &inode->i_dnotify;
  113. while ((dn = *prev) != NULL) {
  114. if ((dn->dn_mask & event) == 0) {
  115. prev = &dn->dn_next;
  116. continue;
  117. }
  118. fown = &dn->dn_filp->f_owner;
  119. send_sigio(fown, dn->dn_fd, POLL_MSG);
  120. if (dn->dn_mask & DN_MULTISHOT)
  121. prev = &dn->dn_next;
  122. else {
  123. *prev = dn->dn_next;
  124. changed = 1;
  125. kmem_cache_free(dn_cache, dn);
  126. }
  127. }
  128. if (changed)
  129. redo_inode_mask(inode);
  130. spin_unlock(&inode->i_lock);
  131. }
  132. EXPORT_SYMBOL(__inode_dir_notify);
  133. /*
  134. * This is hopelessly wrong, but unfixable without API changes. At
  135. * least it doesn't oops the kernel...
  136. *
  137. * To safely access ->d_parent we need to keep d_move away from it. Use the
  138. * dentry's d_lock for this.
  139. */
  140. void dnotify_parent(struct dentry *dentry, unsigned long event)
  141. {
  142. struct dentry *parent;
  143. if (!dir_notify_enable)
  144. return;
  145. spin_lock(&dentry->d_lock);
  146. parent = dentry->d_parent;
  147. if (parent->d_inode->i_dnotify_mask & event) {
  148. dget(parent);
  149. spin_unlock(&dentry->d_lock);
  150. __inode_dir_notify(parent->d_inode, event);
  151. dput(parent);
  152. } else {
  153. spin_unlock(&dentry->d_lock);
  154. }
  155. }
  156. EXPORT_SYMBOL_GPL(dnotify_parent);
  157. static int __init dnotify_init(void)
  158. {
  159. dn_cache = kmem_cache_create("dnotify_cache",
  160. sizeof(struct dnotify_struct), 0, SLAB_PANIC, NULL, NULL);
  161. return 0;
  162. }
  163. module_init(dnotify_init)