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 = -EIO);
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;
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);
797 * as Peter asked, mkdir() should distribute new directories
798 * over the whole cluster in order to distribute namespace
799 * processing load. first, we calculate which MDS to use to put
800 * new directory's inode in.
802 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
803 rec->ur_flags, &req->rq_peer, dir);
804 if (i == mds->mds_num) {
805 /* inode will be created locally */
806 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
808 GOTO(cleanup, rc = PTR_ERR(handle));
810 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
812 CERROR("Can't create dir \"%s\", rc = %d\n",
813 dchild->d_name.name, rc);
817 down(&dchild->d_inode->i_sem);
819 rc = mds_update_inode_sid(obd, dchild->d_inode,
820 handle, rec->ur_id2);
822 CERROR("mds_update_inode_sid() failed, inode %lu, "
823 "rc %d\n", dchild->d_inode->i_ino, rc);
827 * make sure, that fid is up-to-date.
829 mds_set_last_fid(obd, id_fid(rec->ur_id2));
831 rc = mds_alloc_inode_sid(obd, dchild->d_inode,
834 CERROR("mds_alloc_inode_sid() failed, inode %lu, "
835 "rc %d\n", dchild->d_inode->i_ino, rc);
838 up(&dchild->d_inode->i_sem);
844 nstripes = *(u16 *)rec->ur_eadata;
846 if (rc == 0 && nstripes) {
848 * we pass LCK_EX to split routine to signal,
849 * that we have exclusive access to the
850 * directory. Simple because nobody knows it
851 * already exists -bzzz
853 rc = mds_try_to_split_dir(obd, dchild,
857 /* dir got splitted */
860 /* an error occured during
865 } else if (!DENTRY_VALID(dchild)) {
866 /* inode will be created on another MDS */
867 struct obdo *oa = NULL;
868 struct mds_body *body;
872 /* first, create that inode */
875 GOTO(cleanup, rc = -ENOMEM);
880 if (rec->ur_eadata) {
881 /* user asks for creating splitted dir */
882 oa->o_easize = *((u16 *) rec->ur_eadata);
885 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
886 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
888 /* adjust the uid/gid/mode bits */
889 oa->o_mode = rec->ur_mode;
890 oa->o_uid = current->fsuid;
891 oa->o_gid = (dir->i_mode & S_ISGID) ?
892 dir->i_gid : current->fsgid;
893 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
895 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
898 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
900 * here inode number and generation are
901 * important, as this is replay request and we
902 * need them to check if such an object is
905 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
906 obd->obd_name, rec->ur_namelen - 1,
907 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
908 (unsigned)id_gen(rec->ur_id2));
909 oa->o_id = id_ino(rec->ur_id2);
910 oa->o_fid = id_fid(rec->ur_id2);
911 oa->o_generation = id_gen(rec->ur_id2);
912 oa->o_flags |= OBD_FL_RECREATE_OBJS;
913 LASSERT(oa->o_fid != 0);
916 /* obtain default ACL */
917 acl_size = mds_get_default_acl(dir, &acl);
920 GOTO(cleanup, rc = -ENOMEM);
924 * before obd_create() is called, o_fid is not known if
925 * this is not recovery of cause.
927 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
931 OBD_FREE(acl, acl_size);
934 CERROR("can't create remote inode: %d\n", rc);
935 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
936 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
937 rec->ur_name, rec->ur_mode);
942 LASSERT(oa->o_fid != 0);
944 /* now, add new dir entry for it */
945 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
946 if (IS_ERR(handle)) {
948 GOTO(cleanup, rc = PTR_ERR(handle));
951 /* creating local dentry for remote inode. */
952 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
953 rec->ur_namelen - 1, oa->o_id,
954 oa->o_generation, i, oa->o_fid);
957 CERROR("Can't create local entry %*s for "
958 "remote inode.\n", rec->ur_namelen - 1,
964 body = lustre_msg_buf(req->rq_repmsg,
966 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
968 obdo2id(&body->id1, oa);
971 /* requested name exists in the directory */
978 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
980 GOTO(cleanup, rc = PTR_ERR(handle));
981 if (rec->ur_tgt == NULL) /* no target supplied */
982 rc = -EINVAL; /* -EPROTO? */
984 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
992 int rdev = rec->ur_rdev;
993 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
995 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
996 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1001 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1002 dchild->d_fsdata = NULL;
1003 GOTO(cleanup, rc = -EINVAL);
1006 /* In case we stored the desired inum in here, we want to clean up. */
1007 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1008 dchild->d_fsdata = NULL;
1011 CDEBUG(D_INODE, "error during create: %d\n", rc);
1013 } else if (dchild->d_inode) {
1014 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1016 struct mds_body *body;
1017 struct inode *inode = dchild->d_inode;
1020 iattr.ia_uid = rec->ur_fsuid;
1021 LTIME_S(iattr.ia_atime) = rec->ur_time;
1022 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1023 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1025 if (dir->i_mode & S_ISGID)
1026 iattr.ia_gid = dir->i_gid;
1028 iattr.ia_gid = rec->ur_fsgid;
1030 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1031 ATTR_MTIME | ATTR_CTIME;
1033 if (id_ino(rec->ur_id2)) {
1034 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1035 inode->i_generation = id_gen(rec->ur_id2);
1037 if (type != S_IFDIR) {
1038 down(&inode->i_sem);
1039 rc = mds_update_inode_sid(obd, inode,
1040 handle, rec->ur_id2);
1043 CERROR("Can't update inode self id, "
1048 * make sure, that fid is up-to-date.
1050 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1053 /* dirtied and committed by the upcoming setattr. */
1054 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1055 inode->i_ino, inode->i_generation);
1057 struct lustre_handle child_ino_lockh;
1059 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1060 inode->i_ino, inode->i_generation);
1062 if (type != S_IFDIR) {
1064 * allocate new id for @inode if it is not dir,
1065 * because for dir it was already done.
1067 down(&inode->i_sem);
1068 rc = mds_alloc_inode_sid(obd, inode,
1072 CERROR("mds_alloc_inode_sid() failed, "
1073 "inode %lu, rc %d\n", inode->i_ino,
1080 * the inode we were allocated may have just
1081 * been freed by an unlink operation. We take
1082 * this lock to synchronize against the matching
1083 * reply-ack-lock taken in unlink, to avoid
1084 * replay problems if this reply makes it out to
1085 * the client but the unlink's does not. See
1086 * bug 2029 for more detail.
1088 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1089 if (rc != ELDLM_OK) {
1090 CERROR("error locking for unlink/create sync: "
1093 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1098 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1100 CERROR("error on child setattr: rc = %d\n", rc);
1102 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1103 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1105 CERROR("error on parent setattr: rc = %d\n", rc);
1107 MD_COUNTER_INCREMENT(obd, create);
1109 /* take care of default stripe inheritance */
1110 if (type == S_IFDIR) {
1111 struct lov_mds_md lmm;
1112 int lmm_size = sizeof(lmm);
1114 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1116 down(&inode->i_sem);
1117 rc = fsfilt_set_md(obd, inode, handle,
1118 &lmm, lmm_size, EA_LOV);
1122 CERROR("error on copy stripe info: rc = %d\n",
1128 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1129 mds_pack_inode2body(obd, body, inode, 1);
1130 mds_body_do_reverse_map(med, body);
1135 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1137 if (rc && created) {
1138 /* Destroy the file we just created. This should not need extra
1139 * journal credits, as we have already modified all of the
1140 * blocks needed in order to create the file in the first
1144 err = vfs_rmdir(dir, dchild);
1146 CERROR("rmdir in error path: %d\n", err);
1149 err = vfs_unlink(dir, dchild);
1151 CERROR("unlink in error path: %d\n", err);
1157 switch (cleanup_phase) {
1158 case 2: /* child dentry */
1160 case 1: /* locked parent dentry */
1162 if (lockh[1].cookie != 0)
1163 ldlm_lock_decref(lockh + 1, parent_mode);
1166 ldlm_lock_decref(lockh, LCK_PW);
1168 ptlrpc_save_lock(req, lockh, LCK_PW);
1174 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1178 OBD_FREE(mea, mea_size);
1179 req->rq_status = rc;
1184 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1185 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1189 for (i = 0; i < RES_NAME_SIZE; i++) {
1191 * this is needed to make zeroed res_id entries to be put at the
1192 * end of list in *ordered_locks() .
1194 if (res1->name[i] == 0 && res2->name[i] != 0)
1196 if (res2->name[i] == 0 && res1->name[i] != 0)
1198 if (res1->name[i] > res2->name[i])
1200 if (res1->name[i] < res2->name[i])
1207 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1213 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1214 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1215 * because they take the place of local semaphores.
1217 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1218 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1219 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1220 struct lustre_handle *p1_lockh, int p1_lock_mode,
1221 ldlm_policy_data_t *p1_policy,
1222 struct ldlm_res_id *p2_res_id,
1223 struct lustre_handle *p2_lockh, int p2_lock_mode,
1224 ldlm_policy_data_t *p2_policy)
1226 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1227 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1228 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1229 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1233 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1235 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1236 res_id[0]->name[0], res_id[1]->name[0]);
1238 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1239 handles[1] = p1_lockh;
1240 handles[0] = p2_lockh;
1241 res_id[1] = p1_res_id;
1242 res_id[0] = p2_res_id;
1243 lock_modes[1] = p1_lock_mode;
1244 lock_modes[0] = p2_lock_mode;
1245 policies[1] = p1_policy;
1246 policies[0] = p2_policy;
1249 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1250 res_id[0]->name[0], res_id[1]->name[0]);
1252 flags = LDLM_FL_LOCAL_ONLY;
1253 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1254 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1255 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1256 NULL, 0, NULL, handles[0]);
1259 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1261 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1262 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1263 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1264 ldlm_lock_addref(handles[1], lock_modes[1]);
1265 } else if (res_id[1]->name[0] != 0) {
1266 flags = LDLM_FL_LOCAL_ONLY;
1267 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1268 *res_id[1], LDLM_IBITS, policies[1],
1269 lock_modes[1], &flags, mds_blocking_ast,
1270 ldlm_completion_ast, NULL, NULL, NULL, 0,
1272 if (rc != ELDLM_OK) {
1273 ldlm_lock_decref(handles[0], lock_modes[0]);
1276 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1282 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1283 struct lustre_handle *p1_lockh, int p1_lock_mode,
1284 ldlm_policy_data_t *p1_policy,
1285 struct ldlm_res_id *p2_res_id,
1286 struct lustre_handle *p2_lockh, int p2_lock_mode,
1287 ldlm_policy_data_t *p2_policy,
1288 struct ldlm_res_id *c1_res_id,
1289 struct lustre_handle *c1_lockh, int c1_lock_mode,
1290 ldlm_policy_data_t *c1_policy,
1291 struct ldlm_res_id *c2_res_id,
1292 struct lustre_handle *c2_lockh, int c2_lock_mode,
1293 ldlm_policy_data_t *c2_policy)
1295 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1296 c1_res_id, c2_res_id };
1297 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1298 c1_lockh, c2_lockh };
1299 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1300 c1_lock_mode, c2_lock_mode };
1301 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1302 c1_policy, c2_policy};
1303 int rc, i, j, sorted, flags;
1306 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1307 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1308 res_id[3]->name[0]);
1311 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1312 * res_id should be at the end of list after sorting is finished.
1314 for (i = 1; i < 4; i++) {
1316 dlm_handles[4] = dlm_handles[i];
1317 res_id[4] = res_id[i];
1318 lock_modes[4] = lock_modes[i];
1319 policies[4] = policies[i];
1323 if (res_gt(res_id[j], res_id[4], policies[j],
1325 dlm_handles[j + 1] = dlm_handles[j];
1326 res_id[j + 1] = res_id[j];
1327 lock_modes[j + 1] = lock_modes[j];
1328 policies[j + 1] = policies[j];
1333 } while (j >= 0 && !sorted);
1335 dlm_handles[j + 1] = dlm_handles[4];
1336 res_id[j + 1] = res_id[4];
1337 lock_modes[j + 1] = lock_modes[4];
1338 policies[j + 1] = policies[4];
1341 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1342 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1343 res_id[3]->name[0]);
1345 /* XXX we could send ASTs on all these locks first before blocking? */
1346 for (i = 0; i < 4; i++) {
1350 * nevertheless zeroed res_ids should be at the end of list, and
1351 * could use break here, I think, that it is more correctly for
1352 * clear understanding of code to have continue here, as it
1353 * clearly means, that zeroed res_id should be skipped and does
1354 * not mean, that if we meet zeroed res_id we should stop
1357 if (res_id[i]->name[0] == 0)
1361 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1362 (policies[i]->l_inodebits.bits &
1363 policies[i-1]->l_inodebits.bits) ) {
1364 memcpy(dlm_handles[i], dlm_handles[i-1],
1365 sizeof(*(dlm_handles[i])));
1366 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1368 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1369 *res_id[i], LDLM_IBITS,
1371 lock_modes[i], &flags,
1373 ldlm_completion_ast, NULL, NULL,
1374 NULL, 0, NULL, dlm_handles[i]);
1376 GOTO(out_err, rc = -EIO);
1377 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1384 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1389 /* In the unlikely case that the child changed while we were waiting
1390 * on the lock, we need to drop the lock on the old child and either:
1391 * - if the child has a lower resource name, then we have to also
1392 * drop the parent lock and regain the locks in the right order
1393 * - in the rename case, if the child has a lower resource name than one of
1394 * the other parent/child resources (maxres) we also need to reget the locks
1395 * - if the child has a higher resource name (this is the common case)
1396 * we can just get the lock on the new child (still in lock order)
1398 * Returns 0 if the child did not change or if it changed but could be locked.
1399 * Returns 1 if the child changed and we need to re-lock (no locks held).
1400 * Returns -ve error with a valid dchild (no locks held). */
1401 static int mds_verify_child(struct obd_device *obd,
1402 struct ldlm_res_id *parent_res_id,
1403 struct lustre_handle *parent_lockh,
1404 struct dentry *dparent, int parent_mode,
1405 struct ldlm_res_id *child_res_id,
1406 struct lustre_handle *child_lockh,
1407 struct dentry **dchildp, int child_mode,
1408 ldlm_policy_data_t *child_policy,
1409 const char *name, int namelen,
1410 struct ldlm_res_id *maxres,
1411 unsigned long child_ino, __u32 child_gen)
1413 struct lustre_id sid;
1414 struct dentry *vchild, *dchild = *dchildp;
1415 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1418 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1420 GOTO(cleanup, rc = PTR_ERR(vchild));
1422 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1423 if (child_gen == vchild->d_generation &&
1424 child_ino == vchild->d_inum) {
1433 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1434 (vchild->d_inode != NULL &&
1435 child_gen == vchild->d_inode->i_generation &&
1436 child_ino == vchild->d_inode->i_ino))) {
1444 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1445 vchild->d_inode, dchild ? dchild->d_inode : 0,
1446 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1447 child_res_id->name[0]);
1449 if (child_res_id->name[0] != 0)
1450 ldlm_lock_decref(child_lockh, child_mode);
1454 cleanup_phase = 1; /* parent lock only */
1455 *dchildp = dchild = vchild;
1457 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1460 if (dchild->d_inode) {
1461 down(&dchild->d_inode->i_sem);
1462 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1463 up(&dchild->d_inode->i_sem);
1465 CERROR("Can't read inode self id, inode %lu,"
1466 " rc %d\n", dchild->d_inode->i_ino, rc);
1469 child_res_id->name[0] = id_fid(&sid);
1470 child_res_id->name[1] = id_group(&sid);
1472 child_res_id->name[0] = dchild->d_fid;
1473 child_res_id->name[1] = dchild->d_mdsnum;
1476 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1477 res_gt(maxres, child_res_id, NULL, NULL)) {
1478 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1479 child_res_id->name[0], parent_res_id->name[0],
1481 GOTO(cleanup, rc = 1);
1484 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1485 *child_res_id, LDLM_IBITS, child_policy,
1486 child_mode, &flags, mds_blocking_ast,
1487 ldlm_completion_ast, NULL, NULL, NULL, 0,
1490 GOTO(cleanup, rc = -EIO);
1493 memset(child_res_id, 0, sizeof(*child_res_id));
1499 switch(cleanup_phase) {
1501 if (child_res_id->name[0] != 0)
1502 ldlm_lock_decref(child_lockh, child_mode);
1504 ldlm_lock_decref(parent_lockh, parent_mode);
1510 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1511 struct lustre_id *id,
1512 struct lustre_handle *parent_lockh,
1513 struct dentry **dparentp, int parent_mode,
1514 __u64 parent_lockpart, int *update_mode,
1515 char *name, int namelen,
1516 struct lustre_handle *child_lockh,
1517 struct dentry **dchildp, int child_mode,
1518 __u64 child_lockpart)
1520 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1521 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1522 struct ldlm_res_id parent_res_id = { .name = {0} };
1523 struct ldlm_res_id child_res_id = { .name = {0} };
1524 unsigned long child_ino = 0; __u32 child_gen = 0;
1525 int rc = 0, cleanup_phase = 0;
1526 struct lustre_id sid;
1527 struct inode *inode;
1530 /* Step 1: Lookup parent */
1531 *dparentp = mds_id2dentry(obd, id, NULL);
1532 if (IS_ERR(*dparentp)) {
1533 rc = PTR_ERR(*dparentp);
1538 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1539 (*dparentp)->d_inode->i_ino, name);
1541 parent_res_id.name[0] = id_fid(id);
1542 parent_res_id.name[1] = id_group(id);
1545 parent_lockh[1].cookie = 0;
1546 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1547 struct ldlm_res_id res_id = { .name = {0} };
1548 ldlm_policy_data_t policy;
1551 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1553 res_id.name[0] = id_fid(id);
1554 res_id.name[1] = id_group(id);
1555 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1557 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1558 res_id, LDLM_IBITS, &policy,
1559 *update_mode, &flags,
1561 ldlm_completion_ast,
1562 NULL, NULL, NULL, 0, NULL,
1568 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1571 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1572 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1573 parent_res_id.name[2]);
1577 cleanup_phase = 1; /* parent dentry */
1579 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1580 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1581 if (IS_ERR(*dchildp)) {
1582 rc = PTR_ERR(*dchildp);
1583 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1587 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1589 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1590 * lock. Drop possible UPDATE lock!
1592 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1593 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1595 child_res_id.name[0] = (*dchildp)->d_fid;
1596 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1597 child_ino = (*dchildp)->d_inum;
1598 child_gen = (*dchildp)->d_generation;
1602 inode = (*dchildp)->d_inode;
1604 inode = igrab(inode);
1608 down(&inode->i_sem);
1609 rc = mds_read_inode_sid(obd, inode, &sid);
1612 CERROR("Can't read inode self id, inode %lu, "
1613 "rc %d\n", inode->i_ino, rc);
1618 child_ino = inode->i_ino;
1619 child_gen = inode->i_generation;
1620 child_res_id.name[0] = id_fid(&sid);
1621 child_res_id.name[1] = id_group(&sid);
1625 cleanup_phase = 2; /* child dentry */
1627 /* Step 3: Lock parent and child in resource order. If child doesn't
1628 * exist, we still have to lock the parent and re-lookup. */
1629 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1630 &parent_policy, &child_res_id, child_lockh,
1631 child_mode, &child_policy);
1635 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1636 cleanup_phase = 4; /* child lock */
1638 cleanup_phase = 3; /* parent lock */
1640 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1641 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1642 parent_mode, &child_res_id, child_lockh,
1643 dchildp, child_mode, &child_policy,
1644 name, namelen, &parent_res_id, child_ino,
1656 switch (cleanup_phase) {
1658 ldlm_lock_decref(child_lockh, child_mode);
1660 ldlm_lock_decref(parent_lockh, parent_mode);
1665 if (parent_lockh[1].cookie)
1666 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1674 void mds_reconstruct_generic(struct ptlrpc_request *req)
1676 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1677 mds_req_from_mcd(req, med->med_mcd);
1680 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1681 * instead link the inode into the PENDING directory until it is finally
1682 * released. We can't simply call mds_reint_rename() or some part thereof,
1683 * because we don't have the inode to check for link count/open status until
1684 * after it is locked.
1686 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1687 * before starting journal transaction.
1689 * returns 1 on success
1690 * returns 0 if we lost a race and didn't make a new link
1691 * returns negative on error
1693 static int mds_orphan_add_link(struct mds_update_record *rec,
1694 struct obd_device *obd, struct dentry *dentry)
1696 struct mds_obd *mds = &obd->u.mds;
1697 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1698 struct inode *inode = dentry->d_inode;
1699 struct dentry *pending_child;
1700 char idname[LL_ID_NAMELEN];
1701 int idlen = 0, rc, mode;
1704 LASSERT(inode != NULL);
1705 LASSERT(!mds_inode_is_orphan(inode));
1706 #ifndef HAVE_I_ALLOC_SEM
1707 LASSERT(down_trylock(&inode->i_sem) != 0);
1709 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1711 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1713 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1714 mds_orphan_open_count(inode), inode->i_nlink,
1715 S_ISDIR(inode->i_mode) ? "dir" :
1716 S_ISREG(inode->i_mode) ? "file" : "other",
1717 rec->ur_name, idname);
1719 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1722 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1723 if (IS_ERR(pending_child))
1724 RETURN(PTR_ERR(pending_child));
1726 if (pending_child->d_inode != NULL) {
1727 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1728 LASSERT(pending_child->d_inode == inode);
1729 GOTO(out_dput, rc = 0);
1733 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1734 * linking and return real mode back then -bzzz
1736 mode = inode->i_mode;
1737 inode->i_mode = S_IFREG;
1738 rc = vfs_link(dentry, pending_dir, pending_child);
1740 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1743 mds_inode_set_orphan(inode);
1745 /* return mode and correct i_nlink if inode is directory */
1746 inode->i_mode = mode;
1747 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1748 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1750 if (S_ISDIR(mode)) {
1752 pending_dir->i_nlink++;
1753 mark_inode_dirty(inode);
1754 mark_inode_dirty(pending_dir);
1757 GOTO(out_dput, rc = 1);
1759 l_dput(pending_child);
1763 int mds_create_local_dentry(struct mds_update_record *rec,
1764 struct obd_device *obd)
1766 struct mds_obd *mds = &obd->u.mds;
1767 struct inode *id_dir = mds->mds_id_dir->d_inode;
1768 int idlen = 0, rc, cleanup_phase = 0;
1769 struct dentry *new_child = NULL;
1770 char *idname = rec->ur_name;
1771 struct dentry *child = NULL;
1772 struct lustre_handle lockh[2] = {{0}, {0}};
1773 struct lustre_id sid;
1777 down(&id_dir->i_sem);
1778 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1779 id_gen(rec->ur_id1));
1781 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1782 idname, OLID4(rec->ur_id1));
1784 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1787 if (IS_ERR(new_child)) {
1788 CERROR("can't lookup %s: %d\n", idname,
1789 (int) PTR_ERR(new_child));
1790 GOTO(cleanup, rc = PTR_ERR(new_child));
1794 down(&id_dir->i_sem);
1795 rc = mds_read_inode_sid(obd, id_dir, &sid);
1798 CERROR("Can't read inode self id, inode %lu, "
1799 "rc %d\n", id_dir->i_ino, rc);
1803 if (new_child->d_inode != NULL) {
1804 /* nice. we've already have local dentry! */
1805 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1806 (unsigned)new_child->d_inode->i_ino,
1807 (unsigned)new_child->d_inode->i_generation);
1809 id_ino(rec->ur_id1) = id_dir->i_ino;
1810 id_gen(rec->ur_id1) = id_dir->i_generation;
1811 rec->ur_namelen = idlen + 1;
1813 id_fid(rec->ur_id1) = id_fid(&sid);
1814 id_group(rec->ur_id1) = id_group(&sid);
1816 GOTO(cleanup, rc = 0);
1819 /* new, local dentry will be added soon. we need no aliases here */
1822 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1823 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1825 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1826 LCK_EX, lockh, NULL, NULL, 0,
1827 MDS_INODELOCK_UPDATE);
1830 if (IS_ERR(child)) {
1831 rc = PTR_ERR(child);
1832 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1833 CERROR("can't get victim: %d\n", rc);
1838 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1840 GOTO(cleanup, rc = PTR_ERR(handle));
1842 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1843 idlen, id_ino(rec->ur_id1),
1844 id_gen(rec->ur_id1), mds->mds_num,
1845 id_fid(rec->ur_id1));
1847 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1848 (unsigned long)child->d_inode->i_ino,
1849 (unsigned long)child->d_inode->i_generation, rc);
1851 if (S_ISDIR(child->d_inode->i_mode)) {
1853 mark_inode_dirty(id_dir);
1855 mark_inode_dirty(child->d_inode);
1857 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1859 id_ino(rec->ur_id1) = id_dir->i_ino;
1860 id_gen(rec->ur_id1) = id_dir->i_generation;
1861 rec->ur_namelen = idlen + 1;
1863 id_fid(rec->ur_id1) = id_fid(&sid);
1864 id_group(rec->ur_id1) = id_group(&sid);
1868 switch(cleanup_phase) {
1870 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1871 ldlm_lock_decref(lockh, LCK_EX);
1881 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1882 struct ptlrpc_request *slave)
1884 void *cookie, *cookie2;
1885 struct mds_body *body2;
1886 struct mds_body *body;
1890 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1891 LASSERT(body != NULL);
1893 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1894 LASSERT(body2 != NULL);
1896 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1899 memcpy(body2, body, sizeof(*body));
1900 body2->valid &= ~OBD_MD_FLCOOKIE;
1902 if (!(body->valid & OBD_MD_FLEASIZE) &&
1903 !(body->valid & OBD_MD_FLDIREA))
1906 if (body->eadatasize == 0) {
1907 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1911 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1913 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1914 LASSERT(ea != NULL);
1916 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1917 LASSERT(ea2 != NULL);
1919 memcpy(ea2, ea, body->eadatasize);
1921 if (body->valid & OBD_MD_FLCOOKIE) {
1922 LASSERT(master->rq_repmsg->buflens[2] >=
1923 slave->rq_repmsg->buflens[2]);
1924 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1925 slave->rq_repmsg->buflens[2]);
1926 LASSERT(cookie != NULL);
1928 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1929 master->rq_repmsg->buflens[2]);
1930 LASSERT(cookie2 != NULL);
1931 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1932 body2->valid |= OBD_MD_FLCOOKIE;
1937 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1938 int offset, struct ptlrpc_request *req,
1939 struct lustre_handle *parent_lockh,
1940 int update_mode, struct dentry *dparent,
1941 struct lustre_handle *child_lockh,
1942 struct dentry *dchild)
1944 struct obd_device *obd = req->rq_export->exp_obd;
1945 struct mds_obd *mds = mds_req2mds(req);
1946 struct ptlrpc_request *request = NULL;
1947 int rc = 0, cleanup_phase = 0;
1948 struct mdc_op_data *op_data;
1952 LASSERT(offset == 1 || offset == 3);
1954 /* time to drop i_nlink on remote MDS */
1955 OBD_ALLOC(op_data, sizeof(*op_data));
1956 if (op_data == NULL)
1959 memset(op_data, 0, sizeof(*op_data));
1960 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1961 op_data->create_mode = rec->ur_mode;
1963 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1964 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1966 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1967 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1968 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1971 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1972 op_data->create_mode |= MDS_MODE_REPLAY;
1974 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1975 OBD_FREE(op_data, sizeof(*op_data));
1980 mds_copy_unlink_reply(req, request);
1981 ptlrpc_req_finished(request);
1985 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1988 GOTO(cleanup, rc = PTR_ERR(handle));
1989 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1990 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1995 req->rq_status = rc;
1998 if (parent_lockh[1].cookie != 0)
1999 ldlm_lock_decref(parent_lockh + 1, update_mode);
2001 ldlm_lock_decref(child_lockh, LCK_EX);
2003 ldlm_lock_decref(parent_lockh, LCK_PW);
2005 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2012 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2013 struct ptlrpc_request *req,
2014 struct lustre_handle *lh)
2016 struct dentry *dparent = NULL, *dchild;
2017 struct mds_obd *mds = mds_req2mds(req);
2018 struct obd_device *obd = req->rq_export->exp_obd;
2019 struct mds_body *body = NULL;
2020 struct inode *child_inode = NULL;
2021 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2022 struct lustre_handle child_lockh = {0};
2023 struct lustre_handle child_reuse_lockh = {0};
2024 struct lustre_handle *slave_lockh = NULL;
2025 char idname[LL_ID_NAMELEN];
2026 struct llog_create_locks *lcl = NULL;
2027 void *handle = NULL;
2028 int rc = 0, cleanup_phase = 0;
2029 int unlink_by_id = 0;
2033 LASSERT(offset == 1 || offset == 3);
2035 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2036 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2039 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2041 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2042 DEBUG_REQ(D_HA, req, "unlink replay");
2043 LASSERT(offset == 1); /* should not come from intent */
2044 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2045 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2046 req->rq_repmsg->buflens[2]);
2049 MD_COUNTER_INCREMENT(obd, unlink);
2051 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2052 GOTO(cleanup, rc = -ENOENT);
2054 if (rec->ur_namelen == 1) {
2055 /* this is request to drop i_nlink on local inode */
2057 rec->ur_name = idname;
2058 rc = mds_create_local_dentry(rec, obd);
2059 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2060 DEBUG_REQ(D_HA, req,
2061 "drop nlink on inode "DLID4" (replay)",
2062 OLID4(rec->ur_id1));
2068 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2069 /* master mds for directory asks slave removing inode is already
2071 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2072 LCK_PW, parent_lockh,
2073 &update_mode, rec->ur_name,
2075 MDS_INODELOCK_UPDATE);
2076 if (IS_ERR(dparent))
2077 GOTO(cleanup, rc = PTR_ERR(dparent));
2078 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2079 rec->ur_namelen - 1);
2081 GOTO(cleanup, rc = PTR_ERR(dchild));
2082 child_lockh.cookie = 0;
2083 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2084 LASSERT(dchild->d_inode != NULL);
2085 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2087 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2088 parent_lockh, &dparent,
2089 LCK_PW, MDS_INODELOCK_UPDATE,
2090 &update_mode, rec->ur_name,
2091 rec->ur_namelen, &child_lockh,
2093 MDS_INODELOCK_LOOKUP |
2094 MDS_INODELOCK_UPDATE);
2099 if (dchild->d_flags & DCACHE_CROSS_REF) {
2100 /* we should have parent lock only here */
2101 LASSERT(unlink_by_id == 0);
2102 LASSERT(dchild->d_mdsnum != mds->mds_num);
2103 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2104 update_mode, dparent, &child_lockh, dchild);
2108 cleanup_phase = 1; /* dchild, dparent, locks */
2111 child_inode = dchild->d_inode;
2112 if (child_inode == NULL) {
2113 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2114 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2115 GOTO(cleanup, rc = -ENOENT);
2118 cleanup_phase = 2; /* dchild has a lock */
2120 /* We have to do these checks ourselves, in case we are making an
2121 * orphan. The client tells us whether rmdir() or unlink() was called,
2122 * so we need to return appropriate errors (bug 72).
2124 * We don't have to check permissions, because vfs_rename (called from
2125 * mds_open_unlink_rename) also calls may_delete. */
2126 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2127 if (!S_ISDIR(child_inode->i_mode))
2128 GOTO(cleanup, rc = -ENOTDIR);
2130 if (S_ISDIR(child_inode->i_mode))
2131 GOTO(cleanup, rc = -EISDIR);
2134 /* handle splitted dir */
2135 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2139 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2140 * reuse (see bug 2029). */
2141 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2144 cleanup_phase = 3; /* child inum lock */
2146 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2148 /* ldlm_reply in buf[0] if called via intent */
2154 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2155 LASSERT(body != NULL);
2157 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2158 DOWN_READ_I_ALLOC_SEM(child_inode);
2159 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2161 /* If this is potentially the last reference to this inode, get the
2162 * OBD EA data first so the client can destroy OST objects. We
2163 * only do the object removal later if no open files/links remain. */
2164 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2165 child_inode->i_nlink == 1) {
2166 if (mds_orphan_open_count(child_inode) > 0) {
2167 /* need to lock pending_dir before transaction */
2168 down(&mds->mds_pending_dir->d_inode->i_sem);
2169 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2170 } else if (S_ISREG(child_inode->i_mode)) {
2171 mds_pack_inode2body(obd, body, child_inode, 0);
2172 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2173 body, child_inode, MDS_PACK_MD_LOCK, 0);
2177 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2178 switch (child_inode->i_mode & S_IFMT) {
2180 /* Drop any lingering child directories before we start our
2181 * transaction, to avoid doing multiple inode dirty/delete
2182 * in our compound transaction (bug 1321). */
2183 shrink_dcache_parent(dchild);
2184 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2187 GOTO(cleanup, rc = PTR_ERR(handle));
2188 rc = vfs_rmdir(dparent->d_inode, dchild);
2191 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2192 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2194 handle = fsfilt_start_log(obd, dparent->d_inode,
2195 FSFILT_OP_UNLINK, NULL,
2196 le32_to_cpu(lmm->lmm_stripe_count));
2198 GOTO(cleanup, rc = PTR_ERR(handle));
2199 rc = vfs_unlink(dparent->d_inode, dchild);
2207 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2210 GOTO(cleanup, rc = PTR_ERR(handle));
2211 rc = vfs_unlink(dparent->d_inode, dchild);
2214 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2217 GOTO(cleanup, rc = -EINVAL);
2220 if (rc == 0 && child_inode->i_nlink == 0) {
2221 if (mds_orphan_open_count(child_inode) > 0)
2222 rc = mds_orphan_add_link(rec, obd, dchild);
2225 GOTO(cleanup, rc = 0);
2227 if (!S_ISREG(child_inode->i_mode))
2230 if (!(body->valid & OBD_MD_FLEASIZE)) {
2231 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2232 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2233 } else if (mds_log_op_unlink(obd, child_inode,
2234 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2235 req->rq_repmsg->buflens[offset + 1],
2236 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2237 req->rq_repmsg->buflens[offset + 2],
2239 body->valid |= OBD_MD_FLCOOKIE;
2242 rc = mds_destroy_objects(obd, child_inode, 1);
2244 CERROR("can't remove OST object, err %d\n",
2256 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2257 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2258 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2260 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2262 CERROR("error on parent setattr: rc = %d\n", err);
2264 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2265 handle, req, rc, 0);
2267 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2268 "unlinked", 0, NULL);
2269 switch(cleanup_phase) {
2270 case 5: /* pending_dir semaphore */
2271 up(&mds->mds_pending_dir->d_inode->i_sem);
2272 case 4: /* child inode semaphore */
2273 UP_READ_I_ALLOC_SEM(child_inode);
2274 /* handle splitted dir */
2276 /* master directory can be non-empty or something else ... */
2277 mds_unlink_slave_objs(obd, dchild);
2280 ptlrpc_save_llog_lock(req, lcl);
2281 case 3: /* child ino-reuse lock */
2282 if (rc && body != NULL) {
2283 // Don't unlink the OST objects if the MDS unlink failed
2287 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2289 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2290 case 2: /* child lock */
2291 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2292 if (child_lockh.cookie)
2293 ldlm_lock_decref(&child_lockh, LCK_EX);
2294 case 1: /* child and parent dentry, parent lock */
2296 if (parent_lockh[1].cookie != 0)
2297 ldlm_lock_decref(parent_lockh + 1, update_mode);
2300 ldlm_lock_decref(parent_lockh, LCK_PW);
2302 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2309 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2312 req->rq_status = rc;
2317 * to service requests from remote MDS to increment i_nlink
2319 static int mds_reint_link_acquire(struct mds_update_record *rec,
2320 int offset, struct ptlrpc_request *req,
2321 struct lustre_handle *lh)
2323 struct obd_device *obd = req->rq_export->exp_obd;
2324 struct ldlm_res_id src_res_id = { .name = {0} };
2325 struct lustre_handle *handle = NULL, src_lockh = {0};
2326 struct mds_obd *mds = mds_req2mds(req);
2327 int rc = 0, cleanup_phase = 0;
2328 struct dentry *de_src = NULL;
2329 ldlm_policy_data_t policy;
2333 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2334 obd->obd_name, OLID4(rec->ur_id1));
2336 /* Step 1: Lookup the source inode and target directory by ID */
2337 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2339 GOTO(cleanup, rc = PTR_ERR(de_src));
2340 cleanup_phase = 1; /* source dentry */
2342 src_res_id.name[0] = id_fid(rec->ur_id1);
2343 src_res_id.name[1] = id_group(rec->ur_id1);
2344 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2346 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2347 src_res_id, LDLM_IBITS, &policy,
2348 LCK_EX, &flags, mds_blocking_ast,
2349 ldlm_completion_ast, NULL, NULL,
2350 NULL, 0, NULL, &src_lockh);
2352 GOTO(cleanup, rc = -ENOLCK);
2353 cleanup_phase = 2; /* lock */
2355 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2357 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2358 if (IS_ERR(handle)) {
2359 rc = PTR_ERR(handle);
2362 de_src->d_inode->i_nlink++;
2363 mark_inode_dirty(de_src->d_inode);
2367 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2368 handle, req, rc, 0);
2369 switch (cleanup_phase) {
2372 ldlm_lock_decref(&src_lockh, LCK_EX);
2374 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2380 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2383 req->rq_status = rc;
2388 * request to link to foreign inode:
2389 * - acquire i_nlinks on this inode
2392 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2393 int offset, struct ptlrpc_request *req,
2394 struct lustre_handle *lh)
2396 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2397 struct obd_device *obd = req->rq_export->exp_obd;
2398 struct dentry *de_tgt_dir = NULL;
2399 struct mds_obd *mds = mds_req2mds(req);
2400 int rc = 0, cleanup_phase = 0;
2401 struct mdc_op_data *op_data;
2402 struct ptlrpc_request *request = NULL;
2406 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2407 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2408 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2409 OLID4(rec->ur_id1));
2411 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2412 tgt_dir_lockh, &update_mode,
2413 rec->ur_name, rec->ur_namelen - 1,
2414 MDS_INODELOCK_UPDATE);
2415 if (IS_ERR(de_tgt_dir))
2416 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2419 OBD_ALLOC(op_data, sizeof(*op_data));
2420 if (op_data == NULL)
2421 GOTO(cleanup, rc = -ENOMEM);
2423 memset(op_data, 0, sizeof(*op_data));
2424 op_data->id1 = *(rec->ur_id1);
2425 rc = md_link(mds->mds_md_exp, op_data, &request);
2426 OBD_FREE(op_data, sizeof(*op_data));
2429 ptlrpc_req_finished(request);
2435 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2437 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2438 if (IS_ERR(handle)) {
2439 rc = PTR_ERR(handle);
2445 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2446 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2447 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2448 id_fid(rec->ur_id1));
2451 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2452 handle, req, rc, 0);
2454 switch (cleanup_phase) {
2457 OBD_ALLOC(op_data, sizeof(*op_data));
2458 if (op_data != NULL) {
2460 memset(op_data, 0, sizeof(*op_data));
2462 op_data->id1 = *(rec->ur_id1);
2463 op_data->create_mode = rec->ur_mode;
2465 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2466 OBD_FREE(op_data, sizeof(*op_data));
2468 ptlrpc_req_finished(request);
2470 CERROR("error %d while dropping i_nlink on "
2471 "remote inode\n", rc);
2474 CERROR("rc %d prevented dropping i_nlink on "
2475 "remote inode\n", -ENOMEM);
2481 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2483 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2486 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2488 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2494 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2497 req->rq_status = rc;
2501 static int mds_reint_link(struct mds_update_record *rec, int offset,
2502 struct ptlrpc_request *req,
2503 struct lustre_handle *lh)
2505 struct obd_device *obd = req->rq_export->exp_obd;
2506 struct dentry *de_src = NULL;
2507 struct dentry *de_tgt_dir = NULL;
2508 struct dentry *dchild = NULL;
2509 struct mds_obd *mds = mds_req2mds(req);
2510 struct lustre_handle *handle = NULL;
2511 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2512 struct ldlm_res_id src_res_id = { .name = {0} };
2513 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2514 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2515 ldlm_policy_data_t tgt_dir_policy =
2516 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2517 int rc = 0, cleanup_phase = 0;
2519 int update_mode = 0;
2523 LASSERT(offset == 1);
2525 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2526 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2527 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2530 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2531 MD_COUNTER_INCREMENT(obd, link);
2533 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2534 GOTO(cleanup, rc = -ENOENT);
2536 if (id_group(rec->ur_id1) != mds->mds_num) {
2537 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2541 if (rec->ur_namelen == 1) {
2542 rc = mds_reint_link_acquire(rec, offset, req, lh);
2546 /* Step 1: Lookup the source inode and target directory by ID */
2547 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2549 GOTO(cleanup, rc = PTR_ERR(de_src));
2551 cleanup_phase = 1; /* source dentry */
2553 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2554 if (IS_ERR(de_tgt_dir)) {
2555 rc = PTR_ERR(de_tgt_dir);
2560 cleanup_phase = 2; /* target directory dentry */
2562 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2563 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2564 rec->ur_name, de_src->d_inode->i_ino);
2566 /* Step 2: Take the two locks */
2567 src_res_id.name[0] = id_fid(rec->ur_id1);
2568 src_res_id.name[1] = id_group(rec->ur_id1);
2569 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2570 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2573 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2575 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2577 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2578 tgt_dir_res_id, LDLM_IBITS,
2579 &src_policy, update_mode, &flags,
2581 ldlm_completion_ast, NULL, NULL,
2582 NULL, 0, NULL, tgt_dir_lockh + 1);
2584 GOTO(cleanup, rc = -ENOLCK);
2587 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2588 rec->ur_namelen - 1);
2589 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2590 (unsigned long)id_fid(rec->ur_id2),
2591 (unsigned long)id_group(rec->ur_id2),
2592 tgt_dir_res_id.name[2]);
2595 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2596 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2597 LCK_EX, &tgt_dir_policy);
2601 cleanup_phase = 3; /* locks */
2603 /* Step 3: Lookup the child */
2604 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2605 rec->ur_namelen - 1);
2606 if (IS_ERR(dchild)) {
2607 rc = PTR_ERR(dchild);
2608 if (rc != -EPERM && rc != -EACCES)
2609 CERROR("child lookup error %d\n", rc);
2613 cleanup_phase = 4; /* child dentry */
2615 if (dchild->d_inode) {
2616 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2617 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2622 /* Step 4: Do it. */
2623 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2625 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2626 if (IS_ERR(handle)) {
2627 rc = PTR_ERR(handle);
2631 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2632 if (rc && rc != -EPERM && rc != -EACCES)
2633 CERROR("vfs_link error %d\n", rc);
2635 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2636 handle, req, rc, 0);
2639 switch (cleanup_phase) {
2640 case 4: /* child dentry */
2644 ldlm_lock_decref(&src_lockh, LCK_EX);
2645 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2647 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2648 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2650 case 2: /* target dentry */
2652 if (tgt_dir_lockh[1].cookie && update_mode)
2653 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2657 case 1: /* source dentry */
2662 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2665 req->rq_status = rc;
2669 /* The idea here is that we need to get four locks in the end:
2670 * one on each parent directory, one on each child. We need to take
2671 * these locks in some kind of order (to avoid deadlocks), and the order
2672 * I selected is "increasing resource number" order. We need to look up
2673 * the children, however, before we know what the resource number(s) are.
2674 * Thus the following plan:
2676 * 1,2. Look up the parents
2677 * 3,4. Look up the children
2678 * 5. Take locks on the parents and children, in order
2679 * 6. Verify that the children haven't changed since they were looked up
2681 * If there was a race and the children changed since they were first looked
2682 * up, it is possible that mds_verify_child() will be able to just grab the
2683 * lock on the new child resource (if it has a higher resource than any other)
2684 * but we need to compare against not only its parent, but also against the
2685 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2687 * We need the fancy igrab() on the child inodes because we aren't holding a
2688 * lock on the parent after the lookup is done, so dentry->d_inode may change
2689 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2691 static int mds_get_parents_children_locked(struct obd_device *obd,
2692 struct mds_obd *mds,
2693 struct lustre_id *p1_id,
2694 struct dentry **de_srcdirp,
2695 struct lustre_id *p2_id,
2696 struct dentry **de_tgtdirp,
2698 const char *old_name, int old_len,
2699 struct dentry **de_oldp,
2700 const char *new_name, int new_len,
2701 struct dentry **de_newp,
2702 struct lustre_handle *dlm_handles,
2705 struct ldlm_res_id p1_res_id = { .name = {0} };
2706 struct ldlm_res_id p2_res_id = { .name = {0} };
2707 struct ldlm_res_id c1_res_id = { .name = {0} };
2708 struct ldlm_res_id c2_res_id = { .name = {0} };
2709 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2710 /* Only dentry should disappear, but the inode itself would be
2711 intact otherwise. */
2712 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2713 /* If something is going to be replaced, both dentry and inode locks are
2715 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2716 struct ldlm_res_id *maxres_src, *maxres_tgt;
2717 struct inode *inode;
2718 int rc = 0, cleanup_phase = 0;
2719 __u32 child_gen1 = 0;
2720 __u32 child_gen2 = 0;
2721 unsigned long child_ino1 = 0;
2722 unsigned long child_ino2 = 0;
2725 /* Step 1: Lookup the source directory */
2726 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2727 if (IS_ERR(*de_srcdirp))
2728 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2730 cleanup_phase = 1; /* source directory dentry */
2732 p1_res_id.name[0] = id_fid(p1_id);
2733 p1_res_id.name[1] = id_group(p1_id);
2735 /* Step 2: Lookup the target directory */
2736 if (id_equal_stc(p1_id, p2_id)) {
2737 *de_tgtdirp = dget(*de_srcdirp);
2739 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2740 if (IS_ERR(*de_tgtdirp)) {
2741 rc = PTR_ERR(*de_tgtdirp);
2747 cleanup_phase = 2; /* target directory dentry */
2749 p2_res_id.name[0] = id_fid(p2_id);
2750 p2_res_id.name[1] = id_group(p2_id);
2753 dlm_handles[5].cookie = 0;
2754 dlm_handles[6].cookie = 0;
2756 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2758 * get a temp lock on just fid, group to flush client cache and
2759 * to protect dirs from concurrent splitting.
2761 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2762 LCK_PW, &p_policy, &p2_res_id,
2763 &dlm_handles[6], LCK_PW, &p_policy);
2767 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2769 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2772 CDEBUG(D_INFO, "take locks on "
2773 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2774 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2775 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2780 /* Step 3: Lookup the source child entry */
2781 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2783 if (IS_ERR(*de_oldp)) {
2784 rc = PTR_ERR(*de_oldp);
2785 CERROR("old child lookup error (%.*s): %d\n",
2786 old_len - 1, old_name, rc);
2790 cleanup_phase = 4; /* original name dentry */
2792 inode = (*de_oldp)->d_inode;
2793 if (inode != NULL) {
2794 struct lustre_id sid;
2796 inode = igrab(inode);
2798 GOTO(cleanup, rc = -ENOENT);
2800 down(&inode->i_sem);
2801 rc = mds_read_inode_sid(obd, inode, &sid);
2804 CERROR("Can't read inode self id, inode %lu, "
2805 "rc %d\n", inode->i_ino, rc);
2810 child_ino1 = inode->i_ino;
2811 child_gen1 = inode->i_generation;
2812 c1_res_id.name[0] = id_fid(&sid);
2813 c1_res_id.name[1] = id_group(&sid);
2815 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2816 child_ino1 = (*de_oldp)->d_inum;
2817 child_gen1 = (*de_oldp)->d_generation;
2818 c1_res_id.name[0] = (*de_oldp)->d_fid;
2819 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2821 GOTO(cleanup, rc = -ENOENT);
2824 /* Step 4: Lookup the target child entry */
2825 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2827 if (IS_ERR(*de_newp)) {
2828 rc = PTR_ERR(*de_newp);
2829 CERROR("new child lookup error (%.*s): %d\n",
2830 old_len - 1, old_name, rc);
2834 cleanup_phase = 5; /* target dentry */
2836 inode = (*de_newp)->d_inode;
2837 if (inode != NULL) {
2838 struct lustre_id sid;
2840 inode = igrab(inode);
2844 down(&inode->i_sem);
2845 rc = mds_read_inode_sid(obd, inode, &sid);
2848 CERROR("Can't read inode self id, inode %lu, "
2849 "rc %d\n", inode->i_ino, rc);
2853 child_ino2 = inode->i_ino;
2854 child_gen2 = inode->i_generation;
2855 c2_res_id.name[0] = id_fid(&sid);
2856 c2_res_id.name[1] = id_group(&sid);
2858 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2859 child_ino2 = (*de_newp)->d_inum;
2860 child_gen2 = (*de_newp)->d_generation;
2861 c2_res_id.name[0] = (*de_newp)->d_fid;
2862 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2866 /* Step 5: Take locks on the parents and child(ren) */
2867 maxres_src = &p1_res_id;
2868 maxres_tgt = &p2_res_id;
2869 cleanup_phase = 5; /* target dentry */
2871 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2872 maxres_src = &c1_res_id;
2873 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2874 maxres_tgt = &c2_res_id;
2876 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2878 &p2_res_id, &dlm_handles[1], parent_mode,
2880 &c1_res_id, &dlm_handles[2], child_mode,
2882 &c2_res_id, &dlm_handles[3], child_mode,
2887 cleanup_phase = 6; /* parent and child(ren) locks */
2889 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2890 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2891 parent_mode, &c1_res_id, &dlm_handles[2],
2892 de_oldp, child_mode, &c1_policy, old_name, old_len,
2893 maxres_tgt, child_ino1, child_gen1);
2895 if (c2_res_id.name[0] != 0)
2896 ldlm_lock_decref(&dlm_handles[3], child_mode);
2897 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2904 if (!DENTRY_VALID(*de_oldp))
2905 GOTO(cleanup, rc = -ENOENT);
2907 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2908 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2909 parent_mode, &c2_res_id, &dlm_handles[3],
2910 de_newp, child_mode, &c2_policy, new_name,
2911 new_len, maxres_src, child_ino2, child_gen2);
2913 ldlm_lock_decref(&dlm_handles[2], child_mode);
2914 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2924 switch (cleanup_phase) {
2925 case 6: /* child lock(s) */
2926 if (c2_res_id.name[0] != 0)
2927 ldlm_lock_decref(&dlm_handles[3], child_mode);
2928 if (c1_res_id.name[0] != 0)
2929 ldlm_lock_decref(&dlm_handles[2], child_mode);
2930 if (dlm_handles[1].cookie != 0)
2931 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2932 if (dlm_handles[0].cookie != 0)
2933 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2934 case 5: /* target dentry */
2936 case 4: /* source dentry */
2940 if (dlm_handles[5].cookie != 0)
2941 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2942 if (dlm_handles[6].cookie != 0)
2943 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2945 case 2: /* target directory dentry */
2946 l_dput(*de_tgtdirp);
2947 case 1: /* source directry dentry */
2948 l_dput(*de_srcdirp);
2956 * checks if dentry can be removed. This function also handles cross-ref
2959 static int mds_check_for_rename(struct obd_device *obd,
2960 struct dentry *dentry)
2962 struct mds_obd *mds = &obd->u.mds;
2963 struct lustre_handle *rlockh;
2964 struct ptlrpc_request *req;
2965 struct mdc_op_data *op_data;
2966 struct lookup_intent it;
2967 int handle_size, rc = 0;
2970 LASSERT(dentry != NULL);
2972 if (dentry->d_inode) {
2973 if (S_ISDIR(dentry->d_inode->i_mode) &&
2974 !mds_is_dir_empty(obd, dentry))
2977 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2978 handle_size = sizeof(struct lustre_handle);
2980 OBD_ALLOC(rlockh, handle_size);
2984 memset(rlockh, 0, handle_size);
2985 OBD_ALLOC(op_data, sizeof(*op_data));
2986 if (op_data == NULL) {
2987 OBD_FREE(rlockh, handle_size);
2990 memset(op_data, 0, sizeof(*op_data));
2991 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
2993 it.it_op = IT_UNLINK;
2994 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
2997 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
2998 op_data, rlockh, NULL, 0, ldlm_completion_ast,
2999 mds_blocking_ast, NULL);
3000 OBD_FREE(op_data, sizeof(*op_data));
3004 OBD_FREE(it.d.fs_data,
3005 sizeof(struct lustre_intent_data));
3008 if (rlockh->cookie != 0)
3009 ldlm_lock_decref(rlockh, LCK_EX);
3011 if (LUSTRE_IT(&it)->it_data) {
3012 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3013 ptlrpc_req_finished(req);
3016 if (LUSTRE_IT(&it)->it_status)
3017 rc = LUSTRE_IT(&it)->it_status;
3018 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3019 OBD_FREE(rlockh, handle_size);
3024 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3025 struct ptlrpc_request *req, struct lustre_id *id,
3026 struct dentry *de_dir, struct dentry *de)
3028 struct obd_device *obd = req->rq_export->exp_obd;
3029 struct mds_obd *mds = mds_req2mds(req);
3030 void *handle = NULL;
3036 * name exists and points to local inode try to unlink this name
3037 * and create new one.
3039 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3040 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3041 (unsigned long)de->d_inode->i_generation);
3043 /* checking if we can remove local dentry. */
3044 rc = mds_check_for_rename(obd, de);
3048 handle = fsfilt_start(obd, de_dir->d_inode,
3049 FSFILT_OP_RENAME, NULL);
3051 GOTO(cleanup, rc = PTR_ERR(handle));
3052 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3055 } else if (de->d_flags & DCACHE_CROSS_REF) {
3056 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3057 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3058 (unsigned long)de->d_fid);
3060 /* checking if we can remove local dentry. */
3061 rc = mds_check_for_rename(obd, de);
3066 * to be fully POSIX compatible, we should add one more check:
3068 * if de_new is subdir of dir rec->ur_id1. If so - return
3071 * I do not know how to implement it right now, because
3072 * inodes/dentries for new and old names lie on different MDS,
3073 * so add this notice here just to make it visible for the rest
3074 * of developers and do not forget about. And when this check
3075 * will be added, del_cross_ref should gone, that is local
3076 * dentry is able to be removed if all checks passed. --umka
3079 handle = fsfilt_start(obd, de_dir->d_inode,
3080 FSFILT_OP_RENAME, NULL);
3082 GOTO(cleanup, rc = PTR_ERR(handle));
3083 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3087 /* name doesn't exist. the simplest case. */
3088 handle = fsfilt_start(obd, de_dir->d_inode,
3089 FSFILT_OP_LINK, NULL);
3091 GOTO(cleanup, rc = PTR_ERR(handle));
3094 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3095 rec->ur_tgtlen - 1, id_ino(id),
3096 id_gen(id), id_group(id), id_fid(id));
3098 CERROR("add_dir_entry() returned error %d\n", rc);
3104 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3105 handle, req, rc, 0);
3110 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3111 struct ptlrpc_request *req, struct dentry *de_dir,
3114 struct obd_device *obd = req->rq_export->exp_obd;
3115 struct mds_obd *mds = mds_req2mds(req);
3116 void *handle = NULL;
3120 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3122 GOTO(cleanup, rc = PTR_ERR(handle));
3123 rc = fsfilt_del_dir_entry(obd, de);
3128 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3129 handle, req, rc, 0);
3133 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3134 int offset, struct ptlrpc_request *req)
3136 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3137 struct obd_device *obd = req->rq_export->exp_obd;
3138 struct mds_obd *mds = mds_req2mds(req);
3139 struct lustre_handle child_lockh = {0};
3140 struct dentry *de_tgtdir = NULL;
3141 struct dentry *de_new = NULL;
3142 int cleanup_phase = 0;
3143 int update_mode, rc = 0;
3147 * another MDS executing rename operation has asked us to create target
3148 * name. such a creation should destroy existing target name.
3150 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3151 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3153 /* first, lookup the target */
3154 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3155 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3156 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3157 &child_lockh, &de_new, LCK_EX,
3158 MDS_INODELOCK_LOOKUP);
3165 LASSERT(de_tgtdir->d_inode);
3168 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3174 if (cleanup_phase == 1) {
3176 if (parent_lockh[1].cookie != 0)
3177 ldlm_lock_decref(parent_lockh + 1, update_mode);
3179 ldlm_lock_decref(parent_lockh, LCK_PW);
3180 if (child_lockh.cookie != 0)
3181 ldlm_lock_decref(&child_lockh, LCK_EX);
3186 req->rq_status = rc;
3190 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3191 struct ptlrpc_request *req)
3193 struct obd_device *obd = req->rq_export->exp_obd;
3194 struct ptlrpc_request *req2 = NULL;
3195 struct dentry *de_srcdir = NULL;
3196 struct dentry *de_old = NULL;
3197 struct mds_obd *mds = mds_req2mds(req);
3198 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3199 struct lustre_handle child_lockh = {0};
3200 struct mdc_op_data *op_data;
3201 int update_mode, rc = 0;
3204 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3205 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3207 OBD_ALLOC(op_data, sizeof(*op_data));
3208 if (op_data == NULL)
3210 memset(op_data, 0, sizeof(*op_data));
3212 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3213 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3214 &update_mode, rec->ur_name,
3215 rec->ur_namelen, &child_lockh, &de_old,
3216 LCK_EX, MDS_INODELOCK_LOOKUP);
3218 OBD_FREE(op_data, sizeof(*op_data));
3223 LASSERT(de_srcdir->d_inode);
3227 * we already know the target should be created on another MDS so, we
3228 * have to request that MDS to do it.
3231 /* prepare source id */
3232 if (de_old->d_flags & DCACHE_CROSS_REF) {
3233 LASSERT(de_old->d_inode == NULL);
3234 CDEBUG(D_OTHER, "request to move remote name\n");
3235 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3236 } else if (de_old->d_inode == NULL) {
3237 /* oh, source doesn't exist */
3238 OBD_FREE(op_data, sizeof(*op_data));
3239 GOTO(cleanup, rc = -ENOENT);
3241 struct lustre_id sid;
3242 struct inode *inode = de_old->d_inode;
3244 LASSERT(inode != NULL);
3245 CDEBUG(D_OTHER, "request to move local name\n");
3246 id_ino(&op_data->id1) = inode->i_ino;
3247 id_group(&op_data->id1) = mds->mds_num;
3248 id_gen(&op_data->id1) = inode->i_generation;
3250 down(&inode->i_sem);
3251 rc = mds_read_inode_sid(obd, inode, &sid);
3254 CERROR("Can't read inode self id, "
3255 "inode %lu, rc = %d\n",
3260 id_fid(&op_data->id1) = id_fid(&sid);
3263 op_data->id2 = *rec->ur_id2;
3264 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3265 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3266 OBD_FREE(op_data, sizeof(*op_data));
3271 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3277 ptlrpc_req_finished(req2);
3280 if (parent_lockh[1].cookie != 0)
3281 ldlm_lock_decref(parent_lockh + 1, update_mode);
3283 ldlm_lock_decref(parent_lockh, LCK_PW);
3284 if (child_lockh.cookie != 0)
3285 ldlm_lock_decref(&child_lockh, LCK_EX);
3290 req->rq_status = rc;
3294 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3295 struct ptlrpc_request *req, struct lustre_handle *lockh)
3297 struct obd_device *obd = req->rq_export->exp_obd;
3298 struct dentry *de_srcdir = NULL;
3299 struct dentry *de_tgtdir = NULL;
3300 struct dentry *de_old = NULL;
3301 struct dentry *de_new = NULL;
3302 struct inode *old_inode = NULL, *new_inode = NULL;
3303 struct mds_obd *mds = mds_req2mds(req);
3304 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3305 struct mds_body *body = NULL;
3306 struct llog_create_locks *lcl = NULL;
3307 struct lov_mds_md *lmm = NULL;
3308 int rc = 0, cleanup_phase = 0;
3309 void *handle = NULL;
3312 LASSERT(offset == 1);
3314 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3315 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3318 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3320 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3321 DEBUG_REQ(D_HA, req, "rename replay");
3322 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3323 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3324 req->rq_repmsg->buflens[2]);
3327 MD_COUNTER_INCREMENT(obd, rename);
3329 if (rec->ur_namelen == 1) {
3330 rc = mds_reint_rename_create_name(rec, offset, req);
3334 /* check if new name should be located on remote target. */
3335 if (id_group(rec->ur_id2) != mds->mds_num) {
3336 rc = mds_reint_rename_to_remote(rec, offset, req);
3340 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3341 rec->ur_id2, &de_tgtdir, LCK_PW,
3342 rec->ur_name, rec->ur_namelen,
3343 &de_old, rec->ur_tgt,
3344 rec->ur_tgtlen, &de_new,
3345 dlm_handles, LCK_EX);
3349 cleanup_phase = 1; /* parent(s), children, locks */
3350 old_inode = de_old->d_inode;
3351 new_inode = de_new->d_inode;
3353 /* sanity check for src inode */
3354 if (de_old->d_flags & DCACHE_CROSS_REF) {
3355 LASSERT(de_old->d_inode == NULL);
3358 * in the case of cross-ref dir, we can perform this check only
3359 * if child and parent lie on the same mds. This is because
3360 * otherwise they can have the same inode numbers.
3362 if (de_old->d_mdsnum == mds->mds_num) {
3363 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3364 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3365 GOTO(cleanup, rc = -EINVAL);
3368 LASSERT(de_old->d_inode != NULL);
3369 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3370 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3371 GOTO(cleanup, rc = -EINVAL);
3374 /* sanity check for dest inode */
3375 if (de_new->d_flags & DCACHE_CROSS_REF) {
3376 LASSERT(new_inode == NULL);
3378 /* the same check about target dentry. */
3379 if (de_new->d_mdsnum == mds->mds_num) {
3380 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3381 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3382 GOTO(cleanup, rc = -EINVAL);
3386 * regular files usualy do not have ->rename() implemented. But
3387 * we handle only this case when @de_new is cross-ref entry,
3388 * because in other cases it will be handled by vfs_rename().
3390 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3391 !de_old->d_inode->i_op->rename))
3392 GOTO(cleanup, rc = -EPERM);
3395 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3396 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3397 GOTO(cleanup, rc = -EINVAL);
3402 * check if inodes point to each other. This should be checked before
3403 * is_subdir() check, as for the same entries it will think that they
3406 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3407 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3408 old_inode == new_inode)
3409 GOTO(cleanup, rc = 0);
3411 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3413 * check if we are moving old entry into its child. 2.6 does not check
3414 * for this in vfs_rename() anymore.
3416 if (is_subdir(de_new, de_old))
3417 GOTO(cleanup, rc = -EINVAL);
3421 * if we are about to remove the target at first, pass the EA of that
3422 * inode to client to perform and cleanup on OST.
3424 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3425 LASSERT(body != NULL);
3427 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3429 DOWN_READ_I_ALLOC_SEM(new_inode);
3431 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3433 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3434 new_inode->i_nlink == 2) ||
3435 new_inode->i_nlink == 1)) {
3436 if (mds_orphan_open_count(new_inode) > 0) {
3437 /* need to lock pending_dir before transaction */
3438 down(&mds->mds_pending_dir->d_inode->i_sem);
3439 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3440 } else if (S_ISREG(new_inode->i_mode)) {
3441 mds_pack_inode2body(obd, body, new_inode, 0);
3442 mds_pack_md(obd, req->rq_repmsg, 1, body,
3443 new_inode, MDS_PACK_MD_LOCK, 0);
3447 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3448 de_srcdir->d_inode->i_sb);
3450 if (de_old->d_flags & DCACHE_CROSS_REF) {
3451 struct lustre_id old_id;
3453 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3455 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3460 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3465 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3466 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3467 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3470 GOTO(cleanup, rc = PTR_ERR(handle));
3473 de_old->d_fsdata = req;
3474 de_new->d_fsdata = req;
3475 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3478 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3479 if (mds_orphan_open_count(new_inode) > 0)
3480 rc = mds_orphan_add_link(rec, obd, de_new);
3483 GOTO(cleanup, rc = 0);
3485 if (!S_ISREG(new_inode->i_mode))
3488 if (!(body->valid & OBD_MD_FLEASIZE)) {
3489 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3490 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3491 } else if (mds_log_op_unlink(obd, new_inode,
3492 lustre_msg_buf(req->rq_repmsg,1,0),
3493 req->rq_repmsg->buflens[1],
3494 lustre_msg_buf(req->rq_repmsg,2,0),
3495 req->rq_repmsg->buflens[2],
3497 body->valid |= OBD_MD_FLCOOKIE;
3500 rc = mds_destroy_objects(obd, old_inode, 1);
3502 CERROR("can't remove OST object, err %d\n",
3509 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3510 handle, req, rc, 0);
3512 switch (cleanup_phase) {
3514 up(&mds->mds_pending_dir->d_inode->i_sem);
3517 UP_READ_I_ALLOC_SEM(new_inode);
3520 if (dlm_handles[5].cookie != 0)
3521 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3522 if (dlm_handles[6].cookie != 0)
3523 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3526 ptlrpc_save_llog_lock(req, lcl);
3529 if (dlm_handles[3].cookie != 0)
3530 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3531 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3532 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3533 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3535 if (dlm_handles[3].cookie != 0)
3536 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3537 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3538 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3539 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3548 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3551 req->rq_status = rc;
3555 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3556 struct ptlrpc_request *, struct lustre_handle *);
3558 static mds_reinter reinters[REINT_MAX + 1] = {
3559 [REINT_SETATTR] mds_reint_setattr,
3560 [REINT_CREATE] mds_reint_create,
3561 [REINT_LINK] mds_reint_link,
3562 [REINT_UNLINK] mds_reint_unlink,
3563 [REINT_RENAME] mds_reint_rename,
3564 [REINT_OPEN] mds_open
3567 int mds_reint_rec(struct mds_update_record *rec, int offset,
3568 struct ptlrpc_request *req, struct lustre_handle *lockh)
3570 struct obd_device *obd = req->rq_export->exp_obd;
3571 struct lvfs_run_ctxt saved;
3574 /* checked by unpacker */
3575 LASSERT(rec->ur_opcode <= REINT_MAX &&
3576 reinters[rec->ur_opcode] != NULL);
3578 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3579 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3580 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);