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 */
ndmda_fh_initialize(struct ndm_session * sess)49 int ndmda_fh_initialize(struct ndm_session* sess)
50 {
51   struct ndm_data_agent* da = sess->data_acb;
52   struct ndmfhheap* fhh = &da->fhh;
53 
54   ndmfhh_initialize(fhh);
55 
56   return 0;
57 }
58 
59 /* Commission -- Get agent ready. Entire session has been initialize()d */
ndmda_fh_commission(struct ndm_session * sess)60 int ndmda_fh_commission(struct ndm_session* sess)
61 {
62   struct ndm_data_agent* da = sess->data_acb;
63   struct ndmfhheap* fhh = &da->fhh;
64 
65   da->fhh_buf = NDMOS_API_MALLOC(NDMDA_N_FHH_BUF);
66   if (!da->fhh_buf) return -1;
67   // ndmfhh_commission (fhh, da->fhh_buf, sizeof *da->fhh_buf);
68   ndmfhh_commission(fhh, da->fhh_buf, NDMDA_N_FHH_BUF);
69 
70   return 0;
71 }
72 
73 /* Decommission -- Discard agent */
ndmda_fh_decommission(struct ndm_session * sess)74 int ndmda_fh_decommission(struct ndm_session* sess) { return 0; }
75 
76 /* Destroy -- Destroy agent */
ndmda_fh_destroy(struct ndm_session * sess)77 int ndmda_fh_destroy(struct ndm_session* sess)
78 {
79   if (sess->data_acb->fhh_buf) {
80     NDMOS_API_FREE(sess->data_acb->fhh_buf);
81     sess->data_acb->fhh_buf = NULL;
82   }
83 
84   return 0;
85 }
86 
87 /* Belay -- Cancel partially issued activation/start */
ndmda_fh_belay(struct ndm_session * sess)88 int ndmda_fh_belay(struct ndm_session* sess) { return 0; }
89 
90 
91 /*
92  * Semantic actions -- called from ndmda_XXX() butype formatters
93  ****************************************************************
94  */
95 
ndmda_fh_add_file(struct ndm_session * sess,ndmp9_file_stat * filestat,char * name)96 void ndmda_fh_add_file(struct ndm_session* sess,
97                        ndmp9_file_stat* filestat,
98                        char* name)
99 {
100   struct ndm_data_agent* da = sess->data_acb;
101   int nlen = strlen(name) + 1;
102   ndmp9_file* file9;
103   int rc;
104 
105   rc = ndmda_fh_prepare(sess, NDMP9VER, NDMP9_FH_ADD_FILE, sizeof(ndmp9_file),
106                         1, nlen);
107 
108   if (rc != NDMFHH_RET_OK) return;
109 
110   file9 = ndmfhh_add_entry(&da->fhh);
111   file9->fstat = *filestat;
112   file9->unix_path = ndmfhh_save_item(&da->fhh, name, nlen);
113 }
114 
ndmda_fh_add_dir(struct ndm_session * sess,uint64_t dir_fileno,char * name,uint64_t fileno)115 void ndmda_fh_add_dir(struct ndm_session* sess,
116                       uint64_t dir_fileno,
117                       char* name,
118                       uint64_t fileno)
119 {
120   struct ndm_data_agent* da = sess->data_acb;
121   int nlen = strlen(name) + 1;
122   ndmp9_dir* dir9;
123   int rc;
124 
125   rc = ndmda_fh_prepare(sess, NDMP9VER, NDMP9_FH_ADD_DIR, sizeof(ndmp9_dir), 1,
126                         nlen);
127 
128   if (rc != NDMFHH_RET_OK) return;
129 
130   dir9 = ndmfhh_add_entry(&da->fhh);
131   dir9->unix_name = ndmfhh_save_item(&da->fhh, name, nlen);
132   dir9->parent = dir_fileno;
133   dir9->node = fileno;
134 }
135 
ndmda_fh_add_node(struct ndm_session * sess,ndmp9_file_stat * filestat)136 void ndmda_fh_add_node(struct ndm_session* sess, ndmp9_file_stat* filestat)
137 {
138   struct ndm_data_agent* da = sess->data_acb;
139   ndmp9_node* node9;
140   int rc;
141 
142   rc = ndmda_fh_prepare(sess, NDMP9VER, NDMP9_FH_ADD_NODE, sizeof(ndmp9_node),
143                         1, 0);
144 
145   if (rc != NDMFHH_RET_OK) return;
146 
147   node9 = ndmfhh_add_entry(&da->fhh);
148   node9->fstat = *filestat;
149 }
150 
151 
152 /*
153  * Helpers -- prepare/flush
154  ****************************************************************
155  */
156 
ndmda_fh_prepare(struct ndm_session * sess,int vers,int msg,int entry_size,unsigned n_item,unsigned total_size_of_items)157 int ndmda_fh_prepare(struct ndm_session* sess,
158                      int vers,
159                      int msg,
160                      int entry_size,
161                      unsigned n_item,
162                      unsigned total_size_of_items)
163 {
164   struct ndm_data_agent* da = sess->data_acb;
165   struct ndmfhheap* fhh = &da->fhh;
166   int fhtype = (vers << 16) + msg;
167   int rc;
168 
169   rc = ndmfhh_prepare(fhh, fhtype, entry_size, n_item, total_size_of_items);
170 
171   if (rc == NDMFHH_RET_OK) return NDMFHH_RET_OK;
172 
173   ndmda_fh_flush(sess);
174 
175   rc = ndmfhh_prepare(fhh, fhtype, entry_size, n_item, total_size_of_items);
176 
177   return rc;
178 }
179 
ndmda_fh_flush(struct ndm_session * sess)180 void ndmda_fh_flush(struct ndm_session* sess)
181 {
182   struct ndm_data_agent* da = sess->data_acb;
183   struct ndmfhheap* fhh = &da->fhh;
184   int rc;
185   int fhtype;
186   void* table;
187   unsigned n_entry;
188 
189   rc = ndmfhh_get_table(fhh, &fhtype, &table, &n_entry);
190   if (rc == NDMFHH_RET_OK && n_entry > 0) {
191     struct ndmp_xa_buf xa;
192     struct ndmfhh_generic_table* request;
193 
194     request = (void*)&xa.request.body;
195     NDMOS_MACRO_ZEROFILL(&xa);
196 
197     xa.request.protocol_version = fhtype >> 16;
198     xa.request.header.message = fhtype & 0xFFFF;
199 
200     request->table_len = n_entry;
201     request->table_val = table;
202 
203     ndma_send_to_control(sess, &xa, sess->plumb.data);
204   }
205 
206   ndmfhh_reset(fhh);
207 }
208 
209 #endif /* !NDMOS_OPTION_NO_DATA_AGENT */
210