1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * Lustre Metadata Server (mds) request handler
7 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
11 * Author: Mike Shaver <shaver@clusterfs.com>
13 * This file is part of Lustre, http://www.lustre.org.
15 * Lustre is free software; you can redistribute it and/or
16 * modify it under the terms of version 2 of the GNU General Public
17 * License as published by the Free Software Foundation.
19 * Lustre is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Lustre; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/module.h>
35 #include <linux/lustre_mds.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/init.h>
38 #include <linux/obd_class.h>
39 #include <linux/random.h>
41 #include <linux/jbd.h>
42 #include <linux/ext3_fs.h>
43 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
44 # include <linux/smp_lock.h>
45 # include <linux/buffer_head.h>
46 # include <linux/workqueue.h>
47 # include <linux/mount.h>
49 # include <linux/locks.h>
51 #include <linux/obd_lov.h>
52 #include <linux/obd_ost.h>
53 #include <linux/lustre_mds.h>
54 #include <linux/lustre_fsfilt.h>
55 #include <linux/lustre_snap.h>
56 #include <linux/lprocfs_status.h>
57 #include <linux/lustre_commit_confd.h>
59 #include "mds_internal.h"
61 static int mds_intent_policy(struct ldlm_namespace *ns,
62 struct ldlm_lock **lockp, void *req_cookie,
63 ldlm_mode_t mode, int flags, void *data);
64 static int mds_postsetup(struct obd_device *obd);
65 static int mds_cleanup(struct obd_device *obd, int flags);
67 /* Assumes caller has already pushed into the kernel filesystem context */
68 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
69 loff_t offset, int count)
71 struct ptlrpc_bulk_desc *desc;
72 struct l_wait_info lwi;
74 int rc = 0, npages, i, tmpcount, tmpsize = 0;
77 LASSERT((offset & (PAGE_SIZE - 1)) == 0); /* I'm dubious about this */
79 npages = (count + PAGE_SIZE - 1) >> PAGE_SHIFT;
80 OBD_ALLOC(pages, sizeof(*pages) * npages);
82 GOTO(out, rc = -ENOMEM);
84 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
87 GOTO(out_free, rc = -ENOMEM);
89 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
90 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
92 pages[i] = alloc_pages(GFP_KERNEL, 0);
94 GOTO(cleanup_buf, rc = -ENOMEM);
96 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
99 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
100 tmpsize = tmpcount > PAGE_SIZE ? PAGE_SIZE : tmpcount;
101 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
102 tmpsize, offset, file->f_dentry->d_inode->i_ino,
103 file->f_dentry->d_inode->i_size);
105 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
106 kmap(pages[i]), tmpsize, &offset);
110 GOTO(cleanup_buf, rc = -EIO);
113 LASSERT(desc->bd_nob == count);
115 rc = ptlrpc_start_bulk_transfer(desc);
117 GOTO(cleanup_buf, rc);
119 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
120 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
121 OBD_FAIL_MDS_SENDPAGE, rc);
122 GOTO(abort_bulk, rc);
125 lwi = LWI_TIMEOUT(obd_timeout * HZ / 4, NULL, NULL);
126 rc = l_wait_event(desc->bd_waitq, !ptlrpc_bulk_active(desc), &lwi);
127 LASSERT (rc == 0 || rc == -ETIMEDOUT);
130 if (desc->bd_success &&
131 desc->bd_nob_transferred == count)
132 GOTO(cleanup_buf, rc);
134 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
137 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
138 (rc == -ETIMEDOUT) ? "timeout" : "network error",
139 desc->bd_nob_transferred, count,
140 req->rq_export->exp_client_uuid.uuid,
141 req->rq_export->exp_connection->c_remote_uuid.uuid);
143 ptlrpc_fail_export(req->rq_export);
147 ptlrpc_abort_bulk (desc);
149 for (i = 0; i < npages; i++)
151 __free_pages(pages[i], 0);
153 ptlrpc_free_bulk(desc);
155 OBD_FREE(pages, sizeof(*pages) * npages);
160 int mds_lock_mode_for_dir(struct obd_device *obd,
161 struct dentry *dentry, int mode)
165 /* any dir access needs couple locks:
166 * 1) on part of dir we gonna lookup/modify in
167 * 2) on a whole dir to protect it from concurrent splitting
168 * and to flush client's cache for readdir()
169 * so, for a given mode and dentry this routine decides what
170 * lock mode to use for lock #2:
171 * 1) if caller's gonna lookup in dir then we need to protect
172 * dir from being splitted only - LCK_CR
173 * 2) if caller's gonna modify dir then we need to protect
174 * dir from being splitted and to flush cache - LCK_CW
175 * 3) if caller's gonna modify dir and that dir seems ready
176 * for splitting then we need to protect it from any
177 * type of access (lookup/modify/split) - LCK_EX -bzzz */
179 split = mds_splitting_expected(obd, dentry);
180 if (split == MDS_NO_SPLITTABLE) {
181 /* this inode won't be splitted. so we need not to protect from
182 * just flush client's cache on modification */
187 if (mode == LCK_PR) {
189 } else if (mode == LCK_PW) {
190 /* caller gonna modify directory.we use concurrent
191 write lock here to retract client's cache for readdir */
193 if (split == MDS_EXPECT_SPLIT) {
194 /* splitting possible. serialize any access
195 * the idea is that first one seen dir is
196 * splittable is given exclusive lock and
197 * split directory. caller passes lock mode
198 * to mds_try_to_split_dir() and splitting
199 * would be done with exclusive lock only -bzzz */
200 CDEBUG(D_OTHER, "%s: gonna split %u/%u\n",
202 (unsigned) dentry->d_inode->i_ino,
203 (unsigned) dentry->d_inode->i_generation);
207 CWARN("unexpected lock mode %d\n", mode);
214 /* only valid locked dentries or errors should be returned */
215 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
216 struct vfsmount **mnt, int lock_mode,
217 struct lustre_handle *lockh, int *mode,
218 char *name, int namelen, __u64 lockpart)
220 struct mds_obd *mds = &obd->u.mds;
221 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
222 struct ldlm_res_id res_id = { .name = {0} };
224 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
231 res_id.name[0] = de->d_inode->i_ino;
232 res_id.name[1] = de->d_inode->i_generation;
235 if (name && IS_PDIROPS(de->d_inode)) {
236 ldlm_policy_data_t cpolicy =
237 { .l_inodebits = { MDS_INODELOCK_UPDATE } };
238 LASSERT(mode != NULL);
239 *mode = mds_lock_mode_for_dir(obd, de, lock_mode);
241 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
243 &cpolicy, *mode, &flags,
245 ldlm_completion_ast, NULL, NULL,
246 NULL, 0, NULL, lockh + 1);
247 if (rc != ELDLM_OK) {
249 RETURN(ERR_PTR(-ENOLCK));
254 res_id.name[2] = full_name_hash(name, namelen);
256 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
257 de->d_inode->i_ino, de->d_inode->i_generation,
261 #warning "No PDIROPS support in the kernel"
263 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, res_id,
264 LDLM_IBITS, &policy, lock_mode, &flags,
265 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
266 NULL, 0, NULL, lockh);
267 if (rc != ELDLM_OK) {
269 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
272 ldlm_lock_decref(lockh + 1, LCK_CW);
279 #ifndef DCACHE_DISCONNECTED
280 #define DCACHE_DISCONNECTED DCACHE_NFSD_DISCONNECTED
284 /* Look up an entry by inode number. */
285 /* this function ONLY returns valid dget'd dentries with an initialized inode
287 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
288 struct vfsmount **mnt)
291 unsigned long ino = fid->id;
292 __u32 generation = fid->generation;
294 struct dentry *result;
297 RETURN(ERR_PTR(-ESTALE));
299 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
301 CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
302 ino, generation, mds->mds_sb);
304 /* under ext3 this is neither supposed to return bad inodes
306 result = ll_lookup_one_len(fid_name, mds->mds_fid_de, strlen(fid_name));
310 inode = result->d_inode;
312 RETURN(ERR_PTR(-ENOENT));
314 /* here we disabled generation check, as root inode i_generation
315 * of cache mds and real mds are different. */
316 if (inode->i_ino != mds->mds_rootfid.id && generation &&
317 inode->i_generation != generation) {
318 /* we didn't find the right inode.. */
319 CERROR("bad inode %lu, link: %lu ct: %d or generation %u/%u\n",
320 inode->i_ino, (unsigned long)inode->i_nlink,
321 atomic_read(&inode->i_count), inode->i_generation,
324 RETURN(ERR_PTR(-ENOENT));
328 *mnt = mds->mds_vfsmnt;
336 /* Establish a connection to the MDS.
338 * This will set up an export structure for the client to hold state data
339 * about that client, like open files, the last operation number it did
340 * on the server, etc.
342 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
343 struct obd_uuid *cluuid, unsigned long connect_flags)
345 struct obd_export *exp;
346 struct mds_export_data *med; /* */
347 struct mds_client_data *mcd;
351 if (!conn || !obd || !cluuid)
354 /* XXX There is a small race between checking the list and adding a
355 * new connection for the same UUID, but the real threat (list
356 * corruption when multiple different clients connect) is solved.
358 * There is a second race between adding the export to the list,
359 * and filling in the client data below. Hence skipping the case
360 * of NULL mcd above. We should already be controlling multiple
361 * connects at the client, and we can't hold the spinlock over
362 * memory allocations without risk of deadlocking.
364 rc = class_connect(conn, obd, cluuid);
367 exp = class_conn2export(conn);
369 med = &exp->exp_mds_data;
371 OBD_ALLOC(mcd, sizeof(*mcd));
373 CERROR("mds: out of memory for client data\n");
374 GOTO(out, rc = -ENOMEM);
377 memcpy(mcd->mcd_uuid, cluuid, sizeof(mcd->mcd_uuid));
380 rc = mds_client_add(obd, &obd->u.mds, med, -1);
384 if (!(connect_flags & OBD_OPT_MDS_CONNECTION)) {
385 struct mds_obd *mds = &obd->u.mds;
386 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)) {
387 atomic_inc(&mds->mds_real_clients);
388 CDEBUG(D_OTHER,"%s: peer from %s is real client (%d)\n",
389 obd->obd_name, exp->exp_client_uuid.uuid,
390 atomic_read(&mds->mds_real_clients));
391 exp->exp_flags |= OBD_OPT_REAL_CLIENT;
393 if (mds->mds_lmv_name)
394 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
399 OBD_FREE(mcd, sizeof(*mcd));
400 class_disconnect(exp, 0);
402 class_export_put(exp);
407 static int mds_init_export(struct obd_export *exp)
409 struct mds_export_data *med = &exp->exp_mds_data;
411 INIT_LIST_HEAD(&med->med_open_head);
412 spin_lock_init(&med->med_open_lock);
416 static int mds_destroy_export(struct obd_export *export)
418 struct mds_export_data *med;
419 struct obd_device *obd = export->exp_obd;
420 struct lvfs_run_ctxt saved;
424 med = &export->exp_mds_data;
425 target_destroy_export(export);
427 if (obd_uuid_equals(&export->exp_client_uuid, &obd->obd_uuid))
430 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
432 /* Close any open files (which may also cause orphan unlinking). */
433 spin_lock(&med->med_open_lock);
434 while (!list_empty(&med->med_open_head)) {
435 struct list_head *tmp = med->med_open_head.next;
436 struct mds_file_data *mfd =
437 list_entry(tmp, struct mds_file_data, mfd_list);
438 BDEVNAME_DECLARE_STORAGE(btmp);
440 /* bug 1579: fix force-closing for 2.5 */
441 struct dentry *dentry = mfd->mfd_dentry;
443 list_del(&mfd->mfd_list);
444 spin_unlock(&med->med_open_lock);
446 /* If you change this message, be sure to update
447 * replay_single:test_46 */
448 CERROR("force closing client file handle for %*s (%s:%lu)\n",
449 dentry->d_name.len, dentry->d_name.name,
450 ll_bdevname(dentry->d_inode->i_sb, btmp),
451 dentry->d_inode->i_ino);
452 rc = mds_mfd_close(NULL, obd, mfd,
453 !(export->exp_flags & OBD_OPT_FAILOVER));
456 CDEBUG(D_INODE, "Error closing file: %d\n", rc);
457 spin_lock(&med->med_open_lock);
459 spin_unlock(&med->med_open_lock);
460 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
463 mds_client_free(export, !(export->exp_flags & OBD_OPT_FAILOVER));
468 static int mds_disconnect(struct obd_export *exp, int flags)
470 struct obd_device *obd;
472 unsigned long irqflags;
477 class_export_get(exp);
479 obd = class_exp2obd(exp);
481 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
482 exp->exp_handle.h_cookie);
487 if (!(exp->exp_flags & OBD_OPT_REAL_CLIENT)
488 && !atomic_read(&mds->mds_real_clients)) {
489 /* there was no client at all */
490 mds_lmv_disconnect(obd, flags);
493 if ((exp->exp_flags & OBD_OPT_REAL_CLIENT)
494 && atomic_dec_and_test(&mds->mds_real_clients)) {
495 /* time to drop LMV connections */
496 CDEBUG(D_OTHER, "%s: last real client %s disconnected. "
497 "Disconnnect from LMV now\n",
498 obd->obd_name, exp->exp_client_uuid.uuid);
499 mds_lmv_disconnect(obd, flags);
502 spin_lock_irqsave(&exp->exp_lock, irqflags);
503 exp->exp_flags = flags;
504 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
506 /* Disconnect early so that clients can't keep using export */
507 rc = class_disconnect(exp, flags);
508 ldlm_cancel_locks_for_export(exp);
510 /* complete all outstanding replies */
511 spin_lock_irqsave(&exp->exp_lock, irqflags);
512 while (!list_empty(&exp->exp_outstanding_replies)) {
513 struct ptlrpc_reply_state *rs =
514 list_entry(exp->exp_outstanding_replies.next,
515 struct ptlrpc_reply_state, rs_exp_list);
516 struct ptlrpc_service *svc = rs->rs_srv_ni->sni_service;
518 spin_lock(&svc->srv_lock);
519 list_del_init(&rs->rs_exp_list);
520 ptlrpc_schedule_difficult_reply(rs);
521 spin_unlock(&svc->srv_lock);
523 spin_unlock_irqrestore(&exp->exp_lock, irqflags);
525 class_export_put(exp);
529 static int mds_getstatus(struct ptlrpc_request *req)
531 struct mds_obd *mds = mds_req2mds(req);
532 struct mds_body *body;
533 int rc, size = sizeof(*body);
536 rc = lustre_pack_reply(req, 1, &size, NULL);
537 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_GETSTATUS_PACK)) {
538 CERROR("mds: out of memory for message: size=%d\n", size);
539 req->rq_status = -ENOMEM; /* superfluous? */
543 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
544 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
546 /* the last_committed and last_xid fields are filled in for all
547 * replies already - no need to do so here also.
552 int mds_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
553 void *data, int flag)
558 if (flag == LDLM_CB_CANCELING) {
559 /* Don't need to do anything here. */
563 /* XXX layering violation! -phil */
564 l_lock(&lock->l_resource->lr_namespace->ns_lock);
565 /* Get this: if mds_blocking_ast is racing with mds_intent_policy,
566 * such that mds_blocking_ast is called just before l_i_p takes the
567 * ns_lock, then by the time we get the lock, we might not be the
568 * correct blocking function anymore. So check, and return early, if
570 if (lock->l_blocking_ast != mds_blocking_ast) {
571 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
575 lock->l_flags |= LDLM_FL_CBPENDING;
576 do_ast = (!lock->l_readers && !lock->l_writers);
577 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
580 struct lustre_handle lockh;
583 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
584 ldlm_lock2handle(lock, &lockh);
585 rc = ldlm_cli_cancel(&lockh);
587 CERROR("ldlm_cli_cancel: %d\n", rc);
589 LDLM_DEBUG(lock, "Lock still has references, will be "
595 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
603 rc = fsfilt_get_md(obd, inode, md, *size);
608 CERROR("Error %d reading eadata for ino %lu\n",
613 if (S_ISREG(inode->i_mode))
614 rc = mds_convert_lov_ea(obd, inode, md, lmm_size);
615 if (S_ISDIR(inode->i_mode))
616 rc = mds_convert_mea_ea(obd, inode, md, lmm_size);
630 /* Call with lock=1 if you want mds_pack_md to take the i_sem.
631 * Call with lock=0 if the caller has already taken the i_sem. */
632 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
633 struct mds_body *body, struct inode *inode, int lock)
635 struct mds_obd *mds = &obd->u.mds;
641 lmm = lustre_msg_buf(msg, offset, 0);
643 /* Some problem with getting eadata when I sized the reply
645 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
649 lmm_size = msg->buflens[offset];
651 /* I don't really like this, but it is a sanity check on the client
652 * MD request. However, if the client doesn't know how much space
653 * to reserve for the MD, it shouldn't be bad to have too much space.
655 if (lmm_size > mds->mds_max_mdsize) {
656 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
657 inode->i_ino, lmm_size, mds->mds_max_mdsize);
661 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock);
663 if (S_ISDIR(inode->i_mode))
664 body->valid |= OBD_MD_FLDIREA;
666 body->valid |= OBD_MD_FLEASIZE;
667 body->eadatasize = lmm_size;
674 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
675 struct ptlrpc_request *req,
676 struct mds_body *reqbody, int reply_off)
678 struct mds_body *body;
679 struct inode *inode = dentry->d_inode;
683 if (inode == NULL && !(dentry->d_flags & DCACHE_CROSS_REF))
686 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
687 LASSERT(body != NULL); /* caller prepped reply */
689 if (dentry->d_flags & DCACHE_CROSS_REF) {
690 CDEBUG(D_OTHER, "cross reference: %lu/%lu/%lu\n",
691 (unsigned long) dentry->d_mdsnum,
692 (unsigned long) dentry->d_inum,
693 (unsigned long) dentry->d_generation);
694 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
695 body->fid1.id = dentry->d_inum;
696 body->fid1.mds = dentry->d_mdsnum;
697 body->fid1.generation = dentry->d_generation;
700 mds_pack_inode2fid(obd, &body->fid1, inode);
701 mds_pack_inode2body(obd, body, inode);
703 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
704 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
705 rc = mds_pack_md(obd, req->rq_repmsg, reply_off + 1, body,
708 /* If we have LOV EA data, the OST holds size, atime, mtime */
709 if (!(body->valid & OBD_MD_FLEASIZE) &&
710 !(body->valid & OBD_MD_FLDIREA))
711 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
712 OBD_MD_FLATIME | OBD_MD_FLMTIME);
713 } else if (S_ISLNK(inode->i_mode) &&
714 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
715 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off + 1,0);
718 LASSERT (symname != NULL); /* caller prepped reply */
719 len = req->rq_repmsg->buflens[reply_off + 1];
721 rc = inode->i_op->readlink(dentry, symname, len);
723 CERROR("readlink failed: %d\n", rc);
724 } else if (rc != len - 1) {
725 CERROR ("Unexpected readlink rc %d: expecting %d\n",
729 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
730 body->valid |= OBD_MD_LINKNAME;
731 body->eadatasize = rc + 1;
732 symname[rc] = 0; /* NULL terminate */
740 static int mds_getattr_pack_msg_cf(struct ptlrpc_request *req,
741 struct dentry *dentry,
744 int rc = 0, size[1] = {sizeof(struct mds_body)};
747 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
748 CERROR("failed MDS_GETATTR_PACK test\n");
749 req->rq_status = -ENOMEM;
750 GOTO(out, rc = -ENOMEM);
753 rc = lustre_pack_reply(req, 1, size, NULL);
755 CERROR("out of memory\n");
756 GOTO(out, req->rq_status = rc);
764 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
767 struct mds_obd *mds = mds_req2mds(req);
768 struct mds_body *body;
769 int rc = 0, size[2] = {sizeof(*body)}, bufcount = 1;
772 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof (*body));
773 LASSERT(body != NULL); /* checked by caller */
774 LASSERT_REQSWABBED(req, offset); /* swabbed by caller */
776 if ((S_ISREG(inode->i_mode) && (body->valid & OBD_MD_FLEASIZE)) ||
777 (S_ISDIR(inode->i_mode) && (body->valid & OBD_MD_FLDIREA))) {
780 rc = fsfilt_get_md(req->rq_export->exp_obd, inode, NULL, 0);
782 CDEBUG(D_INODE, "got %d bytes MD data for inode %lu\n",
786 CERROR("error getting inode %lu MD: rc = %d\n",
789 } else if (rc > mds->mds_max_mdsize) {
791 CERROR("MD size %d larger than maximum possible %u\n",
792 rc, mds->mds_max_mdsize);
797 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
798 if (inode->i_size + 1 != body->eadatasize)
799 CERROR("symlink size: %Lu, reply space: %d\n",
800 inode->i_size + 1, body->eadatasize);
801 size[bufcount] = min_t(int, inode->i_size+1, body->eadatasize);
803 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
804 inode->i_size + 1, body->eadatasize);
807 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
808 CERROR("failed MDS_GETATTR_PACK test\n");
809 req->rq_status = -ENOMEM;
810 GOTO(out, rc = -ENOMEM);
813 rc = lustre_pack_reply(req, bufcount, size, NULL);
815 CERROR("out of memory\n");
816 GOTO(out, req->rq_status = rc);
824 int mds_check_mds_num(struct obd_device *obd, struct inode* inode,
825 char *name, int namelen)
827 struct mea *mea = NULL;
828 int mea_size, rc = 0;
831 rc = mds_get_lmv_attr(obd, inode, &mea, &mea_size);
835 /* dir is already splitted, check if requested filename
836 * should live at this MDS or at another one */
838 i = mea_name2idx(mea, name, namelen - 1);
839 if (mea->mea_master != mea->mea_fids[i].mds) {
841 "inapropriate MDS(%d) for %s. should be %d(%d)\n",
842 mea->mea_master, name, mea->mea_fids[i].mds, i);
848 OBD_FREE(mea, mea_size);
852 static int mds_getattr_name(int offset, struct ptlrpc_request *req,
853 struct lustre_handle *child_lockh, int child_part)
855 struct obd_device *obd = req->rq_export->exp_obd;
856 struct ldlm_reply *rep = NULL;
857 struct lvfs_run_ctxt saved;
858 struct mds_body *body;
859 struct dentry *dparent = NULL, *dchild = NULL;
860 struct lvfs_ucred uc;
861 struct lustre_handle parent_lockh[2];
862 int namesize, update_mode;
863 int rc = 0, cleanup_phase = 0, resent_req = 0;
864 struct clonefs_info *clone_info = NULL;
868 LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
870 MDS_UPDATE_COUNTER((&obd->u.mds), MDS_GETATTR_NAME_COUNT);
872 /* Swab now, before anyone looks inside the request */
873 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
874 lustre_swab_mds_body);
876 CERROR("Can't swab mds_body\n");
877 GOTO(cleanup, rc = -EFAULT);
880 LASSERT_REQSWAB(req, offset + 1);
881 name = lustre_msg_string(req->rq_reqmsg, offset + 1, 0);
883 CERROR("Can't unpack name\n");
884 GOTO(cleanup, rc = -EFAULT);
887 clone_info = lustre_swab_reqbuf(req, offset + 2, sizeof(*clone_info),
888 lustre_swab_clonefs_info);
890 CDEBUG(D_INFO,"getattr name %s clone_info index %d \n", name,
891 clone_info->clone_index);
894 namesize = req->rq_reqmsg->buflens[offset + 1];
896 LASSERT (offset == 0 || offset == 2);
897 /* if requests were at offset 2, the getattr reply goes back at 1 */
899 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
903 uc.luc_fsuid = body->fsuid;
904 uc.luc_fsgid = body->fsgid;
905 uc.luc_cap = body->capability;
906 uc.luc_suppgid1 = body->suppgid;
907 uc.luc_suppgid2 = -1;
908 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
909 cleanup_phase = 1; /* kernel context */
910 intent_set_disposition(rep, DISP_LOOKUP_EXECD);
912 LASSERT(namesize > 0);
914 /* we have no dentry here, drop LOOKUP bit */
915 child_part &= ~MDS_INODELOCK_LOOKUP;
916 CDEBUG(D_OTHER, "%s: request to retrieve attrs for %lu/%lu\n",
917 obd->obd_name, (unsigned long) body->fid1.id,
918 (unsigned long) body->fid1.generation);
919 dchild = mds_fid2locked_dentry(obd, &body->fid1, NULL, LCK_PR,
920 parent_lockh, &update_mode,
921 NULL, 0, child_part);
922 if (IS_ERR(dchild)) {
923 CERROR("can't find inode: %d\n", (int) PTR_ERR(dchild));
924 GOTO(cleanup, rc = PTR_ERR(dchild));
926 memcpy(child_lockh, parent_lockh, sizeof(parent_lockh[0]));
928 if (parent_lockh[1].cookie)
929 ldlm_lock_decref(parent_lockh + 1, update_mode);
936 /* FIXME: handle raw lookup */
937 if (body->valid == OBD_MD_FLID) {
938 struct mds_obd *mds = &obd->u.mds;
939 struct mds_body *mds_reply;
940 int size = sizeof(*mds_reply);
943 dparent = mds_fid2dentry(mds, &body->fid1, NULL);
944 if (IS_ERR(dparent)) {
945 rc = PTR_ERR(dparent);
948 // The user requested ONLY the inode number, so do a raw lookup
949 rc = lustre_pack_reply(req, 1, &size, NULL);
951 CERROR("out of memory\n");
955 dir = dparent->d_inode;
956 LASSERT(dir->i_op->lookup_raw != NULL);
957 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
959 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
961 mds_reply->fid1.id = inum;
962 mds_reply->valid = OBD_MD_FLID;
967 if (child_lockh->cookie != 0) {
968 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
972 if (resent_req == 0) {
973 rc = mds_get_parent_child_locked(obd, &obd->u.mds, &body->fid1,
974 parent_lockh, &dparent,
975 LCK_PR, MDS_INODELOCK_LOOKUP,
976 &update_mode, name, namesize,
977 child_lockh, &dchild, LCK_PR,
978 child_part, clone_info);
982 /* let's make sure this name should leave on this mds node */
983 rc = mds_check_mds_num(obd, dparent->d_inode, name, namesize);
987 struct ldlm_lock *granted_lock;
988 struct ll_fid child_fid;
989 struct ldlm_resource *res;
990 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
991 granted_lock = ldlm_handle2lock(child_lockh);
992 LASSERT(granted_lock);
994 res = granted_lock->l_resource;
995 child_fid.id = res->lr_name.name[0];
996 child_fid.generation = res->lr_name.name[1];
997 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
999 LDLM_LOCK_PUT(granted_lock);
1002 cleanup_phase = 2; /* dchild, dparent, locks */
1006 if (!DENTRY_VALID(dchild)) {
1007 intent_set_disposition(rep, DISP_LOOKUP_NEG);
1008 /* in the intent case, the policy clears this error:
1009 the disposition is enough */
1013 intent_set_disposition(rep, DISP_LOOKUP_POS);
1016 if (req->rq_repmsg == NULL) {
1017 if (dchild->d_flags & DCACHE_CROSS_REF)
1018 rc = mds_getattr_pack_msg_cf(req, dchild, offset);
1020 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
1022 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1027 rc = mds_getattr_internal(obd, dchild, req, body, offset);
1028 GOTO(cleanup, rc); /* returns the lock to the client */
1031 switch (cleanup_phase) {
1033 if (resent_req == 0) {
1034 if (rc && DENTRY_VALID(dchild))
1035 ldlm_lock_decref(child_lockh, LCK_PR);
1037 ldlm_lock_decref(parent_lockh, LCK_PR);
1039 if (parent_lockh[1].cookie != 0)
1040 ldlm_lock_decref(parent_lockh + 1,
1049 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1055 static int mds_getattr(int offset, struct ptlrpc_request *req)
1057 struct mds_obd *mds = mds_req2mds(req);
1058 struct obd_device *obd = req->rq_export->exp_obd;
1059 struct lvfs_run_ctxt saved;
1061 struct mds_body *body;
1062 struct lvfs_ucred uc;
1066 body = lustre_swab_reqbuf (req, offset, sizeof (*body),
1067 lustre_swab_mds_body);
1069 CERROR ("Can't unpack body\n");
1073 MDS_UPDATE_COUNTER(mds, MDS_GETATTR_COUNT);
1075 uc.luc_fsuid = body->fsuid;
1076 uc.luc_fsgid = body->fsgid;
1077 uc.luc_cap = body->capability;
1078 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1079 de = mds_fid2dentry(mds, &body->fid1, NULL);
1081 rc = req->rq_status = -ENOENT;
1082 GOTO(out_pop, PTR_ERR(de));
1085 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1087 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1091 req->rq_status = mds_getattr_internal(obd, de, req, body, 0);
1096 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1101 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1102 unsigned long max_age)
1106 spin_lock(&obd->obd_osfs_lock);
1107 rc = fsfilt_statfs(obd, obd->u.mds.mds_sb, max_age);
1109 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1110 spin_unlock(&obd->obd_osfs_lock);
1115 static int mds_statfs(struct ptlrpc_request *req)
1117 struct obd_device *obd = req->rq_export->exp_obd;
1118 int rc, size = sizeof(struct obd_statfs);
1121 rc = lustre_pack_reply(req, 1, &size, NULL);
1122 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK)) {
1123 CERROR("mds: statfs lustre_pack_reply failed: rc = %d\n", rc);
1127 MDS_UPDATE_COUNTER((&obd->u.mds), MDS_STATFS_COUNT);
1129 /* We call this so that we can cache a bit - 1 jiffie worth */
1130 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, 0, size),
1133 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1139 req->rq_status = rc;
1143 static int mds_sync(struct ptlrpc_request *req)
1145 struct obd_device *obd = req->rq_export->exp_obd;
1146 struct mds_obd *mds = &obd->u.mds;
1147 struct mds_body *body;
1148 int rc, size = sizeof(*body);
1151 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
1153 GOTO(out, rc = -EPROTO);
1155 rc = lustre_pack_reply(req, 1, &size, NULL);
1156 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK)) {
1157 CERROR("fsync lustre_pack_reply failed: rc = %d\n", rc);
1161 if (body->fid1.id == 0) {
1162 /* a fid of zero is taken to mean "sync whole filesystem" */
1163 rc = fsfilt_sync(obd, mds->mds_sb);
1167 /* just any file to grab fsync method - "file" arg unused */
1168 struct file *file = mds->mds_rcvd_filp;
1171 de = mds_fid2dentry(mds, &body->fid1, NULL);
1173 GOTO(out, rc = PTR_ERR(de));
1175 rc = file->f_op->fsync(NULL, de, 1);
1180 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1181 mds_pack_inode2fid(obd, &body->fid1, de->d_inode);
1182 mds_pack_inode2body(obd, body, de->d_inode);
1185 req->rq_status = rc;
1189 /* mds_readpage does not take a DLM lock on the inode, because the client must
1190 * already have a PR lock.
1192 * If we were to take another one here, a deadlock will result, if another
1193 * thread is already waiting for a PW lock. */
1194 static int mds_readpage(struct ptlrpc_request *req)
1196 struct obd_device *obd = req->rq_export->exp_obd;
1197 struct vfsmount *mnt;
1200 struct mds_body *body, *repbody;
1201 struct lvfs_run_ctxt saved;
1202 int rc, size = sizeof(*repbody);
1203 struct lvfs_ucred uc;
1206 rc = lustre_pack_reply(req, 1, &size, NULL);
1207 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK)) {
1208 CERROR("mds: out of memory\n");
1209 GOTO(out, rc = -ENOMEM);
1212 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_mds_body);
1214 GOTO (out, rc = -EFAULT);
1216 uc.luc_fsuid = body->fsuid;
1217 uc.luc_fsgid = body->fsgid;
1218 uc.luc_cap = body->capability;
1219 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1220 de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
1222 GOTO(out_pop, rc = PTR_ERR(de));
1224 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1226 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1227 /* note: in case of an error, dentry_open puts dentry */
1229 GOTO(out_pop, rc = PTR_ERR(file));
1231 /* body->size is actually the offset -eeb */
1232 if ((body->size & (de->d_inode->i_blksize - 1)) != 0) {
1233 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1234 body->size, de->d_inode->i_blksize);
1235 GOTO(out_file, rc = -EFAULT);
1238 /* body->nlink is actually the #bytes to read -eeb */
1239 if (body->nlink & (de->d_inode->i_blksize - 1)) {
1240 CERROR("size %u is not multiple of blocksize %lu\n",
1241 body->nlink, de->d_inode->i_blksize);
1242 GOTO(out_file, rc = -EFAULT);
1245 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*repbody));
1246 repbody->size = file->f_dentry->d_inode->i_size;
1247 repbody->valid = OBD_MD_FLSIZE;
1249 /* to make this asynchronous make sure that the handling function
1250 doesn't send a reply when this function completes. Instead a
1251 callback function would send the reply */
1252 /* body->size is actually the offset -eeb */
1253 rc = mds_sendpage(req, file, body->size, body->nlink);
1256 filp_close(file, 0);
1258 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1260 req->rq_status = rc;
1264 int mds_reint(struct ptlrpc_request *req, int offset,
1265 struct lustre_handle *lockh)
1267 struct mds_update_record *rec; /* 116 bytes on the stack? no sir! */
1271 OBD_ALLOC(rec, sizeof(*rec));
1275 rc = mds_update_unpack(req, offset, rec);
1276 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1277 CERROR("invalid record\n");
1278 GOTO(out, req->rq_status = -EINVAL);
1280 /* rc will be used to interrupt a for loop over multiple records */
1281 rc = mds_reint_rec(rec, offset, req, lockh);
1283 OBD_FREE(rec, sizeof(*rec));
1287 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1288 struct obd_device *obd, int *process)
1290 switch (req->rq_reqmsg->opc) {
1291 case MDS_CONNECT: /* This will never get here, but for completeness. */
1292 case OST_CONNECT: /* This will never get here, but for completeness. */
1293 case MDS_DISCONNECT:
1294 case OST_DISCONNECT:
1299 case MDS_SYNC: /* used in unmounting */
1304 *process = target_queue_recovery_request(req, obd);
1308 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1310 /* XXX what should we set rq_status to here? */
1311 req->rq_status = -EAGAIN;
1312 RETURN(ptlrpc_error(req));
1316 static char *reint_names[] = {
1317 [REINT_SETATTR] "setattr",
1318 [REINT_CREATE] "create",
1319 [REINT_LINK] "link",
1320 [REINT_UNLINK] "unlink",
1321 [REINT_RENAME] "rename",
1322 [REINT_OPEN] "open",
1325 #define FILTER_VALID_FLAGS (OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLGENER |\
1326 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ|\
1327 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME|\
1330 static void reconstruct_create(struct ptlrpc_request *req)
1332 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1333 struct mds_client_data *mcd = med->med_mcd;
1334 struct ost_body *body;
1336 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1338 /* copy rc, transno and disp; steal locks */
1339 mds_req_from_mcd(req, mcd);
1340 CERROR("reconstruct reply for x"LPU64"\n", req->rq_xid);
1343 static int mdt_obj_create(struct ptlrpc_request *req)
1345 struct obd_device *obd = req->rq_export->exp_obd;
1346 struct mds_obd *mds = &obd->u.mds;
1347 struct ost_body *body, *repbody;
1348 char fidname[LL_FID_NAMELEN];
1349 struct inode *parent_inode;
1350 struct lvfs_run_ctxt saved;
1351 struct dentry *new = NULL;
1352 struct dentry_params dp;
1353 int mealen, flags = 0, rc, size = sizeof(*repbody), cleanup_phase = 0;
1354 struct lvfs_ucred uc;
1356 void *handle = NULL;
1359 DEBUG_REQ(D_HA, req, "create remote object");
1361 parent_inode = mds->mds_unnamed_dir->d_inode;
1363 body = lustre_swab_reqbuf(req, 0, sizeof(*body), lustre_swab_ost_body);
1367 rc = lustre_pack_reply(req, 1, &size, NULL);
1371 MDS_CHECK_RESENT(req, reconstruct_create(req));
1373 uc.luc_fsuid = body->oa.o_uid;
1374 uc.luc_fsgid = body->oa.o_gid;
1376 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1378 repbody = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*repbody));
1380 if (body->oa.o_flags & OBD_FL_RECREATE_OBJS) {
1381 /* this is re-create request from MDS holding directory name.
1382 * we have to lookup given ino/generation first. if it exists
1383 * (good case) then there is nothing to do. if it does not
1384 * then we have to recreate it */
1386 fid.id = body->oa.o_id;
1387 fid.generation = body->oa.o_generation;
1388 new = mds_fid2dentry(mds, &fid, NULL);
1389 if (!IS_ERR(new) && new->d_inode) {
1390 CDEBUG(D_HA, "mkdir() repairing is on its way: %lu/%lu\n",
1391 (unsigned long) fid.id,
1392 (unsigned long) fid.generation);
1393 obdo_from_inode(&repbody->oa, new->d_inode,
1394 FILTER_VALID_FLAGS);
1395 repbody->oa.o_id = new->d_inode->i_ino;
1396 repbody->oa.o_generation = new->d_inode->i_generation;
1397 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1399 GOTO(cleanup, rc = 0);
1401 CWARN("hmm. for some reason dir %lu/%lu (or reply) got lost\n",
1402 (unsigned long) fid.id, (unsigned long) fid.generation);
1403 LASSERT(new->d_inode == NULL ||
1404 new->d_inode->i_generation != fid.generation);
1408 down(&parent_inode->i_sem);
1409 handle = fsfilt_start(obd, parent_inode, FSFILT_OP_MKDIR, NULL);
1410 if (IS_ERR(handle)) {
1411 up(&parent_inode->i_sem);
1412 GOTO(cleanup, rc = PTR_ERR(handle));
1414 cleanup_phase = 1; /* transaction */
1417 rc = sprintf(fidname, "%u.%u", ll_insecure_random_int(), current->pid);
1418 new = lookup_one_len(fidname, mds->mds_unnamed_dir, rc);
1420 CERROR("%s: can't lookup new inode (%s) for mkdir: %d\n",
1421 obd->obd_name, fidname, (int) PTR_ERR(new));
1422 fsfilt_commit(obd, mds->mds_sb, new->d_inode, handle, 0);
1423 up(&parent_inode->i_sem);
1424 RETURN(PTR_ERR(new));
1425 } else if (new->d_inode) {
1426 CERROR("%s: name exists. repeat\n", obd->obd_name);
1430 new->d_fsdata = (void *) &dp;
1434 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1435 DEBUG_REQ(D_HA, req, "replay create obj %lu/%lu",
1436 (unsigned long) body->oa.o_id,
1437 (unsigned long) body->oa.o_generation);
1438 dp.p_inum = body->oa.o_id;
1439 dp.p_generation = body->oa.o_generation;
1441 rc = vfs_mkdir(parent_inode, new, body->oa.o_mode);
1443 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1444 LASSERTF(body->oa.o_id == new->d_inode->i_ino,
1445 "BUG 3550: failed to recreate obj "
1447 body->oa.o_id, new->d_inode->i_ino);
1448 LASSERTF(body->oa.o_generation ==
1449 new->d_inode->i_generation,
1450 "BUG 3550: failed to recreate obj/gen "
1451 LPU64"/%u -> %lu/%u\n",
1452 body->oa.o_id, body->oa.o_generation,
1453 new->d_inode->i_ino,
1454 new->d_inode->i_generation);
1457 obdo_from_inode(&repbody->oa, new->d_inode, FILTER_VALID_FLAGS);
1458 repbody->oa.o_id = new->d_inode->i_ino;
1459 repbody->oa.o_generation = new->d_inode->i_generation;
1460 repbody->oa.o_valid |= OBD_MD_FLID | OBD_MD_FLGENER;
1462 rc = fsfilt_del_dir_entry(obd, new);
1463 up(&parent_inode->i_sem);
1466 CERROR("can't remove name for object: %d\n", rc);
1470 cleanup_phase = 2; /* created directory object */
1472 CDEBUG(D_OTHER, "created dirobj: %lu/%lu mode %o\n",
1473 (unsigned long) new->d_inode->i_ino,
1474 (unsigned long) new->d_inode->i_generation,
1475 (unsigned) new->d_inode->i_mode);
1477 up(&parent_inode->i_sem);
1478 CERROR("%s: can't create dirobj: %d\n", obd->obd_name, rc);
1482 if (body->oa.o_valid & OBD_MD_FLID) {
1483 /* this is new object for splitted dir. we have to
1484 * prevent recursive splitting on it -bzzz */
1485 mealen = obd_size_diskmd(mds->mds_lmv_exp, NULL);
1486 OBD_ALLOC(mea, mealen);
1488 GOTO(cleanup, rc = -ENOMEM);
1489 mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1490 mea->mea_master = 0;
1492 down(&new->d_inode->i_sem);
1493 rc = fsfilt_set_md(obd, new->d_inode, handle, mea, mealen);
1494 up(&new->d_inode->i_sem);
1495 OBD_FREE(mea, mealen);
1496 CDEBUG(D_OTHER, "%s: mark non-splittable %lu/%u - %d\n",
1497 obd->obd_name, new->d_inode->i_ino,
1498 new->d_inode->i_generation, flags);
1499 } else if (body->oa.o_easize) {
1500 /* we pass LCK_EX to split routine to signal that we have
1501 * exclusive access to the directory. simple because nobody
1502 * knows it already exists -bzzz */
1503 mds_try_to_split_dir(obd, new, NULL, body->oa.o_easize, LCK_EX);
1507 switch (cleanup_phase) {
1508 case 2: /* object has been created, but we'll may want to replay it later */
1510 ptlrpc_require_repack(req);
1511 case 1: /* transaction */
1512 rc = mds_finish_transno(mds, parent_inode, handle, req, rc, 0);
1516 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1520 static int mdt_get_info(struct ptlrpc_request *req)
1523 struct obd_export *exp = req->rq_export;
1524 int keylen, rc = 0, size = sizeof(obd_id);
1528 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1530 DEBUG_REQ(D_HA, req, "no get_info key");
1533 keylen = req->rq_reqmsg->buflens[0];
1535 if (keylen < strlen("mdsize") || memcmp(key, "mdsize", 6) != 0)
1538 rc = lustre_pack_reply(req, 1, &size, NULL);
1542 reply = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*reply));
1543 rc = obd_get_info(exp, keylen, key, &size, reply);
1544 req->rq_repmsg->status = 0;
1548 static int mds_set_info(struct obd_export *exp, __u32 keylen,
1549 void *key, __u32 vallen, void *val)
1551 struct obd_device *obd;
1552 struct mds_obd *mds;
1556 obd = class_exp2obd(exp);
1558 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1559 exp->exp_handle.h_cookie);
1563 #define KEY_IS(str) \
1564 (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
1567 if (KEY_IS("mds_num")) {
1570 CDEBUG(D_IOCTL, "set mds num %d\n", *(int*)val);
1571 mds->mds_num = *(int*)val;
1572 group = FILTER_GROUP_FIRST_MDS + mds->mds_num;
1573 valsize = sizeof(group);
1574 /*mds number has been changed, so the corresponding obdfilter exp
1575 *need to be changed too*/
1576 rc = obd_set_info(mds->mds_osc_exp, strlen("mds_conn"), "mds_conn",
1580 CDEBUG(D_IOCTL, "invalid key\n");
1584 static int mdt_set_info(struct ptlrpc_request *req)
1587 struct obd_export *exp = req->rq_export;
1588 int keylen, rc = 0, vallen;
1591 key = lustre_msg_buf(req->rq_reqmsg, 0, 1);
1593 DEBUG_REQ(D_HA, req, "no set_info key");
1596 keylen = req->rq_reqmsg->buflens[0];
1598 if (keylen == strlen("mds_num") &&
1599 memcmp(key, "mds_num", keylen) == 0) {
1600 rc = lustre_pack_reply(req, 0, NULL, NULL);
1603 val = lustre_msg_buf(req->rq_reqmsg, 1, 0);
1605 vallen = req->rq_reqmsg->buflens[1];
1607 rc = obd_set_info(exp, keylen, key, vallen, val);
1608 req->rq_repmsg->status = 0;
1610 } else if (keylen == strlen("client") &&
1611 memcmp(key, "client", keylen) == 0) {
1612 rc = lustre_pack_reply(req, 0, NULL, NULL);
1615 rc = obd_set_info(exp, keylen, key, sizeof(obd_id), NULL);
1616 req->rq_repmsg->status = 0;
1619 CDEBUG(D_IOCTL, "invalid key\n");
1623 int mds_handle(struct ptlrpc_request *req)
1625 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1627 struct mds_obd *mds = NULL; /* quell gcc overwarning */
1628 struct obd_device *obd = NULL;
1631 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1633 LASSERT(current->journal_info == NULL);
1634 /* XXX identical to OST */
1635 if (req->rq_reqmsg->opc != MDS_CONNECT) {
1636 struct mds_export_data *med;
1639 if (req->rq_export == NULL) {
1640 CERROR("lustre_mds: operation %d on unconnected MDS\n",
1641 req->rq_reqmsg->opc);
1642 req->rq_status = -ENOTCONN;
1643 GOTO(out, rc = -ENOTCONN);
1646 med = &req->rq_export->exp_mds_data;
1647 obd = req->rq_export->exp_obd;
1650 /* sanity check: if the xid matches, the request must
1651 * be marked as a resent or replayed */
1652 if (req->rq_xid == med->med_mcd->mcd_last_xid)
1653 LASSERTF(lustre_msg_get_flags(req->rq_reqmsg) &
1654 (MSG_RESENT | MSG_REPLAY),
1655 "rq_xid "LPU64" matches last_xid, "
1656 "expected RESENT flag\n",
1658 /* else: note the opposite is not always true; a
1659 * RESENT req after a failover will usually not match
1660 * the last_xid, since it was likely never
1661 * committed. A REPLAYed request will almost never
1662 * match the last xid, however it could for a
1663 * committed, but still retained, open. */
1665 spin_lock_bh(&obd->obd_processing_task_lock);
1666 recovering = obd->obd_recovering;
1667 spin_unlock_bh(&obd->obd_processing_task_lock);
1669 rc = mds_filter_recovery_request(req, obd,
1671 if (rc || should_process == 0) {
1673 } else if (should_process < 0) {
1674 req->rq_status = should_process;
1675 rc = ptlrpc_error(req);
1681 switch (req->rq_reqmsg->opc) {
1683 DEBUG_REQ(D_INODE, req, "connect");
1684 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1685 rc = target_handle_connect(req);
1687 /* Now that we have an export, set mds. */
1688 mds = mds_req2mds(req);
1691 case MDS_DISCONNECT:
1692 DEBUG_REQ(D_INODE, req, "disconnect");
1693 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1694 rc = target_handle_disconnect(req);
1695 req->rq_status = rc; /* superfluous? */
1699 DEBUG_REQ(D_INODE, req, "getstatus");
1700 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1701 rc = mds_getstatus(req);
1705 DEBUG_REQ(D_INODE, req, "getattr");
1706 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1707 rc = mds_getattr(0, req);
1710 case MDS_GETATTR_NAME: {
1711 struct lustre_handle lockh;
1712 DEBUG_REQ(D_INODE, req, "getattr_name");
1713 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1715 /* If this request gets a reconstructed reply, we won't be
1716 * acquiring any new locks in mds_getattr_name, so we don't
1720 rc = mds_getattr_name(0, req, &lockh, MDS_INODELOCK_UPDATE);
1721 /* this non-intent call (from an ioctl) is special */
1722 req->rq_status = rc;
1723 if (rc == 0 && lockh.cookie)
1724 ldlm_lock_decref(&lockh, LCK_PR);
1728 DEBUG_REQ(D_INODE, req, "statfs");
1729 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1730 rc = mds_statfs(req);
1734 DEBUG_REQ(D_INODE, req, "readpage");
1735 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1736 rc = mds_readpage(req);
1738 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1739 if (req->rq_reply_state) {
1740 lustre_free_reply_state (req->rq_reply_state);
1741 req->rq_reply_state = NULL;
1749 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*opcp));
1751 int size[3] = {sizeof(struct mds_body), mds->mds_max_mdsize,
1752 mds->mds_max_cookiesize};
1755 /* NB only peek inside req now; mds_reint() will swab it */
1757 CERROR ("Can't inspect opcode\n");
1762 if (lustre_msg_swabbed (req->rq_reqmsg))
1765 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1766 (opc < sizeof(reint_names) / sizeof(reint_names[0]) ||
1767 reint_names[opc] == NULL) ? reint_names[opc] :
1770 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1772 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1774 else if (opc == REINT_OPEN)
1779 rc = lustre_pack_reply(req, bufcount, size, NULL);
1783 rc = mds_reint(req, 0, NULL);
1784 fail = OBD_FAIL_MDS_REINT_NET_REP;
1789 DEBUG_REQ(D_INODE, req, "close");
1790 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1791 rc = mds_close(req);
1794 case MDS_DONE_WRITING:
1795 DEBUG_REQ(D_INODE, req, "done_writing");
1796 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1797 rc = mds_done_writing(req);
1801 DEBUG_REQ(D_INODE, req, "pin");
1802 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1807 DEBUG_REQ(D_INODE, req, "sync");
1808 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1813 DEBUG_REQ(D_INODE, req, "ping");
1814 rc = target_handle_ping(req);
1817 case OBD_LOG_CANCEL:
1818 CDEBUG(D_INODE, "log cancel\n");
1819 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1820 rc = -ENOTSUPP; /* la la la */
1824 DEBUG_REQ(D_INODE, req, "enqueue");
1825 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1826 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1827 ldlm_server_blocking_ast, NULL);
1830 DEBUG_REQ(D_INODE, req, "convert");
1831 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1832 rc = ldlm_handle_convert(req);
1834 case LDLM_BL_CALLBACK:
1835 case LDLM_CP_CALLBACK:
1836 DEBUG_REQ(D_INODE, req, "callback");
1837 CERROR("callbacks should not happen on MDS\n");
1839 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1841 case LLOG_ORIGIN_HANDLE_OPEN:
1842 DEBUG_REQ(D_INODE, req, "llog_init");
1843 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1844 rc = llog_origin_handle_open(req);
1846 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1847 DEBUG_REQ(D_INODE, req, "llog next block");
1848 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1849 rc = llog_origin_handle_next_block(req);
1851 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1852 DEBUG_REQ(D_INODE, req, "llog prev block");
1853 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1854 rc = llog_origin_handle_prev_block(req);
1856 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1857 DEBUG_REQ(D_INODE, req, "llog read header");
1858 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1859 rc = llog_origin_handle_read_header(req);
1861 case LLOG_ORIGIN_HANDLE_CLOSE:
1862 DEBUG_REQ(D_INODE, req, "llog close");
1863 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1864 rc = llog_origin_handle_close(req);
1867 DEBUG_REQ(D_INODE, req, "ost_create");
1868 rc = mdt_obj_create(req);
1871 DEBUG_REQ(D_INODE, req, "get_info");
1872 rc = mdt_get_info(req);
1875 DEBUG_REQ(D_INODE, req, "set_info");
1876 rc = mdt_set_info(req);
1879 CDEBUG(D_INODE, "write\n");
1880 OBD_FAIL_RETURN(OBD_FAIL_OST_BRW_NET, 0);
1881 rc = ost_brw_write(req, NULL);
1882 LASSERT(current->journal_info == NULL);
1883 /* mdt_brw sends its own replies */
1887 DEBUG_REQ(D_INODE, req, "llog catinfo");
1888 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1889 rc = llog_catinfo(req);
1892 req->rq_status = -ENOTSUPP;
1893 rc = ptlrpc_error(req);
1897 LASSERT(current->journal_info == NULL);
1901 /* If we're DISCONNECTing, the mds_export_data is already freed */
1902 if (!rc && req->rq_reqmsg->opc != MDS_DISCONNECT) {
1903 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1904 struct obd_device *obd = list_entry(mds, struct obd_device,
1906 req->rq_repmsg->last_xid =
1907 le64_to_cpu(med->med_mcd->mcd_last_xid);
1909 if (!obd->obd_no_transno) {
1910 req->rq_repmsg->last_committed =
1911 obd->obd_last_committed;
1913 DEBUG_REQ(D_IOCTL, req,
1914 "not sending last_committed update");
1916 CDEBUG(D_INFO, "last_transno "LPU64", last_committed "LPU64
1918 mds->mds_last_transno, obd->obd_last_committed,
1923 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LAST_REPLAY) {
1924 if (obd && obd->obd_recovering) {
1925 DEBUG_REQ(D_HA, req, "LAST_REPLAY, queuing reply");
1926 return target_queue_final_reply(req, rc);
1928 /* Lost a race with recovery; let the error path DTRT. */
1929 rc = req->rq_status = -ENOTCONN;
1932 target_send_reply(req, rc, fail);
1936 /* Update the server data on disk. This stores the new mount_count and
1937 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1938 * then the server last_rcvd value may be less than that of the clients.
1939 * This will alert us that we may need to do client recovery.
1941 * Also assumes for mds_last_transno that we are not modifying it (no locking).
1943 int mds_update_server_data(struct obd_device *obd, int force_sync)
1945 struct mds_obd *mds = &obd->u.mds;
1946 struct mds_server_data *msd = mds->mds_server_data;
1947 struct file *filp = mds->mds_rcvd_filp;
1948 struct lvfs_run_ctxt saved;
1953 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1954 msd->msd_last_transno = cpu_to_le64(mds->mds_last_transno);
1956 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1957 mds->mds_mount_count, mds->mds_last_transno);
1958 rc = fsfilt_write_record(obd, filp, msd, sizeof(*msd), &off,force_sync);
1960 CERROR("error writing MDS server data: rc = %d\n", rc);
1961 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1966 /* mount the file system (secretly) */
1967 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1969 struct lustre_cfg* lcfg = buf;
1970 struct mds_obd *mds = &obd->u.mds;
1971 char *options = NULL;
1972 struct vfsmount *mnt;
1977 dev_clear_rdonly(2);
1979 if (!lcfg->lcfg_inlbuf1 || !lcfg->lcfg_inlbuf2)
1980 RETURN(rc = -EINVAL);
1982 obd->obd_fsops = fsfilt_get_ops(lcfg->lcfg_inlbuf2);
1983 if (IS_ERR(obd->obd_fsops))
1984 RETURN(rc = PTR_ERR(obd->obd_fsops));
1986 mds->mds_max_mdsize = sizeof(struct lov_mds_md);
1988 page = __get_free_page(GFP_KERNEL);
1992 options = (char *)page;
1993 memset(options, 0, PAGE_SIZE);
1995 /* here we use "iopen_nopriv" hardcoded, because it affects MDS utility
1996 * and the rest of options are passed by mount options. Probably this
1997 * should be moved to somewhere else like startup scripts or lconf. */
1998 sprintf(options, "iopen_nopriv");
2000 if (lcfg->lcfg_inllen4 > 0 && lcfg->lcfg_inlbuf4)
2001 sprintf(options + strlen(options), ",%s",
2002 lcfg->lcfg_inlbuf4);
2004 /* we have to know mdsnum before touching underlying fs -bzzz */
2005 sema_init(&mds->mds_lmv_sem, 1);
2006 mds->mds_lmv_connected = 0;
2007 if (lcfg->lcfg_inllen5 > 0 && lcfg->lcfg_inlbuf5 &&
2008 strcmp(lcfg->lcfg_inlbuf5, "dumb")) {
2011 CDEBUG(D_OTHER, "MDS: %s is master for %s\n",
2012 obd->obd_name, lcfg->lcfg_inlbuf5);
2014 generate_random_uuid(uuid);
2015 class_uuid_unparse(uuid, &mds->mds_lmv_uuid);
2017 OBD_ALLOC(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2018 if (mds->mds_lmv_name == NULL)
2019 RETURN(rc = -ENOMEM);
2021 memcpy(mds->mds_lmv_name, lcfg->lcfg_inlbuf5,
2022 lcfg->lcfg_inllen5);
2024 rc = mds_lmv_connect(obd, mds->mds_lmv_name);
2026 OBD_FREE(mds->mds_lmv_name, lcfg->lcfg_inllen5);
2031 /* FIXME-WANGDI: this should be reworked when we will use lmv along
2032 * with cobd, because correct mdsnum is set in mds_lmv_connect(). */
2033 if (lcfg->lcfg_inllen6 > 0 && lcfg->lcfg_inlbuf6 && !mds->mds_lmv_obd &&
2034 strcmp(lcfg->lcfg_inlbuf6, "dumb")) {
2035 if (!memcmp(lcfg->lcfg_inlbuf6, "master", strlen("master")) &&
2036 mds->mds_num == 0) {
2037 mds->mds_num = REAL_MDS_NUMBER;
2038 } else if (!memcmp(lcfg->lcfg_inlbuf6, "cache", strlen("cache")) &&
2039 mds->mds_num == 0) {
2040 mds->mds_num = CACHE_MDS_NUMBER;
2044 mnt = do_kern_mount(lcfg->lcfg_inlbuf2, 0,
2045 lcfg->lcfg_inlbuf1, options);
2051 CERROR("do_kern_mount failed: rc = %d\n", rc);
2055 CDEBUG(D_SUPER, "%s: mnt = %p\n", lcfg->lcfg_inlbuf1, mnt);
2057 sema_init(&mds->mds_orphan_recovery_sem, 1);
2058 sema_init(&mds->mds_epoch_sem, 1);
2059 spin_lock_init(&mds->mds_transno_lock);
2060 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
2061 atomic_set(&mds->mds_real_clients, 0);
2063 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
2064 LDLM_NAMESPACE_SERVER);
2065 if (obd->obd_namespace == NULL) {
2066 mds_cleanup(obd, 0);
2067 GOTO(err_put, rc = -ENOMEM);
2069 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
2071 rc = mds_fs_setup(obd, mnt);
2073 CERROR("MDS filesystem method init failed: rc = %d\n", rc);
2077 rc = llog_start_commit_thread();
2081 /* this check for @dumb string is needed to handle mounting MDS with
2082 * smfs. Read lconf:MDSDEV.write_conf() for more details. */
2083 if (lcfg->lcfg_inllen3 > 0 && lcfg->lcfg_inlbuf3 &&
2084 strcmp(lcfg->lcfg_inlbuf3, "dumb")) {
2087 generate_random_uuid(uuid);
2088 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
2090 OBD_ALLOC(mds->mds_profile, lcfg->lcfg_inllen3);
2091 if (mds->mds_profile == NULL)
2092 GOTO(err_fs, rc = -ENOMEM);
2094 memcpy(mds->mds_profile, lcfg->lcfg_inlbuf3,
2095 lcfg->lcfg_inllen3);
2098 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2099 "mds_ldlm_client", &obd->obd_ldlm_client);
2100 obd->obd_replayable = 1;
2102 mds->mds_counters = lprocfs_alloc_mds_counters();
2104 rc = mds_postsetup(obd);
2111 /* No extra cleanup needed for llog_init_commit_thread() */
2112 mds_fs_cleanup(obd, 0);
2114 ldlm_namespace_free(obd->obd_namespace, 0);
2115 obd->obd_namespace = NULL;
2118 mntput(mds->mds_vfsmnt);
2122 fsfilt_put_ops(obd->obd_fsops);
2126 static int mds_postsetup(struct obd_device *obd)
2128 struct mds_obd *mds = &obd->u.mds;
2132 rc = obd_llog_setup(obd, &obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT,
2133 obd, 0, NULL, &llog_lvfs_ops);
2137 if (mds->mds_profile) {
2138 struct llog_ctxt *lgctxt;
2139 struct lvfs_run_ctxt saved;
2140 struct lustre_profile *lprof;
2141 struct config_llog_instance cfg;
2143 cfg.cfg_instance = NULL;
2144 cfg.cfg_uuid = mds->mds_lov_uuid;
2145 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2147 lgctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
2149 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2150 GOTO(err_llog, rc = -EINVAL);
2153 rc = class_config_process_llog(lgctxt, mds->mds_profile, &cfg);
2154 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2159 lprof = class_get_profile(mds->mds_profile);
2160 if (lprof == NULL) {
2161 CERROR("No profile found: %s\n", mds->mds_profile);
2162 GOTO(err_cleanup, rc = -ENOENT);
2164 rc = mds_lov_connect(obd, lprof->lp_osc);
2166 GOTO(err_cleanup, rc);
2168 rc = mds_lmv_postsetup(obd);
2170 GOTO(err_cleanup, rc);
2178 obd_llog_cleanup(llog_get_context(&obd->obd_llogs,
2179 LLOG_CONFIG_ORIG_CTXT));
2183 int mds_postrecov(struct obd_device *obd)
2185 struct mds_obd *mds = &obd->u.mds;
2186 struct llog_ctxt *ctxt;
2190 LASSERT(!obd->obd_recovering);
2191 ctxt = llog_get_context(&obd->obd_llogs, LLOG_UNLINK_ORIG_CTXT);
2192 LASSERT(ctxt != NULL);
2194 /* set nextid first, so we are sure it happens */
2195 rc = mds_lov_set_nextid(obd);
2197 CERROR("%s: mds_lov_set_nextid failed\n", obd->obd_name);
2201 /* clean PENDING dir */
2202 rc = mds_cleanup_orphans(obd);
2207 rc = llog_connect(ctxt, obd->u.mds.mds_lov_desc.ld_tgt_count,
2210 CERROR("%s: failed at llog_origin_connect: %d\n",
2215 /* remove the orphaned precreated objects */
2216 rc = mds_lov_clearorphans(mds, NULL /* all OSTs */);
2221 RETURN(rc < 0 ? rc : item);
2224 /* cleanup all llogging subsystems */
2225 rc = obd_llog_finish(obd, &obd->obd_llogs,
2226 mds->mds_lov_desc.ld_tgt_count);
2228 CERROR("%s: failed to cleanup llogging subsystems\n",
2233 int mds_lov_clean(struct obd_device *obd)
2235 struct mds_obd *mds = &obd->u.mds;
2237 if (mds->mds_profile) {
2239 struct config_llog_instance cfg;
2240 struct lvfs_run_ctxt saved;
2241 int len = strlen(mds->mds_profile) + sizeof("-clean") + 1;
2243 OBD_ALLOC(cln_prof, len);
2244 sprintf(cln_prof, "%s-clean", mds->mds_profile);
2246 cfg.cfg_instance = NULL;
2247 cfg.cfg_uuid = mds->mds_lov_uuid;
2249 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2250 class_config_process_llog(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT),
2252 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
2254 OBD_FREE(cln_prof, len);
2255 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2256 mds->mds_profile = NULL;
2261 int mds_lmv_clean(struct obd_device *obd)
2263 struct mds_obd *mds = &obd->u.mds;
2265 if (mds->mds_lmv_name) {
2266 OBD_FREE(mds->mds_lmv_name, strlen(mds->mds_lmv_name) + 1);
2267 mds->mds_lmv_name = NULL;
2272 static int mds_precleanup(struct obd_device *obd, int flags)
2278 mds_lov_disconnect(obd, flags);
2280 obd_llog_cleanup(llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT));
2284 static int mds_cleanup(struct obd_device *obd, int flags)
2286 struct mds_obd *mds = &obd->u.mds;
2289 if (mds->mds_sb == NULL)
2292 mds_update_server_data(obd, 1);
2293 if (mds->mds_lov_objids != NULL) {
2294 OBD_FREE(mds->mds_lov_objids,
2295 mds->mds_lov_desc.ld_tgt_count * sizeof(obd_id));
2297 mds_fs_cleanup(obd, flags);
2301 /* 2 seems normal on mds, (may_umount() also expects 2
2302 fwiw), but we only see 1 at this point in obdfilter. */
2303 if (atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count) > 2)
2304 CERROR("%s: mount busy, mnt_count %d != 2\n", obd->obd_name,
2305 atomic_read(&obd->u.mds.mds_vfsmnt->mnt_count));
2307 mntput(mds->mds_vfsmnt);
2311 ldlm_namespace_free(obd->obd_namespace, flags & OBD_OPT_FORCE);
2313 if (mds->mds_counters) {
2314 lprocfs_free_mds_counters(mds->mds_counters);
2317 spin_lock_bh(&obd->obd_processing_task_lock);
2318 if (obd->obd_recovering) {
2319 target_cancel_recovery_timer(obd);
2320 obd->obd_recovering = 0;
2322 spin_unlock_bh(&obd->obd_processing_task_lock);
2325 dev_clear_rdonly(2);
2326 fsfilt_put_ops(obd->obd_fsops);
2331 static void fixup_handle_for_resent_req(struct ptlrpc_request *req,
2332 struct ldlm_lock *new_lock,
2333 struct lustre_handle *lockh)
2335 struct obd_export *exp = req->rq_export;
2336 struct obd_device *obd = exp->exp_obd;
2337 struct ldlm_request *dlmreq =
2338 lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*dlmreq));
2339 struct lustre_handle remote_hdl = dlmreq->lock_handle1;
2340 struct list_head *iter;
2342 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2345 l_lock(&obd->obd_namespace->ns_lock);
2346 list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
2347 struct ldlm_lock *lock;
2348 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
2349 if (lock == new_lock)
2351 if (lock->l_remote_handle.cookie == remote_hdl.cookie) {
2352 lockh->cookie = lock->l_handle.h_cookie;
2353 DEBUG_REQ(D_HA, req, "restoring lock cookie "LPX64,
2355 l_unlock(&obd->obd_namespace->ns_lock);
2359 l_unlock(&obd->obd_namespace->ns_lock);
2361 /* If the xid matches, then we know this is a resent request,
2362 * and allow it. (It's probably an OPEN, for which we don't
2364 if (req->rq_xid == exp->exp_mds_data.med_mcd->mcd_last_xid)
2367 /* This remote handle isn't enqueued, so we never received or
2368 * processed this request. Clear MSG_RESENT, because it can
2369 * be handled like any normal request now. */
2371 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2373 DEBUG_REQ(D_HA, req, "no existing lock with rhandle "LPX64,
2377 int intent_disposition(struct ldlm_reply *rep, int flag)
2381 return (rep->lock_policy_res1 & flag);
2384 void intent_set_disposition(struct ldlm_reply *rep, int flag)
2388 rep->lock_policy_res1 |= flag;
2391 static int mds_intent_policy(struct ldlm_namespace *ns,
2392 struct ldlm_lock **lockp, void *req_cookie,
2393 ldlm_mode_t mode, int flags, void *data)
2395 struct ptlrpc_request *req = req_cookie;
2396 struct ldlm_lock *lock = *lockp;
2397 struct ldlm_intent *it;
2398 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2399 struct ldlm_reply *rep;
2400 struct lustre_handle lockh = { 0 };
2401 struct ldlm_lock *new_lock;
2402 int getattr_part = MDS_INODELOCK_UPDATE;
2403 int rc, offset = 2, repsize[4] = {sizeof(struct ldlm_reply),
2404 sizeof(struct mds_body),
2405 mds->mds_max_mdsize,
2406 mds->mds_max_cookiesize};
2409 LASSERT(req != NULL);
2411 if (req->rq_reqmsg->bufcount <= 1) {
2412 /* No intent was provided */
2413 int size = sizeof(struct ldlm_reply);
2414 rc = lustre_pack_reply(req, 1, &size, NULL);
2419 it = lustre_swab_reqbuf(req, 1, sizeof(*it), lustre_swab_ldlm_intent);
2421 CERROR("Intent missing\n");
2422 RETURN(req->rq_status = -EFAULT);
2425 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2427 rc = lustre_pack_reply(req, 3, repsize, NULL);
2429 RETURN(req->rq_status = rc);
2431 rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*rep));
2432 intent_set_disposition(rep, DISP_IT_EXECD);
2434 fixup_handle_for_resent_req(req, lock, &lockh);
2436 /* execute policy */
2437 switch ((long)it->opc) {
2439 case IT_CREAT|IT_OPEN:
2440 /* XXX swab here to assert that an mds_open reint
2441 * packet is following */
2442 rep->lock_policy_res2 = mds_reint(req, offset, &lockh);
2444 /* We abort the lock if the lookup was negative and
2445 * we did not make it to the OPEN portion */
2446 if (!intent_disposition(rep, DISP_LOOKUP_EXECD))
2447 RETURN(ELDLM_LOCK_ABORTED);
2448 if (intent_disposition(rep, DISP_LOOKUP_NEG) &&
2449 !intent_disposition(rep, DISP_OPEN_OPEN))
2451 /* IT_OPEN may return lock on cross-node dentry
2452 * that we want to hold during attr retrival -bzzz */
2453 if (rc != 0 || lockh.cookie == 0)
2454 RETURN(ELDLM_LOCK_ABORTED);
2457 getattr_part = MDS_INODELOCK_LOOKUP;
2460 getattr_part |= MDS_INODELOCK_LOOKUP;
2462 rep->lock_policy_res2 = mds_getattr_name(offset, req, &lockh,
2464 /* FIXME: LDLM can set req->rq_status. MDS sets
2465 policy_res{1,2} with disposition and status.
2466 - replay: returns 0 & req->status is old status
2467 - otherwise: returns req->status */
2468 if (intent_disposition(rep, DISP_LOOKUP_NEG))
2469 rep->lock_policy_res2 = 0;
2470 if (!intent_disposition(rep, DISP_LOOKUP_POS) ||
2471 rep->lock_policy_res2)
2472 RETURN(ELDLM_LOCK_ABORTED);
2473 if (req->rq_status != 0) {
2475 rep->lock_policy_res2 = req->rq_status;
2476 RETURN(ELDLM_LOCK_ABORTED);
2480 rc = mds_lock_and_check_slave(offset, req, &lockh);
2481 if ((rep->lock_policy_res2 = rc)) {
2483 rep->lock_policy_res2 = 0;
2484 RETURN(ELDLM_LOCK_ABORTED);
2488 CERROR("Unhandled intent "LPD64"\n", it->opc);
2492 /* By this point, whatever function we called above must have either
2493 * filled in 'lockh', been an intent replay, or returned an error. We
2494 * want to allow replayed RPCs to not get a lock, since we would just
2495 * drop it below anyways because lock replay is done separately by the
2496 * client afterwards. For regular RPCs we want to give the new lock to
2497 * the client instead of whatever lock it was about to get. */
2498 new_lock = ldlm_handle2lock(&lockh);
2499 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2502 LASSERT(new_lock != NULL);
2504 /* If we've already given this lock to a client once, then we should
2505 * have no readers or writers. Otherwise, we should have one reader
2506 * _or_ writer ref (which will be zeroed below) before returning the
2507 * lock to a client. */
2508 if (new_lock->l_export == req->rq_export) {
2509 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2511 LASSERT(new_lock->l_export == NULL);
2512 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2517 if (new_lock->l_export == req->rq_export) {
2518 /* Already gave this to the client, which means that we
2519 * reconstructed a reply. */
2520 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2522 RETURN(ELDLM_LOCK_REPLACED);
2525 /* Fixup the lock to be given to the client */
2526 l_lock(&new_lock->l_resource->lr_namespace->ns_lock);
2527 new_lock->l_readers = 0;
2528 new_lock->l_writers = 0;
2530 new_lock->l_export = class_export_get(req->rq_export);
2531 list_add(&new_lock->l_export_chain,
2532 &new_lock->l_export->exp_ldlm_data.led_held_locks);
2534 new_lock->l_blocking_ast = lock->l_blocking_ast;
2535 new_lock->l_completion_ast = lock->l_completion_ast;
2537 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2538 sizeof(lock->l_remote_handle));
2540 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2542 LDLM_LOCK_PUT(new_lock);
2543 l_unlock(&new_lock->l_resource->lr_namespace->ns_lock);
2545 RETURN(ELDLM_LOCK_REPLACED);
2548 int mds_attach(struct obd_device *dev, obd_count len, void *data)
2550 struct lprocfs_static_vars lvars;
2552 lprocfs_init_multi_vars(0, &lvars);
2553 return lprocfs_obd_attach(dev, lvars.obd_vars);
2556 int mds_detach(struct obd_device *dev)
2558 return lprocfs_obd_detach(dev);
2561 int mdt_attach(struct obd_device *dev, obd_count len, void *data)
2563 struct lprocfs_static_vars lvars;
2565 lprocfs_init_multi_vars(1, &lvars);
2566 return lprocfs_obd_attach(dev, lvars.obd_vars);
2569 int mdt_detach(struct obd_device *dev)
2571 return lprocfs_obd_detach(dev);
2574 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2576 struct mds_obd *mds = &obd->u.mds;
2581 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2582 MDS_REQUEST_PORTAL, MDC_REPLY_PORTAL,
2583 mds_handle, "mds", obd->obd_proc_entry);
2585 if (!mds->mds_service) {
2586 CERROR("failed to start service\n");
2590 rc = ptlrpc_start_n_threads(obd, mds->mds_service, MDT_NUM_THREADS,
2593 GOTO(err_thread, rc);
2595 mds->mds_setattr_service =
2596 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2597 MDS_SETATTR_PORTAL, MDC_REPLY_PORTAL,
2598 mds_handle, "mds_setattr",
2599 obd->obd_proc_entry);
2600 if (!mds->mds_setattr_service) {
2601 CERROR("failed to start getattr service\n");
2602 GOTO(err_thread, rc = -ENOMEM);
2605 rc = ptlrpc_start_n_threads(obd, mds->mds_setattr_service,
2606 MDT_NUM_THREADS, "ll_mdt_attr");
2608 GOTO(err_thread2, rc);
2610 mds->mds_readpage_service =
2611 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2612 MDS_READPAGE_PORTAL, MDC_REPLY_PORTAL,
2613 mds_handle, "mds_readpage",
2614 obd->obd_proc_entry);
2615 if (!mds->mds_readpage_service) {
2616 CERROR("failed to start readpage service\n");
2617 GOTO(err_thread2, rc = -ENOMEM);
2620 rc = ptlrpc_start_n_threads(obd, mds->mds_readpage_service,
2621 MDT_NUM_THREADS, "ll_mdt_rdpg");
2624 GOTO(err_thread3, rc);
2629 ptlrpc_unregister_service(mds->mds_readpage_service);
2631 ptlrpc_unregister_service(mds->mds_setattr_service);
2633 ptlrpc_unregister_service(mds->mds_service);
2637 static int mdt_cleanup(struct obd_device *obd, int flags)
2639 struct mds_obd *mds = &obd->u.mds;
2642 ptlrpc_stop_all_threads(mds->mds_readpage_service);
2643 ptlrpc_unregister_service(mds->mds_readpage_service);
2645 ptlrpc_stop_all_threads(mds->mds_setattr_service);
2646 ptlrpc_unregister_service(mds->mds_setattr_service);
2648 ptlrpc_stop_all_threads(mds->mds_service);
2649 ptlrpc_unregister_service(mds->mds_service);
2654 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2657 struct obd_device *obd = data;
2660 fid.generation = gen;
2661 return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2664 static int mds_get_info(struct obd_export *exp, __u32 keylen,
2665 void *key, __u32 *vallen, void *val)
2667 struct obd_device *obd;
2668 struct mds_obd *mds;
2671 obd = class_exp2obd(exp);
2673 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2674 exp->exp_handle.h_cookie);
2678 if (keylen >= strlen("reint_log") && memcmp(key, "reint_log", 9) == 0) {
2679 /*Get log_context handle*/
2680 unsigned long *llh_handle = val;
2681 *vallen = sizeof(unsigned long);
2682 *llh_handle = (unsigned long)obd->obd_llog_ctxt[LLOG_REINT_ORIG_CTXT];
2685 if (keylen >= strlen("cache_sb") && memcmp(key, "cache_sb", 8) == 0) {
2686 /*Get log_context handle*/
2687 unsigned long *sb = val;
2688 *vallen = sizeof(unsigned long);
2689 *sb = (unsigned long)obd->u.mds.mds_sb;
2694 if (keylen >= strlen("mdsize") && memcmp(key, "mdsize", keylen) == 0) {
2695 __u32 *mdsize = val;
2696 *vallen = sizeof(*mdsize);
2697 *mdsize = mds->mds_max_mdsize;
2701 CDEBUG(D_IOCTL, "invalid key\n");
2705 struct lvfs_callback_ops mds_lvfs_ops = {
2706 l_fid2dentry: mds_lvfs_fid2dentry,
2709 int mds_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
2710 int objcount, struct obd_ioobj *obj,
2711 int niocount, struct niobuf_remote *nb,
2712 struct niobuf_local *res,
2713 struct obd_trans_info *oti);
2714 int mds_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
2715 int objcount, struct obd_ioobj *obj, int niocount,
2716 struct niobuf_local *res, struct obd_trans_info *oti,
2719 /* use obd ops to offer management infrastructure */
2720 static struct obd_ops mds_obd_ops = {
2721 .o_owner = THIS_MODULE,
2722 .o_attach = mds_attach,
2723 .o_detach = mds_detach,
2724 .o_connect = mds_connect,
2725 .o_init_export = mds_init_export,
2726 .o_destroy_export = mds_destroy_export,
2727 .o_disconnect = mds_disconnect,
2728 .o_setup = mds_setup,
2729 .o_precleanup = mds_precleanup,
2730 .o_cleanup = mds_cleanup,
2731 .o_postrecov = mds_postrecov,
2732 .o_statfs = mds_obd_statfs,
2733 .o_iocontrol = mds_iocontrol,
2734 .o_create = mds_obd_create,
2735 .o_destroy = mds_obd_destroy,
2736 .o_llog_init = mds_llog_init,
2737 .o_llog_finish = mds_llog_finish,
2738 .o_notify = mds_notify,
2739 .o_get_info = mds_get_info,
2740 .o_set_info = mds_set_info,
2741 .o_preprw = mds_preprw,
2742 .o_commitrw = mds_commitrw,
2745 static struct obd_ops mdt_obd_ops = {
2746 .o_owner = THIS_MODULE,
2747 .o_attach = mdt_attach,
2748 .o_detach = mdt_detach,
2749 .o_setup = mdt_setup,
2750 .o_cleanup = mdt_cleanup,
2751 .o_attach = mdt_attach,
2752 .o_detach = mdt_detach,
2755 static int __init mds_init(void)
2757 struct lprocfs_static_vars lvars;
2759 lprocfs_init_multi_vars(0, &lvars);
2760 class_register_type(&mds_obd_ops, NULL, lvars.module_vars,
2762 lprocfs_init_multi_vars(1, &lvars);
2763 class_register_type(&mdt_obd_ops, NULL, lvars.module_vars,
2769 static void /*__exit*/ mds_exit(void)
2771 class_unregister_type(LUSTRE_MDS_NAME);
2772 class_unregister_type(LUSTRE_MDT_NAME);
2775 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2776 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2777 MODULE_LICENSE("GPL");
2779 module_init(mds_init);
2780 module_exit(mds_exit);