fdinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <linux/file.h>
  2. #include <linux/fs.h>
  3. #include <linux/fsnotify_backend.h>
  4. #include <linux/idr.h>
  5. #include <linux/init.h>
  6. #include <linux/inotify.h>
  7. #include <linux/fanotify.h>
  8. #include <linux/kernel.h>
  9. #include <linux/namei.h>
  10. #include <linux/sched.h>
  11. #include <linux/types.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/exportfs.h>
  15. #include "inotify/inotify.h"
  16. #include "../fs/mount.h"
  17. #if defined(CONFIG_PROC_FS)
  18. #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
  19. static void show_fdinfo(struct seq_file *m, struct file *f,
  20. void (*show)(struct seq_file *m,
  21. struct fsnotify_mark *mark))
  22. {
  23. struct fsnotify_group *group = f->private_data;
  24. struct fsnotify_mark *mark;
  25. mutex_lock(&group->mark_mutex);
  26. list_for_each_entry(mark, &group->marks_list, g_list) {
  27. show(m, mark);
  28. if (seq_has_overflowed(m))
  29. break;
  30. }
  31. mutex_unlock(&group->mark_mutex);
  32. }
  33. #if defined(CONFIG_EXPORTFS)
  34. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  35. {
  36. struct {
  37. struct file_handle handle;
  38. u8 pad[MAX_HANDLE_SZ];
  39. } f;
  40. int size, ret, i;
  41. f.handle.handle_bytes = sizeof(f.pad);
  42. size = f.handle.handle_bytes >> 2;
  43. ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, 0);
  44. if ((ret == FILEID_INVALID) || (ret < 0)) {
  45. WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
  46. return;
  47. }
  48. f.handle.handle_type = ret;
  49. f.handle.handle_bytes = size * sizeof(u32);
  50. seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
  51. f.handle.handle_bytes, f.handle.handle_type);
  52. for (i = 0; i < f.handle.handle_bytes; i++)
  53. seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
  54. }
  55. #else
  56. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  57. {
  58. }
  59. #endif
  60. #ifdef CONFIG_INOTIFY_USER
  61. static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  62. {
  63. struct inotify_inode_mark *inode_mark;
  64. struct inode *inode;
  65. if (!(mark->flags & (FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_INODE)))
  66. return;
  67. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  68. inode = igrab(mark->inode);
  69. if (inode) {
  70. seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
  71. inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
  72. mark->mask, mark->ignored_mask);
  73. show_mark_fhandle(m, inode);
  74. seq_putc(m, '\n');
  75. iput(inode);
  76. }
  77. }
  78. void inotify_show_fdinfo(struct seq_file *m, struct file *f)
  79. {
  80. show_fdinfo(m, f, inotify_fdinfo);
  81. }
  82. #endif /* CONFIG_INOTIFY_USER */
  83. #ifdef CONFIG_FANOTIFY
  84. static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  85. {
  86. unsigned int mflags = 0;
  87. struct inode *inode;
  88. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE))
  89. return;
  90. if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
  91. mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
  92. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
  93. inode = igrab(mark->inode);
  94. if (!inode)
  95. return;
  96. seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
  97. inode->i_ino, inode->i_sb->s_dev,
  98. mflags, mark->mask, mark->ignored_mask);
  99. show_mark_fhandle(m, inode);
  100. seq_putc(m, '\n');
  101. iput(inode);
  102. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) {
  103. struct mount *mnt = real_mount(mark->mnt);
  104. seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
  105. mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
  106. }
  107. }
  108. void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  109. {
  110. struct fsnotify_group *group = f->private_data;
  111. unsigned int flags = 0;
  112. switch (group->priority) {
  113. case FS_PRIO_0:
  114. flags |= FAN_CLASS_NOTIF;
  115. break;
  116. case FS_PRIO_1:
  117. flags |= FAN_CLASS_CONTENT;
  118. break;
  119. case FS_PRIO_2:
  120. flags |= FAN_CLASS_PRE_CONTENT;
  121. break;
  122. }
  123. if (group->max_events == UINT_MAX)
  124. flags |= FAN_UNLIMITED_QUEUE;
  125. if (group->fanotify_data.max_marks == UINT_MAX)
  126. flags |= FAN_UNLIMITED_MARKS;
  127. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  128. flags, group->fanotify_data.f_flags);
  129. show_fdinfo(m, f, fanotify_fdinfo);
  130. }
  131. #endif /* CONFIG_FANOTIFY */
  132. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  133. #endif /* CONFIG_PROC_FS */