dnotify.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/sched.h>
  18. #include <linux/dnotify.h>
  19. #include <linux/init.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. extern void send_sigio(struct fown_struct *fown, int fd, int band);
  23. int dir_notify_enable = 1;
  24. static rwlock_t dn_lock = RW_LOCK_UNLOCKED;
  25. static kmem_cache_t *dn_cache;
  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_dentry->d_inode;
  41. if (!S_ISDIR(inode->i_mode))
  42. return;
  43. write_lock(&dn_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. write_unlock(&dn_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. 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_dentry->d_inode;
  70. if (!S_ISDIR(inode->i_mode))
  71. return -ENOTDIR;
  72. dn = kmem_cache_alloc(dn_cache, SLAB_KERNEL);
  73. if (dn == NULL)
  74. return -ENOMEM;
  75. write_lock(&dn_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. kmem_cache_free(dn_cache, dn);
  83. goto out;
  84. }
  85. prev = &odn->dn_next;
  86. }
  87. filp->f_owner.pid = current->pid;
  88. filp->f_owner.uid = current->uid;
  89. filp->f_owner.euid = current->euid;
  90. dn->dn_mask = arg;
  91. dn->dn_fd = fd;
  92. dn->dn_filp = filp;
  93. dn->dn_owner = id;
  94. inode->i_dnotify_mask |= arg & ~DN_MULTISHOT;
  95. dn->dn_next = inode->i_dnotify;
  96. inode->i_dnotify = dn;
  97. out:
  98. write_unlock(&dn_lock);
  99. return 0;
  100. }
  101. void __inode_dir_notify(struct inode *inode, unsigned long event)
  102. {
  103. struct dnotify_struct * dn;
  104. struct dnotify_struct **prev;
  105. struct fown_struct * fown;
  106. int changed = 0;
  107. write_lock(&dn_lock);
  108. prev = &inode->i_dnotify;
  109. while ((dn = *prev) != NULL) {
  110. if ((dn->dn_mask & event) == 0) {
  111. prev = &dn->dn_next;
  112. continue;
  113. }
  114. fown = &dn->dn_filp->f_owner;
  115. if (fown->pid)
  116.         send_sigio(fown, dn->dn_fd, POLL_MSG);
  117. if (dn->dn_mask & DN_MULTISHOT)
  118. prev = &dn->dn_next;
  119. else {
  120. *prev = dn->dn_next;
  121. changed = 1;
  122. kmem_cache_free(dn_cache, dn);
  123. }
  124. }
  125. if (changed)
  126. redo_inode_mask(inode);
  127. write_unlock(&dn_lock);
  128. }
  129. static int __init dnotify_init(void)
  130. {
  131. dn_cache = kmem_cache_create("dnotify_cache",
  132. sizeof(struct dnotify_struct), 0, 0, NULL, NULL);
  133. if (!dn_cache)
  134. panic("cannot create dnotify slab cache");
  135. return 0;
  136. }
  137. module_init(dnotify_init)