generic.c 19 KB

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