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 CDEBUG(D_INODE, "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 ll_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),
219 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
222 it->it_mode |= S_IFDIR;
224 case (IT_CREAT|IT_OPEN):
226 it->it_mode |= S_IFREG;
229 it->it_mode |= S_IFLNK;
232 it->it_mode &= ~current->fs->umask;
234 size[2] = sizeof(struct mds_rec_create);
235 size[3] = de->d_name.len + 1;
236 size[4] = tgtlen + 1;
237 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
242 /* pack the intent */
243 lit = lustre_msg_buf(req->rq_reqmsg, 1);
244 lit->opc = NTOH__u64((__u64)it->it_op);
246 /* pack the intended request */
247 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
248 current->fsgid, CURRENT_TIME, de->d_name.name,
249 de->d_name.len, tgt, tgtlen);
250 req->rq_replen = lustre_msg_size(3, repsize);
251 } else if (it->it_op == IT_RENAME2) {
252 struct dentry *old_de = it->it_data;
254 size[2] = sizeof(struct mds_rec_rename);
255 size[3] = old_de->d_name.len + 1;
256 size[4] = de->d_name.len + 1;
257 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
262 /* pack the intent */
263 lit = lustre_msg_buf(req->rq_reqmsg, 1);
264 lit->opc = NTOH__u64((__u64)it->it_op);
266 /* pack the intended request */
267 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
268 old_de->d_name.name, old_de->d_name.len,
269 de->d_name.name, de->d_name.len);
270 req->rq_replen = lustre_msg_size(3, repsize);
271 } else if (it->it_op == IT_LINK2) {
272 struct dentry *old_de = it->it_data;
274 size[2] = sizeof(struct mds_rec_link);
275 size[3] = de->d_name.len + 1;
276 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
281 /* pack the intent */
282 lit = lustre_msg_buf(req->rq_reqmsg, 1);
283 lit->opc = NTOH__u64((__u64)it->it_op);
285 /* pack the intended request */
286 mds_link_pack(req, 2, old_de->d_inode, dir,
287 de->d_name.name, de->d_name.len);
288 req->rq_replen = lustre_msg_size(3, repsize);
289 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
290 size[2] = sizeof(struct mds_rec_unlink);
291 size[3] = de->d_name.len + 1;
292 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
297 /* pack the intent */
298 lit = lustre_msg_buf(req->rq_reqmsg, 1);
299 lit->opc = NTOH__u64((__u64)it->it_op);
301 /* pack the intended request */
302 mds_unlink_pack(req, 2, dir, NULL,
303 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
304 de->d_name.name, de->d_name.len);
306 req->rq_replen = lustre_msg_size(3, repsize);
307 } else if (it->it_op & (IT_GETATTR | IT_RENAME | IT_LINK |
308 IT_OPEN | IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
309 size[2] = sizeof(struct mds_body);
310 size[3] = de->d_name.len + 1;
312 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
317 /* pack the intent */
318 lit = lustre_msg_buf(req->rq_reqmsg, 1);
319 lit->opc = NTOH__u64((__u64)it->it_op);
321 /* pack the intended request */
322 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
324 /* we need to replay opens */
325 if (it->it_op == IT_OPEN)
326 req->rq_flags |= PTL_RPC_FL_REPLAY;
328 /* get ready for the reply */
329 req->rq_replen = lustre_msg_size(3, repsize);
330 } else if (it->it_op == IT_READDIR) {
331 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
336 /* get ready for the reply */
337 req->rq_replen = lustre_msg_size(1, repsize);
342 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
343 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
344 lock_type, NULL, 0, lock_mode, &flags,
345 ldlm_completion_ast, mdc_blocking_ast, data,
348 /* This can go when we're sure that this can never happen */
351 if (rc == ELDLM_LOCK_ABORTED) {
353 memset(lockh, 0, sizeof(*lockh));
355 } else if (rc != 0) {
356 CERROR("ldlm_cli_enqueue: %d\n", rc);
360 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
361 it->it_disposition = (int) dlm_rep->lock_policy_res1;
362 it->it_status = (int) dlm_rep->lock_policy_res2;
363 it->it_lock_mode = lock_mode;
369 struct replay_open_data {
370 struct lustre_handle *fh;
373 static void mdc_replay_open(struct ptlrpc_request *req)
376 struct replay_open_data *saved;
377 struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
379 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA)
384 saved = lustre_msg_buf(req->rq_reqmsg, offset);
385 mds_unpack_body(body);
386 CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
387 saved->fh->addr, saved->fh->cookie,
388 body->handle.addr, body->handle.cookie);
389 memcpy(saved->fh, &body->handle, sizeof(body->handle));
392 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
393 struct lov_stripe_md *lsm, struct lustre_handle *fh,
394 struct ptlrpc_request **request)
396 struct mds_body *body;
397 struct replay_open_data *replay_data;
398 int rc, size[3] = {sizeof(*body), sizeof(*replay_data)}, bufcount = 2;
399 struct ptlrpc_request *req;
404 size[2] = size[1]; /* shuffle the spare data along */
406 size[1] = lsm->lsm_mds_easize;
409 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
412 GOTO(out, rc = -ENOMEM);
415 lustre_msg_set_op_flags(req->rq_reqmsg, MDS_OPEN_HAS_EA);
418 req->rq_flags |= PTL_RPC_FL_REPLAY;
419 body = lustre_msg_buf(req->rq_reqmsg, 0);
421 ll_ino2fid(&body->fid1, ino, 0, type);
422 body->flags = HTON__u32(flags);
423 memcpy(&body->handle, fh, sizeof(body->handle));
426 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), lsm);
428 req->rq_replen = lustre_msg_size(1, size);
430 rc = ptlrpc_queue_wait(req);
431 rc = ptlrpc_check_status(req, rc);
433 body = lustre_msg_buf(req->rq_repmsg, 0);
434 mds_unpack_body(body);
435 memcpy(fh, &body->handle, sizeof(*fh));
438 /* If open is replayed, we need to fix up the fh. */
439 req->rq_replay_cb = mdc_replay_open;
440 replay_data = lustre_msg_buf(req->rq_reqmsg, lsm ? 2 : 1);
441 replay_data->fh = fh;
449 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
450 struct lustre_handle *fh, struct ptlrpc_request **request)
452 struct mds_body *body;
453 int rc, size = sizeof(*body);
454 struct ptlrpc_request *req;
456 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
459 GOTO(out, rc = -ENOMEM);
461 body = lustre_msg_buf(req->rq_reqmsg, 0);
462 ll_ino2fid(&body->fid1, ino, 0, type);
463 memcpy(&body->handle, fh, sizeof(body->handle));
465 req->rq_replen = lustre_msg_size(0, NULL);
467 rc = ptlrpc_queue_wait(req);
468 rc = ptlrpc_check_status(req, rc);
476 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
477 char *addr, struct ptlrpc_request **request)
479 struct ptlrpc_connection *connection =
480 client_conn2cli(conn)->cl_import.imp_connection;
481 struct ptlrpc_request *req = NULL;
482 struct ptlrpc_bulk_desc *desc = NULL;
483 struct ptlrpc_bulk_page *bulk = NULL;
484 struct mds_body *body;
485 int rc, size = sizeof(*body);
488 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
490 desc = ptlrpc_prep_bulk(connection);
492 GOTO(out, rc = -ENOMEM);
494 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
497 GOTO(out2, rc = -ENOMEM);
499 bulk = ptlrpc_prep_bulk_page(desc);
500 bulk->bp_buflen = PAGE_SIZE;
502 bulk->bp_xid = req->rq_xid;
503 desc->bd_portal = MDS_BULK_PORTAL;
505 rc = ptlrpc_register_bulk(desc);
507 CERROR("couldn't setup bulk sink: error %d.\n", rc);
511 body = lustre_msg_buf(req->rq_reqmsg, 0);
513 body->fid1.f_type = type;
516 req->rq_replen = lustre_msg_size(1, &size);
517 rc = ptlrpc_queue_wait(req);
518 rc = ptlrpc_check_status(req, rc);
520 ptlrpc_abort_bulk(desc);
523 body = lustre_msg_buf(req->rq_repmsg, 0);
524 mds_unpack_body(body);
529 ptlrpc_free_bulk(desc);
535 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
536 struct ptlrpc_request **request)
538 struct ptlrpc_request *req;
539 int rc, size = sizeof(*osfs);
542 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
545 GOTO(out, rc = -ENOMEM);
546 req->rq_replen = lustre_msg_size(1, &size);
548 rc = ptlrpc_queue_wait(req);
549 rc = ptlrpc_check_status(req, rc);
554 obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
563 struct obd_ops mdc_obd_ops = {
564 o_setup: client_obd_setup,
565 o_cleanup: client_obd_cleanup,
566 o_connect: client_obd_connect,
567 o_disconnect: client_obd_disconnect,
570 static int __init ptlrpc_request_init(void)
572 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
575 static void __exit ptlrpc_request_exit(void)
577 class_unregister_type(LUSTRE_MDC_NAME);
580 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
581 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
582 MODULE_LICENSE("GPL");
584 EXPORT_SYMBOL(mdc_getstatus);
585 EXPORT_SYMBOL(mdc_getlovinfo);
586 EXPORT_SYMBOL(mdc_enqueue);
587 EXPORT_SYMBOL(mdc_getattr);
588 EXPORT_SYMBOL(mdc_statfs);
589 EXPORT_SYMBOL(mdc_create);
590 EXPORT_SYMBOL(mdc_unlink);
591 EXPORT_SYMBOL(mdc_rename);
592 EXPORT_SYMBOL(mdc_link);
593 EXPORT_SYMBOL(mdc_readpage);
594 EXPORT_SYMBOL(mdc_setattr);
595 EXPORT_SYMBOL(mdc_close);
596 EXPORT_SYMBOL(mdc_open);
598 module_init(ptlrpc_request_init);
599 module_exit(ptlrpc_request_exit);