fdinfo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/file.h>
  3. #include <linux/fs.h>
  4. #include <linux/fsnotify_backend.h>
  5. #include <linux/idr.h>
  6. #include <linux/init.h>
  7. #include <linux/inotify.h>
  8. #include <linux/fanotify.h>
  9. #include <linux/kernel.h>
  10. #include <linux/namei.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/exportfs.h>
  15. #include "inotify/inotify.h"
  16. #include "fdinfo.h"
  17. #include "fsnotify.h"
  18. #if defined(CONFIG_PROC_FS)
  19. #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
  20. static void show_fdinfo(struct seq_file *m, struct file *f,
  21. void (*show)(struct seq_file *m,
  22. struct fsnotify_mark *mark))
  23. {
  24. struct fsnotify_group *group = f->private_data;
  25. struct fsnotify_mark *mark;
  26. mutex_lock(&group->mark_mutex);
  27. list_for_each_entry(mark, &group->marks_list, g_list) {
  28. show(m, mark);
  29. if (seq_has_overflowed(m))
  30. break;
  31. }
  32. mutex_unlock(&group->mark_mutex);
  33. }
  34. #if defined(CONFIG_EXPORTFS)
  35. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  36. {
  37. struct {
  38. struct file_handle handle;
  39. u8 pad[MAX_HANDLE_SZ];
  40. } f;
  41. int size, ret, i;
  42. f.handle.handle_bytes = sizeof(f.pad);
  43. size = f.handle.handle_bytes >> 2;
  44. ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
  45. if ((ret == FILEID_INVALID) || (ret < 0)) {
  46. WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
  47. return;
  48. }
  49. f.handle.handle_type = ret;
  50. f.handle.handle_bytes = size * sizeof(u32);
  51. seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
  52. f.handle.handle_bytes, f.handle.handle_type);
  53. for (i = 0; i < f.handle.handle_bytes; i++)
  54. seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
  55. }
  56. #else
  57. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  58. {
  59. }
  60. #endif
  61. #ifdef CONFIG_INOTIFY_USER
  62. static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  63. {
  64. struct inotify_inode_mark *inode_mark;
  65. struct inode *inode;
  66. if (mark->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
  67. return;
  68. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  69. inode = igrab(fsnotify_conn_inode(mark->connector));
  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_IGNORED_SURV_MODIFY)
  97. mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
  98. if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
  99. inode = igrab(fsnotify_conn_inode(mark->connector));
  100. if (!inode)
  101. return;
  102. seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
  103. inode->i_ino, inode->i_sb->s_dev,
  104. mflags, mark->mask, mark->ignored_mask);
  105. show_mark_fhandle(m, inode);
  106. seq_putc(m, '\n');
  107. iput(inode);
  108. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  109. struct mount *mnt = fsnotify_conn_mount(mark->connector);
  110. seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
  111. mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
  112. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
  113. struct super_block *sb = fsnotify_conn_sb(mark->connector);
  114. seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
  115. sb->s_dev, mflags, mark->mask, mark->ignored_mask);
  116. }
  117. }
  118. void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  119. {
  120. struct fsnotify_group *group = f->private_data;
  121. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  122. group->fanotify_data.flags, group->fanotify_data.f_flags);
  123. show_fdinfo(m, f, fanotify_fdinfo);
  124. }
  125. #endif /* CONFIG_FANOTIFY */
  126. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  127. #endif /* CONFIG_PROC_FS */