generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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/slab.h>
  18. #include <linux/printk.h>
  19. #include <linux/mount.h>
  20. #include <linux/init.h>
  21. #include <linux/idr.h>
  22. #include <linux/bitops.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/completion.h>
  25. #include <linux/uaccess.h>
  26. #include "internal.h"
  27. static DEFINE_RWLOCK(proc_subdir_lock);
  28. struct kmem_cache *proc_dir_entry_cache __ro_after_init;
  29. void pde_free(struct proc_dir_entry *pde)
  30. {
  31. if (S_ISLNK(pde->mode))
  32. kfree(pde->data);
  33. if (pde->name != pde->inline_name)
  34. kfree(pde->name);
  35. kmem_cache_free(proc_dir_entry_cache, pde);
  36. }
  37. static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
  38. {
  39. if (len < de->namelen)
  40. return -1;
  41. if (len > de->namelen)
  42. return 1;
  43. return memcmp(name, de->name, len);
  44. }
  45. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  46. {
  47. return rb_entry_safe(rb_first_cached(&dir->subdir),
  48. struct proc_dir_entry, subdir_node);
  49. }
  50. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  51. {
  52. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  53. subdir_node);
  54. }
  55. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  56. const char *name,
  57. unsigned int len)
  58. {
  59. struct rb_node *node = dir->subdir.rb_root.rb_node;
  60. while (node) {
  61. struct proc_dir_entry *de = rb_entry(node,
  62. struct proc_dir_entry,
  63. subdir_node);
  64. int result = proc_match(name, de, len);
  65. if (result < 0)
  66. node = node->rb_left;
  67. else if (result > 0)
  68. node = node->rb_right;
  69. else
  70. return de;
  71. }
  72. return NULL;
  73. }
  74. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  75. struct proc_dir_entry *de)
  76. {
  77. struct rb_root_cached *root = &dir->subdir;
  78. struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
  79. bool leftmost = true;
  80. /* Figure out where to put new node */
  81. while (*new) {
  82. struct proc_dir_entry *this = rb_entry(*new,
  83. struct proc_dir_entry,
  84. subdir_node);
  85. int result = proc_match(de->name, this, de->namelen);
  86. parent = *new;
  87. if (result < 0)
  88. new = &(*new)->rb_left;
  89. else if (result > 0) {
  90. new = &(*new)->rb_right;
  91. leftmost = false;
  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_cached(&de->subdir_node, root, leftmost);
  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. /*
  187. * Don't create negative dentries here, return -ENOENT by hand
  188. * instead.
  189. */
  190. struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
  191. struct proc_dir_entry *de)
  192. {
  193. struct inode *inode;
  194. read_lock(&proc_subdir_lock);
  195. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  196. if (de) {
  197. pde_get(de);
  198. read_unlock(&proc_subdir_lock);
  199. inode = proc_get_inode(dir->i_sb, de);
  200. if (!inode)
  201. return ERR_PTR(-ENOMEM);
  202. d_set_d_op(dentry, &simple_dentry_operations);
  203. d_add(dentry, inode);
  204. return NULL;
  205. }
  206. read_unlock(&proc_subdir_lock);
  207. return ERR_PTR(-ENOENT);
  208. }
  209. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  210. unsigned int flags)
  211. {
  212. return proc_lookup_de(dir, dentry, PDE(dir));
  213. }
  214. /*
  215. * This returns non-zero if at EOF, so that the /proc
  216. * root directory can use this and check if it should
  217. * continue with the <pid> entries..
  218. *
  219. * Note that the VFS-layer doesn't care about the return
  220. * value of the readdir() call, as long as it's non-negative
  221. * for success..
  222. */
  223. int proc_readdir_de(struct file *file, struct dir_context *ctx,
  224. struct proc_dir_entry *de)
  225. {
  226. int i;
  227. if (!dir_emit_dots(file, ctx))
  228. return 0;
  229. read_lock(&proc_subdir_lock);
  230. de = pde_subdir_first(de);
  231. i = ctx->pos - 2;
  232. for (;;) {
  233. if (!de) {
  234. read_unlock(&proc_subdir_lock);
  235. return 0;
  236. }
  237. if (!i)
  238. break;
  239. de = pde_subdir_next(de);
  240. i--;
  241. }
  242. do {
  243. struct proc_dir_entry *next;
  244. pde_get(de);
  245. read_unlock(&proc_subdir_lock);
  246. if (!dir_emit(ctx, de->name, de->namelen,
  247. de->low_ino, de->mode >> 12)) {
  248. pde_put(de);
  249. return 0;
  250. }
  251. read_lock(&proc_subdir_lock);
  252. ctx->pos++;
  253. next = pde_subdir_next(de);
  254. pde_put(de);
  255. de = next;
  256. } while (de);
  257. read_unlock(&proc_subdir_lock);
  258. return 1;
  259. }
  260. int proc_readdir(struct file *file, struct dir_context *ctx)
  261. {
  262. struct inode *inode = file_inode(file);
  263. return proc_readdir_de(file, ctx, PDE(inode));
  264. }
  265. /*
  266. * These are the generic /proc directory operations. They
  267. * use the in-memory "struct proc_dir_entry" tree to parse
  268. * the /proc directory.
  269. */
  270. static const struct file_operations proc_dir_operations = {
  271. .llseek = generic_file_llseek,
  272. .read = generic_read_dir,
  273. .iterate_shared = proc_readdir,
  274. };
  275. /*
  276. * proc directories can do almost nothing..
  277. */
  278. static const struct inode_operations proc_dir_inode_operations = {
  279. .lookup = proc_lookup,
  280. .getattr = proc_getattr,
  281. .setattr = proc_notify_change,
  282. };
  283. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  284. {
  285. int ret;
  286. ret = proc_alloc_inum(&dp->low_ino);
  287. if (ret)
  288. return ret;
  289. write_lock(&proc_subdir_lock);
  290. dp->parent = dir;
  291. if (pde_subdir_insert(dir, dp) == false) {
  292. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  293. dir->name, dp->name);
  294. write_unlock(&proc_subdir_lock);
  295. proc_free_inum(dp->low_ino);
  296. return -EEXIST;
  297. }
  298. write_unlock(&proc_subdir_lock);
  299. return 0;
  300. }
  301. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  302. const char *name,
  303. umode_t mode,
  304. nlink_t nlink)
  305. {
  306. struct proc_dir_entry *ent = NULL;
  307. const char *fn;
  308. struct qstr qstr;
  309. if (xlate_proc_name(name, parent, &fn) != 0)
  310. goto out;
  311. qstr.name = fn;
  312. qstr.len = strlen(fn);
  313. if (qstr.len == 0 || qstr.len >= 256) {
  314. WARN(1, "name len %u\n", qstr.len);
  315. return NULL;
  316. }
  317. if (qstr.len == 1 && fn[0] == '.') {
  318. WARN(1, "name '.'\n");
  319. return NULL;
  320. }
  321. if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
  322. WARN(1, "name '..'\n");
  323. return NULL;
  324. }
  325. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  326. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  327. return NULL;
  328. }
  329. if (is_empty_pde(*parent)) {
  330. WARN(1, "attempt to add to permanently empty directory");
  331. return NULL;
  332. }
  333. ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  334. if (!ent)
  335. goto out;
  336. if (qstr.len + 1 <= sizeof(ent->inline_name)) {
  337. ent->name = ent->inline_name;
  338. } else {
  339. ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
  340. if (!ent->name) {
  341. pde_free(ent);
  342. return NULL;
  343. }
  344. }
  345. memcpy(ent->name, fn, qstr.len + 1);
  346. ent->namelen = qstr.len;
  347. ent->mode = mode;
  348. ent->nlink = nlink;
  349. ent->subdir = RB_ROOT_CACHED;
  350. refcount_set(&ent->refcnt, 1);
  351. spin_lock_init(&ent->pde_unload_lock);
  352. INIT_LIST_HEAD(&ent->pde_openers);
  353. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  354. out:
  355. return ent;
  356. }
  357. struct proc_dir_entry *proc_symlink(const char *name,
  358. struct proc_dir_entry *parent, const char *dest)
  359. {
  360. struct proc_dir_entry *ent;
  361. ent = __proc_create(&parent, name,
  362. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  363. if (ent) {
  364. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  365. if (ent->data) {
  366. strcpy((char*)ent->data,dest);
  367. ent->proc_iops = &proc_link_inode_operations;
  368. if (proc_register(parent, ent) < 0) {
  369. pde_free(ent);
  370. ent = NULL;
  371. }
  372. } else {
  373. pde_free(ent);
  374. ent = NULL;
  375. }
  376. }
  377. return ent;
  378. }
  379. EXPORT_SYMBOL(proc_symlink);
  380. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  381. struct proc_dir_entry *parent, void *data)
  382. {
  383. struct proc_dir_entry *ent;
  384. if (mode == 0)
  385. mode = S_IRUGO | S_IXUGO;
  386. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  387. if (ent) {
  388. ent->data = data;
  389. ent->proc_fops = &proc_dir_operations;
  390. ent->proc_iops = &proc_dir_inode_operations;
  391. parent->nlink++;
  392. if (proc_register(parent, ent) < 0) {
  393. pde_free(ent);
  394. parent->nlink--;
  395. ent = NULL;
  396. }
  397. }
  398. return ent;
  399. }
  400. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  401. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  402. struct proc_dir_entry *parent)
  403. {
  404. return proc_mkdir_data(name, mode, parent, NULL);
  405. }
  406. EXPORT_SYMBOL(proc_mkdir_mode);
  407. struct proc_dir_entry *proc_mkdir(const char *name,
  408. struct proc_dir_entry *parent)
  409. {
  410. return proc_mkdir_data(name, 0, parent, NULL);
  411. }
  412. EXPORT_SYMBOL(proc_mkdir);
  413. struct proc_dir_entry *proc_create_mount_point(const char *name)
  414. {
  415. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  416. struct proc_dir_entry *ent, *parent = NULL;
  417. ent = __proc_create(&parent, name, mode, 2);
  418. if (ent) {
  419. ent->data = NULL;
  420. ent->proc_fops = NULL;
  421. ent->proc_iops = NULL;
  422. parent->nlink++;
  423. if (proc_register(parent, ent) < 0) {
  424. pde_free(ent);
  425. parent->nlink--;
  426. ent = NULL;
  427. }
  428. }
  429. return ent;
  430. }
  431. EXPORT_SYMBOL(proc_create_mount_point);
  432. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  433. struct proc_dir_entry *parent,
  434. const struct file_operations *proc_fops,
  435. void *data)
  436. {
  437. struct proc_dir_entry *pde;
  438. if ((mode & S_IFMT) == 0)
  439. mode |= S_IFREG;
  440. if (!S_ISREG(mode)) {
  441. WARN_ON(1); /* use proc_mkdir() */
  442. return NULL;
  443. }
  444. BUG_ON(proc_fops == NULL);
  445. if ((mode & S_IALLUGO) == 0)
  446. mode |= S_IRUGO;
  447. pde = __proc_create(&parent, name, mode, 1);
  448. if (!pde)
  449. goto out;
  450. pde->proc_fops = proc_fops;
  451. pde->data = data;
  452. pde->proc_iops = &proc_file_inode_operations;
  453. if (proc_register(parent, pde) < 0)
  454. goto out_free;
  455. return pde;
  456. out_free:
  457. pde_free(pde);
  458. out:
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL(proc_create_data);
  462. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  463. struct proc_dir_entry *parent,
  464. const struct file_operations *proc_fops)
  465. {
  466. return proc_create_data(name, mode, parent, proc_fops, NULL);
  467. }
  468. EXPORT_SYMBOL(proc_create);
  469. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  470. {
  471. de->size = size;
  472. }
  473. EXPORT_SYMBOL(proc_set_size);
  474. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  475. {
  476. de->uid = uid;
  477. de->gid = gid;
  478. }
  479. EXPORT_SYMBOL(proc_set_user);
  480. void pde_put(struct proc_dir_entry *pde)
  481. {
  482. if (refcount_dec_and_test(&pde->refcnt)) {
  483. proc_free_inum(pde->low_ino);
  484. pde_free(pde);
  485. }
  486. }
  487. /*
  488. * Remove a /proc entry and free it if it's not currently in use.
  489. */
  490. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  491. {
  492. struct proc_dir_entry *de = NULL;
  493. const char *fn = name;
  494. unsigned int len;
  495. write_lock(&proc_subdir_lock);
  496. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  497. write_unlock(&proc_subdir_lock);
  498. return;
  499. }
  500. len = strlen(fn);
  501. de = pde_subdir_find(parent, fn, len);
  502. if (de)
  503. rb_erase_cached(&de->subdir_node, &parent->subdir);
  504. write_unlock(&proc_subdir_lock);
  505. if (!de) {
  506. WARN(1, "name '%s'\n", name);
  507. return;
  508. }
  509. proc_entry_rundown(de);
  510. if (S_ISDIR(de->mode))
  511. parent->nlink--;
  512. de->nlink = 0;
  513. WARN(pde_subdir_first(de),
  514. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  515. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  516. pde_put(de);
  517. }
  518. EXPORT_SYMBOL(remove_proc_entry);
  519. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  520. {
  521. struct proc_dir_entry *root = NULL, *de, *next;
  522. const char *fn = name;
  523. unsigned int len;
  524. write_lock(&proc_subdir_lock);
  525. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  526. write_unlock(&proc_subdir_lock);
  527. return -ENOENT;
  528. }
  529. len = strlen(fn);
  530. root = pde_subdir_find(parent, fn, len);
  531. if (!root) {
  532. write_unlock(&proc_subdir_lock);
  533. return -ENOENT;
  534. }
  535. rb_erase_cached(&root->subdir_node, &parent->subdir);
  536. de = root;
  537. while (1) {
  538. next = pde_subdir_first(de);
  539. if (next) {
  540. rb_erase_cached(&next->subdir_node, &de->subdir);
  541. de = next;
  542. continue;
  543. }
  544. write_unlock(&proc_subdir_lock);
  545. proc_entry_rundown(de);
  546. next = de->parent;
  547. if (S_ISDIR(de->mode))
  548. next->nlink--;
  549. de->nlink = 0;
  550. if (de == root)
  551. break;
  552. pde_put(de);
  553. write_lock(&proc_subdir_lock);
  554. de = next;
  555. }
  556. pde_put(root);
  557. return 0;
  558. }
  559. EXPORT_SYMBOL(remove_proc_subtree);
  560. void *proc_get_parent_data(const struct inode *inode)
  561. {
  562. struct proc_dir_entry *de = PDE(inode);
  563. return de->parent->data;
  564. }
  565. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  566. void proc_remove(struct proc_dir_entry *de)
  567. {
  568. if (de)
  569. remove_proc_subtree(de->name, de->parent);
  570. }
  571. EXPORT_SYMBOL(proc_remove);
  572. void *PDE_DATA(const struct inode *inode)
  573. {
  574. return __PDE_DATA(inode);
  575. }
  576. EXPORT_SYMBOL(PDE_DATA);