generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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/errno.h>
  11. #include <linux/time.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/stat.h>
  14. #include <linux/mm.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/printk.h>
  18. #include <linux/mount.h>
  19. #include <linux/init.h>
  20. #include <linux/idr.h>
  21. #include <linux/bitops.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/completion.h>
  24. #include <linux/uaccess.h>
  25. #include "internal.h"
  26. static DEFINE_RWLOCK(proc_subdir_lock);
  27. static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
  28. {
  29. if (len < de->namelen)
  30. return -1;
  31. if (len > de->namelen)
  32. return 1;
  33. return memcmp(name, de->name, len);
  34. }
  35. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  36. {
  37. return rb_entry_safe(rb_first_cached(&dir->subdir),
  38. struct proc_dir_entry, subdir_node);
  39. }
  40. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  41. {
  42. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  43. subdir_node);
  44. }
  45. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  46. const char *name,
  47. unsigned int len)
  48. {
  49. struct rb_node *node = dir->subdir.rb_root.rb_node;
  50. while (node) {
  51. struct proc_dir_entry *de = rb_entry(node,
  52. struct proc_dir_entry,
  53. subdir_node);
  54. int result = proc_match(len, name, de);
  55. if (result < 0)
  56. node = node->rb_left;
  57. else if (result > 0)
  58. node = node->rb_right;
  59. else
  60. return de;
  61. }
  62. return NULL;
  63. }
  64. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  65. struct proc_dir_entry *de)
  66. {
  67. struct rb_root_cached *root = &dir->subdir;
  68. struct rb_node **new = &root->rb_root.rb_node, *parent = NULL;
  69. bool leftmost = true;
  70. /* Figure out where to put new node */
  71. while (*new) {
  72. struct proc_dir_entry *this = rb_entry(*new,
  73. struct proc_dir_entry,
  74. subdir_node);
  75. int result = proc_match(de->namelen, de->name, this);
  76. parent = *new;
  77. if (result < 0)
  78. new = &(*new)->rb_left;
  79. else if (result > 0) {
  80. new = &(*new)->rb_right;
  81. leftmost = false;
  82. } else
  83. return false;
  84. }
  85. /* Add new node and rebalance tree. */
  86. rb_link_node(&de->subdir_node, parent, new);
  87. rb_insert_color_cached(&de->subdir_node, root, leftmost);
  88. return true;
  89. }
  90. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  91. {
  92. struct inode *inode = d_inode(dentry);
  93. struct proc_dir_entry *de = PDE(inode);
  94. int error;
  95. error = setattr_prepare(dentry, iattr);
  96. if (error)
  97. return error;
  98. setattr_copy(inode, iattr);
  99. mark_inode_dirty(inode);
  100. proc_set_user(de, inode->i_uid, inode->i_gid);
  101. de->mode = inode->i_mode;
  102. return 0;
  103. }
  104. static int proc_getattr(const struct path *path, struct kstat *stat,
  105. u32 request_mask, unsigned int query_flags)
  106. {
  107. struct inode *inode = d_inode(path->dentry);
  108. struct proc_dir_entry *de = PDE(inode);
  109. if (de && de->nlink)
  110. set_nlink(inode, de->nlink);
  111. generic_fillattr(inode, stat);
  112. return 0;
  113. }
  114. static const struct inode_operations proc_file_inode_operations = {
  115. .setattr = proc_notify_change,
  116. };
  117. /*
  118. * This function parses a name such as "tty/driver/serial", and
  119. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  120. * returns "serial" in residual.
  121. */
  122. static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  123. const char **residual)
  124. {
  125. const char *cp = name, *next;
  126. struct proc_dir_entry *de;
  127. unsigned int len;
  128. de = *ret;
  129. if (!de)
  130. de = &proc_root;
  131. while (1) {
  132. next = strchr(cp, '/');
  133. if (!next)
  134. break;
  135. len = next - cp;
  136. de = pde_subdir_find(de, cp, len);
  137. if (!de) {
  138. WARN(1, "name '%s'\n", name);
  139. return -ENOENT;
  140. }
  141. cp += len + 1;
  142. }
  143. *residual = cp;
  144. *ret = de;
  145. return 0;
  146. }
  147. static int xlate_proc_name(const char *name, struct proc_dir_entry **ret,
  148. const char **residual)
  149. {
  150. int rv;
  151. read_lock(&proc_subdir_lock);
  152. rv = __xlate_proc_name(name, ret, residual);
  153. read_unlock(&proc_subdir_lock);
  154. return rv;
  155. }
  156. static DEFINE_IDA(proc_inum_ida);
  157. #define PROC_DYNAMIC_FIRST 0xF0000000U
  158. /*
  159. * Return an inode number between PROC_DYNAMIC_FIRST and
  160. * 0xffffffff, or zero on failure.
  161. */
  162. int proc_alloc_inum(unsigned int *inum)
  163. {
  164. int i;
  165. i = ida_simple_get(&proc_inum_ida, 0, UINT_MAX - PROC_DYNAMIC_FIRST + 1,
  166. GFP_KERNEL);
  167. if (i < 0)
  168. return i;
  169. *inum = PROC_DYNAMIC_FIRST + (unsigned int)i;
  170. return 0;
  171. }
  172. void proc_free_inum(unsigned int inum)
  173. {
  174. ida_simple_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  175. }
  176. /*
  177. * Don't create negative dentries here, return -ENOENT by hand
  178. * instead.
  179. */
  180. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  181. struct dentry *dentry)
  182. {
  183. struct inode *inode;
  184. read_lock(&proc_subdir_lock);
  185. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  186. if (de) {
  187. pde_get(de);
  188. read_unlock(&proc_subdir_lock);
  189. inode = proc_get_inode(dir->i_sb, de);
  190. if (!inode)
  191. return ERR_PTR(-ENOMEM);
  192. d_set_d_op(dentry, &simple_dentry_operations);
  193. d_add(dentry, inode);
  194. return NULL;
  195. }
  196. read_unlock(&proc_subdir_lock);
  197. return ERR_PTR(-ENOENT);
  198. }
  199. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  200. unsigned int flags)
  201. {
  202. return proc_lookup_de(PDE(dir), dir, dentry);
  203. }
  204. /*
  205. * This returns non-zero if at EOF, so that the /proc
  206. * root directory can use this and check if it should
  207. * continue with the <pid> entries..
  208. *
  209. * Note that the VFS-layer doesn't care about the return
  210. * value of the readdir() call, as long as it's non-negative
  211. * for success..
  212. */
  213. int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
  214. struct dir_context *ctx)
  215. {
  216. int i;
  217. if (!dir_emit_dots(file, ctx))
  218. return 0;
  219. read_lock(&proc_subdir_lock);
  220. de = pde_subdir_first(de);
  221. i = ctx->pos - 2;
  222. for (;;) {
  223. if (!de) {
  224. read_unlock(&proc_subdir_lock);
  225. return 0;
  226. }
  227. if (!i)
  228. break;
  229. de = pde_subdir_next(de);
  230. i--;
  231. }
  232. do {
  233. struct proc_dir_entry *next;
  234. pde_get(de);
  235. read_unlock(&proc_subdir_lock);
  236. if (!dir_emit(ctx, de->name, de->namelen,
  237. de->low_ino, de->mode >> 12)) {
  238. pde_put(de);
  239. return 0;
  240. }
  241. read_lock(&proc_subdir_lock);
  242. ctx->pos++;
  243. next = pde_subdir_next(de);
  244. pde_put(de);
  245. de = next;
  246. } while (de);
  247. read_unlock(&proc_subdir_lock);
  248. return 1;
  249. }
  250. int proc_readdir(struct file *file, struct dir_context *ctx)
  251. {
  252. struct inode *inode = file_inode(file);
  253. return proc_readdir_de(PDE(inode), file, ctx);
  254. }
  255. /*
  256. * These are the generic /proc directory operations. They
  257. * use the in-memory "struct proc_dir_entry" tree to parse
  258. * the /proc directory.
  259. */
  260. static const struct file_operations proc_dir_operations = {
  261. .llseek = generic_file_llseek,
  262. .read = generic_read_dir,
  263. .iterate_shared = proc_readdir,
  264. };
  265. /*
  266. * proc directories can do almost nothing..
  267. */
  268. static const struct inode_operations proc_dir_inode_operations = {
  269. .lookup = proc_lookup,
  270. .getattr = proc_getattr,
  271. .setattr = proc_notify_change,
  272. };
  273. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  274. {
  275. int ret;
  276. ret = proc_alloc_inum(&dp->low_ino);
  277. if (ret)
  278. return ret;
  279. write_lock(&proc_subdir_lock);
  280. dp->parent = dir;
  281. if (pde_subdir_insert(dir, dp) == false) {
  282. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  283. dir->name, dp->name);
  284. write_unlock(&proc_subdir_lock);
  285. proc_free_inum(dp->low_ino);
  286. return -EEXIST;
  287. }
  288. write_unlock(&proc_subdir_lock);
  289. return 0;
  290. }
  291. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  292. const char *name,
  293. umode_t mode,
  294. nlink_t nlink)
  295. {
  296. struct proc_dir_entry *ent = NULL;
  297. const char *fn;
  298. struct qstr qstr;
  299. if (xlate_proc_name(name, parent, &fn) != 0)
  300. goto out;
  301. qstr.name = fn;
  302. qstr.len = strlen(fn);
  303. if (qstr.len == 0 || qstr.len >= 256) {
  304. WARN(1, "name len %u\n", qstr.len);
  305. return NULL;
  306. }
  307. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  308. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  309. return NULL;
  310. }
  311. if (is_empty_pde(*parent)) {
  312. WARN(1, "attempt to add to permanently empty directory");
  313. return NULL;
  314. }
  315. ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
  316. if (!ent)
  317. goto out;
  318. memcpy(ent->name, fn, qstr.len + 1);
  319. ent->namelen = qstr.len;
  320. ent->mode = mode;
  321. ent->nlink = nlink;
  322. ent->subdir = RB_ROOT_CACHED;
  323. atomic_set(&ent->count, 1);
  324. spin_lock_init(&ent->pde_unload_lock);
  325. INIT_LIST_HEAD(&ent->pde_openers);
  326. proc_set_user(ent, (*parent)->uid, (*parent)->gid);
  327. out:
  328. return ent;
  329. }
  330. struct proc_dir_entry *proc_symlink(const char *name,
  331. struct proc_dir_entry *parent, const char *dest)
  332. {
  333. struct proc_dir_entry *ent;
  334. ent = __proc_create(&parent, name,
  335. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  336. if (ent) {
  337. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  338. if (ent->data) {
  339. strcpy((char*)ent->data,dest);
  340. ent->proc_iops = &proc_link_inode_operations;
  341. if (proc_register(parent, ent) < 0) {
  342. kfree(ent->data);
  343. kfree(ent);
  344. ent = NULL;
  345. }
  346. } else {
  347. kfree(ent);
  348. ent = NULL;
  349. }
  350. }
  351. return ent;
  352. }
  353. EXPORT_SYMBOL(proc_symlink);
  354. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  355. struct proc_dir_entry *parent, void *data)
  356. {
  357. struct proc_dir_entry *ent;
  358. if (mode == 0)
  359. mode = S_IRUGO | S_IXUGO;
  360. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  361. if (ent) {
  362. ent->data = data;
  363. ent->proc_fops = &proc_dir_operations;
  364. ent->proc_iops = &proc_dir_inode_operations;
  365. parent->nlink++;
  366. if (proc_register(parent, ent) < 0) {
  367. kfree(ent);
  368. parent->nlink--;
  369. ent = NULL;
  370. }
  371. }
  372. return ent;
  373. }
  374. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  375. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  376. struct proc_dir_entry *parent)
  377. {
  378. return proc_mkdir_data(name, mode, parent, NULL);
  379. }
  380. EXPORT_SYMBOL(proc_mkdir_mode);
  381. struct proc_dir_entry *proc_mkdir(const char *name,
  382. struct proc_dir_entry *parent)
  383. {
  384. return proc_mkdir_data(name, 0, parent, NULL);
  385. }
  386. EXPORT_SYMBOL(proc_mkdir);
  387. struct proc_dir_entry *proc_create_mount_point(const char *name)
  388. {
  389. umode_t mode = S_IFDIR | S_IRUGO | S_IXUGO;
  390. struct proc_dir_entry *ent, *parent = NULL;
  391. ent = __proc_create(&parent, name, mode, 2);
  392. if (ent) {
  393. ent->data = NULL;
  394. ent->proc_fops = NULL;
  395. ent->proc_iops = NULL;
  396. parent->nlink++;
  397. if (proc_register(parent, ent) < 0) {
  398. kfree(ent);
  399. parent->nlink--;
  400. ent = NULL;
  401. }
  402. }
  403. return ent;
  404. }
  405. EXPORT_SYMBOL(proc_create_mount_point);
  406. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  407. struct proc_dir_entry *parent,
  408. const struct file_operations *proc_fops,
  409. void *data)
  410. {
  411. struct proc_dir_entry *pde;
  412. if ((mode & S_IFMT) == 0)
  413. mode |= S_IFREG;
  414. if (!S_ISREG(mode)) {
  415. WARN_ON(1); /* use proc_mkdir() */
  416. return NULL;
  417. }
  418. BUG_ON(proc_fops == NULL);
  419. if ((mode & S_IALLUGO) == 0)
  420. mode |= S_IRUGO;
  421. pde = __proc_create(&parent, name, mode, 1);
  422. if (!pde)
  423. goto out;
  424. pde->proc_fops = proc_fops;
  425. pde->data = data;
  426. pde->proc_iops = &proc_file_inode_operations;
  427. if (proc_register(parent, pde) < 0)
  428. goto out_free;
  429. return pde;
  430. out_free:
  431. kfree(pde);
  432. out:
  433. return NULL;
  434. }
  435. EXPORT_SYMBOL(proc_create_data);
  436. struct proc_dir_entry *proc_create(const char *name, umode_t mode,
  437. struct proc_dir_entry *parent,
  438. const struct file_operations *proc_fops)
  439. {
  440. return proc_create_data(name, mode, parent, proc_fops, NULL);
  441. }
  442. EXPORT_SYMBOL(proc_create);
  443. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  444. {
  445. de->size = size;
  446. }
  447. EXPORT_SYMBOL(proc_set_size);
  448. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  449. {
  450. de->uid = uid;
  451. de->gid = gid;
  452. }
  453. EXPORT_SYMBOL(proc_set_user);
  454. static void free_proc_entry(struct proc_dir_entry *de)
  455. {
  456. proc_free_inum(de->low_ino);
  457. if (S_ISLNK(de->mode))
  458. kfree(de->data);
  459. kfree(de);
  460. }
  461. void pde_put(struct proc_dir_entry *pde)
  462. {
  463. if (atomic_dec_and_test(&pde->count))
  464. free_proc_entry(pde);
  465. }
  466. /*
  467. * Remove a /proc entry and free it if it's not currently in use.
  468. */
  469. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  470. {
  471. struct proc_dir_entry *de = NULL;
  472. const char *fn = name;
  473. unsigned int len;
  474. write_lock(&proc_subdir_lock);
  475. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  476. write_unlock(&proc_subdir_lock);
  477. return;
  478. }
  479. len = strlen(fn);
  480. de = pde_subdir_find(parent, fn, len);
  481. if (de)
  482. rb_erase_cached(&de->subdir_node, &parent->subdir);
  483. write_unlock(&proc_subdir_lock);
  484. if (!de) {
  485. WARN(1, "name '%s'\n", name);
  486. return;
  487. }
  488. proc_entry_rundown(de);
  489. if (S_ISDIR(de->mode))
  490. parent->nlink--;
  491. de->nlink = 0;
  492. WARN(pde_subdir_first(de),
  493. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  494. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  495. pde_put(de);
  496. }
  497. EXPORT_SYMBOL(remove_proc_entry);
  498. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  499. {
  500. struct proc_dir_entry *root = NULL, *de, *next;
  501. const char *fn = name;
  502. unsigned int len;
  503. write_lock(&proc_subdir_lock);
  504. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  505. write_unlock(&proc_subdir_lock);
  506. return -ENOENT;
  507. }
  508. len = strlen(fn);
  509. root = pde_subdir_find(parent, fn, len);
  510. if (!root) {
  511. write_unlock(&proc_subdir_lock);
  512. return -ENOENT;
  513. }
  514. rb_erase_cached(&root->subdir_node, &parent->subdir);
  515. de = root;
  516. while (1) {
  517. next = pde_subdir_first(de);
  518. if (next) {
  519. rb_erase_cached(&next->subdir_node, &de->subdir);
  520. de = next;
  521. continue;
  522. }
  523. write_unlock(&proc_subdir_lock);
  524. proc_entry_rundown(de);
  525. next = de->parent;
  526. if (S_ISDIR(de->mode))
  527. next->nlink--;
  528. de->nlink = 0;
  529. if (de == root)
  530. break;
  531. pde_put(de);
  532. write_lock(&proc_subdir_lock);
  533. de = next;
  534. }
  535. pde_put(root);
  536. return 0;
  537. }
  538. EXPORT_SYMBOL(remove_proc_subtree);
  539. void *proc_get_parent_data(const struct inode *inode)
  540. {
  541. struct proc_dir_entry *de = PDE(inode);
  542. return de->parent->data;
  543. }
  544. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  545. void proc_remove(struct proc_dir_entry *de)
  546. {
  547. if (de)
  548. remove_proc_subtree(de->name, de->parent);
  549. }
  550. EXPORT_SYMBOL(proc_remove);
  551. void *PDE_DATA(const struct inode *inode)
  552. {
  553. return __PDE_DATA(inode);
  554. }
  555. EXPORT_SYMBOL(PDE_DATA);