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);
130 struct client_obd *mdc = &class_conn2obd(conn)->u.cli;
132 size[1] = mdc->cl_max_mdsize;
133 } else if (valid & OBD_MD_LINKNAME) {
137 req->rq_replen = lustre_msg_size(bufcount, size);
139 rc = ptlrpc_queue_wait(req);
140 rc = ptlrpc_check_status(req, rc);
143 body = lustre_msg_buf(req->rq_repmsg, 0);
144 mds_unpack_body(body);
145 CDEBUG(D_NET, "mode: %o\n", body->mode);
154 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
155 void *data, __u32 data_len)
158 struct inode *inode = data;
159 struct lustre_handle lockh;
162 if (data_len != sizeof(*inode)) {
163 CERROR("data_len should be %d, but is %d\n", sizeof(*inode),
169 /* FIXME: do something better than throwing away everything */
172 if (S_ISDIR(inode->i_mode)) {
173 CDEBUG(D_INODE, "invalidating inode %ld\n", inode->i_ino);
174 invalidate_inode_pages(inode);
177 ldlm_lock2handle(lock, &lockh);
178 rc = ldlm_cli_cancel(&lockh);
180 CERROR("ldlm_cli_cancel: %d\n", rc);
186 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
187 struct lookup_intent *it, int lock_mode, struct inode *dir,
188 struct dentry *de, struct lustre_handle *lockh,
189 char *tgt, int tgtlen, void *data, int datalen)
191 struct ptlrpc_request *req;
192 struct obd_device *obddev = class_conn2obd(conn);
193 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino};
194 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
196 int repsize[3] = {sizeof(struct ldlm_reply),
197 sizeof(struct mds_body),
198 obddev->u.cli.cl_max_mdsize};
199 struct ldlm_reply *dlm_rep;
200 struct ldlm_intent *lit;
203 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
208 it->it_mode = (it->it_mode | S_IFDIR) & ~current->fs->umask;
210 case (IT_CREAT|IT_OPEN):
212 it->it_mode |= S_IFREG; /* no break */
214 it->it_mode &= ~current->fs->umask;
217 it->it_mode = (it->it_mode | S_IFLNK) & ~current->fs->umask;
221 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
222 size[2] = sizeof(struct mds_rec_create);
223 size[3] = de->d_name.len + 1;
224 size[4] = tgtlen + 1;
225 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
230 /* pack the intent */
231 lit = lustre_msg_buf(req->rq_reqmsg, 1);
232 lit->opc = NTOH__u64((__u64)it->it_op);
234 /* pack the intended request */
235 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
236 current->fsgid, CURRENT_TIME, de->d_name.name,
237 de->d_name.len, tgt, tgtlen);
238 req->rq_replen = lustre_msg_size(3, repsize);
239 } else if (it->it_op == IT_RENAME2) {
240 struct dentry *old_de = it->it_data;
242 size[2] = sizeof(struct mds_rec_rename);
243 size[3] = old_de->d_name.len + 1;
244 size[4] = de->d_name.len + 1;
245 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
250 /* pack the intent */
251 lit = lustre_msg_buf(req->rq_reqmsg, 1);
252 lit->opc = NTOH__u64((__u64)it->it_op);
254 /* pack the intended request */
255 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
256 old_de->d_name.name, old_de->d_name.len,
257 de->d_name.name, de->d_name.len);
258 req->rq_replen = lustre_msg_size(3, repsize);
259 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
260 size[2] = sizeof(struct mds_rec_unlink);
261 size[3] = de->d_name.len + 1;
262 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
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_unlink_pack(req, 2, dir, NULL,
273 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
274 de->d_name.name, de->d_name.len);
276 req->rq_replen = lustre_msg_size(3, repsize);
277 } else if (it->it_op == IT_GETATTR || it->it_op == IT_RENAME ||
278 it->it_op == IT_OPEN || it->it_op == IT_SETATTR ||
279 it->it_op == IT_LOOKUP || it->it_op == IT_READLINK) {
280 size[2] = sizeof(struct mds_body);
281 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_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
295 /* we need to replay opens */
296 if (it->it_op == IT_OPEN)
297 req->rq_flags |= PTL_RPC_FL_REPLAY;
299 /* get ready for the reply */
300 req->rq_replen = lustre_msg_size(3, repsize);
301 } else if (it->it_op == IT_READDIR) {
302 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
307 /* get ready for the reply */
308 req->rq_replen = lustre_msg_size(1, repsize);
313 #warning FIXME: the data here needs to be different if a lock was granted for a different inode
314 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
315 lock_type, NULL, 0, lock_mode, &flags,
316 ldlm_completion_ast, mdc_blocking_ast, data,
319 /* This can go when we're sure that this can never happen */
322 if (rc == ELDLM_LOCK_ABORTED) {
324 memset(lockh, 0, sizeof(*lockh));
326 } else if (rc != 0) {
327 CERROR("ldlm_cli_enqueue: %d\n", rc);
331 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
332 it->it_disposition = (int) dlm_rep->lock_policy_res1;
333 it->it_status = (int) dlm_rep->lock_policy_res2;
334 it->it_lock_mode = lock_mode;
340 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
341 struct lov_stripe_md *smd, __u64 cookie, __u64 *fh,
342 struct ptlrpc_request **request)
344 struct mds_body *body;
345 int rc, size[2] = {sizeof(*body)}, bufcount = 1;
346 struct ptlrpc_request *req;
351 size[1] = smd->lmd_easize;
354 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
357 GOTO(out, rc = -ENOMEM);
359 req->rq_flags |= PTL_RPC_FL_REPLAY;
360 body = lustre_msg_buf(req->rq_reqmsg, 0);
362 ll_ino2fid(&body->fid1, ino, 0, type);
363 body->flags = HTON__u32(flags);
364 body->extra = cookie;
367 lov_packmd(lustre_msg_buf(req->rq_reqmsg, 1), smd);
369 req->rq_replen = lustre_msg_size(1, size);
371 rc = ptlrpc_queue_wait(req);
372 rc = ptlrpc_check_status(req, rc);
374 body = lustre_msg_buf(req->rq_repmsg, 0);
375 mds_unpack_body(body);
385 int mdc_close(struct lustre_handle *conn,
386 obd_id ino, int type, __u64 fh, struct ptlrpc_request **request)
388 struct mds_body *body;
389 int rc, size = sizeof(*body);
390 struct ptlrpc_request *req;
392 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
395 GOTO(out, rc = -ENOMEM);
397 body = lustre_msg_buf(req->rq_reqmsg, 0);
398 ll_ino2fid(&body->fid1, ino, 0, type);
401 req->rq_replen = lustre_msg_size(0, NULL);
403 rc = ptlrpc_queue_wait(req);
404 rc = ptlrpc_check_status(req, rc);
412 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
413 char *addr, struct ptlrpc_request **request)
415 struct ptlrpc_connection *connection =
416 client_conn2cli(conn)->cl_import.imp_connection;
417 struct ptlrpc_request *req = NULL;
418 struct ptlrpc_bulk_desc *desc = NULL;
419 struct ptlrpc_bulk_page *bulk = NULL;
420 struct mds_body *body;
421 int rc, size = sizeof(*body);
424 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
426 desc = ptlrpc_prep_bulk(connection);
428 GOTO(out, rc = -ENOMEM);
430 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
433 GOTO(out2, rc = -ENOMEM);
435 bulk = ptlrpc_prep_bulk_page(desc);
436 bulk->bp_buflen = PAGE_SIZE;
438 bulk->bp_xid = req->rq_xid;
439 desc->bd_portal = MDS_BULK_PORTAL;
441 rc = ptlrpc_register_bulk(desc);
443 CERROR("couldn't setup bulk sink: error %d.\n", rc);
447 body = lustre_msg_buf(req->rq_reqmsg, 0);
449 body->fid1.f_type = type;
452 req->rq_replen = lustre_msg_size(1, &size);
453 rc = ptlrpc_queue_wait(req);
454 rc = ptlrpc_check_status(req, rc);
456 ptlrpc_abort_bulk(desc);
459 body = lustre_msg_buf(req->rq_repmsg, 0);
460 mds_unpack_body(body);
465 ptlrpc_free_bulk(desc);
471 int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs,
472 struct ptlrpc_request **request)
474 struct ptlrpc_request *req;
475 int rc, size = sizeof(*osfs);
478 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
481 GOTO(out, rc = -ENOMEM);
482 req->rq_replen = lustre_msg_size(1, &size);
484 rc = ptlrpc_queue_wait(req);
485 rc = ptlrpc_check_status(req, rc);
490 obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
499 struct obd_ops mdc_obd_ops = {
500 o_setup: client_obd_setup,
501 o_cleanup: client_obd_cleanup,
502 o_connect: client_obd_connect,
503 o_disconnect: client_obd_disconnect,
506 static int __init ptlrpc_request_init(void)
508 return class_register_type(&mdc_obd_ops, LUSTRE_MDC_NAME);
511 static void __exit ptlrpc_request_exit(void)
513 class_unregister_type(LUSTRE_MDC_NAME);
516 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
517 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
518 MODULE_LICENSE("GPL");
520 EXPORT_SYMBOL(mdc_getstatus);
521 EXPORT_SYMBOL(mdc_getlovinfo);
522 EXPORT_SYMBOL(mdc_enqueue);
523 EXPORT_SYMBOL(mdc_getattr);
524 EXPORT_SYMBOL(mdc_statfs);
525 EXPORT_SYMBOL(mdc_create);
526 EXPORT_SYMBOL(mdc_unlink);
527 EXPORT_SYMBOL(mdc_rename);
528 EXPORT_SYMBOL(mdc_link);
529 EXPORT_SYMBOL(mdc_readpage);
530 EXPORT_SYMBOL(mdc_setattr);
531 EXPORT_SYMBOL(mdc_close);
532 EXPORT_SYMBOL(mdc_open);
534 module_init(ptlrpc_request_init);
535 module_exit(ptlrpc_request_exit);