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 "mds_internal.h"
50 struct mds_logcancel_data {
51 struct lov_mds_md *mlcd_lmm;
55 struct llog_cookie mlcd_cookies[0];
58 static void mds_cancel_cookies_cb(struct obd_device *obd,
59 __u64 transno, void *cb_data,
62 struct mds_logcancel_data *mlcd = cb_data;
63 struct lov_stripe_md *lsm = NULL;
64 struct llog_ctxt *ctxt;
67 obd_transno_commit_cb(obd, transno, error);
69 CDEBUG(D_HA, "cancelling %d cookies\n",
70 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
72 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
73 mlcd->mlcd_eadatalen);
75 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
76 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
79 ///* XXX 0 normally, SENDNOW for debug */);
80 ctxt = llog_get_context(&obd->obd_llogs,
81 mlcd->mlcd_cookies[0].lgc_subsys + 1);
82 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
83 sizeof(*mlcd->mlcd_cookies),
84 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
86 CERROR("error cancelling %d log cookies: rc %d\n",
87 (int)(mlcd->mlcd_cookielen /
88 sizeof(*mlcd->mlcd_cookies)), rc);
89 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
92 OBD_FREE(mlcd, mlcd->mlcd_size);
95 /* Assumes caller has already pushed us into the kernel context. */
96 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
97 struct ptlrpc_request *req, int rc, __u32 op_data)
99 struct mds_export_data *med = &req->rq_export->exp_mds_data;
100 struct obd_device *obd = req->rq_export->exp_obd;
101 struct mds_client_data *mcd = med->med_mcd;
102 int err, log_pri = D_HA;
107 /* if the export has already been failed, we have no last_rcvd slot */
108 if (req->rq_export->exp_failed) {
109 CERROR("committing transaction for disconnected client\n");
111 GOTO(out_commit, rc);
118 if (handle == NULL) {
119 /* if we're starting our own xaction, use our own inode */
120 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
121 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
122 if (IS_ERR(handle)) {
123 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
124 RETURN(PTR_ERR(handle));
130 transno = req->rq_reqmsg->transno;
132 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
133 } else if (transno == 0) {
134 spin_lock(&mds->mds_transno_lock);
135 transno = ++mds->mds_last_transno;
136 spin_unlock(&mds->mds_transno_lock);
138 spin_lock(&mds->mds_transno_lock);
139 if (transno > mds->mds_last_transno)
140 mds->mds_last_transno = transno;
141 spin_unlock(&mds->mds_transno_lock);
143 req->rq_repmsg->transno = req->rq_transno = transno;
144 if (req->rq_reqmsg->opc == MDS_CLOSE) {
145 mcd->mcd_last_close_transno = cpu_to_le64(transno);
146 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
147 mcd->mcd_last_close_result = cpu_to_le32(rc);
148 mcd->mcd_last_close_data = cpu_to_le32(op_data);
150 mcd->mcd_last_transno = cpu_to_le64(transno);
151 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
152 mcd->mcd_last_result = cpu_to_le32(rc);
153 mcd->mcd_last_data = cpu_to_le32(op_data);
156 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
157 mds_commit_last_transno_cb, NULL);
159 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
160 sizeof(*mcd), &off, 0);
168 DEBUG_REQ(log_pri, req,
169 "wrote trans #"LPU64" client %s at idx %u: err = %d",
170 transno, mcd->mcd_uuid, med->med_idx, err);
172 err = mds_update_last_fid(obd, handle, 0);
179 err = mds_dt_write_objids(obd);
185 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
189 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
190 req->rq_export->exp_sync);
192 CERROR("error committing transaction: %d\n", err);
200 /* this gives the same functionality as the code between
201 * sys_chmod and inode_setattr
202 * chown_common and inode_setattr
203 * utimes and inode_setattr
206 /* Just for the case if we have some clients that know about ATTR_RAW */
207 #define ATTR_RAW 8192
209 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
211 time_t now = LTIME_S(CURRENT_TIME);
212 struct iattr *attr = &rec->ur_iattr;
213 unsigned int ia_valid = attr->ia_valid;
217 /* only fix up attrs if the client VFS didn't already */
219 if (!(ia_valid & ATTR_RAW))
222 if (!(ia_valid & ATTR_CTIME_SET))
223 LTIME_S(attr->ia_ctime) = now;
224 if (!(ia_valid & ATTR_ATIME_SET))
225 LTIME_S(attr->ia_atime) = now;
226 if (!(ia_valid & ATTR_MTIME_SET))
227 LTIME_S(attr->ia_mtime) = now;
229 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
233 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
234 if (rec->ur_fsuid != inode->i_uid &&
235 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
239 if (ia_valid & ATTR_SIZE) {
240 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
244 if (ia_valid & ATTR_UID) {
247 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
249 if (attr->ia_uid == (uid_t) -1)
250 attr->ia_uid = inode->i_uid;
251 if (attr->ia_gid == (gid_t) -1)
252 attr->ia_gid = inode->i_gid;
253 attr->ia_mode = inode->i_mode;
255 * If the user or group of a non-directory has been
256 * changed by a non-root user, remove the setuid bit.
257 * 19981026 David C Niemi <niemi@tux.org>
259 * Changed this to apply to all users, including root,
260 * to avoid some races. This is the behavior we had in
261 * 2.0. The check for non-root was definitely wrong
262 * for 2.2 anyway, as it should have been using
263 * CAP_FSETID rather than fsuid -- 19990830 SD.
265 if ((inode->i_mode & S_ISUID) == S_ISUID &&
266 !S_ISDIR(inode->i_mode)) {
267 attr->ia_mode &= ~S_ISUID;
268 attr->ia_valid |= ATTR_MODE;
271 * Likewise, if the user or group of a non-directory
272 * has been changed by a non-root user, remove the
273 * setgid bit UNLESS there is no group execute bit
274 * (this would be a file marked for mandatory
275 * locking). 19981026 David C Niemi <niemi@tux.org>
277 * Removed the fsuid check (see the comment above) --
280 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
281 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
282 attr->ia_mode &= ~S_ISGID;
283 attr->ia_valid |= ATTR_MODE;
285 } else if (ia_valid & ATTR_MODE) {
286 int mode = attr->ia_mode;
288 if (attr->ia_mode == (mode_t) -1)
289 attr->ia_mode = inode->i_mode;
291 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
296 void mds_steal_ack_locks(struct ptlrpc_request *req)
298 struct obd_export *exp = req->rq_export;
299 char str[PTL_NALFMT_SIZE];
300 struct list_head *tmp;
301 struct ptlrpc_reply_state *oldrep;
302 struct ptlrpc_service *svc;
303 struct llog_create_locks *lcl;
307 /* CAVEAT EMPTOR: spinlock order */
308 spin_lock_irqsave (&exp->exp_lock, flags);
309 list_for_each (tmp, &exp->exp_outstanding_replies) {
310 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
312 if (oldrep->rs_xid != req->rq_xid)
315 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
316 CERROR ("Resent req xid "LPX64" has mismatched opc: "
317 "new %d old %d\n", req->rq_xid,
318 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
320 svc = oldrep->rs_srv_ni->sni_service;
321 spin_lock (&svc->srv_lock);
323 list_del_init (&oldrep->rs_exp_list);
325 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
326 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
327 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
328 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
330 for (i = 0; i < oldrep->rs_nlocks; i++)
331 ptlrpc_save_lock(req,
332 &oldrep->rs_locks[i],
333 oldrep->rs_modes[i]);
334 oldrep->rs_nlocks = 0;
336 lcl = oldrep->rs_llog_locks;
337 oldrep->rs_llog_locks = NULL;
339 ptlrpc_save_llog_lock(req, lcl);
341 DEBUG_REQ(D_HA, req, "stole locks for");
342 ptlrpc_schedule_difficult_reply (oldrep);
344 spin_unlock (&svc->srv_lock);
345 spin_unlock_irqrestore (&exp->exp_lock, flags);
348 spin_unlock_irqrestore (&exp->exp_lock, flags);
351 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
353 if (req->rq_reqmsg->opc == MDS_CLOSE) {
354 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
355 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
356 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
357 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
359 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
360 mcd->mcd_last_transno, mcd->mcd_last_result);
361 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
362 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
365 mds_steal_ack_locks(req);
368 static void reconstruct_reint_setattr(struct mds_update_record *rec,
369 int offset, struct ptlrpc_request *req)
371 struct mds_export_data *med = &req->rq_export->exp_mds_data;
372 struct mds_body *body;
375 mds_req_from_mcd(req, med->med_mcd);
377 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
379 LASSERT(PTR_ERR(de) == req->rq_status);
383 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
384 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
386 /* Don't return OST-specific attributes if we didn't just set them */
387 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
388 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
389 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
390 body->valid |= OBD_MD_FLMTIME;
391 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
392 body->valid |= OBD_MD_FLATIME;
397 /* In the raw-setattr case, we lock the child inode.
398 * In the write-back case or if being called from open, the client holds a lock
401 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
402 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
403 struct ptlrpc_request *req, struct lustre_handle *lh)
405 struct mds_obd *mds = mds_req2mds(req);
406 struct obd_device *obd = req->rq_export->exp_obd;
407 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
408 struct mds_body *body;
409 struct dentry *de = NULL;
410 struct inode *inode = NULL;
411 struct lustre_handle lockh[2] = {{0}, {0}};
414 struct mds_logcancel_data *mlcd = NULL;
415 int rc = 0, cleanup_phase = 0, err;
419 LASSERT(offset == 1);
421 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
422 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
423 rec->ur_iattr.ia_valid);
425 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
426 MD_COUNTER_INCREMENT(obd, setattr);
428 if (med->med_remote) {
429 if (rec->ur_iattr.ia_valid & ATTR_GID) {
430 CWARN("Deny chgrp from remote client\n");
431 GOTO(cleanup, rc = -EPERM);
433 if (rec->ur_iattr.ia_valid & ATTR_UID) {
436 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
437 rec->ur_iattr.ia_uid);
438 if (uid == MDS_IDMAP_NOTFOUND) {
439 CWARN("Deny chown to uid %u\n",
440 rec->ur_iattr.ia_uid);
441 GOTO(cleanup, rc = -EPERM);
443 rec->ur_iattr.ia_uid = uid;
447 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
448 de = mds_id2dentry(obd, rec->ur_id1, NULL);
450 GOTO(cleanup, rc = PTR_ERR(de));
452 __u64 lockpart = MDS_INODELOCK_UPDATE;
453 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
454 lockpart |= MDS_INODELOCK_LOOKUP;
455 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
456 lockh, &parent_mode, NULL, 0, lockpart);
458 GOTO(cleanup, rc = PTR_ERR(de));
466 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
467 rec->ur_eadata != NULL)
470 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
472 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
474 GOTO(cleanup, rc = PTR_ERR(handle));
476 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
477 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
478 LTIME_S(rec->ur_iattr.ia_mtime),
479 LTIME_S(rec->ur_iattr.ia_ctime));
480 rc = mds_fix_attr(inode, rec);
484 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
485 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
486 (long)&rec->ur_iattr.ia_attr_flags);
488 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
491 if (rec->ur_iattr.ia_valid & ATTR_EA) {
492 int flags = (int)rec->ur_iattr.ia_attr_flags;
495 if (inode->i_op && inode->i_op->setxattr)
496 rc = inode->i_op->setxattr(de, rec->ur_eadata,
497 rec->ur_ea2data, rec->ur_ea2datalen,
499 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
501 if (inode->i_op && inode->i_op->removexattr)
502 rc = inode->i_op->removexattr(de,
504 } else if ((S_ISREG(inode->i_mode) ||
505 S_ISDIR(inode->i_mode)) && rec->ur_eadata != NULL) {
506 struct lov_stripe_md *lsm = NULL;
507 struct lov_user_md *lum = NULL;
509 rc = ll_permission(inode, MAY_WRITE, NULL);
513 lum = rec->ur_eadata;
514 /* if lmm_stripe_size is -1 delete default stripe from dir */
515 if (S_ISDIR(inode->i_mode) &&
516 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
517 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
521 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, mds->mds_dt_exp,
522 0, &lsm, rec->ur_eadata);
526 obd_free_memmd(mds->mds_dt_exp, &lsm);
527 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
528 rec->ur_eadatalen, EA_LOV);
535 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
536 mds_pack_inode2body(obd, body, inode, 1);
538 /* Don't return OST-specific attributes if we didn't just set them */
539 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
540 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
541 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
542 body->valid |= OBD_MD_FLMTIME;
543 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
544 body->valid |= OBD_MD_FLATIME;
546 mds_body_do_reverse_map(med, body);
548 /* The logcookie should be no use anymore, why nobody remove
549 * following code block?
551 LASSERT(rec->ur_cookielen == 0);
552 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
553 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
556 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
558 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
559 mlcd->mlcd_cookielen = rec->ur_cookielen;
560 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
561 mlcd->mlcd_cookielen;
562 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
563 mlcd->mlcd_cookielen);
564 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
565 mlcd->mlcd_eadatalen);
567 CERROR("unable to allocate log cancel data\n");
573 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
574 handle, mds_cancel_cookies_cb, mlcd);
575 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
576 switch (cleanup_phase) {
578 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
579 rec->ur_eadata != NULL)
584 if (lockh[1].cookie != 0)
585 ldlm_lock_decref(lockh + 1, parent_mode);
588 ldlm_lock_decref(lockh, LCK_PW);
590 ptlrpc_save_lock (req, lockh, LCK_PW);
605 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
606 struct ptlrpc_request *req)
608 struct mds_export_data *med = &req->rq_export->exp_mds_data;
609 struct dentry *parent, *child;
610 struct mds_body *body;
613 mds_req_from_mcd(req, med->med_mcd);
615 if (req->rq_status) {
620 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
621 LASSERT(!IS_ERR(parent));
622 child = ll_lookup_one_len(rec->ur_name, parent,
623 rec->ur_namelen - 1);
624 LASSERT(!IS_ERR(child));
625 if ((child->d_flags & DCACHE_CROSS_REF)) {
626 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
627 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
628 mds_pack_dentry2body(req2obd(req), body, child, 1);
629 } else if (child->d_inode == NULL) {
630 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
631 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
632 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
634 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
635 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
642 static int mds_get_default_acl(struct inode *dir, void **pacl)
644 struct dentry de = { .d_inode = dir };
647 LASSERT(S_ISDIR(dir->i_mode));
649 if (!dir->i_op->getxattr)
652 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
653 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
658 OBD_ALLOC(*pacl, size);
662 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
664 /* since we already locked the dir, it should not change
665 * between the 2 getxattr calls
667 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
668 OBD_FREE(*pacl, size);
675 static int mds_reint_create(struct mds_update_record *rec, int offset,
676 struct ptlrpc_request *req,
677 struct lustre_handle *lh)
679 struct dentry *dparent = NULL;
680 struct mds_obd *mds = mds_req2mds(req);
681 struct obd_device *obd = req->rq_export->exp_obd;
682 struct dentry *dchild = NULL;
683 struct inode *dir = NULL;
685 struct lustre_handle lockh[2] = {{0}, {0}};
687 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
689 struct dentry_params dp;
690 struct mea *mea = NULL;
694 LASSERT(offset == 1);
696 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
699 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
700 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
701 rec->ur_name, rec->ur_mode);
703 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
705 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
706 GOTO(cleanup, rc = -ESTALE);
708 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
709 lockh, &parent_mode, rec->ur_name,
710 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
711 if (IS_ERR(dparent)) {
712 rc = PTR_ERR(dparent);
713 CERROR("parent lookup error %d\n", rc);
716 cleanup_phase = 1; /* locked parent dentry */
717 dir = dparent->d_inode;
720 ldlm_lock_dump_handle(D_OTHER, lockh);
722 /* try to retrieve MEA data for this dir */
723 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
729 * dir is already splitted, check is requested filename should
730 * live at this MDS or at another one.
732 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
733 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
734 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
735 " should be %lu(%d)\n",
736 mea->mea_master, dparent->d_inode->i_ino,
737 dparent->d_inode->i_generation, rec->ur_name,
738 (unsigned long)id_group(&mea->mea_ids[i]), i);
739 GOTO(cleanup, rc = -ERESTART);
743 dchild = ll_lookup_one_len(rec->ur_name, dparent,
744 rec->ur_namelen - 1);
745 if (IS_ERR(dchild)) {
746 rc = PTR_ERR(dchild);
747 CERROR("Can't find "DLID4"/%s, error %d\n",
748 OLID4(rec->ur_id1), rec->ur_name, rc);
752 cleanup_phase = 2; /* child dentry */
754 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
756 if (type == S_IFREG || type == S_IFDIR) {
757 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
758 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
759 obd->obd_name, dparent->d_inode->i_ino,
760 dparent->d_inode->i_generation, rc, parent_mode);
762 /* dir got splitted */
763 GOTO(cleanup, rc = -ERESTART);
765 /* error happened during spitting. */
770 if (dir->i_mode & S_ISGID) {
771 if (S_ISDIR(rec->ur_mode))
772 rec->ur_mode |= S_ISGID;
776 * here inode number should be used only in the case of replaying. It is
777 * needed to check if object already created in the case of creating
780 dchild->d_fsdata = (void *)&dp;
781 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
786 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
788 GOTO(cleanup, rc = PTR_ERR(handle));
789 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
791 if (rc == 0 && rec->ur_eadata) {
792 /*for CMOBD to set lov md info when cmobd reint create*/
793 CDEBUG(D_INFO, "set lsm %p, len %d to inode %lu \n",
794 rec->ur_eadata, rec->ur_eadatalen,
795 dchild->d_inode->i_ino);
796 fsfilt_set_md(obd, dchild->d_inode, handle, rec->ur_eadata,
797 rec->ur_eadatalen, EA_LOV);
806 * as Peter asked, mkdir() should distribute new directories
807 * over the whole cluster in order to distribute namespace
808 * processing load. first, we calculate which MDS to use to put
809 * new directory's inode in.
811 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
812 rec->ur_flags, &req->rq_peer, dir);
813 if (i == mds->mds_num) {
814 /* inode will be created locally */
815 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
817 GOTO(cleanup, rc = PTR_ERR(handle));
819 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
822 "Can't create dir \"%s\", rc = %d\n",
823 dchild->d_name.name, rc);
827 down(&dchild->d_inode->i_sem);
829 rc = mds_update_inode_sid(obd, dchild->d_inode,
830 handle, rec->ur_id2);
832 CERROR("mds_update_inode_sid() failed, inode %lu, "
833 "rc %d\n", dchild->d_inode->i_ino, rc);
837 * make sure, that fid is up-to-date.
839 mds_set_last_fid(obd, id_fid(rec->ur_id2));
841 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
844 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
845 "rc %d\n", dchild->d_inode->i_ino, rc);
848 up(&dchild->d_inode->i_sem);
854 nstripes = *(u16 *)rec->ur_eadata;
856 if (rc == 0 && nstripes) {
858 * we pass LCK_EX to split routine to signal,
859 * that we have exclusive access to the
860 * directory. Simple because nobody knows it
861 * already exists -bzzz
863 rc = mds_try_to_split_dir(obd, dchild,
867 /* dir got splitted */
870 /* an error occured during
875 } else if (!DENTRY_VALID(dchild)) {
876 /* inode will be created on another MDS */
877 struct obdo *oa = NULL;
878 struct mds_body *body;
882 /* first, create that inode */
885 GOTO(cleanup, rc = -ENOMEM);
890 if (rec->ur_eadata) {
891 /* user asks for creating splitted dir */
892 oa->o_easize = *((u16 *) rec->ur_eadata);
895 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
896 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
898 /* adjust the uid/gid/mode bits */
899 oa->o_mode = rec->ur_mode;
900 oa->o_uid = current->fsuid;
901 oa->o_gid = (dir->i_mode & S_ISGID) ?
902 dir->i_gid : current->fsgid;
903 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
905 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
908 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
910 * here inode number and generation are
911 * important, as this is replay request and we
912 * need them to check if such an object is
915 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
916 obd->obd_name, rec->ur_namelen - 1,
917 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
918 (unsigned)id_gen(rec->ur_id2));
919 oa->o_id = id_ino(rec->ur_id2);
920 oa->o_fid = id_fid(rec->ur_id2);
921 oa->o_generation = id_gen(rec->ur_id2);
922 oa->o_flags |= OBD_FL_RECREATE_OBJS;
923 LASSERT(oa->o_fid != 0);
926 /* obtain default ACL */
927 acl_size = mds_get_default_acl(dir, &acl);
930 GOTO(cleanup, rc = -ENOMEM);
934 * before obd_create() is called, o_fid is not known if
935 * this is not recovery of cause.
937 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
941 OBD_FREE(acl, acl_size);
944 CERROR("can't create remote inode: %d\n", rc);
945 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
946 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
947 rec->ur_name, rec->ur_mode);
952 LASSERT(oa->o_fid != 0);
954 /* now, add new dir entry for it */
955 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
956 if (IS_ERR(handle)) {
958 GOTO(cleanup, rc = PTR_ERR(handle));
961 /* creating local dentry for remote inode. */
962 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
963 rec->ur_namelen - 1, oa->o_id,
964 oa->o_generation, i, oa->o_fid);
967 CERROR("Can't create local entry %*s for "
968 "remote inode.\n", rec->ur_namelen - 1,
974 body = lustre_msg_buf(req->rq_repmsg,
976 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
978 obdo2id(&body->id1, oa);
981 /* requested name exists in the directory */
988 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
990 GOTO(cleanup, rc = PTR_ERR(handle));
991 if (rec->ur_tgt == NULL) /* no target supplied */
992 rc = -EINVAL; /* -EPROTO? */
994 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1002 int rdev = rec->ur_rdev;
1003 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1005 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1006 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1011 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1012 dchild->d_fsdata = NULL;
1013 GOTO(cleanup, rc = -EINVAL);
1016 /* In case we stored the desired inum in here, we want to clean up. */
1017 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1018 dchild->d_fsdata = NULL;
1021 CDEBUG(D_INODE, "error during create: %d\n", rc);
1023 } else if (dchild->d_inode) {
1024 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1026 struct mds_body *body;
1027 struct inode *inode = dchild->d_inode;
1030 iattr.ia_uid = rec->ur_fsuid;
1031 LTIME_S(iattr.ia_atime) = rec->ur_time;
1032 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1033 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1035 if (dir->i_mode & S_ISGID)
1036 iattr.ia_gid = dir->i_gid;
1038 iattr.ia_gid = rec->ur_fsgid;
1040 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1041 ATTR_MTIME | ATTR_CTIME;
1043 if (id_ino(rec->ur_id2)) {
1044 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1045 inode->i_generation = id_gen(rec->ur_id2);
1047 if (type != S_IFDIR) {
1048 down(&inode->i_sem);
1049 rc = mds_update_inode_sid(obd, inode,
1050 handle, rec->ur_id2);
1053 CERROR("Can't update inode self id, "
1058 * make sure, that fid is up-to-date.
1060 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1063 /* dirtied and committed by the upcoming setattr. */
1064 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1065 inode->i_ino, inode->i_generation);
1067 struct lustre_handle child_ino_lockh;
1069 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1070 inode->i_ino, inode->i_generation);
1072 if (type != S_IFDIR) {
1074 * allocate new id for @inode if it is not dir,
1075 * because for dir it was already done.
1077 down(&inode->i_sem);
1078 rc = mds_alloc_inode_sid(obd, inode,
1082 CERROR("mds_alloc_inode_sid() failed, "
1083 "inode %lu, rc %d\n", inode->i_ino,
1090 * the inode we were allocated may have just
1091 * been freed by an unlink operation. We take
1092 * this lock to synchronize against the matching
1093 * reply-ack-lock taken in unlink, to avoid
1094 * replay problems if this reply makes it out to
1095 * the client but the unlink's does not. See
1096 * bug 2029 for more detail.
1098 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1099 if (rc != ELDLM_OK) {
1100 CERROR("error locking for unlink/create sync: "
1103 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1108 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1110 CERROR("error on child setattr: rc = %d\n", rc);
1112 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1113 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1115 CERROR("error on parent setattr: rc = %d\n", rc);
1117 MD_COUNTER_INCREMENT(obd, create);
1119 /* take care of default stripe inheritance */
1120 if (type == S_IFDIR) {
1121 struct lov_mds_md lmm;
1122 int lmm_size = sizeof(lmm);
1124 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1126 down(&inode->i_sem);
1127 rc = fsfilt_set_md(obd, inode, handle,
1128 &lmm, lmm_size, EA_LOV);
1132 CERROR("error on copy stripe info: rc = %d\n",
1138 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1139 mds_pack_inode2body(obd, body, inode, 1);
1140 mds_body_do_reverse_map(med, body);
1145 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1147 if (rc && created) {
1148 /* Destroy the file we just created. This should not need extra
1149 * journal credits, as we have already modified all of the
1150 * blocks needed in order to create the file in the first
1154 err = vfs_rmdir(dir, dchild);
1156 CERROR("rmdir in error path: %d\n", err);
1159 err = vfs_unlink(dir, dchild);
1161 CERROR("unlink in error path: %d\n", err);
1167 switch (cleanup_phase) {
1168 case 2: /* child dentry */
1170 case 1: /* locked parent dentry */
1172 if (lockh[1].cookie != 0)
1173 ldlm_lock_decref(lockh + 1, parent_mode);
1176 ldlm_lock_decref(lockh, LCK_PW);
1178 ptlrpc_save_lock(req, lockh, LCK_PW);
1184 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1188 OBD_FREE(mea, mea_size);
1189 req->rq_status = rc;
1194 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1195 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1199 for (i = 0; i < RES_NAME_SIZE; i++) {
1201 * this is needed to make zeroed res_id entries to be put at the
1202 * end of list in *ordered_locks() .
1204 if (res1->name[i] == 0 && res2->name[i] != 0)
1206 if (res2->name[i] == 0 && res1->name[i] != 0)
1208 if (res1->name[i] > res2->name[i])
1210 if (res1->name[i] < res2->name[i])
1217 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1223 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1224 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1225 * because they take the place of local semaphores.
1227 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1228 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1229 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1230 struct lustre_handle *p1_lockh, int p1_lock_mode,
1231 ldlm_policy_data_t *p1_policy,
1232 struct ldlm_res_id *p2_res_id,
1233 struct lustre_handle *p2_lockh, int p2_lock_mode,
1234 ldlm_policy_data_t *p2_policy)
1236 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1237 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1238 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1239 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1243 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1245 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1246 res_id[0]->name[0], res_id[1]->name[0]);
1248 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1249 handles[1] = p1_lockh;
1250 handles[0] = p2_lockh;
1251 res_id[1] = p1_res_id;
1252 res_id[0] = p2_res_id;
1253 lock_modes[1] = p1_lock_mode;
1254 lock_modes[0] = p2_lock_mode;
1255 policies[1] = p1_policy;
1256 policies[0] = p2_policy;
1259 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1260 res_id[0]->name[0], res_id[1]->name[0]);
1262 flags = LDLM_FL_LOCAL_ONLY;
1263 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1264 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1265 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1266 NULL, 0, NULL, handles[0]);
1269 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1271 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1272 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1273 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1274 ldlm_lock_addref(handles[1], lock_modes[1]);
1275 } else if (res_id[1]->name[0] != 0) {
1276 flags = LDLM_FL_LOCAL_ONLY;
1277 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1278 *res_id[1], LDLM_IBITS, policies[1],
1279 lock_modes[1], &flags, mds_blocking_ast,
1280 ldlm_completion_ast, NULL, NULL, NULL, 0,
1282 if (rc != ELDLM_OK) {
1283 ldlm_lock_decref(handles[0], lock_modes[0]);
1286 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1292 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1293 struct lustre_handle *p1_lockh, int p1_lock_mode,
1294 ldlm_policy_data_t *p1_policy,
1295 struct ldlm_res_id *p2_res_id,
1296 struct lustre_handle *p2_lockh, int p2_lock_mode,
1297 ldlm_policy_data_t *p2_policy,
1298 struct ldlm_res_id *c1_res_id,
1299 struct lustre_handle *c1_lockh, int c1_lock_mode,
1300 ldlm_policy_data_t *c1_policy,
1301 struct ldlm_res_id *c2_res_id,
1302 struct lustre_handle *c2_lockh, int c2_lock_mode,
1303 ldlm_policy_data_t *c2_policy)
1305 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1306 c1_res_id, c2_res_id };
1307 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1308 c1_lockh, c2_lockh };
1309 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1310 c1_lock_mode, c2_lock_mode };
1311 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1312 c1_policy, c2_policy};
1313 int rc, i, j, sorted, flags;
1316 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1317 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1318 res_id[3]->name[0]);
1321 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1322 * res_id should be at the end of list after sorting is finished.
1324 for (i = 1; i < 4; i++) {
1326 dlm_handles[4] = dlm_handles[i];
1327 res_id[4] = res_id[i];
1328 lock_modes[4] = lock_modes[i];
1329 policies[4] = policies[i];
1333 if (res_gt(res_id[j], res_id[4], policies[j],
1335 dlm_handles[j + 1] = dlm_handles[j];
1336 res_id[j + 1] = res_id[j];
1337 lock_modes[j + 1] = lock_modes[j];
1338 policies[j + 1] = policies[j];
1343 } while (j >= 0 && !sorted);
1345 dlm_handles[j + 1] = dlm_handles[4];
1346 res_id[j + 1] = res_id[4];
1347 lock_modes[j + 1] = lock_modes[4];
1348 policies[j + 1] = policies[4];
1351 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1352 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1353 res_id[3]->name[0]);
1355 /* XXX we could send ASTs on all these locks first before blocking? */
1356 for (i = 0; i < 4; i++) {
1360 * nevertheless zeroed res_ids should be at the end of list, and
1361 * could use break here, I think, that it is more correctly for
1362 * clear understanding of code to have continue here, as it
1363 * clearly means, that zeroed res_id should be skipped and does
1364 * not mean, that if we meet zeroed res_id we should stop
1367 if (res_id[i]->name[0] == 0)
1371 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1372 (policies[i]->l_inodebits.bits &
1373 policies[i-1]->l_inodebits.bits) ) {
1374 memcpy(dlm_handles[i], dlm_handles[i-1],
1375 sizeof(*(dlm_handles[i])));
1376 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1378 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1379 *res_id[i], LDLM_IBITS,
1381 lock_modes[i], &flags,
1383 ldlm_completion_ast, NULL, NULL,
1384 NULL, 0, NULL, dlm_handles[i]);
1386 GOTO(out_err, rc = -EIO);
1387 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1394 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1399 /* In the unlikely case that the child changed while we were waiting
1400 * on the lock, we need to drop the lock on the old child and either:
1401 * - if the child has a lower resource name, then we have to also
1402 * drop the parent lock and regain the locks in the right order
1403 * - in the rename case, if the child has a lower resource name than one of
1404 * the other parent/child resources (maxres) we also need to reget the locks
1405 * - if the child has a higher resource name (this is the common case)
1406 * we can just get the lock on the new child (still in lock order)
1408 * Returns 0 if the child did not change or if it changed but could be locked.
1409 * Returns 1 if the child changed and we need to re-lock (no locks held).
1410 * Returns -ve error with a valid dchild (no locks held). */
1411 static int mds_verify_child(struct obd_device *obd,
1412 struct ldlm_res_id *parent_res_id,
1413 struct lustre_handle *parent_lockh,
1414 struct dentry *dparent, int parent_mode,
1415 struct ldlm_res_id *child_res_id,
1416 struct lustre_handle *child_lockh,
1417 struct dentry **dchildp, int child_mode,
1418 ldlm_policy_data_t *child_policy,
1419 const char *name, int namelen,
1420 struct ldlm_res_id *maxres,
1421 unsigned long child_ino, __u32 child_gen)
1423 struct lustre_id sid;
1424 struct dentry *vchild, *dchild = *dchildp;
1425 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1428 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1430 GOTO(cleanup, rc = PTR_ERR(vchild));
1432 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1433 if (child_gen == vchild->d_generation &&
1434 child_ino == vchild->d_inum) {
1443 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1444 (vchild->d_inode != NULL &&
1445 child_gen == vchild->d_inode->i_generation &&
1446 child_ino == vchild->d_inode->i_ino))) {
1454 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1455 vchild->d_inode, dchild ? dchild->d_inode : 0,
1456 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1457 child_res_id->name[0]);
1459 if (child_res_id->name[0] != 0)
1460 ldlm_lock_decref(child_lockh, child_mode);
1464 cleanup_phase = 1; /* parent lock only */
1465 *dchildp = dchild = vchild;
1467 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1470 if (dchild->d_inode) {
1471 down(&dchild->d_inode->i_sem);
1472 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1473 up(&dchild->d_inode->i_sem);
1475 CERROR("Can't read inode self id, inode %lu,"
1476 " rc %d\n", dchild->d_inode->i_ino, rc);
1479 child_res_id->name[0] = id_fid(&sid);
1480 child_res_id->name[1] = id_group(&sid);
1482 child_res_id->name[0] = dchild->d_fid;
1483 child_res_id->name[1] = dchild->d_mdsnum;
1486 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1487 res_gt(maxres, child_res_id, NULL, NULL)) {
1488 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1489 child_res_id->name[0], parent_res_id->name[0],
1491 GOTO(cleanup, rc = 1);
1494 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1495 *child_res_id, LDLM_IBITS, child_policy,
1496 child_mode, &flags, mds_blocking_ast,
1497 ldlm_completion_ast, NULL, NULL, NULL, 0,
1500 GOTO(cleanup, rc = -EIO);
1503 memset(child_res_id, 0, sizeof(*child_res_id));
1509 switch(cleanup_phase) {
1511 if (child_res_id->name[0] != 0)
1512 ldlm_lock_decref(child_lockh, child_mode);
1514 ldlm_lock_decref(parent_lockh, parent_mode);
1520 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1521 struct lustre_id *id,
1522 struct lustre_handle *parent_lockh,
1523 struct dentry **dparentp, int parent_mode,
1524 __u64 parent_lockpart, int *update_mode,
1525 char *name, int namelen,
1526 struct lustre_handle *child_lockh,
1527 struct dentry **dchildp, int child_mode,
1528 __u64 child_lockpart)
1530 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1531 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1532 struct ldlm_res_id parent_res_id = { .name = {0} };
1533 struct ldlm_res_id child_res_id = { .name = {0} };
1534 unsigned long child_ino = 0; __u32 child_gen = 0;
1535 int rc = 0, cleanup_phase = 0;
1536 struct lustre_id sid;
1537 struct inode *inode;
1540 /* Step 1: Lookup parent */
1541 *dparentp = mds_id2dentry(obd, id, NULL);
1542 if (IS_ERR(*dparentp)) {
1543 rc = PTR_ERR(*dparentp);
1548 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1549 (*dparentp)->d_inode->i_ino, name);
1551 parent_res_id.name[0] = id_fid(id);
1552 parent_res_id.name[1] = id_group(id);
1555 parent_lockh[1].cookie = 0;
1556 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1557 struct ldlm_res_id res_id = { .name = {0} };
1558 ldlm_policy_data_t policy;
1561 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1563 res_id.name[0] = id_fid(id);
1564 res_id.name[1] = id_group(id);
1565 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1567 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1568 res_id, LDLM_IBITS, &policy,
1569 *update_mode, &flags,
1571 ldlm_completion_ast,
1572 NULL, NULL, NULL, 0, NULL,
1578 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1581 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1582 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1583 parent_res_id.name[2]);
1587 cleanup_phase = 1; /* parent dentry */
1589 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1590 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1591 if (IS_ERR(*dchildp)) {
1592 rc = PTR_ERR(*dchildp);
1593 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1597 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1599 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1600 * lock. Drop possible UPDATE lock!
1602 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1603 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1605 child_res_id.name[0] = (*dchildp)->d_fid;
1606 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1607 child_ino = (*dchildp)->d_inum;
1608 child_gen = (*dchildp)->d_generation;
1612 inode = (*dchildp)->d_inode;
1614 inode = igrab(inode);
1618 down(&inode->i_sem);
1619 rc = mds_read_inode_sid(obd, inode, &sid);
1622 CERROR("Can't read inode self id, inode %lu, "
1623 "rc %d\n", inode->i_ino, rc);
1628 child_ino = inode->i_ino;
1629 child_gen = inode->i_generation;
1630 child_res_id.name[0] = id_fid(&sid);
1631 child_res_id.name[1] = id_group(&sid);
1635 cleanup_phase = 2; /* child dentry */
1637 /* Step 3: Lock parent and child in resource order. If child doesn't
1638 * exist, we still have to lock the parent and re-lookup. */
1639 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1640 &parent_policy, &child_res_id, child_lockh,
1641 child_mode, &child_policy);
1645 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1646 cleanup_phase = 4; /* child lock */
1648 cleanup_phase = 3; /* parent lock */
1650 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1651 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1652 parent_mode, &child_res_id, child_lockh,
1653 dchildp, child_mode, &child_policy,
1654 name, namelen, &parent_res_id, child_ino,
1666 switch (cleanup_phase) {
1668 ldlm_lock_decref(child_lockh, child_mode);
1670 ldlm_lock_decref(parent_lockh, parent_mode);
1675 if (parent_lockh[1].cookie)
1676 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1684 void mds_reconstruct_generic(struct ptlrpc_request *req)
1686 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1687 mds_req_from_mcd(req, med->med_mcd);
1690 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1691 * instead link the inode into the PENDING directory until it is finally
1692 * released. We can't simply call mds_reint_rename() or some part thereof,
1693 * because we don't have the inode to check for link count/open status until
1694 * after it is locked.
1696 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1697 * before starting journal transaction.
1699 * returns 1 on success
1700 * returns 0 if we lost a race and didn't make a new link
1701 * returns negative on error
1703 static int mds_orphan_add_link(struct mds_update_record *rec,
1704 struct obd_device *obd, struct dentry *dentry)
1706 struct mds_obd *mds = &obd->u.mds;
1707 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1708 struct inode *inode = dentry->d_inode;
1709 struct dentry *pending_child;
1710 char idname[LL_ID_NAMELEN];
1711 int idlen = 0, rc, mode;
1714 LASSERT(inode != NULL);
1715 LASSERT(!mds_inode_is_orphan(inode));
1716 #ifndef HAVE_I_ALLOC_SEM
1717 LASSERT(down_trylock(&inode->i_sem) != 0);
1719 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1721 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1723 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1724 mds_orphan_open_count(inode), inode->i_nlink,
1725 S_ISDIR(inode->i_mode) ? "dir" :
1726 S_ISREG(inode->i_mode) ? "file" : "other",
1727 rec->ur_name, idname);
1729 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1732 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1733 if (IS_ERR(pending_child))
1734 RETURN(PTR_ERR(pending_child));
1736 if (pending_child->d_inode != NULL) {
1737 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1738 LASSERT(pending_child->d_inode == inode);
1739 GOTO(out_dput, rc = 0);
1743 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1744 * linking and return real mode back then -bzzz
1746 mode = inode->i_mode;
1747 inode->i_mode = S_IFREG;
1748 rc = vfs_link(dentry, pending_dir, pending_child);
1750 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1753 mds_inode_set_orphan(inode);
1755 /* return mode and correct i_nlink if inode is directory */
1756 inode->i_mode = mode;
1757 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1758 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1760 if (S_ISDIR(mode)) {
1762 pending_dir->i_nlink++;
1763 mark_inode_dirty(inode);
1764 mark_inode_dirty(pending_dir);
1767 GOTO(out_dput, rc = 1);
1769 l_dput(pending_child);
1773 int mds_create_local_dentry(struct mds_update_record *rec,
1774 struct obd_device *obd)
1776 struct mds_obd *mds = &obd->u.mds;
1777 struct inode *id_dir = mds->mds_id_dir->d_inode;
1778 int idlen = 0, rc, cleanup_phase = 0;
1779 struct dentry *new_child = NULL;
1780 char *idname = rec->ur_name;
1781 struct dentry *child = NULL;
1782 struct lustre_handle lockh[2] = {{0}, {0}};
1783 struct lustre_id sid;
1787 down(&id_dir->i_sem);
1788 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1789 id_gen(rec->ur_id1));
1791 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1792 idname, OLID4(rec->ur_id1));
1794 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1797 if (IS_ERR(new_child)) {
1798 CERROR("can't lookup %s: %d\n", idname,
1799 (int) PTR_ERR(new_child));
1800 GOTO(cleanup, rc = PTR_ERR(new_child));
1804 down(&id_dir->i_sem);
1805 rc = mds_read_inode_sid(obd, id_dir, &sid);
1808 CERROR("Can't read inode self id, inode %lu, "
1809 "rc %d\n", id_dir->i_ino, rc);
1813 if (new_child->d_inode != NULL) {
1814 /* nice. we've already have local dentry! */
1815 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1816 (unsigned)new_child->d_inode->i_ino,
1817 (unsigned)new_child->d_inode->i_generation);
1819 id_ino(rec->ur_id1) = id_dir->i_ino;
1820 id_gen(rec->ur_id1) = id_dir->i_generation;
1821 rec->ur_namelen = idlen + 1;
1823 id_fid(rec->ur_id1) = id_fid(&sid);
1824 id_group(rec->ur_id1) = id_group(&sid);
1826 GOTO(cleanup, rc = 0);
1829 /* new, local dentry will be added soon. we need no aliases here */
1832 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1833 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1835 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1836 LCK_EX, lockh, NULL, NULL, 0,
1837 MDS_INODELOCK_UPDATE);
1840 if (IS_ERR(child)) {
1841 rc = PTR_ERR(child);
1842 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1843 CERROR("can't get victim: %d\n", rc);
1848 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1850 GOTO(cleanup, rc = PTR_ERR(handle));
1852 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1853 idlen, id_ino(rec->ur_id1),
1854 id_gen(rec->ur_id1), mds->mds_num,
1855 id_fid(rec->ur_id1));
1857 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1858 (unsigned long)child->d_inode->i_ino,
1859 (unsigned long)child->d_inode->i_generation, rc);
1861 if (S_ISDIR(child->d_inode->i_mode)) {
1863 mark_inode_dirty(id_dir);
1865 mark_inode_dirty(child->d_inode);
1867 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1869 id_ino(rec->ur_id1) = id_dir->i_ino;
1870 id_gen(rec->ur_id1) = id_dir->i_generation;
1871 rec->ur_namelen = idlen + 1;
1873 id_fid(rec->ur_id1) = id_fid(&sid);
1874 id_group(rec->ur_id1) = id_group(&sid);
1878 switch(cleanup_phase) {
1880 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1881 ldlm_lock_decref(lockh, LCK_EX);
1891 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1892 struct ptlrpc_request *slave)
1894 void *cookie, *cookie2;
1895 struct mds_body *body2;
1896 struct mds_body *body;
1900 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1901 LASSERT(body != NULL);
1903 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1904 LASSERT(body2 != NULL);
1906 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1909 memcpy(body2, body, sizeof(*body));
1910 body2->valid &= ~OBD_MD_FLCOOKIE;
1912 if (!(body->valid & OBD_MD_FLEASIZE) &&
1913 !(body->valid & OBD_MD_FLDIREA))
1916 if (body->eadatasize == 0) {
1917 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1921 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1923 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1924 LASSERT(ea != NULL);
1926 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1927 LASSERT(ea2 != NULL);
1929 memcpy(ea2, ea, body->eadatasize);
1931 if (body->valid & OBD_MD_FLCOOKIE) {
1932 LASSERT(master->rq_repmsg->buflens[2] >=
1933 slave->rq_repmsg->buflens[2]);
1934 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1935 slave->rq_repmsg->buflens[2]);
1936 LASSERT(cookie != NULL);
1938 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1939 master->rq_repmsg->buflens[2]);
1940 LASSERT(cookie2 != NULL);
1941 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1942 body2->valid |= OBD_MD_FLCOOKIE;
1947 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1948 int offset, struct ptlrpc_request *req,
1949 struct lustre_handle *parent_lockh,
1950 int update_mode, struct dentry *dparent,
1951 struct lustre_handle *child_lockh,
1952 struct dentry *dchild)
1954 struct obd_device *obd = req->rq_export->exp_obd;
1955 struct mds_obd *mds = mds_req2mds(req);
1956 struct ptlrpc_request *request = NULL;
1957 int rc = 0, cleanup_phase = 0;
1958 struct mdc_op_data *op_data;
1962 LASSERT(offset == 1 || offset == 3);
1964 /* time to drop i_nlink on remote MDS */
1965 OBD_ALLOC(op_data, sizeof(*op_data));
1966 if (op_data == NULL)
1969 memset(op_data, 0, sizeof(*op_data));
1970 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1971 op_data->create_mode = rec->ur_mode;
1973 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1974 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1976 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1977 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1978 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1981 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1982 op_data->create_mode |= MDS_MODE_REPLAY;
1984 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1985 OBD_FREE(op_data, sizeof(*op_data));
1990 mds_copy_unlink_reply(req, request);
1991 ptlrpc_req_finished(request);
1995 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1998 GOTO(cleanup, rc = PTR_ERR(handle));
1999 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2000 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2005 req->rq_status = rc;
2008 if (parent_lockh[1].cookie != 0)
2009 ldlm_lock_decref(parent_lockh + 1, update_mode);
2011 ldlm_lock_decref(child_lockh, LCK_EX);
2013 ldlm_lock_decref(parent_lockh, LCK_PW);
2015 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2022 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2023 struct ptlrpc_request *req,
2024 struct lustre_handle *lh)
2026 struct dentry *dparent = NULL, *dchild;
2027 struct mds_obd *mds = mds_req2mds(req);
2028 struct obd_device *obd = req->rq_export->exp_obd;
2029 struct mds_body *body = NULL;
2030 struct inode *child_inode = NULL;
2031 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2032 struct lustre_handle child_lockh = {0};
2033 struct lustre_handle child_reuse_lockh = {0};
2034 struct lustre_handle *slave_lockh = NULL;
2035 char idname[LL_ID_NAMELEN];
2036 struct llog_create_locks *lcl = NULL;
2037 void *handle = NULL;
2038 int rc = 0, cleanup_phase = 0;
2039 int unlink_by_id = 0;
2043 LASSERT(offset == 1 || offset == 3);
2045 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2046 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2049 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2051 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2052 DEBUG_REQ(D_HA, req, "unlink replay");
2053 LASSERT(offset == 1); /* should not come from intent */
2054 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2055 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2056 req->rq_repmsg->buflens[2]);
2059 MD_COUNTER_INCREMENT(obd, unlink);
2061 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2062 GOTO(cleanup, rc = -ENOENT);
2064 if (rec->ur_namelen == 1) {
2065 /* this is request to drop i_nlink on local inode */
2067 rec->ur_name = idname;
2068 rc = mds_create_local_dentry(rec, obd);
2069 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2070 DEBUG_REQ(D_HA, req,
2071 "drop nlink on inode "DLID4" (replay)",
2072 OLID4(rec->ur_id1));
2078 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2079 /* master mds for directory asks slave removing inode is already
2081 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2082 LCK_PW, parent_lockh,
2083 &update_mode, rec->ur_name,
2085 MDS_INODELOCK_UPDATE);
2086 if (IS_ERR(dparent))
2087 GOTO(cleanup, rc = PTR_ERR(dparent));
2088 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2089 rec->ur_namelen - 1);
2091 GOTO(cleanup, rc = PTR_ERR(dchild));
2092 child_lockh.cookie = 0;
2093 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2094 LASSERT(dchild->d_inode != NULL);
2095 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2097 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2098 parent_lockh, &dparent,
2099 LCK_PW, MDS_INODELOCK_UPDATE,
2100 &update_mode, rec->ur_name,
2101 rec->ur_namelen, &child_lockh,
2103 MDS_INODELOCK_LOOKUP |
2104 MDS_INODELOCK_UPDATE);
2109 if (dchild->d_flags & DCACHE_CROSS_REF) {
2110 /* we should have parent lock only here */
2111 LASSERT(unlink_by_id == 0);
2112 LASSERT(dchild->d_mdsnum != mds->mds_num);
2113 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2114 update_mode, dparent, &child_lockh, dchild);
2118 cleanup_phase = 1; /* dchild, dparent, locks */
2121 child_inode = dchild->d_inode;
2122 if (child_inode == NULL) {
2123 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2124 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2125 GOTO(cleanup, rc = -ENOENT);
2128 cleanup_phase = 2; /* dchild has a lock */
2130 /* We have to do these checks ourselves, in case we are making an
2131 * orphan. The client tells us whether rmdir() or unlink() was called,
2132 * so we need to return appropriate errors (bug 72).
2134 * We don't have to check permissions, because vfs_rename (called from
2135 * mds_open_unlink_rename) also calls may_delete. */
2136 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2137 if (!S_ISDIR(child_inode->i_mode))
2138 GOTO(cleanup, rc = -ENOTDIR);
2140 if (S_ISDIR(child_inode->i_mode))
2141 GOTO(cleanup, rc = -EISDIR);
2144 /* handle splitted dir */
2145 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2149 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2150 * reuse (see bug 2029). */
2151 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2154 cleanup_phase = 3; /* child inum lock */
2156 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2158 /* ldlm_reply in buf[0] if called via intent */
2164 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2165 LASSERT(body != NULL);
2167 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2168 DOWN_READ_I_ALLOC_SEM(child_inode);
2169 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2171 /* If this is potentially the last reference to this inode, get the
2172 * OBD EA data first so the client can destroy OST objects. We
2173 * only do the object removal later if no open files/links remain. */
2174 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2175 child_inode->i_nlink == 1) {
2176 if (mds_orphan_open_count(child_inode) > 0) {
2177 /* need to lock pending_dir before transaction */
2178 down(&mds->mds_pending_dir->d_inode->i_sem);
2179 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2180 } else if (S_ISREG(child_inode->i_mode)) {
2181 mds_pack_inode2body(obd, body, child_inode, 0);
2182 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2183 body, child_inode, MDS_PACK_MD_LOCK, 0);
2187 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2188 switch (child_inode->i_mode & S_IFMT) {
2190 /* Drop any lingering child directories before we start our
2191 * transaction, to avoid doing multiple inode dirty/delete
2192 * in our compound transaction (bug 1321). */
2193 shrink_dcache_parent(dchild);
2194 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2197 GOTO(cleanup, rc = PTR_ERR(handle));
2198 rc = vfs_rmdir(dparent->d_inode, dchild);
2201 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2202 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2204 handle = fsfilt_start_log(obd, dparent->d_inode,
2205 FSFILT_OP_UNLINK, NULL,
2206 le32_to_cpu(lmm->lmm_stripe_count));
2208 GOTO(cleanup, rc = PTR_ERR(handle));
2209 rc = vfs_unlink(dparent->d_inode, dchild);
2217 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2220 GOTO(cleanup, rc = PTR_ERR(handle));
2221 rc = vfs_unlink(dparent->d_inode, dchild);
2224 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2227 GOTO(cleanup, rc = -EINVAL);
2230 if (rc == 0 && child_inode->i_nlink == 0) {
2231 if (mds_orphan_open_count(child_inode) > 0)
2232 rc = mds_orphan_add_link(rec, obd, dchild);
2235 GOTO(cleanup, rc = 0);
2237 if (!S_ISREG(child_inode->i_mode))
2240 if (!(body->valid & OBD_MD_FLEASIZE)) {
2241 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2242 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2243 } else if (mds_log_op_unlink(obd, child_inode,
2244 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2245 req->rq_repmsg->buflens[offset + 1],
2246 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2247 req->rq_repmsg->buflens[offset + 2],
2249 body->valid |= OBD_MD_FLCOOKIE;
2252 rc = mds_destroy_objects(obd, child_inode, 1);
2254 CERROR("can't remove OST object, err %d\n",
2266 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2267 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2268 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2270 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2272 CERROR("error on parent setattr: rc = %d\n", err);
2274 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2275 handle, req, rc, 0);
2277 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2278 "unlinked", 0, NULL);
2279 switch(cleanup_phase) {
2280 case 5: /* pending_dir semaphore */
2281 up(&mds->mds_pending_dir->d_inode->i_sem);
2282 case 4: /* child inode semaphore */
2283 UP_READ_I_ALLOC_SEM(child_inode);
2284 /* handle splitted dir */
2286 /* master directory can be non-empty or something else ... */
2287 mds_unlink_slave_objs(obd, dchild);
2290 ptlrpc_save_llog_lock(req, lcl);
2291 case 3: /* child ino-reuse lock */
2292 if (rc && body != NULL) {
2293 // Don't unlink the OST objects if the MDS unlink failed
2297 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2299 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2300 case 2: /* child lock */
2301 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2302 if (child_lockh.cookie)
2303 ldlm_lock_decref(&child_lockh, LCK_EX);
2304 case 1: /* child and parent dentry, parent lock */
2306 if (parent_lockh[1].cookie != 0)
2307 ldlm_lock_decref(parent_lockh + 1, update_mode);
2310 ldlm_lock_decref(parent_lockh, LCK_PW);
2312 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2319 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2322 req->rq_status = rc;
2327 * to service requests from remote MDS to increment i_nlink
2329 static int mds_reint_link_acquire(struct mds_update_record *rec,
2330 int offset, struct ptlrpc_request *req,
2331 struct lustre_handle *lh)
2333 struct obd_device *obd = req->rq_export->exp_obd;
2334 struct ldlm_res_id src_res_id = { .name = {0} };
2335 struct lustre_handle *handle = NULL, src_lockh = {0};
2336 struct mds_obd *mds = mds_req2mds(req);
2337 int rc = 0, cleanup_phase = 0;
2338 struct dentry *de_src = NULL;
2339 ldlm_policy_data_t policy;
2343 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2344 obd->obd_name, OLID4(rec->ur_id1));
2346 /* Step 1: Lookup the source inode and target directory by ID */
2347 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2349 GOTO(cleanup, rc = PTR_ERR(de_src));
2350 cleanup_phase = 1; /* source dentry */
2352 src_res_id.name[0] = id_fid(rec->ur_id1);
2353 src_res_id.name[1] = id_group(rec->ur_id1);
2354 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2356 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2357 src_res_id, LDLM_IBITS, &policy,
2358 LCK_EX, &flags, mds_blocking_ast,
2359 ldlm_completion_ast, NULL, NULL,
2360 NULL, 0, NULL, &src_lockh);
2362 GOTO(cleanup, rc = -ENOLCK);
2363 cleanup_phase = 2; /* lock */
2365 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2367 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2368 if (IS_ERR(handle)) {
2369 rc = PTR_ERR(handle);
2372 de_src->d_inode->i_nlink++;
2373 mark_inode_dirty(de_src->d_inode);
2377 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2378 handle, req, rc, 0);
2379 switch (cleanup_phase) {
2382 ldlm_lock_decref(&src_lockh, LCK_EX);
2384 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2390 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2393 req->rq_status = rc;
2398 * request to link to foreign inode:
2399 * - acquire i_nlinks on this inode
2402 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2403 int offset, struct ptlrpc_request *req,
2404 struct lustre_handle *lh)
2406 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2407 struct obd_device *obd = req->rq_export->exp_obd;
2408 struct dentry *de_tgt_dir = NULL;
2409 struct mds_obd *mds = mds_req2mds(req);
2410 int rc = 0, cleanup_phase = 0;
2411 struct mdc_op_data *op_data;
2412 struct ptlrpc_request *request = NULL;
2416 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2417 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2418 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2419 OLID4(rec->ur_id1));
2421 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2422 tgt_dir_lockh, &update_mode,
2423 rec->ur_name, rec->ur_namelen - 1,
2424 MDS_INODELOCK_UPDATE);
2425 if (IS_ERR(de_tgt_dir))
2426 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2429 OBD_ALLOC(op_data, sizeof(*op_data));
2430 if (op_data == NULL)
2431 GOTO(cleanup, rc = -ENOMEM);
2433 memset(op_data, 0, sizeof(*op_data));
2434 op_data->id1 = *(rec->ur_id1);
2435 rc = md_link(mds->mds_md_exp, op_data, &request);
2436 OBD_FREE(op_data, sizeof(*op_data));
2439 ptlrpc_req_finished(request);
2445 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2447 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2448 if (IS_ERR(handle)) {
2449 rc = PTR_ERR(handle);
2455 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2456 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2457 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2458 id_fid(rec->ur_id1));
2461 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2462 handle, req, rc, 0);
2464 switch (cleanup_phase) {
2467 OBD_ALLOC(op_data, sizeof(*op_data));
2468 if (op_data != NULL) {
2470 memset(op_data, 0, sizeof(*op_data));
2472 op_data->id1 = *(rec->ur_id1);
2473 op_data->create_mode = rec->ur_mode;
2475 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2476 OBD_FREE(op_data, sizeof(*op_data));
2478 ptlrpc_req_finished(request);
2480 CERROR("error %d while dropping i_nlink on "
2481 "remote inode\n", rc);
2484 CERROR("rc %d prevented dropping i_nlink on "
2485 "remote inode\n", -ENOMEM);
2491 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2493 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2496 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2498 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2504 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2507 req->rq_status = rc;
2511 static int mds_reint_link(struct mds_update_record *rec, int offset,
2512 struct ptlrpc_request *req,
2513 struct lustre_handle *lh)
2515 struct obd_device *obd = req->rq_export->exp_obd;
2516 struct dentry *de_src = NULL;
2517 struct dentry *de_tgt_dir = NULL;
2518 struct dentry *dchild = NULL;
2519 struct mds_obd *mds = mds_req2mds(req);
2520 struct lustre_handle *handle = NULL;
2521 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2522 struct ldlm_res_id src_res_id = { .name = {0} };
2523 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2524 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2525 ldlm_policy_data_t tgt_dir_policy =
2526 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2527 int rc = 0, cleanup_phase = 0;
2529 int update_mode = 0;
2533 LASSERT(offset == 1);
2535 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2536 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2537 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2540 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2541 MD_COUNTER_INCREMENT(obd, link);
2543 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2544 GOTO(cleanup, rc = -ENOENT);
2546 if (id_group(rec->ur_id1) != mds->mds_num) {
2547 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2551 if (rec->ur_namelen == 1) {
2552 rc = mds_reint_link_acquire(rec, offset, req, lh);
2556 /* Step 1: Lookup the source inode and target directory by ID */
2557 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2559 GOTO(cleanup, rc = PTR_ERR(de_src));
2561 cleanup_phase = 1; /* source dentry */
2563 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2564 if (IS_ERR(de_tgt_dir)) {
2565 rc = PTR_ERR(de_tgt_dir);
2570 cleanup_phase = 2; /* target directory dentry */
2572 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2573 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2574 rec->ur_name, de_src->d_inode->i_ino);
2576 /* Step 2: Take the two locks */
2577 src_res_id.name[0] = id_fid(rec->ur_id1);
2578 src_res_id.name[1] = id_group(rec->ur_id1);
2579 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2580 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2583 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2585 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2587 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2588 tgt_dir_res_id, LDLM_IBITS,
2589 &src_policy, update_mode, &flags,
2591 ldlm_completion_ast, NULL, NULL,
2592 NULL, 0, NULL, tgt_dir_lockh + 1);
2594 GOTO(cleanup, rc = -ENOLCK);
2597 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2598 rec->ur_namelen - 1);
2599 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2600 (unsigned long)id_fid(rec->ur_id2),
2601 (unsigned long)id_group(rec->ur_id2),
2602 tgt_dir_res_id.name[2]);
2605 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2606 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2607 LCK_EX, &tgt_dir_policy);
2611 cleanup_phase = 3; /* locks */
2613 /* Step 3: Lookup the child */
2614 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2615 rec->ur_namelen - 1);
2616 if (IS_ERR(dchild)) {
2617 rc = PTR_ERR(dchild);
2618 if (rc != -EPERM && rc != -EACCES)
2619 CERROR("child lookup error %d\n", rc);
2623 cleanup_phase = 4; /* child dentry */
2625 if (dchild->d_inode) {
2626 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2627 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2632 /* Step 4: Do it. */
2633 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2635 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2636 if (IS_ERR(handle)) {
2637 rc = PTR_ERR(handle);
2641 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2642 if (rc && rc != -EPERM && rc != -EACCES)
2643 CERROR("vfs_link error %d\n", rc);
2645 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2646 handle, req, rc, 0);
2649 switch (cleanup_phase) {
2650 case 4: /* child dentry */
2654 ldlm_lock_decref(&src_lockh, LCK_EX);
2655 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2657 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2658 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2660 case 2: /* target dentry */
2662 if (tgt_dir_lockh[1].cookie && update_mode)
2663 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2667 case 1: /* source dentry */
2672 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2675 req->rq_status = rc;
2679 /* The idea here is that we need to get four locks in the end:
2680 * one on each parent directory, one on each child. We need to take
2681 * these locks in some kind of order (to avoid deadlocks), and the order
2682 * I selected is "increasing resource number" order. We need to look up
2683 * the children, however, before we know what the resource number(s) are.
2684 * Thus the following plan:
2686 * 1,2. Look up the parents
2687 * 3,4. Look up the children
2688 * 5. Take locks on the parents and children, in order
2689 * 6. Verify that the children haven't changed since they were looked up
2691 * If there was a race and the children changed since they were first looked
2692 * up, it is possible that mds_verify_child() will be able to just grab the
2693 * lock on the new child resource (if it has a higher resource than any other)
2694 * but we need to compare against not only its parent, but also against the
2695 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2697 * We need the fancy igrab() on the child inodes because we aren't holding a
2698 * lock on the parent after the lookup is done, so dentry->d_inode may change
2699 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2701 static int mds_get_parents_children_locked(struct obd_device *obd,
2702 struct mds_obd *mds,
2703 struct lustre_id *p1_id,
2704 struct dentry **de_srcdirp,
2705 struct lustre_id *p2_id,
2706 struct dentry **de_tgtdirp,
2708 const char *old_name, int old_len,
2709 struct dentry **de_oldp,
2710 const char *new_name, int new_len,
2711 struct dentry **de_newp,
2712 struct lustre_handle *dlm_handles,
2715 struct ldlm_res_id p1_res_id = { .name = {0} };
2716 struct ldlm_res_id p2_res_id = { .name = {0} };
2717 struct ldlm_res_id c1_res_id = { .name = {0} };
2718 struct ldlm_res_id c2_res_id = { .name = {0} };
2719 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2720 /* Only dentry should disappear, but the inode itself would be
2721 intact otherwise. */
2722 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2723 /* If something is going to be replaced, both dentry and inode locks are
2725 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2726 struct ldlm_res_id *maxres_src, *maxres_tgt;
2727 struct inode *inode;
2728 int rc = 0, cleanup_phase = 0;
2729 __u32 child_gen1 = 0;
2730 __u32 child_gen2 = 0;
2731 unsigned long child_ino1 = 0;
2732 unsigned long child_ino2 = 0;
2735 /* Step 1: Lookup the source directory */
2736 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2737 if (IS_ERR(*de_srcdirp))
2738 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2740 cleanup_phase = 1; /* source directory dentry */
2742 p1_res_id.name[0] = id_fid(p1_id);
2743 p1_res_id.name[1] = id_group(p1_id);
2745 /* Step 2: Lookup the target directory */
2746 if (id_equal_stc(p1_id, p2_id)) {
2747 *de_tgtdirp = dget(*de_srcdirp);
2749 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2750 if (IS_ERR(*de_tgtdirp)) {
2751 rc = PTR_ERR(*de_tgtdirp);
2757 cleanup_phase = 2; /* target directory dentry */
2759 p2_res_id.name[0] = id_fid(p2_id);
2760 p2_res_id.name[1] = id_group(p2_id);
2763 dlm_handles[5].cookie = 0;
2764 dlm_handles[6].cookie = 0;
2766 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2768 * get a temp lock on just fid, group to flush client cache and
2769 * to protect dirs from concurrent splitting.
2771 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2772 LCK_PW, &p_policy, &p2_res_id,
2773 &dlm_handles[6], LCK_PW, &p_policy);
2777 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2779 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2782 CDEBUG(D_INFO, "take locks on "
2783 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2784 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2785 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2790 /* Step 3: Lookup the source child entry */
2791 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2793 if (IS_ERR(*de_oldp)) {
2794 rc = PTR_ERR(*de_oldp);
2795 CERROR("old child lookup error (%.*s): %d\n",
2796 old_len - 1, old_name, rc);
2800 cleanup_phase = 4; /* original name dentry */
2802 inode = (*de_oldp)->d_inode;
2803 if (inode != NULL) {
2804 struct lustre_id sid;
2806 inode = igrab(inode);
2808 GOTO(cleanup, rc = -ENOENT);
2810 down(&inode->i_sem);
2811 rc = mds_read_inode_sid(obd, inode, &sid);
2814 CERROR("Can't read inode self id, inode %lu, "
2815 "rc %d\n", inode->i_ino, rc);
2820 child_ino1 = inode->i_ino;
2821 child_gen1 = inode->i_generation;
2822 c1_res_id.name[0] = id_fid(&sid);
2823 c1_res_id.name[1] = id_group(&sid);
2825 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2826 child_ino1 = (*de_oldp)->d_inum;
2827 child_gen1 = (*de_oldp)->d_generation;
2828 c1_res_id.name[0] = (*de_oldp)->d_fid;
2829 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2831 GOTO(cleanup, rc = -ENOENT);
2834 /* Step 4: Lookup the target child entry */
2835 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2837 if (IS_ERR(*de_newp)) {
2838 rc = PTR_ERR(*de_newp);
2839 CERROR("new child lookup error (%.*s): %d\n",
2840 old_len - 1, old_name, rc);
2844 cleanup_phase = 5; /* target dentry */
2846 inode = (*de_newp)->d_inode;
2847 if (inode != NULL) {
2848 struct lustre_id sid;
2850 inode = igrab(inode);
2854 down(&inode->i_sem);
2855 rc = mds_read_inode_sid(obd, inode, &sid);
2858 CERROR("Can't read inode self id, inode %lu, "
2859 "rc %d\n", inode->i_ino, rc);
2863 child_ino2 = inode->i_ino;
2864 child_gen2 = inode->i_generation;
2865 c2_res_id.name[0] = id_fid(&sid);
2866 c2_res_id.name[1] = id_group(&sid);
2868 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2869 child_ino2 = (*de_newp)->d_inum;
2870 child_gen2 = (*de_newp)->d_generation;
2871 c2_res_id.name[0] = (*de_newp)->d_fid;
2872 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2876 /* Step 5: Take locks on the parents and child(ren) */
2877 maxres_src = &p1_res_id;
2878 maxres_tgt = &p2_res_id;
2879 cleanup_phase = 5; /* target dentry */
2881 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2882 maxres_src = &c1_res_id;
2883 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2884 maxres_tgt = &c2_res_id;
2886 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2888 &p2_res_id, &dlm_handles[1], parent_mode,
2890 &c1_res_id, &dlm_handles[2], child_mode,
2892 &c2_res_id, &dlm_handles[3], child_mode,
2897 cleanup_phase = 6; /* parent and child(ren) locks */
2899 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2900 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2901 parent_mode, &c1_res_id, &dlm_handles[2],
2902 de_oldp, child_mode, &c1_policy, old_name, old_len,
2903 maxres_tgt, child_ino1, child_gen1);
2905 if (c2_res_id.name[0] != 0)
2906 ldlm_lock_decref(&dlm_handles[3], child_mode);
2907 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2914 if (!DENTRY_VALID(*de_oldp))
2915 GOTO(cleanup, rc = -ENOENT);
2917 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2918 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2919 parent_mode, &c2_res_id, &dlm_handles[3],
2920 de_newp, child_mode, &c2_policy, new_name,
2921 new_len, maxres_src, child_ino2, child_gen2);
2923 ldlm_lock_decref(&dlm_handles[2], child_mode);
2924 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2934 switch (cleanup_phase) {
2935 case 6: /* child lock(s) */
2936 if (c2_res_id.name[0] != 0)
2937 ldlm_lock_decref(&dlm_handles[3], child_mode);
2938 if (c1_res_id.name[0] != 0)
2939 ldlm_lock_decref(&dlm_handles[2], child_mode);
2940 if (dlm_handles[1].cookie != 0)
2941 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2942 if (dlm_handles[0].cookie != 0)
2943 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2944 case 5: /* target dentry */
2946 case 4: /* source dentry */
2950 if (dlm_handles[5].cookie != 0)
2951 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2952 if (dlm_handles[6].cookie != 0)
2953 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2955 case 2: /* target directory dentry */
2956 l_dput(*de_tgtdirp);
2957 case 1: /* source directry dentry */
2958 l_dput(*de_srcdirp);
2966 * checks if dentry can be removed. This function also handles cross-ref
2969 static int mds_check_for_rename(struct obd_device *obd,
2970 struct dentry *dentry)
2972 struct mds_obd *mds = &obd->u.mds;
2973 struct lustre_handle *rlockh;
2974 struct ptlrpc_request *req;
2975 struct mdc_op_data *op_data;
2976 struct lookup_intent it;
2977 int handle_size, rc = 0;
2980 LASSERT(dentry != NULL);
2982 if (dentry->d_inode) {
2983 if (S_ISDIR(dentry->d_inode->i_mode) &&
2984 !mds_is_dir_empty(obd, dentry))
2987 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2988 handle_size = sizeof(struct lustre_handle);
2990 OBD_ALLOC(rlockh, handle_size);
2994 memset(rlockh, 0, handle_size);
2995 OBD_ALLOC(op_data, sizeof(*op_data));
2996 if (op_data == NULL) {
2997 OBD_FREE(rlockh, handle_size);
3000 memset(op_data, 0, sizeof(*op_data));
3001 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3003 it.it_op = IT_UNLINK;
3004 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3007 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3008 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3009 mds_blocking_ast, NULL);
3010 OBD_FREE(op_data, sizeof(*op_data));
3014 OBD_FREE(it.d.fs_data,
3015 sizeof(struct lustre_intent_data));
3018 if (rlockh->cookie != 0)
3019 ldlm_lock_decref(rlockh, LCK_EX);
3021 if (LUSTRE_IT(&it)->it_data) {
3022 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3023 ptlrpc_req_finished(req);
3026 if (LUSTRE_IT(&it)->it_status)
3027 rc = LUSTRE_IT(&it)->it_status;
3028 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3029 OBD_FREE(rlockh, handle_size);
3034 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3035 struct ptlrpc_request *req, struct lustre_id *id,
3036 struct dentry *de_dir, struct dentry *de)
3038 struct obd_device *obd = req->rq_export->exp_obd;
3039 struct mds_obd *mds = mds_req2mds(req);
3040 void *handle = NULL;
3046 * name exists and points to local inode try to unlink this name
3047 * and create new one.
3049 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3050 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3051 (unsigned long)de->d_inode->i_generation);
3053 /* checking if we can remove local dentry. */
3054 rc = mds_check_for_rename(obd, de);
3058 handle = fsfilt_start(obd, de_dir->d_inode,
3059 FSFILT_OP_RENAME, NULL);
3061 GOTO(cleanup, rc = PTR_ERR(handle));
3062 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3065 } else if (de->d_flags & DCACHE_CROSS_REF) {
3066 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3067 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3068 (unsigned long)de->d_fid);
3070 /* checking if we can remove local dentry. */
3071 rc = mds_check_for_rename(obd, de);
3076 * to be fully POSIX compatible, we should add one more check:
3078 * if de_new is subdir of dir rec->ur_id1. If so - return
3081 * I do not know how to implement it right now, because
3082 * inodes/dentries for new and old names lie on different MDS,
3083 * so add this notice here just to make it visible for the rest
3084 * of developers and do not forget about. And when this check
3085 * will be added, del_cross_ref should gone, that is local
3086 * dentry is able to be removed if all checks passed. --umka
3089 handle = fsfilt_start(obd, de_dir->d_inode,
3090 FSFILT_OP_RENAME, NULL);
3092 GOTO(cleanup, rc = PTR_ERR(handle));
3093 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3097 /* name doesn't exist. the simplest case. */
3098 handle = fsfilt_start(obd, de_dir->d_inode,
3099 FSFILT_OP_LINK, NULL);
3101 GOTO(cleanup, rc = PTR_ERR(handle));
3104 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3105 rec->ur_tgtlen - 1, id_ino(id),
3106 id_gen(id), id_group(id), id_fid(id));
3108 CERROR("add_dir_entry() returned error %d\n", rc);
3114 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3115 handle, req, rc, 0);
3120 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3121 struct ptlrpc_request *req, struct dentry *de_dir,
3124 struct obd_device *obd = req->rq_export->exp_obd;
3125 struct mds_obd *mds = mds_req2mds(req);
3126 void *handle = NULL;
3130 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3132 GOTO(cleanup, rc = PTR_ERR(handle));
3133 rc = fsfilt_del_dir_entry(obd, de);
3138 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3139 handle, req, rc, 0);
3143 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3144 int offset, struct ptlrpc_request *req)
3146 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3147 struct obd_device *obd = req->rq_export->exp_obd;
3148 struct mds_obd *mds = mds_req2mds(req);
3149 struct lustre_handle child_lockh = {0};
3150 struct dentry *de_tgtdir = NULL;
3151 struct dentry *de_new = NULL;
3152 int cleanup_phase = 0;
3153 int update_mode, rc = 0;
3157 * another MDS executing rename operation has asked us to create target
3158 * name. such a creation should destroy existing target name.
3160 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3161 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3163 /* first, lookup the target */
3164 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3165 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3166 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3167 &child_lockh, &de_new, LCK_EX,
3168 MDS_INODELOCK_LOOKUP);
3175 LASSERT(de_tgtdir->d_inode);
3178 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3184 if (cleanup_phase == 1) {
3186 if (parent_lockh[1].cookie != 0)
3187 ldlm_lock_decref(parent_lockh + 1, update_mode);
3189 ldlm_lock_decref(parent_lockh, LCK_PW);
3190 if (child_lockh.cookie != 0)
3191 ldlm_lock_decref(&child_lockh, LCK_EX);
3196 req->rq_status = rc;
3200 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3201 struct ptlrpc_request *req)
3203 struct obd_device *obd = req->rq_export->exp_obd;
3204 struct ptlrpc_request *req2 = NULL;
3205 struct dentry *de_srcdir = NULL;
3206 struct dentry *de_old = NULL;
3207 struct mds_obd *mds = mds_req2mds(req);
3208 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3209 struct lustre_handle child_lockh = {0};
3210 struct mdc_op_data *op_data;
3211 int update_mode, rc = 0;
3214 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3215 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3217 OBD_ALLOC(op_data, sizeof(*op_data));
3218 if (op_data == NULL)
3220 memset(op_data, 0, sizeof(*op_data));
3222 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3223 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3224 &update_mode, rec->ur_name,
3225 rec->ur_namelen, &child_lockh, &de_old,
3226 LCK_EX, MDS_INODELOCK_LOOKUP);
3228 OBD_FREE(op_data, sizeof(*op_data));
3233 LASSERT(de_srcdir->d_inode);
3237 * we already know the target should be created on another MDS so, we
3238 * have to request that MDS to do it.
3241 /* prepare source id */
3242 if (de_old->d_flags & DCACHE_CROSS_REF) {
3243 LASSERT(de_old->d_inode == NULL);
3244 CDEBUG(D_OTHER, "request to move remote name\n");
3245 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3246 } else if (de_old->d_inode == NULL) {
3247 /* oh, source doesn't exist */
3248 OBD_FREE(op_data, sizeof(*op_data));
3249 GOTO(cleanup, rc = -ENOENT);
3251 struct lustre_id sid;
3252 struct inode *inode = de_old->d_inode;
3254 LASSERT(inode != NULL);
3255 CDEBUG(D_OTHER, "request to move local name\n");
3256 id_ino(&op_data->id1) = inode->i_ino;
3257 id_group(&op_data->id1) = mds->mds_num;
3258 id_gen(&op_data->id1) = inode->i_generation;
3260 down(&inode->i_sem);
3261 rc = mds_read_inode_sid(obd, inode, &sid);
3264 CERROR("Can't read inode self id, "
3265 "inode %lu, rc = %d\n",
3270 id_fid(&op_data->id1) = id_fid(&sid);
3273 op_data->id2 = *rec->ur_id2;
3274 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3275 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3276 OBD_FREE(op_data, sizeof(*op_data));
3281 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3287 ptlrpc_req_finished(req2);
3290 if (parent_lockh[1].cookie != 0)
3291 ldlm_lock_decref(parent_lockh + 1, update_mode);
3293 ldlm_lock_decref(parent_lockh, LCK_PW);
3294 if (child_lockh.cookie != 0)
3295 ldlm_lock_decref(&child_lockh, LCK_EX);
3300 req->rq_status = rc;
3304 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3305 struct ptlrpc_request *req, struct lustre_handle *lockh)
3307 struct obd_device *obd = req->rq_export->exp_obd;
3308 struct dentry *de_srcdir = NULL;
3309 struct dentry *de_tgtdir = NULL;
3310 struct dentry *de_old = NULL;
3311 struct dentry *de_new = NULL;
3312 struct inode *old_inode = NULL, *new_inode = NULL;
3313 struct mds_obd *mds = mds_req2mds(req);
3314 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3315 struct mds_body *body = NULL;
3316 struct llog_create_locks *lcl = NULL;
3317 struct lov_mds_md *lmm = NULL;
3318 int rc = 0, cleanup_phase = 0;
3319 void *handle = NULL;
3322 LASSERT(offset == 1);
3324 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3325 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3328 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3330 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3331 DEBUG_REQ(D_HA, req, "rename replay");
3332 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3333 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3334 req->rq_repmsg->buflens[2]);
3337 MD_COUNTER_INCREMENT(obd, rename);
3339 if (rec->ur_namelen == 1) {
3340 rc = mds_reint_rename_create_name(rec, offset, req);
3344 /* check if new name should be located on remote target. */
3345 if (id_group(rec->ur_id2) != mds->mds_num) {
3346 rc = mds_reint_rename_to_remote(rec, offset, req);
3350 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3351 rec->ur_id2, &de_tgtdir, LCK_PW,
3352 rec->ur_name, rec->ur_namelen,
3353 &de_old, rec->ur_tgt,
3354 rec->ur_tgtlen, &de_new,
3355 dlm_handles, LCK_EX);
3359 cleanup_phase = 1; /* parent(s), children, locks */
3360 old_inode = de_old->d_inode;
3361 new_inode = de_new->d_inode;
3363 /* sanity check for src inode */
3364 if (de_old->d_flags & DCACHE_CROSS_REF) {
3365 LASSERT(de_old->d_inode == NULL);
3368 * in the case of cross-ref dir, we can perform this check only
3369 * if child and parent lie on the same mds. This is because
3370 * otherwise they can have the same inode numbers.
3372 if (de_old->d_mdsnum == mds->mds_num) {
3373 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3374 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3375 GOTO(cleanup, rc = -EINVAL);
3378 LASSERT(de_old->d_inode != NULL);
3379 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3380 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3381 GOTO(cleanup, rc = -EINVAL);
3384 /* sanity check for dest inode */
3385 if (de_new->d_flags & DCACHE_CROSS_REF) {
3386 LASSERT(new_inode == NULL);
3388 /* the same check about target dentry. */
3389 if (de_new->d_mdsnum == mds->mds_num) {
3390 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3391 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3392 GOTO(cleanup, rc = -EINVAL);
3396 * regular files usualy do not have ->rename() implemented. But
3397 * we handle only this case when @de_new is cross-ref entry,
3398 * because in other cases it will be handled by vfs_rename().
3400 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3401 !de_old->d_inode->i_op->rename))
3402 GOTO(cleanup, rc = -EPERM);
3405 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3406 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3407 GOTO(cleanup, rc = -EINVAL);
3412 * check if inodes point to each other. This should be checked before
3413 * is_subdir() check, as for the same entries it will think that they
3416 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3417 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3418 old_inode == new_inode)
3419 GOTO(cleanup, rc = 0);
3421 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3423 * check if we are moving old entry into its child. 2.6 does not check
3424 * for this in vfs_rename() anymore.
3426 if (is_subdir(de_new, de_old))
3427 GOTO(cleanup, rc = -EINVAL);
3431 * if we are about to remove the target at first, pass the EA of that
3432 * inode to client to perform and cleanup on OST.
3434 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3435 LASSERT(body != NULL);
3437 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3439 DOWN_READ_I_ALLOC_SEM(new_inode);
3441 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3443 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3444 new_inode->i_nlink == 2) ||
3445 new_inode->i_nlink == 1)) {
3446 if (mds_orphan_open_count(new_inode) > 0) {
3447 /* need to lock pending_dir before transaction */
3448 down(&mds->mds_pending_dir->d_inode->i_sem);
3449 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3450 } else if (S_ISREG(new_inode->i_mode)) {
3451 mds_pack_inode2body(obd, body, new_inode, 0);
3452 mds_pack_md(obd, req->rq_repmsg, 1, body,
3453 new_inode, MDS_PACK_MD_LOCK, 0);
3457 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3458 de_srcdir->d_inode->i_sb);
3460 if (de_old->d_flags & DCACHE_CROSS_REF) {
3461 struct lustre_id old_id;
3463 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3465 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3470 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3475 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3476 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3477 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3480 GOTO(cleanup, rc = PTR_ERR(handle));
3483 de_old->d_fsdata = req;
3484 de_new->d_fsdata = req;
3485 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3488 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3489 if (mds_orphan_open_count(new_inode) > 0)
3490 rc = mds_orphan_add_link(rec, obd, de_new);
3493 GOTO(cleanup, rc = 0);
3495 if (!S_ISREG(new_inode->i_mode))
3498 if (!(body->valid & OBD_MD_FLEASIZE)) {
3499 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3500 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3501 } else if (mds_log_op_unlink(obd, new_inode,
3502 lustre_msg_buf(req->rq_repmsg,1,0),
3503 req->rq_repmsg->buflens[1],
3504 lustre_msg_buf(req->rq_repmsg,2,0),
3505 req->rq_repmsg->buflens[2],
3507 body->valid |= OBD_MD_FLCOOKIE;
3510 rc = mds_destroy_objects(obd, old_inode, 1);
3512 CERROR("can't remove OST object, err %d\n",
3519 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3520 handle, req, rc, 0);
3522 switch (cleanup_phase) {
3524 up(&mds->mds_pending_dir->d_inode->i_sem);
3527 UP_READ_I_ALLOC_SEM(new_inode);
3530 if (dlm_handles[5].cookie != 0)
3531 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3532 if (dlm_handles[6].cookie != 0)
3533 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3536 ptlrpc_save_llog_lock(req, lcl);
3539 if (dlm_handles[3].cookie != 0)
3540 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3541 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3542 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3543 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3545 if (dlm_handles[3].cookie != 0)
3546 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3547 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3548 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3549 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3558 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3561 req->rq_status = rc;
3565 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3566 struct ptlrpc_request *, struct lustre_handle *);
3568 static mds_reinter reinters[REINT_MAX + 1] = {
3569 [REINT_SETATTR] mds_reint_setattr,
3570 [REINT_CREATE] mds_reint_create,
3571 [REINT_LINK] mds_reint_link,
3572 [REINT_UNLINK] mds_reint_unlink,
3573 [REINT_RENAME] mds_reint_rename,
3574 [REINT_OPEN] mds_open
3577 int mds_reint_rec(struct mds_update_record *rec, int offset,
3578 struct ptlrpc_request *req, struct lustre_handle *lockh)
3580 struct obd_device *obd = req->rq_export->exp_obd;
3581 struct lvfs_run_ctxt saved;
3584 /* checked by unpacker */
3585 LASSERT(rec->ur_opcode <= REINT_MAX &&
3586 reinters[rec->ur_opcode] != NULL);
3588 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3589 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3590 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);