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/lprocfs_status.h>
34 #define REQUEST_MINOR 244
36 extern int mds_queue_req(struct ptlrpc_request *);
37 extern struct lprocfs_vars status_var_nm_1[];
38 extern struct lprocfs_vars status_class_var[];
40 /* should become mdc_getinfo() */
41 int mdc_getstatus(struct lustre_handle *conn, struct ll_fid *rootfid)
43 struct ptlrpc_request *req;
44 struct mds_body *body;
45 int rc, size = sizeof(*body);
48 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETSTATUS, 1, &size,
51 GOTO(out, rc = -ENOMEM);
53 body = lustre_msg_buf(req->rq_reqmsg, 0);
54 req->rq_level = LUSTRE_CONN_CON;
55 req->rq_replen = lustre_msg_size(1, &size);
57 mds_pack_req_body(req);
58 rc = ptlrpc_queue_wait(req);
59 rc = ptlrpc_check_status(req, rc);
62 body = lustre_msg_buf(req->rq_repmsg, 0);
63 mds_unpack_body(body);
64 memcpy(rootfid, &body->fid1, sizeof(*rootfid));
66 CDEBUG(D_NET, "root ino="LPU64", last_committed="LPU64
67 ", last_xid="LPU64"\n",
68 rootfid->id, req->rq_repmsg->last_committed,
69 req->rq_repmsg->last_xid);
74 ptlrpc_req_finished(req);
78 int mdc_getlovinfo(struct obd_device *obd, struct lustre_handle *mdc_connh,
79 struct ptlrpc_request **request)
81 struct ptlrpc_request *req;
82 struct mds_status_req *streq;
83 int rc, size[2] = {sizeof(*streq)};
86 req = ptlrpc_prep_req(class_conn2cliimp(mdc_connh), MDS_GETLOVINFO, 1,
89 GOTO(out, rc = -ENOMEM);
92 streq = lustre_msg_buf(req->rq_reqmsg, 0);
93 streq->flags = HTON__u32(MDS_STATUS_LOV);
94 streq->repbuf = HTON__u32(8192);
96 /* prepare for reply */
97 req->rq_level = LUSTRE_CONN_CON;
100 req->rq_replen = lustre_msg_size(2, size);
102 rc = ptlrpc_queue_wait(req);
103 rc = ptlrpc_check_status(req, rc);
110 int mdc_getattr(struct lustre_handle *conn,
111 obd_id ino, int type, unsigned long valid, size_t ea_size,
112 struct ptlrpc_request **request)
114 struct ptlrpc_request *req;
115 struct mds_body *body;
116 int rc, size[2] = {sizeof(*body), 0}, bufcount = 1;
119 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_GETATTR, 1, size,
122 GOTO(out, rc = -ENOMEM);
124 body = lustre_msg_buf(req->rq_reqmsg, 0);
125 ll_ino2fid(&body->fid1, ino, 0, type);
129 size[bufcount] = ea_size;
131 body->size = ea_size;
132 CDEBUG(D_INODE, "reserving %d bytes for MD/symlink in packet\n",
135 req->rq_replen = lustre_msg_size(bufcount, size);
136 mds_pack_req_body(req);
138 rc = ptlrpc_queue_wait(req);
139 rc = ptlrpc_check_status(req, rc);
142 body = lustre_msg_buf(req->rq_repmsg, 0);
143 mds_unpack_body(body);
144 CDEBUG(D_NET, "mode: %o\n", body->mode);
153 void d_delete_aliases(struct inode *inode)
155 struct dentry *dentry = NULL;
156 struct list_head *tmp;
157 struct ll_sb_info *sbi = ll_i2sbi(inode);
160 spin_lock(&dcache_lock);
161 list_for_each(tmp, &inode->i_dentry) {
162 dentry = list_entry(tmp, struct dentry, d_alias);
164 // if (atomic_read(&dentry->d_count))
166 //if (!list_empty(&dentry->d_lru))
169 list_del_init(&dentry->d_hash);
170 list_add(&dentry->d_hash, &sbi->ll_orphan_dentry_list);
173 spin_unlock(&dcache_lock);
177 static int mdc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
178 void *data, __u32 data_len, int flag)
181 struct lustre_handle lockh;
185 case LDLM_CB_BLOCKING:
186 ldlm_lock2handle(lock, &lockh);
187 rc = ldlm_cli_cancel(&lockh);
189 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
193 case LDLM_CB_CANCELING: {
194 /* Invalidate all dentries associated with this inode */
195 struct inode *inode = data;
197 #warning "FIXME: what tells us that 'inode' is valid at all?"
198 if (inode->i_state & I_FREEING)
201 LASSERT(inode != NULL);
202 LASSERT(data_len == sizeof(*inode));
204 if (S_ISDIR(inode->i_mode)) {
205 CDEBUG(D_INODE, "invalidating inode %ld\n",
208 ll_invalidate_inode_pages(inode);
211 if ( inode != inode->i_sb->s_root->d_inode ) {
212 /* XXX should this igrab move up 12 lines? */
213 LASSERT(igrab(inode) == inode);
214 d_delete_aliases(inode);
226 /* This should be called with both the request and the reply still packed. */
227 void mdc_store_inode_generation(struct ptlrpc_request *req, int reqoff,
230 struct mds_rec_create *rec = lustre_msg_buf(req->rq_reqmsg, reqoff);
231 struct mds_body *body = lustre_msg_buf(req->rq_repmsg, repoff);
233 memcpy(&rec->cr_replayfid, &body->fid1, sizeof rec->cr_replayfid);
236 int mdc_enqueue(struct lustre_handle *conn, int lock_type,
237 struct lookup_intent *it, int lock_mode, struct inode *dir,
238 struct dentry *de, struct lustre_handle *lockh,
239 char *tgt, int tgtlen, void *data, int datalen)
241 struct ptlrpc_request *req;
242 struct obd_device *obddev = class_conn2obd(conn);
243 __u64 res_id[RES_NAME_SIZE] = {dir->i_ino, (__u64)dir->i_generation};
244 int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
245 int rc, flags = LDLM_FL_HAS_INTENT;
246 int repsize[3] = {sizeof(struct ldlm_reply),
247 sizeof(struct mds_body),
248 obddev->u.cli.cl_max_mds_easize};
249 struct ldlm_reply *dlm_rep;
250 struct ldlm_intent *lit;
251 struct ldlm_request *lockreq;
254 LDLM_DEBUG_NOLOCK("mdsintent %s dir %ld", ldlm_it2str(it->it_op),
257 if (it->it_op & (IT_MKDIR | IT_CREAT | IT_SYMLINK | IT_MKNOD)) {
260 it->it_mode |= S_IFDIR;
262 case (IT_CREAT|IT_OPEN):
264 it->it_mode |= S_IFREG;
267 it->it_mode |= S_IFLNK;
270 it->it_mode &= ~current->fs->umask;
272 size[2] = sizeof(struct mds_rec_create);
273 size[3] = de->d_name.len + 1;
274 size[4] = tgtlen + 1;
275 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
280 /* pack the intent */
281 lit = lustre_msg_buf(req->rq_reqmsg, 1);
282 lit->opc = NTOH__u64((__u64)it->it_op);
284 /* pack the intended request */
285 mds_create_pack(req, 2, dir, it->it_mode, 0, current->fsuid,
286 current->fsgid, CURRENT_TIME, de->d_name.name,
287 de->d_name.len, tgt, tgtlen);
288 req->rq_replen = lustre_msg_size(3, repsize);
289 } else if (it->it_op == IT_RENAME2) {
290 struct dentry *old_de = it->it_data;
292 size[2] = sizeof(struct mds_rec_rename);
293 size[3] = old_de->d_name.len + 1;
294 size[4] = de->d_name.len + 1;
295 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 5,
300 /* pack the intent */
301 lit = lustre_msg_buf(req->rq_reqmsg, 1);
302 lit->opc = NTOH__u64((__u64)it->it_op);
304 /* pack the intended request */
305 mds_rename_pack(req, 2, old_de->d_parent->d_inode, dir,
306 old_de->d_name.name, old_de->d_name.len,
307 de->d_name.name, de->d_name.len);
308 req->rq_replen = lustre_msg_size(3, repsize);
309 } else if (it->it_op == IT_LINK2) {
310 struct dentry *old_de = it->it_data;
312 size[2] = sizeof(struct mds_rec_link);
313 size[3] = de->d_name.len + 1;
314 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
319 /* pack the intent */
320 lit = lustre_msg_buf(req->rq_reqmsg, 1);
321 lit->opc = NTOH__u64((__u64)it->it_op);
323 /* pack the intended request */
324 mds_link_pack(req, 2, old_de->d_inode, dir,
325 de->d_name.name, de->d_name.len);
326 req->rq_replen = lustre_msg_size(3, repsize);
327 } else if (it->it_op == IT_UNLINK || it->it_op == IT_RMDIR) {
328 size[2] = sizeof(struct mds_rec_unlink);
329 size[3] = de->d_name.len + 1;
330 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
335 /* pack the intent */
336 lit = lustre_msg_buf(req->rq_reqmsg, 1);
337 lit->opc = NTOH__u64((__u64)it->it_op);
339 /* pack the intended request */
340 mds_unlink_pack(req, 2, dir, NULL,
341 it->it_op == IT_UNLINK ? S_IFREG : S_IFDIR,
342 de->d_name.name, de->d_name.len);
344 req->rq_replen = lustre_msg_size(3, repsize);
345 } else if (it->it_op & (IT_GETATTR | IT_RENAME | IT_LINK |
346 IT_OPEN | IT_SETATTR | IT_LOOKUP | IT_READLINK)) {
347 size[2] = sizeof(struct mds_body);
348 size[3] = de->d_name.len + 1;
350 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 4,
355 /* pack the intent */
356 lit = lustre_msg_buf(req->rq_reqmsg, 1);
357 lit->opc = NTOH__u64((__u64)it->it_op);
359 /* pack the intended request */
360 mds_getattr_pack(req, 2, dir, de->d_name.name, de->d_name.len);
362 /* get ready for the reply */
363 req->rq_replen = lustre_msg_size(3, repsize);
364 } else if (it->it_op == IT_READDIR) {
365 req = ptlrpc_prep_req(class_conn2cliimp(conn), LDLM_ENQUEUE, 1,
370 /* get ready for the reply */
371 req->rq_replen = lustre_msg_size(1, repsize);
377 rc = ldlm_cli_enqueue(conn, req, obddev->obd_namespace, NULL, res_id,
378 lock_type, NULL, 0, lock_mode, &flags,
379 ldlm_completion_ast, mdc_blocking_ast, data,
382 if (it->it_op != IT_READDIR) {
383 /* XXX This should become a lustre_msg flag, but for now... */
384 __u32 *opp = lustre_msg_buf(req->rq_reqmsg, 2);
385 *opp |= REINT_REPLAYING;
389 /* This can go when we're sure that this can never happen */
392 if (rc == ELDLM_LOCK_ABORTED) {
394 memset(lockh, 0, sizeof(*lockh));
396 } else if (rc != 0) {
397 CERROR("ldlm_cli_enqueue: %d\n", rc);
400 /* The server almost certainly gave us a lock other than the one
401 * that we asked for. If we already have a matching lock, then
402 * cancel this one--we don't need two. */
403 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
404 struct lustre_handle lockh2;
407 LDLM_DEBUG(lock, "matching against this");
409 memcpy(&lockh2, lockh, sizeof(lockh2));
410 if (ldlm_lock_match(NULL, NULL, LDLM_PLAIN, NULL, 0, LCK_NL,
412 /* We already have a lock; cancel the old one */
413 ldlm_lock_decref(lockh, lock_mode);
414 ldlm_cli_cancel(lockh);
415 memcpy(lockh, &lockh2, sizeof(lockh2));
420 /* On replay, we don't want the lock granted. */
421 lockreq = lustre_msg_buf(req->rq_reqmsg, 0);
422 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
424 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
425 it->it_disposition = (int) dlm_rep->lock_policy_res1;
426 it->it_status = (int) dlm_rep->lock_policy_res2;
427 it->it_lock_mode = lock_mode;
433 int mdc_cancel_unused(struct lustre_handle *conn, struct inode *inode,
436 __u64 res_id[RES_NAME_SIZE] = {inode->i_ino, inode->i_generation};
437 struct obd_device *obddev = class_conn2obd(conn);
439 RETURN(ldlm_cli_cancel_unused(obddev->obd_namespace, res_id, flags));
442 struct replay_open_data {
443 struct lustre_handle *fh;
446 static void mdc_replay_open(struct ptlrpc_request *req)
449 struct replay_open_data *saved;
450 struct mds_body *body = lustre_msg_buf(req->rq_repmsg, 0);
452 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MDS_OPEN_HAS_EA)
457 saved = lustre_msg_buf(req->rq_reqmsg, offset);
458 mds_unpack_body(body);
459 CDEBUG(D_HA, "updating from "LPD64"/"LPD64" to "LPD64"/"LPD64"\n",
460 saved->fh->addr, saved->fh->cookie,
461 body->handle.addr, body->handle.cookie);
462 memcpy(saved->fh, &body->handle, sizeof(body->handle));
465 int mdc_open(struct lustre_handle *conn, obd_id ino, int type, int flags,
466 struct lov_mds_md *lmm, int lmm_size, struct lustre_handle *fh,
467 struct ptlrpc_request **request)
469 struct mds_body *body;
470 struct replay_open_data *replay_data;
471 int rc, size[3] = {sizeof(*body), sizeof(*replay_data)}, bufcount = 2;
472 struct ptlrpc_request *req;
475 if (lmm && lmm_size) {
477 size[2] = size[1]; /* shuffle the spare data along */
481 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_OPEN, bufcount, size,
484 GOTO(out, rc = -ENOMEM);
486 req->rq_flags |= PTL_RPC_FL_REPLAY;
487 body = lustre_msg_buf(req->rq_reqmsg, 0);
489 ll_ino2fid(&body->fid1, ino, 0, type);
490 body->flags = HTON__u32(flags);
491 memcpy(&body->handle, fh, sizeof(body->handle));
493 if (lmm && lmm_size) {
494 CDEBUG(D_INODE, "sending %u bytes MD for ino LPU64\n",
496 lustre_msg_set_op_flags(req->rq_reqmsg, MDS_OPEN_HAS_EA);
497 memcpy(lustre_msg_buf(req->rq_reqmsg, 1), lmm, lmm_size);
498 body->flags |= HTON__u32(OBD_MD_FLEASIZE);
501 req->rq_replen = lustre_msg_size(1, size);
503 rc = ptlrpc_queue_wait(req);
504 rc = ptlrpc_check_status(req, rc);
506 body = lustre_msg_buf(req->rq_repmsg, 0);
507 mds_unpack_body(body);
508 memcpy(fh, &body->handle, sizeof(*fh));
511 /* If open is replayed, we need to fix up the fh. */
512 req->rq_replay_cb = mdc_replay_open;
513 replay_data = lustre_msg_buf(req->rq_reqmsg, lmm ? 2 : 1);
514 replay_data->fh = fh;
522 int mdc_close(struct lustre_handle *conn, obd_id ino, int type,
523 struct lustre_handle *fh, struct ptlrpc_request **request)
525 struct mds_body *body;
526 int rc, size = sizeof(*body);
527 struct ptlrpc_request *req;
529 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_CLOSE, 1, &size,
532 GOTO(out, rc = -ENOMEM);
534 body = lustre_msg_buf(req->rq_reqmsg, 0);
535 ll_ino2fid(&body->fid1, ino, 0, type);
536 memcpy(&body->handle, fh, sizeof(body->handle));
538 req->rq_replen = lustre_msg_size(0, NULL);
540 rc = ptlrpc_queue_wait(req);
541 rc = ptlrpc_check_status(req, rc);
549 int mdc_readpage(struct lustre_handle *conn, obd_id ino, int type, __u64 offset,
550 char *addr, struct ptlrpc_request **request)
552 struct ptlrpc_connection *connection =
553 client_conn2cli(conn)->cl_import.imp_connection;
554 struct ptlrpc_request *req = NULL;
555 struct ptlrpc_bulk_desc *desc = NULL;
556 struct ptlrpc_bulk_page *bulk = NULL;
557 struct mds_body *body;
558 int rc, size = sizeof(*body);
561 CDEBUG(D_INODE, "inode: %ld\n", (long)ino);
563 desc = ptlrpc_prep_bulk(connection);
565 GOTO(out, rc = -ENOMEM);
567 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_READPAGE, 1, &size,
570 GOTO(out2, rc = -ENOMEM);
572 bulk = ptlrpc_prep_bulk_page(desc);
573 bulk->bp_buflen = PAGE_SIZE;
575 bulk->bp_xid = req->rq_xid;
576 desc->bd_portal = MDS_BULK_PORTAL;
578 rc = ptlrpc_register_bulk(desc);
580 CERROR("couldn't setup bulk sink: error %d.\n", rc);
584 body = lustre_msg_buf(req->rq_reqmsg, 0);
586 body->fid1.f_type = type;
589 req->rq_replen = lustre_msg_size(1, &size);
590 rc = ptlrpc_queue_wait(req);
591 rc = ptlrpc_check_status(req, rc);
593 ptlrpc_abort_bulk(desc);
596 body = lustre_msg_buf(req->rq_repmsg, 0);
597 mds_unpack_body(body);
602 ptlrpc_free_bulk(desc);
608 static int mdc_statfs(struct lustre_handle *conn, struct obd_statfs *osfs)
610 struct ptlrpc_request *req;
611 int rc, size = sizeof(*osfs);
614 req = ptlrpc_prep_req(class_conn2cliimp(conn), MDS_STATFS, 0, NULL,
619 req->rq_replen = lustre_msg_size(1, &size);
621 rc = ptlrpc_queue_wait(req);
622 rc = ptlrpc_check_status(req, rc);
627 obd_statfs_unpack(osfs, lustre_msg_buf(req->rq_repmsg, 0));
631 ptlrpc_req_finished(req);
635 int mdc_attach(struct obd_device *dev, obd_count len, void *data)
637 return lprocfs_reg_obd(dev, status_var_nm_1, dev);
640 int mdc_detach(struct obd_device *dev)
642 return lprocfs_dereg_obd(dev);
644 struct obd_ops mdc_obd_ops = {
645 o_attach: mdc_attach,
646 o_detach: mdc_detach,
647 o_setup: client_obd_setup,
648 o_cleanup: client_obd_cleanup,
649 o_connect: client_obd_connect,
650 o_disconnect: client_obd_disconnect,
651 o_statfs: mdc_statfs,
654 static int __init ptlrpc_request_init(void)
656 return class_register_type(&mdc_obd_ops, status_class_var,
660 static void __exit ptlrpc_request_exit(void)
662 class_unregister_type(LUSTRE_MDC_NAME);
665 MODULE_AUTHOR("Cluster File Systems <info@clusterfs.com>");
666 MODULE_DESCRIPTION("Lustre Metadata Client v1.0");
667 MODULE_LICENSE("GPL");
669 EXPORT_SYMBOL(d_delete_aliases);
670 EXPORT_SYMBOL(mdc_getstatus);
671 EXPORT_SYMBOL(mdc_getlovinfo);
672 EXPORT_SYMBOL(mdc_enqueue);
673 EXPORT_SYMBOL(mdc_cancel_unused);
674 EXPORT_SYMBOL(mdc_getattr);
675 EXPORT_SYMBOL(mdc_create);
676 EXPORT_SYMBOL(mdc_unlink);
677 EXPORT_SYMBOL(mdc_rename);
678 EXPORT_SYMBOL(mdc_link);
679 EXPORT_SYMBOL(mdc_readpage);
680 EXPORT_SYMBOL(mdc_setattr);
681 EXPORT_SYMBOL(mdc_close);
682 EXPORT_SYMBOL(mdc_open);
684 EXPORT_SYMBOL(mdc_store_inode_generation);
686 module_init(ptlrpc_request_init);
687 module_exit(ptlrpc_request_exit);