1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_MDC
26 #include <linux/module.h>
27 #include <linux/miscdevice.h>
28 #include <linux/lustre_mds.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/init.h>
32 #include <linux/obd_lov.h>
34 #define REQUEST_MINOR 244
36 extern int mds_queue_req(struct ptlrpc_request *);
38 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
39 __u64 *last_committed, __u64 *last_xid,
40 struct ptlrpc_request **request)
42 struct ptlrpc_request *req;
43 struct mds_body *body;
44 int rc, size = sizeof(*body);
47 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETSTATUS, 1, &size,
50 GOTO(out, rc = -ENOMEM);
52 body = lustre_msg_buf(req->rq_reqmsg, 0);
53 req->rq_level = LUSTRE_CONN_CON;
54 req->rq_replen = lustre_msg_size(1, &size);
56 mds_pack_req_body(req);
57 rc = ptlrpc_queue_wait(req);
58 rc = ptlrpc_check_status(req, rc);
61 body = lustre_msg_buf(req->rq_repmsg, 0);
62 mds_unpack_body(body);
63 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
64 *last_committed = req->rq_repmsg->last_committed;
65 *last_xid = req->rq_repmsg->last_xid;
67 CDEBUG(D_NET,"root ino=%ld, last_committed=%Lu, last_xid=%Ld\n",
68 (unsigned long)rootfid->id,
69 (unsigned long long)*last_committed,
70 (unsigned long long)*last_xid);
75 ptlrpc_req_finished(req);
79 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
80 struct ptlrpc_request **request)
82 struct ptlrpc_request *req;
83 struct mds_status_req *streq;
84 int rc, size[2] = {sizeof(*streq)};
87 req = ptlrpc_prep_req(class_conn2cliimp(mdc_connh), MDS_GETLOVINFO, 1,
90 GOTO(out, rc = -ENOMEM);
93 streq = lustre_msg_buf(req->rq_reqmsg, 0);
94 streq->flags = HTON__u32(MDS_STATUS_LOV);
95 streq->repbuf = HTON__u32(8192);
97 /* prepare for reply */
98 req->rq_level = LUSTRE_CONN_CON;
101 req->rq_replen = lustre_msg_size(2, size);
103 rc = ptlrpc_queue_wait(req);
104 rc = ptlrpc_check_status(req, rc);
111 int mdc_getattr(struct lustre_handle *conn,
112 obd_id ino, int type, unsigned long valid, size_t ea_size,
113 struct ptlrpc_request **request)
115 struct ptlrpc_request *req;
116 struct mds_body *body;
117 int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
120 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR, 1, size,
123 GOTO(out, rc = -ENOMEM);
125 body = lustre_msg_buf(req->rq_reqmsg, 0);
126 ll_ino2fid(&body->fid1, ino, 0, type);
130 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
132 size[1] = mdc->cl_max_mds_easize;
133 } else if (valid & OBD_MD_LINKNAME) {
136 body->size = ea_size;
137 CDEBUG(D_INODE, "allocating %d bytes for symlink in packet\n",
140 req->rq_replen = lustre_msg_size(bufcount, size);
141 mds_pack_req_body(req);
143 rc = ptlrpc_queue_wait(req);
144 rc = ptlrpc_check_status(req, rc);
147 body = lustre_msg_buf(req->rq_repmsg, 0);
148 mds_unpack_body(body);
149 CDEBUG(D_NET, "mode: %o\n", body->mode);
158 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
159 void *data, __u32 data_len, int flag)
162 struct inode *inode = data;
163 struct lustre_handle lockh;
166 if (data_len != sizeof(*inode)) {
167 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
174 case LDLM_CB_BLOCKING:
175 ldlm_lock2handle(lock, &lockh);
176 rc = ldlm_cli_cancel(&lockh);
178 CERROR("ldlm_cli_cancel: %d\n", rc);
182 case LDLM_CB_CANCELING:
183 /* FIXME: do something better than throwing away everything */
186 if (S_ISDIR(inode->i_mode)) {
187 CDEBUG(D_INODE, "invalidating inode %ld\n",
189 invalidate_inode_pages(inode);
199 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
200 struct lookup_intent *it, int lock_mode, struct inode *dir,
201 struct dentry *de, struct lustre_handle *lockh,
202 char *tgt, int tgtlen, void *data, int datalen)
204 struct ptlrpc_request *req;
205 struct obd_device *obddev = class_conn2obd(conn);
206 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
207 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
209 int repsize[3] = {sizeof(struct ldlm_reply),
210 sizeof(struct mds_body),
211 obddev->u.cli.cl_max_mds_easize};
212 struct ldlm_reply *dlm_rep;
213 struct ldlm_intent *lit;
216 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
221 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
223 case (IT_CREAT|IT_OPEN):
225 it->it_mode |= S_IFREG; /* no break */
227 it->it_mode &= ~current->fs->umask;
230 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
234 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
235 size[2] = sizeof(struct mds_rec_create);
236 size[3] = de->d_name.len + 1;
237 size[4] = tgtlen + 1;
238 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
243 /* pack the intent */
244 lit = lustre_msg_buf(req->rq_reqmsg, 1);
245 lit->opc = NTOH__u64((__u64)it->it_op);
247 /* pack the intended request */
248 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
249 current->fsgid, CURRENT_TIME, de->d_name.name,
250 de->d_name.len, tgt, tgtlen);
251 req->rq_replen = lustre_msg_size(3, repsize);
252 } else if (it->it_op == IT_RENAME2) {
253 struct dentry *old_de = it->it_data;
255 size[2] = sizeof(struct mds_rec_rename);
256 size[3] = old_de->d_name.len + 1;
257 size[4] = de->d_name.len + 1;
258 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
263 /* pack the intent */
264 lit = lustre_msg_buf(req->rq_reqmsg, 1);
265 lit->opc = NTOH__u64((__u64)it->it_op);
267 /* pack the intended request */
268 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
269 old_de->d_name.name, old_de->d_name.len,
270 de->d_name.name, de->d_name.len);
271 req->rq_replen = lustre_msg_size(3, repsize);
272 } else if (it->it_op == IT_LINK2) {
273 struct dentry *old_de = it->it_data;
275 size[2] = sizeof(struct mds_rec_link);
276 size[3] = de->d_name.len + 1;
277 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
282 /* pack the intent */
283 lit = lustre_msg_buf(req->rq_reqmsg, 1);
284 lit->opc = NTOH__u64((__u64)it->it_op);
286 /* pack the intended request */
287 mds_link_pack(req, 2, old_de->d_inode, dir,
288 de->d_name.name, de->d_name.len);
289 req->rq_replen = lustre_msg_size(3, repsize);
290 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
291 size[2] = sizeof(struct mds_rec_unlink);
292 size[3] = de->d_name.len + 1;
293 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
298 /* pack the intent */
299 lit = lustre_msg_buf(req->rq_reqmsg, 1);
300 lit->opc = NTOH__u64((__u64)it->it_op);
302 /* pack the intended request */
303 mds_unlink_pack(req, 2, dir, NULL,
304 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
305 de->d_name.name, de->d_name.len);
307 req->rq_replen = lustre_msg_size(3, repsize);
308 } else if (it->it_op & (IT_GETATTR | IT_RENAME | IT_LINK |
309 IT_OPEN | IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
310 size[2] = sizeof(struct mds_body);
311 size[3] = de->d_name.len + 1;
313 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
318 /* pack the intent */
319 lit = lustre_msg_buf(req->rq_reqmsg, 1);
320 lit->opc = NTOH__u64((__u64)it->it_op);
322 /* pack the intended request */
323 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
325 /* we need to replay opens */
326 if (it->it_op == IT_OPEN)
327 req->rq_flags |= PTL_RPC_FL_REPLAY;
329 /* get ready for the reply */
330 req->rq_replen = lustre_msg_size(3, repsize);
331 } else if (it->it_op == IT_READDIR) {
332 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
337 /* get ready for the reply */
338 req->rq_replen = lustre_msg_size(1, repsize);
343 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
344 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
345 lock_type, NULL, 0, lock_mode, &flags,
346 ldlm_completion_ast, mdc_blocking_ast, data,
349 /* This can go when we're sure that this can never happen */
352 if (rc == ELDLM_LOCK_ABORTED) {
354 memset(lockh, 0, sizeof(*lockh));
356 } else if (rc != 0) {
357 CERROR("ldlm_cli_enqueue: %d\n", rc);
361 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
362 it->it_disposition = (int) dlm_rep->lock_policy_res1;
363 it->it_status = (int) dlm_rep->lock_policy_res2;
364 it->it_lock_mode = lock_mode;
370 static void mdc_replay_open(struct ptlrpc_request *req,
371 struct lustre_handle *data)
373 struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
375 mds_unpack_body(body);
376 CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
377 data->addr, data->cookie, body->handle.addr, body->handle.cookie);
378 memcpy(data, &body->handle, sizeof(*data));
381 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
382 struct lov_stripe_md *lsm, struct lustre_handle *fh,
383 struct ptlrpc_request **request)
385 struct mds_body *body;
386 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
387 struct ptlrpc_request *req;
392 // size[1] = mdc->cl_max_mds_easize; soon...
393 size[1] = lsm->lsm_mds_easize;
396 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
399 GOTO(out, rc = -ENOMEM);
401 req->rq_flags |= PTL_RPC_FL_REPLAY;
402 body = lustre_msg_buf(req->rq_reqmsg, 0);
404 ll_ino2fid(&body->fid1, ino, 0, type);
405 body->flags = HTON__u32(flags);
406 memcpy(&body->handle, fh, sizeof(body->handle));
409 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), lsm);
411 req->rq_replen = lustre_msg_size(1, size);
413 rc = ptlrpc_queue_wait(req);
414 rc = ptlrpc_check_status(req, rc);
416 body = lustre_msg_buf(req->rq_repmsg, 0);
417 mds_unpack_body(body);
418 memcpy(fh, &body->handle, sizeof(*fh));
421 /* If open is replayed, we need to fix up the fh. */
422 req->rq_replay_cb = mdc_replay_open;
423 memcpy(&req->rq_replay_cb_handle, fh, sizeof(req->rq_replay_cb_handle));
431 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
432 struct lustre_handle *fh, struct ptlrpc_request **request)
434 struct mds_body *body;
435 int rc, size = sizeof(*body);
436 struct ptlrpc_request *req;
438 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
441 GOTO(out, rc = -ENOMEM);
443 body = lustre_msg_buf(req->rq_reqmsg, 0);
444 ll_ino2fid(&body->fid1, ino, 0, type);
445 memcpy(&body->handle, fh, sizeof(body->handle));
447 req->rq_replen = lustre_msg_size(0, NULL);
449 rc = ptlrpc_queue_wait(req);
450 rc = ptlrpc_check_status(req, rc);
458 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
459 char *addr, struct ptlrpc_request **request)
461 struct ptlrpc_connection *connection =
462 client_conn2cli(conn)->cl_import.imp_connection;
463 struct ptlrpc_request *req = NULL;
464 struct ptlrpc_bulk_desc *desc = NULL;
465 struct ptlrpc_bulk_page *bulk = NULL;
466 struct mds_body *body;
467 int rc, size = sizeof(*body);
470 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
472 desc = ptlrpc_prep_bulk(connection);
474 GOTO(out, rc = -ENOMEM);
476 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
479 GOTO(out2, rc = -ENOMEM);
481 bulk = ptlrpc_prep_bulk_page(desc);
482 bulk->bp_buflen = PAGE_SIZE;
484 bulk->bp_xid = req->rq_xid;
485 desc->bd_portal = MDS_BULK_PORTAL;
487 rc = ptlrpc_register_bulk(desc);
489 CERROR("couldn't setup bulk sink: error %d.\n", rc);
493 body = lustre_msg_buf(req->rq_reqmsg, 0);
495 body->fid1.f_type = type;
498 req->rq_replen = lustre_msg_size(1, &size);
499 rc = ptlrpc_queue_wait(req);
500 rc = ptlrpc_check_status(req, rc);
502 ptlrpc_abort_bulk(desc);
505 body = lustre_msg_buf(req->rq_repmsg, 0);
506 mds_unpack_body(body);
511 ptlrpc_free_bulk(desc);
517 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
518 struct ptlrpc_request **request)
520 struct ptlrpc_request *req;
521 int rc, size = sizeof(*osfs);
524 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
527 GOTO(out, rc = -ENOMEM);
528 req->rq_replen = lustre_msg_size(1, &size);
530 rc = ptlrpc_queue_wait(req);
531 rc = ptlrpc_check_status(req, rc);
536 obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
545 struct obd_ops mdc_obd_ops = {
546 o_setup: client_obd_setup,
547 o_cleanup: client_obd_cleanup,
548 o_connect: client_obd_connect,
549 o_disconnect: client_obd_disconnect,
552 static int __init ptlrpc_request_init(void)
554 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
557 static void __exit ptlrpc_request_exit(void)
559 class_unregister_type(LUSTRE_MDC_NAME);
562 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
563 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
564 MODULE_LICENSE("GPL");
566 EXPORT_SYMBOL(mdc_getstatus);
567 EXPORT_SYMBOL(mdc_getlovinfo);
568 EXPORT_SYMBOL(mdc_enqueue);
569 EXPORT_SYMBOL(mdc_getattr);
570 EXPORT_SYMBOL(mdc_statfs);
571 EXPORT_SYMBOL(mdc_create);
572 EXPORT_SYMBOL(mdc_unlink);
573 EXPORT_SYMBOL(mdc_rename);
574 EXPORT_SYMBOL(mdc_link);
575 EXPORT_SYMBOL(mdc_readpage);
576 EXPORT_SYMBOL(mdc_setattr);
577 EXPORT_SYMBOL(mdc_close);
578 EXPORT_SYMBOL(mdc_open);
580 module_init(ptlrpc_request_init);
581 module_exit(ptlrpc_request_exit);