fs_registerblockdriver.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /****************************************************************************
  2. * fs/driver/fs_registerblockdriver.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 <errno.h>
  26. #include <nuttx/fs/fs.h>
  27. #include "inode/inode.h"
  28. #ifndef CONFIG_DISABLE_MOUNTPOINT
  29. /****************************************************************************
  30. * Public Functions
  31. ****************************************************************************/
  32. /****************************************************************************
  33. * Name: register_blockdriver
  34. *
  35. * Description:
  36. * Register a block driver inode the pseudo file system.
  37. *
  38. * Input Parameters:
  39. * path - The path to the inode to create
  40. * bops - The block driver operations structure
  41. * mode - inmode privileges (not used)
  42. * priv - Private, user data that will be associated with the inode.
  43. *
  44. * Returned Value:
  45. * Zero on success (with the inode point in 'inode'); A negated errno
  46. * value is returned on a failure (all error values returned by
  47. * inode_reserve):
  48. *
  49. * EINVAL - 'path' is invalid for this operation
  50. * EEXIST - An inode already exists at 'path'
  51. * ENOMEM - Failed to allocate in-memory resources for the operation
  52. *
  53. ****************************************************************************/
  54. int register_blockdriver(FAR const char *path,
  55. FAR const struct block_operations *bops,
  56. mode_t mode, FAR void *priv)
  57. {
  58. FAR struct inode *node;
  59. int ret;
  60. /* Insert an inode for the device driver -- we need to hold the inode
  61. * semaphore to prevent access to the tree while we this. This is because
  62. * we will have a momentarily bad true until we populate the inode with
  63. * valid data.
  64. */
  65. ret = inode_semtake();
  66. if (ret < 0)
  67. {
  68. return ret;
  69. }
  70. ret = inode_reserve(path, &node);
  71. if (ret >= 0)
  72. {
  73. /* We have it, now populate it with block driver specific information.
  74. * NOTE that the initial reference count on the new inode is zero.
  75. */
  76. INODE_SET_BLOCK(node);
  77. node->u.i_bops = bops;
  78. #ifdef CONFIG_FILE_MODE
  79. node->i_mode = mode;
  80. #endif
  81. node->i_private = priv;
  82. ret = OK;
  83. }
  84. inode_semgive();
  85. return ret;
  86. }
  87. #endif /* !CONFIG_DISABLE_MOUNTPOINT */