dnotify.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <linux/fdtable.h>
  24. int dir_notify_enable __read_mostly = 1;
  25. static struct kmem_cache *dn_cache __read_mostly;
  26. static void redo_inode_mask(struct inode *inode)
  27. {
  28. unsigned long new_mask;
  29. struct dnotify_struct *dn;
  30. new_mask = 0;
  31. for (dn = inode->i_dnotify; dn != NULL; dn = dn->dn_next)
  32. new_mask |= dn->dn_mask & ~DN_MULTISHOT;
  33. inode->i_dnotify_mask = new_mask;
  34. }
  35. void dnotify_flush(struct file *filp, fl_owner_t id)
  36. {
  37. struct dnotify_struct *dn;
  38. struct dnotify_struct **prev;
  39. struct inode *inode;
  40. inode = filp->f_path.dentry->d_inode;
  41. if (!S_ISDIR(inode->i_mode))
  42. return;
  43. spin_lock(&inode->i_lock);
  44. prev = &inode->i_dnotify;
  45. while ((dn = *prev) != NULL) {
  46. if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
  47. *prev = dn->dn_next;
  48. redo_inode_mask(inode);
  49. kmem_cache_free(dn_cache, dn);
  50. break;
  51. }
  52. prev = &dn->dn_next;
  53. }
  54. spin_unlock(&inode->i_lock);
  55. }
  56. int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
  57. {
  58. struct dnotify_struct *dn;
  59. struct dnotify_struct *odn;
  60. struct dnotify_struct **prev;
  61. struct inode *inode;
  62. fl_owner_t id = current->files;
  63. struct file *f;
  64. int error = 0;
  65. if ((arg & ~DN_MULTISHOT) == 0) {
  66. dnotify_flush(filp, id);
  67. return 0;
  68. }
  69. if (!dir_notify_enable)
  70. return -EINVAL;
  71. inode = filp->f_path.dentry->d_inode;
  72. if (!S_ISDIR(inode->i_mode))
  73. return -ENOTDIR;
  74. dn = kmem_cache_alloc(dn_cache, GFP_KERNEL);
  75. if (dn == NULL)
  76. return -ENOMEM;
  77. spin_lock(&inode->i_lock);
  78. prev = &inode->i_dnotify;
  79. while ((odn = *prev) != NULL) {
  80. if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
  81. odn->dn_fd = fd;
  82. odn->dn_mask |= arg;
  83. inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
  84. goto out_free;
  85. }
  86. prev = &odn->dn_next;
  87. }
  88. rcu_read_lock();
  89. f = fcheck(fd);
  90. rcu_read_unlock();
  91. /* we'd lost the race with close(), sod off silently */
  92. /* note that inode->i_lock prevents reordering problems
  93. * between accesses to descriptor table and ->i_dnotify */
  94. if (f != filp)
  95. goto out_free;
  96. error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  97. if (error)
  98. goto out_free;
  99. dn->dn_mask = arg;
  100. dn->dn_fd = fd;
  101. dn->dn_filp = filp;
  102. dn->dn_owner = id;
  103. inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
  104. dn->dn_next = inode->i_dnotify;
  105. inode->i_dnotify = dn;
  106. spin_unlock(&inode->i_lock);
  107. return 0;
  108. out_free:
  109. spin_unlock(&inode->i_lock);
  110. kmem_cache_free(dn_cache, dn);
  111. return error;
  112. }
  113. void __inode_dir_notify(struct inode *inode, unsigned long event)
  114. {
  115. struct dnotify_struct * dn;
  116. struct dnotify_struct **prev;
  117. struct fown_struct * fown;
  118. int changed = 0;
  119. spin_lock(&inode->i_lock);
  120. prev = &inode->i_dnotify;
  121. while ((dn = *prev) != NULL) {
  122. if ((dn->dn_mask & event) == 0) {
  123. prev = &dn->dn_next;
  124. continue;
  125. }
  126. fown = &dn->dn_filp->f_owner;
  127. send_sigio(fown, dn->dn_fd, POLL_MSG);
  128. if (dn->dn_mask & DN_MULTISHOT)
  129. prev = &dn->dn_next;
  130. else {
  131. *prev = dn->dn_next;
  132. changed = 1;
  133. kmem_cache_free(dn_cache, dn);
  134. }
  135. }
  136. if (changed)
  137. redo_inode_mask(inode);
  138. spin_unlock(&inode->i_lock);
  139. }
  140. EXPORT_SYMBOL(__inode_dir_notify);
  141. /*
  142. * This is hopelessly wrong, but unfixable without API changes. At
  143. * least it doesn't oops the kernel...
  144. *
  145. * To safely access ->d_parent we need to keep d_move away from it. Use the
  146. * dentry's d_lock for this.
  147. */
  148. void dnotify_parent(struct dentry *dentry, unsigned long event)
  149. {
  150. struct dentry *parent;
  151. if (!dir_notify_enable)
  152. return;
  153. spin_lock(&dentry->d_lock);
  154. parent = dentry->d_parent;
  155. if (parent->d_inode->i_dnotify_mask & event) {
  156. dget(parent);
  157. spin_unlock(&dentry->d_lock);
  158. __inode_dir_notify(parent->d_inode, event);
  159. dput(parent);
  160. } else {
  161. spin_unlock(&dentry->d_lock);
  162. }
  163. }
  164. EXPORT_SYMBOL_GPL(dnotify_parent);
  165. static int __init dnotify_init(void)
  166. {
  167. dn_cache = kmem_cache_create("dnotify_cache",
  168. sizeof(struct dnotify_struct), 0, SLAB_PANIC, NULL);
  169. return 0;
  170. }
  171. module_init(dnotify_init)