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 == 0);
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");
556 body = lustre_msg_buf(req->rq_repmsg, offset, 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");
566 body = lustre_msg_buf(req->rq_repmsg, offset, 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 == 0);
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);
759 /* now, add new dir entry for it */
760 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
761 if (IS_ERR(handle)) {
763 GOTO(cleanup, rc = PTR_ERR(handle));
765 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
767 oa->o_id, oa->o_generation,
772 body = lustre_msg_buf(req->rq_repmsg,
773 offset, sizeof (*body));
774 body->valid |= OBD_MD_FLID | OBD_MD_MDS;
775 body->fid1.id = oa->o_id;
777 body->fid1.generation = oa->o_generation;
780 /* requested name exists in the directory */
787 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
789 GOTO(cleanup, rc = PTR_ERR(handle));
790 if (rec->ur_tgt == NULL) /* no target supplied */
791 rc = -EINVAL; /* -EPROTO? */
793 rc = vfs_symlink(dir, dchild, rec->ur_tgt);
801 int rdev = rec->ur_rdev;
802 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
804 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
805 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
810 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
811 dchild->d_fsdata = NULL;
812 GOTO(cleanup, rc = -EINVAL);
815 /* In case we stored the desired inum in here, we want to clean up. */
816 if (dchild->d_fsdata == (void *)(unsigned long)rec->ur_fid2->id)
817 dchild->d_fsdata = NULL;
820 CDEBUG(D_INODE, "error during create: %d\n", rc);
822 } else if (dchild->d_inode) {
824 struct inode *inode = dchild->d_inode;
825 struct mds_body *body;
828 LTIME_S(iattr.ia_atime) = LTIME_S(rec->ur_time);
829 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
830 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
831 iattr.ia_uid = rec->_ur_fsuid;
832 if (dir->i_mode & S_ISGID)
833 iattr.ia_gid = dir->i_gid;
835 iattr.ia_gid = rec->_ur_fsgid;
836 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
837 ATTR_MTIME | ATTR_CTIME;
839 if (rec->ur_fid2->id) {
840 LASSERT(rec->ur_fid2->id == inode->i_ino);
841 inode->i_generation = rec->ur_fid2->generation;
842 /* Dirtied and committed by the upcoming setattr. */
843 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
844 inode->i_ino, inode->i_generation);
847 struct lustre_handle child_ino_lockh;
850 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
851 inode->i_ino, inode->i_generation);
854 /* The inode we were allocated may have just been freed
855 * by an unlink operation. We take this lock to
856 * synchronize against the matching reply-ack-lock taken
857 * in unlink, to avoid replay problems if this reply
858 * makes it out to the client but the unlink's does not.
859 * See bug 2029 for more detail.*/
860 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
861 if (rc != ELDLM_OK) {
862 CERROR("error locking for unlink/create sync: "
865 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
870 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
872 CERROR("error on child setattr: rc = %d\n", rc);
874 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
875 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
877 CERROR("error on parent setattr: rc = %d\n", rc);
879 MDS_UPDATE_COUNTER(mds, MDS_CREATE_COUNT);
881 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
882 mds_pack_inode2fid(obd, &body->fid1, inode);
883 mds_pack_inode2body(obd, body, inode);
888 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
891 /* Destroy the file we just created. This should not need extra
892 * journal credits, as we have already modified all of the
893 * blocks needed in order to create the file in the first
897 err = vfs_rmdir(dir, dchild);
899 CERROR("rmdir in error path: %d\n", err);
902 err = vfs_unlink(dir, dchild);
904 CERROR("unlink in error path: %d\n", err);
910 switch (cleanup_phase) {
911 case 2: /* child dentry */
913 case 1: /* locked parent dentry */
915 if (lockh[1].cookie != 0)
916 ldlm_lock_decref(lockh + 1, parent_mode);
919 ldlm_lock_decref(lockh, LCK_PW);
921 ptlrpc_save_lock(req, lockh, LCK_PW);
927 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
931 OBD_FREE(mea, mea_size);
936 static int res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
937 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
941 for (i = 0; i < RES_NAME_SIZE; i++) {
942 /* return 1 here, because enqueue_ordered will skip resources
943 * of all zeroes if they're sorted to the end of the list. */
944 if (res1->name[i] == 0 && res2->name[i] != 0)
946 if (res2->name[i] == 0 && res1->name[i] != 0)
949 if (res1->name[i] > res2->name[i])
951 if (res1->name[i] < res2->name[i])
958 if (memcmp(p1, p2, sizeof(*p1)) < 0)
964 /* This function doesn't use ldlm_match_or_enqueue because we're always called
965 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
966 * because they take the place of local semaphores.
968 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
969 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
970 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
971 struct lustre_handle *p1_lockh, int p1_lock_mode,
972 ldlm_policy_data_t *p1_policy,
973 struct ldlm_res_id *p2_res_id,
974 struct lustre_handle *p2_lockh, int p2_lock_mode,
975 ldlm_policy_data_t *p2_policy)
977 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
978 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
979 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
980 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
984 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
986 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n", res_id[0]->name[0],
989 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
990 handles[1] = p1_lockh;
991 handles[0] = p2_lockh;
992 res_id[1] = p1_res_id;
993 res_id[0] = p2_res_id;
994 lock_modes[1] = p1_lock_mode;
995 lock_modes[0] = p2_lock_mode;
996 policies[1] = p1_policy;
997 policies[0] = p2_policy;
1000 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1001 res_id[0]->name[0], res_id[1]->name[0]);
1003 flags = LDLM_FL_LOCAL_ONLY;
1004 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1005 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1006 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1007 NULL, 0, NULL, handles[0]);
1010 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1012 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1013 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1014 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1015 ldlm_lock_addref(handles[1], lock_modes[1]);
1016 } else if (res_id[1]->name[0] != 0) {
1017 flags = LDLM_FL_LOCAL_ONLY;
1018 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1019 *res_id[1], LDLM_IBITS, policies[1],
1020 lock_modes[1], &flags, mds_blocking_ast,
1021 ldlm_completion_ast, NULL, NULL, NULL, 0,
1023 if (rc != ELDLM_OK) {
1024 ldlm_lock_decref(handles[0], lock_modes[0]);
1027 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1033 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1034 struct lustre_handle *p1_lockh, int p1_lock_mode,
1035 ldlm_policy_data_t *p1_policy,
1036 struct ldlm_res_id *p2_res_id,
1037 struct lustre_handle *p2_lockh, int p2_lock_mode,
1038 ldlm_policy_data_t *p2_policy,
1039 struct ldlm_res_id *c1_res_id,
1040 struct lustre_handle *c1_lockh, int c1_lock_mode,
1041 ldlm_policy_data_t *c1_policy,
1042 struct ldlm_res_id *c2_res_id,
1043 struct lustre_handle *c2_lockh, int c2_lock_mode,
1044 ldlm_policy_data_t *c2_policy)
1046 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1047 c1_res_id, c2_res_id };
1048 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1049 c1_lockh, c2_lockh };
1050 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1051 c1_lock_mode, c2_lock_mode };
1052 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1053 c1_policy, c2_policy};
1054 int rc, i, j, sorted, flags;
1058 "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1059 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1060 res_id[3]->name[0]);
1062 /* simple insertion sort - we have at most 4 elements */
1063 for (i = 1; i < 4; i++) {
1065 dlm_handles[4] = dlm_handles[i];
1066 res_id[4] = res_id[i];
1067 lock_modes[4] = lock_modes[i];
1068 policies[4] = policies[i];
1072 if (res_gt(res_id[j], res_id[4], policies[j],
1074 dlm_handles[j + 1] = dlm_handles[j];
1075 res_id[j + 1] = res_id[j];
1076 lock_modes[j + 1] = lock_modes[j];
1077 policies[j + 1] = policies[j];
1082 } while (j >= 0 && !sorted);
1084 dlm_handles[j + 1] = dlm_handles[4];
1085 res_id[j + 1] = res_id[4];
1086 lock_modes[j + 1] = lock_modes[4];
1087 policies[j + 1] = policies[4];
1091 "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1092 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1093 res_id[3]->name[0]);
1095 /* XXX we could send ASTs on all these locks first before blocking? */
1096 for (i = 0; i < 4; i++) {
1098 if (res_id[i]->name[0] == 0)
1101 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1102 (policies[i]->l_inodebits.bits &
1103 policies[i-1]->l_inodebits.bits) ) {
1104 memcpy(dlm_handles[i], dlm_handles[i-1],
1105 sizeof(*(dlm_handles[i])));
1106 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1108 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1109 *res_id[i], LDLM_IBITS,
1111 lock_modes[i], &flags,
1113 ldlm_completion_ast, NULL, NULL,
1114 NULL, 0, NULL, dlm_handles[i]);
1116 GOTO(out_err, rc = -EIO);
1117 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1124 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1129 /* In the unlikely case that the child changed while we were waiting
1130 * on the lock, we need to drop the lock on the old child and either:
1131 * - if the child has a lower resource name, then we have to also
1132 * drop the parent lock and regain the locks in the right order
1133 * - in the rename case, if the child has a lower resource name than one of
1134 * the other parent/child resources (maxres) we also need to reget the locks
1135 * - if the child has a higher resource name (this is the common case)
1136 * we can just get the lock on the new child (still in lock order)
1138 * Returns 0 if the child did not change or if it changed but could be locked.
1139 * Returns 1 if the child changed and we need to re-lock (no locks held).
1140 * Returns -ve error with a valid dchild (no locks held). */
1141 static int mds_verify_child(struct obd_device *obd,
1142 struct ldlm_res_id *parent_res_id,
1143 struct lustre_handle *parent_lockh,
1144 struct dentry *dparent, int parent_mode,
1145 struct ldlm_res_id *child_res_id,
1146 struct lustre_handle *child_lockh,
1147 struct dentry **dchildp, int child_mode,
1148 ldlm_policy_data_t *child_policy,
1149 const char *name, int namelen,
1150 struct ldlm_res_id *maxres)
1152 struct dentry *vchild, *dchild = *dchildp;
1153 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1156 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1158 GOTO(cleanup, rc = PTR_ERR(vchild));
1160 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1161 if (child_res_id->name[0] == vchild->d_inum &&
1162 child_res_id->name[1] == vchild->d_generation) {
1171 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1172 (vchild->d_inode != NULL &&
1173 child_res_id->name[0] == vchild->d_inode->i_ino &&
1174 child_res_id->name[1] == vchild->d_inode->i_generation))) {
1183 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1184 vchild->d_inode, dchild ? dchild->d_inode : 0,
1185 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1186 child_res_id->name[0]);
1187 if (child_res_id->name[0] != 0)
1188 ldlm_lock_decref(child_lockh, child_mode);
1192 cleanup_phase = 1; /* parent lock only */
1193 *dchildp = dchild = vchild;
1195 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1197 if (dchild->d_inode) {
1198 child_res_id->name[0] = dchild->d_inode->i_ino;
1199 child_res_id->name[1] = dchild->d_inode->i_generation;
1201 child_res_id->name[0] = dchild->d_inum;
1202 child_res_id->name[1] = dchild->d_generation;
1205 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1206 res_gt(maxres, child_res_id, NULL, NULL)) {
1207 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1208 child_res_id->name[0], parent_res_id->name[0],
1210 GOTO(cleanup, rc = 1);
1213 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1214 *child_res_id, LDLM_IBITS, child_policy,
1215 child_mode, &flags, mds_blocking_ast,
1216 ldlm_completion_ast, NULL, NULL, NULL, 0,
1219 GOTO(cleanup, rc = -EIO);
1222 memset(child_res_id, 0, sizeof(*child_res_id));
1228 switch(cleanup_phase) {
1230 if (child_res_id->name[0] != 0)
1231 ldlm_lock_decref(child_lockh, child_mode);
1233 ldlm_lock_decref(parent_lockh, parent_mode);
1239 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1241 struct lustre_handle *parent_lockh,
1242 struct dentry **dparentp, int parent_mode,
1243 __u64 parent_lockpart, int *update_mode,
1244 char *name, int namelen,
1245 struct lustre_handle *child_lockh,
1246 struct dentry **dchildp, int child_mode,
1247 __u64 child_lockpart, void *clone_info)
1249 struct ldlm_res_id child_res_id = { .name = {0} };
1250 struct ldlm_res_id parent_res_id = { .name = {0} };
1251 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1252 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1253 struct inode *inode;
1254 int rc = 0, cleanup_phase = 0;
1257 /* Step 1: Lookup parent */
1258 *dparentp = mds_fid2dentry(mds, fid, NULL);
1259 if (IS_ERR(*dparentp)) {
1260 rc = PTR_ERR(*dparentp);
1265 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1266 (*dparentp)->d_inode->i_ino, name);
1268 parent_res_id.name[0] = (*dparentp)->d_inode->i_ino;
1269 parent_res_id.name[1] = (*dparentp)->d_inode->i_generation;
1271 parent_lockh[1].cookie = 0;
1272 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1273 struct ldlm_res_id res_id = { .name = {0} };
1274 ldlm_policy_data_t policy;
1276 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1278 res_id.name[0] = (*dparentp)->d_inode->i_ino;
1279 res_id.name[1] = (*dparentp)->d_inode->i_generation;
1280 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1281 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1282 res_id, LDLM_IBITS, &policy,
1283 *update_mode, &flags,
1285 ldlm_completion_ast,
1286 NULL, NULL, NULL, 0, NULL,
1292 parent_res_id.name[2] = full_name_hash(name, namelen - 1);
1293 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
1294 (*dparentp)->d_inode->i_ino,
1295 (*dparentp)->d_inode->i_generation,
1296 parent_res_id.name[2]);
1300 cleanup_phase = 1; /* parent dentry */
1301 #ifdef CONFIG_SNAPFS
1303 /*FIXME is there any other FUNC will use d_fsdata,
1304 *excepet creating inode according inum*/
1305 (*dparentp)->d_fsdata = clone_info;
1309 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1310 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1311 if (IS_ERR(*dchildp)) {
1312 rc = PTR_ERR(*dchildp);
1313 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1317 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1318 /* inode lives on another MDS: return * mds/ino/gen
1319 * and LOOKUP lock. drop possible UPDATE lock! */
1320 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1321 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1322 child_res_id.name[0] = (*dchildp)->d_inum;
1323 child_res_id.name[1] = (*dchildp)->d_generation;
1327 inode = (*dchildp)->d_inode;
1329 inode = igrab(inode);
1333 child_res_id.name[0] = inode->i_ino;
1334 child_res_id.name[1] = inode->i_generation;
1339 cleanup_phase = 2; /* child dentry */
1341 /* Step 3: Lock parent and child in resource order. If child doesn't
1342 * exist, we still have to lock the parent and re-lookup. */
1343 rc = enqueue_ordered_locks(obd,&parent_res_id,parent_lockh,parent_mode,
1344 &parent_policy, &child_res_id, child_lockh,
1345 child_mode, &child_policy);
1349 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1350 cleanup_phase = 4; /* child lock */
1352 cleanup_phase = 3; /* parent lock */
1354 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1355 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1356 parent_mode, &child_res_id, child_lockh,
1357 dchildp, child_mode, &child_policy,
1358 name, namelen, &parent_res_id);
1368 switch (cleanup_phase) {
1370 ldlm_lock_decref(child_lockh, child_mode);
1372 ldlm_lock_decref(parent_lockh, parent_mode);
1377 if (parent_lockh[1].cookie)
1378 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1387 void mds_reconstruct_generic(struct ptlrpc_request *req)
1389 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1391 mds_req_from_mcd(req, med->med_mcd);
1394 int mds_create_local_dentry(struct mds_update_record *rec,
1395 struct obd_device *obd)
1397 struct mds_obd *mds = &obd->u.mds;
1398 struct inode *fids_dir = mds->mds_fids_dir->d_inode;
1399 int fidlen = 0, rc, cleanup_phase = 0;
1400 struct dentry *new_child = NULL;
1401 char *fidname = rec->ur_name;
1402 struct dentry *child = NULL;
1403 struct lustre_handle lockh[2] = {{0}, {0}};
1407 down(&fids_dir->i_sem);
1408 fidlen = ll_fid2str(fidname, rec->ur_fid1->id, rec->ur_fid1->generation);
1409 CDEBUG(D_OTHER, "look for local dentry '%s' for %u/%u\n",
1410 fidname, (unsigned) rec->ur_fid1->id,
1411 (unsigned) rec->ur_fid1->generation);
1413 new_child = lookup_one_len(fidname, mds->mds_fids_dir, fidlen);
1414 up(&fids_dir->i_sem);
1415 if (IS_ERR(new_child)) {
1416 CERROR("can't lookup %s: %d\n", fidname,
1417 (int) PTR_ERR(new_child));
1418 GOTO(cleanup, rc = PTR_ERR(new_child));
1422 if (new_child->d_inode != NULL) {
1423 /* nice. we've already have local dentry! */
1424 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1425 (unsigned) new_child->d_inode->i_ino,
1426 (unsigned) new_child->d_inode->i_generation);
1427 rec->ur_fid1->id = fids_dir->i_ino;
1428 rec->ur_fid1->generation = fids_dir->i_generation;
1429 rec->ur_namelen = fidlen + 1;
1430 GOTO(cleanup, rc = 0);
1433 /* new, local dentry will be added soon. we need no aliases here */
1436 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1437 child = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1439 child = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1440 LCK_EX, lockh, NULL, NULL, 0,
1441 MDS_INODELOCK_UPDATE);
1444 if (IS_ERR(child)) {
1445 rc = PTR_ERR(child);
1446 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1447 CERROR("can't get victim: %d\n", rc);
1452 handle = fsfilt_start(obd, fids_dir, FSFILT_OP_LINK, NULL);
1454 GOTO(cleanup, rc = PTR_ERR(handle));
1456 rc = fsfilt_add_dir_entry(obd, mds->mds_fids_dir, fidname, fidlen,
1457 rec->ur_fid1->id, rec->ur_fid1->generation,
1460 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1461 (unsigned long) child->d_inode->i_ino,
1462 (unsigned long) child->d_inode->i_generation, rc);
1464 if (S_ISDIR(child->d_inode->i_mode)) {
1465 fids_dir->i_nlink++;
1466 mark_inode_dirty(fids_dir);
1468 mark_inode_dirty(child->d_inode);
1470 fsfilt_commit(obd, mds->mds_sb, fids_dir, handle, 0);
1472 rec->ur_fid1->id = fids_dir->i_ino;
1473 rec->ur_fid1->generation = fids_dir->i_generation;
1474 rec->ur_namelen = fidlen + 1;
1477 switch(cleanup_phase) {
1479 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1480 ldlm_lock_decref(lockh, LCK_EX);
1490 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1491 struct ptlrpc_request *slave)
1493 void *cookie, *cookie2;
1494 struct mds_body *body2;
1495 struct mds_body *body;
1499 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1500 LASSERT(body != NULL);
1502 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1503 LASSERT(body2 != NULL);
1505 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER))) {
1509 memcpy(body2, body, sizeof(*body));
1510 body2->valid &= ~OBD_MD_FLCOOKIE;
1512 if (!(body->valid & OBD_MD_FLEASIZE) &&
1513 !(body->valid & OBD_MD_FLDIREA))
1516 if (body->eadatasize == 0) {
1517 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1521 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1523 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1524 LASSERT(ea != NULL);
1526 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1527 LASSERT(ea2 != NULL);
1529 memcpy(ea2, ea, body->eadatasize);
1531 if (body->valid & OBD_MD_FLCOOKIE) {
1532 LASSERT(master->rq_repmsg->buflens[2] >=
1533 slave->rq_repmsg->buflens[2]);
1534 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1535 slave->rq_repmsg->buflens[2]);
1536 LASSERT(cookie != NULL);
1538 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1539 master->rq_repmsg->buflens[2]);
1540 LASSERT(cookie2 != NULL);
1541 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1542 body2->valid |= OBD_MD_FLCOOKIE;
1547 static int mds_reint_unlink_remote(struct mds_update_record *rec, int offset,
1548 struct ptlrpc_request *req,
1549 struct lustre_handle *parent_lockh,
1551 struct dentry *dparent,
1552 struct lustre_handle *child_lockh,
1553 struct dentry *dchild)
1555 struct obd_device *obd = req->rq_export->exp_obd;
1556 struct mds_obd *mds = mds_req2mds(req);
1557 struct mdc_op_data op_data;
1558 int rc = 0, cleanup_phase = 0;
1559 struct ptlrpc_request *request = NULL;
1563 LASSERT(offset == 0 || offset == 2);
1565 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode %u/%u/%u)",
1566 rec->ur_namelen - 1, rec->ur_name, (unsigned)dchild->d_mdsnum,
1567 (unsigned) dchild->d_inum, (unsigned) dchild->d_generation);
1568 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1569 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode %u/%u/%u)",
1570 rec->ur_namelen - 1, rec->ur_name,
1571 (unsigned)dchild->d_mdsnum,
1572 (unsigned) dchild->d_inum,
1573 (unsigned) dchild->d_generation);
1575 /* time to drop i_nlink on remote MDS */
1576 memset(&op_data, 0, sizeof(op_data));
1577 op_data.fid1.mds = dchild->d_mdsnum;
1578 op_data.fid1.id = dchild->d_inum;
1579 op_data.fid1.generation = dchild->d_generation;
1580 op_data.create_mode = rec->ur_mode;
1581 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1582 op_data.create_mode |= MDS_MODE_REPLAY;
1583 rc = md_unlink(mds->mds_lmv_exp, &op_data, &request);
1587 mds_copy_unlink_reply(req, request);
1588 ptlrpc_req_finished(request);
1591 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1594 GOTO(cleanup, rc = PTR_ERR(handle));
1595 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1596 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1600 req->rq_status = rc;
1603 if (parent_lockh[1].cookie != 0)
1604 ldlm_lock_decref(parent_lockh + 1, update_mode);
1606 ldlm_lock_decref(child_lockh, LCK_EX);
1608 ldlm_lock_decref(parent_lockh, LCK_PW);
1610 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1617 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
1618 struct ptlrpc_request *req,
1619 struct lustre_handle *lh)
1621 struct dentry *dparent, *dchild;
1622 struct mds_obd *mds = mds_req2mds(req);
1623 struct obd_device *obd = req->rq_export->exp_obd;
1624 struct mds_body *body = NULL;
1625 struct inode *child_inode;
1626 struct lustre_handle parent_lockh[2] = {{0}, {0}};
1627 struct lustre_handle child_lockh = {0};
1629 struct lustre_handle child_reuse_lockh = {0};
1631 struct lustre_handle * slave_lockh = NULL;
1632 struct llog_create_locks *lcl = NULL;
1633 char fidname[LL_FID_NAMELEN];
1634 void *handle = NULL;
1635 int rc = 0, log_unlink = 0, cleanup_phase = 0;
1636 int unlink_by_fid = 0;
1640 LASSERT(offset == 0 || offset == 2);
1642 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
1643 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name);
1645 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
1647 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1648 DEBUG_REQ(D_HA, req, "unlink replay\n");
1649 memcpy(lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1650 lustre_msg_buf(req->rq_reqmsg, 2, 0),
1651 req->rq_repmsg->buflens[offset + 2]);
1654 MDS_UPDATE_COUNTER(mds, MDS_UNLINK_COUNT);
1656 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
1657 GOTO(cleanup, rc = -ENOENT);
1659 if (rec->ur_namelen == 1) {
1660 /* this is request to drop i_nlink on local inode */
1662 rec->ur_name = fidname;
1663 rc = mds_create_local_dentry(rec, obd);
1664 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
1665 DEBUG_REQ(D_HA, req,
1666 "drop nlink on inode %u/%u/%u (replay)",
1667 (unsigned) rec->ur_fid1->mds,
1668 (unsigned) rec->ur_fid1->id,
1669 (unsigned) rec->ur_fid1->generation);
1675 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1676 /* master mds for directory asks slave removing
1677 * inode is already locked */
1678 dparent = mds_fid2locked_dentry(obd, rec->ur_fid1, NULL,
1679 LCK_PW, parent_lockh,
1680 &update_mode, rec->ur_name,
1682 MDS_INODELOCK_UPDATE);
1683 if (IS_ERR(dparent))
1684 GOTO(cleanup, rc = PTR_ERR(dparent));
1685 dchild = ll_lookup_one_len(rec->ur_name, dparent,
1686 rec->ur_namelen - 1);
1688 GOTO(cleanup, rc = PTR_ERR(dchild));
1689 child_lockh.cookie = 0;
1690 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
1691 LASSERT(dchild->d_inode != NULL);
1692 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
1694 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1,
1695 parent_lockh, &dparent,
1696 LCK_PW, MDS_INODELOCK_UPDATE,
1697 &update_mode, rec->ur_name,
1698 rec->ur_namelen, &child_lockh,
1700 MDS_INODELOCK_LOOKUP |
1701 MDS_INODELOCK_UPDATE, NULL);
1706 if (dchild->d_flags & DCACHE_CROSS_REF) {
1707 /* we should have parent lock only here */
1708 LASSERT(unlink_by_fid == 0);
1709 LASSERT(dchild->d_mdsnum != mds->mds_num);
1710 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
1711 update_mode, dparent, &child_lockh, dchild);
1715 cleanup_phase = 1; /* dchild, dparent, locks */
1718 child_inode = dchild->d_inode;
1719 if (child_inode == NULL) {
1720 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
1721 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
1722 GOTO(cleanup, rc = -ENOENT);
1725 cleanup_phase = 2; /* dchild has a lock */
1727 /* We have to do these checks ourselves, in case we are making an
1728 * orphan. The client tells us whether rmdir() or unlink() was called,
1729 * so we need to return appropriate errors (bug 72).
1731 * We don't have to check permissions, because vfs_rename (called from
1732 * mds_open_unlink_rename) also calls may_delete. */
1733 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
1734 if (!S_ISDIR(child_inode->i_mode))
1735 GOTO(cleanup, rc = -ENOTDIR);
1737 if (S_ISDIR(child_inode->i_mode))
1738 GOTO(cleanup, rc = -EISDIR);
1741 /* handle splitted dir */
1742 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
1747 /* Step 4: Get a lock on the ino to sync with creation WRT inode
1748 * reuse (see bug 2029). */
1749 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
1753 cleanup_phase = 3; /* child inum lock */
1755 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
1757 /* ldlm_reply in buf[0] if called via intent */
1761 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
1762 LASSERT(body != NULL);
1764 /* If this is the last reference to this inode, get the OBD EA
1765 * data first so the client can destroy OST objects.
1766 * we only do the object removal if no open files remain.
1767 * Nobody can get at this name anymore because of the locks so
1768 * we make decisions here as to whether to remove the inode */
1769 if (S_ISREG(child_inode->i_mode) && child_inode->i_nlink == 1 &&
1770 mds_open_orphan_count(child_inode) == 0) {
1771 mds_pack_inode2fid(obd, &body->fid1, child_inode);
1772 mds_pack_inode2body(obd, body, child_inode);
1773 mds_pack_md(obd, req->rq_repmsg, offset + 1, body,
1775 if (!(body->valid & OBD_MD_FLEASIZE)) {
1776 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
1777 OBD_MD_FLATIME | OBD_MD_FLMTIME);
1783 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
1784 switch (child_inode->i_mode & S_IFMT) {
1786 /* Drop any lingering child directories before we start our
1787 * transaction, to avoid doing multiple inode dirty/delete
1788 * in our compound transaction (bug 1321). */
1789 shrink_dcache_parent(dchild);
1790 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1793 GOTO(cleanup, rc = PTR_ERR(handle));
1794 cleanup_phase = 4; /* transaction */
1795 rc = vfs_rmdir(dparent->d_inode, dchild);
1798 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
1799 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
1801 handle = fsfilt_start_log(obd, dparent->d_inode,
1802 FSFILT_OP_UNLINK, NULL,
1803 le32_to_cpu(lmm->lmm_stripe_count));
1805 GOTO(cleanup, rc = PTR_ERR(handle));
1807 cleanup_phase = 4; /* transaction */
1808 rc = vfs_unlink(dparent->d_inode, dchild);
1810 if (!rc && log_unlink)
1811 if (mds_log_op_unlink(obd, child_inode,
1812 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
1813 req->rq_repmsg->buflens[offset + 1],
1814 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
1815 req->rq_repmsg->buflens[offset + 2], &lcl) > 0)
1816 body->valid |= OBD_MD_FLCOOKIE;
1824 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
1827 GOTO(cleanup, rc = PTR_ERR(handle));
1828 cleanup_phase = 4; /* transaction */
1829 rc = vfs_unlink(dparent->d_inode, dchild);
1832 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
1835 GOTO(cleanup, rc = -EINVAL);
1843 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1844 LTIME_S(iattr.ia_mtime) = LTIME_S(rec->ur_time);
1845 LTIME_S(iattr.ia_ctime) = LTIME_S(rec->ur_time);
1847 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1849 CERROR("error on parent setattr: rc = %d\n", err);
1852 switch(cleanup_phase) {
1854 LASSERT(dchild != NULL && dchild->d_inode != NULL);
1855 LASSERT(atomic_read(&dchild->d_inode->i_count) > 0);
1856 if (rc == 0 && dchild->d_inode->i_nlink == 0 &&
1857 mds_open_orphan_count(dchild->d_inode) > 0) {
1859 /* filesystem is really going to destroy an inode
1860 * we have to delay this till inode is opened -bzzz */
1861 mds_open_unlink_rename(rec, obd, dparent, dchild, NULL);
1863 /* handle splitted dir */
1865 /* master directory can be non-empty or something else ... */
1866 mds_unlink_slave_objs(obd, dchild);
1868 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1871 (void)obd_set_info(mds->mds_osc_exp, strlen("unlinked"),
1872 "unlinked", 0, NULL);
1874 ptlrpc_save_llog_lock(req, lcl);
1875 case 3: /* child ino-reuse lock */
1877 if (rc && body != NULL) {
1878 // Don't unlink the OST objects if the MDS unlink failed
1882 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
1884 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
1886 case 2: /* child lock */
1887 mds_unlock_slave_objs(obd, dchild, slave_lockh);
1888 if (child_lockh.cookie)
1889 ldlm_lock_decref(&child_lockh, LCK_EX);
1890 case 1: /* child and parent dentry, parent lock */
1892 if (parent_lockh[1].cookie != 0)
1893 ldlm_lock_decref(parent_lockh + 1, update_mode);
1896 ldlm_lock_decref(parent_lockh, LCK_PW);
1898 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
1905 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1908 req->rq_status = rc;
1913 * to service requests from remote MDS to increment i_nlink
1915 static int mds_reint_link_acquire(struct mds_update_record *rec,
1916 int offset, struct ptlrpc_request *req,
1917 struct lustre_handle *lh)
1919 struct obd_device *obd = req->rq_export->exp_obd;
1920 struct ldlm_res_id src_res_id = { .name = {0} };
1921 struct lustre_handle *handle = NULL, src_lockh = {0};
1922 struct mds_obd *mds = mds_req2mds(req);
1923 int rc = 0, cleanup_phase = 0;
1924 struct dentry *de_src = NULL;
1925 ldlm_policy_data_t policy;
1929 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks %u/%u/%u\n",
1930 obd->obd_name, (unsigned) rec->ur_fid1->mds,
1931 (unsigned) rec->ur_fid1->id,
1932 (unsigned) rec->ur_fid1->generation);
1934 /* Step 1: Lookup the source inode and target directory by FID */
1935 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
1937 GOTO(cleanup, rc = PTR_ERR(de_src));
1938 cleanup_phase = 1; /* source dentry */
1940 src_res_id.name[0] = de_src->d_inode->i_ino;
1941 src_res_id.name[1] = de_src->d_inode->i_generation;
1942 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1944 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1945 src_res_id, LDLM_IBITS, &policy,
1946 LCK_EX, &flags, mds_blocking_ast,
1947 ldlm_completion_ast, NULL, NULL,
1948 NULL, 0, NULL, &src_lockh);
1950 GOTO(cleanup, rc = -ENOLCK);
1951 cleanup_phase = 2; /* lock */
1953 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
1955 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
1956 if (IS_ERR(handle)) {
1957 rc = PTR_ERR(handle);
1960 de_src->d_inode->i_nlink++;
1961 mark_inode_dirty(de_src->d_inode);
1964 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
1965 handle, req, rc, 0);
1967 switch (cleanup_phase) {
1970 ldlm_lock_decref(&src_lockh, LCK_EX);
1972 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
1978 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1981 req->rq_status = rc;
1986 * request to link to foreign inode:
1987 * - acquire i_nlinks on this inode
1990 static int mds_reint_link_to_remote(struct mds_update_record *rec,
1991 int offset, struct ptlrpc_request *req,
1992 struct lustre_handle *lh)
1994 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
1995 struct obd_device *obd = req->rq_export->exp_obd;
1996 struct dentry *de_tgt_dir = NULL;
1997 struct mds_obd *mds = mds_req2mds(req);
1998 int rc = 0, cleanup_phase = 0;
1999 struct mdc_op_data op_data;
2000 struct ptlrpc_request *request = NULL;
2004 #define fmt "%s: request to link %u/%u/%u:%*s to foreign inode %u/%u/%u\n"
2005 DEBUG_REQ(D_INODE, req, fmt, obd->obd_name,
2006 (unsigned) rec->ur_fid2->mds,
2007 (unsigned) rec->ur_fid2->id,
2008 (unsigned) rec->ur_fid2->generation,
2009 rec->ur_namelen - 1, rec->ur_name,
2010 (unsigned) rec->ur_fid1->mds,
2011 (unsigned) rec->ur_fid1->id,
2012 (unsigned)rec->ur_fid1->generation);
2014 de_tgt_dir = mds_fid2locked_dentry(obd, rec->ur_fid2, NULL, LCK_EX,
2015 tgt_dir_lockh, &update_mode,
2016 rec->ur_name, rec->ur_namelen - 1,
2017 MDS_INODELOCK_UPDATE);
2018 if (IS_ERR(de_tgt_dir))
2019 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2022 op_data.fid1 = *(rec->ur_fid1);
2023 op_data.namelen = 0;
2024 op_data.name = NULL;
2025 rc = md_link(mds->mds_lmv_exp, &op_data, &request);
2031 ptlrpc_req_finished(request);
2033 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2035 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2036 if (IS_ERR(handle)) {
2037 rc = PTR_ERR(handle);
2041 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2042 rec->ur_namelen - 1, rec->ur_fid1->id,
2043 rec->ur_fid1->generation, rec->ur_fid1->mds);
2047 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2048 handle, req, rc, 0);
2051 switch (cleanup_phase) {
2054 /* FIXME: drop i_nlink on remote inode here */
2055 CERROR("MUST drop drop i_nlink here\n");
2060 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2062 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2065 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2067 ptlrpc_save_lock(req, tgt_dir_lockh+1, update_mode);
2073 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2076 req->rq_status = rc;
2080 static int mds_reint_link(struct mds_update_record *rec, int offset,
2081 struct ptlrpc_request *req,
2082 struct lustre_handle *lh)
2084 struct obd_device *obd = req->rq_export->exp_obd;
2085 struct dentry *de_src = NULL;
2086 struct dentry *de_tgt_dir = NULL;
2087 struct dentry *dchild = NULL;
2088 struct mds_obd *mds = mds_req2mds(req);
2089 struct lustre_handle *handle = NULL;
2090 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2091 struct ldlm_res_id src_res_id = { .name = {0} };
2092 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2093 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2094 ldlm_policy_data_t tgt_dir_policy =
2095 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2096 int rc = 0, cleanup_phase = 0;
2097 int update_mode = 0;
2100 LASSERT(offset == 0);
2102 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2103 rec->ur_fid1->id, rec->ur_fid1->generation,
2104 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_name);
2106 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2108 MDS_UPDATE_COUNTER(mds, MDS_LINK_COUNT);
2110 // memset(tgt_dir_lockh, 0, 2*sizeof(tgt_dir_lockh[0]));
2111 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2112 GOTO(cleanup, rc = -ENOENT);
2114 if (rec->ur_fid1->mds != mds->mds_num) {
2115 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2119 if (rec->ur_namelen == 1) {
2120 rc = mds_reint_link_acquire(rec, offset, req, lh);
2124 /* Step 1: Lookup the source inode and target directory by FID */
2125 de_src = mds_fid2dentry(mds, rec->ur_fid1, NULL);
2127 GOTO(cleanup, rc = PTR_ERR(de_src));
2129 cleanup_phase = 1; /* source dentry */
2131 de_tgt_dir = mds_fid2dentry(mds, rec->ur_fid2, NULL);
2132 if (IS_ERR(de_tgt_dir)) {
2133 rc = PTR_ERR(de_tgt_dir);
2138 cleanup_phase = 2; /* target directory dentry */
2140 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2141 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name, rec->ur_name,
2142 de_src->d_inode->i_ino);
2144 /* Step 2: Take the two locks */
2145 src_res_id.name[0] = de_src->d_inode->i_ino;
2146 src_res_id.name[1] = de_src->d_inode->i_generation;
2147 tgt_dir_res_id.name[0] = de_tgt_dir->d_inode->i_ino;
2148 tgt_dir_res_id.name[1] = de_tgt_dir->d_inode->i_generation;
2150 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2152 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2154 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2155 tgt_dir_res_id, LDLM_IBITS,
2156 &src_policy, update_mode, &flags,
2158 ldlm_completion_ast, NULL, NULL,
2159 NULL, 0, NULL, tgt_dir_lockh + 1);
2161 GOTO(cleanup, rc = -ENOLCK);
2164 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2165 rec->ur_namelen - 1);
2166 CDEBUG(D_INFO, "take lock on %lu:%u:"LPX64"\n",
2167 de_tgt_dir->d_inode->i_ino,
2168 de_tgt_dir->d_inode->i_generation,
2169 tgt_dir_res_id.name[2]);
2172 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2174 &tgt_dir_res_id, tgt_dir_lockh, LCK_EX,
2179 cleanup_phase = 3; /* locks */
2181 /* Step 3: Lookup the child */
2182 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir, rec->ur_namelen-1);
2183 if (IS_ERR(dchild)) {
2184 rc = PTR_ERR(dchild);
2185 if (rc != -EPERM && rc != -EACCES)
2186 CERROR("child lookup error %d\n", rc);
2190 cleanup_phase = 4; /* child dentry */
2192 if (dchild->d_inode) {
2193 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2194 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2199 /* Step 4: Do it. */
2200 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2202 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2203 if (IS_ERR(handle)) {
2204 rc = PTR_ERR(handle);
2208 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2209 if (rc && rc != -EPERM && rc != -EACCES)
2210 CERROR("vfs_link error %d\n", rc);
2212 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2213 handle, req, rc, 0);
2216 switch (cleanup_phase) {
2217 case 4: /* child dentry */
2221 ldlm_lock_decref(&src_lockh, LCK_EX);
2222 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2224 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2225 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2227 case 2: /* target dentry */
2229 if (tgt_dir_lockh[1].cookie && update_mode)
2230 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2234 case 1: /* source dentry */
2239 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2242 req->rq_status = rc;
2247 * add a hard link in the PENDING directory, only used by rename()
2249 static int mds_add_link_orphan(struct mds_update_record *rec,
2250 struct obd_device *obd,
2251 struct dentry *dentry)
2253 struct mds_obd *mds = &obd->u.mds;
2254 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
2255 struct dentry *pending_child;
2256 char fidname[LL_FID_NAMELEN];
2260 LASSERT(dentry->d_inode);
2261 LASSERT(!mds_inode_is_orphan(dentry->d_inode));
2263 down(&pending_dir->i_sem);
2264 fidlen = ll_fid2str(fidname, dentry->d_inode->i_ino,
2265 dentry->d_inode->i_generation);
2267 CDEBUG(D_ERROR, "pending destroy of %dx open file %s = %s\n",
2268 mds_open_orphan_count(dentry->d_inode),
2269 rec->ur_name, fidname);
2271 pending_child = lookup_one_len(fidname, mds->mds_pending_dir, fidlen);
2272 if (IS_ERR(pending_child))
2273 GOTO(out_lock, rc = PTR_ERR(pending_child));
2275 if (pending_child->d_inode != NULL) {
2276 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
2277 LASSERT(pending_child->d_inode == dentry->d_inode);
2278 GOTO(out_dput, rc = 0);
2282 rc = vfs_link(dentry, pending_dir, pending_child);
2285 CERROR("error addlink orphan %s to PENDING: rc = %d\n",
2288 mds_inode_set_orphan(dentry->d_inode);
2290 l_dput(pending_child);
2292 up(&pending_dir->i_sem);
2296 /* The idea here is that we need to get four locks in the end:
2297 * one on each parent directory, one on each child. We need to take
2298 * these locks in some kind of order (to avoid deadlocks), and the order
2299 * I selected is "increasing resource number" order. We need to look up
2300 * the children, however, before we know what the resource number(s) are.
2301 * Thus the following plan:
2303 * 1,2. Look up the parents
2304 * 3,4. Look up the children
2305 * 5. Take locks on the parents and children, in order
2306 * 6. Verify that the children haven't changed since they were looked up
2308 * If there was a race and the children changed since they were first looked
2309 * up, it is possible that mds_verify_child() will be able to just grab the
2310 * lock on the new child resource (if it has a higher resource than any other)
2311 * but we need to compare against not only its parent, but also against the
2312 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2314 * We need the fancy igrab() on the child inodes because we aren't holding a
2315 * lock on the parent after the lookup is done, so dentry->d_inode may change
2316 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2318 static int mds_get_parents_children_locked(struct obd_device *obd,
2319 struct mds_obd *mds,
2320 struct ll_fid *p1_fid,
2321 struct dentry **de_srcdirp,
2322 struct ll_fid *p2_fid,
2323 struct dentry **de_tgtdirp,
2325 const char *old_name, int old_len,
2326 struct dentry **de_oldp,
2327 const char *new_name, int new_len,
2328 struct dentry **de_newp,
2329 struct lustre_handle *dlm_handles,
2332 struct ldlm_res_id p1_res_id = { .name = {0} };
2333 struct ldlm_res_id p2_res_id = { .name = {0} };
2334 struct ldlm_res_id c1_res_id = { .name = {0} };
2335 struct ldlm_res_id c2_res_id = { .name = {0} };
2336 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2337 /* Only dentry should change, but the inode itself would be
2339 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2340 /* If something is going to be replaced, both dentry and inode locks are
2342 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP|
2343 MDS_INODELOCK_UPDATE}};
2344 struct ldlm_res_id *maxres_src, *maxres_tgt;
2345 struct inode *inode;
2346 int rc = 0, cleanup_phase = 0;
2349 /* Step 1: Lookup the source directory */
2350 *de_srcdirp = mds_fid2dentry(mds, p1_fid, NULL);
2351 if (IS_ERR(*de_srcdirp))
2352 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2354 cleanup_phase = 1; /* source directory dentry */
2356 p1_res_id.name[0] = (*de_srcdirp)->d_inode->i_ino;
2357 p1_res_id.name[1] = (*de_srcdirp)->d_inode->i_generation;
2359 /* Step 2: Lookup the target directory */
2360 if (memcmp(p1_fid, p2_fid, sizeof(*p1_fid)) == 0) {
2361 *de_tgtdirp = dget(*de_srcdirp);
2363 *de_tgtdirp = mds_fid2dentry(mds, p2_fid, NULL);
2364 if (IS_ERR(*de_tgtdirp)) {
2365 rc = PTR_ERR(*de_tgtdirp);
2371 cleanup_phase = 2; /* target directory dentry */
2373 p2_res_id.name[0] = (*de_tgtdirp)->d_inode->i_ino;
2374 p2_res_id.name[1] = (*de_tgtdirp)->d_inode->i_generation;
2377 dlm_handles[5].cookie = 0;
2378 dlm_handles[6].cookie = 0;
2379 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2380 /* Get a temp lock on just ino, gen to flush client cache and
2381 * to protect dirs from concurrent splitting */
2382 rc = enqueue_ordered_locks(obd, &p1_res_id, &(dlm_handles[5]),
2383 LCK_PW, &p_policy, &p2_res_id,
2384 &(dlm_handles[6]),LCK_PW,&p_policy);
2387 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2388 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2389 CDEBUG(D_INFO, "take locks on %lu:%u:"LPX64", %lu:%u:"LPX64"\n",
2390 (*de_srcdirp)->d_inode->i_ino,
2391 (*de_srcdirp)->d_inode->i_generation, p1_res_id.name[2],
2392 (*de_tgtdirp)->d_inode->i_ino,
2393 (*de_tgtdirp)->d_inode->i_generation, p2_res_id.name[2]);
2398 /* Step 3: Lookup the source child entry */
2399 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp, old_len - 1);
2400 if (IS_ERR(*de_oldp)) {
2401 rc = PTR_ERR(*de_oldp);
2402 CERROR("old child lookup error (%*s): %d\n",
2403 old_len - 1, old_name, rc);
2407 cleanup_phase = 4; /* original name dentry */
2409 inode = (*de_oldp)->d_inode;
2410 if (inode != NULL) {
2411 inode = igrab(inode);
2413 GOTO(cleanup, rc = -ENOENT);
2415 c1_res_id.name[0] = inode->i_ino;
2416 c1_res_id.name[1] = inode->i_generation;
2418 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2419 c1_res_id.name[0] = (*de_oldp)->d_inum;
2420 c1_res_id.name[1] = (*de_oldp)->d_generation;
2423 /* Step 4: Lookup the target child entry */
2424 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp, new_len - 1);
2425 if (IS_ERR(*de_newp)) {
2426 rc = PTR_ERR(*de_newp);
2427 CERROR("new child lookup error (%*s): %d\n",
2428 old_len - 1, old_name, rc);
2432 cleanup_phase = 5; /* target dentry */
2434 inode = (*de_newp)->d_inode;
2435 if (inode != NULL) {
2436 inode = igrab(inode);
2440 c2_res_id.name[0] = inode->i_ino;
2441 c2_res_id.name[1] = inode->i_generation;
2443 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2444 c2_res_id.name[0] = (*de_newp)->d_inum;
2445 c2_res_id.name[1] = (*de_newp)->d_generation;
2449 /* Step 5: Take locks on the parents and child(ren) */
2450 maxres_src = &p1_res_id;
2451 maxres_tgt = &p2_res_id;
2452 cleanup_phase = 5; /* target dentry */
2454 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL,NULL))
2455 maxres_src = &c1_res_id;
2456 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL,NULL))
2457 maxres_tgt = &c2_res_id;
2459 rc = enqueue_4ordered_locks(obd, &p1_res_id,&dlm_handles[0],parent_mode,
2461 &p2_res_id, &dlm_handles[1], parent_mode,
2463 &c1_res_id, &dlm_handles[2], child_mode,
2465 &c2_res_id, &dlm_handles[3], child_mode,
2470 cleanup_phase = 6; /* parent and child(ren) locks */
2472 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2473 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2474 parent_mode, &c1_res_id, &dlm_handles[2],
2475 de_oldp, child_mode, &c1_policy, old_name,old_len,
2478 if (c2_res_id.name[0] != 0)
2479 ldlm_lock_decref(&dlm_handles[3], child_mode);
2480 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2487 if (!DENTRY_VALID(*de_oldp))
2488 GOTO(cleanup, rc = -ENOENT);
2490 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2491 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2492 parent_mode, &c2_res_id, &dlm_handles[3],
2493 de_newp, child_mode, &c2_policy, new_name,
2494 new_len, maxres_src);
2496 ldlm_lock_decref(&dlm_handles[2], child_mode);
2497 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2507 switch (cleanup_phase) {
2508 case 6: /* child lock(s) */
2509 if (c2_res_id.name[0] != 0)
2510 ldlm_lock_decref(&dlm_handles[3], child_mode);
2511 if (c1_res_id.name[0] != 0)
2512 ldlm_lock_decref(&dlm_handles[2], child_mode);
2513 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2514 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2515 case 5: /* target dentry */
2517 case 4: /* source dentry */
2521 if (dlm_handles[5].cookie != 0)
2522 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2523 if (dlm_handles[6].cookie != 0)
2524 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2526 case 2: /* target directory dentry */
2527 l_dput(*de_tgtdirp);
2528 case 1: /* source directry dentry */
2529 l_dput(*de_srcdirp);
2536 static int mds_reint_rename_create_name(struct mds_update_record *rec,
2537 int offset, struct ptlrpc_request *req)
2539 struct obd_device *obd = req->rq_export->exp_obd;
2540 struct dentry *de_srcdir = NULL;
2541 struct dentry *de_new = NULL;
2542 struct mds_obd *mds = mds_req2mds(req);
2543 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2544 struct lustre_handle child_lockh = {0};
2545 int cleanup_phase = 0;
2546 void *handle = NULL;
2547 int update_mode, rc = 0;
2550 /* another MDS executing rename operation has asked us
2551 * to create target name. such a creation should destroy
2552 * existing target name */
2554 CDEBUG(D_OTHER, "%s: request to create name %s for %lu/%lu/%lu\n",
2555 obd->obd_name, rec->ur_tgt,
2556 (unsigned long) rec->ur_fid1->mds,
2557 (unsigned long) rec->ur_fid1->id,
2558 (unsigned long) rec->ur_fid1->generation);
2560 /* first, lookup the target */
2561 child_lockh.cookie = 0;
2562 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid2, parent_lockh,
2563 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2564 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
2565 &child_lockh, &de_new, LCK_EX,
2566 MDS_INODELOCK_LOOKUP, NULL);
2573 LASSERT(de_srcdir->d_inode);
2576 if (de_new->d_inode) {
2577 /* name exists and points to local inode
2578 * try to unlink this name and create new one */
2579 CERROR("%s: %s points to local inode %lu/%lu\n",
2580 obd->obd_name, rec->ur_tgt,
2581 (unsigned long) de_new->d_inode->i_ino,
2582 (unsigned long) de_new->d_inode->i_generation);
2583 handle = fsfilt_start(obd, de_srcdir->d_inode,
2584 FSFILT_OP_RENAME, NULL);
2586 GOTO(cleanup, rc = PTR_ERR(handle));
2587 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de_new);
2590 } else if (de_new->d_flags & DCACHE_CROSS_REF) {
2591 /* name exists adn points to remove inode */
2592 CERROR("%s: %s points to remote inode %lu/%lu/%lu\n",
2593 obd->obd_name, rec->ur_tgt,
2594 (unsigned long) de_new->d_mdsnum,
2595 (unsigned long) de_new->d_inum,
2596 (unsigned long) de_new->d_generation);
2598 /* name doesn't exist. the simplest case */
2599 handle = fsfilt_start(obd, de_srcdir->d_inode,
2600 FSFILT_OP_LINK, NULL);
2602 GOTO(cleanup, rc = PTR_ERR(handle));
2606 rc = fsfilt_add_dir_entry(obd, de_srcdir, rec->ur_tgt,
2607 rec->ur_tgtlen - 1, rec->ur_fid1->id,
2608 rec->ur_fid1->generation, rec->ur_fid1->mds);
2610 CERROR("add_dir_entry() returned error %d\n", rc);
2613 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2614 handle, req, rc, 0);
2615 switch(cleanup_phase) {
2619 if (parent_lockh[1].cookie != 0)
2620 ldlm_lock_decref(&parent_lockh[1], update_mode);
2622 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2623 if (child_lockh.cookie != 0)
2624 ldlm_lock_decref(&child_lockh, LCK_EX);
2632 req->rq_status = rc;
2637 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
2638 struct ptlrpc_request *req)
2640 struct obd_device *obd = req->rq_export->exp_obd;
2641 struct ptlrpc_request *req2 = NULL;
2642 struct dentry *de_srcdir = NULL;
2643 struct dentry *de_old = NULL;
2644 struct mds_obd *mds = mds_req2mds(req);
2645 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2646 struct lustre_handle child_lockh = {0};
2647 struct mdc_op_data opdata;
2648 void *handle = NULL;
2649 int update_mode, rc = 0;
2652 CDEBUG(D_OTHER, "%s: move name %s onto another mds%u\n",
2653 obd->obd_name, rec->ur_name, rec->ur_fid2->mds + 1);
2654 memset(&opdata, 0, sizeof(opdata));
2656 child_lockh.cookie = 0;
2657 rc = mds_get_parent_child_locked(obd, mds, rec->ur_fid1, parent_lockh,
2658 &de_srcdir,LCK_PW,MDS_INODELOCK_UPDATE,
2659 &update_mode, rec->ur_name,
2660 rec->ur_namelen, &child_lockh, &de_old,
2661 LCK_EX, MDS_INODELOCK_LOOKUP, NULL);
2664 LASSERT(de_srcdir->d_inode);
2667 /* we already know the target should be created on another MDS
2668 * so, we have to request that MDS to do it */
2670 /* prepare source fid */
2671 if (de_old->d_flags & DCACHE_CROSS_REF) {
2672 LASSERT(de_old->d_inode == NULL);
2673 CDEBUG(D_OTHER, "request to move remote name\n");
2674 opdata.fid1.mds = de_old->d_mdsnum;
2675 opdata.fid1.id = de_old->d_inum;
2676 opdata.fid1.generation = de_old->d_generation;
2677 } else if (de_old->d_inode == NULL) {
2678 /* oh, source doesn't exist */
2679 GOTO(cleanup, rc = -ENOENT);
2681 LASSERT(de_old->d_inode != NULL);
2682 CDEBUG(D_OTHER, "request to move local name\n");
2683 opdata.fid1.mds = mds->mds_num;
2684 opdata.fid1.id = de_old->d_inode->i_ino;
2685 opdata.fid1.generation = de_old->d_inode->i_generation;
2688 opdata.fid2 = *(rec->ur_fid2);
2689 rc = md_rename(mds->mds_lmv_exp, &opdata, NULL, 0, rec->ur_tgt,
2690 rec->ur_tgtlen - 1, &req2);
2695 handle = fsfilt_start(obd, de_srcdir->d_inode, FSFILT_OP_UNLINK, NULL);
2697 GOTO(cleanup, rc = PTR_ERR(handle));
2698 rc = fsfilt_del_dir_entry(obd, de_old);
2703 rc = mds_finish_transno(mds, de_srcdir ? de_srcdir->d_inode : NULL,
2704 handle, req, rc, 0);
2706 ptlrpc_req_finished(req2);
2709 if (parent_lockh[1].cookie != 0)
2710 ldlm_lock_decref(&parent_lockh[1], update_mode);
2712 ldlm_lock_decref(&parent_lockh[0], LCK_PW);
2713 if (child_lockh.cookie != 0)
2714 ldlm_lock_decref(&child_lockh, LCK_EX);
2719 req->rq_status = rc;
2724 static int mds_reint_rename(struct mds_update_record *rec, int offset,
2725 struct ptlrpc_request *req, struct lustre_handle *lockh)
2727 struct obd_device *obd = req->rq_export->exp_obd;
2728 struct dentry *de_srcdir = NULL;
2729 struct dentry *de_tgtdir = NULL;
2730 struct dentry *de_old = NULL;
2731 struct dentry *de_new = NULL;
2732 struct mds_obd *mds = mds_req2mds(req);
2733 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
2734 struct mds_body *body = NULL;
2735 struct llog_create_locks *lcl = NULL;
2736 int rc = 0, log_unlink = 0, lock_count = 3;
2737 int cleanup_phase = 0;
2738 void *handle = NULL;
2741 LASSERT(offset == 0);
2743 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u %s to "LPU64"/%u %s",
2744 rec->ur_fid1->id, rec->ur_fid1->generation, rec->ur_name,
2745 rec->ur_fid2->id, rec->ur_fid2->generation, rec->ur_tgt);
2747 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2749 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2750 DEBUG_REQ(D_HA, req, "rename replay\n");
2751 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2752 lustre_msg_buf(req->rq_reqmsg, 3, 0),
2753 req->rq_repmsg->buflens[2]);
2756 MDS_UPDATE_COUNTER(mds, MDS_RENAME_COUNT);
2758 if (rec->ur_namelen == 1) {
2759 rc = mds_reint_rename_create_name(rec, offset, req);
2763 if (rec->ur_fid2->mds != mds->mds_num) {
2764 rc = mds_reint_rename_to_remote(rec, offset, req);
2768 rc = mds_get_parents_children_locked(obd, mds, rec->ur_fid1, &de_srcdir,
2769 rec->ur_fid2, &de_tgtdir, LCK_PW,
2770 rec->ur_name, rec->ur_namelen,
2771 &de_old, rec->ur_tgt,
2772 rec->ur_tgtlen, &de_new,
2773 dlm_handles, LCK_EX);
2777 cleanup_phase = 1; /* parent(s), children, locks */
2779 if (de_new->d_inode)
2782 /* sanity check for src inode */
2783 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2784 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
2785 GOTO(cleanup, rc = -EINVAL);
2787 /* sanity check for dest inode */
2788 if (de_new->d_inode &&
2789 (de_new->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
2790 de_new->d_inode->i_ino == de_tgtdir->d_inode->i_ino))
2791 GOTO(cleanup, rc = -EINVAL);
2793 if (de_old->d_inode == de_new->d_inode)
2794 GOTO(cleanup, rc = 0);
2796 /* if we are about to remove the target at first, pass the EA of
2797 * that inode to client to perform and cleanup on OST */
2798 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
2799 LASSERT(body != NULL);
2801 if (de_new->d_inode &&
2802 S_ISREG(de_new->d_inode->i_mode) &&
2803 de_new->d_inode->i_nlink == 1 &&
2804 mds_open_orphan_count(de_new->d_inode) == 0) {
2805 mds_pack_inode2fid(obd, &body->fid1, de_new->d_inode);
2806 mds_pack_inode2body(obd, body, de_new->d_inode);
2807 mds_pack_md(obd, req->rq_repmsg, 1, body, de_new->d_inode, 1);
2808 if (!(body->valid & OBD_MD_FLEASIZE)) {
2809 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2810 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2816 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
2817 de_srcdir->d_inode->i_sb);
2819 handle = fsfilt_start(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME, NULL);
2821 GOTO(cleanup, rc = PTR_ERR(handle));
2823 /* FIXME need adjust the journal block count? */
2824 /* if the target should be moved to PENDING, we at first increase the
2825 * link and later vfs_rename() will decrease the link count again */
2826 if (de_new->d_inode &&
2827 S_ISREG(de_new->d_inode->i_mode) &&
2828 de_new->d_inode->i_nlink == 1 &&
2829 mds_open_orphan_count(de_new->d_inode) > 0) {
2830 rc = mds_add_link_orphan(rec, obd, de_new);
2836 de_old->d_fsdata = req;
2837 de_new->d_fsdata = req;
2838 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
2841 if (!rc && log_unlink) {
2842 if (mds_log_op_unlink(obd, de_tgtdir->d_inode,
2843 lustre_msg_buf(req->rq_repmsg, 1, 0),
2844 req->rq_repmsg->buflens[1],
2845 lustre_msg_buf(req->rq_repmsg, 2, 0),
2846 req->rq_repmsg->buflens[2], &lcl) > 0) {
2847 body->valid |= OBD_MD_FLCOOKIE;
2853 rc = mds_finish_transno(mds, de_tgtdir ? de_tgtdir->d_inode : NULL,
2854 handle, req, rc, 0);
2855 switch (cleanup_phase) {
2858 if (dlm_handles[5].cookie != 0)
2859 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2860 if (dlm_handles[6].cookie != 0)
2861 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2864 ptlrpc_save_llog_lock(req, lcl);
2867 if (lock_count == 4)
2868 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
2869 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
2870 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
2871 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
2873 if (lock_count == 4)
2874 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
2875 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
2876 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
2877 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
2886 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2889 req->rq_status = rc;
2893 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
2894 struct ptlrpc_request *, struct lustre_handle *);
2896 static mds_reinter reinters[REINT_MAX + 1] = {
2897 [REINT_SETATTR] mds_reint_setattr,
2898 [REINT_CREATE] mds_reint_create,
2899 [REINT_LINK] mds_reint_link,
2900 [REINT_UNLINK] mds_reint_unlink,
2901 [REINT_RENAME] mds_reint_rename,
2902 [REINT_OPEN] mds_open
2905 int mds_reint_rec(struct mds_update_record *rec, int offset,
2906 struct ptlrpc_request *req, struct lustre_handle *lockh)
2908 struct obd_device *obd = req->rq_export->exp_obd;
2909 struct lvfs_run_ctxt saved;
2912 /* checked by unpacker */
2913 LASSERT(rec->ur_opcode <= REINT_MAX &&
2914 reinters[rec->ur_opcode] != NULL);
2916 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
2917 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
2918 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);