generic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * proc/fs/generic.c --- generic routines for the proc-fs
  3. *
  4. * This file contains generic proc-fs routines for handling
  5. * directories and files.
  6. *
  7. * Copyright (C) 1991, 1992 Linus Torvalds.
  8. * Copyright (C) 1997 Theodore Ts'o
  9. */
  10. #include <linux/cache.h>
  11. #include <linux/errno.h>
  12. #include <linux/time.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/namei.h>
  18. #include <linux/slab.h>
  19. #include <linux/printk.h>
  20. #include <linux/mount.h>
  21. #include <linux/init.h>
  22. #include <linux/idr.h>
  23. #include <linux/bitops.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/completion.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/seq_file.h>
  28. #include "internal.h"
  29. static DEFINE_RWLOCK(proc_subdir_lock);
  30. struct kmem_cache *proc_dir_entry_cache __ro_after_init;
  31. void pde_free(struct proc_dir_entry *pde)
  32. {
  33. if (S_ISLNK(pde->mode))
  34. kfree(pde->data);
  35. if (pde->name != pde->inline_name)
  36. kfree(pde->name);
  37. kmem_cache_free(proc_dir_entry_cache, pde);
  38. }
  39. static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
  40. {
  41. if (len < de->namelen)
  42. return -1;
  43. if (len > de->namelen)
  44. return 1;
  45. return memcmp(name, de->name, len);
  46. }
  47. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  48. {
  49. return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
  50. subdir_node);
  51. }
  52. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  53. {
  54. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  55. subdir_node);
  56. }
  57. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  58. const char *name,
  59. unsigned int len)
  60. {
  61. struct rb_node *node = dir->subdir.rb_node;
  62. while (node) {
  63. struct proc_dir_entry *de = rb_entry(node,
  64. struct proc_dir_entry,
  65. subdir_node);
  66. int result = proc_match(name, de, len);
  67. if (result < 0)
  68. node = node->rb_left;
  69. else if (result > 0)
  70. node = node->rb_right;
  71. else
  72. return de;
  73. }
  74. return NULL;
  75. }
  76. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  77. struct proc_dir_entry *de)
  78. {
  79. struct rb_root *root = &dir->subdir;
  80. struct rb_node **new = &root->rb_node, *parent = NULL;
  81. /* Figure out where to put new node */
  82. while (*new) {
  83. struct proc_dir_entry *this = rb_entry(*new,
  84. struct proc_dir_entry,
  85. subdir_node);
  86. int result = proc_match(de->name, this, de->namelen);
  87. parent = *new;
  88. if (result < 0)
  89. new = &(*new)->rb_left;
  90. else if (result > 0)
  91. new = &(*new)->rb_right;
  92. else
  93. return false;
  94. }
  95. /* Add new node and rebalance tree. */
  96. rb_link_node(&de->subdir_node, parent, new);
  97. rb_insert_color(&de->subdir_node, root);
  98. return true;
  99. }
  100. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  101. {
  102. struct inode *inode = d_inode(dentry);
  103. struct proc_dir_entry *de = PDE(inode);
  104. int error;
  105. error = setattr_prepare(dentry, iattr);
  106. if (error)
  107. return error;
  108. setattr_copy(inode, iattr);
  109. mark_inode_dirty(inode);
  110. proc_set_user(de, inode->i_uid, inode->i_gid);
  111. de->mode = inode->i_mode;
  112. return 0;
  113. }
  114. static int proc_getattr(const struct path *path, struct kstat *stat,
  115. u32 request_mask, unsigned int query_flags)
  116. {
  117. struct inode *inode = d_inode(path->dentry);
  118. struct proc_dir_entry *de = PDE(inode);
  119. if (de && de->nlink)
  120. set_nlink(inode, de->nlink);
  121. generic_fillattr(inode, stat);
  122. return 0;
  123. }
  124. static const struct inode_operations proc_file_inode_operations = {
  125. .setattr = proc_notify_change,
  126. };
  127. /*
  128. * This function parses a name such as "tty/driver/serial", and
  129. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  130. * returns "serial" in residual.
  131. */
  132. static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  133. const char **residual)
  134. {
  135. const char *cp = name, *next;
  136. struct proc_dir_entry *de;
  137. unsigned int len;
  138. de = *ret;
  139. if (!de)
  140. de = &proc_root;
  141. while (1) {
  142. next = strchr(cp, '/');
  143. if (!next)
  144. break;
  145. len = next - cp;
  146. de = pde_subdir_find(de, cp, len);
  147. if (!de) {
  148. WARN(1, "name '%s'\n", name);
  149. return -ENOENT;
  150. }
  151. cp += len + 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, &proc_misc_dentry_ops);
  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. return proc_lookup_de(dir, dentry, PDE(dir));
  228. }
  229. /*
  230. * This returns non-zero if at EOF, so that the /proc
  231. * root directory can use this and check if it should
  232. * continue with the <pid> entries..
  233. *
  234. * Note that the VFS-layer doesn't care about the return
  235. * value of the readdir() call, as long as it's non-negative
  236. * for success..
  237. */
  238. int proc_readdir_de(struct file *file, struct dir_context *ctx,
  239. struct proc_dir_entry *de)
  240. {
  241. int i;
  242. if (!dir_emit_dots(file, ctx))
  243. return 0;
  244. read_lock(&proc_subdir_lock);
  245. de = pde_subdir_first(de);
  246. i = ctx->pos - 2;
  247. for (;;) {
  248. if (!de) {
  249. read_unlock(&proc_subdir_lock);
  250. return 0;
  251. }
  252. if (!i)
  253. break;
  254. de = pde_subdir_next(de);
  255. i--;
  256. }
  257. do {
  258. struct proc_dir_entry *next;
  259. pde_get(de);
  260. read_unlock(&proc_subdir_lock);
  261. if (!dir_emit(ctx, de->name, de->namelen,
  262. de->low_ino, de->mode >> 12)) {
  263. pde_put(de);
  264. return 0;
  265. }
  266. read_lock(&proc_subdir_lock);
  267. ctx->pos++;
  268. next = pde_subdir_next(de);
  269. pde_put(de);
  270. de = next;
  271. } while (de);
  272. read_unlock(&proc_subdir_lock);
  273. return 1;
  274. }
  275. int proc_readdir(struct file *file, struct dir_context *ctx)
  276. {
  277. struct inode *inode = file_inode(file);
  278. return proc_readdir_de(file, ctx, PDE(inode));
  279. }
  280. /*
  281. * These are the generic /proc directory operations. They
  282. * use the in-memory "struct proc_dir_entry" tree to parse
  283. * the /proc directory.
  284. */
  285. static const struct file_operations proc_dir_operations = {
  286. .llseek = generic_file_llseek,
  287. .read = generic_read_dir,
  288. .iterate_shared = proc_readdir,
  289. };
  290. /*
  291. * proc directories can do almost nothing..
  292. */
  293. static const struct inode_operations proc_dir_inode_operations = {
  294. .lookup = proc_lookup,
  295. .getattr = proc_getattr,
  296. .setattr = proc_notify_change,
  297. };
  298. /* returns the registered entry, or frees dp and returns NULL on failure */
  299. struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
  300. struct proc_dir_entry *dp)
  301. {
  302. if (proc_alloc_inum(&dp->low_ino))
  303. goto out_free_entry;
  304. write_lock(&proc_subdir_lock);
  305. dp->parent = dir;
  306. if (pde_subdir_insert(dir, dp) == false) {
  307. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  308. dir->name, dp->name);
  309. write_unlock(&proc_subdir_lock);
  310. goto out_free_inum;
  311. }
  312. write_unlock(&proc_subdir_lock);
  313. return dp;
  314. out_free_inum:
  315. proc_free_inum(dp->low_ino);
  316. out_free_entry:
  317. pde_free(dp);
  318. return NULL;
  319. }
  320. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  321. const char *name,
  322. umode_t mode,
  323. nlink_t nlink)
  324. {
  325. struct proc_dir_entry *ent = NULL;
  326. const char *fn;
  327. struct qstr qstr;
  328. if (xlate_proc_name(name, parent, &fn) != 0)
  329. goto out;
  330. qstr.name = fn;
  331. qstr.len = strlen(fn);
  332. if (qstr.len == 0 || qstr.len >= 256) {
  333. WARN(1, "name len %u\n", qstr.len);
  334. return NULL;
  335. }
  336. if (qstr.len == 1 && fn[0] == '.') {
  337. WARN(1, "name '.'\n");
  338. return NULL;
  339. }
  340. if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
  341. WARN(1, "name '..'\n");
  342. return NULL;
  343. }
  344. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  345. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  346. return NULL;
  347. }
  348. if (is_empty_pde(*parent)) {
  349. WARN(1, "attempt to add to permanently empty directory");
  350. return NULL;
  351. }
  352. ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  353. if (!ent)
  354. goto out;
  355. if (qstr.len + 1 <= SIZEOF_PDE_INLINE_NAME) {
  356. ent->name = ent->inline_name;
  357. } else {
  358. ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
  359. if (!ent->name) {
  360. pde_free(ent);
  361. return NULL;
  362. }
  363. }
  364. memcpy(ent->name, fn, qstr.len + 1);
  365. ent->namelen = qstr.len;
  366. ent->mode = mode;
  367. ent->nlink = nlink;
  368. ent->subdir = RB_ROOT;
  369. refcount_set(&ent->refcnt, 1);
  370. spin_lock_init(&ent->pde_unload_lock);
  371. INIT_LIST_HEAD(&ent->pde_openers);
  372. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  373. out:
  374. return ent;
  375. }
  376. struct proc_dir_entry *proc_symlink(const char *name,
  377. struct proc_dir_entry *parent, const char *dest)
  378. {
  379. struct proc_dir_entry *ent;
  380. ent = __proc_create(&parent, name,
  381. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  382. if (ent) {
  383. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  384. if (ent->data) {
  385. strcpy((char*)ent->data,dest);
  386. ent->proc_iops = &proc_link_inode_operations;
  387. ent = proc_register(parent, ent);
  388. } else {
  389. pde_free(ent);
  390. ent = NULL;
  391. }
  392. }
  393. return ent;
  394. }
  395. EXPORT_SYMBOL(proc_symlink);
  396. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  397. struct proc_dir_entry *parent, void *data)
  398. {
  399. struct proc_dir_entry *ent;
  400. if (mode == 0)
  401. mode = S_IRUGO | S_IXUGO;
  402. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  403. if (ent) {
  404. ent->data = data;
  405. ent->proc_fops = &proc_dir_operations;
  406. ent->proc_iops = &proc_dir_inode_operations;
  407. parent->nlink++;
  408. ent = proc_register(parent, ent);
  409. if (!ent)
  410. parent->nlink--;
  411. }
  412. return ent;
  413. }
  414. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  415. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  416. struct proc_dir_entry *parent)
  417. {
  418. return proc_mkdir_data(name, mode, parent, NULL);
  419. }
  420. EXPORT_SYMBOL(proc_mkdir_mode);
  421. struct proc_dir_entry *proc_mkdir(const char *name,
  422. struct proc_dir_entry *parent)
  423. {
  424. return proc_mkdir_data(name, 0, parent, NULL);
  425. }
  426. EXPORT_SYMBOL(proc_mkdir);
  427. struct proc_dir_entry *proc_create_mount_point(const char *name)
  428. {
  429. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  430. struct proc_dir_entry *ent, *parent = NULL;
  431. ent = __proc_create(&parent, name, mode, 2);
  432. if (ent) {
  433. ent->data = NULL;
  434. ent->proc_fops = NULL;
  435. ent->proc_iops = NULL;
  436. parent->nlink++;
  437. ent = proc_register(parent, ent);
  438. if (!ent)
  439. parent->nlink--;
  440. }
  441. return ent;
  442. }
  443. EXPORT_SYMBOL(proc_create_mount_point);
  444. struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
  445. struct proc_dir_entry **parent, void *data)
  446. {
  447. struct proc_dir_entry *p;
  448. if ((mode & S_IFMT) == 0)
  449. mode |= S_IFREG;
  450. if ((mode & S_IALLUGO) == 0)
  451. mode |= S_IRUGO;
  452. if (WARN_ON_ONCE(!S_ISREG(mode)))
  453. return NULL;
  454. p = __proc_create(parent, name, mode, 1);
  455. if (p) {
  456. p->proc_iops = &proc_file_inode_operations;
  457. p->data = data;
  458. }
  459. return p;
  460. }
  461. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  462. struct proc_dir_entry *parent,
  463. const struct file_operations *proc_fops, void *data)
  464. {
  465. struct proc_dir_entry *p;
  466. BUG_ON(proc_fops == NULL);
  467. p = proc_create_reg(name, mode, &parent, data);
  468. if (!p)
  469. return NULL;
  470. p->proc_fops = proc_fops;
  471. return proc_register(parent, p);
  472. }
  473. EXPORT_SYMBOL(proc_create_data);
  474. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  475. struct proc_dir_entry *parent,
  476. const struct file_operations *proc_fops)
  477. {
  478. return proc_create_data(name, mode, parent, proc_fops, NULL);
  479. }
  480. EXPORT_SYMBOL(proc_create);
  481. static int proc_seq_open(struct inode *inode, struct file *file)
  482. {
  483. struct proc_dir_entry *de = PDE(inode);
  484. if (de->state_size)
  485. return seq_open_private(file, de->seq_ops, de->state_size);
  486. return seq_open(file, de->seq_ops);
  487. }
  488. static const struct file_operations proc_seq_fops = {
  489. .open = proc_seq_open,
  490. .read = seq_read,
  491. .llseek = seq_lseek,
  492. .release = seq_release,
  493. };
  494. struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
  495. struct proc_dir_entry *parent, const struct seq_operations *ops,
  496. unsigned int state_size, void *data)
  497. {
  498. struct proc_dir_entry *p;
  499. p = proc_create_reg(name, mode, &parent, data);
  500. if (!p)
  501. return NULL;
  502. p->proc_fops = &proc_seq_fops;
  503. p->seq_ops = ops;
  504. p->state_size = state_size;
  505. return proc_register(parent, p);
  506. }
  507. EXPORT_SYMBOL(proc_create_seq_private);
  508. static int proc_single_open(struct inode *inode, struct file *file)
  509. {
  510. struct proc_dir_entry *de = PDE(inode);
  511. return single_open(file, de->single_show, de->data);
  512. }
  513. static const struct file_operations proc_single_fops = {
  514. .open = proc_single_open,
  515. .read = seq_read,
  516. .llseek = seq_lseek,
  517. .release = single_release,
  518. };
  519. struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
  520. struct proc_dir_entry *parent,
  521. int (*show)(struct seq_file *, void *), void *data)
  522. {
  523. struct proc_dir_entry *p;
  524. p = proc_create_reg(name, mode, &parent, data);
  525. if (!p)
  526. return NULL;
  527. p->proc_fops = &proc_single_fops;
  528. p->single_show = show;
  529. return proc_register(parent, p);
  530. }
  531. EXPORT_SYMBOL(proc_create_single_data);
  532. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  533. {
  534. de->size = size;
  535. }
  536. EXPORT_SYMBOL(proc_set_size);
  537. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  538. {
  539. de->uid = uid;
  540. de->gid = gid;
  541. }
  542. EXPORT_SYMBOL(proc_set_user);
  543. void pde_put(struct proc_dir_entry *pde)
  544. {
  545. if (refcount_dec_and_test(&pde->refcnt)) {
  546. proc_free_inum(pde->low_ino);
  547. pde_free(pde);
  548. }
  549. }
  550. /*
  551. * Remove a /proc entry and free it if it's not currently in use.
  552. */
  553. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  554. {
  555. struct proc_dir_entry *de = NULL;
  556. const char *fn = name;
  557. unsigned int len;
  558. write_lock(&proc_subdir_lock);
  559. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  560. write_unlock(&proc_subdir_lock);
  561. return;
  562. }
  563. len = strlen(fn);
  564. de = pde_subdir_find(parent, fn, len);
  565. if (de)
  566. rb_erase(&de->subdir_node, &parent->subdir);
  567. write_unlock(&proc_subdir_lock);
  568. if (!de) {
  569. WARN(1, "name '%s'\n", name);
  570. return;
  571. }
  572. proc_entry_rundown(de);
  573. if (S_ISDIR(de->mode))
  574. parent->nlink--;
  575. de->nlink = 0;
  576. WARN(pde_subdir_first(de),
  577. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  578. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  579. pde_put(de);
  580. }
  581. EXPORT_SYMBOL(remove_proc_entry);
  582. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  583. {
  584. struct proc_dir_entry *root = NULL, *de, *next;
  585. const char *fn = name;
  586. unsigned int len;
  587. write_lock(&proc_subdir_lock);
  588. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  589. write_unlock(&proc_subdir_lock);
  590. return -ENOENT;
  591. }
  592. len = strlen(fn);
  593. root = pde_subdir_find(parent, fn, len);
  594. if (!root) {
  595. write_unlock(&proc_subdir_lock);
  596. return -ENOENT;
  597. }
  598. rb_erase(&root->subdir_node, &parent->subdir);
  599. de = root;
  600. while (1) {
  601. next = pde_subdir_first(de);
  602. if (next) {
  603. rb_erase(&next->subdir_node, &de->subdir);
  604. de = next;
  605. continue;
  606. }
  607. write_unlock(&proc_subdir_lock);
  608. proc_entry_rundown(de);
  609. next = de->parent;
  610. if (S_ISDIR(de->mode))
  611. next->nlink--;
  612. de->nlink = 0;
  613. if (de == root)
  614. break;
  615. pde_put(de);
  616. write_lock(&proc_subdir_lock);
  617. de = next;
  618. }
  619. pde_put(root);
  620. return 0;
  621. }
  622. EXPORT_SYMBOL(remove_proc_subtree);
  623. void *proc_get_parent_data(const struct inode *inode)
  624. {
  625. struct proc_dir_entry *de = PDE(inode);
  626. return de->parent->data;
  627. }
  628. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  629. void proc_remove(struct proc_dir_entry *de)
  630. {
  631. if (de)
  632. remove_proc_subtree(de->name, de->parent);
  633. }
  634. EXPORT_SYMBOL(proc_remove);
  635. void *PDE_DATA(const struct inode *inode)
  636. {
  637. return __PDE_DATA(inode);
  638. }
  639. EXPORT_SYMBOL(PDE_DATA);
  640. /*
  641. * Pull a user buffer into memory and pass it to the file's write handler if
  642. * one is supplied. The ->write() method is permitted to modify the
  643. * kernel-side buffer.
  644. */
  645. ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
  646. loff_t *_pos)
  647. {
  648. struct proc_dir_entry *pde = PDE(file_inode(f));
  649. char *buf;
  650. int ret;
  651. if (!pde->write)
  652. return -EACCES;
  653. if (size == 0 || size > PAGE_SIZE - 1)
  654. return -EINVAL;
  655. buf = memdup_user_nul(ubuf, size);
  656. if (IS_ERR(buf))
  657. return PTR_ERR(buf);
  658. ret = pde->write(f, buf, size);
  659. kfree(buf);
  660. return ret == 0 ? size : ret;
  661. }