generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
  227. struct kstat *stat)
  228. {
  229. struct inode *inode = dentry->d_inode;
  230. struct proc_dir_entry *de = PROC_I(inode)->pde;
  231. if (de && de->nlink)
  232. inode->i_nlink = de->nlink;
  233. generic_fillattr(inode, stat);
  234. return 0;
  235. }
  236. static struct inode_operations proc_file_inode_operations = {
  237. .setattr = proc_notify_change,
  238. };
  239. /*
  240. * This function parses a name such as "tty/driver/serial", and
  241. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  242. * returns "serial" in residual.
  243. */
  244. static int xlate_proc_name(const char *name,
  245. struct proc_dir_entry **ret, const char **residual)
  246. {
  247. const char *cp = name, *next;
  248. struct proc_dir_entry *de;
  249. int len;
  250. de = &proc_root;
  251. while (1) {
  252. next = strchr(cp, '/');
  253. if (!next)
  254. break;
  255. len = next - cp;
  256. for (de = de->subdir; de ; de = de->next) {
  257. if (proc_match(len, cp, de))
  258. break;
  259. }
  260. if (!de)
  261. return -ENOENT;
  262. cp += len + 1;
  263. }
  264. *residual = cp;
  265. *ret = de;
  266. return 0;
  267. }
  268. static DEFINE_IDR(proc_inum_idr);
  269. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  270. #define PROC_DYNAMIC_FIRST 0xF0000000UL
  271. /*
  272. * Return an inode number between PROC_DYNAMIC_FIRST and
  273. * 0xffffffff, or zero on failure.
  274. */
  275. static unsigned int get_inode_number(void)
  276. {
  277. int i, inum = 0;
  278. int error;
  279. retry:
  280. if (idr_pre_get(&proc_inum_idr, GFP_KERNEL) == 0)
  281. return 0;
  282. spin_lock(&proc_inum_lock);
  283. error = idr_get_new(&proc_inum_idr, NULL, &i);
  284. spin_unlock(&proc_inum_lock);
  285. if (error == -EAGAIN)
  286. goto retry;
  287. else if (error)
  288. return 0;
  289. inum = (i & MAX_ID_MASK) + PROC_DYNAMIC_FIRST;
  290. /* inum will never be more than 0xf0ffffff, so no check
  291. * for overflow.
  292. */
  293. return inum;
  294. }
  295. static void release_inode_number(unsigned int inum)
  296. {
  297. int id = (inum - PROC_DYNAMIC_FIRST) | ~MAX_ID_MASK;
  298. spin_lock(&proc_inum_lock);
  299. idr_remove(&proc_inum_idr, id);
  300. spin_unlock(&proc_inum_lock);
  301. }
  302. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  303. {
  304. nd_set_link(nd, PDE(dentry->d_inode)->data);
  305. return NULL;
  306. }
  307. static struct inode_operations proc_link_inode_operations = {
  308. .readlink = generic_readlink,
  309. .follow_link = proc_follow_link,
  310. };
  311. /*
  312. * As some entries in /proc are volatile, we want to
  313. * get rid of unused dentries. This could be made
  314. * smarter: we could keep a "volatile" flag in the
  315. * inode to indicate which ones to keep.
  316. */
  317. static int proc_delete_dentry(struct dentry * dentry)
  318. {
  319. return 1;
  320. }
  321. static struct dentry_operations proc_dentry_operations =
  322. {
  323. .d_delete = proc_delete_dentry,
  324. };
  325. /*
  326. * Don't create negative dentries here, return -ENOENT by hand
  327. * instead.
  328. */
  329. struct dentry *proc_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
  330. {
  331. struct inode *inode = NULL;
  332. struct proc_dir_entry * de;
  333. int error = -ENOENT;
  334. lock_kernel();
  335. de = PDE(dir);
  336. if (de) {
  337. for (de = de->subdir; de ; de = de->next) {
  338. if (de->namelen != dentry->d_name.len)
  339. continue;
  340. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  341. unsigned int ino = de->low_ino;
  342. error = -EINVAL;
  343. inode = proc_get_inode(dir->i_sb, ino, de);
  344. break;
  345. }
  346. }
  347. }
  348. unlock_kernel();
  349. if (inode) {
  350. dentry->d_op = &proc_dentry_operations;
  351. d_add(dentry, inode);
  352. return NULL;
  353. }
  354. return ERR_PTR(error);
  355. }
  356. /*
  357. * This returns non-zero if at EOF, so that the /proc
  358. * root directory can use this and check if it should
  359. * continue with the <pid> entries..
  360. *
  361. * Note that the VFS-layer doesn't care about the return
  362. * value of the readdir() call, as long as it's non-negative
  363. * for success..
  364. */
  365. int proc_readdir(struct file * filp,
  366. void * dirent, filldir_t filldir)
  367. {
  368. struct proc_dir_entry * de;
  369. unsigned int ino;
  370. int i;
  371. struct inode *inode = filp->f_dentry->d_inode;
  372. int ret = 0;
  373. lock_kernel();
  374. ino = inode->i_ino;
  375. de = PDE(inode);
  376. if (!de) {
  377. ret = -EINVAL;
  378. goto out;
  379. }
  380. i = filp->f_pos;
  381. switch (i) {
  382. case 0:
  383. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  384. goto out;
  385. i++;
  386. filp->f_pos++;
  387. /* fall through */
  388. case 1:
  389. if (filldir(dirent, "..", 2, i,
  390. parent_ino(filp->f_dentry),
  391. DT_DIR) < 0)
  392. goto out;
  393. i++;
  394. filp->f_pos++;
  395. /* fall through */
  396. default:
  397. de = de->subdir;
  398. i -= 2;
  399. for (;;) {
  400. if (!de) {
  401. ret = 1;
  402. goto out;
  403. }
  404. if (!i)
  405. break;
  406. de = de->next;
  407. i--;
  408. }
  409. do {
  410. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  411. de->low_ino, de->mode >> 12) < 0)
  412. goto out;
  413. filp->f_pos++;
  414. de = de->next;
  415. } while (de);
  416. }
  417. ret = 1;
  418. out: unlock_kernel();
  419. return ret;
  420. }
  421. /*
  422. * These are the generic /proc directory operations. They
  423. * use the in-memory "struct proc_dir_entry" tree to parse
  424. * the /proc directory.
  425. */
  426. static struct file_operations proc_dir_operations = {
  427. .read = generic_read_dir,
  428. .readdir = proc_readdir,
  429. };
  430. /*
  431. * proc directories can do almost nothing..
  432. */
  433. static struct inode_operations proc_dir_inode_operations = {
  434. .lookup = proc_lookup,
  435. .getattr = proc_getattr,
  436. .setattr = proc_notify_change,
  437. };
  438. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  439. {
  440. unsigned int i;
  441. i = get_inode_number();
  442. if (i == 0)
  443. return -EAGAIN;
  444. dp->low_ino = i;
  445. dp->next = dir->subdir;
  446. dp->parent = dir;
  447. dir->subdir = dp;
  448. if (S_ISDIR(dp->mode)) {
  449. if (dp->proc_iops == NULL) {
  450. dp->proc_fops = &proc_dir_operations;
  451. dp->proc_iops = &proc_dir_inode_operations;
  452. }
  453. dir->nlink++;
  454. } else if (S_ISLNK(dp->mode)) {
  455. if (dp->proc_iops == NULL)
  456. dp->proc_iops = &proc_link_inode_operations;
  457. } else if (S_ISREG(dp->mode)) {
  458. if (dp->proc_fops == NULL)
  459. dp->proc_fops = &proc_file_operations;
  460. if (dp->proc_iops == NULL)
  461. dp->proc_iops = &proc_file_inode_operations;
  462. }
  463. return 0;
  464. }
  465. /*
  466. * Kill an inode that got unregistered..
  467. */
  468. static void proc_kill_inodes(struct proc_dir_entry *de)
  469. {
  470. struct list_head *p;
  471. struct super_block *sb = proc_mnt->mnt_sb;
  472. /*
  473. * Actually it's a partial revoke().
  474. */
  475. file_list_lock();
  476. list_for_each(p, &sb->s_files) {
  477. struct file * filp = list_entry(p, struct file, f_list);
  478. struct dentry * dentry = filp->f_dentry;
  479. struct inode * inode;
  480. struct file_operations *fops;
  481. if (dentry->d_op != &proc_dentry_operations)
  482. continue;
  483. inode = dentry->d_inode;
  484. if (PDE(inode) != de)
  485. continue;
  486. fops = filp->f_op;
  487. filp->f_op = NULL;
  488. fops_put(fops);
  489. }
  490. file_list_unlock();
  491. }
  492. static struct proc_dir_entry *proc_create(struct proc_dir_entry **parent,
  493. const char *name,
  494. mode_t mode,
  495. nlink_t nlink)
  496. {
  497. struct proc_dir_entry *ent = NULL;
  498. const char *fn = name;
  499. int len;
  500. /* make sure name is valid */
  501. if (!name || !strlen(name)) goto out;
  502. if (!(*parent) && xlate_proc_name(name, parent, &fn) != 0)
  503. goto out;
  504. /* At this point there must not be any '/' characters beyond *fn */
  505. if (strchr(fn, '/'))
  506. goto out;
  507. len = strlen(fn);
  508. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  509. if (!ent) goto out;
  510. memset(ent, 0, sizeof(struct proc_dir_entry));
  511. memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
  512. ent->name = ((char *) ent) + sizeof(*ent);
  513. ent->namelen = len;
  514. ent->mode = mode;
  515. ent->nlink = nlink;
  516. out:
  517. return ent;
  518. }
  519. struct proc_dir_entry *proc_symlink(const char *name,
  520. struct proc_dir_entry *parent, const char *dest)
  521. {
  522. struct proc_dir_entry *ent;
  523. ent = proc_create(&parent,name,
  524. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  525. if (ent) {
  526. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  527. if (ent->data) {
  528. strcpy((char*)ent->data,dest);
  529. if (proc_register(parent, ent) < 0) {
  530. kfree(ent->data);
  531. kfree(ent);
  532. ent = NULL;
  533. }
  534. } else {
  535. kfree(ent);
  536. ent = NULL;
  537. }
  538. }
  539. return ent;
  540. }
  541. struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
  542. struct proc_dir_entry *parent)
  543. {
  544. struct proc_dir_entry *ent;
  545. ent = proc_create(&parent, name, S_IFDIR | mode, 2);
  546. if (ent) {
  547. ent->proc_fops = &proc_dir_operations;
  548. ent->proc_iops = &proc_dir_inode_operations;
  549. if (proc_register(parent, ent) < 0) {
  550. kfree(ent);
  551. ent = NULL;
  552. }
  553. }
  554. return ent;
  555. }
  556. struct proc_dir_entry *proc_mkdir(const char *name,
  557. struct proc_dir_entry *parent)
  558. {
  559. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  560. }
  561. struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
  562. struct proc_dir_entry *parent)
  563. {
  564. struct proc_dir_entry *ent;
  565. nlink_t nlink;
  566. if (S_ISDIR(mode)) {
  567. if ((mode & S_IALLUGO) == 0)
  568. mode |= S_IRUGO | S_IXUGO;
  569. nlink = 2;
  570. } else {
  571. if ((mode & S_IFMT) == 0)
  572. mode |= S_IFREG;
  573. if ((mode & S_IALLUGO) == 0)
  574. mode |= S_IRUGO;
  575. nlink = 1;
  576. }
  577. ent = proc_create(&parent,name,mode,nlink);
  578. if (ent) {
  579. if (S_ISDIR(mode)) {
  580. ent->proc_fops = &proc_dir_operations;
  581. ent->proc_iops = &proc_dir_inode_operations;
  582. }
  583. if (proc_register(parent, ent) < 0) {
  584. kfree(ent);
  585. ent = NULL;
  586. }
  587. }
  588. return ent;
  589. }
  590. void free_proc_entry(struct proc_dir_entry *de)
  591. {
  592. unsigned int ino = de->low_ino;
  593. if (ino < PROC_DYNAMIC_FIRST)
  594. return;
  595. release_inode_number(ino);
  596. if (S_ISLNK(de->mode) && de->data)
  597. kfree(de->data);
  598. kfree(de);
  599. }
  600. /*
  601. * Remove a /proc entry and free it if it's not currently in use.
  602. * If it is in use, we set the 'deleted' flag.
  603. */
  604. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  605. {
  606. struct proc_dir_entry **p;
  607. struct proc_dir_entry *de;
  608. const char *fn = name;
  609. int len;
  610. if (!parent && xlate_proc_name(name, &parent, &fn) != 0)
  611. goto out;
  612. len = strlen(fn);
  613. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  614. if (!proc_match(len, fn, *p))
  615. continue;
  616. de = *p;
  617. *p = de->next;
  618. de->next = NULL;
  619. if (S_ISDIR(de->mode))
  620. parent->nlink--;
  621. proc_kill_inodes(de);
  622. de->nlink = 0;
  623. WARN_ON(de->subdir);
  624. if (!atomic_read(&de->count))
  625. free_proc_entry(de);
  626. else {
  627. de->deleted = 1;
  628. printk("remove_proc_entry: %s/%s busy, count=%d\n",
  629. parent->name, de->name, atomic_read(&de->count));
  630. }
  631. break;
  632. }
  633. out:
  634. return;
  635. }