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_mount_count = cpu_to_le64(mds->mds_mount_count);
149 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
150 mcd->mcd_last_result = cpu_to_le32(rc);
151 mcd->mcd_last_data = cpu_to_le32(op_data);
153 fsfilt_add_journal_cb(req->rq_export->exp_obd, transno, handle,
154 mds_commit_cb, NULL);
155 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd, sizeof(*mcd),
164 DEBUG_REQ(log_pri, req,
165 "wrote trans #"LPU64" client %s at idx %u: err = %d\n",
166 transno, mcd->mcd_uuid, med->med_idx, err);
168 err = mds_lov_write_objids(obd);
174 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
177 err = fsfilt_commit(obd, inode, handle, 0);
179 CERROR("error committing transaction: %d\n", err);
187 /* this gives the same functionality as the code between
188 * sys_chmod and inode_setattr
189 * chown_common and inode_setattr
190 * utimes and inode_setattr
192 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
194 time_t now = LTIME_S(CURRENT_TIME);
195 struct iattr *attr = &rec->ur_iattr;
196 unsigned int ia_valid = attr->ia_valid;
200 /* only fix up attrs if the client VFS didn't already */
201 if (!(ia_valid & ATTR_RAW))
204 if (!(ia_valid & ATTR_CTIME_SET))
205 LTIME_S(attr->ia_ctime) = now;
206 if (!(ia_valid & ATTR_ATIME_SET))
207 LTIME_S(attr->ia_atime) = now;
208 if (!(ia_valid & ATTR_MTIME_SET))
209 LTIME_S(attr->ia_mtime) = now;
211 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
215 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
216 if (rec->ur_fsuid != inode->i_uid &&
217 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
221 if (ia_valid & ATTR_SIZE) {
222 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
226 if (ia_valid & ATTR_UID) {
229 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
231 if (attr->ia_uid == (uid_t) -1)
232 attr->ia_uid = inode->i_uid;
233 if (attr->ia_gid == (gid_t) -1)
234 attr->ia_gid = inode->i_gid;
235 attr->ia_mode = inode->i_mode;
237 * If the user or group of a non-directory has been
238 * changed by a non-root user, remove the setuid bit.
239 * 19981026 David C Niemi <niemi@tux.org>
241 * Changed this to apply to all users, including root,
242 * to avoid some races. This is the behavior we had in
243 * 2.0. The check for non-root was definitely wrong
244 * for 2.2 anyway, as it should have been using
245 * CAP_FSETID rather than fsuid -- 19990830 SD.
247 if ((inode->i_mode & S_ISUID) == S_ISUID &&
248 !S_ISDIR(inode->i_mode)) {
249 attr->ia_mode &= ~S_ISUID;
250 attr->ia_valid |= ATTR_MODE;
253 * Likewise, if the user or group of a non-directory
254 * has been changed by a non-root user, remove the
255 * setgid bit UNLESS there is no group execute bit
256 * (this would be a file marked for mandatory
257 * locking). 19981026 David C Niemi <niemi@tux.org>
259 * Removed the fsuid check (see the comment above) --
262 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
263 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
264 attr->ia_mode &= ~S_ISGID;
265 attr->ia_valid |= ATTR_MODE;
267 } else if (ia_valid & ATTR_MODE) {
268 int mode = attr->ia_mode;
270 if (attr->ia_mode == (mode_t) -1)
271 attr->ia_mode = inode->i_mode;
273 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
278 void mds_steal_ack_locks(struct obd_export *exp, struct ptlrpc_request *req)
281 struct ptlrpc_request *oldrep = exp->exp_outstanding_reply;
285 memcpy(req->rq_ack_locks, oldrep->rq_ack_locks,
286 sizeof req->rq_ack_locks);
287 spin_lock_irqsave(&req->rq_lock, flags);
288 oldrep->rq_resent = 1;
289 wake_up(&oldrep->rq_reply_waitq);
290 spin_unlock_irqrestore(&req->rq_lock, flags);
291 DEBUG_REQ(D_HA, oldrep, "stole locks from");
292 DEBUG_REQ(D_HA, req, "stole locks for");
295 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
297 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
298 mcd->mcd_last_transno, mcd->mcd_last_result);
299 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
300 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
302 if (req->rq_export->exp_outstanding_reply)
303 mds_steal_ack_locks(req->rq_export, req);
306 static void reconstruct_reint_setattr(struct mds_update_record *rec,
307 int offset, struct ptlrpc_request *req)
309 struct mds_export_data *med = &req->rq_export->exp_mds_data;
310 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
312 struct mds_body *body;
314 mds_req_from_mcd(req, med->med_mcd);
316 de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
318 LASSERT(PTR_ERR(de) == req->rq_status);
322 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
323 mds_pack_inode2fid(&body->fid1, de->d_inode);
324 mds_pack_inode2body(body, de->d_inode);
326 /* Don't return OST-specific attributes if we didn't just set them */
327 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
328 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
329 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
330 body->valid |= OBD_MD_FLMTIME;
331 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
332 body->valid |= OBD_MD_FLATIME;
337 /* In the raw-setattr case, we lock the child inode.
338 * In the write-back case or if being called from open, the client holds a lock
341 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
342 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
343 struct ptlrpc_request *req,
344 struct lustre_handle *lh)
346 struct mds_obd *mds = mds_req2mds(req);
347 struct obd_device *obd = req->rq_export->exp_obd;
348 struct mds_body *body;
350 struct inode *inode = NULL;
351 struct lustre_handle lockh;
353 struct mds_logcancel_data *mlcd = NULL;
354 int rc = 0, cleanup_phase = 0, err, locked = 0;
357 LASSERT(offset == 0);
359 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x\n", rec->ur_fid1->id,
360 rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
362 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
364 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
365 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
367 GOTO(cleanup, rc = PTR_ERR(de));
369 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
372 GOTO(cleanup, rc = PTR_ERR(de));
379 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
382 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
384 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
386 GOTO(cleanup, rc = PTR_ERR(handle));
388 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
389 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
390 LTIME_S(rec->ur_iattr.ia_mtime),
391 LTIME_S(rec->ur_iattr.ia_ctime));
392 rc = mds_fix_attr(inode, rec);
396 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
397 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
398 (long)&rec->ur_iattr.ia_attr_flags);
400 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
402 if (rc == 0 && S_ISREG(inode->i_mode) && rec->ur_eadata != NULL) {
403 rc = fsfilt_set_md(obd, inode, handle,
404 rec->ur_eadata, rec->ur_eadatalen);
407 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
408 mds_pack_inode2fid(&body->fid1, inode);
409 mds_pack_inode2body(body, inode);
411 /* Don't return OST-specific attributes if we didn't just set them */
412 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
413 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
414 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
415 body->valid |= OBD_MD_FLMTIME;
416 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
417 body->valid |= OBD_MD_FLATIME;
419 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
420 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
423 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
425 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
426 mlcd->mlcd_cookielen = rec->ur_cookielen;
427 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
428 mlcd->mlcd_cookielen;
429 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
430 mlcd->mlcd_cookielen);
431 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
432 mlcd->mlcd_eadatalen);
434 CERROR("unable to allocate log cancel data\n");
440 fsfilt_add_journal_cb(req->rq_export->exp_obd, 0, handle,
441 mds_cancel_cookies_cb, mlcd);
442 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
443 switch (cleanup_phase) {
445 if (S_ISREG(inode->i_mode) && rec->ur_eadata != NULL)
450 ldlm_lock_decref(&lockh, LCK_PW);
452 ldlm_put_lock_into_req(req, &lockh, LCK_PW);
467 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
468 struct ptlrpc_request *req)
470 struct mds_export_data *med = &req->rq_export->exp_mds_data;
471 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
472 struct dentry *parent, *child;
473 struct mds_body *body;
475 mds_req_from_mcd(req, med->med_mcd);
480 parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
481 LASSERT(!IS_ERR(parent));
482 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
483 LASSERT(!IS_ERR(child));
484 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
485 mds_pack_inode2fid(&body->fid1, child->d_inode);
486 mds_pack_inode2body(body, child->d_inode);
491 static int mds_reint_create(struct mds_update_record *rec, int offset,
492 struct ptlrpc_request *req,
493 struct lustre_handle *lh)
495 struct dentry *dparent = NULL;
496 struct mds_obd *mds = mds_req2mds(req);
497 struct obd_device *obd = req->rq_export->exp_obd;
498 struct dentry *dchild = NULL;
499 struct inode *dir = NULL;
501 struct lustre_handle lockh;
502 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
504 struct dentry_params dp;
507 LASSERT(offset == 0);
508 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, "mds"));
510 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o\n",
511 rec->ur_fid1->id, rec->ur_fid1->generation,
512 rec->ur_name, rec->ur_mode);
514 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
516 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
517 GOTO(cleanup, rc = -ESTALE);
519 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, &lockh,
520 rec->ur_name, rec->ur_namelen - 1);
521 if (IS_ERR(dparent)) {
522 rc = PTR_ERR(dparent);
523 CERROR("parent lookup error %d\n", rc);
526 cleanup_phase = 1; /* locked parent dentry */
527 dir = dparent->d_inode;
530 ldlm_lock_dump_handle(D_OTHER, &lockh);
532 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
533 if (IS_ERR(dchild)) {
534 rc = PTR_ERR(dchild);
535 CERROR("child lookup error %d\n", rc);
539 cleanup_phase = 2; /* child dentry */
541 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
543 if (dir->i_mode & S_ISGID) {
544 if (S_ISDIR(rec->ur_mode))
545 rec->ur_mode |= S_ISGID;
548 dchild->d_fsdata = (void *)&dp;
549 dp.p_inum = (unsigned long)rec->ur_fid2->id;
554 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
556 GOTO(cleanup, rc = PTR_ERR(handle));
557 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
562 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
564 GOTO(cleanup, rc = PTR_ERR(handle));
565 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
570 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
572 GOTO(cleanup, rc = PTR_ERR(handle));
573 if (rec->ur_tgt == NULL) /* no target supplied */
574 rc = -EINVAL; /* -EPROTO? */
576 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
584 int rdev = rec->ur_rdev;
585 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
587 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
588 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
593 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
594 dchild->d_fsdata = NULL;
595 GOTO(cleanup, rc = -EINVAL);
598 /* In case we stored the desired inum in here, we want to clean up. */
599 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
600 dchild->d_fsdata = NULL;
603 CDEBUG(D_INODE, "error during create: %d\n", rc);
607 struct inode *inode = dchild->d_inode;
608 struct mds_body *body;
611 LTIME_S(iattr.ia_atime) = rec->ur_time;
612 LTIME_S(iattr.ia_ctime) = rec->ur_time;
613 LTIME_S(iattr.ia_mtime) = rec->ur_time;
614 iattr.ia_uid = rec->ur_fsuid;
615 if (dir->i_mode & S_ISGID)
616 iattr.ia_gid = dir->i_gid;
618 iattr.ia_gid = rec->ur_fsgid;
619 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
620 ATTR_MTIME | ATTR_CTIME;
622 if (rec->ur_fid2->id) {
623 LASSERT(rec->ur_fid2->id == inode->i_ino);
624 inode->i_generation = rec->ur_fid2->generation;
625 /* Dirtied and committed by the upcoming setattr. */
626 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
627 inode->i_ino, inode->i_generation);
629 struct lustre_handle child_ino_lockh;
630 struct ldlm_res_id child_res_id =
631 { .name = { inode->i_ino, 0 } };
634 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
635 inode->i_ino, inode->i_generation);
637 /* The inode we were allocated may have just been freed
638 * by an unlink operation. We take this lock to
639 * synchronize against the matching reply-ack-lock taken
640 * in unlink, to avoid replay problems if this reply
641 * makes it out to the client but the unlink's does not.
642 * See bug 2029 for more detail.*/
643 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
644 NULL, child_res_id, LDLM_PLAIN,
645 NULL, 0, LCK_EX, &lock_flags,
647 mds_blocking_ast, NULL,
649 if (rc != ELDLM_OK) {
650 CERROR("error locking for unlink/create sync: "
653 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
657 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
659 CERROR("error on child setattr: rc = %d\n", rc);
661 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
662 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
664 CERROR("error on parent setattr: rc = %d\n", rc);
666 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
667 mds_pack_inode2fid(&body->fid1, inode);
668 mds_pack_inode2body(body, inode);
673 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
676 /* Destroy the file we just created. This should not need
677 * extra journal credits, as we have already modified all of
678 * the blocks needed in order to create the file in the first
683 err = vfs_rmdir(dir, dchild);
685 CERROR("rmdir in error path: %d\n", err);
688 err = vfs_unlink(dir, dchild);
690 CERROR("unlink in error path: %d\n", err);
696 switch (cleanup_phase) {
697 case 2: /* child dentry */
699 case 1: /* locked parent dentry */
701 ldlm_lock_decref(&lockh, LCK_PW);
703 ldlm_put_lock_into_req(req, &lockh, LCK_PW);
709 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
716 int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2)
720 for (i = 0; i < RES_NAME_SIZE; i++) {
721 /* return 1 here, because enqueue_ordered will skip resources
722 * of all zeroes if they're sorted to the end of the list. */
723 if (res1->name[i] == 0 && res2->name[i] != 0)
725 if (res2->name[i] == 0 && res1->name[i] != 0)
728 if (res1->name[i] > res2->name[i])
730 if (res1->name[i] < res2->name[i])
736 /* This function doesn't use ldlm_match_or_enqueue because we're always called
737 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
738 * because they take the place of local semaphores.
740 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
741 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
742 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
743 struct lustre_handle *p1_lockh, int p1_lock_mode,
744 struct ldlm_res_id *p2_res_id,
745 struct lustre_handle *p2_lockh, int p2_lock_mode)
747 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
748 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
749 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
753 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
755 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
756 res_id[0]->name[0], res_id[1]->name[0]);
758 if (res_gt(p1_res_id, p2_res_id)) {
759 handles[1] = p1_lockh;
760 handles[0] = p2_lockh;
761 res_id[1] = p1_res_id;
762 res_id[0] = p2_res_id;
763 lock_modes[1] = p1_lock_mode;
764 lock_modes[0] = p2_lock_mode;
767 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
768 res_id[0]->name[0], res_id[1]->name[0]);
770 flags = LDLM_FL_LOCAL_ONLY;
771 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL, *res_id[0],
772 LDLM_PLAIN, NULL, 0, lock_modes[0], &flags,
773 ldlm_completion_ast, mds_blocking_ast, NULL,
777 ldlm_lock_dump_handle(D_OTHER, handles[0]);
779 if (memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) == 0) {
780 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
781 ldlm_lock_addref(handles[1], lock_modes[1]);
782 } else if (res_id[1]->name[0] != 0) {
783 flags = LDLM_FL_LOCAL_ONLY;
784 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, NULL,
785 *res_id[1], LDLM_PLAIN, NULL, 0,
786 lock_modes[1], &flags,ldlm_completion_ast,
787 mds_blocking_ast, NULL, handles[1]);
788 if (rc != ELDLM_OK) {
789 ldlm_lock_decref(handles[0], lock_modes[0]);
792 ldlm_lock_dump_handle(D_OTHER, handles[1]);
798 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
799 struct lustre_handle *p1_lockh, int p1_lock_mode,
800 struct ldlm_res_id *p2_res_id,
801 struct lustre_handle *p2_lockh, int p2_lock_mode,
802 struct ldlm_res_id *c1_res_id,
803 struct lustre_handle *c1_lockh, int c1_lock_mode,
804 struct ldlm_res_id *c2_res_id,
805 struct lustre_handle *c2_lockh, int c2_lock_mode)
807 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
808 c1_res_id, c2_res_id };
809 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
810 c1_lockh, c2_lockh };
811 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
812 c1_lock_mode, c2_lock_mode };
813 int rc, i, j, sorted, flags;
816 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
817 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
820 /* simple insertion sort - we have at most 4 elements */
821 for (i = 1; i < 4; i++) {
823 dlm_handles[4] = dlm_handles[i];
824 res_id[4] = res_id[i];
825 lock_modes[4] = lock_modes[i];
829 if (res_gt(res_id[j], res_id[4])) {
830 dlm_handles[j + 1] = dlm_handles[j];
831 res_id[j + 1] = res_id[j];
832 lock_modes[j + 1] = lock_modes[j];
837 } while (j >= 0 && !sorted);
839 dlm_handles[j + 1] = dlm_handles[4];
840 res_id[j + 1] = res_id[4];
841 lock_modes[j + 1] = lock_modes[4];
844 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
845 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
848 /* XXX we could send ASTs on all these locks first before blocking? */
849 for (i = 0; i < 4; i++) {
851 if (res_id[i]->name[0] == 0)
854 memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) == 0) {
855 memcpy(dlm_handles[i], dlm_handles[i-1],
856 sizeof(*(dlm_handles[i])));
857 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
859 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
860 NULL, *res_id[i], LDLM_PLAIN,
861 NULL, 0, lock_modes[i], &flags,
863 mds_blocking_ast, NULL,
866 GOTO(out_err, rc = -EIO);
867 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
874 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
879 /* In the unlikely case that the child changed while we were waiting
880 * on the lock, we need to drop the lock on the old child and either:
881 * - if the child has a lower resource name, then we have to also
882 * drop the parent lock and regain the locks in the right order
883 * - in the rename case, if the child has a lower resource name than one of
884 * the other parent/child resources (maxres) we also need to reget the locks
885 * - if the child has a higher resource name (this is the common case)
886 * we can just get the lock on the new child (still in lock order)
888 * Returns 0 if the child did not change or if it changed but could be locked.
889 * Returns 1 if the child changed and we need to re-lock (no locks held).
890 * Returns -ve error with a valid dchild (no locks held). */
891 static int mds_verify_child(struct obd_device *obd,
892 struct ldlm_res_id *parent_res_id,
893 struct lustre_handle *parent_lockh,
894 struct dentry *dparent, int parent_mode,
895 struct ldlm_res_id *child_res_id,
896 struct lustre_handle *child_lockh,
897 struct dentry **dchildp, int child_mode,
898 const char *name, int namelen,
899 struct ldlm_res_id *maxres)
901 struct dentry *vchild, *dchild = *dchildp;
902 int rc = 0, cleanup_phase = 2; /* parent, child locks */
905 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
907 GOTO(cleanup, rc = PTR_ERR(vchild));
909 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
910 (vchild->d_inode != NULL &&
911 child_res_id->name[0] == vchild->d_inode->i_ino &&
912 child_res_id->name[1] == vchild->d_inode->i_generation))) {
920 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
921 vchild->d_inode, dchild ? dchild->d_inode : 0,
922 vchild->d_inode ? vchild->d_inode->i_ino : 0,
923 child_res_id->name[0]);
924 if (child_res_id->name[0] != 0)
925 ldlm_lock_decref(child_lockh, child_mode);
929 cleanup_phase = 1; /* parent lock only */
930 *dchildp = dchild = vchild;
932 if (dchild->d_inode) {
934 child_res_id->name[0] = dchild->d_inode->i_ino;
935 child_res_id->name[1] = dchild->d_inode->i_generation;
937 if (res_gt(parent_res_id, child_res_id) ||
938 res_gt(maxres, child_res_id)) {
939 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
940 child_res_id->name[0], parent_res_id->name[0],
942 GOTO(cleanup, rc = 1);
945 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
946 NULL, *child_res_id, LDLM_PLAIN,
947 NULL, 0, child_mode, &flags,
948 ldlm_completion_ast, mds_blocking_ast,
951 GOTO(cleanup, rc = -EIO);
953 memset(child_res_id, 0, sizeof(*child_res_id));
959 switch(cleanup_phase) {
961 if (child_res_id->name[0] != 0)
962 ldlm_lock_decref(child_lockh, child_mode);
964 ldlm_lock_decref(parent_lockh, parent_mode);
970 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
972 struct lustre_handle *parent_lockh,
973 struct dentry **dparentp, int parent_mode,
974 char *name, int namelen,
975 struct lustre_handle *child_lockh,
976 struct dentry **dchildp, int child_mode)
978 struct ldlm_res_id child_res_id = { .name = {0} };
979 struct ldlm_res_id parent_res_id = { .name = {0} };
981 int rc = 0, cleanup_phase = 0;
984 /* Step 1: Lookup parent */
985 *dparentp = mds_fid2dentry(mds, fid, NULL);
986 if (IS_ERR(*dparentp))
987 RETURN(rc = PTR_ERR(*dparentp));
988 LASSERT((*dparentp)->d_inode);
990 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
991 (*dparentp)->d_inode->i_ino, name);
993 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
994 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
996 cleanup_phase = 1; /* parent dentry */
998 /* Step 2: Lookup child (without lock, to get resource name) */
999 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1000 if (IS_ERR(*dchildp)) {
1001 rc = PTR_ERR(*dchildp);
1002 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1006 inode = (*dchildp)->d_inode;
1008 inode = igrab(inode);
1012 child_res_id.name[0] = inode->i_ino;
1013 child_res_id.name[1] = inode->i_generation;
1017 cleanup_phase = 2; /* child dentry */
1019 /* Step 3: Lock parent and child in resource order. If child doesn't
1020 * exist, we still have to lock the parent and re-lookup. */
1021 rc = enqueue_ordered_locks(obd, &parent_res_id,parent_lockh,parent_mode,
1022 &child_res_id, child_lockh, child_mode);
1026 if (!(*dchildp)->d_inode)
1027 cleanup_phase = 3; /* parent lock */
1029 cleanup_phase = 4; /* child lock */
1031 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1032 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1033 parent_mode, &child_res_id, child_lockh, dchildp,
1034 child_mode, name, namelen, &parent_res_id);
1044 switch (cleanup_phase) {
1046 ldlm_lock_decref(child_lockh, child_mode);
1048 ldlm_lock_decref(parent_lockh, parent_mode);
1059 void mds_reconstruct_generic(struct ptlrpc_request *req)
1061 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1063 mds_req_from_mcd(req, med->med_mcd);
1066 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1067 struct ptlrpc_request *req,
1068 struct lustre_handle *lh)
1070 struct dentry *dparent, *dchild;
1071 struct mds_obd *mds = mds_req2mds(req);
1072 struct obd_device *obd = req->rq_export->exp_obd;
1073 struct mds_body *body = NULL;
1074 struct inode *child_inode;
1075 struct lustre_handle parent_lockh, child_lockh, child_reuse_lockh;
1076 void *handle = NULL;
1077 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1080 LASSERT(offset == 0 || offset == 2);
1082 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1083 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1085 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1087 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1088 GOTO(cleanup, rc = -ENOENT);
1090 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1091 &parent_lockh, &dparent, LCK_PW,
1092 rec->ur_name, rec->ur_namelen,
1093 &child_lockh, &dchild, LCK_EX);
1097 cleanup_phase = 1; /* dchild, dparent, locks */
1099 child_inode = dchild->d_inode;
1100 if (child_inode == NULL) {
1101 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1102 dparent->d_inode->i_ino, rec->ur_name);
1103 GOTO(cleanup, rc = -ENOENT);
1106 cleanup_phase = 2; /* dchild has a lock */
1108 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1109 * reuse (see bug 2029). */
1110 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1114 cleanup_phase = 3; /* child inum lock */
1116 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1118 /* ldlm_reply in buf[0] if called via intent */
1122 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1123 LASSERT(body != NULL);
1125 /* If this is the last reference to this inode, get the OBD EA
1126 * data first so the client can destroy OST objects.
1127 * we only do the object removal if no open files remain.
1128 * Nobody can get at this name anymore because of the locks so
1129 * we make decisions here as to whether to remove the inode */
1130 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1131 mds_open_orphan_count(child_inode) == 0) {
1132 mds_pack_inode2fid(&body->fid1, child_inode);
1133 mds_pack_inode2body(body, child_inode);
1134 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1136 if (!(body->valid & OBD_MD_FLEASIZE)) {
1137 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1138 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1144 /* We have to do these checks ourselves, in case we are making an
1145 * orphan. The client tells us whether rmdir() or unlink() was called,
1146 * so we need to return appropriate errors (bug 72).
1148 * We don't have to check permissions, because vfs_rename (called from
1149 * mds_open_unlink_rename) also calls may_delete. */
1150 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1151 if (!S_ISDIR(child_inode->i_mode))
1152 GOTO(cleanup, rc = -ENOTDIR);
1154 if (S_ISDIR(child_inode->i_mode))
1155 GOTO(cleanup, rc = -EISDIR);
1158 if (child_inode->i_nlink == (S_ISDIR(child_inode->i_mode) ? 2 : 1) &&
1159 mds_open_orphan_count(child_inode) > 0) {
1160 rc = mds_open_unlink_rename(rec, obd, dparent, dchild, &handle);
1161 cleanup_phase = 4; /* transaction */
1165 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1166 switch (child_inode->i_mode & S_IFMT) {
1168 /* Drop any lingering child directories before we start our
1169 * transaction, to avoid doing multiple inode dirty/delete
1170 * in our compound transaction (bug 1321). */
1171 shrink_dcache_parent(dchild);
1172 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1175 GOTO(cleanup, rc = PTR_ERR(handle));
1176 cleanup_phase = 4; /* transaction */
1177 rc = vfs_rmdir(dparent->d_inode, dchild);
1180 handle = fsfilt_start(obd, dparent->d_inode,
1181 FSFILT_OP_UNLINK_LOG, NULL);
1183 GOTO(cleanup, rc = PTR_ERR(handle));
1185 cleanup_phase = 4; /* transaction */
1186 rc = vfs_unlink(dparent->d_inode, dchild);
1188 #ifdef ENABLE_ORPHANS
1190 if (!rc && log_unlink)
1191 if (mds_log_op_unlink(obd, child_inode, req->rq_repmsg,
1193 body->valid |= OBD_MD_FLCOOKIE;
1202 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1205 GOTO(cleanup, rc = PTR_ERR(handle));
1206 cleanup_phase = 4; /* transaction */
1207 rc = vfs_unlink(dparent->d_inode, dchild);
1210 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1213 GOTO(cleanup, rc = -EINVAL);
1221 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1222 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1223 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1225 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1227 CERROR("error on parent setattr: rc = %d\n", err);
1230 switch(cleanup_phase) {
1232 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1235 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1236 "unlinked", 0, NULL);
1237 case 3: /* child ino-reuse lock */
1238 if (rc && body != NULL) {
1239 // Don't unlink the OST objects if the MDS unlink failed
1243 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1245 ldlm_put_lock_into_req(req, &child_reuse_lockh, LCK_EX);
1246 case 2: /* child lock */
1247 ldlm_lock_decref(&child_lockh, LCK_EX);
1248 case 1: /* child and parent dentry, parent lock */
1250 ldlm_lock_decref(&parent_lockh, LCK_PW);
1252 ldlm_put_lock_into_req(req, &parent_lockh, LCK_PW);
1258 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1261 req->rq_status = rc;
1265 static int mds_reint_link(struct mds_update_record *rec, int offset,
1266 struct ptlrpc_request *req,
1267 struct lustre_handle *lh)
1269 struct obd_device *obd = req->rq_export->exp_obd;
1270 struct dentry *de_src = NULL;
1271 struct dentry *de_tgt_dir = NULL;
1272 struct dentry *dchild = NULL;
1273 struct mds_obd *mds = mds_req2mds(req);
1274 struct lustre_handle *handle = NULL, tgt_dir_lockh, src_lockh;
1275 struct ldlm_res_id src_res_id = { .name = {0} };
1276 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
1277 int rc = 0, cleanup_phase = 0;
1280 LASSERT(offset == 0);
1282 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
1283 rec->ur_fid1->id, rec->ur_fid1->generation,
1284 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
1286 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1288 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
1289 GOTO(cleanup, rc = -ENOENT);
1291 /* Step 1: Lookup the source inode and target directory by FID */
1292 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1294 GOTO(cleanup, rc = PTR_ERR(de_src));
1296 cleanup_phase = 1; /* source dentry */
1298 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
1299 if (IS_ERR(de_tgt_dir))
1300 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1302 cleanup_phase = 2; /* target directory dentry */
1304 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
1305 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
1306 de_src->d_inode->i_ino);
1308 /* Step 2: Take the two locks */
1309 src_res_id.name[0] = de_src->d_inode->i_ino;
1310 src_res_id.name[1] = de_src->d_inode->i_generation;
1311 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
1312 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
1314 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
1315 &tgt_dir_res_id, &tgt_dir_lockh, LCK_EX);
1319 cleanup_phase = 3; /* locks */
1321 /* Step 3: Lookup the child */
1322 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
1323 if (IS_ERR(dchild)) {
1324 rc = PTR_ERR(dchild);
1325 if (rc != -EPERM && rc != -EACCES)
1326 CERROR("child lookup error %d\n", rc);
1330 cleanup_phase = 4; /* child dentry */
1332 if (dchild->d_inode) {
1333 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
1334 de_tgt_dir->d_inode->i_ino, rec->ur_name);
1339 /* Step 4: Do it. */
1340 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1342 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1343 if (IS_ERR(handle)) {
1344 rc = PTR_ERR(handle);
1348 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
1349 if (rc && rc != -EPERM && rc != -EACCES)
1350 CERROR("vfs_link error %d\n", rc);
1352 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
1353 handle, req, rc, 0);
1356 switch (cleanup_phase) {
1357 case 4: /* child dentry */
1361 ldlm_lock_decref(&src_lockh, LCK_EX);
1362 ldlm_lock_decref(&tgt_dir_lockh, LCK_EX);
1364 ldlm_put_lock_into_req(req, &src_lockh, LCK_EX);
1365 ldlm_put_lock_into_req(req, &tgt_dir_lockh, LCK_EX);
1367 case 2: /* target dentry */
1369 case 1: /* source dentry */
1374 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1377 req->rq_status = rc;
1382 * add a hard link in the PENDING directory, only used by rename()
1384 static int mds_add_link_orphan(struct mds_update_record *rec,
1385 struct obd_device *obd,
1386 struct dentry *dentry)
1388 struct mds_obd *mds = &obd->u.mds;
1389 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1390 struct dentry *pending_child;
1391 char fidname[LL_FID_NAMELEN];
1395 LASSERT(dentry->d_inode);
1396 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
1398 down(&pending_dir->i_sem);
1399 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
1400 dentry->d_inode->i_generation);
1402 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
1403 mds_open_orphan_count(dentry->d_inode),
1404 rec->ur_name, fidname);
1406 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
1407 if (IS_ERR(pending_child))
1408 GOTO(out_lock, rc = PTR_ERR(pending_child));
1410 if (pending_child->d_inode != NULL) {
1411 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1412 LASSERT(pending_child->d_inode == dentry->d_inode);
1413 GOTO(out_dput, rc = 0);
1417 rc = vfs_link(dentry, pending_dir, pending_child);
1420 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
1423 mds_inode_set_orphan(dentry->d_inode);
1425 l_dput(pending_child);
1427 up(&pending_dir->i_sem);
1431 /* The idea here is that we need to get four locks in the end:
1432 * one on each parent directory, one on each child. We need to take
1433 * these locks in some kind of order (to avoid deadlocks), and the order
1434 * I selected is "increasing resource number" order. We need to look up
1435 * the children, however, before we know what the resource number(s) are.
1436 * Thus the following plan:
1438 * 1,2. Look up the parents
1439 * 3,4. Look up the children
1440 * 5. Take locks on the parents and children, in order
1441 * 6. Verify that the children haven't changed since they were looked up
1443 * If there was a race and the children changed since they were first looked
1444 * up, it is possible that mds_verify_child() will be able to just grab the
1445 * lock on the new child resource (if it has a higher resource than any other)
1446 * but we need to compare against not only its parent, but also against the
1447 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
1449 * We need the fancy igrab() on the child inodes because we aren't holding a
1450 * lock on the parent after the lookup is done, so dentry->d_inode may change
1451 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
1453 static int mds_get_parents_children_locked(struct obd_device *obd,
1454 struct mds_obd *mds,
1455 struct ll_fid *p1_fid,
1456 struct dentry **de_srcdirp,
1457 struct ll_fid *p2_fid,
1458 struct dentry **de_tgtdirp,
1460 const char *old_name, int old_len,
1461 struct dentry **de_oldp,
1462 const char *new_name, int new_len,
1463 struct dentry **de_newp,
1464 struct lustre_handle *dlm_handles,
1467 struct ldlm_res_id p1_res_id = { .name = {0} };
1468 struct ldlm_res_id p2_res_id = { .name = {0} };
1469 struct ldlm_res_id c1_res_id = { .name = {0} };
1470 struct ldlm_res_id c2_res_id = { .name = {0} };
1471 struct ldlm_res_id *maxres_src, *maxres_tgt;
1472 struct inode *inode;
1473 int rc = 0, cleanup_phase = 0;
1476 /* Step 1: Lookup the source directory */
1477 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
1478 if (IS_ERR(*de_srcdirp))
1479 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
1481 cleanup_phase = 1; /* source directory dentry */
1483 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
1484 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
1486 /* Step 2: Lookup the target directory */
1487 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
1488 *de_tgtdirp = dget(*de_srcdirp);
1490 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
1491 if (IS_ERR(*de_tgtdirp))
1492 GOTO(cleanup, rc = PTR_ERR(*de_tgtdirp));
1495 cleanup_phase = 2; /* target directory dentry */
1497 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
1498 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
1500 /* Step 3: Lookup the source child entry */
1501 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
1502 if (IS_ERR(*de_oldp)) {
1503 rc = PTR_ERR(*de_oldp);
1504 CERROR("old child lookup error (%*s): %d\n",
1505 old_len - 1, old_name, rc);
1509 cleanup_phase = 3; /* original name dentry */
1511 inode = (*de_oldp)->d_inode;
1513 inode = igrab(inode);
1515 GOTO(cleanup, rc = -ENOENT);
1517 c1_res_id.name[0] = inode->i_ino;
1518 c1_res_id.name[1] = inode->i_generation;
1521 /* Step 4: Lookup the target child entry */
1522 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
1523 if (IS_ERR(*de_newp)) {
1524 rc = PTR_ERR(*de_newp);
1525 CERROR("new child lookup error (%*s): %d\n",
1526 old_len - 1, old_name, rc);
1530 cleanup_phase = 4; /* target dentry */
1532 inode = (*de_newp)->d_inode;
1534 inode = igrab(inode);
1538 c2_res_id.name[0] = inode->i_ino;
1539 c2_res_id.name[1] = inode->i_generation;
1543 /* Step 5: Take locks on the parents and child(ren) */
1544 maxres_src = &p1_res_id;
1545 maxres_tgt = &p2_res_id;
1546 cleanup_phase = 4; /* target dentry */
1548 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id))
1549 maxres_src = &c1_res_id;
1550 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id))
1551 maxres_tgt = &c2_res_id;
1553 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
1554 &p2_res_id, &dlm_handles[1], parent_mode,
1555 &c1_res_id, &dlm_handles[2], child_mode,
1556 &c2_res_id, &dlm_handles[3], child_mode);
1560 cleanup_phase = 6; /* parent and child(ren) locks */
1562 /* Step 6a: Re-lookup source child to verify it hasn't changed */
1563 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
1564 parent_mode, &c1_res_id, &dlm_handles[2], de_oldp,
1565 child_mode, old_name, old_len, maxres_tgt);
1567 if (c2_res_id.name[0] != 0)
1568 ldlm_lock_decref(&dlm_handles[3], child_mode);
1569 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1576 if ((*de_oldp)->d_inode == NULL)
1577 GOTO(cleanup, rc = -ENOENT);
1579 /* Step 6b: Re-lookup target child to verify it hasn't changed */
1580 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
1581 parent_mode, &c2_res_id, &dlm_handles[3], de_newp,
1582 child_mode, new_name, new_len, maxres_src);
1584 ldlm_lock_decref(&dlm_handles[2], child_mode);
1585 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1595 switch (cleanup_phase) {
1596 case 6: /* child lock(s) */
1597 if (c2_res_id.name[0] != 0)
1598 ldlm_lock_decref(&dlm_handles[3], child_mode);
1599 if (c1_res_id.name[0] != 0)
1600 ldlm_lock_decref(&dlm_handles[2], child_mode);
1601 case 5: /* parent locks */
1602 ldlm_lock_decref(&dlm_handles[1], parent_mode);
1603 ldlm_lock_decref(&dlm_handles[0], parent_mode);
1604 case 4: /* target dentry */
1606 case 3: /* source dentry */
1608 case 2: /* target directory dentry */
1609 l_dput(*de_tgtdirp);
1610 case 1: /* source directry dentry */
1611 l_dput(*de_srcdirp);
1618 static int mds_reint_rename(struct mds_update_record *rec, int offset,
1619 struct ptlrpc_request *req,
1620 struct lustre_handle *lockh)
1622 struct obd_device *obd = req->rq_export->exp_obd;
1623 struct dentry *de_srcdir = NULL;
1624 struct dentry *de_tgtdir = NULL;
1625 struct dentry *de_old = NULL;
1626 struct dentry *de_new = NULL;
1627 struct mds_obd *mds = mds_req2mds(req);
1628 struct lustre_handle dlm_handles[4];
1629 struct mds_body *body = NULL;
1630 int rc = 0, lock_count = 3;
1631 int cleanup_phase = 0;
1632 void *handle = NULL;
1635 LASSERT(offset == 0);
1637 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
1638 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
1639 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
1641 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1643 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
1644 rec->ur_fid2, &de_tgtdir, LCK_PW,
1645 rec->ur_name, rec->ur_namelen,
1646 &de_old, rec->ur_tgt,
1647 rec->ur_tgtlen, &de_new,
1648 dlm_handles, LCK_EX);
1652 cleanup_phase = 1; /* parent(s), children, locks */
1654 if (de_new->d_inode)
1657 /* sanity check for src inode */
1658 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1659 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
1660 GOTO(cleanup, rc = -EINVAL);
1662 /* sanity check for dest inode */
1663 if (de_new->d_inode &&
1664 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
1665 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
1666 GOTO(cleanup, rc = -EINVAL);
1668 /* if we are about to remove the target at first, pass the EA of
1669 * that inode to client to perform and cleanup on OST */
1670 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
1671 LASSERT(body != NULL);
1673 if (de_new->d_inode &&
1674 S_ISREG(de_new->d_inode->i_mode) &&
1675 de_new->d_inode->i_nlink == 1 &&
1676 mds_open_orphan_count(de_new->d_inode) == 0) {
1677 mds_pack_inode2fid(&body->fid1, de_new->d_inode);
1678 mds_pack_inode2body(body, de_new->d_inode);
1679 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
1680 if (!(body->valid & OBD_MD_FLEASIZE)) {
1681 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1682 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1684 /* XXX need log unlink? */
1688 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
1689 de_srcdir->d_inode->i_sb);
1691 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
1693 GOTO(cleanup, rc = PTR_ERR(handle));
1695 /* FIXME need adjust the journal block count? */
1696 /* if the target should be moved to PENDING, we at first increase the
1697 * link and later vfs_rename() will decrease the link count again */
1698 if (de_new->d_inode &&
1699 S_ISREG(de_new->d_inode->i_mode) &&
1700 de_new->d_inode->i_nlink == 1 &&
1701 mds_open_orphan_count(de_new->d_inode) > 0) {
1702 rc = mds_add_link_orphan(rec, obd, de_new);
1708 de_old->d_fsdata = req;
1709 de_new->d_fsdata = req;
1710 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
1715 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
1716 handle, req, rc, 0);
1717 switch (cleanup_phase) {
1720 if (lock_count == 4)
1721 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
1722 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
1723 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
1724 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
1726 if (lock_count == 4)
1727 ldlm_put_lock_into_req(req,
1728 &(dlm_handles[3]), LCK_EX);
1729 ldlm_put_lock_into_req(req, &(dlm_handles[2]), LCK_EX);
1730 ldlm_put_lock_into_req(req, &(dlm_handles[1]), LCK_PW);
1731 ldlm_put_lock_into_req(req, &(dlm_handles[0]), LCK_PW);
1740 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1743 req->rq_status = rc;
1747 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
1748 struct ptlrpc_request *, struct lustre_handle *);
1750 static mds_reinter reinters[REINT_MAX + 1] = {
1751 [REINT_SETATTR] mds_reint_setattr,
1752 [REINT_CREATE] mds_reint_create,
1753 [REINT_UNLINK] mds_reint_unlink,
1754 [REINT_LINK] mds_reint_link,
1755 [REINT_RENAME] mds_reint_rename,
1756 [REINT_OPEN] mds_open
1759 int mds_reint_rec(struct mds_update_record *rec, int offset,
1760 struct ptlrpc_request *req, struct lustre_handle *lockh)
1762 struct obd_device *obd = req->rq_export->exp_obd;
1763 struct obd_run_ctxt saved;
1767 /* checked by unpacker */
1768 LASSERT(rec->ur_opcode <= REINT_MAX &&
1769 reinters[rec->ur_opcode] != NULL);
1771 push_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);
1772 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
1773 pop_ctxt(&saved, &obd->obd_ctxt, &rec->ur_uc);