generic.c 14 KB

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