forked from gregkh/ndas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdev.c
More file actions
473 lines (418 loc) · 12.8 KB
/
sdev.c
File metadata and controls
473 lines (418 loc) · 12.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#ifndef NDAS_NO_LANSCSI
#include "linux_ver.h"
#include "inc/xplatcfg.h"
#include "inc/sal/debug.h"
#include "inc/ndasuser/ndasuser.h"
#include "registrar.h"
#include "inc/netdisk/sdev.h"
#include "inc/netdisk/conn.h"
#include "inc/raid/bitmap.h"
#include "inc/raid/raid.h"
#include "udev.h"
#define NDAS_IO_DEFAULT_TIMEOUT (60*1000)
#ifdef DEBUG
#define debug_sdev(l, x...) do {\
if(l <= DEBUG_LEVEL_SDEV) { \
sal_debug_print("SD|%d|%s|",l,__FUNCTION__); \
sal_debug_println(x); \
} \
} while(0)
#define debug_sdev_lock(l, x...) do {\
if(l <= DEBUG_LEVEL_SDEV_LOCK) { \
sal_debug_print("SDL|%d|%s|",l,__FUNCTION__); \
sal_debug_println(x); \
} \
} while(0)
#else
#define debug_sdev(l, x...) do {} while(0)
#define debug_sdev_lock(l, x...) do {} while(0)
#endif
#define MEMORY_FREE(ptr) if ( ptr ) { sal_free(ptr); ptr = NULL; }
struct ops_s *choose_op(int type)
{
#ifdef XPLAT_RAID
if ( type == NDAS_DISK_MODE_RAID0 ) {
return &raid0;
}
if ( type == NDAS_DISK_MODE_RAID1 ) {
return &raid1;
}
if ( type == NDAS_DISK_MODE_RAID4 ) {
return &raid5;
}
#endif
if ( type == NDAS_DISK_MODE_MEDIAJUKE) {
return &udev_ops;
}
//sal_assert(type == NDAS_DISK_MODE_SINGLE || type == NDAS_DISK_MODE_ATAPI);
return &udev_ops;
}
/**
* Initialize the logunit_t * data structure.
*
* old : driver_init, udev_create
* called by :
*
* To do: Create unit devices in discover and link them in here
* Use sperate function for creating logical device with single disk and multiple disk
*/
logunit_t *sdev_create(sdev_create_param_t* param)
{
ndas_error_t err;
int ret;
logunit_t *sdev = (logunit_t *) sal_malloc(sizeof(logunit_t));
if ( !sdev )
return NULL;
debug_sdev(1, "ing mode=%d", param->mode);
sal_memset(sdev, 0, sizeof(logunit_t));
#ifdef DEBUG
sdev->magic = SDEV_MAGIC;
#endif
sdev->nmd = nmd_create();
if ( !sdev->nmd) {
goto out;
}
ret = sal_spinlock_create("s", &sdev->lock_mutex);
if (!ret) {
goto out0;
}
sdev->ops = choose_op(param->mode);
sdev->info.mode = param->mode;
sdev->info.timeout = NDAS_IO_DEFAULT_TIMEOUT;
err = sdev->ops->create_disks(sdev, param); // uop_create, raid_create
if ( !NDAS_SUCCESS(err) )
goto out1;
sdev->info.enabled = 0; // not called with ndas_enabled_device
sdev->has_key = 0; /* unknown */
sdev->lock = TRUE; // locked from the beginning
err = sdev_register(sdev);
if ( !NDAS_SUCCESS(err) ) {
goto out2;
}
err = sdev->ops->init(sdev); // uop_init, r0op_init, r1op_init, r5op_init
if ( !NDAS_SUCCESS(err) ) {
goto out3;
}
debug_sdev(2, "ed sdev=%p",sdev);
return sdev;
out3:
sdev_unregister(sdev);
out2:
sdev->ops->destroy_disks(sdev); // uop_cleanup, raid_cleanup
out1:
sal_spinlock_destroy(sdev->lock_mutex);
out0:
nmd_destroy(sdev->nmd);
out:
MEMORY_FREE(sdev);
debug_sdev(1, "err = %d",err);
return NULL;
}
struct cleanup_arg {
ndas_io_done disabled_func;
void *arg;
int *wait;
};
int sdev_cleanup(logunit_t *sdev, void *arg)
{
debug_sdev(1, "slot=%d",sdev->info.slot);
// SDEV_REENTER(sdev, sdev_cleanup, sdev, NULL);
while(!sdev_lock(sdev) ) {
debug_sdev(1, "slot=%d is locked", sdev->info.slot);
sal_msleep(200); // fix me!!
}
sdev_do_cleanup(sdev);
return 0;
}
static NDAS_CALL void sdev_cleaning_up(int slot, ndas_error_t err, void *arg)
{
logunit_t *sdev = sdev_lookup_byslot(slot);
struct cleanup_arg *carg = (struct cleanup_arg *)arg;
ndas_io_done disabled_func = carg->disabled_func;
debug_sdev(1, "slot=%d",slot);
// sal_assert(sdev_is_locked(sdev));
arg = carg->arg;
// sal_assert(sdev);
sdev->ops->deinit(sdev); // uop_deinit, r0op_deinit
sdev->ops->destroy_disks(sdev); //uop_cleanup, raid_cleanup
sdev_unregister(sdev);
if ( sdev->nmd ) nmd_destroy(sdev->nmd);
if (sdev->info2) sal_free(sdev->info2);
sdev->disabled_func = disabled_func;
sdev->arg = arg;
sdev_unlock(sdev);
if ( disabled_func ) {
debug_sdev(1, "slot=%d calling disabled_func=%p",slot,disabled_func );
disabled_func(slot, err, arg);
}
sal_spinlock_destroy(sdev->lock_mutex);
sal_free(sdev);
(*carg->wait) = 0;
}
/*
* Destroy logunit_t * data structure.
* Only called by ndas_unregister_device.
* 1. disable the disks
* 2. de allocate the memory allocated to sdev->disks
* 3. unregister from the registrar
*/
void sdev_do_cleanup(logunit_t *sdev)
{
debug_sdev(2, "slot=%d",sdev->info.slot);
// sal_assert(sdev_is_locked(sdev));
if ( sdev->info.enabled ) {
struct cleanup_arg *carg;
int wait = 1;
carg = sal_malloc(sizeof(struct cleanup_arg));
if ( !carg ) {
debug_sdev(1, "OUT OF MEMORY TO clean up");
sdev_unlock(sdev);
return;// NDAS_ERROR_OUT_OF_MEMORY;
}
carg->arg = sdev->arg;
carg->disabled_func = sdev->disabled_func;
carg->wait = &wait;
sdev->arg = carg;
debug_sdev(2, " disabled_func=%p",sdev->disabled_func );
sdev->disabled_func = sdev_cleaning_up;// TODO : change and wait till notify
sdev->ops->disable(sdev, NDAS_ERROR_SHUTDOWN_IN_PROGRESS);
if ( int_wait(&wait, 1000, 0) == -1 ) {
debug_sdev(1, "fail to disable cleanly");
sdev_unlock(sdev);
sal_free(carg);
return;// NDAS_ERROR_TIME_OUT;
}
sal_free(carg);
return;
} else {
ndas_io_done disabled_func = sdev->disabled_func;
void *arg = sdev->arg;
sdev->ops->deinit(sdev); // uop_deinit, r0op_deinit
sdev->ops->destroy_disks(sdev); //uop_cleanup, raid_cleanup
sdev_unregister(sdev);
if ( sdev->nmd ) nmd_destroy(sdev->nmd);
if (sdev->info2) sal_free(sdev->info2);
sdev_unlock(sdev);
if ( disabled_func ) {
debug_sdev(2, "slot=%d calling disabled_func=%p",sdev->info.slot,disabled_func );
disabled_func(sdev->info.slot, NDAS_ERROR_SHUTDOWN_IN_PROGRESS, arg);
}
sal_spinlock_destroy(sdev->lock_mutex);
sal_free(sdev);
}
}
static
void sdev_do_disable(logunit_t *sdev)
{
debug_sdev(2, "slot=%d", sdev->info.slot);
if ( !sdev->info.enabled ) {
ndas_io_done disabled_func = sdev->disabled_func;
sdev_unlock(sdev);
if ( disabled_func )
disabled_func(sdev->info.slot, NDAS_ERROR_ALREADY_DISABLED_DEVICE, sdev->arg);
return;
}
//uop_disable , jbod_op_disable, rddc_op_disable
sdev->ops->disable(sdev, NDAS_ERROR_SHUTDOWN_IN_PROGRESS);
}
int sdev_disable(logunit_t *sdev, void * reserved)
{
debug_sdev(2, "slot=%d", sdev->info.slot);
SDEV_REENTER(sdev, sdev_disable, sdev, NULL);
sdev_do_disable(sdev);
return 0;
}
#ifdef XPLAT_RAID
void sdev_queue_write_rmd(logunit_t *sdev)
{
int i = 0;
debug_sdev(2, "sdev=%p",sdev);
// sal_assert(sdev);
// sal_assert(sdev->nmd);
// sal_assert(sdev->nmd->rmd);
debug_sdev(2, "sdev->nmd->rmd=%p",sdev->nmd->rmd);
sdev->nmd->rmd->u.s.usn++;
for ( i = 0; i < SDEV_RAID_INFO(sdev)->disks; i++) {
if ( !sdev->disks[i] || !sdev->disks[i]->accept_io )
continue;
udev_queue_write_rmd(SDEV2UDEV(sdev->disks[i]), 0, sdev->nmd->rmd);
debug_sdev(2, "1st [%d]child=%p {status=%x}",i,sdev->disks[i],
sdev->nmd->rmd->UnitMetaData[i].UnitDeviceStatus);
}
for ( i = 0; i < SDEV_RAID_INFO(sdev)->disks; i++)
{
if ( !sdev->disks[i] || !sdev->disks[i]->accept_io )
continue;
udev_queue_write_rmd(SDEV2UDEV(sdev->disks[i]), 1, sdev->nmd->rmd);
debug_sdev(2, "2nd [%d]child=%p",i,sdev->disks[i]);
}
}
#endif
ndas_metadata_t *nmd_create()
{
ndas_metadata_t *ret = sal_malloc(sizeof(ndas_metadata_t));
if ( !ret ) return NULL;
#ifdef XPLAT_RAID
ret->rmd = NULL;
ret->bitmap = NULL;
ret->rmd_usn_match = 0;
#endif
ret->dib = sal_malloc(sizeof(NDAS_DIB_V2));
if ( !ret->dib ) goto out;
return ret;
out:
sal_free(ret);
return NULL;
}
#ifdef XPLAT_RAID
ndas_error_t nmd_init_rmd(ndas_metadata_t *nmd)
{
debug_sdev(3, "ing nmd=%p", nmd);
// sal_assert(nmd->dib);
// sal_assert(nmd->rmd == NULL);
nmd->rmd = sal_malloc(sizeof(NDAS_RAID_META_DATA));
if ( !nmd->rmd ) {
return NDAS_ERROR_OUT_OF_MEMORY;
}
debug_sdev(3, "nmd->rmd=%p", nmd->rmd);
return NDAS_OK;
}
void nmd_free_bitmap(ndas_metadata_t *nmd)
{
if ( nmd->bitmap ) bitmap_destroy(nmd->bitmap);
}
ndas_error_t nmd_init_bitmap(ndas_metadata_t *nmd)
{
xuint32 size;
debug_sdev(3, "ing nmd=%p", nmd);
// sal_assert(nmd->dib);
// sal_assert(nmd->bitmap == NULL);
if ( sal_memcmp(nmd->dib->u.s.Signature,NDAS_DIB_V2_SIGNATURE,8) != 0 )
return NDAS_ERROR_INVALID_METADATA;
size = nmd->dib->u.s.sizeUserSpace >> ( bitmap_ffb(nmd->dib->u.s.iSectorsPerBit) + 3);
nmd->bitmap = bitmap_create(size);
debug_sdev(3, "nmd->bitmap=%p", nmd->bitmap);
if ( !nmd->bitmap )
return NDAS_ERROR_OUT_OF_MEMORY;
debug_sdev(3, "nmd->rmd=%p", nmd->rmd);
return NDAS_OK;
}
#endif
void nmd_destroy(ndas_metadata_t *nmd)
{
if ( !nmd ) return;
#ifdef XPLAT_RAID
debug_sdev(3, "nmd->rmd=%p", nmd->rmd);
if ( nmd->rmd ) sal_free(nmd->rmd);
debug_sdev(3, "nmd->bitmap=%p", nmd->bitmap);
if ( nmd->bitmap ) bitmap_destroy(nmd->bitmap);
#endif
debug_sdev(3, "nmd->dib=%p", nmd->dib);
if ( nmd->dib ) sal_free(nmd->dib);
debug_sdev(3, "nmd=%p", nmd);
sal_free(nmd);
}
xbool sdev_lock(logunit_t *sdev)
{
sal_spinlock_take_softirq(sdev->lock_mutex);
if ( sdev->lock ) {
debug_sdev_lock(1,"slot=%d conflict", sdev->info.slot);
sal_spinlock_give_softirq(sdev->lock_mutex);
return FALSE;
}
sdev->lock = TRUE;
debug_sdev_lock(2,"ed slot=%d", sdev->info.slot);
sal_spinlock_give_softirq(sdev->lock_mutex);
return TRUE;
}
void sdev_notify_disable_result(logunit_t *sdev, ndas_error_t err)
{
ndas_io_done disabled_func = sdev->disabled_func;
/* Bug
1. enable
2. meet bad sector
3. enable again
3. meet bad sector
no disable_func set detected*/
//sdev->disabled_func = NULL;
if ( !slot_is_the_raid(&sdev->info)) {
sdev_set_disabled(sdev);
sdev_unlock(sdev);
}
if ( disabled_func ) {
/* ndcmd_disabled_handler, jb_slot_disabled, rd_slot_disabled */
disabled_func(sdev->info.slot, err, sdev->arg);
} else {
debug_sdev(1, "No disabled_func for slot %d (%p)", sdev->info.slot, sdev);
}
}
void sdev_raid_notify_disable_result(logunit_t *sdev, ndas_error_t err)
{
sdev_notify_disable_result(sdev, err);
}
struct notify_arg {
ndas_io_done noti_func;
void *arg;
logunit_t *sdev;
};
int notify_func(void *a, void *b)
{
struct notify_arg *arg = a;
long _b = (long)b;
ndas_error_t err = (ndas_error_t)_b;
arg->noti_func(arg->sdev->info.slot, err, arg->arg);
sal_free(arg);
return 0;
}
void sdev_notify_enable_result(logunit_t *sdev, ndas_error_t err)
{
ndas_io_done enabled_func = sdev->enabled_func;
void *enabled_arg = sdev->arg;
dpc_id dpcid;
int ret;
#ifdef XPLAT_RAID
if ( NDAS_SUCCESS(err) )
{
sdev_set_enabled(sdev);
sdev_accept_io(sdev);
if ( !slot_is_the_raid(&sdev->info) ) {
//sal_error_print("ndas: slot %d is ready to use\n", sdev->info.slot);
}
}
if ( !slot_is_the_raid(&sdev->info) ) {
sdev_unlock(sdev);
}
#else
if ( NDAS_SUCCESS(err) ) {
sdev_set_enabled(sdev);
sdev_accept_io(sdev);
//sal_error_print("ndas: slot %d is ready to use\n", sdev->info.slot);
} else {
//sal_error_print("ndas: slot %d can't be enabled: %s\n", sdev->info.slot,
// NDAS_GET_STRING_ERROR(err));
}
sdev_unlock(sdev);
#endif
if (enabled_func) {
struct notify_arg *arg = sal_malloc(sizeof(struct notify_arg));
if ( arg == NULL ) {
/* ndcmd_enabled_handler in Linux */
enabled_func(sdev->info.slot, NDAS_ERROR_OUT_OF_MEMORY, enabled_arg);
return;
}
arg->noti_func = enabled_func;
arg->arg = enabled_arg;
arg->sdev = sdev;
dpcid = bpc_create(notify_func, arg, (void*)(long)err, NULL, 0);
// sal_assert(dpcid);
if(dpcid) {
ret = bpc_invoke(dpcid);
// sal_assert(ret >= 0);
}
}
else {
debug_sdev(1, "No enabled_func for slot %d (%p)", sdev->info.slot, sdev);
}
return;
}
#endif /* #ifndef NDAS_NO_LANSCSI */