generic.c 16 KB

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