generic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. * Don't create negative dentries here, return -ENOENT by hand
  152. * instead.
  153. */
  154. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  155. struct dentry *dentry)
  156. {
  157. struct inode *inode;
  158. spin_lock(&proc_subdir_lock);
  159. for (de = de->subdir; de ; de = de->next) {
  160. if (de->namelen != dentry->d_name.len)
  161. continue;
  162. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  163. pde_get(de);
  164. spin_unlock(&proc_subdir_lock);
  165. inode = proc_get_inode(dir->i_sb, de);
  166. if (!inode)
  167. return ERR_PTR(-ENOMEM);
  168. d_set_d_op(dentry, &simple_dentry_operations);
  169. d_add(dentry, inode);
  170. return NULL;
  171. }
  172. }
  173. spin_unlock(&proc_subdir_lock);
  174. return ERR_PTR(-ENOENT);
  175. }
  176. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  177. unsigned int flags)
  178. {
  179. return proc_lookup_de(PDE(dir), dir, dentry);
  180. }
  181. /*
  182. * This returns non-zero if at EOF, so that the /proc
  183. * root directory can use this and check if it should
  184. * continue with the <pid> entries..
  185. *
  186. * Note that the VFS-layer doesn't care about the return
  187. * value of the readdir() call, as long as it's non-negative
  188. * for success..
  189. */
  190. int proc_readdir_de(struct proc_dir_entry *de, struct file *file,
  191. struct dir_context *ctx)
  192. {
  193. int i;
  194. if (!dir_emit_dots(file, ctx))
  195. return 0;
  196. spin_lock(&proc_subdir_lock);
  197. de = de->subdir;
  198. i = ctx->pos - 2;
  199. for (;;) {
  200. if (!de) {
  201. spin_unlock(&proc_subdir_lock);
  202. return 0;
  203. }
  204. if (!i)
  205. break;
  206. de = de->next;
  207. i--;
  208. }
  209. do {
  210. struct proc_dir_entry *next;
  211. pde_get(de);
  212. spin_unlock(&proc_subdir_lock);
  213. if (!dir_emit(ctx, de->name, de->namelen,
  214. de->low_ino, de->mode >> 12)) {
  215. pde_put(de);
  216. return 0;
  217. }
  218. spin_lock(&proc_subdir_lock);
  219. ctx->pos++;
  220. next = de->next;
  221. pde_put(de);
  222. de = next;
  223. } while (de);
  224. spin_unlock(&proc_subdir_lock);
  225. return 1;
  226. }
  227. int proc_readdir(struct file *file, struct dir_context *ctx)
  228. {
  229. struct inode *inode = file_inode(file);
  230. return proc_readdir_de(PDE(inode), file, ctx);
  231. }
  232. /*
  233. * These are the generic /proc directory operations. They
  234. * use the in-memory "struct proc_dir_entry" tree to parse
  235. * the /proc directory.
  236. */
  237. static const struct file_operations proc_dir_operations = {
  238. .llseek = generic_file_llseek,
  239. .read = generic_read_dir,
  240. .iterate = proc_readdir,
  241. };
  242. /*
  243. * proc directories can do almost nothing..
  244. */
  245. static const struct inode_operations proc_dir_inode_operations = {
  246. .lookup = proc_lookup,
  247. .getattr = proc_getattr,
  248. .setattr = proc_notify_change,
  249. };
  250. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  251. {
  252. struct proc_dir_entry *tmp;
  253. int ret;
  254. ret = proc_alloc_inum(&dp->low_ino);
  255. if (ret)
  256. return ret;
  257. if (S_ISDIR(dp->mode)) {
  258. dp->proc_fops = &proc_dir_operations;
  259. dp->proc_iops = &proc_dir_inode_operations;
  260. dir->nlink++;
  261. } else if (S_ISLNK(dp->mode)) {
  262. dp->proc_iops = &proc_link_inode_operations;
  263. } else if (S_ISREG(dp->mode)) {
  264. BUG_ON(dp->proc_fops == NULL);
  265. dp->proc_iops = &proc_file_inode_operations;
  266. } else {
  267. WARN_ON(1);
  268. return -EINVAL;
  269. }
  270. spin_lock(&proc_subdir_lock);
  271. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  272. if (strcmp(tmp->name, dp->name) == 0) {
  273. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  274. dir->name, dp->name);
  275. break;
  276. }
  277. dp->next = dir->subdir;
  278. dp->parent = dir;
  279. dir->subdir = dp;
  280. spin_unlock(&proc_subdir_lock);
  281. return 0;
  282. }
  283. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  284. const char *name,
  285. umode_t mode,
  286. nlink_t nlink)
  287. {
  288. struct proc_dir_entry *ent = NULL;
  289. const char *fn = name;
  290. unsigned int len;
  291. /* make sure name is valid */
  292. if (!name || !strlen(name))
  293. goto out;
  294. if (xlate_proc_name(name, parent, &fn) != 0)
  295. goto out;
  296. /* At this point there must not be any '/' characters beyond *fn */
  297. if (strchr(fn, '/'))
  298. goto out;
  299. len = strlen(fn);
  300. ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  301. if (!ent)
  302. goto out;
  303. memcpy(ent->name, fn, len + 1);
  304. ent->namelen = len;
  305. ent->mode = mode;
  306. ent->nlink = nlink;
  307. atomic_set(&ent->count, 1);
  308. spin_lock_init(&ent->pde_unload_lock);
  309. INIT_LIST_HEAD(&ent->pde_openers);
  310. out:
  311. return ent;
  312. }
  313. struct proc_dir_entry *proc_symlink(const char *name,
  314. struct proc_dir_entry *parent, const char *dest)
  315. {
  316. struct proc_dir_entry *ent;
  317. ent = __proc_create(&parent, name,
  318. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  319. if (ent) {
  320. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  321. if (ent->data) {
  322. strcpy((char*)ent->data,dest);
  323. if (proc_register(parent, ent) < 0) {
  324. kfree(ent->data);
  325. kfree(ent);
  326. ent = NULL;
  327. }
  328. } else {
  329. kfree(ent);
  330. ent = NULL;
  331. }
  332. }
  333. return ent;
  334. }
  335. EXPORT_SYMBOL(proc_symlink);
  336. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  337. struct proc_dir_entry *parent, void *data)
  338. {
  339. struct proc_dir_entry *ent;
  340. if (mode == 0)
  341. mode = S_IRUGO | S_IXUGO;
  342. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  343. if (ent) {
  344. ent->data = data;
  345. if (proc_register(parent, ent) < 0) {
  346. kfree(ent);
  347. ent = NULL;
  348. }
  349. }
  350. return ent;
  351. }
  352. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  353. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  354. struct proc_dir_entry *parent)
  355. {
  356. return proc_mkdir_data(name, mode, parent, NULL);
  357. }
  358. EXPORT_SYMBOL(proc_mkdir_mode);
  359. struct proc_dir_entry *proc_mkdir(const char *name,
  360. struct proc_dir_entry *parent)
  361. {
  362. return proc_mkdir_data(name, 0, parent, NULL);
  363. }
  364. EXPORT_SYMBOL(proc_mkdir);
  365. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  366. struct proc_dir_entry *parent,
  367. const struct file_operations *proc_fops,
  368. void *data)
  369. {
  370. struct proc_dir_entry *pde;
  371. if ((mode & S_IFMT) == 0)
  372. mode |= S_IFREG;
  373. if (!S_ISREG(mode)) {
  374. WARN_ON(1); /* use proc_mkdir() */
  375. return NULL;
  376. }
  377. if ((mode & S_IALLUGO) == 0)
  378. mode |= S_IRUGO;
  379. pde = __proc_create(&parent, name, mode, 1);
  380. if (!pde)
  381. goto out;
  382. pde->proc_fops = proc_fops;
  383. pde->data = data;
  384. if (proc_register(parent, pde) < 0)
  385. goto out_free;
  386. return pde;
  387. out_free:
  388. kfree(pde);
  389. out:
  390. return NULL;
  391. }
  392. EXPORT_SYMBOL(proc_create_data);
  393. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  394. {
  395. de->size = size;
  396. }
  397. EXPORT_SYMBOL(proc_set_size);
  398. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  399. {
  400. de->uid = uid;
  401. de->gid = gid;
  402. }
  403. EXPORT_SYMBOL(proc_set_user);
  404. static void free_proc_entry(struct proc_dir_entry *de)
  405. {
  406. proc_free_inum(de->low_ino);
  407. if (S_ISLNK(de->mode))
  408. kfree(de->data);
  409. kfree(de);
  410. }
  411. void pde_put(struct proc_dir_entry *pde)
  412. {
  413. if (atomic_dec_and_test(&pde->count))
  414. free_proc_entry(pde);
  415. }
  416. /*
  417. * Remove a /proc entry and free it if it's not currently in use.
  418. */
  419. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  420. {
  421. struct proc_dir_entry **p;
  422. struct proc_dir_entry *de = NULL;
  423. const char *fn = name;
  424. unsigned int len;
  425. spin_lock(&proc_subdir_lock);
  426. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  427. spin_unlock(&proc_subdir_lock);
  428. return;
  429. }
  430. len = strlen(fn);
  431. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  432. if (proc_match(len, fn, *p)) {
  433. de = *p;
  434. *p = de->next;
  435. de->next = NULL;
  436. break;
  437. }
  438. }
  439. spin_unlock(&proc_subdir_lock);
  440. if (!de) {
  441. WARN(1, "name '%s'\n", name);
  442. return;
  443. }
  444. proc_entry_rundown(de);
  445. if (S_ISDIR(de->mode))
  446. parent->nlink--;
  447. de->nlink = 0;
  448. WARN(de->subdir, "%s: removing non-empty directory "
  449. "'%s/%s', leaking at least '%s'\n", __func__,
  450. de->parent->name, de->name, de->subdir->name);
  451. pde_put(de);
  452. }
  453. EXPORT_SYMBOL(remove_proc_entry);
  454. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  455. {
  456. struct proc_dir_entry **p;
  457. struct proc_dir_entry *root = NULL, *de, *next;
  458. const char *fn = name;
  459. unsigned int len;
  460. spin_lock(&proc_subdir_lock);
  461. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  462. spin_unlock(&proc_subdir_lock);
  463. return -ENOENT;
  464. }
  465. len = strlen(fn);
  466. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  467. if (proc_match(len, fn, *p)) {
  468. root = *p;
  469. *p = root->next;
  470. root->next = NULL;
  471. break;
  472. }
  473. }
  474. if (!root) {
  475. spin_unlock(&proc_subdir_lock);
  476. return -ENOENT;
  477. }
  478. de = root;
  479. while (1) {
  480. next = de->subdir;
  481. if (next) {
  482. de->subdir = next->next;
  483. next->next = NULL;
  484. de = next;
  485. continue;
  486. }
  487. spin_unlock(&proc_subdir_lock);
  488. proc_entry_rundown(de);
  489. next = de->parent;
  490. if (S_ISDIR(de->mode))
  491. next->nlink--;
  492. de->nlink = 0;
  493. if (de == root)
  494. break;
  495. pde_put(de);
  496. spin_lock(&proc_subdir_lock);
  497. de = next;
  498. }
  499. pde_put(root);
  500. return 0;
  501. }
  502. EXPORT_SYMBOL(remove_proc_subtree);
  503. void *proc_get_parent_data(const struct inode *inode)
  504. {
  505. struct proc_dir_entry *de = PDE(inode);
  506. return de->parent->data;
  507. }
  508. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  509. void proc_remove(struct proc_dir_entry *de)
  510. {
  511. if (de)
  512. remove_proc_subtree(de->name, de->parent);
  513. }
  514. EXPORT_SYMBOL(proc_remove);
  515. void *PDE_DATA(const struct inode *inode)
  516. {
  517. return __PDE_DATA(inode);
  518. }
  519. EXPORT_SYMBOL(PDE_DATA);