nxffs_ioctl.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_ioctl.c
  3. *
  4. * Copyright (C) 2011, 2013, 2017-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <string.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/fs/fs.h>
  46. #include <nuttx/fs/ioctl.h>
  47. #include <nuttx/mtd/mtd.h>
  48. #include "nxffs.h"
  49. /****************************************************************************
  50. * Public Functions
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Name: nxffs_ioctl
  54. *
  55. * Description:
  56. * Standard mountpoint ioctl method.
  57. *
  58. ****************************************************************************/
  59. int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  60. {
  61. FAR struct nxffs_volume_s *volume;
  62. int ret;
  63. finfo("cmd: %d arg: %08lx\n", cmd, arg);
  64. /* Sanity checks */
  65. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  66. /* Recover the file system state from the open file */
  67. volume = filep->f_inode->i_private;
  68. DEBUGASSERT(volume != NULL);
  69. /* Get exclusive access to the volume. Note that the volume exclsem
  70. * protects the open file list.
  71. */
  72. ret = nxsem_wait(&volume->exclsem);
  73. if (ret < 0)
  74. {
  75. ferr("ERROR: nxsem_wait failed: %d\n", ret);
  76. goto errout;
  77. }
  78. /* Only a reformat and optimize commands are supported */
  79. if (cmd == FIOC_REFORMAT)
  80. {
  81. finfo("Reformat command\n");
  82. /* We cannot reformat the volume if there are any open inodes */
  83. if (volume->ofiles)
  84. {
  85. ferr("ERROR: Open files\n");
  86. ret = -EBUSY;
  87. goto errout_with_semaphore;
  88. }
  89. /* Re-format the volume -- all is lost */
  90. ret = nxffs_reformat(volume);
  91. }
  92. else if (cmd == FIOC_OPTIMIZE)
  93. {
  94. finfo("Optimize command\n");
  95. /* Pack the volume */
  96. ret = nxffs_pack(volume);
  97. }
  98. else
  99. {
  100. /* Command not recognized, forward to the MTD driver */
  101. ret = MTD_IOCTL(volume->mtd, cmd, arg);
  102. }
  103. errout_with_semaphore:
  104. nxsem_post(&volume->exclsem);
  105. errout:
  106. return ret;
  107. }