1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 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>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/obd.h>
37 #include <linux/lustre_lib.h>
38 #include <linux/lustre_idl.h>
39 #include <linux/lustre_mds.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_fsfilt.h>
43 #include "mds_internal.h"
45 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
48 obd_transno_commit_cb(obd, transno, error);
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
60 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
61 void *cb_data, int error)
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
66 struct llog_ctxt *ctxt;
70 obd_transno_commit_cb(obd, transno, error);
72 CDEBUG(D_HA, "cancelling %d cookies\n",
73 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
75 rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
76 mlcd->mlcd_eadatalen);
78 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
79 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
83 ///* XXX 0 normally, SENDNOW for debug */);
84 ctxt = llog_get_context(obd, mlcd->mlcd_cookies[0].lgc_subsys + 1);
85 rc = llog_cancel(ctxt, lsm,
86 mlcd->mlcd_cookielen /
87 sizeof(*mlcd->mlcd_cookies),
88 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW);
90 CERROR("error cancelling %d log cookies: rc %d\n",
91 (int)(mlcd->mlcd_cookielen /
92 sizeof(*mlcd->mlcd_cookies)), rc);
96 OBD_FREE(mlcd, mlcd->mlcd_size);
99 /* Assumes caller has already pushed us into the kernel context. */
100 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
101 struct ptlrpc_request *req, int rc, __u32 op_data)
103 struct mds_export_data *med = &req->rq_export->exp_mds_data;
104 struct mds_client_data *mcd = med->med_mcd;
105 struct obd_device *obd = req->rq_export->exp_obd;
112 /* if the export has already been failed, we have no last_rcvd slot */
113 if (req->rq_export->exp_failed) {
114 CERROR("committing transaction for disconnected client\n");
123 if (handle == NULL) {
124 /* if we're starting our own xaction, use our own inode */
125 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
126 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
127 if (IS_ERR(handle)) {
128 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
129 RETURN(PTR_ERR(handle));
135 transno = req->rq_reqmsg->transno;
137 spin_lock(&mds->mds_transno_lock);
138 transno = ++mds->mds_last_transno;
139 spin_unlock(&mds->mds_transno_lock);
141 spin_lock(&mds->mds_transno_lock);
142 if (transno > mds->mds_last_transno)
143 mds->mds_last_transno = transno;
144 spin_unlock(&mds->mds_transno_lock);
146 req->rq_repmsg->transno = req->rq_transno = transno;
147 mcd->mcd_last_transno = cpu_to_le64(transno);
148 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
149 mcd->mcd_last_result = cpu_to_le32(rc);
150 mcd->mcd_last_data = cpu_to_le32(op_data);
152 fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
153 mds_commit_cb, NULL);
154 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
163 DEBUG_REQ(log_pri, req,
164 "wrote trans #"LPU64" client %s at idx %u: err = %d",
165 transno, mcd->mcd_uuid, med->med_idx, err);
167 err = mds_lov_write_objids(obd);
173 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
176 err = fsfilt_commit(obd, inode, handle, 0);
178 CERROR("error committing transaction: %d\n", err);
186 /* this gives the same functionality as the code between
187 * sys_chmod and inode_setattr
188 * chown_common and inode_setattr
189 * utimes and inode_setattr
191 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
193 time_t now = LTIME_S(CURRENT_TIME);
194 struct iattr *attr = &rec->ur_iattr;
195 unsigned int ia_valid = attr->ia_valid;
199 /* only fix up attrs if the client VFS didn't already */
200 if (!(ia_valid & ATTR_RAW))
203 if (!(ia_valid & ATTR_CTIME_SET))
204 LTIME_S(attr->ia_ctime) = now;
205 if (!(ia_valid & ATTR_ATIME_SET))
206 LTIME_S(attr->ia_atime) = now;
207 if (!(ia_valid & ATTR_MTIME_SET))
208 LTIME_S(attr->ia_mtime) = now;
210 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
214 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
215 if (rec->ur_fsuid != inode->i_uid &&
216 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
220 if (ia_valid & ATTR_SIZE) {
221 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
225 if (ia_valid & ATTR_UID) {
228 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
230 if (attr->ia_uid == (uid_t) -1)
231 attr->ia_uid = inode->i_uid;
232 if (attr->ia_gid == (gid_t) -1)
233 attr->ia_gid = inode->i_gid;
234 attr->ia_mode = inode->i_mode;
236 * If the user or group of a non-directory has been
237 * changed by a non-root user, remove the setuid bit.
238 * 19981026 David C Niemi <niemi@tux.org>
240 * Changed this to apply to all users, including root,
241 * to avoid some races. This is the behavior we had in
242 * 2.0. The check for non-root was definitely wrong
243 * for 2.2 anyway, as it should have been using
244 * CAP_FSETID rather than fsuid -- 19990830 SD.
246 if ((inode->i_mode & S_ISUID) == S_ISUID &&
247 !S_ISDIR(inode->i_mode)) {
248 attr->ia_mode &= ~S_ISUID;
249 attr->ia_valid |= ATTR_MODE;
252 * Likewise, if the user or group of a non-directory
253 * has been changed by a non-root user, remove the
254 * setgid bit UNLESS there is no group execute bit
255 * (this would be a file marked for mandatory
256 * locking). 19981026 David C Niemi <niemi@tux.org>
258 * Removed the fsuid check (see the comment above) --
261 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
262 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
263 attr->ia_mode &= ~S_ISGID;
264 attr->ia_valid |= ATTR_MODE;
266 } else if (ia_valid & ATTR_MODE) {
267 int mode = attr->ia_mode;
269 if (attr->ia_mode == (mode_t) -1)
270 attr->ia_mode = inode->i_mode;
272 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
277 void mds_steal_ack_locks(struct obd_export *exp, struct ptlrpc_request *req)
280 struct ptlrpc_request *oldrep = exp->exp_outstanding_reply;
284 memcpy(req->rq_ack_locks, oldrep->rq_ack_locks,
285 sizeof req->rq_ack_locks);
286 spin_lock_irqsave(&req->rq_lock, flags);
287 oldrep->rq_resent = 1;
288 wake_up(&oldrep->rq_reply_waitq);
289 spin_unlock_irqrestore(&req->rq_lock, flags);
290 DEBUG_REQ(D_HA, oldrep, "stole locks from");
291 DEBUG_REQ(D_HA, req, "stole locks for");
294 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
296 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
297 mcd->mcd_last_transno, mcd->mcd_last_result);
298 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
299 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
301 if (req->rq_export->exp_outstanding_reply)
302 mds_steal_ack_locks(req->rq_export, req);
305 static void reconstruct_reint_setattr(struct mds_update_record *rec,
306 int offset, struct ptlrpc_request *req)
308 struct mds_export_data *med = &req->rq_export->exp_mds_data;
309 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
311 struct mds_body *body;
313 mds_req_from_mcd(req, med->med_mcd);
315 de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
317 LASSERT(PTR_ERR(de) == req->rq_status);
321 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
322 mds_pack_inode2fid(&body->fid1, de->d_inode);
323 mds_pack_inode2body(body, de->d_inode);
325 /* Don't return OST-specific attributes if we didn't just set them */
326 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
327 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
328 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
329 body->valid |= OBD_MD_FLMTIME;
330 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
331 body->valid |= OBD_MD_FLATIME;
336 /* In the raw-setattr case, we lock the child inode.
337 * In the write-back case or if being called from open, the client holds a lock
340 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
341 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
342 struct ptlrpc_request *req,
343 struct lustre_handle *lh)
345 struct mds_obd *mds = mds_req2mds(req);
346 struct obd_device *obd = req->rq_export->exp_obd;
347 struct mds_body *body;
349 struct inode *inode = NULL;
350 struct lustre_handle lockh;
352 struct mds_logcancel_data *mlcd = NULL;
353 int rc = 0, cleanup_phase = 0, err, locked = 0;
356 LASSERT(offset == 0);
358 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
359 rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
361 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
363 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
364 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
366 GOTO(cleanup, rc = PTR_ERR(de));
368 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
371 GOTO(cleanup, rc = PTR_ERR(de));
378 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
381 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
383 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
385 GOTO(cleanup, rc = PTR_ERR(handle));
387 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
388 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
389 LTIME_S(rec->ur_iattr.ia_mtime),
390 LTIME_S(rec->ur_iattr.ia_ctime));
391 rc = mds_fix_attr(inode, rec);
395 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
396 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
397 (long)&rec->ur_iattr.ia_attr_flags);
399 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
401 if (rc == 0 && S_ISREG(inode->i_mode) && rec->ur_eadata != NULL) {
402 rc = fsfilt_set_md(obd, inode, handle,
403 rec->ur_eadata, rec->ur_eadatalen);
406 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
407 mds_pack_inode2fid(&body->fid1, inode);
408 mds_pack_inode2body(body, inode);
410 /* Don't return OST-specific attributes if we didn't just set them */
411 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
412 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
413 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
414 body->valid |= OBD_MD_FLMTIME;
415 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
416 body->valid |= OBD_MD_FLATIME;
418 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
419 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
422 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
424 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
425 mlcd->mlcd_cookielen = rec->ur_cookielen;
426 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
427 mlcd->mlcd_cookielen;
428 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
429 mlcd->mlcd_cookielen);
430 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
431 mlcd->mlcd_eadatalen);
433 CERROR("unable to allocate log cancel data\n");
439 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
440 mds_cancel_cookies_cb, mlcd);
441 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
442 switch (cleanup_phase) {
444 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
449 ldlm_lock_decref(&lockh, LCK_PW);
451 ldlm_put_lock_into_req(req, &lockh, LCK_PW);
466 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
467 struct ptlrpc_request *req)
469 struct mds_export_data *med = &req->rq_export->exp_mds_data;
470 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
471 struct dentry *parent, *child;
472 struct mds_body *body;
474 mds_req_from_mcd(req, med->med_mcd);
479 parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
480 LASSERT(!IS_ERR(parent));
481 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
482 LASSERT(!IS_ERR(child));
483 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
484 mds_pack_inode2fid(&body->fid1, child->d_inode);
485 mds_pack_inode2body(body, child->d_inode);
490 static int mds_reint_create(struct mds_update_record *rec, int offset,
491 struct ptlrpc_request *req,
492 struct lustre_handle *lh)
494 struct dentry *dparent = NULL;
495 struct mds_obd *mds = mds_req2mds(req);
496 struct obd_device *obd = req->rq_export->exp_obd;
497 struct dentry *dchild = NULL;
498 struct inode *dir = NULL;
500 struct lustre_handle lockh;
501 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
503 struct dentry_params dp;
506 LASSERT(offset == 0);
507 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
509 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
510 rec->ur_fid1->id, rec->ur_fid1->generation,
511 rec->ur_name, rec->ur_mode);
513 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
515 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
516 GOTO(cleanup, rc = -ESTALE);
518 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, &lockh,
519 rec->ur_name, rec->ur_namelen - 1);
520 if (IS_ERR(dparent)) {
521 rc = PTR_ERR(dparent);
522 CERROR("parent lookup error %d\n", rc);
525 cleanup_phase = 1; /* locked parent dentry */
526 dir = dparent->d_inode;
529 ldlm_lock_dump_handle(D_OTHER, &lockh);
531 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
532 if (IS_ERR(dchild)) {
533 rc = PTR_ERR(dchild);
534 CERROR("child lookup error %d\n", rc);
538 cleanup_phase = 2; /* child dentry */
540 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
542 if (dir->i_mode & S_ISGID) {
543 if (S_ISDIR(rec->ur_mode))
544 rec->ur_mode |= S_ISGID;
547 dchild->d_fsdata = (void *)&dp;
548 dp.p_inum = (unsigned long)rec->ur_fid2->id;
553 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
555 GOTO(cleanup, rc = PTR_ERR(handle));
556 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
561 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
563 GOTO(cleanup, rc = PTR_ERR(handle));
564 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
569 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
571 GOTO(cleanup, rc = PTR_ERR(handle));
572 if (rec->ur_tgt == NULL) /* no target supplied */
573 rc = -EINVAL; /* -EPROTO? */
575 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
583 int rdev = rec->ur_rdev;
584 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
586 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
587 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
592 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
593 dchild->d_fsdata = NULL;
594 GOTO(cleanup, rc = -EINVAL);
597 /* In case we stored the desired inum in here, we want to clean up. */
598 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
599 dchild->d_fsdata = NULL;
602 CDEBUG(D_INODE, "error during create: %d\n", rc);
606 struct inode *inode = dchild->d_inode;
607 struct mds_body *body;
610 LTIME_S(iattr.ia_atime) = rec->ur_time;
611 LTIME_S(iattr.ia_ctime) = rec->ur_time;
612 LTIME_S(iattr.ia_mtime) = rec->ur_time;
613 iattr.ia_uid = rec->ur_fsuid;
614 if (dir->i_mode & S_ISGID)
615 iattr.ia_gid = dir->i_gid;
617 iattr.ia_gid = rec->ur_fsgid;
618 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
619 ATTR_MTIME | ATTR_CTIME;
621 if (rec->ur_fid2->id) {
622 LASSERT(rec->ur_fid2->id == inode->i_ino);
623 inode->i_generation = rec->ur_fid2->generation;
624 /* Dirtied and committed by the upcoming setattr. */
625 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
626 inode->i_ino, inode->i_generation);
628 struct lustre_handle child_ino_lockh;
629 struct ldlm_res_id child_res_id =
630 { .name = { inode->i_ino, 0 } };
633 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
634 inode->i_ino, inode->i_generation);
636 /* The inode we were allocated may have just been freed
637 * by an unlink operation. We take this lock to
638 * synchronize against the matching reply-ack-lock taken
639 * in unlink, to avoid replay problems if this reply
640 * makes it out to the client but the unlink's does not.
641 * See bug 2029 for more detail.*/
642 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
643 NULL, child_res_id, LDLM_PLAIN,
644 NULL, 0, LCK_EX, &lock_flags,
646 mds_blocking_ast, NULL,
648 if (rc != ELDLM_OK) {
649 CERROR("error locking for unlink/create sync: "
652 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
656 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
658 CERROR("error on child setattr: rc = %d\n", rc);
660 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
661 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
663 CERROR("error on parent setattr: rc = %d\n", rc);
665 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
666 mds_pack_inode2fid(&body->fid1, inode);
667 mds_pack_inode2body(body, inode);
672 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
675 /* Destroy the file we just created. This should not need
676 * extra journal credits, as we have already modified all of
677 * the blocks needed in order to create the file in the first
682 err = vfs_rmdir(dir, dchild);
684 CERROR("rmdir in error path: %d\n", err);
687 err = vfs_unlink(dir, dchild);
689 CERROR("unlink in error path: %d\n", err);
695 switch (cleanup_phase) {
696 case 2: /* child dentry */
698 case 1: /* locked parent dentry */
700 ldlm_lock_decref(&lockh, LCK_PW);
702 ldlm_put_lock_into_req(req, &lockh, LCK_PW);
708 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
715 int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2)
719 for (i = 0; i < RES_NAME_SIZE; i++) {
720 /* return 1 here, because enqueue_ordered will skip resources
721 * of all zeroes if they're sorted to the end of the list. */
722 if (res1->name[i] == 0 && res2->name[i] != 0)
724 if (res2->name[i] == 0 && res1->name[i] != 0)
727 if (res1->name[i] > res2->name[i])
729 if (res1->name[i] < res2->name[i])
735 /* This function doesn't use ldlm_match_or_enqueue because we're always called
736 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
737 * because they take the place of local semaphores.
739 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
740 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
741 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
742 struct lustre_handle *p1_lockh, int p1_lock_mode,
743 struct ldlm_res_id *p2_res_id,
744 struct lustre_handle *p2_lockh, int p2_lock_mode)
746 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
747 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
748 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
752 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
754 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
755 res_id[0]->name[0], res_id[1]->name[0]);
757 if (res_gt(p1_res_id, p2_res_id)) {
758 handles[1] = p1_lockh;
759 handles[0] = p2_lockh;
760 res_id[1] = p1_res_id;
761 res_id[0] = p2_res_id;
762 lock_modes[1] = p1_lock_mode;
763 lock_modes[0] = p2_lock_mode;
766 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
767 res_id[0]->name[0], res_id[1]->name[0]);
769 flags = LDLM_FL_LOCAL_ONLY;
770 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL, *res_id[0],
771 LDLM_PLAIN, NULL, 0, lock_modes[0], &flags,
772 ldlm_completion_ast, mds_blocking_ast, NULL,
776 ldlm_lock_dump_handle(D_OTHER, handles[0]);
778 if (memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) == 0) {
779 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
780 ldlm_lock_addref(handles[1], lock_modes[1]);
781 } else if (res_id[1]->name[0] != 0) {
782 flags = LDLM_FL_LOCAL_ONLY;
783 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
784 *res_id[1], LDLM_PLAIN, NULL, 0,
785 lock_modes[1], &flags,ldlm_completion_ast,
786 mds_blocking_ast, NULL, handles[1]);
787 if (rc != ELDLM_OK) {
788 ldlm_lock_decref(handles[0], lock_modes[0]);
791 ldlm_lock_dump_handle(D_OTHER, handles[1]);
797 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
798 struct lustre_handle *p1_lockh, int p1_lock_mode,
799 struct ldlm_res_id *p2_res_id,
800 struct lustre_handle *p2_lockh, int p2_lock_mode,
801 struct ldlm_res_id *c1_res_id,
802 struct lustre_handle *c1_lockh, int c1_lock_mode,
803 struct ldlm_res_id *c2_res_id,
804 struct lustre_handle *c2_lockh, int c2_lock_mode)
806 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
807 c1_res_id, c2_res_id };
808 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
809 c1_lockh, c2_lockh };
810 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
811 c1_lock_mode, c2_lock_mode };
812 int rc, i, j, sorted, flags;
815 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
816 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
819 /* simple insertion sort - we have at most 4 elements */
820 for (i = 1; i < 4; i++) {
822 dlm_handles[4] = dlm_handles[i];
823 res_id[4] = res_id[i];
824 lock_modes[4] = lock_modes[i];
828 if (res_gt(res_id[j], res_id[4])) {
829 dlm_handles[j + 1] = dlm_handles[j];
830 res_id[j + 1] = res_id[j];
831 lock_modes[j + 1] = lock_modes[j];
836 } while (j >= 0 && !sorted);
838 dlm_handles[j + 1] = dlm_handles[4];
839 res_id[j + 1] = res_id[4];
840 lock_modes[j + 1] = lock_modes[4];
843 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
844 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
847 /* XXX we could send ASTs on all these locks first before blocking? */
848 for (i = 0; i < 4; i++) {
850 if (res_id[i]->name[0] == 0)
853 memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) == 0) {
854 memcpy(dlm_handles[i], dlm_handles[i-1],
855 sizeof(*(dlm_handles[i])));
856 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
858 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
859 NULL, *res_id[i], LDLM_PLAIN,
860 NULL, 0, lock_modes[i], &flags,
862 mds_blocking_ast, NULL,
865 GOTO(out_err, rc = -EIO);
866 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
873 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
878 /* In the unlikely case that the child changed while we were waiting
879 * on the lock, we need to drop the lock on the old child and either:
880 * - if the child has a lower resource name, then we have to also
881 * drop the parent lock and regain the locks in the right order
882 * - in the rename case, if the child has a lower resource name than one of
883 * the other parent/child resources (maxres) we also need to reget the locks
884 * - if the child has a higher resource name (this is the common case)
885 * we can just get the lock on the new child (still in lock order)
887 * Returns 0 if the child did not change or if it changed but could be locked.
888 * Returns 1 if the child changed and we need to re-lock (no locks held).
889 * Returns -ve error with a valid dchild (no locks held). */
890 static int mds_verify_child(struct obd_device *obd,
891 struct ldlm_res_id *parent_res_id,
892 struct lustre_handle *parent_lockh,
893 struct dentry *dparent, int parent_mode,
894 struct ldlm_res_id *child_res_id,
895 struct lustre_handle *child_lockh,
896 struct dentry **dchildp, int child_mode,
897 const char *name, int namelen,
898 struct ldlm_res_id *maxres)
900 struct dentry *vchild, *dchild = *dchildp;
901 int rc = 0, cleanup_phase = 2; /* parent, child locks */
904 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
906 GOTO(cleanup, rc = PTR_ERR(vchild));
908 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
909 (vchild->d_inode != NULL &&
910 child_res_id->name[0] == vchild->d_inode->i_ino &&
911 child_res_id->name[1] == vchild->d_inode->i_generation))) {
919 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
920 vchild->d_inode, dchild ? dchild->d_inode : 0,
921 vchild->d_inode ? vchild->d_inode->i_ino : 0,
922 child_res_id->name[0]);
923 if (child_res_id->name[0] != 0)
924 ldlm_lock_decref(child_lockh, child_mode);
928 cleanup_phase = 1; /* parent lock only */
929 *dchildp = dchild = vchild;
931 if (dchild->d_inode) {
933 child_res_id->name[0] = dchild->d_inode->i_ino;
934 child_res_id->name[1] = dchild->d_inode->i_generation;
936 if (res_gt(parent_res_id, child_res_id) ||
937 res_gt(maxres, child_res_id)) {
938 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
939 child_res_id->name[0], parent_res_id->name[0],
941 GOTO(cleanup, rc = 1);
944 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
945 NULL, *child_res_id, LDLM_PLAIN,
946 NULL, 0, child_mode, &flags,
947 ldlm_completion_ast, mds_blocking_ast,
950 GOTO(cleanup, rc = -EIO);
952 memset(child_res_id, 0, sizeof(*child_res_id));
958 switch(cleanup_phase) {
960 if (child_res_id->name[0] != 0)
961 ldlm_lock_decref(child_lockh, child_mode);
963 ldlm_lock_decref(parent_lockh, parent_mode);
969 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
971 struct lustre_handle *parent_lockh,
972 struct dentry **dparentp, int parent_mode,
973 char *name, int namelen,
974 struct lustre_handle *child_lockh,
975 struct dentry **dchildp, int child_mode)
977 struct ldlm_res_id child_res_id = { .name = {0} };
978 struct ldlm_res_id parent_res_id = { .name = {0} };
980 int rc = 0, cleanup_phase = 0;
983 /* Step 1: Lookup parent */
984 *dparentp = mds_fid2dentry(mds, fid, NULL);
985 if (IS_ERR(*dparentp))
986 RETURN(rc = PTR_ERR(*dparentp));
987 LASSERT((*dparentp)->d_inode);
989 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
990 (*dparentp)->d_inode->i_ino, name);
992 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
993 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
995 cleanup_phase = 1; /* parent dentry */
997 /* Step 2: Lookup child (without lock, to get resource name) */
998 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
999 if (IS_ERR(*dchildp)) {
1000 rc = PTR_ERR(*dchildp);
1001 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1005 inode = (*dchildp)->d_inode;
1007 inode = igrab(inode);
1011 child_res_id.name[0] = inode->i_ino;
1012 child_res_id.name[1] = inode->i_generation;
1016 cleanup_phase = 2; /* child dentry */
1018 /* Step 3: Lock parent and child in resource order. If child doesn't
1019 * exist, we still have to lock the parent and re-lookup. */
1020 rc = enqueue_ordered_locks(obd, &parent_res_id,parent_lockh,parent_mode,
1021 &child_res_id, child_lockh, child_mode);
1025 if (!(*dchildp)->d_inode)
1026 cleanup_phase = 3; /* parent lock */
1028 cleanup_phase = 4; /* child lock */
1030 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1031 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1032 parent_mode, &child_res_id, child_lockh, dchildp,
1033 child_mode, name, namelen, &parent_res_id);
1043 switch (cleanup_phase) {
1045 ldlm_lock_decref(child_lockh, child_mode);
1047 ldlm_lock_decref(parent_lockh, parent_mode);
1058 void mds_reconstruct_generic(struct ptlrpc_request *req)
1060 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1062 mds_req_from_mcd(req, med->med_mcd);
1065 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1066 struct ptlrpc_request *req,
1067 struct lustre_handle *lh)
1069 struct dentry *dparent, *dchild;
1070 struct mds_obd *mds = mds_req2mds(req);
1071 struct obd_device *obd = req->rq_export->exp_obd;
1072 struct mds_body *body = NULL;
1073 struct inode *child_inode;
1074 struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1075 void *handle = NULL;
1076 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1079 LASSERT(offset == 0 || offset == 2);
1081 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1082 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1084 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1086 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1087 GOTO(cleanup, rc = -ENOENT);
1089 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1090 &parent_lockh, &dparent, LCK_PW,
1091 rec->ur_name, rec->ur_namelen,
1092 &child_lockh, &dchild, LCK_EX);
1096 cleanup_phase = 1; /* dchild, dparent, locks */
1098 child_inode = dchild->d_inode;
1099 if (child_inode == NULL) {
1100 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1101 dparent->d_inode->i_ino, rec->ur_name);
1102 GOTO(cleanup, rc = -ENOENT);
1105 cleanup_phase = 2; /* dchild has a lock */
1107 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1108 * reuse (see bug 2029). */
1109 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1113 cleanup_phase = 3; /* child inum lock */
1115 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1117 /* ldlm_reply in buf[0] if called via intent */
1121 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1122 LASSERT(body != NULL);
1124 /* If this is the last reference to this inode, get the OBD EA
1125 * data first so the client can destroy OST objects.
1126 * we only do the object removal if no open files remain.
1127 * Nobody can get at this name anymore because of the locks so
1128 * we make decisions here as to whether to remove the inode */
1129 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1130 mds_open_orphan_count(child_inode) == 0) {
1131 mds_pack_inode2fid(&body->fid1, child_inode);
1132 mds_pack_inode2body(body, child_inode);
1133 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1135 if (!(body->valid & OBD_MD_FLEASIZE)) {
1136 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1137 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1143 /* We have to do these checks ourselves, in case we are making an
1144 * orphan. The client tells us whether rmdir() or unlink() was called,
1145 * so we need to return appropriate errors (bug 72).
1147 * We don't have to check permissions, because vfs_rename (called from
1148 * mds_open_unlink_rename) also calls may_delete. */
1149 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1150 if (!S_ISDIR(child_inode->i_mode))
1151 GOTO(cleanup, rc = -ENOTDIR);
1153 if (S_ISDIR(child_inode->i_mode))
1154 GOTO(cleanup, rc = -EISDIR);
1157 if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1158 mds_open_orphan_count(child_inode) > 0) {
1159 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1160 cleanup_phase = 4; /* transaction */
1164 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1165 switch (child_inode->i_mode & S_IFMT) {
1167 /* Drop any lingering child directories before we start our
1168 * transaction, to avoid doing multiple inode dirty/delete
1169 * in our compound transaction (bug 1321). */
1170 shrink_dcache_parent(dchild);
1171 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1174 GOTO(cleanup, rc = PTR_ERR(handle));
1175 cleanup_phase = 4; /* transaction */
1176 rc = vfs_rmdir(dparent->d_inode, dchild);
1179 handle = fsfilt_start(obd, dparent->d_inode,
1180 FSFILT_OP_UNLINK_LOG, NULL);
1182 GOTO(cleanup, rc = PTR_ERR(handle));
1184 cleanup_phase = 4; /* transaction */
1185 rc = vfs_unlink(dparent->d_inode, dchild);
1187 #ifdef ENABLE_ORPHANS
1189 if (!rc && log_unlink)
1190 if (mds_log_op_unlink(obd, child_inode, req->rq_repmsg,
1192 body->valid |= OBD_MD_FLCOOKIE;
1201 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1204 GOTO(cleanup, rc = PTR_ERR(handle));
1205 cleanup_phase = 4; /* transaction */
1206 rc = vfs_unlink(dparent->d_inode, dchild);
1209 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1212 GOTO(cleanup, rc = -EINVAL);
1220 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1221 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1222 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1224 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1226 CERROR("error on parent setattr: rc = %d\n", err);
1229 switch(cleanup_phase) {
1231 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1234 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1235 "unlinked", 0, NULL);
1236 case 3: /* child ino-reuse lock */
1237 if (rc && body != NULL) {
1238 // Don't unlink the OST objects if the MDS unlink failed
1242 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1244 ldlm_put_lock_into_req(req, &child_reuse_lockh, LCK_EX);
1245 case 2: /* child lock */
1246 ldlm_lock_decref(&child_lockh, LCK_EX);
1247 case 1: /* child and parent dentry, parent lock */
1249 ldlm_lock_decref(&parent_lockh, LCK_PW);
1251 ldlm_put_lock_into_req(req, &parent_lockh, LCK_PW);
1257 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1260 req->rq_status = rc;
1264 static int mds_reint_link(struct mds_update_record *rec, int offset,
1265 struct ptlrpc_request *req,
1266 struct lustre_handle *lh)
1268 struct obd_device *obd = req->rq_export->exp_obd;
1269 struct dentry *de_src = NULL;
1270 struct dentry *de_tgt_dir = NULL;
1271 struct dentry *dchild = NULL;
1272 struct mds_obd *mds = mds_req2mds(req);
1273 struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1274 struct ldlm_res_id src_res_id = { .name = {0} };
1275 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1276 int rc = 0, cleanup_phase = 0;
1279 LASSERT(offset == 0);
1281 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1282 rec->ur_fid1->id, rec->ur_fid1->generation,
1283 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1285 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1287 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1288 GOTO(cleanup, rc = -ENOENT);
1290 /* Step 1: Lookup the source inode and target directory by FID */
1291 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1293 GOTO(cleanup, rc = PTR_ERR(de_src));
1295 cleanup_phase = 1; /* source dentry */
1297 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1298 if (IS_ERR(de_tgt_dir))
1299 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1301 cleanup_phase = 2; /* target directory dentry */
1303 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1304 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1305 de_src->d_inode->i_ino);
1307 /* Step 2: Take the two locks */
1308 src_res_id.name[0] = de_src->d_inode->i_ino;
1309 src_res_id.name[1] = de_src->d_inode->i_generation;
1310 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1311 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1313 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1314 &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX);
1318 cleanup_phase = 3; /* locks */
1320 /* Step 3: Lookup the child */
1321 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1322 if (IS_ERR(dchild)) {
1323 rc = PTR_ERR(dchild);
1324 if (rc != -EPERM && rc != -EACCES)
1325 CERROR("child lookup error %d\n", rc);
1329 cleanup_phase = 4; /* child dentry */
1331 if (dchild->d_inode) {
1332 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1333 de_tgt_dir->d_inode->i_ino, rec->ur_name);
1338 /* Step 4: Do it. */
1339 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1341 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1342 if (IS_ERR(handle)) {
1343 rc = PTR_ERR(handle);
1347 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1348 if (rc && rc != -EPERM && rc != -EACCES)
1349 CERROR("vfs_link error %d\n", rc);
1351 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1352 handle, req, rc, 0);
1355 switch (cleanup_phase) {
1356 case 4: /* child dentry */
1360 ldlm_lock_decref(&src_lockh, LCK_EX);
1361 ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1363 ldlm_put_lock_into_req(req, &src_lockh, LCK_EX);
1364 ldlm_put_lock_into_req(req, &tgt_dir_lockh, LCK_EX);
1366 case 2: /* target dentry */
1368 case 1: /* source dentry */
1373 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1376 req->rq_status = rc;
1381 * add a hard link in the PENDING directory, only used by rename()
1383 static int mds_add_link_orphan(struct mds_update_record *rec,
1384 struct obd_device *obd,
1385 struct dentry *dentry)
1387 struct mds_obd *mds = &obd->u.mds;
1388 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1389 struct dentry *pending_child;
1390 char fidname[LL_FID_NAMELEN];
1394 LASSERT(dentry->d_inode);
1395 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
1397 down(&pending_dir->i_sem);
1398 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
1399 dentry->d_inode->i_generation);
1401 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
1402 mds_open_orphan_count(dentry->d_inode),
1403 rec->ur_name, fidname);
1405 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1406 if (IS_ERR(pending_child))
1407 GOTO(out_lock, rc = PTR_ERR(pending_child));
1409 if (pending_child->d_inode != NULL) {
1410 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1411 LASSERT(pending_child->d_inode == dentry->d_inode);
1412 GOTO(out_dput, rc = 0);
1416 rc = vfs_link(dentry, pending_dir, pending_child);
1419 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
1422 mds_inode_set_orphan(dentry->d_inode);
1424 l_dput(pending_child);
1426 up(&pending_dir->i_sem);
1430 /* The idea here is that we need to get four locks in the end:
1431 * one on each parent directory, one on each child. We need to take
1432 * these locks in some kind of order (to avoid deadlocks), and the order
1433 * I selected is "increasing resource number" order. We need to look up
1434 * the children, however, before we know what the resource number(s) are.
1435 * Thus the following plan:
1437 * 1,2. Look up the parents
1438 * 3,4. Look up the children
1439 * 5. Take locks on the parents and children, in order
1440 * 6. Verify that the children haven't changed since they were looked up
1442 * If there was a race and the children changed since they were first looked
1443 * up, it is possible that mds_verify_child() will be able to just grab the
1444 * lock on the new child resource (if it has a higher resource than any other)
1445 * but we need to compare against not only its parent, but also against the
1446 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1448 * We need the fancy igrab() on the child inodes because we aren't holding a
1449 * lock on the parent after the lookup is done, so dentry->d_inode may change
1450 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1452 static int mds_get_parents_children_locked(struct obd_device *obd,
1453 struct mds_obd *mds,
1454 struct ll_fid *p1_fid,
1455 struct dentry **de_srcdirp,
1456 struct ll_fid *p2_fid,
1457 struct dentry **de_tgtdirp,
1459 const char *old_name, int old_len,
1460 struct dentry **de_oldp,
1461 const char *new_name, int new_len,
1462 struct dentry **de_newp,
1463 struct lustre_handle *dlm_handles,
1466 struct ldlm_res_id p1_res_id = { .name = {0} };
1467 struct ldlm_res_id p2_res_id = { .name = {0} };
1468 struct ldlm_res_id c1_res_id = { .name = {0} };
1469 struct ldlm_res_id c2_res_id = { .name = {0} };
1470 struct ldlm_res_id *maxres_src, *maxres_tgt;
1471 struct inode *inode;
1472 int rc = 0, cleanup_phase = 0;
1475 /* Step 1: Lookup the source directory */
1476 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1477 if (IS_ERR(*de_srcdirp))
1478 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1480 cleanup_phase = 1; /* source directory dentry */
1482 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1483 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1485 /* Step 2: Lookup the target directory */
1486 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1487 *de_tgtdirp = dget(*de_srcdirp);
1489 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
1490 if (IS_ERR(*de_tgtdirp))
1491 GOTO(cleanup, rc = PTR_ERR(*de_tgtdirp));
1494 cleanup_phase = 2; /* target directory dentry */
1496 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
1497 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
1499 /* Step 3: Lookup the source child entry */
1500 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
1501 if (IS_ERR(*de_oldp)) {
1502 rc = PTR_ERR(*de_oldp);
1503 CERROR("old child lookup error (%*s): %d\n",
1504 old_len - 1, old_name, rc);
1508 cleanup_phase = 3; /* original name dentry */
1510 inode = (*de_oldp)->d_inode;
1512 inode = igrab(inode);
1514 GOTO(cleanup, rc = -ENOENT);
1516 c1_res_id.name[0] = inode->i_ino;
1517 c1_res_id.name[1] = inode->i_generation;
1520 /* Step 4: Lookup the target child entry */
1521 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
1522 if (IS_ERR(*de_newp)) {
1523 rc = PTR_ERR(*de_newp);
1524 CERROR("new child lookup error (%*s): %d\n",
1525 old_len - 1, old_name, rc);
1529 cleanup_phase = 4; /* target dentry */
1531 inode = (*de_newp)->d_inode;
1533 inode = igrab(inode);
1537 c2_res_id.name[0] = inode->i_ino;
1538 c2_res_id.name[1] = inode->i_generation;
1542 /* Step 5: Take locks on the parents and child(ren) */
1543 maxres_src = &p1_res_id;
1544 maxres_tgt = &p2_res_id;
1545 cleanup_phase = 4; /* target dentry */
1547 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id))
1548 maxres_src = &c1_res_id;
1549 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id))
1550 maxres_tgt = &c2_res_id;
1552 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
1553 &p2_res_id, &dlm_handles[1], parent_mode,
1554 &c1_res_id, &dlm_handles[2], child_mode,
1555 &c2_res_id, &dlm_handles[3], child_mode);
1559 cleanup_phase = 6; /* parent and child(ren) locks */
1561 /* Step 6a: Re-lookup source child to verify it hasn't changed */
1562 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
1563 parent_mode, &c1_res_id, &dlm_handles[2], de_oldp,
1564 child_mode, old_name, old_len, maxres_tgt);
1566 if (c2_res_id.name[0] != 0)
1567 ldlm_lock_decref(&dlm_handles[3], child_mode);
1568 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1575 if ((*de_oldp)->d_inode == NULL)
1576 GOTO(cleanup, rc = -ENOENT);
1578 /* Step 6b: Re-lookup target child to verify it hasn't changed */
1579 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
1580 parent_mode, &c2_res_id, &dlm_handles[3], de_newp,
1581 child_mode, new_name, new_len, maxres_src);
1583 ldlm_lock_decref(&dlm_handles[2], child_mode);
1584 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1594 switch (cleanup_phase) {
1595 case 6: /* child lock(s) */
1596 if (c2_res_id.name[0] != 0)
1597 ldlm_lock_decref(&dlm_handles[3], child_mode);
1598 if (c1_res_id.name[0] != 0)
1599 ldlm_lock_decref(&dlm_handles[2], child_mode);
1600 case 5: /* parent locks */
1601 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1602 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1603 case 4: /* target dentry */
1605 case 3: /* source dentry */
1607 case 2: /* target directory dentry */
1608 l_dput(*de_tgtdirp);
1609 case 1: /* source directry dentry */
1610 l_dput(*de_srcdirp);
1617 static int mds_reint_rename(struct mds_update_record *rec, int offset,
1618 struct ptlrpc_request *req,
1619 struct lustre_handle *lockh)
1621 struct obd_device *obd = req->rq_export->exp_obd;
1622 struct dentry *de_srcdir = NULL;
1623 struct dentry *de_tgtdir = NULL;
1624 struct dentry *de_old = NULL;
1625 struct dentry *de_new = NULL;
1626 struct mds_obd *mds = mds_req2mds(req);
1627 struct lustre_handle dlm_handles[4];
1628 struct mds_body *body = NULL;
1629 int rc = 0, lock_count = 3;
1630 int cleanup_phase = 0;
1631 void *handle = NULL;
1634 LASSERT(offset == 0);
1636 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
1637 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
1638 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
1640 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1642 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
1643 rec->ur_fid2, &de_tgtdir, LCK_PW,
1644 rec->ur_name, rec->ur_namelen,
1645 &de_old, rec->ur_tgt,
1646 rec->ur_tgtlen, &de_new,
1647 dlm_handles, LCK_EX);
1651 cleanup_phase = 1; /* parent(s), children, locks */
1653 if (de_new->d_inode)
1656 /* sanity check for src inode */
1657 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1658 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
1659 GOTO(cleanup, rc = -EINVAL);
1661 /* sanity check for dest inode */
1662 if (de_new->d_inode &&
1663 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1664 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
1665 GOTO(cleanup, rc = -EINVAL);
1667 /* if we are about to remove the target at first, pass the EA of
1668 * that inode to client to perform and cleanup on OST */
1669 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1670 LASSERT(body != NULL);
1672 if (de_new->d_inode &&
1673 S_ISREG(de_new->d_inode->i_mode) &&
1674 de_new->d_inode->i_nlink == 1 &&
1675 mds_open_orphan_count(de_new->d_inode) == 0) {
1676 mds_pack_inode2fid(&body->fid1, de_new->d_inode);
1677 mds_pack_inode2body(body, de_new->d_inode);
1678 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
1679 if (!(body->valid & OBD_MD_FLEASIZE)) {
1680 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1681 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1683 /* XXX need log unlink? */
1687 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
1688 de_srcdir->d_inode->i_sb);
1690 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
1692 GOTO(cleanup, rc = PTR_ERR(handle));
1694 /* FIXME need adjust the journal block count? */
1695 /* if the target should be moved to PENDING, we at first increase the
1696 * link and later vfs_rename() will decrease the link count again */
1697 if (de_new->d_inode &&
1698 S_ISREG(de_new->d_inode->i_mode) &&
1699 de_new->d_inode->i_nlink == 1 &&
1700 mds_open_orphan_count(de_new->d_inode) > 0) {
1701 rc = mds_add_link_orphan(rec, obd, de_new);
1707 de_old->d_fsdata = req;
1708 de_new->d_fsdata = req;
1709 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
1714 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
1715 handle, req, rc, 0);
1716 switch (cleanup_phase) {
1719 if (lock_count == 4)
1720 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
1721 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
1722 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
1723 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
1725 if (lock_count == 4)
1726 ldlm_put_lock_into_req(req,
1727 &(dlm_handles[3]), LCK_EX);
1728 ldlm_put_lock_into_req(req, &(dlm_handles[2]), LCK_EX);
1729 ldlm_put_lock_into_req(req, &(dlm_handles[1]), LCK_PW);
1730 ldlm_put_lock_into_req(req, &(dlm_handles[0]), LCK_PW);
1739 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1742 req->rq_status = rc;
1746 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
1747 struct ptlrpc_request *, struct lustre_handle *);
1749 static mds_reinter reinters[REINT_MAX + 1] = {
1750 [REINT_SETATTR] mds_reint_setattr,
1751 [REINT_CREATE] mds_reint_create,
1752 [REINT_UNLINK] mds_reint_unlink,
1753 [REINT_LINK] mds_reint_link,
1754 [REINT_RENAME] mds_reint_rename,
1755 [REINT_OPEN] mds_open
1758 int mds_reint_rec(struct mds_update_record *rec, int offset,
1759 struct ptlrpc_request *req, struct lustre_handle *lockh)
1761 struct obd_device *obd = req->rq_export->exp_obd;
1762 struct obd_run_ctxt saved;
1766 /* checked by unpacker */
1767 LASSERT(rec->ur_opcode <= REINT_MAX &&
1768 reinters[rec->ur_opcode] != NULL);
1770 push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1771 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
1772 pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);