generic.c 18 KB

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