generic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 dentry *dentry, unsigned int flags)
  186. {
  187. if (flags & LOOKUP_RCU)
  188. return -ECHILD;
  189. if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0)
  190. return 0; /* revalidate */
  191. return 1;
  192. }
  193. static int proc_misc_d_delete(const struct dentry *dentry)
  194. {
  195. return atomic_read(&PDE(d_inode(dentry))->in_use) < 0;
  196. }
  197. static const struct dentry_operations proc_misc_dentry_ops = {
  198. .d_revalidate = proc_misc_d_revalidate,
  199. .d_delete = proc_misc_d_delete,
  200. };
  201. /*
  202. * Don't create negative dentries here, return -ENOENT by hand
  203. * instead.
  204. */
  205. struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
  206. struct proc_dir_entry *de)
  207. {
  208. struct inode *inode;
  209. read_lock(&proc_subdir_lock);
  210. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  211. if (de) {
  212. pde_get(de);
  213. read_unlock(&proc_subdir_lock);
  214. inode = proc_get_inode(dir->i_sb, de);
  215. if (!inode)
  216. return ERR_PTR(-ENOMEM);
  217. d_set_d_op(dentry, de->proc_dops);
  218. return d_splice_alias(inode, dentry);
  219. }
  220. read_unlock(&proc_subdir_lock);
  221. return ERR_PTR(-ENOENT);
  222. }
  223. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  224. unsigned int flags)
  225. {
  226. struct proc_fs_info *fs_info = proc_sb_info(dir->i_sb);
  227. if (fs_info->pidonly == PROC_PIDONLY_ON)
  228. return ERR_PTR(-ENOENT);
  229. return proc_lookup_de(dir, dentry, PDE(dir));
  230. }
  231. /*
  232. * This returns non-zero if at EOF, so that the /proc
  233. * root directory can use this and check if it should
  234. * continue with the <pid> entries..
  235. *
  236. * Note that the VFS-layer doesn't care about the return
  237. * value of the readdir() call, as long as it's non-negative
  238. * for success..
  239. */
  240. int proc_readdir_de(struct file *file, struct dir_context *ctx,
  241. struct proc_dir_entry *de)
  242. {
  243. int i;
  244. if (!dir_emit_dots(file, ctx))
  245. return 0;
  246. i = ctx->pos - 2;
  247. read_lock(&proc_subdir_lock);
  248. de = pde_subdir_first(de);
  249. for (;;) {
  250. if (!de) {
  251. read_unlock(&proc_subdir_lock);
  252. return 0;
  253. }
  254. if (!i)
  255. break;
  256. de = pde_subdir_next(de);
  257. i--;
  258. }
  259. do {
  260. struct proc_dir_entry *next;
  261. pde_get(de);
  262. read_unlock(&proc_subdir_lock);
  263. if (!dir_emit(ctx, de->name, de->namelen,
  264. de->low_ino, de->mode >> 12)) {
  265. pde_put(de);
  266. return 0;
  267. }
  268. ctx->pos++;
  269. read_lock(&proc_subdir_lock);
  270. next = pde_subdir_next(de);
  271. pde_put(de);
  272. de = next;
  273. } while (de);
  274. read_unlock(&proc_subdir_lock);
  275. return 1;
  276. }
  277. int proc_readdir(struct file *file, struct dir_context *ctx)
  278. {
  279. struct inode *inode = file_inode(file);
  280. struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
  281. if (fs_info->pidonly == PROC_PIDONLY_ON)
  282. return 1;
  283. return proc_readdir_de(file, ctx, PDE(inode));
  284. }
  285. /*
  286. * These are the generic /proc directory operations. They
  287. * use the in-memory "struct proc_dir_entry" tree to parse
  288. * the /proc directory.
  289. */
  290. static const struct file_operations proc_dir_operations = {
  291. .llseek = generic_file_llseek,
  292. .read = generic_read_dir,
  293. .iterate_shared = proc_readdir,
  294. };
  295. static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
  296. {
  297. return 0;
  298. }
  299. const struct dentry_operations proc_net_dentry_ops = {
  300. .d_revalidate = proc_net_d_revalidate,
  301. .d_delete = always_delete_dentry,
  302. };
  303. /*
  304. * proc directories can do almost nothing..
  305. */
  306. static const struct inode_operations proc_dir_inode_operations = {
  307. .lookup = proc_lookup,
  308. .getattr = proc_getattr,
  309. .setattr = proc_notify_change,
  310. };
  311. static void pde_set_flags(struct proc_dir_entry *pde)
  312. {
  313. const struct proc_ops *proc_ops = pde->proc_ops;
  314. if (!proc_ops)
  315. return;
  316. if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
  317. pde->flags |= PROC_ENTRY_PERMANENT;
  318. if (proc_ops->proc_read_iter)
  319. pde->flags |= PROC_ENTRY_proc_read_iter;
  320. #ifdef CONFIG_COMPAT
  321. if (proc_ops->proc_compat_ioctl)
  322. pde->flags |= PROC_ENTRY_proc_compat_ioctl;
  323. #endif
  324. if (proc_ops->proc_lseek)
  325. pde->flags |= PROC_ENTRY_proc_lseek;
  326. }
  327. /* returns the registered entry, or frees dp and returns NULL on failure */
  328. struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
  329. struct proc_dir_entry *dp)
  330. {
  331. if (proc_alloc_inum(&dp->low_ino))
  332. goto out_free_entry;
  333. if (!S_ISDIR(dp->mode))
  334. pde_set_flags(dp);
  335. write_lock(&proc_subdir_lock);
  336. dp->parent = dir;
  337. if (pde_subdir_insert(dir, dp) == false) {
  338. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  339. dir->name, dp->name);
  340. write_unlock(&proc_subdir_lock);
  341. goto out_free_inum;
  342. }
  343. dir->nlink++;
  344. write_unlock(&proc_subdir_lock);
  345. return dp;
  346. out_free_inum:
  347. proc_free_inum(dp->low_ino);
  348. out_free_entry:
  349. pde_free(dp);
  350. return NULL;
  351. }
  352. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  353. const char *name,
  354. umode_t mode,
  355. nlink_t nlink)
  356. {
  357. struct proc_dir_entry *ent = NULL;
  358. const char *fn;
  359. struct qstr qstr;
  360. if (xlate_proc_name(name, parent, &fn) != 0)
  361. goto out;
  362. qstr.name = fn;
  363. qstr.len = strlen(fn);
  364. if (qstr.len == 0 || qstr.len >= 256) {
  365. WARN(1, "name len %u\n", qstr.len);
  366. return NULL;
  367. }
  368. if (qstr.len == 1 && fn[0] == '.') {
  369. WARN(1, "name '.'\n");
  370. return NULL;
  371. }
  372. if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
  373. WARN(1, "name '..'\n");
  374. return NULL;
  375. }
  376. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  377. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  378. return NULL;
  379. }
  380. if (is_empty_pde(*parent)) {
  381. WARN(1, "attempt to add to permanently empty directory");
  382. return NULL;
  383. }
  384. ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  385. if (!ent)
  386. goto out;
  387. if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) {
  388. ent->name = ent->inline_name;
  389. } else {
  390. ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
  391. if (!ent->name) {
  392. pde_free(ent);
  393. return NULL;
  394. }
  395. }
  396. memcpy(ent->name, fn, qstr.len + 1);
  397. ent->namelen = qstr.len;
  398. ent->mode = mode;
  399. ent->nlink = nlink;
  400. ent->subdir = RB_ROOT;
  401. refcount_set(&ent->refcnt, 1);
  402. spin_lock_init(&ent->pde_unload_lock);
  403. INIT_LIST_HEAD(&ent->pde_openers);
  404. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  405. ent->proc_dops = &proc_misc_dentry_ops;
  406. /* Revalidate everything under /proc/${pid}/net */
  407. if ((*parent)->proc_dops == &proc_net_dentry_ops)
  408. pde_force_lookup(ent);
  409. out:
  410. return ent;
  411. }
  412. struct proc_dir_entry *proc_symlink(const char *name,
  413. struct proc_dir_entry *parent, const char *dest)
  414. {
  415. struct proc_dir_entry *ent;
  416. ent = __proc_create(&parent, name,
  417. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  418. if (ent) {
  419. ent->size = strlen(dest);
  420. ent->data = kmemdup(dest, ent->size + 1, GFP_KERNEL);
  421. if (ent->data) {
  422. ent->proc_iops = &proc_link_inode_operations;
  423. ent = proc_register(parent, ent);
  424. } else {
  425. pde_free(ent);
  426. ent = NULL;
  427. }
  428. }
  429. return ent;
  430. }
  431. EXPORT_SYMBOL(proc_symlink);
  432. struct proc_dir_entry *_proc_mkdir(const char *name, umode_t mode,
  433. struct proc_dir_entry *parent, void *data, bool force_lookup)
  434. {
  435. struct proc_dir_entry *ent;
  436. if (mode == 0)
  437. mode = S_IRUGO | S_IXUGO;
  438. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  439. if (ent) {
  440. ent->data = data;
  441. ent->proc_dir_ops = &proc_dir_operations;
  442. ent->proc_iops = &proc_dir_inode_operations;
  443. if (force_lookup) {
  444. pde_force_lookup(ent);
  445. }
  446. ent = proc_register(parent, ent);
  447. }
  448. return ent;
  449. }
  450. EXPORT_SYMBOL_GPL(_proc_mkdir);
  451. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  452. struct proc_dir_entry *parent, void *data)
  453. {
  454. return _proc_mkdir(name, mode, parent, data, false);
  455. }
  456. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  457. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  458. struct proc_dir_entry *parent)
  459. {
  460. return proc_mkdir_data(name, mode, parent, NULL);
  461. }
  462. EXPORT_SYMBOL(proc_mkdir_mode);
  463. struct proc_dir_entry *proc_mkdir(const char *name,
  464. struct proc_dir_entry *parent)
  465. {
  466. return proc_mkdir_data(name, 0, parent, NULL);
  467. }
  468. EXPORT_SYMBOL(proc_mkdir);
  469. struct proc_dir_entry *proc_create_mount_point(const char *name)
  470. {
  471. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  472. struct proc_dir_entry *ent, *parent = NULL;
  473. ent = __proc_create(&parent, name, mode, 2);
  474. if (ent) {
  475. ent->data = NULL;
  476. ent->proc_dir_ops = NULL;
  477. ent->proc_iops = NULL;
  478. ent = proc_register(parent, ent);
  479. }
  480. return ent;
  481. }
  482. EXPORT_SYMBOL(proc_create_mount_point);
  483. struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
  484. struct proc_dir_entry **parent, void *data)
  485. {
  486. struct proc_dir_entry *p;
  487. if ((mode & S_IFMT) == 0)
  488. mode |= S_IFREG;
  489. if ((mode & S_IALLUGO) == 0)
  490. mode |= S_IRUGO;
  491. if (WARN_ON_ONCE(!S_ISREG(mode)))
  492. return NULL;
  493. p = __proc_create(parent, name, mode, 1);
  494. if (p) {
  495. p->proc_iops = &proc_file_inode_operations;
  496. p->data = data;
  497. }
  498. return p;
  499. }
  500. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  501. struct proc_dir_entry *parent,
  502. const struct proc_ops *proc_ops, void *data)
  503. {
  504. struct proc_dir_entry *p;
  505. p = proc_create_reg(name, mode, &parent, data);
  506. if (!p)
  507. return NULL;
  508. p->proc_ops = proc_ops;
  509. return proc_register(parent, p);
  510. }
  511. EXPORT_SYMBOL(proc_create_data);
  512. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  513. struct proc_dir_entry *parent,
  514. const struct proc_ops *proc_ops)
  515. {
  516. return proc_create_data(name, mode, parent, proc_ops, NULL);
  517. }
  518. EXPORT_SYMBOL(proc_create);
  519. static int proc_seq_open(struct inode *inode, struct file *file)
  520. {
  521. struct proc_dir_entry *de = PDE(inode);
  522. if (de->state_size)
  523. return seq_open_private(file, de->seq_ops, de->state_size);
  524. return seq_open(file, de->seq_ops);
  525. }
  526. static int proc_seq_release(struct inode *inode, struct file *file)
  527. {
  528. struct proc_dir_entry *de = PDE(inode);
  529. if (de->state_size)
  530. return seq_release_private(inode, file);
  531. return seq_release(inode, file);
  532. }
  533. static const struct proc_ops proc_seq_ops = {
  534. /* not permanent -- can call into arbitrary seq_operations */
  535. .proc_open = proc_seq_open,
  536. .proc_read_iter = seq_read_iter,
  537. .proc_lseek = seq_lseek,
  538. .proc_release = proc_seq_release,
  539. };
  540. struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
  541. struct proc_dir_entry *parent, const struct seq_operations *ops,
  542. unsigned int state_size, void *data)
  543. {
  544. struct proc_dir_entry *p;
  545. p = proc_create_reg(name, mode, &parent, data);
  546. if (!p)
  547. return NULL;
  548. p->proc_ops = &proc_seq_ops;
  549. p->seq_ops = ops;
  550. p->state_size = state_size;
  551. return proc_register(parent, p);
  552. }
  553. EXPORT_SYMBOL(proc_create_seq_private);
  554. static int proc_single_open(struct inode *inode, struct file *file)
  555. {
  556. struct proc_dir_entry *de = PDE(inode);
  557. return single_open(file, de->single_show, de->data);
  558. }
  559. static const struct proc_ops proc_single_ops = {
  560. /* not permanent -- can call into arbitrary ->single_show */
  561. .proc_open = proc_single_open,
  562. .proc_read_iter = seq_read_iter,
  563. .proc_lseek = seq_lseek,
  564. .proc_release = single_release,
  565. };
  566. struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
  567. struct proc_dir_entry *parent,
  568. int (*show)(struct seq_file *, void *), void *data)
  569. {
  570. struct proc_dir_entry *p;
  571. p = proc_create_reg(name, mode, &parent, data);
  572. if (!p)
  573. return NULL;
  574. p->proc_ops = &proc_single_ops;
  575. p->single_show = show;
  576. return proc_register(parent, p);
  577. }
  578. EXPORT_SYMBOL(proc_create_single_data);
  579. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  580. {
  581. de->size = size;
  582. }
  583. EXPORT_SYMBOL(proc_set_size);
  584. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  585. {
  586. de->uid = uid;
  587. de->gid = gid;
  588. }
  589. EXPORT_SYMBOL(proc_set_user);
  590. void pde_put(struct proc_dir_entry *pde)
  591. {
  592. if (refcount_dec_and_test(&pde->refcnt)) {
  593. proc_free_inum(pde->low_ino);
  594. pde_free(pde);
  595. }
  596. }
  597. /*
  598. * Remove a /proc entry and free it if it's not currently in use.
  599. */
  600. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  601. {
  602. struct proc_dir_entry *de = NULL;
  603. const char *fn = name;
  604. unsigned int len;
  605. write_lock(&proc_subdir_lock);
  606. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  607. write_unlock(&proc_subdir_lock);
  608. return;
  609. }
  610. len = strlen(fn);
  611. de = pde_subdir_find(parent, fn, len);
  612. if (de) {
  613. if (unlikely(pde_is_permanent(de))) {
  614. WARN(1, "removing permanent /proc entry '%s'", de->name);
  615. de = NULL;
  616. } else {
  617. rb_erase(&de->subdir_node, &parent->subdir);
  618. if (S_ISDIR(de->mode))
  619. parent->nlink--;
  620. }
  621. }
  622. write_unlock(&proc_subdir_lock);
  623. if (!de) {
  624. WARN(1, "name '%s'\n", name);
  625. return;
  626. }
  627. proc_entry_rundown(de);
  628. WARN(pde_subdir_first(de),
  629. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  630. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  631. pde_put(de);
  632. }
  633. EXPORT_SYMBOL(remove_proc_entry);
  634. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  635. {
  636. struct proc_dir_entry *root = NULL, *de, *next;
  637. const char *fn = name;
  638. unsigned int len;
  639. write_lock(&proc_subdir_lock);
  640. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  641. write_unlock(&proc_subdir_lock);
  642. return -ENOENT;
  643. }
  644. len = strlen(fn);
  645. root = pde_subdir_find(parent, fn, len);
  646. if (!root) {
  647. write_unlock(&proc_subdir_lock);
  648. return -ENOENT;
  649. }
  650. if (unlikely(pde_is_permanent(root))) {
  651. write_unlock(&proc_subdir_lock);
  652. WARN(1, "removing permanent /proc entry '%s/%s'",
  653. root->parent->name, root->name);
  654. return -EINVAL;
  655. }
  656. rb_erase(&root->subdir_node, &parent->subdir);
  657. de = root;
  658. while (1) {
  659. next = pde_subdir_first(de);
  660. if (next) {
  661. if (unlikely(pde_is_permanent(next))) {
  662. write_unlock(&proc_subdir_lock);
  663. WARN(1, "removing permanent /proc entry '%s/%s'",
  664. next->parent->name, next->name);
  665. return -EINVAL;
  666. }
  667. rb_erase(&next->subdir_node, &de->subdir);
  668. de = next;
  669. continue;
  670. }
  671. next = de->parent;
  672. if (S_ISDIR(de->mode))
  673. next->nlink--;
  674. write_unlock(&proc_subdir_lock);
  675. proc_entry_rundown(de);
  676. if (de == root)
  677. break;
  678. pde_put(de);
  679. write_lock(&proc_subdir_lock);
  680. de = next;
  681. }
  682. pde_put(root);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL(remove_proc_subtree);
  686. void *proc_get_parent_data(const struct inode *inode)
  687. {
  688. struct proc_dir_entry *de = PDE(inode);
  689. return de->parent->data;
  690. }
  691. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  692. void proc_remove(struct proc_dir_entry *de)
  693. {
  694. if (de)
  695. remove_proc_subtree(de->name, de->parent);
  696. }
  697. EXPORT_SYMBOL(proc_remove);
  698. /*
  699. * Pull a user buffer into memory and pass it to the file's write handler if
  700. * one is supplied. The ->write() method is permitted to modify the
  701. * kernel-side buffer.
  702. */
  703. ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
  704. loff_t *_pos)
  705. {
  706. struct proc_dir_entry *pde = PDE(file_inode(f));
  707. char *buf;
  708. int ret;
  709. if (!pde->write)
  710. return -EACCES;
  711. if (size == 0 || size > PAGE_SIZE - 1)
  712. return -EINVAL;
  713. buf = memdup_user_nul(ubuf, size);
  714. if (IS_ERR(buf))
  715. return PTR_ERR(buf);
  716. ret = pde->write(f, buf, size);
  717. kfree(buf);
  718. return ret == 0 ? size : ret;
  719. }