generic.c 19 KB

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