event_inode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * event_inode.c - part of tracefs, a pseudo file system for activating tracing
  4. *
  5. * Copyright (C) 2020-23 VMware Inc, author: Steven Rostedt <rostedt@goodmis.org>
  6. * Copyright (C) 2020-23 VMware Inc, author: Ajay Kaher <akaher@vmware.com>
  7. * Copyright (C) 2023 Google, author: Steven Rostedt <rostedt@goodmis.org>
  8. *
  9. * eventfs is used to dynamically create inodes and dentries based on the
  10. * meta data provided by the tracing system.
  11. *
  12. * eventfs stores the meta-data of files/dirs and holds off on creating
  13. * inodes/dentries of the files. When accessed, the eventfs will create the
  14. * inodes/dentries in a just-in-time (JIT) manner. The eventfs will clean up
  15. * and delete the inodes/dentries when they are no longer referenced.
  16. */
  17. #include <linux/fsnotify.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/security.h>
  22. #include <linux/tracefs.h>
  23. #include <linux/kref.h>
  24. #include <linux/delay.h>
  25. #include "internal.h"
  26. /*
  27. * eventfs_mutex protects the eventfs_inode (ei) dentry. Any access
  28. * to the ei->dentry must be done under this mutex and after checking
  29. * if ei->is_freed is not set. When ei->is_freed is set, the dentry
  30. * is on its way to being freed after the last dput() is made on it.
  31. */
  32. static DEFINE_MUTEX(eventfs_mutex);
  33. /* Choose something "unique" ;-) */
  34. #define EVENTFS_FILE_INODE_INO 0x12c4e37
  35. struct eventfs_root_inode {
  36. struct eventfs_inode ei;
  37. struct dentry *events_dir;
  38. };
  39. static struct eventfs_root_inode *get_root_inode(struct eventfs_inode *ei)
  40. {
  41. WARN_ON_ONCE(!ei->is_events);
  42. return container_of(ei, struct eventfs_root_inode, ei);
  43. }
  44. /* Just try to make something consistent and unique */
  45. static int eventfs_dir_ino(struct eventfs_inode *ei)
  46. {
  47. if (!ei->ino) {
  48. ei->ino = get_next_ino();
  49. /* Must not have the file inode number */
  50. if (ei->ino == EVENTFS_FILE_INODE_INO)
  51. ei->ino = get_next_ino();
  52. }
  53. return ei->ino;
  54. }
  55. /*
  56. * The eventfs_inode (ei) itself is protected by SRCU. It is released from
  57. * its parent's list and will have is_freed set (under eventfs_mutex).
  58. * After the SRCU grace period is over and the last dput() is called
  59. * the ei is freed.
  60. */
  61. DEFINE_STATIC_SRCU(eventfs_srcu);
  62. /* Mode is unsigned short, use the upper bits for flags */
  63. enum {
  64. EVENTFS_SAVE_MODE = BIT(16),
  65. EVENTFS_SAVE_UID = BIT(17),
  66. EVENTFS_SAVE_GID = BIT(18),
  67. };
  68. #define EVENTFS_MODE_MASK (EVENTFS_SAVE_MODE - 1)
  69. static void free_ei_rcu(struct rcu_head *rcu)
  70. {
  71. struct eventfs_inode *ei = container_of(rcu, struct eventfs_inode, rcu);
  72. struct eventfs_root_inode *rei;
  73. kfree(ei->entry_attrs);
  74. kfree_const(ei->name);
  75. if (ei->is_events) {
  76. rei = get_root_inode(ei);
  77. kfree(rei);
  78. } else {
  79. kfree(ei);
  80. }
  81. }
  82. /*
  83. * eventfs_inode reference count management.
  84. *
  85. * NOTE! We count only references from dentries, in the
  86. * form 'dentry->d_fsdata'. There are also references from
  87. * directory inodes ('ti->private'), but the dentry reference
  88. * count is always a superset of the inode reference count.
  89. */
  90. static void release_ei(struct kref *ref)
  91. {
  92. struct eventfs_inode *ei = container_of(ref, struct eventfs_inode, kref);
  93. const struct eventfs_entry *entry;
  94. WARN_ON_ONCE(!ei->is_freed);
  95. for (int i = 0; i < ei->nr_entries; i++) {
  96. entry = &ei->entries[i];
  97. if (entry->release)
  98. entry->release(entry->name, ei->data);
  99. }
  100. call_srcu(&eventfs_srcu, &ei->rcu, free_ei_rcu);
  101. }
  102. static inline void put_ei(struct eventfs_inode *ei)
  103. {
  104. if (ei)
  105. kref_put(&ei->kref, release_ei);
  106. }
  107. static inline void free_ei(struct eventfs_inode *ei)
  108. {
  109. if (ei) {
  110. ei->is_freed = 1;
  111. put_ei(ei);
  112. }
  113. }
  114. /*
  115. * Called when creation of an ei fails, do not call release() functions.
  116. */
  117. static inline void cleanup_ei(struct eventfs_inode *ei)
  118. {
  119. if (ei) {
  120. /* Set nr_entries to 0 to prevent release() function being called */
  121. ei->nr_entries = 0;
  122. free_ei(ei);
  123. }
  124. }
  125. static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)
  126. {
  127. if (ei)
  128. kref_get(&ei->kref);
  129. return ei;
  130. }
  131. static struct dentry *eventfs_root_lookup(struct inode *dir,
  132. struct dentry *dentry,
  133. unsigned int flags);
  134. static int eventfs_iterate(struct file *file, struct dir_context *ctx);
  135. static void update_attr(struct eventfs_attr *attr, struct iattr *iattr)
  136. {
  137. unsigned int ia_valid = iattr->ia_valid;
  138. if (ia_valid & ATTR_MODE) {
  139. attr->mode = (attr->mode & ~EVENTFS_MODE_MASK) |
  140. (iattr->ia_mode & EVENTFS_MODE_MASK) |
  141. EVENTFS_SAVE_MODE;
  142. }
  143. if (ia_valid & ATTR_UID) {
  144. attr->mode |= EVENTFS_SAVE_UID;
  145. attr->uid = iattr->ia_uid;
  146. }
  147. if (ia_valid & ATTR_GID) {
  148. attr->mode |= EVENTFS_SAVE_GID;
  149. attr->gid = iattr->ia_gid;
  150. }
  151. }
  152. static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
  153. struct iattr *iattr)
  154. {
  155. const struct eventfs_entry *entry;
  156. struct eventfs_inode *ei;
  157. const char *name;
  158. int ret;
  159. mutex_lock(&eventfs_mutex);
  160. ei = dentry->d_fsdata;
  161. if (ei->is_freed) {
  162. /* Do not allow changes if the event is about to be removed. */
  163. mutex_unlock(&eventfs_mutex);
  164. return -ENODEV;
  165. }
  166. /* Preallocate the children mode array if necessary */
  167. if (!(dentry->d_inode->i_mode & S_IFDIR)) {
  168. if (!ei->entry_attrs) {
  169. ei->entry_attrs = kcalloc(ei->nr_entries, sizeof(*ei->entry_attrs),
  170. GFP_NOFS);
  171. if (!ei->entry_attrs) {
  172. ret = -ENOMEM;
  173. goto out;
  174. }
  175. }
  176. }
  177. ret = simple_setattr(idmap, dentry, iattr);
  178. if (ret < 0)
  179. goto out;
  180. /*
  181. * If this is a dir, then update the ei cache, only the file
  182. * mode is saved in the ei->m_children, and the ownership is
  183. * determined by the parent directory.
  184. */
  185. if (dentry->d_inode->i_mode & S_IFDIR) {
  186. /* Just use the inode permissions for the events directory */
  187. if (!ei->is_events)
  188. update_attr(&ei->attr, iattr);
  189. } else {
  190. name = dentry->d_name.name;
  191. for (int i = 0; i < ei->nr_entries; i++) {
  192. entry = &ei->entries[i];
  193. if (strcmp(name, entry->name) == 0) {
  194. update_attr(&ei->entry_attrs[i], iattr);
  195. break;
  196. }
  197. }
  198. }
  199. out:
  200. mutex_unlock(&eventfs_mutex);
  201. return ret;
  202. }
  203. static const struct inode_operations eventfs_dir_inode_operations = {
  204. .lookup = eventfs_root_lookup,
  205. .setattr = eventfs_set_attr,
  206. };
  207. static const struct inode_operations eventfs_file_inode_operations = {
  208. .setattr = eventfs_set_attr,
  209. };
  210. static const struct file_operations eventfs_file_operations = {
  211. .read = generic_read_dir,
  212. .iterate_shared = eventfs_iterate,
  213. .llseek = generic_file_llseek,
  214. };
  215. static void eventfs_set_attrs(struct eventfs_inode *ei, bool update_uid, kuid_t uid,
  216. bool update_gid, kgid_t gid, int level)
  217. {
  218. struct eventfs_inode *ei_child;
  219. /* Update events/<system>/<event> */
  220. if (WARN_ON_ONCE(level > 3))
  221. return;
  222. if (update_uid) {
  223. ei->attr.mode &= ~EVENTFS_SAVE_UID;
  224. ei->attr.uid = uid;
  225. }
  226. if (update_gid) {
  227. ei->attr.mode &= ~EVENTFS_SAVE_GID;
  228. ei->attr.gid = gid;
  229. }
  230. list_for_each_entry(ei_child, &ei->children, list) {
  231. eventfs_set_attrs(ei_child, update_uid, uid, update_gid, gid, level + 1);
  232. }
  233. if (!ei->entry_attrs)
  234. return;
  235. for (int i = 0; i < ei->nr_entries; i++) {
  236. if (update_uid) {
  237. ei->entry_attrs[i].mode &= ~EVENTFS_SAVE_UID;
  238. ei->entry_attrs[i].uid = uid;
  239. }
  240. if (update_gid) {
  241. ei->entry_attrs[i].mode &= ~EVENTFS_SAVE_GID;
  242. ei->entry_attrs[i].gid = gid;
  243. }
  244. }
  245. }
  246. /*
  247. * On a remount of tracefs, if UID or GID options are set, then
  248. * the mount point inode permissions should be used.
  249. * Reset the saved permission flags appropriately.
  250. */
  251. void eventfs_remount(struct tracefs_inode *ti, bool update_uid, bool update_gid)
  252. {
  253. struct eventfs_inode *ei = ti->private;
  254. /* Only the events directory does the updates */
  255. if (!ei || !ei->is_events || ei->is_freed)
  256. return;
  257. eventfs_set_attrs(ei, update_uid, ti->vfs_inode.i_uid,
  258. update_gid, ti->vfs_inode.i_gid, 0);
  259. }
  260. static void update_inode_attr(struct inode *inode, umode_t mode,
  261. struct eventfs_attr *attr, struct eventfs_root_inode *rei)
  262. {
  263. if (attr && attr->mode & EVENTFS_SAVE_MODE)
  264. inode->i_mode = attr->mode & EVENTFS_MODE_MASK;
  265. else
  266. inode->i_mode = mode;
  267. if (attr && attr->mode & EVENTFS_SAVE_UID)
  268. inode->i_uid = attr->uid;
  269. else
  270. inode->i_uid = rei->ei.attr.uid;
  271. if (attr && attr->mode & EVENTFS_SAVE_GID)
  272. inode->i_gid = attr->gid;
  273. else
  274. inode->i_gid = rei->ei.attr.gid;
  275. }
  276. static struct inode *eventfs_get_inode(struct dentry *dentry, struct eventfs_attr *attr,
  277. umode_t mode, struct eventfs_inode *ei)
  278. {
  279. struct eventfs_root_inode *rei;
  280. struct eventfs_inode *pei;
  281. struct tracefs_inode *ti;
  282. struct inode *inode;
  283. inode = tracefs_get_inode(dentry->d_sb);
  284. if (!inode)
  285. return NULL;
  286. ti = get_tracefs(inode);
  287. ti->private = ei;
  288. ti->flags |= TRACEFS_EVENT_INODE;
  289. /* Find the top dentry that holds the "events" directory */
  290. do {
  291. dentry = dentry->d_parent;
  292. /* Directories always have d_fsdata */
  293. pei = dentry->d_fsdata;
  294. } while (!pei->is_events);
  295. rei = get_root_inode(pei);
  296. update_inode_attr(inode, mode, attr, rei);
  297. return inode;
  298. }
  299. /**
  300. * lookup_file - look up a file in the tracefs filesystem
  301. * @parent_ei: Pointer to the eventfs_inode that represents parent of the file
  302. * @dentry: the dentry to look up
  303. * @mode: the permission that the file should have.
  304. * @attr: saved attributes changed by user
  305. * @data: something that the caller will want to get to later on.
  306. * @fop: struct file_operations that should be used for this file.
  307. *
  308. * This function creates a dentry that represents a file in the eventsfs_inode
  309. * directory. The inode.i_private pointer will point to @data in the open()
  310. * call.
  311. */
  312. static struct dentry *lookup_file(struct eventfs_inode *parent_ei,
  313. struct dentry *dentry,
  314. umode_t mode,
  315. struct eventfs_attr *attr,
  316. void *data,
  317. const struct file_operations *fop)
  318. {
  319. struct inode *inode;
  320. if (!(mode & S_IFMT))
  321. mode |= S_IFREG;
  322. if (WARN_ON_ONCE(!S_ISREG(mode)))
  323. return ERR_PTR(-EIO);
  324. /* Only directories have ti->private set to an ei, not files */
  325. inode = eventfs_get_inode(dentry, attr, mode, NULL);
  326. if (unlikely(!inode))
  327. return ERR_PTR(-ENOMEM);
  328. inode->i_op = &eventfs_file_inode_operations;
  329. inode->i_fop = fop;
  330. inode->i_private = data;
  331. /* All files will have the same inode number */
  332. inode->i_ino = EVENTFS_FILE_INODE_INO;
  333. // Files have their parent's ei as their fsdata
  334. dentry->d_fsdata = get_ei(parent_ei);
  335. d_add(dentry, inode);
  336. return NULL;
  337. };
  338. /**
  339. * lookup_dir_entry - look up a dir in the tracefs filesystem
  340. * @dentry: the directory to look up
  341. * @pei: Pointer to the parent eventfs_inode if available
  342. * @ei: the eventfs_inode that represents the directory to create
  343. *
  344. * This function will look up a dentry for a directory represented by
  345. * a eventfs_inode.
  346. */
  347. static struct dentry *lookup_dir_entry(struct dentry *dentry,
  348. struct eventfs_inode *pei, struct eventfs_inode *ei)
  349. {
  350. struct inode *inode;
  351. umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  352. inode = eventfs_get_inode(dentry, &ei->attr, mode, ei);
  353. if (unlikely(!inode))
  354. return ERR_PTR(-ENOMEM);
  355. inode->i_op = &eventfs_dir_inode_operations;
  356. inode->i_fop = &eventfs_file_operations;
  357. /* All directories will have the same inode number */
  358. inode->i_ino = eventfs_dir_ino(ei);
  359. dentry->d_fsdata = get_ei(ei);
  360. d_add(dentry, inode);
  361. return NULL;
  362. }
  363. static inline struct eventfs_inode *init_ei(struct eventfs_inode *ei, const char *name)
  364. {
  365. ei->name = kstrdup_const(name, GFP_KERNEL);
  366. if (!ei->name)
  367. return NULL;
  368. kref_init(&ei->kref);
  369. return ei;
  370. }
  371. static inline struct eventfs_inode *alloc_ei(const char *name)
  372. {
  373. struct eventfs_inode *ei = kzalloc(sizeof(*ei), GFP_KERNEL);
  374. struct eventfs_inode *result;
  375. if (!ei)
  376. return NULL;
  377. result = init_ei(ei, name);
  378. if (!result)
  379. kfree(ei);
  380. return result;
  381. }
  382. static inline struct eventfs_inode *alloc_root_ei(const char *name)
  383. {
  384. struct eventfs_root_inode *rei = kzalloc(sizeof(*rei), GFP_KERNEL);
  385. struct eventfs_inode *ei;
  386. if (!rei)
  387. return NULL;
  388. rei->ei.is_events = 1;
  389. ei = init_ei(&rei->ei, name);
  390. if (!ei)
  391. kfree(rei);
  392. return ei;
  393. }
  394. /**
  395. * eventfs_d_release - dentry is going away
  396. * @dentry: dentry which has the reference to remove.
  397. *
  398. * Remove the association between a dentry from an eventfs_inode.
  399. */
  400. void eventfs_d_release(struct dentry *dentry)
  401. {
  402. put_ei(dentry->d_fsdata);
  403. }
  404. /**
  405. * lookup_file_dentry - create a dentry for a file of an eventfs_inode
  406. * @dentry: The parent dentry under which the new file's dentry will be created
  407. * @ei: the eventfs_inode that the file will be created under
  408. * @idx: the index into the entry_attrs[] of the @ei
  409. * @mode: The mode of the file.
  410. * @data: The data to use to set the inode of the file with on open()
  411. * @fops: The fops of the file to be created.
  412. *
  413. * This function creates a dentry for a file associated with an
  414. * eventfs_inode @ei. It uses the entry attributes specified by @idx,
  415. * if available. The file will have the specified @mode and its inode will be
  416. * set up with @data upon open. The file operations will be set to @fops.
  417. *
  418. * Return: Returns a pointer to the newly created file's dentry or an error
  419. * pointer.
  420. */
  421. static struct dentry *
  422. lookup_file_dentry(struct dentry *dentry,
  423. struct eventfs_inode *ei, int idx,
  424. umode_t mode, void *data,
  425. const struct file_operations *fops)
  426. {
  427. struct eventfs_attr *attr = NULL;
  428. if (ei->entry_attrs)
  429. attr = &ei->entry_attrs[idx];
  430. return lookup_file(ei, dentry, mode, attr, data, fops);
  431. }
  432. /**
  433. * eventfs_root_lookup - lookup routine to create file/dir
  434. * @dir: in which a lookup is being done
  435. * @dentry: file/dir dentry
  436. * @flags: Just passed to simple_lookup()
  437. *
  438. * Used to create dynamic file/dir with-in @dir, search with-in @ei
  439. * list, if @dentry found go ahead and create the file/dir
  440. */
  441. static struct dentry *eventfs_root_lookup(struct inode *dir,
  442. struct dentry *dentry,
  443. unsigned int flags)
  444. {
  445. struct eventfs_inode *ei_child;
  446. struct tracefs_inode *ti;
  447. struct eventfs_inode *ei;
  448. const char *name = dentry->d_name.name;
  449. struct dentry *result = NULL;
  450. ti = get_tracefs(dir);
  451. if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
  452. return ERR_PTR(-EIO);
  453. mutex_lock(&eventfs_mutex);
  454. ei = ti->private;
  455. if (!ei || ei->is_freed)
  456. goto out;
  457. list_for_each_entry(ei_child, &ei->children, list) {
  458. if (strcmp(ei_child->name, name) != 0)
  459. continue;
  460. /* A child is freed and removed from the list at the same time */
  461. if (WARN_ON_ONCE(ei_child->is_freed))
  462. goto out;
  463. result = lookup_dir_entry(dentry, ei, ei_child);
  464. goto out;
  465. }
  466. for (int i = 0; i < ei->nr_entries; i++) {
  467. void *data;
  468. umode_t mode;
  469. const struct file_operations *fops;
  470. const struct eventfs_entry *entry = &ei->entries[i];
  471. if (strcmp(name, entry->name) != 0)
  472. continue;
  473. data = ei->data;
  474. if (entry->callback(name, &mode, &data, &fops) <= 0)
  475. goto out;
  476. result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
  477. goto out;
  478. }
  479. out:
  480. mutex_unlock(&eventfs_mutex);
  481. return result;
  482. }
  483. /*
  484. * Walk the children of a eventfs_inode to fill in getdents().
  485. */
  486. static int eventfs_iterate(struct file *file, struct dir_context *ctx)
  487. {
  488. const struct file_operations *fops;
  489. struct inode *f_inode = file_inode(file);
  490. const struct eventfs_entry *entry;
  491. struct eventfs_inode *ei_child;
  492. struct tracefs_inode *ti;
  493. struct eventfs_inode *ei;
  494. const char *name;
  495. umode_t mode;
  496. int idx;
  497. int ret = -EINVAL;
  498. int ino;
  499. int i, r, c;
  500. if (!dir_emit_dots(file, ctx))
  501. return 0;
  502. ti = get_tracefs(f_inode);
  503. if (!(ti->flags & TRACEFS_EVENT_INODE))
  504. return -EINVAL;
  505. c = ctx->pos - 2;
  506. idx = srcu_read_lock(&eventfs_srcu);
  507. mutex_lock(&eventfs_mutex);
  508. ei = READ_ONCE(ti->private);
  509. if (ei && ei->is_freed)
  510. ei = NULL;
  511. mutex_unlock(&eventfs_mutex);
  512. if (!ei)
  513. goto out;
  514. /*
  515. * Need to create the dentries and inodes to have a consistent
  516. * inode number.
  517. */
  518. ret = 0;
  519. /* Start at 'c' to jump over already read entries */
  520. for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
  521. void *cdata = ei->data;
  522. entry = &ei->entries[i];
  523. name = entry->name;
  524. mutex_lock(&eventfs_mutex);
  525. /* If ei->is_freed then just bail here, nothing more to do */
  526. if (ei->is_freed) {
  527. mutex_unlock(&eventfs_mutex);
  528. goto out;
  529. }
  530. r = entry->callback(name, &mode, &cdata, &fops);
  531. mutex_unlock(&eventfs_mutex);
  532. if (r <= 0)
  533. continue;
  534. ino = EVENTFS_FILE_INODE_INO;
  535. if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
  536. goto out;
  537. }
  538. /* Subtract the skipped entries above */
  539. c -= min((unsigned int)c, (unsigned int)ei->nr_entries);
  540. list_for_each_entry_srcu(ei_child, &ei->children, list,
  541. srcu_read_lock_held(&eventfs_srcu)) {
  542. if (c > 0) {
  543. c--;
  544. continue;
  545. }
  546. ctx->pos++;
  547. if (ei_child->is_freed)
  548. continue;
  549. name = ei_child->name;
  550. ino = eventfs_dir_ino(ei_child);
  551. if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
  552. goto out_dec;
  553. }
  554. ret = 1;
  555. out:
  556. srcu_read_unlock(&eventfs_srcu, idx);
  557. return ret;
  558. out_dec:
  559. /* Incremented ctx->pos without adding something, reset it */
  560. ctx->pos--;
  561. goto out;
  562. }
  563. /**
  564. * eventfs_create_dir - Create the eventfs_inode for this directory
  565. * @name: The name of the directory to create.
  566. * @parent: The eventfs_inode of the parent directory.
  567. * @entries: A list of entries that represent the files under this directory
  568. * @size: The number of @entries
  569. * @data: The default data to pass to the files (an entry may override it).
  570. *
  571. * This function creates the descriptor to represent a directory in the
  572. * eventfs. This descriptor is an eventfs_inode, and it is returned to be
  573. * used to create other children underneath.
  574. *
  575. * The @entries is an array of eventfs_entry structures which has:
  576. * const char *name
  577. * eventfs_callback callback;
  578. *
  579. * The name is the name of the file, and the callback is a pointer to a function
  580. * that will be called when the file is reference (either by lookup or by
  581. * reading a directory). The callback is of the prototype:
  582. *
  583. * int callback(const char *name, umode_t *mode, void **data,
  584. * const struct file_operations **fops);
  585. *
  586. * When a file needs to be created, this callback will be called with
  587. * name = the name of the file being created (so that the same callback
  588. * may be used for multiple files).
  589. * mode = a place to set the file's mode
  590. * data = A pointer to @data, and the callback may replace it, which will
  591. * cause the file created to pass the new data to the open() call.
  592. * fops = the fops to use for the created file.
  593. *
  594. * NB. @callback is called while holding internal locks of the eventfs
  595. * system. The callback must not call any code that might also call into
  596. * the tracefs or eventfs system or it will risk creating a deadlock.
  597. */
  598. struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode *parent,
  599. const struct eventfs_entry *entries,
  600. int size, void *data)
  601. {
  602. struct eventfs_inode *ei;
  603. if (!parent)
  604. return ERR_PTR(-EINVAL);
  605. ei = alloc_ei(name);
  606. if (!ei)
  607. return ERR_PTR(-ENOMEM);
  608. ei->entries = entries;
  609. ei->nr_entries = size;
  610. ei->data = data;
  611. INIT_LIST_HEAD(&ei->children);
  612. INIT_LIST_HEAD(&ei->list);
  613. mutex_lock(&eventfs_mutex);
  614. if (!parent->is_freed)
  615. list_add_tail(&ei->list, &parent->children);
  616. mutex_unlock(&eventfs_mutex);
  617. /* Was the parent freed? */
  618. if (list_empty(&ei->list)) {
  619. cleanup_ei(ei);
  620. ei = ERR_PTR(-EBUSY);
  621. }
  622. return ei;
  623. }
  624. /**
  625. * eventfs_create_events_dir - create the top level events directory
  626. * @name: The name of the top level directory to create.
  627. * @parent: Parent dentry for this file in the tracefs directory.
  628. * @entries: A list of entries that represent the files under this directory
  629. * @size: The number of @entries
  630. * @data: The default data to pass to the files (an entry may override it).
  631. *
  632. * This function creates the top of the trace event directory.
  633. *
  634. * See eventfs_create_dir() for use of @entries.
  635. */
  636. struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry *parent,
  637. const struct eventfs_entry *entries,
  638. int size, void *data)
  639. {
  640. struct dentry *dentry = tracefs_start_creating(name, parent);
  641. struct eventfs_root_inode *rei;
  642. struct eventfs_inode *ei;
  643. struct tracefs_inode *ti;
  644. struct inode *inode;
  645. kuid_t uid;
  646. kgid_t gid;
  647. if (security_locked_down(LOCKDOWN_TRACEFS))
  648. return NULL;
  649. if (IS_ERR(dentry))
  650. return ERR_CAST(dentry);
  651. ei = alloc_root_ei(name);
  652. if (!ei)
  653. goto fail;
  654. inode = tracefs_get_inode(dentry->d_sb);
  655. if (unlikely(!inode))
  656. goto fail;
  657. // Note: we have a ref to the dentry from tracefs_start_creating()
  658. rei = get_root_inode(ei);
  659. rei->events_dir = dentry;
  660. ei->entries = entries;
  661. ei->nr_entries = size;
  662. ei->data = data;
  663. /* Save the ownership of this directory */
  664. uid = d_inode(dentry->d_parent)->i_uid;
  665. gid = d_inode(dentry->d_parent)->i_gid;
  666. /*
  667. * The ei->attr will be used as the default values for the
  668. * files beneath this directory.
  669. */
  670. ei->attr.uid = uid;
  671. ei->attr.gid = gid;
  672. INIT_LIST_HEAD(&ei->children);
  673. INIT_LIST_HEAD(&ei->list);
  674. ti = get_tracefs(inode);
  675. ti->flags |= TRACEFS_EVENT_INODE;
  676. ti->private = ei;
  677. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  678. inode->i_uid = uid;
  679. inode->i_gid = gid;
  680. inode->i_op = &eventfs_dir_inode_operations;
  681. inode->i_fop = &eventfs_file_operations;
  682. dentry->d_fsdata = get_ei(ei);
  683. /*
  684. * Keep all eventfs directories with i_nlink == 1.
  685. * Due to the dynamic nature of the dentry creations and not
  686. * wanting to add a pointer to the parent eventfs_inode in the
  687. * eventfs_inode structure, keeping the i_nlink in sync with the
  688. * number of directories would cause too much complexity for
  689. * something not worth much. Keeping directory links at 1
  690. * tells userspace not to trust the link number.
  691. */
  692. d_instantiate(dentry, inode);
  693. /* The dentry of the "events" parent does keep track though */
  694. inc_nlink(dentry->d_parent->d_inode);
  695. fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
  696. tracefs_end_creating(dentry);
  697. return ei;
  698. fail:
  699. cleanup_ei(ei);
  700. tracefs_failed_creating(dentry);
  701. return ERR_PTR(-ENOMEM);
  702. }
  703. /**
  704. * eventfs_remove_rec - remove eventfs dir or file from list
  705. * @ei: eventfs_inode to be removed.
  706. * @level: prevent recursion from going more than 3 levels deep.
  707. *
  708. * This function recursively removes eventfs_inodes which
  709. * contains info of files and/or directories.
  710. */
  711. static void eventfs_remove_rec(struct eventfs_inode *ei, int level)
  712. {
  713. struct eventfs_inode *ei_child;
  714. /*
  715. * Check recursion depth. It should never be greater than 3:
  716. * 0 - events/
  717. * 1 - events/group/
  718. * 2 - events/group/event/
  719. * 3 - events/group/event/file
  720. */
  721. if (WARN_ON_ONCE(level > 3))
  722. return;
  723. /* search for nested folders or files */
  724. list_for_each_entry(ei_child, &ei->children, list)
  725. eventfs_remove_rec(ei_child, level + 1);
  726. list_del_rcu(&ei->list);
  727. free_ei(ei);
  728. }
  729. /**
  730. * eventfs_remove_dir - remove eventfs dir or file from list
  731. * @ei: eventfs_inode to be removed.
  732. *
  733. * This function acquire the eventfs_mutex lock and call eventfs_remove_rec()
  734. */
  735. void eventfs_remove_dir(struct eventfs_inode *ei)
  736. {
  737. if (!ei)
  738. return;
  739. mutex_lock(&eventfs_mutex);
  740. eventfs_remove_rec(ei, 0);
  741. mutex_unlock(&eventfs_mutex);
  742. }
  743. /**
  744. * eventfs_remove_events_dir - remove the top level eventfs directory
  745. * @ei: the event_inode returned by eventfs_create_events_dir().
  746. *
  747. * This function removes the events main directory
  748. */
  749. void eventfs_remove_events_dir(struct eventfs_inode *ei)
  750. {
  751. struct eventfs_root_inode *rei;
  752. struct dentry *dentry;
  753. rei = get_root_inode(ei);
  754. dentry = rei->events_dir;
  755. if (!dentry)
  756. return;
  757. rei->events_dir = NULL;
  758. eventfs_remove_dir(ei);
  759. /*
  760. * Matches the dget() done by tracefs_start_creating()
  761. * in eventfs_create_events_dir() when it the dentry was
  762. * created. In other words, it's a normal dentry that
  763. * sticks around while the other ei->dentry are created
  764. * and destroyed dynamically.
  765. */
  766. d_invalidate(dentry);
  767. dput(dentry);
  768. }