generic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * proc/fs/generic.c --- generic routines for the proc-fs
  4. *
  5. * This file contains generic proc-fs routines for handling
  6. * directories and files.
  7. *
  8. * Copyright (C) 1991, 1992 Linus Torvalds.
  9. * Copyright (C) 1997 Theodore Ts'o
  10. */
  11. #include <linux/cache.h>
  12. #include <linux/errno.h>
  13. #include <linux/time.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/stat.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/namei.h>
  19. #include <linux/slab.h>
  20. #include <linux/printk.h>
  21. #include <linux/mount.h>
  22. #include <linux/init.h>
  23. #include <linux/idr.h>
  24. #include <linux/bitops.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/completion.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/seq_file.h>
  29. #include "internal.h"
  30. static DEFINE_RWLOCK(proc_subdir_lock);
  31. struct kmem_cache *proc_dir_entry_cache __ro_after_init;
  32. void pde_free(struct proc_dir_entry *pde)
  33. {
  34. if (S_ISLNK(pde->mode))
  35. kfree(pde->data);
  36. if (pde->name != pde->inline_name)
  37. kfree(pde->name);
  38. kmem_cache_free(proc_dir_entry_cache, pde);
  39. }
  40. static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
  41. {
  42. if (len < de->namelen)
  43. return -1;
  44. if (len > de->namelen)
  45. return 1;
  46. return memcmp(name, de->name, len);
  47. }
  48. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  49. {
  50. return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
  51. subdir_node);
  52. }
  53. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  54. {
  55. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  56. subdir_node);
  57. }
  58. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  59. const char *name,
  60. unsigned int len)
  61. {
  62. struct rb_node *node = dir->subdir.rb_node;
  63. while (node) {
  64. struct proc_dir_entry *de = rb_entry(node,
  65. struct proc_dir_entry,
  66. subdir_node);
  67. int result = proc_match(name, de, len);
  68. if (result < 0)
  69. node = node->rb_left;
  70. else if (result > 0)
  71. node = node->rb_right;
  72. else
  73. return de;
  74. }
  75. return NULL;
  76. }
  77. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  78. struct proc_dir_entry *de)
  79. {
  80. struct rb_root *root = &dir->subdir;
  81. struct rb_node **new = &root->rb_node, *parent = NULL;
  82. /* Figure out where to put new node */
  83. while (*new) {
  84. struct proc_dir_entry *this = rb_entry(*new,
  85. struct proc_dir_entry,
  86. subdir_node);
  87. int result = proc_match(de->name, this, de->namelen);
  88. parent = *new;
  89. if (result < 0)
  90. new = &(*new)->rb_left;
  91. else if (result > 0)
  92. new = &(*new)->rb_right;
  93. else
  94. return false;
  95. }
  96. /* Add new node and rebalance tree. */
  97. rb_link_node(&de->subdir_node, parent, new);
  98. rb_insert_color(&de->subdir_node, root);
  99. return true;
  100. }
  101. static int proc_notify_change(struct mnt_idmap *idmap,
  102. struct dentry *dentry, struct iattr *iattr)
  103. {
  104. struct inode *inode = d_inode(dentry);
  105. struct proc_dir_entry *de = PDE(inode);
  106. int error;
  107. error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
  108. if (error)
  109. return error;
  110. setattr_copy(&nop_mnt_idmap, inode, iattr);
  111. proc_set_user(de, inode->i_uid, inode->i_gid);
  112. de->mode = inode->i_mode;
  113. return 0;
  114. }
  115. static int proc_getattr(struct mnt_idmap *idmap,
  116. const struct path *path, struct kstat *stat,
  117. u32 request_mask, unsigned int query_flags)
  118. {
  119. struct inode *inode = d_inode(path->dentry);
  120. struct proc_dir_entry *de = PDE(inode);
  121. if (de) {
  122. nlink_t nlink = READ_ONCE(de->nlink);
  123. if (nlink > 0) {
  124. set_nlink(inode, nlink);
  125. }
  126. }
  127. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  128. return 0;
  129. }
  130. static const struct inode_operations proc_file_inode_operations = {
  131. .setattr = proc_notify_change,
  132. };
  133. /*
  134. * This function parses a name such as "tty/driver/serial", and
  135. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  136. * returns "serial" in residual.
  137. */
  138. static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  139. const char **residual)
  140. {
  141. const char *cp = name, *next;
  142. struct proc_dir_entry *de;
  143. de = *ret ?: &proc_root;
  144. while ((next = strchr(cp, '/')) != NULL) {
  145. de = pde_subdir_find(de, cp, next - cp);
  146. if (!de) {
  147. WARN(1, "name '%s'\n", name);
  148. return -ENOENT;
  149. }
  150. cp = next + 1;
  151. }
  152. *residual = cp;
  153. *ret = de;
  154. return 0;
  155. }
  156. static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  157. const char **residual)
  158. {
  159. int rv;
  160. read_lock(&proc_subdir_lock);
  161. rv = __xlate_proc_name(name, ret, residual);
  162. read_unlock(&proc_subdir_lock);
  163. return rv;
  164. }
  165. static DEFINE_IDA(proc_inum_ida);
  166. #define PROC_DYNAMIC_FIRST 0xF0000000U
  167. /*
  168. * Return an inode number between PROC_DYNAMIC_FIRST and
  169. * 0xffffffff, or zero on failure.
  170. */
  171. int proc_alloc_inum(unsigned int *inum)
  172. {
  173. int i;
  174. i = ida_alloc_max(&proc_inum_ida, UINT_MAX - PROC_DYNAMIC_FIRST,
  175. GFP_KERNEL);
  176. if (i < 0)
  177. return i;
  178. *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
  179. return 0;
  180. }
  181. void proc_free_inum(unsigned int inum)
  182. {
  183. ida_free(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  184. }
  185. static int proc_misc_d_revalidate(struct inode *dir, const struct qstr *name,
  186. struct dentry *dentry, unsigned int flags)
  187. {
  188. if (flags & LOOKUP_RCU)
  189. return -ECHILD;
  190. if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0)
  191. return 0; /* revalidate */
  192. return 1;
  193. }
  194. static int proc_misc_d_delete(const struct dentry *dentry)
  195. {
  196. return atomic_read(&PDE(d_inode(dentry))->in_use) < 0;
  197. }
  198. static const struct dentry_operations proc_misc_dentry_ops = {
  199. .d_revalidate = proc_misc_d_revalidate,
  200. .d_delete = proc_misc_d_delete,
  201. };
  202. /*
  203. * Don't create negative dentries here, return -ENOENT by hand
  204. * instead.
  205. */
  206. struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
  207. struct proc_dir_entry *de)
  208. {
  209. struct inode *inode;
  210. read_lock(&proc_subdir_lock);
  211. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  212. if (de) {
  213. pde_get(de);
  214. read_unlock(&proc_subdir_lock);
  215. inode = proc_get_inode(dir->i_sb, de);
  216. if (!inode)
  217. return ERR_PTR(-ENOMEM);
  218. if (de->flags & PROC_ENTRY_FORCE_LOOKUP)
  219. return d_splice_alias_ops(inode, dentry,
  220. &proc_net_dentry_ops);
  221. return d_splice_alias_ops(inode, dentry,
  222. &proc_misc_dentry_ops);
  223. }
  224. read_unlock(&proc_subdir_lock);
  225. return ERR_PTR(-ENOENT);
  226. }
  227. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  228. unsigned int flags)
  229. {
  230. struct proc_fs_info *fs_info = proc_sb_info(dir->i_sb);
  231. if (fs_info->pidonly == PROC_PIDONLY_ON)
  232. return ERR_PTR(-ENOENT);
  233. return proc_lookup_de(dir, dentry, PDE(dir));
  234. }
  235. /*
  236. * This returns non-zero if at EOF, so that the /proc
  237. * root directory can use this and check if it should
  238. * continue with the <pid> entries..
  239. *
  240. * Note that the VFS-layer doesn't care about the return
  241. * value of the readdir() call, as long as it's non-negative
  242. * for success..
  243. */
  244. int proc_readdir_de(struct file *file, struct dir_context *ctx,
  245. struct proc_dir_entry *de)
  246. {
  247. int i;
  248. if (!dir_emit_dots(file, ctx))
  249. return 0;
  250. i = ctx->pos - 2;
  251. read_lock(&proc_subdir_lock);
  252. de = pde_subdir_first(de);
  253. for (;;) {
  254. if (!de) {
  255. read_unlock(&proc_subdir_lock);
  256. return 0;
  257. }
  258. if (!i)
  259. break;
  260. de = pde_subdir_next(de);
  261. i--;
  262. }
  263. do {
  264. struct proc_dir_entry *next;
  265. pde_get(de);
  266. read_unlock(&proc_subdir_lock);
  267. if (!dir_emit(ctx, de->name, de->namelen,
  268. de->low_ino, de->mode >> 12)) {
  269. pde_put(de);
  270. return 0;
  271. }
  272. ctx->pos++;
  273. read_lock(&proc_subdir_lock);
  274. next = pde_subdir_next(de);
  275. pde_put(de);
  276. de = next;
  277. } while (de);
  278. read_unlock(&proc_subdir_lock);
  279. return 1;
  280. }
  281. int proc_readdir(struct file *file, struct dir_context *ctx)
  282. {
  283. struct inode *inode = file_inode(file);
  284. struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
  285. if (fs_info->pidonly == PROC_PIDONLY_ON)
  286. return 1;
  287. return proc_readdir_de(file, ctx, PDE(inode));
  288. }
  289. /*
  290. * These are the generic /proc directory operations. They
  291. * use the in-memory "struct proc_dir_entry" tree to parse
  292. * the /proc directory.
  293. */
  294. static const struct file_operations proc_dir_operations = {
  295. .llseek = generic_file_llseek,
  296. .read = generic_read_dir,
  297. .iterate_shared = proc_readdir,
  298. };
  299. static int proc_net_d_revalidate(struct inode *dir, const struct qstr *name,
  300. struct dentry *dentry, unsigned int flags)
  301. {
  302. return 0;
  303. }
  304. const struct dentry_operations proc_net_dentry_ops = {
  305. .d_revalidate = proc_net_d_revalidate,
  306. .d_delete = always_delete_dentry,
  307. };
  308. /*
  309. * proc directories can do almost nothing..
  310. */
  311. static const struct inode_operations proc_dir_inode_operations = {
  312. .lookup = proc_lookup,
  313. .getattr = proc_getattr,
  314. .setattr = proc_notify_change,
  315. };
  316. static void pde_set_flags(struct proc_dir_entry *pde)
  317. {
  318. const struct proc_ops *proc_ops = pde->proc_ops;
  319. if (!proc_ops)
  320. return;
  321. if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
  322. pde->flags |= PROC_ENTRY_PERMANENT;
  323. if (proc_ops->proc_read_iter)
  324. pde->flags |= PROC_ENTRY_proc_read_iter;
  325. #ifdef CONFIG_COMPAT
  326. if (proc_ops->proc_compat_ioctl)
  327. pde->flags |= PROC_ENTRY_proc_compat_ioctl;
  328. #endif
  329. if (proc_ops->proc_lseek)
  330. pde->flags |= PROC_ENTRY_proc_lseek;
  331. }
  332. /* returns the registered entry, or frees dp and returns NULL on failure */
  333. struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
  334. struct proc_dir_entry *dp)
  335. {
  336. if (proc_alloc_inum(&dp->low_ino))
  337. goto out_free_entry;
  338. if (!S_ISDIR(dp->mode))
  339. pde_set_flags(dp);
  340. write_lock(&proc_subdir_lock);
  341. dp->parent = dir;
  342. if (pde_subdir_insert(dir, dp) == false) {
  343. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  344. dir->name, dp->name);
  345. write_unlock(&proc_subdir_lock);
  346. goto out_free_inum;
  347. }
  348. dir->nlink++;
  349. write_unlock(&proc_subdir_lock);
  350. return dp;
  351. out_free_inum:
  352. proc_free_inum(dp->low_ino);
  353. out_free_entry:
  354. pde_free(dp);
  355. return NULL;
  356. }
  357. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  358. const char *name,
  359. umode_t mode,
  360. nlink_t nlink)
  361. {
  362. struct proc_dir_entry *ent = NULL;
  363. const char *fn;
  364. struct qstr qstr;
  365. if (xlate_proc_name(name, parent, &fn) != 0)
  366. goto out;
  367. qstr.name = fn;
  368. qstr.len = strlen(fn);
  369. if (qstr.len == 0 || qstr.len >= 256) {
  370. WARN(1, "name len %u\n", qstr.len);
  371. return NULL;
  372. }
  373. if (qstr.len == 1 && fn[0] == '.') {
  374. WARN(1, "name '.'\n");
  375. return NULL;
  376. }
  377. if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
  378. WARN(1, "name '..'\n");
  379. return NULL;
  380. }
  381. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  382. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  383. return NULL;
  384. }
  385. if (is_empty_pde(*parent)) {
  386. WARN(1, "attempt to add to permanently empty directory");
  387. return NULL;
  388. }
  389. ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  390. if (!ent)
  391. goto out;
  392. if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) {
  393. ent->name = ent->inline_name;
  394. } else {
  395. ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
  396. if (!ent->name) {
  397. pde_free(ent);
  398. return NULL;
  399. }
  400. }
  401. memcpy(ent->name, fn, qstr.len + 1);
  402. ent->namelen = qstr.len;
  403. ent->mode = mode;
  404. ent->nlink = nlink;
  405. ent->subdir = RB_ROOT;
  406. refcount_set(&ent->refcnt, 1);
  407. spin_lock_init(&ent->pde_unload_lock);
  408. INIT_LIST_HEAD(&ent->pde_openers);
  409. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  410. /* Revalidate everything under /proc/${pid}/net */
  411. if ((*parent)->flags & PROC_ENTRY_FORCE_LOOKUP)
  412. pde_force_lookup(ent);
  413. out:
  414. return ent;
  415. }
  416. struct proc_dir_entry *proc_symlink(const char *name,
  417. struct proc_dir_entry *parent, const char *dest)
  418. {
  419. struct proc_dir_entry *ent;
  420. ent = __proc_create(&parent, name,
  421. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  422. if (ent) {
  423. ent->size = strlen(dest);
  424. ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL);
  425. if (ent->data) {
  426. ent->proc_iops = &proc_link_inode_operations;
  427. ent = proc_register(parent, ent);
  428. } else {
  429. pde_free(ent);
  430. ent = NULL;
  431. }
  432. }
  433. return ent;
  434. }
  435. EXPORT_SYMBOL(proc_symlink);
  436. struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode,
  437. struct proc_dir_entry *parent, void *data, bool force_lookup)
  438. {
  439. struct proc_dir_entry *ent;
  440. if (mode == 0)
  441. mode = S_IRUGO | S_IXUGO;
  442. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  443. if (ent) {
  444. ent->data = data;
  445. ent->proc_dir_ops = &proc_dir_operations;
  446. ent->proc_iops = &proc_dir_inode_operations;
  447. if (force_lookup) {
  448. pde_force_lookup(ent);
  449. }
  450. ent = proc_register(parent, ent);
  451. }
  452. return ent;
  453. }
  454. EXPORT_SYMBOL_GPL(_proc_mkdir);
  455. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  456. struct proc_dir_entry *parent, void *data)
  457. {
  458. return _proc_mkdir(name, mode, parent, data, false);
  459. }
  460. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  461. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  462. struct proc_dir_entry *parent)
  463. {
  464. return proc_mkdir_data(name, mode, parent, NULL);
  465. }
  466. EXPORT_SYMBOL(proc_mkdir_mode);
  467. struct proc_dir_entry *proc_mkdir(const char *name,
  468. struct proc_dir_entry *parent)
  469. {
  470. return proc_mkdir_data(name, 0, parent, NULL);
  471. }
  472. EXPORT_SYMBOL(proc_mkdir);
  473. struct proc_dir_entry *proc_create_mount_point(const char *name)
  474. {
  475. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  476. struct proc_dir_entry *ent, *parent = NULL;
  477. ent = __proc_create(&parent, name, mode, 2);
  478. if (ent) {
  479. ent->data = NULL;
  480. ent->proc_dir_ops = NULL;
  481. ent->proc_iops = NULL;
  482. ent = proc_register(parent, ent);
  483. }
  484. return ent;
  485. }
  486. EXPORT_SYMBOL(proc_create_mount_point);
  487. struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
  488. struct proc_dir_entry **parent, void *data)
  489. {
  490. struct proc_dir_entry *p;
  491. if ((mode & S_IFMT) == 0)
  492. mode |= S_IFREG;
  493. if ((mode & S_IALLUGO) == 0)
  494. mode |= S_IRUGO;
  495. if (WARN_ON_ONCE(!S_ISREG(mode)))
  496. return NULL;
  497. p = __proc_create(parent, name, mode, 1);
  498. if (p) {
  499. p->proc_iops = &proc_file_inode_operations;
  500. p->data = data;
  501. }
  502. return p;
  503. }
  504. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  505. struct proc_dir_entry *parent,
  506. const struct proc_ops *proc_ops, void *data)
  507. {
  508. struct proc_dir_entry *p;
  509. p = proc_create_reg(name, mode, &parent, data);
  510. if (!p)
  511. return NULL;
  512. p->proc_ops = proc_ops;
  513. return proc_register(parent, p);
  514. }
  515. EXPORT_SYMBOL(proc_create_data);
  516. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  517. struct proc_dir_entry *parent,
  518. const struct proc_ops *proc_ops)
  519. {
  520. return proc_create_data(name, mode, parent, proc_ops, NULL);
  521. }
  522. EXPORT_SYMBOL(proc_create);
  523. static int proc_seq_open(struct inode *inode, struct file *file)
  524. {
  525. struct proc_dir_entry *de = PDE(inode);
  526. if (de->state_size)
  527. return seq_open_private(file, de->seq_ops, de->state_size);
  528. return seq_open(file, de->seq_ops);
  529. }
  530. static int proc_seq_release(struct inode *inode, struct file *file)
  531. {
  532. struct proc_dir_entry *de = PDE(inode);
  533. if (de->state_size)
  534. return seq_release_private(inode, file);
  535. return seq_release(inode, file);
  536. }
  537. static const struct proc_ops proc_seq_ops = {
  538. /* not permanent -- can call into arbitrary seq_operations */
  539. .proc_open = proc_seq_open,
  540. .proc_read_iter = seq_read_iter,
  541. .proc_lseek = seq_lseek,
  542. .proc_release = proc_seq_release,
  543. };
  544. struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
  545. struct proc_dir_entry *parent, const struct seq_operations *ops,
  546. unsigned int state_size, void *data)
  547. {
  548. struct proc_dir_entry *p;
  549. p = proc_create_reg(name, mode, &parent, data);
  550. if (!p)
  551. return NULL;
  552. p->proc_ops = &proc_seq_ops;
  553. p->seq_ops = ops;
  554. p->state_size = state_size;
  555. return proc_register(parent, p);
  556. }
  557. EXPORT_SYMBOL(proc_create_seq_private);
  558. static int proc_single_open(struct inode *inode, struct file *file)
  559. {
  560. struct proc_dir_entry *de = PDE(inode);
  561. return single_open(file, de->single_show, de->data);
  562. }
  563. static const struct proc_ops proc_single_ops = {
  564. /* not permanent -- can call into arbitrary ->single_show */
  565. .proc_open = proc_single_open,
  566. .proc_read_iter = seq_read_iter,
  567. .proc_lseek = seq_lseek,
  568. .proc_release = single_release,
  569. };
  570. struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
  571. struct proc_dir_entry *parent,
  572. int (*show)(struct seq_file *, void *), void *data)
  573. {
  574. struct proc_dir_entry *p;
  575. p = proc_create_reg(name, mode, &parent, data);
  576. if (!p)
  577. return NULL;
  578. p->proc_ops = &proc_single_ops;
  579. p->single_show = show;
  580. return proc_register(parent, p);
  581. }
  582. EXPORT_SYMBOL(proc_create_single_data);
  583. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  584. {
  585. de->size = size;
  586. }
  587. EXPORT_SYMBOL(proc_set_size);
  588. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  589. {
  590. de->uid = uid;
  591. de->gid = gid;
  592. }
  593. EXPORT_SYMBOL(proc_set_user);
  594. void pde_put(struct proc_dir_entry *pde)
  595. {
  596. if (refcount_dec_and_test(&pde->refcnt)) {
  597. proc_free_inum(pde->low_ino);
  598. pde_free(pde);
  599. }
  600. }
  601. static void pde_erase(struct proc_dir_entry *pde, struct proc_dir_entry *parent)
  602. {
  603. rb_erase(&pde->subdir_node, &parent->subdir);
  604. RB_CLEAR_NODE(&pde->subdir_node);
  605. }
  606. /*
  607. * Remove a /proc entry and free it if it's not currently in use.
  608. */
  609. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  610. {
  611. struct proc_dir_entry *de = NULL;
  612. const char *fn = name;
  613. unsigned int len;
  614. write_lock(&proc_subdir_lock);
  615. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  616. write_unlock(&proc_subdir_lock);
  617. return;
  618. }
  619. len = strlen(fn);
  620. de = pde_subdir_find(parent, fn, len);
  621. if (de) {
  622. if (unlikely(pde_is_permanent(de))) {
  623. WARN(1, "removing permanent /proc entry '%s'", de->name);
  624. de = NULL;
  625. } else {
  626. pde_erase(de, parent);
  627. if (S_ISDIR(de->mode))
  628. parent->nlink--;
  629. }
  630. }
  631. write_unlock(&proc_subdir_lock);
  632. if (!de) {
  633. WARN(1, "name '%s'\n", name);
  634. return;
  635. }
  636. proc_entry_rundown(de);
  637. WARN(pde_subdir_first(de),
  638. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  639. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  640. pde_put(de);
  641. }
  642. EXPORT_SYMBOL(remove_proc_entry);
  643. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  644. {
  645. struct proc_dir_entry *root = NULL, *de, *next;
  646. const char *fn = name;
  647. unsigned int len;
  648. write_lock(&proc_subdir_lock);
  649. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  650. write_unlock(&proc_subdir_lock);
  651. return -ENOENT;
  652. }
  653. len = strlen(fn);
  654. root = pde_subdir_find(parent, fn, len);
  655. if (!root) {
  656. write_unlock(&proc_subdir_lock);
  657. return -ENOENT;
  658. }
  659. if (unlikely(pde_is_permanent(root))) {
  660. write_unlock(&proc_subdir_lock);
  661. WARN(1, "removing permanent /proc entry '%s/%s'",
  662. root->parent->name, root->name);
  663. return -EINVAL;
  664. }
  665. pde_erase(root, parent);
  666. de = root;
  667. while (1) {
  668. next = pde_subdir_first(de);
  669. if (next) {
  670. if (unlikely(pde_is_permanent(next))) {
  671. write_unlock(&proc_subdir_lock);
  672. WARN(1, "removing permanent /proc entry '%s/%s'",
  673. next->parent->name, next->name);
  674. return -EINVAL;
  675. }
  676. pde_erase(next, de);
  677. de = next;
  678. continue;
  679. }
  680. next = de->parent;
  681. if (S_ISDIR(de->mode))
  682. next->nlink--;
  683. write_unlock(&proc_subdir_lock);
  684. proc_entry_rundown(de);
  685. if (de == root)
  686. break;
  687. pde_put(de);
  688. write_lock(&proc_subdir_lock);
  689. de = next;
  690. }
  691. pde_put(root);
  692. return 0;
  693. }
  694. EXPORT_SYMBOL(remove_proc_subtree);
  695. void *proc_get_parent_data(const struct inode *inode)
  696. {
  697. struct proc_dir_entry *de = PDE(inode);
  698. return de->parent->data;
  699. }
  700. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  701. void proc_remove(struct proc_dir_entry *de)
  702. {
  703. if (de)
  704. remove_proc_subtree(de->name, de->parent);
  705. }
  706. EXPORT_SYMBOL(proc_remove);
  707. /*
  708. * Pull a user buffer into memory and pass it to the file's write handler if
  709. * one is supplied. The ->write() method is permitted to modify the
  710. * kernel-side buffer.
  711. */
  712. ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
  713. loff_t *_pos)
  714. {
  715. struct proc_dir_entry *pde = PDE(file_inode(f));
  716. char *buf;
  717. int ret;
  718. if (!pde->write)
  719. return -EACCES;
  720. if (size == 0 || size > PAGE_SIZE - 1)
  721. return -EINVAL;
  722. buf = memdup_user_nul(ubuf, size);
  723. if (IS_ERR(buf))
  724. return PTR_ERR(buf);
  725. ret = pde->write(f, buf, size);
  726. kfree(buf);
  727. return ret == 0 ? size : ret;
  728. }