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_con2cl(struct lustre_handle *conn, struct ptlrpc_client **cl,
39 struct ptlrpc_connection **connection,
40 struct lustre_handle **rconn)
42 struct obd_export *export;
43 struct client_obd *mdc;
45 export = class_conn2export(conn);
49 mdc = &export->exp_obd->u.cli;
52 *connection = mdc->cl_conn;
53 *rconn = &export->exp_rconnh;
58 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid,
59 __u64 *last_committed, __u32 *last_xid,
60 struct ptlrpc_request **request)
62 struct ptlrpc_request *req;
63 struct mds_body *body;
64 int rc, size = sizeof(*body);
67 req = ptlrpc_prep_req2(conn, MDS_GETSTATUS, 1, &size, NULL);
69 GOTO(out, rc = -ENOMEM);
71 body = lustre_msg_buf(req->rq_reqmsg, 0);
72 req->rq_level = LUSTRE_CONN_CON;
73 req->rq_replen = lustre_msg_size(1, &size);
75 mds_pack_req_body(req);
76 rc = ptlrpc_queue_wait(req);
77 rc = ptlrpc_check_status(req, rc);
80 body = lustre_msg_buf(req->rq_repmsg, 0);
81 mds_unpack_body(body);
82 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
83 *last_committed = req->rq_repmsg->last_committed;
84 *last_xid = req->rq_repmsg->last_xid;
86 CDEBUG(D_NET, "root ino=%ld, last_committed=%Lu, last_xid=%d\n",
87 (unsigned long)rootfid->id,
88 (unsigned long long)*last_committed, *last_xid);
97 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
98 uuid_t **uuids, struct ptlrpc_request **request)
100 struct ptlrpc_request *req;
101 struct mds_status_req *streq;
102 struct lov_obd *lov = &obd->u.lov;
103 struct client_obd *mdc = &lov->mdcobd->u.cli;
104 struct lov_desc *desc = &lov->desc;
105 int rc, size[2] = {sizeof(*streq)};
108 req = ptlrpc_prep_req2(mdc_connh, MDS_GETLOVINFO, 1, size, NULL);
110 GOTO(out, rc = -ENOMEM);
113 streq = lustre_msg_buf(req->rq_reqmsg, 0);
114 streq->flags = HTON__u32(MDS_STATUS_LOV);
115 streq->repbuf = HTON__u32(8000);
117 /* prepare for reply */
118 req->rq_level = LUSTRE_CONN_CON;
119 size[0] = sizeof(*desc);
121 req->rq_replen = lustre_msg_size(2, size);
123 rc = ptlrpc_queue_wait(req);
124 rc = ptlrpc_check_status(req, rc);
127 memcpy(desc, lustre_msg_buf(req->rq_repmsg, 0), sizeof(*desc));
128 *uuids = lustre_msg_buf(req->rq_repmsg, 1);
129 lov_unpackdesc(desc);
130 mdc->cl_max_mdsize = sizeof(struct lov_stripe_md) +
131 desc->ld_tgt_count * sizeof(struct lov_object_id);
140 int mdc_getattr(struct lustre_handle *conn,
141 obd_id ino, int type, unsigned long valid, size_t ea_size,
142 struct ptlrpc_request **request)
144 struct ptlrpc_request *req;
145 struct mds_body *body;
146 int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
149 req = ptlrpc_prep_req2(conn, MDS_GETATTR, 1, size, NULL);
151 GOTO(out, rc = -ENOMEM);
153 body = lustre_msg_buf(req->rq_reqmsg, 0);
154 ll_ino2fid(&body->fid1, ino, 0, type);
158 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
160 size[1] = mdc->cl_max_mdsize;
161 } else if (valid & OBD_MD_LINKNAME) {
165 req->rq_replen = lustre_msg_size(bufcount, size);
167 rc = ptlrpc_queue_wait(req);
168 rc = ptlrpc_check_status(req, rc);
171 body = lustre_msg_buf(req->rq_repmsg, 0);
172 mds_unpack_body(body);
173 CDEBUG(D_NET, "mode: %o\n", body->mode);
182 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
183 void *data, __u32 data_len)
186 struct inode *inode = data;
187 struct lustre_handle lockh;
190 if (data_len != sizeof(*inode)) {
191 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
197 /* FIXME: do something better than throwing away everything */
200 if (S_ISDIR(inode->i_mode)) {
201 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
202 invalidate_inode_pages(inode);
205 ldlm_lock2handle(lock, &lockh);
206 rc = ldlm_cli_cancel(&lockh);
208 CERROR("ldlm_cli_cancel: %d\n", rc);
214 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
215 struct lookup_intent *it, int lock_mode, struct inode *dir,
216 struct dentry *de, struct lustre_handle *lockh, __u64 id,
217 char *tgt, int tgtlen, void *data, int datalen)
219 struct ptlrpc_request *req;
220 struct obd_device *obddev = class_conn2obd(conn);
221 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
222 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
224 int repsize[3] = {sizeof(struct ldlm_reply),
225 sizeof(struct mds_body),
226 obddev->u.cli.cl_max_mdsize};
227 struct ldlm_reply *dlm_rep;
228 struct ldlm_intent *lit;
231 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
236 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
238 case (IT_CREAT|IT_OPEN):
240 it->it_mode |= S_IFREG; /* no break */
242 it->it_mode &= ~current->fs->umask;
245 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
249 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
250 size[2] = sizeof(struct mds_rec_create);
251 size[3] = de->d_name.len + 1;
252 size[4] = tgtlen + 1;
253 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
257 /* pack the intent */
258 lit = lustre_msg_buf(req->rq_reqmsg, 1);
259 lit->opc = NTOH__u64((__u64)it->it_op);
261 /* pack the intended request */
262 mds_create_pack(req, 2, dir, it->it_mode, id, current->fsuid,
263 current->fsgid, CURRENT_TIME, de->d_name.name,
264 de->d_name.len, tgt, tgtlen);
265 req->rq_replen = lustre_msg_size(3, repsize);
266 } else if (it->it_op == IT_RENAME2) {
267 struct dentry *old_de = it->it_data;
269 size[2] = sizeof(struct mds_rec_rename);
270 size[3] = old_de->d_name.len + 1;
271 size[4] = de->d_name.len + 1;
272 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 5, size, NULL);
276 /* pack the intent */
277 lit = lustre_msg_buf(req->rq_reqmsg, 1);
278 lit->opc = NTOH__u64((__u64)it->it_op);
280 /* pack the intended request */
281 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
282 old_de->d_name.name, old_de->d_name.len,
283 de->d_name.name, de->d_name.len);
284 req->rq_replen = lustre_msg_size(1, repsize);
285 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
286 size[2] = sizeof(struct mds_rec_unlink);
287 size[3] = de->d_name.len + 1;
288 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
292 /* pack the intent */
293 lit = lustre_msg_buf(req->rq_reqmsg, 1);
294 lit->opc = NTOH__u64((__u64)it->it_op);
296 /* pack the intended request */
297 mds_unlink_pack(req, 2, dir, NULL,
298 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
299 de->d_name.name, de->d_name.len);
301 req->rq_replen = lustre_msg_size(3, repsize);
302 } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
303 it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
304 it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
305 size[2] = sizeof(struct mds_body);
306 size[3] = de->d_name.len + 1;
308 req = ptlrpc_prep_req2(conn, LDLM_ENQUEUE, 4, size, NULL);
312 /* pack the intent */
313 lit = lustre_msg_buf(req->rq_reqmsg, 1);
314 lit->opc = NTOH__u64((__u64)it->it_op);
316 /* pack the intended request */
317 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
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_req2(conn, LDLM_ENQUEUE, 1, size, NULL);
326 /* get ready for the reply */
327 req->rq_replen = lustre_msg_size(1, repsize);
332 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
333 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
334 lock_type, NULL, 0, lock_mode, &flags,
335 ldlm_completion_ast, mdc_blocking_ast, data,
338 /* This can go when we're sure that this can never happen */
341 if (rc == ELDLM_LOCK_ABORTED) {
343 memset(lockh, 0, sizeof(*lockh));
345 } else if (rc != 0) {
346 CERROR("ldlm_cli_enqueue: %d\n", rc);
350 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
351 it->it_disposition = (int) dlm_rep->lock_policy_res1;
352 it->it_status = (int) dlm_rep->lock_policy_res2;
353 it->it_lock_mode = lock_mode;
359 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
360 struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
361 struct ptlrpc_request **request)
363 struct ptlrpc_client *cl;
364 struct ptlrpc_connection *connection;
365 struct lustre_handle *rconn;
366 struct mds_body *body;
367 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
368 struct ptlrpc_request *req;
373 size[1] = smd->lmd_easize;
376 mdc_con2cl(conn, &cl, &connection, &rconn);
377 req = ptlrpc_prep_req2(conn, MDS_OPEN, bufcount, size, NULL);
379 GOTO(out, rc = -ENOMEM);
381 req->rq_flags |= PTL_RPC_FL_REPLAY;
382 body = lustre_msg_buf(req->rq_reqmsg, 0);
384 ll_ino2fid(&body->fid1, ino, 0, type);
385 body->flags = HTON__u32(flags);
386 body->extra = cookie;
389 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), smd, smd->lmd_easize);
391 req->rq_replen = lustre_msg_size(1, size);
393 rc = ptlrpc_queue_wait(req);
394 rc = ptlrpc_check_status(req, rc);
396 body = lustre_msg_buf(req->rq_repmsg, 0);
397 mds_unpack_body(body);
407 int mdc_close(struct lustre_handle *conn,
408 obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
410 struct mds_body *body;
411 int rc, size = sizeof(*body);
412 struct ptlrpc_request *req;
414 req = ptlrpc_prep_req2(conn, MDS_CLOSE, 1, &size, NULL);
416 GOTO(out, rc = -ENOMEM);
418 body = lustre_msg_buf(req->rq_reqmsg, 0);
419 ll_ino2fid(&body->fid1, ino, 0, type);
422 req->rq_replen = lustre_msg_size(0, NULL);
424 rc = ptlrpc_queue_wait(req);
425 rc = ptlrpc_check_status(req, rc);
433 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
434 char *addr, struct ptlrpc_request **request)
436 struct ptlrpc_connection *connection = client_conn2cli(conn)->cl_conn;
437 struct ptlrpc_request *req = NULL;
438 struct ptlrpc_bulk_desc *desc = NULL;
439 struct ptlrpc_bulk_page *bulk = NULL;
440 struct mds_body *body;
441 int rc, size = sizeof(*body);
444 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
446 desc = ptlrpc_prep_bulk(connection);
448 GOTO(out, rc = -ENOMEM);
450 req = ptlrpc_prep_req2(conn, MDS_READPAGE, 1, &size, NULL);
452 GOTO(out2, rc = -ENOMEM);
454 bulk = ptlrpc_prep_bulk_page(desc);
455 bulk->b_buflen = PAGE_SIZE;
457 bulk->b_xid = req->rq_xid;
458 desc->b_portal = MDS_BULK_PORTAL;
460 rc = ptlrpc_register_bulk(desc);
462 CERROR("couldn't setup bulk sink: error %d.\n", rc);
466 body = lustre_msg_buf(req->rq_reqmsg, 0);
468 body->fid1.f_type = type;
471 req->rq_replen = lustre_msg_size(1, &size);
472 rc = ptlrpc_queue_wait(req);
473 rc = ptlrpc_check_status(req, rc);
475 ptlrpc_abort_bulk(desc);
478 body = lustre_msg_buf(req->rq_repmsg, 0);
479 mds_unpack_body(body);
484 ptlrpc_free_bulk(desc);
490 int mdc_statfs(struct lustre_handle *conn, struct statfs *sfs,
491 struct ptlrpc_request **request)
493 struct obd_statfs *osfs;
494 struct ptlrpc_request *req;
495 int rc, size = sizeof(*osfs);
498 req = ptlrpc_prep_req2(conn, MDS_STATFS, 0, NULL, NULL);
500 GOTO(out, rc = -ENOMEM);
501 req->rq_replen = lustre_msg_size(1, &size);
503 rc = ptlrpc_queue_wait(req);
504 rc = ptlrpc_check_status(req, rc);
509 osfs = lustre_msg_buf(req->rq_repmsg, 0);
510 obd_statfs_unpack(osfs, sfs);
519 struct obd_ops mdc_obd_ops = {
520 o_setup: client_obd_setup,
521 o_cleanup: client_obd_cleanup,
522 o_connect: client_obd_connect,
523 o_disconnect: client_obd_disconnect,
526 static int __init ptlrpc_request_init(void)
528 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
531 static void __exit ptlrpc_request_exit(void)
533 class_unregister_type(LUSTRE_MDC_NAME);
536 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
537 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
538 MODULE_LICENSE("GPL");
540 EXPORT_SYMBOL(mdc_getstatus);
541 EXPORT_SYMBOL(mdc_getlovinfo);
542 EXPORT_SYMBOL(mdc_enqueue);
543 EXPORT_SYMBOL(mdc_getattr);
544 EXPORT_SYMBOL(mdc_statfs);
545 EXPORT_SYMBOL(mdc_create);
546 EXPORT_SYMBOL(mdc_unlink);
547 EXPORT_SYMBOL(mdc_rename);
548 EXPORT_SYMBOL(mdc_link);
549 EXPORT_SYMBOL(mdc_readpage);
550 EXPORT_SYMBOL(mdc_setattr);
551 EXPORT_SYMBOL(mdc_close);
552 EXPORT_SYMBOL(mdc_open);
554 module_init(ptlrpc_request_init);
555 module_exit(ptlrpc_request_exit);