1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mds/handler.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Andreas Dilger <adilger@clusterfs.com>
40 * Author: Phil Schwan <phil@clusterfs.com>
41 * Author: Mike Shaver <shaver@clusterfs.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include <lustre_mds.h>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/random.h>
54 #include <linux/jbd.h>
55 # include <linux/smp_lock.h>
56 # include <linux/buffer_head.h>
57 # include <linux/workqueue.h>
58 # include <linux/mount.h>
60 #include <obd_class.h>
61 #include <lustre_dlm.h>
63 #include <lustre_fsfilt.h>
64 #include <lprocfs_status.h>
65 #include <lustre_quota.h>
66 #include <lustre_disk.h>
67 #include <lustre_param.h>
69 #include "mds_internal.h"
72 CFS_MODULE_PARM(mds_num_threads, "i", int, 0444,
73 "number of MDS service threads to start");
75 static int mds_intent_policy(struct ldlm_namespace *ns,
76 struct ldlm_lock **lockp, void *req_cookie,
77 ldlm_mode_t mode, int flags, void *data);
78 static int mds_postsetup(struct obd_device *obd);
79 static int mds_cleanup(struct obd_device *obd);
81 /* Assumes caller has already pushed into the kernel filesystem context */
82 static int mds_sendpage(struct ptlrpc_request *req, struct file *file,
83 loff_t offset, int count)
85 struct ptlrpc_bulk_desc *desc;
86 struct l_wait_info lwi;
89 int rc = 0, npages, i, tmpcount, tmpsize = 0;
92 LASSERT((offset & ~CFS_PAGE_MASK) == 0); /* I'm dubious about this */
94 npages = (count + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
95 OBD_ALLOC(pages, sizeof(*pages) * npages);
97 GOTO(out, rc = -ENOMEM);
99 desc = ptlrpc_prep_bulk_exp(req, npages, BULK_PUT_SOURCE,
102 GOTO(out_free, rc = -ENOMEM);
104 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
105 tmpsize = tmpcount > CFS_PAGE_SIZE ? CFS_PAGE_SIZE : tmpcount;
107 OBD_PAGE_ALLOC(pages[i], CFS_ALLOC_STD);
108 if (pages[i] == NULL)
109 GOTO(cleanup_buf, rc = -ENOMEM);
111 ptlrpc_prep_bulk_page(desc, pages[i], 0, tmpsize);
114 for (i = 0, tmpcount = count; i < npages; i++, tmpcount -= tmpsize) {
115 tmpsize = tmpcount > CFS_PAGE_SIZE ? CFS_PAGE_SIZE : tmpcount;
116 CDEBUG(D_EXT2, "reading %u@%llu from dir %lu (size %llu)\n",
117 tmpsize, offset, file->f_dentry->d_inode->i_ino,
118 i_size_read(file->f_dentry->d_inode));
120 rc = fsfilt_readpage(req->rq_export->exp_obd, file,
121 kmap(pages[i]), tmpsize, &offset);
125 GOTO(cleanup_buf, rc = -EIO);
128 LASSERT(desc->bd_nob == count);
130 rc = ptlrpc_start_bulk_transfer(desc);
132 GOTO(cleanup_buf, rc);
134 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
135 CERROR("obd_fail_loc=%x, fail operation rc=%d\n",
136 OBD_FAIL_MDS_SENDPAGE, rc);
137 GOTO(abort_bulk, rc);
140 timeout = (int)req->rq_deadline - (int)cfs_time_current_sec();
142 CERROR("Req deadline already passed %lu (now: %lu)\n",
143 req->rq_deadline, cfs_time_current_sec());
145 lwi = LWI_TIMEOUT(cfs_time_seconds(max(timeout, 1)), NULL, NULL);
146 rc = l_wait_event(desc->bd_waitq, !ptlrpc_server_bulk_active(desc), &lwi);
147 LASSERT (rc == 0 || rc == -ETIMEDOUT);
150 if (desc->bd_success &&
151 desc->bd_nob_transferred == count)
152 GOTO(cleanup_buf, rc);
154 rc = -ETIMEDOUT; /* XXX should this be a different errno? */
157 DEBUG_REQ(D_ERROR, req, "bulk failed: %s %d(%d), evicting %s@%s\n",
158 (rc == -ETIMEDOUT) ? "timeout" : "network error",
159 desc->bd_nob_transferred, count,
160 req->rq_export->exp_client_uuid.uuid,
161 req->rq_export->exp_connection->c_remote_uuid.uuid);
163 class_fail_export(req->rq_export);
167 ptlrpc_abort_bulk(desc);
169 for (i = 0; i < npages; i++)
171 OBD_PAGE_FREE(pages[i]);
173 ptlrpc_free_bulk(desc);
175 OBD_FREE(pages, sizeof(*pages) * npages);
180 /* only valid locked dentries or errors should be returned */
181 struct dentry *mds_fid2locked_dentry(struct obd_device *obd, struct ll_fid *fid,
182 struct vfsmount **mnt, int lock_mode,
183 struct lustre_handle *lockh,
184 char *name, int namelen, __u64 lockpart)
186 struct mds_obd *mds = &obd->u.mds;
187 struct dentry *de = mds_fid2dentry(mds, fid, mnt), *retval = de;
188 struct ldlm_res_id res_id = { .name = {0} };
189 int flags = LDLM_FL_ATOMIC_CB, rc;
190 ldlm_policy_data_t policy = { .l_inodebits = { lockpart} };
196 res_id.name[0] = de->d_inode->i_ino;
197 res_id.name[1] = de->d_inode->i_generation;
198 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
199 LDLM_IBITS, &policy, lock_mode, &flags,
200 ldlm_blocking_ast, ldlm_completion_ast,
201 NULL, NULL, 0, NULL, lockh);
202 if (rc != ELDLM_OK) {
204 retval = ERR_PTR(-EIO); /* XXX translate ldlm code */
210 /* Look up an entry by inode number. */
211 /* this function ONLY returns valid dget'd dentries with an initialized inode
213 struct dentry *mds_fid2dentry(struct mds_obd *mds, struct ll_fid *fid,
214 struct vfsmount **mnt)
216 struct obd_device *obd = container_of(mds, struct obd_device, u.mds);
218 unsigned long ino = fid->id;
219 __u32 generation = fid->generation;
221 struct dentry *result;
224 RETURN(ERR_PTR(-ESTALE));
226 snprintf(fid_name, sizeof(fid_name), "0x%lx", ino);
228 CDEBUG(D_DENTRY, "--> mds_fid2dentry: ino/gen %lu/%u, sb %p\n",
229 ino, generation, mds->mds_obt.obt_sb);
231 /* under ext3 this is neither supposed to return bad inodes
233 result = mds_lookup(obd, fid_name, mds->mds_fid_de, strlen(fid_name));
237 inode = result->d_inode;
239 RETURN(ERR_PTR(-ENOENT));
241 if (inode->i_nlink == 0) {
242 if (inode->i_mode == 0 &&
243 LTIME_S(inode->i_ctime) == 0 ) {
244 LCONSOLE_WARN("Found inode with zero nlink, mode and "
245 "ctime -- this may indicate disk"
246 "corruption (device %s, inode %lu, link:"
247 " %lu, count: %d)\n", obd->obd_name, inode->i_ino,
248 (unsigned long)inode->i_nlink,
249 atomic_read(&inode->i_count));
252 RETURN(ERR_PTR(-ENOENT));
255 if (generation && inode->i_generation != generation) {
256 /* we didn't find the right inode.. */
257 CDEBUG(D_INODE, "found wrong generation: inode %lu, link: %lu, "
258 "count: %d, generation %u/%u\n", inode->i_ino,
259 (unsigned long)inode->i_nlink,
260 atomic_read(&inode->i_count), inode->i_generation,
263 RETURN(ERR_PTR(-ENOENT));
267 *mnt = mds->mds_vfsmnt;
274 static int mds_connect_internal(struct obd_export *exp,
275 struct obd_connect_data *data)
277 struct obd_device *obd = exp->exp_obd;
279 data->ocd_connect_flags &= MDS_CONNECT_SUPPORTED;
280 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
282 /* If no known bits (which should not happen, probably,
283 as everybody should support LOOKUP and UPDATE bits at least)
284 revert to compat mode with plain locks. */
285 if (!data->ocd_ibits_known &&
286 data->ocd_connect_flags & OBD_CONNECT_IBITS)
287 data->ocd_connect_flags &= ~OBD_CONNECT_IBITS;
289 if (!obd->u.mds.mds_fl_acl)
290 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
292 if (!obd->u.mds.mds_fl_user_xattr)
293 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
295 exp->exp_connect_flags = data->ocd_connect_flags;
296 data->ocd_version = LUSTRE_VERSION_CODE;
297 exp->exp_mds_data.med_ibits_known = data->ocd_ibits_known;
300 if (obd->u.mds.mds_fl_acl &&
301 ((exp->exp_connect_flags & OBD_CONNECT_ACL) == 0)) {
302 CWARN("%s: MDS requires ACL support but client does not\n",
309 static int mds_reconnect(struct obd_export *exp, struct obd_device *obd,
310 struct obd_uuid *cluuid,
311 struct obd_connect_data *data,
317 if (exp == NULL || obd == NULL || cluuid == NULL)
320 rc = mds_connect_internal(exp, data);
322 mds_export_stats_init(obd, exp, 1, localdata);
327 /* Establish a connection to the MDS.
329 * This will set up an export structure for the client to hold state data
330 * about that client, like open files, the last operation number it did
331 * on the server, etc.
333 static int mds_connect(struct lustre_handle *conn, struct obd_device *obd,
334 struct obd_uuid *cluuid, struct obd_connect_data *data,
337 struct obd_export *exp;
338 struct mds_export_data *med;
339 struct lsd_client_data *lcd = NULL;
343 if (!conn || !obd || !cluuid)
346 /* Check for aborted recovery. */
347 target_recovery_check_and_stop(obd);
349 /* XXX There is a small race between checking the list and adding a
350 * new connection for the same UUID, but the real threat (list
351 * corruption when multiple different clients connect) is solved.
353 * There is a second race between adding the export to the list,
354 * and filling in the client data below. Hence skipping the case
355 * of NULL lcd above. We should already be controlling multiple
356 * connects at the client, and we can't hold the spinlock over
357 * memory allocations without risk of deadlocking.
359 rc = class_connect(conn, obd, cluuid);
362 exp = class_conn2export(conn);
364 med = &exp->exp_mds_data;
366 rc = mds_connect_internal(exp, data);
372 GOTO(out, rc = -ENOMEM);
374 memcpy(lcd->lcd_uuid, cluuid, sizeof(lcd->lcd_uuid));
377 rc = mds_client_add(obd, exp, -1, localdata);
386 class_disconnect(exp);
387 /* release nid stat refererence */
388 lprocfs_exp_cleanup(exp);
390 class_export_put(exp);
396 int mds_init_export(struct obd_export *exp)
398 struct mds_export_data *med = &exp->exp_mds_data;
401 INIT_LIST_HEAD(&med->med_open_head);
402 spin_lock_init(&med->med_open_lock);
404 spin_lock(&exp->exp_lock);
405 exp->exp_connecting = 1;
406 spin_unlock(&exp->exp_lock);
408 RETURN(ldlm_init_export(exp));
411 static int mds_destroy_export(struct obd_export *exp)
415 target_destroy_export(exp);
416 ldlm_destroy_export(exp);
418 LASSERT(list_empty(&exp->exp_mds_data.med_open_head));
419 mds_client_free(exp);
424 static int mds_cleanup_mfd(struct obd_export *exp)
426 struct mds_export_data *med;
427 struct obd_device *obd = exp->exp_obd;
428 struct mds_obd *mds = &obd->u.mds;
429 struct lvfs_run_ctxt saved;
430 struct lov_mds_md *lmm;
431 __u32 lmm_sz, cookie_sz;
432 struct llog_cookie *logcookies;
433 struct list_head closing_list;
434 struct mds_file_data *mfd, *n;
438 med = &exp->exp_mds_data;
440 spin_lock(&med->med_open_lock);
441 if (list_empty(&med->med_open_head)) {
442 spin_unlock(&med->med_open_lock);
446 CFS_INIT_LIST_HEAD(&closing_list);
447 while (!list_empty(&med->med_open_head)) {
448 struct list_head *tmp = med->med_open_head.next;
449 struct mds_file_data *mfd =
450 list_entry(tmp, struct mds_file_data, mfd_list);
452 /* Remove mfd handle so it can't be found again.
453 * We are consuming the mfd_list reference here. */
454 mds_mfd_unlink(mfd, 0);
455 list_add_tail(&mfd->mfd_list, &closing_list);
457 spin_unlock(&med->med_open_lock);
459 lmm_sz = mds->mds_max_mdsize;
460 OBD_ALLOC(lmm, lmm_sz);
462 CWARN("%s: allocation failure during cleanup; can not force "
463 "close file handles on this service.\n", obd->obd_name);
464 GOTO(out, rc = -ENOMEM);
467 cookie_sz = mds->mds_max_cookiesize;
468 OBD_ALLOC(logcookies, cookie_sz);
469 if (logcookies == NULL) {
470 CWARN("%s: allocation failure during cleanup; can not force "
471 "close file handles on this service.\n", obd->obd_name);
472 OBD_FREE(lmm, lmm_sz);
473 GOTO(out, rc = -ENOMEM);
476 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
477 /* Close any open files (which may also cause orphan unlinking). */
478 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
479 int lmm_size = lmm_sz;
480 umode_t mode = mfd->mfd_dentry->d_inode->i_mode;
483 /* If you change this message, be sure to update
484 * replay_single:test_46 */
485 CDEBUG(D_INODE|D_IOCTL, "%s: force closing file handle for "
486 "%.*s (ino %lu)\n", obd->obd_name,
487 mfd->mfd_dentry->d_name.len,mfd->mfd_dentry->d_name.name,
488 mfd->mfd_dentry->d_inode->i_ino);
490 rc = mds_get_md(obd, mfd->mfd_dentry->d_inode, lmm,
493 CWARN("mds_get_md failure, rc=%d\n", rc);
495 valid |= OBD_MD_FLEASIZE;
497 /* child orphan sem protects orphan_dec_test and
498 * is_orphan race, mds_mfd_close drops it */
499 MDS_DOWN_WRITE_ORPHAN_SEM(mfd->mfd_dentry->d_inode);
501 list_del_init(&mfd->mfd_list);
502 rc = mds_mfd_close(NULL, REQ_REC_OFF, obd, mfd,
503 !(exp->exp_flags & OBD_OPT_FAILOVER),
504 lmm, lmm_size, logcookies,
505 mds->mds_max_cookiesize,
509 CDEBUG(D_INODE|D_IOCTL, "Error closing file: %d\n", rc);
511 if (valid & OBD_MD_FLCOOKIE) {
512 rc = mds_osc_destroy_orphan(obd, mode, lmm,
513 lmm_size, logcookies, 1);
515 CDEBUG(D_INODE, "%s: destroy of orphan failed,"
516 " rc = %d\n", obd->obd_name, rc);
519 valid &= ~OBD_MD_FLCOOKIE;
523 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
524 OBD_FREE(logcookies, cookie_sz);
525 OBD_FREE(lmm, lmm_sz);
530 static int mds_disconnect(struct obd_export *exp)
536 class_export_get(exp);
538 rc = server_disconnect_export(exp);
540 rc = mds_cleanup_mfd(exp);
542 class_export_put(exp);
546 static int mds_getstatus(struct ptlrpc_request *req)
548 struct mds_obd *mds = mds_req2mds(req);
549 struct mds_body *body;
550 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
553 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_PACK, req->rq_status = -ENOMEM);
554 rc = lustre_pack_reply(req, 2, size, NULL);
556 RETURN(req->rq_status = rc);
558 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body));
559 memcpy(&body->fid1, &mds->mds_rootfid, sizeof(body->fid1));
561 /* the last_committed and last_xid fields are filled in for all
562 * replies already - no need to do so here also.
567 /* get the LOV EA from @inode and store it into @md. It can be at most
568 * @size bytes, and @size is updated with the actual EA size.
569 * The EA size is also returned on success, and -ve errno on failure.
570 * If there is no EA then 0 is returned. */
571 int mds_get_md(struct obd_device *obd, struct inode *inode, void *md,
572 int *size, int lock, int flags,
579 LOCK_INODE_MUTEX(inode);
580 rc = fsfilt_get_md(obd, inode, md, *size, "lov");
582 if (rc == 0 && flags == MDS_GETATTR)
583 rc = mds_get_default_md(obd, md, &lmm_size);
586 CERROR("Error %d reading eadata for ino %lu\n",
590 rc = mds_convert_lov_ea(obd, inode, md, lmm_size,
603 UNLOCK_INODE_MUTEX(inode);
609 /* Call with lock=1 if you want mds_pack_md to take the i_mutex.
610 * Call with lock=0 if the caller has already taken the i_mutex. */
611 int mds_pack_md(struct obd_device *obd, struct lustre_msg *msg, int offset,
612 struct mds_body *body, struct inode *inode, int lock, int flags,
615 struct mds_obd *mds = &obd->u.mds;
621 lmm = lustre_msg_buf(msg, offset, 0);
623 /* Some problem with getting eadata when I sized the reply
625 CDEBUG(D_INFO, "no space reserved for inode %lu MD\n",
629 /* if this replay request we should be silencely exist without fill md*/
630 lmm_size = lustre_msg_buflen(msg, offset);
634 /* I don't really like this, but it is a sanity check on the client
635 * MD request. However, if the client doesn't know how much space
636 * to reserve for the MD, it shouldn't be bad to have too much space.
638 if (lmm_size > mds->mds_max_mdsize) {
639 CWARN("Reading MD for inode %lu of %d bytes > max %d\n",
640 inode->i_ino, lmm_size, mds->mds_max_mdsize);
644 rc = mds_get_md(obd, inode, lmm, &lmm_size, lock, flags,
647 if (S_ISDIR(inode->i_mode))
648 body->valid |= OBD_MD_FLDIREA;
650 body->valid |= OBD_MD_FLEASIZE;
651 body->eadatasize = lmm_size;
658 #ifdef CONFIG_FS_POSIX_ACL
660 int mds_pack_posix_acl(struct inode *inode, struct lustre_msg *repmsg,
661 struct mds_body *repbody, int repoff)
663 struct dentry de = { .d_inode = inode };
667 LASSERT(repbody->aclsize == 0);
668 LASSERT(lustre_msg_bufcount(repmsg) > repoff);
670 buflen = lustre_msg_buflen(repmsg, repoff);
674 if (!inode->i_op || !inode->i_op->getxattr)
677 rc = inode->i_op->getxattr(&de, MDS_XATTR_NAME_ACL_ACCESS,
678 lustre_msg_buf(repmsg, repoff, buflen),
681 repbody->aclsize = rc;
682 } else if (rc != -ENODATA) {
683 CERROR("buflen %d, get acl: %d\n", buflen, rc);
688 repbody->valid |= OBD_MD_FLACL;
692 #define mds_pack_posix_acl(inode, repmsg, repbody, repoff) 0
695 int mds_pack_acl(struct mds_export_data *med, struct inode *inode,
696 struct lustre_msg *repmsg, struct mds_body *repbody,
699 return mds_pack_posix_acl(inode, repmsg, repbody, repoff);
702 static int mds_getattr_internal(struct obd_device *obd, struct dentry *dentry,
703 struct ptlrpc_request *req,
704 struct mds_body *reqbody, int reply_off)
706 struct mds_body *body;
707 struct inode *inode = dentry->d_inode;
715 body = lustre_msg_buf(req->rq_repmsg, reply_off, sizeof(*body));
716 LASSERT(body != NULL); /* caller prepped reply */
718 body->flags = reqbody->flags; /* copy MDS_BFLAG_EXT_FLAGS if present */
719 mds_pack_inode2body(body, inode);
722 if ((S_ISREG(inode->i_mode) && (reqbody->valid & OBD_MD_FLEASIZE)) ||
723 (S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))) {
724 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR &&
725 ((S_ISDIR(inode->i_mode) && (reqbody->valid & OBD_MD_FLDIREA))))
728 rc = mds_pack_md(obd, req->rq_repmsg, reply_off, body,
730 req->rq_export->exp_connect_flags);
732 /* If we have LOV EA data, the OST holds size, atime, mtime */
733 if (!(body->valid & OBD_MD_FLEASIZE) &&
734 !(body->valid & OBD_MD_FLDIREA))
735 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
736 OBD_MD_FLATIME | OBD_MD_FLMTIME);
739 } else if (S_ISLNK(inode->i_mode) &&
740 (reqbody->valid & OBD_MD_LINKNAME) != 0) {
741 char *symname = lustre_msg_buf(req->rq_repmsg, reply_off, 0);
744 LASSERT (symname != NULL); /* caller prepped reply */
745 len = lustre_msg_buflen(req->rq_repmsg, reply_off);
747 rc = inode->i_op->readlink(dentry, symname, len);
749 CERROR("readlink failed: %d\n", rc);
750 } else if (rc != len - 1) {
751 CERROR ("Unexpected readlink rc %d: expecting %d\n",
755 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
758 CDEBUG(D_INODE, "read symlink dest %s\n", symname);
759 body->valid |= OBD_MD_LINKNAME;
760 body->eadatasize = rc + 1;
761 symname[rc] = 0; /* NULL terminate */
765 } else if (reqbody->valid == OBD_MD_FLFLAGS &&
766 reqbody->flags & MDS_BFLAG_EXT_FLAGS) {
769 /* We only return the full set of flags on ioctl, otherwise we
770 * get enough flags from the inode in mds_pack_inode2body(). */
771 rc = fsfilt_iocontrol(obd, dentry, FSFILT_IOC_GETFLAGS,
774 body->flags = flags | MDS_BFLAG_EXT_FLAGS;
777 if (reqbody->valid & OBD_MD_FLMODEASIZE) {
778 struct mds_obd *mds = mds_req2mds(req);
779 body->max_cookiesize = mds->mds_max_cookiesize;
780 body->max_mdsize = mds->mds_max_mdsize;
781 body->valid |= OBD_MD_FLMODEASIZE;
787 #ifdef CONFIG_FS_POSIX_ACL
788 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
789 (reqbody->valid & OBD_MD_FLACL)) {
790 rc = mds_pack_acl(&req->rq_export->exp_mds_data,
791 inode, req->rq_repmsg,
802 static int mds_getattr_pack_msg(struct ptlrpc_request *req, struct inode *inode,
805 struct mds_obd *mds = mds_req2mds(req);
806 struct mds_body *body;
807 int rc, bufcount = REPLY_REC_OFF + 1;
808 int size[4] = { sizeof(struct ptlrpc_body),
812 LASSERT(offset == REQ_REC_OFF); /* non-intent */
814 body = lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*body));
815 LASSERT(body != NULL); /* checked by caller */
816 LASSERT(lustre_req_swabbed(req, offset)); /* swabbed by caller */
818 if (body->valid & (OBD_MD_FLEASIZE | OBD_MD_FLDIREA)) {
819 /* this will be shrinked to actual size before size */
820 if (S_ISREG(inode->i_mode) || (S_ISDIR(inode->i_mode)))
821 size[bufcount ++] = mds->mds_max_mdsize;
823 /* we not want LSM for specfial files */
824 body->valid &= ~(OBD_MD_FLEASIZE | OBD_MD_FLDIREA);
825 } else if (S_ISLNK(inode->i_mode) && (body->valid & OBD_MD_LINKNAME)) {
826 if (i_size_read(inode) > body->eadatasize)
827 CERROR("symlink size: %Lu, reply space: %d\n",
828 i_size_read(inode) + 1, body->eadatasize);
829 size[bufcount ++] = min_t(int, i_size_read(inode) + 1,
831 CDEBUG(D_INODE, "symlink size: %Lu, reply space: %d\n",
832 i_size_read(inode) + 1, body->eadatasize);
834 #ifdef CONFIG_FS_POSIX_ACL
835 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
836 (body->valid & OBD_MD_FLACL)) {
837 size[bufcount ++] = LUSTRE_POSIX_ACL_MAX_SIZE;
841 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK)) {
842 CERROR("failed MDS_GETATTR_PACK test\n");
843 req->rq_status = -ENOMEM;
847 rc = lustre_pack_reply(req, bufcount, size, NULL);
856 static int mds_getattr_lock(struct ptlrpc_request *req, int offset,
857 int child_part, struct lustre_handle *child_lockh)
859 struct obd_device *obd = req->rq_export->exp_obd;
860 struct mds_obd *mds = &obd->u.mds;
861 struct ldlm_reply *rep = NULL;
862 struct lvfs_run_ctxt saved;
863 struct mds_body *body;
864 struct dentry *dparent = NULL, *dchild = NULL;
865 struct lvfs_ucred uc = {NULL,};
866 struct lustre_handle parent_lockh;
868 int rc = 0, cleanup_phase = 0, resent_req = 0;
869 int rq_offset = offset;
873 LASSERT(!strcmp(obd->obd_type->typ_name, LUSTRE_MDS_NAME));
874 LASSERT(offset == REQ_REC_OFF || offset == DLM_INTENT_REC_OFF);
875 /* if requests were at offset 2, the getattr reply goes back at 1 */
876 if (offset == DLM_INTENT_REC_OFF) {
877 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
879 offset = DLM_REPLY_REC_OFF;
882 /* Swab now, before anyone looks inside the request */
883 body = lustre_swab_reqbuf(req, rq_offset, sizeof(*body),
884 lustre_swab_mds_body);
886 CERROR("Can't swab mds_body\n");
887 GOTO(cleanup_exit, rc = -EFAULT);
890 lustre_set_req_swabbed(req, rq_offset + 1);
891 name = lustre_msg_string(req->rq_reqmsg, rq_offset + 1, 0);
893 CERROR("Can't unpack name\n");
894 GOTO(cleanup_exit, rc = -EFAULT);
896 namesize = lustre_msg_buflen(req->rq_reqmsg, rq_offset + 1);
897 /* namesize less than 2 means we have empty name, probably came from
898 revalidate by cfid, so no point in having name to be set */
902 rc = mds_init_ucred(&uc, req, rq_offset);
907 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
908 cleanup_phase = 1; /* kernel context */
909 ldlm_reply_set_disposition(rep, DISP_LOOKUP_EXECD);
911 /* FIXME: handle raw lookup */
913 if (body->valid == OBD_MD_FLID) {
914 struct mds_body *mds_reply;
915 int size = sizeof(*mds_reply);
917 // The user requested ONLY the inode number, so do a raw lookup
918 rc = lustre_pack_reply(req, 1, &size, NULL);
920 CERROR("out of memory\n");
924 rc = dir->i_op->lookup_raw(dir, name, namesize - 1, &inum);
926 mds_reply = lustre_msg_buf(req->rq_repmsg, offset,
928 mds_reply->fid1.id = inum;
929 mds_reply->valid = OBD_MD_FLID;
934 /* child_lockh() is only set in fixup_handle_for_resent_req()
935 * if MSG_RESENT is set */
936 if (lustre_handle_is_used(child_lockh)) {
937 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
941 if (resent_req == 0) {
943 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout*2);
944 rc = mds_get_parent_child_locked(obd, &obd->u.mds,
948 MDS_INODELOCK_UPDATE,
950 child_lockh, &dchild,
953 /* For revalidate by fid we always take UPDATE lock */
954 dchild = mds_fid2locked_dentry(obd, &body->fid2, NULL,
956 NULL, 0, child_part);
959 rc = PTR_ERR(dchild);
964 struct ldlm_lock *granted_lock;
965 struct ll_fid child_fid;
966 struct ldlm_resource *res;
967 DEBUG_REQ(D_DLMTRACE, req, "resent, not enqueuing new locks");
968 granted_lock = ldlm_handle2lock(child_lockh);
969 /* lock was granted in fixup_handle_for_resent_req() if
970 * MSG_RESENT is set */
971 LASSERTF(granted_lock != NULL, LPU64"/%u lockh "LPX64"\n",
972 body->fid1.id, body->fid1.generation,
973 child_lockh->cookie);
976 res = granted_lock->l_resource;
977 child_fid.id = res->lr_name.name[0];
978 child_fid.generation = res->lr_name.name[1];
979 dchild = mds_fid2dentry(&obd->u.mds, &child_fid, NULL);
980 if (IS_ERR(dchild)) {
981 rc = PTR_ERR(dchild);
982 LCONSOLE_WARN("Child "LPU64"/%u lookup error %d.",
983 child_fid.id, child_fid.generation, rc);
986 LDLM_LOCK_PUT(granted_lock);
989 cleanup_phase = 2; /* dchild, dparent, locks */
991 if (dchild->d_inode == NULL) {
992 ldlm_reply_set_disposition(rep, DISP_LOOKUP_NEG);
993 /* in the intent case, the policy clears this error:
994 the disposition is enough */
995 GOTO(cleanup, rc = -ENOENT);
997 ldlm_reply_set_disposition(rep, DISP_LOOKUP_POS);
1000 if (req->rq_repmsg == NULL) {
1001 rc = mds_getattr_pack_msg(req, dchild->d_inode, offset);
1003 CERROR ("mds_getattr_pack_msg: %d\n", rc);
1008 rc = mds_getattr_internal(obd, dchild, req, body, offset);
1009 GOTO(cleanup, rc); /* returns the lock to the client */
1012 switch (cleanup_phase) {
1014 if (resent_req == 0) {
1015 if (rc && dchild->d_inode)
1016 ldlm_lock_decref(child_lockh, LCK_CR);
1018 ldlm_lock_decref(&parent_lockh, LCK_CR);
1024 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1026 mds_exit_ucred(&uc, mds);
1027 if (!req->rq_packed_final) {
1028 int rc2 = lustre_pack_reply(req, 1, NULL, NULL);
1031 req->rq_status = rc;
1038 static int mds_getattr(struct ptlrpc_request *req, int offset)
1040 struct mds_obd *mds = mds_req2mds(req);
1041 struct obd_device *obd = req->rq_export->exp_obd;
1042 struct lvfs_run_ctxt saved;
1044 struct mds_body *body;
1045 struct lvfs_ucred uc = { NULL, };
1049 OBD_COUNTER_INCREMENT(obd, getattr);
1051 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1052 lustre_swab_mds_body);
1054 GOTO(cleanup_exit, rc = -EFAULT);
1056 rc = mds_init_ucred(&uc, req, offset);
1058 GOTO(out_ucred, rc);
1060 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1061 de = mds_fid2dentry(mds, &body->fid1, NULL);
1063 req->rq_status = PTR_ERR(de);
1067 rc = mds_getattr_pack_msg(req, de->d_inode, offset);
1069 CERROR("mds_getattr_pack_msg: %d\n", rc);
1073 req->rq_status = mds_getattr_internal(obd, de, req, body,REPLY_REC_OFF);
1078 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1080 if (!req->rq_packed_final) {
1081 int rc2 = lustre_pack_reply(req, 1, NULL, NULL);
1085 req->rq_status = rc;
1087 mds_exit_ucred(&uc, mds);
1090 mds_shrink_body_reply(req, offset, REPLY_REC_OFF);
1094 static int mds_obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1095 __u64 max_age, __u32 flags)
1099 spin_lock(&obd->obd_osfs_lock);
1100 rc = fsfilt_statfs(obd, obd->u.obt.obt_sb, max_age);
1102 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1103 spin_unlock(&obd->obd_osfs_lock);
1108 static int mds_statfs(struct ptlrpc_request *req)
1110 struct obd_device *obd = req->rq_export->exp_obd;
1111 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1112 int rc, size[2] = { sizeof(struct ptlrpc_body),
1113 sizeof(struct obd_statfs) };
1116 /* This will trigger a watchdog timeout */
1117 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
1118 (MDS_SERVICE_WATCHDOG_FACTOR *
1119 at_get(&svc->srv_at_estimate)) + 1);
1120 OBD_COUNTER_INCREMENT(obd, statfs);
1122 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
1123 GOTO(out, rc = -ENOMEM);
1124 rc = lustre_pack_reply(req, 2, size, NULL);
1128 /* We call this so that we can cache a bit - 1 jiffie worth */
1129 rc = mds_obd_statfs(obd, lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1130 size[REPLY_REC_OFF]),
1131 cfs_time_current_64() - HZ, 0);
1133 CERROR("mds_obd_statfs failed: rc %d\n", rc);
1139 req->rq_status = rc;
1143 static int mds_sync(struct ptlrpc_request *req, int offset)
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[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
1151 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1152 lustre_swab_mds_body);
1154 GOTO(out, rc = -EFAULT);
1156 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
1157 GOTO(out, rc = -ENOMEM);
1158 rc = lustre_pack_reply(req, 2, size, NULL);
1162 rc = fsfilt_sync(obd, obd->u.obt.obt_sb);
1163 if (rc == 0 && body->fid1.id != 0) {
1166 de = mds_fid2dentry(mds, &body->fid1, NULL);
1168 GOTO(out, rc = PTR_ERR(de));
1170 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1172 mds_pack_inode2body(body, de->d_inode);
1178 req->rq_status = rc;
1182 /* mds_readpage does not take a DLM lock on the inode, because the client must
1183 * already have a PR lock.
1185 * If we were to take another one here, a deadlock will result, if another
1186 * thread is already waiting for a PW lock. */
1187 static int mds_readpage(struct ptlrpc_request *req, int offset)
1189 struct obd_device *obd = req->rq_export->exp_obd;
1190 struct mds_obd *mds = &obd->u.mds;
1191 struct vfsmount *mnt;
1194 struct mds_body *body, *repbody;
1195 struct lvfs_run_ctxt saved;
1196 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repbody) };
1197 struct lvfs_ucred uc = {NULL,};
1200 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_PACK, -ENOMEM);
1201 rc = lustre_pack_reply(req, 2, size, NULL);
1205 body = lustre_swab_reqbuf(req, offset, sizeof(*body),
1206 lustre_swab_mds_body);
1208 GOTO (out, rc = -EFAULT);
1210 rc = mds_init_ucred(&uc, req, offset);
1214 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1215 de = mds_fid2dentry(&obd->u.mds, &body->fid1, &mnt);
1217 GOTO(out_pop, rc = PTR_ERR(de));
1219 CDEBUG(D_INODE, "ino %lu\n", de->d_inode->i_ino);
1221 file = dentry_open(de, mnt, O_RDONLY | O_LARGEFILE);
1222 /* note: in case of an error, dentry_open puts dentry */
1224 GOTO(out_pop, rc = PTR_ERR(file));
1226 /* body->size is actually the offset -eeb */
1227 if ((body->size & (de->d_inode->i_sb->s_blocksize - 1)) != 0) {
1228 CERROR("offset "LPU64" not on a block boundary of %lu\n",
1229 body->size, de->d_inode->i_sb->s_blocksize);
1230 GOTO(out_file, rc = -EFAULT);
1233 /* body->nlink is actually the #bytes to read -eeb */
1234 if (body->nlink & (de->d_inode->i_sb->s_blocksize - 1)) {
1235 CERROR("size %u is not multiple of blocksize %lu\n",
1236 body->nlink, de->d_inode->i_sb->s_blocksize);
1237 GOTO(out_file, rc = -EFAULT);
1240 repbody = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
1242 repbody->size = i_size_read(file->f_dentry->d_inode);
1243 repbody->valid = OBD_MD_FLSIZE;
1245 /* to make this asynchronous make sure that the handling function
1246 doesn't send a reply when this function completes. Instead a
1247 callback function would send the reply */
1248 /* body->size is actually the offset -eeb */
1249 rc = mds_sendpage(req, file, body->size, body->nlink);
1252 filp_close(file, 0);
1254 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &uc);
1256 mds_exit_ucred(&uc, mds);
1257 req->rq_status = rc;
1261 int mds_reint(struct ptlrpc_request *req, int offset,
1262 struct lustre_handle *lockh)
1264 struct mds_update_record *rec; /* 116 bytes on the stack? no sir! */
1267 OBD_ALLOC(rec, sizeof(*rec));
1271 rc = mds_update_unpack(req, offset, rec);
1272 if (rc || OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNPACK)) {
1273 CERROR("invalid record\n");
1274 GOTO(out, req->rq_status = -EINVAL);
1277 mds_root_squash(&req->rq_export->exp_obd->u.mds, &req->rq_peer.nid,
1278 &rec->ur_uc.luc_fsuid, &rec->ur_uc.luc_fsgid,
1279 &rec->ur_uc.luc_cap, &rec->ur_uc.luc_suppgid1,
1280 &rec->ur_uc.luc_suppgid2);
1282 /* rc will be used to interrupt a for loop over multiple records */
1283 rc = mds_reint_rec(rec, offset, req, lockh);
1285 OBD_FREE(rec, sizeof(*rec));
1289 static int mds_filter_recovery_request(struct ptlrpc_request *req,
1290 struct obd_device *obd, int *process)
1292 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1293 case MDS_CONNECT: /* This will never get here, but for completeness. */
1294 case OST_CONNECT: /* This will never get here, but for completeness. */
1295 case MDS_DISCONNECT:
1296 case OST_DISCONNECT:
1301 case MDS_SYNC: /* used in unmounting */
1306 *process = target_queue_recovery_request(req, obd);
1310 DEBUG_REQ(D_ERROR, req, "not permitted during recovery");
1312 /* XXX what should we set rq_status to here? */
1313 req->rq_status = -EAGAIN;
1314 RETURN(ptlrpc_error(req));
1318 static char *reint_names[] = {
1319 [REINT_SETATTR] "setattr",
1320 [REINT_CREATE] "create",
1321 [REINT_LINK] "link",
1322 [REINT_UNLINK] "unlink",
1323 [REINT_RENAME] "rename",
1324 [REINT_OPEN] "open",
1327 static int mds_set_info_rpc(struct obd_export *exp, struct ptlrpc_request *req)
1330 int keylen, vallen, rc = 0;
1333 key = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, 1);
1335 DEBUG_REQ(D_HA, req, "no set_info key");
1338 keylen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF);
1340 val = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 1, 0);
1341 vallen = lustre_msg_buflen(req->rq_reqmsg, REQ_REC_OFF + 1);
1343 rc = lustre_pack_reply(req, 1, NULL, NULL);
1347 lustre_msg_set_status(req->rq_repmsg, 0);
1349 /* Accept the broken "read-only" key from 1.6.6 clients. b=17493 */
1350 if (KEY_IS(KEY_READONLY) || KEY_IS(KEY_READONLY_166COMPAT)) {
1351 if (val == NULL || vallen < sizeof(__u32)) {
1352 DEBUG_REQ(D_HA, req, "no set_info val");
1357 exp->exp_connect_flags |= OBD_CONNECT_RDONLY;
1359 exp->exp_connect_flags &= ~OBD_CONNECT_RDONLY;
1367 #ifdef HAVE_QUOTA_SUPPORT
1368 static int mds_handle_quotacheck(struct ptlrpc_request *req)
1370 struct obd_quotactl *oqctl;
1374 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1375 lustre_swab_obd_quotactl);
1379 rc = lustre_pack_reply(req, 1, NULL, NULL);
1383 req->rq_status = obd_quotacheck(req->rq_export, oqctl);
1387 static int mds_handle_quotactl(struct ptlrpc_request *req)
1389 struct obd_quotactl *oqctl, *repoqc;
1390 int rc, size[2] = { sizeof(struct ptlrpc_body), sizeof(*repoqc) };
1393 oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1394 lustre_swab_obd_quotactl);
1398 rc = lustre_pack_reply(req, 2, size, NULL);
1402 repoqc = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*repoqc));
1404 req->rq_status = obd_quotactl(req->rq_export, oqctl);
1410 static int mds_msg_check_version(struct lustre_msg *msg)
1414 switch (lustre_msg_get_opc(msg)) {
1416 case MDS_DISCONNECT:
1418 rc = lustre_msg_check_version(msg, LUSTRE_OBD_VERSION);
1420 CERROR("bad opc %u version %08x, expecting %08x\n",
1421 lustre_msg_get_opc(msg),
1422 lustre_msg_get_version(msg),
1423 LUSTRE_OBD_VERSION);
1427 case MDS_GETATTR_NAME:
1432 case MDS_DONE_WRITING:
1438 case MDS_QUOTACHECK:
1442 rc = lustre_msg_check_version(msg, LUSTRE_MDS_VERSION);
1444 CERROR("bad opc %u version %08x, expecting %08x\n",
1445 lustre_msg_get_opc(msg),
1446 lustre_msg_get_version(msg),
1447 LUSTRE_MDS_VERSION);
1451 case LDLM_BL_CALLBACK:
1452 case LDLM_CP_CALLBACK:
1453 rc = lustre_msg_check_version(msg, LUSTRE_DLM_VERSION);
1455 CERROR("bad opc %u version %08x, expecting %08x\n",
1456 lustre_msg_get_opc(msg),
1457 lustre_msg_get_version(msg),
1458 LUSTRE_DLM_VERSION);
1460 case OBD_LOG_CANCEL:
1461 case LLOG_ORIGIN_HANDLE_CREATE:
1462 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1463 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1464 case LLOG_ORIGIN_HANDLE_CLOSE:
1465 case LLOG_ORIGIN_HANDLE_DESTROY:
1466 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1468 rc = lustre_msg_check_version(msg, LUSTRE_LOG_VERSION);
1470 CERROR("bad opc %u version %08x, expecting %08x\n",
1471 lustre_msg_get_opc(msg),
1472 lustre_msg_get_version(msg),
1473 LUSTRE_LOG_VERSION);
1476 CERROR("MDS unknown opcode %d\n", lustre_msg_get_opc(msg));
1482 int mds_handle(struct ptlrpc_request *req)
1484 int should_process, fail = OBD_FAIL_MDS_ALL_REPLY_NET;
1486 struct mds_obd *mds = NULL; /* quell gcc overwarning */
1487 struct obd_device *obd = NULL;
1490 OBD_FAIL_RETURN(OBD_FAIL_MDS_ALL_REQUEST_NET | OBD_FAIL_ONCE, 0);
1492 LASSERT(current->journal_info == NULL);
1494 rc = mds_msg_check_version(req->rq_reqmsg);
1496 CERROR("MDS drop mal-formed request\n");
1500 /* XXX identical to OST */
1501 if (lustre_msg_get_opc(req->rq_reqmsg) != MDS_CONNECT) {
1502 struct mds_export_data *med;
1505 if (req->rq_export == NULL) {
1506 CERROR("operation %d on unconnected MDS from %s\n",
1507 lustre_msg_get_opc(req->rq_reqmsg),
1508 libcfs_id2str(req->rq_peer));
1509 req->rq_status = -ENOTCONN;
1510 GOTO(out, rc = -ENOTCONN);
1513 med = &req->rq_export->exp_mds_data;
1514 obd = req->rq_export->exp_obd;
1517 /* sanity check: if the xid matches, the request must
1518 * be marked as a resent or replayed */
1519 if (req->rq_xid == le64_to_cpu(med->med_lcd->lcd_last_xid) ||
1520 req->rq_xid == le64_to_cpu(med->med_lcd->lcd_last_close_xid))
1521 if (!(lustre_msg_get_flags(req->rq_reqmsg) &
1522 (MSG_RESENT | MSG_REPLAY))) {
1523 CERROR("rq_xid "LPU64" matches last_xid, "
1524 "expected RESENT flag\n",
1526 req->rq_status = -ENOTCONN;
1527 GOTO(out, rc = -EFAULT);
1529 /* else: note the opposite is not always true; a
1530 * RESENT req after a failover will usually not match
1531 * the last_xid, since it was likely never
1532 * committed. A REPLAYed request will almost never
1533 * match the last xid, however it could for a
1534 * committed, but still retained, open. */
1536 /* Check for aborted recovery. */
1537 spin_lock_bh(&obd->obd_processing_task_lock);
1538 recovering = obd->obd_recovering;
1539 spin_unlock_bh(&obd->obd_processing_task_lock);
1541 target_recovery_check_and_stop(obd) == 0) {
1542 rc = mds_filter_recovery_request(req, obd,
1544 if (rc || !should_process)
1549 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1551 DEBUG_REQ(D_INODE, req, "connect");
1552 OBD_FAIL_RETURN(OBD_FAIL_MDS_CONNECT_NET, 0);
1553 rc = target_handle_connect(req, mds_handle);
1555 /* Now that we have an export, set obd. */
1556 obd = req->rq_export->exp_obd;
1560 case MDS_DISCONNECT:
1561 DEBUG_REQ(D_INODE, req, "disconnect");
1562 OBD_FAIL_RETURN(OBD_FAIL_MDS_DISCONNECT_NET, 0);
1563 rc = target_handle_disconnect(req);
1564 req->rq_status = rc; /* superfluous? */
1568 DEBUG_REQ(D_INODE, req, "getstatus");
1569 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETSTATUS_NET, 0);
1570 rc = mds_getstatus(req);
1574 DEBUG_REQ(D_INODE, req, "getattr");
1575 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NET, 0);
1576 rc = mds_getattr(req, REQ_REC_OFF);
1580 DEBUG_REQ(D_INODE, req, "setxattr");
1581 OBD_FAIL_RETURN(OBD_FAIL_MDS_SETXATTR_NET, 0);
1582 rc = mds_setxattr(req);
1586 DEBUG_REQ(D_INODE, req, "getxattr");
1587 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETXATTR_NET, 0);
1588 rc = mds_getxattr(req);
1591 case MDS_GETATTR_NAME: {
1592 struct lustre_handle lockh = { 0 };
1593 DEBUG_REQ(D_INODE, req, "getattr_name");
1594 OBD_FAIL_RETURN(OBD_FAIL_MDS_GETATTR_NAME_NET, 0);
1596 /* If this request gets a reconstructed reply, we won't be
1597 * acquiring any new locks in mds_getattr_lock, so we don't
1600 rc = mds_getattr_lock(req, REQ_REC_OFF, MDS_INODELOCK_UPDATE,
1602 mds_shrink_body_reply(req, REQ_REC_OFF, REPLY_REC_OFF);
1603 /* this non-intent call (from an ioctl) is special */
1604 req->rq_status = rc;
1605 if (rc == 0 && lustre_handle_is_used(&lockh))
1606 ldlm_lock_decref(&lockh, LCK_CR);
1610 DEBUG_REQ(D_INODE, req, "statfs");
1611 OBD_FAIL_RETURN(OBD_FAIL_MDS_STATFS_NET, 0);
1612 rc = mds_statfs(req);
1616 DEBUG_REQ(D_INODE, req, "readpage");
1617 OBD_FAIL_RETURN(OBD_FAIL_MDS_READPAGE_NET, 0);
1618 rc = mds_readpage(req, REQ_REC_OFF);
1620 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_SENDPAGE)) {
1627 __u32 *opcp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
1631 int size[4] = { sizeof(struct ptlrpc_body),
1632 sizeof(struct mds_body),
1633 mds->mds_max_mdsize,
1634 mds->mds_max_cookiesize };
1637 /* NB only peek inside req now; mds_reint() will swab it */
1639 CERROR ("Can't inspect opcode\n");
1644 if (lustre_req_need_swab(req))
1647 DEBUG_REQ(D_INODE, req, "reint %d (%s)", opc,
1648 (opc < REINT_MAX) ? reint_names[opc] :
1653 op = PTLRPC_LAST_CNTR + MDS_REINT_CREATE;
1656 op = PTLRPC_LAST_CNTR + MDS_REINT_LINK;
1659 op = PTLRPC_LAST_CNTR + MDS_REINT_OPEN;
1662 op = PTLRPC_LAST_CNTR + MDS_REINT_SETATTR;
1665 op = PTLRPC_LAST_CNTR + MDS_REINT_RENAME;
1668 op = PTLRPC_LAST_CNTR + MDS_REINT_UNLINK;
1675 if (op && req->rq_rqbd->rqbd_service->srv_stats)
1676 lprocfs_counter_incr(
1677 req->rq_rqbd->rqbd_service->srv_stats, op);
1679 OBD_FAIL_RETURN(OBD_FAIL_MDS_REINT_NET, 0);
1681 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1683 else if (opc == REINT_OPEN)
1688 /* if we do recovery we isn't send reply mds state is restored */
1689 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1690 size[DLM_REPLY_REC_OFF] = 0;
1691 if (opc == REINT_UNLINK || opc == REINT_RENAME)
1692 size[DLM_REPLY_REC_OFF + 1] = 0;
1695 rc = lustre_pack_reply(req, bufcount, size, NULL);
1699 rc = mds_reint(req, REQ_REC_OFF, NULL);
1700 mds_shrink_intent_reply(req, opc, REPLY_REC_OFF);
1701 fail = OBD_FAIL_MDS_REINT_NET_REP;
1706 DEBUG_REQ(D_INODE, req, "close");
1707 OBD_FAIL_RETURN(OBD_FAIL_MDS_CLOSE_NET, 0);
1708 rc = mds_close(req, REQ_REC_OFF);
1709 mds_shrink_body_reply(req, REQ_REC_OFF, REPLY_REC_OFF);
1710 fail = OBD_FAIL_MDS_CLOSE_NET_REP;
1713 case MDS_DONE_WRITING:
1714 DEBUG_REQ(D_INODE, req, "done_writing");
1715 OBD_FAIL_RETURN(OBD_FAIL_MDS_DONE_WRITING_NET, 0);
1716 rc = mds_done_writing(req, REQ_REC_OFF);
1720 DEBUG_REQ(D_INODE, req, "pin");
1721 OBD_FAIL_RETURN(OBD_FAIL_MDS_PIN_NET, 0);
1722 rc = mds_pin(req, REQ_REC_OFF);
1726 DEBUG_REQ(D_INODE, req, "sync");
1727 OBD_FAIL_RETURN(OBD_FAIL_MDS_SYNC_NET, 0);
1728 rc = mds_sync(req, REQ_REC_OFF);
1732 DEBUG_REQ(D_INODE, req, "set_info");
1733 rc = mds_set_info_rpc(req->rq_export, req);
1735 #ifdef HAVE_QUOTA_SUPPORT
1736 case MDS_QUOTACHECK:
1737 DEBUG_REQ(D_INODE, req, "quotacheck");
1738 OBD_FAIL_RETURN(OBD_FAIL_MDS_QUOTACHECK_NET, 0);
1739 rc = mds_handle_quotacheck(req);
1743 DEBUG_REQ(D_INODE, req, "quotactl");
1744 OBD_FAIL_RETURN(OBD_FAIL_MDS_QUOTACTL_NET, 0);
1745 rc = mds_handle_quotactl(req);
1749 DEBUG_REQ(D_INODE, req, "ping");
1750 rc = target_handle_ping(req);
1751 if (req->rq_export->exp_delayed)
1752 mds_update_client_epoch(req->rq_export);
1755 case OBD_LOG_CANCEL:
1756 CDEBUG(D_INODE, "log cancel\n");
1757 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1758 rc = -ENOTSUPP; /* la la la */
1762 DEBUG_REQ(D_INODE, req, "enqueue");
1763 OBD_FAIL_RETURN(OBD_FAIL_LDLM_ENQUEUE, 0);
1764 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
1765 ldlm_server_blocking_ast, NULL);
1766 fail = OBD_FAIL_LDLM_REPLY;
1769 DEBUG_REQ(D_INODE, req, "convert");
1770 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CONVERT, 0);
1771 rc = ldlm_handle_convert(req);
1773 case LDLM_BL_CALLBACK:
1774 case LDLM_CP_CALLBACK:
1775 DEBUG_REQ(D_INODE, req, "callback");
1776 CERROR("callbacks should not happen on MDS\n");
1778 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
1780 case LLOG_ORIGIN_HANDLE_CREATE:
1781 DEBUG_REQ(D_INODE, req, "llog_init");
1782 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1783 rc = llog_origin_handle_create(req);
1785 case LLOG_ORIGIN_HANDLE_DESTROY:
1786 DEBUG_REQ(D_INODE, req, "llog_init");
1787 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1788 rc = llog_origin_handle_destroy(req);
1790 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1791 DEBUG_REQ(D_INODE, req, "llog next block");
1792 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1793 rc = llog_origin_handle_next_block(req);
1795 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1796 DEBUG_REQ(D_INODE, req, "llog prev block");
1797 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1798 rc = llog_origin_handle_prev_block(req);
1800 case LLOG_ORIGIN_HANDLE_READ_HEADER:
1801 DEBUG_REQ(D_INODE, req, "llog read header");
1802 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1803 rc = llog_origin_handle_read_header(req);
1805 case LLOG_ORIGIN_HANDLE_CLOSE:
1806 DEBUG_REQ(D_INODE, req, "llog close");
1807 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1808 rc = llog_origin_handle_close(req);
1811 DEBUG_REQ(D_INODE, req, "llog catinfo");
1812 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1813 rc = llog_catinfo(req);
1816 req->rq_status = -ENOTSUPP;
1817 rc = ptlrpc_error(req);
1821 LASSERT(current->journal_info == NULL);
1823 /* If we're DISCONNECTing, the mds_export_data is already freed */
1824 if (!rc && lustre_msg_get_opc(req->rq_reqmsg) != MDS_DISCONNECT) {
1825 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1827 /* I don't think last_xid is used for anyway, so I'm not sure
1828 if we need to care about last_close_xid here.*/
1829 lustre_msg_set_last_xid(req->rq_repmsg,
1830 le64_to_cpu(med->med_lcd->lcd_last_xid));
1831 target_committed_to_req(req);
1836 return target_handle_reply(req, rc, fail);
1839 /* Update the server data on disk. This stores the new mount_count and
1840 * also the last_rcvd value to disk. If we don't have a clean shutdown,
1841 * then the server last_rcvd value may be less than that of the clients.
1842 * This will alert us that we may need to do client recovery.
1844 * Also assumes for mds_last_transno that we are not modifying it (no locking).
1846 int mds_update_server_data(struct obd_device *obd, int force_sync)
1848 struct mds_obd *mds = &obd->u.mds;
1849 struct lr_server_data *lsd = mds->mds_server_data;
1850 struct file *filp = mds->mds_rcvd_filp;
1851 struct lvfs_run_ctxt saved;
1856 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
1857 mds->mds_mount_count, mds->mds_last_transno);
1859 spin_lock(&mds->mds_transno_lock);
1860 lsd->lsd_last_transno = cpu_to_le64(mds->mds_last_transno);
1861 spin_unlock(&mds->mds_transno_lock);
1863 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1864 rc = fsfilt_write_record(obd, filp, lsd, sizeof(*lsd), &off,force_sync);
1865 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1867 CERROR("error writing MDS server data: rc = %d\n", rc);
1872 static void fsoptions_to_mds_flags(struct mds_obd *mds, char *options)
1882 while (*p && *p != ',')
1886 if (len == sizeof("user_xattr") - 1 &&
1887 memcmp(options, "user_xattr", len) == 0) {
1888 mds->mds_fl_user_xattr = 1;
1889 LCONSOLE_INFO("Enabling user_xattr\n");
1890 } else if (len == sizeof("nouser_xattr") - 1 &&
1891 memcmp(options, "nouser_xattr", len) == 0) {
1892 mds->mds_fl_user_xattr = 0;
1893 LCONSOLE_INFO("Disabling user_xattr\n");
1894 } else if (len == sizeof("acl") - 1 &&
1895 memcmp(options, "acl", len) == 0) {
1896 #ifdef CONFIG_FS_POSIX_ACL
1897 mds->mds_fl_acl = 1;
1898 LCONSOLE_INFO("Enabling ACL\n");
1900 CWARN("ignoring unsupported acl mount option\n");
1902 } else if (len == sizeof("noacl") - 1 &&
1903 memcmp(options, "noacl", len) == 0) {
1904 #ifdef CONFIG_FS_POSIX_ACL
1905 mds->mds_fl_acl = 0;
1906 LCONSOLE_INFO("Disabling ACL\n");
1914 /* mount the file system (secretly). lustre_cfg parameters are:
1920 static int mds_setup(struct obd_device *obd, obd_count len, void *buf)
1922 struct lprocfs_static_vars lvars;
1923 struct lustre_cfg* lcfg = buf;
1924 struct mds_obd *mds = &obd->u.mds;
1925 struct lustre_sb_info *lsi;
1926 struct lustre_mount_info *lmi;
1927 struct vfsmount *mnt;
1928 struct obd_uuid uuid;
1935 /* setup 1:/dev/loop/0 2:ext3 3:mdsA 4:errors=remount-ro,iopen_nopriv */
1937 CLASSERT(offsetof(struct obd_device, u.obt) ==
1938 offsetof(struct obd_device, u.mds.mds_obt));
1940 if (lcfg->lcfg_bufcount < 3)
1943 if (LUSTRE_CFG_BUFLEN(lcfg, 1) == 0 || LUSTRE_CFG_BUFLEN(lcfg, 2) == 0)
1946 lmi = server_get_mount(obd->obd_name);
1948 CERROR("Not mounted in lustre_fill_super?\n");
1952 /* We mounted in lustre_fill_super.
1953 lcfg bufs 1, 2, 4 (device, fstype, mount opts) are ignored.*/
1954 lsi = s2lsi(lmi->lmi_sb);
1955 fsoptions_to_mds_flags(mds, lsi->lsi_ldd->ldd_mount_opts);
1956 fsoptions_to_mds_flags(mds, lsi->lsi_lmd->lmd_opts);
1958 obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
1959 if (IS_ERR(obd->obd_fsops))
1960 GOTO(err_put, rc = PTR_ERR(obd->obd_fsops));
1962 CDEBUG(D_SUPER, "%s: mnt = %p\n", lustre_cfg_string(lcfg, 1), mnt);
1964 if (lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb))) {
1965 CERROR("%s: Underlying device is marked as read-only. "
1966 "Setup failed\n", obd->obd_name);
1967 GOTO(err_ops, rc = -EROFS);
1970 sema_init(&mds->mds_epoch_sem, 1);
1971 spin_lock_init(&mds->mds_transno_lock);
1972 mds->mds_max_mdsize = sizeof(struct lov_mds_md_v3);
1973 mds->mds_max_cookiesize = sizeof(struct llog_cookie);
1974 mds->mds_atime_diff = MAX_ATIME_DIFF;
1975 mds->mds_evict_ost_nids = 1;
1976 /* sync permission changes */
1977 mds->mds_sync_permission = 0;
1979 sprintf(ns_name, "mds-%s", obd->obd_uuid.uuid);
1980 obd->obd_namespace = ldlm_namespace_new(obd, ns_name, LDLM_NAMESPACE_SERVER,
1981 LDLM_NAMESPACE_GREEDY);
1982 if (obd->obd_namespace == NULL) {
1984 GOTO(err_ops, rc = -ENOMEM);
1986 ldlm_register_intent(obd->obd_namespace, mds_intent_policy);
1988 lprocfs_mds_init_vars(&lvars);
1989 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
1990 lprocfs_alloc_obd_stats(obd, LPROC_MDS_LAST) == 0) {
1991 /* Init private stats here */
1992 mds_stats_counter_init(obd->obd_stats);
1993 #ifdef HAVE_DELAYED_RECOVERY
1994 lprocfs_obd_attach_stale_exports(obd);
1996 obd->obd_proc_exports_entry = proc_mkdir("exports",
1997 obd->obd_proc_entry);
2000 rc = mds_fs_setup(obd, mnt);
2002 CERROR("%s: MDS filesystem method init failed: rc = %d\n",
2007 if (obd->obd_proc_exports_entry)
2008 lprocfs_add_simple(obd->obd_proc_exports_entry,
2009 "clear", lprocfs_nid_stats_clear_read,
2010 lprocfs_nid_stats_clear_write, obd, NULL);
2012 if (lcfg->lcfg_bufcount >= 4 && LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
2015 ll_generate_random_uuid(uuid);
2016 class_uuid_unparse(uuid, &mds->mds_lov_uuid);
2018 OBD_ALLOC(mds->mds_profile, LUSTRE_CFG_BUFLEN(lcfg, 3));
2019 if (mds->mds_profile == NULL)
2020 GOTO(err_fs, rc = -ENOMEM);
2022 strncpy(mds->mds_profile, lustre_cfg_string(lcfg, 3),
2023 LUSTRE_CFG_BUFLEN(lcfg, 3));
2026 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2027 "mds_ldlm_client", &obd->obd_ldlm_client);
2028 obd->obd_replayable = 1;
2030 rc = lquota_setup(mds_quota_interface_ref, obd);
2034 mds->mds_group_hash = upcall_cache_init(obd->obd_name);
2035 if (IS_ERR(mds->mds_group_hash)) {
2036 rc = PTR_ERR(mds->mds_group_hash);
2037 mds->mds_group_hash = NULL;
2038 GOTO(err_qctxt, rc);
2041 /* Don't wait for mds_postrecov trying to clear orphans */
2042 obd->obd_async_recov = 1;
2043 rc = mds_postsetup(obd);
2044 /* Bug 11557 - allow async abort_recov start
2045 FIXME can remove most of this obd_async_recov plumbing
2046 obd->obd_async_recov = 0;
2049 GOTO(err_qctxt, rc);
2051 uuid_ptr = fsfilt_uuid(obd, obd->u.obt.obt_sb);
2052 if (uuid_ptr != NULL) {
2053 class_uuid_unparse(uuid_ptr, &uuid);
2059 label = fsfilt_get_label(obd, obd->u.obt.obt_sb);
2060 if (obd->obd_recovering) {
2061 LCONSOLE_WARN("MDT %s now serving %s (%s%s%s), but will be in "
2062 "recovery for at least %d:%.02d, or until %d "
2063 "client%s reconnect%s. \n",
2064 obd->obd_name, lustre_cfg_string(lcfg, 1),
2065 label ?: "", label ? "/" : "", str,
2066 obd->obd_recovery_timeout / 60,
2067 obd->obd_recovery_timeout % 60,
2068 obd->obd_recoverable_clients,
2069 (obd->obd_recoverable_clients == 1) ? "":"s",
2070 (obd->obd_recoverable_clients == 1) ? "s":"");
2072 LCONSOLE_INFO("MDT %s now serving %s (%s%s%s) with recovery "
2073 "%s\n", obd->obd_name, lustre_cfg_string(lcfg, 1),
2074 label ?: "", label ? "/" : "", str,
2075 obd->obd_replayable ? "enabled" : "disabled");
2078 /* Reduce the initial timeout on an MDS because it doesn't need such
2079 * a long timeout as an OST does. Adaptive timeouts will adjust this
2080 * value appropriately. */
2081 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
2082 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
2087 lquota_cleanup(mds_quota_interface_ref, obd);
2089 /* No extra cleanup needed for llog_init_commit_thread() */
2090 mds_fs_cleanup(obd);
2091 upcall_cache_cleanup(mds->mds_group_hash);
2092 mds->mds_group_hash = NULL;
2093 remove_proc_entry("clear", obd->obd_proc_exports_entry);
2095 lprocfs_free_per_client_stats(obd);
2096 lprocfs_free_obd_stats(obd);
2097 lprocfs_obd_cleanup(obd);
2098 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
2099 obd->obd_namespace = NULL;
2101 fsfilt_put_ops(obd->obd_fsops);
2103 server_put_mount(obd->obd_name, mnt);
2104 obd->u.obt.obt_sb = NULL;
2108 static int mds_lov_clean(struct obd_device *obd)
2110 struct mds_obd *mds = &obd->u.mds;
2111 struct obd_device *osc = mds->mds_osc_obd;
2114 if (mds->mds_profile) {
2115 class_del_profile(mds->mds_profile);
2116 OBD_FREE(mds->mds_profile, strlen(mds->mds_profile) + 1);
2117 mds->mds_profile = NULL;
2120 /* There better be a lov */
2124 RETURN(PTR_ERR(osc));
2126 obd_register_observer(osc, NULL);
2128 /* Give lov our same shutdown flags */
2129 osc->obd_force = obd->obd_force;
2130 osc->obd_fail = obd->obd_fail;
2132 /* Cleanup the lov */
2133 obd_disconnect(mds->mds_osc_exp);
2134 class_manual_cleanup(osc);
2135 mds->mds_osc_exp = NULL;
2140 static int mds_postsetup(struct obd_device *obd)
2142 struct mds_obd *mds = &obd->u.mds;
2143 struct llog_ctxt *ctxt;
2147 rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
2152 rc = llog_setup(obd, LLOG_LOVEA_ORIG_CTXT, obd, 0, NULL,
2157 if (mds->mds_profile) {
2158 struct lustre_profile *lprof;
2159 /* The profile defines which osc and mdc to connect to, for a
2160 client. We reuse that here to figure out the name of the
2161 lov to use (and ignore lprof->lp_mdc).
2162 The profile was set in the config log with
2163 LCFG_MOUNTOPT profilenm oscnm mdcnm */
2164 lprof = class_get_profile(mds->mds_profile);
2165 if (lprof == NULL) {
2166 CERROR("No profile found: %s\n", mds->mds_profile);
2167 GOTO(err_cleanup, rc = -ENOENT);
2169 rc = mds_lov_connect(obd, lprof->lp_osc);
2171 GOTO(err_cleanup, rc);
2178 ctxt = llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT);
2182 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
2188 int mds_postrecov(struct obd_device *obd)
2196 LASSERT(!obd->obd_recovering);
2198 /* VBR: update boot epoch after recovery */
2199 mds_update_last_epoch(obd);
2201 /* clean PENDING dir */
2202 rc = mds_cleanup_pending(obd);
2205 /* FIXME Does target_finish_recovery really need this to block? */
2206 /* Notify the LOV, which will in turn call mds_notify for each tgt */
2207 /* This means that we have to hack obd_notify to think we're obd_set_up
2208 during mds_lov_connect. */
2209 obd_notify(obd->u.mds.mds_osc_obd, NULL,
2210 obd->obd_async_recov ? OBD_NOTIFY_SYNC_NONBLOCK :
2211 OBD_NOTIFY_SYNC, NULL);
2213 /* quota recovery */
2214 if (likely(obd->obd_stopping == 0))
2215 lquota_recovery(mds_quota_interface_ref, obd);
2221 /* We need to be able to stop an mds_lov_synchronize */
2222 static int mds_lov_early_clean(struct obd_device *obd)
2224 struct mds_obd *mds = &obd->u.mds;
2225 struct obd_device *osc = mds->mds_osc_obd;
2227 if (!osc || (!obd->obd_force && !obd->obd_fail))
2230 CDEBUG(D_HA, "abort inflight\n");
2231 return (obd_precleanup(osc, OBD_CLEANUP_EARLY));
2234 static int mds_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2240 case OBD_CLEANUP_EARLY:
2242 case OBD_CLEANUP_EXPORTS:
2243 target_cleanup_recovery(obd);
2244 mds_lov_early_clean(obd);
2246 case OBD_CLEANUP_SELF_EXP:
2247 mds_lov_disconnect(obd);
2249 llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
2250 llog_cleanup(llog_get_context(obd, LLOG_LOVEA_ORIG_CTXT));
2251 rc = obd_llog_finish(obd, 0);
2253 case OBD_CLEANUP_OBD:
2259 static int mds_cleanup(struct obd_device *obd)
2261 struct mds_obd *mds = &obd->u.mds;
2262 lvfs_sbdev_type save_dev;
2265 if (obd->u.obt.obt_sb == NULL)
2267 save_dev = lvfs_sbdev(obd->u.obt.obt_sb);
2269 if (mds->mds_osc_exp)
2270 /* lov export was disconnected by mds_lov_clean;
2271 we just need to drop our ref */
2272 class_export_put(mds->mds_osc_exp);
2274 remove_proc_entry("clear", obd->obd_proc_exports_entry);
2275 lprocfs_free_per_client_stats(obd);
2276 lprocfs_free_obd_stats(obd);
2277 lprocfs_obd_cleanup(obd);
2279 lquota_cleanup(mds_quota_interface_ref, obd);
2281 mds_update_server_data(obd, 1);
2282 mds_fs_cleanup(obd);
2284 upcall_cache_cleanup(mds->mds_group_hash);
2285 mds->mds_group_hash = NULL;
2287 server_put_mount(obd->obd_name, mds->mds_vfsmnt);
2288 obd->u.obt.obt_sb = NULL;
2290 ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
2291 obd->obd_namespace = NULL;
2293 spin_lock_bh(&obd->obd_processing_task_lock);
2294 if (obd->obd_recovering) {
2295 target_cancel_recovery_timer(obd);
2296 obd->obd_recovering = 0;
2298 spin_unlock_bh(&obd->obd_processing_task_lock);
2300 fsfilt_put_ops(obd->obd_fsops);
2302 LCONSOLE_INFO("MDT %s has stopped.\n", obd->obd_name);
2307 static void fixup_handle_for_resent_req(struct ptlrpc_request *req, int offset,
2308 struct ldlm_lock *new_lock,
2309 struct ldlm_lock **old_lock,
2310 struct lustre_handle *lockh)
2312 struct obd_export *exp = req->rq_export;
2313 struct ldlm_request *dlmreq =
2314 lustre_msg_buf(req->rq_reqmsg, offset, sizeof(*dlmreq));
2315 struct lustre_handle remote_hdl = dlmreq->lock_handle[0];
2316 struct ldlm_lock *lock;
2318 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2321 lock = lustre_hash_lookup(exp->exp_lock_hash, &remote_hdl);
2323 if (lock != new_lock) {
2324 lockh->cookie = lock->l_handle.h_cookie;
2325 LDLM_DEBUG(lock, "restoring lock cookie");
2326 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "
2327 LPX64, lockh->cookie);
2329 *old_lock = LDLM_LOCK_GET(lock);
2331 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
2334 lh_put(exp->exp_lock_hash, &lock->l_exp_hash);
2337 /* If the xid matches, then we know this is a resent request,
2338 * and allow it. (It's probably an OPEN, for which we don't
2341 le64_to_cpu(exp->exp_mds_data.med_lcd->lcd_last_xid))
2345 le64_to_cpu(exp->exp_mds_data.med_lcd->lcd_last_close_xid))
2348 /* This remote handle isn't enqueued, so we never received or
2349 * processed this request. Clear MSG_RESENT, because it can
2350 * be handled like any normal request now. */
2352 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2354 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
2358 #define IS_CLIENT_DISCONNECT_ERROR(error) \
2359 (error == -ENOTCONN || error == -ENODEV)
2361 static int mds_intent_policy(struct ldlm_namespace *ns,
2362 struct ldlm_lock **lockp, void *req_cookie,
2363 ldlm_mode_t mode, int flags, void *data)
2365 struct ptlrpc_request *req = req_cookie;
2366 struct ldlm_lock *lock = *lockp;
2367 struct ldlm_intent *it;
2368 struct mds_obd *mds = &req->rq_export->exp_obd->u.mds;
2369 struct ldlm_reply *rep;
2370 struct lustre_handle lockh = { 0 };
2371 struct ldlm_lock *new_lock = NULL;
2372 int getattr_part = MDS_INODELOCK_UPDATE;
2373 int repsize[5] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
2374 [DLM_LOCKREPLY_OFF] = sizeof(struct ldlm_reply),
2375 [DLM_REPLY_REC_OFF] = sizeof(struct mds_body),
2376 [DLM_REPLY_REC_OFF+1] = mds->mds_max_mdsize };
2377 int repbufcnt = 4, rc;
2380 LASSERT(req != NULL);
2382 if (lustre_msg_bufcount(req->rq_reqmsg) <= DLM_INTENT_IT_OFF) {
2383 /* No intent was provided */
2384 rc = lustre_pack_reply(req, 2, repsize, NULL);
2390 it = lustre_swab_reqbuf(req, DLM_INTENT_IT_OFF, sizeof(*it),
2391 lustre_swab_ldlm_intent);
2393 CERROR("Intent missing\n");
2394 RETURN(req->rq_status = -EFAULT);
2397 LDLM_DEBUG(lock, "intent policy, opc: %s", ldlm_it2str(it->opc));
2399 if ((req->rq_export->exp_connect_flags & OBD_CONNECT_ACL) &&
2400 (it->opc & (IT_OPEN | IT_GETATTR | IT_LOOKUP)))
2401 /* we should never allow OBD_CONNECT_ACL if not configured */
2402 repsize[repbufcnt++] = LUSTRE_POSIX_ACL_MAX_SIZE;
2403 else if (it->opc & IT_UNLINK)
2404 repsize[repbufcnt++] = mds->mds_max_cookiesize;
2406 /* if we do recovery we isn't send reply mds state is restored */
2407 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2408 repsize[DLM_REPLY_REC_OFF+1] = 0;
2409 if (it->opc & IT_UNLINK)
2410 repsize[DLM_REPLY_REC_OFF+2] = 0;
2413 rc = lustre_pack_reply(req, repbufcnt, repsize, NULL);
2415 RETURN(req->rq_status = rc);
2417 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
2418 ldlm_reply_set_disposition(rep, DISP_IT_EXECD);
2420 /* execute policy */
2421 switch ((long)it->opc) {
2423 case IT_CREAT|IT_OPEN:
2424 mds_counter_incr(req->rq_export, LPROC_MDS_OPEN);
2425 fixup_handle_for_resent_req(req, DLM_LOCKREQ_OFF, lock, NULL,
2427 /* XXX swab here to assert that an mds_open reint
2428 * packet is following */
2429 rep->lock_policy_res2 = mds_reint(req, DLM_INTENT_REC_OFF,
2431 mds_shrink_intent_reply(req, REINT_OPEN, DLM_REPLY_REC_OFF);
2433 /* We abort the lock if the lookup was negative and
2434 * we did not make it to the OPEN portion */
2435 if (!ldlm_reply_disposition(rep, DISP_LOOKUP_EXECD))
2436 RETURN(ELDLM_LOCK_ABORTED);
2437 if (ldlm_reply_disposition(rep, DISP_LOOKUP_NEG) &&
2438 !ldlm_reply_disposition(rep, DISP_OPEN_OPEN))
2441 /* If there was an error of some sort or if we are not
2442 * returning any locks */
2443 if (rep->lock_policy_res2 ||
2444 !ldlm_reply_disposition(rep, DISP_OPEN_LOCK)) {
2445 /* If it is the disconnect error (ENODEV & ENOCONN)
2446 * ptlrpc layer should know this imediately, it should
2447 * be replied by rq_stats, otherwise, return it by
2450 /* if VBR failure then return error in rq_stats too */
2451 if (IS_CLIENT_DISCONNECT_ERROR(rep->lock_policy_res2) ||
2452 rep->lock_policy_res2 == -EOVERFLOW)
2453 RETURN(rep->lock_policy_res2);
2455 RETURN(ELDLM_LOCK_ABORTED);
2459 getattr_part = MDS_INODELOCK_LOOKUP;
2461 getattr_part |= MDS_INODELOCK_LOOKUP;
2462 OBD_COUNTER_INCREMENT(req->rq_export->exp_obd, getattr);
2464 fixup_handle_for_resent_req(req, DLM_LOCKREQ_OFF, lock,
2467 /* INODEBITS_INTEROP: if this lock was converted from a
2468 * plain lock (client does not support inodebits), then
2469 * child lock must be taken with both lookup and update
2470 * bits set for all operations.
2472 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS))
2473 getattr_part = MDS_INODELOCK_LOOKUP |
2474 MDS_INODELOCK_UPDATE;
2476 rep->lock_policy_res2 = mds_getattr_lock(req,DLM_INTENT_REC_OFF,
2477 getattr_part, &lockh);
2478 mds_shrink_body_reply(req,DLM_INTENT_REC_OFF, DLM_REPLY_REC_OFF);
2479 /* FIXME: LDLM can set req->rq_status. MDS sets
2480 policy_res{1,2} with disposition and status.
2481 - replay: returns 0 & req->status is old status
2482 - otherwise: returns req->status */
2483 if (ldlm_reply_disposition(rep, DISP_LOOKUP_NEG))
2484 rep->lock_policy_res2 = 0;
2485 if (!ldlm_reply_disposition(rep, DISP_LOOKUP_POS) ||
2486 rep->lock_policy_res2)
2487 RETURN(ELDLM_LOCK_ABORTED);
2488 if (req->rq_status != 0) {
2490 rep->lock_policy_res2 = req->rq_status;
2491 RETURN(ELDLM_LOCK_ABORTED);
2495 CERROR("Unhandled intent "LPD64"\n", it->opc);
2499 /* By this point, whatever function we called above must have either
2500 * filled in 'lockh', been an intent replay, or returned an error. We
2501 * want to allow replayed RPCs to not get a lock, since we would just
2502 * drop it below anyways because lock replay is done separately by the
2503 * client afterwards. For regular RPCs we want to give the new lock to
2504 * the client instead of whatever lock it was about to get. */
2505 if (new_lock == NULL)
2506 new_lock = ldlm_handle2lock(&lockh);
2507 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY))
2510 LASSERTF(new_lock != NULL, "op "LPX64" lockh "LPX64"\n",
2511 it->opc, lockh.cookie);
2513 /* If we've already given this lock to a client once, then we should
2514 * have no readers or writers. Otherwise, we should have one reader
2515 * _or_ writer ref (which will be zeroed below) before returning the
2516 * lock to a client. */
2517 if (new_lock->l_export == req->rq_export) {
2518 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
2520 LASSERT(new_lock->l_export == NULL);
2521 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
2526 if (new_lock->l_export == req->rq_export) {
2527 /* Already gave this to the client, which means that we
2528 * reconstructed a reply. */
2529 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
2531 RETURN(ELDLM_LOCK_REPLACED);
2534 /* Fixup the lock to be given to the client */
2535 lock_res_and_lock(new_lock);
2536 new_lock->l_readers = 0;
2537 new_lock->l_writers = 0;
2539 new_lock->l_export = class_export_get(req->rq_export);
2540 new_lock->l_blocking_ast = lock->l_blocking_ast;
2541 new_lock->l_completion_ast = lock->l_completion_ast;
2542 new_lock->l_flags &= ~LDLM_FL_LOCAL;
2544 memcpy(&new_lock->l_remote_handle, &lock->l_remote_handle,
2545 sizeof(lock->l_remote_handle));
2547 unlock_res_and_lock(new_lock);
2549 lustre_hash_add(new_lock->l_export->exp_lock_hash,
2550 &new_lock->l_remote_handle,
2551 &new_lock->l_exp_hash);
2552 LDLM_LOCK_PUT(new_lock);
2554 RETURN(ELDLM_LOCK_REPLACED);
2557 static int mdt_setup(struct obd_device *obd, obd_count len, void *buf)
2559 struct mds_obd *mds = &obd->u.mds;
2560 struct lprocfs_static_vars lvars;
2561 int mds_min_threads;
2562 int mds_max_threads;
2566 lprocfs_mdt_init_vars(&lvars);
2567 lprocfs_obd_setup(obd, lvars.obd_vars);
2569 sema_init(&mds->mds_health_sem, 1);
2571 if (mds_num_threads) {
2572 /* If mds_num_threads is set, it is the min and the max. */
2573 if (mds_num_threads > MDS_THREADS_MAX)
2574 mds_num_threads = MDS_THREADS_MAX;
2575 if (mds_num_threads < MDS_THREADS_MIN)
2576 mds_num_threads = MDS_THREADS_MIN;
2577 mds_max_threads = mds_min_threads = mds_num_threads;
2579 /* Base min threads on memory and cpus */
2580 mds_min_threads = num_possible_cpus() * CFS_NUM_CACHEPAGES >>
2581 (27 - CFS_PAGE_SHIFT);
2582 if (mds_min_threads < MDS_THREADS_MIN)
2583 mds_min_threads = MDS_THREADS_MIN;
2584 /* Largest auto threads start value */
2585 if (mds_min_threads > 32)
2586 mds_min_threads = 32;
2587 mds_max_threads = min(MDS_THREADS_MAX, mds_min_threads * 4);
2591 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2592 MDS_MAXREPSIZE, MDS_REQUEST_PORTAL,
2593 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_FACTOR,
2594 mds_handle, LUSTRE_MDS_NAME,
2595 obd->obd_proc_entry, target_print_req,
2596 mds_min_threads, mds_max_threads, "ll_mdt",
2599 if (!mds->mds_service) {
2600 CERROR("failed to start service\n");
2601 GOTO(err_lprocfs, rc = -ENOMEM);
2604 rc = ptlrpc_start_threads(obd, mds->mds_service);
2606 GOTO(err_thread, rc);
2608 mds->mds_setattr_service =
2609 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2610 MDS_MAXREPSIZE, MDS_SETATTR_PORTAL,
2611 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_FACTOR,
2612 mds_handle, "mds_setattr",
2613 obd->obd_proc_entry, target_print_req,
2614 mds_min_threads, mds_max_threads,
2615 "ll_mdt_attr", NULL);
2616 if (!mds->mds_setattr_service) {
2617 CERROR("failed to start getattr service\n");
2618 GOTO(err_thread, rc = -ENOMEM);
2621 rc = ptlrpc_start_threads(obd, mds->mds_setattr_service);
2623 GOTO(err_thread2, rc);
2625 mds->mds_readpage_service =
2626 ptlrpc_init_svc(MDS_NBUFS, MDS_BUFSIZE, MDS_MAXREQSIZE,
2627 MDS_MAXREPSIZE, MDS_READPAGE_PORTAL,
2628 MDC_REPLY_PORTAL, MDS_SERVICE_WATCHDOG_FACTOR,
2629 mds_handle, "mds_readpage",
2630 obd->obd_proc_entry, target_print_req,
2631 MDS_THREADS_MIN_READPAGE, mds_max_threads,
2632 "ll_mdt_rdpg", NULL);
2633 if (!mds->mds_readpage_service) {
2634 CERROR("failed to start readpage service\n");
2635 GOTO(err_thread2, rc = -ENOMEM);
2638 rc = ptlrpc_start_threads(obd, mds->mds_readpage_service);
2641 GOTO(err_thread3, rc);
2643 ping_evictor_start();
2648 ptlrpc_unregister_service(mds->mds_readpage_service);
2649 mds->mds_readpage_service = NULL;
2651 ptlrpc_unregister_service(mds->mds_setattr_service);
2652 mds->mds_setattr_service = NULL;
2654 ptlrpc_unregister_service(mds->mds_service);
2655 mds->mds_service = NULL;
2657 lprocfs_obd_cleanup(obd);
2661 static int mdt_cleanup(struct obd_device *obd)
2663 struct mds_obd *mds = &obd->u.mds;
2666 ping_evictor_stop();
2668 down(&mds->mds_health_sem);
2669 ptlrpc_unregister_service(mds->mds_readpage_service);
2670 ptlrpc_unregister_service(mds->mds_setattr_service);
2671 ptlrpc_unregister_service(mds->mds_service);
2672 mds->mds_readpage_service = NULL;
2673 mds->mds_setattr_service = NULL;
2674 mds->mds_service = NULL;
2675 up(&mds->mds_health_sem);
2677 lprocfs_obd_cleanup(obd);
2682 static int mdt_health_check(struct obd_device *obd)
2684 struct mds_obd *mds = &obd->u.mds;
2687 down(&mds->mds_health_sem);
2688 rc |= ptlrpc_service_health_check(mds->mds_readpage_service);
2689 rc |= ptlrpc_service_health_check(mds->mds_setattr_service);
2690 rc |= ptlrpc_service_health_check(mds->mds_service);
2691 up(&mds->mds_health_sem);
2694 * health_check to return 0 on healthy
2695 * and 1 on unhealthy.
2703 static struct dentry *mds_lvfs_fid2dentry(__u64 id, __u32 gen, __u64 gr,
2706 struct obd_device *obd = data;
2709 fid.generation = gen;
2710 return mds_fid2dentry(&obd->u.mds, &fid, NULL);
2713 static int mds_health_check(struct obd_device *obd)
2715 struct obd_device_target *odt = &obd->u.obt;
2716 #ifdef USE_HEALTH_CHECK_WRITE
2717 struct mds_obd *mds = &obd->u.mds;
2721 if (odt->obt_sb->s_flags & MS_RDONLY)
2724 #ifdef USE_HEALTH_CHECK_WRITE
2725 LASSERT(mds->mds_obt.obt_health_check_filp != NULL);
2726 rc |= !!lvfs_check_io_health(obd, mds->mds_obt.obt_health_check_filp);
2732 static int mds_process_config(struct obd_device *obd, obd_count len, void *buf)
2734 struct lustre_cfg *lcfg = buf;
2737 switch(lcfg->lcfg_command) {
2739 struct lprocfs_static_vars lvars;
2740 lprocfs_mds_init_vars(&lvars);
2742 rc = class_process_proc_param(PARAM_MDT, lvars.obd_vars, lcfg, obd);
2752 struct lvfs_callback_ops mds_lvfs_ops = {
2753 l_fid2dentry: mds_lvfs_fid2dentry,
2756 /* use obd ops to offer management infrastructure */
2757 static struct obd_ops mds_obd_ops = {
2758 .o_owner = THIS_MODULE,
2759 .o_connect = mds_connect,
2760 .o_reconnect = mds_reconnect,
2761 .o_init_export = mds_init_export,
2762 .o_destroy_export = mds_destroy_export,
2763 .o_disconnect = mds_disconnect,
2764 .o_setup = mds_setup,
2765 .o_precleanup = mds_precleanup,
2766 .o_cleanup = mds_cleanup,
2767 .o_postrecov = mds_postrecov,
2768 .o_statfs = mds_obd_statfs,
2769 .o_iocontrol = mds_iocontrol,
2770 .o_create = mds_obd_create,
2771 .o_destroy = mds_obd_destroy,
2772 .o_llog_init = mds_llog_init,
2773 .o_llog_finish = mds_llog_finish,
2774 .o_notify = mds_notify,
2775 .o_health_check = mds_health_check,
2776 .o_process_config = mds_process_config,
2779 static struct obd_ops mdt_obd_ops = {
2780 .o_owner = THIS_MODULE,
2781 .o_setup = mdt_setup,
2782 .o_cleanup = mdt_cleanup,
2783 .o_health_check = mdt_health_check,
2786 quota_interface_t *mds_quota_interface_ref;
2787 extern quota_interface_t mds_quota_interface;
2789 static int __init mds_init(void)
2792 struct lprocfs_static_vars lvars;
2794 request_module("lquota");
2795 mds_quota_interface_ref = PORTAL_SYMBOL_GET(mds_quota_interface);
2796 rc = lquota_init(mds_quota_interface_ref);
2798 if (mds_quota_interface_ref)
2799 PORTAL_SYMBOL_PUT(mds_quota_interface);
2802 init_obd_quota_ops(mds_quota_interface_ref, &mds_obd_ops);
2804 lprocfs_mds_init_vars(&lvars);
2805 class_register_type(&mds_obd_ops, lvars.module_vars, LUSTRE_MDS_NAME);
2806 lprocfs_mdt_init_vars(&lvars);
2807 class_register_type(&mdt_obd_ops, lvars.module_vars, LUSTRE_MDT_NAME);
2812 static void /*__exit*/ mds_exit(void)
2814 lquota_exit(mds_quota_interface_ref);
2815 if (mds_quota_interface_ref)
2816 PORTAL_SYMBOL_PUT(mds_quota_interface);
2818 class_unregister_type(LUSTRE_MDS_NAME);
2819 class_unregister_type(LUSTRE_MDT_NAME);
2822 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2823 MODULE_DESCRIPTION("Lustre Metadata Server (MDS)");
2824 MODULE_LICENSE("GPL");
2826 module_init(mds_init);
2827 module_exit(mds_exit);