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/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include <linux/lustre_smfs.h>
49 #include "mds_internal.h"
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
59 static void mds_cancel_cookies_cb(struct obd_device *obd,
60 __u64 transno, void *cb_data,
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
90 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
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 obd_device *obd = req->rq_export->exp_obd;
102 struct mds_client_data *mcd = med->med_mcd;
103 int err, log_pri = D_HA;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
112 GOTO(out_commit, rc);
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 if (req->rq_reqmsg->opc == MDS_CLOSE) {
146 mcd->mcd_last_close_transno = cpu_to_le64(transno);
147 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_close_result = cpu_to_le32(rc);
149 mcd->mcd_last_close_data = cpu_to_le32(op_data);
151 mcd->mcd_last_transno = cpu_to_le64(transno);
152 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
153 mcd->mcd_last_result = cpu_to_le32(rc);
154 mcd->mcd_last_data = cpu_to_le32(op_data);
157 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
158 mds_commit_last_transno_cb, NULL);
160 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
161 sizeof(*mcd), &off, 0);
169 DEBUG_REQ(log_pri, req,
170 "wrote trans #"LPU64" client %s at idx %u: err = %d",
171 transno, mcd->mcd_uuid, med->med_idx, err);
173 err = mds_update_last_fid(obd, handle, 0);
180 err = mds_dt_write_objids(obd);
186 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
190 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
191 req->rq_export->exp_sync);
193 CERROR("error committing transaction: %d\n", err);
201 /* this gives the same functionality as the code between
202 * sys_chmod and inode_setattr
203 * chown_common and inode_setattr
204 * utimes and inode_setattr
207 /* Just for the case if we have some clients that know about ATTR_RAW */
208 #define ATTR_RAW 8192
210 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
212 time_t now = LTIME_S(CURRENT_TIME);
213 struct iattr *attr = &rec->ur_iattr;
214 unsigned int ia_valid = attr->ia_valid;
218 /* only fix up attrs if the client VFS didn't already */
220 if (!(ia_valid & ATTR_RAW))
223 if (!(ia_valid & ATTR_CTIME_SET))
224 LTIME_S(attr->ia_ctime) = now;
225 if (!(ia_valid & ATTR_ATIME_SET))
226 LTIME_S(attr->ia_atime) = now;
227 if (!(ia_valid & ATTR_MTIME_SET))
228 LTIME_S(attr->ia_mtime) = now;
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
235 if (rec->ur_fsuid != inode->i_uid &&
236 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
240 if (ia_valid & ATTR_SIZE) {
241 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
245 if (ia_valid & ATTR_UID) {
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 if (attr->ia_uid == (uid_t) -1)
251 attr->ia_uid = inode->i_uid;
252 if (attr->ia_gid == (gid_t) -1)
253 attr->ia_gid = inode->i_gid;
254 attr->ia_mode = inode->i_mode;
256 * If the user or group of a non-directory has been
257 * changed by a non-root user, remove the setuid bit.
258 * 19981026 David C Niemi <niemi@tux.org>
260 * Changed this to apply to all users, including root,
261 * to avoid some races. This is the behavior we had in
262 * 2.0. The check for non-root was definitely wrong
263 * for 2.2 anyway, as it should have been using
264 * CAP_FSETID rather than fsuid -- 19990830 SD.
266 if ((inode->i_mode & S_ISUID) == S_ISUID &&
267 !S_ISDIR(inode->i_mode)) {
268 attr->ia_mode &= ~S_ISUID;
269 attr->ia_valid |= ATTR_MODE;
272 * Likewise, if the user or group of a non-directory
273 * has been changed by a non-root user, remove the
274 * setgid bit UNLESS there is no group execute bit
275 * (this would be a file marked for mandatory
276 * locking). 19981026 David C Niemi <niemi@tux.org>
278 * Removed the fsuid check (see the comment above) --
281 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
282 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
283 attr->ia_mode &= ~S_ISGID;
284 attr->ia_valid |= ATTR_MODE;
286 } else if (ia_valid & ATTR_MODE) {
287 int mode = attr->ia_mode;
289 if (attr->ia_mode == (mode_t) -1)
290 attr->ia_mode = inode->i_mode;
292 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 void mds_steal_ack_locks(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 char str[PTL_NALFMT_SIZE];
301 struct list_head *tmp;
302 struct ptlrpc_reply_state *oldrep;
303 struct ptlrpc_service *svc;
304 struct llog_create_locks *lcl;
308 /* CAVEAT EMPTOR: spinlock order */
309 spin_lock_irqsave (&exp->exp_lock, flags);
310 list_for_each (tmp, &exp->exp_outstanding_replies) {
311 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
313 if (oldrep->rs_xid != req->rq_xid)
316 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
317 CERROR ("Resent req xid "LPX64" has mismatched opc: "
318 "new %d old %d\n", req->rq_xid,
319 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
321 svc = oldrep->rs_srv_ni->sni_service;
322 spin_lock (&svc->srv_lock);
324 list_del_init (&oldrep->rs_exp_list);
326 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
327 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
328 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
329 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
331 for (i = 0; i < oldrep->rs_nlocks; i++)
332 ptlrpc_save_lock(req,
333 &oldrep->rs_locks[i],
334 oldrep->rs_modes[i]);
335 oldrep->rs_nlocks = 0;
337 lcl = oldrep->rs_llog_locks;
338 oldrep->rs_llog_locks = NULL;
340 ptlrpc_save_llog_lock(req, lcl);
342 DEBUG_REQ(D_HA, req, "stole locks for");
343 ptlrpc_schedule_difficult_reply (oldrep);
345 spin_unlock (&svc->srv_lock);
346 spin_unlock_irqrestore (&exp->exp_lock, flags);
349 spin_unlock_irqrestore (&exp->exp_lock, flags);
352 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
354 if (req->rq_reqmsg->opc == MDS_CLOSE) {
355 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
356 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
357 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
358 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
360 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
361 mcd->mcd_last_transno, mcd->mcd_last_result);
362 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
363 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
366 mds_steal_ack_locks(req);
369 static void reconstruct_reint_setattr(struct mds_update_record *rec,
370 int offset, struct ptlrpc_request *req)
372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
373 struct mds_body *body;
376 mds_req_from_mcd(req, med->med_mcd);
378 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
380 LASSERT(PTR_ERR(de) == req->rq_status);
384 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
385 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
387 /* Don't return OST-specific attributes if we didn't just set them */
388 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
389 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
390 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
391 body->valid |= OBD_MD_FLMTIME;
392 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
393 body->valid |= OBD_MD_FLATIME;
398 /* In the raw-setattr case, we lock the child inode.
399 * In the write-back case or if being called from open, the client holds a lock
402 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
403 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
404 struct ptlrpc_request *req, struct lustre_handle *lh)
406 struct mds_obd *mds = mds_req2mds(req);
407 struct obd_device *obd = req->rq_export->exp_obd;
408 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
409 struct mds_body *body;
410 struct dentry *de = NULL;
411 struct inode *inode = NULL;
412 struct lustre_handle lockh[2] = {{0}, {0}};
415 struct mds_logcancel_data *mlcd = NULL;
416 int rc = 0, cleanup_phase = 0, err;
420 LASSERT(offset == 1);
422 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
423 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
424 rec->ur_iattr.ia_valid);
426 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
427 MD_COUNTER_INCREMENT(obd, setattr);
429 if (med->med_remote) {
430 if (rec->ur_iattr.ia_valid & ATTR_GID) {
431 CWARN("Deny chgrp from remote client\n");
432 GOTO(cleanup, rc = -EPERM);
434 if (rec->ur_iattr.ia_valid & ATTR_UID) {
437 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
438 rec->ur_iattr.ia_uid);
439 if (uid == MDS_IDMAP_NOTFOUND) {
440 CWARN("Deny chown to uid %u\n",
441 rec->ur_iattr.ia_uid);
442 GOTO(cleanup, rc = -EPERM);
444 rec->ur_iattr.ia_uid = uid;
448 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
449 de = mds_id2dentry(obd, rec->ur_id1, NULL);
451 GOTO(cleanup, rc = PTR_ERR(de));
453 __u64 lockpart = MDS_INODELOCK_UPDATE;
454 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
455 lockpart |= MDS_INODELOCK_LOOKUP;
456 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
457 lockh, &parent_mode, NULL, 0, lockpart);
459 GOTO(cleanup, rc = PTR_ERR(de));
467 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
468 rec->ur_eadata != NULL)
471 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
473 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
475 GOTO(cleanup, rc = PTR_ERR(handle));
477 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
478 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
479 LTIME_S(rec->ur_iattr.ia_mtime),
480 LTIME_S(rec->ur_iattr.ia_ctime));
481 rc = mds_fix_attr(inode, rec);
485 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
486 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
487 (long)&rec->ur_iattr.ia_attr_flags);
489 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
492 if (rec->ur_iattr.ia_valid & ATTR_EA) {
493 int flags = (int)rec->ur_iattr.ia_attr_flags;
496 if (inode->i_op && inode->i_op->setxattr)
497 rc = inode->i_op->setxattr(de, rec->ur_eadata,
498 rec->ur_ea2data, rec->ur_ea2datalen,
500 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
502 if (inode->i_op && inode->i_op->removexattr)
503 rc = inode->i_op->removexattr(de, rec->ur_eadata);
504 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
505 struct lov_stripe_md *lsm = NULL;
506 struct lov_user_md *lum = NULL;
508 if (rec->ur_eadata != NULL) {
509 rc = ll_permission(inode, MAY_WRITE, NULL);
513 lum = rec->ur_eadata;
515 /* if lmm_stripe_size is -1 delete default
517 if (S_ISDIR(inode->i_mode) &&
518 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
519 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
523 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
525 &lsm, rec->ur_eadata);
529 obd_free_memmd(mds->mds_dt_exp, &lsm);
530 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
531 rec->ur_eadatalen, EA_LOV);
539 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
540 mds_pack_inode2body(obd, body, inode, 1);
542 /* Don't return OST-specific attributes if we didn't just set them */
543 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
544 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
545 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
546 body->valid |= OBD_MD_FLMTIME;
547 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
548 body->valid |= OBD_MD_FLATIME;
550 mds_body_do_reverse_map(med, body);
552 /* The logcookie should be no use anymore, why nobody remove
553 * following code block?
555 LASSERT(rec->ur_cookielen == 0);
556 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
557 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
560 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
562 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
563 mlcd->mlcd_cookielen = rec->ur_cookielen;
564 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
565 mlcd->mlcd_cookielen;
566 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
567 mlcd->mlcd_cookielen);
568 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
569 mlcd->mlcd_eadatalen);
571 CERROR("unable to allocate log cancel data\n");
577 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
578 handle, mds_cancel_cookies_cb, mlcd);
579 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
580 switch (cleanup_phase) {
582 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
583 rec->ur_eadata != NULL)
588 if (lockh[1].cookie != 0)
589 ldlm_lock_decref(lockh + 1, parent_mode);
592 ldlm_lock_decref(lockh, LCK_PW);
594 ptlrpc_save_lock (req, lockh, LCK_PW);
609 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
610 struct ptlrpc_request *req)
612 struct mds_export_data *med = &req->rq_export->exp_mds_data;
613 struct dentry *parent, *child;
614 struct mds_body *body;
617 mds_req_from_mcd(req, med->med_mcd);
619 if (req->rq_status) {
624 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
625 LASSERT(!IS_ERR(parent));
626 child = ll_lookup_one_len(rec->ur_name, parent,
627 rec->ur_namelen - 1);
628 LASSERT(!IS_ERR(child));
629 if ((child->d_flags & DCACHE_CROSS_REF)) {
630 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
631 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
632 mds_pack_dentry2body(req2obd(req), body, child, 1);
633 } else if (child->d_inode == NULL) {
634 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
635 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
636 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
638 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
639 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
646 static int mds_get_default_acl(struct inode *dir, void **pacl)
648 struct dentry de = { .d_inode = dir };
651 LASSERT(S_ISDIR(dir->i_mode));
653 if (!dir->i_op->getxattr)
656 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
657 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
662 OBD_ALLOC(*pacl, size);
666 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
668 /* since we already locked the dir, it should not change
669 * between the 2 getxattr calls
671 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
672 OBD_FREE(*pacl, size);
679 static int mds_reint_create(struct mds_update_record *rec, int offset,
680 struct ptlrpc_request *req,
681 struct lustre_handle *lh)
683 struct dentry *dparent = NULL;
684 struct mds_obd *mds = mds_req2mds(req);
685 struct obd_device *obd = req->rq_export->exp_obd;
686 struct dentry *dchild = NULL;
687 struct inode *dir = NULL;
689 struct lustre_handle lockh[2] = {{0}, {0}};
691 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
693 struct dentry_params dp;
694 struct mea *mea = NULL;
698 LASSERT(offset == 1);
700 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
701 OBD_MDS_DEVICENAME));
703 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
704 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
705 rec->ur_name, rec->ur_mode);
707 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
709 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
710 GOTO(cleanup, rc = -ESTALE);
712 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
713 lockh, &parent_mode, rec->ur_name,
714 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
715 if (IS_ERR(dparent)) {
716 rc = PTR_ERR(dparent);
717 CERROR("parent lookup error %d\n", rc);
720 cleanup_phase = 1; /* locked parent dentry */
721 dir = dparent->d_inode;
724 ldlm_lock_dump_handle(D_OTHER, lockh);
726 /* try to retrieve MEA data for this dir */
727 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
733 * dir is already splitted, check is requested filename should
734 * live at this MDS or at another one.
736 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
737 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
738 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
739 " should be %lu(%d)\n",
740 mea->mea_master, dparent->d_inode->i_ino,
741 dparent->d_inode->i_generation, rec->ur_name,
742 (unsigned long)id_group(&mea->mea_ids[i]), i);
743 GOTO(cleanup, rc = -ERESTART);
747 dchild = ll_lookup_one_len(rec->ur_name, dparent,
748 rec->ur_namelen - 1);
749 if (IS_ERR(dchild)) {
750 rc = PTR_ERR(dchild);
751 CERROR("Can't find "DLID4"/%s, error %d\n",
752 OLID4(rec->ur_id1), rec->ur_name, rc);
756 cleanup_phase = 2; /* child dentry */
758 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
760 if (type == S_IFREG || type == S_IFDIR) {
761 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
762 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
763 obd->obd_name, dparent->d_inode->i_ino,
764 dparent->d_inode->i_generation, rc, parent_mode);
766 /* dir got splitted */
767 GOTO(cleanup, rc = -ERESTART);
769 /* error happened during spitting. */
774 if (dir->i_mode & S_ISGID) {
775 if (S_ISDIR(rec->ur_mode))
776 rec->ur_mode |= S_ISGID;
780 * here inode number should be used only in the case of replaying. It is
781 * needed to check if object already created in the case of creating
784 dchild->d_fsdata = (void *)&dp;
785 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
790 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
792 GOTO(cleanup, rc = PTR_ERR(handle));
793 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
795 if (rc == 0 && rec->ur_eadata) {
796 /*for CMOBD to set lov md info when cmobd reint create*/
797 CDEBUG(D_INFO, "set lsm %p, len %d to inode %lu \n",
798 rec->ur_eadata, rec->ur_eadatalen,
799 dchild->d_inode->i_ino);
800 fsfilt_set_md(obd, dchild->d_inode, handle, rec->ur_eadata,
801 rec->ur_eadatalen, EA_LOV);
810 * as Peter asked, mkdir() should distribute new directories
811 * over the whole cluster in order to distribute namespace
812 * processing load. first, we calculate which MDS to use to put
813 * new directory's inode in.
815 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
816 rec->ur_flags, &req->rq_peer, dir);
817 if (i == mds->mds_num) {
818 /* inode will be created locally */
819 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
821 GOTO(cleanup, rc = PTR_ERR(handle));
823 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
826 "Can't create dir \"%s\", rc = %d\n",
827 dchild->d_name.name, rc);
831 down(&dchild->d_inode->i_sem);
833 rc = mds_update_inode_sid(obd, dchild->d_inode,
834 handle, rec->ur_id2);
836 CERROR("mds_update_inode_sid() failed, inode %lu, "
837 "rc %d\n", dchild->d_inode->i_ino, rc);
841 * make sure, that fid is up-to-date.
843 mds_set_last_fid(obd, id_fid(rec->ur_id2));
845 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
848 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
849 "rc %d\n", dchild->d_inode->i_ino, rc);
852 up(&dchild->d_inode->i_sem);
858 nstripes = *(u16 *)rec->ur_eadata;
860 if (rc == 0 && nstripes) {
862 * we pass LCK_EX to split routine to signal,
863 * that we have exclusive access to the
864 * directory. Simple because nobody knows it
865 * already exists -bzzz
867 rc = mds_try_to_split_dir(obd, dchild,
871 /* dir got splitted */
874 /* an error occured during
879 } else if (!DENTRY_VALID(dchild)) {
880 /* inode will be created on another MDS */
881 struct obdo *oa = NULL;
882 struct mds_body *body;
886 /* first, create that inode */
889 GOTO(cleanup, rc = -ENOMEM);
894 if (rec->ur_eadata) {
895 /* user asks for creating splitted dir */
896 oa->o_easize = *((u16 *) rec->ur_eadata);
899 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
900 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
902 /* adjust the uid/gid/mode bits */
903 oa->o_mode = rec->ur_mode;
904 oa->o_uid = current->fsuid;
905 oa->o_gid = (dir->i_mode & S_ISGID) ?
906 dir->i_gid : current->fsgid;
907 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
909 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
912 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
914 * here inode number and generation are
915 * important, as this is replay request and we
916 * need them to check if such an object is
919 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
920 obd->obd_name, rec->ur_namelen - 1,
921 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
922 (unsigned)id_gen(rec->ur_id2));
923 oa->o_id = id_ino(rec->ur_id2);
924 oa->o_fid = id_fid(rec->ur_id2);
925 oa->o_generation = id_gen(rec->ur_id2);
926 oa->o_flags |= OBD_FL_RECREATE_OBJS;
927 LASSERT(oa->o_fid != 0);
930 /* obtain default ACL */
931 acl_size = mds_get_default_acl(dir, &acl);
934 GOTO(cleanup, rc = -ENOMEM);
938 * before obd_create() is called, o_fid is not known if
939 * this is not recovery of cause.
941 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
945 OBD_FREE(acl, acl_size);
948 CERROR("can't create remote inode: %d\n", rc);
949 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
950 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
951 rec->ur_name, rec->ur_mode);
956 LASSERT(oa->o_fid != 0);
958 /* now, add new dir entry for it */
959 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
960 if (IS_ERR(handle)) {
962 GOTO(cleanup, rc = PTR_ERR(handle));
965 /* creating local dentry for remote inode. */
966 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
967 rec->ur_namelen - 1, oa->o_id,
968 oa->o_generation, i, oa->o_fid);
971 CERROR("Can't create local entry %*s for "
972 "remote inode.\n", rec->ur_namelen - 1,
978 body = lustre_msg_buf(req->rq_repmsg,
980 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
982 obdo2id(&body->id1, oa);
985 /* requested name exists in the directory */
992 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
994 GOTO(cleanup, rc = PTR_ERR(handle));
995 if (rec->ur_tgt == NULL) /* no target supplied */
996 rc = -EINVAL; /* -EPROTO? */
998 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1006 int rdev = rec->ur_rdev;
1007 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1009 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1010 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1015 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1016 dchild->d_fsdata = NULL;
1017 GOTO(cleanup, rc = -EINVAL);
1020 /* In case we stored the desired inum in here, we want to clean up. */
1021 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1022 dchild->d_fsdata = NULL;
1025 CDEBUG(D_INODE, "error during create: %d\n", rc);
1027 } else if (dchild->d_inode) {
1028 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1030 struct mds_body *body;
1031 struct inode *inode = dchild->d_inode;
1034 iattr.ia_uid = rec->ur_fsuid;
1035 LTIME_S(iattr.ia_atime) = rec->ur_time;
1036 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1037 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1039 if (dir->i_mode & S_ISGID)
1040 iattr.ia_gid = dir->i_gid;
1042 iattr.ia_gid = rec->ur_fsgid;
1044 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1045 ATTR_MTIME | ATTR_CTIME;
1047 if (id_ino(rec->ur_id2)) {
1048 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1049 inode->i_generation = id_gen(rec->ur_id2);
1051 if (type != S_IFDIR) {
1052 down(&inode->i_sem);
1053 rc = mds_update_inode_sid(obd, inode,
1054 handle, rec->ur_id2);
1057 CERROR("Can't update inode self id, "
1062 * make sure, that fid is up-to-date.
1064 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1067 /* dirtied and committed by the upcoming setattr. */
1068 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1069 inode->i_ino, inode->i_generation);
1071 struct lustre_handle child_ino_lockh;
1073 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1074 inode->i_ino, inode->i_generation);
1076 if (type != S_IFDIR) {
1078 * allocate new id for @inode if it is not dir,
1079 * because for dir it was already done.
1081 down(&inode->i_sem);
1082 rc = mds_alloc_inode_sid(obd, inode,
1086 CERROR("mds_alloc_inode_sid() failed, "
1087 "inode %lu, rc %d\n", inode->i_ino,
1094 * the inode we were allocated may have just
1095 * been freed by an unlink operation. We take
1096 * this lock to synchronize against the matching
1097 * reply-ack-lock taken in unlink, to avoid
1098 * replay problems if this reply makes it out to
1099 * the client but the unlink's does not. See
1100 * bug 2029 for more detail.
1102 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1103 if (rc != ELDLM_OK) {
1104 CERROR("error locking for unlink/create sync: "
1107 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1112 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1114 CERROR("error on child setattr: rc = %d\n", rc);
1116 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1117 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1119 CERROR("error on parent setattr: rc = %d\n", rc);
1121 MD_COUNTER_INCREMENT(obd, create);
1123 /* take care of default stripe inheritance */
1124 if (type == S_IFDIR) {
1125 struct lov_mds_md lmm;
1126 int lmm_size = sizeof(lmm);
1128 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1130 down(&inode->i_sem);
1131 rc = fsfilt_set_md(obd, inode, handle,
1132 &lmm, lmm_size, EA_LOV);
1136 CERROR("error on copy stripe info: rc = %d\n",
1142 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1143 mds_pack_inode2body(obd, body, inode, 1);
1144 mds_body_do_reverse_map(med, body);
1149 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1151 if (rc && created) {
1152 /* Destroy the file we just created. This should not need extra
1153 * journal credits, as we have already modified all of the
1154 * blocks needed in order to create the file in the first
1158 err = vfs_rmdir(dir, dchild);
1160 CERROR("rmdir in error path: %d\n", err);
1163 err = vfs_unlink(dir, dchild);
1165 CERROR("unlink in error path: %d\n", err);
1171 switch (cleanup_phase) {
1172 case 2: /* child dentry */
1174 case 1: /* locked parent dentry */
1176 if (lockh[1].cookie != 0)
1177 ldlm_lock_decref(lockh + 1, parent_mode);
1180 ldlm_lock_decref(lockh, LCK_PW);
1182 ptlrpc_save_lock(req, lockh, LCK_PW);
1188 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1192 OBD_FREE(mea, mea_size);
1193 req->rq_status = rc;
1198 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1199 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1203 for (i = 0; i < RES_NAME_SIZE; i++) {
1205 * this is needed to make zeroed res_id entries to be put at the
1206 * end of list in *ordered_locks() .
1208 if (res1->name[i] == 0 && res2->name[i] != 0)
1210 if (res2->name[i] == 0 && res1->name[i] != 0)
1212 if (res1->name[i] > res2->name[i])
1214 if (res1->name[i] < res2->name[i])
1221 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1227 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1228 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1229 * because they take the place of local semaphores.
1231 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1232 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1233 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1234 struct lustre_handle *p1_lockh, int p1_lock_mode,
1235 ldlm_policy_data_t *p1_policy,
1236 struct ldlm_res_id *p2_res_id,
1237 struct lustre_handle *p2_lockh, int p2_lock_mode,
1238 ldlm_policy_data_t *p2_policy)
1240 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1241 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1242 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1243 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1247 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1249 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1250 res_id[0]->name[0], res_id[1]->name[0]);
1252 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1253 handles[1] = p1_lockh;
1254 handles[0] = p2_lockh;
1255 res_id[1] = p1_res_id;
1256 res_id[0] = p2_res_id;
1257 lock_modes[1] = p1_lock_mode;
1258 lock_modes[0] = p2_lock_mode;
1259 policies[1] = p1_policy;
1260 policies[0] = p2_policy;
1263 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1264 res_id[0]->name[0], res_id[1]->name[0]);
1266 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1267 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1268 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1269 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1270 NULL, 0, NULL, handles[0]);
1273 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1275 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1276 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1277 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1278 ldlm_lock_addref(handles[1], lock_modes[1]);
1279 } else if (res_id[1]->name[0] != 0) {
1280 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1281 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1282 *res_id[1], LDLM_IBITS, policies[1],
1283 lock_modes[1], &flags, mds_blocking_ast,
1284 ldlm_completion_ast, NULL, NULL, NULL, 0,
1286 if (rc != ELDLM_OK) {
1287 ldlm_lock_decref(handles[0], lock_modes[0]);
1290 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1296 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1297 struct lustre_handle *p1_lockh, int p1_lock_mode,
1298 ldlm_policy_data_t *p1_policy,
1299 struct ldlm_res_id *p2_res_id,
1300 struct lustre_handle *p2_lockh, int p2_lock_mode,
1301 ldlm_policy_data_t *p2_policy,
1302 struct ldlm_res_id *c1_res_id,
1303 struct lustre_handle *c1_lockh, int c1_lock_mode,
1304 ldlm_policy_data_t *c1_policy,
1305 struct ldlm_res_id *c2_res_id,
1306 struct lustre_handle *c2_lockh, int c2_lock_mode,
1307 ldlm_policy_data_t *c2_policy)
1309 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1310 c1_res_id, c2_res_id };
1311 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1312 c1_lockh, c2_lockh };
1313 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1314 c1_lock_mode, c2_lock_mode };
1315 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1316 c1_policy, c2_policy};
1317 int rc, i, j, sorted, flags;
1320 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1321 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1322 res_id[3]->name[0]);
1325 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1326 * res_id should be at the end of list after sorting is finished.
1328 for (i = 1; i < 4; i++) {
1330 dlm_handles[4] = dlm_handles[i];
1331 res_id[4] = res_id[i];
1332 lock_modes[4] = lock_modes[i];
1333 policies[4] = policies[i];
1337 if (res_gt(res_id[j], res_id[4], policies[j],
1339 dlm_handles[j + 1] = dlm_handles[j];
1340 res_id[j + 1] = res_id[j];
1341 lock_modes[j + 1] = lock_modes[j];
1342 policies[j + 1] = policies[j];
1347 } while (j >= 0 && !sorted);
1349 dlm_handles[j + 1] = dlm_handles[4];
1350 res_id[j + 1] = res_id[4];
1351 lock_modes[j + 1] = lock_modes[4];
1352 policies[j + 1] = policies[4];
1355 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1356 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1357 res_id[3]->name[0]);
1359 /* XXX we could send ASTs on all these locks first before blocking? */
1360 for (i = 0; i < 4; i++) {
1364 * nevertheless zeroed res_ids should be at the end of list, and
1365 * could use break here, I think, that it is more correctly for
1366 * clear understanding of code to have continue here, as it
1367 * clearly means, that zeroed res_id should be skipped and does
1368 * not mean, that if we meet zeroed res_id we should stop
1371 if (res_id[i]->name[0] == 0)
1375 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1376 (policies[i]->l_inodebits.bits &
1377 policies[i-1]->l_inodebits.bits) ) {
1378 memcpy(dlm_handles[i], dlm_handles[i-1],
1379 sizeof(*(dlm_handles[i])));
1380 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1382 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1383 *res_id[i], LDLM_IBITS,
1385 lock_modes[i], &flags,
1387 ldlm_completion_ast, NULL, NULL,
1388 NULL, 0, NULL, dlm_handles[i]);
1390 GOTO(out_err, rc = -EIO);
1391 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1398 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1403 /* In the unlikely case that the child changed while we were waiting
1404 * on the lock, we need to drop the lock on the old child and either:
1405 * - if the child has a lower resource name, then we have to also
1406 * drop the parent lock and regain the locks in the right order
1407 * - in the rename case, if the child has a lower resource name than one of
1408 * the other parent/child resources (maxres) we also need to reget the locks
1409 * - if the child has a higher resource name (this is the common case)
1410 * we can just get the lock on the new child (still in lock order)
1412 * Returns 0 if the child did not change or if it changed but could be locked.
1413 * Returns 1 if the child changed and we need to re-lock (no locks held).
1414 * Returns -ve error with a valid dchild (no locks held). */
1415 static int mds_verify_child(struct obd_device *obd,
1416 struct ldlm_res_id *parent_res_id,
1417 struct lustre_handle *parent_lockh,
1418 struct dentry *dparent, int parent_mode,
1419 struct ldlm_res_id *child_res_id,
1420 struct lustre_handle *child_lockh,
1421 struct dentry **dchildp, int child_mode,
1422 ldlm_policy_data_t *child_policy,
1423 const char *name, int namelen,
1424 struct ldlm_res_id *maxres,
1425 unsigned long child_ino, __u32 child_gen)
1427 struct lustre_id sid;
1428 struct dentry *vchild, *dchild = *dchildp;
1429 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1432 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1434 GOTO(cleanup, rc = PTR_ERR(vchild));
1436 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1437 if (child_gen == vchild->d_generation &&
1438 child_ino == vchild->d_inum) {
1447 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1448 (vchild->d_inode != NULL &&
1449 child_gen == vchild->d_inode->i_generation &&
1450 child_ino == vchild->d_inode->i_ino))) {
1458 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1459 vchild->d_inode, dchild ? dchild->d_inode : 0,
1460 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1461 child_res_id->name[0]);
1463 if (child_res_id->name[0] != 0)
1464 ldlm_lock_decref(child_lockh, child_mode);
1468 cleanup_phase = 1; /* parent lock only */
1469 *dchildp = dchild = vchild;
1471 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1472 int flags = LDLM_FL_ATOMIC_CB;
1474 if (dchild->d_inode) {
1475 down(&dchild->d_inode->i_sem);
1476 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1477 up(&dchild->d_inode->i_sem);
1479 CERROR("Can't read inode self id, inode %lu,"
1480 " rc %d\n", dchild->d_inode->i_ino, rc);
1483 child_res_id->name[0] = id_fid(&sid);
1484 child_res_id->name[1] = id_group(&sid);
1486 child_res_id->name[0] = dchild->d_fid;
1487 child_res_id->name[1] = dchild->d_mdsnum;
1490 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1491 res_gt(maxres, child_res_id, NULL, NULL)) {
1492 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1493 child_res_id->name[0], parent_res_id->name[0],
1495 GOTO(cleanup, rc = 1);
1498 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1499 *child_res_id, LDLM_IBITS, child_policy,
1500 child_mode, &flags, mds_blocking_ast,
1501 ldlm_completion_ast, NULL, NULL, NULL, 0,
1504 GOTO(cleanup, rc = -EIO);
1507 memset(child_res_id, 0, sizeof(*child_res_id));
1513 switch(cleanup_phase) {
1515 if (child_res_id->name[0] != 0)
1516 ldlm_lock_decref(child_lockh, child_mode);
1518 ldlm_lock_decref(parent_lockh, parent_mode);
1524 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1525 struct lustre_id *id,
1526 struct lustre_handle *parent_lockh,
1527 struct dentry **dparentp, int parent_mode,
1528 __u64 parent_lockpart, int *update_mode,
1529 char *name, int namelen,
1530 struct lustre_handle *child_lockh,
1531 struct dentry **dchildp, int child_mode,
1532 __u64 child_lockpart)
1534 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1535 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1536 struct ldlm_res_id parent_res_id = { .name = {0} };
1537 struct ldlm_res_id child_res_id = { .name = {0} };
1538 unsigned long child_ino = 0; __u32 child_gen = 0;
1539 int rc = 0, cleanup_phase = 0;
1540 struct lustre_id sid;
1541 struct inode *inode;
1544 /* Step 1: Lookup parent */
1545 *dparentp = mds_id2dentry(obd, id, NULL);
1546 if (IS_ERR(*dparentp)) {
1547 rc = PTR_ERR(*dparentp);
1552 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1553 (*dparentp)->d_inode->i_ino, name);
1555 parent_res_id.name[0] = id_fid(id);
1556 parent_res_id.name[1] = id_group(id);
1559 parent_lockh[1].cookie = 0;
1560 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1561 struct ldlm_res_id res_id = { .name = {0} };
1562 ldlm_policy_data_t policy;
1563 int flags = LDLM_FL_ATOMIC_CB;
1565 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1567 res_id.name[0] = id_fid(id);
1568 res_id.name[1] = id_group(id);
1569 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1571 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1572 res_id, LDLM_IBITS, &policy,
1573 *update_mode, &flags,
1575 ldlm_completion_ast,
1576 NULL, NULL, NULL, 0, NULL,
1582 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1585 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1586 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1587 parent_res_id.name[2]);
1591 cleanup_phase = 1; /* parent dentry */
1593 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1594 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1595 if (IS_ERR(*dchildp)) {
1596 rc = PTR_ERR(*dchildp);
1597 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1601 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1603 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1604 * lock. Drop possible UPDATE lock!
1606 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1607 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1609 child_res_id.name[0] = (*dchildp)->d_fid;
1610 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1611 child_ino = (*dchildp)->d_inum;
1612 child_gen = (*dchildp)->d_generation;
1616 inode = (*dchildp)->d_inode;
1618 inode = igrab(inode);
1622 down(&inode->i_sem);
1623 rc = mds_read_inode_sid(obd, inode, &sid);
1626 CERROR("Can't read inode self id, inode %lu, "
1627 "rc %d\n", inode->i_ino, rc);
1632 child_ino = inode->i_ino;
1633 child_gen = inode->i_generation;
1634 child_res_id.name[0] = id_fid(&sid);
1635 child_res_id.name[1] = id_group(&sid);
1639 cleanup_phase = 2; /* child dentry */
1641 /* Step 3: Lock parent and child in resource order. If child doesn't
1642 * exist, we still have to lock the parent and re-lookup. */
1643 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1644 &parent_policy, &child_res_id, child_lockh,
1645 child_mode, &child_policy);
1649 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1650 cleanup_phase = 4; /* child lock */
1652 cleanup_phase = 3; /* parent lock */
1654 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1655 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1656 parent_mode, &child_res_id, child_lockh,
1657 dchildp, child_mode, &child_policy,
1658 name, namelen, &parent_res_id, child_ino,
1670 switch (cleanup_phase) {
1672 ldlm_lock_decref(child_lockh, child_mode);
1674 ldlm_lock_decref(parent_lockh, parent_mode);
1679 if (parent_lockh[1].cookie)
1680 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1688 void mds_reconstruct_generic(struct ptlrpc_request *req)
1690 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1691 mds_req_from_mcd(req, med->med_mcd);
1694 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1695 * instead link the inode into the PENDING directory until it is finally
1696 * released. We can't simply call mds_reint_rename() or some part thereof,
1697 * because we don't have the inode to check for link count/open status until
1698 * after it is locked.
1700 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1701 * before starting journal transaction.
1703 * returns 1 on success
1704 * returns 0 if we lost a race and didn't make a new link
1705 * returns negative on error
1707 static int mds_orphan_add_link(struct mds_update_record *rec,
1708 struct obd_device *obd, struct dentry *dentry)
1710 struct mds_obd *mds = &obd->u.mds;
1711 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1712 struct inode *inode = dentry->d_inode;
1713 struct dentry *pending_child;
1714 char idname[LL_ID_NAMELEN];
1715 int idlen = 0, rc, mode;
1718 LASSERT(inode != NULL);
1719 LASSERT(!mds_inode_is_orphan(inode));
1720 #ifndef HAVE_I_ALLOC_SEM
1721 LASSERT(down_trylock(&inode->i_sem) != 0);
1723 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1725 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1727 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1728 mds_orphan_open_count(inode), inode->i_nlink,
1729 S_ISDIR(inode->i_mode) ? "dir" :
1730 S_ISREG(inode->i_mode) ? "file" : "other",
1731 rec->ur_name, idname);
1733 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1736 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1737 if (IS_ERR(pending_child))
1738 RETURN(PTR_ERR(pending_child));
1740 if (pending_child->d_inode != NULL) {
1741 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1742 LASSERT(pending_child->d_inode == inode);
1743 GOTO(out_dput, rc = 0);
1747 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1748 * linking and return real mode back then -bzzz
1750 mode = inode->i_mode;
1751 inode->i_mode = S_IFREG;
1752 rc = vfs_link(dentry, pending_dir, pending_child);
1754 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1757 mds_inode_set_orphan(inode);
1759 /* return mode and correct i_nlink if inode is directory */
1760 inode->i_mode = mode;
1761 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1762 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1764 if (S_ISDIR(mode)) {
1766 i_nlink_inc(pending_dir);
1767 mark_inode_dirty(inode);
1768 mark_inode_dirty(pending_dir);
1771 GOTO(out_dput, rc = 1);
1773 l_dput(pending_child);
1777 int mds_create_local_dentry(struct mds_update_record *rec,
1778 struct obd_device *obd)
1780 struct mds_obd *mds = &obd->u.mds;
1781 struct inode *id_dir = mds->mds_id_dir->d_inode;
1782 int idlen = 0, rc, cleanup_phase = 0;
1783 struct dentry *new_child = NULL;
1784 char *idname = rec->ur_name;
1785 struct dentry *child = NULL;
1786 struct lustre_handle lockh[2] = {{0}, {0}};
1787 struct lustre_id sid;
1791 down(&id_dir->i_sem);
1792 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1793 id_gen(rec->ur_id1));
1795 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1796 idname, OLID4(rec->ur_id1));
1798 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1801 if (IS_ERR(new_child)) {
1802 CERROR("can't lookup %s: %d\n", idname,
1803 (int) PTR_ERR(new_child));
1804 GOTO(cleanup, rc = PTR_ERR(new_child));
1808 down(&id_dir->i_sem);
1809 rc = mds_read_inode_sid(obd, id_dir, &sid);
1812 CERROR("Can't read inode self id, inode %lu, "
1813 "rc %d\n", id_dir->i_ino, rc);
1817 if (new_child->d_inode != NULL) {
1818 /* nice. we've already have local dentry! */
1819 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1820 (unsigned)new_child->d_inode->i_ino,
1821 (unsigned)new_child->d_inode->i_generation);
1823 id_ino(rec->ur_id1) = id_dir->i_ino;
1824 id_gen(rec->ur_id1) = id_dir->i_generation;
1825 rec->ur_namelen = idlen + 1;
1827 id_fid(rec->ur_id1) = id_fid(&sid);
1828 id_group(rec->ur_id1) = id_group(&sid);
1830 GOTO(cleanup, rc = 0);
1833 /* new, local dentry will be added soon. we need no aliases here */
1836 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1837 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1839 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1840 LCK_EX, lockh, NULL, NULL, 0,
1841 MDS_INODELOCK_UPDATE);
1844 if (IS_ERR(child)) {
1845 rc = PTR_ERR(child);
1846 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1847 CERROR("can't get victim: %d\n", rc);
1852 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1854 GOTO(cleanup, rc = PTR_ERR(handle));
1856 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1857 idlen, id_ino(rec->ur_id1),
1858 id_gen(rec->ur_id1), mds->mds_num,
1859 id_fid(rec->ur_id1));
1861 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1862 (unsigned long)child->d_inode->i_ino,
1863 (unsigned long)child->d_inode->i_generation, rc);
1865 if (S_ISDIR(child->d_inode->i_mode)) {
1866 i_nlink_inc(id_dir);
1867 mark_inode_dirty(id_dir);
1869 mark_inode_dirty(child->d_inode);
1871 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1873 id_ino(rec->ur_id1) = id_dir->i_ino;
1874 id_gen(rec->ur_id1) = id_dir->i_generation;
1875 rec->ur_namelen = idlen + 1;
1877 id_fid(rec->ur_id1) = id_fid(&sid);
1878 id_group(rec->ur_id1) = id_group(&sid);
1882 switch(cleanup_phase) {
1884 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1885 ldlm_lock_decref(lockh, LCK_EX);
1895 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1896 struct ptlrpc_request *slave)
1898 void *cookie, *cookie2;
1899 struct mds_body *body2;
1900 struct mds_body *body;
1904 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1905 LASSERT(body != NULL);
1907 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1908 LASSERT(body2 != NULL);
1910 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1913 memcpy(body2, body, sizeof(*body));
1914 body2->valid &= ~OBD_MD_FLCOOKIE;
1916 if (!(body->valid & OBD_MD_FLEASIZE) &&
1917 !(body->valid & OBD_MD_FLDIREA))
1920 if (body->eadatasize == 0) {
1921 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1925 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1927 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1928 LASSERT(ea != NULL);
1930 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1931 LASSERT(ea2 != NULL);
1933 memcpy(ea2, ea, body->eadatasize);
1935 if (body->valid & OBD_MD_FLCOOKIE) {
1936 LASSERT(master->rq_repmsg->buflens[2] >=
1937 slave->rq_repmsg->buflens[2]);
1938 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1939 slave->rq_repmsg->buflens[2]);
1940 LASSERT(cookie != NULL);
1942 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1943 master->rq_repmsg->buflens[2]);
1944 LASSERT(cookie2 != NULL);
1945 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1946 body2->valid |= OBD_MD_FLCOOKIE;
1951 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1952 int offset, struct ptlrpc_request *req,
1953 struct lustre_handle *parent_lockh,
1954 int update_mode, struct dentry *dparent,
1955 struct lustre_handle *child_lockh,
1956 struct dentry *dchild)
1958 struct obd_device *obd = req->rq_export->exp_obd;
1959 struct mds_obd *mds = mds_req2mds(req);
1960 struct ptlrpc_request *request = NULL;
1961 int rc = 0, cleanup_phase = 0;
1962 struct mdc_op_data *op_data;
1966 LASSERT(offset == 1 || offset == 3);
1968 /* time to drop i_nlink on remote MDS */
1969 OBD_ALLOC(op_data, sizeof(*op_data));
1970 if (op_data == NULL)
1973 memset(op_data, 0, sizeof(*op_data));
1974 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1975 op_data->create_mode = rec->ur_mode;
1977 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1978 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1980 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1981 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1982 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1985 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1986 op_data->create_mode |= MDS_MODE_REPLAY;
1988 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1989 OBD_FREE(op_data, sizeof(*op_data));
1994 mds_copy_unlink_reply(req, request);
1995 ptlrpc_req_finished(request);
1999 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2002 GOTO(cleanup, rc = PTR_ERR(handle));
2003 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2004 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2009 req->rq_status = rc;
2012 if (parent_lockh[1].cookie != 0)
2013 ldlm_lock_decref(parent_lockh + 1, update_mode);
2015 ldlm_lock_decref(child_lockh, LCK_EX);
2017 ldlm_lock_decref(parent_lockh, LCK_PW);
2019 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2026 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2027 struct ptlrpc_request *req,
2028 struct lustre_handle *lh)
2030 struct dentry *dparent = NULL, *dchild;
2031 struct mds_obd *mds = mds_req2mds(req);
2032 struct obd_device *obd = req->rq_export->exp_obd;
2033 struct mds_body *body = NULL;
2034 struct inode *child_inode = NULL;
2035 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2036 struct lustre_handle child_lockh = {0};
2037 struct lustre_handle child_reuse_lockh = {0};
2038 struct lustre_handle *slave_lockh = NULL;
2039 char idname[LL_ID_NAMELEN];
2040 struct llog_create_locks *lcl = NULL;
2041 void *handle = NULL;
2042 int rc = 0, cleanup_phase = 0;
2043 int unlink_by_id = 0;
2047 LASSERT(offset == 1 || offset == 3);
2049 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2050 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2053 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2055 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2056 DEBUG_REQ(D_HA, req, "unlink replay");
2057 LASSERT(offset == 1); /* should not come from intent */
2058 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2059 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2060 req->rq_repmsg->buflens[2]);
2063 MD_COUNTER_INCREMENT(obd, unlink);
2065 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2066 GOTO(cleanup, rc = -ENOENT);
2068 if (rec->ur_namelen == 1) {
2069 /* this is request to drop i_nlink on local inode */
2071 rec->ur_name = idname;
2072 rc = mds_create_local_dentry(rec, obd);
2073 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2074 DEBUG_REQ(D_HA, req,
2075 "drop nlink on inode "DLID4" (replay)",
2076 OLID4(rec->ur_id1));
2082 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2083 /* master mds for directory asks slave removing inode is already
2085 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2086 LCK_PW, parent_lockh,
2087 &update_mode, rec->ur_name,
2089 MDS_INODELOCK_UPDATE);
2090 if (IS_ERR(dparent))
2091 GOTO(cleanup, rc = PTR_ERR(dparent));
2092 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2093 rec->ur_namelen - 1);
2095 GOTO(cleanup, rc = PTR_ERR(dchild));
2096 child_lockh.cookie = 0;
2097 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2098 LASSERT(dchild->d_inode != NULL);
2099 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2101 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2102 parent_lockh, &dparent,
2103 LCK_PW, MDS_INODELOCK_UPDATE,
2104 &update_mode, rec->ur_name,
2105 rec->ur_namelen, &child_lockh,
2107 MDS_INODELOCK_LOOKUP |
2108 MDS_INODELOCK_UPDATE);
2113 if (dchild->d_flags & DCACHE_CROSS_REF) {
2114 /* we should have parent lock only here */
2115 LASSERT(unlink_by_id == 0);
2116 LASSERT(dchild->d_mdsnum != mds->mds_num);
2117 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2118 update_mode, dparent, &child_lockh, dchild);
2122 cleanup_phase = 1; /* dchild, dparent, locks */
2125 child_inode = dchild->d_inode;
2126 if (child_inode == NULL) {
2127 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2128 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2129 GOTO(cleanup, rc = -ENOENT);
2132 cleanup_phase = 2; /* dchild has a lock */
2134 /* We have to do these checks ourselves, in case we are making an
2135 * orphan. The client tells us whether rmdir() or unlink() was called,
2136 * so we need to return appropriate errors (bug 72).
2138 * We don't have to check permissions, because vfs_rename (called from
2139 * mds_open_unlink_rename) also calls may_delete. */
2140 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2141 if (!S_ISDIR(child_inode->i_mode))
2142 GOTO(cleanup, rc = -ENOTDIR);
2144 if (S_ISDIR(child_inode->i_mode))
2145 GOTO(cleanup, rc = -EISDIR);
2148 /* handle splitted dir */
2149 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2153 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2154 * reuse (see bug 2029). */
2155 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2158 cleanup_phase = 3; /* child inum lock */
2160 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2162 /* ldlm_reply in buf[0] if called via intent */
2168 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2169 LASSERT(body != NULL);
2171 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2172 DOWN_READ_I_ALLOC_SEM(child_inode);
2173 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2175 /* If this is potentially the last reference to this inode, get the
2176 * OBD EA data first so the client can destroy OST objects. We
2177 * only do the object removal later if no open files/links remain. */
2178 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2179 child_inode->i_nlink == 1) {
2180 if (mds_orphan_open_count(child_inode) > 0) {
2181 /* need to lock pending_dir before transaction */
2182 down(&mds->mds_pending_dir->d_inode->i_sem);
2183 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2184 } else if (S_ISREG(child_inode->i_mode)) {
2185 mds_pack_inode2body(obd, body, child_inode, 0);
2186 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2187 body, child_inode, MDS_PACK_MD_LOCK, 0);
2191 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2192 switch (child_inode->i_mode & S_IFMT) {
2194 /* Drop any lingering child directories before we start our
2195 * transaction, to avoid doing multiple inode dirty/delete
2196 * in our compound transaction (bug 1321). */
2197 shrink_dcache_parent(dchild);
2198 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2201 GOTO(cleanup, rc = PTR_ERR(handle));
2202 rc = vfs_rmdir(dparent->d_inode, dchild);
2205 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2206 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2208 handle = fsfilt_start_log(obd, dparent->d_inode,
2209 FSFILT_OP_UNLINK, NULL,
2210 le32_to_cpu(lmm->lmm_stripe_count));
2212 GOTO(cleanup, rc = PTR_ERR(handle));
2213 rc = vfs_unlink(dparent->d_inode, dchild);
2221 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2224 GOTO(cleanup, rc = PTR_ERR(handle));
2225 rc = vfs_unlink(dparent->d_inode, dchild);
2228 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2231 GOTO(cleanup, rc = -EINVAL);
2234 if (rc == 0 && child_inode->i_nlink == 0) {
2235 if (mds_orphan_open_count(child_inode) > 0)
2236 rc = mds_orphan_add_link(rec, obd, dchild);
2239 GOTO(cleanup, rc = 0);
2241 if (!S_ISREG(child_inode->i_mode))
2244 if (!(body->valid & OBD_MD_FLEASIZE)) {
2245 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2246 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2247 } else if (mds_log_op_unlink(obd, child_inode,
2248 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2249 req->rq_repmsg->buflens[offset + 1],
2250 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2251 req->rq_repmsg->buflens[offset + 2],
2253 body->valid |= OBD_MD_FLCOOKIE;
2256 rc = mds_destroy_object(obd, child_inode, 1);
2258 CERROR("can't remove OST object, err %d\n",
2270 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2271 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2272 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2274 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2276 CERROR("error on parent setattr: rc = %d\n", err);
2278 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2279 handle, req, rc, 0);
2281 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2282 "unlinked", 0, NULL);
2283 switch(cleanup_phase) {
2284 case 5: /* pending_dir semaphore */
2285 up(&mds->mds_pending_dir->d_inode->i_sem);
2286 case 4: /* child inode semaphore */
2287 UP_READ_I_ALLOC_SEM(child_inode);
2288 /* handle splitted dir */
2290 /* master directory can be non-empty or something else ... */
2291 mds_unlink_slave_objs(obd, dchild);
2294 ptlrpc_save_llog_lock(req, lcl);
2295 case 3: /* child ino-reuse lock */
2296 if (rc && body != NULL) {
2297 // Don't unlink the OST objects if the MDS unlink failed
2301 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2303 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2304 case 2: /* child lock */
2305 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2306 if (child_lockh.cookie)
2307 ldlm_lock_decref(&child_lockh, LCK_EX);
2308 case 1: /* child and parent dentry, parent lock */
2310 if (parent_lockh[1].cookie != 0)
2311 ldlm_lock_decref(parent_lockh + 1, update_mode);
2314 ldlm_lock_decref(parent_lockh, LCK_PW);
2316 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2323 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2326 req->rq_status = rc;
2331 * to service requests from remote MDS to increment i_nlink
2333 static int mds_reint_link_acquire(struct mds_update_record *rec,
2334 int offset, struct ptlrpc_request *req,
2335 struct lustre_handle *lh)
2337 struct obd_device *obd = req->rq_export->exp_obd;
2338 struct ldlm_res_id src_res_id = { .name = {0} };
2339 struct lustre_handle *handle = NULL, src_lockh = {0};
2340 struct mds_obd *mds = mds_req2mds(req);
2341 int rc = 0, cleanup_phase = 0;
2342 struct dentry *de_src = NULL;
2343 ldlm_policy_data_t policy;
2344 int flags = LDLM_FL_ATOMIC_CB;
2347 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2348 obd->obd_name, OLID4(rec->ur_id1));
2350 /* Step 1: Lookup the source inode and target directory by ID */
2351 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2353 GOTO(cleanup, rc = PTR_ERR(de_src));
2354 cleanup_phase = 1; /* source dentry */
2356 src_res_id.name[0] = id_fid(rec->ur_id1);
2357 src_res_id.name[1] = id_group(rec->ur_id1);
2358 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2360 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2361 src_res_id, LDLM_IBITS, &policy,
2362 LCK_EX, &flags, mds_blocking_ast,
2363 ldlm_completion_ast, NULL, NULL,
2364 NULL, 0, NULL, &src_lockh);
2366 GOTO(cleanup, rc = -ENOLCK);
2367 cleanup_phase = 2; /* lock */
2369 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2371 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2372 if (IS_ERR(handle)) {
2373 rc = PTR_ERR(handle);
2376 i_nlink_inc(de_src->d_inode);
2377 mark_inode_dirty(de_src->d_inode);
2381 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2382 handle, req, rc, 0);
2383 switch (cleanup_phase) {
2386 ldlm_lock_decref(&src_lockh, LCK_EX);
2388 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2394 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2397 req->rq_status = rc;
2402 * request to link to foreign inode:
2403 * - acquire i_nlinks on this inode
2406 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2407 int offset, struct ptlrpc_request *req,
2408 struct lustre_handle *lh)
2410 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2411 struct obd_device *obd = req->rq_export->exp_obd;
2412 struct dentry *de_tgt_dir = NULL;
2413 struct mds_obd *mds = mds_req2mds(req);
2414 int rc = 0, cleanup_phase = 0;
2415 struct mdc_op_data *op_data;
2416 struct ptlrpc_request *request = NULL;
2420 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2421 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2422 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2423 OLID4(rec->ur_id1));
2425 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2426 tgt_dir_lockh, &update_mode,
2427 rec->ur_name, rec->ur_namelen - 1,
2428 MDS_INODELOCK_UPDATE);
2429 if (IS_ERR(de_tgt_dir))
2430 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2433 OBD_ALLOC(op_data, sizeof(*op_data));
2434 if (op_data == NULL)
2435 GOTO(cleanup, rc = -ENOMEM);
2437 memset(op_data, 0, sizeof(*op_data));
2438 op_data->id1 = *(rec->ur_id1);
2439 rc = md_link(mds->mds_md_exp, op_data, &request);
2440 OBD_FREE(op_data, sizeof(*op_data));
2443 ptlrpc_req_finished(request);
2449 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2451 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2452 if (IS_ERR(handle)) {
2453 rc = PTR_ERR(handle);
2459 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2460 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2461 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2462 id_fid(rec->ur_id1));
2465 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2466 handle, req, rc, 0);
2468 switch (cleanup_phase) {
2471 OBD_ALLOC(op_data, sizeof(*op_data));
2472 if (op_data != NULL) {
2474 memset(op_data, 0, sizeof(*op_data));
2476 op_data->id1 = *(rec->ur_id1);
2477 op_data->create_mode = rec->ur_mode;
2479 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2480 OBD_FREE(op_data, sizeof(*op_data));
2482 ptlrpc_req_finished(request);
2484 CERROR("error %d while dropping i_nlink on "
2485 "remote inode\n", rc);
2488 CERROR("rc %d prevented dropping i_nlink on "
2489 "remote inode\n", -ENOMEM);
2495 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2497 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2500 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2502 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2508 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2511 req->rq_status = rc;
2515 static int mds_reint_link(struct mds_update_record *rec, int offset,
2516 struct ptlrpc_request *req,
2517 struct lustre_handle *lh)
2519 struct obd_device *obd = req->rq_export->exp_obd;
2520 struct dentry *de_src = NULL;
2521 struct dentry *de_tgt_dir = NULL;
2522 struct dentry *dchild = NULL;
2523 struct mds_obd *mds = mds_req2mds(req);
2524 struct lustre_handle *handle = NULL;
2525 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2526 struct ldlm_res_id src_res_id = { .name = {0} };
2527 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2528 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2529 ldlm_policy_data_t tgt_dir_policy =
2530 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2531 int rc = 0, cleanup_phase = 0;
2533 int update_mode = 0;
2537 LASSERT(offset == 1);
2539 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2540 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2541 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2544 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2545 MD_COUNTER_INCREMENT(obd, link);
2547 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2548 GOTO(cleanup, rc = -ENOENT);
2550 if (id_group(rec->ur_id1) != mds->mds_num) {
2551 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2555 if (rec->ur_namelen == 1) {
2556 rc = mds_reint_link_acquire(rec, offset, req, lh);
2560 /* Step 1: Lookup the source inode and target directory by ID */
2561 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2563 GOTO(cleanup, rc = PTR_ERR(de_src));
2565 cleanup_phase = 1; /* source dentry */
2567 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2568 if (IS_ERR(de_tgt_dir)) {
2569 rc = PTR_ERR(de_tgt_dir);
2574 cleanup_phase = 2; /* target directory dentry */
2576 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2577 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2578 rec->ur_name, de_src->d_inode->i_ino);
2580 /* Step 2: Take the two locks */
2581 src_res_id.name[0] = id_fid(rec->ur_id1);
2582 src_res_id.name[1] = id_group(rec->ur_id1);
2583 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2584 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2587 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2588 int flags = LDLM_FL_ATOMIC_CB;
2589 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2591 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2592 tgt_dir_res_id, LDLM_IBITS,
2593 &src_policy, update_mode, &flags,
2595 ldlm_completion_ast, NULL, NULL,
2596 NULL, 0, NULL, tgt_dir_lockh + 1);
2598 GOTO(cleanup, rc = -ENOLCK);
2601 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2602 rec->ur_namelen - 1);
2603 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2604 (unsigned long)id_fid(rec->ur_id2),
2605 (unsigned long)id_group(rec->ur_id2),
2606 tgt_dir_res_id.name[2]);
2609 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2610 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2611 LCK_EX, &tgt_dir_policy);
2615 cleanup_phase = 3; /* locks */
2617 /* Step 3: Lookup the child */
2618 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2619 rec->ur_namelen - 1);
2620 if (IS_ERR(dchild)) {
2621 rc = PTR_ERR(dchild);
2622 if (rc != -EPERM && rc != -EACCES)
2623 CERROR("child lookup error %d\n", rc);
2627 cleanup_phase = 4; /* child dentry */
2629 if (dchild->d_inode) {
2630 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2631 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2636 /* Step 4: Do it. */
2637 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2639 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2640 if (IS_ERR(handle)) {
2641 rc = PTR_ERR(handle);
2645 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2646 if (rc && rc != -EPERM && rc != -EACCES)
2647 CERROR("vfs_link error %d\n", rc);
2649 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2650 handle, req, rc, 0);
2653 switch (cleanup_phase) {
2654 case 4: /* child dentry */
2658 ldlm_lock_decref(&src_lockh, LCK_EX);
2659 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2661 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2662 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2664 case 2: /* target dentry */
2666 if (tgt_dir_lockh[1].cookie && update_mode)
2667 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2671 case 1: /* source dentry */
2676 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2679 req->rq_status = rc;
2683 /* The idea here is that we need to get four locks in the end:
2684 * one on each parent directory, one on each child. We need to take
2685 * these locks in some kind of order (to avoid deadlocks), and the order
2686 * I selected is "increasing resource number" order. We need to look up
2687 * the children, however, before we know what the resource number(s) are.
2688 * Thus the following plan:
2690 * 1,2. Look up the parents
2691 * 3,4. Look up the children
2692 * 5. Take locks on the parents and children, in order
2693 * 6. Verify that the children haven't changed since they were looked up
2695 * If there was a race and the children changed since they were first looked
2696 * up, it is possible that mds_verify_child() will be able to just grab the
2697 * lock on the new child resource (if it has a higher resource than any other)
2698 * but we need to compare against not only its parent, but also against the
2699 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2701 * We need the fancy igrab() on the child inodes because we aren't holding a
2702 * lock on the parent after the lookup is done, so dentry->d_inode may change
2703 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2705 static int mds_get_parents_children_locked(struct obd_device *obd,
2706 struct mds_obd *mds,
2707 struct lustre_id *p1_id,
2708 struct dentry **de_srcdirp,
2709 struct lustre_id *p2_id,
2710 struct dentry **de_tgtdirp,
2712 const char *old_name, int old_len,
2713 struct dentry **de_oldp,
2714 const char *new_name, int new_len,
2715 struct dentry **de_newp,
2716 struct lustre_handle *dlm_handles,
2719 struct ldlm_res_id p1_res_id = { .name = {0} };
2720 struct ldlm_res_id p2_res_id = { .name = {0} };
2721 struct ldlm_res_id c1_res_id = { .name = {0} };
2722 struct ldlm_res_id c2_res_id = { .name = {0} };
2723 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2724 /* Only dentry should disappear, but the inode itself would be
2725 intact otherwise. */
2726 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2727 /* If something is going to be replaced, both dentry and inode locks are
2729 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2730 struct ldlm_res_id *maxres_src, *maxres_tgt;
2731 struct inode *inode;
2732 int rc = 0, cleanup_phase = 0;
2733 __u32 child_gen1 = 0;
2734 __u32 child_gen2 = 0;
2735 unsigned long child_ino1 = 0;
2736 unsigned long child_ino2 = 0;
2739 /* Step 1: Lookup the source directory */
2740 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2741 if (IS_ERR(*de_srcdirp))
2742 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2744 cleanup_phase = 1; /* source directory dentry */
2746 p1_res_id.name[0] = id_fid(p1_id);
2747 p1_res_id.name[1] = id_group(p1_id);
2749 /* Step 2: Lookup the target directory */
2750 if (id_equal_stc(p1_id, p2_id)) {
2751 *de_tgtdirp = dget(*de_srcdirp);
2753 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2754 if (IS_ERR(*de_tgtdirp)) {
2755 rc = PTR_ERR(*de_tgtdirp);
2761 cleanup_phase = 2; /* target directory dentry */
2763 p2_res_id.name[0] = id_fid(p2_id);
2764 p2_res_id.name[1] = id_group(p2_id);
2767 dlm_handles[5].cookie = 0;
2768 dlm_handles[6].cookie = 0;
2770 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2772 * get a temp lock on just fid, group to flush client cache and
2773 * to protect dirs from concurrent splitting.
2775 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2776 LCK_PW, &p_policy, &p2_res_id,
2777 &dlm_handles[6], LCK_PW, &p_policy);
2781 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2783 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2786 CDEBUG(D_INFO, "take locks on "
2787 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2788 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2789 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2794 /* Step 3: Lookup the source child entry */
2795 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2797 if (IS_ERR(*de_oldp)) {
2798 rc = PTR_ERR(*de_oldp);
2799 CERROR("old child lookup error (%.*s): %d\n",
2800 old_len - 1, old_name, rc);
2804 cleanup_phase = 4; /* original name dentry */
2806 inode = (*de_oldp)->d_inode;
2807 if (inode != NULL) {
2808 struct lustre_id sid;
2810 inode = igrab(inode);
2812 GOTO(cleanup, rc = -ENOENT);
2814 down(&inode->i_sem);
2815 rc = mds_read_inode_sid(obd, inode, &sid);
2818 CERROR("Can't read inode self id, inode %lu, "
2819 "rc %d\n", inode->i_ino, rc);
2824 child_ino1 = inode->i_ino;
2825 child_gen1 = inode->i_generation;
2826 c1_res_id.name[0] = id_fid(&sid);
2827 c1_res_id.name[1] = id_group(&sid);
2829 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2830 child_ino1 = (*de_oldp)->d_inum;
2831 child_gen1 = (*de_oldp)->d_generation;
2832 c1_res_id.name[0] = (*de_oldp)->d_fid;
2833 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2835 GOTO(cleanup, rc = -ENOENT);
2838 /* Step 4: Lookup the target child entry */
2839 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2841 if (IS_ERR(*de_newp)) {
2842 rc = PTR_ERR(*de_newp);
2843 CERROR("new child lookup error (%.*s): %d\n",
2844 old_len - 1, old_name, rc);
2848 cleanup_phase = 5; /* target dentry */
2850 inode = (*de_newp)->d_inode;
2851 if (inode != NULL) {
2852 struct lustre_id sid;
2854 inode = igrab(inode);
2858 down(&inode->i_sem);
2859 rc = mds_read_inode_sid(obd, inode, &sid);
2862 CERROR("Can't read inode self id, inode %lu, "
2863 "rc %d\n", inode->i_ino, rc);
2867 child_ino2 = inode->i_ino;
2868 child_gen2 = inode->i_generation;
2869 c2_res_id.name[0] = id_fid(&sid);
2870 c2_res_id.name[1] = id_group(&sid);
2872 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2873 child_ino2 = (*de_newp)->d_inum;
2874 child_gen2 = (*de_newp)->d_generation;
2875 c2_res_id.name[0] = (*de_newp)->d_fid;
2876 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2880 /* Step 5: Take locks on the parents and child(ren) */
2881 maxres_src = &p1_res_id;
2882 maxres_tgt = &p2_res_id;
2883 cleanup_phase = 5; /* target dentry */
2885 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2886 maxres_src = &c1_res_id;
2887 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2888 maxres_tgt = &c2_res_id;
2890 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2892 &p2_res_id, &dlm_handles[1], parent_mode,
2894 &c1_res_id, &dlm_handles[2], child_mode,
2896 &c2_res_id, &dlm_handles[3], child_mode,
2901 cleanup_phase = 6; /* parent and child(ren) locks */
2903 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2904 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2905 parent_mode, &c1_res_id, &dlm_handles[2],
2906 de_oldp, child_mode, &c1_policy, old_name, old_len,
2907 maxres_tgt, child_ino1, child_gen1);
2909 if (c2_res_id.name[0] != 0)
2910 ldlm_lock_decref(&dlm_handles[3], child_mode);
2911 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2918 if (!DENTRY_VALID(*de_oldp))
2919 GOTO(cleanup, rc = -ENOENT);
2921 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2922 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2923 parent_mode, &c2_res_id, &dlm_handles[3],
2924 de_newp, child_mode, &c2_policy, new_name,
2925 new_len, maxres_src, child_ino2, child_gen2);
2927 ldlm_lock_decref(&dlm_handles[2], child_mode);
2928 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2938 switch (cleanup_phase) {
2939 case 6: /* child lock(s) */
2940 if (c2_res_id.name[0] != 0)
2941 ldlm_lock_decref(&dlm_handles[3], child_mode);
2942 if (c1_res_id.name[0] != 0)
2943 ldlm_lock_decref(&dlm_handles[2], child_mode);
2944 if (dlm_handles[1].cookie != 0)
2945 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2946 if (dlm_handles[0].cookie != 0)
2947 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2948 case 5: /* target dentry */
2950 case 4: /* source dentry */
2954 if (dlm_handles[5].cookie != 0)
2955 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2956 if (dlm_handles[6].cookie != 0)
2957 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2959 case 2: /* target directory dentry */
2960 l_dput(*de_tgtdirp);
2961 case 1: /* source directry dentry */
2962 l_dput(*de_srcdirp);
2970 * checks if dentry can be removed. This function also handles cross-ref
2973 static int mds_check_for_rename(struct obd_device *obd,
2974 struct dentry *dentry)
2976 struct mds_obd *mds = &obd->u.mds;
2977 struct lustre_handle *rlockh;
2978 struct ptlrpc_request *req;
2979 struct mdc_op_data *op_data;
2980 struct lookup_intent it;
2981 int handle_size, rc = 0;
2984 LASSERT(dentry != NULL);
2986 if (dentry->d_inode) {
2987 if (S_ISDIR(dentry->d_inode->i_mode) &&
2988 !mds_is_dir_empty(obd, dentry))
2991 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2992 handle_size = sizeof(struct lustre_handle);
2994 OBD_ALLOC(rlockh, handle_size);
2998 memset(rlockh, 0, handle_size);
2999 OBD_ALLOC(op_data, sizeof(*op_data));
3000 if (op_data == NULL) {
3001 OBD_FREE(rlockh, handle_size);
3004 memset(op_data, 0, sizeof(*op_data));
3005 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3007 it.it_op = IT_UNLINK;
3008 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3011 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3012 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3013 mds_blocking_ast, NULL);
3014 OBD_FREE(op_data, sizeof(*op_data));
3018 OBD_FREE(it.d.fs_data,
3019 sizeof(struct lustre_intent_data));
3022 if (rlockh->cookie != 0)
3023 ldlm_lock_decref(rlockh, LCK_EX);
3025 if (LUSTRE_IT(&it)->it_data) {
3026 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3027 ptlrpc_req_finished(req);
3030 if (LUSTRE_IT(&it)->it_status)
3031 rc = LUSTRE_IT(&it)->it_status;
3032 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3033 OBD_FREE(rlockh, handle_size);
3038 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3039 struct ptlrpc_request *req, struct lustre_id *id,
3040 struct dentry *de_dir, struct dentry *de)
3042 struct obd_device *obd = req->rq_export->exp_obd;
3043 struct mds_obd *mds = mds_req2mds(req);
3044 void *handle = NULL;
3050 * name exists and points to local inode try to unlink this name
3051 * and create new one.
3053 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3054 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3055 (unsigned long)de->d_inode->i_generation);
3057 /* checking if we can remove local dentry. */
3058 rc = mds_check_for_rename(obd, de);
3062 handle = fsfilt_start(obd, de_dir->d_inode,
3063 FSFILT_OP_RENAME, NULL);
3065 GOTO(cleanup, rc = PTR_ERR(handle));
3066 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3069 } else if (de->d_flags & DCACHE_CROSS_REF) {
3070 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3071 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3072 (unsigned long)de->d_fid);
3074 /* checking if we can remove local dentry. */
3075 rc = mds_check_for_rename(obd, de);
3080 * to be fully POSIX compatible, we should add one more check:
3082 * if de_new is subdir of dir rec->ur_id1. If so - return
3085 * I do not know how to implement it right now, because
3086 * inodes/dentries for new and old names lie on different MDS,
3087 * so add this notice here just to make it visible for the rest
3088 * of developers and do not forget about. And when this check
3089 * will be added, del_cross_ref should gone, that is local
3090 * dentry is able to be removed if all checks passed. --umka
3093 handle = fsfilt_start(obd, de_dir->d_inode,
3094 FSFILT_OP_RENAME, NULL);
3096 GOTO(cleanup, rc = PTR_ERR(handle));
3097 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3101 /* name doesn't exist. the simplest case. */
3102 handle = fsfilt_start(obd, de_dir->d_inode,
3103 FSFILT_OP_LINK, NULL);
3105 GOTO(cleanup, rc = PTR_ERR(handle));
3108 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3109 rec->ur_tgtlen - 1, id_ino(id),
3110 id_gen(id), id_group(id), id_fid(id));
3112 CERROR("add_dir_entry() returned error %d\n", rc);
3118 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3119 handle, req, rc, 0);
3124 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3125 struct ptlrpc_request *req, struct dentry *de_dir,
3128 struct obd_device *obd = req->rq_export->exp_obd;
3129 struct mds_obd *mds = mds_req2mds(req);
3130 void *handle = NULL;
3134 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3136 GOTO(cleanup, rc = PTR_ERR(handle));
3137 rc = fsfilt_del_dir_entry(obd, de);
3142 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3143 handle, req, rc, 0);
3147 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3148 int offset, struct ptlrpc_request *req)
3150 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3151 struct obd_device *obd = req->rq_export->exp_obd;
3152 struct mds_obd *mds = mds_req2mds(req);
3153 struct lustre_handle child_lockh = {0};
3154 struct dentry *de_tgtdir = NULL;
3155 struct dentry *de_new = NULL;
3156 int cleanup_phase = 0;
3157 int update_mode, rc = 0;
3161 * another MDS executing rename operation has asked us to create target
3162 * name. such a creation should destroy existing target name.
3164 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3165 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3167 /* first, lookup the target */
3168 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3169 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3170 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3171 &child_lockh, &de_new, LCK_EX,
3172 MDS_INODELOCK_LOOKUP);
3179 LASSERT(de_tgtdir->d_inode);
3182 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3188 if (cleanup_phase == 1) {
3190 if (parent_lockh[1].cookie != 0)
3191 ldlm_lock_decref(parent_lockh + 1, update_mode);
3193 ldlm_lock_decref(parent_lockh, LCK_PW);
3194 if (child_lockh.cookie != 0)
3195 ldlm_lock_decref(&child_lockh, LCK_EX);
3200 req->rq_status = rc;
3204 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3205 struct ptlrpc_request *req)
3207 struct obd_device *obd = req->rq_export->exp_obd;
3208 struct ptlrpc_request *req2 = NULL;
3209 struct dentry *de_srcdir = NULL;
3210 struct dentry *de_old = NULL;
3211 struct mds_obd *mds = mds_req2mds(req);
3212 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3213 struct lustre_handle child_lockh = {0};
3214 struct mdc_op_data *op_data;
3215 int update_mode, rc = 0;
3218 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3219 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3221 OBD_ALLOC(op_data, sizeof(*op_data));
3222 if (op_data == NULL)
3224 memset(op_data, 0, sizeof(*op_data));
3226 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3227 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3228 &update_mode, rec->ur_name,
3229 rec->ur_namelen, &child_lockh, &de_old,
3230 LCK_EX, MDS_INODELOCK_LOOKUP);
3232 OBD_FREE(op_data, sizeof(*op_data));
3237 LASSERT(de_srcdir->d_inode);
3241 * we already know the target should be created on another MDS so, we
3242 * have to request that MDS to do it.
3245 /* prepare source id */
3246 if (de_old->d_flags & DCACHE_CROSS_REF) {
3247 LASSERT(de_old->d_inode == NULL);
3248 CDEBUG(D_OTHER, "request to move remote name\n");
3249 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3250 } else if (de_old->d_inode == NULL) {
3251 /* oh, source doesn't exist */
3252 OBD_FREE(op_data, sizeof(*op_data));
3253 GOTO(cleanup, rc = -ENOENT);
3255 struct lustre_id sid;
3256 struct inode *inode = de_old->d_inode;
3258 LASSERT(inode != NULL);
3259 CDEBUG(D_OTHER, "request to move local name\n");
3260 id_ino(&op_data->id1) = inode->i_ino;
3261 id_group(&op_data->id1) = mds->mds_num;
3262 id_gen(&op_data->id1) = inode->i_generation;
3264 down(&inode->i_sem);
3265 rc = mds_read_inode_sid(obd, inode, &sid);
3268 CERROR("Can't read inode self id, "
3269 "inode %lu, rc = %d\n",
3274 id_fid(&op_data->id1) = id_fid(&sid);
3277 op_data->id2 = *rec->ur_id2;
3278 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3279 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3280 OBD_FREE(op_data, sizeof(*op_data));
3285 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3291 ptlrpc_req_finished(req2);
3294 if (parent_lockh[1].cookie != 0)
3295 ldlm_lock_decref(parent_lockh + 1, update_mode);
3297 ldlm_lock_decref(parent_lockh, LCK_PW);
3298 if (child_lockh.cookie != 0)
3299 ldlm_lock_decref(&child_lockh, LCK_EX);
3304 req->rq_status = rc;
3308 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3309 struct ptlrpc_request *req, struct lustre_handle *lockh)
3311 struct obd_device *obd = req->rq_export->exp_obd;
3312 struct dentry *de_srcdir = NULL;
3313 struct dentry *de_tgtdir = NULL;
3314 struct dentry *de_old = NULL;
3315 struct dentry *de_new = NULL;
3316 struct inode *old_inode = NULL, *new_inode = NULL;
3317 struct mds_obd *mds = mds_req2mds(req);
3318 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3319 struct mds_body *body = NULL;
3320 struct llog_create_locks *lcl = NULL;
3321 struct lov_mds_md *lmm = NULL;
3322 int rc = 0, cleanup_phase = 0;
3323 void *handle = NULL;
3326 LASSERT(offset == 1);
3328 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3329 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3332 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3334 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3335 DEBUG_REQ(D_HA, req, "rename replay");
3336 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3337 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3338 req->rq_repmsg->buflens[2]);
3341 MD_COUNTER_INCREMENT(obd, rename);
3343 if (rec->ur_namelen == 1) {
3344 rc = mds_reint_rename_create_name(rec, offset, req);
3348 /* check if new name should be located on remote target. */
3349 if (id_group(rec->ur_id2) != mds->mds_num) {
3350 rc = mds_reint_rename_to_remote(rec, offset, req);
3354 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3355 rec->ur_id2, &de_tgtdir, LCK_PW,
3356 rec->ur_name, rec->ur_namelen,
3357 &de_old, rec->ur_tgt,
3358 rec->ur_tgtlen, &de_new,
3359 dlm_handles, LCK_EX);
3363 cleanup_phase = 1; /* parent(s), children, locks */
3364 old_inode = de_old->d_inode;
3365 new_inode = de_new->d_inode;
3367 /* sanity check for src inode */
3368 if (de_old->d_flags & DCACHE_CROSS_REF) {
3369 LASSERT(de_old->d_inode == NULL);
3372 * in the case of cross-ref dir, we can perform this check only
3373 * if child and parent lie on the same mds. This is because
3374 * otherwise they can have the same inode numbers.
3376 if (de_old->d_mdsnum == mds->mds_num) {
3377 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3378 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3379 GOTO(cleanup, rc = -EINVAL);
3382 LASSERT(de_old->d_inode != NULL);
3383 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3384 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3385 GOTO(cleanup, rc = -EINVAL);
3388 /* sanity check for dest inode */
3389 if (de_new->d_flags & DCACHE_CROSS_REF) {
3390 LASSERT(new_inode == NULL);
3392 /* the same check about target dentry. */
3393 if (de_new->d_mdsnum == mds->mds_num) {
3394 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3395 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3396 GOTO(cleanup, rc = -EINVAL);
3400 * regular files usualy do not have ->rename() implemented. But
3401 * we handle only this case when @de_new is cross-ref entry,
3402 * because in other cases it will be handled by vfs_rename().
3404 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3405 !de_old->d_inode->i_op->rename))
3406 GOTO(cleanup, rc = -EPERM);
3409 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3410 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3411 GOTO(cleanup, rc = -EINVAL);
3416 * check if inodes point to each other. This should be checked before
3417 * is_subdir() check, as for the same entries it will think that they
3420 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3421 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3422 old_inode == new_inode)
3423 GOTO(cleanup, rc = 0);
3425 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3427 * check if we are moving old entry into its child. 2.6 does not check
3428 * for this in vfs_rename() anymore.
3430 if (is_subdir(de_new, de_old))
3431 GOTO(cleanup, rc = -EINVAL);
3435 * if we are about to remove the target at first, pass the EA of that
3436 * inode to client to perform and cleanup on OST.
3438 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3439 LASSERT(body != NULL);
3441 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3443 DOWN_READ_I_ALLOC_SEM(new_inode);
3445 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3447 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3448 new_inode->i_nlink == 2) ||
3449 new_inode->i_nlink == 1)) {
3450 if (mds_orphan_open_count(new_inode) > 0) {
3451 /* need to lock pending_dir before transaction */
3452 down(&mds->mds_pending_dir->d_inode->i_sem);
3453 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3454 } else if (S_ISREG(new_inode->i_mode)) {
3455 mds_pack_inode2body(obd, body, new_inode, 0);
3456 mds_pack_md(obd, req->rq_repmsg, 1, body,
3457 new_inode, MDS_PACK_MD_LOCK, 0);
3461 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3462 de_srcdir->d_inode->i_sb);
3464 if (de_old->d_flags & DCACHE_CROSS_REF) {
3465 struct lustre_id old_id;
3467 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3469 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3474 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3479 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3480 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3481 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3484 GOTO(cleanup, rc = PTR_ERR(handle));
3487 de_old->d_fsdata = req;
3488 de_new->d_fsdata = req;
3489 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3492 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3493 if (mds_orphan_open_count(new_inode) > 0)
3494 rc = mds_orphan_add_link(rec, obd, de_new);
3497 GOTO(cleanup, rc = 0);
3499 if (!S_ISREG(new_inode->i_mode))
3502 if (!(body->valid & OBD_MD_FLEASIZE)) {
3503 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3504 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3505 } else if (mds_log_op_unlink(obd, new_inode,
3506 lustre_msg_buf(req->rq_repmsg,1,0),
3507 req->rq_repmsg->buflens[1],
3508 lustre_msg_buf(req->rq_repmsg,2,0),
3509 req->rq_repmsg->buflens[2],
3511 body->valid |= OBD_MD_FLCOOKIE;
3514 rc = mds_destroy_object(obd, old_inode, 1);
3516 CERROR("can't remove OST object, err %d\n",
3523 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3524 handle, req, rc, 0);
3526 switch (cleanup_phase) {
3528 up(&mds->mds_pending_dir->d_inode->i_sem);
3531 UP_READ_I_ALLOC_SEM(new_inode);
3534 if (dlm_handles[5].cookie != 0)
3535 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3536 if (dlm_handles[6].cookie != 0)
3537 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3540 ptlrpc_save_llog_lock(req, lcl);
3543 if (dlm_handles[3].cookie != 0)
3544 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3545 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3546 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3547 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3549 if (dlm_handles[3].cookie != 0)
3550 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3551 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3552 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3553 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3562 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3565 req->rq_status = rc;
3569 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3570 struct ptlrpc_request *, struct lustre_handle *);
3572 static mds_reinter reinters[REINT_MAX + 1] = {
3573 [REINT_SETATTR] mds_reint_setattr,
3574 [REINT_CREATE] mds_reint_create,
3575 [REINT_LINK] mds_reint_link,
3576 [REINT_UNLINK] mds_reint_unlink,
3577 [REINT_RENAME] mds_reint_rename,
3578 [REINT_OPEN] mds_open
3581 int mds_reint_rec(struct mds_update_record *rec, int offset,
3582 struct ptlrpc_request *req, struct lustre_handle *lockh)
3584 struct obd_device *obd = req->rq_export->exp_obd;
3585 struct lvfs_run_ctxt saved;
3588 /* checked by unpacker */
3589 LASSERT(rec->ur_opcode <= REINT_MAX &&
3590 reinters[rec->ur_opcode] != NULL);
3592 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3593 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3594 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);