fdinfo.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 int show_fdinfo(struct seq_file *m, struct file *f,
  20. int (*show)(struct seq_file *m, struct fsnotify_mark *mark))
  21. {
  22. struct fsnotify_group *group = f->private_data;
  23. struct fsnotify_mark *mark;
  24. int ret = 0;
  25. mutex_lock(&group->mark_mutex);
  26. list_for_each_entry(mark, &group->marks_list, g_list) {
  27. ret = show(m, mark);
  28. if (ret)
  29. break;
  30. }
  31. mutex_unlock(&group->mark_mutex);
  32. return ret;
  33. }
  34. #if defined(CONFIG_EXPORTFS)
  35. static int show_mark_fhandle(struct seq_file *m, struct inode *inode)
  36. {
  37. struct {
  38. struct file_handle handle;
  39. u8 pad[64];
  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, 0);
  45. if ((ret == 255) || (ret == -ENOSPC)) {
  46. WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
  47. return 0;
  48. }
  49. f.handle.handle_type = ret;
  50. f.handle.handle_bytes = size * sizeof(u32);
  51. ret = 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. ret |= seq_printf(m, "%02x", (int)f.handle.f_handle[i]);
  55. return ret;
  56. }
  57. #else
  58. static int show_mark_fhandle(struct seq_file *m, struct inode *inode)
  59. {
  60. return 0;
  61. }
  62. #endif
  63. #ifdef CONFIG_INOTIFY_USER
  64. static int inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  65. {
  66. struct inotify_inode_mark *inode_mark;
  67. struct inode *inode;
  68. int ret = 0;
  69. if (!(mark->flags & (FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_INODE)))
  70. return 0;
  71. inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
  72. inode = igrab(mark->i.inode);
  73. if (inode) {
  74. ret = seq_printf(m, "inotify wd:%x ino:%lx sdev:%x "
  75. "mask:%x ignored_mask:%x ",
  76. inode_mark->wd, inode->i_ino,
  77. inode->i_sb->s_dev,
  78. mark->mask, mark->ignored_mask);
  79. ret |= show_mark_fhandle(m, inode);
  80. ret |= seq_putc(m, '\n');
  81. iput(inode);
  82. }
  83. return ret;
  84. }
  85. int inotify_show_fdinfo(struct seq_file *m, struct file *f)
  86. {
  87. return show_fdinfo(m, f, inotify_fdinfo);
  88. }
  89. #endif /* CONFIG_INOTIFY_USER */
  90. #ifdef CONFIG_FANOTIFY
  91. static int fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
  92. {
  93. unsigned int mflags = 0;
  94. struct inode *inode;
  95. int ret = 0;
  96. if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE))
  97. return 0;
  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->i.inode);
  102. if (!inode)
  103. goto out;
  104. ret = seq_printf(m, "fanotify ino:%lx sdev:%x "
  105. "mflags:%x mask:%x ignored_mask:%x ",
  106. inode->i_ino, inode->i_sb->s_dev,
  107. mflags, mark->mask, mark->ignored_mask);
  108. ret |= show_mark_fhandle(m, inode);
  109. ret |= seq_putc(m, '\n');
  110. iput(inode);
  111. } else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) {
  112. struct mount *mnt = real_mount(mark->m.mnt);
  113. ret = seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x "
  114. "ignored_mask:%x\n", mnt->mnt_id, mflags,
  115. mark->mask, mark->ignored_mask);
  116. }
  117. out:
  118. return ret;
  119. }
  120. int fanotify_show_fdinfo(struct seq_file *m, struct file *f)
  121. {
  122. struct fsnotify_group *group = f->private_data;
  123. unsigned int flags = 0;
  124. switch (group->priority) {
  125. case FS_PRIO_0:
  126. flags |= FAN_CLASS_NOTIF;
  127. break;
  128. case FS_PRIO_1:
  129. flags |= FAN_CLASS_CONTENT;
  130. break;
  131. case FS_PRIO_2:
  132. flags |= FAN_CLASS_PRE_CONTENT;
  133. break;
  134. }
  135. if (group->max_events == UINT_MAX)
  136. flags |= FAN_UNLIMITED_QUEUE;
  137. if (group->fanotify_data.max_marks == UINT_MAX)
  138. flags |= FAN_UNLIMITED_MARKS;
  139. seq_printf(m, "fanotify flags:%x event-flags:%x\n",
  140. flags, group->fanotify_data.f_flags);
  141. return show_fdinfo(m, f, fanotify_fdinfo);
  142. }
  143. #endif /* CONFIG_FANOTIFY */
  144. #endif /* CONFIG_INOTIFY_USER || CONFIG_FANOTIFY */
  145. #endif /* CONFIG_PROC_FS */