~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to ubuntu/iscsitarget/volume.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Volume manager
3
 
 * (C) 2004 - 2005 FUJITA Tomonori <tomof@acm.org>
4
 
 * This code is licenced under the GPL.
5
 
 */
6
 
 
7
 
#include <linux/types.h>
8
 
#include <linux/parser.h>
9
 
 
10
 
#include "iscsi.h"
11
 
#include "iscsi_dbg.h"
12
 
#include "iotype.h"
13
 
 
14
 
struct iet_volume *volume_lookup(struct iscsi_target *target, u32 lun)
15
 
{
16
 
        struct iet_volume *volume;
17
 
 
18
 
        list_for_each_entry(volume, &target->volumes, list) {
19
 
                if (volume->lun == lun)
20
 
                        return volume;
21
 
        }
22
 
        return NULL;
23
 
}
24
 
 
25
 
enum {
26
 
        opt_type,
27
 
        opt_iomode,
28
 
        opt_scsiid,
29
 
        opt_scsisn,
30
 
        opt_blk_size,
31
 
        opt_err,
32
 
};
33
 
 
34
 
static match_table_t tokens = {
35
 
        {opt_type, "type=%s"},
36
 
        {opt_iomode, "iomode=%s"},
37
 
        {opt_scsiid, "scsiid=%s"},
38
 
        {opt_scsisn, "scsisn=%s"},
39
 
        {opt_blk_size, "blocksize=%u"},
40
 
        {opt_err, NULL},
41
 
};
42
 
 
43
 
static int set_scsiid(struct iet_volume *volume, const char *id)
44
 
{
45
 
        size_t len;
46
 
 
47
 
        if ((len = strlen(id)) > SCSI_ID_LEN) {
48
 
                eprintk("SCSI ID too long, %zd provided, %u max\n", len,
49
 
                                                                SCSI_ID_LEN);
50
 
                return -EINVAL;
51
 
        }
52
 
 
53
 
        memcpy(volume->scsi_id, id, len);
54
 
 
55
 
        return 0;
56
 
}
57
 
 
58
 
static int set_scsisn(struct iet_volume *volume, const char *sn)
59
 
{
60
 
        size_t len;
61
 
        int i;
62
 
 
63
 
        if ((len = strlen(sn)) > SCSI_SN_LEN) {
64
 
                eprintk("SCSI SN too long, %zd provided, %u max\n", len,
65
 
                                                                SCSI_SN_LEN);
66
 
                return -EINVAL;
67
 
        }
68
 
 
69
 
        for (i = 0; i < len; i++) {
70
 
                if (!isascii(*(sn + i)) || !isprint(*(sn + i))) {
71
 
                        eprintk("invalid characters in SCSI SN, %s\n",
72
 
                                "only printable ascii characters allowed!");
73
 
                        return -EINVAL;
74
 
                }
75
 
        }
76
 
 
77
 
        memcpy(volume->scsi_sn, sn, len);
78
 
 
79
 
        return 0;
80
 
}
81
 
 
82
 
/* Generate a MD5 hash of the target IQN and LUN number */
83
 
static void gen_scsiid(struct iet_volume *volume)
84
 
{
85
 
        struct hash_desc hash;
86
 
 
87
 
        hash.tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
88
 
        hash.flags = 0;
89
 
 
90
 
        if (hash.tfm) {
91
 
                struct scatterlist sg[2];
92
 
                unsigned int nbytes = 0;
93
 
 
94
 
                sg_init_table(sg, 2);
95
 
 
96
 
                sg_set_buf(&sg[0], volume->target->name,
97
 
                                        strlen(volume->target->name));
98
 
                nbytes += strlen(volume->target->name);
99
 
 
100
 
                sg_set_buf(&sg[1], &volume->lun, sizeof(volume->lun));
101
 
                nbytes += sizeof(volume->lun);
102
 
 
103
 
                crypto_hash_init(&hash);
104
 
                crypto_hash_update(&hash, sg, nbytes);
105
 
                crypto_hash_final(&hash, volume->scsi_id);
106
 
 
107
 
                crypto_free_hash(hash.tfm);
108
 
        } else {
109
 
                /* If no MD5 available set ID to TID and LUN */
110
 
                memcpy(volume->scsi_id, &volume->target->tid,
111
 
                                                sizeof(volume->target->tid));
112
 
                memcpy(volume->scsi_id + sizeof(volume->target->tid),
113
 
                                        &volume->lun, sizeof(volume->lun));
114
 
        }       
115
 
 
116
 
}
117
 
 
118
 
static int parse_volume_params(struct iet_volume *volume, char *params)
119
 
{
120
 
        int err = 0;
121
 
        unsigned blk_sz;
122
 
        substring_t args[MAX_OPT_ARGS];
123
 
        char *p, *argp = NULL, *buf = (char *) get_zeroed_page(GFP_USER);
124
 
 
125
 
        if (!buf)
126
 
                return -ENOMEM;
127
 
 
128
 
        strncpy(buf, params, PAGE_CACHE_SIZE);
129
 
 
130
 
        while ((p = strsep(&buf, ",")) != NULL) {
131
 
                int token;
132
 
 
133
 
                if (!*p)
134
 
                        continue;
135
 
                iet_strtolower(p);
136
 
                token = match_token(p, tokens, args);
137
 
                switch (token) {
138
 
                case opt_type:
139
 
                        argp = match_strdup(&args[0]);
140
 
                        if (!argp) {
141
 
                                err = -ENOMEM;
142
 
                                break;
143
 
                        }
144
 
                        if (!(volume->iotype = get_iotype(argp)))
145
 
                                err = -ENOENT;
146
 
                        kfree(argp);
147
 
                        break;
148
 
                case opt_iomode:
149
 
                        argp = match_strdup(&args[0]);
150
 
                        if (!argp) {
151
 
                                err = -ENOMEM;
152
 
                                break;
153
 
                        }
154
 
                        if (!strcmp(argp, "ro"))
155
 
                                SetLUReadonly(volume);
156
 
                        else if (!strcmp(argp, "wb"))
157
 
                                SetLUWCache(volume);
158
 
                        else if (strcmp(argp, "wt"))
159
 
                                err = -EINVAL;
160
 
                        kfree(argp);
161
 
                        break;
162
 
                case opt_scsiid:
163
 
                        argp = match_strdup(&args[0]);
164
 
                        if (!argp) {
165
 
                                err = -ENOMEM;
166
 
                                break;
167
 
                        }
168
 
                        err = set_scsiid(volume, argp);
169
 
                        kfree(argp);
170
 
                        break;
171
 
                case opt_scsisn:
172
 
                        argp = match_strdup(&args[0]);
173
 
                        if (!argp) {
174
 
                                err = -ENOMEM;
175
 
                                break;
176
 
                        }
177
 
                        err = set_scsisn(volume, argp);
178
 
                        kfree(argp);
179
 
                        break;
180
 
                case opt_blk_size:
181
 
                        argp = match_strdup(&args[0]);
182
 
                        if (!argp) {
183
 
                                err = -ENOMEM;
184
 
                                break;
185
 
                        }
186
 
                        blk_sz = simple_strtoull(argp, NULL, 10);
187
 
                        if (is_power_of_2(blk_sz) &&
188
 
                            512 <= blk_sz && blk_sz <= IET_MAX_BLOCK_SIZE)
189
 
                                volume->blk_shift = blksize_bits(blk_sz);
190
 
                        else {
191
 
                                eprintk("invalid BlockSize=%u\n", blk_sz);
192
 
                                err = -EINVAL;
193
 
                        }
194
 
                        kfree(argp);
195
 
                        break;
196
 
                default:
197
 
                        break;
198
 
                }
199
 
        }
200
 
 
201
 
        if (!err && !volume->iotype && !(volume->iotype = get_iotype("fileio"))) {
202
 
                eprintk("%s\n", "Cannot find fileio");
203
 
                err = -EINVAL;
204
 
        }
205
 
 
206
 
        free_page((unsigned long) buf);
207
 
 
208
 
        return err;
209
 
}
210
 
 
211
 
int volume_add(struct iscsi_target *target, struct volume_info *info)
212
 
{
213
 
        int ret;
214
 
        struct iet_volume *volume;
215
 
        char *args;
216
 
 
217
 
        volume = volume_lookup(target, info->lun);
218
 
        if (volume)
219
 
                return -EEXIST;
220
 
 
221
 
        if (info->lun > 0x3fff)
222
 
                return -EINVAL;
223
 
 
224
 
        volume = kzalloc(sizeof(*volume), GFP_KERNEL);
225
 
        if (!volume)
226
 
                return -ENOMEM;
227
 
 
228
 
        volume->target = target;
229
 
        volume->lun = info->lun;
230
 
 
231
 
        args = kzalloc(info->args_len + 1, GFP_KERNEL);
232
 
        if (!args) {
233
 
                ret = -ENOMEM;
234
 
                goto free_volume;
235
 
        }
236
 
 
237
 
        ret = copy_from_user(args, (void *)(unsigned long)info->args_ptr,
238
 
                             info->args_len);
239
 
        if (ret) {
240
 
                ret = -EFAULT;
241
 
                goto free_args;
242
 
        }
243
 
 
244
 
        ret = parse_volume_params(volume, args);
245
 
        if (ret < 0)
246
 
                goto free_args;
247
 
 
248
 
        ret = volume->iotype->attach(volume, args);
249
 
        if (ret < 0)
250
 
                goto free_args;
251
 
 
252
 
        if (!volume->scsi_id[0])
253
 
                gen_scsiid(volume);
254
 
 
255
 
        if (!volume->scsi_sn[0]) {
256
 
                int i;
257
 
 
258
 
                for (i = 0; i < SCSI_ID_LEN; i++)
259
 
                        snprintf(volume->scsi_sn + (i * 2), 3, "%02x",
260
 
                                                        volume->scsi_id[i]);
261
 
        }
262
 
 
263
 
        INIT_LIST_HEAD(&volume->queue.wait_list);
264
 
        spin_lock_init(&volume->queue.queue_lock);
265
 
        spin_lock_init(&volume->reserve_lock);
266
 
 
267
 
        volume->l_state = IDEV_RUNNING;
268
 
        atomic_set(&volume->l_count, 0);
269
 
 
270
 
        list_add_tail(&volume->list, &target->volumes);
271
 
        atomic_inc(&target->nr_volumes);
272
 
 
273
 
        kfree(args);
274
 
 
275
 
        return 0;
276
 
free_args:
277
 
        kfree(args);
278
 
free_volume:
279
 
        put_iotype(volume->iotype);
280
 
        kfree(volume);
281
 
 
282
 
        return ret;
283
 
}
284
 
 
285
 
void iscsi_volume_destroy(struct iet_volume *volume)
286
 
{
287
 
        assert(volume->l_state == IDEV_DEL);
288
 
        assert(!atomic_read(&volume->l_count));
289
 
 
290
 
        volume->iotype->detach(volume);
291
 
        put_iotype(volume->iotype);
292
 
        list_del(&volume->list);
293
 
        kfree(volume);
294
 
}
295
 
 
296
 
int iscsi_volume_del(struct iscsi_target *target, struct volume_info *info)
297
 
{
298
 
        struct iet_volume *volume;
299
 
 
300
 
        eprintk("%x %x\n", target->tid, info->lun);
301
 
        if (!(volume = volume_lookup(target, info->lun)))
302
 
                return -ENOENT;
303
 
 
304
 
        volume->l_state = IDEV_DEL;
305
 
        atomic_dec(&target->nr_volumes);
306
 
        if (!atomic_read(&volume->l_count))
307
 
                iscsi_volume_destroy(volume);
308
 
 
309
 
        return 0;
310
 
}
311
 
 
312
 
struct iet_volume *volume_get(struct iscsi_target *target, u32 lun)
313
 
{
314
 
        struct iet_volume *volume;
315
 
 
316
 
        if ((volume = volume_lookup(target, lun))) {
317
 
                if (volume->l_state == IDEV_RUNNING)
318
 
                        atomic_inc(&volume->l_count);
319
 
                else
320
 
                        volume = NULL;
321
 
        }
322
 
        return volume;
323
 
}
324
 
 
325
 
void volume_put(struct iet_volume *volume)
326
 
{
327
 
        if (atomic_dec_and_test(&volume->l_count) && volume->l_state == IDEV_DEL)
328
 
                iscsi_volume_destroy(volume);
329
 
}
330
 
 
331
 
int volume_reserve(struct iet_volume *volume, u64 sid)
332
 
{
333
 
        int err = 0;
334
 
 
335
 
        if (!volume)
336
 
                return -ENOENT;
337
 
 
338
 
        spin_lock(&volume->reserve_lock);
339
 
        if (volume->reserve_sid && volume->reserve_sid != sid)
340
 
                err = -EBUSY;
341
 
        else
342
 
                volume->reserve_sid = sid;
343
 
 
344
 
        spin_unlock(&volume->reserve_lock);
345
 
        return err;
346
 
}
347
 
 
348
 
int is_volume_reserved(struct iet_volume *volume, u64 sid)
349
 
{
350
 
        int err = 0;
351
 
 
352
 
        if (!volume)
353
 
                return -ENOENT;
354
 
 
355
 
        spin_lock(&volume->reserve_lock);
356
 
        if (!volume->reserve_sid || volume->reserve_sid == sid)
357
 
                err = 0;
358
 
        else
359
 
                err = -EBUSY;
360
 
 
361
 
        spin_unlock(&volume->reserve_lock);
362
 
        return err;
363
 
}
364
 
 
365
 
int volume_release(struct iet_volume *volume, u64 sid, int force)
366
 
{
367
 
        int err = 0;
368
 
 
369
 
        if (!volume)
370
 
                return -ENOENT;
371
 
 
372
 
        spin_lock(&volume->reserve_lock);
373
 
 
374
 
        if (force || volume->reserve_sid == sid)
375
 
                volume->reserve_sid = 0;
376
 
        else
377
 
                err = -EBUSY;
378
 
 
379
 
        spin_unlock(&volume->reserve_lock);
380
 
        return err;
381
 
}
382
 
 
383
 
static void iet_volume_info_show(struct seq_file *seq, struct iscsi_target *target)
384
 
{
385
 
        struct iet_volume *volume;
386
 
 
387
 
        list_for_each_entry(volume, &target->volumes, list) {
388
 
                seq_printf(seq, "\tlun:%u state:%x iotype:%s",
389
 
                           volume->lun, volume->l_state, volume->iotype->name);
390
 
                if (LUReadonly(volume))
391
 
                        seq_printf(seq, " iomode:ro");
392
 
                else if (LUWCache(volume))
393
 
                        seq_printf(seq, " iomode:wb");
394
 
                else
395
 
                        seq_printf(seq, " iomode:wt");
396
 
 
397
 
                seq_printf(seq, " blocks:%llu blocksize:%u",
398
 
                        volume->blk_cnt, 1 << volume->blk_shift);
399
 
                if (volume->iotype->show)
400
 
                        volume->iotype->show(volume, seq);
401
 
                else
402
 
                        seq_printf(seq, "\n");
403
 
        }
404
 
}
405
 
 
406
 
static int iet_volume_seq_open(struct inode *inode, struct file *file)
407
 
{
408
 
        int res;
409
 
        res = seq_open(file, &iet_seq_op);
410
 
        if (!res)
411
 
                ((struct seq_file *)file->private_data)->private =
412
 
                        iet_volume_info_show;
413
 
        return res;
414
 
}
415
 
 
416
 
struct file_operations volume_seq_fops = {
417
 
        .owner          = THIS_MODULE,
418
 
        .open           = iet_volume_seq_open,
419
 
        .read           = seq_read,
420
 
        .llseek         = seq_lseek,
421
 
        .release        = seq_release,
422
 
};