fdinfo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "fanotify/fanotify.h"
  17. #include "fdinfo.h"
  18. #include "fsnotify.h"
  19. #include "../internal.h"
  20. #if defined(CONFIG_PROC_FS)
  21. #if defined(CONFIG_INOTIFY_USER) || defined(CONFIG_FANOTIFY)
  22. static void show_fdinfo(struct seq_file *m, struct file *f,
  23. void (*show)(struct seq_file *m,
  24. struct fsnotify_mark *mark))
  25. {
  26. struct fsnotify_group *group = f->private_data;
  27. struct fsnotify_mark *mark;
  28. fsnotify_group_lock(group);
  29. list_for_each_entry(mark, &group->marks_list, g_list) {
  30. show(m, mark);
  31. if (seq_has_overflowed(m))
  32. break;
  33. }
  34. fsnotify_group_unlock(group);
  35. }
  36. #if defined(CONFIG_EXPORTFS)
  37. static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
  38. {
  39. DEFINE_FLEX(struct file_handle, f, f_handle, handle_bytes, MAX_HANDLE_SZ);
  40. int size, ret, i;
  41. size = f->handle_bytes >> 2;
  42. if (!super_trylock_shared(inode->i_sb))
  43. return;
  44. ret = exportfs_encode_fid(inode, (struct fid *)f->f_handle, &size);
  45. up_read(&inode->i_sb->s_umount);
  46. if ((ret == FILEID_INVALID) || (ret < 0))
  47. return;
  48. f->handle_type = ret;
  49. f->handle_bytes = size * sizeof(u32);
  50. seq_printf(m, "fhandle-bytes:%x fhandle-type:%x f_handle:",
  51. f->handle_bytes, f->handle_type);
  52. for (i = 0; i < f->handle_bytes; i++)
  53. seq_printf(m, "%02x", (int)f->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->connector->type != FSNOTIFY_OBJ_TYPE_INODE)
  66. return;
  67. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  68. inode = igrab(fsnotify_conn_inode(mark->connector));
  69. if (inode) {
  70. seq_printf(m, "inotify wd:%x ino:%lx sdev:%x mask:%x ignored_mask:0 ",
  71. inode_mark->wd, inode->i_ino, inode->i_sb->s_dev,
  72. inotify_mark_user_mask(mark));
  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 = fanotify_mark_user_flags(mark);
  87. struct inode *inode;
  88. if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
  89. inode = igrab(fsnotify_conn_inode(mark->connector));
  90. if (!inode)
  91. return;
  92. seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
  93. inode->i_ino, inode->i_sb->s_dev,
  94. mflags, mark->mask, mark->ignore_mask);
  95. show_mark_fhandle(m, inode);
  96. seq_putc(m, '\n');
  97. iput(inode);
  98. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
  99. struct mount *mnt = fsnotify_conn_mount(mark->connector);
  100. seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
  101. mnt->mnt_id, mflags, mark->mask, mark->ignore_mask);
  102. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_SB) {
  103. struct super_block *sb = fsnotify_conn_sb(mark->connector);
  104. seq_printf(m, "fanotify sdev:%x mflags:%x mask:%x ignored_mask:%x\n",
  105. sb->s_dev, mflags, mark->mask, mark->ignore_mask);
  106. } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_MNTNS) {
  107. struct mnt_namespace *mnt_ns = fsnotify_conn_mntns(mark->connector);
  108. seq_printf(m, "fanotify mnt_ns:%u mflags:%x mask:%x ignored_mask:%x\n",
  109. mnt_ns->ns.inum, mflags, mark->mask, mark->ignore_mask);
  110. }
  111. }
  112. void fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  113. {
  114. struct fsnotify_group *group = f->private_data;
  115. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  116. group->fanotify_data.flags & FANOTIFY_INIT_FLAGS,
  117. group->fanotify_data.f_flags);
  118. show_fdinfo(m, f, fanotify_fdinfo);
  119. }
  120. #endif /* CONFIG_FANOTIFY */
  121. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  122. #endif /* CONFIG_PROC_FS */