generic.c 12 KB

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