generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/module.h>
  15. #include <linux/mount.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/init.h>
  18. #include <linux/idr.h>
  19. #include <linux/namei.h>
  20. #include <linux/bitops.h>
  21. #include <asm/uaccess.h>
  22. #include "internal.h"
  23. static ssize_t proc_file_read(struct file *file, char __user *buf,
  24. size_t nbytes, loff_t *ppos);
  25. static ssize_t proc_file_write(struct file *file, const char __user *buffer,
  26. size_t count, loff_t *ppos);
  27. static loff_t proc_file_lseek(struct file *, loff_t, int);
  28. int proc_match(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 struct file_operations proc_file_operations = {
  35. .llseek = proc_file_lseek,
  36. .read = proc_file_read,
  37. .write = proc_file_write,
  38. };
  39. /* buffer size is one page but our output routines use some slack for overruns */
  40. #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
  41. static ssize_t
  42. proc_file_read(struct file *file, char __user *buf, size_t nbytes,
  43. loff_t *ppos)
  44. {
  45. struct inode * inode = file->f_dentry->d_inode;
  46. char *page;
  47. ssize_t retval=0;
  48. int eof=0;
  49. ssize_t n, count;
  50. char *start;
  51. struct proc_dir_entry * dp;
  52. unsigned long long pos;
  53. /*
  54. * Gaah, please just use "seq_file" instead. The legacy /proc
  55. * interfaces cut loff_t down to off_t for reads, and ignore
  56. * the offset entirely for writes..
  57. */
  58. pos = *ppos;
  59. if (pos > MAX_NON_LFS)
  60. return 0;
  61. if (nbytes > MAX_NON_LFS - pos)
  62. nbytes = MAX_NON_LFS - pos;
  63. dp = PDE(inode);
  64. if (!(page = (char*) __get_free_page(GFP_KERNEL)))
  65. return -ENOMEM;
  66. while ((nbytes > 0) && !eof) {
  67. count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
  68. start = NULL;
  69. if (dp->get_info) {
  70. /* Handle old net routines */
  71. n = dp->get_info(page, &start, *ppos, count);
  72. if (n < count)
  73. eof = 1;
  74. } else if (dp->read_proc) {
  75. /*
  76. * How to be a proc read function
  77. * ------------------------------
  78. * Prototype:
  79. * int f(char *buffer, char **start, off_t offset,
  80. * int count, int *peof, void *dat)
  81. *
  82. * Assume that the buffer is "count" bytes in size.
  83. *
  84. * If you know you have supplied all the data you
  85. * have, set *peof.
  86. *
  87. * You have three ways to return data:
  88. * 0) Leave *start = NULL. (This is the default.)
  89. * Put the data of the requested offset at that
  90. * offset within the buffer. Return the number (n)
  91. * of bytes there are from the beginning of the
  92. * buffer up to the last byte of data. If the
  93. * number of supplied bytes (= n - offset) is
  94. * greater than zero and you didn't signal eof
  95. * and the reader is prepared to take more data
  96. * you will be called again with the requested
  97. * offset advanced by the number of bytes
  98. * absorbed. This interface is useful for files
  99. * no larger than the buffer.
  100. * 1) Set *start = an unsigned long value less than
  101. * the buffer address but greater than zero.
  102. * Put the data of the requested offset at the
  103. * beginning of the buffer. Return the number of
  104. * bytes of data placed there. If this number is
  105. * greater than zero and you didn't signal eof
  106. * and the reader is prepared to take more data
  107. * you will be called again with the requested
  108. * offset advanced by *start. This interface is
  109. * useful when you have a large file consisting
  110. * of a series of blocks which you want to count
  111. * and return as wholes.
  112. * (Hack by Paul.Russell@rustcorp.com.au)
  113. * 2) Set *start = an address within the buffer.
  114. * Put the data of the requested offset at *start.
  115. * Return the number of bytes of data placed there.
  116. * If this number is greater than zero and you
  117. * didn't signal eof and the reader is prepared to
  118. * take more data you will be called again with the
  119. * requested offset advanced by the number of bytes
  120. * absorbed.
  121. */
  122. n = dp->read_proc(page, &start, *ppos,
  123. count, &eof, dp->data);
  124. } else
  125. break;
  126. if (n == 0) /* end of file */
  127. break;
  128. if (n < 0) { /* error */
  129. if (retval == 0)
  130. retval = n;
  131. break;
  132. }
  133. if (start == NULL) {
  134. if (n > PAGE_SIZE) {
  135. printk(KERN_ERR
  136. "proc_file_read: Apparent buffer overflow!\n");
  137. n = PAGE_SIZE;
  138. }
  139. n -= *ppos;
  140. if (n <= 0)
  141. break;
  142. if (n > count)
  143. n = count;
  144. start = page + *ppos;
  145. } else if (start < page) {
  146. if (n > PAGE_SIZE) {
  147. printk(KERN_ERR
  148. "proc_file_read: Apparent buffer overflow!\n");
  149. n = PAGE_SIZE;
  150. }
  151. if (n > count) {
  152. /*
  153. * Don't reduce n because doing so might
  154. * cut off part of a data block.
  155. */
  156. printk(KERN_WARNING
  157. "proc_file_read: Read count exceeded\n");
  158. }
  159. } else /* start >= page */ {
  160. unsigned long startoff = (unsigned long)(start - page);
  161. if (n > (PAGE_SIZE - startoff)) {
  162. printk(KERN_ERR
  163. "proc_file_read: Apparent buffer overflow!\n");
  164. n = PAGE_SIZE - startoff;
  165. }
  166. if (n > count)
  167. n = count;
  168. }
  169. n -= copy_to_user(buf, start < page ? page : start, n);
  170. if (n == 0) {
  171. if (retval == 0)
  172. retval = -EFAULT;
  173. break;
  174. }
  175. *ppos += start < page ? (unsigned long)start : n;
  176. nbytes -= n;
  177. buf += n;
  178. retval += n;
  179. }
  180. free_page((unsigned long) page);
  181. return retval;
  182. }
  183. static ssize_t
  184. proc_file_write(struct file *file, const char __user *buffer,
  185. size_t count, loff_t *ppos)
  186. {
  187. struct inode *inode = file->f_dentry->d_inode;
  188. struct proc_dir_entry * dp;
  189. dp = PDE(inode);
  190. if (!dp->write_proc)
  191. return -EIO;
  192. /* FIXME: does this routine need ppos? probably... */
  193. return dp->write_proc(file, buffer, count, dp->data);
  194. }
  195. static loff_t
  196. proc_file_lseek(struct file *file, loff_t offset, int orig)
  197. {
  198. loff_t retval = -EINVAL;
  199. switch (orig) {
  200. case 1:
  201. offset += file->f_pos;
  202. /* fallthrough */
  203. case 0:
  204. if (offset < 0 || offset > MAX_NON_LFS)
  205. break;
  206. file->f_pos = retval = offset;
  207. }
  208. return retval;
  209. }
  210. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  211. {
  212. struct inode *inode = dentry->d_inode;
  213. struct proc_dir_entry *de = PDE(inode);
  214. int error;
  215. error = inode_change_ok(inode, iattr);
  216. if (error)
  217. goto out;
  218. error = inode_setattr(inode, iattr);
  219. if (error)
  220. goto out;
  221. de->uid = inode->i_uid;
  222. de->gid = inode->i_gid;
  223. de->mode = inode->i_mode;
  224. out:
  225. return error;
  226. }
  227. static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
  228. struct kstat *stat)
  229. {
  230. struct inode *inode = dentry->d_inode;
  231. struct proc_dir_entry *de = PROC_I(inode)->pde;
  232. if (de && de->nlink)
  233. inode->i_nlink = de->nlink;
  234. generic_fillattr(inode, stat);
  235. return 0;
  236. }
  237. static struct inode_operations proc_file_inode_operations = {
  238. .setattr = proc_notify_change,
  239. };
  240. /*
  241. * This function parses a name such as "tty/driver/serial", and
  242. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  243. * returns "serial" in residual.
  244. */
  245. static int xlate_proc_name(const char *name,
  246. struct proc_dir_entry **ret, const char **residual)
  247. {
  248. const char *cp = name, *next;
  249. struct proc_dir_entry *de;
  250. int len;
  251. de = &proc_root;
  252. while (1) {
  253. next = strchr(cp, '/');
  254. if (!next)
  255. break;
  256. len = next - cp;
  257. for (de = de->subdir; de ; de = de->next) {
  258. if (proc_match(len, cp, de))
  259. break;
  260. }
  261. if (!de)
  262. return -ENOENT;
  263. cp += len + 1;
  264. }
  265. *residual = cp;
  266. *ret = de;
  267. return 0;
  268. }
  269. static DEFINE_IDR(proc_inum_idr);
  270. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  271. #define PROC_DYNAMIC_FIRST 0xF0000000UL
  272. /*
  273. * Return an inode number between PROC_DYNAMIC_FIRST and
  274. * 0xffffffff, or zero on failure.
  275. */
  276. static unsigned int get_inode_number(void)
  277. {
  278. int i, inum = 0;
  279. int error;
  280. retry:
  281. if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
  282. return 0;
  283. spin_lock(&proc_inum_lock);
  284. error = idr_get_new(&proc_inum_idr, NULL, &i);
  285. spin_unlock(&proc_inum_lock);
  286. if (error == -EAGAIN)
  287. goto retry;
  288. else if (error)
  289. return 0;
  290. inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
  291. /* inum will never be more than 0xf0ffffff, so no check
  292. * for overflow.
  293. */
  294. return inum;
  295. }
  296. static void release_inode_number(unsigned int inum)
  297. {
  298. int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
  299. spin_lock(&proc_inum_lock);
  300. idr_remove(&proc_inum_idr, id);
  301. spin_unlock(&proc_inum_lock);
  302. }
  303. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  304. {
  305. nd_set_link(nd, PDE(dentry->d_inode)->data);
  306. return NULL;
  307. }
  308. static struct inode_operations proc_link_inode_operations = {
  309. .readlink = generic_readlink,
  310. .follow_link = proc_follow_link,
  311. };
  312. /*
  313. * As some entries in /proc are volatile, we want to
  314. * get rid of unused dentries. This could be made
  315. * smarter: we could keep a "volatile" flag in the
  316. * inode to indicate which ones to keep.
  317. */
  318. static int proc_delete_dentry(struct dentry * dentry)
  319. {
  320. return 1;
  321. }
  322. static struct dentry_operations proc_dentry_operations =
  323. {
  324. .d_delete = proc_delete_dentry,
  325. };
  326. /*
  327. * Don't create negative dentries here, return -ENOENT by hand
  328. * instead.
  329. */
  330. struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  331. {
  332. struct inode *inode = NULL;
  333. struct proc_dir_entry * de;
  334. int error = -ENOENT;
  335. lock_kernel();
  336. de = PDE(dir);
  337. if (de) {
  338. for (de = de->subdir; de ; de = de->next) {
  339. if (de->namelen != dentry->d_name.len)
  340. continue;
  341. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  342. unsigned int ino = de->low_ino;
  343. error = -EINVAL;
  344. inode = proc_get_inode(dir->i_sb, ino, de);
  345. break;
  346. }
  347. }
  348. }
  349. unlock_kernel();
  350. if (inode) {
  351. dentry->d_op = &proc_dentry_operations;
  352. d_add(dentry, inode);
  353. return NULL;
  354. }
  355. return ERR_PTR(error);
  356. }
  357. /*
  358. * This returns non-zero if at EOF, so that the /proc
  359. * root directory can use this and check if it should
  360. * continue with the <pid> entries..
  361. *
  362. * Note that the VFS-layer doesn't care about the return
  363. * value of the readdir() call, as long as it's non-negative
  364. * for success..
  365. */
  366. int proc_readdir(struct file * filp,
  367. void * dirent, filldir_t filldir)
  368. {
  369. struct proc_dir_entry * de;
  370. unsigned int ino;
  371. int i;
  372. struct inode *inode = filp->f_dentry->d_inode;
  373. int ret = 0;
  374. lock_kernel();
  375. ino = inode->i_ino;
  376. de = PDE(inode);
  377. if (!de) {
  378. ret = -EINVAL;
  379. goto out;
  380. }
  381. i = filp->f_pos;
  382. switch (i) {
  383. case 0:
  384. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  385. goto out;
  386. i++;
  387. filp->f_pos++;
  388. /* fall through */
  389. case 1:
  390. if (filldir(dirent, "..", 2, i,
  391. parent_ino(filp->f_dentry),
  392. DT_DIR) < 0)
  393. goto out;
  394. i++;
  395. filp->f_pos++;
  396. /* fall through */
  397. default:
  398. de = de->subdir;
  399. i -= 2;
  400. for (;;) {
  401. if (!de) {
  402. ret = 1;
  403. goto out;
  404. }
  405. if (!i)
  406. break;
  407. de = de->next;
  408. i--;
  409. }
  410. do {
  411. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  412. de->low_ino, de->mode >> 12) < 0)
  413. goto out;
  414. filp->f_pos++;
  415. de = de->next;
  416. } while (de);
  417. }
  418. ret = 1;
  419. out: unlock_kernel();
  420. return ret;
  421. }
  422. /*
  423. * These are the generic /proc directory operations. They
  424. * use the in-memory "struct proc_dir_entry" tree to parse
  425. * the /proc directory.
  426. */
  427. static struct file_operations proc_dir_operations = {
  428. .read = generic_read_dir,
  429. .readdir = proc_readdir,
  430. };
  431. /*
  432. * proc directories can do almost nothing..
  433. */
  434. static struct inode_operations proc_dir_inode_operations = {
  435. .lookup = proc_lookup,
  436. .getattr = proc_getattr,
  437. .setattr = proc_notify_change,
  438. };
  439. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  440. {
  441. unsigned int i;
  442. i = get_inode_number();
  443. if (i == 0)
  444. return -EAGAIN;
  445. dp->low_ino = i;
  446. dp->next = dir->subdir;
  447. dp->parent = dir;
  448. dir->subdir = dp;
  449. if (S_ISDIR(dp->mode)) {
  450. if (dp->proc_iops == NULL) {
  451. dp->proc_fops = &proc_dir_operations;
  452. dp->proc_iops = &proc_dir_inode_operations;
  453. }
  454. dir->nlink++;
  455. } else if (S_ISLNK(dp->mode)) {
  456. if (dp->proc_iops == NULL)
  457. dp->proc_iops = &proc_link_inode_operations;
  458. } else if (S_ISREG(dp->mode)) {
  459. if (dp->proc_fops == NULL)
  460. dp->proc_fops = &proc_file_operations;
  461. if (dp->proc_iops == NULL)
  462. dp->proc_iops = &proc_file_inode_operations;
  463. }
  464. return 0;
  465. }
  466. /*
  467. * Kill an inode that got unregistered..
  468. */
  469. static void proc_kill_inodes(struct proc_dir_entry *de)
  470. {
  471. struct list_head *p;
  472. struct super_block *sb = proc_mnt->mnt_sb;
  473. /*
  474. * Actually it's a partial revoke().
  475. */
  476. file_list_lock();
  477. list_for_each(p, &sb->s_files) {
  478. struct file * filp = list_entry(p, struct file, f_u.fu_list);
  479. struct dentry * dentry = filp->f_dentry;
  480. struct inode * inode;
  481. struct file_operations *fops;
  482. if (dentry->d_op != &proc_dentry_operations)
  483. continue;
  484. inode = dentry->d_inode;
  485. if (PDE(inode) != de)
  486. continue;
  487. fops = filp->f_op;
  488. filp->f_op = NULL;
  489. fops_put(fops);
  490. }
  491. file_list_unlock();
  492. }
  493. static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
  494. const char *name,
  495. mode_t mode,
  496. nlink_t nlink)
  497. {
  498. struct proc_dir_entry *ent = NULL;
  499. const char *fn = name;
  500. int len;
  501. /* make sure name is valid */
  502. if (!name || !strlen(name)) goto out;
  503. if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
  504. goto out;
  505. /* At this point there must not be any '/' characters beyond *fn */
  506. if (strchr(fn, '/'))
  507. goto out;
  508. len = strlen(fn);
  509. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  510. if (!ent) goto out;
  511. memset(ent, 0, sizeof(struct proc_dir_entry));
  512. memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
  513. ent->name = ((char *) ent) + sizeof(*ent);
  514. ent->namelen = len;
  515. ent->mode = mode;
  516. ent->nlink = nlink;
  517. out:
  518. return ent;
  519. }
  520. struct proc_dir_entry *proc_symlink(const char *name,
  521. struct proc_dir_entry *parent, const char *dest)
  522. {
  523. struct proc_dir_entry *ent;
  524. ent = proc_create(&parent,name,
  525. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  526. if (ent) {
  527. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  528. if (ent->data) {
  529. strcpy((char*)ent->data,dest);
  530. if (proc_register(parent, ent) < 0) {
  531. kfree(ent->data);
  532. kfree(ent);
  533. ent = NULL;
  534. }
  535. } else {
  536. kfree(ent);
  537. ent = NULL;
  538. }
  539. }
  540. return ent;
  541. }
  542. struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
  543. struct proc_dir_entry *parent)
  544. {
  545. struct proc_dir_entry *ent;
  546. ent = proc_create(&parent, name, S_IFDIR | mode, 2);
  547. if (ent) {
  548. ent->proc_fops = &proc_dir_operations;
  549. ent->proc_iops = &proc_dir_inode_operations;
  550. if (proc_register(parent, ent) < 0) {
  551. kfree(ent);
  552. ent = NULL;
  553. }
  554. }
  555. return ent;
  556. }
  557. struct proc_dir_entry *proc_mkdir(const char *name,
  558. struct proc_dir_entry *parent)
  559. {
  560. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  561. }
  562. struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
  563. struct proc_dir_entry *parent)
  564. {
  565. struct proc_dir_entry *ent;
  566. nlink_t nlink;
  567. if (S_ISDIR(mode)) {
  568. if ((mode & S_IALLUGO) == 0)
  569. mode |= S_IRUGO | S_IXUGO;
  570. nlink = 2;
  571. } else {
  572. if ((mode & S_IFMT) == 0)
  573. mode |= S_IFREG;
  574. if ((mode & S_IALLUGO) == 0)
  575. mode |= S_IRUGO;
  576. nlink = 1;
  577. }
  578. ent = proc_create(&parent,name,mode,nlink);
  579. if (ent) {
  580. if (S_ISDIR(mode)) {
  581. ent->proc_fops = &proc_dir_operations;
  582. ent->proc_iops = &proc_dir_inode_operations;
  583. }
  584. if (proc_register(parent, ent) < 0) {
  585. kfree(ent);
  586. ent = NULL;
  587. }
  588. }
  589. return ent;
  590. }
  591. void free_proc_entry(struct proc_dir_entry *de)
  592. {
  593. unsigned int ino = de->low_ino;
  594. if (ino < PROC_DYNAMIC_FIRST)
  595. return;
  596. release_inode_number(ino);
  597. if (S_ISLNK(de->mode) && de->data)
  598. kfree(de->data);
  599. kfree(de);
  600. }
  601. /*
  602. * Remove a /proc entry and free it if it's not currently in use.
  603. * If it is in use, we set the 'deleted' flag.
  604. */
  605. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  606. {
  607. struct proc_dir_entry **p;
  608. struct proc_dir_entry *de;
  609. const char *fn = name;
  610. int len;
  611. if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
  612. goto out;
  613. len = strlen(fn);
  614. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  615. if (!proc_match(len, fn, *p))
  616. continue;
  617. de = *p;
  618. *p = de->next;
  619. de->next = NULL;
  620. if (S_ISDIR(de->mode))
  621. parent->nlink--;
  622. proc_kill_inodes(de);
  623. de->nlink = 0;
  624. WARN_ON(de->subdir);
  625. if (!atomic_read(&de->count))
  626. free_proc_entry(de);
  627. else {
  628. de->deleted = 1;
  629. printk("remove_proc_entry: %s/%s busy, count=%d\n",
  630. parent->name, de->name, atomic_read(&de->count));
  631. }
  632. break;
  633. }
  634. out:
  635. return;
  636. }