fs_mtdproxy.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /****************************************************************************
  2. * fs/driver/fs_mtdproxy.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <debug.h>
  31. #include <nuttx/kmalloc.h>
  32. #include <nuttx/mtd/mtd.h>
  33. #include <nuttx/semaphore.h>
  34. #include "driver/driver.h"
  35. /****************************************************************************
  36. * Private Data
  37. ****************************************************************************/
  38. static uint32_t g_devno;
  39. static sem_t g_devno_sem = SEM_INITIALIZER(1);
  40. /****************************************************************************
  41. * Private Functions
  42. ****************************************************************************/
  43. /****************************************************************************
  44. * Name: unique_blkdev
  45. *
  46. * Description:
  47. * Create a unique temporary device name in the /dev/ directory of the
  48. * pseudo-file system. We cannot use mktemp for this because it will
  49. * attempt to open() the file.
  50. *
  51. * Input Parameters:
  52. * None
  53. *
  54. * Returned Value:
  55. * The allocated path to the device. This must be released by the caller
  56. * to prevent memory links. NULL will be returned only the case where
  57. * we fail to allocate memory.
  58. *
  59. ****************************************************************************/
  60. static FAR char *unique_blkdev(void)
  61. {
  62. struct stat statbuf;
  63. char devbuf[16];
  64. uint32_t devno;
  65. int ret;
  66. /* Loop until we get a unique device name */
  67. for (; ; )
  68. {
  69. /* Get the semaphore protecting the path number */
  70. ret = nxsem_wait_uninterruptible(&g_devno_sem);
  71. if (ret < 0)
  72. {
  73. ferr("ERROR: nxsem_wait_uninterruptible failed: %d\n", ret);
  74. return NULL;
  75. }
  76. /* Get the next device number and release the semaphore */
  77. devno = ++g_devno;
  78. nxsem_post(&g_devno_sem);
  79. /* Construct the full device number */
  80. devno &= 0xffffff;
  81. snprintf(devbuf, 16, "/dev/tmpb%06lx", (unsigned long)devno);
  82. /* Make sure that file name is not in use */
  83. ret = stat(devbuf, &statbuf);
  84. if (ret < 0)
  85. {
  86. DEBUGASSERT(errno == ENOENT);
  87. return strdup(devbuf);
  88. }
  89. /* It is in use, try again */
  90. }
  91. }
  92. /****************************************************************************
  93. * Public Functions
  94. ****************************************************************************/
  95. /****************************************************************************
  96. * Name: mtd_proxy
  97. *
  98. * Description:
  99. * Create a temporary block driver using drivers/mtd/ftl to mediate block
  100. * oriented accessed to the mtd driver.
  101. *
  102. * Input Parameters:
  103. * mtddev - The path to the mtd driver
  104. * mountflags - if MS_RDONLY is not set, then driver must support write
  105. * operations (see include/sys/mount.h)
  106. * ppinode - address of the location to return the inode reference
  107. *
  108. * Returned Value:
  109. * If zero, non-zero inode pointer is returned on success. This
  110. * is the inode pointer of the nameless block driver that mediates
  111. * accesses to the mtd driver.
  112. *
  113. * Errors that may be returned:
  114. *
  115. * ENOMEM - Failed to create a temporary path name.
  116. *
  117. * Plus:
  118. *
  119. * - Errors reported from ftl_initialize()
  120. * - Errors reported from open() or unlink()
  121. *
  122. ****************************************************************************/
  123. int mtd_proxy(FAR const char *mtddev, int mountflags,
  124. FAR struct inode **ppinode)
  125. {
  126. FAR struct inode *mtd;
  127. FAR char *blkdev;
  128. int ret;
  129. /* Create a unique temporary file name for the block device */
  130. blkdev = unique_blkdev();
  131. if (blkdev == NULL)
  132. {
  133. ferr("ERROR: Failed to create temporary device name\n");
  134. return -ENOMEM;
  135. }
  136. /* Wrap the mtd driver with an instance of the ftl driver */
  137. ret = find_mtddriver(mtddev, &mtd);
  138. if (ret < 0)
  139. {
  140. ferr("ERROR: Failed to find %s mtd driver\n", mtddev);
  141. goto out_with_blkdev;
  142. }
  143. ret = ftl_initialize_by_path(blkdev, mtd->u.i_mtd);
  144. inode_release(mtd);
  145. if (ret < 0)
  146. {
  147. ferr("ERROR: ftl_initialize_by_path(%s, %s) failed: %d\n",
  148. mtddev, blkdev, ret);
  149. goto out_with_blkdev;
  150. }
  151. /* Open the newly created block driver */
  152. ret = open_blockdriver(blkdev, mountflags, ppinode);
  153. if (ret < 0)
  154. {
  155. ferr("ERROR: Failed to open %s: %d\n", blkdev, ret);
  156. goto out_with_fltdev;
  157. }
  158. /* Unlink and free the block device name. The driver instance will
  159. * persist, provided that CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y (otherwise,
  160. * we have a problem here!)
  161. */
  162. out_with_fltdev:
  163. unlink(blkdev);
  164. out_with_blkdev:
  165. kmm_free(blkdev);
  166. return ret;
  167. }