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);
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);
128 mds_pack_req_body(req);
131 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
133 size[1] = mdc->cl_max_mds_easize;
134 } else if (valid & OBD_MD_LINKNAME) {
138 req->rq_replen = lustre_msg_size(bufcount, size);
140 rc = ptlrpc_queue_wait(req);
141 rc = ptlrpc_check_status(req, rc);
144 body = lustre_msg_buf(req->rq_repmsg, 0);
145 mds_unpack_body(body);
146 CDEBUG(D_NET, "mode: %o\n", body->mode);
155 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
156 void *data, __u32 data_len)
159 struct inode *inode = data;
160 struct lustre_handle lockh;
163 if (data_len != sizeof(*inode)) {
164 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
173 /* FIXME: do something better than throwing away everything */
174 if (S_ISDIR(inode->i_mode)) {
175 CDEBUG(D_INODE, "invalidating inode %ld\n",
177 invalidate_inode_pages(inode);
180 ldlm_lock2handle(lock, &lockh);
181 rc = ldlm_cli_cancel(&lockh);
183 CERROR("ldlm_cli_cancel: %d\n", rc);
189 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
190 struct lookup_intent *it, int lock_mode, struct inode *dir,
191 struct dentry *de, struct lustre_handle *lockh,
192 char *tgt, int tgtlen, void *data, int datalen)
194 struct ptlrpc_request *req;
195 struct obd_device *obddev = class_conn2obd(conn);
196 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
197 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
199 int repsize[3] = {sizeof(struct ldlm_reply),
200 sizeof(struct mds_body),
201 obddev->u.cli.cl_max_mds_easize};
202 struct ldlm_reply *dlm_rep;
203 struct ldlm_intent *lit;
206 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
211 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
213 case (IT_CREAT|IT_OPEN):
215 it->it_mode |= S_IFREG; /* no break */
217 it->it_mode &= ~current->fs->umask;
220 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
224 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
225 size[2] = sizeof(struct mds_rec_create);
226 size[3] = de->d_name.len + 1;
227 size[4] = tgtlen + 1;
228 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
233 /* pack the intent */
234 lit = lustre_msg_buf(req->rq_reqmsg, 1);
235 lit->opc = NTOH__u64((__u64)it->it_op);
237 /* pack the intended request */
238 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
239 current->fsgid, CURRENT_TIME, de->d_name.name,
240 de->d_name.len, tgt, tgtlen);
241 req->rq_replen = lustre_msg_size(3, repsize);
242 } else if (it->it_op == IT_RENAME2) {
243 struct dentry *old_de = it->it_data;
245 size[2] = sizeof(struct mds_rec_rename);
246 size[3] = old_de->d_name.len + 1;
247 size[4] = de->d_name.len + 1;
248 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
253 /* pack the intent */
254 lit = lustre_msg_buf(req->rq_reqmsg, 1);
255 lit->opc = NTOH__u64((__u64)it->it_op);
257 /* pack the intended request */
258 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
259 old_de->d_name.name, old_de->d_name.len,
260 de->d_name.name, de->d_name.len);
261 req->rq_replen = lustre_msg_size(3, repsize);
262 } else if (it->it_op == IT_LINK2) {
263 struct dentry *old_de = it->it_data;
265 size[2] = sizeof(struct mds_rec_link);
266 size[3] = de->d_name.len + 1;
267 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
272 /* pack the intent */
273 lit = lustre_msg_buf(req->rq_reqmsg, 1);
274 lit->opc = NTOH__u64((__u64)it->it_op);
276 /* pack the intended request */
277 mds_link_pack(req, 2, old_de->d_inode, dir,
278 de->d_name.name, de->d_name.len);
279 req->rq_replen = lustre_msg_size(3, repsize);
280 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
281 size[2] = sizeof(struct mds_rec_unlink);
282 size[3] = de->d_name.len + 1;
283 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
288 /* pack the intent */
289 lit = lustre_msg_buf(req->rq_reqmsg, 1);
290 lit->opc = NTOH__u64((__u64)it->it_op);
292 /* pack the intended request */
293 mds_unlink_pack(req, 2, dir, NULL,
294 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
295 de->d_name.name, de->d_name.len);
297 req->rq_replen = lustre_msg_size(3, repsize);
298 } else if (it->it_op & (IT_GETATTR | IT_RENAME | IT_LINK |
299 IT_OPEN | IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
300 size[2] = sizeof(struct mds_body);
301 size[3] = de->d_name.len + 1;
303 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
308 /* pack the intent */
309 lit = lustre_msg_buf(req->rq_reqmsg, 1);
310 lit->opc = NTOH__u64((__u64)it->it_op);
312 /* pack the intended request */
313 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
315 /* we need to replay opens */
316 if (it->it_op == IT_OPEN)
317 req->rq_flags |= PTL_RPC_FL_REPLAY;
319 /* get ready for the reply */
320 req->rq_replen = lustre_msg_size(3, repsize);
321 } else if (it->it_op == IT_READDIR) {
322 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
327 /* get ready for the reply */
328 req->rq_replen = lustre_msg_size(1, repsize);
333 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
334 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
335 lock_type, NULL, 0, lock_mode, &flags,
336 ldlm_completion_ast, mdc_blocking_ast, data,
339 /* This can go when we're sure that this can never happen */
342 if (rc == ELDLM_LOCK_ABORTED) {
344 memset(lockh, 0, sizeof(*lockh));
346 } else if (rc != 0) {
347 CERROR("ldlm_cli_enqueue: %d\n", rc);
351 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
352 it->it_disposition = (int) dlm_rep->lock_policy_res1;
353 it->it_status = (int) dlm_rep->lock_policy_res2;
354 it->it_lock_mode = lock_mode;
360 static void mdc_replay_open(struct ptlrpc_request *req, void *data)
363 struct mds_body *body;
365 body = lustre_msg_buf(req->rq_repmsg, 0);
366 mds_unpack_body(body);
370 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
371 struct lov_stripe_md *lsm, __u64 cookie, __u64 *fh,
372 struct ptlrpc_request **request)
374 struct mds_body *body;
375 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
376 struct ptlrpc_request *req;
381 // size[1] = mdc->cl_max_mds_easize; soon...
382 size[1] = lsm->lsm_mds_easize;
385 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
388 GOTO(out, rc = -ENOMEM);
390 req->rq_flags |= PTL_RPC_FL_REPLAY;
391 body = lustre_msg_buf(req->rq_reqmsg, 0);
393 ll_ino2fid(&body->fid1, ino, 0, type);
394 body->flags = HTON__u32(flags);
395 body->extra = cookie;
398 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), lsm);
400 req->rq_replen = lustre_msg_size(1, size);
402 rc = ptlrpc_queue_wait(req);
403 rc = ptlrpc_check_status(req, rc);
405 body = lustre_msg_buf(req->rq_repmsg, 0);
406 mds_unpack_body(body);
410 /* If open is replayed, we need to fix up the fh. */
411 req->rq_replay_cb = mdc_replay_open;
412 req->rq_replay_cb_data = fh;
420 int mdc_close(struct lustre_handle *conn,
421 obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
423 struct mds_body *body;
424 int rc, size = sizeof(*body);
425 struct ptlrpc_request *req;
427 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
430 GOTO(out, rc = -ENOMEM);
432 body = lustre_msg_buf(req->rq_reqmsg, 0);
433 ll_ino2fid(&body->fid1, ino, 0, type);
436 req->rq_replen = lustre_msg_size(0, NULL);
438 rc = ptlrpc_queue_wait(req);
439 rc = ptlrpc_check_status(req, rc);
447 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
448 char *addr, struct ptlrpc_request **request)
450 struct ptlrpc_connection *connection =
451 client_conn2cli(conn)->cl_import.imp_connection;
452 struct ptlrpc_request *req = NULL;
453 struct ptlrpc_bulk_desc *desc = NULL;
454 struct ptlrpc_bulk_page *bulk = NULL;
455 struct mds_body *body;
456 int rc, size = sizeof(*body);
459 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
461 desc = ptlrpc_prep_bulk(connection);
463 GOTO(out, rc = -ENOMEM);
465 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
468 GOTO(out2, rc = -ENOMEM);
470 bulk = ptlrpc_prep_bulk_page(desc);
471 bulk->bp_buflen = PAGE_SIZE;
473 bulk->bp_xid = req->rq_xid;
474 desc->bd_portal = MDS_BULK_PORTAL;
476 rc = ptlrpc_register_bulk(desc);
478 CERROR("couldn't setup bulk sink: error %d.\n", rc);
482 body = lustre_msg_buf(req->rq_reqmsg, 0);
484 body->fid1.f_type = type;
487 req->rq_replen = lustre_msg_size(1, &size);
488 rc = ptlrpc_queue_wait(req);
489 rc = ptlrpc_check_status(req, rc);
491 ptlrpc_abort_bulk(desc);
494 body = lustre_msg_buf(req->rq_repmsg, 0);
495 mds_unpack_body(body);
500 ptlrpc_free_bulk(desc);
506 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
507 struct ptlrpc_request **request)
509 struct ptlrpc_request *req;
510 int rc, size = sizeof(*osfs);
513 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
516 GOTO(out, rc = -ENOMEM);
517 req->rq_replen = lustre_msg_size(1, &size);
519 rc = ptlrpc_queue_wait(req);
520 rc = ptlrpc_check_status(req, rc);
525 obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
534 struct obd_ops mdc_obd_ops = {
535 o_setup: client_obd_setup,
536 o_cleanup: client_obd_cleanup,
537 o_connect: client_obd_connect,
538 o_disconnect: client_obd_disconnect,
541 static int __init ptlrpc_request_init(void)
543 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
546 static void __exit ptlrpc_request_exit(void)
548 class_unregister_type(LUSTRE_MDC_NAME);
551 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
552 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
553 MODULE_LICENSE("GPL");
555 EXPORT_SYMBOL(mdc_getstatus);
556 EXPORT_SYMBOL(mdc_getlovinfo);
557 EXPORT_SYMBOL(mdc_enqueue);
558 EXPORT_SYMBOL(mdc_getattr);
559 EXPORT_SYMBOL(mdc_statfs);
560 EXPORT_SYMBOL(mdc_create);
561 EXPORT_SYMBOL(mdc_unlink);
562 EXPORT_SYMBOL(mdc_rename);
563 EXPORT_SYMBOL(mdc_link);
564 EXPORT_SYMBOL(mdc_readpage);
565 EXPORT_SYMBOL(mdc_setattr);
566 EXPORT_SYMBOL(mdc_close);
567 EXPORT_SYMBOL(mdc_open);
569 module_init(ptlrpc_request_init);
570 module_exit(ptlrpc_request_exit);