1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mds/mds_reint.c
5 * Lustre Metadata Server (mds) reintegration routines
7 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
8 * Author: Peter Braam <braam@clusterfs.com>
9 * Author: Andreas Dilger <adilger@clusterfs.com>
10 * Author: Phil Schwan <phil@clusterfs.com>
12 * This file is part of Lustre, http://www.lustre.org.
14 * Lustre is free software; you can redistribute it and/or
15 * modify it under the terms of version 2 of the GNU General Public
16 * License as published by the Free Software Foundation.
18 * Lustre is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Lustre; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # define EXPORT_SYMTAB
31 #define DEBUG_SUBSYSTEM S_MDS
34 #include <linux/jbd.h>
35 #include <linux/namei.h>
36 #include <linux/ext3_fs.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_log.h>
45 #include <linux/lustre_fsfilt.h>
46 #include <linux/lustre_acl.h>
47 #include <linux/lustre_lite.h>
48 #include <linux/lustre_smfs.h>
49 #include "mds_internal.h"
51 struct mds_logcancel_data {
52 struct lov_mds_md *mlcd_lmm;
56 struct llog_cookie mlcd_cookies[0];
59 static void mds_cancel_cookies_cb(struct obd_device *obd,
60 __u64 transno, void *cb_data,
63 struct mds_logcancel_data *mlcd = cb_data;
64 struct lov_stripe_md *lsm = NULL;
65 struct llog_ctxt *ctxt;
68 obd_transno_commit_cb(obd, transno, error);
70 CDEBUG(D_HA, "cancelling %d cookies\n",
71 (int)(mlcd->mlcd_cookielen / sizeof(*mlcd->mlcd_cookies)));
73 rc = obd_unpackmd(obd->u.mds.mds_dt_exp, &lsm, mlcd->mlcd_lmm,
74 mlcd->mlcd_eadatalen);
76 CERROR("bad LSM cancelling %d log cookies: rc %d\n",
77 (int)(mlcd->mlcd_cookielen/sizeof(*mlcd->mlcd_cookies)),
80 ///* XXX 0 normally, SENDNOW for debug */);
81 ctxt = llog_get_context(&obd->obd_llogs,
82 mlcd->mlcd_cookies[0].lgc_subsys + 1);
83 rc = llog_cancel(ctxt, mlcd->mlcd_cookielen /
84 sizeof(*mlcd->mlcd_cookies),
85 mlcd->mlcd_cookies, OBD_LLOG_FL_SENDNOW, lsm);
87 CERROR("error cancelling %d log cookies: rc %d\n",
88 (int)(mlcd->mlcd_cookielen /
89 sizeof(*mlcd->mlcd_cookies)), rc);
90 obd_free_memmd(obd->u.mds.mds_dt_exp, &lsm);
93 OBD_FREE(mlcd, mlcd->mlcd_size);
96 /* Assumes caller has already pushed us into the kernel context. */
97 int mds_finish_transno(struct mds_obd *mds, struct inode *inode, void *handle,
98 struct ptlrpc_request *req, int rc, __u32 op_data)
100 struct mds_export_data *med = &req->rq_export->exp_mds_data;
101 struct obd_device *obd = req->rq_export->exp_obd;
102 struct mds_client_data *mcd = med->med_mcd;
103 int err, log_pri = D_HA;
108 /* if the export has already been failed, we have no last_rcvd slot */
109 if (req->rq_export->exp_failed) {
110 CERROR("committing transaction for disconnected client\n");
112 GOTO(out_commit, rc);
119 if (handle == NULL) {
120 /* if we're starting our own xaction, use our own inode */
121 inode = mds->mds_rcvd_filp->f_dentry->d_inode;
122 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
123 if (IS_ERR(handle)) {
124 CERROR("fsfilt_start: %ld\n", PTR_ERR(handle));
125 RETURN(PTR_ERR(handle));
131 transno = req->rq_reqmsg->transno;
133 LASSERTF(transno == 0, "BUG 3934, t"LPU64" rc %d\n", transno, rc);
134 } else if (transno == 0) {
135 spin_lock(&mds->mds_transno_lock);
136 transno = ++mds->mds_last_transno;
137 spin_unlock(&mds->mds_transno_lock);
139 spin_lock(&mds->mds_transno_lock);
140 if (transno > mds->mds_last_transno)
141 mds->mds_last_transno = transno;
142 spin_unlock(&mds->mds_transno_lock);
144 req->rq_repmsg->transno = req->rq_transno = transno;
145 if (req->rq_reqmsg->opc == MDS_CLOSE) {
146 mcd->mcd_last_close_transno = cpu_to_le64(transno);
147 mcd->mcd_last_close_xid = cpu_to_le64(req->rq_xid);
148 mcd->mcd_last_close_result = cpu_to_le32(rc);
149 mcd->mcd_last_close_data = cpu_to_le32(op_data);
151 mcd->mcd_last_transno = cpu_to_le64(transno);
152 mcd->mcd_last_xid = cpu_to_le64(req->rq_xid);
153 mcd->mcd_last_result = cpu_to_le32(rc);
154 mcd->mcd_last_data = cpu_to_le32(op_data);
157 fsfilt_add_journal_cb(obd, mds->mds_sb, transno, handle,
158 mds_commit_last_transno_cb, NULL);
160 err = fsfilt_write_record(obd, mds->mds_rcvd_filp, mcd,
161 sizeof(*mcd), &off, 0);
169 DEBUG_REQ(log_pri, req,
170 "wrote trans #"LPU64" client %s at idx %u: err = %d",
171 transno, mcd->mcd_uuid, med->med_idx, err);
173 err = mds_update_last_fid(obd, handle, 0);
180 err = mds_dt_write_objids(obd);
186 CDEBUG(log_pri, "wrote objids: err = %d\n", err);
190 err = fsfilt_commit(obd, mds->mds_sb, inode, handle,
191 req->rq_export->exp_sync);
193 CERROR("error committing transaction: %d\n", err);
201 /* this gives the same functionality as the code between
202 * sys_chmod and inode_setattr
203 * chown_common and inode_setattr
204 * utimes and inode_setattr
207 /* Just for the case if we have some clients that know about ATTR_RAW */
208 #define ATTR_RAW 8192
210 int mds_fix_attr(struct inode *inode, struct mds_update_record *rec)
212 time_t now = LTIME_S(CURRENT_TIME);
213 struct iattr *attr = &rec->ur_iattr;
214 unsigned int ia_valid = attr->ia_valid;
218 /* only fix up attrs if the client VFS didn't already */
220 if (!(ia_valid & ATTR_RAW))
223 if (!(ia_valid & ATTR_CTIME_SET))
224 LTIME_S(attr->ia_ctime) = now;
225 if (!(ia_valid & ATTR_ATIME_SET))
226 LTIME_S(attr->ia_atime) = now;
227 if (!(ia_valid & ATTR_MTIME_SET))
228 LTIME_S(attr->ia_mtime) = now;
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 if ((ia_valid & (ATTR_MTIME|ATTR_ATIME)) == (ATTR_MTIME|ATTR_ATIME)) {
235 if (rec->ur_fsuid != inode->i_uid &&
236 (error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
240 if (ia_valid & ATTR_SIZE) {
241 if ((error = ll_permission(inode, MAY_WRITE, NULL)) != 0)
245 if (ia_valid & ATTR_UID) {
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 if (attr->ia_uid == (uid_t) -1)
251 attr->ia_uid = inode->i_uid;
252 if (attr->ia_gid == (gid_t) -1)
253 attr->ia_gid = inode->i_gid;
254 attr->ia_mode = inode->i_mode;
256 * If the user or group of a non-directory has been
257 * changed by a non-root user, remove the setuid bit.
258 * 19981026 David C Niemi <niemi@tux.org>
260 * Changed this to apply to all users, including root,
261 * to avoid some races. This is the behavior we had in
262 * 2.0. The check for non-root was definitely wrong
263 * for 2.2 anyway, as it should have been using
264 * CAP_FSETID rather than fsuid -- 19990830 SD.
266 if ((inode->i_mode & S_ISUID) == S_ISUID &&
267 !S_ISDIR(inode->i_mode)) {
268 attr->ia_mode &= ~S_ISUID;
269 attr->ia_valid |= ATTR_MODE;
272 * Likewise, if the user or group of a non-directory
273 * has been changed by a non-root user, remove the
274 * setgid bit UNLESS there is no group execute bit
275 * (this would be a file marked for mandatory
276 * locking). 19981026 David C Niemi <niemi@tux.org>
278 * Removed the fsuid check (see the comment above) --
281 if (((inode->i_mode & (S_ISGID | S_IXGRP)) ==
282 (S_ISGID | S_IXGRP)) && !S_ISDIR(inode->i_mode)) {
283 attr->ia_mode &= ~S_ISGID;
284 attr->ia_valid |= ATTR_MODE;
286 } else if (ia_valid & ATTR_MODE) {
287 int mode = attr->ia_mode;
289 if (attr->ia_mode == (mode_t) -1)
290 attr->ia_mode = inode->i_mode;
292 (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
297 void mds_steal_ack_locks(struct ptlrpc_request *req)
299 struct obd_export *exp = req->rq_export;
300 char str[PTL_NALFMT_SIZE];
301 struct list_head *tmp;
302 struct ptlrpc_reply_state *oldrep;
303 struct ptlrpc_service *svc;
304 struct llog_create_locks *lcl;
308 /* CAVEAT EMPTOR: spinlock order */
309 spin_lock_irqsave (&exp->exp_lock, flags);
310 list_for_each (tmp, &exp->exp_outstanding_replies) {
311 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
313 if (oldrep->rs_xid != req->rq_xid)
316 if (oldrep->rs_msg->opc != req->rq_reqmsg->opc)
317 CERROR ("Resent req xid "LPX64" has mismatched opc: "
318 "new %d old %d\n", req->rq_xid,
319 req->rq_reqmsg->opc, oldrep->rs_msg->opc);
321 svc = oldrep->rs_srv_ni->sni_service;
322 spin_lock (&svc->srv_lock);
324 list_del_init (&oldrep->rs_exp_list);
326 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
327 " o%d NID %s\n", oldrep->rs_nlocks, oldrep,
328 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_msg->opc,
329 ptlrpc_peernid2str(&exp->exp_connection->c_peer, str));
331 for (i = 0; i < oldrep->rs_nlocks; i++)
332 ptlrpc_save_lock(req,
333 &oldrep->rs_locks[i],
334 oldrep->rs_modes[i]);
335 oldrep->rs_nlocks = 0;
337 lcl = oldrep->rs_llog_locks;
338 oldrep->rs_llog_locks = NULL;
340 ptlrpc_save_llog_lock(req, lcl);
342 DEBUG_REQ(D_HA, req, "stole locks for");
343 ptlrpc_schedule_difficult_reply (oldrep);
345 spin_unlock (&svc->srv_lock);
346 spin_unlock_irqrestore (&exp->exp_lock, flags);
349 spin_unlock_irqrestore (&exp->exp_lock, flags);
352 void mds_req_from_mcd(struct ptlrpc_request *req, struct mds_client_data *mcd)
354 if (req->rq_reqmsg->opc == MDS_CLOSE) {
355 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
356 mcd->mcd_last_close_transno, mcd->mcd_last_close_result);
357 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_close_transno;
358 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_close_result;
360 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
361 mcd->mcd_last_transno, mcd->mcd_last_result);
362 req->rq_repmsg->transno = req->rq_transno = mcd->mcd_last_transno;
363 req->rq_repmsg->status = req->rq_status = mcd->mcd_last_result;
366 mds_steal_ack_locks(req);
369 static void reconstruct_reint_setattr(struct mds_update_record *rec,
370 int offset, struct ptlrpc_request *req)
372 struct mds_export_data *med = &req->rq_export->exp_mds_data;
373 struct mds_body *body;
376 mds_req_from_mcd(req, med->med_mcd);
378 de = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
380 LASSERT(PTR_ERR(de) == req->rq_status);
384 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
385 mds_pack_inode2body(req2obd(req), body, de->d_inode, 1);
387 /* Don't return OST-specific attributes if we didn't just set them */
388 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
389 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
390 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
391 body->valid |= OBD_MD_FLMTIME;
392 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
393 body->valid |= OBD_MD_FLATIME;
398 static int mds_reint_remote_setfacl(struct obd_device *obd,
399 struct mds_export_data *med,
400 struct mds_update_record *rec,
401 struct ptlrpc_request *req)
403 struct rmtacl_upcall_desc desc;
406 struct mds_body *body;
408 int repsize[2] = { sizeof(*body), LUSTRE_ACL_SIZE_MAX };
411 rc = lustre_pack_reply(req, 2, repsize, NULL);
415 de = mds_id2dentry(obd, rec->ur_id1, NULL);
417 GOTO(out, rc = PTR_ERR(de));
422 /* setxattr from remote client:
424 memset(&desc, 0, sizeof(desc));
425 desc.cmd = (char *) rec->ur_ea2data;
426 desc.cmdlen = rec->ur_ea2datalen;
427 desc.res = lustre_msg_buf(req->rq_repmsg, 1, LUSTRE_ACL_SIZE_MAX);
428 desc.reslen = LUSTRE_ACL_SIZE_MAX;
430 mds_do_remote_acl_upcall(&desc);
431 if (desc.upcall_status)
432 GOTO(out_put, rc = desc.upcall_status);
435 desc.status = -desc.status;
437 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
440 /* client (lmv) will do limited checking upon replied mds_body,
441 * we pack it as normal, but "steal" field "flags" field to store
442 * the acl execution status.
444 mds_pack_inode2body(obd, body, inode, 1);
445 body->flags = desc.status;
446 mds_body_do_reverse_map(med, body);
456 /*This is a tmp fix for cmobd setattr reint*/
458 #define XATTR_LUSTRE_MDS_LOV_EA "lov"
459 #define XATTR_LUSTRE_MDS_MEA_EA "mea"
460 #define XATTR_LUSTRE_MDS_MID_EA "mid"
461 #define XATTR_LUSTRE_MDS_SID_EA "sid"
462 #define XATTR_LUSTRE_MDS_PID_EA "pid"
463 #define XATTR_LUSTRE_MDS_KEY_EA "key"
465 static int mds_get_md_type(char *name)
467 if (!strcmp(name, XATTR_LUSTRE_MDS_LOV_EA))
469 if (!strcmp(name, XATTR_LUSTRE_MDS_MEA_EA))
471 if (!strcmp(name, XATTR_LUSTRE_MDS_MID_EA))
473 if (!strcmp(name, XATTR_LUSTRE_MDS_SID_EA))
475 if (!strcmp(name, XATTR_LUSTRE_MDS_PID_EA))
477 if (!strcmp(name, XATTR_LUSTRE_MDS_KEY_EA))
483 /* In the raw-setattr case, we lock the child inode.
484 * In the write-back case or if being called from open, the client holds a lock
487 * We use the ATTR_FROM_OPEN flag to tell these cases apart. */
488 static int mds_reint_setattr(struct mds_update_record *rec, int offset,
489 struct ptlrpc_request *req, struct lustre_handle *lh)
491 struct mds_obd *mds = mds_req2mds(req);
492 struct obd_device *obd = req->rq_export->exp_obd;
493 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
494 struct mds_body *body;
495 struct dentry *de = NULL;
496 struct inode *inode = NULL;
497 struct lustre_handle lockh[2] = {{0}, {0}};
500 struct mds_logcancel_data *mlcd = NULL;
501 int rc = 0, cleanup_phase = 0, err;
502 int repsize = sizeof(*body), locked = 0;
505 LASSERT(offset == 1);
507 DEBUG_REQ(D_INODE, req, "setattr "LPU64"/%u %x",
508 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
509 rec->ur_iattr.ia_valid);
511 /* remote setfacl need special handling */
512 if ((rec->ur_iattr.ia_valid & ATTR_EA) &&
513 !strcmp(rec->ur_eadata, XATTR_NAME_LUSTRE_ACL)) {
514 return mds_reint_remote_setfacl(obd, med, rec, req);
517 rc = lustre_pack_reply(req, 1, &repsize, NULL);
521 MDS_CHECK_RESENT(req, reconstruct_reint_setattr(rec, offset, req));
522 MD_COUNTER_INCREMENT(obd, setattr);
524 if (med->med_remote) {
525 if (rec->ur_iattr.ia_valid & ATTR_GID) {
526 CWARN("Deny chgrp from remote client\n");
527 GOTO(cleanup, rc = -EPERM);
529 if (rec->ur_iattr.ia_valid & ATTR_UID) {
532 uid = mds_idmap_lookup_uid(med->med_idmap, 0,
533 rec->ur_iattr.ia_uid);
534 if (uid == MDS_IDMAP_NOTFOUND) {
535 CWARN("Deny chown to uid %u\n",
536 rec->ur_iattr.ia_uid);
537 GOTO(cleanup, rc = -EPERM);
539 rec->ur_iattr.ia_uid = uid;
543 if (rec->ur_iattr.ia_valid & ATTR_FROM_OPEN) {
544 de = mds_id2dentry(obd, rec->ur_id1, NULL);
546 GOTO(cleanup, rc = PTR_ERR(de));
548 __u64 lockpart = MDS_INODELOCK_UPDATE;
549 if (rec->ur_iattr.ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
550 lockpart |= MDS_INODELOCK_LOOKUP;
551 de = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
552 lockh, &parent_mode, NULL, 0, lockpart);
554 GOTO(cleanup, rc = PTR_ERR(de));
562 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
563 rec->ur_eadata != NULL)
566 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_SETATTR_WRITE, inode->i_sb);
568 handle = fsfilt_start(obd, inode, FSFILT_OP_SETATTR, NULL);
570 GOTO(cleanup, rc = PTR_ERR(handle));
572 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_CTIME))
573 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu\n",
574 LTIME_S(rec->ur_iattr.ia_mtime),
575 LTIME_S(rec->ur_iattr.ia_ctime));
576 rc = mds_fix_attr(inode, rec);
580 if (rec->ur_iattr.ia_valid & ATTR_ATTR_FLAG) /* ioctl */
581 rc = fsfilt_iocontrol(obd, inode, NULL, EXT3_IOC_SETFLAGS,
582 (long)&rec->ur_iattr.ia_attr_flags);
584 rc = fsfilt_setattr(obd, de, handle, &rec->ur_iattr, 0);
587 if (rec->ur_iattr.ia_valid & ATTR_EA) {
588 int flags = (int) rec->ur_iattr.ia_attr_flags;
591 if (!med->med_remote && inode->i_op &&
592 inode->i_op->setxattr)
593 rc = inode->i_op->setxattr(
598 } else if (rec->ur_iattr.ia_valid & ATTR_EA_RM) {
600 if (inode->i_op && inode->i_op->removexattr)
601 rc = inode->i_op->removexattr(de,
603 } else if (rec->ur_iattr.ia_valid & ATTR_EA_CMOBD) {
606 /*tmp fix for cmobd set md reint*/
607 LASSERT(rec->ur_eadata != NULL);
608 LASSERT(rec->ur_ea2data != NULL);
609 name = rec->ur_eadata;
610 CDEBUG(D_INFO, "set %s EA for cmobd \n", name);
611 type = mds_get_md_type(name);
613 rc = fsfilt_set_md(obd, inode, handle,
615 rec->ur_ea2datalen, type);
618 } else if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
619 !((rec->ur_iattr.ia_valid & ATTR_KEY) ||
620 (rec->ur_iattr.ia_valid & ATTR_MAC))) {
621 struct lov_stripe_md *lsm = NULL;
622 struct lov_user_md *lum = NULL;
624 if (rec->ur_eadata != NULL) {
625 rc = ll_permission(inode, MAY_WRITE, NULL);
629 lum = rec->ur_eadata;
631 /* if lmm_stripe_size is -1 delete default
633 if (S_ISDIR(inode->i_mode) &&
634 lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
635 rc = fsfilt_set_md(obd, inode, handle, NULL, 0, EA_LOV);
639 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
641 &lsm, rec->ur_eadata);
645 obd_free_memmd(mds->mds_dt_exp, &lsm);
646 rc = fsfilt_set_md(obd, inode, handle, rec->ur_eadata,
647 rec->ur_eadatalen, EA_LOV);
653 if ((rec->ur_iattr.ia_valid & ATTR_KEY) ||
654 (rec->ur_iattr.ia_valid & ATTR_MAC)) {
657 LASSERT(rec->ur_eadatalen || rec->ur_ea3datalen);
658 LASSERT(rec->ur_eadata || rec->ur_ea3data);
659 key = rec->ur_eadata ? rec->ur_eadata : rec->ur_ea3data;
660 keylen = rec->ur_eadatalen ? rec->ur_eadatalen :
662 mds_set_gskey(obd, handle, inode, key, keylen,
663 rec->ur_iattr.ia_valid);
667 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
668 mds_pack_inode2body(obd, body, inode, 1);
670 /* Don't return OST-specific attributes if we didn't just set them */
671 if (rec->ur_iattr.ia_valid & ATTR_SIZE)
672 body->valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
673 if (rec->ur_iattr.ia_valid & (ATTR_MTIME | ATTR_MTIME_SET))
674 body->valid |= OBD_MD_FLMTIME;
675 if (rec->ur_iattr.ia_valid & (ATTR_ATIME | ATTR_ATIME_SET))
676 body->valid |= OBD_MD_FLATIME;
678 mds_body_do_reverse_map(med, body);
680 /* The logcookie should be no use anymore, why nobody remove
681 * following code block?
683 LASSERT(rec->ur_cookielen == 0);
684 if (rc == 0 && rec->ur_cookielen && !IS_ERR(mds->mds_dt_obd)) {
685 OBD_ALLOC(mlcd, sizeof(*mlcd) + rec->ur_cookielen +
688 mlcd->mlcd_size = sizeof(*mlcd) + rec->ur_cookielen +
690 mlcd->mlcd_eadatalen = rec->ur_eadatalen;
691 mlcd->mlcd_cookielen = rec->ur_cookielen;
692 mlcd->mlcd_lmm = (void *)&mlcd->mlcd_cookies +
693 mlcd->mlcd_cookielen;
694 memcpy(&mlcd->mlcd_cookies, rec->ur_logcookies,
695 mlcd->mlcd_cookielen);
696 memcpy(mlcd->mlcd_lmm, rec->ur_eadata,
697 mlcd->mlcd_eadatalen);
699 CERROR("unable to allocate log cancel data\n");
705 fsfilt_add_journal_cb(req->rq_export->exp_obd, mds->mds_sb, 0,
706 handle, mds_cancel_cookies_cb, mlcd);
707 err = mds_finish_transno(mds, inode, handle, req, rc, 0);
708 switch (cleanup_phase) {
710 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)) &&
711 rec->ur_eadata != NULL)
716 if (lockh[1].cookie != 0)
717 ldlm_lock_decref(lockh + 1, parent_mode);
720 ldlm_lock_decref(lockh, LCK_PW);
722 ptlrpc_save_lock (req, lockh, LCK_PW);
737 static void reconstruct_reint_create(struct mds_update_record *rec, int offset,
738 struct ptlrpc_request *req)
740 struct mds_export_data *med = &req->rq_export->exp_mds_data;
741 struct dentry *parent, *child;
742 struct mds_body *body;
745 mds_req_from_mcd(req, med->med_mcd);
747 if (req->rq_status) {
752 parent = mds_id2dentry(req2obd(req), rec->ur_id1, NULL);
753 LASSERT(!IS_ERR(parent));
754 child = ll_lookup_one_len(rec->ur_name, parent,
755 rec->ur_namelen - 1);
756 LASSERT(!IS_ERR(child));
757 if ((child->d_flags & DCACHE_CROSS_REF)) {
758 LASSERTF(child->d_inode == NULL, "BUG 3869\n");
759 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
760 mds_pack_dentry2body(req2obd(req), body, child, 1);
761 } else if (child->d_inode == NULL) {
762 DEBUG_REQ(D_ERROR, req, "parent "DLID4" name %s mode %o",
763 OLID4(rec->ur_id1), rec->ur_name, rec->ur_mode);
764 LASSERTF(child->d_inode != NULL, "BUG 3869\n");
766 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
767 mds_pack_inode2body(req2obd(req), body, child->d_inode, 1);
774 static int mds_get_default_acl(struct inode *dir, void **pacl)
776 struct dentry de = { .d_inode = dir };
779 LASSERT(S_ISDIR(dir->i_mode));
781 if (!dir->i_op->getxattr)
784 size = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, NULL, 0);
785 if (size == 0 || size == -ENODATA || size == -EOPNOTSUPP)
790 OBD_ALLOC(*pacl, size);
794 size2 = dir->i_op->getxattr(&de, XATTR_NAME_ACL_DEFAULT, *pacl, size);
796 /* since we already locked the dir, it should not change
797 * between the 2 getxattr calls
799 CERROR("2'nd getxattr got %d, expect %d\n", size2, size);
800 OBD_FREE(*pacl, size);
807 static int mds_reint_create(struct mds_update_record *rec, int offset,
808 struct ptlrpc_request *req,
809 struct lustre_handle *lh)
811 struct dentry *dparent = NULL;
812 struct mds_obd *mds = mds_req2mds(req);
813 struct obd_device *obd = req->rq_export->exp_obd;
814 struct mds_body *body = NULL;
815 struct dentry *dchild = NULL;
816 struct inode *dir = NULL;
818 struct lustre_handle lockh[2] = {{0}, {0}};
820 int rc = 0, err, type = rec->ur_mode & S_IFMT, cleanup_phase = 0;
822 struct dentry_params dp;
823 struct mea *mea = NULL;
825 struct lustre_id sid;
829 LASSERT(offset == 1);
831 LASSERT(!strcmp(req->rq_export->exp_obd->obd_type->typ_name,
832 OBD_MDS_DEVICENAME));
834 DEBUG_REQ(D_INODE, req, "parent "LPU64"/%u name %s mode %o",
835 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
836 rec->ur_name, rec->ur_mode);
838 MDS_CHECK_RESENT(req, reconstruct_reint_create(rec, offset, req));
840 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_CREATE))
841 GOTO(cleanup, rc = -ESTALE);
843 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL, LCK_PW,
844 lockh, &parent_mode, rec->ur_name,
845 rec->ur_namelen - 1, MDS_INODELOCK_UPDATE);
846 if (IS_ERR(dparent)) {
847 rc = PTR_ERR(dparent);
848 CERROR("parent lookup error %d\n", rc);
851 cleanup_phase = 1; /* locked parent dentry */
852 dir = dparent->d_inode;
855 ldlm_lock_dump_handle(D_OTHER, lockh);
857 /* get parent id: ldlm lock on the parent protects ea */
858 rc = mds_read_inode_sid(obd, dir, &sid);
860 CERROR("can't read parent id. ino(%lu) rc(%d)\n",
865 /* try to retrieve MEA data for this dir */
866 rc = mds_md_get_attr(obd, dparent->d_inode, &mea, &mea_size);
870 if (mea != NULL && mea->mea_count) {
872 * dir is already splitted, check is requested filename should
873 * live at this MDS or at another one.
875 int i = mea_name2idx(mea, rec->ur_name, rec->ur_namelen - 1);
876 if (mea->mea_master != id_group(&mea->mea_ids[i])) {
877 CDEBUG(D_OTHER, "inapropriate MDS(%d) for %lu/%u:%s."
878 " should be %lu(%d)\n",
879 mea->mea_master, dparent->d_inode->i_ino,
880 dparent->d_inode->i_generation, rec->ur_name,
881 (unsigned long)id_group(&mea->mea_ids[i]), i);
882 GOTO(cleanup, rc = -ERESTART);
886 dchild = ll_lookup_one_len(rec->ur_name, dparent,
887 rec->ur_namelen - 1);
888 if (IS_ERR(dchild)) {
889 rc = PTR_ERR(dchild);
890 CERROR("Can't find "DLID4"/%s, error %d\n",
891 OLID4(rec->ur_id1), rec->ur_name, rc);
895 cleanup_phase = 2; /* child dentry */
897 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_CREATE_WRITE, dir->i_sb);
899 if (type == S_IFREG || type == S_IFDIR) {
900 rc = mds_try_to_split_dir(obd, dparent, &mea, 0, parent_mode);
901 CDEBUG(D_OTHER, "%s: splitted %lu/%u - %d/%d\n",
902 obd->obd_name, dparent->d_inode->i_ino,
903 dparent->d_inode->i_generation, rc, parent_mode);
905 /* dir got splitted */
906 GOTO(cleanup, rc = -ERESTART);
908 /* error happened during spitting. */
913 if (dir->i_mode & S_ISGID) {
914 if (S_ISDIR(rec->ur_mode))
915 rec->ur_mode |= S_ISGID;
919 * here inode number should be used only in the case of replaying. It is
920 * needed to check if object already created in the case of creating
923 if (id_ino(rec->ur_id2))
924 fid = id_fid(rec->ur_id2);
926 fid = mds_alloc_fid(obd);
927 dchild->d_fsdata = (void *)&dp;
928 dp.p_inum = (unsigned long)id_ino(rec->ur_id2);
931 dp.p_group = mds->mds_num;
933 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
937 handle = fsfilt_start(obd, dir, FSFILT_OP_CREATE, NULL);
939 GOTO(cleanup, rc = PTR_ERR(handle));
940 rc = ll_vfs_create(dir, dchild, rec->ur_mode, NULL);
941 if (rec->ur_eadata && rec->ur_eadatalen &&
942 (rc == 0) && (dchild->d_inode != NULL)) {
943 /*Assumption: When ur_eadata is not NULL,
944 *ur_eadata is crypto key, should fix it later, Wangdi*/
945 mds_set_gskey(obd, handle, dchild->d_inode,
946 rec->ur_eadata, rec->ur_eadatalen,
947 ATTR_MAC | ATTR_KEY);
956 * as Peter asked, mkdir() should distribute new directories
957 * over the whole cluster in order to distribute namespace
958 * processing load. first, we calculate which MDS to use to put
959 * new directory's inode in.
961 i = mds_choose_mdsnum(obd, rec->ur_name, rec->ur_namelen - 1,
962 rec->ur_flags, &req->rq_peer, dir);
963 if (i == mds->mds_num) {
964 /* inode will be created locally */
965 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
967 GOTO(cleanup, rc = PTR_ERR(handle));
969 rc = vfs_mkdir(dir, dchild, rec->ur_mode);
972 "Can't create dir \"%s\", rc = %d\n",
973 dchild->d_name.name, rc);
978 nstripes = *(u16 *)rec->ur_eadata;
980 if (rc == 0 && nstripes) {
982 * we pass LCK_EX to split routine to signal,
983 * that we have exclusive access to the
984 * directory. Simple because nobody knows it
985 * already exists -bzzz
987 rc = mds_try_to_split_dir(obd, dchild,
991 /* dir got splitted */
994 /* an error occured during
999 } else if (!DENTRY_VALID(dchild)) {
1000 /* inode will be created on another MDS */
1001 struct obdo *oa = NULL;
1005 /* first, create that inode */
1008 GOTO(cleanup, rc = -ENOMEM);
1013 if (rec->ur_eadata) {
1014 /* user asks for creating splitted dir */
1015 oa->o_easize = *((u16 *) rec->ur_eadata);
1018 obdo_from_inode(oa, dir, OBD_MD_FLATIME |
1019 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1021 /* adjust the uid/gid/mode bits */
1022 oa->o_mode = rec->ur_mode;
1023 oa->o_uid = current->fsuid;
1024 oa->o_gid = (dir->i_mode & S_ISGID) ?
1025 dir->i_gid : current->fsgid;
1026 /* transfer parent id to remote inode */
1027 memcpy(obdo_id(oa), &sid, sizeof(sid));
1028 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLUID |
1029 OBD_MD_FLGID | OBD_MD_FLIFID;
1031 CDEBUG(D_OTHER, "%s: create dir on MDS %u\n",
1034 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1036 * here inode number and generation are
1037 * important, as this is replay request and we
1038 * need them to check if such an object is
1041 CDEBUG(D_HA, "%s: replay dir creation %*s -> %u/%u\n",
1042 obd->obd_name, rec->ur_namelen - 1,
1043 rec->ur_name, (unsigned)id_ino(rec->ur_id2),
1044 (unsigned)id_gen(rec->ur_id2));
1045 oa->o_id = id_ino(rec->ur_id2);
1046 oa->o_fid = id_fid(rec->ur_id2);
1047 oa->o_generation = id_gen(rec->ur_id2);
1048 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1049 LASSERT(oa->o_fid != 0);
1052 /* obtain default ACL */
1053 acl_size = mds_get_default_acl(dir, &acl);
1056 GOTO(cleanup, rc = -ENOMEM);
1060 * before obd_create() is called, o_fid is not known if
1061 * this is not recovery of cause.
1063 rc = obd_create(mds->mds_md_exp, oa, acl, acl_size,
1067 OBD_FREE(acl, acl_size);
1070 CERROR("can't create remote inode: %d\n", rc);
1071 DEBUG_REQ(D_ERROR, req, "parent "LPU64"/%u name %s mode %o",
1072 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
1073 rec->ur_name, rec->ur_mode);
1078 LASSERT(oa->o_fid != 0);
1080 /* now, add new dir entry for it */
1081 handle = fsfilt_start(obd, dir, FSFILT_OP_MKDIR, NULL);
1082 if (IS_ERR(handle)) {
1084 GOTO(cleanup, rc = PTR_ERR(handle));
1087 /* creating local dentry for remote inode. */
1088 rc = fsfilt_add_dir_entry(obd, dparent, rec->ur_name,
1089 rec->ur_namelen - 1, oa->o_id,
1090 oa->o_generation, i, oa->o_fid);
1093 CERROR("Can't create local entry %*s for "
1094 "remote inode.\n", rec->ur_namelen - 1,
1100 body->valid |= OBD_MD_FLID | OBD_MD_MDS | OBD_MD_FID;
1102 obdo2id(&body->id1, oa);
1105 /* requested name exists in the directory */
1112 handle = fsfilt_start(obd, dir, FSFILT_OP_SYMLINK, NULL);
1114 GOTO(cleanup, rc = PTR_ERR(handle));
1115 if (rec->ur_tgt == NULL) /* no target supplied */
1116 rc = -EINVAL; /* -EPROTO? */
1118 rc = ll_vfs_symlink(dir, dchild, rec->ur_tgt, S_IALLUGO);
1126 int rdev = rec->ur_rdev;
1127 handle = fsfilt_start(obd, dir, FSFILT_OP_MKNOD, NULL);
1129 GOTO(cleanup, (handle = NULL, rc = PTR_ERR(handle)));
1130 rc = vfs_mknod(dir, dchild, rec->ur_mode, rdev);
1135 CERROR("bad file type %o creating %s\n", type, rec->ur_name);
1136 dchild->d_fsdata = NULL;
1137 GOTO(cleanup, rc = -EINVAL);
1140 /* In case we stored the desired inum in here, we want to clean up. */
1141 if (dchild->d_fsdata == (void *)(unsigned long)id_ino(rec->ur_id2))
1142 dchild->d_fsdata = NULL;
1145 CDEBUG(D_INODE, "error during create: %d\n", rc);
1147 } else if (dchild->d_inode) {
1148 struct mds_export_data *med = &req->rq_export->u.eu_mds_data;
1150 struct inode *inode = dchild->d_inode;
1153 iattr.ia_uid = rec->ur_fsuid;
1154 LTIME_S(iattr.ia_atime) = rec->ur_time;
1155 LTIME_S(iattr.ia_ctime) = rec->ur_time;
1156 LTIME_S(iattr.ia_mtime) = rec->ur_time;
1158 if (dir->i_mode & S_ISGID)
1159 iattr.ia_gid = dir->i_gid;
1161 iattr.ia_gid = rec->ur_fsgid;
1163 iattr.ia_valid = ATTR_UID | ATTR_GID | ATTR_ATIME |
1164 ATTR_MTIME | ATTR_CTIME;
1166 if (id_ino(rec->ur_id2)) {
1167 LASSERT(id_ino(rec->ur_id2) == inode->i_ino);
1168 inode->i_generation = id_gen(rec->ur_id2);
1169 /* dirtied and committed by the upcoming setattr. */
1170 CDEBUG(D_INODE, "recreated ino %lu with gen %u\n",
1171 inode->i_ino, inode->i_generation);
1173 mds_inode2id(obd, &body->id1, dchild->d_inode, fid);
1174 mds_update_inode_ids(obd, inode, handle, &body->id1, &sid);
1176 rc = fsfilt_setattr(obd, dchild, handle, &iattr, 0);
1178 CERROR("error on child setattr: rc = %d\n", rc);
1180 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
1181 rc = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
1183 CERROR("error on parent setattr: rc = %d\n", rc);
1185 MD_COUNTER_INCREMENT(obd, create);
1187 /* take care of default stripe inheritance */
1188 if (type == S_IFDIR) {
1189 struct lov_mds_md lmm;
1190 int lmm_size = sizeof(lmm);
1192 rc = mds_get_md(obd, dir, &lmm, &lmm_size, 1, 0);
1194 down(&inode->i_sem);
1195 rc = fsfilt_set_md(obd, inode, handle,
1196 &lmm, lmm_size, EA_LOV);
1200 CERROR("error on copy stripe info: rc = %d\n",
1206 mds_pack_inode2body(obd, body, inode, 1);
1207 mds_body_do_reverse_map(med, body);
1212 err = mds_finish_transno(mds, dir, handle, req, rc, 0);
1214 if (rc && created) {
1215 /* Destroy the file we just created. This should not need extra
1216 * journal credits, as we have already modified all of the
1217 * blocks needed in order to create the file in the first
1221 err = vfs_rmdir(dir, dchild);
1223 CERROR("rmdir in error path: %d\n", err);
1226 err = vfs_unlink(dir, dchild);
1228 CERROR("unlink in error path: %d\n", err);
1231 } else if (created) {
1232 /* The inode we were allocated may have just been freed
1233 * by an unlink operation. We take this lock to
1234 * synchronize against the matching reply-ack-lock taken
1235 * in unlink, to avoid replay problems if this reply
1236 * makes it out to the client but the unlink's does not.
1237 * See bug 2029 for more detail.*/
1238 mds_lock_new_child(obd, dchild->d_inode, NULL);
1242 switch (cleanup_phase) {
1243 case 2: /* child dentry */
1245 case 1: /* locked parent dentry */
1247 if (lockh[1].cookie != 0)
1248 ldlm_lock_decref(lockh + 1, parent_mode);
1251 ldlm_lock_decref(lockh, LCK_PW);
1253 ptlrpc_save_lock(req, lockh, LCK_PW);
1259 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
1263 OBD_FREE(mea, mea_size);
1264 req->rq_status = rc;
1269 res_gt(struct ldlm_res_id *res1, struct ldlm_res_id *res2,
1270 ldlm_policy_data_t *p1, ldlm_policy_data_t *p2)
1274 for (i = 0; i < RES_NAME_SIZE; i++) {
1276 * this is needed to make zeroed res_id entries to be put at the
1277 * end of list in *ordered_locks() .
1279 if (res1->name[i] == 0 && res2->name[i] != 0)
1281 if (res2->name[i] == 0 && res1->name[i] != 0)
1283 if (res1->name[i] > res2->name[i])
1285 if (res1->name[i] < res2->name[i])
1292 if (memcmp(p1, p2, sizeof(*p1)) < 0)
1298 /* This function doesn't use ldlm_match_or_enqueue because we're always called
1299 * with EX or PW locks, and the MDS is no longer allowed to match write locks,
1300 * because they take the place of local semaphores.
1302 * One or two locks are taken in numerical order. A res_id->name[0] of 0 means
1303 * no lock is taken for that res_id. Must be at least one non-zero res_id. */
1304 int enqueue_ordered_locks(struct obd_device *obd, struct ldlm_res_id *p1_res_id,
1305 struct lustre_handle *p1_lockh, int p1_lock_mode,
1306 ldlm_policy_data_t *p1_policy,
1307 struct ldlm_res_id *p2_res_id,
1308 struct lustre_handle *p2_lockh, int p2_lock_mode,
1309 ldlm_policy_data_t *p2_policy)
1311 int lock_modes[2] = { p1_lock_mode, p2_lock_mode };
1312 struct ldlm_res_id *res_id[2] = { p1_res_id, p2_res_id };
1313 struct lustre_handle *handles[2] = { p1_lockh, p2_lockh };
1314 ldlm_policy_data_t *policies[2] = { p1_policy, p2_policy };
1318 LASSERT(p1_res_id != NULL && p2_res_id != NULL);
1320 CDEBUG(D_INFO, "locks before: "LPU64"/"LPU64"\n",
1321 res_id[0]->name[0], res_id[1]->name[0]);
1323 if (res_gt(p1_res_id, p2_res_id, p1_policy, p2_policy)) {
1324 handles[1] = p1_lockh;
1325 handles[0] = p2_lockh;
1326 res_id[1] = p1_res_id;
1327 res_id[0] = p2_res_id;
1328 lock_modes[1] = p1_lock_mode;
1329 lock_modes[0] = p2_lock_mode;
1330 policies[1] = p1_policy;
1331 policies[0] = p2_policy;
1334 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"\n",
1335 res_id[0]->name[0], res_id[1]->name[0]);
1337 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1338 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace, *res_id[0],
1339 LDLM_IBITS, policies[0], lock_modes[0], &flags,
1340 mds_blocking_ast, ldlm_completion_ast, NULL, NULL,
1341 NULL, 0, NULL, handles[0]);
1344 ldlm_lock_dump_handle(D_OTHER, handles[0]);
1346 if (!memcmp(res_id[0], res_id[1], sizeof(*res_id[0])) &&
1347 (policies[0]->l_inodebits.bits & policies[1]->l_inodebits.bits)) {
1348 memcpy(handles[1], handles[0], sizeof(*(handles[1])));
1349 ldlm_lock_addref(handles[1], lock_modes[1]);
1350 } else if (res_id[1]->name[0] != 0) {
1351 flags = LDLM_FL_LOCAL_ONLY | LDLM_FL_ATOMIC_CB;
1352 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1353 *res_id[1], LDLM_IBITS, policies[1],
1354 lock_modes[1], &flags, mds_blocking_ast,
1355 ldlm_completion_ast, NULL, NULL, NULL, 0,
1357 if (rc != ELDLM_OK) {
1358 ldlm_lock_decref(handles[0], lock_modes[0]);
1361 ldlm_lock_dump_handle(D_OTHER, handles[1]);
1367 int enqueue_4ordered_locks(struct obd_device *obd,struct ldlm_res_id *p1_res_id,
1368 struct lustre_handle *p1_lockh, int p1_lock_mode,
1369 ldlm_policy_data_t *p1_policy,
1370 struct ldlm_res_id *p2_res_id,
1371 struct lustre_handle *p2_lockh, int p2_lock_mode,
1372 ldlm_policy_data_t *p2_policy,
1373 struct ldlm_res_id *c1_res_id,
1374 struct lustre_handle *c1_lockh, int c1_lock_mode,
1375 ldlm_policy_data_t *c1_policy,
1376 struct ldlm_res_id *c2_res_id,
1377 struct lustre_handle *c2_lockh, int c2_lock_mode,
1378 ldlm_policy_data_t *c2_policy)
1380 struct ldlm_res_id *res_id[5] = { p1_res_id, p2_res_id,
1381 c1_res_id, c2_res_id };
1382 struct lustre_handle *dlm_handles[5] = { p1_lockh, p2_lockh,
1383 c1_lockh, c2_lockh };
1384 int lock_modes[5] = { p1_lock_mode, p2_lock_mode,
1385 c1_lock_mode, c2_lock_mode };
1386 ldlm_policy_data_t *policies[5] = { p1_policy, p2_policy,
1387 c1_policy, c2_policy};
1388 int rc, i, j, sorted, flags;
1391 CDEBUG(D_DLMTRACE, "locks before: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1392 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1393 res_id[3]->name[0]);
1396 * simple insertion sort - we have at most 4 elements. Note, that zeroed
1397 * res_id should be at the end of list after sorting is finished.
1399 for (i = 1; i < 4; i++) {
1401 dlm_handles[4] = dlm_handles[i];
1402 res_id[4] = res_id[i];
1403 lock_modes[4] = lock_modes[i];
1404 policies[4] = policies[i];
1408 if (res_gt(res_id[j], res_id[4], policies[j],
1410 dlm_handles[j + 1] = dlm_handles[j];
1411 res_id[j + 1] = res_id[j];
1412 lock_modes[j + 1] = lock_modes[j];
1413 policies[j + 1] = policies[j];
1418 } while (j >= 0 && !sorted);
1420 dlm_handles[j + 1] = dlm_handles[4];
1421 res_id[j + 1] = res_id[4];
1422 lock_modes[j + 1] = lock_modes[4];
1423 policies[j + 1] = policies[4];
1426 CDEBUG(D_DLMTRACE, "lock order: "LPU64"/"LPU64"/"LPU64"/"LPU64"\n",
1427 res_id[0]->name[0], res_id[1]->name[0], res_id[2]->name[0],
1428 res_id[3]->name[0]);
1430 /* XXX we could send ASTs on all these locks first before blocking? */
1431 for (i = 0; i < 4; i++) {
1435 * nevertheless zeroed res_ids should be at the end of list, and
1436 * could use break here, I think, that it is more correctly for
1437 * clear understanding of code to have continue here, as it
1438 * clearly means, that zeroed res_id should be skipped and does
1439 * not mean, that if we meet zeroed res_id we should stop
1442 if (res_id[i]->name[0] == 0)
1446 !memcmp(res_id[i], res_id[i-1], sizeof(*res_id[i])) &&
1447 (policies[i]->l_inodebits.bits &
1448 policies[i-1]->l_inodebits.bits) ) {
1449 memcpy(dlm_handles[i], dlm_handles[i-1],
1450 sizeof(*(dlm_handles[i])));
1451 ldlm_lock_addref(dlm_handles[i], lock_modes[i]);
1453 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1454 *res_id[i], LDLM_IBITS,
1456 lock_modes[i], &flags,
1458 ldlm_completion_ast, NULL, NULL,
1459 NULL, 0, NULL, dlm_handles[i]);
1461 GOTO(out_err, rc = -EIO);
1462 ldlm_lock_dump_handle(D_OTHER, dlm_handles[i]);
1469 ldlm_lock_decref(dlm_handles[i], lock_modes[i]);
1474 /* In the unlikely case that the child changed while we were waiting
1475 * on the lock, we need to drop the lock on the old child and either:
1476 * - if the child has a lower resource name, then we have to also
1477 * drop the parent lock and regain the locks in the right order
1478 * - in the rename case, if the child has a lower resource name than one of
1479 * the other parent/child resources (maxres) we also need to reget the locks
1480 * - if the child has a higher resource name (this is the common case)
1481 * we can just get the lock on the new child (still in lock order)
1483 * Returns 0 if the child did not change or if it changed but could be locked.
1484 * Returns 1 if the child changed and we need to re-lock (no locks held).
1485 * Returns -ve error with a valid dchild (no locks held). */
1486 static int mds_verify_child(struct obd_device *obd,
1487 struct ldlm_res_id *parent_res_id,
1488 struct lustre_handle *parent_lockh,
1489 struct dentry *dparent, int parent_mode,
1490 struct ldlm_res_id *child_res_id,
1491 struct lustre_handle *child_lockh,
1492 struct dentry **dchildp, int child_mode,
1493 ldlm_policy_data_t *child_policy,
1494 const char *name, int namelen,
1495 struct ldlm_res_id *maxres,
1496 unsigned long child_ino, __u32 child_gen)
1498 struct lustre_id sid;
1499 struct dentry *vchild, *dchild = *dchildp;
1500 int rc = 0, cleanup_phase = 2; /* parent, child locks */
1503 vchild = ll_lookup_one_len(name, dparent, namelen - 1);
1505 GOTO(cleanup, rc = PTR_ERR(vchild));
1507 if ((vchild->d_flags & DCACHE_CROSS_REF)) {
1508 if (child_gen == vchild->d_generation &&
1509 child_ino == vchild->d_inum) {
1518 if (likely((vchild->d_inode == NULL && child_res_id->name[0] == 0) ||
1519 (vchild->d_inode != NULL &&
1520 child_gen == vchild->d_inode->i_generation &&
1521 child_ino == vchild->d_inode->i_ino))) {
1529 CDEBUG(D_DLMTRACE, "child inode changed: %p != %p (%lu != "LPU64")\n",
1530 vchild->d_inode, dchild ? dchild->d_inode : 0,
1531 vchild->d_inode ? vchild->d_inode->i_ino : 0,
1532 child_res_id->name[0]);
1534 if (child_res_id->name[0] != 0)
1535 ldlm_lock_decref(child_lockh, child_mode);
1539 cleanup_phase = 1; /* parent lock only */
1540 *dchildp = dchild = vchild;
1542 if (dchild->d_inode || (dchild->d_flags & DCACHE_CROSS_REF)) {
1543 int flags = LDLM_FL_ATOMIC_CB;
1545 if (dchild->d_inode) {
1546 down(&dchild->d_inode->i_sem);
1547 rc = mds_read_inode_sid(obd, dchild->d_inode, &sid);
1548 up(&dchild->d_inode->i_sem);
1550 CERROR("Can't read inode self id, inode %lu,"
1551 " rc %d\n", dchild->d_inode->i_ino, rc);
1554 child_res_id->name[0] = id_fid(&sid);
1555 child_res_id->name[1] = id_group(&sid);
1557 child_res_id->name[0] = dchild->d_fid;
1558 child_res_id->name[1] = dchild->d_mdsnum;
1561 if (res_gt(parent_res_id, child_res_id, NULL, NULL) ||
1562 res_gt(maxres, child_res_id, NULL, NULL)) {
1563 CDEBUG(D_DLMTRACE, "relock "LPU64"<("LPU64"|"LPU64")\n",
1564 child_res_id->name[0], parent_res_id->name[0],
1566 GOTO(cleanup, rc = 1);
1569 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1570 *child_res_id, LDLM_IBITS, child_policy,
1571 child_mode, &flags, mds_blocking_ast,
1572 ldlm_completion_ast, NULL, NULL, NULL, 0,
1575 GOTO(cleanup, rc = -EIO);
1578 memset(child_res_id, 0, sizeof(*child_res_id));
1584 switch(cleanup_phase) {
1586 if (child_res_id->name[0] != 0)
1587 ldlm_lock_decref(child_lockh, child_mode);
1589 ldlm_lock_decref(parent_lockh, parent_mode);
1595 int mds_get_parent_child_locked(struct obd_device *obd, struct mds_obd *mds,
1596 struct lustre_id *id,
1597 struct lustre_handle *parent_lockh,
1598 struct dentry **dparentp, int parent_mode,
1599 __u64 parent_lockpart, int *update_mode,
1600 char *name, int namelen,
1601 struct lustre_handle *child_lockh,
1602 struct dentry **dchildp, int child_mode,
1603 __u64 child_lockpart)
1605 ldlm_policy_data_t parent_policy = {.l_inodebits = { parent_lockpart }};
1606 ldlm_policy_data_t child_policy = {.l_inodebits = { child_lockpart }};
1607 struct ldlm_res_id parent_res_id = { .name = {0} };
1608 struct ldlm_res_id child_res_id = { .name = {0} };
1609 unsigned long child_ino = 0; __u32 child_gen = 0;
1610 int rc = 0, cleanup_phase = 0;
1611 struct lustre_id sid;
1612 struct inode *inode;
1615 /* Step 1: Lookup parent */
1616 *dparentp = mds_id2dentry(obd, id, NULL);
1617 if (IS_ERR(*dparentp)) {
1618 rc = PTR_ERR(*dparentp);
1623 CDEBUG(D_INODE, "parent ino %lu, name %s\n",
1624 (*dparentp)->d_inode->i_ino, name);
1626 parent_res_id.name[0] = id_fid(id);
1627 parent_res_id.name[1] = id_group(id);
1630 parent_lockh[1].cookie = 0;
1631 if (name && IS_PDIROPS((*dparentp)->d_inode)) {
1632 struct ldlm_res_id res_id = { .name = {0} };
1633 ldlm_policy_data_t policy;
1634 int flags = LDLM_FL_ATOMIC_CB;
1636 *update_mode = mds_lock_mode_for_dir(obd, *dparentp, parent_mode);
1638 res_id.name[0] = id_fid(id);
1639 res_id.name[1] = id_group(id);
1640 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
1642 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
1643 res_id, LDLM_IBITS, &policy,
1644 *update_mode, &flags,
1646 ldlm_completion_ast,
1647 NULL, NULL, NULL, 0, NULL,
1653 parent_res_id.name[2] = full_name_hash((unsigned char *)name,
1656 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
1657 (unsigned long)id_fid(id), (unsigned long)id_group(id),
1658 parent_res_id.name[2]);
1662 cleanup_phase = 1; /* parent dentry */
1664 /* Step 2: Lookup child (without DLM lock, to get resource name) */
1665 *dchildp = ll_lookup_one_len(name, *dparentp, namelen - 1);
1666 if (IS_ERR(*dchildp)) {
1667 rc = PTR_ERR(*dchildp);
1668 CDEBUG(D_INODE, "child lookup error %d\n", rc);
1672 if ((*dchildp)->d_flags & DCACHE_CROSS_REF) {
1674 * inode lives on another MDS: return * fid/mdsnum and LOOKUP
1675 * lock. Drop possible UPDATE lock!
1677 child_policy.l_inodebits.bits &= ~MDS_INODELOCK_UPDATE;
1678 child_policy.l_inodebits.bits |= MDS_INODELOCK_LOOKUP;
1680 child_res_id.name[0] = (*dchildp)->d_fid;
1681 child_res_id.name[1] = (*dchildp)->d_mdsnum;
1682 child_ino = (*dchildp)->d_inum;
1683 child_gen = (*dchildp)->d_generation;
1687 inode = (*dchildp)->d_inode;
1689 inode = igrab(inode);
1693 down(&inode->i_sem);
1694 rc = mds_read_inode_sid(obd, inode, &sid);
1697 CERROR("Can't read inode self id, inode %lu, "
1698 "rc %d\n", inode->i_ino, rc);
1703 child_ino = inode->i_ino;
1704 child_gen = inode->i_generation;
1705 child_res_id.name[0] = id_fid(&sid);
1706 child_res_id.name[1] = id_group(&sid);
1710 cleanup_phase = 2; /* child dentry */
1712 /* Step 3: Lock parent and child in resource order. If child doesn't
1713 * exist, we still have to lock the parent and re-lookup. */
1714 rc = enqueue_ordered_locks(obd, &parent_res_id, parent_lockh, parent_mode,
1715 &parent_policy, &child_res_id, child_lockh,
1716 child_mode, &child_policy);
1720 if ((*dchildp)->d_inode || ((*dchildp)->d_flags & DCACHE_CROSS_REF))
1721 cleanup_phase = 4; /* child lock */
1723 cleanup_phase = 3; /* parent lock */
1725 /* Step 4: Re-lookup child to verify it hasn't changed since locking */
1726 rc = mds_verify_child(obd, &parent_res_id, parent_lockh, *dparentp,
1727 parent_mode, &child_res_id, child_lockh,
1728 dchildp, child_mode, &child_policy,
1729 name, namelen, &parent_res_id, child_ino,
1741 switch (cleanup_phase) {
1743 ldlm_lock_decref(child_lockh, child_mode);
1745 ldlm_lock_decref(parent_lockh, parent_mode);
1750 if (parent_lockh[1].cookie)
1751 ldlm_lock_decref(parent_lockh + 1, *update_mode);
1759 void mds_reconstruct_generic(struct ptlrpc_request *req)
1761 struct mds_export_data *med = &req->rq_export->exp_mds_data;
1762 mds_req_from_mcd(req, med->med_mcd);
1765 /* If we are unlinking an open file/dir (i.e. creating an orphan) then we
1766 * instead link the inode into the PENDING directory until it is finally
1767 * released. We can't simply call mds_reint_rename() or some part thereof,
1768 * because we don't have the inode to check for link count/open status until
1769 * after it is locked.
1771 * For lock ordering, caller must get child->i_sem first, then pending->i_sem
1772 * before starting journal transaction.
1774 * returns 1 on success
1775 * returns 0 if we lost a race and didn't make a new link
1776 * returns negative on error
1778 static int mds_orphan_add_link(struct mds_update_record *rec,
1779 struct obd_device *obd, struct dentry *dentry)
1781 struct mds_obd *mds = &obd->u.mds;
1782 struct inode *pending_dir = mds->mds_pending_dir->d_inode;
1783 struct inode *inode = dentry->d_inode;
1784 struct dentry *pending_child;
1785 char idname[LL_ID_NAMELEN];
1786 int idlen = 0, rc, mode;
1789 LASSERT(inode != NULL);
1790 LASSERT(!mds_inode_is_orphan(inode));
1791 #ifndef HAVE_I_ALLOC_SEM
1792 LASSERT(down_trylock(&inode->i_sem) != 0);
1794 LASSERT(down_trylock(&pending_dir->i_sem) != 0);
1796 idlen = ll_id2str(idname, inode->i_ino, inode->i_generation);
1798 CDEBUG(D_INODE, "pending destroy of %dx open %d linked %s %s = %s\n",
1799 mds_orphan_open_count(inode), inode->i_nlink,
1800 S_ISDIR(inode->i_mode) ? "dir" :
1801 S_ISREG(inode->i_mode) ? "file" : "other",
1802 rec->ur_name, idname);
1804 if (mds_orphan_open_count(inode) == 0 || inode->i_nlink != 0)
1807 pending_child = lookup_one_len(idname, mds->mds_pending_dir, idlen);
1808 if (IS_ERR(pending_child))
1809 RETURN(PTR_ERR(pending_child));
1811 if (pending_child->d_inode != NULL) {
1812 CERROR("re-destroying orphan file %s?\n", rec->ur_name);
1813 LASSERT(pending_child->d_inode == inode);
1814 GOTO(out_dput, rc = 0);
1818 * link() is semanticaly-wrong for S_IFDIR, so we set S_IFREG for
1819 * linking and return real mode back then -bzzz
1821 mode = inode->i_mode;
1822 inode->i_mode = S_IFREG;
1823 rc = vfs_link(dentry, pending_dir, pending_child);
1825 CERROR("error linking orphan %s to PENDING: rc = %d\n",
1828 mds_inode_set_orphan(inode);
1830 /* return mode and correct i_nlink if inode is directory */
1831 inode->i_mode = mode;
1832 LASSERTF(inode->i_nlink == 1, "%s nlink == %d\n",
1833 S_ISDIR(mode) ? "dir" : S_ISREG(mode) ? "file" : "other",
1835 if (S_ISDIR(mode)) {
1837 i_nlink_inc(pending_dir);
1838 mark_inode_dirty(inode);
1839 mark_inode_dirty(pending_dir);
1842 GOTO(out_dput, rc = 1);
1844 l_dput(pending_child);
1848 int mds_create_local_dentry(struct mds_update_record *rec,
1849 struct obd_device *obd)
1851 struct mds_obd *mds = &obd->u.mds;
1852 struct inode *id_dir = mds->mds_id_dir->d_inode;
1853 int idlen = 0, rc, cleanup_phase = 0;
1854 struct dentry *new_child = NULL;
1855 char *idname = rec->ur_name;
1856 struct dentry *child = NULL;
1857 struct lustre_handle lockh[2] = {{0}, {0}};
1858 struct lustre_id sid;
1862 down(&id_dir->i_sem);
1863 idlen = ll_id2str(idname, id_ino(rec->ur_id1),
1864 id_gen(rec->ur_id1));
1866 CDEBUG(D_OTHER, "look for local dentry '%s' for "DLID4"\n",
1867 idname, OLID4(rec->ur_id1));
1869 new_child = ll_lookup_one_len(idname, mds->mds_id_dir,
1872 if (IS_ERR(new_child)) {
1873 CERROR("can't lookup %s: %d\n", idname,
1874 (int) PTR_ERR(new_child));
1875 GOTO(cleanup, rc = PTR_ERR(new_child));
1879 down(&id_dir->i_sem);
1880 rc = mds_read_inode_sid(obd, id_dir, &sid);
1883 CERROR("Can't read inode self id, inode %lu, "
1884 "rc %d\n", id_dir->i_ino, rc);
1888 if (new_child->d_inode != NULL) {
1889 /* nice. we've already have local dentry! */
1890 CDEBUG(D_OTHER, "found dentry in FIDS/: %u/%u\n",
1891 (unsigned)new_child->d_inode->i_ino,
1892 (unsigned)new_child->d_inode->i_generation);
1894 id_ino(rec->ur_id1) = id_dir->i_ino;
1895 id_gen(rec->ur_id1) = id_dir->i_generation;
1896 rec->ur_namelen = idlen + 1;
1898 id_fid(rec->ur_id1) = id_fid(&sid);
1899 id_group(rec->ur_id1) = id_group(&sid);
1901 GOTO(cleanup, rc = 0);
1904 /* new, local dentry will be added soon. we need no aliases here */
1907 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
1908 child = mds_id2dentry(obd, rec->ur_id1, NULL);
1910 child = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
1911 LCK_EX, lockh, NULL, NULL, 0,
1912 MDS_INODELOCK_UPDATE);
1915 if (IS_ERR(child)) {
1916 rc = PTR_ERR(child);
1917 if (rc != -ENOENT || !(rec->ur_mode & MDS_MODE_REPLAY))
1918 CERROR("can't get victim: %d\n", rc);
1923 handle = fsfilt_start(obd, id_dir, FSFILT_OP_LINK, NULL);
1925 GOTO(cleanup, rc = PTR_ERR(handle));
1927 rc = fsfilt_add_dir_entry(obd, mds->mds_id_dir, idname,
1928 idlen, id_ino(rec->ur_id1),
1929 id_gen(rec->ur_id1), mds->mds_num,
1930 id_fid(rec->ur_id1));
1932 CERROR("error linking orphan %lu/%lu to FIDS: rc = %d\n",
1933 (unsigned long)child->d_inode->i_ino,
1934 (unsigned long)child->d_inode->i_generation, rc);
1936 if (S_ISDIR(child->d_inode->i_mode)) {
1937 i_nlink_inc(id_dir);
1938 mark_inode_dirty(id_dir);
1940 mark_inode_dirty(child->d_inode);
1942 fsfilt_commit(obd, mds->mds_sb, id_dir, handle, 0);
1944 id_ino(rec->ur_id1) = id_dir->i_ino;
1945 id_gen(rec->ur_id1) = id_dir->i_generation;
1946 rec->ur_namelen = idlen + 1;
1948 id_fid(rec->ur_id1) = id_fid(&sid);
1949 id_group(rec->ur_id1) = id_group(&sid);
1953 switch(cleanup_phase) {
1955 if (!(rec->ur_mode & MDS_MODE_DONT_LOCK))
1956 ldlm_lock_decref(lockh, LCK_EX);
1966 static int mds_copy_unlink_reply(struct ptlrpc_request *master,
1967 struct ptlrpc_request *slave)
1969 void *cookie, *cookie2;
1970 struct mds_body *body2;
1971 struct mds_body *body;
1975 body = lustre_msg_buf(slave->rq_repmsg, 0, sizeof(*body));
1976 LASSERT(body != NULL);
1978 body2 = lustre_msg_buf(master->rq_repmsg, 0, sizeof (*body));
1979 LASSERT(body2 != NULL);
1981 if (!(body->valid & (OBD_MD_FLID | OBD_MD_FLGENER)))
1984 memcpy(body2, body, sizeof(*body));
1985 body2->valid &= ~OBD_MD_FLCOOKIE;
1987 if (!(body->valid & OBD_MD_FLEASIZE) &&
1988 !(body->valid & OBD_MD_FLDIREA))
1991 if (body->eadatasize == 0) {
1992 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
1996 LASSERT(master->rq_repmsg->buflens[1] >= body->eadatasize);
1998 ea = lustre_msg_buf(slave->rq_repmsg, 1, body->eadatasize);
1999 LASSERT(ea != NULL);
2001 ea2 = lustre_msg_buf(master->rq_repmsg, 1, body->eadatasize);
2002 LASSERT(ea2 != NULL);
2004 memcpy(ea2, ea, body->eadatasize);
2006 if (body->valid & OBD_MD_FLCOOKIE) {
2007 LASSERT(master->rq_repmsg->buflens[2] >=
2008 slave->rq_repmsg->buflens[2]);
2009 cookie = lustre_msg_buf(slave->rq_repmsg, 2,
2010 slave->rq_repmsg->buflens[2]);
2011 LASSERT(cookie != NULL);
2013 cookie2 = lustre_msg_buf(master->rq_repmsg, 2,
2014 master->rq_repmsg->buflens[2]);
2015 LASSERT(cookie2 != NULL);
2016 memcpy(cookie2, cookie, slave->rq_repmsg->buflens[2]);
2017 body2->valid |= OBD_MD_FLCOOKIE;
2022 static int mds_reint_unlink_remote(struct mds_update_record *rec,
2023 int offset, struct ptlrpc_request *req,
2024 struct lustre_handle *parent_lockh,
2025 int update_mode, struct dentry *dparent,
2026 struct lustre_handle *child_lockh,
2027 struct dentry *dchild)
2029 struct obd_device *obd = req->rq_export->exp_obd;
2030 struct mds_obd *mds = mds_req2mds(req);
2031 struct ptlrpc_request *request = NULL;
2032 int rc = 0, cleanup_phase = 0;
2033 struct mdc_op_data *op_data;
2037 LASSERT(offset == 1 || offset == 3);
2039 /* time to drop i_nlink on remote MDS */
2040 OBD_ALLOC(op_data, sizeof(*op_data));
2041 if (op_data == NULL)
2044 memset(op_data, 0, sizeof(*op_data));
2045 mds_pack_dentry2id(obd, &op_data->id1, dchild, 1);
2046 op_data->create_mode = rec->ur_mode;
2048 DEBUG_REQ(D_INODE, req, "unlink %*s (remote inode "DLID4")",
2049 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2051 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2052 DEBUG_REQ(D_HA, req, "unlink %*s (remote inode "DLID4")",
2053 rec->ur_namelen - 1, rec->ur_name, OLID4(&op_data->id1));
2056 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2057 op_data->create_mode |= MDS_MODE_REPLAY;
2059 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2060 OBD_FREE(op_data, sizeof(*op_data));
2065 mds_copy_unlink_reply(req, request);
2066 ptlrpc_req_finished(request);
2070 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2073 GOTO(cleanup, rc = PTR_ERR(handle));
2074 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, dchild);
2075 rc = mds_finish_transno(mds, dparent->d_inode, handle, req,
2080 req->rq_status = rc;
2083 if (parent_lockh[1].cookie != 0)
2084 ldlm_lock_decref(parent_lockh + 1, update_mode);
2086 ldlm_lock_decref(child_lockh, LCK_EX);
2088 ldlm_lock_decref(parent_lockh, LCK_PW);
2090 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2097 static int mds_reint_unlink(struct mds_update_record *rec, int offset,
2098 struct ptlrpc_request *req,
2099 struct lustre_handle *lh)
2101 struct dentry *dparent = NULL, *dchild;
2102 struct mds_obd *mds = mds_req2mds(req);
2103 struct obd_device *obd = req->rq_export->exp_obd;
2104 struct mds_body *body = NULL;
2105 struct inode *child_inode = NULL;
2106 struct lustre_handle parent_lockh[2] = {{0}, {0}};
2107 struct lustre_handle child_lockh = {0};
2108 struct lustre_handle child_reuse_lockh = {0};
2109 struct lustre_handle *slave_lockh = NULL;
2110 char idname[LL_ID_NAMELEN];
2111 struct llog_create_locks *lcl = NULL;
2112 void *handle = NULL;
2113 int rc = 0, cleanup_phase = 0;
2114 int unlink_by_id = 0;
2118 LASSERT(offset == 1 || offset == 3);
2120 DEBUG_REQ(D_INODE, req, "parent ino "LPU64"/%u, child %s",
2121 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2124 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2126 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
2127 DEBUG_REQ(D_HA, req, "unlink replay");
2128 LASSERT(offset == 1); /* should not come from intent */
2129 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
2130 lustre_msg_buf(req->rq_reqmsg, offset + 2, 0),
2131 req->rq_repmsg->buflens[2]);
2134 MD_COUNTER_INCREMENT(obd, unlink);
2136 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_UNLINK))
2137 GOTO(cleanup, rc = -ENOENT);
2139 if (rec->ur_namelen == 1) {
2140 /* this is request to drop i_nlink on local inode */
2142 rec->ur_name = idname;
2143 rc = mds_create_local_dentry(rec, obd);
2144 if (rc == -ENOENT || (rec->ur_mode & MDS_MODE_REPLAY)) {
2145 DEBUG_REQ(D_HA, req,
2146 "drop nlink on inode "DLID4" (replay)",
2147 OLID4(rec->ur_id1));
2153 if (rec->ur_mode & MDS_MODE_DONT_LOCK) {
2154 /* master mds for directory asks slave removing inode is already
2156 dparent = mds_id2locked_dentry(obd, rec->ur_id1, NULL,
2157 LCK_PW, parent_lockh,
2158 &update_mode, rec->ur_name,
2160 MDS_INODELOCK_UPDATE);
2161 if (IS_ERR(dparent))
2162 GOTO(cleanup, rc = PTR_ERR(dparent));
2163 dchild = ll_lookup_one_len(rec->ur_name, dparent,
2164 rec->ur_namelen - 1);
2166 GOTO(cleanup, rc = PTR_ERR(dchild));
2167 child_lockh.cookie = 0;
2168 LASSERT(!(dchild->d_flags & DCACHE_CROSS_REF));
2169 LASSERT(dchild->d_inode != NULL);
2170 LASSERT(S_ISDIR(dchild->d_inode->i_mode));
2172 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1,
2173 parent_lockh, &dparent,
2174 LCK_PW, MDS_INODELOCK_UPDATE,
2175 &update_mode, rec->ur_name,
2176 rec->ur_namelen, &child_lockh,
2178 MDS_INODELOCK_LOOKUP |
2179 MDS_INODELOCK_UPDATE);
2184 if (dchild->d_flags & DCACHE_CROSS_REF) {
2185 /* we should have parent lock only here */
2186 LASSERT(unlink_by_id == 0);
2187 LASSERT(dchild->d_mdsnum != mds->mds_num);
2188 mds_reint_unlink_remote(rec, offset, req, parent_lockh,
2189 update_mode, dparent, &child_lockh, dchild);
2193 cleanup_phase = 1; /* dchild, dparent, locks */
2196 child_inode = dchild->d_inode;
2197 if (child_inode == NULL) {
2198 CDEBUG(D_INODE, "child doesn't exist (dir %lu, name %s)\n",
2199 dparent ? dparent->d_inode->i_ino : 0, rec->ur_name);
2200 GOTO(cleanup, rc = -ENOENT);
2203 cleanup_phase = 2; /* dchild has a lock */
2205 /* We have to do these checks ourselves, in case we are making an
2206 * orphan. The client tells us whether rmdir() or unlink() was called,
2207 * so we need to return appropriate errors (bug 72).
2209 * We don't have to check permissions, because vfs_rename (called from
2210 * mds_open_unlink_rename) also calls may_delete. */
2211 if ((rec->ur_mode & S_IFMT) == S_IFDIR) {
2212 if (!S_ISDIR(child_inode->i_mode))
2213 GOTO(cleanup, rc = -ENOTDIR);
2215 if (S_ISDIR(child_inode->i_mode))
2216 GOTO(cleanup, rc = -EISDIR);
2219 /* handle splitted dir */
2220 rc = mds_lock_slave_objs(obd, dchild, &slave_lockh);
2224 /* Step 4: Get a lock on the ino to sync with creation WRT inode
2225 * reuse (see bug 2029). */
2226 rc = mds_lock_new_child(obd, child_inode, &child_reuse_lockh);
2229 cleanup_phase = 3; /* child inum lock */
2231 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_UNLINK_WRITE, dparent->d_inode->i_sb);
2233 /* ldlm_reply in buf[0] if called via intent */
2239 body = lustre_msg_buf(req->rq_repmsg, offset, sizeof (*body));
2240 LASSERT(body != NULL);
2242 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
2243 DOWN_READ_I_ALLOC_SEM(child_inode);
2244 cleanup_phase = 4; /* up(&child_inode->i_sem) when finished */
2246 /* If this is potentially the last reference to this inode, get the
2247 * OBD EA data first so the client can destroy OST objects. We
2248 * only do the object removal later if no open files/links remain. */
2249 if ((S_ISDIR(child_inode->i_mode) && child_inode->i_nlink == 2) ||
2250 child_inode->i_nlink == 1) {
2251 if (mds_orphan_open_count(child_inode) > 0) {
2252 /* need to lock pending_dir before transaction */
2253 down(&mds->mds_pending_dir->d_inode->i_sem);
2254 cleanup_phase = 5; /* up(&pending_dir->i_sem) */
2255 } else if (S_ISREG(child_inode->i_mode)) {
2256 mds_pack_inode2body(obd, body, child_inode, 0);
2257 mds_pack_md(obd, req->rq_repmsg, offset + 1,
2258 body, child_inode, MDS_PACK_MD_LOCK, 0);
2262 /* Step 4: Do the unlink: we already verified ur_mode above (bug 72) */
2263 switch (child_inode->i_mode & S_IFMT) {
2265 /* Drop any lingering child directories before we start our
2266 * transaction, to avoid doing multiple inode dirty/delete
2267 * in our compound transaction (bug 1321). */
2268 shrink_dcache_parent(dchild);
2269 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_RMDIR,
2272 GOTO(cleanup, rc = PTR_ERR(handle));
2273 rc = vfs_rmdir(dparent->d_inode, dchild);
2276 #warning "optimization is possible here: we could drop nlink w/o removing local dentry in FIDS/"
2277 struct lov_mds_md *lmm = lustre_msg_buf(req->rq_repmsg,
2279 handle = fsfilt_start_log(obd, dparent->d_inode,
2280 FSFILT_OP_UNLINK, NULL,
2281 le32_to_cpu(lmm->lmm_stripe_count));
2283 GOTO(cleanup, rc = PTR_ERR(handle));
2284 rc = vfs_unlink(dparent->d_inode, dchild);
2292 handle = fsfilt_start(obd, dparent->d_inode, FSFILT_OP_UNLINK,
2295 GOTO(cleanup, rc = PTR_ERR(handle));
2296 rc = vfs_unlink(dparent->d_inode, dchild);
2299 CERROR("bad file type %o unlinking %s\n", rec->ur_mode,
2302 GOTO(cleanup, rc = -EINVAL);
2305 if (rc == 0 && child_inode->i_nlink == 0) {
2306 if (mds_orphan_open_count(child_inode) > 0)
2307 rc = mds_orphan_add_link(rec, obd, dchild);
2310 GOTO(cleanup, rc = 0);
2312 if (!S_ISREG(child_inode->i_mode))
2315 if (!(body->valid & OBD_MD_FLEASIZE)) {
2316 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
2317 OBD_MD_FLATIME | OBD_MD_FLMTIME);
2318 } else if (mds_log_op_unlink(obd, child_inode,
2319 lustre_msg_buf(req->rq_repmsg, offset + 1, 0),
2320 req->rq_repmsg->buflens[offset + 1],
2321 lustre_msg_buf(req->rq_repmsg, offset + 2, 0),
2322 req->rq_repmsg->buflens[offset + 2],
2324 body->valid |= OBD_MD_FLCOOKIE;
2327 rc = mds_destroy_object(obd, child_inode, 1);
2329 CERROR("can't remove OST object, err %d\n",
2341 iattr.ia_valid = ATTR_MTIME | ATTR_CTIME;
2342 LTIME_S(iattr.ia_mtime) = rec->ur_time;
2343 LTIME_S(iattr.ia_ctime) = rec->ur_time;
2345 err = fsfilt_setattr(obd, dparent, handle, &iattr, 0);
2347 CERROR("error on parent setattr: rc = %d\n", err);
2349 rc = mds_finish_transno(mds, dparent ? dparent->d_inode : NULL,
2350 handle, req, rc, 0);
2352 (void)obd_set_info(mds->mds_dt_exp, strlen("unlinked"),
2353 "unlinked", 0, NULL);
2354 switch(cleanup_phase) {
2355 case 5: /* pending_dir semaphore */
2356 up(&mds->mds_pending_dir->d_inode->i_sem);
2357 case 4: /* child inode semaphore */
2358 UP_READ_I_ALLOC_SEM(child_inode);
2359 /* handle splitted dir */
2361 /* master directory can be non-empty or something else ... */
2362 mds_unlink_slave_objs(obd, dchild);
2365 ptlrpc_save_llog_lock(req, lcl);
2366 case 3: /* child ino-reuse lock */
2367 if (rc && body != NULL) {
2368 // Don't unlink the OST objects if the MDS unlink failed
2372 ldlm_lock_decref(&child_reuse_lockh, LCK_EX);
2374 ptlrpc_save_lock(req, &child_reuse_lockh, LCK_EX);
2375 case 2: /* child lock */
2376 mds_unlock_slave_objs(obd, dchild, slave_lockh);
2377 if (child_lockh.cookie)
2378 ldlm_lock_decref(&child_lockh, LCK_EX);
2379 case 1: /* child and parent dentry, parent lock */
2381 if (parent_lockh[1].cookie != 0)
2382 ldlm_lock_decref(parent_lockh + 1, update_mode);
2385 ldlm_lock_decref(parent_lockh, LCK_PW);
2387 ptlrpc_save_lock(req, parent_lockh, LCK_PW);
2394 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2397 req->rq_status = rc;
2402 * to service requests from remote MDS to increment i_nlink
2404 static int mds_reint_link_acquire(struct mds_update_record *rec,
2405 int offset, struct ptlrpc_request *req,
2406 struct lustre_handle *lh)
2408 struct obd_device *obd = req->rq_export->exp_obd;
2409 struct ldlm_res_id src_res_id = { .name = {0} };
2410 struct lustre_handle *handle = NULL, src_lockh = {0};
2411 struct mds_obd *mds = mds_req2mds(req);
2412 int rc = 0, cleanup_phase = 0;
2413 struct dentry *de_src = NULL;
2414 ldlm_policy_data_t policy;
2415 int flags = LDLM_FL_ATOMIC_CB;
2418 DEBUG_REQ(D_INODE, req, "%s: request to acquire i_nlinks "DLID4"\n",
2419 obd->obd_name, OLID4(rec->ur_id1));
2421 /* Step 1: Lookup the source inode and target directory by ID */
2422 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2424 GOTO(cleanup, rc = PTR_ERR(de_src));
2425 cleanup_phase = 1; /* source dentry */
2427 src_res_id.name[0] = id_fid(rec->ur_id1);
2428 src_res_id.name[1] = id_group(rec->ur_id1);
2429 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
2431 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2432 src_res_id, LDLM_IBITS, &policy,
2433 LCK_EX, &flags, mds_blocking_ast,
2434 ldlm_completion_ast, NULL, NULL,
2435 NULL, 0, NULL, &src_lockh);
2437 GOTO(cleanup, rc = -ENOLCK);
2438 cleanup_phase = 2; /* lock */
2440 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2442 handle = fsfilt_start(obd, de_src->d_inode, FSFILT_OP_LINK, NULL);
2443 if (IS_ERR(handle)) {
2444 rc = PTR_ERR(handle);
2447 i_nlink_inc(de_src->d_inode);
2448 mark_inode_dirty(de_src->d_inode);
2452 rc = mds_finish_transno(mds, de_src ? de_src->d_inode : NULL,
2453 handle, req, rc, 0);
2454 switch (cleanup_phase) {
2457 ldlm_lock_decref(&src_lockh, LCK_EX);
2459 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2465 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2468 req->rq_status = rc;
2473 * request to link to foreign inode:
2474 * - acquire i_nlinks on this inode
2477 static int mds_reint_link_to_remote(struct mds_update_record *rec,
2478 int offset, struct ptlrpc_request *req,
2479 struct lustre_handle *lh)
2481 struct lustre_handle *handle = NULL, tgt_dir_lockh[2] = {{0}, {0}};
2482 struct obd_device *obd = req->rq_export->exp_obd;
2483 struct dentry *de_tgt_dir = NULL;
2484 struct mds_obd *mds = mds_req2mds(req);
2485 int rc = 0, cleanup_phase = 0;
2486 struct mdc_op_data *op_data;
2487 struct ptlrpc_request *request = NULL;
2491 DEBUG_REQ(D_INODE, req, "%s: request to link "DLID4
2492 ":%*s to foreign inode "DLID4"\n", obd->obd_name,
2493 OLID4(rec->ur_id2), rec->ur_namelen - 1, rec->ur_name,
2494 OLID4(rec->ur_id1));
2496 de_tgt_dir = mds_id2locked_dentry(obd, rec->ur_id2, NULL, LCK_EX,
2497 tgt_dir_lockh, &update_mode,
2498 rec->ur_name, rec->ur_namelen - 1,
2499 MDS_INODELOCK_UPDATE);
2500 if (IS_ERR(de_tgt_dir))
2501 GOTO(cleanup, rc = PTR_ERR(de_tgt_dir));
2504 OBD_ALLOC(op_data, sizeof(*op_data));
2505 if (op_data == NULL)
2506 GOTO(cleanup, rc = -ENOMEM);
2508 memset(op_data, 0, sizeof(*op_data));
2509 op_data->id1 = *(rec->ur_id1);
2510 rc = md_link(mds->mds_md_exp, op_data, &request);
2511 OBD_FREE(op_data, sizeof(*op_data));
2514 ptlrpc_req_finished(request);
2520 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_tgt_dir->d_inode->i_sb);
2522 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2523 if (IS_ERR(handle)) {
2524 rc = PTR_ERR(handle);
2530 rc = fsfilt_add_dir_entry(obd, de_tgt_dir, rec->ur_name,
2531 rec->ur_namelen - 1, id_ino(rec->ur_id1),
2532 id_gen(rec->ur_id1), id_group(rec->ur_id1),
2533 id_fid(rec->ur_id1));
2536 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2537 handle, req, rc, 0);
2539 switch (cleanup_phase) {
2542 OBD_ALLOC(op_data, sizeof(*op_data));
2543 if (op_data != NULL) {
2545 memset(op_data, 0, sizeof(*op_data));
2547 op_data->id1 = *(rec->ur_id1);
2548 op_data->create_mode = rec->ur_mode;
2550 rc = md_unlink(mds->mds_md_exp, op_data, &request);
2551 OBD_FREE(op_data, sizeof(*op_data));
2553 ptlrpc_req_finished(request);
2555 CERROR("error %d while dropping i_nlink on "
2556 "remote inode\n", rc);
2559 CERROR("rc %d prevented dropping i_nlink on "
2560 "remote inode\n", -ENOMEM);
2566 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2568 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2571 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2573 ptlrpc_save_lock(req, tgt_dir_lockh + 1, update_mode);
2579 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2582 req->rq_status = rc;
2586 static int mds_reint_link(struct mds_update_record *rec, int offset,
2587 struct ptlrpc_request *req,
2588 struct lustre_handle *lh)
2590 struct obd_device *obd = req->rq_export->exp_obd;
2591 struct dentry *de_src = NULL;
2592 struct dentry *de_tgt_dir = NULL;
2593 struct dentry *dchild = NULL;
2594 struct mds_obd *mds = mds_req2mds(req);
2595 struct lustre_handle *handle = NULL;
2596 struct lustre_handle tgt_dir_lockh[2] = {{0}, {0}}, src_lockh = {0};
2597 struct ldlm_res_id src_res_id = { .name = {0} };
2598 struct ldlm_res_id tgt_dir_res_id = { .name = {0} };
2599 ldlm_policy_data_t src_policy ={.l_inodebits = {MDS_INODELOCK_UPDATE}};
2600 ldlm_policy_data_t tgt_dir_policy =
2601 {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2602 int rc = 0, cleanup_phase = 0;
2604 int update_mode = 0;
2608 LASSERT(offset == 1);
2610 DEBUG_REQ(D_INODE, req, "original "LPU64"/%u to "LPU64"/%u %s",
2611 id_ino(rec->ur_id1), id_gen(rec->ur_id1),
2612 id_ino(rec->ur_id2), id_gen(rec->ur_id2),
2615 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
2616 MD_COUNTER_INCREMENT(obd, link);
2618 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_LINK))
2619 GOTO(cleanup, rc = -ENOENT);
2621 if (id_group(rec->ur_id1) != mds->mds_num) {
2622 rc = mds_reint_link_to_remote(rec, offset, req, lh);
2626 if (rec->ur_namelen == 1) {
2627 rc = mds_reint_link_acquire(rec, offset, req, lh);
2631 /* Step 1: Lookup the source inode and target directory by ID */
2632 de_src = mds_id2dentry(obd, rec->ur_id1, NULL);
2634 GOTO(cleanup, rc = PTR_ERR(de_src));
2636 cleanup_phase = 1; /* source dentry */
2638 de_tgt_dir = mds_id2dentry(obd, rec->ur_id2, NULL);
2639 if (IS_ERR(de_tgt_dir)) {
2640 rc = PTR_ERR(de_tgt_dir);
2645 cleanup_phase = 2; /* target directory dentry */
2647 CDEBUG(D_INODE, "linking %*s/%s to inode %lu\n",
2648 de_tgt_dir->d_name.len, de_tgt_dir->d_name.name,
2649 rec->ur_name, de_src->d_inode->i_ino);
2651 /* Step 2: Take the two locks */
2652 src_res_id.name[0] = id_fid(rec->ur_id1);
2653 src_res_id.name[1] = id_group(rec->ur_id1);
2654 tgt_dir_res_id.name[0] = id_fid(rec->ur_id2);
2655 tgt_dir_res_id.name[1] = id_group(rec->ur_id2);
2658 if (IS_PDIROPS(de_tgt_dir->d_inode)) {
2659 int flags = LDLM_FL_ATOMIC_CB;
2660 update_mode = mds_lock_mode_for_dir(obd, de_tgt_dir, LCK_EX);
2662 rc = ldlm_cli_enqueue(NULL, NULL, obd->obd_namespace,
2663 tgt_dir_res_id, LDLM_IBITS,
2664 &src_policy, update_mode, &flags,
2666 ldlm_completion_ast, NULL, NULL,
2667 NULL, 0, NULL, tgt_dir_lockh + 1);
2669 GOTO(cleanup, rc = -ENOLCK);
2672 tgt_dir_res_id.name[2] = full_name_hash((unsigned char *)rec->ur_name,
2673 rec->ur_namelen - 1);
2674 CDEBUG(D_INFO, "take lock on %lu:%lu:"LPX64"\n",
2675 (unsigned long)id_fid(rec->ur_id2),
2676 (unsigned long)id_group(rec->ur_id2),
2677 tgt_dir_res_id.name[2]);
2680 rc = enqueue_ordered_locks(obd, &src_res_id, &src_lockh, LCK_EX,
2681 &src_policy, &tgt_dir_res_id, tgt_dir_lockh,
2682 LCK_EX, &tgt_dir_policy);
2686 cleanup_phase = 3; /* locks */
2688 /* Step 3: Lookup the child */
2689 dchild = ll_lookup_one_len(rec->ur_name, de_tgt_dir,
2690 rec->ur_namelen - 1);
2691 if (IS_ERR(dchild)) {
2692 rc = PTR_ERR(dchild);
2693 if (rc != -EPERM && rc != -EACCES)
2694 CERROR("child lookup error %d\n", rc);
2698 cleanup_phase = 4; /* child dentry */
2700 if (dchild->d_inode) {
2701 CDEBUG(D_INODE, "child exists (dir %lu, name %s)\n",
2702 de_tgt_dir->d_inode->i_ino, rec->ur_name);
2707 /* Step 4: Do it. */
2708 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_LINK_WRITE, de_src->d_inode->i_sb);
2710 handle = fsfilt_start(obd, de_tgt_dir->d_inode, FSFILT_OP_LINK, NULL);
2711 if (IS_ERR(handle)) {
2712 rc = PTR_ERR(handle);
2716 rc = vfs_link(de_src, de_tgt_dir->d_inode, dchild);
2717 if (rc && rc != -EPERM && rc != -EACCES)
2718 CERROR("vfs_link error %d\n", rc);
2720 rc = mds_finish_transno(mds, de_tgt_dir ? de_tgt_dir->d_inode : NULL,
2721 handle, req, rc, 0);
2724 switch (cleanup_phase) {
2725 case 4: /* child dentry */
2729 ldlm_lock_decref(&src_lockh, LCK_EX);
2730 ldlm_lock_decref(tgt_dir_lockh, LCK_EX);
2732 ptlrpc_save_lock(req, &src_lockh, LCK_EX);
2733 ptlrpc_save_lock(req, tgt_dir_lockh, LCK_EX);
2735 case 2: /* target dentry */
2737 if (tgt_dir_lockh[1].cookie && update_mode)
2738 ldlm_lock_decref(tgt_dir_lockh + 1, update_mode);
2742 case 1: /* source dentry */
2747 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
2750 req->rq_status = rc;
2754 /* The idea here is that we need to get four locks in the end:
2755 * one on each parent directory, one on each child. We need to take
2756 * these locks in some kind of order (to avoid deadlocks), and the order
2757 * I selected is "increasing resource number" order. We need to look up
2758 * the children, however, before we know what the resource number(s) are.
2759 * Thus the following plan:
2761 * 1,2. Look up the parents
2762 * 3,4. Look up the children
2763 * 5. Take locks on the parents and children, in order
2764 * 6. Verify that the children haven't changed since they were looked up
2766 * If there was a race and the children changed since they were first looked
2767 * up, it is possible that mds_verify_child() will be able to just grab the
2768 * lock on the new child resource (if it has a higher resource than any other)
2769 * but we need to compare against not only its parent, but also against the
2770 * parent and child of the "other half" of the rename, hence maxres_{src,tgt}.
2772 * We need the fancy igrab() on the child inodes because we aren't holding a
2773 * lock on the parent after the lookup is done, so dentry->d_inode may change
2774 * at any time, and igrab() itself doesn't like getting passed a NULL argument.
2776 static int mds_get_parents_children_locked(struct obd_device *obd,
2777 struct mds_obd *mds,
2778 struct lustre_id *p1_id,
2779 struct dentry **de_srcdirp,
2780 struct lustre_id *p2_id,
2781 struct dentry **de_tgtdirp,
2783 const char *old_name, int old_len,
2784 struct dentry **de_oldp,
2785 const char *new_name, int new_len,
2786 struct dentry **de_newp,
2787 struct lustre_handle *dlm_handles,
2790 struct ldlm_res_id p1_res_id = { .name = {0} };
2791 struct ldlm_res_id p2_res_id = { .name = {0} };
2792 struct ldlm_res_id c1_res_id = { .name = {0} };
2793 struct ldlm_res_id c2_res_id = { .name = {0} };
2794 ldlm_policy_data_t p_policy = {.l_inodebits = {MDS_INODELOCK_UPDATE}};
2795 /* Only dentry should disappear, but the inode itself would be
2796 intact otherwise. */
2797 ldlm_policy_data_t c1_policy = {.l_inodebits = {MDS_INODELOCK_LOOKUP}};
2798 /* If something is going to be replaced, both dentry and inode locks are
2800 ldlm_policy_data_t c2_policy = {.l_inodebits = {MDS_INODELOCK_FULL}};
2801 struct ldlm_res_id *maxres_src, *maxres_tgt;
2802 struct inode *inode;
2803 int rc = 0, cleanup_phase = 0;
2804 __u32 child_gen1 = 0;
2805 __u32 child_gen2 = 0;
2806 unsigned long child_ino1 = 0;
2807 unsigned long child_ino2 = 0;
2810 /* Step 1: Lookup the source directory */
2811 *de_srcdirp = mds_id2dentry(obd, p1_id, NULL);
2812 if (IS_ERR(*de_srcdirp))
2813 GOTO(cleanup, rc = PTR_ERR(*de_srcdirp));
2815 cleanup_phase = 1; /* source directory dentry */
2817 p1_res_id.name[0] = id_fid(p1_id);
2818 p1_res_id.name[1] = id_group(p1_id);
2820 /* Step 2: Lookup the target directory */
2821 if (id_equal_stc(p1_id, p2_id)) {
2822 *de_tgtdirp = dget(*de_srcdirp);
2824 *de_tgtdirp = mds_id2dentry(obd, p2_id, NULL);
2825 if (IS_ERR(*de_tgtdirp)) {
2826 rc = PTR_ERR(*de_tgtdirp);
2832 cleanup_phase = 2; /* target directory dentry */
2834 p2_res_id.name[0] = id_fid(p2_id);
2835 p2_res_id.name[1] = id_group(p2_id);
2838 dlm_handles[5].cookie = 0;
2839 dlm_handles[6].cookie = 0;
2841 if (IS_PDIROPS((*de_srcdirp)->d_inode)) {
2843 * get a temp lock on just fid, group to flush client cache and
2844 * to protect dirs from concurrent splitting.
2846 rc = enqueue_ordered_locks(obd, &p1_res_id, &dlm_handles[5],
2847 LCK_PW, &p_policy, &p2_res_id,
2848 &dlm_handles[6], LCK_PW, &p_policy);
2852 p1_res_id.name[2] = full_name_hash((unsigned char *)old_name,
2854 p2_res_id.name[2] = full_name_hash((unsigned char *)new_name,
2857 CDEBUG(D_INFO, "take locks on "
2858 LPX64":"LPX64":"LPX64", "LPX64":"LPX64":"LPX64"\n",
2859 p1_res_id.name[0], p1_res_id.name[1], p1_res_id.name[2],
2860 p2_res_id.name[0], p2_res_id.name[1], p2_res_id.name[2]);
2865 /* Step 3: Lookup the source child entry */
2866 *de_oldp = ll_lookup_one_len(old_name, *de_srcdirp,
2868 if (IS_ERR(*de_oldp)) {
2869 rc = PTR_ERR(*de_oldp);
2870 CERROR("old child lookup error (%.*s): %d\n",
2871 old_len - 1, old_name, rc);
2875 cleanup_phase = 4; /* original name dentry */
2877 inode = (*de_oldp)->d_inode;
2878 if (inode != NULL) {
2879 struct lustre_id sid;
2881 inode = igrab(inode);
2883 GOTO(cleanup, rc = -ENOENT);
2885 down(&inode->i_sem);
2886 rc = mds_read_inode_sid(obd, inode, &sid);
2889 CERROR("Can't read inode self id, inode %lu, "
2890 "rc %d\n", inode->i_ino, rc);
2895 child_ino1 = inode->i_ino;
2896 child_gen1 = inode->i_generation;
2897 c1_res_id.name[0] = id_fid(&sid);
2898 c1_res_id.name[1] = id_group(&sid);
2900 } else if ((*de_oldp)->d_flags & DCACHE_CROSS_REF) {
2901 child_ino1 = (*de_oldp)->d_inum;
2902 child_gen1 = (*de_oldp)->d_generation;
2903 c1_res_id.name[0] = (*de_oldp)->d_fid;
2904 c1_res_id.name[1] = (*de_oldp)->d_mdsnum;
2906 GOTO(cleanup, rc = -ENOENT);
2909 /* Step 4: Lookup the target child entry */
2910 *de_newp = ll_lookup_one_len(new_name, *de_tgtdirp,
2912 if (IS_ERR(*de_newp)) {
2913 rc = PTR_ERR(*de_newp);
2914 CERROR("new child lookup error (%.*s): %d\n",
2915 old_len - 1, old_name, rc);
2919 cleanup_phase = 5; /* target dentry */
2921 inode = (*de_newp)->d_inode;
2922 if (inode != NULL) {
2923 struct lustre_id sid;
2925 inode = igrab(inode);
2929 down(&inode->i_sem);
2930 rc = mds_read_inode_sid(obd, inode, &sid);
2933 CERROR("Can't read inode self id, inode %lu, "
2934 "rc %d\n", inode->i_ino, rc);
2938 child_ino2 = inode->i_ino;
2939 child_gen2 = inode->i_generation;
2940 c2_res_id.name[0] = id_fid(&sid);
2941 c2_res_id.name[1] = id_group(&sid);
2943 } else if ((*de_newp)->d_flags & DCACHE_CROSS_REF) {
2944 child_ino2 = (*de_newp)->d_inum;
2945 child_gen2 = (*de_newp)->d_generation;
2946 c2_res_id.name[0] = (*de_newp)->d_fid;
2947 c2_res_id.name[1] = (*de_newp)->d_mdsnum;
2951 /* Step 5: Take locks on the parents and child(ren) */
2952 maxres_src = &p1_res_id;
2953 maxres_tgt = &p2_res_id;
2954 cleanup_phase = 5; /* target dentry */
2956 if (c1_res_id.name[0] != 0 && res_gt(&c1_res_id, &p1_res_id, NULL, NULL))
2957 maxres_src = &c1_res_id;
2958 if (c2_res_id.name[0] != 0 && res_gt(&c2_res_id, &p2_res_id, NULL, NULL))
2959 maxres_tgt = &c2_res_id;
2961 rc = enqueue_4ordered_locks(obd, &p1_res_id, &dlm_handles[0], parent_mode,
2963 &p2_res_id, &dlm_handles[1], parent_mode,
2965 &c1_res_id, &dlm_handles[2], child_mode,
2967 &c2_res_id, &dlm_handles[3], child_mode,
2972 cleanup_phase = 6; /* parent and child(ren) locks */
2974 /* Step 6a: Re-lookup source child to verify it hasn't changed */
2975 rc = mds_verify_child(obd, &p1_res_id, &dlm_handles[0], *de_srcdirp,
2976 parent_mode, &c1_res_id, &dlm_handles[2],
2977 de_oldp, child_mode, &c1_policy, old_name, old_len,
2978 maxres_tgt, child_ino1, child_gen1);
2980 if (c2_res_id.name[0] != 0)
2981 ldlm_lock_decref(&dlm_handles[3], child_mode);
2982 ldlm_lock_decref(&dlm_handles[1], parent_mode);
2989 if (!DENTRY_VALID(*de_oldp))
2990 GOTO(cleanup, rc = -ENOENT);
2992 /* Step 6b: Re-lookup target child to verify it hasn't changed */
2993 rc = mds_verify_child(obd, &p2_res_id, &dlm_handles[1], *de_tgtdirp,
2994 parent_mode, &c2_res_id, &dlm_handles[3],
2995 de_newp, child_mode, &c2_policy, new_name,
2996 new_len, maxres_src, child_ino2, child_gen2);
2998 ldlm_lock_decref(&dlm_handles[2], child_mode);
2999 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3009 switch (cleanup_phase) {
3010 case 6: /* child lock(s) */
3011 if (c2_res_id.name[0] != 0)
3012 ldlm_lock_decref(&dlm_handles[3], child_mode);
3013 if (c1_res_id.name[0] != 0)
3014 ldlm_lock_decref(&dlm_handles[2], child_mode);
3015 if (dlm_handles[1].cookie != 0)
3016 ldlm_lock_decref(&dlm_handles[1], parent_mode);
3017 if (dlm_handles[0].cookie != 0)
3018 ldlm_lock_decref(&dlm_handles[0], parent_mode);
3019 case 5: /* target dentry */
3021 case 4: /* source dentry */
3025 if (dlm_handles[5].cookie != 0)
3026 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3027 if (dlm_handles[6].cookie != 0)
3028 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3030 case 2: /* target directory dentry */
3031 l_dput(*de_tgtdirp);
3032 case 1: /* source directry dentry */
3033 l_dput(*de_srcdirp);
3041 * checks if dentry can be removed. This function also handles cross-ref
3044 static int mds_check_for_rename(struct obd_device *obd,
3045 struct dentry *dentry)
3047 struct mds_obd *mds = &obd->u.mds;
3048 struct lustre_handle *rlockh;
3049 struct ptlrpc_request *req;
3050 struct mdc_op_data *op_data;
3051 struct lookup_intent it;
3052 int handle_size, rc = 0;
3055 LASSERT(dentry != NULL);
3057 if (dentry->d_inode) {
3058 if (S_ISDIR(dentry->d_inode->i_mode) &&
3059 !mds_is_dir_empty(obd, dentry))
3062 LASSERT((dentry->d_flags & DCACHE_CROSS_REF));
3063 handle_size = sizeof(struct lustre_handle);
3065 OBD_ALLOC(rlockh, handle_size);
3069 memset(rlockh, 0, handle_size);
3070 OBD_ALLOC(op_data, sizeof(*op_data));
3071 if (op_data == NULL) {
3072 OBD_FREE(rlockh, handle_size);
3075 memset(op_data, 0, sizeof(*op_data));
3076 mds_pack_dentry2id(obd, &op_data->id1, dentry, 1);
3078 it.it_op = IT_UNLINK;
3079 OBD_ALLOC(it.d.fs_data, sizeof(struct lustre_intent_data));
3082 rc = md_enqueue(mds->mds_md_exp, LDLM_IBITS, &it, LCK_EX,
3083 op_data, rlockh, NULL, 0, ldlm_completion_ast,
3084 mds_blocking_ast, NULL);
3085 OBD_FREE(op_data, sizeof(*op_data));
3089 OBD_FREE(it.d.fs_data,
3090 sizeof(struct lustre_intent_data));
3093 if (rlockh->cookie != 0)
3094 ldlm_lock_decref(rlockh, LCK_EX);
3096 if (LUSTRE_IT(&it)->it_data) {
3097 req = (struct ptlrpc_request *)LUSTRE_IT(&it)->it_data;
3098 ptlrpc_req_finished(req);
3101 if (LUSTRE_IT(&it)->it_status)
3102 rc = LUSTRE_IT(&it)->it_status;
3103 OBD_FREE(it.d.fs_data, sizeof(struct lustre_intent_data));
3104 OBD_FREE(rlockh, handle_size);
3109 static int mds_add_local_dentry(struct mds_update_record *rec, int offset,
3110 struct ptlrpc_request *req, struct lustre_id *id,
3111 struct dentry *de_dir, struct dentry *de)
3113 struct obd_device *obd = req->rq_export->exp_obd;
3114 struct mds_obd *mds = mds_req2mds(req);
3115 void *handle = NULL;
3121 * name exists and points to local inode try to unlink this name
3122 * and create new one.
3124 CDEBUG(D_OTHER, "%s: %s points to local inode %lu/%lu\n",
3125 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_inode->i_ino,
3126 (unsigned long)de->d_inode->i_generation);
3128 /* checking if we can remove local dentry. */
3129 rc = mds_check_for_rename(obd, de);
3133 handle = fsfilt_start(obd, de_dir->d_inode,
3134 FSFILT_OP_RENAME, NULL);
3136 GOTO(cleanup, rc = PTR_ERR(handle));
3137 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3140 } else if (de->d_flags & DCACHE_CROSS_REF) {
3141 CDEBUG(D_OTHER, "%s: %s points to remote inode %lu/%lu\n",
3142 obd->obd_name, rec->ur_tgt, (unsigned long)de->d_mdsnum,
3143 (unsigned long)de->d_fid);
3145 /* checking if we can remove local dentry. */
3146 rc = mds_check_for_rename(obd, de);
3151 * to be fully POSIX compatible, we should add one more check:
3153 * if de_new is subdir of dir rec->ur_id1. If so - return
3156 * I do not know how to implement it right now, because
3157 * inodes/dentries for new and old names lie on different MDS,
3158 * so add this notice here just to make it visible for the rest
3159 * of developers and do not forget about. And when this check
3160 * will be added, del_cross_ref should gone, that is local
3161 * dentry is able to be removed if all checks passed. --umka
3164 handle = fsfilt_start(obd, de_dir->d_inode,
3165 FSFILT_OP_RENAME, NULL);
3167 GOTO(cleanup, rc = PTR_ERR(handle));
3168 rc = fsfilt_del_dir_entry(req->rq_export->exp_obd, de);
3172 /* name doesn't exist. the simplest case. */
3173 handle = fsfilt_start(obd, de_dir->d_inode,
3174 FSFILT_OP_LINK, NULL);
3176 GOTO(cleanup, rc = PTR_ERR(handle));
3179 rc = fsfilt_add_dir_entry(obd, de_dir, rec->ur_tgt,
3180 rec->ur_tgtlen - 1, id_ino(id),
3181 id_gen(id), id_group(id), id_fid(id));
3183 CERROR("add_dir_entry() returned error %d\n", rc);
3189 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3190 handle, req, rc, 0);
3195 static int mds_del_local_dentry(struct mds_update_record *rec, int offset,
3196 struct ptlrpc_request *req, struct dentry *de_dir,
3199 struct obd_device *obd = req->rq_export->exp_obd;
3200 struct mds_obd *mds = mds_req2mds(req);
3201 void *handle = NULL;
3205 handle = fsfilt_start(obd, de_dir->d_inode, FSFILT_OP_UNLINK, NULL);
3207 GOTO(cleanup, rc = PTR_ERR(handle));
3208 rc = fsfilt_del_dir_entry(obd, de);
3213 rc = mds_finish_transno(mds, de_dir ? de_dir->d_inode : NULL,
3214 handle, req, rc, 0);
3218 static int mds_reint_rename_create_name(struct mds_update_record *rec,
3219 int offset, struct ptlrpc_request *req)
3221 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3222 struct obd_device *obd = req->rq_export->exp_obd;
3223 struct mds_obd *mds = mds_req2mds(req);
3224 struct lustre_handle child_lockh = {0};
3225 struct dentry *de_tgtdir = NULL;
3226 struct dentry *de_new = NULL;
3227 int cleanup_phase = 0;
3228 int update_mode, rc = 0;
3229 struct lustre_id ids[2]; /* sid, pid */
3233 * another MDS executing rename operation has asked us to create target
3234 * name. such a creation should destroy existing target name.
3236 CDEBUG(D_OTHER, "%s: request to create name %s for "DLID4"\n",
3237 obd->obd_name, rec->ur_tgt, OLID4(rec->ur_id1));
3239 /* get parent id: ldlm lock on the parent protects ea */
3240 rc = mds_read_inode_sid(obd, de_tgtdir->d_inode, &ids[1]);
3244 /* first, lookup the target */
3245 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id2, parent_lockh,
3246 &de_tgtdir, LCK_PW, MDS_INODELOCK_UPDATE,
3247 &update_mode, rec->ur_tgt, rec->ur_tgtlen,
3248 &child_lockh, &de_new, LCK_EX,
3249 MDS_INODELOCK_LOOKUP);
3256 LASSERT(de_tgtdir->d_inode);
3259 rc = mds_add_local_dentry(rec, offset, req, rec->ur_id1,
3264 ids[0] = *(rec->ur_id1);
3265 rc = obd_set_info(mds->mds_md_obd->u.lmv.tgts[id_group(rec->ur_id1)].ltd_exp,
3266 strlen("ids"), "ids",
3267 sizeof(struct lustre_id) * 2, ids);
3272 if (cleanup_phase == 1) {
3274 if (parent_lockh[1].cookie != 0)
3275 ldlm_lock_decref(parent_lockh + 1, update_mode);
3277 ldlm_lock_decref(parent_lockh, LCK_PW);
3278 if (child_lockh.cookie != 0)
3279 ldlm_lock_decref(&child_lockh, LCK_EX);
3284 req->rq_status = rc;
3288 static int mds_reint_rename_to_remote(struct mds_update_record *rec, int offset,
3289 struct ptlrpc_request *req)
3291 struct obd_device *obd = req->rq_export->exp_obd;
3292 struct ptlrpc_request *req2 = NULL;
3293 struct dentry *de_srcdir = NULL;
3294 struct dentry *de_old = NULL;
3295 struct mds_obd *mds = mds_req2mds(req);
3296 struct lustre_handle parent_lockh[2] = {{0}, {0}};
3297 struct lustre_handle child_lockh = {0};
3298 struct mdc_op_data *op_data;
3299 int update_mode, rc = 0;
3302 CDEBUG(D_OTHER, "%s: move name %s onto another mds #%lu\n",
3303 obd->obd_name, rec->ur_name, (unsigned long)id_group(rec->ur_id2));
3305 OBD_ALLOC(op_data, sizeof(*op_data));
3306 if (op_data == NULL)
3308 memset(op_data, 0, sizeof(*op_data));
3310 rc = mds_get_parent_child_locked(obd, mds, rec->ur_id1, parent_lockh,
3311 &de_srcdir, LCK_PW, MDS_INODELOCK_UPDATE,
3312 &update_mode, rec->ur_name,
3313 rec->ur_namelen, &child_lockh, &de_old,
3314 LCK_EX, MDS_INODELOCK_LOOKUP);
3316 OBD_FREE(op_data, sizeof(*op_data));
3321 LASSERT(de_srcdir->d_inode);
3325 * we already know the target should be created on another MDS so, we
3326 * have to request that MDS to do it.
3329 /* prepare source id */
3330 if (de_old->d_flags & DCACHE_CROSS_REF) {
3331 LASSERT(de_old->d_inode == NULL);
3332 CDEBUG(D_OTHER, "request to move remote name\n");
3333 mds_pack_dentry2id(obd, &op_data->id1, de_old, 1);
3334 } else if (de_old->d_inode == NULL) {
3335 /* oh, source doesn't exist */
3336 OBD_FREE(op_data, sizeof(*op_data));
3337 GOTO(cleanup, rc = -ENOENT);
3339 struct lustre_id sid;
3340 struct inode *inode = de_old->d_inode;
3342 LASSERT(inode != NULL);
3343 CDEBUG(D_OTHER, "request to move local name\n");
3344 id_ino(&op_data->id1) = inode->i_ino;
3345 id_group(&op_data->id1) = mds->mds_num;
3346 id_gen(&op_data->id1) = inode->i_generation;
3348 down(&inode->i_sem);
3349 rc = mds_read_inode_sid(obd, inode, &sid);
3352 CERROR("Can't read inode self id, "
3353 "inode %lu, rc = %d\n",
3358 id_fid(&op_data->id1) = id_fid(&sid);
3361 op_data->id2 = *rec->ur_id2;
3362 rc = md_rename(mds->mds_md_exp, op_data, NULL, 0,
3363 rec->ur_tgt, rec->ur_tgtlen - 1, &req2);
3364 OBD_FREE(op_data, sizeof(*op_data));
3369 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3375 ptlrpc_req_finished(req2);
3378 if (parent_lockh[1].cookie != 0)
3379 ldlm_lock_decref(parent_lockh + 1, update_mode);
3381 ldlm_lock_decref(parent_lockh, LCK_PW);
3382 if (child_lockh.cookie != 0)
3383 ldlm_lock_decref(&child_lockh, LCK_EX);
3388 req->rq_status = rc;
3392 static int mds_reint_rename(struct mds_update_record *rec, int offset,
3393 struct ptlrpc_request *req, struct lustre_handle *lockh)
3395 struct obd_device *obd = req->rq_export->exp_obd;
3396 struct dentry *de_srcdir = NULL;
3397 struct dentry *de_tgtdir = NULL;
3398 struct dentry *de_old = NULL;
3399 struct dentry *de_new = NULL;
3400 struct inode *old_inode = NULL, *new_inode = NULL;
3401 struct mds_obd *mds = mds_req2mds(req);
3402 struct lustre_handle dlm_handles[7] = {{0},{0},{0},{0},{0},{0},{0}};
3403 struct mds_body *body = NULL;
3404 struct llog_create_locks *lcl = NULL;
3405 struct lov_mds_md *lmm = NULL;
3406 int rc = 0, cleanup_phase = 0;
3407 struct lustre_id ids[2]; /* sid, pid */
3408 void *handle = NULL;
3411 LASSERT(offset == 1);
3413 DEBUG_REQ(D_INODE, req, "parent "DLID4" %s to "DLID4" %s",
3414 OLID4(rec->ur_id1), rec->ur_name, OLID4(rec->ur_id2),
3417 MDS_CHECK_RESENT(req, mds_reconstruct_generic(req));
3419 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
3420 DEBUG_REQ(D_HA, req, "rename replay");
3421 memcpy(lustre_msg_buf(req->rq_repmsg, 2, 0),
3422 lustre_msg_buf(req->rq_reqmsg, offset + 3, 0),
3423 req->rq_repmsg->buflens[2]);
3426 MD_COUNTER_INCREMENT(obd, rename);
3428 if (rec->ur_namelen == 1) {
3429 rc = mds_reint_rename_create_name(rec, offset, req);
3433 /* check if new name should be located on remote target. */
3434 if (id_group(rec->ur_id2) != mds->mds_num) {
3435 rc = mds_reint_rename_to_remote(rec, offset, req);
3439 rc = mds_get_parents_children_locked(obd, mds, rec->ur_id1, &de_srcdir,
3440 rec->ur_id2, &de_tgtdir, LCK_PW,
3441 rec->ur_name, rec->ur_namelen,
3442 &de_old, rec->ur_tgt,
3443 rec->ur_tgtlen, &de_new,
3444 dlm_handles, LCK_EX);
3448 cleanup_phase = 1; /* parent(s), children, locks */
3449 old_inode = de_old->d_inode;
3450 new_inode = de_new->d_inode;
3452 /* sanity check for src inode */
3453 if (de_old->d_flags & DCACHE_CROSS_REF) {
3454 LASSERT(de_old->d_inode == NULL);
3457 * in the case of cross-ref dir, we can perform this check only
3458 * if child and parent lie on the same mds. This is because
3459 * otherwise they can have the same inode numbers.
3461 if (de_old->d_mdsnum == mds->mds_num) {
3462 if (de_old->d_inum == de_srcdir->d_inode->i_ino ||
3463 de_old->d_inum == de_tgtdir->d_inode->i_ino)
3464 GOTO(cleanup, rc = -EINVAL);
3467 LASSERT(de_old->d_inode != NULL);
3468 if (de_old->d_inode->i_ino == de_srcdir->d_inode->i_ino ||
3469 de_old->d_inode->i_ino == de_tgtdir->d_inode->i_ino)
3470 GOTO(cleanup, rc = -EINVAL);
3473 /* sanity check for dest inode */
3474 if (de_new->d_flags & DCACHE_CROSS_REF) {
3475 LASSERT(new_inode == NULL);
3477 /* the same check about target dentry. */
3478 if (de_new->d_mdsnum == mds->mds_num) {
3479 if (de_new->d_inum == de_srcdir->d_inode->i_ino ||
3480 de_new->d_inum == de_tgtdir->d_inode->i_ino)
3481 GOTO(cleanup, rc = -EINVAL);
3485 * regular files usualy do not have ->rename() implemented. But
3486 * we handle only this case when @de_new is cross-ref entry,
3487 * because in other cases it will be handled by vfs_rename().
3489 if (de_old->d_inode && (!de_old->d_inode->i_op ||
3490 !de_old->d_inode->i_op->rename))
3491 GOTO(cleanup, rc = -EPERM);
3494 (new_inode->i_ino == de_srcdir->d_inode->i_ino ||
3495 new_inode->i_ino == de_tgtdir->d_inode->i_ino))
3496 GOTO(cleanup, rc = -EINVAL);
3501 * check if inodes point to each other. This should be checked before
3502 * is_subdir() check, as for the same entries it will think that they
3505 if (!(de_old->d_flags & DCACHE_CROSS_REF) &&
3506 !(de_new->d_flags & DCACHE_CROSS_REF) &&
3507 old_inode == new_inode)
3508 GOTO(cleanup, rc = 0);
3510 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
3512 * check if we are moving old entry into its child. 2.6 does not check
3513 * for this in vfs_rename() anymore.
3515 if (is_subdir(de_new, de_old))
3516 GOTO(cleanup, rc = -EINVAL);
3520 * if we are about to remove the target at first, pass the EA of that
3521 * inode to client to perform and cleanup on OST.
3523 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
3524 LASSERT(body != NULL);
3526 /* get new parent id: ldlm lock on the parent protects ea */
3527 rc = mds_read_inode_sid(obd, de_tgtdir->d_inode, &ids[1]);
3531 /* child i_alloc_sem protects orphan_dec_test && is_orphan race */
3533 DOWN_READ_I_ALLOC_SEM(new_inode);
3535 cleanup_phase = 2; /* up(&new_inode->i_sem) when finished */
3537 if (new_inode && ((S_ISDIR(new_inode->i_mode) &&
3538 new_inode->i_nlink == 2) ||
3539 new_inode->i_nlink == 1)) {
3540 if (mds_orphan_open_count(new_inode) > 0) {
3541 /* need to lock pending_dir before transaction */
3542 down(&mds->mds_pending_dir->d_inode->i_sem);
3543 cleanup_phase = 3; /* up(&pending_dir->i_sem) */
3544 } else if (S_ISREG(new_inode->i_mode)) {
3545 mds_pack_inode2body(obd, body, new_inode, 0);
3546 mds_pack_md(obd, req->rq_repmsg, 1, body,
3547 new_inode, MDS_PACK_MD_LOCK, 0);
3551 OBD_FAIL_WRITE(OBD_FAIL_MDS_REINT_RENAME_WRITE,
3552 de_srcdir->d_inode->i_sb);
3554 if (de_old->d_flags & DCACHE_CROSS_REF) {
3555 struct lustre_id old_id;
3556 struct obd_export *tgt_exp =
3557 mds->mds_md_obd->u.lmv.tgts[de_old->d_mdsnum].ltd_exp;
3560 mds_pack_dentry2id(obd, &old_id, de_old, 1);
3562 rc = mds_add_local_dentry(rec, offset, req, &old_id,
3567 rc = mds_del_local_dentry(rec, offset, req, de_srcdir,
3573 rc = obd_set_info(tgt_exp, strlen("ids"), "ids",
3574 sizeof(struct lustre_id) * 2, ids);
3579 lmm = lustre_msg_buf(req->rq_repmsg, 1, 0);
3580 handle = fsfilt_start_log(obd, de_tgtdir->d_inode, FSFILT_OP_RENAME,
3581 NULL, le32_to_cpu(lmm->lmm_stripe_count));
3584 GOTO(cleanup, rc = PTR_ERR(handle));
3587 de_old->d_fsdata = req;
3588 de_new->d_fsdata = req;
3589 rc = vfs_rename(de_srcdir->d_inode, de_old, de_tgtdir->d_inode, de_new);
3592 if (rc == 0 && new_inode != NULL && new_inode->i_nlink == 0) {
3593 if (mds_orphan_open_count(new_inode) > 0)
3594 rc = mds_orphan_add_link(rec, obd, de_new);
3597 GOTO(cleanup, rc = 0);
3599 if (!S_ISREG(new_inode->i_mode))
3602 if (!(body->valid & OBD_MD_FLEASIZE)) {
3603 body->valid |= (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
3604 OBD_MD_FLATIME | OBD_MD_FLMTIME);
3605 } else if (mds_log_op_unlink(obd, new_inode,
3606 lustre_msg_buf(req->rq_repmsg,1,0),
3607 req->rq_repmsg->buflens[1],
3608 lustre_msg_buf(req->rq_repmsg,2,0),
3609 req->rq_repmsg->buflens[2],
3611 body->valid |= OBD_MD_FLCOOKIE;
3614 rc = mds_destroy_object(obd, old_inode, 1);
3616 CERROR("can't remove OST object, err %d\n",
3622 rc = mds_update_inode_ids(obd, de_old->d_inode,
3623 handle, NULL, &ids[1]);
3627 rc = mds_finish_transno(mds, (de_tgtdir ? de_tgtdir->d_inode : NULL),
3628 handle, req, rc, 0);
3630 switch (cleanup_phase) {
3632 up(&mds->mds_pending_dir->d_inode->i_sem);
3635 UP_READ_I_ALLOC_SEM(new_inode);
3638 if (dlm_handles[5].cookie != 0)
3639 ldlm_lock_decref(&(dlm_handles[5]), LCK_PW);
3640 if (dlm_handles[6].cookie != 0)
3641 ldlm_lock_decref(&(dlm_handles[6]), LCK_PW);
3644 ptlrpc_save_llog_lock(req, lcl);
3647 if (dlm_handles[3].cookie != 0)
3648 ldlm_lock_decref(&(dlm_handles[3]), LCK_EX);
3649 ldlm_lock_decref(&(dlm_handles[2]), LCK_EX);
3650 ldlm_lock_decref(&(dlm_handles[1]), LCK_PW);
3651 ldlm_lock_decref(&(dlm_handles[0]), LCK_PW);
3653 if (dlm_handles[3].cookie != 0)
3654 ptlrpc_save_lock(req,&(dlm_handles[3]), LCK_EX);
3655 ptlrpc_save_lock(req, &(dlm_handles[2]), LCK_EX);
3656 ptlrpc_save_lock(req, &(dlm_handles[1]), LCK_PW);
3657 ptlrpc_save_lock(req, &(dlm_handles[0]), LCK_PW);
3666 CERROR("invalid cleanup_phase %d\n", cleanup_phase);
3669 req->rq_status = rc;
3673 typedef int (*mds_reinter)(struct mds_update_record *, int offset,
3674 struct ptlrpc_request *, struct lustre_handle *);
3676 static mds_reinter reinters[REINT_MAX + 1] = {
3677 [REINT_SETATTR] mds_reint_setattr,
3678 [REINT_CREATE] mds_reint_create,
3679 [REINT_LINK] mds_reint_link,
3680 [REINT_UNLINK] mds_reint_unlink,
3681 [REINT_RENAME] mds_reint_rename,
3682 [REINT_OPEN] mds_open
3685 int mds_reint_rec(struct mds_update_record *rec, int offset,
3686 struct ptlrpc_request *req, struct lustre_handle *lockh)
3688 struct obd_device *obd = req->rq_export->exp_obd;
3689 struct lvfs_run_ctxt saved;
3692 /* checked by unpacker */
3693 LASSERT(rec->ur_opcode <= REINT_MAX &&
3694 reinters[rec->ur_opcode] != NULL);
3696 push_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);
3697 rc = reinters[rec->ur_opcode] (rec, offset, req, lockh);
3698 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, &rec->ur_uc);