generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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/namei.h>
  22. #include <linux/bitops.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/completion.h>
  25. #include <asm/uaccess.h>
  26. #include "internal.h"
  27. static DEFINE_SPINLOCK(proc_subdir_lock);
  28. static int proc_match(unsigned int len, const char *name, struct proc_dir_entry *de)
  29. {
  30. if (len < de->namelen)
  31. return -1;
  32. if (len > de->namelen)
  33. return 1;
  34. return memcmp(name, de->name, len);
  35. }
  36. static struct proc_dir_entry *pde_subdir_first(struct proc_dir_entry *dir)
  37. {
  38. return rb_entry_safe(rb_first(&dir->subdir), struct proc_dir_entry,
  39. subdir_node);
  40. }
  41. static struct proc_dir_entry *pde_subdir_next(struct proc_dir_entry *dir)
  42. {
  43. return rb_entry_safe(rb_next(&dir->subdir_node), struct proc_dir_entry,
  44. subdir_node);
  45. }
  46. static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
  47. const char *name,
  48. unsigned int len)
  49. {
  50. struct rb_node *node = dir->subdir.rb_node;
  51. while (node) {
  52. struct proc_dir_entry *de = container_of(node,
  53. struct proc_dir_entry,
  54. subdir_node);
  55. int result = proc_match(len, name, de);
  56. if (result < 0)
  57. node = node->rb_left;
  58. else if (result > 0)
  59. node = node->rb_right;
  60. else
  61. return de;
  62. }
  63. return NULL;
  64. }
  65. static bool pde_subdir_insert(struct proc_dir_entry *dir,
  66. struct proc_dir_entry *de)
  67. {
  68. struct rb_root *root = &dir->subdir;
  69. struct rb_node **new = &root->rb_node, *parent = NULL;
  70. /* Figure out where to put new node */
  71. while (*new) {
  72. struct proc_dir_entry *this =
  73. container_of(*new, struct proc_dir_entry, 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 = dentry->d_inode;
  91. struct proc_dir_entry *de = PDE(inode);
  92. int error;
  93. error = inode_change_ok(inode, 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(struct vfsmount *mnt, struct dentry *dentry,
  103. struct kstat *stat)
  104. {
  105. struct inode *inode = dentry->d_inode;
  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. spin_lock(&proc_subdir_lock);
  150. rv = __xlate_proc_name(name, ret, residual);
  151. spin_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. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  192. {
  193. nd_set_link(nd, __PDE_DATA(dentry->d_inode));
  194. return NULL;
  195. }
  196. static const struct inode_operations proc_link_inode_operations = {
  197. .readlink = generic_readlink,
  198. .follow_link = proc_follow_link,
  199. };
  200. /*
  201. * Don't create negative dentries here, return -ENOENT by hand
  202. * instead.
  203. */
  204. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  205. struct dentry *dentry)
  206. {
  207. struct inode *inode;
  208. spin_lock(&proc_subdir_lock);
  209. de = pde_subdir_find(de, dentry->d_name.name, dentry->d_name.len);
  210. if (de) {
  211. pde_get(de);
  212. spin_unlock(&proc_subdir_lock);
  213. inode = proc_get_inode(dir->i_sb, de);
  214. if (!inode)
  215. return ERR_PTR(-ENOMEM);
  216. d_set_d_op(dentry, &simple_dentry_operations);
  217. d_add(dentry, inode);
  218. return NULL;
  219. }
  220. spin_unlock(&proc_subdir_lock);
  221. return ERR_PTR(-ENOENT);
  222. }
  223. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  224. unsigned int flags)
  225. {
  226. return proc_lookup_de(PDE(dir), dir, dentry);
  227. }
  228. /*
  229. * This returns non-zero if at EOF, so that the /proc
  230. * root directory can use this and check if it should
  231. * continue with the <pid> entries..
  232. *
  233. * Note that the VFS-layer doesn't care about the return
  234. * value of the readdir() call, as long as it's non-negative
  235. * for success..
  236. */
  237. int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
  238. struct dir_context *ctx)
  239. {
  240. int i;
  241. if (!dir_emit_dots(file, ctx))
  242. return 0;
  243. spin_lock(&proc_subdir_lock);
  244. de = pde_subdir_first(de);
  245. i = ctx->pos - 2;
  246. for (;;) {
  247. if (!de) {
  248. spin_unlock(&proc_subdir_lock);
  249. return 0;
  250. }
  251. if (!i)
  252. break;
  253. de = pde_subdir_next(de);
  254. i--;
  255. }
  256. do {
  257. struct proc_dir_entry *next;
  258. pde_get(de);
  259. spin_unlock(&proc_subdir_lock);
  260. if (!dir_emit(ctx, de->name, de->namelen,
  261. de->low_ino, de->mode >> 12)) {
  262. pde_put(de);
  263. return 0;
  264. }
  265. spin_lock(&proc_subdir_lock);
  266. ctx->pos++;
  267. next = pde_subdir_next(de);
  268. pde_put(de);
  269. de = next;
  270. } while (de);
  271. spin_unlock(&proc_subdir_lock);
  272. return 1;
  273. }
  274. int proc_readdir(struct file *file, struct dir_context *ctx)
  275. {
  276. struct inode *inode = file_inode(file);
  277. return proc_readdir_de(PDE(inode), file, ctx);
  278. }
  279. /*
  280. * These are the generic /proc directory operations. They
  281. * use the in-memory "struct proc_dir_entry" tree to parse
  282. * the /proc directory.
  283. */
  284. static const struct file_operations proc_dir_operations = {
  285. .llseek = generic_file_llseek,
  286. .read = generic_read_dir,
  287. .iterate = proc_readdir,
  288. };
  289. /*
  290. * proc directories can do almost nothing..
  291. */
  292. static const struct inode_operations proc_dir_inode_operations = {
  293. .lookup = proc_lookup,
  294. .getattr = proc_getattr,
  295. .setattr = proc_notify_change,
  296. };
  297. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  298. {
  299. int ret;
  300. ret = proc_alloc_inum(&dp->low_ino);
  301. if (ret)
  302. return ret;
  303. spin_lock(&proc_subdir_lock);
  304. dp->parent = dir;
  305. if (pde_subdir_insert(dir, dp) == false) {
  306. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  307. dir->name, dp->name);
  308. spin_unlock(&proc_subdir_lock);
  309. proc_free_inum(dp->low_ino);
  310. return -EEXIST;
  311. }
  312. spin_unlock(&proc_subdir_lock);
  313. return 0;
  314. }
  315. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  316. const char *name,
  317. umode_t mode,
  318. nlink_t nlink)
  319. {
  320. struct proc_dir_entry *ent = NULL;
  321. const char *fn;
  322. struct qstr qstr;
  323. if (xlate_proc_name(name, parent, &fn) != 0)
  324. goto out;
  325. qstr.name = fn;
  326. qstr.len = strlen(fn);
  327. if (qstr.len == 0 || qstr.len >= 256) {
  328. WARN(1, "name len %u\n", qstr.len);
  329. return NULL;
  330. }
  331. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  332. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  333. return NULL;
  334. }
  335. ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
  336. if (!ent)
  337. goto out;
  338. memcpy(ent->name, fn, qstr.len + 1);
  339. ent->namelen = qstr.len;
  340. ent->mode = mode;
  341. ent->nlink = nlink;
  342. ent->subdir = RB_ROOT;
  343. atomic_set(&ent->count, 1);
  344. spin_lock_init(&ent->pde_unload_lock);
  345. INIT_LIST_HEAD(&ent->pde_openers);
  346. out:
  347. return ent;
  348. }
  349. struct proc_dir_entry *proc_symlink(const char *name,
  350. struct proc_dir_entry *parent, const char *dest)
  351. {
  352. struct proc_dir_entry *ent;
  353. ent = __proc_create(&parent, name,
  354. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  355. if (ent) {
  356. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  357. if (ent->data) {
  358. strcpy((char*)ent->data,dest);
  359. ent->proc_iops = &proc_link_inode_operations;
  360. if (proc_register(parent, ent) < 0) {
  361. kfree(ent->data);
  362. kfree(ent);
  363. ent = NULL;
  364. }
  365. } else {
  366. kfree(ent);
  367. ent = NULL;
  368. }
  369. }
  370. return ent;
  371. }
  372. EXPORT_SYMBOL(proc_symlink);
  373. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  374. struct proc_dir_entry *parent, void *data)
  375. {
  376. struct proc_dir_entry *ent;
  377. if (mode == 0)
  378. mode = S_IRUGO | S_IXUGO;
  379. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  380. if (ent) {
  381. ent->data = data;
  382. ent->proc_fops = &proc_dir_operations;
  383. ent->proc_iops = &proc_dir_inode_operations;
  384. parent->nlink++;
  385. if (proc_register(parent, ent) < 0) {
  386. kfree(ent);
  387. parent->nlink--;
  388. ent = NULL;
  389. }
  390. }
  391. return ent;
  392. }
  393. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  394. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  395. struct proc_dir_entry *parent)
  396. {
  397. return proc_mkdir_data(name, mode, parent, NULL);
  398. }
  399. EXPORT_SYMBOL(proc_mkdir_mode);
  400. struct proc_dir_entry *proc_mkdir(const char *name,
  401. struct proc_dir_entry *parent)
  402. {
  403. return proc_mkdir_data(name, 0, parent, NULL);
  404. }
  405. EXPORT_SYMBOL(proc_mkdir);
  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. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  437. {
  438. de->size = size;
  439. }
  440. EXPORT_SYMBOL(proc_set_size);
  441. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  442. {
  443. de->uid = uid;
  444. de->gid = gid;
  445. }
  446. EXPORT_SYMBOL(proc_set_user);
  447. static void free_proc_entry(struct proc_dir_entry *de)
  448. {
  449. proc_free_inum(de->low_ino);
  450. if (S_ISLNK(de->mode))
  451. kfree(de->data);
  452. kfree(de);
  453. }
  454. void pde_put(struct proc_dir_entry *pde)
  455. {
  456. if (atomic_dec_and_test(&pde->count))
  457. free_proc_entry(pde);
  458. }
  459. /*
  460. * Remove a /proc entry and free it if it's not currently in use.
  461. */
  462. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  463. {
  464. struct proc_dir_entry *de = NULL;
  465. const char *fn = name;
  466. unsigned int len;
  467. spin_lock(&proc_subdir_lock);
  468. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  469. spin_unlock(&proc_subdir_lock);
  470. return;
  471. }
  472. len = strlen(fn);
  473. de = pde_subdir_find(parent, fn, len);
  474. if (de)
  475. rb_erase(&de->subdir_node, &parent->subdir);
  476. spin_unlock(&proc_subdir_lock);
  477. if (!de) {
  478. WARN(1, "name '%s'\n", name);
  479. return;
  480. }
  481. proc_entry_rundown(de);
  482. if (S_ISDIR(de->mode))
  483. parent->nlink--;
  484. de->nlink = 0;
  485. WARN(pde_subdir_first(de),
  486. "%s: removing non-empty directory '%s/%s', leaking at least '%s'\n",
  487. __func__, de->parent->name, de->name, pde_subdir_first(de)->name);
  488. pde_put(de);
  489. }
  490. EXPORT_SYMBOL(remove_proc_entry);
  491. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  492. {
  493. struct proc_dir_entry *root = NULL, *de, *next;
  494. const char *fn = name;
  495. unsigned int len;
  496. spin_lock(&proc_subdir_lock);
  497. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  498. spin_unlock(&proc_subdir_lock);
  499. return -ENOENT;
  500. }
  501. len = strlen(fn);
  502. root = pde_subdir_find(parent, fn, len);
  503. if (!root) {
  504. spin_unlock(&proc_subdir_lock);
  505. return -ENOENT;
  506. }
  507. rb_erase(&root->subdir_node, &parent->subdir);
  508. de = root;
  509. while (1) {
  510. next = pde_subdir_first(de);
  511. if (next) {
  512. rb_erase(&next->subdir_node, &de->subdir);
  513. de = next;
  514. continue;
  515. }
  516. spin_unlock(&proc_subdir_lock);
  517. proc_entry_rundown(de);
  518. next = de->parent;
  519. if (S_ISDIR(de->mode))
  520. next->nlink--;
  521. de->nlink = 0;
  522. if (de == root)
  523. break;
  524. pde_put(de);
  525. spin_lock(&proc_subdir_lock);
  526. de = next;
  527. }
  528. pde_put(root);
  529. return 0;
  530. }
  531. EXPORT_SYMBOL(remove_proc_subtree);
  532. void *proc_get_parent_data(const struct inode *inode)
  533. {
  534. struct proc_dir_entry *de = PDE(inode);
  535. return de->parent->data;
  536. }
  537. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  538. void proc_remove(struct proc_dir_entry *de)
  539. {
  540. if (de)
  541. remove_proc_subtree(de->name, de->parent);
  542. }
  543. EXPORT_SYMBOL(proc_remove);
  544. void *PDE_DATA(const struct inode *inode)
  545. {
  546. return __PDE_DATA(inode);
  547. }
  548. EXPORT_SYMBOL(PDE_DATA);