fdinfo.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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) ||
  66. !(mark->flags & FSNOTIFY_MARK_FLAG_INODE))
  67. return;
  68. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  69. inode = igrab(mark->inode);
  70. if (inode) {
  71. /*
  72. * IN_ALL_EVENTS represents all of the mask bits
  73. * that we expose to userspace. There is at
  74. * least one bit (FS_EVENT_ON_CHILD) which is
  75. * used only internally to the kernel.
  76. */
  77. u32 mask = mark->mask & IN_ALL_EVENTS;
  78. seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:%x ",
  79. inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
  80. mask, mark->ignored_mask);
  81. show_mark_fhandle(m, inode);
  82. seq_putc(m, '\n');
  83. iput(inode);
  84. }
  85. }
  86. void inotify_show_fdinfo(struct seq_file *m, struct file *f)
  87. {
  88. show_fdinfo(m, f, inotify_fdinfo);
  89. }
  90. #endif /* CONFIG_INOTIFY_USER */
  91. #ifdef CONFIG_FANOTIFY
  92. static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  93. {
  94. unsigned int mflags = 0;
  95. struct inode *inode;
  96. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE))
  97. return;
  98. if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
  99. mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
  100. if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
  101. inode = igrab(mark->inode);
  102. if (!inode)
  103. return;
  104. seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
  105. inode->i_ino, inode->i_sb->s_dev,
  106. mflags, mark->mask, mark->ignored_mask);
  107. show_mark_fhandle(m, inode);
  108. seq_putc(m, '\n');
  109. iput(inode);
  110. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) {
  111. struct mount *mnt = real_mount(mark->mnt);
  112. seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
  113. mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
  114. }
  115. }
  116. void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  117. {
  118. struct fsnotify_group *group = f->private_data;
  119. unsigned int flags = 0;
  120. switch (group->priority) {
  121. case FS_PRIO_0:
  122. flags |= FAN_CLASS_NOTIF;
  123. break;
  124. case FS_PRIO_1:
  125. flags |= FAN_CLASS_CONTENT;
  126. break;
  127. case FS_PRIO_2:
  128. flags |= FAN_CLASS_PRE_CONTENT;
  129. break;
  130. }
  131. if (group->max_events == UINT_MAX)
  132. flags |= FAN_UNLIMITED_QUEUE;
  133. if (group->fanotify_data.max_marks == UINT_MAX)
  134. flags |= FAN_UNLIMITED_MARKS;
  135. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  136. flags, group->fanotify_data.f_flags);
  137. show_fdinfo(m, f, fanotify_fdinfo);
  138. }
  139. #endif /* CONFIG_FANOTIFY */
  140. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  141. #endif /* CONFIG_PROC_FS */