generic.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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/init.h>
  17. #include <linux/idr.h>
  18. #include <linux/namei.h>
  19. #include <linux/bitops.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/completion.h>
  22. #include <asm/uaccess.h>
  23. #include "internal.h"
  24. DEFINE_SPINLOCK(proc_subdir_lock);
  25. static int proc_match(int len, const char *name, struct proc_dir_entry *de)
  26. {
  27. if (de->namelen != len)
  28. return 0;
  29. return !memcmp(name, de->name, len);
  30. }
  31. /* buffer size is one page but our output routines use some slack for overruns */
  32. #define PROC_BLOCK_SIZE (PAGE_SIZE - 1024)
  33. static ssize_t
  34. __proc_file_read(struct file *file, char __user *buf, size_t nbytes,
  35. loff_t *ppos)
  36. {
  37. struct inode * inode = file->f_path.dentry->d_inode;
  38. char *page;
  39. ssize_t retval=0;
  40. int eof=0;
  41. ssize_t n, count;
  42. char *start;
  43. struct proc_dir_entry * dp;
  44. unsigned long long pos;
  45. /*
  46. * Gaah, please just use "seq_file" instead. The legacy /proc
  47. * interfaces cut loff_t down to off_t for reads, and ignore
  48. * the offset entirely for writes..
  49. */
  50. pos = *ppos;
  51. if (pos > MAX_NON_LFS)
  52. return 0;
  53. if (nbytes > MAX_NON_LFS - pos)
  54. nbytes = MAX_NON_LFS - pos;
  55. dp = PDE(inode);
  56. if (!(page = (char*) __get_free_page(GFP_TEMPORARY)))
  57. return -ENOMEM;
  58. while ((nbytes > 0) && !eof) {
  59. count = min_t(size_t, PROC_BLOCK_SIZE, nbytes);
  60. start = NULL;
  61. if (dp->read_proc) {
  62. /*
  63. * How to be a proc read function
  64. * ------------------------------
  65. * Prototype:
  66. * int f(char *buffer, char **start, off_t offset,
  67. * int count, int *peof, void *dat)
  68. *
  69. * Assume that the buffer is "count" bytes in size.
  70. *
  71. * If you know you have supplied all the data you
  72. * have, set *peof.
  73. *
  74. * You have three ways to return data:
  75. * 0) Leave *start = NULL. (This is the default.)
  76. * Put the data of the requested offset at that
  77. * offset within the buffer. Return the number (n)
  78. * of bytes there are from the beginning of the
  79. * buffer up to the last byte of data. If the
  80. * number of supplied bytes (= n - offset) is
  81. * greater than zero and you didn't signal eof
  82. * and the reader is prepared to take more data
  83. * you will be called again with the requested
  84. * offset advanced by the number of bytes
  85. * absorbed. This interface is useful for files
  86. * no larger than the buffer.
  87. * 1) Set *start = an unsigned long value less than
  88. * the buffer address but greater than zero.
  89. * Put the data of the requested offset at the
  90. * beginning of the buffer. Return the number of
  91. * bytes of data placed there. If this number is
  92. * greater than zero and you didn't signal eof
  93. * and the reader is prepared to take more data
  94. * you will be called again with the requested
  95. * offset advanced by *start. This interface is
  96. * useful when you have a large file consisting
  97. * of a series of blocks which you want to count
  98. * and return as wholes.
  99. * (Hack by Paul.Russell@rustcorp.com.au)
  100. * 2) Set *start = an address within the buffer.
  101. * Put the data of the requested offset at *start.
  102. * Return the number of bytes of data placed there.
  103. * If this number is greater than zero and you
  104. * didn't signal eof and the reader is prepared to
  105. * take more data you will be called again with the
  106. * requested offset advanced by the number of bytes
  107. * absorbed.
  108. */
  109. n = dp->read_proc(page, &start, *ppos,
  110. count, &eof, dp->data);
  111. } else
  112. break;
  113. if (n == 0) /* end of file */
  114. break;
  115. if (n < 0) { /* error */
  116. if (retval == 0)
  117. retval = n;
  118. break;
  119. }
  120. if (start == NULL) {
  121. if (n > PAGE_SIZE) {
  122. printk(KERN_ERR
  123. "proc_file_read: Apparent buffer overflow!\n");
  124. n = PAGE_SIZE;
  125. }
  126. n -= *ppos;
  127. if (n <= 0)
  128. break;
  129. if (n > count)
  130. n = count;
  131. start = page + *ppos;
  132. } else if (start < page) {
  133. if (n > PAGE_SIZE) {
  134. printk(KERN_ERR
  135. "proc_file_read: Apparent buffer overflow!\n");
  136. n = PAGE_SIZE;
  137. }
  138. if (n > count) {
  139. /*
  140. * Don't reduce n because doing so might
  141. * cut off part of a data block.
  142. */
  143. printk(KERN_WARNING
  144. "proc_file_read: Read count exceeded\n");
  145. }
  146. } else /* start >= page */ {
  147. unsigned long startoff = (unsigned long)(start - page);
  148. if (n > (PAGE_SIZE - startoff)) {
  149. printk(KERN_ERR
  150. "proc_file_read: Apparent buffer overflow!\n");
  151. n = PAGE_SIZE - startoff;
  152. }
  153. if (n > count)
  154. n = count;
  155. }
  156. n -= copy_to_user(buf, start < page ? page : start, n);
  157. if (n == 0) {
  158. if (retval == 0)
  159. retval = -EFAULT;
  160. break;
  161. }
  162. *ppos += start < page ? (unsigned long)start : n;
  163. nbytes -= n;
  164. buf += n;
  165. retval += n;
  166. }
  167. free_page((unsigned long) page);
  168. return retval;
  169. }
  170. static ssize_t
  171. proc_file_read(struct file *file, char __user *buf, size_t nbytes,
  172. loff_t *ppos)
  173. {
  174. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  175. ssize_t rv = -EIO;
  176. spin_lock(&pde->pde_unload_lock);
  177. if (!pde->proc_fops) {
  178. spin_unlock(&pde->pde_unload_lock);
  179. return rv;
  180. }
  181. pde->pde_users++;
  182. spin_unlock(&pde->pde_unload_lock);
  183. rv = __proc_file_read(file, buf, nbytes, ppos);
  184. pde_users_dec(pde);
  185. return rv;
  186. }
  187. static ssize_t
  188. proc_file_write(struct file *file, const char __user *buffer,
  189. size_t count, loff_t *ppos)
  190. {
  191. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  192. ssize_t rv = -EIO;
  193. if (pde->write_proc) {
  194. spin_lock(&pde->pde_unload_lock);
  195. if (!pde->proc_fops) {
  196. spin_unlock(&pde->pde_unload_lock);
  197. return rv;
  198. }
  199. pde->pde_users++;
  200. spin_unlock(&pde->pde_unload_lock);
  201. /* FIXME: does this routine need ppos? probably... */
  202. rv = pde->write_proc(file, buffer, count, pde->data);
  203. pde_users_dec(pde);
  204. }
  205. return rv;
  206. }
  207. static loff_t
  208. proc_file_lseek(struct file *file, loff_t offset, int orig)
  209. {
  210. loff_t retval = -EINVAL;
  211. switch (orig) {
  212. case 1:
  213. offset += file->f_pos;
  214. /* fallthrough */
  215. case 0:
  216. if (offset < 0 || offset > MAX_NON_LFS)
  217. break;
  218. file->f_pos = retval = offset;
  219. }
  220. return retval;
  221. }
  222. static const struct file_operations proc_file_operations = {
  223. .llseek = proc_file_lseek,
  224. .read = proc_file_read,
  225. .write = proc_file_write,
  226. };
  227. static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
  228. {
  229. struct inode *inode = dentry->d_inode;
  230. struct proc_dir_entry *de = PDE(inode);
  231. int error;
  232. error = inode_change_ok(inode, iattr);
  233. if (error)
  234. goto out;
  235. error = inode_setattr(inode, iattr);
  236. if (error)
  237. goto out;
  238. de->uid = inode->i_uid;
  239. de->gid = inode->i_gid;
  240. de->mode = inode->i_mode;
  241. out:
  242. return error;
  243. }
  244. static int proc_getattr(struct vfsmount *mnt, struct dentry *dentry,
  245. struct kstat *stat)
  246. {
  247. struct inode *inode = dentry->d_inode;
  248. struct proc_dir_entry *de = PROC_I(inode)->pde;
  249. if (de && de->nlink)
  250. inode->i_nlink = de->nlink;
  251. generic_fillattr(inode, stat);
  252. return 0;
  253. }
  254. static const struct inode_operations proc_file_inode_operations = {
  255. .setattr = proc_notify_change,
  256. };
  257. /*
  258. * This function parses a name such as "tty/driver/serial", and
  259. * returns the struct proc_dir_entry for "/proc/tty/driver", and
  260. * returns "serial" in residual.
  261. */
  262. static int xlate_proc_name(const char *name,
  263. struct proc_dir_entry **ret, const char **residual)
  264. {
  265. const char *cp = name, *next;
  266. struct proc_dir_entry *de;
  267. int len;
  268. int rtn = 0;
  269. de = *ret;
  270. if (!de)
  271. de = &proc_root;
  272. spin_lock(&proc_subdir_lock);
  273. while (1) {
  274. next = strchr(cp, '/');
  275. if (!next)
  276. break;
  277. len = next - cp;
  278. for (de = de->subdir; de ; de = de->next) {
  279. if (proc_match(len, cp, de))
  280. break;
  281. }
  282. if (!de) {
  283. rtn = -ENOENT;
  284. goto out;
  285. }
  286. cp += len + 1;
  287. }
  288. *residual = cp;
  289. *ret = de;
  290. out:
  291. spin_unlock(&proc_subdir_lock);
  292. return rtn;
  293. }
  294. static DEFINE_IDA(proc_inum_ida);
  295. static DEFINE_SPINLOCK(proc_inum_lock); /* protects the above */
  296. #define PROC_DYNAMIC_FIRST 0xF0000000U
  297. /*
  298. * Return an inode number between PROC_DYNAMIC_FIRST and
  299. * 0xffffffff, or zero on failure.
  300. *
  301. * Current inode allocations in the proc-fs (hex-numbers):
  302. *
  303. * 00000000 reserved
  304. * 00000001-00000fff static entries (goners)
  305. * 001 root-ino
  306. *
  307. * 00001000-00001fff unused
  308. * 0001xxxx-7fffxxxx pid-dir entries for pid 1-7fff
  309. * 80000000-efffffff unused
  310. * f0000000-ffffffff dynamic entries
  311. *
  312. * Goal:
  313. * Once we split the thing into several virtual filesystems,
  314. * we will get rid of magical ranges (and this comment, BTW).
  315. */
  316. static unsigned int get_inode_number(void)
  317. {
  318. unsigned int i;
  319. int error;
  320. retry:
  321. if (ida_pre_get(&proc_inum_ida, GFP_KERNEL) == 0)
  322. return 0;
  323. spin_lock(&proc_inum_lock);
  324. error = ida_get_new(&proc_inum_ida, &i);
  325. spin_unlock(&proc_inum_lock);
  326. if (error == -EAGAIN)
  327. goto retry;
  328. else if (error)
  329. return 0;
  330. if (i > UINT_MAX - PROC_DYNAMIC_FIRST) {
  331. spin_lock(&proc_inum_lock);
  332. ida_remove(&proc_inum_ida, i);
  333. spin_unlock(&proc_inum_lock);
  334. return 0;
  335. }
  336. return PROC_DYNAMIC_FIRST + i;
  337. }
  338. static void release_inode_number(unsigned int inum)
  339. {
  340. spin_lock(&proc_inum_lock);
  341. ida_remove(&proc_inum_ida, inum - PROC_DYNAMIC_FIRST);
  342. spin_unlock(&proc_inum_lock);
  343. }
  344. static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
  345. {
  346. nd_set_link(nd, PDE(dentry->d_inode)->data);
  347. return NULL;
  348. }
  349. static const struct inode_operations proc_link_inode_operations = {
  350. .readlink = generic_readlink,
  351. .follow_link = proc_follow_link,
  352. };
  353. /*
  354. * As some entries in /proc are volatile, we want to
  355. * get rid of unused dentries. This could be made
  356. * smarter: we could keep a "volatile" flag in the
  357. * inode to indicate which ones to keep.
  358. */
  359. static int proc_delete_dentry(struct dentry * dentry)
  360. {
  361. return 1;
  362. }
  363. static const struct dentry_operations proc_dentry_operations =
  364. {
  365. .d_delete = proc_delete_dentry,
  366. };
  367. /*
  368. * Don't create negative dentries here, return -ENOENT by hand
  369. * instead.
  370. */
  371. struct dentry *proc_lookup_de(struct proc_dir_entry *de, struct inode *dir,
  372. struct dentry *dentry)
  373. {
  374. struct inode *inode = NULL;
  375. int error = -ENOENT;
  376. spin_lock(&proc_subdir_lock);
  377. for (de = de->subdir; de ; de = de->next) {
  378. if (de->namelen != dentry->d_name.len)
  379. continue;
  380. if (!memcmp(dentry->d_name.name, de->name, de->namelen)) {
  381. unsigned int ino;
  382. ino = de->low_ino;
  383. de_get(de);
  384. spin_unlock(&proc_subdir_lock);
  385. error = -EINVAL;
  386. inode = proc_get_inode(dir->i_sb, ino, de);
  387. goto out_unlock;
  388. }
  389. }
  390. spin_unlock(&proc_subdir_lock);
  391. out_unlock:
  392. if (inode) {
  393. dentry->d_op = &proc_dentry_operations;
  394. d_add(dentry, inode);
  395. return NULL;
  396. }
  397. if (de)
  398. de_put(de);
  399. return ERR_PTR(error);
  400. }
  401. struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
  402. struct nameidata *nd)
  403. {
  404. return proc_lookup_de(PDE(dir), dir, dentry);
  405. }
  406. /*
  407. * This returns non-zero if at EOF, so that the /proc
  408. * root directory can use this and check if it should
  409. * continue with the <pid> entries..
  410. *
  411. * Note that the VFS-layer doesn't care about the return
  412. * value of the readdir() call, as long as it's non-negative
  413. * for success..
  414. */
  415. int proc_readdir_de(struct proc_dir_entry *de, struct file *filp, void *dirent,
  416. filldir_t filldir)
  417. {
  418. unsigned int ino;
  419. int i;
  420. struct inode *inode = filp->f_path.dentry->d_inode;
  421. int ret = 0;
  422. ino = inode->i_ino;
  423. i = filp->f_pos;
  424. switch (i) {
  425. case 0:
  426. if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
  427. goto out;
  428. i++;
  429. filp->f_pos++;
  430. /* fall through */
  431. case 1:
  432. if (filldir(dirent, "..", 2, i,
  433. parent_ino(filp->f_path.dentry),
  434. DT_DIR) < 0)
  435. goto out;
  436. i++;
  437. filp->f_pos++;
  438. /* fall through */
  439. default:
  440. spin_lock(&proc_subdir_lock);
  441. de = de->subdir;
  442. i -= 2;
  443. for (;;) {
  444. if (!de) {
  445. ret = 1;
  446. spin_unlock(&proc_subdir_lock);
  447. goto out;
  448. }
  449. if (!i)
  450. break;
  451. de = de->next;
  452. i--;
  453. }
  454. do {
  455. struct proc_dir_entry *next;
  456. /* filldir passes info to user space */
  457. de_get(de);
  458. spin_unlock(&proc_subdir_lock);
  459. if (filldir(dirent, de->name, de->namelen, filp->f_pos,
  460. de->low_ino, de->mode >> 12) < 0) {
  461. de_put(de);
  462. goto out;
  463. }
  464. spin_lock(&proc_subdir_lock);
  465. filp->f_pos++;
  466. next = de->next;
  467. de_put(de);
  468. de = next;
  469. } while (de);
  470. spin_unlock(&proc_subdir_lock);
  471. }
  472. ret = 1;
  473. out:
  474. return ret;
  475. }
  476. int proc_readdir(struct file *filp, void *dirent, filldir_t filldir)
  477. {
  478. struct inode *inode = filp->f_path.dentry->d_inode;
  479. return proc_readdir_de(PDE(inode), filp, dirent, filldir);
  480. }
  481. /*
  482. * These are the generic /proc directory operations. They
  483. * use the in-memory "struct proc_dir_entry" tree to parse
  484. * the /proc directory.
  485. */
  486. static const struct file_operations proc_dir_operations = {
  487. .llseek = generic_file_llseek,
  488. .read = generic_read_dir,
  489. .readdir = proc_readdir,
  490. };
  491. /*
  492. * proc directories can do almost nothing..
  493. */
  494. static const struct inode_operations proc_dir_inode_operations = {
  495. .lookup = proc_lookup,
  496. .getattr = proc_getattr,
  497. .setattr = proc_notify_change,
  498. };
  499. static int proc_register(struct proc_dir_entry * dir, struct proc_dir_entry * dp)
  500. {
  501. unsigned int i;
  502. struct proc_dir_entry *tmp;
  503. i = get_inode_number();
  504. if (i == 0)
  505. return -EAGAIN;
  506. dp->low_ino = i;
  507. if (S_ISDIR(dp->mode)) {
  508. if (dp->proc_iops == NULL) {
  509. dp->proc_fops = &proc_dir_operations;
  510. dp->proc_iops = &proc_dir_inode_operations;
  511. }
  512. dir->nlink++;
  513. } else if (S_ISLNK(dp->mode)) {
  514. if (dp->proc_iops == NULL)
  515. dp->proc_iops = &proc_link_inode_operations;
  516. } else if (S_ISREG(dp->mode)) {
  517. if (dp->proc_fops == NULL)
  518. dp->proc_fops = &proc_file_operations;
  519. if (dp->proc_iops == NULL)
  520. dp->proc_iops = &proc_file_inode_operations;
  521. }
  522. spin_lock(&proc_subdir_lock);
  523. for (tmp = dir->subdir; tmp; tmp = tmp->next)
  524. if (strcmp(tmp->name, dp->name) == 0) {
  525. WARN(1, KERN_WARNING "proc_dir_entry '%s/%s' already registered\n",
  526. dir->name, dp->name);
  527. break;
  528. }
  529. dp->next = dir->subdir;
  530. dp->parent = dir;
  531. dir->subdir = dp;
  532. spin_unlock(&proc_subdir_lock);
  533. return 0;
  534. }
  535. static struct proc_dir_entry *__proc_create(struct proc_dir_entry **parent,
  536. const char *name,
  537. mode_t mode,
  538. nlink_t nlink)
  539. {
  540. struct proc_dir_entry *ent = NULL;
  541. const char *fn = name;
  542. int len;
  543. /* make sure name is valid */
  544. if (!name || !strlen(name)) goto out;
  545. if (xlate_proc_name(name, parent, &fn) != 0)
  546. goto out;
  547. /* At this point there must not be any '/' characters beyond *fn */
  548. if (strchr(fn, '/'))
  549. goto out;
  550. len = strlen(fn);
  551. ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);
  552. if (!ent) goto out;
  553. memset(ent, 0, sizeof(struct proc_dir_entry));
  554. memcpy(((char *) ent) + sizeof(struct proc_dir_entry), fn, len + 1);
  555. ent->name = ((char *) ent) + sizeof(*ent);
  556. ent->namelen = len;
  557. ent->mode = mode;
  558. ent->nlink = nlink;
  559. atomic_set(&ent->count, 1);
  560. ent->pde_users = 0;
  561. spin_lock_init(&ent->pde_unload_lock);
  562. ent->pde_unload_completion = NULL;
  563. INIT_LIST_HEAD(&ent->pde_openers);
  564. out:
  565. return ent;
  566. }
  567. struct proc_dir_entry *proc_symlink(const char *name,
  568. struct proc_dir_entry *parent, const char *dest)
  569. {
  570. struct proc_dir_entry *ent;
  571. ent = __proc_create(&parent, name,
  572. (S_IFLNK | S_IRUGO | S_IWUGO | S_IXUGO),1);
  573. if (ent) {
  574. ent->data = kmalloc((ent->size=strlen(dest))+1, GFP_KERNEL);
  575. if (ent->data) {
  576. strcpy((char*)ent->data,dest);
  577. if (proc_register(parent, ent) < 0) {
  578. kfree(ent->data);
  579. kfree(ent);
  580. ent = NULL;
  581. }
  582. } else {
  583. kfree(ent);
  584. ent = NULL;
  585. }
  586. }
  587. return ent;
  588. }
  589. struct proc_dir_entry *proc_mkdir_mode(const char *name, mode_t mode,
  590. struct proc_dir_entry *parent)
  591. {
  592. struct proc_dir_entry *ent;
  593. ent = __proc_create(&parent, name, S_IFDIR | mode, 2);
  594. if (ent) {
  595. if (proc_register(parent, ent) < 0) {
  596. kfree(ent);
  597. ent = NULL;
  598. }
  599. }
  600. return ent;
  601. }
  602. struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
  603. struct proc_dir_entry *parent)
  604. {
  605. struct proc_dir_entry *ent;
  606. ent = __proc_create(&parent, name, S_IFDIR | S_IRUGO | S_IXUGO, 2);
  607. if (ent) {
  608. ent->data = net;
  609. if (proc_register(parent, ent) < 0) {
  610. kfree(ent);
  611. ent = NULL;
  612. }
  613. }
  614. return ent;
  615. }
  616. EXPORT_SYMBOL_GPL(proc_net_mkdir);
  617. struct proc_dir_entry *proc_mkdir(const char *name,
  618. struct proc_dir_entry *parent)
  619. {
  620. return proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent);
  621. }
  622. struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
  623. struct proc_dir_entry *parent)
  624. {
  625. struct proc_dir_entry *ent;
  626. nlink_t nlink;
  627. if (S_ISDIR(mode)) {
  628. if ((mode & S_IALLUGO) == 0)
  629. mode |= S_IRUGO | S_IXUGO;
  630. nlink = 2;
  631. } else {
  632. if ((mode & S_IFMT) == 0)
  633. mode |= S_IFREG;
  634. if ((mode & S_IALLUGO) == 0)
  635. mode |= S_IRUGO;
  636. nlink = 1;
  637. }
  638. ent = __proc_create(&parent, name, mode, nlink);
  639. if (ent) {
  640. if (proc_register(parent, ent) < 0) {
  641. kfree(ent);
  642. ent = NULL;
  643. }
  644. }
  645. return ent;
  646. }
  647. struct proc_dir_entry *proc_create_data(const char *name, mode_t mode,
  648. struct proc_dir_entry *parent,
  649. const struct file_operations *proc_fops,
  650. void *data)
  651. {
  652. struct proc_dir_entry *pde;
  653. nlink_t nlink;
  654. if (S_ISDIR(mode)) {
  655. if ((mode & S_IALLUGO) == 0)
  656. mode |= S_IRUGO | S_IXUGO;
  657. nlink = 2;
  658. } else {
  659. if ((mode & S_IFMT) == 0)
  660. mode |= S_IFREG;
  661. if ((mode & S_IALLUGO) == 0)
  662. mode |= S_IRUGO;
  663. nlink = 1;
  664. }
  665. pde = __proc_create(&parent, name, mode, nlink);
  666. if (!pde)
  667. goto out;
  668. pde->proc_fops = proc_fops;
  669. pde->data = data;
  670. if (proc_register(parent, pde) < 0)
  671. goto out_free;
  672. return pde;
  673. out_free:
  674. kfree(pde);
  675. out:
  676. return NULL;
  677. }
  678. void free_proc_entry(struct proc_dir_entry *de)
  679. {
  680. unsigned int ino = de->low_ino;
  681. if (ino < PROC_DYNAMIC_FIRST)
  682. return;
  683. release_inode_number(ino);
  684. if (S_ISLNK(de->mode))
  685. kfree(de->data);
  686. kfree(de);
  687. }
  688. /*
  689. * Remove a /proc entry and free it if it's not currently in use.
  690. */
  691. void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
  692. {
  693. struct proc_dir_entry **p;
  694. struct proc_dir_entry *de = NULL;
  695. const char *fn = name;
  696. int len;
  697. if (xlate_proc_name(name, &parent, &fn) != 0)
  698. return;
  699. len = strlen(fn);
  700. spin_lock(&proc_subdir_lock);
  701. for (p = &parent->subdir; *p; p=&(*p)->next ) {
  702. if (proc_match(len, fn, *p)) {
  703. de = *p;
  704. *p = de->next;
  705. de->next = NULL;
  706. break;
  707. }
  708. }
  709. spin_unlock(&proc_subdir_lock);
  710. if (!de)
  711. return;
  712. spin_lock(&de->pde_unload_lock);
  713. /*
  714. * Stop accepting new callers into module. If you're
  715. * dynamically allocating ->proc_fops, save a pointer somewhere.
  716. */
  717. de->proc_fops = NULL;
  718. /* Wait until all existing callers into module are done. */
  719. if (de->pde_users > 0) {
  720. DECLARE_COMPLETION_ONSTACK(c);
  721. if (!de->pde_unload_completion)
  722. de->pde_unload_completion = &c;
  723. spin_unlock(&de->pde_unload_lock);
  724. wait_for_completion(de->pde_unload_completion);
  725. goto continue_removing;
  726. }
  727. spin_unlock(&de->pde_unload_lock);
  728. continue_removing:
  729. spin_lock(&de->pde_unload_lock);
  730. while (!list_empty(&de->pde_openers)) {
  731. struct pde_opener *pdeo;
  732. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  733. list_del(&pdeo->lh);
  734. spin_unlock(&de->pde_unload_lock);
  735. pdeo->release(pdeo->inode, pdeo->file);
  736. kfree(pdeo);
  737. spin_lock(&de->pde_unload_lock);
  738. }
  739. spin_unlock(&de->pde_unload_lock);
  740. if (S_ISDIR(de->mode))
  741. parent->nlink--;
  742. de->nlink = 0;
  743. WARN(de->subdir, KERN_WARNING "%s: removing non-empty directory "
  744. "'%s/%s', leaking at least '%s'\n", __func__,
  745. de->parent->name, de->name, de->subdir->name);
  746. if (atomic_dec_and_test(&de->count))
  747. free_proc_entry(de);
  748. }