generic.c 13 KB

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