generic.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 *filp, void *dirent,
  205. filldir_t filldir)
  206. {
  207. unsigned int ino;
  208. int i;
  209. struct inode *inode = file_inode(filp);
  210. int ret = 0;
  211. ino = inode->i_ino;
  212. i = filp->f_pos;
  213. switch (i) {
  214. case 0:
  215. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  216. goto out;
  217. i++;
  218. filp->f_pos++;
  219. /* fall through */
  220. case 1:
  221. if (filldir(dirent, "..", 2, i,
  222. parent_ino(filp->f_path.dentry),
  223. DT_DIR) < 0)
  224. goto out;
  225. i++;
  226. filp->f_pos++;
  227. /* fall through */
  228. default:
  229. spin_lock(&proc_subdir_lock);
  230. de = de->subdir;
  231. i -= 2;
  232. for (;;) {
  233. if (!de) {
  234. ret = 1;
  235. spin_unlock(&proc_subdir_lock);
  236. goto out;
  237. }
  238. if (!i)
  239. break;
  240. de = de->next;
  241. i--;
  242. }
  243. do {
  244. struct proc_dir_entry *next;
  245. /* filldir passes info to user space */
  246. pde_get(de);
  247. spin_unlock(&proc_subdir_lock);
  248. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  249. de->low_ino, de->mode >> 12) < 0) {
  250. pde_put(de);
  251. goto out;
  252. }
  253. spin_lock(&proc_subdir_lock);
  254. filp->f_pos++;
  255. next = de->next;
  256. pde_put(de);
  257. de = next;
  258. } while (de);
  259. spin_unlock(&proc_subdir_lock);
  260. }
  261. ret = 1;
  262. out:
  263. return ret;
  264. }
  265. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  266. {
  267. struct inode *inode = file_inode(filp);
  268. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  269. }
  270. /*
  271. * These are the generic /proc directory operations. They
  272. * use the in-memory "struct proc_dir_entry" tree to parse
  273. * the /proc directory.
  274. */
  275. static const struct file_operations proc_dir_operations = {
  276. .llseek = generic_file_llseek,
  277. .read = generic_read_dir,
  278. .readdir = proc_readdir,
  279. };
  280. /*
  281. * proc directories can do almost nothing..
  282. */
  283. static const struct inode_operations proc_dir_inode_operations = {
  284. .lookup = proc_lookup,
  285. .getattr = proc_getattr,
  286. .setattr = proc_notify_change,
  287. };
  288. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  289. {
  290. struct proc_dir_entry *tmp;
  291. int ret;
  292. ret = proc_alloc_inum(&dp->low_ino);
  293. if (ret)
  294. return ret;
  295. if (S_ISDIR(dp->mode)) {
  296. dp->proc_fops = &proc_dir_operations;
  297. dp->proc_iops = &proc_dir_inode_operations;
  298. dir->nlink++;
  299. } else if (S_ISLNK(dp->mode)) {
  300. dp->proc_iops = &proc_link_inode_operations;
  301. } else if (S_ISREG(dp->mode)) {
  302. BUG_ON(dp->proc_fops == NULL);
  303. dp->proc_iops = &proc_file_inode_operations;
  304. } else {
  305. WARN_ON(1);
  306. return -EINVAL;
  307. }
  308. spin_lock(&proc_subdir_lock);
  309. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  310. if (strcmp(tmp->name, dp->name) == 0) {
  311. WARN(1, "proc_dir_entry '%s/%s' already registered\n",
  312. dir->name, dp->name);
  313. break;
  314. }
  315. dp->next = dir->subdir;
  316. dp->parent = dir;
  317. dir->subdir = dp;
  318. spin_unlock(&proc_subdir_lock);
  319. return 0;
  320. }
  321. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  322. const char *name,
  323. umode_t mode,
  324. nlink_t nlink)
  325. {
  326. struct proc_dir_entry *ent = NULL;
  327. const char *fn = name;
  328. unsigned int len;
  329. /* make sure name is valid */
  330. if (!name || !strlen(name))
  331. goto out;
  332. if (xlate_proc_name(name, parent, &fn) != 0)
  333. goto out;
  334. /* At this point there must not be any '/' characters beyond *fn */
  335. if (strchr(fn, '/'))
  336. goto out;
  337. len = strlen(fn);
  338. ent = kzalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  339. if (!ent)
  340. goto out;
  341. memcpy(ent->name, fn, len + 1);
  342. ent->namelen = len;
  343. ent->mode = mode;
  344. ent->nlink = nlink;
  345. atomic_set(&ent->count, 1);
  346. spin_lock_init(&ent->pde_unload_lock);
  347. INIT_LIST_HEAD(&ent->pde_openers);
  348. out:
  349. return ent;
  350. }
  351. struct proc_dir_entry *proc_symlink(const char *name,
  352. struct proc_dir_entry *parent, const char *dest)
  353. {
  354. struct proc_dir_entry *ent;
  355. ent = __proc_create(&parent, name,
  356. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  357. if (ent) {
  358. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  359. if (ent->data) {
  360. strcpy((char*)ent->data,dest);
  361. if (proc_register(parent, ent) < 0) {
  362. kfree(ent->data);
  363. kfree(ent);
  364. ent = NULL;
  365. }
  366. } else {
  367. kfree(ent);
  368. ent = NULL;
  369. }
  370. }
  371. return ent;
  372. }
  373. EXPORT_SYMBOL(proc_symlink);
  374. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  375. struct proc_dir_entry *parent)
  376. {
  377. struct proc_dir_entry *ent;
  378. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  379. if (ent) {
  380. if (proc_register(parent, ent) < 0) {
  381. kfree(ent);
  382. ent = NULL;
  383. }
  384. }
  385. return ent;
  386. }
  387. EXPORT_SYMBOL(proc_mkdir_mode);
  388. struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
  389. struct proc_dir_entry *parent)
  390. {
  391. struct proc_dir_entry *ent;
  392. ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
  393. if (ent) {
  394. ent->data = net;
  395. if (proc_register(parent, ent) < 0) {
  396. kfree(ent);
  397. ent = NULL;
  398. }
  399. }
  400. return ent;
  401. }
  402. EXPORT_SYMBOL_GPL(proc_net_mkdir);
  403. struct proc_dir_entry *proc_mkdir(const char *name,
  404. struct proc_dir_entry *parent)
  405. {
  406. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  407. }
  408. EXPORT_SYMBOL(proc_mkdir);
  409. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  410. struct proc_dir_entry *parent,
  411. const struct file_operations *proc_fops,
  412. void *data)
  413. {
  414. struct proc_dir_entry *pde;
  415. if ((mode & S_IFMT) == 0)
  416. mode |= S_IFREG;
  417. if (!S_ISREG(mode)) {
  418. WARN_ON(1); /* use proc_mkdir() */
  419. return NULL;
  420. }
  421. if ((mode & S_IALLUGO) == 0)
  422. mode |= S_IRUGO;
  423. pde = __proc_create(&parent, name, mode, 1);
  424. if (!pde)
  425. goto out;
  426. pde->proc_fops = proc_fops;
  427. pde->data = data;
  428. if (proc_register(parent, pde) < 0)
  429. goto out_free;
  430. return pde;
  431. out_free:
  432. kfree(pde);
  433. out:
  434. return NULL;
  435. }
  436. EXPORT_SYMBOL(proc_create_data);
  437. static void free_proc_entry(struct proc_dir_entry *de)
  438. {
  439. proc_free_inum(de->low_ino);
  440. if (S_ISLNK(de->mode))
  441. kfree(de->data);
  442. kfree(de);
  443. }
  444. void pde_put(struct proc_dir_entry *pde)
  445. {
  446. if (atomic_dec_and_test(&pde->count))
  447. free_proc_entry(pde);
  448. }
  449. /*
  450. * Remove a /proc entry and free it if it's not currently in use.
  451. */
  452. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  453. {
  454. struct proc_dir_entry **p;
  455. struct proc_dir_entry *de = NULL;
  456. const char *fn = name;
  457. unsigned int len;
  458. spin_lock(&proc_subdir_lock);
  459. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  460. spin_unlock(&proc_subdir_lock);
  461. return;
  462. }
  463. len = strlen(fn);
  464. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  465. if (proc_match(len, fn, *p)) {
  466. de = *p;
  467. *p = de->next;
  468. de->next = NULL;
  469. break;
  470. }
  471. }
  472. spin_unlock(&proc_subdir_lock);
  473. if (!de) {
  474. WARN(1, "name '%s'\n", name);
  475. return;
  476. }
  477. proc_entry_rundown(de);
  478. if (S_ISDIR(de->mode))
  479. parent->nlink--;
  480. de->nlink = 0;
  481. WARN(de->subdir, "%s: removing non-empty directory "
  482. "'%s/%s', leaking at least '%s'\n", __func__,
  483. de->parent->name, de->name, de->subdir->name);
  484. pde_put(de);
  485. }
  486. EXPORT_SYMBOL(remove_proc_entry);
  487. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  488. {
  489. struct proc_dir_entry **p;
  490. struct proc_dir_entry *root = NULL, *de, *next;
  491. const char *fn = name;
  492. unsigned int len;
  493. spin_lock(&proc_subdir_lock);
  494. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  495. spin_unlock(&proc_subdir_lock);
  496. return -ENOENT;
  497. }
  498. len = strlen(fn);
  499. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  500. if (proc_match(len, fn, *p)) {
  501. root = *p;
  502. *p = root->next;
  503. root->next = NULL;
  504. break;
  505. }
  506. }
  507. if (!root) {
  508. spin_unlock(&proc_subdir_lock);
  509. return -ENOENT;
  510. }
  511. de = root;
  512. while (1) {
  513. next = de->subdir;
  514. if (next) {
  515. de->subdir = next->next;
  516. next->next = NULL;
  517. de = next;
  518. continue;
  519. }
  520. spin_unlock(&proc_subdir_lock);
  521. proc_entry_rundown(de);
  522. next = de->parent;
  523. if (S_ISDIR(de->mode))
  524. next->nlink--;
  525. de->nlink = 0;
  526. if (de == root)
  527. break;
  528. pde_put(de);
  529. spin_lock(&proc_subdir_lock);
  530. de = next;
  531. }
  532. pde_put(root);
  533. return 0;
  534. }
  535. EXPORT_SYMBOL(remove_proc_subtree);