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/obd_class.h>
23 #include <linux/obd_lov.h>
25 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
29 static int lov_getinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
30 struct lov_desc *desc, uuid_t **uuids,
31 struct ptlrpc_request **request)
33 struct ptlrpc_request *req;
34 struct mds_status_req *streq;
35 struct lov_obd *lov = &obd->u.lov;
36 struct mdc_obd *mdc = &lov->mdcobd->u.mdc;
37 int rc, size[2] = {sizeof(*streq)};
40 req = ptlrpc_prep_req2(mdc->mdc_client, mdc->mdc_conn, mdc_connh,
41 MDS_LOVINFO, 1, size, NULL);
43 GOTO(out, rc = -ENOMEM);
46 streq = lustre_msg_buf(req->rq_reqmsg, 0);
47 streq->flags = HTON__u32(MDS_STATUS_LOV);
48 streq->repbuf = HTON__u32(8000);
50 /* prepare for reply */
51 req->rq_level = LUSTRE_CONN_CON;
52 size[0] = sizeof(*desc);
54 req->rq_replen = lustre_msg_size(2, size);
56 rc = ptlrpc_queue_wait(req);
57 rc = ptlrpc_check_status(req, rc);
60 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
61 *uuids = lustre_msg_buf(req->rq_repmsg, 1);
64 mdc->mdc_max_mdsize = sizeof(*desc) +
65 desc->ld_tgt_count * sizeof(uuid_t);
72 static int lov_connect(struct lustre_handle *conn, struct obd_device *obd)
74 struct ptlrpc_request *req;
75 struct lustre_handle mdc_conn;
76 struct lov_obd *lov = &obd->u.lov;
82 memcpy(&mdc_conn, conn, sizeof(mdc_conn));
85 rc = class_connect(conn, obd);
91 rc = lov_getinfo(obd, &mdc_conn, &lov->desc, &uuidarray, &req);
93 CERROR("cannot get lov info %d\n", rc);
97 if (lov->desc.ld_tgt_count > 1000) {
98 CERROR("configuration error: target count > 1000 (%d)\n",
99 lov->desc.ld_tgt_count);
100 GOTO(out, rc = -EINVAL);
103 if (strcmp(obd->obd_uuid, lov->desc.ld_uuid)) {
104 CERROR("lov uuid %s not on mds device (%s)\n",
105 obd->obd_uuid, lov->desc.ld_uuid);
106 GOTO(out, rc = -EINVAL);
109 if (req->rq_repmsg->bufcount < 2 || req->rq_repmsg->buflens[1] <
110 sizeof(uuid_t) * lov->desc.ld_tgt_count) {
111 CERROR("invalid uuid array returned\n");
112 GOTO(out, rc = -EINVAL);
115 lov->bufsize = sizeof(struct lov_tgt_desc) * lov->desc.ld_tgt_count;
116 OBD_ALLOC(lov->tgts, lov->bufsize);
118 CERROR("Out of memory\n");
119 GOTO(out, rc = -ENOMEM);
122 uuidarray = lustre_msg_buf(req->rq_repmsg, 1);
123 for (i = 0 ; i < lov->desc.ld_tgt_count; i++)
124 memcpy(lov->tgts[i].uuid, uuidarray[i], sizeof(uuid_t));
126 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
127 struct obd_device *tgt = class_uuid2obd(uuidarray[i]);
129 CERROR("Target %s not configured\n", uuidarray[i]);
130 GOTO(out_mem, rc = -EINVAL);
132 rc = obd_connect(&lov->tgts[i].conn, tgt);
134 CERROR("Target %s connect error %d\n",
142 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
144 rc2 = obd_disconnect(&lov->tgts[i].conn);
146 CERROR("BAD: Target %s disconnect error %d\n",
149 OBD_FREE(lov->tgts, lov->bufsize);
153 class_disconnect(conn);
155 ptlrpc_free_req(req);
159 static int lov_disconnect(struct lustre_handle *conn)
161 struct obd_device *obd = class_conn2obd(conn);
162 struct lov_obd *lov = &obd->u.lov;
169 for (i = 0 ; i < lov->desc.ld_tgt_count; i++) {
170 rc = obd_disconnect(&lov->tgts[i].conn);
172 CERROR("Target %s disconnect error %d\n",
173 lov->tgts[i].uuid, rc);
177 OBD_FREE(lov->tgts, lov->bufsize);
183 rc = class_disconnect(conn);
190 static int lov_setup(struct obd_device *obd, obd_count len, void *buf)
192 struct obd_ioctl_data* data = buf;
193 struct lov_obd *lov = &obd->u.lov;
197 if (data->ioc_inllen1 < 1) {
198 CERROR("osc setup requires an MDC UUID\n");
202 if (data->ioc_inllen1 > 37) {
203 CERROR("mdc UUID must be less than 38 characters\n");
207 /* FIXME: we should make a connection instead perhaps to avoid
208 the mdc from walking away? The fs guarantees this. */
209 lov->mdcobd = class_uuid2obd(data->ioc_inlbuf1);
211 CERROR("LOV %s cannot locate MDC %s\n", obd->obd_uuid,
219 static inline int lov_stripe_md_size(struct obd_device *obd)
221 struct lov_obd *lov = &obd->u.lov;
224 size = sizeof(struct lov_stripe_md) +
225 lov->desc.ld_tgt_count * sizeof(struct lov_object_id);
229 static int lov_create(struct lustre_handle *conn, struct obdo *oa, struct lov_stripe_md **ea)
233 struct obd_export *export = class_conn2export(conn);
235 struct lov_stripe_md *md;
239 CERROR("lov_create needs EA for striping information\n");
245 lov = &export->exp_obd->u.lov;
247 oa->o_easize = lov_stripe_md_size(export->exp_obd);
249 OBD_ALLOC(*ea, oa->o_easize);
255 md->lmd_size = oa->o_easize;
256 md->lmd_object_id = oa->o_id;
257 if (!md->lmd_stripe_count) {
258 md->lmd_stripe_count = lov->desc.ld_default_stripecount;
261 for (i = 0; i < md->lmd_stripe_count; i++) {
262 struct lov_stripe_md obj_md;
263 struct lov_stripe_md *obj_mdp = &obj_md;
264 /* create data objects with "parent" OA */
265 memcpy(&tmp, oa, sizeof(tmp));
266 tmp.o_easize = sizeof(struct lov_stripe_md);
267 rc = obd_create(&lov->tgts[i].conn, &tmp, &obj_mdp);
269 GOTO(out_cleanup, rc);
270 md->lmd_objects[i].l_object_id = tmp.o_id;
276 for (i2 = 0 ; i2 < i ; i2++) {
277 /* destroy already created objects here */
278 tmp.o_id = md->lmd_objects[i].l_object_id;
279 rc2 = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
281 CERROR("Failed to remove object from target %d\n",
289 static int lov_destroy(struct lustre_handle *conn, struct obdo *oa,
290 struct lov_stripe_md *ea)
294 struct obd_export *export = class_conn2export(conn);
296 struct lov_stripe_md *md;
300 CERROR("LOV requires striping ea for desctruction\n");
304 if (!export || !export->exp_obd)
307 lov = &export->exp_obd->u.lov;
310 for (i = 0; i < md->lmd_stripe_count; i++) {
311 /* create data objects with "parent" OA */
312 memcpy(&tmp, oa, sizeof(tmp));
313 oa->o_id = md->lmd_objects[i].l_object_id;
314 rc = obd_destroy(&lov->tgts[i].conn, &tmp, NULL);
316 CERROR("Error destroying object %Ld on %d\n",
324 static int lov_getattr(struct lustre_handle *conn, struct obdo *oa)
329 if (!class_conn2export(conn))
332 rc = obd_getattr(&conn->oc_dev->obd_multi_conn[0], oa);
336 static int lov_setattr(struct lustre_handle *conn, struct obdo *oa)
341 if (!class_conn2export(conn))
344 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
345 rc = obd_setattr(&conn->oc_dev->obd_multi_conn[i], oa);
348 else if (retval != rc)
349 CERROR("different results on multiple OBDs!\n");
355 static int lov_open(struct lustre_handle *conn, struct obdo *oa)
360 if (!class_conn2export(conn))
363 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
364 rc = obd_open(&conn->oc_dev->obd_multi_conn[i], oa);
367 else if (retval != rc)
368 CERROR("different results on multiple OBDs!\n");
374 static int lov_close(struct lustre_handle *conn, struct obdo *oa)
379 if (!class_conn2export(conn))
382 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
383 rc = obd_close(&conn->oc_dev->obd_multi_conn[i], oa);
386 else if (retval != rc)
387 CERROR("different results on multiple OBDs!\n");
395 /* FIXME: maybe we'll just make one node the authoritative attribute node, then
396 * we can send this 'punch' to just the authoritative node and the nodes
397 * that the punch will affect. */
398 static int lov_punch(struct lustre_handle *conn, struct obdo *oa,
399 obd_size count, obd_off offset)
404 if (!class_conn2export(conn))
407 for (i = 0; i < conn->oc_dev->obd_multi_count; i++) {
408 rc = obd_punch(&conn->oc_dev->obd_multi_conn[i], oa, count,
412 else if (retval != rc)
413 CERROR("different results on multiple OBDs!\n");
419 struct lov_callback_data {
421 wait_queue_head waitq;
424 static void lov_read_callback(struct ptlrpc_bulk_desc *desc, void *data)
426 struct lov_callback_data *cb_data = data;
428 if (atomic_dec_and_test(&cb_data->count))
429 wake_up(&cb_data->waitq);
432 static int lov_read_check_status(struct lov_callback_data *cb_data)
435 if (sigismember(&(current->pending.signal), SIGKILL) ||
436 sigismember(&(current->pending.signal), SIGTERM) ||
437 sigismember(&(current->pending.signal), SIGINT)) {
438 cb_data->flags |= PTL_RPC_FL_INTR;
441 if (atomic_read(&cb_data->count) == 0)
446 /* buffer must lie in user memory here */
447 static int lov_brw(int cmd, struct lustre_handle *conn, obd_count num_oa,
449 obd_count *oa_bufs, struct page **buf,
450 obd_size *count, obd_off *offset, obd_flag *flags,
451 bulk_callback_t callback, void *data)
453 int rc, i, page_array_offset = 0;
454 obd_off off = offset;
456 struct lov_callback_data *cb_data;
462 if (!class_conn2export(conn))
465 OBD_ALLOC(cb_data, sizeof(*cb_data));
466 if (cb_data == NULL) {
470 INIT_WAITQUEUE_HEAD(&cb_data->waitq);
471 atomic_set(&cb_data->count, 0);
473 for (i = 0; i < oa_bufs[0]; i++) {
474 struct page *current_page = buf[i];
476 struct lov_md *md = (struct lov_md *)oa[i]->inline;
477 int bufcount = oa_bufs[i];
478 // md->lmd_stripe_count
480 for (k = page_array_offset; k < bufcount + page_array_offset;
484 page_array_offset += bufcount;
487 while (off < offset + count) {
491 stripe = off / conn->oc_dev->u.lov.lov_stripe_size;
492 size = (stripe + 1) * conn->oc_dev->u.lov.lov_strip_size - off;
496 conn = stripe % conn->oc_dev->obd_multi_count;
499 atomic_inc(&cb_data->count);
500 rc = obd_brw(cmd, &conn->oc_dev->obd_multi_conn[conn],
502 &size, off, lov_read_callback, cb_data);
506 CERROR("read(off=%Lu, count=%Lu): %d\n",
507 (unsigned long long)off,
508 (unsigned long long)size, rc);
515 wait_event(&cb_data->waitq, lov_read_check_status(cb_data));
516 if (cb_data->flags & PTL_RPC_FL_INTR)
519 /* FIXME: The error handling here sucks */
521 OBD_FREE(cb_data, sizeof(*cb_data));
525 static void lov_write_finished(struct ptlrpc_bulk_desc *desc, void *data)
530 /* buffer must lie in user memory here */
531 static int filter_write(struct lustre_handle *conn, struct obdo *oa, char *buf,
532 obd_size *count, obd_off offset)
536 unsigned long retval;
539 if (!class_conn2export(conn)) {
540 CDEBUG(D_IOCTL, "invalid client %u\n", conn->oc_id);
545 file = filter_obj_open(conn->oc_dev, oa->o_id, oa->o_mode);
546 if (!file || IS_ERR(file)) {
548 return -PTR_ERR(file);
551 /* count doubles as retval */
552 retval = file->f_op->write(file, buf, *count, (loff_t *)&offset);
568 static int lov_enqueue(struct lustre_handle *conn, struct ldlm_namespace *ns,
569 struct ldlm_handle *parent_lock, __u64 *res_id,
570 __u32 type, struct ldlm_extent *extent, __u32 mode,
571 int *flags, void *data, int datalen,
572 struct ldlm_handle *lockh)
577 if (!class_conn2export(conn))
580 rc = obd_enqueue(&conn->oc_dev->obd_multi_conn[0], ns, parent_lock,
581 res_id, type, extent, mode, flags, data, datalen,
586 static int lov_cancel(struct lustre_handle *conn, __u32 mode,
587 struct ldlm_handle *lockh)
592 if (!class_conn2export(conn))
595 rc = obd_cancel(&conn->oc_dev->obd_multi_conn[0], oa);
600 struct obd_ops lov_obd_ops = {
602 o_connect: lov_connect,
603 o_disconnect: lov_disconnect,
604 o_create: lov_create,
605 o_destroy: lov_destroy,
607 o_getattr: lov_getattr,
608 o_setattr: lov_setattr,
611 o_brw: lov_pgcache_brw,
613 o_enqueue: lov_enqueue,
619 #define LOV_VERSION "v0.1"
621 static int __init lov_init(void)
623 printk(KERN_INFO "Lustre Logical Object Volume driver " LOV_VERSION
624 ", info@clusterfs.com\n");
625 return class_register_type(&lov_obd_ops, OBD_LOV_DEVICENAME);
628 static void __exit lov_exit(void)
630 class_unregister_type(OBD_LOV_DEVICENAME);
633 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
634 MODULE_DESCRIPTION("Lustre Logical Object Volume OBD driver v0.1");
635 MODULE_LICENSE("GPL");
637 module_init(lov_init);
638 module_exit(lov_exit);