1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include <linux/lustre_smfs.h>
49 #include "mds_internal.h"
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
59 static void mds_cancel_cookies_cb(struct obd_device *obd,
60 __u64 transno, void *cb_data,
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
90 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
93 OBD_FREE(mlcd, mlcd->mlcd_size);
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98 struct ptlrpc_request *req, int rc, __u32 op_data)
100 struct mds_export_data *med = &req->rq_export->exp_mds_data;
101 struct obd_device *obd = req->rq_export->exp_obd;
102 struct mds_client_data *mcd = med->med_mcd;
103 int err, log_pri = D_HA;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
112 GOTO(out_commit, rc);
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 if (req->rq_reqmsg->opc == MDS_CLOSE) {
146 mcd->mcd_last_close_transno = cpu_to_le64(transno);
147 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_close_result = cpu_to_le32(rc);
149 mcd->mcd_last_close_data = cpu_to_le32(op_data);
151 mcd->mcd_last_transno = cpu_to_le64(transno);
152 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
153 mcd->mcd_last_result = cpu_to_le32(rc);
154 mcd->mcd_last_data = cpu_to_le32(op_data);
157 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
158 mds_commit_last_transno_cb, NULL);
160 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
161 sizeof(*mcd), &off, 0);
169 DEBUG_REQ(log_pri, req,
170 "wrote trans #"LPU64" client %s at idx %u: err = %d",
171 transno, mcd->mcd_uuid, med->med_idx, err);
173 err = mds_update_last_fid(obd, handle, 0);
180 err = mds_dt_write_objids(obd);
186 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
190 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
191 req->rq_export->exp_sync);
193 CERROR("error committing transaction: %d\n", err);
201 /* this gives the same functionality as the code between
202 * sys_chmod and inode_setattr
203 * chown_common and inode_setattr
204 * utimes and inode_setattr
207 /* Just for the case if we have some clients that know about ATTR_RAW */
208 #define ATTR_RAW 8192
210 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
212 time_t now = LTIME_S(CURRENT_TIME);
213 struct iattr *attr = &rec->ur_iattr;
214 unsigned int ia_valid = attr->ia_valid;
218 /* only fix up attrs if the client VFS didn't already */
220 if (!(ia_valid & ATTR_RAW))
223 if (!(ia_valid & ATTR_CTIME_SET))
224 LTIME_S(attr->ia_ctime) = now;
225 if (!(ia_valid & ATTR_ATIME_SET))
226 LTIME_S(attr->ia_atime) = now;
227 if (!(ia_valid & ATTR_MTIME_SET))
228 LTIME_S(attr->ia_mtime) = now;
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
235 if (rec->ur_fsuid != inode->i_uid &&
236 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
240 if (ia_valid & ATTR_SIZE) {
241 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
245 if (ia_valid & ATTR_UID) {
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 if (attr->ia_uid == (uid_t) -1)
251 attr->ia_uid = inode->i_uid;
252 if (attr->ia_gid == (gid_t) -1)
253 attr->ia_gid = inode->i_gid;
254 attr->ia_mode = inode->i_mode;
256 * If the user or group of a non-directory has been
257 * changed by a non-root user, remove the setuid bit.
258 * 19981026 David C Niemi <niemi@tux.org>
260 * Changed this to apply to all users, including root,
261 * to avoid some races. This is the behavior we had in
262 * 2.0. The check for non-root was definitely wrong
263 * for 2.2 anyway, as it should have been using
264 * CAP_FSETID rather than fsuid -- 19990830 SD.
266 if ((inode->i_mode & S_ISUID) == S_ISUID &&
267 !S_ISDIR(inode->i_mode)) {
268 attr->ia_mode &= ~S_ISUID;
269 attr->ia_valid |= ATTR_MODE;
272 * Likewise, if the user or group of a non-directory
273 * has been changed by a non-root user, remove the
274 * setgid bit UNLESS there is no group execute bit
275 * (this would be a file marked for mandatory
276 * locking). 19981026 David C Niemi <niemi@tux.org>
278 * Removed the fsuid check (see the comment above) --
281 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
282 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
283 attr->ia_mode &= ~S_ISGID;
284 attr->ia_valid |= ATTR_MODE;
286 } else if (ia_valid & ATTR_MODE) {
287 int mode = attr->ia_mode;
289 if (attr->ia_mode == (mode_t) -1)
290 attr->ia_mode = inode->i_mode;
292 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 void mds_steal_ack_locks(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 char str[PTL_NALFMT_SIZE];
301 struct list_head *tmp;
302 struct ptlrpc_reply_state *oldrep;
303 struct ptlrpc_service *svc;
304 struct llog_create_locks *lcl;
308 /* CAVEAT EMPTOR: spinlock order */
309 spin_lock_irqsave (&exp->exp_lock, flags);
310 list_for_each (tmp, &exp->exp_outstanding_replies) {
311 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
313 if (oldrep->rs_xid != req->rq_xid)
316 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
317 CERROR ("Resent req xid "LPX64" has mismatched opc: "
318 "new %d old %d\n", req->rq_xid,
319 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
321 svc = oldrep->rs_srv_ni->sni_service;
322 spin_lock (&svc->srv_lock);
324 list_del_init (&oldrep->rs_exp_list);
326 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
327 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
328 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
329 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
331 for (i = 0; i < oldrep->rs_nlocks; i++)
332 ptlrpc_save_lock(req,
333 &oldrep->rs_locks[i],
334 oldrep->rs_modes[i]);
335 oldrep->rs_nlocks = 0;
337 lcl = oldrep->rs_llog_locks;
338 oldrep->rs_llog_locks = NULL;
340 ptlrpc_save_llog_lock(req, lcl);
342 DEBUG_REQ(D_HA, req, "stole locks for");
343 ptlrpc_schedule_difficult_reply (oldrep);
345 spin_unlock (&svc->srv_lock);
346 spin_unlock_irqrestore (&exp->exp_lock, flags);
349 spin_unlock_irqrestore (&exp->exp_lock, flags);
352 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
354 if (req->rq_reqmsg->opc == MDS_CLOSE) {
355 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
356 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
357 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
358 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
360 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
361 mcd->mcd_last_transno, mcd->mcd_last_result);
362 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
363 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
366 mds_steal_ack_locks(req);
369 static void reconstruct_reint_setattr(struct mds_update_record *rec,
370 int offset, struct ptlrpc_request *req)
372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
373 struct mds_body *body;
376 mds_req_from_mcd(req, med->med_mcd);
378 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
380 LASSERT(PTR_ERR(de) == req->rq_status);
384 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
385 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
387 /* Don't return OST-specific attributes if we didn't just set them */
388 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
389 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
390 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
391 body->valid |= OBD_MD_FLMTIME;
392 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
393 body->valid |= OBD_MD_FLATIME;
397 /*This is a tmp fix for cmobd setattr reint*/
399 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
400 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
401 #define XATTR_LUSTRE_MDS_MID_EA "mid"
402 #define XATTR_LUSTRE_MDS_SID_EA "sid"
404 static int mds_get_md_type(char *name)
406 if (!strcmp(name, XATTR_LUSTRE_MDS_LOV_EA))
408 if (!strcmp(name, XATTR_LUSTRE_MDS_MEA_EA))
410 if (!strcmp(name, XATTR_LUSTRE_MDS_MID_EA))
412 if (!strcmp(name, XATTR_LUSTRE_MDS_SID_EA))
416 /* In the raw-setattr case, we lock the child inode.
417 * In the write-back case or if being called from open, the client holds a lock
420 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
421 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
422 struct ptlrpc_request *req, struct lustre_handle *lh)
424 struct mds_obd *mds = mds_req2mds(req);
425 struct obd_device *obd = req->rq_export->exp_obd;
426 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
427 struct mds_body *body;
428 struct dentry *de = NULL;
429 struct inode *inode = NULL;
430 struct lustre_handle lockh[2] = {{0}, {0}};
433 struct mds_logcancel_data *mlcd = NULL;
434 int rc = 0, cleanup_phase = 0, err;
438 LASSERT(offset == 1);
440 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
441 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
442 rec->ur_iattr.ia_valid);
444 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
445 MD_COUNTER_INCREMENT(obd, setattr);
447 if (med->med_remote) {
448 if (rec->ur_iattr.ia_valid & ATTR_GID) {
449 CWARN("Deny chgrp from remote client\n");
450 GOTO(cleanup, rc = -EPERM);
452 if (rec->ur_iattr.ia_valid & ATTR_UID) {
455 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
456 rec->ur_iattr.ia_uid);
457 if (uid == MDS_IDMAP_NOTFOUND) {
458 CWARN("Deny chown to uid %u\n",
459 rec->ur_iattr.ia_uid);
460 GOTO(cleanup, rc = -EPERM);
462 rec->ur_iattr.ia_uid = uid;
466 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
467 de = mds_id2dentry(obd, rec->ur_id1, NULL);
469 GOTO(cleanup, rc = PTR_ERR(de));
471 __u64 lockpart = MDS_INODELOCK_UPDATE;
472 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
473 lockpart |= MDS_INODELOCK_LOOKUP;
474 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
475 lockh, &parent_mode, NULL, 0, lockpart);
477 GOTO(cleanup, rc = PTR_ERR(de));
485 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
486 rec->ur_eadata != NULL)
489 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
491 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
493 GOTO(cleanup, rc = PTR_ERR(handle));
495 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
496 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
497 LTIME_S(rec->ur_iattr.ia_mtime),
498 LTIME_S(rec->ur_iattr.ia_ctime));
499 rc = mds_fix_attr(inode, rec);
503 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
504 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
505 (long)&rec->ur_iattr.ia_attr_flags);
507 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
510 if (rec->ur_iattr.ia_valid & ATTR_EA) {
511 int flags = (int)rec->ur_iattr.ia_attr_flags;
514 if (inode->i_op && inode->i_op->setxattr)
515 rc = inode->i_op->setxattr(de, rec->ur_eadata,
516 rec->ur_ea2data, rec->ur_ea2datalen,
518 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
520 if (inode->i_op && inode->i_op->removexattr)
521 rc = inode->i_op->removexattr(de, rec->ur_eadata);
522 } else if (rec->ur_iattr.ia_valid & ATTR_EA_CMOBD) {
525 /*tmp fix for cmobd set md reint*/
526 LASSERT(rec->ur_eadata != NULL);
527 LASSERT(rec->ur_ea2data != NULL);
528 name = rec->ur_eadata;
529 CDEBUG(D_INFO, "set %s EA for cmobd \n", name);
530 type = mds_get_md_type(name);
532 rc = fsfilt_set_md(obd, inode, handle,
534 rec->ur_ea2datalen, type);
537 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) {
538 struct lov_stripe_md *lsm = NULL;
539 struct lov_user_md *lum = NULL;
541 if (rec->ur_eadata != NULL) {
542 rc = ll_permission(inode, MAY_WRITE, NULL);
546 lum = rec->ur_eadata;
548 /* if lmm_stripe_size is -1 delete default
550 if (S_ISDIR(inode->i_mode) &&
551 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
552 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
556 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
558 &lsm, rec->ur_eadata);
562 obd_free_memmd(mds->mds_dt_exp, &lsm);
563 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
564 rec->ur_eadatalen, EA_LOV);
572 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
573 mds_pack_inode2body(obd, body, inode, 1);
575 /* Don't return OST-specific attributes if we didn't just set them */
576 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
577 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
578 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
579 body->valid |= OBD_MD_FLMTIME;
580 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
581 body->valid |= OBD_MD_FLATIME;
583 mds_body_do_reverse_map(med, body);
585 /* The logcookie should be no use anymore, why nobody remove
586 * following code block?
588 LASSERT(rec->ur_cookielen == 0);
589 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
590 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
593 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
595 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
596 mlcd->mlcd_cookielen = rec->ur_cookielen;
597 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
598 mlcd->mlcd_cookielen;
599 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
600 mlcd->mlcd_cookielen);
601 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
602 mlcd->mlcd_eadatalen);
604 CERROR("unable to allocate log cancel data\n");
610 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
611 handle, mds_cancel_cookies_cb, mlcd);
612 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
613 switch (cleanup_phase) {
615 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
616 rec->ur_eadata != NULL)
621 if (lockh[1].cookie != 0)
622 ldlm_lock_decref(lockh + 1, parent_mode);
625 ldlm_lock_decref(lockh, LCK_PW);
627 ptlrpc_save_lock (req, lockh, LCK_PW);
642 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
643 struct ptlrpc_request *req)
645 struct mds_export_data *med = &req->rq_export->exp_mds_data;
646 struct dentry *parent, *child;
647 struct mds_body *body;
650 mds_req_from_mcd(req, med->med_mcd);
652 if (req->rq_status) {
657 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
658 LASSERT(!IS_ERR(parent));
659 child = ll_lookup_one_len(rec->ur_name, parent,
660 rec->ur_namelen - 1);
661 LASSERT(!IS_ERR(child));
662 if ((child->d_flags & DCACHE_CROSS_REF)) {
663 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
664 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
665 mds_pack_dentry2body(req2obd(req), body, child, 1);
666 } else if (child->d_inode == NULL) {
667 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
668 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
669 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
671 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
672 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
679 static int mds_get_default_acl(struct inode *dir, void **pacl)
681 struct dentry de = { .d_inode = dir };
684 LASSERT(S_ISDIR(dir->i_mode));
686 if (!dir->i_op->getxattr)
689 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
690 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
695 OBD_ALLOC(*pacl, size);
699 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
701 /* since we already locked the dir, it should not change
702 * between the 2 getxattr calls
704 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
705 OBD_FREE(*pacl, size);
712 static int mds_reint_create(struct mds_update_record *rec, int offset,
713 struct ptlrpc_request *req,
714 struct lustre_handle *lh)
716 struct dentry *dparent = NULL;
717 struct mds_obd *mds = mds_req2mds(req);
718 struct obd_device *obd = req->rq_export->exp_obd;
719 struct dentry *dchild = NULL;
720 struct inode *dir = NULL;
722 struct lustre_handle lockh[2] = {{0}, {0}};
724 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
726 struct dentry_params dp;
727 struct mea *mea = NULL;
732 LASSERT(offset == 1);
734 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
735 OBD_MDS_DEVICENAME));
737 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
738 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
739 rec->ur_name, rec->ur_mode);
741 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
743 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
744 GOTO(cleanup, rc = -ESTALE);
746 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
747 lockh, &parent_mode, rec->ur_name,
748 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
749 if (IS_ERR(dparent)) {
750 rc = PTR_ERR(dparent);
751 CERROR("parent lookup error %d\n", rc);
754 cleanup_phase = 1; /* locked parent dentry */
755 dir = dparent->d_inode;
758 ldlm_lock_dump_handle(D_OTHER, lockh);
760 /* try to retrieve MEA data for this dir */
761 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
767 * dir is already splitted, check is requested filename should
768 * live at this MDS or at another one.
770 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
771 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
772 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
773 " should be %lu(%d)\n",
774 mea->mea_master, dparent->d_inode->i_ino,
775 dparent->d_inode->i_generation, rec->ur_name,
776 (unsigned long)id_group(&mea->mea_ids[i]), i);
777 GOTO(cleanup, rc = -ERESTART);
781 dchild = ll_lookup_one_len(rec->ur_name, dparent,
782 rec->ur_namelen - 1);
783 if (IS_ERR(dchild)) {
784 rc = PTR_ERR(dchild);
785 CERROR("Can't find "DLID4"/%s, error %d\n",
786 OLID4(rec->ur_id1), rec->ur_name, rc);
790 cleanup_phase = 2; /* child dentry */
792 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
794 if (type == S_IFREG || type == S_IFDIR) {
795 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
796 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
797 obd->obd_name, dparent->d_inode->i_ino,
798 dparent->d_inode->i_generation, rc, parent_mode);
800 /* dir got splitted */
801 GOTO(cleanup, rc = -ERESTART);
803 /* error happened during spitting. */
808 if (dir->i_mode & S_ISGID) {
809 if (S_ISDIR(rec->ur_mode))
810 rec->ur_mode |= S_ISGID;
814 * here inode number should be used only in the case of replaying. It is
815 * needed to check if object already created in the case of creating
818 if (id_ino(rec->ur_id2))
819 fid = id_fid(rec->ur_id2);
821 fid = mds_alloc_fid(obd);
822 dchild->d_fsdata = (void *)&dp;
823 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
826 dp.p_group = mds->mds_num;
830 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
832 GOTO(cleanup, rc = PTR_ERR(handle));
833 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
835 if (rc == 0 && rec->ur_eadata) {
836 /*for CMOBD to set lov md info when cmobd reint create*/
837 CDEBUG(D_INFO, "set lsm %p, len %d to inode %lu \n",
838 rec->ur_eadata, rec->ur_eadatalen,
839 dchild->d_inode->i_ino);
840 fsfilt_set_md(obd, dchild->d_inode, handle, rec->ur_eadata,
841 rec->ur_eadatalen, EA_LOV);
851 * as Peter asked, mkdir() should distribute new directories
852 * over the whole cluster in order to distribute namespace
853 * processing load. first, we calculate which MDS to use to put
854 * new directory's inode in.
856 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
857 rec->ur_flags, &req->rq_peer, dir);
858 if (i == mds->mds_num) {
859 /* inode will be created locally */
860 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
862 GOTO(cleanup, rc = PTR_ERR(handle));
864 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
867 "Can't create dir \"%s\", rc = %d\n",
868 dchild->d_name.name, rc);
872 down(&dchild->d_inode->i_sem);
874 rc = mds_update_inode_sid(obd, dchild->d_inode,
875 handle, rec->ur_id2);
877 CERROR("mds_update_inode_sid() failed, inode %lu, "
878 "rc %d\n", dchild->d_inode->i_ino, rc);
882 * make sure, that fid is up-to-date.
884 mds_set_last_fid(obd, id_fid(rec->ur_id2));
886 rc = mds_set_inode_sid(obd, dchild->d_inode,
889 CERROR("mds_set_inode_sid() failed, inode %lu, "
890 "rc %d\n", dchild->d_inode->i_ino, rc);
893 up(&dchild->d_inode->i_sem);
899 nstripes = *(u16 *)rec->ur_eadata;
901 if (rc == 0 && nstripes) {
903 * we pass LCK_EX to split routine to signal,
904 * that we have exclusive access to the
905 * directory. Simple because nobody knows it
906 * already exists -bzzz
908 rc = mds_try_to_split_dir(obd, dchild,
912 /* dir got splitted */
915 /* an error occured during
920 } else if (!DENTRY_VALID(dchild)) {
921 /* inode will be created on another MDS */
922 struct obdo *oa = NULL;
923 struct mds_body *body;
927 /* first, create that inode */
930 GOTO(cleanup, rc = -ENOMEM);
935 if (rec->ur_eadata) {
936 /* user asks for creating splitted dir */
937 oa->o_easize = *((u16 *) rec->ur_eadata);
940 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
941 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
943 /* adjust the uid/gid/mode bits */
944 oa->o_mode = rec->ur_mode;
945 oa->o_uid = current->fsuid;
946 oa->o_gid = (dir->i_mode & S_ISGID) ?
947 dir->i_gid : current->fsgid;
948 oa->o_valid |= OBD_MD_FLTYPE|OBD_MD_FLUID|OBD_MD_FLGID;
950 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
953 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
955 * here inode number and generation are
956 * important, as this is replay request and we
957 * need them to check if such an object is
960 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
961 obd->obd_name, rec->ur_namelen - 1,
962 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
963 (unsigned)id_gen(rec->ur_id2));
964 oa->o_id = id_ino(rec->ur_id2);
965 oa->o_fid = id_fid(rec->ur_id2);
966 oa->o_generation = id_gen(rec->ur_id2);
967 oa->o_flags |= OBD_FL_RECREATE_OBJS;
968 LASSERT(oa->o_fid != 0);
971 /* obtain default ACL */
972 acl_size = mds_get_default_acl(dir, &acl);
975 GOTO(cleanup, rc = -ENOMEM);
979 * before obd_create() is called, o_fid is not known if
980 * this is not recovery of cause.
982 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
986 OBD_FREE(acl, acl_size);
989 CERROR("can't create remote inode: %d\n", rc);
990 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
991 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
992 rec->ur_name, rec->ur_mode);
997 LASSERT(oa->o_fid != 0);
999 /* now, add new dir entry for it */
1000 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
1001 if (IS_ERR(handle)) {
1003 GOTO(cleanup, rc = PTR_ERR(handle));
1006 /* creating local dentry for remote inode. */
1007 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
1008 rec->ur_namelen - 1, oa->o_id,
1009 oa->o_generation, i, oa->o_fid);
1012 CERROR("Can't create local entry %*s for "
1013 "remote inode.\n", rec->ur_namelen - 1,
1019 body = lustre_msg_buf(req->rq_repmsg,
1021 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
1023 obdo2id(&body->id1, oa);
1026 /* requested name exists in the directory */
1033 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
1035 GOTO(cleanup, rc = PTR_ERR(handle));
1036 if (rec->ur_tgt == NULL) /* no target supplied */
1037 rc = -EINVAL; /* -EPROTO? */
1039 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1047 int rdev = rec->ur_rdev;
1048 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1050 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1051 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1056 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1057 dchild->d_fsdata = NULL;
1058 GOTO(cleanup, rc = -EINVAL);
1061 /* In case we stored the desired inum in here, we want to clean up. */
1062 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1063 dchild->d_fsdata = NULL;
1066 CDEBUG(D_INODE, "error during create: %d\n", rc);
1068 } else if (dchild->d_inode) {
1069 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1071 struct mds_body *body;
1072 struct inode *inode = dchild->d_inode;
1075 iattr.ia_uid = rec->ur_fsuid;
1076 LTIME_S(iattr.ia_atime) = rec->ur_time;
1077 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1078 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1080 if (dir->i_mode & S_ISGID)
1081 iattr.ia_gid = dir->i_gid;
1083 iattr.ia_gid = rec->ur_fsgid;
1085 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1086 ATTR_MTIME | ATTR_CTIME;
1088 if (id_ino(rec->ur_id2)) {
1089 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1090 inode->i_generation = id_gen(rec->ur_id2);
1092 if (type != S_IFDIR) {
1093 down(&inode->i_sem);
1094 rc = mds_update_inode_sid(obd, inode,
1095 handle, rec->ur_id2);
1098 CERROR("Can't update inode self id, "
1103 * make sure, that fid is up-to-date.
1105 mds_set_last_fid(obd, id_fid(rec->ur_id2));
1108 /* dirtied and committed by the upcoming setattr. */
1109 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1110 inode->i_ino, inode->i_generation);
1112 struct lustre_handle child_ino_lockh;
1114 CDEBUG(D_INODE, "created ino %lu with gen %x\n",
1115 inode->i_ino, inode->i_generation);
1117 if (type != S_IFDIR) {
1119 * allocate new id for @inode if it is not dir,
1120 * because for dir it was already done.
1122 down(&inode->i_sem);
1123 rc = mds_set_inode_sid(obd, inode,
1127 CERROR("mds_set_inode_sid() failed, "
1128 "inode %lu, rc %d\n", inode->i_ino,
1135 * the inode we were allocated may have just
1136 * been freed by an unlink operation. We take
1137 * this lock to synchronize against the matching
1138 * reply-ack-lock taken in unlink, to avoid
1139 * replay problems if this reply makes it out to
1140 * the client but the unlink's does not. See
1141 * bug 2029 for more detail.
1143 rc = mds_lock_new_child(obd, inode, &child_ino_lockh);
1144 if (rc != ELDLM_OK) {
1145 CERROR("error locking for unlink/create sync: "
1148 ldlm_lock_decref(&child_ino_lockh, LCK_EX);
1153 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1155 CERROR("error on child setattr: rc = %d\n", rc);
1157 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1158 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1160 CERROR("error on parent setattr: rc = %d\n", rc);
1162 MD_COUNTER_INCREMENT(obd, create);
1164 /* take care of default stripe inheritance */
1165 if (type == S_IFDIR) {
1166 struct lov_mds_md lmm;
1167 int lmm_size = sizeof(lmm);
1169 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1171 down(&inode->i_sem);
1172 rc = fsfilt_set_md(obd, inode, handle,
1173 &lmm, lmm_size, EA_LOV);
1177 CERROR("error on copy stripe info: rc = %d\n",
1183 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1184 mds_pack_inode2body(obd, body, inode, 1);
1185 mds_body_do_reverse_map(med, body);
1190 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1192 if (rc && created) {
1193 /* Destroy the file we just created. This should not need extra
1194 * journal credits, as we have already modified all of the
1195 * blocks needed in order to create the file in the first
1199 err = vfs_rmdir(dir, dchild);
1201 CERROR("rmdir in error path: %d\n", err);
1204 err = vfs_unlink(dir, dchild);
1206 CERROR("unlink in error path: %d\n", err);
1212 switch (cleanup_phase) {
1213 case 2: /* child dentry */
1215 case 1: /* locked parent dentry */
1217 if (lockh[1].cookie != 0)
1218 ldlm_lock_decref(lockh + 1, parent_mode);
1221 ldlm_lock_decref(lockh, LCK_PW);
1223 ptlrpc_save_lock(req, lockh, LCK_PW);
1229 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1233 OBD_FREE(mea, mea_size);
1234 req->rq_status = rc;
1239 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1240 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1244 for (i = 0; i < RES_NAME_SIZE; i++) {
1246 * this is needed to make zeroed res_id entries to be put at the
1247 * end of list in *ordered_locks() .
1249 if (res1->name[i] == 0 && res2->name[i] != 0)
1251 if (res2->name[i] == 0 && res1->name[i] != 0)
1253 if (res1->name[i] > res2->name[i])
1255 if (res1->name[i] < res2->name[i])
1262 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1268 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1269 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1270 * because they take the place of local semaphores.
1272 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1273 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1274 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1275 struct lustre_handle *p1_lockh, int p1_lock_mode,
1276 ldlm_policy_data_t *p1_policy,
1277 struct ldlm_res_id *p2_res_id,
1278 struct lustre_handle *p2_lockh, int p2_lock_mode,
1279 ldlm_policy_data_t *p2_policy)
1281 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1282 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1283 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1284 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1288 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1290 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1291 res_id[0]->name[0], res_id[1]->name[0]);
1293 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1294 handles[1] = p1_lockh;
1295 handles[0] = p2_lockh;
1296 res_id[1] = p1_res_id;
1297 res_id[0] = p2_res_id;
1298 lock_modes[1] = p1_lock_mode;
1299 lock_modes[0] = p2_lock_mode;
1300 policies[1] = p1_policy;
1301 policies[0] = p2_policy;
1304 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1305 res_id[0]->name[0], res_id[1]->name[0]);
1307 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1308 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1309 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1310 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1311 NULL, 0, NULL, handles[0]);
1314 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1316 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1317 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1318 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1319 ldlm_lock_addref(handles[1], lock_modes[1]);
1320 } else if (res_id[1]->name[0] != 0) {
1321 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1322 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1323 *res_id[1], LDLM_IBITS, policies[1],
1324 lock_modes[1], &flags, mds_blocking_ast,
1325 ldlm_completion_ast, NULL, NULL, NULL, 0,
1327 if (rc != ELDLM_OK) {
1328 ldlm_lock_decref(handles[0], lock_modes[0]);
1331 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1337 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1338 struct lustre_handle *p1_lockh, int p1_lock_mode,
1339 ldlm_policy_data_t *p1_policy,
1340 struct ldlm_res_id *p2_res_id,
1341 struct lustre_handle *p2_lockh, int p2_lock_mode,
1342 ldlm_policy_data_t *p2_policy,
1343 struct ldlm_res_id *c1_res_id,
1344 struct lustre_handle *c1_lockh, int c1_lock_mode,
1345 ldlm_policy_data_t *c1_policy,
1346 struct ldlm_res_id *c2_res_id,
1347 struct lustre_handle *c2_lockh, int c2_lock_mode,
1348 ldlm_policy_data_t *c2_policy)
1350 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1351 c1_res_id, c2_res_id };
1352 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1353 c1_lockh, c2_lockh };
1354 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1355 c1_lock_mode, c2_lock_mode };
1356 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1357 c1_policy, c2_policy};
1358 int rc, i, j, sorted, flags;
1361 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1362 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1363 res_id[3]->name[0]);
1366 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1367 * res_id should be at the end of list after sorting is finished.
1369 for (i = 1; i < 4; i++) {
1371 dlm_handles[4] = dlm_handles[i];
1372 res_id[4] = res_id[i];
1373 lock_modes[4] = lock_modes[i];
1374 policies[4] = policies[i];
1378 if (res_gt(res_id[j], res_id[4], policies[j],
1380 dlm_handles[j + 1] = dlm_handles[j];
1381 res_id[j + 1] = res_id[j];
1382 lock_modes[j + 1] = lock_modes[j];
1383 policies[j + 1] = policies[j];
1388 } while (j >= 0 && !sorted);
1390 dlm_handles[j + 1] = dlm_handles[4];
1391 res_id[j + 1] = res_id[4];
1392 lock_modes[j + 1] = lock_modes[4];
1393 policies[j + 1] = policies[4];
1396 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1397 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1398 res_id[3]->name[0]);
1400 /* XXX we could send ASTs on all these locks first before blocking? */
1401 for (i = 0; i < 4; i++) {
1405 * nevertheless zeroed res_ids should be at the end of list, and
1406 * could use break here, I think, that it is more correctly for
1407 * clear understanding of code to have continue here, as it
1408 * clearly means, that zeroed res_id should be skipped and does
1409 * not mean, that if we meet zeroed res_id we should stop
1412 if (res_id[i]->name[0] == 0)
1416 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1417 (policies[i]->l_inodebits.bits &
1418 policies[i-1]->l_inodebits.bits) ) {
1419 memcpy(dlm_handles[i], dlm_handles[i-1],
1420 sizeof(*(dlm_handles[i])));
1421 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1423 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1424 *res_id[i], LDLM_IBITS,
1426 lock_modes[i], &flags,
1428 ldlm_completion_ast, NULL, NULL,
1429 NULL, 0, NULL, dlm_handles[i]);
1431 GOTO(out_err, rc = -EIO);
1432 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1439 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1444 /* In the unlikely case that the child changed while we were waiting
1445 * on the lock, we need to drop the lock on the old child and either:
1446 * - if the child has a lower resource name, then we have to also
1447 * drop the parent lock and regain the locks in the right order
1448 * - in the rename case, if the child has a lower resource name than one of
1449 * the other parent/child resources (maxres) we also need to reget the locks
1450 * - if the child has a higher resource name (this is the common case)
1451 * we can just get the lock on the new child (still in lock order)
1453 * Returns 0 if the child did not change or if it changed but could be locked.
1454 * Returns 1 if the child changed and we need to re-lock (no locks held).
1455 * Returns -ve error with a valid dchild (no locks held). */
1456 static int mds_verify_child(struct obd_device *obd,
1457 struct ldlm_res_id *parent_res_id,
1458 struct lustre_handle *parent_lockh,
1459 struct dentry *dparent, int parent_mode,
1460 struct ldlm_res_id *child_res_id,
1461 struct lustre_handle *child_lockh,
1462 struct dentry **dchildp, int child_mode,
1463 ldlm_policy_data_t *child_policy,
1464 const char *name, int namelen,
1465 struct ldlm_res_id *maxres,
1466 unsigned long child_ino, __u32 child_gen)
1468 struct lustre_id sid;
1469 struct dentry *vchild, *dchild = *dchildp;
1470 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1473 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1475 GOTO(cleanup, rc = PTR_ERR(vchild));
1477 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1478 if (child_gen == vchild->d_generation &&
1479 child_ino == vchild->d_inum) {
1488 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1489 (vchild->d_inode != NULL &&
1490 child_gen == vchild->d_inode->i_generation &&
1491 child_ino == vchild->d_inode->i_ino))) {
1499 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1500 vchild->d_inode, dchild ? dchild->d_inode : 0,
1501 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1502 child_res_id->name[0]);
1504 if (child_res_id->name[0] != 0)
1505 ldlm_lock_decref(child_lockh, child_mode);
1509 cleanup_phase = 1; /* parent lock only */
1510 *dchildp = dchild = vchild;
1512 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1513 int flags = LDLM_FL_ATOMIC_CB;
1515 if (dchild->d_inode) {
1516 down(&dchild->d_inode->i_sem);
1517 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1518 up(&dchild->d_inode->i_sem);
1520 CERROR("Can't read inode self id, inode %lu,"
1521 " rc %d\n", dchild->d_inode->i_ino, rc);
1524 child_res_id->name[0] = id_fid(&sid);
1525 child_res_id->name[1] = id_group(&sid);
1527 child_res_id->name[0] = dchild->d_fid;
1528 child_res_id->name[1] = dchild->d_mdsnum;
1531 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1532 res_gt(maxres, child_res_id, NULL, NULL)) {
1533 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1534 child_res_id->name[0], parent_res_id->name[0],
1536 GOTO(cleanup, rc = 1);
1539 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1540 *child_res_id, LDLM_IBITS, child_policy,
1541 child_mode, &flags, mds_blocking_ast,
1542 ldlm_completion_ast, NULL, NULL, NULL, 0,
1545 GOTO(cleanup, rc = -EIO);
1548 memset(child_res_id, 0, sizeof(*child_res_id));
1554 switch(cleanup_phase) {
1556 if (child_res_id->name[0] != 0)
1557 ldlm_lock_decref(child_lockh, child_mode);
1559 ldlm_lock_decref(parent_lockh, parent_mode);
1565 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1566 struct lustre_id *id,
1567 struct lustre_handle *parent_lockh,
1568 struct dentry **dparentp, int parent_mode,
1569 __u64 parent_lockpart, int *update_mode,
1570 char *name, int namelen,
1571 struct lustre_handle *child_lockh,
1572 struct dentry **dchildp, int child_mode,
1573 __u64 child_lockpart)
1575 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1576 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1577 struct ldlm_res_id parent_res_id = { .name = {0} };
1578 struct ldlm_res_id child_res_id = { .name = {0} };
1579 unsigned long child_ino = 0; __u32 child_gen = 0;
1580 int rc = 0, cleanup_phase = 0;
1581 struct lustre_id sid;
1582 struct inode *inode;
1585 /* Step 1: Lookup parent */
1586 *dparentp = mds_id2dentry(obd, id, NULL);
1587 if (IS_ERR(*dparentp)) {
1588 rc = PTR_ERR(*dparentp);
1593 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1594 (*dparentp)->d_inode->i_ino, name);
1596 parent_res_id.name[0] = id_fid(id);
1597 parent_res_id.name[1] = id_group(id);
1600 parent_lockh[1].cookie = 0;
1601 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1602 struct ldlm_res_id res_id = { .name = {0} };
1603 ldlm_policy_data_t policy;
1604 int flags = LDLM_FL_ATOMIC_CB;
1606 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1608 res_id.name[0] = id_fid(id);
1609 res_id.name[1] = id_group(id);
1610 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1612 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1613 res_id, LDLM_IBITS, &policy,
1614 *update_mode, &flags,
1616 ldlm_completion_ast,
1617 NULL, NULL, NULL, 0, NULL,
1623 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1626 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1627 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1628 parent_res_id.name[2]);
1632 cleanup_phase = 1; /* parent dentry */
1634 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1635 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1636 if (IS_ERR(*dchildp)) {
1637 rc = PTR_ERR(*dchildp);
1638 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1642 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1644 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1645 * lock. Drop possible UPDATE lock!
1647 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1648 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1650 child_res_id.name[0] = (*dchildp)->d_fid;
1651 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1652 child_ino = (*dchildp)->d_inum;
1653 child_gen = (*dchildp)->d_generation;
1657 inode = (*dchildp)->d_inode;
1659 inode = igrab(inode);
1663 down(&inode->i_sem);
1664 rc = mds_read_inode_sid(obd, inode, &sid);
1667 CERROR("Can't read inode self id, inode %lu, "
1668 "rc %d\n", inode->i_ino, rc);
1673 child_ino = inode->i_ino;
1674 child_gen = inode->i_generation;
1675 child_res_id.name[0] = id_fid(&sid);
1676 child_res_id.name[1] = id_group(&sid);
1680 cleanup_phase = 2; /* child dentry */
1682 /* Step 3: Lock parent and child in resource order. If child doesn't
1683 * exist, we still have to lock the parent and re-lookup. */
1684 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1685 &parent_policy, &child_res_id, child_lockh,
1686 child_mode, &child_policy);
1690 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1691 cleanup_phase = 4; /* child lock */
1693 cleanup_phase = 3; /* parent lock */
1695 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1696 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1697 parent_mode, &child_res_id, child_lockh,
1698 dchildp, child_mode, &child_policy,
1699 name, namelen, &parent_res_id, child_ino,
1711 switch (cleanup_phase) {
1713 ldlm_lock_decref(child_lockh, child_mode);
1715 ldlm_lock_decref(parent_lockh, parent_mode);
1720 if (parent_lockh[1].cookie)
1721 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1729 void mds_reconstruct_generic(struct ptlrpc_request *req)
1731 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1732 mds_req_from_mcd(req, med->med_mcd);
1735 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1736 * instead link the inode into the PENDING directory until it is finally
1737 * released. We can't simply call mds_reint_rename() or some part thereof,
1738 * because we don't have the inode to check for link count/open status until
1739 * after it is locked.
1741 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1742 * before starting journal transaction.
1744 * returns 1 on success
1745 * returns 0 if we lost a race and didn't make a new link
1746 * returns negative on error
1748 static int mds_orphan_add_link(struct mds_update_record *rec,
1749 struct obd_device *obd, struct dentry *dentry)
1751 struct mds_obd *mds = &obd->u.mds;
1752 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1753 struct inode *inode = dentry->d_inode;
1754 struct dentry *pending_child;
1755 char idname[LL_ID_NAMELEN];
1756 int idlen = 0, rc, mode;
1759 LASSERT(inode != NULL);
1760 LASSERT(!mds_inode_is_orphan(inode));
1761 #ifndef HAVE_I_ALLOC_SEM
1762 LASSERT(down_trylock(&inode->i_sem) != 0);
1764 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1766 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1768 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1769 mds_orphan_open_count(inode), inode->i_nlink,
1770 S_ISDIR(inode->i_mode) ? "dir" :
1771 S_ISREG(inode->i_mode) ? "file" : "other",
1772 rec->ur_name, idname);
1774 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1777 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1778 if (IS_ERR(pending_child))
1779 RETURN(PTR_ERR(pending_child));
1781 if (pending_child->d_inode != NULL) {
1782 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1783 LASSERT(pending_child->d_inode == inode);
1784 GOTO(out_dput, rc = 0);
1788 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1789 * linking and return real mode back then -bzzz
1791 mode = inode->i_mode;
1792 inode->i_mode = S_IFREG;
1793 rc = vfs_link(dentry, pending_dir, pending_child);
1795 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1798 mds_inode_set_orphan(inode);
1800 /* return mode and correct i_nlink if inode is directory */
1801 inode->i_mode = mode;
1802 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1803 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1805 if (S_ISDIR(mode)) {
1807 i_nlink_inc(pending_dir);
1808 mark_inode_dirty(inode);
1809 mark_inode_dirty(pending_dir);
1812 GOTO(out_dput, rc = 1);
1814 l_dput(pending_child);
1818 int mds_create_local_dentry(struct mds_update_record *rec,
1819 struct obd_device *obd)
1821 struct mds_obd *mds = &obd->u.mds;
1822 struct inode *id_dir = mds->mds_id_dir->d_inode;
1823 int idlen = 0, rc, cleanup_phase = 0;
1824 struct dentry *new_child = NULL;
1825 char *idname = rec->ur_name;
1826 struct dentry *child = NULL;
1827 struct lustre_handle lockh[2] = {{0}, {0}};
1828 struct lustre_id sid;
1832 down(&id_dir->i_sem);
1833 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1834 id_gen(rec->ur_id1));
1836 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1837 idname, OLID4(rec->ur_id1));
1839 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1842 if (IS_ERR(new_child)) {
1843 CERROR("can't lookup %s: %d\n", idname,
1844 (int) PTR_ERR(new_child));
1845 GOTO(cleanup, rc = PTR_ERR(new_child));
1849 down(&id_dir->i_sem);
1850 rc = mds_read_inode_sid(obd, id_dir, &sid);
1853 CERROR("Can't read inode self id, inode %lu, "
1854 "rc %d\n", id_dir->i_ino, rc);
1858 if (new_child->d_inode != NULL) {
1859 /* nice. we've already have local dentry! */
1860 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1861 (unsigned)new_child->d_inode->i_ino,
1862 (unsigned)new_child->d_inode->i_generation);
1864 id_ino(rec->ur_id1) = id_dir->i_ino;
1865 id_gen(rec->ur_id1) = id_dir->i_generation;
1866 rec->ur_namelen = idlen + 1;
1868 id_fid(rec->ur_id1) = id_fid(&sid);
1869 id_group(rec->ur_id1) = id_group(&sid);
1871 GOTO(cleanup, rc = 0);
1874 /* new, local dentry will be added soon. we need no aliases here */
1877 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1878 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1880 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1881 LCK_EX, lockh, NULL, NULL, 0,
1882 MDS_INODELOCK_UPDATE);
1885 if (IS_ERR(child)) {
1886 rc = PTR_ERR(child);
1887 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1888 CERROR("can't get victim: %d\n", rc);
1893 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1895 GOTO(cleanup, rc = PTR_ERR(handle));
1897 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1898 idlen, id_ino(rec->ur_id1),
1899 id_gen(rec->ur_id1), mds->mds_num,
1900 id_fid(rec->ur_id1));
1902 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1903 (unsigned long)child->d_inode->i_ino,
1904 (unsigned long)child->d_inode->i_generation, rc);
1906 if (S_ISDIR(child->d_inode->i_mode)) {
1907 i_nlink_inc(id_dir);
1908 mark_inode_dirty(id_dir);
1910 mark_inode_dirty(child->d_inode);
1912 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1914 id_ino(rec->ur_id1) = id_dir->i_ino;
1915 id_gen(rec->ur_id1) = id_dir->i_generation;
1916 rec->ur_namelen = idlen + 1;
1918 id_fid(rec->ur_id1) = id_fid(&sid);
1919 id_group(rec->ur_id1) = id_group(&sid);
1923 switch(cleanup_phase) {
1925 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1926 ldlm_lock_decref(lockh, LCK_EX);
1936 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1937 struct ptlrpc_request *slave)
1939 void *cookie, *cookie2;
1940 struct mds_body *body2;
1941 struct mds_body *body;
1945 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1946 LASSERT(body != NULL);
1948 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1949 LASSERT(body2 != NULL);
1951 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1954 memcpy(body2, body, sizeof(*body));
1955 body2->valid &= ~OBD_MD_FLCOOKIE;
1957 if (!(body->valid & OBD_MD_FLEASIZE) &&
1958 !(body->valid & OBD_MD_FLDIREA))
1961 if (body->eadatasize == 0) {
1962 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1966 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1968 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1969 LASSERT(ea != NULL);
1971 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
1972 LASSERT(ea2 != NULL);
1974 memcpy(ea2, ea, body->eadatasize);
1976 if (body->valid & OBD_MD_FLCOOKIE) {
1977 LASSERT(master->rq_repmsg->buflens[2] >=
1978 slave->rq_repmsg->buflens[2]);
1979 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
1980 slave->rq_repmsg->buflens[2]);
1981 LASSERT(cookie != NULL);
1983 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
1984 master->rq_repmsg->buflens[2]);
1985 LASSERT(cookie2 != NULL);
1986 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
1987 body2->valid |= OBD_MD_FLCOOKIE;
1992 static int mds_reint_unlink_remote(struct mds_update_record *rec,
1993 int offset, struct ptlrpc_request *req,
1994 struct lustre_handle *parent_lockh,
1995 int update_mode, struct dentry *dparent,
1996 struct lustre_handle *child_lockh,
1997 struct dentry *dchild)
1999 struct obd_device *obd = req->rq_export->exp_obd;
2000 struct mds_obd *mds = mds_req2mds(req);
2001 struct ptlrpc_request *request = NULL;
2002 int rc = 0, cleanup_phase = 0;
2003 struct mdc_op_data *op_data;
2007 LASSERT(offset == 1 || offset == 3);
2009 /* time to drop i_nlink on remote MDS */
2010 OBD_ALLOC(op_data, sizeof(*op_data));
2011 if (op_data == NULL)
2014 memset(op_data, 0, sizeof(*op_data));
2015 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
2016 op_data->create_mode = rec->ur_mode;
2018 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
2019 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2021 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2022 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
2023 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2026 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2027 op_data->create_mode |= MDS_MODE_REPLAY;
2029 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2030 OBD_FREE(op_data, sizeof(*op_data));
2035 mds_copy_unlink_reply(req, request);
2036 ptlrpc_req_finished(request);
2040 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2043 GOTO(cleanup, rc = PTR_ERR(handle));
2044 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2045 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2050 req->rq_status = rc;
2053 if (parent_lockh[1].cookie != 0)
2054 ldlm_lock_decref(parent_lockh + 1, update_mode);
2056 ldlm_lock_decref(child_lockh, LCK_EX);
2058 ldlm_lock_decref(parent_lockh, LCK_PW);
2060 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2067 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2068 struct ptlrpc_request *req,
2069 struct lustre_handle *lh)
2071 struct dentry *dparent = NULL, *dchild;
2072 struct mds_obd *mds = mds_req2mds(req);
2073 struct obd_device *obd = req->rq_export->exp_obd;
2074 struct mds_body *body = NULL;
2075 struct inode *child_inode = NULL;
2076 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2077 struct lustre_handle child_lockh = {0};
2078 struct lustre_handle child_reuse_lockh = {0};
2079 struct lustre_handle *slave_lockh = NULL;
2080 char idname[LL_ID_NAMELEN];
2081 struct llog_create_locks *lcl = NULL;
2082 void *handle = NULL;
2083 int rc = 0, cleanup_phase = 0;
2084 int unlink_by_id = 0;
2088 LASSERT(offset == 1 || offset == 3);
2090 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2091 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2094 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2096 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2097 DEBUG_REQ(D_HA, req, "unlink replay");
2098 LASSERT(offset == 1); /* should not come from intent */
2099 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2100 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2101 req->rq_repmsg->buflens[2]);
2104 MD_COUNTER_INCREMENT(obd, unlink);
2106 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2107 GOTO(cleanup, rc = -ENOENT);
2109 if (rec->ur_namelen == 1) {
2110 /* this is request to drop i_nlink on local inode */
2112 rec->ur_name = idname;
2113 rc = mds_create_local_dentry(rec, obd);
2114 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2115 DEBUG_REQ(D_HA, req,
2116 "drop nlink on inode "DLID4" (replay)",
2117 OLID4(rec->ur_id1));
2123 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2124 /* master mds for directory asks slave removing inode is already
2126 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2127 LCK_PW, parent_lockh,
2128 &update_mode, rec->ur_name,
2130 MDS_INODELOCK_UPDATE);
2131 if (IS_ERR(dparent))
2132 GOTO(cleanup, rc = PTR_ERR(dparent));
2133 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2134 rec->ur_namelen - 1);
2136 GOTO(cleanup, rc = PTR_ERR(dchild));
2137 child_lockh.cookie = 0;
2138 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2139 LASSERT(dchild->d_inode != NULL);
2140 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2142 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2143 parent_lockh, &dparent,
2144 LCK_PW, MDS_INODELOCK_UPDATE,
2145 &update_mode, rec->ur_name,
2146 rec->ur_namelen, &child_lockh,
2148 MDS_INODELOCK_LOOKUP |
2149 MDS_INODELOCK_UPDATE);
2154 if (dchild->d_flags & DCACHE_CROSS_REF) {
2155 /* we should have parent lock only here */
2156 LASSERT(unlink_by_id == 0);
2157 LASSERT(dchild->d_mdsnum != mds->mds_num);
2158 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2159 update_mode, dparent, &child_lockh, dchild);
2163 cleanup_phase = 1; /* dchild, dparent, locks */
2166 child_inode = dchild->d_inode;
2167 if (child_inode == NULL) {
2168 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2169 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2170 GOTO(cleanup, rc = -ENOENT);
2173 cleanup_phase = 2; /* dchild has a lock */
2175 /* We have to do these checks ourselves, in case we are making an
2176 * orphan. The client tells us whether rmdir() or unlink() was called,
2177 * so we need to return appropriate errors (bug 72).
2179 * We don't have to check permissions, because vfs_rename (called from
2180 * mds_open_unlink_rename) also calls may_delete. */
2181 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2182 if (!S_ISDIR(child_inode->i_mode))
2183 GOTO(cleanup, rc = -ENOTDIR);
2185 if (S_ISDIR(child_inode->i_mode))
2186 GOTO(cleanup, rc = -EISDIR);
2189 /* handle splitted dir */
2190 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2194 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2195 * reuse (see bug 2029). */
2196 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2199 cleanup_phase = 3; /* child inum lock */
2201 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2203 /* ldlm_reply in buf[0] if called via intent */
2209 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2210 LASSERT(body != NULL);
2212 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2213 DOWN_READ_I_ALLOC_SEM(child_inode);
2214 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2216 /* If this is potentially the last reference to this inode, get the
2217 * OBD EA data first so the client can destroy OST objects. We
2218 * only do the object removal later if no open files/links remain. */
2219 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2220 child_inode->i_nlink == 1) {
2221 if (mds_orphan_open_count(child_inode) > 0) {
2222 /* need to lock pending_dir before transaction */
2223 down(&mds->mds_pending_dir->d_inode->i_sem);
2224 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2225 } else if (S_ISREG(child_inode->i_mode)) {
2226 mds_pack_inode2body(obd, body, child_inode, 0);
2227 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2228 body, child_inode, MDS_PACK_MD_LOCK, 0);
2232 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2233 switch (child_inode->i_mode & S_IFMT) {
2235 /* Drop any lingering child directories before we start our
2236 * transaction, to avoid doing multiple inode dirty/delete
2237 * in our compound transaction (bug 1321). */
2238 shrink_dcache_parent(dchild);
2239 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2242 GOTO(cleanup, rc = PTR_ERR(handle));
2243 rc = vfs_rmdir(dparent->d_inode, dchild);
2246 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2247 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2249 handle = fsfilt_start_log(obd, dparent->d_inode,
2250 FSFILT_OP_UNLINK, NULL,
2251 le32_to_cpu(lmm->lmm_stripe_count));
2253 GOTO(cleanup, rc = PTR_ERR(handle));
2254 rc = vfs_unlink(dparent->d_inode, dchild);
2262 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2265 GOTO(cleanup, rc = PTR_ERR(handle));
2266 rc = vfs_unlink(dparent->d_inode, dchild);
2269 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2272 GOTO(cleanup, rc = -EINVAL);
2275 if (rc == 0 && child_inode->i_nlink == 0) {
2276 if (mds_orphan_open_count(child_inode) > 0)
2277 rc = mds_orphan_add_link(rec, obd, dchild);
2280 GOTO(cleanup, rc = 0);
2282 if (!S_ISREG(child_inode->i_mode))
2285 if (!(body->valid & OBD_MD_FLEASIZE)) {
2286 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2287 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2288 } else if (mds_log_op_unlink(obd, child_inode,
2289 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2290 req->rq_repmsg->buflens[offset + 1],
2291 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2292 req->rq_repmsg->buflens[offset + 2],
2294 body->valid |= OBD_MD_FLCOOKIE;
2297 rc = mds_destroy_object(obd, child_inode, 1);
2299 CERROR("can't remove OST object, err %d\n",
2311 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2312 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2313 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2315 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2317 CERROR("error on parent setattr: rc = %d\n", err);
2319 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2320 handle, req, rc, 0);
2322 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2323 "unlinked", 0, NULL);
2324 switch(cleanup_phase) {
2325 case 5: /* pending_dir semaphore */
2326 up(&mds->mds_pending_dir->d_inode->i_sem);
2327 case 4: /* child inode semaphore */
2328 UP_READ_I_ALLOC_SEM(child_inode);
2329 /* handle splitted dir */
2331 /* master directory can be non-empty or something else ... */
2332 mds_unlink_slave_objs(obd, dchild);
2335 ptlrpc_save_llog_lock(req, lcl);
2336 case 3: /* child ino-reuse lock */
2337 if (rc && body != NULL) {
2338 // Don't unlink the OST objects if the MDS unlink failed
2342 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2344 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2345 case 2: /* child lock */
2346 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2347 if (child_lockh.cookie)
2348 ldlm_lock_decref(&child_lockh, LCK_EX);
2349 case 1: /* child and parent dentry, parent lock */
2351 if (parent_lockh[1].cookie != 0)
2352 ldlm_lock_decref(parent_lockh + 1, update_mode);
2355 ldlm_lock_decref(parent_lockh, LCK_PW);
2357 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2364 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2367 req->rq_status = rc;
2372 * to service requests from remote MDS to increment i_nlink
2374 static int mds_reint_link_acquire(struct mds_update_record *rec,
2375 int offset, struct ptlrpc_request *req,
2376 struct lustre_handle *lh)
2378 struct obd_device *obd = req->rq_export->exp_obd;
2379 struct ldlm_res_id src_res_id = { .name = {0} };
2380 struct lustre_handle *handle = NULL, src_lockh = {0};
2381 struct mds_obd *mds = mds_req2mds(req);
2382 int rc = 0, cleanup_phase = 0;
2383 struct dentry *de_src = NULL;
2384 ldlm_policy_data_t policy;
2385 int flags = LDLM_FL_ATOMIC_CB;
2388 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2389 obd->obd_name, OLID4(rec->ur_id1));
2391 /* Step 1: Lookup the source inode and target directory by ID */
2392 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2394 GOTO(cleanup, rc = PTR_ERR(de_src));
2395 cleanup_phase = 1; /* source dentry */
2397 src_res_id.name[0] = id_fid(rec->ur_id1);
2398 src_res_id.name[1] = id_group(rec->ur_id1);
2399 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2401 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2402 src_res_id, LDLM_IBITS, &policy,
2403 LCK_EX, &flags, mds_blocking_ast,
2404 ldlm_completion_ast, NULL, NULL,
2405 NULL, 0, NULL, &src_lockh);
2407 GOTO(cleanup, rc = -ENOLCK);
2408 cleanup_phase = 2; /* lock */
2410 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2412 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2413 if (IS_ERR(handle)) {
2414 rc = PTR_ERR(handle);
2417 i_nlink_inc(de_src->d_inode);
2418 mark_inode_dirty(de_src->d_inode);
2422 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2423 handle, req, rc, 0);
2424 switch (cleanup_phase) {
2427 ldlm_lock_decref(&src_lockh, LCK_EX);
2429 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2435 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2438 req->rq_status = rc;
2443 * request to link to foreign inode:
2444 * - acquire i_nlinks on this inode
2447 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2448 int offset, struct ptlrpc_request *req,
2449 struct lustre_handle *lh)
2451 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2452 struct obd_device *obd = req->rq_export->exp_obd;
2453 struct dentry *de_tgt_dir = NULL;
2454 struct mds_obd *mds = mds_req2mds(req);
2455 int rc = 0, cleanup_phase = 0;
2456 struct mdc_op_data *op_data;
2457 struct ptlrpc_request *request = NULL;
2461 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2462 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2463 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2464 OLID4(rec->ur_id1));
2466 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2467 tgt_dir_lockh, &update_mode,
2468 rec->ur_name, rec->ur_namelen - 1,
2469 MDS_INODELOCK_UPDATE);
2470 if (IS_ERR(de_tgt_dir))
2471 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2474 OBD_ALLOC(op_data, sizeof(*op_data));
2475 if (op_data == NULL)
2476 GOTO(cleanup, rc = -ENOMEM);
2478 memset(op_data, 0, sizeof(*op_data));
2479 op_data->id1 = *(rec->ur_id1);
2480 rc = md_link(mds->mds_md_exp, op_data, &request);
2481 OBD_FREE(op_data, sizeof(*op_data));
2484 ptlrpc_req_finished(request);
2490 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2492 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2493 if (IS_ERR(handle)) {
2494 rc = PTR_ERR(handle);
2500 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2501 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2502 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2503 id_fid(rec->ur_id1));
2506 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2507 handle, req, rc, 0);
2509 switch (cleanup_phase) {
2512 OBD_ALLOC(op_data, sizeof(*op_data));
2513 if (op_data != NULL) {
2515 memset(op_data, 0, sizeof(*op_data));
2517 op_data->id1 = *(rec->ur_id1);
2518 op_data->create_mode = rec->ur_mode;
2520 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2521 OBD_FREE(op_data, sizeof(*op_data));
2523 ptlrpc_req_finished(request);
2525 CERROR("error %d while dropping i_nlink on "
2526 "remote inode\n", rc);
2529 CERROR("rc %d prevented dropping i_nlink on "
2530 "remote inode\n", -ENOMEM);
2536 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2538 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2541 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2543 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2549 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2552 req->rq_status = rc;
2556 static int mds_reint_link(struct mds_update_record *rec, int offset,
2557 struct ptlrpc_request *req,
2558 struct lustre_handle *lh)
2560 struct obd_device *obd = req->rq_export->exp_obd;
2561 struct dentry *de_src = NULL;
2562 struct dentry *de_tgt_dir = NULL;
2563 struct dentry *dchild = NULL;
2564 struct mds_obd *mds = mds_req2mds(req);
2565 struct lustre_handle *handle = NULL;
2566 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2567 struct ldlm_res_id src_res_id = { .name = {0} };
2568 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2569 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2570 ldlm_policy_data_t tgt_dir_policy =
2571 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2572 int rc = 0, cleanup_phase = 0;
2574 int update_mode = 0;
2578 LASSERT(offset == 1);
2580 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2581 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2582 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2585 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2586 MD_COUNTER_INCREMENT(obd, link);
2588 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2589 GOTO(cleanup, rc = -ENOENT);
2591 if (id_group(rec->ur_id1) != mds->mds_num) {
2592 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2596 if (rec->ur_namelen == 1) {
2597 rc = mds_reint_link_acquire(rec, offset, req, lh);
2601 /* Step 1: Lookup the source inode and target directory by ID */
2602 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2604 GOTO(cleanup, rc = PTR_ERR(de_src));
2606 cleanup_phase = 1; /* source dentry */
2608 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2609 if (IS_ERR(de_tgt_dir)) {
2610 rc = PTR_ERR(de_tgt_dir);
2615 cleanup_phase = 2; /* target directory dentry */
2617 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2618 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2619 rec->ur_name, de_src->d_inode->i_ino);
2621 /* Step 2: Take the two locks */
2622 src_res_id.name[0] = id_fid(rec->ur_id1);
2623 src_res_id.name[1] = id_group(rec->ur_id1);
2624 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2625 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2628 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2629 int flags = LDLM_FL_ATOMIC_CB;
2630 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2632 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2633 tgt_dir_res_id, LDLM_IBITS,
2634 &src_policy, update_mode, &flags,
2636 ldlm_completion_ast, NULL, NULL,
2637 NULL, 0, NULL, tgt_dir_lockh + 1);
2639 GOTO(cleanup, rc = -ENOLCK);
2642 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2643 rec->ur_namelen - 1);
2644 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2645 (unsigned long)id_fid(rec->ur_id2),
2646 (unsigned long)id_group(rec->ur_id2),
2647 tgt_dir_res_id.name[2]);
2650 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2651 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2652 LCK_EX, &tgt_dir_policy);
2656 cleanup_phase = 3; /* locks */
2658 /* Step 3: Lookup the child */
2659 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2660 rec->ur_namelen - 1);
2661 if (IS_ERR(dchild)) {
2662 rc = PTR_ERR(dchild);
2663 if (rc != -EPERM && rc != -EACCES)
2664 CERROR("child lookup error %d\n", rc);
2668 cleanup_phase = 4; /* child dentry */
2670 if (dchild->d_inode) {
2671 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2672 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2677 /* Step 4: Do it. */
2678 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2680 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2681 if (IS_ERR(handle)) {
2682 rc = PTR_ERR(handle);
2686 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2687 if (rc && rc != -EPERM && rc != -EACCES)
2688 CERROR("vfs_link error %d\n", rc);
2690 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2691 handle, req, rc, 0);
2694 switch (cleanup_phase) {
2695 case 4: /* child dentry */
2699 ldlm_lock_decref(&src_lockh, LCK_EX);
2700 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2702 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2703 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2705 case 2: /* target dentry */
2707 if (tgt_dir_lockh[1].cookie && update_mode)
2708 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2712 case 1: /* source dentry */
2717 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2720 req->rq_status = rc;
2724 /* The idea here is that we need to get four locks in the end:
2725 * one on each parent directory, one on each child. We need to take
2726 * these locks in some kind of order (to avoid deadlocks), and the order
2727 * I selected is "increasing resource number" order. We need to look up
2728 * the children, however, before we know what the resource number(s) are.
2729 * Thus the following plan:
2731 * 1,2. Look up the parents
2732 * 3,4. Look up the children
2733 * 5. Take locks on the parents and children, in order
2734 * 6. Verify that the children haven't changed since they were looked up
2736 * If there was a race and the children changed since they were first looked
2737 * up, it is possible that mds_verify_child() will be able to just grab the
2738 * lock on the new child resource (if it has a higher resource than any other)
2739 * but we need to compare against not only its parent, but also against the
2740 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2742 * We need the fancy igrab() on the child inodes because we aren't holding a
2743 * lock on the parent after the lookup is done, so dentry->d_inode may change
2744 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2746 static int mds_get_parents_children_locked(struct obd_device *obd,
2747 struct mds_obd *mds,
2748 struct lustre_id *p1_id,
2749 struct dentry **de_srcdirp,
2750 struct lustre_id *p2_id,
2751 struct dentry **de_tgtdirp,
2753 const char *old_name, int old_len,
2754 struct dentry **de_oldp,
2755 const char *new_name, int new_len,
2756 struct dentry **de_newp,
2757 struct lustre_handle *dlm_handles,
2760 struct ldlm_res_id p1_res_id = { .name = {0} };
2761 struct ldlm_res_id p2_res_id = { .name = {0} };
2762 struct ldlm_res_id c1_res_id = { .name = {0} };
2763 struct ldlm_res_id c2_res_id = { .name = {0} };
2764 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2765 /* Only dentry should disappear, but the inode itself would be
2766 intact otherwise. */
2767 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2768 /* If something is going to be replaced, both dentry and inode locks are
2770 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2771 struct ldlm_res_id *maxres_src, *maxres_tgt;
2772 struct inode *inode;
2773 int rc = 0, cleanup_phase = 0;
2774 __u32 child_gen1 = 0;
2775 __u32 child_gen2 = 0;
2776 unsigned long child_ino1 = 0;
2777 unsigned long child_ino2 = 0;
2780 /* Step 1: Lookup the source directory */
2781 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2782 if (IS_ERR(*de_srcdirp))
2783 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2785 cleanup_phase = 1; /* source directory dentry */
2787 p1_res_id.name[0] = id_fid(p1_id);
2788 p1_res_id.name[1] = id_group(p1_id);
2790 /* Step 2: Lookup the target directory */
2791 if (id_equal_stc(p1_id, p2_id)) {
2792 *de_tgtdirp = dget(*de_srcdirp);
2794 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2795 if (IS_ERR(*de_tgtdirp)) {
2796 rc = PTR_ERR(*de_tgtdirp);
2802 cleanup_phase = 2; /* target directory dentry */
2804 p2_res_id.name[0] = id_fid(p2_id);
2805 p2_res_id.name[1] = id_group(p2_id);
2808 dlm_handles[5].cookie = 0;
2809 dlm_handles[6].cookie = 0;
2811 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2813 * get a temp lock on just fid, group to flush client cache and
2814 * to protect dirs from concurrent splitting.
2816 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2817 LCK_PW, &p_policy, &p2_res_id,
2818 &dlm_handles[6], LCK_PW, &p_policy);
2822 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2824 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2827 CDEBUG(D_INFO, "take locks on "
2828 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2829 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2830 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2835 /* Step 3: Lookup the source child entry */
2836 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2838 if (IS_ERR(*de_oldp)) {
2839 rc = PTR_ERR(*de_oldp);
2840 CERROR("old child lookup error (%.*s): %d\n",
2841 old_len - 1, old_name, rc);
2845 cleanup_phase = 4; /* original name dentry */
2847 inode = (*de_oldp)->d_inode;
2848 if (inode != NULL) {
2849 struct lustre_id sid;
2851 inode = igrab(inode);
2853 GOTO(cleanup, rc = -ENOENT);
2855 down(&inode->i_sem);
2856 rc = mds_read_inode_sid(obd, inode, &sid);
2859 CERROR("Can't read inode self id, inode %lu, "
2860 "rc %d\n", inode->i_ino, rc);
2865 child_ino1 = inode->i_ino;
2866 child_gen1 = inode->i_generation;
2867 c1_res_id.name[0] = id_fid(&sid);
2868 c1_res_id.name[1] = id_group(&sid);
2870 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2871 child_ino1 = (*de_oldp)->d_inum;
2872 child_gen1 = (*de_oldp)->d_generation;
2873 c1_res_id.name[0] = (*de_oldp)->d_fid;
2874 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2876 GOTO(cleanup, rc = -ENOENT);
2879 /* Step 4: Lookup the target child entry */
2880 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2882 if (IS_ERR(*de_newp)) {
2883 rc = PTR_ERR(*de_newp);
2884 CERROR("new child lookup error (%.*s): %d\n",
2885 old_len - 1, old_name, rc);
2889 cleanup_phase = 5; /* target dentry */
2891 inode = (*de_newp)->d_inode;
2892 if (inode != NULL) {
2893 struct lustre_id sid;
2895 inode = igrab(inode);
2899 down(&inode->i_sem);
2900 rc = mds_read_inode_sid(obd, inode, &sid);
2903 CERROR("Can't read inode self id, inode %lu, "
2904 "rc %d\n", inode->i_ino, rc);
2908 child_ino2 = inode->i_ino;
2909 child_gen2 = inode->i_generation;
2910 c2_res_id.name[0] = id_fid(&sid);
2911 c2_res_id.name[1] = id_group(&sid);
2913 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2914 child_ino2 = (*de_newp)->d_inum;
2915 child_gen2 = (*de_newp)->d_generation;
2916 c2_res_id.name[0] = (*de_newp)->d_fid;
2917 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2921 /* Step 5: Take locks on the parents and child(ren) */
2922 maxres_src = &p1_res_id;
2923 maxres_tgt = &p2_res_id;
2924 cleanup_phase = 5; /* target dentry */
2926 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2927 maxres_src = &c1_res_id;
2928 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2929 maxres_tgt = &c2_res_id;
2931 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2933 &p2_res_id, &dlm_handles[1], parent_mode,
2935 &c1_res_id, &dlm_handles[2], child_mode,
2937 &c2_res_id, &dlm_handles[3], child_mode,
2942 cleanup_phase = 6; /* parent and child(ren) locks */
2944 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2945 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2946 parent_mode, &c1_res_id, &dlm_handles[2],
2947 de_oldp, child_mode, &c1_policy, old_name, old_len,
2948 maxres_tgt, child_ino1, child_gen1);
2950 if (c2_res_id.name[0] != 0)
2951 ldlm_lock_decref(&dlm_handles[3], child_mode);
2952 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2959 if (!DENTRY_VALID(*de_oldp))
2960 GOTO(cleanup, rc = -ENOENT);
2962 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2963 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2964 parent_mode, &c2_res_id, &dlm_handles[3],
2965 de_newp, child_mode, &c2_policy, new_name,
2966 new_len, maxres_src, child_ino2, child_gen2);
2968 ldlm_lock_decref(&dlm_handles[2], child_mode);
2969 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2979 switch (cleanup_phase) {
2980 case 6: /* child lock(s) */
2981 if (c2_res_id.name[0] != 0)
2982 ldlm_lock_decref(&dlm_handles[3], child_mode);
2983 if (c1_res_id.name[0] != 0)
2984 ldlm_lock_decref(&dlm_handles[2], child_mode);
2985 if (dlm_handles[1].cookie != 0)
2986 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2987 if (dlm_handles[0].cookie != 0)
2988 ldlm_lock_decref(&dlm_handles[0], parent_mode);
2989 case 5: /* target dentry */
2991 case 4: /* source dentry */
2995 if (dlm_handles[5].cookie != 0)
2996 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
2997 if (dlm_handles[6].cookie != 0)
2998 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3000 case 2: /* target directory dentry */
3001 l_dput(*de_tgtdirp);
3002 case 1: /* source directry dentry */
3003 l_dput(*de_srcdirp);
3011 * checks if dentry can be removed. This function also handles cross-ref
3014 static int mds_check_for_rename(struct obd_device *obd,
3015 struct dentry *dentry)
3017 struct mds_obd *mds = &obd->u.mds;
3018 struct lustre_handle *rlockh;
3019 struct ptlrpc_request *req;
3020 struct mdc_op_data *op_data;
3021 struct lookup_intent it;
3022 int handle_size, rc = 0;
3025 LASSERT(dentry != NULL);
3027 if (dentry->d_inode) {
3028 if (S_ISDIR(dentry->d_inode->i_mode) &&
3029 !mds_is_dir_empty(obd, dentry))
3032 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
3033 handle_size = sizeof(struct lustre_handle);
3035 OBD_ALLOC(rlockh, handle_size);
3039 memset(rlockh, 0, handle_size);
3040 OBD_ALLOC(op_data, sizeof(*op_data));
3041 if (op_data == NULL) {
3042 OBD_FREE(rlockh, handle_size);
3045 memset(op_data, 0, sizeof(*op_data));
3046 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3048 it.it_op = IT_UNLINK;
3049 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3052 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3053 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3054 mds_blocking_ast, NULL);
3055 OBD_FREE(op_data, sizeof(*op_data));
3059 OBD_FREE(it.d.fs_data,
3060 sizeof(struct lustre_intent_data));
3063 if (rlockh->cookie != 0)
3064 ldlm_lock_decref(rlockh, LCK_EX);
3066 if (LUSTRE_IT(&it)->it_data) {
3067 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3068 ptlrpc_req_finished(req);
3071 if (LUSTRE_IT(&it)->it_status)
3072 rc = LUSTRE_IT(&it)->it_status;
3073 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3074 OBD_FREE(rlockh, handle_size);
3079 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3080 struct ptlrpc_request *req, struct lustre_id *id,
3081 struct dentry *de_dir, struct dentry *de)
3083 struct obd_device *obd = req->rq_export->exp_obd;
3084 struct mds_obd *mds = mds_req2mds(req);
3085 void *handle = NULL;
3091 * name exists and points to local inode try to unlink this name
3092 * and create new one.
3094 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3095 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3096 (unsigned long)de->d_inode->i_generation);
3098 /* checking if we can remove local dentry. */
3099 rc = mds_check_for_rename(obd, de);
3103 handle = fsfilt_start(obd, de_dir->d_inode,
3104 FSFILT_OP_RENAME, NULL);
3106 GOTO(cleanup, rc = PTR_ERR(handle));
3107 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3110 } else if (de->d_flags & DCACHE_CROSS_REF) {
3111 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3112 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3113 (unsigned long)de->d_fid);
3115 /* checking if we can remove local dentry. */
3116 rc = mds_check_for_rename(obd, de);
3121 * to be fully POSIX compatible, we should add one more check:
3123 * if de_new is subdir of dir rec->ur_id1. If so - return
3126 * I do not know how to implement it right now, because
3127 * inodes/dentries for new and old names lie on different MDS,
3128 * so add this notice here just to make it visible for the rest
3129 * of developers and do not forget about. And when this check
3130 * will be added, del_cross_ref should gone, that is local
3131 * dentry is able to be removed if all checks passed. --umka
3134 handle = fsfilt_start(obd, de_dir->d_inode,
3135 FSFILT_OP_RENAME, NULL);
3137 GOTO(cleanup, rc = PTR_ERR(handle));
3138 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3142 /* name doesn't exist. the simplest case. */
3143 handle = fsfilt_start(obd, de_dir->d_inode,
3144 FSFILT_OP_LINK, NULL);
3146 GOTO(cleanup, rc = PTR_ERR(handle));
3149 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3150 rec->ur_tgtlen - 1, id_ino(id),
3151 id_gen(id), id_group(id), id_fid(id));
3153 CERROR("add_dir_entry() returned error %d\n", rc);
3159 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3160 handle, req, rc, 0);
3165 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3166 struct ptlrpc_request *req, struct dentry *de_dir,
3169 struct obd_device *obd = req->rq_export->exp_obd;
3170 struct mds_obd *mds = mds_req2mds(req);
3171 void *handle = NULL;
3175 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3177 GOTO(cleanup, rc = PTR_ERR(handle));
3178 rc = fsfilt_del_dir_entry(obd, de);
3183 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3184 handle, req, rc, 0);
3188 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3189 int offset, struct ptlrpc_request *req)
3191 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3192 struct obd_device *obd = req->rq_export->exp_obd;
3193 struct mds_obd *mds = mds_req2mds(req);
3194 struct lustre_handle child_lockh = {0};
3195 struct dentry *de_tgtdir = NULL;
3196 struct dentry *de_new = NULL;
3197 int cleanup_phase = 0;
3198 int update_mode, rc = 0;
3202 * another MDS executing rename operation has asked us to create target
3203 * name. such a creation should destroy existing target name.
3205 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3206 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3208 /* first, lookup the target */
3209 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3210 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3211 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3212 &child_lockh, &de_new, LCK_EX,
3213 MDS_INODELOCK_LOOKUP);
3220 LASSERT(de_tgtdir->d_inode);
3223 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3229 if (cleanup_phase == 1) {
3231 if (parent_lockh[1].cookie != 0)
3232 ldlm_lock_decref(parent_lockh + 1, update_mode);
3234 ldlm_lock_decref(parent_lockh, LCK_PW);
3235 if (child_lockh.cookie != 0)
3236 ldlm_lock_decref(&child_lockh, LCK_EX);
3241 req->rq_status = rc;
3245 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3246 struct ptlrpc_request *req)
3248 struct obd_device *obd = req->rq_export->exp_obd;
3249 struct ptlrpc_request *req2 = NULL;
3250 struct dentry *de_srcdir = NULL;
3251 struct dentry *de_old = NULL;
3252 struct mds_obd *mds = mds_req2mds(req);
3253 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3254 struct lustre_handle child_lockh = {0};
3255 struct mdc_op_data *op_data;
3256 int update_mode, rc = 0;
3259 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3260 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3262 OBD_ALLOC(op_data, sizeof(*op_data));
3263 if (op_data == NULL)
3265 memset(op_data, 0, sizeof(*op_data));
3267 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3268 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3269 &update_mode, rec->ur_name,
3270 rec->ur_namelen, &child_lockh, &de_old,
3271 LCK_EX, MDS_INODELOCK_LOOKUP);
3273 OBD_FREE(op_data, sizeof(*op_data));
3278 LASSERT(de_srcdir->d_inode);
3282 * we already know the target should be created on another MDS so, we
3283 * have to request that MDS to do it.
3286 /* prepare source id */
3287 if (de_old->d_flags & DCACHE_CROSS_REF) {
3288 LASSERT(de_old->d_inode == NULL);
3289 CDEBUG(D_OTHER, "request to move remote name\n");
3290 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3291 } else if (de_old->d_inode == NULL) {
3292 /* oh, source doesn't exist */
3293 OBD_FREE(op_data, sizeof(*op_data));
3294 GOTO(cleanup, rc = -ENOENT);
3296 struct lustre_id sid;
3297 struct inode *inode = de_old->d_inode;
3299 LASSERT(inode != NULL);
3300 CDEBUG(D_OTHER, "request to move local name\n");
3301 id_ino(&op_data->id1) = inode->i_ino;
3302 id_group(&op_data->id1) = mds->mds_num;
3303 id_gen(&op_data->id1) = inode->i_generation;
3305 down(&inode->i_sem);
3306 rc = mds_read_inode_sid(obd, inode, &sid);
3309 CERROR("Can't read inode self id, "
3310 "inode %lu, rc = %d\n",
3315 id_fid(&op_data->id1) = id_fid(&sid);
3318 op_data->id2 = *rec->ur_id2;
3319 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3320 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3321 OBD_FREE(op_data, sizeof(*op_data));
3326 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3332 ptlrpc_req_finished(req2);
3335 if (parent_lockh[1].cookie != 0)
3336 ldlm_lock_decref(parent_lockh + 1, update_mode);
3338 ldlm_lock_decref(parent_lockh, LCK_PW);
3339 if (child_lockh.cookie != 0)
3340 ldlm_lock_decref(&child_lockh, LCK_EX);
3345 req->rq_status = rc;
3349 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3350 struct ptlrpc_request *req, struct lustre_handle *lockh)
3352 struct obd_device *obd = req->rq_export->exp_obd;
3353 struct dentry *de_srcdir = NULL;
3354 struct dentry *de_tgtdir = NULL;
3355 struct dentry *de_old = NULL;
3356 struct dentry *de_new = NULL;
3357 struct inode *old_inode = NULL, *new_inode = NULL;
3358 struct mds_obd *mds = mds_req2mds(req);
3359 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3360 struct mds_body *body = NULL;
3361 struct llog_create_locks *lcl = NULL;
3362 struct lov_mds_md *lmm = NULL;
3363 int rc = 0, cleanup_phase = 0;
3364 void *handle = NULL;
3367 LASSERT(offset == 1);
3369 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3370 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3373 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3375 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3376 DEBUG_REQ(D_HA, req, "rename replay");
3377 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3378 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3379 req->rq_repmsg->buflens[2]);
3382 MD_COUNTER_INCREMENT(obd, rename);
3384 if (rec->ur_namelen == 1) {
3385 rc = mds_reint_rename_create_name(rec, offset, req);
3389 /* check if new name should be located on remote target. */
3390 if (id_group(rec->ur_id2) != mds->mds_num) {
3391 rc = mds_reint_rename_to_remote(rec, offset, req);
3395 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3396 rec->ur_id2, &de_tgtdir, LCK_PW,
3397 rec->ur_name, rec->ur_namelen,
3398 &de_old, rec->ur_tgt,
3399 rec->ur_tgtlen, &de_new,
3400 dlm_handles, LCK_EX);
3404 cleanup_phase = 1; /* parent(s), children, locks */
3405 old_inode = de_old->d_inode;
3406 new_inode = de_new->d_inode;
3408 /* sanity check for src inode */
3409 if (de_old->d_flags & DCACHE_CROSS_REF) {
3410 LASSERT(de_old->d_inode == NULL);
3413 * in the case of cross-ref dir, we can perform this check only
3414 * if child and parent lie on the same mds. This is because
3415 * otherwise they can have the same inode numbers.
3417 if (de_old->d_mdsnum == mds->mds_num) {
3418 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3419 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3420 GOTO(cleanup, rc = -EINVAL);
3423 LASSERT(de_old->d_inode != NULL);
3424 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3425 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3426 GOTO(cleanup, rc = -EINVAL);
3429 /* sanity check for dest inode */
3430 if (de_new->d_flags & DCACHE_CROSS_REF) {
3431 LASSERT(new_inode == NULL);
3433 /* the same check about target dentry. */
3434 if (de_new->d_mdsnum == mds->mds_num) {
3435 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3436 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3437 GOTO(cleanup, rc = -EINVAL);
3441 * regular files usualy do not have ->rename() implemented. But
3442 * we handle only this case when @de_new is cross-ref entry,
3443 * because in other cases it will be handled by vfs_rename().
3445 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3446 !de_old->d_inode->i_op->rename))
3447 GOTO(cleanup, rc = -EPERM);
3450 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3451 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3452 GOTO(cleanup, rc = -EINVAL);
3457 * check if inodes point to each other. This should be checked before
3458 * is_subdir() check, as for the same entries it will think that they
3461 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3462 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3463 old_inode == new_inode)
3464 GOTO(cleanup, rc = 0);
3466 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3468 * check if we are moving old entry into its child. 2.6 does not check
3469 * for this in vfs_rename() anymore.
3471 if (is_subdir(de_new, de_old))
3472 GOTO(cleanup, rc = -EINVAL);
3476 * if we are about to remove the target at first, pass the EA of that
3477 * inode to client to perform and cleanup on OST.
3479 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3480 LASSERT(body != NULL);
3482 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3484 DOWN_READ_I_ALLOC_SEM(new_inode);
3486 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3488 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3489 new_inode->i_nlink == 2) ||
3490 new_inode->i_nlink == 1)) {
3491 if (mds_orphan_open_count(new_inode) > 0) {
3492 /* need to lock pending_dir before transaction */
3493 down(&mds->mds_pending_dir->d_inode->i_sem);
3494 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3495 } else if (S_ISREG(new_inode->i_mode)) {
3496 mds_pack_inode2body(obd, body, new_inode, 0);
3497 mds_pack_md(obd, req->rq_repmsg, 1, body,
3498 new_inode, MDS_PACK_MD_LOCK, 0);
3502 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3503 de_srcdir->d_inode->i_sb);
3505 if (de_old->d_flags & DCACHE_CROSS_REF) {
3506 struct lustre_id old_id;
3508 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3510 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3515 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3520 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3521 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3522 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3525 GOTO(cleanup, rc = PTR_ERR(handle));
3528 de_old->d_fsdata = req;
3529 de_new->d_fsdata = req;
3530 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3533 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3534 if (mds_orphan_open_count(new_inode) > 0)
3535 rc = mds_orphan_add_link(rec, obd, de_new);
3538 GOTO(cleanup, rc = 0);
3540 if (!S_ISREG(new_inode->i_mode))
3543 if (!(body->valid & OBD_MD_FLEASIZE)) {
3544 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3545 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3546 } else if (mds_log_op_unlink(obd, new_inode,
3547 lustre_msg_buf(req->rq_repmsg,1,0),
3548 req->rq_repmsg->buflens[1],
3549 lustre_msg_buf(req->rq_repmsg,2,0),
3550 req->rq_repmsg->buflens[2],
3552 body->valid |= OBD_MD_FLCOOKIE;
3555 rc = mds_destroy_object(obd, old_inode, 1);
3557 CERROR("can't remove OST object, err %d\n",
3564 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3565 handle, req, rc, 0);
3567 switch (cleanup_phase) {
3569 up(&mds->mds_pending_dir->d_inode->i_sem);
3572 UP_READ_I_ALLOC_SEM(new_inode);
3575 if (dlm_handles[5].cookie != 0)
3576 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3577 if (dlm_handles[6].cookie != 0)
3578 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3581 ptlrpc_save_llog_lock(req, lcl);
3584 if (dlm_handles[3].cookie != 0)
3585 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3586 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3587 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3588 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3590 if (dlm_handles[3].cookie != 0)
3591 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3592 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3593 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3594 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3603 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3606 req->rq_status = rc;
3610 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3611 struct ptlrpc_request *, struct lustre_handle *);
3613 static mds_reinter reinters[REINT_MAX + 1] = {
3614 [REINT_SETATTR] mds_reint_setattr,
3615 [REINT_CREATE] mds_reint_create,
3616 [REINT_LINK] mds_reint_link,
3617 [REINT_UNLINK] mds_reint_unlink,
3618 [REINT_RENAME] mds_reint_rename,
3619 [REINT_OPEN] mds_open
3622 int mds_reint_rec(struct mds_update_record *rec, int offset,
3623 struct ptlrpc_request *req, struct lustre_handle *lockh)
3625 struct obd_device *obd = req->rq_export->exp_obd;
3626 struct lvfs_run_ctxt saved;
3629 /* checked by unpacker */
3630 LASSERT(rec->ur_opcode <= REINT_MAX &&
3631 reinters[rec->ur_opcode] != NULL);
3633 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3634 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3635 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);