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>
33 #define REQUEST_MINOR 244
35 extern int mds_queue_req(struct ptlrpc_request *);
37 int mdc_con2cl(struct lustre_handle *conn, struct ptlrpc_client **cl,
38 struct ptlrpc_connection **connection,
39 struct lustre_handle **rconn)
41 struct obd_export *export;
42 struct client_obd *mdc;
44 export = class_conn2export(conn);
48 mdc = &export->exp_obd->u.cli;
51 *connection = mdc->cl_conn;
52 *rconn = &export->exp_rconnh;
57 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
58 __u64 *last_committed, __u64 *last_rcvd,
59 __u32 *last_xid, struct ptlrpc_request **request)
61 struct ptlrpc_request *req;
62 struct mds_body *body;
63 int rc, size = sizeof(*body);
66 req = ptlrpc_prep_req2(conn, MDS_GETSTATUS, 1, &size, NULL);
68 GOTO(out, rc = -ENOMEM);
70 body = lustre_msg_buf(req->rq_reqmsg, 0);
71 req->rq_level = LUSTRE_CONN_CON;
72 req->rq_replen = lustre_msg_size(1, &size);
74 mds_pack_req_body(req);
75 rc = ptlrpc_queue_wait(req);
76 rc = ptlrpc_check_status(req, rc);
79 body = lustre_msg_buf(req->rq_repmsg, 0);
80 mds_unpack_body(body);
81 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
82 *last_committed = req->rq_repmsg->last_committed;
83 *last_rcvd = req->rq_repmsg->last_rcvd;
84 *last_xid = body->last_xid;
86 CDEBUG(D_NET, "root ino=%ld, last_committed=%Lu, last_rcvd=%Lu,"
88 (unsigned long)rootfid->id,
89 (unsigned long long)*last_committed,
90 (unsigned long long)*last_rcvd,
100 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
101 uuid_t **uuids, struct ptlrpc_request **request)
103 struct ptlrpc_request *req;
104 struct mds_status_req *streq;
105 struct lov_obd *lov = &obd->u.lov;
106 struct client_obd *mdc = &lov->mdcobd->u.cli;
107 struct lov_desc *desc = &lov->desc;
108 int rc, size[2] = {sizeof(*streq)};
111 req = ptlrpc_prep_req2(mdc_connh, MDS_GETLOVINFO, 1, size, NULL);
113 GOTO(out, rc = -ENOMEM);
116 streq = lustre_msg_buf(req->rq_reqmsg, 0);
117 streq->flags = HTON__u32(MDS_STATUS_LOV);
118 streq->repbuf = HTON__u32(8000);
120 /* prepare for reply */
121 req->rq_level = LUSTRE_CONN_CON;
122 size[0] = sizeof(*desc);
124 req->rq_replen = lustre_msg_size(2, size);
126 rc = ptlrpc_queue_wait(req);
127 rc = ptlrpc_check_status(req, rc);
130 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
131 *uuids = lustre_msg_buf(req->rq_repmsg, 1);
132 lov_unpackdesc(desc);
134 mdc->cl_max_mdsize = sizeof(*desc) +
135 desc->ld_tgt_count * sizeof(uuid_t);
143 int mdc_getattr(struct lustre_handle *conn,
144 obd_id ino, int type, unsigned long valid, size_t ea_size,
145 struct ptlrpc_request **request)
147 struct ptlrpc_client *cl;
148 struct ptlrpc_connection *connection;
149 struct lustre_handle *rconn;
150 struct ptlrpc_request *req;
151 struct mds_body *body;
152 int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
155 mdc_con2cl(conn, &cl, &connection, &rconn);
156 req = ptlrpc_prep_req2(conn, MDS_GETATTR, 1, size, NULL);
158 GOTO(out, rc = -ENOMEM);
160 body = lustre_msg_buf(req->rq_reqmsg, 0);
161 ll_ino2fid(&body->fid1, ino, 0, type);
165 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
167 size[1] = mdc->cl_max_mdsize;
168 } else if (valid & OBD_MD_LINKNAME) {
172 req->rq_replen = lustre_msg_size(bufcount, size);
174 rc = ptlrpc_queue_wait(req);
175 rc = ptlrpc_check_status(req, rc);
178 body = lustre_msg_buf(req->rq_repmsg, 0);
179 mds_unpack_body(body);
180 CDEBUG(D_NET, "mode: %o\n", body->mode);
189 static int mdc_lock_callback(struct lustre_handle *lockh,
190 struct ldlm_lock_desc *desc, void *data,
191 int data_len, struct ptlrpc_request **req)
194 struct inode *inode = data;
198 /* Completion AST. Do nothing. */
202 if (data_len != sizeof(*inode)) {
203 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
209 /* FIXME: do something better than throwing away everything */
212 if (S_ISDIR(inode->i_mode)) {
213 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
214 invalidate_inode_pages(inode);
217 rc = ldlm_cli_cancel(lockh);
219 CERROR("ldlm_cli_cancel: %d\n", rc);
225 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
226 struct lookup_intent *it, int lock_mode, struct inode *dir,
227 struct dentry *de, struct lustre_handle *lockh, __u64 id,
228 char *tgt, int tgtlen, void *data, int datalen)
230 struct ptlrpc_request *req;
231 struct obd_device *obddev = class_conn2obd(conn);
232 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
233 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
235 int repsize[3] = {sizeof(struct ldlm_reply),
236 sizeof(struct mds_body),
237 obddev->u.cli.cl_max_mdsize};
238 struct ldlm_reply *dlm_rep;
239 struct ldlm_intent *lit;
242 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op), dir->i_ino);
246 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
248 case (IT_CREAT|IT_OPEN):
250 it->it_mode |= S_IFREG; /* no break */
252 it->it_mode &= ~current->fs->umask;
255 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
259 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
260 size[2] = sizeof(struct mds_rec_create);
261 size[3] = de->d_name.len + 1;
262 size[4] = tgtlen + 1;
263 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
267 /* pack the intent */
268 lit = lustre_msg_buf(req->rq_reqmsg, 1);
269 lit->opc = NTOH__u64((__u64)it->it_op);
271 /* pack the intended request */
272 mds_create_pack(req, 2, dir, it->it_mode, id, current->fsuid,
273 current->fsgid, CURRENT_TIME, de->d_name.name,
274 de->d_name.len, tgt, tgtlen);
275 req->rq_replen = lustre_msg_size(3, repsize);
276 } else if (it->it_op == IT_RENAME2) {
277 struct dentry *old_de = it->it_data;
279 size[2] = sizeof(struct mds_rec_rename);
280 size[3] = old_de->d_name.len + 1;
281 size[4] = de->d_name.len + 1;
282 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
286 /* pack the intent */
287 lit = lustre_msg_buf(req->rq_reqmsg, 1);
288 lit->opc = NTOH__u64((__u64)it->it_op);
290 /* pack the intended request */
291 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
292 old_de->d_name.name, old_de->d_name.len,
293 de->d_name.name, de->d_name.len);
294 req->rq_replen = lustre_msg_size(1, repsize);
295 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
296 size[2] = sizeof(struct mds_rec_unlink);
297 size[3] = de->d_name.len + 1;
298 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
302 /* pack the intent */
303 lit = lustre_msg_buf(req->rq_reqmsg, 1);
304 lit->opc = NTOH__u64((__u64)it->it_op);
306 /* pack the intended request */
307 mds_unlink_pack(req, 2, dir, NULL, de->d_name.name,
310 req->rq_replen = lustre_msg_size(3, repsize);
311 } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
312 it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
313 it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
314 size[2] = sizeof(struct mds_body);
315 size[3] = de->d_name.len + 1;
317 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
321 /* pack the intent */
322 lit = lustre_msg_buf(req->rq_reqmsg, 1);
323 lit->opc = NTOH__u64((__u64)it->it_op);
325 /* pack the intended request */
326 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
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_req2(conn, LDLM_ENQUEUE, 1, size, NULL);
335 /* get ready for the reply */
336 req->rq_replen = lustre_msg_size(1, repsize);
341 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
342 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id, lock_type,
343 NULL, 0, lock_mode, &flags, ldlm_completion_ast,
344 (void *)mdc_lock_callback, data, datalen, lockh);
345 if (rc == -ENOENT || rc == ELDLM_LOCK_ABORTED) {
347 memset(lockh, 0, sizeof(*lockh));
349 } else if (rc != 0) {
350 CERROR("ldlm_cli_enqueue: %d\n", rc);
354 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
355 it->it_disposition = (int) dlm_rep->lock_policy_res1;
356 it->it_status = (int) dlm_rep->lock_policy_res2;
357 it->it_lock_mode = lock_mode;
363 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
364 struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
365 struct ptlrpc_request **request)
367 struct ptlrpc_client *cl;
368 struct ptlrpc_connection *connection;
369 struct lustre_handle *rconn;
370 struct mds_body *body;
371 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
372 struct ptlrpc_request *req;
377 size[1] = smd->lmd_easize;
380 mdc_con2cl(conn, &cl, &connection, &rconn);
381 req = ptlrpc_prep_req2(conn, MDS_OPEN, bufcount, size, NULL);
383 GOTO(out, rc = -ENOMEM);
385 req->rq_flags |= PTL_RPC_FL_REPLAY;
386 body = lustre_msg_buf(req->rq_reqmsg, 0);
388 ll_ino2fid(&body->fid1, ino, 0, type);
389 body->flags = HTON__u32(flags);
390 body->extra = cookie;
393 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), smd, smd->lmd_easize);
395 req->rq_replen = lustre_msg_size(1, size);
397 rc = ptlrpc_queue_wait(req);
398 rc = ptlrpc_check_status(req, rc);
400 body = lustre_msg_buf(req->rq_repmsg, 0);
401 mds_unpack_body(body);
411 int mdc_close(struct lustre_handle *conn,
412 obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
414 struct mds_body *body;
415 int rc, size = sizeof(*body);
416 struct ptlrpc_request *req;
418 req = ptlrpc_prep_req2(conn, MDS_CLOSE, 1, &size, NULL);
420 GOTO(out, rc = -ENOMEM);
422 body = lustre_msg_buf(req->rq_reqmsg, 0);
423 ll_ino2fid(&body->fid1, ino, 0, type);
426 req->rq_replen = lustre_msg_size(0, NULL);
428 rc = ptlrpc_queue_wait(req);
429 rc = ptlrpc_check_status(req, rc);
437 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
438 char *addr, struct ptlrpc_request **request)
440 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
441 struct ptlrpc_request *req = NULL;
442 struct ptlrpc_bulk_desc *desc = NULL;
443 struct ptlrpc_bulk_page *bulk = NULL;
444 struct mds_body *body;
445 int rc, size = sizeof(*body);
448 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
450 desc = ptlrpc_prep_bulk(connection);
452 GOTO(out, rc = -ENOMEM);
454 req = ptlrpc_prep_req2(conn, MDS_READPAGE, 1, &size, NULL);
456 GOTO(out2, rc = -ENOMEM);
458 bulk = ptlrpc_prep_bulk_page(desc);
459 bulk->b_buflen = PAGE_SIZE;
461 bulk->b_xid = req->rq_xid;
462 desc->b_portal = MDS_BULK_PORTAL;
464 rc = ptlrpc_register_bulk(desc);
466 CERROR("couldn't setup bulk sink: error %d.\n", rc);
470 body = lustre_msg_buf(req->rq_reqmsg, 0);
472 body->fid1.f_type = type;
475 req->rq_replen = lustre_msg_size(1, &size);
476 rc = ptlrpc_queue_wait(req);
477 rc = ptlrpc_check_status(req, rc);
479 ptlrpc_abort_bulk(desc);
482 body = lustre_msg_buf(req->rq_repmsg, 0);
483 mds_unpack_body(body);
488 ptlrpc_free_bulk(desc);
494 int mdc_statfs(struct lustre_handle *conn, struct statfs *sfs,
495 struct ptlrpc_request **request)
497 struct obd_statfs *osfs;
498 struct ptlrpc_request *req;
499 int rc, size = sizeof(*osfs);
502 req = ptlrpc_prep_req2(conn, MDS_STATFS, 0, NULL, NULL);
504 GOTO(out, rc = -ENOMEM);
505 req->rq_replen = lustre_msg_size(1, &size);
507 rc = ptlrpc_queue_wait(req);
508 rc = ptlrpc_check_status(req, rc);
513 osfs = lustre_msg_buf(req->rq_repmsg, 0);
514 obd_statfs_unpack(osfs, sfs);
523 struct obd_ops mdc_obd_ops = {
524 o_setup: client_obd_setup,
525 o_cleanup: client_obd_cleanup,
526 o_connect: client_obd_connect,
527 o_disconnect: client_obd_disconnect,
530 static int __init ptlrpc_request_init(void)
532 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
535 static void __exit ptlrpc_request_exit(void)
537 class_unregister_type(LUSTRE_MDC_NAME);
540 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
541 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
542 MODULE_LICENSE("GPL");
544 EXPORT_SYMBOL(mdc_getstatus);
545 EXPORT_SYMBOL(mdc_getlovinfo);
546 EXPORT_SYMBOL(mdc_enqueue);
547 EXPORT_SYMBOL(mdc_getattr);
548 EXPORT_SYMBOL(mdc_statfs);
549 EXPORT_SYMBOL(mdc_create);
550 EXPORT_SYMBOL(mdc_unlink);
551 EXPORT_SYMBOL(mdc_rename);
552 EXPORT_SYMBOL(mdc_link);
553 EXPORT_SYMBOL(mdc_readpage);
554 EXPORT_SYMBOL(mdc_setattr);
555 EXPORT_SYMBOL(mdc_close);
556 EXPORT_SYMBOL(mdc_open);
558 module_init(ptlrpc_request_init);
559 module_exit(ptlrpc_request_exit);