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 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
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;
284 struct llog_create_locks *lcl;
286 char str[PTL_NALFMT_SIZE];
289 /* CAVEAT EMPTOR: spinlock order */
290 spin_lock_irqsave (&exp->exp_lock, flags);
291 list_for_each (tmp, &exp->exp_outstanding_replies) {
292 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
294 if (oldrep->rs_xid != req->rq_xid)
297 if (oldrep->rs_msg.opc != req->rq_reqmsg->opc)
298 CERROR ("Resent req xid "LPX64" has mismatched opc: "
299 "new %d old %d\n", req->rq_xid,
300 req->rq_reqmsg->opc, oldrep->rs_msg.opc);
302 svc = oldrep->rs_srv_ni->sni_service;
303 spin_lock (&svc->srv_lock);
305 list_del_init (&oldrep->rs_exp_list);
307 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
309 oldrep->rs_nlocks, oldrep,
310 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg.opc,
311 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
313 for (i = 0; i < oldrep->rs_nlocks; i++)
314 ptlrpc_save_lock(req,
315 &oldrep->rs_locks[i],
316 oldrep->rs_modes[i]);
317 oldrep->rs_nlocks = 0;
319 lcl = oldrep->rs_llog_locks;
320 oldrep->rs_llog_locks = NULL;
322 ptlrpc_save_llog_lock(req, lcl);
324 DEBUG_REQ(D_HA, req, "stole locks for");
325 ptlrpc_schedule_difficult_reply (oldrep);
327 spin_unlock (&svc->srv_lock);
328 spin_unlock_irqrestore (&exp->exp_lock, flags);
331 spin_unlock_irqrestore (&exp->exp_lock, flags);
334 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
336 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
337 mcd->mcd_last_transno, mcd->mcd_last_result);
338 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
339 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
341 mds_steal_ack_locks(req);
344 static void reconstruct_reint_setattr(struct mds_update_record *rec,
345 int offset, struct ptlrpc_request *req)
347 struct mds_export_data *med = &req->rq_export->exp_mds_data;
348 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
350 struct mds_body *body;
352 mds_req_from_mcd(req, med->med_mcd);
354 de = mds_fid2dentry(obd, rec->ur_fid1, NULL);
356 LASSERT(PTR_ERR(de) == req->rq_status);
360 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
361 mds_pack_inode2fid(req2obd(req), &body->fid1, de->d_inode);
362 mds_pack_inode2body(req2obd(req), body, de->d_inode);
364 /* Don't return OST-specific attributes if we didn't just set them */
365 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
366 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
367 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
368 body->valid |= OBD_MD_FLMTIME;
369 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
370 body->valid |= OBD_MD_FLATIME;
375 /* In the raw-setattr case, we lock the child inode.
376 * In the write-back case or if being called from open, the client holds a lock
379 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
380 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
381 struct ptlrpc_request *req,
382 struct lustre_handle *lh)
384 struct mds_obd *mds = mds_req2mds(req);
385 struct obd_device *obd = req->rq_export->exp_obd;
386 struct mds_body *body;
388 struct inode *inode = NULL;
389 struct lustre_handle lockh[2] = {{0}, {0}};
392 struct mds_logcancel_data *mlcd = NULL;
393 int rc = 0, cleanup_phase = 0, err, locked = 0;
396 LASSERT(offset == 1);
398 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x", rec->ur_fid1->id,
399 rec->ur_fid1->generation, rec->ur_iattr.ia_valid);
401 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
403 MDS_UPDATE_COUNTER(mds, MDS_SETATTR_COUNT);
405 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
406 de = mds_fid2dentry(mds, rec->ur_fid1, NULL);
408 GOTO(cleanup, rc = PTR_ERR(de));
410 __u64 lockpart = MDS_INODELOCK_UPDATE;
411 if (rec->ur_iattr.ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID) )
412 lockpart |= MDS_INODELOCK_LOOKUP;
413 de = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW,
414 lockh, &parent_mode, NULL, 0, lockpart);
416 GOTO(cleanup, rc = PTR_ERR(de));
424 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
425 rec->ur_eadata != NULL)
428 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
430 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
432 GOTO(cleanup, rc = PTR_ERR(handle));
434 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
435 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
436 LTIME_S(rec->ur_iattr.ia_mtime),
437 LTIME_S(rec->ur_iattr.ia_ctime));
438 rc = mds_fix_attr(inode, rec);
442 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
443 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
444 (long)&rec->ur_iattr.ia_attr_flags);
446 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
448 if (rc == 0 && (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
449 rec->ur_eadata != NULL) {
450 struct lov_stripe_md *lsm = NULL;
452 rc = ll_permission(inode, MAY_WRITE, NULL);
456 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
457 mds->mds_osc_exp, 0, &lsm, rec->ur_eadata);
461 obd_free_memmd(mds->mds_osc_exp, &lsm);
463 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
469 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
470 mds_pack_inode2fid(obd, &body->fid1, inode);
471 mds_pack_inode2body(obd, body, inode);
473 /* Don't return OST-specific attributes if we didn't just set them */
474 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
475 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
476 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
477 body->valid |= OBD_MD_FLMTIME;
478 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
479 body->valid |= OBD_MD_FLATIME;
481 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_osc_obd)) {
482 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
485 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
487 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
488 mlcd->mlcd_cookielen = rec->ur_cookielen;
489 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
490 mlcd->mlcd_cookielen;
491 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
492 mlcd->mlcd_cookielen);
493 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
494 mlcd->mlcd_eadatalen);
496 CERROR("unable to allocate log cancel data\n");
502 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
503 handle, mds_cancel_cookies_cb, mlcd);
504 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
505 switch (cleanup_phase) {
507 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
508 rec->ur_eadata != NULL)
513 if (lockh[1].cookie != 0)
514 ldlm_lock_decref(lockh + 1, parent_mode);
517 ldlm_lock_decref(lockh, LCK_PW);
519 ptlrpc_save_lock (req, lockh, LCK_PW);
534 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
535 struct ptlrpc_request *req)
537 struct mds_export_data *med = &req->rq_export->exp_mds_data;
538 struct mds_obd *obd = &req->rq_export->exp_obd->u.mds;
539 struct dentry *parent, *child;
540 struct mds_body *body;
543 mds_req_from_mcd(req, med->med_mcd);
545 if (req->rq_status) {
550 parent = mds_fid2dentry(obd, rec->ur_fid1, NULL);
551 LASSERT(!IS_ERR(parent));
552 child = ll_lookup_one_len(rec->ur_name, parent, rec->ur_namelen - 1);
553 LASSERT(!IS_ERR(child));
554 if ((child->d_flags & DCACHE_CROSS_REF)) {
555 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
556 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
557 mds_pack_dentry2fid(&body->fid1, child);
558 mds_pack_dentry2body(body, child);
559 body->valid |= OBD_MD_MDS;
560 } else if (child->d_inode == NULL) {
561 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
562 rec->ur_fid1->id, rec->ur_fid1->generation,
563 rec->ur_name, rec->ur_mode);
564 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
566 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
567 mds_pack_inode2fid(req2obd(req), &body->fid1, child->d_inode);
568 mds_pack_inode2body(req2obd(req), body, child->d_inode);
575 static int mds_reint_create(struct mds_update_record *rec, int offset,
576 struct ptlrpc_request *req,
577 struct lustre_handle *lh)
579 struct dentry *dparent = NULL;
580 struct mds_obd *mds = mds_req2mds(req);
581 struct obd_device *obd = req->rq_export->exp_obd;
582 struct dentry *dchild = NULL;
583 struct inode *dir = NULL;
585 struct lustre_handle lockh[2] = {{0}, {0}};
587 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
589 struct dentry_params dp;
590 struct mea *mea = NULL;
594 LASSERT(offset == 1);
595 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name, LUSTRE_MDS_NAME));
597 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
598 rec->ur_fid1->id, rec->ur_fid1->generation,
599 rec->ur_name, rec->ur_mode);
601 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
604 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
605 GOTO(cleanup, rc = -ESTALE);
607 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL, LCK_PW, lockh,
608 &parent_mode, rec->ur_name,
609 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
610 if (IS_ERR(dparent)) {
611 rc = PTR_ERR(dparent);
612 CERROR("parent lookup error %d\n", rc);
615 cleanup_phase = 1; /* locked parent dentry */
616 dir = dparent->d_inode;
619 ldlm_lock_dump_handle(D_OTHER, lockh);
621 /* try to retrieve MEA data for this dir */
622 rc = mds_get_lmv_attr(obd, dparent->d_inode, &mea, &mea_size);
627 /* dir is already splitted, check is requested filename
628 * should live at this MDS or at another one */
630 i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
631 if (mea->mea_master != mea->mea_fids[i].mds) {
632 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
633 " should be %d(%d)\n",
634 mea->mea_master, dparent->d_inode->i_ino,
635 dparent->d_inode->i_generation, rec->ur_name,
636 mea->mea_fids[i].mds, i);
637 GOTO(cleanup, rc = -ERESTART);
641 dchild = ll_lookup_one_len(rec->ur_name, dparent, rec->ur_namelen - 1);
642 if (IS_ERR(dchild)) {
643 rc = PTR_ERR(dchild);
644 CERROR("child lookup error %d\n", rc);
648 cleanup_phase = 2; /* child dentry */
650 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
652 if (type == S_IFREG || type == S_IFDIR) {
653 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
654 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
655 obd->obd_name, dparent->d_inode->i_ino,
656 dparent->d_inode->i_generation, rc, parent_mode);
658 /* dir got splitted */
659 GOTO(cleanup, rc = -ERESTART);
661 /* error happened during spitting. */
666 if (dir->i_mode & S_ISGID) {
667 if (S_ISDIR(rec->ur_mode))
668 rec->ur_mode |= S_ISGID;
671 dchild->d_fsdata = (void *)&dp;
672 dp.p_inum = (unsigned long)rec->ur_fid2->id;
677 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
679 GOTO(cleanup, rc = PTR_ERR(handle));
680 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
688 /* as Peter asked, mkdir() should distribute new directories
689 * over the whole cluster in order to distribute namespace
690 * processing load. first, we calculate which MDS to use to put
691 * new directory's inode in. */
692 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
694 if (i == mds->mds_num) {
695 /* inode will be created locally */
696 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
698 GOTO(cleanup, rc = PTR_ERR(handle));
700 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
703 nstripes = *(u16 *)rec->ur_eadata;
705 if (rc == 0 && nstripes) {
706 /* we pass LCK_EX to split routine to
707 * signalthat we have exclusive access
708 * to the directory. simple because
709 * nobody knows it already exists -bzzz */
710 rc = mds_try_to_split_dir(obd, dchild,
714 /* dir got splitted */
717 /* an error occured during
722 } else if (!DENTRY_VALID(dchild)) {
723 /* inode will be created on another MDS */
724 struct obdo *oa = NULL;
725 struct mds_body *body;
727 /* first, create that inode */
732 if (rec->ur_eadata) {
733 /* user asks for creating splitted dir */
734 oa->o_easize = *((u16 *) rec->ur_eadata);
737 obdo_from_inode(oa, dir, OBD_MD_FLTYPE | OBD_MD_FLATIME |
738 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
739 OBD_MD_FLUID | OBD_MD_FLGID);
740 oa->o_mode = dir->i_mode;
741 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
743 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
744 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
745 obd->obd_name, rec->ur_namelen - 1,
746 rec->ur_name, (unsigned) rec->ur_fid2->id,
747 (unsigned) rec->ur_fid2->generation);
748 oa->o_id = rec->ur_fid2->id;
749 oa->o_generation = rec->ur_fid2->generation;
750 oa->o_flags |= OBD_FL_RECREATE_OBJS;
753 rc = obd_create(mds->mds_lmv_exp, oa, NULL, NULL);
755 CERROR("can't create remote inode: %d\n", rc);
756 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
757 rec->ur_fid1->id, rec->ur_fid1->generation,
758 rec->ur_name, rec->ur_mode);
763 /* now, add new dir entry for it */
764 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
765 if (IS_ERR(handle)) {
767 GOTO(cleanup, rc = PTR_ERR(handle));
769 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
771 oa->o_id, oa->o_generation,
776 body = lustre_msg_buf(req->rq_repmsg, 0,
778 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
779 body->fid1.id = oa->o_id;
781 body->fid1.generation = oa->o_generation;
784 /* requested name exists in the directory */
791 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
793 GOTO(cleanup, rc = PTR_ERR(handle));
794 if (rec->ur_tgt == NULL) /* no target supplied */
795 rc = -EINVAL; /* -EPROTO? */
797 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
805 int rdev = rec->ur_rdev;
806 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
808 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
809 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
814 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
815 dchild->d_fsdata = NULL;
816 GOTO(cleanup, rc = -EINVAL);
819 /* In case we stored the desired inum in here, we want to clean up. */
820 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
821 dchild->d_fsdata = NULL;
824 CDEBUG(D_INODE, "error during create: %d\n", rc);
826 } else if (dchild->d_inode) {
828 struct inode *inode = dchild->d_inode;
829 struct mds_body *body;
832 LTIME_S(iattr.ia_atime) = rec->ur_time;
833 LTIME_S(iattr.ia_ctime) = rec->ur_time;
834 LTIME_S(iattr.ia_mtime) = rec->ur_time;
835 iattr.ia_uid = rec->_ur_fsuid;
836 if (dir->i_mode & S_ISGID)
837 iattr.ia_gid = dir->i_gid;
839 iattr.ia_gid = rec->_ur_fsgid;
840 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
841 ATTR_MTIME | ATTR_CTIME;
843 if (rec->ur_fid2->id) {
844 LASSERT(rec->ur_fid2->id == inode->i_ino);
845 inode->i_generation = rec->ur_fid2->generation;
846 /* Dirtied and committed by the upcoming setattr. */
847 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
848 inode->i_ino, inode->i_generation);
851 struct lustre_handle child_ino_lockh;
854 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
855 inode->i_ino, inode->i_generation);
858 /* The inode we were allocated may have just been freed
859 * by an unlink operation. We take this lock to
860 * synchronize against the matching reply-ack-lock taken
861 * in unlink, to avoid replay problems if this reply
862 * makes it out to the client but the unlink's does not.
863 * See bug 2029 for more detail.*/
864 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
865 if (rc != ELDLM_OK) {
866 CERROR("error locking for unlink/create sync: "
869 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
874 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
876 CERROR("error on child setattr: rc = %d\n", rc);
878 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
879 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
881 CERROR("error on parent setattr: rc = %d\n", rc);
883 MDS_UPDATE_COUNTER(mds, MDS_CREATE_COUNT);
885 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
886 mds_pack_inode2fid(obd, &body->fid1, inode);
887 mds_pack_inode2body(obd, body, inode);
892 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
895 /* Destroy the file we just created. This should not need extra
896 * journal credits, as we have already modified all of the
897 * blocks needed in order to create the file in the first
901 err = vfs_rmdir(dir, dchild);
903 CERROR("rmdir in error path: %d\n", err);
906 err = vfs_unlink(dir, dchild);
908 CERROR("unlink in error path: %d\n", err);
914 switch (cleanup_phase) {
915 case 2: /* child dentry */
917 case 1: /* locked parent dentry */
919 if (lockh[1].cookie != 0)
920 ldlm_lock_decref(lockh + 1, parent_mode);
923 ldlm_lock_decref(lockh, LCK_PW);
925 ptlrpc_save_lock(req, lockh, LCK_PW);
931 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
935 OBD_FREE(mea, mea_size);
940 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
941 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
945 for (i = 0; i < RES_NAME_SIZE; i++) {
946 /* return 1 here, because enqueue_ordered will skip resources
947 * of all zeroes if they're sorted to the end of the list. */
948 if (res1->name[i] == 0 && res2->name[i] != 0)
950 if (res2->name[i] == 0 && res1->name[i] != 0)
953 if (res1->name[i] > res2->name[i])
955 if (res1->name[i] < res2->name[i])
962 if (memcmp(p1, p2, sizeof(*p1)) < 0)
968 /* This function doesn't use ldlm_match_or_enqueue because we're always called
969 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
970 * because they take the place of local semaphores.
972 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
973 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
974 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
975 struct lustre_handle *p1_lockh, int p1_lock_mode,
976 ldlm_policy_data_t *p1_policy,
977 struct ldlm_res_id *p2_res_id,
978 struct lustre_handle *p2_lockh, int p2_lock_mode,
979 ldlm_policy_data_t *p2_policy)
981 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
982 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
983 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
984 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
988 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
990 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
993 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
994 handles[1] = p1_lockh;
995 handles[0] = p2_lockh;
996 res_id[1] = p1_res_id;
997 res_id[0] = p2_res_id;
998 lock_modes[1] = p1_lock_mode;
999 lock_modes[0] = p2_lock_mode;
1000 policies[1] = p1_policy;
1001 policies[0] = p2_policy;
1004 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1005 res_id[0]->name[0], res_id[1]->name[0]);
1007 flags = LDLM_FL_LOCAL_ONLY;
1008 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1009 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1010 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1011 NULL, 0, NULL, handles[0]);
1014 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1016 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1017 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1018 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1019 ldlm_lock_addref(handles[1], lock_modes[1]);
1020 } else if (res_id[1]->name[0] != 0) {
1021 flags = LDLM_FL_LOCAL_ONLY;
1022 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1023 *res_id[1], LDLM_IBITS, policies[1],
1024 lock_modes[1], &flags, mds_blocking_ast,
1025 ldlm_completion_ast, NULL, NULL, NULL, 0,
1027 if (rc != ELDLM_OK) {
1028 ldlm_lock_decref(handles[0], lock_modes[0]);
1031 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1037 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1038 struct lustre_handle *p1_lockh, int p1_lock_mode,
1039 ldlm_policy_data_t *p1_policy,
1040 struct ldlm_res_id *p2_res_id,
1041 struct lustre_handle *p2_lockh, int p2_lock_mode,
1042 ldlm_policy_data_t *p2_policy,
1043 struct ldlm_res_id *c1_res_id,
1044 struct lustre_handle *c1_lockh, int c1_lock_mode,
1045 ldlm_policy_data_t *c1_policy,
1046 struct ldlm_res_id *c2_res_id,
1047 struct lustre_handle *c2_lockh, int c2_lock_mode,
1048 ldlm_policy_data_t *c2_policy)
1050 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1051 c1_res_id, c2_res_id };
1052 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1053 c1_lockh, c2_lockh };
1054 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1055 c1_lock_mode, c2_lock_mode };
1056 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1057 c1_policy, c2_policy};
1058 int rc, i, j, sorted, flags;
1062 "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1063 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1064 res_id[3]->name[0]);
1066 /* simple insertion sort - we have at most 4 elements */
1067 for (i = 1; i < 4; i++) {
1069 dlm_handles[4] = dlm_handles[i];
1070 res_id[4] = res_id[i];
1071 lock_modes[4] = lock_modes[i];
1072 policies[4] = policies[i];
1076 if (res_gt(res_id[j], res_id[4], policies[j],
1078 dlm_handles[j + 1] = dlm_handles[j];
1079 res_id[j + 1] = res_id[j];
1080 lock_modes[j + 1] = lock_modes[j];
1081 policies[j + 1] = policies[j];
1086 } while (j >= 0 && !sorted);
1088 dlm_handles[j + 1] = dlm_handles[4];
1089 res_id[j + 1] = res_id[4];
1090 lock_modes[j + 1] = lock_modes[4];
1091 policies[j + 1] = policies[4];
1095 "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1096 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1097 res_id[3]->name[0]);
1099 /* XXX we could send ASTs on all these locks first before blocking? */
1100 for (i = 0; i < 4; i++) {
1102 if (res_id[i]->name[0] == 0)
1105 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1106 (policies[i]->l_inodebits.bits &
1107 policies[i-1]->l_inodebits.bits) ) {
1108 memcpy(dlm_handles[i], dlm_handles[i-1],
1109 sizeof(*(dlm_handles[i])));
1110 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1112 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1113 *res_id[i], LDLM_IBITS,
1115 lock_modes[i], &flags,
1117 ldlm_completion_ast, NULL, NULL,
1118 NULL, 0, NULL, dlm_handles[i]);
1120 GOTO(out_err, rc = -EIO);
1121 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1128 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1133 /* In the unlikely case that the child changed while we were waiting
1134 * on the lock, we need to drop the lock on the old child and either:
1135 * - if the child has a lower resource name, then we have to also
1136 * drop the parent lock and regain the locks in the right order
1137 * - in the rename case, if the child has a lower resource name than one of
1138 * the other parent/child resources (maxres) we also need to reget the locks
1139 * - if the child has a higher resource name (this is the common case)
1140 * we can just get the lock on the new child (still in lock order)
1142 * Returns 0 if the child did not change or if it changed but could be locked.
1143 * Returns 1 if the child changed and we need to re-lock (no locks held).
1144 * Returns -ve error with a valid dchild (no locks held). */
1145 static int mds_verify_child(struct obd_device *obd,
1146 struct ldlm_res_id *parent_res_id,
1147 struct lustre_handle *parent_lockh,
1148 struct dentry *dparent, int parent_mode,
1149 struct ldlm_res_id *child_res_id,
1150 struct lustre_handle *child_lockh,
1151 struct dentry **dchildp, int child_mode,
1152 ldlm_policy_data_t *child_policy,
1153 const char *name, int namelen,
1154 struct ldlm_res_id *maxres)
1156 struct dentry *vchild, *dchild = *dchildp;
1157 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1160 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1162 GOTO(cleanup, rc = PTR_ERR(vchild));
1164 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1165 if (child_res_id->name[0] == vchild->d_inum &&
1166 child_res_id->name[1] == vchild->d_generation) {
1175 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1176 (vchild->d_inode != NULL &&
1177 child_res_id->name[0] == vchild->d_inode->i_ino &&
1178 child_res_id->name[1] == vchild->d_inode->i_generation))) {
1187 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1188 vchild->d_inode, dchild ? dchild->d_inode : 0,
1189 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1190 child_res_id->name[0]);
1191 if (child_res_id->name[0] != 0)
1192 ldlm_lock_decref(child_lockh, child_mode);
1196 cleanup_phase = 1; /* parent lock only */
1197 *dchildp = dchild = vchild;
1199 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1201 if (dchild->d_inode) {
1202 child_res_id->name[0] = dchild->d_inode->i_ino;
1203 child_res_id->name[1] = dchild->d_inode->i_generation;
1205 child_res_id->name[0] = dchild->d_inum;
1206 child_res_id->name[1] = dchild->d_generation;
1209 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1210 res_gt(maxres, child_res_id, NULL, NULL)) {
1211 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1212 child_res_id->name[0], parent_res_id->name[0],
1214 GOTO(cleanup, rc = 1);
1217 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1218 *child_res_id, LDLM_IBITS, child_policy,
1219 child_mode, &flags, mds_blocking_ast,
1220 ldlm_completion_ast, NULL, NULL, NULL, 0,
1223 GOTO(cleanup, rc = -EIO);
1226 memset(child_res_id, 0, sizeof(*child_res_id));
1232 switch(cleanup_phase) {
1234 if (child_res_id->name[0] != 0)
1235 ldlm_lock_decref(child_lockh, child_mode);
1237 ldlm_lock_decref(parent_lockh, parent_mode);
1243 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1245 struct lustre_handle *parent_lockh,
1246 struct dentry **dparentp, int parent_mode,
1247 __u64 parent_lockpart, int *update_mode,
1248 char *name, int namelen,
1249 struct lustre_handle *child_lockh,
1250 struct dentry **dchildp, int child_mode,
1251 __u64 child_lockpart, void *clone_info)
1253 struct ldlm_res_id child_res_id = { .name = {0} };
1254 struct ldlm_res_id parent_res_id = { .name = {0} };
1255 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1256 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1257 struct inode *inode;
1258 int rc = 0, cleanup_phase = 0;
1261 /* Step 1: Lookup parent */
1262 *dparentp = mds_fid2dentry(mds, fid, NULL);
1263 if (IS_ERR(*dparentp)) {
1264 rc = PTR_ERR(*dparentp);
1269 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1270 (*dparentp)->d_inode->i_ino, name);
1272 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1273 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1275 parent_lockh[1].cookie = 0;
1276 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1277 struct ldlm_res_id res_id = { .name = {0} };
1278 ldlm_policy_data_t policy;
1280 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1282 res_id.name[0] = (*dparentp)->d_inode->i_ino;
1283 res_id.name[1] = (*dparentp)->d_inode->i_generation;
1284 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1285 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1286 res_id, LDLM_IBITS, &policy,
1287 *update_mode, &flags,
1289 ldlm_completion_ast,
1290 NULL, NULL, NULL, 0, NULL,
1296 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1297 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1298 (*dparentp)->d_inode->i_ino,
1299 (*dparentp)->d_inode->i_generation,
1300 parent_res_id.name[2]);
1304 cleanup_phase = 1; /* parent dentry */
1305 #ifdef CONFIG_SNAPFS
1307 /*FIXME is there any other FUNC will use d_fsdata,
1308 *excepet creating inode according inum*/
1309 (*dparentp)->d_fsdata = clone_info;
1313 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1314 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1315 if (IS_ERR(*dchildp)) {
1316 rc = PTR_ERR(*dchildp);
1317 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1321 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1322 /* inode lives on another MDS: return * mds/ino/gen
1323 * and LOOKUP lock. drop possible UPDATE lock! */
1324 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1325 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1326 child_res_id.name[0] = (*dchildp)->d_inum;
1327 child_res_id.name[1] = (*dchildp)->d_generation;
1331 inode = (*dchildp)->d_inode;
1333 inode = igrab(inode);
1337 child_res_id.name[0] = inode->i_ino;
1338 child_res_id.name[1] = inode->i_generation;
1343 cleanup_phase = 2; /* child dentry */
1345 /* Step 3: Lock parent and child in resource order. If child doesn't
1346 * exist, we still have to lock the parent and re-lookup. */
1347 rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1348 &parent_policy, &child_res_id, child_lockh,
1349 child_mode, &child_policy);
1353 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1354 cleanup_phase = 4; /* child lock */
1356 cleanup_phase = 3; /* parent lock */
1358 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1359 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1360 parent_mode, &child_res_id, child_lockh,
1361 dchildp, child_mode, &child_policy,
1362 name, namelen, &parent_res_id);
1372 switch (cleanup_phase) {
1374 ldlm_lock_decref(child_lockh, child_mode);
1376 ldlm_lock_decref(parent_lockh, parent_mode);
1381 if (parent_lockh[1].cookie)
1382 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1391 void mds_reconstruct_generic(struct ptlrpc_request *req)
1393 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1395 mds_req_from_mcd(req, med->med_mcd);
1398 int mds_create_local_dentry(struct mds_update_record *rec,
1399 struct obd_device *obd)
1401 struct mds_obd *mds = &obd->u.mds;
1402 struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1403 int fidlen = 0, rc, cleanup_phase = 0;
1404 struct dentry *new_child = NULL;
1405 char *fidname = rec->ur_name;
1406 struct dentry *child = NULL;
1407 struct lustre_handle lockh[2] = {{0}, {0}};
1411 down(&fids_dir->i_sem);
1412 fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1413 CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1414 fidname, (unsigned) rec->ur_fid1->id,
1415 (unsigned) rec->ur_fid1->generation);
1417 new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1418 up(&fids_dir->i_sem);
1419 if (IS_ERR(new_child)) {
1420 CERROR("can't lookup %s: %d\n", fidname,
1421 (int) PTR_ERR(new_child));
1422 GOTO(cleanup, rc = PTR_ERR(new_child));
1426 if (new_child->d_inode != NULL) {
1427 /* nice. we've already have local dentry! */
1428 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1429 (unsigned) new_child->d_inode->i_ino,
1430 (unsigned) new_child->d_inode->i_generation);
1431 rec->ur_fid1->id = fids_dir->i_ino;
1432 rec->ur_fid1->generation = fids_dir->i_generation;
1433 rec->ur_namelen = fidlen + 1;
1434 GOTO(cleanup, rc = 0);
1437 /* new, local dentry will be added soon. we need no aliases here */
1440 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1441 child = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1443 child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1444 LCK_EX, lockh, NULL, NULL, 0,
1445 MDS_INODELOCK_UPDATE);
1448 if (IS_ERR(child)) {
1449 rc = PTR_ERR(child);
1450 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1451 CERROR("can't get victim: %d\n", rc);
1456 handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1458 GOTO(cleanup, rc = PTR_ERR(handle));
1460 rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1461 rec->ur_fid1->id, rec->ur_fid1->generation,
1464 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1465 (unsigned long) child->d_inode->i_ino,
1466 (unsigned long) child->d_inode->i_generation, rc);
1468 if (S_ISDIR(child->d_inode->i_mode)) {
1469 fids_dir->i_nlink++;
1470 mark_inode_dirty(fids_dir);
1472 mark_inode_dirty(child->d_inode);
1474 fsfilt_commit(obd, mds->mds_sb, fids_dir, handle, 0);
1476 rec->ur_fid1->id = fids_dir->i_ino;
1477 rec->ur_fid1->generation = fids_dir->i_generation;
1478 rec->ur_namelen = fidlen + 1;
1481 switch(cleanup_phase) {
1483 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1484 ldlm_lock_decref(lockh, LCK_EX);
1494 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1495 struct ptlrpc_request *slave)
1497 void *cookie, *cookie2;
1498 struct mds_body *body2;
1499 struct mds_body *body;
1503 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1504 LASSERT(body != NULL);
1506 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1507 LASSERT(body2 != NULL);
1509 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1513 memcpy(body2, body, sizeof(*body));
1514 body2->valid &= ~OBD_MD_FLCOOKIE;
1516 if (!(body->valid & OBD_MD_FLEASIZE) &&
1517 !(body->valid & OBD_MD_FLDIREA))
1520 if (body->eadatasize == 0) {
1521 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1525 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1527 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1528 LASSERT(ea != NULL);
1530 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1531 LASSERT(ea2 != NULL);
1533 memcpy(ea2, ea, body->eadatasize);
1535 if (body->valid & OBD_MD_FLCOOKIE) {
1536 LASSERT(master->rq_repmsg->buflens[2] >=
1537 slave->rq_repmsg->buflens[2]);
1538 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1539 slave->rq_repmsg->buflens[2]);
1540 LASSERT(cookie != NULL);
1542 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1543 master->rq_repmsg->buflens[2]);
1544 LASSERT(cookie2 != NULL);
1545 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1546 body2->valid |= OBD_MD_FLCOOKIE;
1551 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1552 struct ptlrpc_request *req,
1553 struct lustre_handle *parent_lockh,
1555 struct dentry *dparent,
1556 struct lustre_handle *child_lockh,
1557 struct dentry *dchild)
1559 struct obd_device *obd = req->rq_export->exp_obd;
1560 struct mds_obd *mds = mds_req2mds(req);
1561 struct mdc_op_data op_data;
1562 int rc = 0, cleanup_phase = 0;
1563 struct ptlrpc_request *request = NULL;
1567 LASSERT(offset == 1 || offset == 3);
1569 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)",
1570 rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1571 (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1572 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1573 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode %u/%u/%u)",
1574 rec->ur_namelen - 1, rec->ur_name,
1575 (unsigned)dchild->d_mdsnum,
1576 (unsigned) dchild->d_inum,
1577 (unsigned) dchild->d_generation);
1579 /* time to drop i_nlink on remote MDS */
1580 memset(&op_data, 0, sizeof(op_data));
1581 op_data.fid1.mds = dchild->d_mdsnum;
1582 op_data.fid1.id = dchild->d_inum;
1583 op_data.fid1.generation = dchild->d_generation;
1584 op_data.create_mode = rec->ur_mode;
1585 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1586 op_data.create_mode |= MDS_MODE_REPLAY;
1587 rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1591 mds_copy_unlink_reply(req, request);
1592 ptlrpc_req_finished(request);
1595 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1598 GOTO(cleanup, rc = PTR_ERR(handle));
1599 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1600 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1604 req->rq_status = rc;
1607 if (parent_lockh[1].cookie != 0)
1608 ldlm_lock_decref(parent_lockh + 1, update_mode);
1610 ldlm_lock_decref(child_lockh, LCK_EX);
1612 ldlm_lock_decref(parent_lockh, LCK_PW);
1614 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1621 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1622 struct ptlrpc_request *req,
1623 struct lustre_handle *lh)
1625 struct dentry *dparent, *dchild;
1626 struct mds_obd *mds = mds_req2mds(req);
1627 struct obd_device *obd = req->rq_export->exp_obd;
1628 struct mds_body *body = NULL;
1629 struct inode *child_inode;
1630 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1631 struct lustre_handle child_lockh = {0};
1633 struct lustre_handle child_reuse_lockh = {0};
1635 struct lustre_handle * slave_lockh = NULL;
1636 struct llog_create_locks *lcl = NULL;
1637 char fidname[LL_FID_NAMELEN];
1638 void *handle = NULL;
1639 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1640 int unlink_by_fid = 0;
1644 LASSERT(offset == 1 || offset == 3);
1646 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1647 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1649 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1651 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1652 DEBUG_REQ(D_HA, req, "unlink replay\n");
1653 LASSERT(offset == 1); /* should not come from intent */
1654 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
1655 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
1656 req->rq_repmsg->buflens[2]);
1659 MDS_UPDATE_COUNTER(mds, MDS_UNLINK_COUNT);
1661 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1662 GOTO(cleanup, rc = -ENOENT);
1664 if (rec->ur_namelen == 1) {
1665 /* this is request to drop i_nlink on local inode */
1667 rec->ur_name = fidname;
1668 rc = mds_create_local_dentry(rec, obd);
1669 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1670 DEBUG_REQ(D_HA, req,
1671 "drop nlink on inode %u/%u/%u (replay)",
1672 (unsigned) rec->ur_fid1->mds,
1673 (unsigned) rec->ur_fid1->id,
1674 (unsigned) rec->ur_fid1->generation);
1680 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1681 /* master mds for directory asks slave removing
1682 * inode is already locked */
1683 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1684 LCK_PW, parent_lockh,
1685 &update_mode, rec->ur_name,
1687 MDS_INODELOCK_UPDATE);
1688 if (IS_ERR(dparent))
1689 GOTO(cleanup, rc = PTR_ERR(dparent));
1690 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1691 rec->ur_namelen - 1);
1693 GOTO(cleanup, rc = PTR_ERR(dchild));
1694 child_lockh.cookie = 0;
1695 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1696 LASSERT(dchild->d_inode != NULL);
1697 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1699 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1700 parent_lockh, &dparent,
1701 LCK_PW, MDS_INODELOCK_UPDATE,
1702 &update_mode, rec->ur_name,
1703 rec->ur_namelen, &child_lockh,
1705 MDS_INODELOCK_LOOKUP |
1706 MDS_INODELOCK_UPDATE, NULL);
1711 if (dchild->d_flags & DCACHE_CROSS_REF) {
1712 /* we should have parent lock only here */
1713 LASSERT(unlink_by_fid == 0);
1714 LASSERT(dchild->d_mdsnum != mds->mds_num);
1715 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1716 update_mode, dparent, &child_lockh, dchild);
1720 cleanup_phase = 1; /* dchild, dparent, locks */
1723 child_inode = dchild->d_inode;
1724 if (child_inode == NULL) {
1725 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1726 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1727 GOTO(cleanup, rc = -ENOENT);
1730 cleanup_phase = 2; /* dchild has a lock */
1732 /* We have to do these checks ourselves, in case we are making an
1733 * orphan. The client tells us whether rmdir() or unlink() was called,
1734 * so we need to return appropriate errors (bug 72).
1736 * We don't have to check permissions, because vfs_rename (called from
1737 * mds_open_unlink_rename) also calls may_delete. */
1738 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1739 if (!S_ISDIR(child_inode->i_mode))
1740 GOTO(cleanup, rc = -ENOTDIR);
1742 if (S_ISDIR(child_inode->i_mode))
1743 GOTO(cleanup, rc = -EISDIR);
1746 /* handle splitted dir */
1747 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
1752 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1753 * reuse (see bug 2029). */
1754 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1758 cleanup_phase = 3; /* child inum lock */
1760 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1762 /* ldlm_reply in buf[0] if called via intent */
1768 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1769 LASSERT(body != NULL);
1771 /* If this is the last reference to this inode, get the OBD EA
1772 * data first so the client can destroy OST objects.
1773 * we only do the object removal if no open files remain.
1774 * Nobody can get at this name anymore because of the locks so
1775 * we make decisions here as to whether to remove the inode */
1776 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1777 mds_open_orphan_count(child_inode) == 0) {
1778 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1779 mds_pack_inode2body(obd, body, child_inode);
1780 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1782 if (!(body->valid & OBD_MD_FLEASIZE)) {
1783 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1784 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1790 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1791 switch (child_inode->i_mode & S_IFMT) {
1793 /* Drop any lingering child directories before we start our
1794 * transaction, to avoid doing multiple inode dirty/delete
1795 * in our compound transaction (bug 1321). */
1796 shrink_dcache_parent(dchild);
1797 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1800 GOTO(cleanup, rc = PTR_ERR(handle));
1801 cleanup_phase = 4; /* transaction */
1802 rc = vfs_rmdir(dparent->d_inode, dchild);
1805 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1806 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1808 handle = fsfilt_start_log(obd, dparent->d_inode,
1809 FSFILT_OP_UNLINK, NULL,
1810 le32_to_cpu(lmm->lmm_stripe_count));
1812 GOTO(cleanup, rc = PTR_ERR(handle));
1814 cleanup_phase = 4; /* transaction */
1815 rc = vfs_unlink(dparent->d_inode, dchild);
1817 if (!rc && log_unlink)
1818 if (mds_log_op_unlink(obd, child_inode,
1819 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1820 req->rq_repmsg->buflens[offset + 1],
1821 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1822 req->rq_repmsg->buflens[offset + 2], &lcl) > 0)
1823 body->valid |= OBD_MD_FLCOOKIE;
1831 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1834 GOTO(cleanup, rc = PTR_ERR(handle));
1835 cleanup_phase = 4; /* transaction */
1836 rc = vfs_unlink(dparent->d_inode, dchild);
1839 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1842 GOTO(cleanup, rc = -EINVAL);
1850 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1851 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1852 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1854 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1856 CERROR("error on parent setattr: rc = %d\n", err);
1859 switch(cleanup_phase) {
1861 LASSERT(dchild != NULL && dchild->d_inode != NULL);
1862 LASSERT(atomic_read(&dchild->d_inode->i_count) > 0);
1863 if (rc == 0 && dchild->d_inode->i_nlink == 0 &&
1864 mds_open_orphan_count(dchild->d_inode) > 0) {
1866 /* filesystem is really going to destroy an inode
1867 * we have to delay this till inode is opened -bzzz */
1868 mds_open_unlink_rename(rec, obd, dparent, dchild, NULL);
1870 /* handle splitted dir */
1872 /* master directory can be non-empty or something else ... */
1873 mds_unlink_slave_objs(obd, dchild);
1875 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1878 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1879 "unlinked", 0, NULL);
1881 ptlrpc_save_llog_lock(req, lcl);
1882 case 3: /* child ino-reuse lock */
1884 if (rc && body != NULL) {
1885 // Don't unlink the OST objects if the MDS unlink failed
1889 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1891 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1893 case 2: /* child lock */
1894 mds_unlock_slave_objs(obd, dchild, slave_lockh);
1895 if (child_lockh.cookie)
1896 ldlm_lock_decref(&child_lockh, LCK_EX);
1897 case 1: /* child and parent dentry, parent lock */
1899 if (parent_lockh[1].cookie != 0)
1900 ldlm_lock_decref(parent_lockh + 1, update_mode);
1903 ldlm_lock_decref(parent_lockh, LCK_PW);
1905 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1912 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1915 req->rq_status = rc;
1920 * to service requests from remote MDS to increment i_nlink
1922 static int mds_reint_link_acquire(struct mds_update_record *rec,
1923 int offset, struct ptlrpc_request *req,
1924 struct lustre_handle *lh)
1926 struct obd_device *obd = req->rq_export->exp_obd;
1927 struct ldlm_res_id src_res_id = { .name = {0} };
1928 struct lustre_handle *handle = NULL, src_lockh = {0};
1929 struct mds_obd *mds = mds_req2mds(req);
1930 int rc = 0, cleanup_phase = 0;
1931 struct dentry *de_src = NULL;
1932 ldlm_policy_data_t policy;
1936 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1937 obd->obd_name, (unsigned) rec->ur_fid1->mds,
1938 (unsigned) rec->ur_fid1->id,
1939 (unsigned) rec->ur_fid1->generation);
1941 /* Step 1: Lookup the source inode and target directory by FID */
1942 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1944 GOTO(cleanup, rc = PTR_ERR(de_src));
1945 cleanup_phase = 1; /* source dentry */
1947 src_res_id.name[0] = de_src->d_inode->i_ino;
1948 src_res_id.name[1] = de_src->d_inode->i_generation;
1949 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1951 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1952 src_res_id, LDLM_IBITS, &policy,
1953 LCK_EX, &flags, mds_blocking_ast,
1954 ldlm_completion_ast, NULL, NULL,
1955 NULL, 0, NULL, &src_lockh);
1957 GOTO(cleanup, rc = -ENOLCK);
1958 cleanup_phase = 2; /* lock */
1960 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1962 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1963 if (IS_ERR(handle)) {
1964 rc = PTR_ERR(handle);
1967 de_src->d_inode->i_nlink++;
1968 mark_inode_dirty(de_src->d_inode);
1971 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1972 handle, req, rc, 0);
1974 switch (cleanup_phase) {
1977 ldlm_lock_decref(&src_lockh, LCK_EX);
1979 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1985 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1988 req->rq_status = rc;
1993 * request to link to foreign inode:
1994 * - acquire i_nlinks on this inode
1997 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1998 int offset, struct ptlrpc_request *req,
1999 struct lustre_handle *lh)
2001 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2002 struct obd_device *obd = req->rq_export->exp_obd;
2003 struct dentry *de_tgt_dir = NULL;
2004 struct mds_obd *mds = mds_req2mds(req);
2005 int rc = 0, cleanup_phase = 0;
2006 struct mdc_op_data op_data;
2007 struct ptlrpc_request *request = NULL;
2011 #define fmt "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
2012 DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
2013 (unsigned) rec->ur_fid2->mds,
2014 (unsigned) rec->ur_fid2->id,
2015 (unsigned) rec->ur_fid2->generation,
2016 rec->ur_namelen - 1, rec->ur_name,
2017 (unsigned) rec->ur_fid1->mds,
2018 (unsigned) rec->ur_fid1->id,
2019 (unsigned)rec->ur_fid1->generation);
2021 de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
2022 tgt_dir_lockh, &update_mode,
2023 rec->ur_name, rec->ur_namelen - 1,
2024 MDS_INODELOCK_UPDATE);
2025 if (IS_ERR(de_tgt_dir))
2026 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2029 op_data.fid1 = *(rec->ur_fid1);
2030 op_data.namelen = 0;
2031 op_data.name = NULL;
2032 rc = md_link(mds->mds_lmv_exp, &op_data, &request);
2038 ptlrpc_req_finished(request);
2040 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2042 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2043 if (IS_ERR(handle)) {
2044 rc = PTR_ERR(handle);
2048 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2049 rec->ur_namelen - 1, rec->ur_fid1->id,
2050 rec->ur_fid1->generation, rec->ur_fid1->mds);
2054 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2055 handle, req, rc, 0);
2058 switch (cleanup_phase) {
2061 /* FIXME: drop i_nlink on remote inode here */
2062 CERROR("MUST drop drop i_nlink here\n");
2067 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2069 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2072 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2074 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2080 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2083 req->rq_status = rc;
2087 static int mds_reint_link(struct mds_update_record *rec, int offset,
2088 struct ptlrpc_request *req,
2089 struct lustre_handle *lh)
2091 struct obd_device *obd = req->rq_export->exp_obd;
2092 struct dentry *de_src = NULL;
2093 struct dentry *de_tgt_dir = NULL;
2094 struct dentry *dchild = NULL;
2095 struct mds_obd *mds = mds_req2mds(req);
2096 struct lustre_handle *handle = NULL;
2097 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2098 struct ldlm_res_id src_res_id = { .name = {0} };
2099 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2100 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2101 ldlm_policy_data_t tgt_dir_policy =
2102 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2103 int rc = 0, cleanup_phase = 0;
2104 int update_mode = 0;
2107 LASSERT(offset == 1);
2109 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2110 rec->ur_fid1->id, rec->ur_fid1->generation,
2111 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
2113 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2115 MDS_UPDATE_COUNTER(mds, MDS_LINK_COUNT);
2117 // memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2118 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2119 GOTO(cleanup, rc = -ENOENT);
2121 if (rec->ur_fid1->mds != mds->mds_num) {
2122 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2126 if (rec->ur_namelen == 1) {
2127 rc = mds_reint_link_acquire(rec, offset, req, lh);
2131 /* Step 1: Lookup the source inode and target directory by FID */
2132 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
2134 GOTO(cleanup, rc = PTR_ERR(de_src));
2136 cleanup_phase = 1; /* source dentry */
2138 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
2139 if (IS_ERR(de_tgt_dir)) {
2140 rc = PTR_ERR(de_tgt_dir);
2145 cleanup_phase = 2; /* target directory dentry */
2147 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2148 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
2149 de_src->d_inode->i_ino);
2151 /* Step 2: Take the two locks */
2152 src_res_id.name[0] = de_src->d_inode->i_ino;
2153 src_res_id.name[1] = de_src->d_inode->i_generation;
2154 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
2155 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
2157 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2159 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2161 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2162 tgt_dir_res_id, LDLM_IBITS,
2163 &src_policy, update_mode, &flags,
2165 ldlm_completion_ast, NULL, NULL,
2166 NULL, 0, NULL, tgt_dir_lockh + 1);
2168 GOTO(cleanup, rc = -ENOLCK);
2171 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2172 rec->ur_namelen - 1);
2173 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
2174 de_tgt_dir->d_inode->i_ino,
2175 de_tgt_dir->d_inode->i_generation,
2176 tgt_dir_res_id.name[2]);
2179 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2181 &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
2186 cleanup_phase = 3; /* locks */
2188 /* Step 3: Lookup the child */
2189 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
2190 if (IS_ERR(dchild)) {
2191 rc = PTR_ERR(dchild);
2192 if (rc != -EPERM && rc != -EACCES)
2193 CERROR("child lookup error %d\n", rc);
2197 cleanup_phase = 4; /* child dentry */
2199 if (dchild->d_inode) {
2200 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2201 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2206 /* Step 4: Do it. */
2207 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2209 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2210 if (IS_ERR(handle)) {
2211 rc = PTR_ERR(handle);
2215 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2216 if (rc && rc != -EPERM && rc != -EACCES)
2217 CERROR("vfs_link error %d\n", rc);
2219 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2220 handle, req, rc, 0);
2223 switch (cleanup_phase) {
2224 case 4: /* child dentry */
2228 ldlm_lock_decref(&src_lockh, LCK_EX);
2229 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2231 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2232 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2234 case 2: /* target dentry */
2236 if (tgt_dir_lockh[1].cookie && update_mode)
2237 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2241 case 1: /* source dentry */
2246 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2249 req->rq_status = rc;
2254 * add a hard link in the PENDING directory, only used by rename()
2256 static int mds_add_link_orphan(struct mds_update_record *rec,
2257 struct obd_device *obd,
2258 struct dentry *dentry)
2260 struct mds_obd *mds = &obd->u.mds;
2261 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2262 struct dentry *pending_child;
2263 char fidname[LL_FID_NAMELEN];
2267 LASSERT(dentry->d_inode);
2268 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2270 down(&pending_dir->i_sem);
2271 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2272 dentry->d_inode->i_generation);
2274 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2275 mds_open_orphan_count(dentry->d_inode),
2276 rec->ur_name, fidname);
2278 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2279 if (IS_ERR(pending_child))
2280 GOTO(out_lock, rc = PTR_ERR(pending_child));
2282 if (pending_child->d_inode != NULL) {
2283 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2284 LASSERT(pending_child->d_inode == dentry->d_inode);
2285 GOTO(out_dput, rc = 0);
2289 rc = vfs_link(dentry, pending_dir, pending_child);
2292 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2295 mds_inode_set_orphan(dentry->d_inode);
2297 l_dput(pending_child);
2299 up(&pending_dir->i_sem);
2303 /* The idea here is that we need to get four locks in the end:
2304 * one on each parent directory, one on each child. We need to take
2305 * these locks in some kind of order (to avoid deadlocks), and the order
2306 * I selected is "increasing resource number" order. We need to look up
2307 * the children, however, before we know what the resource number(s) are.
2308 * Thus the following plan:
2310 * 1,2. Look up the parents
2311 * 3,4. Look up the children
2312 * 5. Take locks on the parents and children, in order
2313 * 6. Verify that the children haven't changed since they were looked up
2315 * If there was a race and the children changed since they were first looked
2316 * up, it is possible that mds_verify_child() will be able to just grab the
2317 * lock on the new child resource (if it has a higher resource than any other)
2318 * but we need to compare against not only its parent, but also against the
2319 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2321 * We need the fancy igrab() on the child inodes because we aren't holding a
2322 * lock on the parent after the lookup is done, so dentry->d_inode may change
2323 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2325 static int mds_get_parents_children_locked(struct obd_device *obd,
2326 struct mds_obd *mds,
2327 struct ll_fid *p1_fid,
2328 struct dentry **de_srcdirp,
2329 struct ll_fid *p2_fid,
2330 struct dentry **de_tgtdirp,
2332 const char *old_name, int old_len,
2333 struct dentry **de_oldp,
2334 const char *new_name, int new_len,
2335 struct dentry **de_newp,
2336 struct lustre_handle *dlm_handles,
2339 struct ldlm_res_id p1_res_id = { .name = {0} };
2340 struct ldlm_res_id p2_res_id = { .name = {0} };
2341 struct ldlm_res_id c1_res_id = { .name = {0} };
2342 struct ldlm_res_id c2_res_id = { .name = {0} };
2343 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2344 /* Only dentry should change, but the inode itself would be
2346 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2347 /* If something is going to be replaced, both dentry and inode locks are
2349 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2350 MDS_INODELOCK_UPDATE}};
2351 struct ldlm_res_id *maxres_src, *maxres_tgt;
2352 struct inode *inode;
2353 int rc = 0, cleanup_phase = 0;
2356 /* Step 1: Lookup the source directory */
2357 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2358 if (IS_ERR(*de_srcdirp))
2359 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2361 cleanup_phase = 1; /* source directory dentry */
2363 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2364 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2366 /* Step 2: Lookup the target directory */
2367 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2368 *de_tgtdirp = dget(*de_srcdirp);
2370 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2371 if (IS_ERR(*de_tgtdirp)) {
2372 rc = PTR_ERR(*de_tgtdirp);
2378 cleanup_phase = 2; /* target directory dentry */
2380 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2381 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2384 dlm_handles[5].cookie = 0;
2385 dlm_handles[6].cookie = 0;
2386 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2387 /* Get a temp lock on just ino, gen to flush client cache and
2388 * to protect dirs from concurrent splitting */
2389 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2390 LCK_PW, &p_policy, &p2_res_id,
2391 &(dlm_handles[6]),LCK_PW,&p_policy);
2394 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2395 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2396 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2397 (*de_srcdirp)->d_inode->i_ino,
2398 (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2399 (*de_tgtdirp)->d_inode->i_ino,
2400 (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2405 /* Step 3: Lookup the source child entry */
2406 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2407 if (IS_ERR(*de_oldp)) {
2408 rc = PTR_ERR(*de_oldp);
2409 CERROR("old child lookup error (%*s): %d\n",
2410 old_len - 1, old_name, rc);
2414 cleanup_phase = 4; /* original name dentry */
2416 inode = (*de_oldp)->d_inode;
2417 if (inode != NULL) {
2418 inode = igrab(inode);
2420 GOTO(cleanup, rc = -ENOENT);
2422 c1_res_id.name[0] = inode->i_ino;
2423 c1_res_id.name[1] = inode->i_generation;
2425 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2426 c1_res_id.name[0] = (*de_oldp)->d_inum;
2427 c1_res_id.name[1] = (*de_oldp)->d_generation;
2430 /* Step 4: Lookup the target child entry */
2431 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2432 if (IS_ERR(*de_newp)) {
2433 rc = PTR_ERR(*de_newp);
2434 CERROR("new child lookup error (%*s): %d\n",
2435 old_len - 1, old_name, rc);
2439 cleanup_phase = 5; /* target dentry */
2441 inode = (*de_newp)->d_inode;
2442 if (inode != NULL) {
2443 inode = igrab(inode);
2447 c2_res_id.name[0] = inode->i_ino;
2448 c2_res_id.name[1] = inode->i_generation;
2450 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2451 c2_res_id.name[0] = (*de_newp)->d_inum;
2452 c2_res_id.name[1] = (*de_newp)->d_generation;
2456 /* Step 5: Take locks on the parents and child(ren) */
2457 maxres_src = &p1_res_id;
2458 maxres_tgt = &p2_res_id;
2459 cleanup_phase = 5; /* target dentry */
2461 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2462 maxres_src = &c1_res_id;
2463 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2464 maxres_tgt = &c2_res_id;
2466 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2468 &p2_res_id, &dlm_handles[1], parent_mode,
2470 &c1_res_id, &dlm_handles[2], child_mode,
2472 &c2_res_id, &dlm_handles[3], child_mode,
2477 cleanup_phase = 6; /* parent and child(ren) locks */
2479 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2480 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2481 parent_mode, &c1_res_id, &dlm_handles[2],
2482 de_oldp, child_mode, &c1_policy, old_name,old_len,
2485 if (c2_res_id.name[0] != 0)
2486 ldlm_lock_decref(&dlm_handles[3], child_mode);
2487 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2494 if (!DENTRY_VALID(*de_oldp))
2495 GOTO(cleanup, rc = -ENOENT);
2497 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2498 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2499 parent_mode, &c2_res_id, &dlm_handles[3],
2500 de_newp, child_mode, &c2_policy, new_name,
2501 new_len, maxres_src);
2503 ldlm_lock_decref(&dlm_handles[2], child_mode);
2504 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2514 switch (cleanup_phase) {
2515 case 6: /* child lock(s) */
2516 if (c2_res_id.name[0] != 0)
2517 ldlm_lock_decref(&dlm_handles[3], child_mode);
2518 if (c1_res_id.name[0] != 0)
2519 ldlm_lock_decref(&dlm_handles[2], child_mode);
2520 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2521 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2522 case 5: /* target dentry */
2524 case 4: /* source dentry */
2528 if (dlm_handles[5].cookie != 0)
2529 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2530 if (dlm_handles[6].cookie != 0)
2531 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2533 case 2: /* target directory dentry */
2534 l_dput(*de_tgtdirp);
2535 case 1: /* source directry dentry */
2536 l_dput(*de_srcdirp);
2543 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2544 int offset, struct ptlrpc_request *req)
2546 struct obd_device *obd = req->rq_export->exp_obd;
2547 struct dentry *de_srcdir = NULL;
2548 struct dentry *de_new = NULL;
2549 struct mds_obd *mds = mds_req2mds(req);
2550 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2551 struct lustre_handle child_lockh = {0};
2552 int cleanup_phase = 0;
2553 void *handle = NULL;
2554 int update_mode, rc = 0;
2557 /* another MDS executing rename operation has asked us
2558 * to create target name. such a creation should destroy
2559 * existing target name */
2561 CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2562 obd->obd_name, rec->ur_tgt,
2563 (unsigned long) rec->ur_fid1->mds,
2564 (unsigned long) rec->ur_fid1->id,
2565 (unsigned long) rec->ur_fid1->generation);
2567 /* first, lookup the target */
2568 child_lockh.cookie = 0;
2569 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2570 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2571 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2572 &child_lockh, &de_new, LCK_EX,
2573 MDS_INODELOCK_LOOKUP, NULL);
2580 LASSERT(de_srcdir->d_inode);
2583 if (de_new->d_inode) {
2584 /* name exists and points to local inode
2585 * try to unlink this name and create new one */
2586 CERROR("%s: %s points to local inode %lu/%lu\n",
2587 obd->obd_name, rec->ur_tgt,
2588 (unsigned long) de_new->d_inode->i_ino,
2589 (unsigned long) de_new->d_inode->i_generation);
2590 handle = fsfilt_start(obd, de_srcdir->d_inode,
2591 FSFILT_OP_RENAME, NULL);
2593 GOTO(cleanup, rc = PTR_ERR(handle));
2594 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2597 } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2598 /* name exists adn points to remove inode */
2599 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2600 obd->obd_name, rec->ur_tgt,
2601 (unsigned long) de_new->d_mdsnum,
2602 (unsigned long) de_new->d_inum,
2603 (unsigned long) de_new->d_generation);
2605 /* name doesn't exist. the simplest case */
2606 handle = fsfilt_start(obd, de_srcdir->d_inode,
2607 FSFILT_OP_LINK, NULL);
2609 GOTO(cleanup, rc = PTR_ERR(handle));
2613 rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2614 rec->ur_tgtlen - 1, rec->ur_fid1->id,
2615 rec->ur_fid1->generation, rec->ur_fid1->mds);
2617 CERROR("add_dir_entry() returned error %d\n", rc);
2620 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2621 handle, req, rc, 0);
2622 switch(cleanup_phase) {
2626 if (parent_lockh[1].cookie != 0)
2627 ldlm_lock_decref(&parent_lockh[1], update_mode);
2629 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2630 if (child_lockh.cookie != 0)
2631 ldlm_lock_decref(&child_lockh, LCK_EX);
2639 req->rq_status = rc;
2644 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2645 struct ptlrpc_request *req)
2647 struct obd_device *obd = req->rq_export->exp_obd;
2648 struct ptlrpc_request *req2 = NULL;
2649 struct dentry *de_srcdir = NULL;
2650 struct dentry *de_old = NULL;
2651 struct mds_obd *mds = mds_req2mds(req);
2652 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2653 struct lustre_handle child_lockh = {0};
2654 struct mdc_op_data opdata;
2655 void *handle = NULL;
2656 int update_mode, rc = 0;
2659 CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2660 obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2661 memset(&opdata, 0, sizeof(opdata));
2663 child_lockh.cookie = 0;
2664 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2665 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2666 &update_mode, rec->ur_name,
2667 rec->ur_namelen, &child_lockh, &de_old,
2668 LCK_EX, MDS_INODELOCK_LOOKUP, NULL);
2671 LASSERT(de_srcdir->d_inode);
2674 /* we already know the target should be created on another MDS
2675 * so, we have to request that MDS to do it */
2677 /* prepare source fid */
2678 if (de_old->d_flags & DCACHE_CROSS_REF) {
2679 LASSERT(de_old->d_inode == NULL);
2680 CDEBUG(D_OTHER, "request to move remote name\n");
2681 opdata.fid1.mds = de_old->d_mdsnum;
2682 opdata.fid1.id = de_old->d_inum;
2683 opdata.fid1.generation = de_old->d_generation;
2684 } else if (de_old->d_inode == NULL) {
2685 /* oh, source doesn't exist */
2686 GOTO(cleanup, rc = -ENOENT);
2688 LASSERT(de_old->d_inode != NULL);
2689 CDEBUG(D_OTHER, "request to move local name\n");
2690 opdata.fid1.mds = mds->mds_num;
2691 opdata.fid1.id = de_old->d_inode->i_ino;
2692 opdata.fid1.generation = de_old->d_inode->i_generation;
2695 opdata.fid2 = *(rec->ur_fid2);
2696 rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2697 rec->ur_tgtlen - 1, &req2);
2702 handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2704 GOTO(cleanup, rc = PTR_ERR(handle));
2705 rc = fsfilt_del_dir_entry(obd, de_old);
2710 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2711 handle, req, rc, 0);
2713 ptlrpc_req_finished(req2);
2716 if (parent_lockh[1].cookie != 0)
2717 ldlm_lock_decref(&parent_lockh[1], update_mode);
2719 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2720 if (child_lockh.cookie != 0)
2721 ldlm_lock_decref(&child_lockh, LCK_EX);
2726 req->rq_status = rc;
2731 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2732 struct ptlrpc_request *req, struct lustre_handle *lockh)
2734 struct obd_device *obd = req->rq_export->exp_obd;
2735 struct dentry *de_srcdir = NULL;
2736 struct dentry *de_tgtdir = NULL;
2737 struct dentry *de_old = NULL;
2738 struct dentry *de_new = NULL;
2739 struct mds_obd *mds = mds_req2mds(req);
2740 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
2741 struct mds_body *body = NULL;
2742 struct llog_create_locks *lcl = NULL;
2743 int rc = 0, log_unlink = 0, lock_count = 3;
2744 int cleanup_phase = 0;
2745 void *handle = NULL;
2748 LASSERT(offset == 1);
2750 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2751 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2752 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2754 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2756 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2757 DEBUG_REQ(D_HA, req, "rename replay\n");
2758 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2759 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
2760 req->rq_repmsg->buflens[2]);
2763 MDS_UPDATE_COUNTER(mds, MDS_RENAME_COUNT);
2765 if (rec->ur_namelen == 1) {
2766 rc = mds_reint_rename_create_name(rec, offset, req);
2770 if (rec->ur_fid2->mds != mds->mds_num) {
2771 rc = mds_reint_rename_to_remote(rec, offset, req);
2775 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2776 rec->ur_fid2, &de_tgtdir, LCK_PW,
2777 rec->ur_name, rec->ur_namelen,
2778 &de_old, rec->ur_tgt,
2779 rec->ur_tgtlen, &de_new,
2780 dlm_handles, LCK_EX);
2784 cleanup_phase = 1; /* parent(s), children, locks */
2786 if (de_new->d_inode)
2789 /* sanity check for src inode */
2790 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2791 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2792 GOTO(cleanup, rc = -EINVAL);
2794 /* sanity check for dest inode */
2795 if (de_new->d_inode &&
2796 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2797 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2798 GOTO(cleanup, rc = -EINVAL);
2800 if (de_old->d_inode == de_new->d_inode)
2801 GOTO(cleanup, rc = 0);
2803 /* if we are about to remove the target at first, pass the EA of
2804 * that inode to client to perform and cleanup on OST */
2805 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2806 LASSERT(body != NULL);
2808 if (de_new->d_inode &&
2809 S_ISREG(de_new->d_inode->i_mode) &&
2810 de_new->d_inode->i_nlink == 1 &&
2811 mds_open_orphan_count(de_new->d_inode) == 0) {
2812 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2813 mds_pack_inode2body(obd, body, de_new->d_inode);
2814 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2815 if (!(body->valid & OBD_MD_FLEASIZE)) {
2816 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2817 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2823 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2824 de_srcdir->d_inode->i_sb);
2826 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2828 GOTO(cleanup, rc = PTR_ERR(handle));
2830 /* FIXME need adjust the journal block count? */
2831 /* if the target should be moved to PENDING, we at first increase the
2832 * link and later vfs_rename() will decrease the link count again */
2833 if (de_new->d_inode &&
2834 S_ISREG(de_new->d_inode->i_mode) &&
2835 de_new->d_inode->i_nlink == 1 &&
2836 mds_open_orphan_count(de_new->d_inode) > 0) {
2837 rc = mds_add_link_orphan(rec, obd, de_new);
2843 de_old->d_fsdata = req;
2844 de_new->d_fsdata = req;
2845 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2848 if (!rc && log_unlink) {
2849 if (mds_log_op_unlink(obd, de_tgtdir->d_inode,
2850 lustre_msg_buf(req->rq_repmsg, 1, 0),
2851 req->rq_repmsg->buflens[1],
2852 lustre_msg_buf(req->rq_repmsg, 2, 0),
2853 req->rq_repmsg->buflens[2], &lcl) > 0) {
2854 body->valid |= OBD_MD_FLCOOKIE;
2860 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2861 handle, req, rc, 0);
2862 switch (cleanup_phase) {
2865 if (dlm_handles[5].cookie != 0)
2866 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2867 if (dlm_handles[6].cookie != 0)
2868 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2871 ptlrpc_save_llog_lock(req, lcl);
2874 if (lock_count == 4)
2875 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2876 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2877 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2878 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2880 if (lock_count == 4)
2881 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2882 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2883 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2884 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2893 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2896 req->rq_status = rc;
2900 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2901 struct ptlrpc_request *, struct lustre_handle *);
2903 static mds_reinter reinters[REINT_MAX + 1] = {
2904 [REINT_SETATTR] mds_reint_setattr,
2905 [REINT_CREATE] mds_reint_create,
2906 [REINT_LINK] mds_reint_link,
2907 [REINT_UNLINK] mds_reint_unlink,
2908 [REINT_RENAME] mds_reint_rename,
2909 [REINT_OPEN] mds_open
2912 int mds_reint_rec(struct mds_update_record *rec, int offset,
2913 struct ptlrpc_request *req, struct lustre_handle *lockh)
2915 struct obd_device *obd = req->rq_export->exp_obd;
2916 struct lvfs_run_ctxt saved;
2919 /* checked by unpacker */
2920 LASSERT(rec->ur_opcode <= REINT_MAX &&
2921 reinters[rec->ur_opcode] != NULL);
2923 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2924 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2925 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);