1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Copyright (C) 2002 Cluster File Systems, Inc.
7 * Author: Phil Schwan <phil@off.net>
9 * This code is issued under the GNU General Public License.
10 * See the file COPYING in this distribution
14 #define DEBUG_SUBSYSTEM S_LOV
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/obd_support.h>
19 #include <linux/lustre_lib.h>
20 #include <linux/lustre_net.h>
21 #include <linux/lustre_idl.h>
22 #include <linux/lustre_mds.h>
23 #include <linux/obd_class.h>
24 #include <linux/obd_lov.h>
26 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
30 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd)
32 struct ptlrpc_request *req;
33 struct lov_obd *lov = &obd->u.lov;
34 struct lustre_handle mdc_conn;
40 rc = class_connect(conn, obd);
46 rc = obd_connect(&mdc_conn, lov->mdcobd);
48 CERROR("cannot connect to mdc: rc = %d\n", rc);
49 GOTO(out, rc = -EINVAL);
52 rc = mdc_getlovinfo(obd, &mdc_conn, &uuidarray, &req);
53 obd_disconnect(&mdc_conn);
56 CERROR("cannot get lov info %d\n", rc);
61 if (lov->desc.ld_tgt_count > 1000) {
62 CERROR("configuration error: target count > 1000 (%d)\n",
63 lov->desc.ld_tgt_count);
64 GOTO(out, rc = -EINVAL);
67 if (strcmp(obd->obd_uuid, lov->desc.ld_uuid)) {
68 CERROR("lov uuid %s not on mds device (%s)\n",
69 obd->obd_uuid, lov->desc.ld_uuid);
70 GOTO(out, rc = -EINVAL);
73 if (req->rq_repmsg->bufcount < 2 || req->rq_repmsg->buflens[1] <
74 sizeof(uuid_t) * lov->desc.ld_tgt_count) {
75 CERROR("invalid uuid array returned\n");
76 GOTO(out, rc = -EINVAL);
79 lov->bufsize = sizeof(struct lov_tgt_desc) * lov->desc.ld_tgt_count;
80 OBD_ALLOC(lov->tgts, lov->bufsize);
82 CERROR("Out of memory\n");
83 GOTO(out, rc = -ENOMEM);
86 uuidarray = lustre_msg_buf(req->rq_repmsg, 1);
87 for (i = 0 ; i < lov->desc.ld_tgt_count; i++)
88 memcpy(lov->tgts[i].uuid, uuidarray[i], sizeof(uuid_t));
90 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
91 struct obd_device *tgt = class_uuid2obd(uuidarray[i]);
93 CERROR("Target %s not configured\n", uuidarray[i]);
94 GOTO(out_mem, rc = -EINVAL);
96 rc = obd_connect(&lov->tgts[i].conn, tgt);
98 CERROR("Target %s connect error %d\n",
106 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
108 rc2 = obd_disconnect(&lov->tgts[i].conn);
110 CERROR("BAD: Target %s disconnect error %d\n",
113 OBD_FREE(lov->tgts, lov->bufsize);
117 class_disconnect(conn);
119 ptlrpc_free_req(req);
123 static int lov_disconnect(struct lustre_handle *conn)
125 struct obd_device *obd = class_conn2obd(conn);
126 struct lov_obd *lov = &obd->u.lov;
133 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
134 rc = obd_disconnect(&lov->tgts[i].conn);
136 CERROR("Target %s disconnect error %d\n",
137 lov->tgts[i].uuid, rc);
141 OBD_FREE(lov->tgts, lov->bufsize);
147 rc = class_disconnect(conn);
154 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
156 struct obd_ioctl_data* data = buf;
157 struct lov_obd *lov = &obd->u.lov;
161 if (data->ioc_inllen1 < 1) {
162 CERROR("osc setup requires an MDC UUID\n");
166 if (data->ioc_inllen1 > 37) {
167 CERROR("mdc UUID must be less than 38 characters\n");
171 /* FIXME: we should make a connection instead perhaps to avoid
172 the mdc from walking away? The fs guarantees this. */
173 lov->mdcobd = class_uuid2obd(data->ioc_inlbuf1);
175 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid,
183 static inline int lov_stripe_md_size(struct obd_device *obd)
185 struct lov_obd *lov = &obd->u.lov;
188 size = sizeof(struct lov_stripe_md) +
189 lov->desc.ld_tgt_count * sizeof(struct lov_object_id);
193 static int lov_create(struct lustre_handle *conn, struct obdo *oa, struct lov_stripe_md **ea)
197 struct obd_export *export = class_conn2export(conn);
199 struct lov_stripe_md *md;
203 CERROR("lov_create needs EA for striping information\n");
209 lov = &export->exp_obd->u.lov;
211 oa->o_easize = lov_stripe_md_size(export->exp_obd);
213 OBD_ALLOC(*ea, oa->o_easize);
219 md->lmd_size = oa->o_easize;
220 md->lmd_object_id = oa->o_id;
221 if (!md->lmd_stripe_count) {
222 md->lmd_stripe_count = lov->desc.ld_default_stripecount;
225 for (i = 0; i < md->lmd_stripe_count; i++) {
226 struct lov_stripe_md obj_md;
227 struct lov_stripe_md *obj_mdp = &obj_md;
228 /* create data objects with "parent" OA */
229 memcpy(&tmp, oa, sizeof(tmp));
230 tmp.o_easize = sizeof(struct lov_stripe_md);
231 rc = obd_create(&lov->tgts[i].conn, &tmp, &obj_mdp);
233 GOTO(out_cleanup, rc);
234 md->lmd_objects[i].l_object_id = tmp.o_id;
240 for (i2 = 0 ; i2 < i ; i2++) {
241 /* destroy already created objects here */
242 tmp.o_id = md->lmd_objects[i].l_object_id;
243 rc2 = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
245 CERROR("Failed to remove object from target %d\n",
253 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
254 struct lov_stripe_md *ea)
258 struct obd_export *export = class_conn2export(conn);
260 struct lov_stripe_md *md;
264 CERROR("LOV requires striping ea for desctruction\n");
268 if (!export || !export->exp_obd)
271 lov = &export->exp_obd->u.lov;
274 for (i = 0; i < md->lmd_stripe_count; i++) {
275 /* create data objects with "parent" OA */
276 memcpy(&tmp, oa, sizeof(tmp));
277 oa->o_id = md->lmd_objects[i].l_object_id;
278 rc = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
280 CERROR("Error destroying object %Ld on %d\n",
288 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa)
293 if (!class_conn2export(conn))
296 rc = obd_getattr(&conn->oc_dev->obd_multi_conn[0], oa);
300 static int lov_setattr(struct lustre_handle *conn, struct obdo *oa)
305 if (!class_conn2export(conn))
308 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
309 rc = obd_setattr(&conn->oc_dev->obd_multi_conn[i], oa);
312 else if (retval != rc)
313 CERROR("different results on multiple OBDs!\n");
319 static int lov_open(struct lustre_handle *conn, struct obdo *oa)
324 if (!class_conn2export(conn))
327 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
328 rc = obd_open(&conn->oc_dev->obd_multi_conn[i], oa);
331 else if (retval != rc)
332 CERROR("different results on multiple OBDs!\n");
338 static int lov_close(struct lustre_handle *conn, struct obdo *oa)
343 if (!class_conn2export(conn))
346 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
347 rc = obd_close(&conn->oc_dev->obd_multi_conn[i], oa);
350 else if (retval != rc)
351 CERROR("different results on multiple OBDs!\n");
359 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
360 * we can send this 'punch' to just the authoritative node and the nodes
361 * that the punch will affect. */
362 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
363 obd_size count, obd_off offset)
368 if (!class_conn2export(conn))
371 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
372 rc = obd_punch(&conn->oc_dev->obd_multi_conn[i], oa, count,
376 else if (retval != rc)
377 CERROR("different results on multiple OBDs!\n");
383 struct lov_callback_data {
385 wait_queue_head waitq;
388 static void lov_read_callback(struct ptlrpc_bulk_desc *desc, void *data)
390 struct lov_callback_data *cb_data = data;
392 if (atomic_dec_and_test(&cb_data->count))
393 wake_up(&cb_data->waitq);
396 static int lov_read_check_status(struct lov_callback_data *cb_data)
399 if (sigismember(&(current->pending.signal), SIGKILL) ||
400 sigismember(&(current->pending.signal), SIGTERM) ||
401 sigismember(&(current->pending.signal), SIGINT)) {
402 cb_data->flags |= PTL_RPC_FL_INTR;
405 if (atomic_read(&cb_data->count) == 0)
410 /* buffer must lie in user memory here */
411 static int lov_brw(int cmd, struct lustre_handle *conn, obd_count num_oa,
413 obd_count *oa_bufs, struct page **buf,
414 obd_size *count, obd_off *offset, obd_flag *flags,
415 bulk_callback_t callback, void *data)
417 int rc, i, page_array_offset = 0;
418 obd_off off = offset;
420 struct lov_callback_data *cb_data;
426 if (!class_conn2export(conn))
429 OBD_ALLOC(cb_data, sizeof(*cb_data));
430 if (cb_data == NULL) {
434 INIT_WAITQUEUE_HEAD(&cb_data->waitq);
435 atomic_set(&cb_data->count, 0);
437 for (i = 0; i < oa_bufs[0]; i++) {
438 struct page *current_page = buf[i];
440 struct lov_md *md = (struct lov_md *)oa[i]->inline;
441 int bufcount = oa_bufs[i];
442 // md->lmd_stripe_count
444 for (k = page_array_offset; k < bufcount + page_array_offset;
448 page_array_offset += bufcount;
451 while (off < offset + count) {
455 stripe = off / conn->oc_dev->u.lov.lov_stripe_size;
456 size = (stripe + 1) * conn->oc_dev->u.lov.lov_strip_size - off;
460 conn = stripe % conn->oc_dev->obd_multi_count;
463 atomic_inc(&cb_data->count);
464 rc = obd_brw(cmd, &conn->oc_dev->obd_multi_conn[conn],
466 &size, off, lov_read_callback, cb_data);
470 CERROR("read(off=%Lu, count=%Lu): %d\n",
471 (unsigned long long)off,
472 (unsigned long long)size, rc);
479 wait_event(&cb_data->waitq, lov_read_check_status(cb_data));
480 if (cb_data->flags & PTL_RPC_FL_INTR)
483 /* FIXME: The error handling here sucks */
485 OBD_FREE(cb_data, sizeof(*cb_data));
489 static void lov_write_finished(struct ptlrpc_bulk_desc *desc, void *data)
494 /* buffer must lie in user memory here */
495 static int filter_write(struct lustre_handle *conn, struct obdo *oa, char *buf,
496 obd_size *count, obd_off offset)
500 unsigned long retval;
503 if (!class_conn2export(conn)) {
504 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
509 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
510 if (!file || IS_ERR(file)) {
512 return -PTR_ERR(file);
515 /* count doubles as retval */
516 retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
532 static int lov_enqueue(struct lustre_handle *conn, struct ldlm_namespace *ns,
533 struct ldlm_handle *parent_lock, __u64 *res_id,
534 __u32 type, struct ldlm_extent *extent, __u32 mode,
535 int *flags, void *data, int datalen,
536 struct ldlm_handle *lockh)
541 if (!class_conn2export(conn))
544 rc = obd_enqueue(&conn->oc_dev->obd_multi_conn[0], ns, parent_lock,
545 res_id, type, extent, mode, flags, data, datalen,
550 static int lov_cancel(struct lustre_handle *conn, __u32 mode,
551 struct ldlm_handle *lockh)
556 if (!class_conn2export(conn))
559 rc = obd_cancel(&conn->oc_dev->obd_multi_conn[0], oa);
564 struct obd_ops lov_obd_ops = {
566 o_connect: lov_connect,
567 o_disconnect: lov_disconnect,
568 o_create: lov_create,
569 o_destroy: lov_destroy,
571 o_getattr: lov_getattr,
572 o_setattr: lov_setattr,
575 o_brw: lov_pgcache_brw,
577 o_enqueue: lov_enqueue,
583 #define LOV_VERSION "v0.1"
585 static int __init lov_init(void)
587 printk(KERN_INFO "Lustre Logical Object Volume driver " LOV_VERSION
588 ", info@clusterfs.com\n");
589 return class_register_type(&lov_obd_ops, OBD_LOV_DEVICENAME);
592 static void __exit lov_exit(void)
594 class_unregister_type(OBD_LOV_DEVICENAME);
597 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
598 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver v0.1");
599 MODULE_LICENSE("GPL");
601 module_init(lov_init);
602 module_exit(lov_exit);