generic.c 13 KB

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