1 /*
2 * Copyright (c) 1998,1999,2000
3 * Traakan, Inc., Los Altos, CA
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
11 * disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Project: NDMJOB
31 * Ident: $Id: $
32 *
33 * Description:
34 *
35 */
36
37
38 #include "ndmagents.h"
39
40 #ifndef NDMOS_OPTION_NO_DATA_AGENT
41
42
43 /*
44 * Initialization and Cleanup
45 ****************************************************************
46 */
47
48 /* Initialize -- Set data structure to know value, ignore current value */
49 int
ndmda_fh_initialize(struct ndm_session * sess)50 ndmda_fh_initialize (struct ndm_session *sess)
51 {
52 struct ndm_data_agent * da = sess->data_acb;
53 struct ndmfhheap * fhh = &da->fhh;
54
55 ndmfhh_initialize (fhh);
56
57 return 0;
58 }
59
60 /* Commission -- Get agent ready. Entire session has been initialize()d */
61 int
ndmda_fh_commission(struct ndm_session * sess)62 ndmda_fh_commission (struct ndm_session *sess)
63 {
64 struct ndm_data_agent * da = sess->data_acb;
65 struct ndmfhheap * fhh = &da->fhh;
66
67 da->fhh_buf = NDMOS_API_MALLOC (NDMDA_N_FHH_BUF);
68 if (!da->fhh_buf)
69 return -1;
70 //ndmfhh_commission (fhh, da->fhh_buf, sizeof *da->fhh_buf);
71 ndmfhh_commission (fhh, da->fhh_buf, NDMDA_N_FHH_BUF);
72
73 return 0;
74 }
75
76 /* Decommission -- Discard agent */
77 int
ndmda_fh_decommission(struct ndm_session * sess)78 ndmda_fh_decommission (struct ndm_session *sess)
79 {
80 return 0;
81 }
82
83 /* Destroy -- Destroy agent */
84 int
ndmda_fh_destroy(struct ndm_session * sess)85 ndmda_fh_destroy (struct ndm_session *sess)
86 {
87 if (sess->data_acb->fhh_buf) {
88 NDMOS_API_FREE (sess->data_acb->fhh_buf);
89 sess->data_acb->fhh_buf = NULL;
90 }
91
92 return 0;
93 }
94
95 /* Belay -- Cancel partially issued activation/start */
96 int
ndmda_fh_belay(struct ndm_session * sess)97 ndmda_fh_belay (struct ndm_session *sess)
98 {
99 return 0;
100 }
101
102
103
104
105 /*
106 * Semantic actions -- called from ndmda_XXX() butype formatters
107 ****************************************************************
108 */
109
110 void
ndmda_fh_add_file(struct ndm_session * sess,ndmp9_file_stat * filestat,char * name)111 ndmda_fh_add_file (struct ndm_session *sess,
112 ndmp9_file_stat *filestat, char *name)
113 {
114 struct ndm_data_agent * da = sess->data_acb;
115 int nlen = strlen (name) + 1;
116 ndmp9_file * file9;
117 int rc;
118
119 rc = ndmda_fh_prepare (sess, NDMP9VER, NDMP9_FH_ADD_FILE,
120 sizeof (ndmp9_file), 1, nlen);
121
122 if (rc != NDMFHH_RET_OK)
123 return;
124
125 file9 = ndmfhh_add_entry (&da->fhh);
126 file9->fstat = *filestat;
127 file9->unix_path = ndmfhh_save_item (&da->fhh, name, nlen);
128 }
129
130 void
ndmda_fh_add_dir(struct ndm_session * sess,uint64_t dir_fileno,char * name,uint64_t fileno)131 ndmda_fh_add_dir (struct ndm_session *sess,
132 uint64_t dir_fileno, char *name, uint64_t fileno)
133 {
134 struct ndm_data_agent * da = sess->data_acb;
135 int nlen = strlen (name) + 1;
136 ndmp9_dir * dir9;
137 int rc;
138
139 rc = ndmda_fh_prepare (sess, NDMP9VER, NDMP9_FH_ADD_DIR,
140 sizeof (ndmp9_dir), 1, nlen);
141
142 if (rc != NDMFHH_RET_OK)
143 return;
144
145 dir9 = ndmfhh_add_entry (&da->fhh);
146 dir9->unix_name = ndmfhh_save_item (&da->fhh, name, nlen);
147 dir9->parent = dir_fileno;
148 dir9->node = fileno;
149 }
150
151 void
ndmda_fh_add_node(struct ndm_session * sess,ndmp9_file_stat * filestat)152 ndmda_fh_add_node (struct ndm_session *sess, ndmp9_file_stat *filestat)
153 {
154 struct ndm_data_agent * da = sess->data_acb;
155 ndmp9_node * node9;
156 int rc;
157
158 rc = ndmda_fh_prepare (sess, NDMP9VER, NDMP9_FH_ADD_NODE,
159 sizeof (ndmp9_node), 1, 0);
160
161 if (rc != NDMFHH_RET_OK)
162 return;
163
164 node9 = ndmfhh_add_entry (&da->fhh);
165 node9->fstat = *filestat;
166 }
167
168
169
170
171 /*
172 * Helpers -- prepare/flush
173 ****************************************************************
174 */
175
176 int
ndmda_fh_prepare(struct ndm_session * sess,int vers,int msg,int entry_size,unsigned n_item,unsigned total_size_of_items)177 ndmda_fh_prepare (struct ndm_session *sess,
178 int vers, int msg, int entry_size,
179 unsigned n_item, unsigned total_size_of_items)
180 {
181 struct ndm_data_agent * da = sess->data_acb;
182 struct ndmfhheap * fhh = &da->fhh;
183 int fhtype = (vers<<16) + msg;
184 int rc;
185
186 rc = ndmfhh_prepare (fhh, fhtype, entry_size,
187 n_item, total_size_of_items);
188
189 if (rc == NDMFHH_RET_OK)
190 return NDMFHH_RET_OK;
191
192 ndmda_fh_flush (sess);
193
194 rc = ndmfhh_prepare (fhh, fhtype, entry_size,
195 n_item, total_size_of_items);
196
197 return rc;
198 }
199
200 void
ndmda_fh_flush(struct ndm_session * sess)201 ndmda_fh_flush (struct ndm_session *sess)
202 {
203 struct ndm_data_agent * da = sess->data_acb;
204 struct ndmfhheap * fhh = &da->fhh;
205 int rc;
206 int fhtype;
207 void * table;
208 unsigned n_entry;
209
210 rc = ndmfhh_get_table (fhh, &fhtype, &table, &n_entry);
211 if (rc == NDMFHH_RET_OK && n_entry > 0) {
212 struct ndmp_xa_buf xa;
213 struct ndmfhh_generic_table *request;
214
215 request = (void *) &xa.request.body;
216 NDMOS_MACRO_ZEROFILL (&xa);
217
218 xa.request.protocol_version = fhtype >> 16;
219 xa.request.header.message = fhtype & 0xFFFF;
220
221 request->table_len = n_entry;
222 request->table_val = table;
223
224 ndma_send_to_control (sess, &xa, sess->plumb.data);
225 }
226
227 ndmfhh_reset (fhh);
228 }
229
230 #endif /* !NDMOS_OPTION_NO_DATA_AGENT */
231