miscdev.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2008 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/hash.h>
  23. #include <linux/random.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/poll.h>
  26. #include <linux/wait.h>
  27. #include <linux/module.h>
  28. #include "ecryptfs_kernel.h"
  29. static atomic_t ecryptfs_num_miscdev_opens;
  30. /**
  31. * ecryptfs_miscdev_poll
  32. * @file: dev file (ignored)
  33. * @pt: dev poll table (ignored)
  34. *
  35. * Returns the poll mask
  36. */
  37. static unsigned int
  38. ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
  39. {
  40. struct ecryptfs_daemon *daemon;
  41. unsigned int mask = 0;
  42. int rc;
  43. mutex_lock(&ecryptfs_daemon_hash_mux);
  44. /* TODO: Just use file->private_data? */
  45. rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
  46. BUG_ON(rc || !daemon);
  47. mutex_lock(&daemon->mux);
  48. mutex_unlock(&ecryptfs_daemon_hash_mux);
  49. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  50. printk(KERN_WARNING "%s: Attempt to poll on zombified "
  51. "daemon\n", __func__);
  52. goto out_unlock_daemon;
  53. }
  54. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  55. goto out_unlock_daemon;
  56. if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
  57. goto out_unlock_daemon;
  58. daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
  59. mutex_unlock(&daemon->mux);
  60. poll_wait(file, &daemon->wait, pt);
  61. mutex_lock(&daemon->mux);
  62. if (!list_empty(&daemon->msg_ctx_out_queue))
  63. mask |= POLLIN | POLLRDNORM;
  64. out_unlock_daemon:
  65. daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
  66. mutex_unlock(&daemon->mux);
  67. return mask;
  68. }
  69. /**
  70. * ecryptfs_miscdev_open
  71. * @inode: inode of miscdev handle (ignored)
  72. * @file: file for miscdev handle (ignored)
  73. *
  74. * Returns zero on success; non-zero otherwise
  75. */
  76. static int
  77. ecryptfs_miscdev_open(struct inode *inode, struct file *file)
  78. {
  79. struct ecryptfs_daemon *daemon = NULL;
  80. int rc;
  81. mutex_lock(&ecryptfs_daemon_hash_mux);
  82. rc = try_module_get(THIS_MODULE);
  83. if (rc == 0) {
  84. rc = -EIO;
  85. printk(KERN_ERR "%s: Error attempting to increment module use "
  86. "count; rc = [%d]\n", __func__, rc);
  87. goto out_unlock_daemon_list;
  88. }
  89. rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
  90. if (rc || !daemon) {
  91. rc = ecryptfs_spawn_daemon(&daemon, current->euid,
  92. current->pid);
  93. if (rc) {
  94. printk(KERN_ERR "%s: Error attempting to spawn daemon; "
  95. "rc = [%d]\n", __func__, rc);
  96. goto out_module_put_unlock_daemon_list;
  97. }
  98. }
  99. mutex_lock(&daemon->mux);
  100. if (daemon->pid != current->pid) {
  101. rc = -EINVAL;
  102. printk(KERN_ERR "%s: pid [%d] has registered with euid [%d], "
  103. "but pid [%d] has attempted to open the handle "
  104. "instead\n", __func__, daemon->pid, daemon->euid,
  105. current->pid);
  106. goto out_unlock_daemon;
  107. }
  108. if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
  109. rc = -EBUSY;
  110. printk(KERN_ERR "%s: Miscellaneous device handle may only be "
  111. "opened once per daemon; pid [%d] already has this "
  112. "handle open\n", __func__, daemon->pid);
  113. goto out_unlock_daemon;
  114. }
  115. daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
  116. atomic_inc(&ecryptfs_num_miscdev_opens);
  117. out_unlock_daemon:
  118. mutex_unlock(&daemon->mux);
  119. out_module_put_unlock_daemon_list:
  120. if (rc)
  121. module_put(THIS_MODULE);
  122. out_unlock_daemon_list:
  123. mutex_unlock(&ecryptfs_daemon_hash_mux);
  124. return rc;
  125. }
  126. /**
  127. * ecryptfs_miscdev_release
  128. * @inode: inode of fs/ecryptfs/euid handle (ignored)
  129. * @file: file for fs/ecryptfs/euid handle (ignored)
  130. *
  131. * This keeps the daemon registered until the daemon sends another
  132. * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
  133. *
  134. * Returns zero on success; non-zero otherwise
  135. */
  136. static int
  137. ecryptfs_miscdev_release(struct inode *inode, struct file *file)
  138. {
  139. struct ecryptfs_daemon *daemon = NULL;
  140. int rc;
  141. mutex_lock(&ecryptfs_daemon_hash_mux);
  142. rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
  143. BUG_ON(rc || !daemon);
  144. mutex_lock(&daemon->mux);
  145. BUG_ON(daemon->pid != current->pid);
  146. BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
  147. daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
  148. atomic_dec(&ecryptfs_num_miscdev_opens);
  149. mutex_unlock(&daemon->mux);
  150. rc = ecryptfs_exorcise_daemon(daemon);
  151. if (rc) {
  152. printk(KERN_CRIT "%s: Fatal error whilst attempting to "
  153. "shut down daemon; rc = [%d]. Please report this "
  154. "bug.\n", __func__, rc);
  155. BUG();
  156. }
  157. module_put(THIS_MODULE);
  158. mutex_unlock(&ecryptfs_daemon_hash_mux);
  159. return rc;
  160. }
  161. /**
  162. * ecryptfs_send_miscdev
  163. * @data: Data to send to daemon; may be NULL
  164. * @data_size: Amount of data to send to daemon
  165. * @msg_ctx: Message context, which is used to handle the reply. If
  166. * this is NULL, then we do not expect a reply.
  167. * @msg_type: Type of message
  168. * @msg_flags: Flags for message
  169. * @daemon: eCryptfs daemon object
  170. *
  171. * Add msg_ctx to queue and then, if it exists, notify the blocked
  172. * miscdevess about the data being available. Must be called with
  173. * ecryptfs_daemon_hash_mux held.
  174. *
  175. * Returns zero on success; non-zero otherwise
  176. */
  177. int ecryptfs_send_miscdev(char *data, size_t data_size,
  178. struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
  179. u16 msg_flags, struct ecryptfs_daemon *daemon)
  180. {
  181. int rc = 0;
  182. mutex_lock(&msg_ctx->mux);
  183. if (data) {
  184. msg_ctx->msg = kmalloc((sizeof(*msg_ctx->msg) + data_size),
  185. GFP_KERNEL);
  186. if (!msg_ctx->msg) {
  187. rc = -ENOMEM;
  188. printk(KERN_ERR "%s: Out of memory whilst attempting "
  189. "to kmalloc(%Zd, GFP_KERNEL)\n", __func__,
  190. (sizeof(*msg_ctx->msg) + data_size));
  191. goto out_unlock;
  192. }
  193. } else
  194. msg_ctx->msg = NULL;
  195. msg_ctx->msg->index = msg_ctx->index;
  196. msg_ctx->msg->data_len = data_size;
  197. msg_ctx->type = msg_type;
  198. if (data) {
  199. memcpy(msg_ctx->msg->data, data, data_size);
  200. msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
  201. } else
  202. msg_ctx->msg_size = 0;
  203. mutex_lock(&daemon->mux);
  204. list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
  205. daemon->num_queued_msg_ctx++;
  206. wake_up_interruptible(&daemon->wait);
  207. mutex_unlock(&daemon->mux);
  208. out_unlock:
  209. mutex_unlock(&msg_ctx->mux);
  210. return rc;
  211. }
  212. /**
  213. * ecryptfs_miscdev_read - format and send message from queue
  214. * @file: fs/ecryptfs/euid miscdevfs handle (ignored)
  215. * @buf: User buffer into which to copy the next message on the daemon queue
  216. * @count: Amount of space available in @buf
  217. * @ppos: Offset in file (ignored)
  218. *
  219. * Pulls the most recent message from the daemon queue, formats it for
  220. * being sent via a miscdevfs handle, and copies it into @buf
  221. *
  222. * Returns the number of bytes copied into the user buffer
  223. */
  224. static ssize_t
  225. ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
  226. loff_t *ppos)
  227. {
  228. struct ecryptfs_daemon *daemon;
  229. struct ecryptfs_msg_ctx *msg_ctx;
  230. size_t packet_length_size;
  231. u32 counter_nbo;
  232. char packet_length[3];
  233. size_t i;
  234. size_t total_length;
  235. int rc;
  236. mutex_lock(&ecryptfs_daemon_hash_mux);
  237. /* TODO: Just use file->private_data? */
  238. rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
  239. BUG_ON(rc || !daemon);
  240. mutex_lock(&daemon->mux);
  241. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  242. rc = 0;
  243. printk(KERN_WARNING "%s: Attempt to read from zombified "
  244. "daemon\n", __func__);
  245. goto out_unlock_daemon;
  246. }
  247. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
  248. rc = 0;
  249. goto out_unlock_daemon;
  250. }
  251. /* This daemon will not go away so long as this flag is set */
  252. daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
  253. mutex_unlock(&ecryptfs_daemon_hash_mux);
  254. check_list:
  255. if (list_empty(&daemon->msg_ctx_out_queue)) {
  256. mutex_unlock(&daemon->mux);
  257. rc = wait_event_interruptible(
  258. daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
  259. mutex_lock(&daemon->mux);
  260. if (rc < 0) {
  261. rc = 0;
  262. goto out_unlock_daemon;
  263. }
  264. }
  265. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  266. rc = 0;
  267. goto out_unlock_daemon;
  268. }
  269. if (list_empty(&daemon->msg_ctx_out_queue)) {
  270. /* Something else jumped in since the
  271. * wait_event_interruptable() and removed the
  272. * message from the queue; try again */
  273. goto check_list;
  274. }
  275. BUG_ON(current->euid != daemon->euid);
  276. BUG_ON(current->pid != daemon->pid);
  277. msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
  278. struct ecryptfs_msg_ctx, daemon_out_list);
  279. BUG_ON(!msg_ctx);
  280. mutex_lock(&msg_ctx->mux);
  281. if (msg_ctx->msg) {
  282. rc = ecryptfs_write_packet_length(packet_length,
  283. msg_ctx->msg_size,
  284. &packet_length_size);
  285. if (rc) {
  286. rc = 0;
  287. printk(KERN_WARNING "%s: Error writing packet length; "
  288. "rc = [%d]\n", __func__, rc);
  289. goto out_unlock_msg_ctx;
  290. }
  291. } else {
  292. packet_length_size = 0;
  293. msg_ctx->msg_size = 0;
  294. }
  295. /* miscdevfs packet format:
  296. * Octet 0: Type
  297. * Octets 1-4: network byte order msg_ctx->counter
  298. * Octets 5-N0: Size of struct ecryptfs_message to follow
  299. * Octets N0-N1: struct ecryptfs_message (including data)
  300. *
  301. * Octets 5-N1 not written if the packet type does not
  302. * include a message */
  303. total_length = (1 + 4 + packet_length_size + msg_ctx->msg_size);
  304. if (count < total_length) {
  305. rc = 0;
  306. printk(KERN_WARNING "%s: Only given user buffer of "
  307. "size [%Zd], but we need [%Zd] to read the "
  308. "pending message\n", __func__, count, total_length);
  309. goto out_unlock_msg_ctx;
  310. }
  311. i = 0;
  312. buf[i++] = msg_ctx->type;
  313. counter_nbo = cpu_to_be32(msg_ctx->counter);
  314. memcpy(&buf[i], (char *)&counter_nbo, 4);
  315. i += 4;
  316. if (msg_ctx->msg) {
  317. memcpy(&buf[i], packet_length, packet_length_size);
  318. i += packet_length_size;
  319. rc = copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size);
  320. if (rc) {
  321. printk(KERN_ERR "%s: copy_to_user returned error "
  322. "[%d]\n", __func__, rc);
  323. goto out_unlock_msg_ctx;
  324. }
  325. i += msg_ctx->msg_size;
  326. }
  327. rc = i;
  328. list_del(&msg_ctx->daemon_out_list);
  329. kfree(msg_ctx->msg);
  330. msg_ctx->msg = NULL;
  331. /* We do not expect a reply from the userspace daemon for any
  332. * message type other than ECRYPTFS_MSG_REQUEST */
  333. if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
  334. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  335. out_unlock_msg_ctx:
  336. mutex_unlock(&msg_ctx->mux);
  337. out_unlock_daemon:
  338. daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
  339. mutex_unlock(&daemon->mux);
  340. return rc;
  341. }
  342. /**
  343. * ecryptfs_miscdev_helo
  344. * @euid: effective user id of miscdevess sending helo packet
  345. * @pid: miscdevess id of miscdevess sending helo packet
  346. *
  347. * Returns zero on success; non-zero otherwise
  348. */
  349. static int ecryptfs_miscdev_helo(uid_t uid, pid_t pid)
  350. {
  351. int rc;
  352. rc = ecryptfs_process_helo(ECRYPTFS_TRANSPORT_MISCDEV, uid, pid);
  353. if (rc)
  354. printk(KERN_WARNING "Error processing HELO; rc = [%d]\n", rc);
  355. return rc;
  356. }
  357. /**
  358. * ecryptfs_miscdev_quit
  359. * @euid: effective user id of miscdevess sending quit packet
  360. * @pid: miscdevess id of miscdevess sending quit packet
  361. *
  362. * Returns zero on success; non-zero otherwise
  363. */
  364. static int ecryptfs_miscdev_quit(uid_t euid, pid_t pid)
  365. {
  366. int rc;
  367. rc = ecryptfs_process_quit(euid, pid);
  368. if (rc)
  369. printk(KERN_WARNING
  370. "Error processing QUIT message; rc = [%d]\n", rc);
  371. return rc;
  372. }
  373. /**
  374. * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
  375. * @data: Bytes comprising struct ecryptfs_message
  376. * @data_size: sizeof(struct ecryptfs_message) + data len
  377. * @euid: Effective user id of miscdevess sending the miscdev response
  378. * @pid: Miscdevess id of miscdevess sending the miscdev response
  379. * @seq: Sequence number for miscdev response packet
  380. *
  381. * Returns zero on success; non-zero otherwise
  382. */
  383. static int ecryptfs_miscdev_response(char *data, size_t data_size,
  384. uid_t euid, pid_t pid, u32 seq)
  385. {
  386. struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
  387. int rc;
  388. if ((sizeof(*msg) + msg->data_len) != data_size) {
  389. printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
  390. "[%Zd]; data_size = [%Zd]. Invalid packet.\n", __func__,
  391. (sizeof(*msg) + msg->data_len), data_size);
  392. rc = -EINVAL;
  393. goto out;
  394. }
  395. rc = ecryptfs_process_response(msg, euid, pid, seq);
  396. if (rc)
  397. printk(KERN_ERR
  398. "Error processing response message; rc = [%d]\n", rc);
  399. out:
  400. return rc;
  401. }
  402. /**
  403. * ecryptfs_miscdev_write - handle write to daemon miscdev handle
  404. * @file: File for misc dev handle (ignored)
  405. * @buf: Buffer containing user data
  406. * @count: Amount of data in @buf
  407. * @ppos: Pointer to offset in file (ignored)
  408. *
  409. * miscdevfs packet format:
  410. * Octet 0: Type
  411. * Octets 1-4: network byte order msg_ctx->counter (0's for non-response)
  412. * Octets 5-N0: Size of struct ecryptfs_message to follow
  413. * Octets N0-N1: struct ecryptfs_message (including data)
  414. *
  415. * Returns the number of bytes read from @buf
  416. */
  417. static ssize_t
  418. ecryptfs_miscdev_write(struct file *file, const char __user *buf,
  419. size_t count, loff_t *ppos)
  420. {
  421. u32 counter_nbo, seq;
  422. size_t packet_size, packet_size_length, i;
  423. ssize_t sz = 0;
  424. char *data;
  425. int rc;
  426. if (count == 0)
  427. goto out;
  428. data = kmalloc(count, GFP_KERNEL);
  429. if (!data) {
  430. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  431. "kmalloc([%Zd], GFP_KERNEL)\n", __func__, count);
  432. goto out;
  433. }
  434. rc = copy_from_user(data, buf, count);
  435. if (rc) {
  436. printk(KERN_ERR "%s: copy_from_user returned error [%d]\n",
  437. __func__, rc);
  438. goto out_free;
  439. }
  440. sz = count;
  441. i = 0;
  442. switch (data[i++]) {
  443. case ECRYPTFS_MSG_RESPONSE:
  444. if (count < (1 + 4 + 1 + sizeof(struct ecryptfs_message))) {
  445. printk(KERN_WARNING "%s: Minimum acceptable packet "
  446. "size is [%Zd], but amount of data written is "
  447. "only [%Zd]. Discarding response packet.\n",
  448. __func__,
  449. (1 + 4 + 1 + sizeof(struct ecryptfs_message)),
  450. count);
  451. goto out_free;
  452. }
  453. memcpy((char *)&counter_nbo, &data[i], 4);
  454. seq = be32_to_cpu(counter_nbo);
  455. i += 4;
  456. rc = ecryptfs_parse_packet_length(&data[i], &packet_size,
  457. &packet_size_length);
  458. if (rc) {
  459. printk(KERN_WARNING "%s: Error parsing packet length; "
  460. "rc = [%d]\n", __func__, rc);
  461. goto out_free;
  462. }
  463. i += packet_size_length;
  464. if ((1 + 4 + packet_size_length + packet_size) != count) {
  465. printk(KERN_WARNING "%s: (1 + packet_size_length([%Zd])"
  466. " + packet_size([%Zd]))([%Zd]) != "
  467. "count([%Zd]). Invalid packet format.\n",
  468. __func__, packet_size_length, packet_size,
  469. (1 + packet_size_length + packet_size), count);
  470. goto out_free;
  471. }
  472. rc = ecryptfs_miscdev_response(&data[i], packet_size,
  473. current->euid,
  474. current->pid, seq);
  475. if (rc)
  476. printk(KERN_WARNING "%s: Failed to deliver miscdev "
  477. "response to requesting operation; rc = [%d]\n",
  478. __func__, rc);
  479. break;
  480. case ECRYPTFS_MSG_HELO:
  481. rc = ecryptfs_miscdev_helo(current->euid, current->pid);
  482. if (rc) {
  483. printk(KERN_ERR "%s: Error attempting to process "
  484. "helo from pid [%d]; rc = [%d]\n", __func__,
  485. current->pid, rc);
  486. goto out_free;
  487. }
  488. break;
  489. case ECRYPTFS_MSG_QUIT:
  490. rc = ecryptfs_miscdev_quit(current->euid, current->pid);
  491. if (rc) {
  492. printk(KERN_ERR "%s: Error attempting to process "
  493. "quit from pid [%d]; rc = [%d]\n", __func__,
  494. current->pid, rc);
  495. goto out_free;
  496. }
  497. break;
  498. default:
  499. ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
  500. "message of unrecognized type [%d]\n",
  501. data[0]);
  502. break;
  503. }
  504. out_free:
  505. kfree(data);
  506. out:
  507. return sz;
  508. }
  509. static const struct file_operations ecryptfs_miscdev_fops = {
  510. .open = ecryptfs_miscdev_open,
  511. .poll = ecryptfs_miscdev_poll,
  512. .read = ecryptfs_miscdev_read,
  513. .write = ecryptfs_miscdev_write,
  514. .release = ecryptfs_miscdev_release,
  515. };
  516. static struct miscdevice ecryptfs_miscdev = {
  517. .minor = MISC_DYNAMIC_MINOR,
  518. .name = "ecryptfs",
  519. .fops = &ecryptfs_miscdev_fops
  520. };
  521. /**
  522. * ecryptfs_init_ecryptfs_miscdev
  523. *
  524. * Messages sent to the userspace daemon from the kernel are placed on
  525. * a queue associated with the daemon. The next read against the
  526. * miscdev handle by that daemon will return the oldest message placed
  527. * on the message queue for the daemon.
  528. *
  529. * Returns zero on success; non-zero otherwise
  530. */
  531. int ecryptfs_init_ecryptfs_miscdev(void)
  532. {
  533. int rc;
  534. atomic_set(&ecryptfs_num_miscdev_opens, 0);
  535. mutex_lock(&ecryptfs_daemon_hash_mux);
  536. rc = misc_register(&ecryptfs_miscdev);
  537. if (rc)
  538. printk(KERN_ERR "%s: Failed to register miscellaneous device "
  539. "for communications with userspace daemons; rc = [%d]\n",
  540. __func__, rc);
  541. mutex_unlock(&ecryptfs_daemon_hash_mux);
  542. return rc;
  543. }
  544. /**
  545. * ecryptfs_destroy_ecryptfs_miscdev
  546. *
  547. * All of the daemons must be exorcised prior to calling this
  548. * function.
  549. */
  550. void ecryptfs_destroy_ecryptfs_miscdev(void)
  551. {
  552. BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
  553. misc_deregister(&ecryptfs_miscdev);
  554. }