summaryrefslogtreecommitdiff
path: root/ext2fs/storeinfo.c
blob: 7d6bdd2317cc8d0c39db5c9e4b145c98171b0960 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Access to file layout information

   Copyright (C) 1996 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This program 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.

   This program 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 <netinet/in.h>

#include "ext2fs.h"

error_t
diskfs_S_file_get_storage_info (struct protid *cred, int *class,
				off_t **runs, unsigned *runs_len,
				size_t *block_size,
				char *dev_name, mach_port_t *dev_port,
				mach_msg_type_name_t *dev_port_type,
				char **misc, unsigned *misc_len,
				int *flags)
{
  error_t err = 0;
  block_t index = 0;
  unsigned num_fs_blocks;
  unsigned runs_alloced = 0;
  off_t *run = 0;
  struct node *node = cred->po->np;

  *misc_len = sizeof (long) * 4;
  err = vm_allocate (mach_task_self (), (vm_address_t *)misc, *misc_len, 1);
  if (err)
    return err;

  mutex_lock (&node->lock);

  num_fs_blocks = node->dn_stat.st_blocks >> log2_stat_blocks_per_fs_block;
  while (num_fs_blocks > 0)
    {
      block_t block;

      err = ext2_getblk (node, index++, 0, &block);
      if (err == EINVAL)
	/* Either a hole, or past the end of the file.  */
	{
	  block = 0;
	  err = 0;
	}
      if (err)
	goto fail;

      block <<= log2_dev_blocks_per_fs_block;
      if (!run
	  || ((block && run[0] >= 0)        /* Neither is a hole and... */
	      ? (block != run[0] + run[1])  /* ... BLOCK doesn't follow RUN */
	      : (block || run[0] >= 0)))    /* or ... one is, but not both */
	/* Add a new run.  */
	{
	  if (run)
	    /* There are already some runs.  */
	    {
	      run += 2;
	      if (run >= *runs + runs_alloced)
		/* Add a new page to the end of the existing RUNS array.  */
		{
		  err = vm_allocate (mach_task_self (),
				     (vm_address_t *)&run, vm_page_size, 0);
		  if (err)
		    goto fail;
		  runs_alloced += vm_page_size / sizeof (off_t);
		}
	    }
	  else
	    /* Allocate the RUNS array for the first time.  */
	    {
	      err = vm_allocate (mach_task_self (),
				 (vm_address_t *)runs, vm_page_size, 1);
	      if (err)
		goto fail;
	      runs_alloced = vm_page_size / sizeof (off_t);
	      run = *runs;
	    }

	  run[0] = block ?: -1;	/* -1 means a hole in RUNS */
	  run[1] = 0;		/* will get extended just below */
	}

      /* Increase the size of the current run by one filesystem block.  */
      run[1] += 1 << log2_dev_blocks_per_fs_block;

      num_fs_blocks--;
    }

  if (run)
    {
      if (run[0] >= 0)
	/* Include the current run, as long as it's not a hole.  */
	run += 2;
      else if ((off_t *)trunc_page (run) == run)
	/* We allocated just *one* too many pages -- the last run is a hole. */
	vm_deallocate (mach_task_self (), (vm_address_t)run, vm_page_size);
      *runs_len = run - *runs;
    }
  else
    *runs_len = 0;

  ((long *)*misc)[0] = htonl (node->cache_id);
  ((long *)*misc)[1] = htonl (dino (node->cache_id)->i_translator);

  *class = STORAGE_DEVICE;
  *flags = 0;

  *block_size = diskfs_device_block_size;

  strcpy (dev_name, diskfs_device_name);

  if (diskfs_isuid (0, cred))
    *dev_port = diskfs_device;
  else
    *dev_port = MACH_PORT_NULL;
  *dev_port_type = MACH_MSG_TYPE_COPY_SEND;

 fail:
  mutex_unlock (&node->lock);

  if (err)
    {
      if (*runs_len > 0)
	vm_deallocate (mach_task_self (), (vm_address_t)*runs,
		       runs_alloced * sizeof (off_t));
      vm_deallocate (mach_task_self (), (vm_address_t)*misc, *misc_len);
    }

  return err;
}