generic.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 "internal.h"
  28. static DEFINE_RWLOCK(proc_subdir_lock);
  29. struct kmem_cache *proc_dir_entry_cache __ro_after_init;
  30. void pde_free(struct proc_dir_entry *pde)
  31. {
  32. if (S_ISLNK(pde->mode))
  33. kfree(pde->data);
  34. if (pde->name != pde->inline_name)
  35. kfree(pde->name);
  36. kmem_cache_free(proc_dir_entry_cache, pde);
  37. }
  38. static int proc_match(const char *name, struct proc_dir_entry *de, unsigned int len)
  39. {
  40. if (len < de->namelen)
  41. return -1;
  42. if (len > de->namelen)
  43. return 1;
  44. return memcmp(name, de->name, len);
  45. }
  46. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  47. {
  48. return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
  49. subdir_node);
  50. }
  51. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  52. {
  53. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  54. subdir_node);
  55. }
  56. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  57. const char *name,
  58. unsigned int len)
  59. {
  60. struct rb_node *node = dir->subdir.rb_node;
  61. while (node) {
  62. struct proc_dir_entry *de = rb_entry(node,
  63. struct proc_dir_entry,
  64. subdir_node);
  65. int result = proc_match(name, de, len);
  66. if (result < 0)
  67. node = node->rb_left;
  68. else if (result > 0)
  69. node = node->rb_right;
  70. else
  71. return de;
  72. }
  73. return NULL;
  74. }
  75. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  76. struct proc_dir_entry *de)
  77. {
  78. struct rb_root *root = &dir->subdir;
  79. struct rb_node **new = &root->rb_node, *parent = NULL;
  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. else
  92. return false;
  93. }
  94. /* Add new node and rebalance tree. */
  95. rb_link_node(&de->subdir_node, parent, new);
  96. rb_insert_color(&de->subdir_node, root);
  97. return true;
  98. }
  99. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  100. {
  101. struct inode *inode = d_inode(dentry);
  102. struct proc_dir_entry *de = PDE(inode);
  103. int error;
  104. error = setattr_prepare(dentry, iattr);
  105. if (error)
  106. return error;
  107. setattr_copy(inode, iattr);
  108. mark_inode_dirty(inode);
  109. proc_set_user(de, inode->i_uid, inode->i_gid);
  110. de->mode = inode->i_mode;
  111. return 0;
  112. }
  113. static int proc_getattr(const struct path *path, struct kstat *stat,
  114. u32 request_mask, unsigned int query_flags)
  115. {
  116. struct inode *inode = d_inode(path->dentry);
  117. struct proc_dir_entry *de = PDE(inode);
  118. if (de && de->nlink)
  119. set_nlink(inode, de->nlink);
  120. generic_fillattr(inode, stat);
  121. return 0;
  122. }
  123. static const struct inode_operations proc_file_inode_operations = {
  124. .setattr = proc_notify_change,
  125. };
  126. /*
  127. * This function parses a name such as "tty/driver/serial", and
  128. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  129. * returns "serial" in residual.
  130. */
  131. static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  132. const char **residual)
  133. {
  134. const char *cp = name, *next;
  135. struct proc_dir_entry *de;
  136. unsigned int len;
  137. de = *ret;
  138. if (!de)
  139. de = &proc_root;
  140. while (1) {
  141. next = strchr(cp, '/');
  142. if (!next)
  143. break;
  144. len = next - cp;
  145. de = pde_subdir_find(de, cp, len);
  146. if (!de) {
  147. WARN(1, "name '%s'\n", name);
  148. return -ENOENT;
  149. }
  150. cp += len + 1;
  151. }
  152. *residual = cp;
  153. *ret = de;
  154. return 0;
  155. }
  156. static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  157. const char **residual)
  158. {
  159. int rv;
  160. read_lock(&proc_subdir_lock);
  161. rv = __xlate_proc_name(name, ret, residual);
  162. read_unlock(&proc_subdir_lock);
  163. return rv;
  164. }
  165. static DEFINE_IDA(proc_inum_ida);
  166. #define PROC_DYNAMIC_FIRST 0xF0000000U
  167. /*
  168. * Return an inode number between PROC_DYNAMIC_FIRST and
  169. * 0xffffffff, or zero on failure.
  170. */
  171. int proc_alloc_inum(unsigned int *inum)
  172. {
  173. int i;
  174. i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
  175. GFP_KERNEL);
  176. if (i < 0)
  177. return i;
  178. *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
  179. return 0;
  180. }
  181. void proc_free_inum(unsigned int inum)
  182. {
  183. ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  184. }
  185. static int proc_misc_d_revalidate(struct dentry *dentry, unsigned int flags)
  186. {
  187. if (flags & LOOKUP_RCU)
  188. return -ECHILD;
  189. if (atomic_read(&PDE(d_inode(dentry))->in_use) < 0)
  190. return 0; /* revalidate */
  191. return 1;
  192. }
  193. static int proc_misc_d_delete(const struct dentry *dentry)
  194. {
  195. return atomic_read(&PDE(d_inode(dentry))->in_use) < 0;
  196. }
  197. static const struct dentry_operations proc_misc_dentry_ops = {
  198. .d_revalidate = proc_misc_d_revalidate,
  199. .d_delete = proc_misc_d_delete,
  200. };
  201. /*
  202. * Don't create negative dentries here, return -ENOENT by hand
  203. * instead.
  204. */
  205. struct dentry *proc_lookup_de(struct inode *dir, struct dentry *dentry,
  206. struct proc_dir_entry *de)
  207. {
  208. struct inode *inode;
  209. read_lock(&proc_subdir_lock);
  210. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  211. if (de) {
  212. pde_get(de);
  213. read_unlock(&proc_subdir_lock);
  214. inode = proc_get_inode(dir->i_sb, de);
  215. if (!inode)
  216. return ERR_PTR(-ENOMEM);
  217. d_set_d_op(dentry, &proc_misc_dentry_ops);
  218. d_add(dentry, inode);
  219. return NULL;
  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. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  299. {
  300. int ret;
  301. ret = proc_alloc_inum(&dp->low_ino);
  302. if (ret)
  303. return ret;
  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. proc_free_inum(dp->low_ino);
  311. return -EEXIST;
  312. }
  313. write_unlock(&proc_subdir_lock);
  314. return 0;
  315. }
  316. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  317. const char *name,
  318. umode_t mode,
  319. nlink_t nlink)
  320. {
  321. struct proc_dir_entry *ent = NULL;
  322. const char *fn;
  323. struct qstr qstr;
  324. if (xlate_proc_name(name, parent, &fn) != 0)
  325. goto out;
  326. qstr.name = fn;
  327. qstr.len = strlen(fn);
  328. if (qstr.len == 0 || qstr.len >= 256) {
  329. WARN(1, "name len %u\n", qstr.len);
  330. return NULL;
  331. }
  332. if (qstr.len == 1 && fn[0] == '.') {
  333. WARN(1, "name '.'\n");
  334. return NULL;
  335. }
  336. if (qstr.len == 2 && fn[0] == '.' && fn[1] == '.') {
  337. WARN(1, "name '..'\n");
  338. return NULL;
  339. }
  340. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  341. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  342. return NULL;
  343. }
  344. if (is_empty_pde(*parent)) {
  345. WARN(1, "attempt to add to permanently empty directory");
  346. return NULL;
  347. }
  348. ent = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  349. if (!ent)
  350. goto out;
  351. if (qstr.len + 1 <= sizeof(ent->inline_name)) {
  352. ent->name = ent->inline_name;
  353. } else {
  354. ent->name = kmalloc(qstr.len + 1, GFP_KERNEL);
  355. if (!ent->name) {
  356. pde_free(ent);
  357. return NULL;
  358. }
  359. }
  360. memcpy(ent->name, fn, qstr.len + 1);
  361. ent->namelen = qstr.len;
  362. ent->mode = mode;
  363. ent->nlink = nlink;
  364. ent->subdir = RB_ROOT;
  365. refcount_set(&ent->refcnt, 1);
  366. spin_lock_init(&ent->pde_unload_lock);
  367. INIT_LIST_HEAD(&ent->pde_openers);
  368. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  369. out:
  370. return ent;
  371. }
  372. struct proc_dir_entry *proc_symlink(const char *name,
  373. struct proc_dir_entry *parent, const char *dest)
  374. {
  375. struct proc_dir_entry *ent;
  376. ent = __proc_create(&parent, name,
  377. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  378. if (ent) {
  379. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  380. if (ent->data) {
  381. strcpy((char*)ent->data,dest);
  382. ent->proc_iops = &proc_link_inode_operations;
  383. if (proc_register(parent, ent) < 0) {
  384. pde_free(ent);
  385. ent = NULL;
  386. }
  387. } else {
  388. pde_free(ent);
  389. ent = NULL;
  390. }
  391. }
  392. return ent;
  393. }
  394. EXPORT_SYMBOL(proc_symlink);
  395. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  396. struct proc_dir_entry *parent, void *data)
  397. {
  398. struct proc_dir_entry *ent;
  399. if (mode == 0)
  400. mode = S_IRUGO | S_IXUGO;
  401. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  402. if (ent) {
  403. ent->data = data;
  404. ent->proc_fops = &proc_dir_operations;
  405. ent->proc_iops = &proc_dir_inode_operations;
  406. parent->nlink++;
  407. if (proc_register(parent, ent) < 0) {
  408. pde_free(ent);
  409. parent->nlink--;
  410. ent = NULL;
  411. }
  412. }
  413. return ent;
  414. }
  415. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  416. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  417. struct proc_dir_entry *parent)
  418. {
  419. return proc_mkdir_data(name, mode, parent, NULL);
  420. }
  421. EXPORT_SYMBOL(proc_mkdir_mode);
  422. struct proc_dir_entry *proc_mkdir(const char *name,
  423. struct proc_dir_entry *parent)
  424. {
  425. return proc_mkdir_data(name, 0, parent, NULL);
  426. }
  427. EXPORT_SYMBOL(proc_mkdir);
  428. struct proc_dir_entry *proc_create_mount_point(const char *name)
  429. {
  430. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  431. struct proc_dir_entry *ent, *parent = NULL;
  432. ent = __proc_create(&parent, name, mode, 2);
  433. if (ent) {
  434. ent->data = NULL;
  435. ent->proc_fops = NULL;
  436. ent->proc_iops = NULL;
  437. parent->nlink++;
  438. if (proc_register(parent, ent) < 0) {
  439. pde_free(ent);
  440. parent->nlink--;
  441. ent = NULL;
  442. }
  443. }
  444. return ent;
  445. }
  446. EXPORT_SYMBOL(proc_create_mount_point);
  447. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  448. struct proc_dir_entry *parent,
  449. const struct file_operations *proc_fops,
  450. void *data)
  451. {
  452. struct proc_dir_entry *pde;
  453. if ((mode & S_IFMT) == 0)
  454. mode |= S_IFREG;
  455. if (!S_ISREG(mode)) {
  456. WARN_ON(1); /* use proc_mkdir() */
  457. return NULL;
  458. }
  459. BUG_ON(proc_fops == NULL);
  460. if ((mode & S_IALLUGO) == 0)
  461. mode |= S_IRUGO;
  462. pde = __proc_create(&parent, name, mode, 1);
  463. if (!pde)
  464. goto out;
  465. pde->proc_fops = proc_fops;
  466. pde->data = data;
  467. pde->proc_iops = &proc_file_inode_operations;
  468. if (proc_register(parent, pde) < 0)
  469. goto out_free;
  470. return pde;
  471. out_free:
  472. pde_free(pde);
  473. out:
  474. return NULL;
  475. }
  476. EXPORT_SYMBOL(proc_create_data);
  477. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  478. struct proc_dir_entry *parent,
  479. const struct file_operations *proc_fops)
  480. {
  481. return proc_create_data(name, mode, parent, proc_fops, NULL);
  482. }
  483. EXPORT_SYMBOL(proc_create);
  484. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  485. {
  486. de->size = size;
  487. }
  488. EXPORT_SYMBOL(proc_set_size);
  489. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  490. {
  491. de->uid = uid;
  492. de->gid = gid;
  493. }
  494. EXPORT_SYMBOL(proc_set_user);
  495. void pde_put(struct proc_dir_entry *pde)
  496. {
  497. if (refcount_dec_and_test(&pde->refcnt)) {
  498. proc_free_inum(pde->low_ino);
  499. pde_free(pde);
  500. }
  501. }
  502. /*
  503. * Remove a /proc entry and free it if it's not currently in use.
  504. */
  505. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  506. {
  507. struct proc_dir_entry *de = NULL;
  508. const char *fn = name;
  509. unsigned int len;
  510. write_lock(&proc_subdir_lock);
  511. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  512. write_unlock(&proc_subdir_lock);
  513. return;
  514. }
  515. len = strlen(fn);
  516. de = pde_subdir_find(parent, fn, len);
  517. if (de)
  518. rb_erase(&de->subdir_node, &parent->subdir);
  519. write_unlock(&proc_subdir_lock);
  520. if (!de) {
  521. WARN(1, "name '%s'\n", name);
  522. return;
  523. }
  524. proc_entry_rundown(de);
  525. if (S_ISDIR(de->mode))
  526. parent->nlink--;
  527. de->nlink = 0;
  528. WARN(pde_subdir_first(de),
  529. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  530. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  531. pde_put(de);
  532. }
  533. EXPORT_SYMBOL(remove_proc_entry);
  534. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  535. {
  536. struct proc_dir_entry *root = NULL, *de, *next;
  537. const char *fn = name;
  538. unsigned int len;
  539. write_lock(&proc_subdir_lock);
  540. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  541. write_unlock(&proc_subdir_lock);
  542. return -ENOENT;
  543. }
  544. len = strlen(fn);
  545. root = pde_subdir_find(parent, fn, len);
  546. if (!root) {
  547. write_unlock(&proc_subdir_lock);
  548. return -ENOENT;
  549. }
  550. rb_erase(&root->subdir_node, &parent->subdir);
  551. de = root;
  552. while (1) {
  553. next = pde_subdir_first(de);
  554. if (next) {
  555. rb_erase(&next->subdir_node, &de->subdir);
  556. de = next;
  557. continue;
  558. }
  559. write_unlock(&proc_subdir_lock);
  560. proc_entry_rundown(de);
  561. next = de->parent;
  562. if (S_ISDIR(de->mode))
  563. next->nlink--;
  564. de->nlink = 0;
  565. if (de == root)
  566. break;
  567. pde_put(de);
  568. write_lock(&proc_subdir_lock);
  569. de = next;
  570. }
  571. pde_put(root);
  572. return 0;
  573. }
  574. EXPORT_SYMBOL(remove_proc_subtree);
  575. void *proc_get_parent_data(const struct inode *inode)
  576. {
  577. struct proc_dir_entry *de = PDE(inode);
  578. return de->parent->data;
  579. }
  580. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  581. void proc_remove(struct proc_dir_entry *de)
  582. {
  583. if (de)
  584. remove_proc_subtree(de->name, de->parent);
  585. }
  586. EXPORT_SYMBOL(proc_remove);
  587. void *PDE_DATA(const struct inode *inode)
  588. {
  589. return __PDE_DATA(inode);
  590. }
  591. EXPORT_SYMBOL(PDE_DATA);