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;
699 LASSERT(offset == 1);
701 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
702 OBD_MDS_DEVICENAME));
704 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
705 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
706 rec->ur_name, rec->ur_mode);
708 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
710 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
711 GOTO(cleanup, rc = -ESTALE);
713 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
714 lockh, &parent_mode, rec->ur_name,
715 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
716 if (IS_ERR(dparent)) {
717 rc = PTR_ERR(dparent);
718 CERROR("parent lookup error %d\n", rc);
721 cleanup_phase = 1; /* locked parent dentry */
722 dir = dparent->d_inode;
725 ldlm_lock_dump_handle(D_OTHER, lockh);
727 /* try to retrieve MEA data for this dir */
728 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
734 * dir is already splitted, check is requested filename should
735 * live at this MDS or at another one.
737 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
738 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
739 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
740 " should be %lu(%d)\n",
741 mea->mea_master, dparent->d_inode->i_ino,
742 dparent->d_inode->i_generation, rec->ur_name,
743 (unsigned long)id_group(&mea->mea_ids[i]), i);
744 GOTO(cleanup, rc = -ERESTART);
748 dchild = ll_lookup_one_len(rec->ur_name, dparent,
749 rec->ur_namelen - 1);
750 if (IS_ERR(dchild)) {
751 rc = PTR_ERR(dchild);
752 CERROR("Can't find "DLID4"/%s, error %d\n",
753 OLID4(rec->ur_id1), rec->ur_name, rc);
757 cleanup_phase = 2; /* child dentry */
759 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
761 if (type == S_IFREG || type == S_IFDIR) {
762 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
763 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
764 obd->obd_name, dparent->d_inode->i_ino,
765 dparent->d_inode->i_generation, rc, parent_mode);
767 /* dir got splitted */
768 GOTO(cleanup, rc = -ERESTART);
770 /* error happened during spitting. */
775 if (dir->i_mode & S_ISGID) {
776 if (S_ISDIR(rec->ur_mode))
777 rec->ur_mode |= S_ISGID;
781 * here inode number should be used only in the case of replaying. It is
782 * needed to check if object already created in the case of creating
785 fid = mds_alloc_fid(obd);
786 dchild->d_fsdata = (void *)&dp;
787 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
790 dp.p_group = mds->mds_num;
794 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
796 GOTO(cleanup, rc = PTR_ERR(handle));
797 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
799 if (rc == 0 && rec->ur_eadata) {
800 /*for CMOBD to set lov md info when cmobd reint create*/
801 CDEBUG(D_INFO, "set lsm %p, len %d to inode %lu \n",
802 rec->ur_eadata, rec->ur_eadatalen,
803 dchild->d_inode->i_ino);
804 fsfilt_set_md(obd, dchild->d_inode, handle, rec->ur_eadata,
805 rec->ur_eadatalen, EA_LOV);
814 * as Peter asked, mkdir() should distribute new directories
815 * over the whole cluster in order to distribute namespace
816 * processing load. first, we calculate which MDS to use to put
817 * new directory's inode in.
819 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
820 rec->ur_flags, &req->rq_peer, dir);
821 if (i == mds->mds_num) {
822 /* inode will be created locally */
823 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
825 GOTO(cleanup, rc = PTR_ERR(handle));
827 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
830 "Can't create dir \"%s\", rc = %d\n",
831 dchild->d_name.name, rc);
835 down(&dchild->d_inode->i_sem);
837 rc = mds_update_inode_sid(obd, dchild->d_inode,
838 handle, rec->ur_id2);
840 CERROR("mds_update_inode_sid() failed, inode %lu, "
841 "rc %d\n", dchild->d_inode->i_ino, rc);
845 * make sure, that fid is up-to-date.
847 mds_set_last_fid(obd, id_fid(rec->ur_id2));
849 rc = mds_set_inode_sid(obd, dchild->d_inode,
852 CERROR("mds_set_inode_sid() failed, inode %lu, "
853 "rc %d\n", dchild->d_inode->i_ino, rc);
856 up(&dchild->d_inode->i_sem);
862 nstripes = *(u16 *)rec->ur_eadata;
864 if (rc == 0 && nstripes) {
866 * we pass LCK_EX to split routine to signal,
867 * that we have exclusive access to the
868 * directory. Simple because nobody knows it
869 * already exists -bzzz
871 rc = mds_try_to_split_dir(obd, dchild,
875 /* dir got splitted */
878 /* an error occured during
883 } else if (!DENTRY_VALID(dchild)) {
884 /* inode will be created on another MDS */
885 struct obdo *oa = NULL;
886 struct mds_body *body;
890 /* first, create that inode */
893 GOTO(cleanup, rc = -ENOMEM);
898 if (rec->ur_eadata) {
899 /* user asks for creating splitted dir */
900 oa->o_easize = *((u16 *) rec->ur_eadata);
903 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
904 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
906 /* adjust the uid/gid/mode bits */
907 oa->o_mode = rec->ur_mode;
908 oa->o_uid = current->fsuid;
909 oa->o_gid = (dir->i_mode & S_ISGID) ?
910 dir->i_gid : current->fsgid;
911 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
913 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
916 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
918 * here inode number and generation are
919 * important, as this is replay request and we
920 * need them to check if such an object is
923 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
924 obd->obd_name, rec->ur_namelen - 1,
925 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
926 (unsigned)id_gen(rec->ur_id2));
927 oa->o_id = id_ino(rec->ur_id2);
928 oa->o_fid = id_fid(rec->ur_id2);
929 oa->o_generation = id_gen(rec->ur_id2);
930 oa->o_flags |= OBD_FL_RECREATE_OBJS;
931 LASSERT(oa->o_fid != 0);
934 /* obtain default ACL */
935 acl_size = mds_get_default_acl(dir, &acl);
938 GOTO(cleanup, rc = -ENOMEM);
942 * before obd_create() is called, o_fid is not known if
943 * this is not recovery of cause.
945 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
949 OBD_FREE(acl, acl_size);
952 CERROR("can't create remote inode: %d\n", rc);
953 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
954 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
955 rec->ur_name, rec->ur_mode);
960 LASSERT(oa->o_fid != 0);
962 /* now, add new dir entry for it */
963 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
964 if (IS_ERR(handle)) {
966 GOTO(cleanup, rc = PTR_ERR(handle));
969 /* creating local dentry for remote inode. */
970 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
971 rec->ur_namelen - 1, oa->o_id,
972 oa->o_generation, i, oa->o_fid);
975 CERROR("Can't create local entry %*s for "
976 "remote inode.\n", rec->ur_namelen - 1,
982 body = lustre_msg_buf(req->rq_repmsg,
984 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
986 obdo2id(&body->id1, oa);
989 /* requested name exists in the directory */
996 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
998 GOTO(cleanup, rc = PTR_ERR(handle));
999 if (rec->ur_tgt == NULL) /* no target supplied */
1000 rc = -EINVAL; /* -EPROTO? */
1002 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1010 int rdev = rec->ur_rdev;
1011 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1013 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1014 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1019 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1020 dchild->d_fsdata = NULL;
1021 GOTO(cleanup, rc = -EINVAL);
1024 /* In case we stored the desired inum in here, we want to clean up. */
1025 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1026 dchild->d_fsdata = NULL;
1029 CDEBUG(D_INODE, "error during create: %d\n", rc);
1031 } else if (dchild->d_inode) {
1032 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1034 struct mds_body *body;
1035 struct inode *inode = dchild->d_inode;
1038 iattr.ia_uid = rec->ur_fsuid;
1039 LTIME_S(iattr.ia_atime) = rec->ur_time;
1040 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1041 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1043 if (dir->i_mode & S_ISGID)
1044 iattr.ia_gid = dir->i_gid;
1046 iattr.ia_gid = rec->ur_fsgid;
1048 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1049 ATTR_MTIME | ATTR_CTIME;
1051 if (id_ino(rec->ur_id2)) {
1052 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1053 inode->i_generation = id_gen(rec->ur_id2);
1055 if (type != S_IFDIR) {
1056 down(&inode->i_sem);
1057 rc = mds_update_inode_sid(obd, inode,
1058 handle, rec->ur_id2);
1061 CERROR("Can't update inode self id, "
1066 * make sure, that fid is up-to-date.
1068 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1071 /* dirtied and committed by the upcoming setattr. */
1072 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1073 inode->i_ino, inode->i_generation);
1075 struct lustre_handle child_ino_lockh;
1077 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1078 inode->i_ino, inode->i_generation);
1080 if (type != S_IFDIR) {
1082 * allocate new id for @inode if it is not dir,
1083 * because for dir it was already done.
1085 down(&inode->i_sem);
1086 rc = mds_set_inode_sid(obd, inode,
1090 CERROR("mds_set_inode_sid() failed, "
1091 "inode %lu, rc %d\n", inode->i_ino,
1098 * the inode we were allocated may have just
1099 * been freed by an unlink operation. We take
1100 * this lock to synchronize against the matching
1101 * reply-ack-lock taken in unlink, to avoid
1102 * replay problems if this reply makes it out to
1103 * the client but the unlink's does not. See
1104 * bug 2029 for more detail.
1106 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1107 if (rc != ELDLM_OK) {
1108 CERROR("error locking for unlink/create sync: "
1111 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1116 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1118 CERROR("error on child setattr: rc = %d\n", rc);
1120 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1121 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1123 CERROR("error on parent setattr: rc = %d\n", rc);
1125 MD_COUNTER_INCREMENT(obd, create);
1127 /* take care of default stripe inheritance */
1128 if (type == S_IFDIR) {
1129 struct lov_mds_md lmm;
1130 int lmm_size = sizeof(lmm);
1132 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1134 down(&inode->i_sem);
1135 rc = fsfilt_set_md(obd, inode, handle,
1136 &lmm, lmm_size, EA_LOV);
1140 CERROR("error on copy stripe info: rc = %d\n",
1146 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1147 mds_pack_inode2body(obd, body, inode, 1);
1148 mds_body_do_reverse_map(med, body);
1153 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1155 if (rc && created) {
1156 /* Destroy the file we just created. This should not need extra
1157 * journal credits, as we have already modified all of the
1158 * blocks needed in order to create the file in the first
1162 err = vfs_rmdir(dir, dchild);
1164 CERROR("rmdir in error path: %d\n", err);
1167 err = vfs_unlink(dir, dchild);
1169 CERROR("unlink in error path: %d\n", err);
1175 switch (cleanup_phase) {
1176 case 2: /* child dentry */
1178 case 1: /* locked parent dentry */
1180 if (lockh[1].cookie != 0)
1181 ldlm_lock_decref(lockh + 1, parent_mode);
1184 ldlm_lock_decref(lockh, LCK_PW);
1186 ptlrpc_save_lock(req, lockh, LCK_PW);
1192 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1196 OBD_FREE(mea, mea_size);
1197 req->rq_status = rc;
1202 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1203 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1207 for (i = 0; i < RES_NAME_SIZE; i++) {
1209 * this is needed to make zeroed res_id entries to be put at the
1210 * end of list in *ordered_locks() .
1212 if (res1->name[i] == 0 && res2->name[i] != 0)
1214 if (res2->name[i] == 0 && res1->name[i] != 0)
1216 if (res1->name[i] > res2->name[i])
1218 if (res1->name[i] < res2->name[i])
1225 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1231 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1232 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1233 * because they take the place of local semaphores.
1235 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1236 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1237 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1238 struct lustre_handle *p1_lockh, int p1_lock_mode,
1239 ldlm_policy_data_t *p1_policy,
1240 struct ldlm_res_id *p2_res_id,
1241 struct lustre_handle *p2_lockh, int p2_lock_mode,
1242 ldlm_policy_data_t *p2_policy)
1244 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1245 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1246 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1247 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1251 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1253 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1254 res_id[0]->name[0], res_id[1]->name[0]);
1256 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1257 handles[1] = p1_lockh;
1258 handles[0] = p2_lockh;
1259 res_id[1] = p1_res_id;
1260 res_id[0] = p2_res_id;
1261 lock_modes[1] = p1_lock_mode;
1262 lock_modes[0] = p2_lock_mode;
1263 policies[1] = p1_policy;
1264 policies[0] = p2_policy;
1267 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1268 res_id[0]->name[0], res_id[1]->name[0]);
1270 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1271 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1272 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1273 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1274 NULL, 0, NULL, handles[0]);
1277 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1279 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1280 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1281 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1282 ldlm_lock_addref(handles[1], lock_modes[1]);
1283 } else if (res_id[1]->name[0] != 0) {
1284 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1285 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1286 *res_id[1], LDLM_IBITS, policies[1],
1287 lock_modes[1], &flags, mds_blocking_ast,
1288 ldlm_completion_ast, NULL, NULL, NULL, 0,
1290 if (rc != ELDLM_OK) {
1291 ldlm_lock_decref(handles[0], lock_modes[0]);
1294 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1300 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1301 struct lustre_handle *p1_lockh, int p1_lock_mode,
1302 ldlm_policy_data_t *p1_policy,
1303 struct ldlm_res_id *p2_res_id,
1304 struct lustre_handle *p2_lockh, int p2_lock_mode,
1305 ldlm_policy_data_t *p2_policy,
1306 struct ldlm_res_id *c1_res_id,
1307 struct lustre_handle *c1_lockh, int c1_lock_mode,
1308 ldlm_policy_data_t *c1_policy,
1309 struct ldlm_res_id *c2_res_id,
1310 struct lustre_handle *c2_lockh, int c2_lock_mode,
1311 ldlm_policy_data_t *c2_policy)
1313 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1314 c1_res_id, c2_res_id };
1315 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1316 c1_lockh, c2_lockh };
1317 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1318 c1_lock_mode, c2_lock_mode };
1319 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1320 c1_policy, c2_policy};
1321 int rc, i, j, sorted, flags;
1324 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1325 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1326 res_id[3]->name[0]);
1329 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1330 * res_id should be at the end of list after sorting is finished.
1332 for (i = 1; i < 4; i++) {
1334 dlm_handles[4] = dlm_handles[i];
1335 res_id[4] = res_id[i];
1336 lock_modes[4] = lock_modes[i];
1337 policies[4] = policies[i];
1341 if (res_gt(res_id[j], res_id[4], policies[j],
1343 dlm_handles[j + 1] = dlm_handles[j];
1344 res_id[j + 1] = res_id[j];
1345 lock_modes[j + 1] = lock_modes[j];
1346 policies[j + 1] = policies[j];
1351 } while (j >= 0 && !sorted);
1353 dlm_handles[j + 1] = dlm_handles[4];
1354 res_id[j + 1] = res_id[4];
1355 lock_modes[j + 1] = lock_modes[4];
1356 policies[j + 1] = policies[4];
1359 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1360 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1361 res_id[3]->name[0]);
1363 /* XXX we could send ASTs on all these locks first before blocking? */
1364 for (i = 0; i < 4; i++) {
1368 * nevertheless zeroed res_ids should be at the end of list, and
1369 * could use break here, I think, that it is more correctly for
1370 * clear understanding of code to have continue here, as it
1371 * clearly means, that zeroed res_id should be skipped and does
1372 * not mean, that if we meet zeroed res_id we should stop
1375 if (res_id[i]->name[0] == 0)
1379 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1380 (policies[i]->l_inodebits.bits &
1381 policies[i-1]->l_inodebits.bits) ) {
1382 memcpy(dlm_handles[i], dlm_handles[i-1],
1383 sizeof(*(dlm_handles[i])));
1384 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1386 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1387 *res_id[i], LDLM_IBITS,
1389 lock_modes[i], &flags,
1391 ldlm_completion_ast, NULL, NULL,
1392 NULL, 0, NULL, dlm_handles[i]);
1394 GOTO(out_err, rc = -EIO);
1395 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1402 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1407 /* In the unlikely case that the child changed while we were waiting
1408 * on the lock, we need to drop the lock on the old child and either:
1409 * - if the child has a lower resource name, then we have to also
1410 * drop the parent lock and regain the locks in the right order
1411 * - in the rename case, if the child has a lower resource name than one of
1412 * the other parent/child resources (maxres) we also need to reget the locks
1413 * - if the child has a higher resource name (this is the common case)
1414 * we can just get the lock on the new child (still in lock order)
1416 * Returns 0 if the child did not change or if it changed but could be locked.
1417 * Returns 1 if the child changed and we need to re-lock (no locks held).
1418 * Returns -ve error with a valid dchild (no locks held). */
1419 static int mds_verify_child(struct obd_device *obd,
1420 struct ldlm_res_id *parent_res_id,
1421 struct lustre_handle *parent_lockh,
1422 struct dentry *dparent, int parent_mode,
1423 struct ldlm_res_id *child_res_id,
1424 struct lustre_handle *child_lockh,
1425 struct dentry **dchildp, int child_mode,
1426 ldlm_policy_data_t *child_policy,
1427 const char *name, int namelen,
1428 struct ldlm_res_id *maxres,
1429 unsigned long child_ino, __u32 child_gen)
1431 struct lustre_id sid;
1432 struct dentry *vchild, *dchild = *dchildp;
1433 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1436 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1438 GOTO(cleanup, rc = PTR_ERR(vchild));
1440 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1441 if (child_gen == vchild->d_generation &&
1442 child_ino == vchild->d_inum) {
1451 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1452 (vchild->d_inode != NULL &&
1453 child_gen == vchild->d_inode->i_generation &&
1454 child_ino == vchild->d_inode->i_ino))) {
1462 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1463 vchild->d_inode, dchild ? dchild->d_inode : 0,
1464 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1465 child_res_id->name[0]);
1467 if (child_res_id->name[0] != 0)
1468 ldlm_lock_decref(child_lockh, child_mode);
1472 cleanup_phase = 1; /* parent lock only */
1473 *dchildp = dchild = vchild;
1475 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1476 int flags = LDLM_FL_ATOMIC_CB;
1478 if (dchild->d_inode) {
1479 down(&dchild->d_inode->i_sem);
1480 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1481 up(&dchild->d_inode->i_sem);
1483 CERROR("Can't read inode self id, inode %lu,"
1484 " rc %d\n", dchild->d_inode->i_ino, rc);
1487 child_res_id->name[0] = id_fid(&sid);
1488 child_res_id->name[1] = id_group(&sid);
1490 child_res_id->name[0] = dchild->d_fid;
1491 child_res_id->name[1] = dchild->d_mdsnum;
1494 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1495 res_gt(maxres, child_res_id, NULL, NULL)) {
1496 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1497 child_res_id->name[0], parent_res_id->name[0],
1499 GOTO(cleanup, rc = 1);
1502 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1503 *child_res_id, LDLM_IBITS, child_policy,
1504 child_mode, &flags, mds_blocking_ast,
1505 ldlm_completion_ast, NULL, NULL, NULL, 0,
1508 GOTO(cleanup, rc = -EIO);
1511 memset(child_res_id, 0, sizeof(*child_res_id));
1517 switch(cleanup_phase) {
1519 if (child_res_id->name[0] != 0)
1520 ldlm_lock_decref(child_lockh, child_mode);
1522 ldlm_lock_decref(parent_lockh, parent_mode);
1528 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1529 struct lustre_id *id,
1530 struct lustre_handle *parent_lockh,
1531 struct dentry **dparentp, int parent_mode,
1532 __u64 parent_lockpart, int *update_mode,
1533 char *name, int namelen,
1534 struct lustre_handle *child_lockh,
1535 struct dentry **dchildp, int child_mode,
1536 __u64 child_lockpart)
1538 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1539 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1540 struct ldlm_res_id parent_res_id = { .name = {0} };
1541 struct ldlm_res_id child_res_id = { .name = {0} };
1542 unsigned long child_ino = 0; __u32 child_gen = 0;
1543 int rc = 0, cleanup_phase = 0;
1544 struct lustre_id sid;
1545 struct inode *inode;
1548 /* Step 1: Lookup parent */
1549 *dparentp = mds_id2dentry(obd, id, NULL);
1550 if (IS_ERR(*dparentp)) {
1551 rc = PTR_ERR(*dparentp);
1556 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1557 (*dparentp)->d_inode->i_ino, name);
1559 parent_res_id.name[0] = id_fid(id);
1560 parent_res_id.name[1] = id_group(id);
1563 parent_lockh[1].cookie = 0;
1564 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1565 struct ldlm_res_id res_id = { .name = {0} };
1566 ldlm_policy_data_t policy;
1567 int flags = LDLM_FL_ATOMIC_CB;
1569 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1571 res_id.name[0] = id_fid(id);
1572 res_id.name[1] = id_group(id);
1573 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1575 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1576 res_id, LDLM_IBITS, &policy,
1577 *update_mode, &flags,
1579 ldlm_completion_ast,
1580 NULL, NULL, NULL, 0, NULL,
1586 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1589 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1590 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1591 parent_res_id.name[2]);
1595 cleanup_phase = 1; /* parent dentry */
1597 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1598 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1599 if (IS_ERR(*dchildp)) {
1600 rc = PTR_ERR(*dchildp);
1601 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1605 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1607 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1608 * lock. Drop possible UPDATE lock!
1610 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1611 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1613 child_res_id.name[0] = (*dchildp)->d_fid;
1614 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1615 child_ino = (*dchildp)->d_inum;
1616 child_gen = (*dchildp)->d_generation;
1620 inode = (*dchildp)->d_inode;
1622 inode = igrab(inode);
1626 down(&inode->i_sem);
1627 rc = mds_read_inode_sid(obd, inode, &sid);
1630 CERROR("Can't read inode self id, inode %lu, "
1631 "rc %d\n", inode->i_ino, rc);
1636 child_ino = inode->i_ino;
1637 child_gen = inode->i_generation;
1638 child_res_id.name[0] = id_fid(&sid);
1639 child_res_id.name[1] = id_group(&sid);
1643 cleanup_phase = 2; /* child dentry */
1645 /* Step 3: Lock parent and child in resource order. If child doesn't
1646 * exist, we still have to lock the parent and re-lookup. */
1647 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1648 &parent_policy, &child_res_id, child_lockh,
1649 child_mode, &child_policy);
1653 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1654 cleanup_phase = 4; /* child lock */
1656 cleanup_phase = 3; /* parent lock */
1658 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1659 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1660 parent_mode, &child_res_id, child_lockh,
1661 dchildp, child_mode, &child_policy,
1662 name, namelen, &parent_res_id, child_ino,
1674 switch (cleanup_phase) {
1676 ldlm_lock_decref(child_lockh, child_mode);
1678 ldlm_lock_decref(parent_lockh, parent_mode);
1683 if (parent_lockh[1].cookie)
1684 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1692 void mds_reconstruct_generic(struct ptlrpc_request *req)
1694 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1695 mds_req_from_mcd(req, med->med_mcd);
1698 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1699 * instead link the inode into the PENDING directory until it is finally
1700 * released. We can't simply call mds_reint_rename() or some part thereof,
1701 * because we don't have the inode to check for link count/open status until
1702 * after it is locked.
1704 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1705 * before starting journal transaction.
1707 * returns 1 on success
1708 * returns 0 if we lost a race and didn't make a new link
1709 * returns negative on error
1711 static int mds_orphan_add_link(struct mds_update_record *rec,
1712 struct obd_device *obd, struct dentry *dentry)
1714 struct mds_obd *mds = &obd->u.mds;
1715 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1716 struct inode *inode = dentry->d_inode;
1717 struct dentry *pending_child;
1718 char idname[LL_ID_NAMELEN];
1719 int idlen = 0, rc, mode;
1722 LASSERT(inode != NULL);
1723 LASSERT(!mds_inode_is_orphan(inode));
1724 #ifndef HAVE_I_ALLOC_SEM
1725 LASSERT(down_trylock(&inode->i_sem) != 0);
1727 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1729 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1731 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1732 mds_orphan_open_count(inode), inode->i_nlink,
1733 S_ISDIR(inode->i_mode) ? "dir" :
1734 S_ISREG(inode->i_mode) ? "file" : "other",
1735 rec->ur_name, idname);
1737 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1740 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1741 if (IS_ERR(pending_child))
1742 RETURN(PTR_ERR(pending_child));
1744 if (pending_child->d_inode != NULL) {
1745 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1746 LASSERT(pending_child->d_inode == inode);
1747 GOTO(out_dput, rc = 0);
1751 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1752 * linking and return real mode back then -bzzz
1754 mode = inode->i_mode;
1755 inode->i_mode = S_IFREG;
1756 rc = vfs_link(dentry, pending_dir, pending_child);
1758 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1761 mds_inode_set_orphan(inode);
1763 /* return mode and correct i_nlink if inode is directory */
1764 inode->i_mode = mode;
1765 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1766 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1768 if (S_ISDIR(mode)) {
1770 i_nlink_inc(pending_dir);
1771 mark_inode_dirty(inode);
1772 mark_inode_dirty(pending_dir);
1775 GOTO(out_dput, rc = 1);
1777 l_dput(pending_child);
1781 int mds_create_local_dentry(struct mds_update_record *rec,
1782 struct obd_device *obd)
1784 struct mds_obd *mds = &obd->u.mds;
1785 struct inode *id_dir = mds->mds_id_dir->d_inode;
1786 int idlen = 0, rc, cleanup_phase = 0;
1787 struct dentry *new_child = NULL;
1788 char *idname = rec->ur_name;
1789 struct dentry *child = NULL;
1790 struct lustre_handle lockh[2] = {{0}, {0}};
1791 struct lustre_id sid;
1795 down(&id_dir->i_sem);
1796 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1797 id_gen(rec->ur_id1));
1799 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1800 idname, OLID4(rec->ur_id1));
1802 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1805 if (IS_ERR(new_child)) {
1806 CERROR("can't lookup %s: %d\n", idname,
1807 (int) PTR_ERR(new_child));
1808 GOTO(cleanup, rc = PTR_ERR(new_child));
1812 down(&id_dir->i_sem);
1813 rc = mds_read_inode_sid(obd, id_dir, &sid);
1816 CERROR("Can't read inode self id, inode %lu, "
1817 "rc %d\n", id_dir->i_ino, rc);
1821 if (new_child->d_inode != NULL) {
1822 /* nice. we've already have local dentry! */
1823 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1824 (unsigned)new_child->d_inode->i_ino,
1825 (unsigned)new_child->d_inode->i_generation);
1827 id_ino(rec->ur_id1) = id_dir->i_ino;
1828 id_gen(rec->ur_id1) = id_dir->i_generation;
1829 rec->ur_namelen = idlen + 1;
1831 id_fid(rec->ur_id1) = id_fid(&sid);
1832 id_group(rec->ur_id1) = id_group(&sid);
1834 GOTO(cleanup, rc = 0);
1837 /* new, local dentry will be added soon. we need no aliases here */
1840 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1841 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1843 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1844 LCK_EX, lockh, NULL, NULL, 0,
1845 MDS_INODELOCK_UPDATE);
1848 if (IS_ERR(child)) {
1849 rc = PTR_ERR(child);
1850 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1851 CERROR("can't get victim: %d\n", rc);
1856 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1858 GOTO(cleanup, rc = PTR_ERR(handle));
1860 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1861 idlen, id_ino(rec->ur_id1),
1862 id_gen(rec->ur_id1), mds->mds_num,
1863 id_fid(rec->ur_id1));
1865 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1866 (unsigned long)child->d_inode->i_ino,
1867 (unsigned long)child->d_inode->i_generation, rc);
1869 if (S_ISDIR(child->d_inode->i_mode)) {
1870 i_nlink_inc(id_dir);
1871 mark_inode_dirty(id_dir);
1873 mark_inode_dirty(child->d_inode);
1875 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1877 id_ino(rec->ur_id1) = id_dir->i_ino;
1878 id_gen(rec->ur_id1) = id_dir->i_generation;
1879 rec->ur_namelen = idlen + 1;
1881 id_fid(rec->ur_id1) = id_fid(&sid);
1882 id_group(rec->ur_id1) = id_group(&sid);
1886 switch(cleanup_phase) {
1888 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1889 ldlm_lock_decref(lockh, LCK_EX);
1899 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1900 struct ptlrpc_request *slave)
1902 void *cookie, *cookie2;
1903 struct mds_body *body2;
1904 struct mds_body *body;
1908 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1909 LASSERT(body != NULL);
1911 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1912 LASSERT(body2 != NULL);
1914 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1917 memcpy(body2, body, sizeof(*body));
1918 body2->valid &= ~OBD_MD_FLCOOKIE;
1920 if (!(body->valid & OBD_MD_FLEASIZE) &&
1921 !(body->valid & OBD_MD_FLDIREA))
1924 if (body->eadatasize == 0) {
1925 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1929 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1931 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1932 LASSERT(ea != NULL);
1934 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1935 LASSERT(ea2 != NULL);
1937 memcpy(ea2, ea, body->eadatasize);
1939 if (body->valid & OBD_MD_FLCOOKIE) {
1940 LASSERT(master->rq_repmsg->buflens[2] >=
1941 slave->rq_repmsg->buflens[2]);
1942 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1943 slave->rq_repmsg->buflens[2]);
1944 LASSERT(cookie != NULL);
1946 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1947 master->rq_repmsg->buflens[2]);
1948 LASSERT(cookie2 != NULL);
1949 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1950 body2->valid |= OBD_MD_FLCOOKIE;
1955 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1956 int offset, struct ptlrpc_request *req,
1957 struct lustre_handle *parent_lockh,
1958 int update_mode, struct dentry *dparent,
1959 struct lustre_handle *child_lockh,
1960 struct dentry *dchild)
1962 struct obd_device *obd = req->rq_export->exp_obd;
1963 struct mds_obd *mds = mds_req2mds(req);
1964 struct ptlrpc_request *request = NULL;
1965 int rc = 0, cleanup_phase = 0;
1966 struct mdc_op_data *op_data;
1970 LASSERT(offset == 1 || offset == 3);
1972 /* time to drop i_nlink on remote MDS */
1973 OBD_ALLOC(op_data, sizeof(*op_data));
1974 if (op_data == NULL)
1977 memset(op_data, 0, sizeof(*op_data));
1978 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1979 op_data->create_mode = rec->ur_mode;
1981 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1982 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1984 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1985 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1986 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1989 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1990 op_data->create_mode |= MDS_MODE_REPLAY;
1992 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1993 OBD_FREE(op_data, sizeof(*op_data));
1998 mds_copy_unlink_reply(req, request);
1999 ptlrpc_req_finished(request);
2003 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2006 GOTO(cleanup, rc = PTR_ERR(handle));
2007 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2008 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2013 req->rq_status = rc;
2016 if (parent_lockh[1].cookie != 0)
2017 ldlm_lock_decref(parent_lockh + 1, update_mode);
2019 ldlm_lock_decref(child_lockh, LCK_EX);
2021 ldlm_lock_decref(parent_lockh, LCK_PW);
2023 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2030 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2031 struct ptlrpc_request *req,
2032 struct lustre_handle *lh)
2034 struct dentry *dparent = NULL, *dchild;
2035 struct mds_obd *mds = mds_req2mds(req);
2036 struct obd_device *obd = req->rq_export->exp_obd;
2037 struct mds_body *body = NULL;
2038 struct inode *child_inode = NULL;
2039 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2040 struct lustre_handle child_lockh = {0};
2041 struct lustre_handle child_reuse_lockh = {0};
2042 struct lustre_handle *slave_lockh = NULL;
2043 char idname[LL_ID_NAMELEN];
2044 struct llog_create_locks *lcl = NULL;
2045 void *handle = NULL;
2046 int rc = 0, cleanup_phase = 0;
2047 int unlink_by_id = 0;
2051 LASSERT(offset == 1 || offset == 3);
2053 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2054 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2057 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2059 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2060 DEBUG_REQ(D_HA, req, "unlink replay");
2061 LASSERT(offset == 1); /* should not come from intent */
2062 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2063 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2064 req->rq_repmsg->buflens[2]);
2067 MD_COUNTER_INCREMENT(obd, unlink);
2069 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2070 GOTO(cleanup, rc = -ENOENT);
2072 if (rec->ur_namelen == 1) {
2073 /* this is request to drop i_nlink on local inode */
2075 rec->ur_name = idname;
2076 rc = mds_create_local_dentry(rec, obd);
2077 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2078 DEBUG_REQ(D_HA, req,
2079 "drop nlink on inode "DLID4" (replay)",
2080 OLID4(rec->ur_id1));
2086 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2087 /* master mds for directory asks slave removing inode is already
2089 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2090 LCK_PW, parent_lockh,
2091 &update_mode, rec->ur_name,
2093 MDS_INODELOCK_UPDATE);
2094 if (IS_ERR(dparent))
2095 GOTO(cleanup, rc = PTR_ERR(dparent));
2096 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2097 rec->ur_namelen - 1);
2099 GOTO(cleanup, rc = PTR_ERR(dchild));
2100 child_lockh.cookie = 0;
2101 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2102 LASSERT(dchild->d_inode != NULL);
2103 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2105 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2106 parent_lockh, &dparent,
2107 LCK_PW, MDS_INODELOCK_UPDATE,
2108 &update_mode, rec->ur_name,
2109 rec->ur_namelen, &child_lockh,
2111 MDS_INODELOCK_LOOKUP |
2112 MDS_INODELOCK_UPDATE);
2117 if (dchild->d_flags & DCACHE_CROSS_REF) {
2118 /* we should have parent lock only here */
2119 LASSERT(unlink_by_id == 0);
2120 LASSERT(dchild->d_mdsnum != mds->mds_num);
2121 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2122 update_mode, dparent, &child_lockh, dchild);
2126 cleanup_phase = 1; /* dchild, dparent, locks */
2129 child_inode = dchild->d_inode;
2130 if (child_inode == NULL) {
2131 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2132 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2133 GOTO(cleanup, rc = -ENOENT);
2136 cleanup_phase = 2; /* dchild has a lock */
2138 /* We have to do these checks ourselves, in case we are making an
2139 * orphan. The client tells us whether rmdir() or unlink() was called,
2140 * so we need to return appropriate errors (bug 72).
2142 * We don't have to check permissions, because vfs_rename (called from
2143 * mds_open_unlink_rename) also calls may_delete. */
2144 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2145 if (!S_ISDIR(child_inode->i_mode))
2146 GOTO(cleanup, rc = -ENOTDIR);
2148 if (S_ISDIR(child_inode->i_mode))
2149 GOTO(cleanup, rc = -EISDIR);
2152 /* handle splitted dir */
2153 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2157 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2158 * reuse (see bug 2029). */
2159 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2162 cleanup_phase = 3; /* child inum lock */
2164 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2166 /* ldlm_reply in buf[0] if called via intent */
2172 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2173 LASSERT(body != NULL);
2175 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2176 DOWN_READ_I_ALLOC_SEM(child_inode);
2177 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2179 /* If this is potentially the last reference to this inode, get the
2180 * OBD EA data first so the client can destroy OST objects. We
2181 * only do the object removal later if no open files/links remain. */
2182 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2183 child_inode->i_nlink == 1) {
2184 if (mds_orphan_open_count(child_inode) > 0) {
2185 /* need to lock pending_dir before transaction */
2186 down(&mds->mds_pending_dir->d_inode->i_sem);
2187 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2188 } else if (S_ISREG(child_inode->i_mode)) {
2189 mds_pack_inode2body(obd, body, child_inode, 0);
2190 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2191 body, child_inode, MDS_PACK_MD_LOCK, 0);
2195 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2196 switch (child_inode->i_mode & S_IFMT) {
2198 /* Drop any lingering child directories before we start our
2199 * transaction, to avoid doing multiple inode dirty/delete
2200 * in our compound transaction (bug 1321). */
2201 shrink_dcache_parent(dchild);
2202 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2205 GOTO(cleanup, rc = PTR_ERR(handle));
2206 rc = vfs_rmdir(dparent->d_inode, dchild);
2209 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2210 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2212 handle = fsfilt_start_log(obd, dparent->d_inode,
2213 FSFILT_OP_UNLINK, NULL,
2214 le32_to_cpu(lmm->lmm_stripe_count));
2216 GOTO(cleanup, rc = PTR_ERR(handle));
2217 rc = vfs_unlink(dparent->d_inode, dchild);
2225 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2228 GOTO(cleanup, rc = PTR_ERR(handle));
2229 rc = vfs_unlink(dparent->d_inode, dchild);
2232 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2235 GOTO(cleanup, rc = -EINVAL);
2238 if (rc == 0 && child_inode->i_nlink == 0) {
2239 if (mds_orphan_open_count(child_inode) > 0)
2240 rc = mds_orphan_add_link(rec, obd, dchild);
2243 GOTO(cleanup, rc = 0);
2245 if (!S_ISREG(child_inode->i_mode))
2248 if (!(body->valid & OBD_MD_FLEASIZE)) {
2249 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2250 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2251 } else if (mds_log_op_unlink(obd, child_inode,
2252 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2253 req->rq_repmsg->buflens[offset + 1],
2254 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2255 req->rq_repmsg->buflens[offset + 2],
2257 body->valid |= OBD_MD_FLCOOKIE;
2260 rc = mds_destroy_object(obd, child_inode, 1);
2262 CERROR("can't remove OST object, err %d\n",
2274 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2275 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2276 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2278 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2280 CERROR("error on parent setattr: rc = %d\n", err);
2282 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2283 handle, req, rc, 0);
2285 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2286 "unlinked", 0, NULL);
2287 switch(cleanup_phase) {
2288 case 5: /* pending_dir semaphore */
2289 up(&mds->mds_pending_dir->d_inode->i_sem);
2290 case 4: /* child inode semaphore */
2291 UP_READ_I_ALLOC_SEM(child_inode);
2292 /* handle splitted dir */
2294 /* master directory can be non-empty or something else ... */
2295 mds_unlink_slave_objs(obd, dchild);
2298 ptlrpc_save_llog_lock(req, lcl);
2299 case 3: /* child ino-reuse lock */
2300 if (rc && body != NULL) {
2301 // Don't unlink the OST objects if the MDS unlink failed
2305 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2307 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2308 case 2: /* child lock */
2309 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2310 if (child_lockh.cookie)
2311 ldlm_lock_decref(&child_lockh, LCK_EX);
2312 case 1: /* child and parent dentry, parent lock */
2314 if (parent_lockh[1].cookie != 0)
2315 ldlm_lock_decref(parent_lockh + 1, update_mode);
2318 ldlm_lock_decref(parent_lockh, LCK_PW);
2320 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2327 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2330 req->rq_status = rc;
2335 * to service requests from remote MDS to increment i_nlink
2337 static int mds_reint_link_acquire(struct mds_update_record *rec,
2338 int offset, struct ptlrpc_request *req,
2339 struct lustre_handle *lh)
2341 struct obd_device *obd = req->rq_export->exp_obd;
2342 struct ldlm_res_id src_res_id = { .name = {0} };
2343 struct lustre_handle *handle = NULL, src_lockh = {0};
2344 struct mds_obd *mds = mds_req2mds(req);
2345 int rc = 0, cleanup_phase = 0;
2346 struct dentry *de_src = NULL;
2347 ldlm_policy_data_t policy;
2348 int flags = LDLM_FL_ATOMIC_CB;
2351 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2352 obd->obd_name, OLID4(rec->ur_id1));
2354 /* Step 1: Lookup the source inode and target directory by ID */
2355 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2357 GOTO(cleanup, rc = PTR_ERR(de_src));
2358 cleanup_phase = 1; /* source dentry */
2360 src_res_id.name[0] = id_fid(rec->ur_id1);
2361 src_res_id.name[1] = id_group(rec->ur_id1);
2362 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2364 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2365 src_res_id, LDLM_IBITS, &policy,
2366 LCK_EX, &flags, mds_blocking_ast,
2367 ldlm_completion_ast, NULL, NULL,
2368 NULL, 0, NULL, &src_lockh);
2370 GOTO(cleanup, rc = -ENOLCK);
2371 cleanup_phase = 2; /* lock */
2373 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2375 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2376 if (IS_ERR(handle)) {
2377 rc = PTR_ERR(handle);
2380 i_nlink_inc(de_src->d_inode);
2381 mark_inode_dirty(de_src->d_inode);
2385 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2386 handle, req, rc, 0);
2387 switch (cleanup_phase) {
2390 ldlm_lock_decref(&src_lockh, LCK_EX);
2392 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2398 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2401 req->rq_status = rc;
2406 * request to link to foreign inode:
2407 * - acquire i_nlinks on this inode
2410 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2411 int offset, struct ptlrpc_request *req,
2412 struct lustre_handle *lh)
2414 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2415 struct obd_device *obd = req->rq_export->exp_obd;
2416 struct dentry *de_tgt_dir = NULL;
2417 struct mds_obd *mds = mds_req2mds(req);
2418 int rc = 0, cleanup_phase = 0;
2419 struct mdc_op_data *op_data;
2420 struct ptlrpc_request *request = NULL;
2424 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2425 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2426 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2427 OLID4(rec->ur_id1));
2429 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2430 tgt_dir_lockh, &update_mode,
2431 rec->ur_name, rec->ur_namelen - 1,
2432 MDS_INODELOCK_UPDATE);
2433 if (IS_ERR(de_tgt_dir))
2434 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2437 OBD_ALLOC(op_data, sizeof(*op_data));
2438 if (op_data == NULL)
2439 GOTO(cleanup, rc = -ENOMEM);
2441 memset(op_data, 0, sizeof(*op_data));
2442 op_data->id1 = *(rec->ur_id1);
2443 rc = md_link(mds->mds_md_exp, op_data, &request);
2444 OBD_FREE(op_data, sizeof(*op_data));
2447 ptlrpc_req_finished(request);
2453 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2455 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2456 if (IS_ERR(handle)) {
2457 rc = PTR_ERR(handle);
2463 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2464 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2465 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2466 id_fid(rec->ur_id1));
2469 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2470 handle, req, rc, 0);
2472 switch (cleanup_phase) {
2475 OBD_ALLOC(op_data, sizeof(*op_data));
2476 if (op_data != NULL) {
2478 memset(op_data, 0, sizeof(*op_data));
2480 op_data->id1 = *(rec->ur_id1);
2481 op_data->create_mode = rec->ur_mode;
2483 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2484 OBD_FREE(op_data, sizeof(*op_data));
2486 ptlrpc_req_finished(request);
2488 CERROR("error %d while dropping i_nlink on "
2489 "remote inode\n", rc);
2492 CERROR("rc %d prevented dropping i_nlink on "
2493 "remote inode\n", -ENOMEM);
2499 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2501 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2504 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2506 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2512 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2515 req->rq_status = rc;
2519 static int mds_reint_link(struct mds_update_record *rec, int offset,
2520 struct ptlrpc_request *req,
2521 struct lustre_handle *lh)
2523 struct obd_device *obd = req->rq_export->exp_obd;
2524 struct dentry *de_src = NULL;
2525 struct dentry *de_tgt_dir = NULL;
2526 struct dentry *dchild = NULL;
2527 struct mds_obd *mds = mds_req2mds(req);
2528 struct lustre_handle *handle = NULL;
2529 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2530 struct ldlm_res_id src_res_id = { .name = {0} };
2531 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2532 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2533 ldlm_policy_data_t tgt_dir_policy =
2534 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2535 int rc = 0, cleanup_phase = 0;
2537 int update_mode = 0;
2541 LASSERT(offset == 1);
2543 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2544 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2545 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2548 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2549 MD_COUNTER_INCREMENT(obd, link);
2551 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2552 GOTO(cleanup, rc = -ENOENT);
2554 if (id_group(rec->ur_id1) != mds->mds_num) {
2555 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2559 if (rec->ur_namelen == 1) {
2560 rc = mds_reint_link_acquire(rec, offset, req, lh);
2564 /* Step 1: Lookup the source inode and target directory by ID */
2565 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2567 GOTO(cleanup, rc = PTR_ERR(de_src));
2569 cleanup_phase = 1; /* source dentry */
2571 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2572 if (IS_ERR(de_tgt_dir)) {
2573 rc = PTR_ERR(de_tgt_dir);
2578 cleanup_phase = 2; /* target directory dentry */
2580 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2581 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2582 rec->ur_name, de_src->d_inode->i_ino);
2584 /* Step 2: Take the two locks */
2585 src_res_id.name[0] = id_fid(rec->ur_id1);
2586 src_res_id.name[1] = id_group(rec->ur_id1);
2587 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2588 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2591 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2592 int flags = LDLM_FL_ATOMIC_CB;
2593 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2595 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2596 tgt_dir_res_id, LDLM_IBITS,
2597 &src_policy, update_mode, &flags,
2599 ldlm_completion_ast, NULL, NULL,
2600 NULL, 0, NULL, tgt_dir_lockh + 1);
2602 GOTO(cleanup, rc = -ENOLCK);
2605 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2606 rec->ur_namelen - 1);
2607 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2608 (unsigned long)id_fid(rec->ur_id2),
2609 (unsigned long)id_group(rec->ur_id2),
2610 tgt_dir_res_id.name[2]);
2613 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2614 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2615 LCK_EX, &tgt_dir_policy);
2619 cleanup_phase = 3; /* locks */
2621 /* Step 3: Lookup the child */
2622 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2623 rec->ur_namelen - 1);
2624 if (IS_ERR(dchild)) {
2625 rc = PTR_ERR(dchild);
2626 if (rc != -EPERM && rc != -EACCES)
2627 CERROR("child lookup error %d\n", rc);
2631 cleanup_phase = 4; /* child dentry */
2633 if (dchild->d_inode) {
2634 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2635 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2640 /* Step 4: Do it. */
2641 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2643 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2644 if (IS_ERR(handle)) {
2645 rc = PTR_ERR(handle);
2649 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2650 if (rc && rc != -EPERM && rc != -EACCES)
2651 CERROR("vfs_link error %d\n", rc);
2653 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2654 handle, req, rc, 0);
2657 switch (cleanup_phase) {
2658 case 4: /* child dentry */
2662 ldlm_lock_decref(&src_lockh, LCK_EX);
2663 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2665 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2666 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2668 case 2: /* target dentry */
2670 if (tgt_dir_lockh[1].cookie && update_mode)
2671 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2675 case 1: /* source dentry */
2680 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2683 req->rq_status = rc;
2687 /* The idea here is that we need to get four locks in the end:
2688 * one on each parent directory, one on each child. We need to take
2689 * these locks in some kind of order (to avoid deadlocks), and the order
2690 * I selected is "increasing resource number" order. We need to look up
2691 * the children, however, before we know what the resource number(s) are.
2692 * Thus the following plan:
2694 * 1,2. Look up the parents
2695 * 3,4. Look up the children
2696 * 5. Take locks on the parents and children, in order
2697 * 6. Verify that the children haven't changed since they were looked up
2699 * If there was a race and the children changed since they were first looked
2700 * up, it is possible that mds_verify_child() will be able to just grab the
2701 * lock on the new child resource (if it has a higher resource than any other)
2702 * but we need to compare against not only its parent, but also against the
2703 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2705 * We need the fancy igrab() on the child inodes because we aren't holding a
2706 * lock on the parent after the lookup is done, so dentry->d_inode may change
2707 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2709 static int mds_get_parents_children_locked(struct obd_device *obd,
2710 struct mds_obd *mds,
2711 struct lustre_id *p1_id,
2712 struct dentry **de_srcdirp,
2713 struct lustre_id *p2_id,
2714 struct dentry **de_tgtdirp,
2716 const char *old_name, int old_len,
2717 struct dentry **de_oldp,
2718 const char *new_name, int new_len,
2719 struct dentry **de_newp,
2720 struct lustre_handle *dlm_handles,
2723 struct ldlm_res_id p1_res_id = { .name = {0} };
2724 struct ldlm_res_id p2_res_id = { .name = {0} };
2725 struct ldlm_res_id c1_res_id = { .name = {0} };
2726 struct ldlm_res_id c2_res_id = { .name = {0} };
2727 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2728 /* Only dentry should disappear, but the inode itself would be
2729 intact otherwise. */
2730 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2731 /* If something is going to be replaced, both dentry and inode locks are
2733 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2734 struct ldlm_res_id *maxres_src, *maxres_tgt;
2735 struct inode *inode;
2736 int rc = 0, cleanup_phase = 0;
2737 __u32 child_gen1 = 0;
2738 __u32 child_gen2 = 0;
2739 unsigned long child_ino1 = 0;
2740 unsigned long child_ino2 = 0;
2743 /* Step 1: Lookup the source directory */
2744 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2745 if (IS_ERR(*de_srcdirp))
2746 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2748 cleanup_phase = 1; /* source directory dentry */
2750 p1_res_id.name[0] = id_fid(p1_id);
2751 p1_res_id.name[1] = id_group(p1_id);
2753 /* Step 2: Lookup the target directory */
2754 if (id_equal_stc(p1_id, p2_id)) {
2755 *de_tgtdirp = dget(*de_srcdirp);
2757 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2758 if (IS_ERR(*de_tgtdirp)) {
2759 rc = PTR_ERR(*de_tgtdirp);
2765 cleanup_phase = 2; /* target directory dentry */
2767 p2_res_id.name[0] = id_fid(p2_id);
2768 p2_res_id.name[1] = id_group(p2_id);
2771 dlm_handles[5].cookie = 0;
2772 dlm_handles[6].cookie = 0;
2774 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2776 * get a temp lock on just fid, group to flush client cache and
2777 * to protect dirs from concurrent splitting.
2779 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2780 LCK_PW, &p_policy, &p2_res_id,
2781 &dlm_handles[6], LCK_PW, &p_policy);
2785 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2787 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2790 CDEBUG(D_INFO, "take locks on "
2791 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2792 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2793 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2798 /* Step 3: Lookup the source child entry */
2799 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2801 if (IS_ERR(*de_oldp)) {
2802 rc = PTR_ERR(*de_oldp);
2803 CERROR("old child lookup error (%.*s): %d\n",
2804 old_len - 1, old_name, rc);
2808 cleanup_phase = 4; /* original name dentry */
2810 inode = (*de_oldp)->d_inode;
2811 if (inode != NULL) {
2812 struct lustre_id sid;
2814 inode = igrab(inode);
2816 GOTO(cleanup, rc = -ENOENT);
2818 down(&inode->i_sem);
2819 rc = mds_read_inode_sid(obd, inode, &sid);
2822 CERROR("Can't read inode self id, inode %lu, "
2823 "rc %d\n", inode->i_ino, rc);
2828 child_ino1 = inode->i_ino;
2829 child_gen1 = inode->i_generation;
2830 c1_res_id.name[0] = id_fid(&sid);
2831 c1_res_id.name[1] = id_group(&sid);
2833 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2834 child_ino1 = (*de_oldp)->d_inum;
2835 child_gen1 = (*de_oldp)->d_generation;
2836 c1_res_id.name[0] = (*de_oldp)->d_fid;
2837 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2839 GOTO(cleanup, rc = -ENOENT);
2842 /* Step 4: Lookup the target child entry */
2843 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2845 if (IS_ERR(*de_newp)) {
2846 rc = PTR_ERR(*de_newp);
2847 CERROR("new child lookup error (%.*s): %d\n",
2848 old_len - 1, old_name, rc);
2852 cleanup_phase = 5; /* target dentry */
2854 inode = (*de_newp)->d_inode;
2855 if (inode != NULL) {
2856 struct lustre_id sid;
2858 inode = igrab(inode);
2862 down(&inode->i_sem);
2863 rc = mds_read_inode_sid(obd, inode, &sid);
2866 CERROR("Can't read inode self id, inode %lu, "
2867 "rc %d\n", inode->i_ino, rc);
2871 child_ino2 = inode->i_ino;
2872 child_gen2 = inode->i_generation;
2873 c2_res_id.name[0] = id_fid(&sid);
2874 c2_res_id.name[1] = id_group(&sid);
2876 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2877 child_ino2 = (*de_newp)->d_inum;
2878 child_gen2 = (*de_newp)->d_generation;
2879 c2_res_id.name[0] = (*de_newp)->d_fid;
2880 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2884 /* Step 5: Take locks on the parents and child(ren) */
2885 maxres_src = &p1_res_id;
2886 maxres_tgt = &p2_res_id;
2887 cleanup_phase = 5; /* target dentry */
2889 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2890 maxres_src = &c1_res_id;
2891 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2892 maxres_tgt = &c2_res_id;
2894 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2896 &p2_res_id, &dlm_handles[1], parent_mode,
2898 &c1_res_id, &dlm_handles[2], child_mode,
2900 &c2_res_id, &dlm_handles[3], child_mode,
2905 cleanup_phase = 6; /* parent and child(ren) locks */
2907 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2908 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2909 parent_mode, &c1_res_id, &dlm_handles[2],
2910 de_oldp, child_mode, &c1_policy, old_name, old_len,
2911 maxres_tgt, child_ino1, child_gen1);
2913 if (c2_res_id.name[0] != 0)
2914 ldlm_lock_decref(&dlm_handles[3], child_mode);
2915 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2922 if (!DENTRY_VALID(*de_oldp))
2923 GOTO(cleanup, rc = -ENOENT);
2925 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2926 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2927 parent_mode, &c2_res_id, &dlm_handles[3],
2928 de_newp, child_mode, &c2_policy, new_name,
2929 new_len, maxres_src, child_ino2, child_gen2);
2931 ldlm_lock_decref(&dlm_handles[2], child_mode);
2932 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2942 switch (cleanup_phase) {
2943 case 6: /* child lock(s) */
2944 if (c2_res_id.name[0] != 0)
2945 ldlm_lock_decref(&dlm_handles[3], child_mode);
2946 if (c1_res_id.name[0] != 0)
2947 ldlm_lock_decref(&dlm_handles[2], child_mode);
2948 if (dlm_handles[1].cookie != 0)
2949 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2950 if (dlm_handles[0].cookie != 0)
2951 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2952 case 5: /* target dentry */
2954 case 4: /* source dentry */
2958 if (dlm_handles[5].cookie != 0)
2959 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2960 if (dlm_handles[6].cookie != 0)
2961 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2963 case 2: /* target directory dentry */
2964 l_dput(*de_tgtdirp);
2965 case 1: /* source directry dentry */
2966 l_dput(*de_srcdirp);
2974 * checks if dentry can be removed. This function also handles cross-ref
2977 static int mds_check_for_rename(struct obd_device *obd,
2978 struct dentry *dentry)
2980 struct mds_obd *mds = &obd->u.mds;
2981 struct lustre_handle *rlockh;
2982 struct ptlrpc_request *req;
2983 struct mdc_op_data *op_data;
2984 struct lookup_intent it;
2985 int handle_size, rc = 0;
2988 LASSERT(dentry != NULL);
2990 if (dentry->d_inode) {
2991 if (S_ISDIR(dentry->d_inode->i_mode) &&
2992 !mds_is_dir_empty(obd, dentry))
2995 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2996 handle_size = sizeof(struct lustre_handle);
2998 OBD_ALLOC(rlockh, handle_size);
3002 memset(rlockh, 0, handle_size);
3003 OBD_ALLOC(op_data, sizeof(*op_data));
3004 if (op_data == NULL) {
3005 OBD_FREE(rlockh, handle_size);
3008 memset(op_data, 0, sizeof(*op_data));
3009 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3011 it.it_op = IT_UNLINK;
3012 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3015 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3016 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3017 mds_blocking_ast, NULL);
3018 OBD_FREE(op_data, sizeof(*op_data));
3022 OBD_FREE(it.d.fs_data,
3023 sizeof(struct lustre_intent_data));
3026 if (rlockh->cookie != 0)
3027 ldlm_lock_decref(rlockh, LCK_EX);
3029 if (LUSTRE_IT(&it)->it_data) {
3030 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3031 ptlrpc_req_finished(req);
3034 if (LUSTRE_IT(&it)->it_status)
3035 rc = LUSTRE_IT(&it)->it_status;
3036 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3037 OBD_FREE(rlockh, handle_size);
3042 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3043 struct ptlrpc_request *req, struct lustre_id *id,
3044 struct dentry *de_dir, struct dentry *de)
3046 struct obd_device *obd = req->rq_export->exp_obd;
3047 struct mds_obd *mds = mds_req2mds(req);
3048 void *handle = NULL;
3054 * name exists and points to local inode try to unlink this name
3055 * and create new one.
3057 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3058 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3059 (unsigned long)de->d_inode->i_generation);
3061 /* checking if we can remove local dentry. */
3062 rc = mds_check_for_rename(obd, de);
3066 handle = fsfilt_start(obd, de_dir->d_inode,
3067 FSFILT_OP_RENAME, NULL);
3069 GOTO(cleanup, rc = PTR_ERR(handle));
3070 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3073 } else if (de->d_flags & DCACHE_CROSS_REF) {
3074 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3075 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3076 (unsigned long)de->d_fid);
3078 /* checking if we can remove local dentry. */
3079 rc = mds_check_for_rename(obd, de);
3084 * to be fully POSIX compatible, we should add one more check:
3086 * if de_new is subdir of dir rec->ur_id1. If so - return
3089 * I do not know how to implement it right now, because
3090 * inodes/dentries for new and old names lie on different MDS,
3091 * so add this notice here just to make it visible for the rest
3092 * of developers and do not forget about. And when this check
3093 * will be added, del_cross_ref should gone, that is local
3094 * dentry is able to be removed if all checks passed. --umka
3097 handle = fsfilt_start(obd, de_dir->d_inode,
3098 FSFILT_OP_RENAME, NULL);
3100 GOTO(cleanup, rc = PTR_ERR(handle));
3101 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3105 /* name doesn't exist. the simplest case. */
3106 handle = fsfilt_start(obd, de_dir->d_inode,
3107 FSFILT_OP_LINK, NULL);
3109 GOTO(cleanup, rc = PTR_ERR(handle));
3112 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3113 rec->ur_tgtlen - 1, id_ino(id),
3114 id_gen(id), id_group(id), id_fid(id));
3116 CERROR("add_dir_entry() returned error %d\n", rc);
3122 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3123 handle, req, rc, 0);
3128 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3129 struct ptlrpc_request *req, struct dentry *de_dir,
3132 struct obd_device *obd = req->rq_export->exp_obd;
3133 struct mds_obd *mds = mds_req2mds(req);
3134 void *handle = NULL;
3138 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3140 GOTO(cleanup, rc = PTR_ERR(handle));
3141 rc = fsfilt_del_dir_entry(obd, de);
3146 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3147 handle, req, rc, 0);
3151 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3152 int offset, struct ptlrpc_request *req)
3154 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3155 struct obd_device *obd = req->rq_export->exp_obd;
3156 struct mds_obd *mds = mds_req2mds(req);
3157 struct lustre_handle child_lockh = {0};
3158 struct dentry *de_tgtdir = NULL;
3159 struct dentry *de_new = NULL;
3160 int cleanup_phase = 0;
3161 int update_mode, rc = 0;
3165 * another MDS executing rename operation has asked us to create target
3166 * name. such a creation should destroy existing target name.
3168 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3169 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3171 /* first, lookup the target */
3172 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3173 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3174 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3175 &child_lockh, &de_new, LCK_EX,
3176 MDS_INODELOCK_LOOKUP);
3183 LASSERT(de_tgtdir->d_inode);
3186 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3192 if (cleanup_phase == 1) {
3194 if (parent_lockh[1].cookie != 0)
3195 ldlm_lock_decref(parent_lockh + 1, update_mode);
3197 ldlm_lock_decref(parent_lockh, LCK_PW);
3198 if (child_lockh.cookie != 0)
3199 ldlm_lock_decref(&child_lockh, LCK_EX);
3204 req->rq_status = rc;
3208 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3209 struct ptlrpc_request *req)
3211 struct obd_device *obd = req->rq_export->exp_obd;
3212 struct ptlrpc_request *req2 = NULL;
3213 struct dentry *de_srcdir = NULL;
3214 struct dentry *de_old = NULL;
3215 struct mds_obd *mds = mds_req2mds(req);
3216 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3217 struct lustre_handle child_lockh = {0};
3218 struct mdc_op_data *op_data;
3219 int update_mode, rc = 0;
3222 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3223 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3225 OBD_ALLOC(op_data, sizeof(*op_data));
3226 if (op_data == NULL)
3228 memset(op_data, 0, sizeof(*op_data));
3230 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3231 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3232 &update_mode, rec->ur_name,
3233 rec->ur_namelen, &child_lockh, &de_old,
3234 LCK_EX, MDS_INODELOCK_LOOKUP);
3236 OBD_FREE(op_data, sizeof(*op_data));
3241 LASSERT(de_srcdir->d_inode);
3245 * we already know the target should be created on another MDS so, we
3246 * have to request that MDS to do it.
3249 /* prepare source id */
3250 if (de_old->d_flags & DCACHE_CROSS_REF) {
3251 LASSERT(de_old->d_inode == NULL);
3252 CDEBUG(D_OTHER, "request to move remote name\n");
3253 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3254 } else if (de_old->d_inode == NULL) {
3255 /* oh, source doesn't exist */
3256 OBD_FREE(op_data, sizeof(*op_data));
3257 GOTO(cleanup, rc = -ENOENT);
3259 struct lustre_id sid;
3260 struct inode *inode = de_old->d_inode;
3262 LASSERT(inode != NULL);
3263 CDEBUG(D_OTHER, "request to move local name\n");
3264 id_ino(&op_data->id1) = inode->i_ino;
3265 id_group(&op_data->id1) = mds->mds_num;
3266 id_gen(&op_data->id1) = inode->i_generation;
3268 down(&inode->i_sem);
3269 rc = mds_read_inode_sid(obd, inode, &sid);
3272 CERROR("Can't read inode self id, "
3273 "inode %lu, rc = %d\n",
3278 id_fid(&op_data->id1) = id_fid(&sid);
3281 op_data->id2 = *rec->ur_id2;
3282 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3283 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3284 OBD_FREE(op_data, sizeof(*op_data));
3289 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3295 ptlrpc_req_finished(req2);
3298 if (parent_lockh[1].cookie != 0)
3299 ldlm_lock_decref(parent_lockh + 1, update_mode);
3301 ldlm_lock_decref(parent_lockh, LCK_PW);
3302 if (child_lockh.cookie != 0)
3303 ldlm_lock_decref(&child_lockh, LCK_EX);
3308 req->rq_status = rc;
3312 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3313 struct ptlrpc_request *req, struct lustre_handle *lockh)
3315 struct obd_device *obd = req->rq_export->exp_obd;
3316 struct dentry *de_srcdir = NULL;
3317 struct dentry *de_tgtdir = NULL;
3318 struct dentry *de_old = NULL;
3319 struct dentry *de_new = NULL;
3320 struct inode *old_inode = NULL, *new_inode = NULL;
3321 struct mds_obd *mds = mds_req2mds(req);
3322 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3323 struct mds_body *body = NULL;
3324 struct llog_create_locks *lcl = NULL;
3325 struct lov_mds_md *lmm = NULL;
3326 int rc = 0, cleanup_phase = 0;
3327 void *handle = NULL;
3330 LASSERT(offset == 1);
3332 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3333 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3336 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3338 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3339 DEBUG_REQ(D_HA, req, "rename replay");
3340 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3341 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3342 req->rq_repmsg->buflens[2]);
3345 MD_COUNTER_INCREMENT(obd, rename);
3347 if (rec->ur_namelen == 1) {
3348 rc = mds_reint_rename_create_name(rec, offset, req);
3352 /* check if new name should be located on remote target. */
3353 if (id_group(rec->ur_id2) != mds->mds_num) {
3354 rc = mds_reint_rename_to_remote(rec, offset, req);
3358 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3359 rec->ur_id2, &de_tgtdir, LCK_PW,
3360 rec->ur_name, rec->ur_namelen,
3361 &de_old, rec->ur_tgt,
3362 rec->ur_tgtlen, &de_new,
3363 dlm_handles, LCK_EX);
3367 cleanup_phase = 1; /* parent(s), children, locks */
3368 old_inode = de_old->d_inode;
3369 new_inode = de_new->d_inode;
3371 /* sanity check for src inode */
3372 if (de_old->d_flags & DCACHE_CROSS_REF) {
3373 LASSERT(de_old->d_inode == NULL);
3376 * in the case of cross-ref dir, we can perform this check only
3377 * if child and parent lie on the same mds. This is because
3378 * otherwise they can have the same inode numbers.
3380 if (de_old->d_mdsnum == mds->mds_num) {
3381 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3382 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3383 GOTO(cleanup, rc = -EINVAL);
3386 LASSERT(de_old->d_inode != NULL);
3387 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3388 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3389 GOTO(cleanup, rc = -EINVAL);
3392 /* sanity check for dest inode */
3393 if (de_new->d_flags & DCACHE_CROSS_REF) {
3394 LASSERT(new_inode == NULL);
3396 /* the same check about target dentry. */
3397 if (de_new->d_mdsnum == mds->mds_num) {
3398 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3399 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3400 GOTO(cleanup, rc = -EINVAL);
3404 * regular files usualy do not have ->rename() implemented. But
3405 * we handle only this case when @de_new is cross-ref entry,
3406 * because in other cases it will be handled by vfs_rename().
3408 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3409 !de_old->d_inode->i_op->rename))
3410 GOTO(cleanup, rc = -EPERM);
3413 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3414 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3415 GOTO(cleanup, rc = -EINVAL);
3420 * check if inodes point to each other. This should be checked before
3421 * is_subdir() check, as for the same entries it will think that they
3424 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3425 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3426 old_inode == new_inode)
3427 GOTO(cleanup, rc = 0);
3429 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3431 * check if we are moving old entry into its child. 2.6 does not check
3432 * for this in vfs_rename() anymore.
3434 if (is_subdir(de_new, de_old))
3435 GOTO(cleanup, rc = -EINVAL);
3439 * if we are about to remove the target at first, pass the EA of that
3440 * inode to client to perform and cleanup on OST.
3442 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3443 LASSERT(body != NULL);
3445 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3447 DOWN_READ_I_ALLOC_SEM(new_inode);
3449 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3451 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3452 new_inode->i_nlink == 2) ||
3453 new_inode->i_nlink == 1)) {
3454 if (mds_orphan_open_count(new_inode) > 0) {
3455 /* need to lock pending_dir before transaction */
3456 down(&mds->mds_pending_dir->d_inode->i_sem);
3457 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3458 } else if (S_ISREG(new_inode->i_mode)) {
3459 mds_pack_inode2body(obd, body, new_inode, 0);
3460 mds_pack_md(obd, req->rq_repmsg, 1, body,
3461 new_inode, MDS_PACK_MD_LOCK, 0);
3465 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3466 de_srcdir->d_inode->i_sb);
3468 if (de_old->d_flags & DCACHE_CROSS_REF) {
3469 struct lustre_id old_id;
3471 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3473 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3478 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3483 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3484 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3485 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3488 GOTO(cleanup, rc = PTR_ERR(handle));
3491 de_old->d_fsdata = req;
3492 de_new->d_fsdata = req;
3493 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3496 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3497 if (mds_orphan_open_count(new_inode) > 0)
3498 rc = mds_orphan_add_link(rec, obd, de_new);
3501 GOTO(cleanup, rc = 0);
3503 if (!S_ISREG(new_inode->i_mode))
3506 if (!(body->valid & OBD_MD_FLEASIZE)) {
3507 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3508 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3509 } else if (mds_log_op_unlink(obd, new_inode,
3510 lustre_msg_buf(req->rq_repmsg,1,0),
3511 req->rq_repmsg->buflens[1],
3512 lustre_msg_buf(req->rq_repmsg,2,0),
3513 req->rq_repmsg->buflens[2],
3515 body->valid |= OBD_MD_FLCOOKIE;
3518 rc = mds_destroy_object(obd, old_inode, 1);
3520 CERROR("can't remove OST object, err %d\n",
3527 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3528 handle, req, rc, 0);
3530 switch (cleanup_phase) {
3532 up(&mds->mds_pending_dir->d_inode->i_sem);
3535 UP_READ_I_ALLOC_SEM(new_inode);
3538 if (dlm_handles[5].cookie != 0)
3539 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3540 if (dlm_handles[6].cookie != 0)
3541 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3544 ptlrpc_save_llog_lock(req, lcl);
3547 if (dlm_handles[3].cookie != 0)
3548 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3549 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3550 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3551 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3553 if (dlm_handles[3].cookie != 0)
3554 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3555 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3556 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3557 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3566 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3569 req->rq_status = rc;
3573 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3574 struct ptlrpc_request *, struct lustre_handle *);
3576 static mds_reinter reinters[REINT_MAX + 1] = {
3577 [REINT_SETATTR] mds_reint_setattr,
3578 [REINT_CREATE] mds_reint_create,
3579 [REINT_LINK] mds_reint_link,
3580 [REINT_UNLINK] mds_reint_unlink,
3581 [REINT_RENAME] mds_reint_rename,
3582 [REINT_OPEN] mds_open
3585 int mds_reint_rec(struct mds_update_record *rec, int offset,
3586 struct ptlrpc_request *req, struct lustre_handle *lockh)
3588 struct obd_device *obd = req->rq_export->exp_obd;
3589 struct lvfs_run_ctxt saved;
3592 /* checked by unpacker */
3593 LASSERT(rec->ur_opcode <= REINT_MAX &&
3594 reinters[rec->ur_opcode] != NULL);
3596 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3597 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3598 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);