1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include "mds_internal.h"
50 struct mds_logcancel_data {
51 struct lov_mds_md *mlcd_lmm;
55 struct llog_cookie mlcd_cookies[0];
58 static void mds_cancel_cookies_cb(struct obd_device *obd,
59 __u64 transno, void *cb_data,
62 struct mds_logcancel_data *mlcd = cb_data;
63 struct lov_stripe_md *lsm = NULL;
64 struct llog_ctxt *ctxt;
67 obd_transno_commit_cb(obd, transno, error);
69 CDEBUG(D_HA, "cancelling %d cookies\n",
70 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
72 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
73 mlcd->mlcd_eadatalen);
75 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
76 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
79 ///* XXX 0 normally, SENDNOW for debug */);
80 ctxt = llog_get_context(&obd->obd_llogs,
81 mlcd->mlcd_cookies[0].lgc_subsys + 1);
82 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
83 sizeof(*mlcd->mlcd_cookies),
84 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
86 CERROR("error cancelling %d log cookies: rc %d\n",
87 (int)(mlcd->mlcd_cookielen /
88 sizeof(*mlcd->mlcd_cookies)), rc);
89 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
92 OBD_FREE(mlcd, mlcd->mlcd_size);
95 /* Assumes caller has already pushed us into the kernel context. */
96 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
97 struct ptlrpc_request *req, int rc, __u32 op_data)
99 struct mds_export_data *med = &req->rq_export->exp_mds_data;
100 struct obd_device *obd = req->rq_export->exp_obd;
101 struct mds_client_data *mcd = med->med_mcd;
102 int err, log_pri = D_HA;
107 /* if the export has already been failed, we have no last_rcvd slot */
108 if (req->rq_export->exp_failed) {
109 CERROR("committing transaction for disconnected client\n");
111 GOTO(out_commit, rc);
118 if (handle == NULL) {
119 /* if we're starting our own xaction, use our own inode */
120 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
121 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
122 if (IS_ERR(handle)) {
123 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
124 RETURN(PTR_ERR(handle));
130 transno = req->rq_reqmsg->transno;
132 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
133 } else if (transno == 0) {
134 spin_lock(&mds->mds_transno_lock);
135 transno = ++mds->mds_last_transno;
136 spin_unlock(&mds->mds_transno_lock);
138 spin_lock(&mds->mds_transno_lock);
139 if (transno > mds->mds_last_transno)
140 mds->mds_last_transno = transno;
141 spin_unlock(&mds->mds_transno_lock);
143 req->rq_repmsg->transno = req->rq_transno = transno;
144 if (req->rq_reqmsg->opc == MDS_CLOSE) {
145 mcd->mcd_last_close_transno = cpu_to_le64(transno);
146 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
147 mcd->mcd_last_close_result = cpu_to_le32(rc);
148 mcd->mcd_last_close_data = cpu_to_le32(op_data);
150 mcd->mcd_last_transno = cpu_to_le64(transno);
151 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
152 mcd->mcd_last_result = cpu_to_le32(rc);
153 mcd->mcd_last_data = cpu_to_le32(op_data);
156 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
157 mds_commit_last_transno_cb, NULL);
159 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
160 sizeof(*mcd), &off, 0);
168 DEBUG_REQ(log_pri, req,
169 "wrote trans #"LPU64" client %s at idx %u: err = %d",
170 transno, mcd->mcd_uuid, med->med_idx, err);
172 err = mds_update_last_fid(obd, handle, 0);
179 err = mds_dt_write_objids(obd);
185 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
189 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
190 req->rq_export->exp_sync);
192 CERROR("error committing transaction: %d\n", err);
200 /* this gives the same functionality as the code between
201 * sys_chmod and inode_setattr
202 * chown_common and inode_setattr
203 * utimes and inode_setattr
206 /* Just for the case if we have some clients that know about ATTR_RAW */
207 #define ATTR_RAW 8192
209 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
211 time_t now = LTIME_S(CURRENT_TIME);
212 struct iattr *attr = &rec->ur_iattr;
213 unsigned int ia_valid = attr->ia_valid;
217 /* only fix up attrs if the client VFS didn't already */
219 if (!(ia_valid & ATTR_RAW))
222 if (!(ia_valid & ATTR_CTIME_SET))
223 LTIME_S(attr->ia_ctime) = now;
224 if (!(ia_valid & ATTR_ATIME_SET))
225 LTIME_S(attr->ia_atime) = now;
226 if (!(ia_valid & ATTR_MTIME_SET))
227 LTIME_S(attr->ia_mtime) = now;
229 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
233 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
234 if (rec->ur_fsuid != inode->i_uid &&
235 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
239 if (ia_valid & ATTR_SIZE) {
240 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
244 if (ia_valid & ATTR_UID) {
247 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
249 if (attr->ia_uid == (uid_t) -1)
250 attr->ia_uid = inode->i_uid;
251 if (attr->ia_gid == (gid_t) -1)
252 attr->ia_gid = inode->i_gid;
253 attr->ia_mode = inode->i_mode;
255 * If the user or group of a non-directory has been
256 * changed by a non-root user, remove the setuid bit.
257 * 19981026 David C Niemi <niemi@tux.org>
259 * Changed this to apply to all users, including root,
260 * to avoid some races. This is the behavior we had in
261 * 2.0. The check for non-root was definitely wrong
262 * for 2.2 anyway, as it should have been using
263 * CAP_FSETID rather than fsuid -- 19990830 SD.
265 if ((inode->i_mode & S_ISUID) == S_ISUID &&
266 !S_ISDIR(inode->i_mode)) {
267 attr->ia_mode &= ~S_ISUID;
268 attr->ia_valid |= ATTR_MODE;
271 * Likewise, if the user or group of a non-directory
272 * has been changed by a non-root user, remove the
273 * setgid bit UNLESS there is no group execute bit
274 * (this would be a file marked for mandatory
275 * locking). 19981026 David C Niemi <niemi@tux.org>
277 * Removed the fsuid check (see the comment above) --
280 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
281 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
282 attr->ia_mode &= ~S_ISGID;
283 attr->ia_valid |= ATTR_MODE;
285 } else if (ia_valid & ATTR_MODE) {
286 int mode = attr->ia_mode;
288 if (attr->ia_mode == (mode_t) -1)
289 attr->ia_mode = inode->i_mode;
291 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
296 void mds_steal_ack_locks(struct ptlrpc_request *req)
298 struct obd_export *exp = req->rq_export;
299 char str[PTL_NALFMT_SIZE];
300 struct list_head *tmp;
301 struct ptlrpc_reply_state *oldrep;
302 struct ptlrpc_service *svc;
303 struct llog_create_locks *lcl;
307 /* CAVEAT EMPTOR: spinlock order */
308 spin_lock_irqsave (&exp->exp_lock, flags);
309 list_for_each (tmp, &exp->exp_outstanding_replies) {
310 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
312 if (oldrep->rs_xid != req->rq_xid)
315 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
316 CERROR ("Resent req xid "LPX64" has mismatched opc: "
317 "new %d old %d\n", req->rq_xid,
318 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
320 svc = oldrep->rs_srv_ni->sni_service;
321 spin_lock (&svc->srv_lock);
323 list_del_init (&oldrep->rs_exp_list);
325 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
326 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
327 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
328 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
330 for (i = 0; i < oldrep->rs_nlocks; i++)
331 ptlrpc_save_lock(req,
332 &oldrep->rs_locks[i],
333 oldrep->rs_modes[i]);
334 oldrep->rs_nlocks = 0;
336 lcl = oldrep->rs_llog_locks;
337 oldrep->rs_llog_locks = NULL;
339 ptlrpc_save_llog_lock(req, lcl);
341 DEBUG_REQ(D_HA, req, "stole locks for");
342 ptlrpc_schedule_difficult_reply (oldrep);
344 spin_unlock (&svc->srv_lock);
345 spin_unlock_irqrestore (&exp->exp_lock, flags);
348 spin_unlock_irqrestore (&exp->exp_lock, flags);
351 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
353 if (req->rq_reqmsg->opc == MDS_CLOSE) {
354 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
355 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
356 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
357 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
359 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
360 mcd->mcd_last_transno, mcd->mcd_last_result);
361 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
362 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
365 mds_steal_ack_locks(req);
368 static void reconstruct_reint_setattr(struct mds_update_record *rec,
369 int offset, struct ptlrpc_request *req)
371 struct mds_export_data *med = &req->rq_export->exp_mds_data;
372 struct mds_body *body;
375 mds_req_from_mcd(req, med->med_mcd);
377 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
379 LASSERT(PTR_ERR(de) == req->rq_status);
383 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
384 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
386 /* Don't return OST-specific attributes if we didn't just set them */
387 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
388 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
389 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
390 body->valid |= OBD_MD_FLMTIME;
391 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
392 body->valid |= OBD_MD_FLATIME;
397 /* In the raw-setattr case, we lock the child inode.
398 * In the write-back case or if being called from open, the client holds a lock
401 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
402 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
403 struct ptlrpc_request *req, struct lustre_handle *lh)
405 struct mds_obd *mds = mds_req2mds(req);
406 struct obd_device *obd = req->rq_export->exp_obd;
407 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
408 struct mds_body *body;
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,
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(name, namelen - 1);
1570 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1571 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1572 parent_res_id.name[2]);
1576 cleanup_phase = 1; /* parent dentry */
1578 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1579 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1580 if (IS_ERR(*dchildp)) {
1581 rc = PTR_ERR(*dchildp);
1582 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1586 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1588 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1589 * lock. Drop possible UPDATE lock!
1591 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1592 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1594 child_res_id.name[0] = (*dchildp)->d_fid;
1595 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1596 child_ino = (*dchildp)->d_inum;
1597 child_gen = (*dchildp)->d_generation;
1601 inode = (*dchildp)->d_inode;
1603 inode = igrab(inode);
1607 down(&inode->i_sem);
1608 rc = mds_read_inode_sid(obd, inode, &sid);
1611 CERROR("Can't read inode self id, inode %lu, "
1612 "rc %d\n", inode->i_ino, rc);
1617 child_ino = inode->i_ino;
1618 child_gen = inode->i_generation;
1619 child_res_id.name[0] = id_fid(&sid);
1620 child_res_id.name[1] = id_group(&sid);
1624 cleanup_phase = 2; /* child dentry */
1626 /* Step 3: Lock parent and child in resource order. If child doesn't
1627 * exist, we still have to lock the parent and re-lookup. */
1628 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1629 &parent_policy, &child_res_id, child_lockh,
1630 child_mode, &child_policy);
1634 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1635 cleanup_phase = 4; /* child lock */
1637 cleanup_phase = 3; /* parent lock */
1639 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1640 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1641 parent_mode, &child_res_id, child_lockh,
1642 dchildp, child_mode, &child_policy,
1643 name, namelen, &parent_res_id, child_ino,
1655 switch (cleanup_phase) {
1657 ldlm_lock_decref(child_lockh, child_mode);
1659 ldlm_lock_decref(parent_lockh, parent_mode);
1664 if (parent_lockh[1].cookie)
1665 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1673 void mds_reconstruct_generic(struct ptlrpc_request *req)
1675 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1676 mds_req_from_mcd(req, med->med_mcd);
1679 /* If we are unlinking an open file/dir (i.e. creating an orphan) then
1680 * we instead link the inode into the PENDING directory until it is
1681 * finally released. We can't simply call mds_reint_rename() or some
1682 * part thereof, because we don't have the inode to check for link
1683 * count/open status until after it is locked.
1685 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1686 * before starting journal transaction.
1688 * returns 1 on success
1689 * returns 0 if we lost a race and didn't make a new link
1690 * returns negative on error
1692 static int mds_orphan_add_link(struct mds_update_record *rec,
1693 struct obd_device *obd, struct dentry *dentry)
1695 struct mds_obd *mds = &obd->u.mds;
1696 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1697 struct inode *inode = dentry->d_inode;
1698 struct dentry *pending_child;
1699 char idname[LL_ID_NAMELEN];
1700 int idlen = 0, rc, mode;
1703 LASSERT(inode != NULL);
1704 LASSERT(!mds_inode_is_orphan(inode));
1705 #ifndef HAVE_I_ALLOC_SEM
1706 LASSERT(down_trylock(&inode->i_sem) != 0);
1708 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1710 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1712 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1713 mds_orphan_open_count(inode), inode->i_nlink,
1714 S_ISDIR(inode->i_mode) ? "dir" :
1715 S_ISREG(inode->i_mode) ? "file" : "other",
1716 rec->ur_name, idname);
1718 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1721 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1722 if (IS_ERR(pending_child))
1723 RETURN(PTR_ERR(pending_child));
1725 if (pending_child->d_inode != NULL) {
1726 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1727 LASSERT(pending_child->d_inode == inode);
1728 GOTO(out_dput, rc = 0);
1731 /* link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG
1732 * for linking and return real mode back then -bzzz */
1733 mode = inode->i_mode;
1734 inode->i_mode = S_IFREG;
1735 rc = vfs_link(dentry, pending_dir, pending_child);
1737 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1740 mds_inode_set_orphan(inode);
1742 /* return mode and correct i_nlink if inode is directory */
1743 inode->i_mode = mode;
1744 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1745 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1747 if (S_ISDIR(mode)) {
1749 pending_dir->i_nlink++;
1750 mark_inode_dirty(inode);
1751 mark_inode_dirty(pending_dir);
1756 l_dput(pending_child);
1760 int mds_create_local_dentry(struct mds_update_record *rec,
1761 struct obd_device *obd)
1763 struct mds_obd *mds = &obd->u.mds;
1764 struct inode *id_dir = mds->mds_id_dir->d_inode;
1765 int idlen = 0, rc, cleanup_phase = 0;
1766 struct dentry *new_child = NULL;
1767 char *idname = rec->ur_name;
1768 struct dentry *child = NULL;
1769 struct lustre_handle lockh[2] = {{0}, {0}};
1770 struct lustre_id sid;
1774 down(&id_dir->i_sem);
1775 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1776 id_gen(rec->ur_id1));
1778 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1779 idname, OLID4(rec->ur_id1));
1781 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1784 if (IS_ERR(new_child)) {
1785 CERROR("can't lookup %s: %d\n", idname,
1786 (int) PTR_ERR(new_child));
1787 GOTO(cleanup, rc = PTR_ERR(new_child));
1791 down(&id_dir->i_sem);
1792 rc = mds_read_inode_sid(obd, id_dir, &sid);
1795 CERROR("Can't read inode self id, inode %lu, "
1796 "rc %d\n", id_dir->i_ino, rc);
1800 if (new_child->d_inode != NULL) {
1801 /* nice. we've already have local dentry! */
1802 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1803 (unsigned)new_child->d_inode->i_ino,
1804 (unsigned)new_child->d_inode->i_generation);
1806 id_ino(rec->ur_id1) = id_dir->i_ino;
1807 id_gen(rec->ur_id1) = id_dir->i_generation;
1808 rec->ur_namelen = idlen + 1;
1810 id_fid(rec->ur_id1) = id_fid(&sid);
1811 id_group(rec->ur_id1) = id_group(&sid);
1813 GOTO(cleanup, rc = 0);
1816 /* new, local dentry will be added soon. we need no aliases here */
1819 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1820 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1822 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1823 LCK_EX, lockh, NULL, NULL, 0,
1824 MDS_INODELOCK_UPDATE);
1827 if (IS_ERR(child)) {
1828 rc = PTR_ERR(child);
1829 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1830 CERROR("can't get victim: %d\n", rc);
1835 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1837 GOTO(cleanup, rc = PTR_ERR(handle));
1839 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1840 idlen, id_ino(rec->ur_id1),
1841 id_gen(rec->ur_id1), mds->mds_num,
1842 id_fid(rec->ur_id1));
1844 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1845 (unsigned long)child->d_inode->i_ino,
1846 (unsigned long)child->d_inode->i_generation, rc);
1848 if (S_ISDIR(child->d_inode->i_mode)) {
1850 mark_inode_dirty(id_dir);
1852 mark_inode_dirty(child->d_inode);
1854 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1856 id_ino(rec->ur_id1) = id_dir->i_ino;
1857 id_gen(rec->ur_id1) = id_dir->i_generation;
1858 rec->ur_namelen = idlen + 1;
1860 id_fid(rec->ur_id1) = id_fid(&sid);
1861 id_group(rec->ur_id1) = id_group(&sid);
1865 switch(cleanup_phase) {
1867 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1868 ldlm_lock_decref(lockh, LCK_EX);
1878 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1879 struct ptlrpc_request *slave)
1881 void *cookie, *cookie2;
1882 struct mds_body *body2;
1883 struct mds_body *body;
1887 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1888 LASSERT(body != NULL);
1890 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1891 LASSERT(body2 != NULL);
1893 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1896 memcpy(body2, body, sizeof(*body));
1897 body2->valid &= ~OBD_MD_FLCOOKIE;
1899 if (!(body->valid & OBD_MD_FLEASIZE) &&
1900 !(body->valid & OBD_MD_FLDIREA))
1903 if (body->eadatasize == 0) {
1904 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1908 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1910 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1911 LASSERT(ea != NULL);
1913 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1914 LASSERT(ea2 != NULL);
1916 memcpy(ea2, ea, body->eadatasize);
1918 if (body->valid & OBD_MD_FLCOOKIE) {
1919 LASSERT(master->rq_repmsg->buflens[2] >=
1920 slave->rq_repmsg->buflens[2]);
1921 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1922 slave->rq_repmsg->buflens[2]);
1923 LASSERT(cookie != NULL);
1925 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1926 master->rq_repmsg->buflens[2]);
1927 LASSERT(cookie2 != NULL);
1928 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1929 body2->valid |= OBD_MD_FLCOOKIE;
1934 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1935 int offset, struct ptlrpc_request *req,
1936 struct lustre_handle *parent_lockh,
1937 int update_mode, struct dentry *dparent,
1938 struct lustre_handle *child_lockh,
1939 struct dentry *dchild)
1941 struct obd_device *obd = req->rq_export->exp_obd;
1942 struct mds_obd *mds = mds_req2mds(req);
1943 struct ptlrpc_request *request = NULL;
1944 int rc = 0, cleanup_phase = 0;
1945 struct mdc_op_data *op_data;
1949 LASSERT(offset == 1 || offset == 3);
1951 /* time to drop i_nlink on remote MDS */
1952 OBD_ALLOC(op_data, sizeof(*op_data));
1953 if (op_data == NULL)
1956 memset(op_data, 0, sizeof(*op_data));
1957 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
1958 op_data->create_mode = rec->ur_mode;
1960 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
1961 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1963 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1964 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
1965 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
1968 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1969 op_data->create_mode |= MDS_MODE_REPLAY;
1971 rc = md_unlink(mds->mds_md_exp, op_data, &request);
1972 OBD_FREE(op_data, sizeof(*op_data));
1977 mds_copy_unlink_reply(req, request);
1978 ptlrpc_req_finished(request);
1982 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
1985 GOTO(cleanup, rc = PTR_ERR(handle));
1986 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
1987 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
1992 req->rq_status = rc;
1995 if (parent_lockh[1].cookie != 0)
1996 ldlm_lock_decref(parent_lockh + 1, update_mode);
1998 ldlm_lock_decref(child_lockh, LCK_EX);
2000 ldlm_lock_decref(parent_lockh, LCK_PW);
2002 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2009 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2010 struct ptlrpc_request *req,
2011 struct lustre_handle *lh)
2013 struct dentry *dparent = NULL, *dchild;
2014 struct mds_obd *mds = mds_req2mds(req);
2015 struct obd_device *obd = req->rq_export->exp_obd;
2016 struct mds_body *body = NULL;
2017 struct inode *child_inode = NULL;
2018 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2019 struct lustre_handle child_lockh = {0};
2020 struct lustre_handle child_reuse_lockh = {0};
2021 struct lustre_handle *slave_lockh = NULL;
2022 char idname[LL_ID_NAMELEN];
2023 struct llog_create_locks *lcl = NULL;
2024 void *handle = NULL;
2025 int rc = 0, cleanup_phase = 0;
2026 int unlink_by_id = 0;
2030 LASSERT(offset == 1 || offset == 3);
2032 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2033 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2036 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2038 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2039 DEBUG_REQ(D_HA, req, "unlink replay");
2040 LASSERT(offset == 1); /* should not come from intent */
2041 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2042 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2043 req->rq_repmsg->buflens[2]);
2046 MD_COUNTER_INCREMENT(obd, unlink);
2048 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2049 GOTO(cleanup, rc = -ENOENT);
2051 if (rec->ur_namelen == 1) {
2052 /* this is request to drop i_nlink on local inode */
2054 rec->ur_name = idname;
2055 rc = mds_create_local_dentry(rec, obd);
2056 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2057 DEBUG_REQ(D_HA, req,
2058 "drop nlink on inode "DLID4" (replay)",
2059 OLID4(rec->ur_id1));
2065 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2066 /* master mds for directory asks slave removing inode is already
2068 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2069 LCK_PW, parent_lockh,
2070 &update_mode, rec->ur_name,
2072 MDS_INODELOCK_UPDATE);
2073 if (IS_ERR(dparent))
2074 GOTO(cleanup, rc = PTR_ERR(dparent));
2075 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2076 rec->ur_namelen - 1);
2078 GOTO(cleanup, rc = PTR_ERR(dchild));
2079 child_lockh.cookie = 0;
2080 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2081 LASSERT(dchild->d_inode != NULL);
2082 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2084 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2085 parent_lockh, &dparent,
2086 LCK_PW, MDS_INODELOCK_UPDATE,
2087 &update_mode, rec->ur_name,
2088 rec->ur_namelen, &child_lockh,
2090 MDS_INODELOCK_LOOKUP |
2091 MDS_INODELOCK_UPDATE);
2096 if (dchild->d_flags & DCACHE_CROSS_REF) {
2097 /* we should have parent lock only here */
2098 LASSERT(unlink_by_id == 0);
2099 LASSERT(dchild->d_mdsnum != mds->mds_num);
2100 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2101 update_mode, dparent, &child_lockh, dchild);
2105 cleanup_phase = 1; /* dchild, dparent, locks */
2108 child_inode = dchild->d_inode;
2109 if (child_inode == NULL) {
2110 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2111 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2112 GOTO(cleanup, rc = -ENOENT);
2115 cleanup_phase = 2; /* dchild has a lock */
2117 /* We have to do these checks ourselves, in case we are making an
2118 * orphan. The client tells us whether rmdir() or unlink() was called,
2119 * so we need to return appropriate errors (bug 72).
2121 * We don't have to check permissions, because vfs_rename (called from
2122 * mds_open_unlink_rename) also calls may_delete. */
2123 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2124 if (!S_ISDIR(child_inode->i_mode))
2125 GOTO(cleanup, rc = -ENOTDIR);
2127 if (S_ISDIR(child_inode->i_mode))
2128 GOTO(cleanup, rc = -EISDIR);
2131 /* handle splitted dir */
2132 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2136 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2137 * reuse (see bug 2029). */
2138 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2141 cleanup_phase = 3; /* child inum lock */
2143 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2145 /* ldlm_reply in buf[0] if called via intent */
2151 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2152 LASSERT(body != NULL);
2154 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2155 DOWN_READ_I_ALLOC_SEM(child_inode);
2156 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2158 /* If this is potentially the last reference to this inode, get the
2159 * OBD EA data first so the client can destroy OST objects. We
2160 * only do the object removal later if no open files/links remain. */
2161 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2162 child_inode->i_nlink == 1) {
2163 if (mds_orphan_open_count(child_inode) > 0) {
2164 /* need to lock pending_dir before transaction */
2165 down(&mds->mds_pending_dir->d_inode->i_sem);
2166 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2167 } else if (S_ISREG(child_inode->i_mode)) {
2168 mds_pack_inode2body(obd, body, child_inode, 0);
2169 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2170 body, child_inode, MDS_PACK_MD_LOCK, 0);
2174 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2175 switch (child_inode->i_mode & S_IFMT) {
2177 /* Drop any lingering child directories before we start our
2178 * transaction, to avoid doing multiple inode dirty/delete
2179 * in our compound transaction (bug 1321). */
2180 shrink_dcache_parent(dchild);
2181 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2184 GOTO(cleanup, rc = PTR_ERR(handle));
2185 rc = vfs_rmdir(dparent->d_inode, dchild);
2188 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2189 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2191 handle = fsfilt_start_log(obd, dparent->d_inode,
2192 FSFILT_OP_UNLINK, NULL,
2193 le32_to_cpu(lmm->lmm_stripe_count));
2195 GOTO(cleanup, rc = PTR_ERR(handle));
2196 rc = vfs_unlink(dparent->d_inode, dchild);
2204 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2207 GOTO(cleanup, rc = PTR_ERR(handle));
2208 rc = vfs_unlink(dparent->d_inode, dchild);
2211 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2214 GOTO(cleanup, rc = -EINVAL);
2217 if (rc == 0 && child_inode->i_nlink == 0) {
2218 if (mds_orphan_open_count(child_inode) > 0)
2219 rc = mds_orphan_add_link(rec, obd, dchild);
2222 GOTO(cleanup, rc = 0);
2224 if (!S_ISREG(child_inode->i_mode))
2227 if (!(body->valid & OBD_MD_FLEASIZE)) {
2228 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2229 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2230 } else if (mds_log_op_unlink(obd, child_inode,
2231 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2232 req->rq_repmsg->buflens[offset + 1],
2233 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2234 req->rq_repmsg->buflens[offset+2],
2236 body->valid |= OBD_MD_FLCOOKIE;
2247 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2248 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2249 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2251 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2253 CERROR("error on parent setattr: rc = %d\n", err);
2255 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2256 handle, req, rc, 0);
2258 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2259 "unlinked", 0, NULL);
2260 switch(cleanup_phase) {
2261 case 5: /* pending_dir semaphore */
2262 up(&mds->mds_pending_dir->d_inode->i_sem);
2263 case 4: /* child inode semaphore */
2264 UP_READ_I_ALLOC_SEM(child_inode);
2265 /* handle splitted dir */
2267 /* master directory can be non-empty or something else ... */
2268 mds_unlink_slave_objs(obd, dchild);
2271 ptlrpc_save_llog_lock(req, lcl);
2272 case 3: /* child ino-reuse lock */
2273 if (rc && body != NULL) {
2274 // Don't unlink the OST objects if the MDS unlink failed
2278 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2280 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2281 case 2: /* child lock */
2282 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2283 if (child_lockh.cookie)
2284 ldlm_lock_decref(&child_lockh, LCK_EX);
2285 case 1: /* child and parent dentry, parent lock */
2287 if (parent_lockh[1].cookie != 0)
2288 ldlm_lock_decref(parent_lockh + 1, update_mode);
2291 ldlm_lock_decref(parent_lockh, LCK_PW);
2293 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2300 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2303 req->rq_status = rc;
2308 * to service requests from remote MDS to increment i_nlink
2310 static int mds_reint_link_acquire(struct mds_update_record *rec,
2311 int offset, struct ptlrpc_request *req,
2312 struct lustre_handle *lh)
2314 struct obd_device *obd = req->rq_export->exp_obd;
2315 struct ldlm_res_id src_res_id = { .name = {0} };
2316 struct lustre_handle *handle = NULL, src_lockh = {0};
2317 struct mds_obd *mds = mds_req2mds(req);
2318 int rc = 0, cleanup_phase = 0;
2319 struct dentry *de_src = NULL;
2320 ldlm_policy_data_t policy;
2324 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2325 obd->obd_name, OLID4(rec->ur_id1));
2327 /* Step 1: Lookup the source inode and target directory by ID */
2328 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2330 GOTO(cleanup, rc = PTR_ERR(de_src));
2331 cleanup_phase = 1; /* source dentry */
2333 src_res_id.name[0] = id_fid(rec->ur_id1);
2334 src_res_id.name[1] = id_group(rec->ur_id1);
2335 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2337 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2338 src_res_id, LDLM_IBITS, &policy,
2339 LCK_EX, &flags, mds_blocking_ast,
2340 ldlm_completion_ast, NULL, NULL,
2341 NULL, 0, NULL, &src_lockh);
2343 GOTO(cleanup, rc = -ENOLCK);
2344 cleanup_phase = 2; /* lock */
2346 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2348 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2349 if (IS_ERR(handle)) {
2350 rc = PTR_ERR(handle);
2353 de_src->d_inode->i_nlink++;
2354 mark_inode_dirty(de_src->d_inode);
2358 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2359 handle, req, rc, 0);
2360 switch (cleanup_phase) {
2363 ldlm_lock_decref(&src_lockh, LCK_EX);
2365 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2371 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2374 req->rq_status = rc;
2379 * request to link to foreign inode:
2380 * - acquire i_nlinks on this inode
2383 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2384 int offset, struct ptlrpc_request *req,
2385 struct lustre_handle *lh)
2387 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2388 struct obd_device *obd = req->rq_export->exp_obd;
2389 struct dentry *de_tgt_dir = NULL;
2390 struct mds_obd *mds = mds_req2mds(req);
2391 int rc = 0, cleanup_phase = 0;
2392 struct mdc_op_data *op_data;
2393 struct ptlrpc_request *request = NULL;
2397 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2398 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2399 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2400 OLID4(rec->ur_id1));
2402 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2403 tgt_dir_lockh, &update_mode,
2404 rec->ur_name, rec->ur_namelen - 1,
2405 MDS_INODELOCK_UPDATE);
2406 if (IS_ERR(de_tgt_dir))
2407 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2410 OBD_ALLOC(op_data, sizeof(*op_data));
2411 if (op_data == NULL)
2412 GOTO(cleanup, rc = -ENOMEM);
2414 memset(op_data, 0, sizeof(*op_data));
2415 op_data->id1 = *(rec->ur_id1);
2416 rc = md_link(mds->mds_md_exp, op_data, &request);
2417 OBD_FREE(op_data, sizeof(*op_data));
2420 ptlrpc_req_finished(request);
2426 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2428 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2429 if (IS_ERR(handle)) {
2430 rc = PTR_ERR(handle);
2436 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2437 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2438 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2439 id_fid(rec->ur_id1));
2442 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2443 handle, req, rc, 0);
2445 switch (cleanup_phase) {
2448 OBD_ALLOC(op_data, sizeof(*op_data));
2449 if (op_data != NULL) {
2451 memset(op_data, 0, sizeof(*op_data));
2453 op_data->id1 = *(rec->ur_id1);
2454 op_data->create_mode = rec->ur_mode;
2456 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2457 OBD_FREE(op_data, sizeof(*op_data));
2459 ptlrpc_req_finished(request);
2461 CERROR("error %d while dropping i_nlink on "
2462 "remote inode\n", rc);
2465 CERROR("rc %d prevented dropping i_nlink on "
2466 "remote inode\n", -ENOMEM);
2472 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2474 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2477 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2479 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2485 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2488 req->rq_status = rc;
2492 static int mds_reint_link(struct mds_update_record *rec, int offset,
2493 struct ptlrpc_request *req,
2494 struct lustre_handle *lh)
2496 struct obd_device *obd = req->rq_export->exp_obd;
2497 struct dentry *de_src = NULL;
2498 struct dentry *de_tgt_dir = NULL;
2499 struct dentry *dchild = NULL;
2500 struct mds_obd *mds = mds_req2mds(req);
2501 struct lustre_handle *handle = NULL;
2502 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2503 struct ldlm_res_id src_res_id = { .name = {0} };
2504 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2505 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2506 ldlm_policy_data_t tgt_dir_policy =
2507 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2508 int rc = 0, cleanup_phase = 0;
2510 int update_mode = 0;
2514 LASSERT(offset == 1);
2516 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2517 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2518 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2521 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2522 MD_COUNTER_INCREMENT(obd, link);
2524 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2525 GOTO(cleanup, rc = -ENOENT);
2527 if (id_group(rec->ur_id1) != mds->mds_num) {
2528 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2532 if (rec->ur_namelen == 1) {
2533 rc = mds_reint_link_acquire(rec, offset, req, lh);
2537 /* Step 1: Lookup the source inode and target directory by ID */
2538 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2540 GOTO(cleanup, rc = PTR_ERR(de_src));
2542 cleanup_phase = 1; /* source dentry */
2544 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2545 if (IS_ERR(de_tgt_dir)) {
2546 rc = PTR_ERR(de_tgt_dir);
2551 cleanup_phase = 2; /* target directory dentry */
2553 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2554 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2555 rec->ur_name, de_src->d_inode->i_ino);
2557 /* Step 2: Take the two locks */
2558 src_res_id.name[0] = id_fid(rec->ur_id1);
2559 src_res_id.name[1] = id_group(rec->ur_id1);
2560 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2561 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2564 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2566 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2568 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2569 tgt_dir_res_id, LDLM_IBITS,
2570 &src_policy, update_mode, &flags,
2572 ldlm_completion_ast, NULL, NULL,
2573 NULL, 0, NULL, tgt_dir_lockh + 1);
2575 GOTO(cleanup, rc = -ENOLCK);
2578 tgt_dir_res_id.name[2] = full_name_hash(rec->ur_name,
2579 rec->ur_namelen - 1);
2580 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2581 (unsigned long)id_fid(rec->ur_id2),
2582 (unsigned long)id_group(rec->ur_id2),
2583 tgt_dir_res_id.name[2]);
2586 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2587 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2588 LCK_EX, &tgt_dir_policy);
2592 cleanup_phase = 3; /* locks */
2594 /* Step 3: Lookup the child */
2595 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2596 rec->ur_namelen - 1);
2597 if (IS_ERR(dchild)) {
2598 rc = PTR_ERR(dchild);
2599 if (rc != -EPERM && rc != -EACCES)
2600 CERROR("child lookup error %d\n", rc);
2604 cleanup_phase = 4; /* child dentry */
2606 if (dchild->d_inode) {
2607 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2608 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2613 /* Step 4: Do it. */
2614 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2616 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2617 if (IS_ERR(handle)) {
2618 rc = PTR_ERR(handle);
2622 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2623 if (rc && rc != -EPERM && rc != -EACCES)
2624 CERROR("vfs_link error %d\n", rc);
2626 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2627 handle, req, rc, 0);
2630 switch (cleanup_phase) {
2631 case 4: /* child dentry */
2635 ldlm_lock_decref(&src_lockh, LCK_EX);
2636 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2638 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2639 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2641 case 2: /* target dentry */
2643 if (tgt_dir_lockh[1].cookie && update_mode)
2644 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2648 case 1: /* source dentry */
2653 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2656 req->rq_status = rc;
2660 /* The idea here is that we need to get four locks in the end:
2661 * one on each parent directory, one on each child. We need to take
2662 * these locks in some kind of order (to avoid deadlocks), and the order
2663 * I selected is "increasing resource number" order. We need to look up
2664 * the children, however, before we know what the resource number(s) are.
2665 * Thus the following plan:
2667 * 1,2. Look up the parents
2668 * 3,4. Look up the children
2669 * 5. Take locks on the parents and children, in order
2670 * 6. Verify that the children haven't changed since they were looked up
2672 * If there was a race and the children changed since they were first looked
2673 * up, it is possible that mds_verify_child() will be able to just grab the
2674 * lock on the new child resource (if it has a higher resource than any other)
2675 * but we need to compare against not only its parent, but also against the
2676 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2678 * We need the fancy igrab() on the child inodes because we aren't holding a
2679 * lock on the parent after the lookup is done, so dentry->d_inode may change
2680 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2682 static int mds_get_parents_children_locked(struct obd_device *obd,
2683 struct mds_obd *mds,
2684 struct lustre_id *p1_id,
2685 struct dentry **de_srcdirp,
2686 struct lustre_id *p2_id,
2687 struct dentry **de_tgtdirp,
2689 const char *old_name, int old_len,
2690 struct dentry **de_oldp,
2691 const char *new_name, int new_len,
2692 struct dentry **de_newp,
2693 struct lustre_handle *dlm_handles,
2696 struct ldlm_res_id p1_res_id = { .name = {0} };
2697 struct ldlm_res_id p2_res_id = { .name = {0} };
2698 struct ldlm_res_id c1_res_id = { .name = {0} };
2699 struct ldlm_res_id c2_res_id = { .name = {0} };
2700 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2701 /* Only dentry should disappear, but the inode itself would be
2702 intact otherwise. */
2703 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2704 /* If something is going to be replaced, both dentry and inode locks are
2706 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2707 struct ldlm_res_id *maxres_src, *maxres_tgt;
2708 struct inode *inode;
2709 int rc = 0, cleanup_phase = 0;
2710 __u32 child_gen1 = 0;
2711 __u32 child_gen2 = 0;
2712 unsigned long child_ino1 = 0;
2713 unsigned long child_ino2 = 0;
2716 /* Step 1: Lookup the source directory */
2717 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2718 if (IS_ERR(*de_srcdirp))
2719 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2721 cleanup_phase = 1; /* source directory dentry */
2723 p1_res_id.name[0] = id_fid(p1_id);
2724 p1_res_id.name[1] = id_group(p1_id);
2726 /* Step 2: Lookup the target directory */
2727 if (id_equal_stc(p1_id, p2_id)) {
2728 *de_tgtdirp = dget(*de_srcdirp);
2730 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2731 if (IS_ERR(*de_tgtdirp)) {
2732 rc = PTR_ERR(*de_tgtdirp);
2738 cleanup_phase = 2; /* target directory dentry */
2740 p2_res_id.name[0] = id_fid(p2_id);
2741 p2_res_id.name[1] = id_group(p2_id);
2744 dlm_handles[5].cookie = 0;
2745 dlm_handles[6].cookie = 0;
2747 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2749 * get a temp lock on just fid, group to flush client cache and
2750 * to protect dirs from concurrent splitting.
2752 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2753 LCK_PW, &p_policy, &p2_res_id,
2754 &dlm_handles[6], LCK_PW, &p_policy);
2758 p1_res_id.name[2] = full_name_hash(old_name, old_len - 1);
2759 p2_res_id.name[2] = full_name_hash(new_name, new_len - 1);
2761 CDEBUG(D_INFO, "take locks on "
2762 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2763 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2764 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2769 /* Step 3: Lookup the source child entry */
2770 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2772 if (IS_ERR(*de_oldp)) {
2773 rc = PTR_ERR(*de_oldp);
2774 CERROR("old child lookup error (%.*s): %d\n",
2775 old_len - 1, old_name, rc);
2779 cleanup_phase = 4; /* original name dentry */
2781 inode = (*de_oldp)->d_inode;
2782 if (inode != NULL) {
2783 struct lustre_id sid;
2785 inode = igrab(inode);
2787 GOTO(cleanup, rc = -ENOENT);
2789 down(&inode->i_sem);
2790 rc = mds_read_inode_sid(obd, inode, &sid);
2793 CERROR("Can't read inode self id, inode %lu, "
2794 "rc %d\n", inode->i_ino, rc);
2799 child_ino1 = inode->i_ino;
2800 child_gen1 = inode->i_generation;
2801 c1_res_id.name[0] = id_fid(&sid);
2802 c1_res_id.name[1] = id_group(&sid);
2804 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2805 child_ino1 = (*de_oldp)->d_inum;
2806 child_gen1 = (*de_oldp)->d_generation;
2807 c1_res_id.name[0] = (*de_oldp)->d_fid;
2808 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2810 GOTO(cleanup, rc = -ENOENT);
2813 /* Step 4: Lookup the target child entry */
2814 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2816 if (IS_ERR(*de_newp)) {
2817 rc = PTR_ERR(*de_newp);
2818 CERROR("new child lookup error (%.*s): %d\n",
2819 old_len - 1, old_name, rc);
2823 cleanup_phase = 5; /* target dentry */
2825 inode = (*de_newp)->d_inode;
2826 if (inode != NULL) {
2827 struct lustre_id sid;
2829 inode = igrab(inode);
2833 down(&inode->i_sem);
2834 rc = mds_read_inode_sid(obd, inode, &sid);
2837 CERROR("Can't read inode self id, inode %lu, "
2838 "rc %d\n", inode->i_ino, rc);
2842 child_ino2 = inode->i_ino;
2843 child_gen2 = inode->i_generation;
2844 c2_res_id.name[0] = id_fid(&sid);
2845 c2_res_id.name[1] = id_group(&sid);
2847 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2848 child_ino2 = (*de_newp)->d_inum;
2849 child_gen2 = (*de_newp)->d_generation;
2850 c2_res_id.name[0] = (*de_newp)->d_fid;
2851 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2855 /* Step 5: Take locks on the parents and child(ren) */
2856 maxres_src = &p1_res_id;
2857 maxres_tgt = &p2_res_id;
2858 cleanup_phase = 5; /* target dentry */
2860 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2861 maxres_src = &c1_res_id;
2862 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2863 maxres_tgt = &c2_res_id;
2865 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2867 &p2_res_id, &dlm_handles[1], parent_mode,
2869 &c1_res_id, &dlm_handles[2], child_mode,
2871 &c2_res_id, &dlm_handles[3], child_mode,
2876 cleanup_phase = 6; /* parent and child(ren) locks */
2878 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2879 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2880 parent_mode, &c1_res_id, &dlm_handles[2],
2881 de_oldp, child_mode, &c1_policy, old_name, old_len,
2882 maxres_tgt, child_ino1, child_gen1);
2884 if (c2_res_id.name[0] != 0)
2885 ldlm_lock_decref(&dlm_handles[3], child_mode);
2886 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2893 if (!DENTRY_VALID(*de_oldp))
2894 GOTO(cleanup, rc = -ENOENT);
2896 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2897 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2898 parent_mode, &c2_res_id, &dlm_handles[3],
2899 de_newp, child_mode, &c2_policy, new_name,
2900 new_len, maxres_src, child_ino2, child_gen2);
2902 ldlm_lock_decref(&dlm_handles[2], child_mode);
2903 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2913 switch (cleanup_phase) {
2914 case 6: /* child lock(s) */
2915 if (c2_res_id.name[0] != 0)
2916 ldlm_lock_decref(&dlm_handles[3], child_mode);
2917 if (c1_res_id.name[0] != 0)
2918 ldlm_lock_decref(&dlm_handles[2], child_mode);
2919 if (dlm_handles[1].cookie != 0)
2920 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2921 if (dlm_handles[0].cookie != 0)
2922 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2923 case 5: /* target dentry */
2925 case 4: /* source dentry */
2929 if (dlm_handles[5].cookie != 0)
2930 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2931 if (dlm_handles[6].cookie != 0)
2932 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
2934 case 2: /* target directory dentry */
2935 l_dput(*de_tgtdirp);
2936 case 1: /* source directry dentry */
2937 l_dput(*de_srcdirp);
2945 * checks if dentry can be removed. This function also handles cross-ref
2948 static int mds_check_for_rename(struct obd_device *obd,
2949 struct dentry *dentry)
2951 struct mds_obd *mds = &obd->u.mds;
2952 struct lustre_handle *rlockh;
2953 struct ptlrpc_request *req;
2954 struct mdc_op_data *op_data;
2955 struct lookup_intent it;
2956 int handle_size, rc = 0;
2959 LASSERT(dentry != NULL);
2961 if (dentry->d_inode) {
2962 if (S_ISDIR(dentry->d_inode->i_mode) &&
2963 !mds_is_dir_empty(obd, dentry))
2966 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
2967 handle_size = sizeof(struct lustre_handle);
2969 OBD_ALLOC(rlockh, handle_size);
2973 memset(rlockh, 0, handle_size);
2974 OBD_ALLOC(op_data, sizeof(*op_data));
2975 if (op_data == NULL) {
2976 OBD_FREE(rlockh, handle_size);
2979 memset(op_data, 0, sizeof(*op_data));
2980 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
2982 it.it_op = IT_UNLINK;
2983 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
2986 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
2987 op_data, rlockh, NULL, 0, ldlm_completion_ast,
2988 mds_blocking_ast, NULL);
2989 OBD_FREE(op_data, sizeof(*op_data));
2993 OBD_FREE(it.d.fs_data,
2994 sizeof(struct lustre_intent_data));
2997 if (rlockh->cookie != 0)
2998 ldlm_lock_decref(rlockh, LCK_EX);
3000 if (LUSTRE_IT(&it)->it_data) {
3001 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3002 ptlrpc_req_finished(req);
3005 if (LUSTRE_IT(&it)->it_status)
3006 rc = LUSTRE_IT(&it)->it_status;
3007 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3008 OBD_FREE(rlockh, handle_size);
3013 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3014 struct ptlrpc_request *req, struct lustre_id *id,
3015 struct dentry *de_dir, struct dentry *de)
3017 struct obd_device *obd = req->rq_export->exp_obd;
3018 struct mds_obd *mds = mds_req2mds(req);
3019 void *handle = NULL;
3025 * name exists and points to local inode try to unlink this name
3026 * and create new one.
3028 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3029 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3030 (unsigned long)de->d_inode->i_generation);
3032 /* checking if we can remove local dentry. */
3033 rc = mds_check_for_rename(obd, de);
3037 handle = fsfilt_start(obd, de_dir->d_inode,
3038 FSFILT_OP_RENAME, NULL);
3040 GOTO(cleanup, rc = PTR_ERR(handle));
3041 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3044 } else if (de->d_flags & DCACHE_CROSS_REF) {
3045 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3046 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3047 (unsigned long)de->d_fid);
3049 /* checking if we can remove local dentry. */
3050 rc = mds_check_for_rename(obd, de);
3055 * to be fully POSIX compatible, we should add one more check:
3057 * if de_new is subdir of dir rec->ur_id1. If so - return
3060 * I do not know how to implement it right now, because
3061 * inodes/dentries for new and old names lie on different MDS,
3062 * so add this notice here just to make it visible for the rest
3063 * of developers and do not forget about. And when this check
3064 * will be added, del_cross_ref should gone, that is local
3065 * dentry is able to be removed if all checks passed. --umka
3068 handle = fsfilt_start(obd, de_dir->d_inode,
3069 FSFILT_OP_RENAME, NULL);
3071 GOTO(cleanup, rc = PTR_ERR(handle));
3072 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3076 /* name doesn't exist. the simplest case. */
3077 handle = fsfilt_start(obd, de_dir->d_inode,
3078 FSFILT_OP_LINK, NULL);
3080 GOTO(cleanup, rc = PTR_ERR(handle));
3083 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3084 rec->ur_tgtlen - 1, id_ino(id),
3085 id_gen(id), id_group(id), id_fid(id));
3087 CERROR("add_dir_entry() returned error %d\n", rc);
3093 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3094 handle, req, rc, 0);
3099 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3100 struct ptlrpc_request *req, struct dentry *de_dir,
3103 struct obd_device *obd = req->rq_export->exp_obd;
3104 struct mds_obd *mds = mds_req2mds(req);
3105 void *handle = NULL;
3109 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3111 GOTO(cleanup, rc = PTR_ERR(handle));
3112 rc = fsfilt_del_dir_entry(obd, de);
3117 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3118 handle, req, rc, 0);
3122 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3123 int offset, struct ptlrpc_request *req)
3125 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3126 struct obd_device *obd = req->rq_export->exp_obd;
3127 struct mds_obd *mds = mds_req2mds(req);
3128 struct lustre_handle child_lockh = {0};
3129 struct dentry *de_tgtdir = NULL;
3130 struct dentry *de_new = NULL;
3131 int cleanup_phase = 0;
3132 int update_mode, rc = 0;
3136 * another MDS executing rename operation has asked us to create target
3137 * name. such a creation should destroy existing target name.
3139 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3140 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3142 /* first, lookup the target */
3143 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3144 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3145 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3146 &child_lockh, &de_new, LCK_EX,
3147 MDS_INODELOCK_LOOKUP);
3154 LASSERT(de_tgtdir->d_inode);
3157 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3163 if (cleanup_phase == 1) {
3165 if (parent_lockh[1].cookie != 0)
3166 ldlm_lock_decref(parent_lockh + 1, update_mode);
3168 ldlm_lock_decref(parent_lockh, LCK_PW);
3169 if (child_lockh.cookie != 0)
3170 ldlm_lock_decref(&child_lockh, LCK_EX);
3175 req->rq_status = rc;
3179 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3180 struct ptlrpc_request *req)
3182 struct obd_device *obd = req->rq_export->exp_obd;
3183 struct ptlrpc_request *req2 = NULL;
3184 struct dentry *de_srcdir = NULL;
3185 struct dentry *de_old = NULL;
3186 struct mds_obd *mds = mds_req2mds(req);
3187 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3188 struct lustre_handle child_lockh = {0};
3189 struct mdc_op_data *op_data;
3190 int update_mode, rc = 0;
3193 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3194 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3196 OBD_ALLOC(op_data, sizeof(*op_data));
3197 if (op_data == NULL)
3199 memset(op_data, 0, sizeof(*op_data));
3201 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3202 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3203 &update_mode, rec->ur_name,
3204 rec->ur_namelen, &child_lockh, &de_old,
3205 LCK_EX, MDS_INODELOCK_LOOKUP);
3207 OBD_FREE(op_data, sizeof(*op_data));
3212 LASSERT(de_srcdir->d_inode);
3216 * we already know the target should be created on another MDS so, we
3217 * have to request that MDS to do it.
3220 /* prepare source id */
3221 if (de_old->d_flags & DCACHE_CROSS_REF) {
3222 LASSERT(de_old->d_inode == NULL);
3223 CDEBUG(D_OTHER, "request to move remote name\n");
3224 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3225 } else if (de_old->d_inode == NULL) {
3226 /* oh, source doesn't exist */
3227 OBD_FREE(op_data, sizeof(*op_data));
3228 GOTO(cleanup, rc = -ENOENT);
3230 struct lustre_id sid;
3231 struct inode *inode = de_old->d_inode;
3233 LASSERT(inode != NULL);
3234 CDEBUG(D_OTHER, "request to move local name\n");
3235 id_ino(&op_data->id1) = inode->i_ino;
3236 id_group(&op_data->id1) = mds->mds_num;
3237 id_gen(&op_data->id1) = inode->i_generation;
3239 down(&inode->i_sem);
3240 rc = mds_read_inode_sid(obd, inode, &sid);
3243 CERROR("Can't read inode self id, "
3244 "inode %lu, rc = %d\n",
3249 id_fid(&op_data->id1) = id_fid(&sid);
3252 op_data->id2 = *rec->ur_id2;
3253 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3254 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3255 OBD_FREE(op_data, sizeof(*op_data));
3260 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3266 ptlrpc_req_finished(req2);
3269 if (parent_lockh[1].cookie != 0)
3270 ldlm_lock_decref(parent_lockh + 1, update_mode);
3272 ldlm_lock_decref(parent_lockh, LCK_PW);
3273 if (child_lockh.cookie != 0)
3274 ldlm_lock_decref(&child_lockh, LCK_EX);
3279 req->rq_status = rc;
3283 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3284 struct ptlrpc_request *req, struct lustre_handle *lockh)
3286 struct obd_device *obd = req->rq_export->exp_obd;
3287 struct dentry *de_srcdir = NULL;
3288 struct dentry *de_tgtdir = NULL;
3289 struct dentry *de_old = NULL;
3290 struct dentry *de_new = NULL;
3291 struct inode *old_inode = NULL, *new_inode = NULL;
3292 struct mds_obd *mds = mds_req2mds(req);
3293 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3294 struct mds_body *body = NULL;
3295 struct llog_create_locks *lcl = NULL;
3296 struct lov_mds_md *lmm = NULL;
3297 int rc = 0, cleanup_phase = 0;
3298 void *handle = NULL;
3301 LASSERT(offset == 1);
3303 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3304 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3307 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3309 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3310 DEBUG_REQ(D_HA, req, "rename replay\n");
3311 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3312 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3313 req->rq_repmsg->buflens[2]);
3316 MD_COUNTER_INCREMENT(obd, rename);
3318 if (rec->ur_namelen == 1) {
3319 rc = mds_reint_rename_create_name(rec, offset, req);
3323 /* check if new name should be located on remote target. */
3324 if (id_group(rec->ur_id2) != mds->mds_num) {
3325 rc = mds_reint_rename_to_remote(rec, offset, req);
3329 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3330 rec->ur_id2, &de_tgtdir, LCK_PW,
3331 rec->ur_name, rec->ur_namelen,
3332 &de_old, rec->ur_tgt,
3333 rec->ur_tgtlen, &de_new,
3334 dlm_handles, LCK_EX);
3338 cleanup_phase = 1; /* parent(s), children, locks */
3339 old_inode = de_old->d_inode;
3340 new_inode = de_new->d_inode;
3342 /* sanity check for src inode */
3343 if (de_old->d_flags & DCACHE_CROSS_REF) {
3344 LASSERT(de_old->d_inode == NULL);
3347 * in the case of cross-ref dir, we can perform this check only
3348 * if child and parent lie on the same mds. This is because
3349 * otherwise they can have the same inode numbers.
3351 if (de_old->d_mdsnum == mds->mds_num) {
3352 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3353 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3354 GOTO(cleanup, rc = -EINVAL);
3357 LASSERT(de_old->d_inode != NULL);
3358 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3359 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3360 GOTO(cleanup, rc = -EINVAL);
3363 /* sanity check for dest inode */
3364 if (de_new->d_flags & DCACHE_CROSS_REF) {
3365 LASSERT(new_inode == NULL);
3367 /* the same check about target dentry. */
3368 if (de_new->d_mdsnum == mds->mds_num) {
3369 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3370 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3371 GOTO(cleanup, rc = -EINVAL);
3375 * regular files usualy do not have ->rename() implemented. But
3376 * we handle only this case when @de_new is cross-ref entry,
3377 * because in other cases it will be handled by vfs_rename().
3379 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3380 !de_old->d_inode->i_op->rename))
3381 GOTO(cleanup, rc = -EPERM);
3384 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3385 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3386 GOTO(cleanup, rc = -EINVAL);
3391 * check if inodes point to each other. This should be checked before
3392 * is_subdir() check, as for the same entries it will think that they
3395 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3396 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3397 old_inode == new_inode)
3398 GOTO(cleanup, rc = 0);
3400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3402 * check if we are moving old entry into its child. 2.6 does not check
3403 * for this in vfs_rename() anymore.
3405 if (is_subdir(de_new, de_old))
3406 GOTO(cleanup, rc = -EINVAL);
3410 * if we are about to remove the target at first, pass the EA of that
3411 * inode to client to perform and cleanup on OST.
3413 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3414 LASSERT(body != NULL);
3416 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3418 DOWN_READ_I_ALLOC_SEM(new_inode);
3420 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3422 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3423 new_inode->i_nlink == 2) ||
3424 new_inode->i_nlink == 1)) {
3425 if (mds_orphan_open_count(new_inode) > 0) {
3426 /* need to lock pending_dir before transaction */
3427 down(&mds->mds_pending_dir->d_inode->i_sem);
3428 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3429 } else if (S_ISREG(new_inode->i_mode)) {
3430 mds_pack_inode2body(obd, body, new_inode, 0);
3431 mds_pack_md(obd, req->rq_repmsg, 1, body,
3432 new_inode, MDS_PACK_MD_LOCK, 0);
3436 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3437 de_srcdir->d_inode->i_sb);
3439 if (de_old->d_flags & DCACHE_CROSS_REF) {
3440 struct lustre_id old_id;
3442 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3444 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3449 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3454 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3455 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3456 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3459 GOTO(cleanup, rc = PTR_ERR(handle));
3462 de_old->d_fsdata = req;
3463 de_new->d_fsdata = req;
3464 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3467 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3468 if (mds_orphan_open_count(new_inode) > 0)
3469 rc = mds_orphan_add_link(rec, obd, de_new);
3472 GOTO(cleanup, rc = 0);
3474 if (!S_ISREG(new_inode->i_mode))
3477 if (!(body->valid & OBD_MD_FLEASIZE)) {
3478 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3479 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3480 } else if (mds_log_op_unlink(obd, new_inode,
3481 lustre_msg_buf(req->rq_repmsg,1,0),
3482 req->rq_repmsg->buflens[1],
3483 lustre_msg_buf(req->rq_repmsg,2,0),
3484 req->rq_repmsg->buflens[2],
3486 body->valid |= OBD_MD_FLCOOKIE;
3492 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3493 handle, req, rc, 0);
3495 switch (cleanup_phase) {
3497 up(&mds->mds_pending_dir->d_inode->i_sem);
3500 UP_READ_I_ALLOC_SEM(new_inode);
3503 if (dlm_handles[5].cookie != 0)
3504 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3505 if (dlm_handles[6].cookie != 0)
3506 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3509 ptlrpc_save_llog_lock(req, lcl);
3512 if (dlm_handles[3].cookie != 0)
3513 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3514 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3515 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3516 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3518 if (dlm_handles[3].cookie != 0)
3519 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3520 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3521 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3522 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3531 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3534 req->rq_status = rc;
3538 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3539 struct ptlrpc_request *, struct lustre_handle *);
3541 static mds_reinter reinters[REINT_MAX + 1] = {
3542 [REINT_SETATTR] mds_reint_setattr,
3543 [REINT_CREATE] mds_reint_create,
3544 [REINT_LINK] mds_reint_link,
3545 [REINT_UNLINK] mds_reint_unlink,
3546 [REINT_RENAME] mds_reint_rename,
3547 [REINT_OPEN] mds_open
3550 int mds_reint_rec(struct mds_update_record *rec, int offset,
3551 struct ptlrpc_request *req, struct lustre_handle *lockh)
3553 struct obd_device *obd = req->rq_export->exp_obd;
3554 struct lvfs_run_ctxt saved;
3557 /* checked by unpacker */
3558 LASSERT(rec->ur_opcode <= REINT_MAX &&
3559 reinters[rec->ur_opcode] != NULL);
3561 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3562 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3563 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);