Guest User

Untitled

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /**
  2. * \file sofs_ifuncs_1_adb.c (implementation file of function soAllocDataCluster)
  3. *
  4. * \author Artur Carneiro Pereira - September 2008 / September 2011
  5. * \author António Rui Borges - September 2010 / September 2011
  6. *
  7. * \remarks In case an error occurs, all functions return a negative value which is the symmetric of the system error
  8. * or the local error that better represents the error cause. Local errors are out of the range of the
  9. * system errors.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <inttypes.h>
  15. #include <time.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18.  
  19. #include "sofs_probe.h"
  20. #include "sofs_buffercache.h"
  21. #include "sofs_superblock.h"
  22. #include "sofs_inode.h"
  23. #include "sofs_datacluster.h"
  24. #include "sofs_basicoper.h"
  25. #include "sofs_basicconsist.h"
  26.  
  27. /** binary implementation prototype */
  28. int soAllocDataCluster_bin (uint32_t nInode, uint32_t *p_nClust);
  29.  
  30. /**
  31. * \brief Allocate a free data cluster and associate it to an inode.
  32. *
  33. * The inode is supposed to be associated to a file (a regular file, a directory or a symbolic link), but the only
  34. * consistency check at this stage should be to check the inode is not free.
  35. *
  36. * The cluster is retrieved from the retrieval cache of free data cluster references. If the cache is empty, it has to
  37. * be replenished before the retrieval may take place. If the data cluster is in the dirty state, it has to be cleaned
  38. * first. The header fields of the allocated cluster should be all filled in: <tt>prev</tt> and <tt>next</tt> should be
  39. * set to \c NULL_CLUSTER and <tt>stat</tt> to the given inode number.
  40. *
  41. * \param nInode number of the inode the data cluster should be associated to
  42. * \param p_nClust pointer to the location where the logical number of the allocated data cluster is to be stored
  43. *
  44. * \return <tt>0 (zero)</tt>, on success
  45. * \return -\c EINVAL, the <em>inode number</em> is out of range or the <em>pointer to the logical data cluster
  46. * number</em> is \c NULL
  47. * \return -\c ENOSPC, if there are no free data clusters
  48. * \return -\c EIUININVAL, if the inode in use is inconsistent
  49. * \return -\c ELDCININVAL, if the list of data cluster references belonging to an inode is inconsistent
  50. * \return -\c EDCINVAL, if the data cluster header is inconsistent
  51. * \return -\c ELIBBAD, if some kind of inconsistency was detected at some internal storage lower level
  52. * \return -\c EBADF, if the device is not already opened
  53. * \return -\c EIO, if it fails reading or writing
  54. * \return -<em>other specific error</em> issued by \e lseek system call
  55. */
  56. int soAllocDataCluster (uint32_t nInode, uint32_t *p_nClust)
  57. {
  58. SOInode *p_soInode;
  59. uint32_t nBlk, offset;
  60.  
  61. soProbe (413, "soAllocDataCluster(%"PRIu32", %p)\n", nInode, p_nClust);
  62.  
  63. /* Sanity checks
  64. * nInode == 0 => this is where the ROOT DIR is located ...
  65. * p_nClust can't be NULL
  66. */
  67. if (nInode == 0 || p_nClust == NULL) {
  68. return -EINVAL;
  69. }
  70.  
  71. /* Load the nInode */
  72. {
  73. /* Convert the given nInode into the logical stuff */
  74. if ( (status = soConvertRefInT(nInode, &nBlk, &offset)) != 0) {
  75. return status;
  76. }
  77.  
  78. /* Load the block using the acquired information */
  79. if ( (status = soLoadBlockInT(nBlk)) != 0) {
  80. return status;
  81. }
  82.  
  83. /* Grab the pointer to the inode */
  84. if ( (p_soInode = soGetBlockInT(void)) == NULL_INODE ) {
  85. return p_soInode;
  86. }
  87.  
  88. /* Check if iNode is free (via bit 12) */
  89. if ( (p_soInode->mode & FREE_INODE) == 0) {
  90. return
  91. }
  92.  
  93. }
  94. int soConvertRefInT (uint32_t nInode, uint32_t *p_nBlk, uint32_t *p_offset)
  95. Convert the inode number, which translates to an entry of the inode table, into the logical number (the ordinal, starting at zero, of the succession blocks that the table of inodes comprises) and the offset of the block where it is stored.
  96. int soLoadBlockInT (uint32_t nBlk)
  97. Load the contents of a specific block of the table of inodes into internal storage.
  98. SOInode * soGetBlockInT (void)
  99. Get a pointer to the contents of a specific block of the table of inodes.
  100.  
  101. /* check if inode is free (via bit 12) */
  102. if ((nInode & INODE_FREE) == 0) {
  103. return 0;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. return soAllocDataCluster_bin(nInode, p_nClust);
  110. }
Add Comment
Please, Sign In to add comment