generic.c 19 KB

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