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_log.h>
42 #include <linux/lustre_fsfilt.h>
44 #include "mds_internal.h"
46 void mds_commit_cb(struct obd_device *obd, __u64 transno, void *data,
49 obd_transno_commit_cb(obd, transno, error);
52 struct mds_logcancel_data {
53 struct lov_mds_md *mlcd_lmm;
57 struct llog_cookie mlcd_cookies[0];
61 static void mds_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
62 void *cb_data, int error)
64 struct mds_logcancel_data *mlcd = cb_data;
65 struct lov_stripe_md *lsm = NULL;
66 struct llog_ctxt *ctxt;
69 obd_transno_commit_cb(obd, transno, error);
71 CDEBUG(D_HA, "cancelling %d cookies\n",
72 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
74 rc = obd_unpackmd(obd->u.mds.mds_osc_exp, &lsm, mlcd->mlcd_lmm,
75 mlcd->mlcd_eadatalen);
77 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
78 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
81 ///* XXX 0 normally, SENDNOW for debug */);
82 ctxt = llog_get_context(&obd->obd_llogs,
83 mlcd->mlcd_cookies[0].lgc_subsys + 1);
84 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
85 sizeof(*mlcd->mlcd_cookies),
86 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
88 CERROR("error cancelling %d log cookies: rc %d\n",
89 (int)(mlcd->mlcd_cookielen /
90 sizeof(*mlcd->mlcd_cookies)), rc);
93 OBD_FREE(mlcd, mlcd->mlcd_size);
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98 struct ptlrpc_request *req, int rc, __u32 op_data)
100 struct mds_export_data *med = &req->rq_export->exp_mds_data;
101 struct mds_client_data *mcd = med->med_mcd;
102 struct obd_device *obd = req->rq_export->exp_obd;
109 /* if the export has already been failed, we have no last_rcvd slot */
110 if (req->rq_export->exp_failed) {
111 CERROR("committing transaction for disconnected client\n");
120 if (handle == NULL) {
121 /* if we're starting our own xaction, use our own inode */
122 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
123 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
124 if (IS_ERR(handle)) {
125 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
126 RETURN(PTR_ERR(handle));
132 transno = req->rq_reqmsg->transno;
134 LASSERT(transno == 0);
135 } else if (transno == 0) {
136 spin_lock(&mds->mds_transno_lock);
137 transno = ++mds->mds_last_transno;
138 spin_unlock(&mds->mds_transno_lock);
140 spin_lock(&mds->mds_transno_lock);
141 if (transno > mds->mds_last_transno)
142 mds->mds_last_transno = transno;
143 spin_unlock(&mds->mds_transno_lock);
145 req->rq_repmsg->transno = req->rq_transno = transno;
146 mcd->mcd_last_transno = cpu_to_le64(transno);
147 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_result = cpu_to_le32(rc);
149 mcd->mcd_last_data = cpu_to_le32(op_data);
152 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb,
153 transno, handle, 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",
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, mds->mds_sb, 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 ptlrpc_request *req)
280 struct obd_export *exp = req->rq_export;
281 struct list_head *tmp;
282 struct ptlrpc_reply_state *oldrep;
283 struct ptlrpc_service *svc;
285 char str[PTL_NALFMT_SIZE];
288 /* CAVEAT EMPTOR: spinlock order */
289 spin_lock_irqsave (&exp->exp_lock, flags);
290 list_for_each (tmp, &exp->exp_outstanding_replies) {
291 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
293 if (oldrep->rs_xid != req->rq_xid)
296 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
297 CERROR ("Resent req xid "LPX64" has mismatched opc: "
298 "new %d old %d\n", req->rq_xid,
299 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
301 svc = oldrep->rs_srv_ni->sni_service;
302 spin_lock (&svc->srv_lock);
304 list_del_init (&oldrep->rs_exp_list);
306 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
308 oldrep->rs_nlocks, oldrep,
309 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
310 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
312 for (i = 0; i < oldrep->rs_nlocks; i++)
313 ptlrpc_save_lock(req,
314 &oldrep->rs_locks[i],
315 oldrep->rs_modes[i]);
316 oldrep->rs_nlocks = 0;
318 DEBUG_REQ(D_HA, req, "stole locks for");
319 ptlrpc_schedule_difficult_reply (oldrep);
321 spin_unlock (&svc->srv_lock);
322 spin_unlock_irqrestore (&exp->exp_lock, flags);
325 spin_unlock_irqrestore (&exp->exp_lock, flags);
328 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
330 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
331 mcd->mcd_last_transno, mcd->mcd_last_result);
332 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
333 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
335 mds_steal_ack_locks(req);
338 static void reconstruct_reint_setattr(struct mds_update_record *rec,
339 int offset, struct ptlrpc_request *req)
341 struct mds_export_data *med = &req->rq_export->exp_mds_data;
342 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
344 struct mds_body *body;
346 mds_req_from_mcd(req, med->med_mcd);
348 de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
350 LASSERT(PTR_ERR(de) == req->rq_status);
354 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
355 mds_pack_inode2fid(req2obd(req), &body->fid1, de->d_inode);
356 mds_pack_inode2body(req2obd(req), body, de->d_inode);
358 /* Don't return OST-specific attributes if we didn't just set them */
359 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
360 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
361 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
362 body->valid |= OBD_MD_FLMTIME;
363 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
364 body->valid |= OBD_MD_FLATIME;
369 /* In the raw-setattr case, we lock the child inode.
370 * In the write-back case or if being called from open, the client holds a lock
373 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
374 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
375 struct ptlrpc_request *req,
376 struct lustre_handle *lh)
378 struct mds_obd *mds = mds_req2mds(req);
379 struct obd_device *obd = req->rq_export->exp_obd;
380 struct mds_body *body;
382 struct inode *inode = NULL;
383 struct lustre_handle lockh[2] = {{0}, {0}};
386 struct mds_logcancel_data *mlcd = NULL;
387 int rc = 0, cleanup_phase = 0, err, locked = 0;
390 LASSERT(offset == 0);
392 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
393 rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
395 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
397 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
398 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
400 GOTO(cleanup, rc = PTR_ERR(de));
402 __u64 lockpart = MDS_INODELOCK_UPDATE;
403 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
404 lockpart |= MDS_INODELOCK_LOOKUP;
405 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
406 lockh, &parent_mode, NULL, 0, lockpart);
408 GOTO(cleanup, rc = PTR_ERR(de));
416 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
417 rec->ur_eadata != NULL)
420 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
422 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
424 GOTO(cleanup, rc = PTR_ERR(handle));
426 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
427 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
428 LTIME_S(rec->ur_iattr.ia_mtime),
429 LTIME_S(rec->ur_iattr.ia_ctime));
430 rc = mds_fix_attr(inode, rec);
434 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
435 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
436 (long)&rec->ur_iattr.ia_attr_flags);
438 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
440 if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
441 rec->ur_eadata != NULL) {
442 struct lov_stripe_md *lsm = NULL;
444 rc = ll_permission(inode, MAY_WRITE, NULL);
448 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
449 mds->mds_osc_exp, 0, &lsm, rec->ur_eadata);
453 obd_free_memmd(mds->mds_osc_exp, &lsm);
455 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
461 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
462 mds_pack_inode2fid(obd, &body->fid1, inode);
463 mds_pack_inode2body(obd, body, inode);
465 /* Don't return OST-specific attributes if we didn't just set them */
466 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
467 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
468 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
469 body->valid |= OBD_MD_FLMTIME;
470 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
471 body->valid |= OBD_MD_FLATIME;
473 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
474 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
477 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
479 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
480 mlcd->mlcd_cookielen = rec->ur_cookielen;
481 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
482 mlcd->mlcd_cookielen;
483 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
484 mlcd->mlcd_cookielen);
485 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
486 mlcd->mlcd_eadatalen);
488 CERROR("unable to allocate log cancel data\n");
494 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
495 handle, mds_cancel_cookies_cb, mlcd);
496 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
497 switch (cleanup_phase) {
499 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
500 rec->ur_eadata != NULL)
505 if (lockh[1].cookie != 0)
506 ldlm_lock_decref(lockh + 1, parent_mode);
509 ldlm_lock_decref(lockh, LCK_PW);
511 ptlrpc_save_lock (req, lockh, LCK_PW);
526 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
527 struct ptlrpc_request *req)
529 struct mds_export_data *med = &req->rq_export->exp_mds_data;
530 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
531 struct dentry *parent, *child;
532 struct mds_body *body;
535 mds_req_from_mcd(req, med->med_mcd);
537 if (req->rq_status) {
542 parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
543 LASSERT(!IS_ERR(parent));
544 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
545 LASSERT(!IS_ERR(child));
546 if ((child->d_flags & DCACHE_CROSS_REF)) {
547 LASSERTF(child->d_inode == NULL, "BUG 3869");
548 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
549 mds_pack_dentry2fid(&body->fid1, child);
550 } else if (child->d_inode == NULL) {
551 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
552 rec->ur_fid1->id, rec->ur_fid1->generation,
553 rec->ur_name, rec->ur_mode);
554 LASSERTF(child->d_inode != NULL, "BUG 3869");
556 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
557 mds_pack_inode2fid(req2obd(req), &body->fid1, child->d_inode);
558 mds_pack_inode2body(req2obd(req), body, child->d_inode);
565 static int mds_reint_create(struct mds_update_record *rec, int offset,
566 struct ptlrpc_request *req,
567 struct lustre_handle *lh)
569 struct dentry *dparent = NULL;
570 struct mds_obd *mds = mds_req2mds(req);
571 struct obd_device *obd = req->rq_export->exp_obd;
572 struct dentry *dchild = NULL;
573 struct inode *dir = NULL;
575 struct lustre_handle lockh[2] = {{0}, {0}};
577 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
579 struct dentry_params dp;
580 struct mea *mea = NULL;
584 LASSERT(offset == 0);
585 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, LUSTRE_MDS_NAME));
587 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
588 rec->ur_fid1->id, rec->ur_fid1->generation,
589 rec->ur_name, rec->ur_mode);
591 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
593 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
594 GOTO(cleanup, rc = -ESTALE);
596 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, lockh,
597 &parent_mode, rec->ur_name,
598 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
599 if (IS_ERR(dparent)) {
600 rc = PTR_ERR(dparent);
601 CERROR("parent lookup error %d\n", rc);
604 cleanup_phase = 1; /* locked parent dentry */
605 dir = dparent->d_inode;
608 ldlm_lock_dump_handle(D_OTHER, lockh);
610 /* try to retrieve MEA data for this dir */
611 rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
616 /* dir is already splitted, check is requested filename
617 * should live at this MDS or at another one */
619 i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
620 if (mea->mea_master != mea->mea_fids[i].mds) {
621 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
622 " should be %d(%d)\n",
623 mea->mea_master, dparent->d_inode->i_ino,
624 dparent->d_inode->i_generation, rec->ur_name,
625 mea->mea_fids[i].mds, i);
626 GOTO(cleanup, rc = -ERESTART);
630 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
631 if (IS_ERR(dchild)) {
632 rc = PTR_ERR(dchild);
633 CERROR("child lookup error %d\n", rc);
637 cleanup_phase = 2; /* child dentry */
639 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
641 if (type == S_IFREG || type == S_IFDIR) {
642 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
643 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
644 obd->obd_name, dparent->d_inode->i_ino,
645 dparent->d_inode->i_generation, rc, parent_mode);
647 /* dir got splitted */
648 GOTO(cleanup, rc = -ERESTART);
650 /* error happened during spitting. */
655 if (dir->i_mode & S_ISGID) {
656 if (S_ISDIR(rec->ur_mode))
657 rec->ur_mode |= S_ISGID;
660 dchild->d_fsdata = (void *)&dp;
661 dp.p_inum = (unsigned long)rec->ur_fid2->id;
666 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
668 GOTO(cleanup, rc = PTR_ERR(handle));
669 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
677 /* as Peter asked, mkdir() should distribute new directories
678 * over the whole cluster in order to distribute namespace
679 * processing load. first, we calculate which MDS to use to put
680 * new directory's inode in. */
681 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
683 if (i == mds->mds_num) {
684 /* inode will be created locally */
685 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
687 GOTO(cleanup, rc = PTR_ERR(handle));
689 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
692 nstripes = *(u16 *)rec->ur_eadata;
694 if (rc == 0 && nstripes) {
695 /* we pass LCK_EX to split routine to
696 * signalthat we have exclusive access
697 * to the directory. simple because
698 * nobody knows it already exists -bzzz */
699 rc = mds_try_to_split_dir(obd, dchild,
703 /* dir got splitted */
706 /* an error occured during
711 } else if (!DENTRY_VALID(dchild)) {
712 /* inode will be created on another MDS */
713 struct obdo *oa = NULL;
714 struct mds_body *body;
716 /* first, create that inode */
721 if (rec->ur_eadata) {
722 /* user asks for creating splitted dir */
723 oa->o_easize = *((u16 *) rec->ur_eadata);
726 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
727 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
728 OBD_MD_FLUID | OBD_MD_FLGID);
729 oa->o_mode = dir->i_mode;
730 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
732 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
733 CWARN("%s: replay dir creation %*s -> %u/%u\n",
734 obd->obd_name, rec->ur_namelen - 1,
735 rec->ur_name, (unsigned) rec->ur_fid2->id,
736 (unsigned) rec->ur_fid2->generation);
737 oa->o_id = rec->ur_fid2->id;
738 oa->o_generation = rec->ur_fid2->generation;
739 oa->o_flags |= OBD_FL_RECREATE_OBJS;
742 rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
748 /* now, add new dir entry for it */
749 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
750 if (IS_ERR(handle)) {
752 GOTO(cleanup, rc = PTR_ERR(handle));
754 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
756 oa->o_id, oa->o_generation,
761 body = lustre_msg_buf(req->rq_repmsg,
762 offset, sizeof (*body));
763 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
764 body->fid1.id = oa->o_id;
766 body->fid1.generation = oa->o_generation;
769 /* requested name exists in the directory */
776 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
778 GOTO(cleanup, rc = PTR_ERR(handle));
779 if (rec->ur_tgt == NULL) /* no target supplied */
780 rc = -EINVAL; /* -EPROTO? */
782 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
790 int rdev = rec->ur_rdev;
791 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
793 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
794 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
799 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
800 dchild->d_fsdata = NULL;
801 GOTO(cleanup, rc = -EINVAL);
804 /* In case we stored the desired inum in here, we want to clean up. */
805 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
806 dchild->d_fsdata = NULL;
809 CDEBUG(D_INODE, "error during create: %d\n", rc);
811 } else if (dchild->d_inode) {
813 struct inode *inode = dchild->d_inode;
814 struct mds_body *body;
817 LTIME_S(iattr.ia_atime) = LTIME_S(rec->ur_time);
818 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
819 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
820 iattr.ia_uid = rec->_ur_fsuid;
821 if (dir->i_mode & S_ISGID)
822 iattr.ia_gid = dir->i_gid;
824 iattr.ia_gid = rec->_ur_fsgid;
825 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
826 ATTR_MTIME | ATTR_CTIME;
828 if (rec->ur_fid2->id) {
829 LASSERT(rec->ur_fid2->id == inode->i_ino);
830 inode->i_generation = rec->ur_fid2->generation;
831 /* Dirtied and committed by the upcoming setattr. */
832 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
833 inode->i_ino, inode->i_generation);
835 struct lustre_handle child_ino_lockh;
837 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
838 inode->i_ino, inode->i_generation);
840 /* The inode we were allocated may have just been freed
841 * by an unlink operation. We take this lock to
842 * synchronize against the matching reply-ack-lock taken
843 * in unlink, to avoid replay problems if this reply
844 * makes it out to the client but the unlink's does not.
845 * See bug 2029 for more detail.*/
846 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
847 if (rc != ELDLM_OK) {
848 CERROR("error locking for unlink/create sync: "
851 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
855 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
857 CERROR("error on child setattr: rc = %d\n", rc);
859 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
860 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
862 CERROR("error on parent setattr: rc = %d\n", rc);
864 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
865 mds_pack_inode2fid(obd, &body->fid1, inode);
866 mds_pack_inode2body(obd, body, inode);
871 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
874 /* Destroy the file we just created. This should not need extra
875 * journal credits, as we have already modified all of the
876 * blocks needed in order to create the file in the first
880 err = vfs_rmdir(dir, dchild);
882 CERROR("rmdir in error path: %d\n", err);
885 err = vfs_unlink(dir, dchild);
887 CERROR("unlink in error path: %d\n", err);
893 switch (cleanup_phase) {
894 case 2: /* child dentry */
896 case 1: /* locked parent dentry */
898 if (lockh[1].cookie != 0)
899 ldlm_lock_decref(lockh + 1, parent_mode);
902 ldlm_lock_decref(lockh, LCK_PW);
904 ptlrpc_save_lock(req, lockh, LCK_PW);
910 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
914 OBD_FREE(mea, mea_size);
919 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
920 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
924 for (i = 0; i < RES_NAME_SIZE; i++) {
925 /* return 1 here, because enqueue_ordered will skip resources
926 * of all zeroes if they're sorted to the end of the list. */
927 if (res1->name[i] == 0 && res2->name[i] != 0)
929 if (res2->name[i] == 0 && res1->name[i] != 0)
932 if (res1->name[i] > res2->name[i])
934 if (res1->name[i] < res2->name[i])
941 if (memcmp(p1, p2, sizeof(*p1)) < 0)
947 /* This function doesn't use ldlm_match_or_enqueue because we're always called
948 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
949 * because they take the place of local semaphores.
951 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
952 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
953 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
954 struct lustre_handle *p1_lockh, int p1_lock_mode,
955 ldlm_policy_data_t *p1_policy,
956 struct ldlm_res_id *p2_res_id,
957 struct lustre_handle *p2_lockh, int p2_lock_mode,
958 ldlm_policy_data_t *p2_policy)
960 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
961 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
962 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
963 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
967 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
969 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
972 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
973 handles[1] = p1_lockh;
974 handles[0] = p2_lockh;
975 res_id[1] = p1_res_id;
976 res_id[0] = p2_res_id;
977 lock_modes[1] = p1_lock_mode;
978 lock_modes[0] = p2_lock_mode;
979 policies[1] = p1_policy;
980 policies[0] = p2_policy;
983 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
984 res_id[0]->name[0], res_id[1]->name[0]);
986 flags = LDLM_FL_LOCAL_ONLY;
987 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
988 LDLM_IBITS, policies[0], lock_modes[0], &flags,
989 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
990 NULL, 0, NULL, handles[0]);
993 ldlm_lock_dump_handle(D_OTHER, handles[0]);
995 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
996 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
997 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
998 ldlm_lock_addref(handles[1], lock_modes[1]);
999 } else if (res_id[1]->name[0] != 0) {
1000 flags = LDLM_FL_LOCAL_ONLY;
1001 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1002 *res_id[1], LDLM_IBITS, policies[1],
1003 lock_modes[1], &flags, mds_blocking_ast,
1004 ldlm_completion_ast, NULL, NULL, NULL, 0,
1006 if (rc != ELDLM_OK) {
1007 ldlm_lock_decref(handles[0], lock_modes[0]);
1010 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1016 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1017 struct lustre_handle *p1_lockh, int p1_lock_mode,
1018 ldlm_policy_data_t *p1_policy,
1019 struct ldlm_res_id *p2_res_id,
1020 struct lustre_handle *p2_lockh, int p2_lock_mode,
1021 ldlm_policy_data_t *p2_policy,
1022 struct ldlm_res_id *c1_res_id,
1023 struct lustre_handle *c1_lockh, int c1_lock_mode,
1024 ldlm_policy_data_t *c1_policy,
1025 struct ldlm_res_id *c2_res_id,
1026 struct lustre_handle *c2_lockh, int c2_lock_mode,
1027 ldlm_policy_data_t *c2_policy)
1029 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1030 c1_res_id, c2_res_id };
1031 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1032 c1_lockh, c2_lockh };
1033 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1034 c1_lock_mode, c2_lock_mode };
1035 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1036 c1_policy, c2_policy};
1037 int rc, i, j, sorted, flags;
1041 "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1042 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1043 res_id[3]->name[0]);
1045 /* simple insertion sort - we have at most 4 elements */
1046 for (i = 1; i < 4; i++) {
1048 dlm_handles[4] = dlm_handles[i];
1049 res_id[4] = res_id[i];
1050 lock_modes[4] = lock_modes[i];
1051 policies[4] = policies[i];
1055 if (res_gt(res_id[j], res_id[4], policies[j],
1057 dlm_handles[j + 1] = dlm_handles[j];
1058 res_id[j + 1] = res_id[j];
1059 lock_modes[j + 1] = lock_modes[j];
1060 policies[j + 1] = policies[j];
1065 } while (j >= 0 && !sorted);
1067 dlm_handles[j + 1] = dlm_handles[4];
1068 res_id[j + 1] = res_id[4];
1069 lock_modes[j + 1] = lock_modes[4];
1070 policies[j + 1] = policies[4];
1074 "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1075 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1076 res_id[3]->name[0]);
1078 /* XXX we could send ASTs on all these locks first before blocking? */
1079 for (i = 0; i < 4; i++) {
1081 if (res_id[i]->name[0] == 0)
1084 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1085 (policies[i]->l_inodebits.bits &
1086 policies[i-1]->l_inodebits.bits) ) {
1087 memcpy(dlm_handles[i], dlm_handles[i-1],
1088 sizeof(*(dlm_handles[i])));
1089 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1091 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1092 *res_id[i], LDLM_IBITS,
1094 lock_modes[i], &flags,
1096 ldlm_completion_ast, NULL, NULL,
1097 NULL, 0, NULL, dlm_handles[i]);
1099 GOTO(out_err, rc = -EIO);
1100 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1107 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1112 /* In the unlikely case that the child changed while we were waiting
1113 * on the lock, we need to drop the lock on the old child and either:
1114 * - if the child has a lower resource name, then we have to also
1115 * drop the parent lock and regain the locks in the right order
1116 * - in the rename case, if the child has a lower resource name than one of
1117 * the other parent/child resources (maxres) we also need to reget the locks
1118 * - if the child has a higher resource name (this is the common case)
1119 * we can just get the lock on the new child (still in lock order)
1121 * Returns 0 if the child did not change or if it changed but could be locked.
1122 * Returns 1 if the child changed and we need to re-lock (no locks held).
1123 * Returns -ve error with a valid dchild (no locks held). */
1124 static int mds_verify_child(struct obd_device *obd,
1125 struct ldlm_res_id *parent_res_id,
1126 struct lustre_handle *parent_lockh,
1127 struct dentry *dparent, int parent_mode,
1128 struct ldlm_res_id *child_res_id,
1129 struct lustre_handle *child_lockh,
1130 struct dentry **dchildp, int child_mode,
1131 ldlm_policy_data_t *child_policy,
1132 const char *name, int namelen,
1133 struct ldlm_res_id *maxres)
1135 struct dentry *vchild, *dchild = *dchildp;
1136 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1139 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1141 GOTO(cleanup, rc = PTR_ERR(vchild));
1143 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1144 if (child_res_id->name[0] == vchild->d_inum &&
1145 child_res_id->name[1] == vchild->d_generation) {
1154 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1155 (vchild->d_inode != NULL &&
1156 child_res_id->name[0] == vchild->d_inode->i_ino &&
1157 child_res_id->name[1] == vchild->d_inode->i_generation))) {
1166 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1167 vchild->d_inode, dchild ? dchild->d_inode : 0,
1168 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1169 child_res_id->name[0]);
1170 if (child_res_id->name[0] != 0)
1171 ldlm_lock_decref(child_lockh, child_mode);
1175 cleanup_phase = 1; /* parent lock only */
1176 *dchildp = dchild = vchild;
1178 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1180 if (dchild->d_inode) {
1181 child_res_id->name[0] = dchild->d_inode->i_ino;
1182 child_res_id->name[1] = dchild->d_inode->i_generation;
1184 child_res_id->name[0] = dchild->d_inum;
1185 child_res_id->name[1] = dchild->d_generation;
1188 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1189 res_gt(maxres, child_res_id, NULL, NULL)) {
1190 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1191 child_res_id->name[0], parent_res_id->name[0],
1193 GOTO(cleanup, rc = 1);
1196 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1197 *child_res_id, LDLM_IBITS, child_policy,
1198 child_mode, &flags, mds_blocking_ast,
1199 ldlm_completion_ast, NULL, NULL, NULL, 0,
1202 GOTO(cleanup, rc = -EIO);
1205 memset(child_res_id, 0, sizeof(*child_res_id));
1211 switch(cleanup_phase) {
1213 if (child_res_id->name[0] != 0)
1214 ldlm_lock_decref(child_lockh, child_mode);
1216 ldlm_lock_decref(parent_lockh, parent_mode);
1222 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1224 struct lustre_handle *parent_lockh,
1225 struct dentry **dparentp, int parent_mode,
1226 __u64 parent_lockpart, int *update_mode,
1227 char *name, int namelen,
1228 struct lustre_handle *child_lockh,
1229 struct dentry **dchildp, int child_mode,
1230 __u64 child_lockpart, void *clone_info)
1232 struct ldlm_res_id child_res_id = { .name = {0} };
1233 struct ldlm_res_id parent_res_id = { .name = {0} };
1234 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1235 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1236 struct inode *inode;
1237 int rc = 0, cleanup_phase = 0;
1240 /* Step 1: Lookup parent */
1241 *dparentp = mds_fid2dentry(mds, fid, NULL);
1242 if (IS_ERR(*dparentp)) {
1243 rc = PTR_ERR(*dparentp);
1248 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1249 (*dparentp)->d_inode->i_ino, name);
1251 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1252 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1254 parent_lockh[1].cookie = 0;
1255 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1256 struct ldlm_res_id res_id = { .name = {0} };
1257 ldlm_policy_data_t policy;
1259 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1261 res_id.name[0] = (*dparentp)->d_inode->i_ino;
1262 res_id.name[1] = (*dparentp)->d_inode->i_generation;
1263 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1264 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1265 res_id, LDLM_IBITS, &policy,
1266 *update_mode, &flags,
1268 ldlm_completion_ast,
1269 NULL, NULL, NULL, 0, NULL,
1275 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1276 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1277 (*dparentp)->d_inode->i_ino,
1278 (*dparentp)->d_inode->i_generation,
1279 parent_res_id.name[2]);
1283 cleanup_phase = 1; /* parent dentry */
1284 #ifdef CONFIG_SNAPFS
1286 /*FIXME is there any other FUNC will use d_fsdata,
1287 *excepet creating inode according inum*/
1288 (*dparentp)->d_fsdata = clone_info;
1292 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1293 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1294 if (IS_ERR(*dchildp)) {
1295 rc = PTR_ERR(*dchildp);
1296 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1300 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1301 /* inode lives on another MDS: return * mds/ino/gen
1302 * and LOOKUP lock. drop possible UPDATE lock! */
1303 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1304 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1305 child_res_id.name[0] = (*dchildp)->d_inum;
1306 child_res_id.name[1] = (*dchildp)->d_generation;
1310 inode = (*dchildp)->d_inode;
1312 inode = igrab(inode);
1316 child_res_id.name[0] = inode->i_ino;
1317 child_res_id.name[1] = inode->i_generation;
1322 cleanup_phase = 2; /* child dentry */
1324 /* Step 3: Lock parent and child in resource order. If child doesn't
1325 * exist, we still have to lock the parent and re-lookup. */
1326 rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1327 &parent_policy, &child_res_id, child_lockh,
1328 child_mode, &child_policy);
1332 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1333 cleanup_phase = 4; /* child lock */
1335 cleanup_phase = 3; /* parent lock */
1337 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1338 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1339 parent_mode, &child_res_id, child_lockh,
1340 dchildp, child_mode, &child_policy,
1341 name, namelen, &parent_res_id);
1351 switch (cleanup_phase) {
1353 ldlm_lock_decref(child_lockh, child_mode);
1355 ldlm_lock_decref(parent_lockh, parent_mode);
1360 if (parent_lockh[1].cookie)
1361 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1370 void mds_reconstruct_generic(struct ptlrpc_request *req)
1372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1374 mds_req_from_mcd(req, med->med_mcd);
1377 int mds_create_local_dentry(struct mds_update_record *rec,
1378 struct obd_device *obd)
1380 struct mds_obd *mds = &obd->u.mds;
1381 struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1382 int fidlen = 0, rc, cleanup_phase = 0;
1383 struct dentry *new_child = NULL;
1384 char *fidname = rec->ur_name;
1385 struct dentry *child = NULL;
1386 struct lustre_handle lockh[2] = {{0}, {0}};
1390 down(&fids_dir->i_sem);
1391 fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1392 CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1393 fidname, (unsigned) rec->ur_fid1->id,
1394 (unsigned) rec->ur_fid1->generation);
1396 new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1397 up(&fids_dir->i_sem);
1398 if (IS_ERR(new_child)) {
1399 CERROR("can't lookup %s: %d\n", fidname,
1400 (int) PTR_ERR(new_child));
1401 GOTO(cleanup, rc = PTR_ERR(new_child));
1405 if (new_child->d_inode != NULL) {
1406 /* nice. we've already have local dentry! */
1407 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1408 (unsigned) new_child->d_inode->i_ino,
1409 (unsigned) new_child->d_inode->i_generation);
1410 rec->ur_fid1->id = fids_dir->i_ino;
1411 rec->ur_fid1->generation = fids_dir->i_generation;
1412 rec->ur_namelen = fidlen + 1;
1413 GOTO(cleanup, rc = 0);
1416 /* new, local dentry will be added soon. we need no aliases here */
1419 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1420 child = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1422 child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1423 LCK_EX, lockh, NULL, NULL, 0,
1424 MDS_INODELOCK_UPDATE);
1427 if (IS_ERR(child)) {
1428 CERROR("can't get victim: %d\n", (int) PTR_ERR(child));
1429 GOTO(cleanup, rc = PTR_ERR(child));
1433 handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1435 GOTO(cleanup, rc = PTR_ERR(handle));
1437 rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1438 rec->ur_fid1->id, rec->ur_fid1->generation,
1441 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1442 (unsigned long) child->d_inode->i_ino,
1443 (unsigned long) child->d_inode->i_generation, rc);
1445 if (S_ISDIR(child->d_inode->i_mode)) {
1446 fids_dir->i_nlink++;
1447 mark_inode_dirty(fids_dir);
1449 mark_inode_dirty(child->d_inode);
1451 fsfilt_commit(obd, mds->mds_sb, fids_dir, handle, 0);
1453 rec->ur_fid1->id = fids_dir->i_ino;
1454 rec->ur_fid1->generation = fids_dir->i_generation;
1455 rec->ur_namelen = fidlen + 1;
1458 switch(cleanup_phase) {
1460 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1461 ldlm_lock_decref(lockh, LCK_EX);
1471 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1472 struct ptlrpc_request *slave)
1474 void *cookie, *cookie2;
1475 struct mds_body *body2;
1476 struct mds_body *body;
1480 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1481 LASSERT(body != NULL);
1483 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1484 LASSERT(body2 != NULL);
1486 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1490 memcpy(body2, body, sizeof(*body));
1491 body2->valid &= ~OBD_MD_FLCOOKIE;
1493 if (!(body->valid & OBD_MD_FLEASIZE) &&
1494 !(body->valid & OBD_MD_FLDIREA))
1497 if (body->eadatasize == 0) {
1498 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1502 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1504 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1505 LASSERT(ea != NULL);
1507 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1508 LASSERT(ea2 != NULL);
1510 memcpy(ea2, ea, body->eadatasize);
1512 if (body->valid & OBD_MD_FLCOOKIE) {
1513 LASSERT(master->rq_repmsg->buflens[2] >=
1514 slave->rq_repmsg->buflens[2]);
1515 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1516 slave->rq_repmsg->buflens[2]);
1517 LASSERT(cookie != NULL);
1519 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1520 master->rq_repmsg->buflens[2]);
1521 LASSERT(cookie2 != NULL);
1522 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1523 body2->valid |= OBD_MD_FLCOOKIE;
1528 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1529 struct ptlrpc_request *req,
1530 struct lustre_handle *parent_lockh,
1532 struct dentry *dparent,
1533 struct lustre_handle *child_lockh,
1534 struct dentry *dchild)
1536 struct obd_device *obd = req->rq_export->exp_obd;
1537 struct mds_obd *mds = mds_req2mds(req);
1538 struct mdc_op_data op_data;
1539 int rc = 0, cleanup_phase = 0;
1540 struct ptlrpc_request *request = NULL;
1544 LASSERT(offset == 0 || offset == 2);
1546 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)",
1547 rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1548 (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1549 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1550 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode %u/%u/%u)",
1551 rec->ur_namelen - 1, rec->ur_name,
1552 (unsigned)dchild->d_mdsnum,
1553 (unsigned) dchild->d_inum,
1554 (unsigned) dchild->d_generation);
1556 /* time to drop i_nlink on remote MDS */
1557 memset(&op_data, 0, sizeof(op_data));
1558 op_data.fid1.mds = dchild->d_mdsnum;
1559 op_data.fid1.id = dchild->d_inum;
1560 op_data.fid1.generation = dchild->d_generation;
1561 op_data.create_mode = rec->ur_mode;
1562 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1563 op_data.create_mode |= MDS_MODE_REPLAY;
1564 rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1568 mds_copy_unlink_reply(req, request);
1569 ptlrpc_req_finished(request);
1572 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1575 GOTO(cleanup, rc = PTR_ERR(handle));
1576 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1577 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1581 req->rq_status = rc;
1584 if (parent_lockh[1].cookie != 0)
1585 ldlm_lock_decref(parent_lockh + 1, update_mode);
1587 ldlm_lock_decref(child_lockh, LCK_EX);
1589 ldlm_lock_decref(parent_lockh, LCK_PW);
1591 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1598 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1599 struct ptlrpc_request *req,
1600 struct lustre_handle *lh)
1602 struct dentry *dparent, *dchild;
1603 struct mds_obd *mds = mds_req2mds(req);
1604 struct obd_device *obd = req->rq_export->exp_obd;
1605 struct mds_body *body = NULL;
1606 struct inode *child_inode;
1607 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1608 struct lustre_handle child_lockh = {0}, child_reuse_lockh = {0};
1609 struct lustre_handle * slave_lockh = NULL;
1610 char fidname[LL_FID_NAMELEN];
1611 void *handle = NULL;
1612 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1613 int unlink_by_fid = 0;
1617 LASSERT(offset == 0 || offset == 2);
1619 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1620 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1622 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1624 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1625 GOTO(cleanup, rc = -ENOENT);
1627 if (rec->ur_namelen == 1) {
1628 /* this is request to drop i_nlink on local inode */
1630 rec->ur_name = fidname;
1631 rc = mds_create_local_dentry(rec, obd);
1632 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1633 DEBUG_REQ(D_HA, req,
1634 "drop nlink on inode %u/%u/%u (replay)",
1635 (unsigned) rec->ur_fid1->mds,
1636 (unsigned) rec->ur_fid1->id,
1637 (unsigned) rec->ur_fid1->generation);
1643 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1644 /* master mds for directory asks slave removing
1645 * inode is already locked */
1646 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1647 LCK_PW, parent_lockh,
1648 &update_mode, rec->ur_name,
1650 MDS_INODELOCK_UPDATE);
1651 if (IS_ERR(dparent))
1652 GOTO(cleanup, rc = PTR_ERR(dparent));
1653 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1654 rec->ur_namelen - 1);
1656 GOTO(cleanup, rc = PTR_ERR(dchild));
1657 child_lockh.cookie = 0;
1658 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1659 LASSERT(dchild->d_inode != NULL);
1660 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1662 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1663 parent_lockh, &dparent,
1664 LCK_PW, MDS_INODELOCK_UPDATE,
1665 &update_mode, rec->ur_name,
1666 rec->ur_namelen, &child_lockh,
1668 MDS_INODELOCK_LOOKUP |
1669 MDS_INODELOCK_UPDATE, NULL);
1674 if (dchild->d_flags & DCACHE_CROSS_REF) {
1675 /* we should have parent lock only here */
1676 LASSERT(unlink_by_fid == 0);
1677 LASSERT(dchild->d_mdsnum != mds->mds_num);
1678 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1679 update_mode, dparent, &child_lockh, dchild);
1683 cleanup_phase = 1; /* dchild, dparent, locks */
1686 child_inode = dchild->d_inode;
1687 if (child_inode == NULL) {
1688 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1689 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1690 GOTO(cleanup, rc = -ENOENT);
1693 cleanup_phase = 2; /* dchild has a lock */
1695 /* We have to do these checks ourselves, in case we are making an
1696 * orphan. The client tells us whether rmdir() or unlink() was called,
1697 * so we need to return appropriate errors (bug 72).
1699 * We don't have to check permissions, because vfs_rename (called from
1700 * mds_open_unlink_rename) also calls may_delete. */
1701 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1702 if (!S_ISDIR(child_inode->i_mode))
1703 GOTO(cleanup, rc = -ENOTDIR);
1705 if (S_ISDIR(child_inode->i_mode))
1706 GOTO(cleanup, rc = -EISDIR);
1709 /* handle splitted dir */
1710 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
1714 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1715 * reuse (see bug 2029). */
1716 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1720 cleanup_phase = 3; /* child inum lock */
1722 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1724 /* ldlm_reply in buf[0] if called via intent */
1728 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1729 LASSERT(body != NULL);
1731 /* If this is the last reference to this inode, get the OBD EA
1732 * data first so the client can destroy OST objects.
1733 * we only do the object removal if no open files remain.
1734 * Nobody can get at this name anymore because of the locks so
1735 * we make decisions here as to whether to remove the inode */
1736 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1737 mds_open_orphan_count(child_inode) == 0) {
1738 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1739 mds_pack_inode2body(obd, body, child_inode);
1740 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1742 if (!(body->valid & OBD_MD_FLEASIZE)) {
1743 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1744 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1750 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1751 switch (child_inode->i_mode & S_IFMT) {
1753 /* Drop any lingering child directories before we start our
1754 * transaction, to avoid doing multiple inode dirty/delete
1755 * in our compound transaction (bug 1321). */
1756 shrink_dcache_parent(dchild);
1757 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1760 GOTO(cleanup, rc = PTR_ERR(handle));
1761 cleanup_phase = 4; /* transaction */
1762 rc = vfs_rmdir(dparent->d_inode, dchild);
1765 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1766 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1768 handle = fsfilt_start_log(obd, dparent->d_inode,
1769 FSFILT_OP_UNLINK, NULL,
1770 le32_to_cpu(lmm->lmm_stripe_count));
1772 GOTO(cleanup, rc = PTR_ERR(handle));
1774 cleanup_phase = 4; /* transaction */
1775 rc = vfs_unlink(dparent->d_inode, dchild);
1777 if (!rc && log_unlink)
1778 if (mds_log_op_unlink(obd, child_inode,
1779 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1780 req->rq_repmsg->buflens[offset + 1],
1781 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1782 req->rq_repmsg->buflens[offset + 2]) > 0)
1783 body->valid |= OBD_MD_FLCOOKIE;
1791 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1794 GOTO(cleanup, rc = PTR_ERR(handle));
1795 cleanup_phase = 4; /* transaction */
1796 rc = vfs_unlink(dparent->d_inode, dchild);
1799 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1802 GOTO(cleanup, rc = -EINVAL);
1810 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1811 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
1812 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
1814 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1816 CERROR("error on parent setattr: rc = %d\n", err);
1819 switch(cleanup_phase) {
1821 LASSERT(dchild != NULL && dchild->d_inode != NULL);
1822 LASSERT(atomic_read(&dchild->d_inode->i_count) > 0);
1823 if (rc == 0 && dchild->d_inode->i_nlink == 0 &&
1824 mds_open_orphan_count(dchild->d_inode) > 0) {
1826 /* filesystem is really going to destroy an inode
1827 * we have to delay this till inode is opened -bzzz */
1828 mds_open_unlink_rename(rec, obd, dparent, dchild, NULL);
1830 /* handle splitted dir */
1831 mds_unlink_slave_objs(obd, dchild);
1832 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1835 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1836 "unlinked", 0, NULL);
1837 case 3: /* child ino-reuse lock */
1838 if (rc && body != NULL) {
1839 // Don't unlink the OST objects if the MDS unlink failed
1843 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1845 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1846 case 2: /* child lock */
1847 mds_unlock_slave_objs(obd, dchild, slave_lockh);
1848 if (child_lockh.cookie)
1849 ldlm_lock_decref(&child_lockh, LCK_EX);
1850 case 1: /* child and parent dentry, parent lock */
1852 if (parent_lockh[1].cookie != 0)
1853 ldlm_lock_decref(parent_lockh + 1, update_mode);
1856 ldlm_lock_decref(parent_lockh, LCK_PW);
1858 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1865 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1868 req->rq_status = rc;
1873 * to service requests from remote MDS to increment i_nlink
1875 static int mds_reint_link_acquire(struct mds_update_record *rec,
1876 int offset, struct ptlrpc_request *req,
1877 struct lustre_handle *lh)
1879 struct obd_device *obd = req->rq_export->exp_obd;
1880 struct ldlm_res_id src_res_id = { .name = {0} };
1881 struct lustre_handle *handle = NULL, src_lockh = {0};
1882 struct mds_obd *mds = mds_req2mds(req);
1883 int rc = 0, cleanup_phase = 0;
1884 struct dentry *de_src = NULL;
1885 ldlm_policy_data_t policy;
1889 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1890 obd->obd_name, (unsigned) rec->ur_fid1->mds,
1891 (unsigned) rec->ur_fid1->id,
1892 (unsigned) rec->ur_fid1->generation);
1894 /* Step 1: Lookup the source inode and target directory by FID */
1895 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1897 GOTO(cleanup, rc = PTR_ERR(de_src));
1898 cleanup_phase = 1; /* source dentry */
1900 src_res_id.name[0] = de_src->d_inode->i_ino;
1901 src_res_id.name[1] = de_src->d_inode->i_generation;
1902 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1904 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1905 src_res_id, LDLM_IBITS, &policy,
1906 LCK_EX, &flags, mds_blocking_ast,
1907 ldlm_completion_ast, NULL, NULL,
1908 NULL, 0, NULL, &src_lockh);
1910 GOTO(cleanup, rc = -ENOLCK);
1911 cleanup_phase = 2; /* lock */
1913 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1915 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1916 if (IS_ERR(handle)) {
1917 rc = PTR_ERR(handle);
1920 de_src->d_inode->i_nlink++;
1921 mark_inode_dirty(de_src->d_inode);
1924 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1925 handle, req, rc, 0);
1927 switch (cleanup_phase) {
1930 ldlm_lock_decref(&src_lockh, LCK_EX);
1932 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1938 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1941 req->rq_status = rc;
1946 * request to link to foreign inode:
1947 * - acquire i_nlinks on this inode
1950 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1951 int offset, struct ptlrpc_request *req,
1952 struct lustre_handle *lh)
1954 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
1955 struct obd_device *obd = req->rq_export->exp_obd;
1956 struct dentry *de_tgt_dir = NULL;
1957 struct mds_obd *mds = mds_req2mds(req);
1958 int rc = 0, cleanup_phase = 0;
1959 struct mdc_op_data op_data;
1960 struct ptlrpc_request *request = NULL;
1964 #define fmt "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
1965 DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
1966 (unsigned) rec->ur_fid2->mds,
1967 (unsigned) rec->ur_fid2->id,
1968 (unsigned) rec->ur_fid2->generation,
1969 rec->ur_namelen - 1, rec->ur_name,
1970 (unsigned) rec->ur_fid1->mds,
1971 (unsigned) rec->ur_fid1->id,
1972 (unsigned)rec->ur_fid1->generation);
1974 de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
1975 tgt_dir_lockh, &update_mode,
1976 rec->ur_name, rec->ur_namelen - 1,
1977 MDS_INODELOCK_UPDATE);
1978 if (IS_ERR(de_tgt_dir))
1979 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
1982 op_data.fid1 = *(rec->ur_fid1);
1983 op_data.namelen = 0;
1984 op_data.name = NULL;
1985 rc = md_link(mds->mds_lmv_exp, &op_data, &request);
1991 ptlrpc_req_finished(request);
1993 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
1995 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
1996 if (IS_ERR(handle)) {
1997 rc = PTR_ERR(handle);
2001 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2002 rec->ur_namelen - 1, rec->ur_fid1->id,
2003 rec->ur_fid1->generation, rec->ur_fid1->mds);
2007 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2008 handle, req, rc, 0);
2011 switch (cleanup_phase) {
2014 /* FIXME: drop i_nlink on remote inode here */
2015 CERROR("MUST drop drop i_nlink here\n");
2020 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2022 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2025 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2027 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2033 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2036 req->rq_status = rc;
2040 static int mds_reint_link(struct mds_update_record *rec, int offset,
2041 struct ptlrpc_request *req,
2042 struct lustre_handle *lh)
2044 struct obd_device *obd = req->rq_export->exp_obd;
2045 struct dentry *de_src = NULL;
2046 struct dentry *de_tgt_dir = NULL;
2047 struct dentry *dchild = NULL;
2048 struct mds_obd *mds = mds_req2mds(req);
2049 struct lustre_handle *handle = NULL;
2050 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2051 struct ldlm_res_id src_res_id = { .name = {0} };
2052 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2053 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2054 ldlm_policy_data_t tgt_dir_policy =
2055 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2056 int rc = 0, cleanup_phase = 0;
2057 int update_mode = 0;
2060 LASSERT(offset == 0);
2062 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2063 rec->ur_fid1->id, rec->ur_fid1->generation,
2064 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
2066 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2068 // memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2069 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2070 GOTO(cleanup, rc = -ENOENT);
2072 if (rec->ur_fid1->mds != mds->mds_num) {
2073 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2077 if (rec->ur_namelen == 1) {
2078 rc = mds_reint_link_acquire(rec, offset, req, lh);
2082 /* Step 1: Lookup the source inode and target directory by FID */
2083 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
2085 GOTO(cleanup, rc = PTR_ERR(de_src));
2087 cleanup_phase = 1; /* source dentry */
2089 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
2090 if (IS_ERR(de_tgt_dir)) {
2091 rc = PTR_ERR(de_tgt_dir);
2096 cleanup_phase = 2; /* target directory dentry */
2098 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2099 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
2100 de_src->d_inode->i_ino);
2102 /* Step 2: Take the two locks */
2103 src_res_id.name[0] = de_src->d_inode->i_ino;
2104 src_res_id.name[1] = de_src->d_inode->i_generation;
2105 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
2106 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
2108 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2110 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2112 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2113 tgt_dir_res_id, LDLM_IBITS,
2114 &src_policy, update_mode, &flags,
2116 ldlm_completion_ast, NULL, NULL,
2117 NULL, 0, NULL, tgt_dir_lockh + 1);
2119 GOTO(cleanup, rc = -ENOLCK);
2122 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2123 rec->ur_namelen - 1);
2124 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
2125 de_tgt_dir->d_inode->i_ino,
2126 de_tgt_dir->d_inode->i_generation,
2127 tgt_dir_res_id.name[2]);
2130 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2132 &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
2137 cleanup_phase = 3; /* locks */
2139 /* Step 3: Lookup the child */
2140 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
2141 if (IS_ERR(dchild)) {
2142 rc = PTR_ERR(dchild);
2143 if (rc != -EPERM && rc != -EACCES)
2144 CERROR("child lookup error %d\n", rc);
2148 cleanup_phase = 4; /* child dentry */
2150 if (dchild->d_inode) {
2151 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2152 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2157 /* Step 4: Do it. */
2158 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2160 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2161 if (IS_ERR(handle)) {
2162 rc = PTR_ERR(handle);
2166 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2167 if (rc && rc != -EPERM && rc != -EACCES)
2168 CERROR("vfs_link error %d\n", rc);
2170 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2171 handle, req, rc, 0);
2174 switch (cleanup_phase) {
2175 case 4: /* child dentry */
2179 ldlm_lock_decref(&src_lockh, LCK_EX);
2180 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2182 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2183 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2185 case 2: /* target dentry */
2187 if (tgt_dir_lockh[1].cookie && update_mode)
2188 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2192 case 1: /* source dentry */
2197 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2200 req->rq_status = rc;
2205 * add a hard link in the PENDING directory, only used by rename()
2207 static int mds_add_link_orphan(struct mds_update_record *rec,
2208 struct obd_device *obd,
2209 struct dentry *dentry)
2211 struct mds_obd *mds = &obd->u.mds;
2212 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2213 struct dentry *pending_child;
2214 char fidname[LL_FID_NAMELEN];
2218 LASSERT(dentry->d_inode);
2219 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2221 down(&pending_dir->i_sem);
2222 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2223 dentry->d_inode->i_generation);
2225 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2226 mds_open_orphan_count(dentry->d_inode),
2227 rec->ur_name, fidname);
2229 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2230 if (IS_ERR(pending_child))
2231 GOTO(out_lock, rc = PTR_ERR(pending_child));
2233 if (pending_child->d_inode != NULL) {
2234 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2235 LASSERT(pending_child->d_inode == dentry->d_inode);
2236 GOTO(out_dput, rc = 0);
2240 rc = vfs_link(dentry, pending_dir, pending_child);
2243 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2246 mds_inode_set_orphan(dentry->d_inode);
2248 l_dput(pending_child);
2250 up(&pending_dir->i_sem);
2254 /* The idea here is that we need to get four locks in the end:
2255 * one on each parent directory, one on each child. We need to take
2256 * these locks in some kind of order (to avoid deadlocks), and the order
2257 * I selected is "increasing resource number" order. We need to look up
2258 * the children, however, before we know what the resource number(s) are.
2259 * Thus the following plan:
2261 * 1,2. Look up the parents
2262 * 3,4. Look up the children
2263 * 5. Take locks on the parents and children, in order
2264 * 6. Verify that the children haven't changed since they were looked up
2266 * If there was a race and the children changed since they were first looked
2267 * up, it is possible that mds_verify_child() will be able to just grab the
2268 * lock on the new child resource (if it has a higher resource than any other)
2269 * but we need to compare against not only its parent, but also against the
2270 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2272 * We need the fancy igrab() on the child inodes because we aren't holding a
2273 * lock on the parent after the lookup is done, so dentry->d_inode may change
2274 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2276 static int mds_get_parents_children_locked(struct obd_device *obd,
2277 struct mds_obd *mds,
2278 struct ll_fid *p1_fid,
2279 struct dentry **de_srcdirp,
2280 struct ll_fid *p2_fid,
2281 struct dentry **de_tgtdirp,
2283 const char *old_name, int old_len,
2284 struct dentry **de_oldp,
2285 const char *new_name, int new_len,
2286 struct dentry **de_newp,
2287 struct lustre_handle *dlm_handles,
2290 struct ldlm_res_id p1_res_id = { .name = {0} };
2291 struct ldlm_res_id p2_res_id = { .name = {0} };
2292 struct ldlm_res_id c1_res_id = { .name = {0} };
2293 struct ldlm_res_id c2_res_id = { .name = {0} };
2294 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2295 /* Only dentry should change, but the inode itself would be
2297 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2298 /* If something is going to be replaced, both dentry and inode locks are
2300 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2301 MDS_INODELOCK_UPDATE}};
2302 struct ldlm_res_id *maxres_src, *maxres_tgt;
2303 struct inode *inode;
2304 int rc = 0, cleanup_phase = 0;
2307 /* Step 1: Lookup the source directory */
2308 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2309 if (IS_ERR(*de_srcdirp))
2310 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2312 cleanup_phase = 1; /* source directory dentry */
2314 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2315 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2317 /* Step 2: Lookup the target directory */
2318 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2319 *de_tgtdirp = dget(*de_srcdirp);
2321 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2322 if (IS_ERR(*de_tgtdirp)) {
2323 rc = PTR_ERR(*de_tgtdirp);
2329 cleanup_phase = 2; /* target directory dentry */
2331 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2332 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2335 dlm_handles[5].cookie = 0;
2336 dlm_handles[6].cookie = 0;
2337 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2338 /* Get a temp lock on just ino, gen to flush client cache and
2339 * to protect dirs from concurrent splitting */
2340 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2341 LCK_PW, &p_policy, &p2_res_id,
2342 &(dlm_handles[6]),LCK_PW,&p_policy);
2345 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2346 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2347 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2348 (*de_srcdirp)->d_inode->i_ino,
2349 (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2350 (*de_tgtdirp)->d_inode->i_ino,
2351 (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2356 /* Step 3: Lookup the source child entry */
2357 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2358 if (IS_ERR(*de_oldp)) {
2359 rc = PTR_ERR(*de_oldp);
2360 CERROR("old child lookup error (%*s): %d\n",
2361 old_len - 1, old_name, rc);
2365 cleanup_phase = 4; /* original name dentry */
2367 inode = (*de_oldp)->d_inode;
2368 if (inode != NULL) {
2369 inode = igrab(inode);
2371 GOTO(cleanup, rc = -ENOENT);
2373 c1_res_id.name[0] = inode->i_ino;
2374 c1_res_id.name[1] = inode->i_generation;
2376 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2377 c1_res_id.name[0] = (*de_oldp)->d_inum;
2378 c1_res_id.name[1] = (*de_oldp)->d_generation;
2381 /* Step 4: Lookup the target child entry */
2382 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2383 if (IS_ERR(*de_newp)) {
2384 rc = PTR_ERR(*de_newp);
2385 CERROR("new child lookup error (%*s): %d\n",
2386 old_len - 1, old_name, rc);
2390 cleanup_phase = 5; /* target dentry */
2392 inode = (*de_newp)->d_inode;
2393 if (inode != NULL) {
2394 inode = igrab(inode);
2398 c2_res_id.name[0] = inode->i_ino;
2399 c2_res_id.name[1] = inode->i_generation;
2401 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2402 c2_res_id.name[0] = (*de_newp)->d_inum;
2403 c2_res_id.name[1] = (*de_newp)->d_generation;
2407 /* Step 5: Take locks on the parents and child(ren) */
2408 maxres_src = &p1_res_id;
2409 maxres_tgt = &p2_res_id;
2410 cleanup_phase = 5; /* target dentry */
2412 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2413 maxres_src = &c1_res_id;
2414 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2415 maxres_tgt = &c2_res_id;
2417 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2419 &p2_res_id, &dlm_handles[1], parent_mode,
2421 &c1_res_id, &dlm_handles[2], child_mode,
2423 &c2_res_id, &dlm_handles[3], child_mode,
2428 cleanup_phase = 6; /* parent and child(ren) locks */
2430 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2431 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2432 parent_mode, &c1_res_id, &dlm_handles[2],
2433 de_oldp, child_mode, &c1_policy, old_name,old_len,
2436 if (c2_res_id.name[0] != 0)
2437 ldlm_lock_decref(&dlm_handles[3], child_mode);
2438 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2445 if (!DENTRY_VALID(*de_oldp))
2446 GOTO(cleanup, rc = -ENOENT);
2448 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2449 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2450 parent_mode, &c2_res_id, &dlm_handles[3],
2451 de_newp, child_mode, &c2_policy, new_name,
2452 new_len, maxres_src);
2454 ldlm_lock_decref(&dlm_handles[2], child_mode);
2455 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2465 switch (cleanup_phase) {
2466 case 6: /* child lock(s) */
2467 if (c2_res_id.name[0] != 0)
2468 ldlm_lock_decref(&dlm_handles[3], child_mode);
2469 if (c1_res_id.name[0] != 0)
2470 ldlm_lock_decref(&dlm_handles[2], child_mode);
2471 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2472 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2473 case 5: /* target dentry */
2475 case 4: /* source dentry */
2479 if (dlm_handles[5].cookie != 0)
2480 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2481 if (dlm_handles[6].cookie != 0)
2482 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2484 case 2: /* target directory dentry */
2485 l_dput(*de_tgtdirp);
2486 case 1: /* source directry dentry */
2487 l_dput(*de_srcdirp);
2494 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2495 int offset, struct ptlrpc_request *req)
2497 struct obd_device *obd = req->rq_export->exp_obd;
2498 struct dentry *de_srcdir = NULL;
2499 struct dentry *de_new = NULL;
2500 struct mds_obd *mds = mds_req2mds(req);
2501 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2502 struct lustre_handle child_lockh = {0};
2503 int cleanup_phase = 0;
2504 void *handle = NULL;
2505 int update_mode, rc = 0;
2508 /* another MDS executing rename operation has asked us
2509 * to create target name. such a creation should destroy
2510 * existing target name */
2512 CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2513 obd->obd_name, rec->ur_tgt,
2514 (unsigned long) rec->ur_fid1->mds,
2515 (unsigned long) rec->ur_fid1->id,
2516 (unsigned long) rec->ur_fid1->generation);
2518 /* first, lookup the target */
2519 child_lockh.cookie = 0;
2520 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2521 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2522 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2523 &child_lockh, &de_new, LCK_EX,
2524 MDS_INODELOCK_LOOKUP, NULL);
2531 LASSERT(de_srcdir->d_inode);
2534 if (de_new->d_inode) {
2535 /* name exists and points to local inode
2536 * try to unlink this name and create new one */
2537 CERROR("%s: %s points to local inode %lu/%lu\n",
2538 obd->obd_name, rec->ur_tgt,
2539 (unsigned long) de_new->d_inode->i_ino,
2540 (unsigned long) de_new->d_inode->i_generation);
2541 handle = fsfilt_start(obd, de_srcdir->d_inode,
2542 FSFILT_OP_RENAME, NULL);
2544 GOTO(cleanup, rc = PTR_ERR(handle));
2545 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2548 } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2549 /* name exists adn points to remove inode */
2550 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2551 obd->obd_name, rec->ur_tgt,
2552 (unsigned long) de_new->d_mdsnum,
2553 (unsigned long) de_new->d_inum,
2554 (unsigned long) de_new->d_generation);
2556 /* name doesn't exist. the simplest case */
2557 handle = fsfilt_start(obd, de_srcdir->d_inode,
2558 FSFILT_OP_LINK, NULL);
2560 GOTO(cleanup, rc = PTR_ERR(handle));
2564 rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2565 rec->ur_tgtlen - 1, rec->ur_fid1->id,
2566 rec->ur_fid1->generation, rec->ur_fid1->mds);
2568 CERROR("add_dir_entry() returned error %d\n", rc);
2571 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2572 handle, req, rc, 0);
2573 switch(cleanup_phase) {
2577 if (parent_lockh[1].cookie != 0)
2578 ldlm_lock_decref(&parent_lockh[1], update_mode);
2580 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2581 if (child_lockh.cookie != 0)
2582 ldlm_lock_decref(&child_lockh, LCK_EX);
2590 req->rq_status = rc;
2595 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2596 struct ptlrpc_request *req)
2598 struct obd_device *obd = req->rq_export->exp_obd;
2599 struct ptlrpc_request *req2 = NULL;
2600 struct dentry *de_srcdir = NULL;
2601 struct dentry *de_old = NULL;
2602 struct mds_obd *mds = mds_req2mds(req);
2603 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2604 struct lustre_handle child_lockh = {0};
2605 struct mdc_op_data opdata;
2606 void *handle = NULL;
2607 int update_mode, rc = 0;
2610 CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2611 obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2612 memset(&opdata, 0, sizeof(opdata));
2614 child_lockh.cookie = 0;
2615 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2616 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2617 &update_mode, rec->ur_name,
2618 rec->ur_namelen, &child_lockh, &de_old,
2619 LCK_EX, MDS_INODELOCK_LOOKUP, NULL);
2622 LASSERT(de_srcdir->d_inode);
2625 /* we already know the target should be created on another MDS
2626 * so, we have to request that MDS to do it */
2628 /* prepare source fid */
2629 if (de_old->d_flags & DCACHE_CROSS_REF) {
2630 LASSERT(de_old->d_inode == NULL);
2631 CDEBUG(D_OTHER, "request to move remote name\n");
2632 opdata.fid1.mds = de_old->d_mdsnum;
2633 opdata.fid1.id = de_old->d_inum;
2634 opdata.fid1.generation = de_old->d_generation;
2635 } else if (de_old->d_inode == NULL) {
2636 /* oh, source doesn't exist */
2637 GOTO(cleanup, rc = -ENOENT);
2639 LASSERT(de_old->d_inode != NULL);
2640 CDEBUG(D_OTHER, "request to move local name\n");
2641 opdata.fid1.mds = mds->mds_num;
2642 opdata.fid1.id = de_old->d_inode->i_ino;
2643 opdata.fid1.generation = de_old->d_inode->i_generation;
2646 opdata.fid2 = *(rec->ur_fid2);
2647 rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2648 rec->ur_tgtlen - 1, &req2);
2653 handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2655 GOTO(cleanup, rc = PTR_ERR(handle));
2656 rc = fsfilt_del_dir_entry(obd, de_old);
2661 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2662 handle, req, rc, 0);
2664 ptlrpc_req_finished(req2);
2667 if (parent_lockh[1].cookie != 0)
2668 ldlm_lock_decref(&parent_lockh[1], update_mode);
2670 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2671 if (child_lockh.cookie != 0)
2672 ldlm_lock_decref(&child_lockh, LCK_EX);
2677 req->rq_status = rc;
2682 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2683 struct ptlrpc_request *req, struct lustre_handle *lockh)
2685 struct obd_device *obd = req->rq_export->exp_obd;
2686 struct dentry *de_srcdir = NULL;
2687 struct dentry *de_tgtdir = NULL;
2688 struct dentry *de_old = NULL;
2689 struct dentry *de_new = NULL;
2690 struct mds_obd *mds = mds_req2mds(req);
2691 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
2692 struct mds_body *body = NULL;
2693 int rc = 0, lock_count = 3;
2694 int cleanup_phase = 0;
2695 void *handle = NULL;
2698 LASSERT(offset == 0);
2700 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2701 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2702 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2704 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2706 if (rec->ur_namelen == 1) {
2707 rc = mds_reint_rename_create_name(rec, offset, req);
2711 if (rec->ur_fid2->mds != mds->mds_num) {
2712 rc = mds_reint_rename_to_remote(rec, offset, req);
2716 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2717 rec->ur_fid2, &de_tgtdir, LCK_PW,
2718 rec->ur_name, rec->ur_namelen,
2719 &de_old, rec->ur_tgt,
2720 rec->ur_tgtlen, &de_new,
2721 dlm_handles, LCK_EX);
2725 cleanup_phase = 1; /* parent(s), children, locks */
2727 if (de_new->d_inode)
2730 /* sanity check for src inode */
2731 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2732 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2733 GOTO(cleanup, rc = -EINVAL);
2735 /* sanity check for dest inode */
2736 if (de_new->d_inode &&
2737 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2738 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2739 GOTO(cleanup, rc = -EINVAL);
2741 if (de_old->d_inode == de_new->d_inode)
2742 GOTO(cleanup, rc = 0);
2744 /* if we are about to remove the target at first, pass the EA of
2745 * that inode to client to perform and cleanup on OST */
2746 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2747 LASSERT(body != NULL);
2749 if (de_new->d_inode &&
2750 S_ISREG(de_new->d_inode->i_mode) &&
2751 de_new->d_inode->i_nlink == 1 &&
2752 mds_open_orphan_count(de_new->d_inode) == 0) {
2753 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2754 mds_pack_inode2body(obd, body, de_new->d_inode);
2755 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2756 if (!(body->valid & OBD_MD_FLEASIZE)) {
2757 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2758 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2760 /* XXX need log unlink? */
2764 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2765 de_srcdir->d_inode->i_sb);
2767 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2769 GOTO(cleanup, rc = PTR_ERR(handle));
2771 /* FIXME need adjust the journal block count? */
2772 /* if the target should be moved to PENDING, we at first increase the
2773 * link and later vfs_rename() will decrease the link count again */
2774 if (de_new->d_inode &&
2775 S_ISREG(de_new->d_inode->i_mode) &&
2776 de_new->d_inode->i_nlink == 1 &&
2777 mds_open_orphan_count(de_new->d_inode) > 0) {
2778 rc = mds_add_link_orphan(rec, obd, de_new);
2784 de_old->d_fsdata = req;
2785 de_new->d_fsdata = req;
2786 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2791 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2792 handle, req, rc, 0);
2793 switch (cleanup_phase) {
2796 if (dlm_handles[5].cookie != 0)
2797 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2798 if (dlm_handles[6].cookie != 0)
2799 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2802 if (lock_count == 4)
2803 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2804 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2805 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2806 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2808 if (lock_count == 4)
2809 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2810 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2811 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2812 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2821 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2824 req->rq_status = rc;
2828 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2829 struct ptlrpc_request *, struct lustre_handle *);
2831 static mds_reinter reinters[REINT_MAX + 1] = {
2832 [REINT_SETATTR] mds_reint_setattr,
2833 [REINT_CREATE] mds_reint_create,
2834 [REINT_LINK] mds_reint_link,
2835 [REINT_UNLINK] mds_reint_unlink,
2836 [REINT_RENAME] mds_reint_rename,
2837 [REINT_OPEN] mds_open
2840 int mds_reint_rec(struct mds_update_record *rec, int offset,
2841 struct ptlrpc_request *req, struct lustre_handle *lockh)
2843 struct obd_device *obd = req->rq_export->exp_obd;
2844 struct lvfs_run_ctxt saved;
2847 /* checked by unpacker */
2848 LASSERT(rec->ur_opcode <= REINT_MAX &&
2849 reinters[rec->ur_opcode] != NULL);
2851 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2852 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2853 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);