generic.c 19 KB

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