summaryrefslogtreecommitdiff
path: root/ufs/bmap.c
blob: c2eea356b8710e4d13f39e890333e38febc193ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Interpretation of indirect block structure
   Copyright (C) 1994 Free Software Foundation, Inc.
   Written by Michael I. Bushnell.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */


#include "ufs.h"
#include "dinode.h"
#include "fs.h"

/* Number of block pointers that fit in a single disk block */
#define NIPTR (sblock->fs_bsize / sizeof (daddr_t))

/* For logical block number LBN of file NP, look it the block address,
   giving the "path" of indirect blocks to the file, starting 
   with the least indirect.  Fill *INDIRS with information for
   the block.  */
error_t
fetch_indir_spec (struct node *np, daddr_t lbn, struct iblock_spec *indirs)
{
  struct dinode *di = dino (np->dn->number);
  int boff;
  error_t err;
  daddr_t *siblock;
  
  if (err = diskfs_catch_exception ())
    return err;
  
  if (lbn < NDADDR)
    {
      indirs[0].bno = di->di_db[lbn];
      indirs[0].offset = -1;
  
      diskfs_end_catch_exception ();
      return 0;
    }

  lbn -= NDADDR;

  indirs[0].offset = lbn % NIPTR;
  
  if (lbn / NIPTR)
    {
      /* We will use the double indirect block */
      int ibn;
      daddr_t *diblock;

      ibn = lbn / NIPTR - 1;

      indirs[1].offset = ibn % NIPTR;
      
      /* We don't support triple indirect blocks, but this 
	 is where we'd do it. */
      assert (!(ibn / NIPTR));
  
      indirs[2].offset = -1;
      indirs[2].bno = di->di_ib[INDIR_DOUBLE];

      if (indirs[2].bno)
	{
	  diblock = indir_block (indirs[2].bno);
	  indirs[1].bno = diblock[indirs[1].offset];
	}
      else
	indirs[1].bno = 0;
    }
  else
    {
      indirs[1].offset = -1;
      indirs[1].bno = di->di_ib[INDIR_DOUBLE];
    }

  if (indirs[1].bno)
    {
      siblock = indir_block (indirs[1].bno);
      indirs[0].bno = siblock[indirs[0].offset];
    }
  else
    indirs[0].bno = 0;

  diskfs_end_catch_exception ();
  return 0;
}