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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*----------------------------------------------------------------------------*/
/*node.c*/
/*----------------------------------------------------------------------------*/
/*Implementation of node management strategies*/
/*----------------------------------------------------------------------------*/
/*Based on the code of unionfs translator.*/
/*----------------------------------------------------------------------------*/
/*Copyright (C) 2001, 2002, 2005, 2008 Free Software Foundation, Inc.
Written by Sergiu Ivanov <unlimitedscolobb@gmail.com>.
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 of the
License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.*/
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
#define _GNU_SOURCE 1
/*----------------------------------------------------------------------------*/
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h>
/*----------------------------------------------------------------------------*/
#include "debug.h"
#include "node.h"
#include "filter.h"
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*--------Global Variables----------------------------------------------------*/
/*The lock protecting the underlying filesystem*/
struct mutex ulfs_lock = MUTEX_INITIALIZER;
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*--------Functions-----------------------------------------------------------*/
/*Derives a new node from `lnode` and adds a reference to `lnode`*/
error_t
node_create
(
node_t ** node /*store the result here*/
)
{
error_t err = 0;
/*Create a new netnode*/
netnode_t * netnode_new = malloc(sizeof(netnode_t));
/*If the memory could not be allocated*/
if(netnode_new == NULL)
err = ENOMEM;
else
{
/*create a new node from the netnode*/
node_t * node_new = netfs_make_node(netnode_new);
/*If the creation failed*/
if(node_new == NULL)
{
/*set the error code*/
err = ENOMEM;
/*destroy the netnode created above*/
free(netnode_new);
/*stop*/
return err;
}
/*store the result of creation in the second parameter*/
*node = node_new;
}
/*Return the result of operations*/
return err;
}/*node_create*/
/*----------------------------------------------------------------------------*/
/*Destroys the specified node and removes a light reference from the
associated light node*/
void
node_destroy
(
node_t * np
)
{
/*Destroy the port to the underlying filesystem allocated to the node*/
PORT_DEALLOC(np->nn->port);
/*Free the netnode and the node itself*/
free(np->nn);
free(np);
}/*node_destroy*/
/*----------------------------------------------------------------------------*/
/*Creates the root node and the corresponding lnode*/
error_t
node_create_root
(
node_t ** root_node /*store the result here*/
)
{
error_t err;
/*Try to create a node*/
node_t * node;
err = node_create(&node);
if(err)
return err;
/*Store the result in the parameter*/
*root_node = node;
/*Return the result*/
return err;
}/*node_create_root*/
/*----------------------------------------------------------------------------*/
/*Initializes the port to the underlying filesystem for the root node*/
error_t
node_init_root
(
mach_port_t underlying, /*the port to the underlying node*/
node_t * node /*the root node*/
)
{
error_t err = 0;
/*Acquire a lock for operations on the underlying filesystem*/
mutex_lock(&ulfs_lock);
/*Store the specified port in the node*/
node->nn->port = underlying;
LOG_MSG("node_init_root: Port: 0x%ld", (unsigned long)node->nn->port);
/*Stat the root node*/
err = io_stat(node->nn->port, &node->nn_stat);
if(err)
{
/*deallocate the port*/
PORT_DEALLOC(node->nn->port);
LOG_MSG("node_init_root: Could not stat the root node.");
/*unlock the mutex and exit*/
mutex_unlock(&ulfs_lock);
return err;
}
/*Release the lock for operations on the undelying filesystem*/
mutex_unlock(&ulfs_lock);
/*Return the result of operations*/
return err;
}/*node_init_root*/
/*----------------------------------------------------------------------------*/
|