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. static 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;
  289. struct qstr qstr;
  290. if (xlate_proc_name(name, parent, &fn) != 0)
  291. goto out;
  292. qstr.name = fn;
  293. qstr.len = strlen(fn);
  294. if (qstr.len == 0 || qstr.len >= 256) {
  295. WARN(1, "name len %u\n", qstr.len);
  296. return NULL;
  297. }
  298. if (*parent == &proc_root && name_to_int(&qstr) != ~0U) {
  299. WARN(1, "create '/proc/%s' by hand\n", qstr.name);
  300. return NULL;
  301. }
  302. ent = kzalloc(sizeof(struct proc_dir_entry) + qstr.len + 1, GFP_KERNEL);
  303. if (!ent)
  304. goto out;
  305. memcpy(ent->name, fn, qstr.len + 1);
  306. ent->namelen = qstr.len;
  307. ent->mode = mode;
  308. ent->nlink = nlink;
  309. atomic_set(&ent->count, 1);
  310. spin_lock_init(&ent->pde_unload_lock);
  311. INIT_LIST_HEAD(&ent->pde_openers);
  312. out:
  313. return ent;
  314. }
  315. struct proc_dir_entry *proc_symlink(const char *name,
  316. struct proc_dir_entry *parent, const char *dest)
  317. {
  318. struct proc_dir_entry *ent;
  319. ent = __proc_create(&parent, name,
  320. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  321. if (ent) {
  322. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  323. if (ent->data) {
  324. strcpy((char*)ent->data,dest);
  325. if (proc_register(parent, ent) < 0) {
  326. kfree(ent->data);
  327. kfree(ent);
  328. ent = NULL;
  329. }
  330. } else {
  331. kfree(ent);
  332. ent = NULL;
  333. }
  334. }
  335. return ent;
  336. }
  337. EXPORT_SYMBOL(proc_symlink);
  338. struct proc_dir_entry *proc_mkdir_data(const char *name, umode_t mode,
  339. struct proc_dir_entry *parent, void *data)
  340. {
  341. struct proc_dir_entry *ent;
  342. if (mode == 0)
  343. mode = S_IRUGO | S_IXUGO;
  344. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  345. if (ent) {
  346. ent->data = data;
  347. if (proc_register(parent, ent) < 0) {
  348. kfree(ent);
  349. ent = NULL;
  350. }
  351. }
  352. return ent;
  353. }
  354. EXPORT_SYMBOL_GPL(proc_mkdir_data);
  355. struct proc_dir_entry *proc_mkdir_mode(const char *name, umode_t mode,
  356. struct proc_dir_entry *parent)
  357. {
  358. return proc_mkdir_data(name, mode, parent, NULL);
  359. }
  360. EXPORT_SYMBOL(proc_mkdir_mode);
  361. struct proc_dir_entry *proc_mkdir(const char *name,
  362. struct proc_dir_entry *parent)
  363. {
  364. return proc_mkdir_data(name, 0, parent, NULL);
  365. }
  366. EXPORT_SYMBOL(proc_mkdir);
  367. struct proc_dir_entry *proc_create_data(const char *name, umode_t mode,
  368. struct proc_dir_entry *parent,
  369. const struct file_operations *proc_fops,
  370. void *data)
  371. {
  372. struct proc_dir_entry *pde;
  373. if ((mode & S_IFMT) == 0)
  374. mode |= S_IFREG;
  375. if (!S_ISREG(mode)) {
  376. WARN_ON(1); /* use proc_mkdir() */
  377. return NULL;
  378. }
  379. if ((mode & S_IALLUGO) == 0)
  380. mode |= S_IRUGO;
  381. pde = __proc_create(&parent, name, mode, 1);
  382. if (!pde)
  383. goto out;
  384. pde->proc_fops = proc_fops;
  385. pde->data = data;
  386. if (proc_register(parent, pde) < 0)
  387. goto out_free;
  388. return pde;
  389. out_free:
  390. kfree(pde);
  391. out:
  392. return NULL;
  393. }
  394. EXPORT_SYMBOL(proc_create_data);
  395. void proc_set_size(struct proc_dir_entry *de, loff_t size)
  396. {
  397. de->size = size;
  398. }
  399. EXPORT_SYMBOL(proc_set_size);
  400. void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid)
  401. {
  402. de->uid = uid;
  403. de->gid = gid;
  404. }
  405. EXPORT_SYMBOL(proc_set_user);
  406. static void free_proc_entry(struct proc_dir_entry *de)
  407. {
  408. proc_free_inum(de->low_ino);
  409. if (S_ISLNK(de->mode))
  410. kfree(de->data);
  411. kfree(de);
  412. }
  413. void pde_put(struct proc_dir_entry *pde)
  414. {
  415. if (atomic_dec_and_test(&pde->count))
  416. free_proc_entry(pde);
  417. }
  418. /*
  419. * Remove a /proc entry and free it if it's not currently in use.
  420. */
  421. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  422. {
  423. struct proc_dir_entry **p;
  424. struct proc_dir_entry *de = NULL;
  425. const char *fn = name;
  426. unsigned int len;
  427. spin_lock(&proc_subdir_lock);
  428. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  429. spin_unlock(&proc_subdir_lock);
  430. return;
  431. }
  432. len = strlen(fn);
  433. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  434. if (proc_match(len, fn, *p)) {
  435. de = *p;
  436. *p = de->next;
  437. de->next = NULL;
  438. break;
  439. }
  440. }
  441. spin_unlock(&proc_subdir_lock);
  442. if (!de) {
  443. WARN(1, "name '%s'\n", name);
  444. return;
  445. }
  446. proc_entry_rundown(de);
  447. if (S_ISDIR(de->mode))
  448. parent->nlink--;
  449. de->nlink = 0;
  450. WARN(de->subdir, "%s: removing non-empty directory "
  451. "'%s/%s', leaking at least '%s'\n", __func__,
  452. de->parent->name, de->name, de->subdir->name);
  453. pde_put(de);
  454. }
  455. EXPORT_SYMBOL(remove_proc_entry);
  456. int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
  457. {
  458. struct proc_dir_entry **p;
  459. struct proc_dir_entry *root = NULL, *de, *next;
  460. const char *fn = name;
  461. unsigned int len;
  462. spin_lock(&proc_subdir_lock);
  463. if (__xlate_proc_name(name, &parent, &fn) != 0) {
  464. spin_unlock(&proc_subdir_lock);
  465. return -ENOENT;
  466. }
  467. len = strlen(fn);
  468. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  469. if (proc_match(len, fn, *p)) {
  470. root = *p;
  471. *p = root->next;
  472. root->next = NULL;
  473. break;
  474. }
  475. }
  476. if (!root) {
  477. spin_unlock(&proc_subdir_lock);
  478. return -ENOENT;
  479. }
  480. de = root;
  481. while (1) {
  482. next = de->subdir;
  483. if (next) {
  484. de->subdir = next->next;
  485. next->next = NULL;
  486. de = next;
  487. continue;
  488. }
  489. spin_unlock(&proc_subdir_lock);
  490. proc_entry_rundown(de);
  491. next = de->parent;
  492. if (S_ISDIR(de->mode))
  493. next->nlink--;
  494. de->nlink = 0;
  495. if (de == root)
  496. break;
  497. pde_put(de);
  498. spin_lock(&proc_subdir_lock);
  499. de = next;
  500. }
  501. pde_put(root);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL(remove_proc_subtree);
  505. void *proc_get_parent_data(const struct inode *inode)
  506. {
  507. struct proc_dir_entry *de = PDE(inode);
  508. return de->parent->data;
  509. }
  510. EXPORT_SYMBOL_GPL(proc_get_parent_data);
  511. void proc_remove(struct proc_dir_entry *de)
  512. {
  513. if (de)
  514. remove_proc_subtree(de->name, de->parent);
  515. }
  516. EXPORT_SYMBOL(proc_remove);
  517. void *PDE_DATA(const struct inode *inode)
  518. {
  519. return __PDE_DATA(inode);
  520. }
  521. EXPORT_SYMBOL(PDE_DATA);