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