inode.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2002 Juli Mallett. All rights reserved.
  3. *
  4. * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
  5. * FreeBSD project. Redistribution and use in source and binary forms, with
  6. * or without modification, are permitted provided that the following
  7. * conditions are met:
  8. *
  9. * 1. Redistribution of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <u.h>
  28. #include <libc.h>
  29. #include <ufs/libufsdat.h>
  30. #include <ufs/dinode.h>
  31. #include <ufs/fs.h>
  32. #include <ufs/libufs.h>
  33. int
  34. getino(Uufsd *disk, void **dino, ino_t inode, int *mode)
  35. {
  36. ino_t min, max;
  37. caddr_t inoblock;
  38. ufs2_dinode *dp2;
  39. Fs *fs;
  40. libufserror(disk, nil);
  41. fs = &disk->d_fs;
  42. inoblock = disk->d_inoblock;
  43. min = disk->d_inomin;
  44. max = disk->d_inomax;
  45. if (inoblock == nil) {
  46. inoblock = malloc(fs->fs_bsize);
  47. if (inoblock == nil) {
  48. libufserror(disk, "unable to allocate inode block");
  49. return (-1);
  50. }
  51. disk->d_inoblock = inoblock;
  52. }
  53. if (inode >= min && inode < max)
  54. goto gotit;
  55. bread(disk, fsbtodb(fs, ino_to_fsba(fs, inode)), inoblock,
  56. fs->fs_bsize);
  57. disk->d_inomin = min = inode - (inode % INOPB(fs));
  58. disk->d_inomax = max = min + INOPB(fs);
  59. gotit: switch (disk->d_ufs) {
  60. case 1:
  61. libufserror(disk, "UFS1 not supported");
  62. return (-1);
  63. case 2:
  64. dp2 = &((struct ufs2_dinode *)inoblock)[inode - min];
  65. *mode = dp2->di_mode & IFMT;
  66. *dino = dp2;
  67. return (0);
  68. default:
  69. break;
  70. }
  71. libufserror(disk, "unknown UFS filesystem type");
  72. return (-1);
  73. }
  74. int
  75. putino(Uufsd *disk)
  76. {
  77. Fs *fs;
  78. fs = &disk->d_fs;
  79. if (disk->d_inoblock == nil) {
  80. libufserror(disk, "No inode block allocated");
  81. return (-1);
  82. }
  83. if (bwrite(disk, fsbtodb(fs, ino_to_fsba(&disk->d_fs, disk->d_inomin)),
  84. disk->d_inoblock, disk->d_fs.fs_bsize) <= 0)
  85. return (-1);
  86. return (0);
  87. }